misc-defuns.el 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. ;;; Misc defuns go here
  2. ;;; Code:
  3. (require 'cl-macs)
  4. ;;;###autoload
  5. (defun distopico:inhibit-message-advisor(orig-fun &rest args)
  6. "This silences `minibufer' output of `message' and `save' to the echo-area.
  7. Only have effect when is not called interactively or `init-file-debug' it nil
  8. and not prevent `ORIG-FUN' to log in *Messages* buffer.
  9. this call `ORIG-FUN' without change the `ARGS'."
  10. (if (or init-file-debug
  11. (called-interactively-p 'any))
  12. (apply orig-fun args)
  13. (let ((inhibit-message t)
  14. (save-silently t))
  15. (apply orig-fun args))))
  16. ;;;###autoload
  17. (defun distopico:inhibit-outputs-advisor(orig-fun &rest args)
  18. "Run `ORIG-FUN' with their `ARGS' without generating any output.
  19. This silences output to anything that writes to `message', `load',
  20. `write-region' and prevent `ORIG-FUN' to log in *Messages* buffer
  21. or `minibufer', only have effect when is not called interactively
  22. or `init-file-debug' it nil."
  23. (if (or init-file-debug
  24. (called-interactively-p 'any))
  25. (apply orig-fun args)
  26. (cl-letf ((orig-write-region (symbol-function 'write-region))
  27. (orig-load (symbol-function 'load)))
  28. (cl-letf (((symbol-function 'standard-output) (lambda (&rest _)))
  29. ((symbol-function 'message) (lambda (&rest _)))
  30. ((symbol-function 'load)
  31. (lambda (file &optional noerror nomessage nosuffix must-suffix)
  32. (funcall orig-load
  33. file noerror t nosuffix must-suffix)))
  34. ((symbol-function 'write-region)
  35. (lambda (start end filename
  36. &optional append visit lockname mustbenew)
  37. (unless visit (setq visit 'no-message))
  38. (funcall orig-write-region
  39. start end filename append visit lockname mustbenew))))
  40. (apply orig-fun args)))))
  41. (defun distopico:pg-uline (ulinechar)
  42. "Underline the current or the previous line with ULINECHAR."
  43. (interactive "cUnderline with:")
  44. (if (looking-at "^$")
  45. (next-line -1))
  46. (end-of-line)
  47. (let ((linelen (current-column)))
  48. (insert "\n")
  49. (while (> linelen 0)
  50. (setq linelen (1- linelen))
  51. (insert ulinechar)))
  52. (insert "\n"))
  53. (defun distopico:next-link ()
  54. "Move point to next link."
  55. (interactive)
  56. (condition-case nil
  57. (or (let ((next-link (shr-next-link)))
  58. (unless (equal next-link "No next link")
  59. next-link))
  60. ;; Works across all modes
  61. (org-next-link))
  62. (error nil)))
  63. (defun distopico:previous-link ()
  64. "Move point to previous link."
  65. (interactive)
  66. (condition-case nil
  67. (or (let ((previous-link (shr-previous-link)))
  68. (unless (equal previous-link "No previous link")
  69. previous-link))
  70. ;; Works across all modes
  71. (org-previous-link))
  72. (error nil)))
  73. (defun distopico:show-link-url ()
  74. "Return url of link at point."
  75. (interactive)
  76. (message (or (shr-url-at-point nil)
  77. (thing-at-point-url-at-point t))))
  78. (defun distopico:ignore-error-wrapper (func)
  79. "Return new function that ignore errors from `FUNC'.
  80. The function wraps a function with `ignore-errors' macro."
  81. (let ((fn func))
  82. (lambda ()
  83. (interactive)
  84. (ignore-errors
  85. (funcall fn)))))
  86. (defun toggle-margin-right ()
  87. "Toggle the right margin between `fill-column' or window width.
  88. This command is convenient when reading novel, documentation."
  89. (interactive)
  90. (if (eq (cdr (window-margins)) nil)
  91. (set-window-margins nil 0 (- (window-body-width) fill-column))
  92. (set-window-margins nil 0 0) ) )
  93. (defadvice indent-rigidly (after deactivate-mark-nil activate)
  94. "Adjust Margin"
  95. (if (called-interactively-p 'any)
  96. (setq deactivate-mark nil)))
  97. (defun goto-line-with-feedback ()
  98. "Show line numbers temporarily, while prompting for the line number input"
  99. (interactive)
  100. (unwind-protect
  101. (progn
  102. (linum-mode 1)
  103. (call-interactively 'goto-line))
  104. (linum-mode -1)))
  105. (defun open-line-and-indent ()
  106. (interactive)
  107. (newline-and-indent)
  108. (end-of-line 0)
  109. (indent-for-tab-command))
  110. ;; start a httpd-server in current directory
  111. (defun httpd-start-here (directory port)
  112. (interactive (list (read-directory-name "Root directory: " default-directory nil t)
  113. (read-number "Port: " 8017)))
  114. (setq httpd-root directory)
  115. (setq httpd-port port)
  116. (httpd-start)
  117. (browse-url (concat "http://localhost:" (number-to-string port) "/")))
  118. ;; Increase/decrease selective display
  119. (defun inc-selective-display (arg)
  120. (interactive "P")
  121. (if (numberp arg)
  122. (set-selective-display arg)
  123. (if (numberp selective-display)
  124. (set-selective-display (+ 2 selective-display))
  125. (set-selective-display 2)))
  126. (create-temp-selective-display-keymap))
  127. (defun dec-selective-display ()
  128. (interactive)
  129. (when (and (numberp selective-display)
  130. (> selective-display 2))
  131. (set-selective-display (- selective-display 2)))
  132. (create-temp-selective-display-keymap))
  133. (defun clear-selective-display ()
  134. (interactive)
  135. (when (numberp selective-display)
  136. (set-selective-display nil)))
  137. (defun create-temp-selective-display-keymap ()
  138. (set-temporary-overlay-map
  139. (let ((map (make-sparse-keymap)))
  140. (define-key map (kbd "+") 'inc-selective-display)
  141. (define-key map (kbd "-") 'dec-selective-display)
  142. (define-key map (kbd "0") 'clear-selective-display)
  143. map))
  144. (message "Type + to reveal more, - for less, 0 to reset."))
  145. ;; Add spaces and proper formatting to linum-mode. It uses more room than
  146. ;; necessary, but that's not a problem since it's only in use when going to
  147. ;; lines.
  148. (setq linum-format (lambda (line)
  149. (propertize
  150. (format (concat " %"
  151. (number-to-string
  152. (length (number-to-string
  153. (line-number-at-pos (point-max)))))
  154. "d ")
  155. line)
  156. 'face 'linum)))
  157. (defun isearch-yank-selection ()
  158. "Put selection from buffer into search string."
  159. (interactive)
  160. (when (region-active-p)
  161. (deactivate-mark))
  162. (isearch-yank-internal (lambda () (mark))))
  163. (defun region-as-string ()
  164. (buffer-substring (region-beginning)
  165. (region-end)))
  166. (defun isearch-forward-use-region ()
  167. (interactive)
  168. (when (region-active-p)
  169. (add-to-history 'search-ring (region-as-string))
  170. (deactivate-mark))
  171. (call-interactively 'isearch-forward))
  172. (defun isearch-backward-use-region ()
  173. (interactive)
  174. (when (region-active-p)
  175. (add-to-history 'search-ring (region-as-string))
  176. (deactivate-mark))
  177. (call-interactively 'isearch-backward))
  178. (defun view-url ()
  179. "Open a new buffer containing the contents of URL."
  180. (interactive)
  181. (let* ((default (thing-at-point-url-at-point))
  182. (url (read-from-minibuffer "URL: " default)))
  183. (switch-to-buffer (url-retrieve-synchronously url))
  184. (rename-buffer url t)
  185. ;; TODO: switch to nxml/nxhtml mode
  186. (cond ((search-forward "<?xml" nil t) (xml-mode))
  187. ((search-forward "<html" nil t) (html-mode)))))
  188. (defun linkify-region-from-kill-ring (start end)
  189. (interactive "r")
  190. (let ((text (buffer-substring start end)))
  191. (delete-region start end)
  192. (insert "<a href=\"")
  193. (yank)
  194. (insert (concat "\">" text "</a>"))))
  195. (defun buffer-to-html (buffer)
  196. (with-current-buffer (htmlize-buffer buffer)
  197. (buffer-string)))
  198. (defun sudo-edit (&optional arg)
  199. (interactive "p")
  200. (if (or arg (not buffer-file-name))
  201. (find-file (concat "/sudo:root@localhost:" (ido-read-file-name "File: ")))
  202. (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
  203. (defun add-file-find-hook-with-pattern (pattern fn &optional contents)
  204. "Add a find-file-hook that calls FN for files where PATTERN
  205. matches the file name, and optionally, where CONTENT matches file contents.
  206. Both PATTERN and CONTENTS are matched as regular expressions."
  207. (lexical-let ((re-pattern pattern)
  208. (fun fn)
  209. (re-content contents))
  210. (add-hook 'find-file-hook
  211. (lambda ()
  212. (if (and
  213. (string-match re-pattern (buffer-file-name))
  214. (or (null re-content)
  215. (string-match re-content
  216. (buffer-substring (point-min) (point-max)))))
  217. (apply fun ()))))))
  218. ;; Fix kmacro-edit-lossage, it's normal implementation
  219. ;; is bound tightly to C-h
  220. (defun kmacro-edit-lossage ()
  221. "Edit most recent 300 keystrokes as a keyboard macro."
  222. (interactive)
  223. (kmacro-push-ring)
  224. (edit-kbd-macro 'view-lossage))
  225. (defun string-starts-with (s arg)
  226. "returns non-nil if string S starts with ARG. Else nil."
  227. (cond ((>= (length s) (length arg))
  228. (string-equal (substring s 0 (length arg)) arg))
  229. (t nil)))
  230. (provide 'misc-defuns)