diff --git a/lisp/repeat.el b/lisp/repeat.el index ee9e14b515..f4526c20f4 100644 --- a/lisp/repeat.el +++ b/lisp/repeat.el @@ -390,7 +390,10 @@ repeat-mode See `describe-repeat-maps' for a list of all repeatable command." :global t :group 'convenience (if (not repeat-mode) - (remove-hook 'post-command-hook 'repeat-post-hook) + (progn + (remove-hook 'pre-command-hook 'repeat-pre-hook) + (remove-hook 'post-command-hook 'repeat-post-hook)) + (add-hook 'pre-command-hook 'repeat-pre-hook) (add-hook 'post-command-hook 'repeat-post-hook) (let* ((keymaps nil) (commands (all-completions @@ -402,27 +405,60 @@ repeat-mode (length commands) (length (delete-dups keymaps)))))) -(defun repeat-post-hook () - "Function run after commands to set transient keymap for repeatable keys." - (let ((was-in-progress repeat-in-progress)) - (setq repeat-in-progress nil) +(defun repeat-map () + "Return a map for keys repeatable after the current command." (when repeat-mode (let ((rep-map (or repeat-map (and (symbolp real-this-command) (get real-this-command 'repeat-map))))) (when rep-map - (when (boundp rep-map) + (when (and (symbolp rep-map) (boundp rep-map)) (setq rep-map (symbol-value rep-map))) - (let ((map (copy-keymap rep-map))) + (if repeat-exit-key + ;; `repeat-exit-key' modifies the map by adding keys + (copy-keymap rep-map) + rep-map))))) + +(defun repeat-map-valid (map) + "Check if MAP can be used for the next command. +Can contain more conditions." + (and map + ;; Avoid using repeatable keys when minibuffer prompt pops up + (zerop (minibuffer-depth)) ;; Exit when the last char is not among repeatable keys, ;; so e.g. `C-x u u' repeats undo, whereas `C-/ u' doesn't. - (when (and (zerop (minibuffer-depth)) ; avoid remapping in prompts - (or (lookup-key map (this-command-keys-vector)) - prefix-arg)) + (or (lookup-key map (vector last-nonmenu-event)) + ;; `prefix-arg' can affect next repeatable commands + ;; (and repeat-keep-prefix prefix-arg) + ))) + +(defun repeat-pre-hook () + "Function run before commands to handle repeatable keys." + ;; Reset prefix-arg before the next non-repeatable command, + ;; e.g. `C-- C-x o o C-n' or `C-x o C-- o C-n', so `C-n' + ;; should not use `prefix-arg' to go in opposite direction. + (when (and repeat-keep-prefix prefix-arg repeat-in-progress) + (let ((map (repeat-map))) + (if map + ;; Optimize to use less logic in `repeat-map' + ;; when called again from `repeat-post-hook' + (setq repeat-map map) + ;; When `repeat-post-hook' will exit the repeatable sequence, + ;; this means the current command is not repeatable, + ;; so reset `prefix-arg' enabled for repeatable commands only. + (setq prefix-arg nil))))) + +(defun repeat-post-hook () + "Function run after commands to set transient keymap for repeatable keys." + (let ((was-in-progress repeat-in-progress)) + (setq repeat-in-progress nil) + + (let ((map (repeat-map))) + (when (repeat-map-valid map) ;; Messaging - (unless prefix-arg + (unless prefix-arg ;; Don't overwrite prefix arg echo (funcall repeat-echo-function map)) ;; Adding an exit key @@ -446,7 +482,7 @@ repeat-post-hook (lambda () (setq repeat-in-progress nil) (funcall exitfun) - (funcall repeat-echo-function nil))))))))))) + (funcall repeat-echo-function nil)))))))) (setq repeat-map nil) (when (and was-in-progress (not repeat-in-progress))