Skip to content

Commit

Permalink
Use loop with read-event in keypad
Browse files Browse the repository at this point in the history
  • Loading branch information
DogLooksGood committed Nov 30, 2024
1 parent dd79ccc commit a14349f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 91 deletions.
26 changes: 0 additions & 26 deletions meow-command.el
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))
Expand Down
37 changes: 5 additions & 32 deletions meow-keymap.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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 "<backspace>") 'meow-keypad-undo)
(define-key map (kbd "<escape>") 'meow-keypad-quit)
(define-key map [remap keyboard-quit] 'meow-keypad-quit)
(define-key map (kbd "<deletechar>") 'meow-keypad-self-insert)
(define-key map (kbd "<tab>") 'meow-keypad-self-insert)
(define-key map (kbd "TAB") 'meow-keypad-self-insert)
(define-key map (kbd "<return>") 'meow-keypad-self-insert)
(define-key map (kbd "<up>") 'meow-keypad-self-insert)
(define-key map (kbd "<down>") 'meow-keypad-self-insert)
(define-key map (kbd "<left>") 'meow-keypad-self-insert)
(define-key map (kbd "<right>") 'meow-keypad-self-insert)
(define-key map (kbd "<home>") 'meow-keypad-self-insert)
(define-key map (kbd "<end>") 'meow-keypad-self-insert)
(define-key map (kbd "<next>") 'meow-keypad-self-insert)
(define-key map (kbd "<prior>") 'meow-keypad-self-insert)
(define-key map (kbd "<insert>") '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 "<backspace>") 'meow-keypad-undo)
(define-key map (kbd "<escape>") 'meow-keypad-quit)
(define-key map (kbd "ESC") 'meow-keypad-quit)

Check failure on line 81 in meow-keymap.el

View workflow job for this annotation

GitHub Actions / check (26.3, true)

This key sequence is reserved (see Key Binding Conventions in the Emacs Lisp manual)

Check failure on line 81 in meow-keymap.el

View workflow job for this annotation

GitHub Actions / check (27.1, true)

This key sequence is reserved (see Key Binding Conventions in the Emacs Lisp manual)

Check failure on line 81 in meow-keymap.el

View workflow job for this annotation

GitHub Actions / check (28.2, true)

This key sequence is reserved (see Key Binding Conventions in the Emacs Lisp manual)
(define-key map [remap keyboard-quit] 'meow-keypad-quit)
map)
"Keymap for Meow keypad state.")

Expand All @@ -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
Expand Down
91 changes: 60 additions & 31 deletions meow-keypad.el
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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."
Expand All @@ -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
Expand Down Expand Up @@ -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."
Expand All @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions meow-shims.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions meow-var.el
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down

0 comments on commit a14349f

Please sign in to comment.