diff options
| author | Stefan Monnier | 2017-11-24 11:32:14 -0500 |
|---|---|---|
| committer | Stefan Monnier | 2017-11-24 11:32:14 -0500 |
| commit | 4f779ff8e6445d9b46a87f494396ccb5f72b0350 (patch) | |
| tree | 4007a6a4b32718cc73a07adea8a80868ae239bd9 | |
| parent | b407c521f24b07b76eee0cd06d471e629cef18e8 (diff) | |
| download | emacs-4f779ff8e6445d9b46a87f494396ccb5f72b0350.tar.gz emacs-4f779ff8e6445d9b46a87f494396ccb5f72b0350.zip | |
* lisp/macros.el (insert-kbd-macro): Recognize kmacros reliably
* lisp/macros.el (name-last-kbd-macro): Make it an alias of
kmacro-name-last-macro.
(insert-kbd-macro): Use kmacro-keyboard-macro-p so it also works for
kmacros that fail to have a `kmacro' property.
(see https://emacs.stackexchange.com/questions/37065, for an example).
* lisp/kmacro.el: Remove redundant :group args.
(kmacro-insert-counter): Simplify.
(kmacro-extract-lambda): Use car/cdr-safe.
(kmacro-p, kmacro-keyboard-macro-p): New functions.
(kmacro-name-last-macro): Use them.
| -rw-r--r-- | lisp/kmacro.el | 73 | ||||
| -rw-r--r-- | lisp/macros.el | 30 |
2 files changed, 46 insertions, 57 deletions
diff --git a/lisp/kmacro.el b/lisp/kmacro.el index 5729f2fc8d3..da02ab5aca4 100644 --- a/lisp/kmacro.el +++ b/lisp/kmacro.el | |||
| @@ -124,13 +124,11 @@ | |||
| 124 | (defcustom kmacro-call-mouse-event 'S-mouse-3 | 124 | (defcustom kmacro-call-mouse-event 'S-mouse-3 |
| 125 | "The mouse event used by kmacro to call a macro. | 125 | "The mouse event used by kmacro to call a macro. |
| 126 | Set to nil if no mouse binding is desired." | 126 | Set to nil if no mouse binding is desired." |
| 127 | :type 'symbol | 127 | :type 'symbol) |
| 128 | :group 'kmacro) | ||
| 129 | 128 | ||
| 130 | (defcustom kmacro-ring-max 8 | 129 | (defcustom kmacro-ring-max 8 |
| 131 | "Maximum number of keyboard macros to save in macro ring." | 130 | "Maximum number of keyboard macros to save in macro ring." |
| 132 | :type 'integer | 131 | :type 'integer) |
| 133 | :group 'kmacro) | ||
| 134 | 132 | ||
| 135 | 133 | ||
| 136 | (defcustom kmacro-execute-before-append t | 134 | (defcustom kmacro-execute-before-append t |
| @@ -141,32 +139,27 @@ execute the macro. | |||
| 141 | Otherwise, a single \\[universal-argument] prefix does not execute the | 139 | Otherwise, a single \\[universal-argument] prefix does not execute the |
| 142 | macro, while more than one \\[universal-argument] prefix causes the | 140 | macro, while more than one \\[universal-argument] prefix causes the |
| 143 | macro to be executed before appending to it." | 141 | macro to be executed before appending to it." |
| 144 | :type 'boolean | 142 | :type 'boolean) |
| 145 | :group 'kmacro) | ||
| 146 | 143 | ||
| 147 | 144 | ||
| 148 | (defcustom kmacro-repeat-no-prefix t | 145 | (defcustom kmacro-repeat-no-prefix t |
| 149 | "Allow repeating certain macro commands without entering the C-x C-k prefix." | 146 | "Allow repeating certain macro commands without entering the C-x C-k prefix." |
| 150 | :type 'boolean | 147 | :type 'boolean) |
| 151 | :group 'kmacro) | ||
| 152 | 148 | ||
| 153 | (defcustom kmacro-call-repeat-key t | 149 | (defcustom kmacro-call-repeat-key t |
| 154 | "Allow repeating macro call using last key or a specific key." | 150 | "Allow repeating macro call using last key or a specific key." |
| 155 | :type '(choice (const :tag "Disabled" nil) | 151 | :type '(choice (const :tag "Disabled" nil) |
| 156 | (const :tag "Last key" t) | 152 | (const :tag "Last key" t) |
| 157 | (character :tag "Character" :value ?e) | 153 | (character :tag "Character" :value ?e) |
| 158 | (symbol :tag "Key symbol" :value RET)) | 154 | (symbol :tag "Key symbol" :value RET))) |
| 159 | :group 'kmacro) | ||
| 160 | 155 | ||
| 161 | (defcustom kmacro-call-repeat-with-arg nil | 156 | (defcustom kmacro-call-repeat-with-arg nil |
| 162 | "Repeat macro call with original arg when non-nil; repeat once if nil." | 157 | "Repeat macro call with original arg when non-nil; repeat once if nil." |
| 163 | :type 'boolean | 158 | :type 'boolean) |
| 164 | :group 'kmacro) | ||
| 165 | 159 | ||
| 166 | (defcustom kmacro-step-edit-mini-window-height 0.75 | 160 | (defcustom kmacro-step-edit-mini-window-height 0.75 |
| 167 | "Override `max-mini-window-height' when step edit keyboard macro." | 161 | "Override `max-mini-window-height' when step edit keyboard macro." |
| 168 | :type 'number | 162 | :type 'number) |
| 169 | :group 'kmacro) | ||
| 170 | 163 | ||
| 171 | ;; Keymap | 164 | ;; Keymap |
| 172 | 165 | ||
| @@ -261,7 +254,7 @@ previous `kmacro-counter', and do not modify counter." | |||
| 261 | (if kmacro-initial-counter-value | 254 | (if kmacro-initial-counter-value |
| 262 | (setq kmacro-counter kmacro-initial-counter-value | 255 | (setq kmacro-counter kmacro-initial-counter-value |
| 263 | kmacro-initial-counter-value nil)) | 256 | kmacro-initial-counter-value nil)) |
| 264 | (if (and arg (listp arg)) | 257 | (if (consp arg) |
| 265 | (insert (format kmacro-counter-format kmacro-last-counter)) | 258 | (insert (format kmacro-counter-format kmacro-last-counter)) |
| 266 | (insert (format kmacro-counter-format kmacro-counter)) | 259 | (insert (format kmacro-counter-format kmacro-counter)) |
| 267 | (kmacro-add-counter (prefix-numeric-value arg)))) | 260 | (kmacro-add-counter (prefix-numeric-value arg)))) |
| @@ -280,8 +273,8 @@ previous `kmacro-counter', and do not modify counter." | |||
| 280 | (defun kmacro-display-counter (&optional value) | 273 | (defun kmacro-display-counter (&optional value) |
| 281 | "Display current counter value." | 274 | "Display current counter value." |
| 282 | (unless value (setq value kmacro-counter)) | 275 | (unless value (setq value kmacro-counter)) |
| 283 | (message "New macro counter value: %s (%d)" (format kmacro-counter-format value) value)) | 276 | (message "New macro counter value: %s (%d)" |
| 284 | 277 | (format kmacro-counter-format value) value)) | |
| 285 | 278 | ||
| 286 | (defun kmacro-set-counter (arg) | 279 | (defun kmacro-set-counter (arg) |
| 287 | "Set `kmacro-counter' to ARG or prompt if missing. | 280 | "Set `kmacro-counter' to ARG or prompt if missing. |
| @@ -774,19 +767,18 @@ If kbd macro currently being defined end it before activating it." | |||
| 774 | 767 | ||
| 775 | (defun kmacro-extract-lambda (mac) | 768 | (defun kmacro-extract-lambda (mac) |
| 776 | "Extract kmacro from a kmacro lambda form." | 769 | "Extract kmacro from a kmacro lambda form." |
| 777 | (and (consp mac) | 770 | (and (eq (car-safe mac) 'lambda) |
| 778 | (eq (car mac) 'lambda) | ||
| 779 | (setq mac (assoc 'kmacro-exec-ring-item mac)) | 771 | (setq mac (assoc 'kmacro-exec-ring-item mac)) |
| 780 | (consp (cdr mac)) | 772 | (setq mac (car-safe (cdr-safe (car-safe (cdr-safe mac))))) |
| 781 | (consp (car (cdr mac))) | ||
| 782 | (consp (cdr (car (cdr mac)))) | ||
| 783 | (setq mac (car (cdr (car (cdr mac))))) | ||
| 784 | (listp mac) | 773 | (listp mac) |
| 785 | (= (length mac) 3) | 774 | (= (length mac) 3) |
| 786 | (arrayp (car mac)) | 775 | (arrayp (car mac)) |
| 787 | mac)) | 776 | mac)) |
| 788 | 777 | ||
| 789 | 778 | ||
| 779 | (defalias 'kmacro-p #'kmacro-extract-lambda | ||
| 780 | "Return non-nil if MAC is a kmacro keyboard macro.") | ||
| 781 | |||
| 790 | (defun kmacro-bind-to-key (_arg) | 782 | (defun kmacro-bind-to-key (_arg) |
| 791 | "When not defining or executing a macro, offer to bind last macro to a key. | 783 | "When not defining or executing a macro, offer to bind last macro to a key. |
| 792 | The key sequences [C-x C-k 0] through [C-x C-k 9] and [C-x C-k A] | 784 | The key sequences [C-x C-k 0] through [C-x C-k 9] and [C-x C-k A] |
| @@ -827,6 +819,13 @@ The ARG parameter is unused." | |||
| 827 | (kmacro-lambda-form (kmacro-ring-head))) | 819 | (kmacro-lambda-form (kmacro-ring-head))) |
| 828 | (message "Keyboard macro bound to %s" (format-kbd-macro key-seq)))))) | 820 | (message "Keyboard macro bound to %s" (format-kbd-macro key-seq)))))) |
| 829 | 821 | ||
| 822 | (defun kmacro-keyboard-macro-p (symbol) | ||
| 823 | "Return non-nil if SYMBOL is the name of some sort of keyboard macro." | ||
| 824 | (let ((f (symbol-function symbol))) | ||
| 825 | (when f | ||
| 826 | (or (stringp f) | ||
| 827 | (vectorp f) | ||
| 828 | (kmacro-p f))))) | ||
| 830 | 829 | ||
| 831 | (defun kmacro-name-last-macro (symbol) | 830 | (defun kmacro-name-last-macro (symbol) |
| 832 | "Assign a name to the last keyboard macro defined. | 831 | "Assign a name to the last keyboard macro defined. |
| @@ -837,14 +836,18 @@ Such a \"function\" cannot be called from Lisp, but it is a valid editor command | |||
| 837 | (or last-kbd-macro | 836 | (or last-kbd-macro |
| 838 | (error "No keyboard macro defined")) | 837 | (error "No keyboard macro defined")) |
| 839 | (and (fboundp symbol) | 838 | (and (fboundp symbol) |
| 840 | (not (get symbol 'kmacro)) | 839 | (not (kmacro-keyboard-macro-p symbol)) |
| 841 | (not (stringp (symbol-function symbol))) | ||
| 842 | (not (vectorp (symbol-function symbol))) | ||
| 843 | (error "Function %s is already defined and not a keyboard macro" | 840 | (error "Function %s is already defined and not a keyboard macro" |
| 844 | symbol)) | 841 | symbol)) |
| 845 | (if (string-equal symbol "") | 842 | (if (string-equal symbol "") |
| 846 | (error "No command name given")) | 843 | (error "No command name given")) |
| 844 | ;; FIXME: Use plain old `last-kbd-macro' for kmacros where it doesn't | ||
| 845 | ;; make a difference? | ||
| 847 | (fset symbol (kmacro-lambda-form (kmacro-ring-head))) | 846 | (fset symbol (kmacro-lambda-form (kmacro-ring-head))) |
| 847 | ;; This used to be used to detect when a symbol corresponds to a kmacro. | ||
| 848 | ;; Nowadays it's unused because we used `kmacro-p' instead to see if the | ||
| 849 | ;; symbol's function definition matches that of a kmacro, which is more | ||
| 850 | ;; reliable. | ||
| 848 | (put symbol 'kmacro t)) | 851 | (put symbol 'kmacro t)) |
| 849 | 852 | ||
| 850 | 853 | ||
| @@ -1203,7 +1206,7 @@ following additional answers: `insert', `insert-1', `replace', `replace-1', | |||
| 1203 | (setq kmacro-step-edit-key-index next-index))) | 1206 | (setq kmacro-step-edit-key-index next-index))) |
| 1204 | 1207 | ||
| 1205 | (defun kmacro-step-edit-pre-command () | 1208 | (defun kmacro-step-edit-pre-command () |
| 1206 | (remove-hook 'post-command-hook 'kmacro-step-edit-post-command) | 1209 | (remove-hook 'post-command-hook #'kmacro-step-edit-post-command) |
| 1207 | (when kmacro-step-edit-active | 1210 | (when kmacro-step-edit-active |
| 1208 | (cond | 1211 | (cond |
| 1209 | ((eq kmacro-step-edit-active 'ignore) | 1212 | ((eq kmacro-step-edit-active 'ignore) |
| @@ -1223,17 +1226,17 @@ following additional answers: `insert', `insert-1', `replace', `replace-1', | |||
| 1223 | (setq kmacro-step-edit-appending nil | 1226 | (setq kmacro-step-edit-appending nil |
| 1224 | kmacro-step-edit-active 'ignore))))) | 1227 | kmacro-step-edit-active 'ignore))))) |
| 1225 | (when (eq kmacro-step-edit-active t) | 1228 | (when (eq kmacro-step-edit-active t) |
| 1226 | (add-hook 'post-command-hook 'kmacro-step-edit-post-command t))) | 1229 | (add-hook 'post-command-hook #'kmacro-step-edit-post-command t))) |
| 1227 | 1230 | ||
| 1228 | (defun kmacro-step-edit-minibuf-setup () | 1231 | (defun kmacro-step-edit-minibuf-setup () |
| 1229 | (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command t) | 1232 | (remove-hook 'pre-command-hook #'kmacro-step-edit-pre-command t) |
| 1230 | (when kmacro-step-edit-active | 1233 | (when kmacro-step-edit-active |
| 1231 | (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil t))) | 1234 | (add-hook 'pre-command-hook #'kmacro-step-edit-pre-command nil t))) |
| 1232 | 1235 | ||
| 1233 | (defun kmacro-step-edit-post-command () | 1236 | (defun kmacro-step-edit-post-command () |
| 1234 | (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command) | 1237 | (remove-hook 'pre-command-hook #'kmacro-step-edit-pre-command) |
| 1235 | (when kmacro-step-edit-active | 1238 | (when kmacro-step-edit-active |
| 1236 | (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil nil) | 1239 | (add-hook 'pre-command-hook #'kmacro-step-edit-pre-command nil nil) |
| 1237 | (if kmacro-step-edit-key-index | 1240 | (if kmacro-step-edit-key-index |
| 1238 | (setq executing-kbd-macro-index kmacro-step-edit-key-index) | 1241 | (setq executing-kbd-macro-index kmacro-step-edit-key-index) |
| 1239 | (setq kmacro-step-edit-key-index executing-kbd-macro-index)))) | 1242 | (setq kmacro-step-edit-key-index executing-kbd-macro-index)))) |
| @@ -1256,9 +1259,9 @@ To customize possible responses, change the \"bindings\" in `kmacro-step-edit-ma | |||
| 1256 | (pre-command-hook pre-command-hook) | 1259 | (pre-command-hook pre-command-hook) |
| 1257 | (post-command-hook post-command-hook) | 1260 | (post-command-hook post-command-hook) |
| 1258 | (minibuffer-setup-hook minibuffer-setup-hook)) | 1261 | (minibuffer-setup-hook minibuffer-setup-hook)) |
| 1259 | (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil) | 1262 | (add-hook 'pre-command-hook #'kmacro-step-edit-pre-command nil) |
| 1260 | (add-hook 'post-command-hook 'kmacro-step-edit-post-command t) | 1263 | (add-hook 'post-command-hook #'kmacro-step-edit-post-command t) |
| 1261 | (add-hook 'minibuffer-setup-hook 'kmacro-step-edit-minibuf-setup t) | 1264 | (add-hook 'minibuffer-setup-hook #'kmacro-step-edit-minibuf-setup t) |
| 1262 | (call-last-kbd-macro nil nil) | 1265 | (call-last-kbd-macro nil nil) |
| 1263 | (when (and kmacro-step-edit-replace | 1266 | (when (and kmacro-step-edit-replace |
| 1264 | kmacro-step-edit-new-macro | 1267 | kmacro-step-edit-new-macro |
diff --git a/lisp/macros.el b/lisp/macros.el index 34e81f693f5..5583c02f68b 100644 --- a/lisp/macros.el +++ b/lisp/macros.el | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | ;;; macros.el --- non-primitive commands for keyboard macros | 1 | ;;; macros.el --- non-primitive commands for keyboard macros -*- lexical-binding:t -*- |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 1985-1987, 1992, 1994-1995, 2001-2017 Free Software | 3 | ;; Copyright (C) 1985-1987, 1992, 1994-1995, 2001-2017 Free Software |
| 4 | ;; Foundation, Inc. | 4 | ;; Foundation, Inc. |
| @@ -31,23 +31,10 @@ | |||
| 31 | 31 | ||
| 32 | ;;; Code: | 32 | ;;; Code: |
| 33 | 33 | ||
| 34 | (require 'kmacro) | ||
| 35 | |||
| 34 | ;;;###autoload | 36 | ;;;###autoload |
| 35 | (defun name-last-kbd-macro (symbol) | 37 | (defalias 'name-last-kbd-macro #'kmacro-name-last-macro) |
| 36 | "Assign a name to the last keyboard macro defined. | ||
| 37 | Argument SYMBOL is the name to define. | ||
| 38 | The symbol's function definition becomes the keyboard macro string. | ||
| 39 | Such a \"function\" cannot be called from Lisp, but it is a valid editor command." | ||
| 40 | (interactive "SName for last kbd macro: ") | ||
| 41 | (or last-kbd-macro | ||
| 42 | (user-error "No keyboard macro defined")) | ||
| 43 | (and (fboundp symbol) | ||
| 44 | (not (stringp (symbol-function symbol))) | ||
| 45 | (not (vectorp (symbol-function symbol))) | ||
| 46 | (user-error "Function %s is already defined and not a keyboard macro" | ||
| 47 | symbol)) | ||
| 48 | (if (string-equal symbol "") | ||
| 49 | (user-error "No command name given")) | ||
| 50 | (fset symbol last-kbd-macro)) | ||
| 51 | 38 | ||
| 52 | ;;;###autoload | 39 | ;;;###autoload |
| 53 | (defun insert-kbd-macro (macroname &optional keys) | 40 | (defun insert-kbd-macro (macroname &optional keys) |
| @@ -66,11 +53,7 @@ To save a kbd macro, visit a file of Lisp code such as your `~/.emacs', | |||
| 66 | use this command, and then save the file." | 53 | use this command, and then save the file." |
| 67 | (interactive (list (intern (completing-read "Insert kbd macro (name): " | 54 | (interactive (list (intern (completing-read "Insert kbd macro (name): " |
| 68 | obarray | 55 | obarray |
| 69 | (lambda (elt) | 56 | #'kmacro-keyboard-macro-p |
| 70 | (and (fboundp elt) | ||
| 71 | (or (stringp (symbol-function elt)) | ||
| 72 | (vectorp (symbol-function elt)) | ||
| 73 | (get elt 'kmacro)))) | ||
| 74 | t)) | 57 | t)) |
| 75 | current-prefix-arg)) | 58 | current-prefix-arg)) |
| 76 | (let (definition) | 59 | (let (definition) |
| @@ -137,6 +120,9 @@ use this command, and then save the file." | |||
| 137 | (prin1 char (current-buffer)) | 120 | (prin1 char (current-buffer)) |
| 138 | (princ (prin1-char char) (current-buffer)))) | 121 | (princ (prin1-char char) (current-buffer)))) |
| 139 | (insert ?\])) | 122 | (insert ?\])) |
| 123 | ;; FIXME: For kmacros, we shouldn't write the (lambda ...) | ||
| 124 | ;; gunk but instead we should write something more abstract like | ||
| 125 | ;; (kmacro-create [<keys>] 0 "%d"). | ||
| 140 | (prin1 definition (current-buffer)))) | 126 | (prin1 definition (current-buffer)))) |
| 141 | (insert ")\n") | 127 | (insert ")\n") |
| 142 | (if keys | 128 | (if keys |