kmacro.el 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. ;;; kmacro.el --- enhanced keyboard macros -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2002-2017 Free Software Foundation, Inc.
  3. ;; Author: Kim F. Storm <storm@cua.dk>
  4. ;; Keywords: keyboard convenience
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; The kmacro package provides the user interface to emacs' basic
  18. ;; keyboard macro functionality. With kmacro, two function keys are
  19. ;; dedicated to keyboard macros, by default F3 and F4.
  20. ;; Note: The traditional bindings C-x (, C-x ), and C-x e are still
  21. ;; supported, but for some users these bindings are too hard to type
  22. ;; to be really useful for doing small repeated tasks.
  23. ;; To start defining a keyboard macro, use F3. To end the macro,
  24. ;; use F4, and to call the macro also use F4. This makes it very
  25. ;; easy to repeat a macro immediately after defining it.
  26. ;;
  27. ;; You can call the macro repeatedly by pressing F4 multiple times, or
  28. ;; you can give a numeric prefix argument specifying the number of
  29. ;; times to repeat the macro. Macro execution automatically
  30. ;; terminates when point reaches the end of the buffer or if an error
  31. ;; is signaled by ringing the bell.
  32. ;; When you define a macro with F3/F4, it is automatically added to
  33. ;; the head of the "keyboard macro ring", and F4 actually executes the
  34. ;; first element of the macro ring.
  35. ;;
  36. ;; Note: an empty macro is never added to the macro ring.
  37. ;;
  38. ;; You can execute the second element on the macro ring with C-u F4 or
  39. ;; C-x C-k C-l, you can use C-x C-k C-p and C-x C-k C-n to cycle
  40. ;; through the macro ring, and you can swap the first and second
  41. ;; elements with C-x C-k C-t. To delete the first element in the
  42. ;; macro ring, use C-x C-k C-d.
  43. ;;
  44. ;; You can also use C-x C-k C-s to start a macro, and C-x C-k C-k to
  45. ;; end it; then use C-k to execute it immediately, or C-x C-k C-k to
  46. ;; execute it later.
  47. ;;
  48. ;; In general, immediately after using C-x C-k followed by one of C-k,
  49. ;; C-l, C-p, or C-n, you can further cycle the macro ring using C-p or
  50. ;; C-n, execute the first or second macro using C-k or C-l, delete
  51. ;; the head macro with C-d, or edit the current macro with C-e without
  52. ;; repeating the C-x C-k prefix.
  53. ;; If you enter F3 while defining the macro, the numeric value of
  54. ;; `kmacro-counter' is inserted using the `kmacro-counter-format', and
  55. ;; `kmacro-counter' is incremented by 1 (or the numeric prefix value
  56. ;; of F3).
  57. ;;
  58. ;; The initial value of `kmacro-counter' is 0, or the numeric prefix
  59. ;; value given to F3 when starting the macro.
  60. ;;
  61. ;; Now, each time you call the macro using F4, the current
  62. ;; value of `kmacro-counter' is inserted and incremented, making it
  63. ;; easy to insert incremental numbers in the buffer.
  64. ;;
  65. ;; Example:
  66. ;;
  67. ;; The following sequence: M-5 F3 x M-2 F3 y F4 F4 F4 F4
  68. ;; inserts the following string: x5yx7yx9yx11y
  69. ;; A macro can also be called using a mouse click, default S-mouse-3.
  70. ;; This calls the macro at the point where you click the mouse.
  71. ;; You can edit the last macro using C-x C-k C-e.
  72. ;; You can append to the last macro using C-u F3.
  73. ;; You can set the macro counter using C-x C-k C-c, add to it using C-x C-k C-a,
  74. ;; and you can set the macro counter format with C-x C-k C-f.
  75. ;; The following key bindings are performed:
  76. ;;
  77. ;; Normal While defining macro
  78. ;; --------------------------- ------------------------------
  79. ;; f3 Define macro Insert current counter value
  80. ;; Prefix arg specifies initial and increase counter by prefix
  81. ;; counter value (default 0) (default increment: 1)
  82. ;;
  83. ;; C-u f3 APPENDs to last macro
  84. ;;
  85. ;; f4 Call last macro End macro
  86. ;; Prefix arg specifies number
  87. ;; of times to execute macro.
  88. ;;
  89. ;; C-u f4 Swap last and head of macro ring.
  90. ;;
  91. ;; S-mouse-3 Set point at click and End macro and execute macro at
  92. ;; execute last macro. click.
  93. ;;; Code:
  94. ;; Customization:
  95. (defgroup kmacro nil
  96. "Simplified keyboard macro user interface."
  97. :group 'keyboard
  98. :group 'convenience
  99. :version "22.1"
  100. :link '(emacs-commentary-link :tag "Commentary" "kmacro.el")
  101. :link '(emacs-library-link :tag "Lisp File" "kmacro.el"))
  102. (defcustom kmacro-call-mouse-event 'S-mouse-3
  103. "The mouse event used by kmacro to call a macro.
  104. Set to nil if no mouse binding is desired."
  105. :type 'symbol
  106. :group 'kmacro)
  107. (defcustom kmacro-ring-max 8
  108. "Maximum number of keyboard macros to save in macro ring."
  109. :type 'integer
  110. :group 'kmacro)
  111. (defcustom kmacro-execute-before-append t
  112. "Controls whether appending to a macro starts by executing the macro.
  113. If non-nil, using a single \\[universal-argument] prefix executes the macro
  114. before appending, while more than one \\[universal-argument] prefix does not
  115. execute the macro.
  116. Otherwise, a single \\[universal-argument] prefix does not execute the
  117. macro, while more than one \\[universal-argument] prefix causes the
  118. macro to be executed before appending to it."
  119. :type 'boolean
  120. :group 'kmacro)
  121. (defcustom kmacro-repeat-no-prefix t
  122. "Allow repeating certain macro commands without entering the C-x C-k prefix."
  123. :type 'boolean
  124. :group 'kmacro)
  125. (defcustom kmacro-call-repeat-key t
  126. "Allow repeating macro call using last key or a specific key."
  127. :type '(choice (const :tag "Disabled" nil)
  128. (const :tag "Last key" t)
  129. (character :tag "Character" :value ?e)
  130. (symbol :tag "Key symbol" :value RET))
  131. :group 'kmacro)
  132. (defcustom kmacro-call-repeat-with-arg nil
  133. "Repeat macro call with original arg when non-nil; repeat once if nil."
  134. :type 'boolean
  135. :group 'kmacro)
  136. (defcustom kmacro-step-edit-mini-window-height 0.75
  137. "Override `max-mini-window-height' when step edit keyboard macro."
  138. :type 'number
  139. :group 'kmacro)
  140. ;; Keymap
  141. (defvar kmacro-keymap
  142. (let ((map (make-sparse-keymap)))
  143. ;; Start, end, execute macros
  144. (define-key map "s" 'kmacro-start-macro)
  145. (define-key map "\C-s" 'kmacro-start-macro)
  146. (define-key map "\C-k" 'kmacro-end-or-call-macro-repeat)
  147. (define-key map "r" 'apply-macro-to-region-lines)
  148. (define-key map "q" 'kbd-macro-query) ;; Like C-x q
  149. ;; macro ring
  150. (define-key map "\C-n" 'kmacro-cycle-ring-next)
  151. (define-key map "\C-p" 'kmacro-cycle-ring-previous)
  152. (define-key map "\C-v" 'kmacro-view-macro-repeat)
  153. (define-key map "\C-d" 'kmacro-delete-ring-head)
  154. (define-key map "\C-t" 'kmacro-swap-ring)
  155. (define-key map "\C-l" 'kmacro-call-ring-2nd-repeat)
  156. ;; macro counter
  157. (define-key map "\C-f" 'kmacro-set-format)
  158. (define-key map "\C-c" 'kmacro-set-counter)
  159. (define-key map "\C-i" 'kmacro-insert-counter)
  160. (define-key map "\C-a" 'kmacro-add-counter)
  161. ;; macro editing
  162. (define-key map "\C-e" 'kmacro-edit-macro-repeat)
  163. (define-key map "\r" 'kmacro-edit-macro)
  164. (define-key map "e" 'edit-kbd-macro)
  165. (define-key map "l" 'kmacro-edit-lossage)
  166. (define-key map " " 'kmacro-step-edit-macro)
  167. ;; naming and binding
  168. (define-key map "b" 'kmacro-bind-to-key)
  169. (define-key map "n" 'kmacro-name-last-macro)
  170. (define-key map "x" 'kmacro-to-register)
  171. map)
  172. "Keymap for keyboard macro commands.")
  173. (defalias 'kmacro-keymap kmacro-keymap)
  174. ;;; Provide some binding for startup:
  175. ;;;###autoload (global-set-key "\C-x(" 'kmacro-start-macro)
  176. ;;;###autoload (global-set-key "\C-x)" 'kmacro-end-macro)
  177. ;;;###autoload (global-set-key "\C-xe" 'kmacro-end-and-call-macro)
  178. ;;;###autoload (global-set-key [f3] 'kmacro-start-macro-or-insert-counter)
  179. ;;;###autoload (global-set-key [f4] 'kmacro-end-or-call-macro)
  180. ;;;###autoload (global-set-key "\C-x\C-k" 'kmacro-keymap)
  181. ;;;###autoload (autoload 'kmacro-keymap "kmacro" "Keymap for keyboard macro commands." t 'keymap)
  182. (if kmacro-call-mouse-event
  183. (global-set-key (vector kmacro-call-mouse-event) 'kmacro-end-call-mouse))
  184. ;;; Called from keyboard-quit
  185. (defun kmacro-keyboard-quit ()
  186. (or (not defining-kbd-macro)
  187. (eq defining-kbd-macro 'append)
  188. (kmacro-ring-empty-p)
  189. (kmacro-pop-ring)))
  190. ;;; Keyboard macro counter
  191. (defvar kmacro-counter 0
  192. "Current keyboard macro counter.")
  193. (defvar kmacro-default-counter-format "%d")
  194. (defvar kmacro-counter-format "%d"
  195. "Current keyboard macro counter format.")
  196. (defvar kmacro-counter-format-start kmacro-counter-format
  197. "Macro format at start of macro execution.")
  198. (defvar kmacro-counter-value-start kmacro-counter
  199. "Macro counter at start of macro execution.")
  200. (defvar kmacro-last-counter 0
  201. "Last counter inserted by key macro.")
  202. (defvar kmacro-initial-counter-value nil
  203. "Initial counter value for the next keyboard macro to be defined.")
  204. (defun kmacro-insert-counter (arg)
  205. "Insert macro counter, then increment it by ARG.
  206. Interactively, ARG defaults to 1. With \\[universal-argument], insert
  207. previous `kmacro-counter', and do not modify counter."
  208. (interactive "P")
  209. (if kmacro-initial-counter-value
  210. (setq kmacro-counter kmacro-initial-counter-value
  211. kmacro-initial-counter-value nil))
  212. (if (and arg (listp arg))
  213. (insert (format kmacro-counter-format kmacro-last-counter))
  214. (insert (format kmacro-counter-format kmacro-counter))
  215. (kmacro-add-counter (prefix-numeric-value arg))))
  216. (defun kmacro-set-format (format)
  217. "Set macro counter FORMAT."
  218. (interactive "sMacro Counter Format: ")
  219. (setq kmacro-counter-format
  220. (if (equal format "") "%d" format))
  221. ;; redefine initial macro counter if we are not executing a macro.
  222. (if (not (or defining-kbd-macro executing-kbd-macro))
  223. (setq kmacro-default-counter-format kmacro-counter-format)))
  224. (defun kmacro-display-counter (&optional value)
  225. "Display current counter value."
  226. (unless value (setq value kmacro-counter))
  227. (message "New macro counter value: %s (%d)" (format kmacro-counter-format value) value))
  228. (defun kmacro-set-counter (arg)
  229. "Set `kmacro-counter' to ARG or prompt if missing.
  230. With \\[universal-argument] prefix, reset counter to its value prior to this iteration of the macro."
  231. (interactive "NMacro counter value: ")
  232. (if (not (or defining-kbd-macro executing-kbd-macro))
  233. (kmacro-display-counter (setq kmacro-initial-counter-value arg))
  234. (setq kmacro-last-counter kmacro-counter
  235. kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg))
  236. kmacro-counter-value-start
  237. arg))
  238. (unless executing-kbd-macro
  239. (kmacro-display-counter))))
  240. (defun kmacro-add-counter (arg)
  241. "Add numeric prefix arg (prompt if missing) to macro counter.
  242. With \\[universal-argument], restore previous counter value."
  243. (interactive "NAdd to macro counter: ")
  244. (if kmacro-initial-counter-value
  245. (setq kmacro-counter kmacro-initial-counter-value
  246. kmacro-initial-counter-value nil))
  247. (let ((last kmacro-last-counter))
  248. (setq kmacro-last-counter kmacro-counter
  249. kmacro-counter (if (and current-prefix-arg (listp current-prefix-arg))
  250. last
  251. kmacro-counter (+ kmacro-counter arg))))
  252. (unless executing-kbd-macro
  253. (kmacro-display-counter)))
  254. (defun kmacro-loop-setup-function ()
  255. "Function called prior to each iteration of macro."
  256. ;; Restore macro counter format to initial format, so it is ok to change
  257. ;; counter format in the macro without restoring it.
  258. (setq kmacro-counter-format kmacro-counter-format-start)
  259. ;; Save initial counter value so we can restore it with C-u kmacro-set-counter.
  260. (setq kmacro-counter-value-start kmacro-counter)
  261. ;; Return non-nil to continue execution.
  262. t)
  263. ;;; Keyboard macro ring
  264. (defvar kmacro-ring nil
  265. "The keyboard macro ring.
  266. Each element is a list (MACRO COUNTER FORMAT). Actually, the head of
  267. the macro ring (when defining or executing) is not stored in the ring;
  268. instead it is available in the variables `last-kbd-macro', `kmacro-counter',
  269. and `kmacro-counter-format'.")
  270. ;; Remember what we are currently looking at with kmacro-view-macro.
  271. (defvar kmacro-view-last-item nil)
  272. (defvar kmacro-view-item-no 0)
  273. (defun kmacro-ring-head ()
  274. "Return pseudo head element in macro ring."
  275. (and last-kbd-macro
  276. (list last-kbd-macro kmacro-counter kmacro-counter-format-start)))
  277. (defun kmacro-push-ring (&optional elt)
  278. "Push ELT or current macro onto `kmacro-ring'."
  279. (when (setq elt (or elt (kmacro-ring-head)))
  280. (let ((history-delete-duplicates nil))
  281. (add-to-history 'kmacro-ring elt kmacro-ring-max))))
  282. (defun kmacro-split-ring-element (elt)
  283. (setq last-kbd-macro (car elt)
  284. kmacro-counter (nth 1 elt)
  285. kmacro-counter-format-start (nth 2 elt)))
  286. (defun kmacro-pop-ring1 (&optional raw)
  287. "Pop head element off macro ring (no check).
  288. Non-nil arg RAW means just return raw first element."
  289. (prog1 (car kmacro-ring)
  290. (unless raw
  291. (kmacro-split-ring-element (car kmacro-ring)))
  292. (setq kmacro-ring (cdr kmacro-ring))))
  293. (defun kmacro-pop-ring (&optional raw)
  294. "Pop head element off macro ring.
  295. Non-nil arg RAW means just return raw first element."
  296. (unless (kmacro-ring-empty-p)
  297. (kmacro-pop-ring1 raw)))
  298. (defun kmacro-ring-empty-p (&optional none)
  299. "Tell user and return t if `last-kbd-macro' is nil or `kmacro-ring' is empty.
  300. Check only `last-kbd-macro' if optional arg NONE is non-nil."
  301. (while (and (null last-kbd-macro) kmacro-ring)
  302. (kmacro-pop-ring1))
  303. (cond
  304. ((null last-kbd-macro)
  305. (message "No keyboard macro defined.")
  306. t)
  307. ((and (null none) (null kmacro-ring))
  308. (message "Only one keyboard macro defined.")
  309. t)
  310. (t nil)))
  311. (defun kmacro-display (macro &optional trunc descr empty)
  312. "Display a keyboard MACRO.
  313. Optional arg TRUNC non-nil specifies to limit width of macro to 60 chars.
  314. Optional arg DESCR is descriptive text for macro; default is \"Macro:\".
  315. Optional arg EMPTY is message to print if no macros are defined."
  316. (if macro
  317. (let* ((x 60)
  318. (m (format-kbd-macro macro))
  319. (l (length m))
  320. (z (and trunc (> l x))))
  321. (message "%s%s: %s%s" (or descr "Macro")
  322. (if (= kmacro-counter 0) ""
  323. (format " [%s]"
  324. (format kmacro-counter-format-start kmacro-counter)))
  325. (if z (substring m 0 (1- x)) m) (if z "..." "")))
  326. (message "%s" (or empty "No keyboard macros defined"))))
  327. (defun kmacro-repeat-on-last-key (keys)
  328. "Process kmacro commands keys immediately after cycling the ring."
  329. (setq keys (vconcat keys))
  330. (let ((n (1- (length keys)))
  331. cmd done repeat)
  332. (while (and last-kbd-macro
  333. (not done)
  334. (aset keys n (read-event))
  335. (setq cmd (key-binding keys t))
  336. (setq repeat (get cmd 'kmacro-repeat)))
  337. (clear-this-command-keys t)
  338. (cond
  339. ((eq repeat 'ring)
  340. (if kmacro-ring
  341. (let ((kmacro-repeat-no-prefix nil))
  342. (funcall cmd nil))
  343. (kmacro-display last-kbd-macro t)))
  344. ((eq repeat 'head)
  345. (let ((kmacro-repeat-no-prefix nil))
  346. (funcall cmd nil)))
  347. ((eq repeat 'stop)
  348. (funcall cmd nil)
  349. (setq done t)))
  350. (setq last-input-event nil)))
  351. (when last-input-event
  352. (clear-this-command-keys t)
  353. (push last-input-event unread-command-events)))
  354. (defun kmacro-get-repeat-prefix ()
  355. (let (keys)
  356. (and kmacro-repeat-no-prefix
  357. (setq keys (this-single-command-keys))
  358. (> (length keys) 1)
  359. keys)))
  360. ;;;###autoload
  361. (defun kmacro-exec-ring-item (item arg)
  362. "Execute item ITEM from the macro ring.
  363. ARG is the number of times to execute the item."
  364. ;; Use counter and format specific to the macro on the ring!
  365. (let ((kmacro-counter (nth 1 item))
  366. (kmacro-counter-format-start (nth 2 item)))
  367. (execute-kbd-macro (car item) arg #'kmacro-loop-setup-function)
  368. (setcar (cdr item) kmacro-counter)))
  369. (defun kmacro-call-ring-2nd (arg)
  370. "Execute second keyboard macro in macro ring."
  371. (interactive "P")
  372. (unless (kmacro-ring-empty-p)
  373. (kmacro-exec-ring-item (car kmacro-ring) arg)))
  374. (defun kmacro-call-ring-2nd-repeat (arg)
  375. "Execute second keyboard macro in macro ring.
  376. This is like `kmacro-call-ring-2nd', but allows repeating macro commands
  377. without repeating the prefix."
  378. (interactive "P")
  379. (let ((keys (kmacro-get-repeat-prefix)))
  380. (kmacro-call-ring-2nd arg)
  381. (if (and kmacro-ring keys)
  382. (kmacro-repeat-on-last-key keys))))
  383. (put 'kmacro-call-ring-2nd-repeat 'kmacro-repeat 'head)
  384. (defun kmacro-view-ring-2nd ()
  385. "Display the current head of the keyboard macro ring."
  386. (interactive)
  387. (unless (kmacro-ring-empty-p)
  388. (kmacro-display (car (car kmacro-ring)) nil "2nd macro")))
  389. (defun kmacro-cycle-ring-next (&optional _arg)
  390. "Move to next keyboard macro in keyboard macro ring.
  391. Displays the selected macro in the echo area.
  392. The ARG parameter is unused."
  393. (interactive)
  394. (unless (kmacro-ring-empty-p)
  395. (kmacro-push-ring)
  396. (let* ((keys (kmacro-get-repeat-prefix))
  397. (len (length kmacro-ring))
  398. (tail (nthcdr (- len 2) kmacro-ring))
  399. (elt (car (cdr tail))))
  400. (setcdr tail nil)
  401. (kmacro-split-ring-element elt)
  402. (kmacro-display last-kbd-macro t)
  403. (if keys
  404. (kmacro-repeat-on-last-key keys)))))
  405. (put 'kmacro-cycle-ring-next 'kmacro-repeat 'ring)
  406. (defun kmacro-cycle-ring-previous (&optional _arg)
  407. "Move to previous keyboard macro in keyboard macro ring.
  408. Displays the selected macro in the echo area.
  409. The ARG parameter is unused."
  410. (interactive)
  411. (unless (kmacro-ring-empty-p)
  412. (let ((keys (kmacro-get-repeat-prefix))
  413. (cur (kmacro-ring-head)))
  414. (kmacro-pop-ring1)
  415. (if kmacro-ring
  416. (nconc kmacro-ring (list cur))
  417. (setq kmacro-ring (list cur)))
  418. (kmacro-display last-kbd-macro t)
  419. (if keys
  420. (kmacro-repeat-on-last-key keys)))))
  421. (put 'kmacro-cycle-ring-previous 'kmacro-repeat 'ring)
  422. (defun kmacro-swap-ring ()
  423. "Swap first two elements on keyboard macro ring."
  424. (interactive)
  425. (unless (kmacro-ring-empty-p)
  426. (let ((cur (kmacro-ring-head)))
  427. (kmacro-pop-ring1)
  428. (kmacro-push-ring cur))
  429. (kmacro-display last-kbd-macro t)))
  430. (defun kmacro-delete-ring-head (&optional _arg)
  431. "Delete current macro from keyboard macro ring.
  432. The ARG parameter is unused."
  433. (interactive)
  434. (unless (kmacro-ring-empty-p t)
  435. (if (null kmacro-ring)
  436. (setq last-kbd-macro nil)
  437. (kmacro-pop-ring))
  438. (kmacro-display last-kbd-macro t nil "Keyboard macro ring is now empty.")))
  439. (put 'kmacro-delete-ring-head 'kmacro-repeat 'head)
  440. ;;; Traditional bindings:
  441. ;;;###autoload
  442. (defun kmacro-start-macro (arg)
  443. "Record subsequent keyboard input, defining a keyboard macro.
  444. The commands are recorded even as they are executed.
  445. Use \\[kmacro-end-macro] to finish recording and make the macro available.
  446. Use \\[kmacro-end-and-call-macro] to execute the macro.
  447. Non-nil arg (prefix arg) means append to last macro defined.
  448. With \\[universal-argument] prefix, append to last keyboard macro
  449. defined. Depending on `kmacro-execute-before-append', this may begin
  450. by re-executing the last macro as if you typed it again.
  451. Otherwise, it sets `kmacro-counter' to ARG or 0 if missing before
  452. defining the macro.
  453. Use \\[kmacro-insert-counter] to insert (and increment) the macro counter.
  454. The counter value can be set or modified via \\[kmacro-set-counter] and \\[kmacro-add-counter].
  455. The format of the counter can be modified via \\[kmacro-set-format].
  456. Use \\[kmacro-name-last-macro] to give it a name that will remain valid even
  457. after another macro is defined.
  458. Use \\[kmacro-bind-to-key] to bind it to a key sequence."
  459. (interactive "P")
  460. (if (or defining-kbd-macro executing-kbd-macro)
  461. (message "Already defining keyboard macro.")
  462. (let ((append (and arg (listp arg))))
  463. (unless append
  464. (if last-kbd-macro
  465. (kmacro-push-ring
  466. (list last-kbd-macro kmacro-counter kmacro-counter-format-start)))
  467. (setq kmacro-counter (or (if arg (prefix-numeric-value arg))
  468. kmacro-initial-counter-value
  469. 0)
  470. kmacro-initial-counter-value nil
  471. kmacro-counter-value-start kmacro-counter
  472. kmacro-last-counter kmacro-counter
  473. kmacro-counter-format kmacro-default-counter-format
  474. kmacro-counter-format-start kmacro-default-counter-format))
  475. (start-kbd-macro append
  476. (and append
  477. (if kmacro-execute-before-append
  478. (> (car arg) 4)
  479. (= (car arg) 4))))
  480. (if (and defining-kbd-macro append)
  481. (setq defining-kbd-macro 'append)))))
  482. ;;;###autoload
  483. (defun kmacro-end-macro (arg)
  484. "Finish defining a keyboard macro.
  485. The definition was started by \\[kmacro-start-macro].
  486. The macro is now available for use via \\[kmacro-call-macro],
  487. or it can be given a name with \\[kmacro-name-last-macro] and then invoked
  488. under that name.
  489. With numeric arg, repeat macro now that many times,
  490. counting the definition just completed as the first repetition.
  491. An argument of zero means repeat until error."
  492. (interactive "P")
  493. ;; Isearch may push the kmacro-end-macro key sequence onto the macro.
  494. ;; Just ignore it when executing the macro.
  495. (unless executing-kbd-macro
  496. (end-kbd-macro arg #'kmacro-loop-setup-function)
  497. (when (and last-kbd-macro (= (length last-kbd-macro) 0))
  498. (setq last-kbd-macro nil)
  499. (message "Ignore empty macro")
  500. ;; Don't call `kmacro-ring-empty-p' to avoid its messages.
  501. (while (and (null last-kbd-macro) kmacro-ring)
  502. (kmacro-pop-ring1)))))
  503. ;;;###autoload
  504. (defun kmacro-call-macro (arg &optional no-repeat end-macro macro)
  505. "Call the keyboard MACRO that you defined with \\[kmacro-start-macro].
  506. A prefix argument serves as a repeat count. Zero means repeat until error.
  507. MACRO defaults to `last-kbd-macro'.
  508. When you call the macro, you can call the macro again by repeating
  509. just the last key in the key sequence that you used to call this
  510. command. See `kmacro-call-repeat-key' and `kmacro-call-repeat-with-arg'
  511. for details on how to adjust or disable this behavior.
  512. To give a macro a name so you can call it even after defining others,
  513. use \\[kmacro-name-last-macro]."
  514. (interactive "p")
  515. (let ((repeat-key (and (or (and (null no-repeat)
  516. (> (length (this-single-command-keys)) 1))
  517. ;; Used when we're in the process of repeating.
  518. (eq no-repeat 'repeating))
  519. last-input-event)))
  520. (if end-macro
  521. (kmacro-end-macro arg) ; modifies last-kbd-macro
  522. (let ((last-kbd-macro (or macro last-kbd-macro)))
  523. (call-last-kbd-macro arg #'kmacro-loop-setup-function)))
  524. (when (consp arg)
  525. (setq arg (car arg)))
  526. (when (and (or (null arg) (> arg 0))
  527. (setq repeat-key
  528. (if (eq kmacro-call-repeat-key t)
  529. repeat-key
  530. kmacro-call-repeat-key)))
  531. ;; Issue a hint to the user, if the echo area isn't in use.
  532. (unless (current-message)
  533. (message "(Type %s to repeat macro%s)"
  534. (format-kbd-macro (vector repeat-key) nil)
  535. (if (and kmacro-call-repeat-with-arg
  536. arg (> arg 1))
  537. (format " %d times" arg) "")))
  538. ;; Can't use the `keep-pred' arg because this overlay keymap
  539. ;; needs to be removed during the next run of the kmacro
  540. ;; (i.e. we must add and remove this map at each repetition).
  541. (set-transient-map
  542. (let ((map (make-sparse-keymap)))
  543. (define-key map (vector repeat-key)
  544. `(lambda () (interactive)
  545. (kmacro-call-macro ,(and kmacro-call-repeat-with-arg arg)
  546. 'repeating nil ,(if end-macro
  547. last-kbd-macro
  548. (or macro last-kbd-macro)))))
  549. map)))))
  550. ;;; Combined function key bindings:
  551. ;;;###autoload
  552. (defun kmacro-start-macro-or-insert-counter (arg)
  553. "Record subsequent keyboard input, defining a keyboard macro.
  554. The commands are recorded even as they are executed.
  555. Sets the `kmacro-counter' to ARG (or 0 if no prefix arg) before defining the
  556. macro.
  557. With \\[universal-argument], appends to current keyboard macro (keeping
  558. the current value of `kmacro-counter').
  559. When defining/executing macro, inserts macro counter and increments
  560. the counter with ARG or 1 if missing. With \\[universal-argument],
  561. inserts previous `kmacro-counter' (but do not modify counter).
  562. The macro counter can be modified via \\[kmacro-set-counter] and \\[kmacro-add-counter].
  563. The format of the counter can be modified via \\[kmacro-set-format]."
  564. (interactive "P")
  565. (if (or defining-kbd-macro executing-kbd-macro)
  566. (kmacro-insert-counter arg)
  567. (kmacro-start-macro arg)))
  568. ;;;###autoload
  569. (defun kmacro-end-or-call-macro (arg &optional no-repeat)
  570. "End kbd macro if currently being defined; else call last kbd macro.
  571. With numeric prefix ARG, repeat macro that many times.
  572. With \\[universal-argument], call second macro in macro ring."
  573. (interactive "P")
  574. (cond
  575. (defining-kbd-macro
  576. (if kmacro-call-repeat-key
  577. (kmacro-call-macro arg no-repeat t)
  578. (kmacro-end-macro arg)))
  579. ((and (eq this-command 'kmacro-view-macro) ;; We are in repeat mode!
  580. kmacro-view-last-item)
  581. (kmacro-exec-ring-item (car kmacro-view-last-item) arg))
  582. ((and arg (listp arg))
  583. (kmacro-call-ring-2nd 1))
  584. (t
  585. (kmacro-call-macro arg no-repeat))))
  586. (defun kmacro-end-or-call-macro-repeat (arg)
  587. "As `kmacro-end-or-call-macro' but allows repeat without repeating prefix."
  588. (interactive "P")
  589. (let ((keys (kmacro-get-repeat-prefix)))
  590. (kmacro-end-or-call-macro arg t)
  591. (if keys
  592. (kmacro-repeat-on-last-key keys))))
  593. (put 'kmacro-end-or-call-macro-repeat 'kmacro-repeat 'head)
  594. ;;;###autoload
  595. (defun kmacro-end-and-call-macro (arg &optional no-repeat)
  596. "Call last keyboard macro, ending it first if currently being defined.
  597. With numeric prefix ARG, repeat macro that many times.
  598. Zero argument means repeat until there is an error.
  599. To give a macro a name, so you can call it even after defining other
  600. macros, use \\[kmacro-name-last-macro]."
  601. (interactive "P")
  602. (if defining-kbd-macro
  603. (kmacro-end-macro nil))
  604. (kmacro-call-macro arg no-repeat))
  605. ;;;###autoload
  606. (defun kmacro-end-call-mouse (event)
  607. "Move point to the position clicked with the mouse and call last kbd macro.
  608. If kbd macro currently being defined end it before activating it."
  609. (interactive "e")
  610. (when defining-kbd-macro
  611. (end-kbd-macro))
  612. (mouse-set-point event)
  613. (kmacro-call-macro nil t))
  614. ;;; Misc. commands
  615. ;; An idea for macro bindings:
  616. ;; Create a separate keymap installed as a minor-mode keymap (e.g. in
  617. ;; the emulation-mode-map-alists) in which macro bindings are made
  618. ;; independent of any other bindings. When first binding is made,
  619. ;; the keymap is created, installed, and enabled. Key seq. C-x C-k +
  620. ;; can then be used to toggle the use of this keymap on and off.
  621. ;; This means that it would be safe(r) to bind ordinary keys like
  622. ;; letters and digits, provided that we inhibit the keymap while
  623. ;; executing the macro later on (but that's controversial...)
  624. (defun kmacro-lambda-form (mac &optional counter format)
  625. "Create lambda form for macro bound to symbol or key."
  626. (if counter
  627. (setq mac (list mac counter format)))
  628. `(lambda (&optional arg)
  629. "Keyboard macro."
  630. (interactive "p")
  631. (kmacro-exec-ring-item ',mac arg)))
  632. (defun kmacro-extract-lambda (mac)
  633. "Extract kmacro from a kmacro lambda form."
  634. (and (consp mac)
  635. (eq (car mac) 'lambda)
  636. (setq mac (assoc 'kmacro-exec-ring-item mac))
  637. (consp (cdr mac))
  638. (consp (car (cdr mac)))
  639. (consp (cdr (car (cdr mac))))
  640. (setq mac (car (cdr (car (cdr mac)))))
  641. (listp mac)
  642. (= (length mac) 3)
  643. (arrayp (car mac))
  644. mac))
  645. (defun kmacro-bind-to-key (_arg)
  646. "When not defining or executing a macro, offer to bind last macro to a key.
  647. The key sequences [C-x C-k 0] through [C-x C-k 9] and [C-x C-k A]
  648. through [C-x C-k Z] are reserved for user bindings, and to bind to
  649. one of these sequences, just enter the digit or letter, rather than
  650. the whole sequence.
  651. You can bind to any valid key sequence, but if you try to bind to
  652. a key with an existing command binding, you will be asked for
  653. confirmation whether to replace that binding. Note that the
  654. binding is made in the `global-map' keymap, so the macro binding
  655. may be shaded by a local key binding.
  656. The ARG parameter is unused."
  657. (interactive "p")
  658. (if (or defining-kbd-macro executing-kbd-macro)
  659. (if defining-kbd-macro
  660. (message "Cannot save macro while defining it."))
  661. (unless last-kbd-macro
  662. (error "No keyboard macro defined"))
  663. (let ((key-seq (read-key-sequence "Bind last macro to key: "))
  664. ok cmd)
  665. (when (= (length key-seq) 1)
  666. (let ((ch (aref key-seq 0)))
  667. (if (and (integerp ch)
  668. (or (and (>= ch ?0) (<= ch ?9))
  669. (and (>= ch ?A) (<= ch ?Z))))
  670. (setq key-seq (concat "\C-x\C-k" key-seq)
  671. ok t))))
  672. (when (and (not (equal key-seq ""))
  673. (or ok
  674. (not (setq cmd (key-binding key-seq)))
  675. (stringp cmd)
  676. (vectorp cmd)
  677. (yes-or-no-p (format "%s runs command %S. Bind anyway? "
  678. (format-kbd-macro key-seq)
  679. cmd))))
  680. (define-key global-map key-seq
  681. (kmacro-lambda-form (kmacro-ring-head)))
  682. (message "Keyboard macro bound to %s" (format-kbd-macro key-seq))))))
  683. (defun kmacro-name-last-macro (symbol)
  684. "Assign a name to the last keyboard macro defined.
  685. Argument SYMBOL is the name to define.
  686. The symbol's function definition becomes the keyboard macro string.
  687. Such a \"function\" cannot be called from Lisp, but it is a valid editor command."
  688. (interactive "SName for last kbd macro: ")
  689. (or last-kbd-macro
  690. (error "No keyboard macro defined"))
  691. (and (fboundp symbol)
  692. (not (get symbol 'kmacro))
  693. (not (stringp (symbol-function symbol)))
  694. (not (vectorp (symbol-function symbol)))
  695. (error "Function %s is already defined and not a keyboard macro"
  696. symbol))
  697. (if (string-equal symbol "")
  698. (error "No command name given"))
  699. (fset symbol (kmacro-lambda-form (kmacro-ring-head)))
  700. (put symbol 'kmacro t))
  701. (defun kmacro-execute-from-register (k)
  702. (kmacro-call-macro current-prefix-arg nil nil k))
  703. (defun kmacro-to-register (r)
  704. "Store the last keyboard macro in register R.
  705. Interactively, reads the register using `register-read-with-preview'."
  706. (interactive
  707. (progn
  708. (or last-kbd-macro (error "No keyboard macro defined"))
  709. (list (register-read-with-preview "Save to register: "))))
  710. (set-register r (registerv-make
  711. last-kbd-macro
  712. :jump-func 'kmacro-execute-from-register
  713. :print-func (lambda (k)
  714. (princ (format "a keyboard macro:\n %s"
  715. (format-kbd-macro k))))
  716. :insert-func (lambda (k)
  717. (insert (format-kbd-macro k))))))
  718. (defun kmacro-view-macro (&optional _arg)
  719. "Display the last keyboard macro.
  720. If repeated, it shows previous elements in the macro ring.
  721. The ARG parameter is unused."
  722. (interactive)
  723. (cond
  724. ((or (kmacro-ring-empty-p)
  725. (not (eq last-command 'kmacro-view-macro)))
  726. (setq kmacro-view-last-item nil))
  727. ((null kmacro-view-last-item)
  728. (setq kmacro-view-last-item kmacro-ring
  729. kmacro-view-item-no 2))
  730. ((consp kmacro-view-last-item)
  731. (setq kmacro-view-last-item (cdr kmacro-view-last-item)
  732. kmacro-view-item-no (1+ kmacro-view-item-no)))
  733. (t
  734. (setq kmacro-view-last-item nil)))
  735. (setq this-command 'kmacro-view-macro
  736. last-command this-command) ;; in case we repeat
  737. (kmacro-display (if kmacro-view-last-item
  738. (car (car kmacro-view-last-item))
  739. last-kbd-macro)
  740. nil
  741. (if kmacro-view-last-item
  742. (concat (cond ((= kmacro-view-item-no 2) "2nd")
  743. ((= kmacro-view-item-no 3) "3nd")
  744. (t (format "%dth" kmacro-view-item-no)))
  745. " previous macro")
  746. "Last macro")))
  747. (defun kmacro-view-macro-repeat (&optional arg)
  748. "Display the last keyboard macro.
  749. If repeated, it shows previous elements in the macro ring.
  750. To execute the displayed macro ring item without changing the macro ring,
  751. just enter C-k.
  752. This is like `kmacro-view-macro', but allows repeating macro commands
  753. without repeating the prefix."
  754. (interactive)
  755. (let ((keys (kmacro-get-repeat-prefix)))
  756. (kmacro-view-macro arg)
  757. (if (and last-kbd-macro keys)
  758. (kmacro-repeat-on-last-key keys))))
  759. (put 'kmacro-view-macro-repeat 'kmacro-repeat 'ring)
  760. (defun kmacro-edit-macro-repeat (&optional arg)
  761. "Edit last keyboard macro."
  762. (interactive "P")
  763. (edit-kbd-macro "\r" arg))
  764. (put 'kmacro-edit-macro-repeat 'kmacro-repeat 'stop)
  765. (defun kmacro-edit-macro (&optional arg)
  766. "As edit last keyboard macro, but without kmacro-repeat property."
  767. (interactive "P")
  768. (edit-kbd-macro "\r" arg))
  769. (defun kmacro-edit-lossage ()
  770. "Edit most recent 300 keystrokes as a keyboard macro."
  771. (interactive)
  772. (kmacro-push-ring)
  773. (edit-kbd-macro "\C-hl"))
  774. ;;; Single-step editing of keyboard macros
  775. (defvar kmacro-step-edit-active) ;; step-editing active
  776. (defvar kmacro-step-edit-new-macro) ;; storage for new macro
  777. (defvar kmacro-step-edit-inserting) ;; inserting into macro
  778. (defvar kmacro-step-edit-appending) ;; append to end of macro
  779. (defvar kmacro-step-edit-replace) ;; replace orig macro when done
  780. (defvar kmacro-step-edit-key-index) ;; index of current key
  781. (defvar kmacro-step-edit-action) ;; automatic action on next pre-command hook
  782. (defvar kmacro-step-edit-help) ;; kmacro step edit help enabled
  783. (defvar kmacro-step-edit-num-input-keys) ;; to ignore duplicate pre-command hook
  784. (defvar kmacro-step-edit-map
  785. (let ((map (make-sparse-keymap)))
  786. ;; query-replace-map answers include: `act', `skip', `act-and-show',
  787. ;; `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
  788. ;; `automatic', `backup', `exit-prefix', and `help'.")
  789. ;; Also: `quit', `edit-replacement'
  790. (set-keymap-parent map query-replace-map)
  791. (define-key map "\t" 'act-repeat)
  792. (define-key map [tab] 'act-repeat)
  793. (define-key map "\C-k" 'skip-rest)
  794. (define-key map "c" 'automatic)
  795. (define-key map "f" 'skip-keep)
  796. (define-key map "q" 'quit)
  797. (define-key map "d" 'skip)
  798. (define-key map "\C-d" 'skip)
  799. (define-key map "i" 'insert)
  800. (define-key map "I" 'insert-1)
  801. (define-key map "r" 'replace)
  802. (define-key map "R" 'replace-1)
  803. (define-key map "a" 'append)
  804. (define-key map "A" 'append-end)
  805. map)
  806. "Keymap that defines the responses to questions in `kmacro-step-edit-macro'.
  807. This keymap is an extension to the `query-replace-map', allowing the
  808. following additional answers: `insert', `insert-1', `replace', `replace-1',
  809. `append', `append-end', `act-repeat', `skip-end', `skip-keep'.")
  810. (defun kmacro-step-edit-prompt (macro index)
  811. ;; Show step-edit prompt
  812. (let ((keys (and (not kmacro-step-edit-appending)
  813. index (substring macro index executing-kbd-macro-index)))
  814. (future (and (not kmacro-step-edit-appending)
  815. (substring macro executing-kbd-macro-index)))
  816. (message-log-max nil)
  817. (curmsg (current-message)))
  818. ;; TODO: Scroll macro if max-mini-window-height is too small.
  819. (message "%s"
  820. (concat
  821. (format "Macro: %s%s%s%s%s\n"
  822. (format-kbd-macro kmacro-step-edit-new-macro 1)
  823. (if (and kmacro-step-edit-new-macro (> (length kmacro-step-edit-new-macro) 0)) " " "")
  824. (propertize (if keys (format-kbd-macro keys)
  825. (if kmacro-step-edit-appending "<APPEND>" "<INSERT>")) 'face 'region)
  826. (if future " " "")
  827. (if future (format-kbd-macro future) ""))
  828. (cond
  829. ((minibufferp)
  830. (format "%s\n%s\n"
  831. (propertize "\
  832. minibuffer " 'face 'header-line)
  833. (buffer-substring (point-min) (point-max))))
  834. (curmsg
  835. (format "%s\n%s\n"
  836. (propertize "\
  837. echo area " 'face 'header-line)
  838. curmsg))
  839. (t ""))
  840. (if keys
  841. (format "%s\n%s%s %S [yn iIaArR C-k kq!] "
  842. (propertize "\
  843. --------------Step Edit Keyboard Macro [?: help]---------------" 'face 'mode-line)
  844. (if kmacro-step-edit-help "\
  845. Step: y/SPC: execute next, d/n/DEL: skip next, f: skip but keep
  846. TAB: execute while same, ?: toggle help
  847. Edit: i: insert, r: replace, a: append, A: append at end,
  848. I/R: insert/replace with one sequence,
  849. End: !/c: execute rest, C-k: skip rest and save, q/C-g: quit
  850. ----------------------------------------------------------------
  851. " "")
  852. (propertize "Next command:" 'face 'bold)
  853. this-command)
  854. (propertize
  855. (format "Type key sequence%s to insert and execute%s: "
  856. (if (numberp kmacro-step-edit-inserting) "" "s")
  857. (if (numberp kmacro-step-edit-inserting) "" " (end with C-j)"))
  858. 'face 'bold))))))
  859. (defun kmacro-step-edit-query ()
  860. ;; Pre-command hook function for step-edit in "command" mode
  861. (let ((resize-mini-windows t)
  862. (max-mini-window-height kmacro-step-edit-mini-window-height)
  863. act restore-index next-index)
  864. ;; Handle commands which reads additional input using read-char.
  865. (cond
  866. ((and (eq this-command 'quoted-insert)
  867. (not (eq kmacro-step-edit-action t)))
  868. ;; Find the actual end of this key sequence.
  869. ;; Must be able to backtrack in case we actually execute it.
  870. (setq restore-index executing-kbd-macro-index)
  871. (let (unread-command-events)
  872. (quoted-insert 0)
  873. (when unread-command-events
  874. (setq executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events))
  875. next-index executing-kbd-macro-index)))))
  876. ;; Query the user; stop macro execution temporarily.
  877. (let ((macro executing-kbd-macro)
  878. (executing-kbd-macro nil)
  879. (defining-kbd-macro nil))
  880. ;; Any action requested by previous command
  881. (cond
  882. ((eq kmacro-step-edit-action t) ;; Reentry for actual command @ end of prefix arg.
  883. (cond
  884. ((eq this-command 'quoted-insert)
  885. (clear-this-command-keys) ;; recent-keys actually
  886. (let (unread-command-events)
  887. (quoted-insert (prefix-numeric-value current-prefix-arg))
  888. (setq kmacro-step-edit-new-macro
  889. (vconcat kmacro-step-edit-new-macro (recent-keys)))
  890. (when unread-command-events
  891. (setq kmacro-step-edit-new-macro
  892. (substring kmacro-step-edit-new-macro 0 (- (length unread-command-events)))
  893. executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events)))))
  894. (setq current-prefix-arg nil
  895. prefix-arg nil)
  896. (setq act 'ignore))
  897. (t
  898. (setq act 'act)))
  899. (setq kmacro-step-edit-action nil))
  900. ((eq this-command kmacro-step-edit-action) ;; TAB -> activate while same command
  901. (setq act 'act))
  902. (t
  903. (setq kmacro-step-edit-action nil)))
  904. ;; Handle prefix arg, or query user
  905. (cond
  906. (act act) ;; set above
  907. (t
  908. (kmacro-step-edit-prompt macro kmacro-step-edit-key-index)
  909. (setq act (lookup-key kmacro-step-edit-map
  910. (vector (with-current-buffer (current-buffer) (read-event))))))))
  911. ;; Resume macro execution and perform the action
  912. (cond
  913. ((cond
  914. ((eq act 'act)
  915. t)
  916. ((eq act 'act-repeat)
  917. (setq kmacro-step-edit-action this-command)
  918. t)
  919. ((eq act 'quit)
  920. (setq kmacro-step-edit-replace nil)
  921. (setq kmacro-step-edit-active 'ignore)
  922. nil)
  923. ((eq act 'skip)
  924. nil)
  925. ((eq act 'skip-keep)
  926. (setq this-command 'ignore)
  927. t)
  928. ((eq act 'skip-rest)
  929. (setq kmacro-step-edit-active 'ignore)
  930. nil)
  931. ((memq act '(automatic exit))
  932. (setq kmacro-step-edit-active nil)
  933. (setq act t)
  934. t)
  935. ((member act '(insert-1 insert))
  936. (setq executing-kbd-macro-index kmacro-step-edit-key-index)
  937. (setq kmacro-step-edit-inserting (if (eq act 'insert-1) 1 t))
  938. nil)
  939. ((member act '(replace-1 replace))
  940. (setq kmacro-step-edit-inserting (if (eq act 'replace-1) 1 t))
  941. (if (= executing-kbd-macro-index (length executing-kbd-macro))
  942. (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
  943. kmacro-step-edit-appending t))
  944. nil)
  945. ((eq act 'append)
  946. (setq kmacro-step-edit-inserting t)
  947. (if (= executing-kbd-macro-index (length executing-kbd-macro))
  948. (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
  949. kmacro-step-edit-appending t))
  950. t)
  951. ((eq act 'append-end)
  952. (if (= executing-kbd-macro-index (length executing-kbd-macro))
  953. (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
  954. kmacro-step-edit-inserting t
  955. kmacro-step-edit-appending t)
  956. (setq kmacro-step-edit-active 'append-end))
  957. (setq act t)
  958. t)
  959. ((eq act 'help)
  960. (setq executing-kbd-macro-index kmacro-step-edit-key-index)
  961. (setq kmacro-step-edit-help (not kmacro-step-edit-help))
  962. nil)
  963. (t ;; Ignore unknown responses
  964. (setq executing-kbd-macro-index kmacro-step-edit-key-index)
  965. nil))
  966. (if (> executing-kbd-macro-index kmacro-step-edit-key-index)
  967. (setq kmacro-step-edit-new-macro
  968. (vconcat kmacro-step-edit-new-macro
  969. (substring executing-kbd-macro
  970. kmacro-step-edit-key-index
  971. (if (eq act t) nil
  972. executing-kbd-macro-index)))))
  973. (if restore-index
  974. (setq executing-kbd-macro-index restore-index)))
  975. (t
  976. (setq this-command 'ignore)))
  977. (setq kmacro-step-edit-key-index next-index)))
  978. (defun kmacro-step-edit-insert ()
  979. ;; Pre-command hook function for step-edit in "insert" mode
  980. (let ((resize-mini-windows t)
  981. (max-mini-window-height kmacro-step-edit-mini-window-height)
  982. (macro executing-kbd-macro)
  983. (executing-kbd-macro nil)
  984. (defining-kbd-macro nil)
  985. cmd keys next-index)
  986. (setq executing-kbd-macro-index kmacro-step-edit-key-index)
  987. (kmacro-step-edit-prompt macro nil)
  988. ;; Now, we have read a key sequence from the macro, but we don't want
  989. ;; to execute it yet. So push it back and read another sequence.
  990. (setq keys (read-key-sequence nil nil nil nil t))
  991. (setq cmd (key-binding keys t nil))
  992. (if (cond
  993. ((null cmd)
  994. t)
  995. ((eq cmd 'quoted-insert)
  996. (clear-this-command-keys) ;; recent-keys actually
  997. (quoted-insert (prefix-numeric-value current-prefix-arg))
  998. (setq current-prefix-arg nil
  999. prefix-arg nil)
  1000. (setq keys (vconcat keys (recent-keys)))
  1001. (when (numberp kmacro-step-edit-inserting)
  1002. (setq kmacro-step-edit-inserting nil)
  1003. (when unread-command-events
  1004. (setq keys (substring keys 0 (- (length unread-command-events)))
  1005. executing-kbd-macro-index (- executing-kbd-macro-index (length unread-command-events))
  1006. next-index executing-kbd-macro-index
  1007. unread-command-events nil)))
  1008. (setq cmd 'ignore)
  1009. nil)
  1010. ((numberp kmacro-step-edit-inserting)
  1011. (setq kmacro-step-edit-inserting nil)
  1012. nil)
  1013. ((equal keys "\C-j")
  1014. (setq kmacro-step-edit-inserting nil)
  1015. (setq kmacro-step-edit-action nil)
  1016. (setq next-index kmacro-step-edit-key-index)
  1017. t)
  1018. (t nil))
  1019. (setq this-command 'ignore)
  1020. (setq this-command cmd)
  1021. (if (memq this-command '(self-insert-command digit-argument))
  1022. (setq last-command-event (aref keys (1- (length keys)))))
  1023. (if keys
  1024. (setq kmacro-step-edit-new-macro (vconcat kmacro-step-edit-new-macro keys))))
  1025. (setq kmacro-step-edit-key-index next-index)))
  1026. (defun kmacro-step-edit-pre-command ()
  1027. (remove-hook 'post-command-hook 'kmacro-step-edit-post-command)
  1028. (when kmacro-step-edit-active
  1029. (cond
  1030. ((eq kmacro-step-edit-active 'ignore)
  1031. (setq this-command 'ignore))
  1032. ((eq kmacro-step-edit-active 'append-end)
  1033. (if (= executing-kbd-macro-index (length executing-kbd-macro))
  1034. (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
  1035. kmacro-step-edit-inserting t
  1036. kmacro-step-edit-appending t
  1037. kmacro-step-edit-active t)))
  1038. ((/= kmacro-step-edit-num-input-keys num-input-keys)
  1039. (if kmacro-step-edit-inserting
  1040. (kmacro-step-edit-insert)
  1041. (kmacro-step-edit-query))
  1042. (setq kmacro-step-edit-num-input-keys num-input-keys)
  1043. (if (and kmacro-step-edit-appending (not kmacro-step-edit-inserting))
  1044. (setq kmacro-step-edit-appending nil
  1045. kmacro-step-edit-active 'ignore)))))
  1046. (when (eq kmacro-step-edit-active t)
  1047. (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)))
  1048. (defun kmacro-step-edit-minibuf-setup ()
  1049. (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command t)
  1050. (when kmacro-step-edit-active
  1051. (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil t)))
  1052. (defun kmacro-step-edit-post-command ()
  1053. (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command)
  1054. (when kmacro-step-edit-active
  1055. (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil nil)
  1056. (if kmacro-step-edit-key-index
  1057. (setq executing-kbd-macro-index kmacro-step-edit-key-index)
  1058. (setq kmacro-step-edit-key-index executing-kbd-macro-index))))
  1059. (defun kmacro-step-edit-macro ()
  1060. "Step edit and execute last keyboard macro.
  1061. To customize possible responses, change the \"bindings\" in `kmacro-step-edit-map'."
  1062. (interactive)
  1063. (let ((kmacro-step-edit-active t)
  1064. (kmacro-step-edit-new-macro "")
  1065. (kmacro-step-edit-inserting nil)
  1066. (kmacro-step-edit-appending nil)
  1067. (kmacro-step-edit-replace t)
  1068. (kmacro-step-edit-key-index 0)
  1069. (kmacro-step-edit-action nil)
  1070. (kmacro-step-edit-help nil)
  1071. (kmacro-step-edit-num-input-keys num-input-keys)
  1072. (pre-command-hook pre-command-hook)
  1073. (post-command-hook post-command-hook)
  1074. (minibuffer-setup-hook minibuffer-setup-hook))
  1075. (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil)
  1076. (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)
  1077. (add-hook 'minibuffer-setup-hook 'kmacro-step-edit-minibuf-setup t)
  1078. (call-last-kbd-macro nil nil)
  1079. (when (and kmacro-step-edit-replace
  1080. kmacro-step-edit-new-macro
  1081. (not (equal last-kbd-macro kmacro-step-edit-new-macro)))
  1082. (kmacro-push-ring)
  1083. (setq last-kbd-macro kmacro-step-edit-new-macro))))
  1084. (provide 'kmacro)
  1085. ;;; kmacro.el ends here