kmacro.el 45 KB

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