Skip to content

Commit 33414b9

Browse files
DogLooksGoodDogLooksGood
andauthored
Remove motion remap (#705)
* Remove motion remap * Fix keypad handle for non-ascii keys * Add global-map to transparent keypad leader * Use meow--keypad-execute instead of call-interactively * Update doc & changelog * Remove the usage on keymap-lookup * Improve command lookup --------- Co-authored-by: DogLooksGood <doglooksgood@gmail.com>
1 parent af3f10e commit 33414b9

File tree

9 files changed

+79
-119
lines changed

9 files changed

+79
-119
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## Master
4+
5+
### Breaking Changes
6+
7+
* replace motion remapping with transparent keypad leader.
8+
9+
The `meow-motion-overwrite-define-key` has been changed to `meow-motion-define-key`. and now it
10+
works just like other key binding helpers. Users don't have to bind a key to remapped the
11+
keybinding. Now the keypad leader is transparent when a key is not bound in motion state. So that
12+
users can still access the original command with `SPC <original-key>`.
13+
314
## 1.5.0 (2024-10-20)
415

516
### Features

CUSTOMIZATIONS.org

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Define key bindings in a state.
2525

2626
Similar to ~meow-define-keys~. Define key in NORMAL state.
2727

28+
** meow-motion-define-key
29+
30+
Similar to ~meow-define-keys~. Define key in MOTION state.
31+
2832
** meow-leader-define-key
2933

3034
Similar to ~meow-define-keys~. Define key in leader keymap.
@@ -33,14 +37,6 @@ The keymap used is the one listed in ~meow-keymap-alist~.
3337

3438
By default, it is ~mode-specific-map~.
3539

36-
** meow-motion-overwrite-define-key
37-
38-
Similar to ~meow-define-keys~. Define key in MOTION state.
39-
40-
Meow will remap overwritten commands to a keybinding with *HYPER* modifier.
41-
42-
For example, if you define ~j~ as ~C-N~ , the original command on ~j~ will be bound to ~H-j~.
43-
4440
** meow-setup-indicator
4541

4642
A helper function that puts an indicator at the beginning of mode-line. If you want customize mode-line by hand, see `meow-indicator`.
@@ -313,17 +309,27 @@ Default: ~select~
313309

314310
The type of selection activated by ~meow-expand-*~ commands.
315311

316-
** meow-motion-remap-prefix
312+
** meow-keypad-leader-dispatch
313+
314+
Default: ~nil~
315+
316+
The fallback dispatching in KEYPAD when there's no translation.
317+
318+
The value can be either a string or a keymap:
319+
320+
A keymap stands for a base keymap used for further translation. A string stands for finding the
321+
keymap at a specified key binding. Nil stands for taking leader keymap from ~meow-keymap-alist~.
322+
323+
** meow-keypad-leader-transparent
317324

318-
Default: "H-"
325+
Default: ~motion~
319326

320-
The prefix string used when remapping an occupied key in MOTION state.
327+
Use transparent behaivor when a bound command is not found in leader dispatch.
321328

322-
For examples:
323-
#+begin_example
324-
"C-x C-v" will remap the occupied j to C-x C-v j.
325-
"C-M-" will remap the occupied j to C-M-j.
326-
#+end_example
329+
- Value ~t~ stands for always be transparent.
330+
- Value ~motion~ stands for only be transparent in MOTION state.
331+
- Value ~normal~ stands for only be transparent in NORMAL state.
332+
- Value ~nil~ stands for never be transparent."
327333

328334
** meow-state-mode-alist
329335
Association list of symbols of meow states to their corresponding mode functions.

TUTORIAL.org

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,15 @@ MOTION state is the default state for special modes, like ~dired~, ~proced~, etc
6666
By default, MOTION state has no keybindings, except ~SPC~ is used as the leader key. The original command on ~SPC~ can be access via ~SPC SPC~.
6767

6868
Users may want a consistent keybindings for movement globally, for example: k/j to move up/down. Usually, you can bind k/j in MOTION state,
69-
however the original command on k/j is not accessible. To solve the problem, Meow introduce a simple solution.
70-
71-
For any keybinding you defined with ~meow-motion-overwrite-define-key~, the overwritten command will be bound to the key with ~hyper~ modifier.
69+
however then the original command on k/j is not accessible. To access the original command on k/j, use ~SPC k/j~.
7270

7371
Here's an example: you want use ~j~ as ~next-line~ globally.
7472

7573
#+begin_src emacs-lisp
7674
(meow-motion-overwrite-define-key '("j" . next-line))
77-
(meow-leader-define-key '("j" . "H-j"))
78-
#+end_src
79-
80-
Now ~j~ will act as ~next-line~, the original command on ~j~ (e.g. ~dired-goto-file~ in ~dired~) will be bound to ~H-j~.
81-
Since we use ~j~ in the leader for ~H-j~, now we can call it via ~SPC j~.
82-
83-
It is also possible to route the original command(e.g. ~j~) to original motion key(e.g. ~n~).
84-
85-
#+begin_src emacs-lisp
86-
(meow-motion-overwrite-define-key '("j" . next-line))
87-
(meow-motion-overwrite-define-key '("n" . "H-j"))
8875
#+end_src
8976

90-
Now you have swapped ~j~ and ~n~.
91-
92-
The settings we made has nothing to do with the name of a command. Thus, it works in all special modes.
77+
The settings we made works in all special modes.
9378

9479
** KEYPAD
9580

meow-core.el

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,13 @@ test on the commands bound to the keys a-z. If any of the command
129129
names contains \"self-insert\", then NORMAL state will be used.
130130
Otherwise, MOTION state will be used.
131131
132-
Before turning on MOTION state, the original commands will be
133-
remapped. The new keybinding is generated by prepending
134-
`meow-motion-remap-prefix' to the original keybinding.
135-
136132
Note: When this function is called, NORMAL state is already
137133
enabled. NORMAL state is enabled globally when
138134
`meow-global-mode' is used, because in `fundamental-mode',
139135
there's no chance for meow to call an init function."
140-
(let ((state (meow--mode-get-state))
141-
(motion (lambda ()
142-
(meow--disable-current-state)
143-
(meow--save-origin-commands)
144-
(meow-motion-mode 1))))
145-
(cond
146-
;; if MOTION is specified
147-
((eq state 'motion)
148-
(funcall motion))
149-
150-
(state
151-
(meow--disable-current-state)
152-
(meow--switch-state state t)))))
136+
(let ((state (meow--mode-get-state)))
137+
(meow--disable-current-state)
138+
(meow--switch-state state t)))
153139

154140
(defun meow--disable ()
155141
"Disable Meow."

meow-helpers.el

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ with this helper, it will result in recursive calls.
9494
Check `meow-normal-define-key' for usages."
9595
(apply #'meow-define-keys 'leader keybinds))
9696

97-
;; Remap Leader SPC
98-
(meow-leader-define-key (cons "SPC" (concat meow-motion-remap-prefix "SPC")))
99-
100-
(defun meow-motion-overwrite-define-key (&rest keybinds)
97+
(defun meow-motion-define-key (&rest keybinds)
10198
"Define key for MOTION state.
10299
103100
Check `meow-normal-define-key' for usages."
104101
(apply #'meow-define-keys 'motion keybinds))
105102

103+
(defalias 'meow-motion-overwrite-define-key 'meow-motion-define-key)
104+
(make-obsolete 'meow-motion-overwrite-define-key 'meow-motion-define-key "1.6.0")
105+
106106
(defun meow-setup-line-number ()
107107
(add-hook 'display-line-numbers-mode-hook #'meow--toggle-relative-line-number)
108108
(add-hook 'meow-insert-mode-hook #'meow--toggle-relative-line-number))

meow-keypad.el

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
;;
3131
;;; Code:
3232

33+
(require 'cl-lib)
3334
(require 'subr-x)
3435
(require 'meow-var)
3536
(require 'meow-util)
@@ -66,9 +67,7 @@
6667
(let* ((keybind (if meow--keypad-base-keymap
6768
(lookup-key meow--keypad-base-keymap keys)
6869
(key-binding keys))))
69-
(unless (and (meow--is-self-insertp keybind)
70-
(not meow-keypad-self-insert-undefined))
71-
keybind)))
70+
keybind))
7271

7372
(defun meow--keypad-has-sub-meta-keymap-p ()
7473
"Check if there's a keymap belongs to Meta prefix.
@@ -408,8 +407,8 @@ If there are beacons, execute it at every beacon."
408407
(defun meow--keypad-try-execute ()
409408
"Try execute command, return t when the translation progress can be ended.
410409
411-
If there is a command available on the current key binding,
412-
try replacing the last modifier and try again."
410+
This function supports a fallback behavior, where it allows to use `SPC
411+
x f' to execute `C-x C-f' or `C-x f' when `C-x C-f' is not bound."
413412
(unless (or meow--use-literal
414413
meow--use-meta
415414
meow--use-both)
@@ -439,23 +438,32 @@ try replacing the last modifier and try again."
439438
(meow--keypad-try-execute))
440439
(t
441440
(setq meow--prefix-arg nil)
442-
(message "%s is undefined" (meow--keypad-format-keys nil))
443441
(meow--keypad-quit)
442+
(if (or (eq t meow-keypad-leader-transparent)
443+
(eq meow--keypad-previous-state meow-keypad-leader-transparent))
444+
(let* ((key (meow--parse-input-event last-input-event))
445+
(origin-cmd (cl-some (lambda (m)
446+
(lookup-key m key))
447+
(current-active-maps)))
448+
(remapped-cmd (command-remapping origin-cmd))
449+
(cmd-to-call (if (member remapped-cmd '(undefined nil))
450+
(or origin-cmd 'undefined)
451+
remapped-cmd)))
452+
(meow--keypad-execute cmd-to-call))
453+
(message "%s is undefined" key-str))
444454
t)))))
445455

446456
(defun meow--keypad-handle-input-with-keymap (input-event)
447457
"Handle INPUT-EVENT with `meow-keypad-state-keymap'.
448458
449459
Return t if handling is completed."
450-
(if (numberp input-event)
451-
(let* ((k (if (= 27 input-event)
452-
[escape]
453-
(kbd (single-key-description input-event))))
454-
(cmd (lookup-key meow-keypad-state-keymap k)))
455-
(if cmd
456-
(call-interactively cmd)
457-
(meow--keypad-handle-input-event input-event)))
458-
(meow--keypad-quit)))
460+
(if (equal 'escape last-input-event)
461+
(meow--keypad-quit)
462+
(setq last-command-event last-input-event)
463+
(let ((kbd (single-key-description input-event)))
464+
(if-let* ((cmd (lookup-key meow-keypad-state-keymap (read-kbd-macro kbd))))
465+
(call-interactively cmd)
466+
(meow--keypad-handle-input-event input-event)))))
459467

460468
(defun meow--keypad-handle-input-event (input-event)
461469
"Handle the INPUT-EVENT.

meow-tutor.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@
569569
added to your search if the selection is created with \\[meow-visit], \\[meow-mark-word] and \\[meow-mark-symbol].
570570
571571
=================================================================
572-
= KEYPAD AND MOTION MODE =
572+
= KEYPAD =
573573
=================================================================
574574
575575
One of the most notable features of Meow is the Keypad. It

meow-util.el

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -470,41 +470,6 @@ Looks up the state in meow-replace-state-name-list"
470470
(format "<%s>" e))
471471
(t nil)))
472472

473-
(defun meow--save-origin-commands ()
474-
"Save the commands overridden by the Motion map to modified bindings.
475-
476-
The new key binding, modified by the prefix in
477-
`meow-motion-remap-prefix', is bound to a command that calls the
478-
command locally bound to the original key binding, or, if that is
479-
nil, the original command.
480-
481-
For example, under the default and suggested settings, in a
482-
Magit status buffer, `k' could be bound to `meow-previous'
483-
and `H-k' would be bound to a command that would try
484-
to use the status buffer's original `k' binding at point."
485-
(cl-loop for key-code being the key-codes of meow-motion-state-keymap do
486-
(ignore-errors
487-
(let* ((key (meow--parse-input-event key-code))
488-
(cmd (key-binding (kbd key))))
489-
(when (and (commandp cmd)
490-
(not (equal cmd 'undefined)))
491-
(let ((rebind-key (concat meow-motion-remap-prefix key)))
492-
(local-set-key (kbd rebind-key)
493-
(lambda ()
494-
(interactive)
495-
;; Local maps are those local to the buffer
496-
;; or a region of the buffer.
497-
(let* ((local (lookup-key (current-local-map) key))
498-
(remapped (command-remapping local)))
499-
(call-interactively
500-
(cond
501-
((commandp remapped)
502-
remapped)
503-
((commandp local)
504-
local)
505-
(t
506-
cmd))))))))))))
507-
508473
(defun meow--prepare-region-for-kill ()
509474
(when (and (equal 'line (cdr (meow--selection-type)))
510475
(meow--direction-forward-p)

meow-var.el

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,6 @@ Each item is a (THING FORWARD_SYNTAX_TO_INCLUDE BACKWARD-SYNTAX_TO_INCLUDE)."
134134
:group 'meow
135135
:type 'boolean)
136136

137-
(defcustom meow-keypad-self-insert-undefined t
138-
"Whether to self-insert a key in keypad mode if it is undefined"
139-
:group 'meow
140-
:type 'boolean)
141-
142137
(defcustom meow-char-thing-table
143138
'((?r . round)
144139
(?s . square)
@@ -282,6 +277,19 @@ Set to `t' to always update.
282277
:type '(choice (const select)
283278
(const expand)))
284279

280+
(defcustom meow-keypad-leader-transparent 'motion
281+
"Use transparent behaivor when a bound command is not found in leader dispatch.
282+
283+
Value `t' stands for always be transparent.
284+
Value `motion' stands for only be transparent in MOTION state.
285+
Value `normal' stands for only be transparent in NORMAL state.
286+
Value `nil' stands for never be transparent."
287+
:group 'meow
288+
:type '(choice (const t :tag "Always be transparent")
289+
(const motion :tag "Transparent only in MOTION state")
290+
(const normal :tag "Transparent only in NORMAL state")
291+
(const nil :tag "Never be transparent")))
292+
285293
(defcustom meow-keypad-leader-dispatch nil
286294
"The fallback dispatching in KEYPAD when there's no translation.
287295
@@ -320,15 +328,6 @@ prompted to finish the command."
320328
:type '(alist :key-type (character :tag "From")
321329
:value-type (character :tag "To")))
322330

323-
(defcustom meow-motion-remap-prefix "H-"
324-
"The prefix string used when remapping an occupied key in MOTION state.
325-
326-
For examples:
327-
\"C-x C-v\" will remap the occupied j to C-x C-v j.
328-
\"C-M-\" will remap the occupied j to C-M-j."
329-
:group 'meow
330-
:type 'string)
331-
332331
(defcustom meow-goto-line-function nil
333332
"Function to use in `meow-goto-line'.
334333

0 commit comments

Comments
 (0)