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

Merge keymaps' nested keys #142

Open
Fom123 opened this issue Mar 7, 2023 · 1 comment
Open

Merge keymaps' nested keys #142

Fom123 opened this issue Mar 7, 2023 · 1 comment
Labels
enhancement New feature or request request Feature request

Comments

@Fom123
Copy link

Fom123 commented Mar 7, 2023

Is your request related to a specific problem you're having?
I would like nested keys to get merged, as it happens with usual keys in conditional keymaps.

For example

keymap("Brave", {
    C("C-j"): C("C-f6"),
    C("C-x"): {
        C("k"): C("C-w")
    }
}, when=wm_class_match(r"Brave-browser"))

keymap("Emacs-like keys", {
    C("C-b"): with_mark(C("left")),
    C("C-x"): {
        # C-x h (select all)
        C("h"): [C("C-home"), C("C-a"), set_mark(True)],
    }
}, when=lambda ctx: ctx.wm_class != "Emacs")

When we run this, we will get the following

Application Available Bindings
Brave C-b, C-j, C-x k
Anything else C-b, C-x h

The solution you'd prefer or feature you'd like to see added...

I would like to see the following

Application Available Bindings
Brave C-b, C-j, C-x k, C-x h
Anything else C-b, C-x h

Any alternative solutions you considered...

  1. Move combo's binding to separate variable
emacs_bindings = {
     C("h"): [C("C-home"), C("C-a"), set_mark(True)],
}
  1. Unpack it when it's needed.
keymap("Brave", {
    C("C-j"): C("C-f6"),
    C("C-x"): dict({
        C("k"): C("C-w")
    }, **emacs_bindings)
}, when=wm_class_match(r"Brave-browser"))

But it's not that convinient.

@Fom123 Fom123 added enhancement New feature or request request Feature request labels Mar 7, 2023
@joshgoebel
Copy link
Owner

as it happens with usual keys in conditional keymaps.

Except it doesn't. The top-level keymaps are not merged... they are merely available at the top-level and then activated based on the conditional functions/criteria... but once a nested keymap is selected that is now the active keymap - and it handles keystrokes exclusively.

I do see how you might want your suggested behavior, in this example, but it doesn't seem clear to me at all that this would always (or even often) be desiredable behavior.

But it's not that convenient.

It's not that inconvenient. And it has the benefit of being explicit and unsurprising, which is good.


The config is also just Python. So you could write your own helper function that you called at the end of the configuration that did this merging for you (looping over all the keymaps, merging any nested maps, etc) - if that was truly the desired behavior.

In general I'm very slow to add new features when the feature could easily be accomplished with only Python and the existing API (or small hacks). If possible, someone should first write the feature as a plugin/helper and then let it prove it's general usefulness... perhaps then it could be added - and if not it can live on in the wiki as an extension.

In this case I don't even think you need any big hacks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request request Feature request
Projects
None yet
Development

No branches or pull requests

2 participants