atlas-engineer / nyxt

Nyxt - the hacker's browser.
https://nyxt-browser.com/
9.81k stars 409 forks source link

Overriding default keybindings on config file #3052

Open aadcg opened 1 year ago

aadcg commented 1 year ago

Example:

In the emacs keyscheme, M-f is bound to history-forwards-query.

Write a config file as follows:

(defvar *my-keymap* (make-keymap "my-map"))

(define-key *my-keymap*
  "M-f" 'nyxt/mode/hint:follow-hint)

(define-mode my-mode ()
  "Dummy mode for the custom key bindings in `*my-keymap*'."
  ((keyscheme-map (keymaps:make-keyscheme-map nyxt/keyscheme:emacs *my-keymap*))))

(defvar *buffer-modes*
  '(my-mode
    nyxt/mode/emacs:emacs-mode)
  "The modes to enable in buffers by default.")

(define-configuration buffer
  ((default-modes (append *buffer-modes* %slot-default%))))

Start Nyxt and notice that M-f is still bound to history-forwards-query.

A warning should perhaps be shown when binding a key that was previously taken.

Ambrevar commented 1 year ago

Isn't this https://github.com/atlas-engineer/nyxt/issues/2307 ?

aartaka commented 1 year ago

@aadcg, what about starting Nyxt with :verbose t/-vvv (-v should be enough, but -vvv is a fun tradition)? I remember trying to rebind some built-in keys and getting weird warning messages in the shell.

aadcg commented 1 year ago

what about starting Nyxt with :verbose t

I could do that, but the key point is that there's a bug. The warnings are the least of concerns.

aartaka commented 1 year ago

I could do that, but the key point is that there's a bug. The warnings are the least of concerns.

I'm not suggesting it as a fix or whatever, but rather as something that could reveal more details.

Ambrevar commented 1 year ago

I need to look into this more closely.

Ambrevar commented 1 year ago

So I finally went to the bottom of this: the problem has nothing to do with nkeymaps (pfeeew! :p).

Instead, inspect (modes (current-buffer)) and notice that my-mode is far from being first. Indeed, history-mode is before that, which is why M-f is still resolving to the history keybinding.

To know more about the resolution from the REPL:

(nkeymaps:lookup-key "M-f" (current-keymaps))

So the problem, really, is "how do we keep a mode in the first position"? But wait, maybe that's the XY problem... The real problem is how to set global bindings. We have the override-map for that. Is it enough? @aadcg What do you think?

aadcg commented 1 year ago

So the problem, really, is "how do we keep a mode in the first position"?

In my perspective, the problem is that the order of modes as of today matters but it shouldn't. From the user's perspective, it makes little sense. Key clashes should be raised. We would need to think about it carefully nonetheless. The model that Emacs uses doesn't suit our purposes.

But wait, maybe that's the XY problem... The real problem is how to set global bindings.

I think that leveraging global bindings is a way to go around the problem mentioned in the top post.


While this is an issue, I don't think it's a top priority. I consider the workaround good enough as of today.