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