macros.el 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. ;;; macros.el --- non-primitive commands for keyboard macros
  2. ;; Copyright (C) 1985-1987, 1992, 1994-1995, 2001-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Maintainer: FSF
  5. ;; Keywords: abbrev
  6. ;; Package: emacs
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Extension commands for keyboard macros. These permit you to assign
  20. ;; a name to the last-defined keyboard macro, expand and insert the
  21. ;; lisp corresponding to a macro, query the user from within a macro,
  22. ;; or apply a macro to each line in the reason.
  23. ;;; Code:
  24. ;;;###autoload
  25. (defun name-last-kbd-macro (symbol)
  26. "Assign a name to the last keyboard macro defined.
  27. Argument SYMBOL is the name to define.
  28. The symbol's function definition becomes the keyboard macro string.
  29. Such a \"function\" cannot be called from Lisp, but it is a valid editor command."
  30. (interactive "SName for last kbd macro: ")
  31. (or last-kbd-macro
  32. (error "No keyboard macro defined"))
  33. (and (fboundp symbol)
  34. (not (stringp (symbol-function symbol)))
  35. (not (vectorp (symbol-function symbol)))
  36. (error "Function %s is already defined and not a keyboard macro"
  37. symbol))
  38. (if (string-equal symbol "")
  39. (error "No command name given"))
  40. (fset symbol last-kbd-macro))
  41. ;;;###autoload
  42. (defun insert-kbd-macro (macroname &optional keys)
  43. "Insert in buffer the definition of kbd macro NAME, as Lisp code.
  44. Optional second arg KEYS means also record the keys it is on
  45. \(this is the prefix argument, when calling interactively).
  46. This Lisp code will, when executed, define the kbd macro with the same
  47. definition it has now. If you say to record the keys, the Lisp code
  48. will also rebind those keys to the macro. Only global key bindings
  49. are recorded since executing this Lisp code always makes global
  50. bindings.
  51. To save a kbd macro, visit a file of Lisp code such as your `~/.emacs',
  52. use this command, and then save the file."
  53. (interactive (list (intern (completing-read "Insert kbd macro (name): "
  54. obarray
  55. (lambda (elt)
  56. (and (fboundp elt)
  57. (or (stringp (symbol-function elt))
  58. (vectorp (symbol-function elt))
  59. (get elt 'kmacro))))
  60. t))
  61. current-prefix-arg))
  62. (let (definition)
  63. (if (string= (symbol-name macroname) "")
  64. (progn
  65. (setq macroname 'last-kbd-macro definition last-kbd-macro)
  66. (insert "(setq "))
  67. (setq definition (symbol-function macroname))
  68. (insert "(fset '"))
  69. (prin1 macroname (current-buffer))
  70. (insert "\n ")
  71. (if (stringp definition)
  72. (let ((beg (point)) end)
  73. (prin1 definition (current-buffer))
  74. (setq end (point-marker))
  75. (goto-char beg)
  76. (while (< (point) end)
  77. (let ((char (following-char)))
  78. (cond ((= char 0)
  79. (delete-region (point) (1+ (point)))
  80. (insert "\\C-@"))
  81. ((< char 27)
  82. (delete-region (point) (1+ (point)))
  83. (insert "\\C-" (+ 96 char)))
  84. ((= char ?\C-\\)
  85. (delete-region (point) (1+ (point)))
  86. (insert "\\C-\\\\"))
  87. ((< char 32)
  88. (delete-region (point) (1+ (point)))
  89. (insert "\\C-" (+ 64 char)))
  90. ((< char 127)
  91. (forward-char 1))
  92. ((= char 127)
  93. (delete-region (point) (1+ (point)))
  94. (insert "\\C-?"))
  95. ((= char 128)
  96. (delete-region (point) (1+ (point)))
  97. (insert "\\M-\\C-@"))
  98. ((= char (aref "\M-\C-\\" 0))
  99. (delete-region (point) (1+ (point)))
  100. (insert "\\M-\\C-\\\\"))
  101. ((< char 155)
  102. (delete-region (point) (1+ (point)))
  103. (insert "\\M-\\C-" (- char 32)))
  104. ((< char 160)
  105. (delete-region (point) (1+ (point)))
  106. (insert "\\M-\\C-" (- char 64)))
  107. ((= char (aref "\M-\\" 0))
  108. (delete-region (point) (1+ (point)))
  109. (insert "\\M-\\\\"))
  110. ((< char 255)
  111. (delete-region (point) (1+ (point)))
  112. (insert "\\M-" (- char 128)))
  113. ((= char 255)
  114. (delete-region (point) (1+ (point)))
  115. (insert "\\M-\\C-?"))))))
  116. (if (vectorp definition)
  117. (let ((len (length definition)) (i 0) char)
  118. (while (< i len)
  119. (insert (if (zerop i) ?\[ ?\s))
  120. (setq char (aref definition i)
  121. i (1+ i))
  122. (if (not (numberp char))
  123. (prin1 char (current-buffer))
  124. (princ (prin1-char char) (current-buffer))))
  125. (insert ?\]))
  126. (prin1 definition (current-buffer))))
  127. (insert ")\n")
  128. (if keys
  129. (let ((keys (where-is-internal (symbol-function macroname)
  130. '(keymap))))
  131. (while keys
  132. (insert "(global-set-key ")
  133. (prin1 (car keys) (current-buffer))
  134. (insert " '")
  135. (prin1 macroname (current-buffer))
  136. (insert ")\n")
  137. (setq keys (cdr keys)))))))
  138. ;;;###autoload
  139. (defun kbd-macro-query (flag)
  140. "Query user during kbd macro execution.
  141. With prefix argument, enters recursive edit, reading keyboard
  142. commands even within a kbd macro. You can give different commands
  143. each time the macro executes.
  144. Without prefix argument, asks whether to continue running the macro.
  145. Your options are: \\<query-replace-map>
  146. \\[act] Finish this iteration normally and continue with the next.
  147. \\[skip] Skip the rest of this iteration, and start the next.
  148. \\[exit] Stop the macro entirely right now.
  149. \\[recenter] Redisplay the screen, then ask again.
  150. \\[edit] Enter recursive edit; ask again when you exit from that."
  151. (interactive "P")
  152. (or executing-kbd-macro
  153. defining-kbd-macro
  154. (error "Not defining or executing kbd macro"))
  155. (if flag
  156. (let (executing-kbd-macro defining-kbd-macro)
  157. (recursive-edit))
  158. (if (not executing-kbd-macro)
  159. nil
  160. (let ((loop t)
  161. (msg (substitute-command-keys
  162. "Proceed with macro?\\<query-replace-map>\
  163. (\\[act], \\[skip], \\[exit], \\[recenter], \\[edit]) ")))
  164. (while loop
  165. (let ((key (let ((executing-kbd-macro nil)
  166. (defining-kbd-macro nil))
  167. (message "%s" msg)
  168. (read-event)))
  169. def)
  170. (setq key (vector key))
  171. (setq def (lookup-key query-replace-map key))
  172. (cond ((eq def 'act)
  173. (setq loop nil))
  174. ((eq def 'skip)
  175. (setq loop nil)
  176. (setq executing-kbd-macro ""))
  177. ((eq def 'exit)
  178. (setq loop nil)
  179. (setq executing-kbd-macro t))
  180. ((eq def 'recenter)
  181. (recenter nil))
  182. ((eq def 'edit)
  183. (let (executing-kbd-macro defining-kbd-macro)
  184. (recursive-edit)))
  185. ((eq def 'quit)
  186. (setq quit-flag t))
  187. (t
  188. (or (eq def 'help)
  189. (ding))
  190. (with-output-to-temp-buffer "*Help*"
  191. (princ
  192. (substitute-command-keys
  193. "Specify how to proceed with keyboard macro execution.
  194. Possibilities: \\<query-replace-map>
  195. \\[act] Finish this iteration normally and continue with the next.
  196. \\[skip] Skip the rest of this iteration, and start the next.
  197. \\[exit] Stop the macro entirely right now.
  198. \\[recenter] Redisplay the screen, then ask again.
  199. \\[edit] Enter recursive edit; ask again when you exit from that."))
  200. (with-current-buffer standard-output
  201. (help-mode)))))))))))
  202. ;;;###autoload
  203. (defun apply-macro-to-region-lines (top bottom &optional macro)
  204. "Apply last keyboard macro to all lines in the region.
  205. For each line that begins in the region, move to the beginning of
  206. the line, and run the last keyboard macro.
  207. When called from lisp, this function takes two arguments TOP and
  208. BOTTOM, describing the current region. TOP must be before BOTTOM.
  209. The optional third argument MACRO specifies a keyboard macro to
  210. execute.
  211. This is useful for quoting or unquoting included text, adding and
  212. removing comments, or producing tables where the entries are regular.
  213. For example, in Usenet articles, sections of text quoted from another
  214. author are indented, or have each line start with `>'. To quote a
  215. section of text, define a keyboard macro which inserts `>', put point
  216. and mark at opposite ends of the quoted section, and use
  217. `\\[apply-macro-to-region-lines]' to mark the entire section.
  218. Suppose you wanted to build a keyword table in C where each entry
  219. looked like this:
  220. { \"foo\", foo_data, foo_function },
  221. { \"bar\", bar_data, bar_function },
  222. { \"baz\", baz_data, baz_function },
  223. You could enter the names in this format:
  224. foo
  225. bar
  226. baz
  227. and write a macro to massage a word into a table entry:
  228. \\C-x (
  229. \\M-d { \"\\C-y\", \\C-y_data, \\C-y_function },
  230. \\C-x )
  231. and then select the region of un-tablified names and use
  232. `\\[apply-macro-to-region-lines]' to build the table from the names."
  233. (interactive "r")
  234. (or macro
  235. (progn
  236. (if (null last-kbd-macro)
  237. (error "No keyboard macro has been defined"))
  238. (setq macro last-kbd-macro)))
  239. (save-excursion
  240. (let ((end-marker (copy-marker bottom))
  241. next-line-marker)
  242. (goto-char top)
  243. (if (not (bolp))
  244. (forward-line 1))
  245. (setq next-line-marker (point-marker))
  246. (while (< next-line-marker end-marker)
  247. (goto-char next-line-marker)
  248. (save-excursion
  249. (forward-line 1)
  250. (set-marker next-line-marker (point)))
  251. (save-excursion
  252. (let ((mark-active nil))
  253. (execute-kbd-macro macro))))
  254. (set-marker end-marker nil)
  255. (set-marker next-line-marker nil))))
  256. ;;;###autoload (define-key ctl-x-map "q" 'kbd-macro-query)
  257. (provide 'macros)
  258. ;;; macros.el ends here