Function: edit-kbd-macro

edit-kbd-macro is an autoloaded, interactive and byte-compiled function defined in edmacro.el.gz.

Signature

(edit-kbd-macro KEYS &optional PREFIX FINISH-HOOK STORE-HOOK)

Documentation

Edit a keyboard macro.

At the prompt, type any key sequence which is bound to a keyboard macro. Or, type C-x e (kmacro-end-and-call-macro) or \RET to edit the last keyboard macro, C-h l (view-lossage) to edit the last 300 keystrokes as a keyboard macro, or M-x (execute-extended-command) to edit a macro by its command name. With a prefix argument, format the macro in a more concise way.

View in manual

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/edmacro.el.gz
;;;###autoload
(defun edit-kbd-macro (keys &optional prefix finish-hook store-hook)
  "Edit a keyboard macro.
At the prompt, type any key sequence which is bound to a keyboard macro.
Or, type \\[kmacro-end-and-call-macro] or \\`RET' to edit the last
keyboard macro, \\[view-lossage] to edit the last 300
keystrokes as a keyboard macro, or \\[execute-extended-command]
to edit a macro by its command name.
With a prefix argument, format the macro in a more concise way."
  (interactive
   (list (read-key-sequence (substitute-command-keys "Keyboard macro to edit \
\(\\[kmacro-end-and-call-macro], \\[execute-extended-command], \\[view-lossage],\
 or keys): "))
         current-prefix-arg))
  (when keys
    (let ((cmd (if (arrayp keys) (key-binding keys) keys))
          (cmd-noremap (when (arrayp keys) (key-binding keys nil t)))
	  (mac nil) (mac-counter nil) (mac-format nil))
      (cond (store-hook
	     (setq mac keys)
	     (setq cmd nil))
	    ((or (memq cmd '(call-last-kbd-macro kmacro-call-macro kmacro-end-or-call-macro kmacro-end-and-call-macro))
                 (memq cmd-noremap '(call-last-kbd-macro kmacro-call-macro kmacro-end-or-call-macro kmacro-end-and-call-macro))
		 (member keys '("\r" [return])))
	     (or last-kbd-macro
                 (y-or-n-p "No keyboard macro defined.  Create one?")
		 (keyboard-quit))
	     (setq mac (or last-kbd-macro ""))
	     (setq keys nil)
	     (setq cmd 'last-kbd-macro))
	    ((memq 'execute-extended-command (list cmd cmd-noremap))
	     (setq cmd (read-command "Name of keyboard macro to edit: "))
	     (if (string-equal cmd "")
		 (error "No command name given"))
	     (setq keys nil)
	     (setq mac (symbol-function cmd)))
	    ((or (memq cmd '(view-lossage electric-view-lossage))
                 (memq cmd-noremap '(view-lossage electric-view-lossage)))
	     (setq mac (recent-keys))
	     (setq keys nil)
	     (setq cmd 'last-kbd-macro))
	    ((null cmd)
	     (error "Key sequence %s is not defined" (key-description keys)))
	    ((symbolp cmd)
	     (setq mac (symbol-function cmd)))
	    (t
	     (setq mac cmd)
	     (setq cmd nil)))
      (when (kmacro-p mac)
	(setq mac-counter (kmacro--counter mac)
	      mac-format (kmacro--format mac)
              mac (kmacro--keys mac)))
      (unless (arrayp mac)
	(error "Key sequence %s is not a keyboard macro"
	       (key-description keys)))
      (message "Formatting keyboard macro...")
      (let* ((oldbuf (current-buffer))
	     (mmac (edmacro-fix-menu-commands mac))
	     (fmt (edmacro-format-keys mmac 1))
	     (fmtv (let ((fmtv (edmacro-format-keys mmac (not prefix))))
                     (if (not edmacro-reverse-macro-lines)
                         fmtv
                       (with-temp-buffer
                         (insert fmtv)
                         (reverse-region (point-min) (point-max))
                         (buffer-string)))))
	     (buf (get-buffer-create "*Edit Macro*")))
	(message "Formatting keyboard macro...done")
	(switch-to-buffer buf)
	(kill-all-local-variables)
	(use-local-map edmacro-mode-map)
	(setq buffer-read-only nil)
	(setq major-mode 'edmacro-mode)
	(setq mode-name "Edit Macro")
        (setq-local edmacro-original-buffer oldbuf)
        (setq-local edmacro-finish-hook finish-hook)
        (setq-local edmacro-store-hook store-hook)
        (setq-local font-lock-defaults
                    '(edmacro-mode-font-lock-keywords nil nil ((?\" . "w"))))
        (setq font-lock-multiline nil)
        ;; Make buffer-local so that the commands still work
        ;; even if the default value changes.
        (make-local-variable 'edmacro-reverse-macro-lines)
	(erase-buffer)
        (insert (substitute-command-keys
                 (concat
                  ;; When editing this, make sure to update
                  ;; `edmacro-mode-font-lock-keywords' to match.
                  ";; Keyboard Macro Editor.  Press \\[edmacro-finish-edit] "
                  "to finish; press \\[kill-buffer] \\`RET' to cancel.\n")
                 ;; Use 'no-face argument to not conflict with font-lock.
                 'no-face))
	(insert ";; Original keys: " fmt "\n")
	(unless store-hook
	  (insert "\nCommand: " (if cmd (symbol-name cmd) "none") "\n")
	  (let ((gkeys (where-is-internal (or cmd mac) '(keymap))))
	    (if (and keys (not (member keys gkeys)))
		(setq gkeys (cons keys gkeys)))
	    (if gkeys
		(while gkeys
		  (insert "Key: " (edmacro-format-keys (pop gkeys) 1) "\n"))
	      (insert "Key: none\n")))
	  (when (and mac-counter mac-format)
	    (insert (format "Counter: %d\nFormat: \"%s\"\n" mac-counter mac-format))))
	(insert (format "\nMacro%s:\n\n" (if edmacro-reverse-macro-lines
                                             " (most recent line first)"
                                           "")))
	(save-excursion
	  (insert fmtv "\n"))
	(recenter '(4))
	(when (eq mac mmac)
	  (set-buffer-modified-p nil))
	(run-hooks 'edmacro-format-hook)))))