misc-defuns.el 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. ;; Misc defuns go here
  2. ;; It wouldn't hurt to look for patterns and extract once in a while
  3. (defmacro create-simple-keybinding-command (name key)
  4. `(defmacro ,name (&rest fns)
  5. (list 'global-set-key (kbd ,key) `(lambda ()
  6. (interactive)
  7. ,@fns))))
  8. (create-simple-keybinding-command f2 "<f2>")
  9. (create-simple-keybinding-command f5 "<f5>")
  10. (create-simple-keybinding-command f6 "<f6>")
  11. (create-simple-keybinding-command f7 "<f7>")
  12. (create-simple-keybinding-command f8 "<f8>")
  13. (create-simple-keybinding-command f9 "<f9>")
  14. (create-simple-keybinding-command f10 "<f10>")
  15. (create-simple-keybinding-command f11 "<f11>")
  16. (create-simple-keybinding-command f12 "<f12>")
  17. (defun goto-line-with-feedback ()
  18. "Show line numbers temporarily, while prompting for the line number input"
  19. (interactive)
  20. (unwind-protect
  21. (progn
  22. (linum-mode 1)
  23. (call-interactively 'goto-line))
  24. (linum-mode -1)))
  25. (defun open-line-and-indent ()
  26. (interactive)
  27. (newline-and-indent)
  28. (end-of-line 0)
  29. (indent-for-tab-command))
  30. ;; start a httpd-server in current directory
  31. (defun httpd-start-here (directory port)
  32. (interactive (list (read-directory-name "Root directory: " default-directory nil t)
  33. (read-number "Port: " 8017)))
  34. (setq httpd-root directory)
  35. (setq httpd-port port)
  36. (httpd-start)
  37. (browse-url (concat "http://localhost:" (number-to-string port) "/")))
  38. ;; shorthand for interactive lambdas
  39. (defmacro λ (&rest body)
  40. `(lambda ()
  41. (interactive)
  42. ,@body))
  43. (global-set-key (kbd "s-l") (λ (insert "\u03bb")))
  44. ;; command to help set up magit-gh-pulls
  45. (defun magit-gh-pulls-setup (repoid)
  46. (interactive "suser/repo: ")
  47. (shell-command "git config --add magit.extension gh-pulls")
  48. (shell-command (concat "git config magit.gh-pulls-repo " repoid)))
  49. ;; Increase/decrease selective display
  50. (defun inc-selective-display (arg)
  51. (interactive "P")
  52. (if (numberp arg)
  53. (set-selective-display arg)
  54. (if (numberp selective-display)
  55. (set-selective-display (+ 2 selective-display))
  56. (set-selective-display 2)))
  57. (create-temp-selective-display-keymap))
  58. (defun dec-selective-display ()
  59. (interactive)
  60. (when (and (numberp selective-display)
  61. (> selective-display 2))
  62. (set-selective-display (- selective-display 2)))
  63. (create-temp-selective-display-keymap))
  64. (defun clear-selective-display ()
  65. (interactive)
  66. (when (numberp selective-display)
  67. (set-selective-display nil)))
  68. (defun create-temp-selective-display-keymap ()
  69. (set-temporary-overlay-map
  70. (let ((map (make-sparse-keymap)))
  71. (define-key map (kbd "+") 'inc-selective-display)
  72. (define-key map (kbd "-") 'dec-selective-display)
  73. (define-key map (kbd "0") 'clear-selective-display)
  74. map))
  75. (message "Type + to reveal more, - for less, 0 to reset."))
  76. ;; Add spaces and proper formatting to linum-mode. It uses more room than
  77. ;; necessary, but that's not a problem since it's only in use when going to
  78. ;; lines.
  79. (setq linum-format (lambda (line)
  80. (propertize
  81. (format (concat " %"
  82. (number-to-string
  83. (length (number-to-string
  84. (line-number-at-pos (point-max)))))
  85. "d ")
  86. line)
  87. 'face 'linum)))
  88. (defun isearch-yank-selection ()
  89. "Put selection from buffer into search string."
  90. (interactive)
  91. (when (region-active-p)
  92. (deactivate-mark))
  93. (isearch-yank-internal (lambda () (mark))))
  94. (defun region-as-string ()
  95. (buffer-substring (region-beginning)
  96. (region-end)))
  97. (defun isearch-forward-use-region ()
  98. (interactive)
  99. (when (region-active-p)
  100. (add-to-history 'search-ring (region-as-string))
  101. (deactivate-mark))
  102. (call-interactively 'isearch-forward))
  103. (defun isearch-backward-use-region ()
  104. (interactive)
  105. (when (region-active-p)
  106. (add-to-history 'search-ring (region-as-string))
  107. (deactivate-mark))
  108. (call-interactively 'isearch-backward))
  109. (eval-after-load "multiple-cursors"
  110. '(progn
  111. (unsupported-cmd isearch-forward-use-region ".")
  112. (unsupported-cmd isearch-backward-use-region ".")))
  113. (defun view-url ()
  114. "Open a new buffer containing the contents of URL."
  115. (interactive)
  116. (let* ((default (thing-at-point-url-at-point))
  117. (url (read-from-minibuffer "URL: " default)))
  118. (switch-to-buffer (url-retrieve-synchronously url))
  119. (rename-buffer url t)
  120. ;; TODO: switch to nxml/nxhtml mode
  121. (cond ((search-forward "<?xml" nil t) (xml-mode))
  122. ((search-forward "<html" nil t) (html-mode)))))
  123. (defun linkify-region-from-kill-ring (start end)
  124. (interactive "r")
  125. (let ((text (buffer-substring start end)))
  126. (delete-region start end)
  127. (insert "<a href=\"")
  128. (yank)
  129. (insert (concat "\">" text "</a>"))))
  130. (defun buffer-to-html (buffer)
  131. (with-current-buffer (htmlize-buffer buffer)
  132. (buffer-string)))
  133. (defun sudo-edit (&optional arg)
  134. (interactive "p")
  135. (if (or arg (not buffer-file-name))
  136. (find-file (concat "/sudo:root@localhost:" (ido-read-file-name "File: ")))
  137. (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
  138. (defun add-file-find-hook-with-pattern (pattern fn &optional contents)
  139. "Add a find-file-hook that calls FN for files where PATTERN
  140. matches the file name, and optionally, where CONTENT matches file contents.
  141. Both PATTERN and CONTENTS are matched as regular expressions."
  142. (lexical-let ((re-pattern pattern)
  143. (fun fn)
  144. (re-content contents))
  145. (add-hook 'find-file-hook
  146. (lambda ()
  147. (if (and
  148. (string-match re-pattern (buffer-file-name))
  149. (or (null re-content)
  150. (string-match re-content
  151. (buffer-substring (point-min) (point-max)))))
  152. (apply fun ()))))))
  153. ;; Fix kmacro-edit-lossage, it's normal implementation
  154. ;; is bound tightly to C-h
  155. (defun kmacro-edit-lossage ()
  156. "Edit most recent 300 keystrokes as a keyboard macro."
  157. (interactive)
  158. (kmacro-push-ring)
  159. (edit-kbd-macro 'view-lossage))