Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make leader key descriptions in the keymap itself #7339

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

LemonBreezes
Copy link
Contributor

@LemonBreezes LemonBreezes commented Aug 6, 2023

This commit moves leader key descriptions from which-key-replacement-alist to the keymap itself. This helps with performance because that way, which-key does not have to calculate every single leader key description for each keypress. Furthermore, this fixes issues like #1413 where relocating leader keymaps resulted in which-key not showing the correct leader key descriptions.

I also made the leader prefix maps have their own prefix commands by populating the function slot of the keymap variables. This is an Emacs convention. I made doom-leader-map follow this convention as well, though I kept the doom/leader so as to not break existing configs.

@LemonBreezes LemonBreezes changed the title fix: make leader key descriptions in the keymap fix: make leader key descriptions in the keymap itself Aug 6, 2023
@LemonBreezes LemonBreezes force-pushed the make-leader-key-descriptions-in-keymap branch 2 times, most recently from 6cf64d9 to caff7de Compare August 6, 2023 20:31
@LemonBreezes LemonBreezes marked this pull request as ready for review August 6, 2023 20:40
@LemonBreezes LemonBreezes requested a review from a team as a code owner August 6, 2023 20:40
@LemonBreezes LemonBreezes force-pushed the make-leader-key-descriptions-in-keymap branch 3 times, most recently from 4a967eb to 4860b33 Compare August 9, 2023 02:47
@LemonBreezes
Copy link
Contributor Author

LemonBreezes commented Aug 9, 2023

So I removed the doom/leader line because ot broke my config somehow. Dunno why. Also, this doesn't make the localleader map relocatable but I'll look into that eventually.

@LemonBreezes
Copy link
Contributor Author

LemonBreezes commented Aug 13, 2023

Now you can define any key to doom-localleader-map symbol and it will work as expected. The which-key descriptions will work as expected too. The only caveat is that the localleader map is now updated using hooks so there the potential for bugs where the incorrect map is selected.

Basically, now doing

(map! :desc "<leader>" :nmv "DEL" #'doom-leader-map
      :desc "<leader>" :nmv "<backspace>" #'doom-leader-map)

works as expected with respect to the which-key descriptions and the local leader map.

@LemonBreezes LemonBreezes force-pushed the make-leader-key-descriptions-in-keymap branch from 626d679 to 11163bc Compare August 21, 2023 02:11
@LemonBreezes
Copy link
Contributor Author

I just pushed an update to exclude some minibuffer modes from doom-init-localleader-key-h and to also keep track of which major mode is being used by doom-localleader-key with doom-localleader-current-major-mode.

This commit moves leader key descriptions from
`which-key-replacement-alist` to the keymap itself. This helps with
performance because that way, which-key does not have to calculate every
single leader key description for each keypress. Furthermore, this fixes
issues like doomemacs#1413 where relocating leader keymaps resulted in which-key
not showing the correct leader key descriptions.

I also made the leader prefix maps have their own prefix commands by
populating the function slot of the keymap variables. This is an Emacs
convention. I made `doom-leader-map` follow this convention as well.
With this commit, now you can define any key to `doom-locallleader-map`
symbol and it will work as expected. The `which-key` descriptions will
work as expected too. The only caveat is that the localleader map is now
updated using hooks so there the potential for bugs where the incorrect
map is selected.
@LemonBreezes LemonBreezes force-pushed the make-leader-key-descriptions-in-keymap branch from 11163bc to afaf293 Compare August 26, 2023 23:17
@LemonBreezes
Copy link
Contributor Author

Okay. I am currently testing using doom-unreal-buffer-p instead of pattern-matching on modes for doom-init-localleader-key-h.

@LemonBreezes LemonBreezes force-pushed the make-leader-key-descriptions-in-keymap branch from adee892 to 8429b60 Compare September 24, 2023 03:43
@LemonBreezes
Copy link
Contributor Author

Hi. I have updated this to use post-command-hook and made a backwards compatibility fix. This PR is ready for review now.

(set-keymap-parent doom-localleader-map
(cdr (assq major-mode doom-localleader-map-alist))))

(add-hook 'post-command-hook #'doom-update-localleader-key-h)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this need to run after each and every command?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally I had it hooked to doom-switch-buffer-hook, doom-switch-frame-hook, doom-switch-window-hook, and after-change-major-mode-hook but then it kept running in unexpected scenarios that happened inter-command like when the Corfu popup would show up so I had to set a bunch of predicates for that hook to run and I was not able to find a working set of predicates. After switching it to a post-command-hook, I was able to remove all those predicates and just run the set-keymap-parent action itself. It runs fast enough to not cause any noticeable latency:
image

@emenel
Copy link

emenel commented Oct 28, 2023

Just want to say that I've been testing this pr with my meow config, and it fixes the issues with using meow and doom's leader/localleader instead of meow's keypad mode. localleader now appears where it should and works perfectly along with meow's modes and doom's standard leader key (spc). Before, with meow, the localleader would not work via the doom leader (it had to be bound to a different top level key combination).

This is because immediately after switching persps or using similar
commands, the current-buffer is not the buffer that is displayed in the
window.
@LemonBreezes
Copy link
Contributor Author

I just discovered issues with edge cases where the localleader map is used for a minor mode or for a derived mode, so for org-journal-map, the org-mode localleader bindings will not show up. I'm gonna be working on this. This is a pretty tough code to write.

@TheJJ
Copy link
Contributor

TheJJ commented Mar 22, 2024

could we merge or consider #7189 as a previous step please?

for me the ability to activate the leader key or alt-key combos in more evil states already solves my use-case. and it extends the more general approach from this patch to specify which evil states the leader and localleader map is for.

@LemonBreezes
Copy link
Contributor Author

could we merge or consider #7189 as a previous step please?

for me the ability to activate the leader key or alt-key combos in more evil states already solves my use-case already. and it extends the more general approach from this patch to specify which evil states the leader and localleader map is for.

I am going to try to fix this this weekend. I really want to get this PR merged too because it's critical for my config.

@hlissner hlissner added the is:bug Something isn't working as intended label Mar 22, 2024
@hlissner hlissner added re:keybinds Changes to or discussion about Doom's keybinds module:core Relevant to Doom core was:moved Is, was, or will be addressed elsewhere labels Mar 22, 2024
@LemonBreezes
Copy link
Contributor Author

LemonBreezes commented Mar 22, 2024

Ok. I am going to stop working on this for some time since Hlissner said he was going to make major changes to the core keybindings code.

@hlissner hlissner marked this pull request as draft September 7, 2024 20:24
@emenel
Copy link

emenel commented Oct 31, 2024

I realize this is on hold pending bigger changes ... but at the moment it's holding me back from updating with more recent doom commits. When I update to the current main branch with this PR I get errors from modules about bindings:

Error in a Doom module: modules/emacs/dired/config.el, (error Eager macro-expansion failure: (wrong-type-argument symbolp (dired-mode-map)))

I've looked around to see if I can figure out how to adapt the changes, but it's beyond my knowledge of Doom's inner workings and the time i have at the moment.

For now I'm just not updating, but I'd love to be able to keep my bindings working and update to get the latest changes occasionally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
is:bug Something isn't working as intended module:core Relevant to Doom core re:keybinds Changes to or discussion about Doom's keybinds was:moved Is, was, or will be addressed elsewhere
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants