Skip to content

Commit 7598b4b

Browse files
authored
Consider local keybindings when moving commands for the Motion state. (#517)
Instead of directly binding `H-j` to what was bound to `j`, for example, bind `H-j` to an interactive lambda which first checks whether there is a local binding for `j`. If there is, use that. If there is not, then call the orignal command bound to `j`. This should fix using the overrides in Magit buffers (see #493), where the command bound to `H-k` seems to be based on where point is located when `meow--save-origin-commands` is run. Co-authored-by: okamsn <[email protected]>
1 parent 6a12dc4 commit 7598b4b

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

meow-util.el

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,14 +452,34 @@ Looks up the state in meow-replace-state-name-list"
452452
(t nil)))
453453

454454
(defun meow--save-origin-commands ()
455+
"Save the commands overridden by the Motion map to modified bindings.
456+
457+
The new key binding, modified by the prefix in
458+
`meow-motion-remap-prefix', is bound to a command that calls the
459+
command locally bound to the original key binding, or, if that is
460+
nil, the original command.
461+
462+
For example, under the default and suggested settings, in a
463+
Magit status buffer, `k' could be bound to `meow-previous'
464+
and `H-k' would be bound to a command that would try
465+
to use the status buffer's original `k' binding at point."
455466
(cl-loop for key-code being the key-codes of meow-motion-state-keymap do
456467
(ignore-errors
457468
(let* ((key (meow--parse-input-event key-code))
458469
(cmd (key-binding (kbd key))))
459470
(when (and (commandp cmd)
460471
(not (equal cmd 'undefined)))
461472
(let ((rebind-key (concat meow-motion-remap-prefix key)))
462-
(local-set-key (kbd rebind-key) cmd)))))))
473+
(local-set-key (kbd rebind-key)
474+
(lambda ()
475+
(interactive)
476+
(call-interactively
477+
;; Local maps are those local to the buffer
478+
;; or a region of the buffer.
479+
(if-let ((local (lookup-key (current-local-map) key)))
480+
(or (command-remapping local)
481+
local)
482+
cmd))))))))))
463483

464484
(defun meow--prepare-region-for-kill ()
465485
(when (and (equal 'line (cdr (meow--selection-type)))

0 commit comments

Comments
 (0)