geza42 · 2022-05-03 10:35
For example, org mode defines `org-mouse-map`. As far as I understand, this map is used context-sensitively, it is only active on links. So I could use, for example:
(define-key org-mouse-map (kbd "C-l") 'org-open-at-point)
This means that I can use `C-l` to open a link. But if the point is not on a link, `C-l` will does its original function. This works as intended.
However, it doesn't work if I try to use an evil keybinding which is only active in normal mode:
(evil-define-key 'normal org-mouse-map (kbd "RET") 'org-open-at-point)
If it worked, I could use `RET` to open a link when I'm in normal mode.
Some additional info: if the point is on a link, and I execute `(evil-normalize-keymaps)`, then `RET` starts to work, but it will try to open links everywhere, not just on links. If I execute `(evil-normalize-keymaps)` when the point is not on a link, then `RET` doesn't work afterwards. So it seems that `(evil-normalize-keymaps)` is affected by where the point is.
Is this a not-implemented feature of evil?
_noctuid · 2022-05-04 02:20
Do you have RET bound to something besides `org-open-at-point` in normal by default? Why not just bind in `org-mode-map`?
You need to use `define-key` with text property maps. If you want different behavior, you will have to use a menu item or function that checks the current state. Here is an example (using general.el for more convenient syntax, but you can just manually bind to a menu-item):
(general-def org-mouse-map
:predicate '(and (boundp 'evil-state) (eq evil-state 'normal))
"RET" #'org-open-at-point)
geza42 · 2022-05-05 00:21
Yes, I have RET bound to something, but I'd like to override it when the point is on a link. Thanks for this solution! Based on that, I created a solution which doesn't need general-def (I don't use general.el, just for this feature it would have been overkill to use it):
(define-key org-mouse-map (kbd "RET") '(menu-item "" nil :filter (lambda (&optional _) (when (eq evil-state 'normal) 'org-open-at-point))))
Too bad that this solution doesn't work for the general evil "open-link" binding, `(kbd "gx")`, because the "g" prefix key confuses something, and in insert mode, the character "g" cannot be inserted at all.
_noctuid · 2022-05-05 00:41
You might be able to [use general](https://github.com/noctuid/general.el#mapping-under-non-prefix-keys) for that too, but I haven't tried. Fwiw these definition helper functions will be extracted to a separate, small package at some point.
If you can't bind in org-mouse-map, you can always bind a menu item in org-mode-map that checks if the point is on a link intstead.