From a14349f7ed4d5d7987502fa4646fe20ddfe1100e Mon Sep 17 00:00:00 2001 From: DogLooksGood Date: Sat, 30 Nov 2024 17:26:08 +0800 Subject: [PATCH] Use loop with read-event in keypad --- meow-command.el | 26 -------------- meow-keymap.el | 37 +++----------------- meow-keypad.el | 91 ++++++++++++++++++++++++++++++++----------------- meow-shims.el | 7 ++-- meow-var.el | 3 ++ 5 files changed, 73 insertions(+), 91 deletions(-) diff --git a/meow-command.el b/meow-command.el index 448b59d..b7d2581 100644 --- a/meow-command.el +++ b/meow-command.el @@ -1724,30 +1724,6 @@ Use negative argument for backward application." (goto-char (max (point) (overlay-end ov)))) (delete-overlay ov)))))))) -(defun meow-start-kmacro () - "Start kmacro. - -This command is a replacement for built-in `kmacro-start-macro'." - (interactive) - (cond - ((or (meow-normal-mode-p) (meow-motion-mode-p)) - (call-interactively #'kmacro-start-macro)) - (t - (message "Can only start recording in NORMAL or MOTION state.")))) - -(defun meow-start-kmacro-or-insert-counter () - "Start kmacro or insert counter. - -This command is a replacement for built-in - `kmacro-start-macro-or-insert-counter'." - (interactive) - (cond - ((or defining-kbd-macro executing-kbd-macro) - (call-interactively #'kmacro-insert-counter)) - ((or (meow-normal-mode-p) (meow-motion-mode-p)) - (call-interactively #'kmacro-start-macro-or-insert-counter)) - (t (message "Can only start recording in NORMAL or MOTION state.")))) - (defun meow-end-or-call-kmacro () "End kmacro recording or call macro. @@ -1771,8 +1747,6 @@ This command is a replacement for built-in `kmacro-end-or-call-macro'." This command is a replacement for built-in `kmacro-end-macro'." (interactive) (cond - (meow--keypad-this-command - (message "Can't end kmacro with KEYPAD command")) ((or (meow-normal-mode-p) (meow-motion-mode-p)) (call-interactively #'kmacro-end-or-call-macro)) diff --git a/meow-keymap.el b/meow-keymap.el index 0aca757..e58c853 100644 --- a/meow-keymap.el +++ b/meow-keymap.el @@ -33,8 +33,6 @@ (defvar meow-insert-state-keymap (let ((keymap (make-keymap))) (define-key keymap [escape] 'meow-insert-exit) - (define-key keymap [remap kmacro-start-macro] #'meow-start-kmacro) - (define-key keymap [remap kmacro-start-macro-or-insert-counter] #'meow-start-kmacro-or-insert-counter) (define-key keymap [remap kmacro-end-or-call-macro] #'meow-end-or-call-kmacro) (define-key keymap [remap kmacro-end-macro] #'meow-end-kmacro) keymap) @@ -60,8 +58,6 @@ (define-key keymap (kbd "SPC") 'meow-keypad) (define-key keymap (kbd "i") 'meow-insert) (define-key keymap (kbd "a") 'meow-append) - (define-key keymap [remap kmacro-start-macro] #'meow-start-kmacro) - (define-key keymap [remap kmacro-start-macro-or-insert-counter] #'meow-start-kmacro-or-insert-counter) (define-key keymap [remap kmacro-end-or-call-macro] #'meow-end-or-call-kmacro) (define-key keymap [remap kmacro-end-macro] #'meow-end-kmacro) keymap) @@ -77,33 +73,13 @@ (defvar meow-keypad-state-keymap (let ((map (make-sparse-keymap))) (suppress-keymap map t) - (define-key map [remap self-insert-command] 'meow-keypad-self-insert) - (define-key map [remap kmacro-start-macro] #'meow-start-kmacro) - (define-key map [remap kmacro-start-macro-or-insert-counter] #'meow-start-kmacro-or-insert-counter) (define-key map [remap kmacro-end-or-call-macro] #'meow-end-or-call-kmacro) (define-key map [remap kmacro-end-macro] #'meow-end-kmacro) - (let ((i ?\s)) - (while (< i 256) - (define-key map (vector i) 'meow-keypad-self-insert) - (setq i (1+ i))) - (define-key map (kbd "DEL") 'meow-keypad-undo) - (define-key map (kbd "") 'meow-keypad-undo) - (define-key map (kbd "") 'meow-keypad-quit) - (define-key map [remap keyboard-quit] 'meow-keypad-quit) - (define-key map (kbd "") 'meow-keypad-self-insert) - (define-key map (kbd "") 'meow-keypad-self-insert) - (define-key map (kbd "TAB") 'meow-keypad-self-insert) - (define-key map (kbd "") 'meow-keypad-self-insert) - (define-key map (kbd "") 'meow-keypad-self-insert) - (define-key map (kbd "") 'meow-keypad-self-insert) - (define-key map (kbd "") 'meow-keypad-self-insert) - (define-key map (kbd "") 'meow-keypad-self-insert) - (define-key map (kbd "") 'meow-keypad-self-insert) - (define-key map (kbd "") 'meow-keypad-self-insert) - (define-key map (kbd "") 'meow-keypad-self-insert) - (define-key map (kbd "") 'meow-keypad-self-insert) - (define-key map (kbd "") 'meow-keypad-self-insert) - (define-key map (kbd "RET") 'meow-keypad-self-insert)) + (define-key map (kbd "DEL") 'meow-keypad-undo) + (define-key map (kbd "") 'meow-keypad-undo) + (define-key map (kbd "") 'meow-keypad-quit) + (define-key map (kbd "ESC") 'meow-keypad-quit) + (define-key map [remap keyboard-quit] 'meow-keypad-quit) map) "Keymap for Meow keypad state.") @@ -123,9 +99,6 @@ (define-key map [remap kmacro-end-or-call-macro] 'meow-beacon-apply-kmacro) (define-key map [remap kmacro-start-macro-or-insert-counter] 'meow-beacon-start) (define-key map [remap kmacro-start-macro] 'meow-beacon-start) - - (define-key map [remap meow-start-kmacro] 'meow-beacon-start) - (define-key map [remap meow-start-kmacro-or-insert-counter] 'meow-beacon-start) (define-key map [remap meow-end-or-call-kmacro] 'meow-beacon-apply-kmacro) ;; noops diff --git a/meow-keypad.el b/meow-keypad.el index fd71f53..52736d7 100644 --- a/meow-keypad.el +++ b/meow-keypad.el @@ -118,7 +118,10 @@ meow--use-both nil meow--keypad-help nil) (setq overriding-local-map nil) - (meow--exit-keypad-state)) + (meow--keypad-clear-message) + (meow--exit-keypad-state) + ;; Return t to indicate the keypad loop should be stopped + t) (defun meow-keypad-quit () "Quit keypad state." @@ -222,6 +225,9 @@ keymap) km))))))) +(defun meow--keypad-clear-message () + (funcall meow-keypad-clear-describe-keymap-function)) + (defun meow--keypad-display-message () (let (overriding-local-map) (when meow-keypad-describe-keymap-function @@ -368,24 +374,30 @@ Returning DEF will result in a generated title." (propertize pre 'face 'font-lock-comment-face))) (propertize (meow--keypad-format-keys nil) 'face 'font-lock-string-face)))) +(defun meow--keypad-in-beacon-p () + (and (meow--beacon-inside-secondary-selection) + meow--beacon-overlays)) + (defun meow--keypad-execute (command) "Execute the COMMAND. If there are beacons, execute it at every beacon." - (cond - ((and meow-keypad-execute-on-beacons - (not defining-kbd-macro) - (not executing-kbd-macro) - (meow--beacon-inside-secondary-selection) - meow--beacon-overlays) - (call-interactively command) - (meow--beacon-apply-command command)) - (t - (call-interactively command)))) + (if (meow--keypad-in-beacon-p) + (cond + ((member command '(kmacro-start-macro kmacro-start-macro-or-insert-counter)) + (call-interactively 'meow-beacon-start)) + ((member command '(kmacro-end-macro meow-end-kmacro)) + (call-interactively 'meow-beacon-end-and-apply-kmacro)) + ((and (not defining-kbd-macro) + (not executing-kbd-macro) + meow-keypad-execute-on-beacons) + (call-interactively command) + (meow--beacon-apply-command command))) + (call-interactively command))) (defun meow--keypad-try-execute () - "Try execute command. + "Try execute command, return t when the translation progress can be ended. If there is a command available on the current key binding, try replacing the last modifier and try again." @@ -401,28 +413,36 @@ try replacing the last modifier and try again." (if meow--keypad-help (progn (meow--keypad-quit) - (describe-function cmd)) + (describe-function cmd) + t) (let ((meow--keypad-this-command cmd)) (meow--keypad-quit) (setq real-this-command cmd this-command cmd) - (meow--keypad-execute cmd)))) + (meow--keypad-execute cmd) + t))) ((keymapp cmd) (when meow-keypad-message (meow--keypad-show-message)) - (meow--keypad-display-message)) + (meow--keypad-display-message) + nil) ((equal 'control (caar meow--keypad-keys)) (setcar meow--keypad-keys (cons 'literal (cdar meow--keypad-keys))) (meow--keypad-try-execute)) (t (setq meow--prefix-arg nil) (message "%s is undefined" (meow--keypad-format-keys nil)) - (meow--keypad-quit)))))) - -(defun meow-keypad-self-insert () - "Default command when keypad state is enabled." - (interactive) - (setq this-command last-command) - (when-let* ((e (meow--event-key last-input-event)) + (meow--keypad-quit) + t))))) + +(defun meow--keypad-handle-input-with-keymap (input-event) + (let ((cmd (lookup-key meow-keypad-state-keymap (vector input-event)))) + (if cmd + (call-interactively cmd) + (meow--keypad-handle-input-event input-event)))) + +(defun meow--keypad-handle-input-event (input-event) + (meow--keypad-clear-message) + (when-let* ((e (meow--event-key input-event)) (key (meow--parse-input-event e))) (let ((has-sub-meta (meow--keypad-has-sub-meta-keymap-p))) (cond @@ -468,18 +488,15 @@ try replacing the last modifier and try again." meow--use-both) (progn (when meow-keypad-message (meow--keypad-show-message)) - (meow--keypad-display-message)) + (meow--keypad-display-message) + nil) (meow--keypad-try-execute)))) -(defun meow-keypad () - "Enter keypad state." +(defun meow-keypad-self-insert () + "Default command when keypad state is enabled." (interactive) (setq this-command last-command) - (setq meow--keypad-previous-state (meow--current-state)) - (meow--switch-state 'keypad) - (setq overriding-local-map meow-keypad-state-keymap - overriding-terminal-local-map nil) - (meow--keypad-display-message)) + (meow--keypad-handle-input-event last-input-event)) (defun meow-keypad-start () "Enter keypad state with current input as initial key sequences." @@ -490,7 +507,19 @@ try replacing the last modifier and try again." (setq overriding-local-map meow-keypad-state-keymap overriding-terminal-local-map nil meow--keypad-allow-quick-dispatch nil) - (call-interactively 'meow-keypad-self-insert)) + (meow--keypad-handle-input-event last-input-event) + (while (not (meow--keypad-handle-input-with-keymap (read-key))))) + +(defun meow-keypad () + "Enter keypad state and convert inputs." + (interactive) + (setq this-command last-command) + (setq meow--keypad-previous-state (meow--current-state)) + (meow--switch-state 'keypad) + (setq overriding-local-map meow-keypad-state-keymap + overriding-terminal-local-map nil + meow--keypad-allow-quick-dispatch t) + (while (not (meow--keypad-handle-input-with-keymap (read-key))))) (defun meow-keypad-start-with (input) "Enter keypad state with INPUT. diff --git a/meow-shims.el b/meow-shims.el index 6405f91..0767aac 100644 --- a/meow-shims.el +++ b/meow-shims.el @@ -396,8 +396,11 @@ Argument ENABLE non-nil means turn on." (if which-key-mode (setq meow-keypad-describe-keymap-function (lambda (keymap) - (which-key--create-buffer-and-show nil keymap nil (concat "Meow: " (meow--keypad-format-keys))))) - (setq meow-keypad-describe-keymap-function 'meow-describe-keymap))) + (which-key--create-buffer-and-show nil keymap nil (concat "Meow: " (meow--keypad-format-keys)))) + meow-keypad-clear-describe-keymap-function 'which-key--hide-popup) + + (setq meow-keypad-describe-keymap-function 'meow-describe-keymap + meow-keypad-clear-describe-keymap-function nil))) (defun meow--setup-which-key (enable) (setq meow--which-key-setup enable) diff --git a/meow-var.el b/meow-var.el index 2debe76..da44f0e 100644 --- a/meow-var.el +++ b/meow-var.el @@ -353,6 +353,9 @@ Currently, keypad is not working well with which-key, so Meow ships a default `meow-describe-keymap'. Use (setq meow-keypad-describe-keymap-function \\='nil) to disable popup.") +(defvar meow-keypad-clear-describe-keymap-function nil + "The function used to clear the effect of `meow-keypad-describe-keymap-function'.") + (defvar meow-keypad-get-title-function 'meow-keypad-get-title "The function used to get the title of a keymap or command.")