PageRenderTime 27ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/lisp/macros.el

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