reddit / emacs
Issue overriding view-mode-map key
guitmz · 2022-03-29 11:33
Hi, I have a stupd issue where I cannot override `view-mode-map` keys for some reason.
The code I want to use is below. F13 binding is working but the others aren't.
```
(setq view-read-only t)
(global-set-key (kbd "<f13>") 'view-mode)
(with-eval-after-load 'view-mode
(define-key view-mode-map (kbd "f") #'forward-char)
(define-key view-mode-map (kbd "n") #'next-line)
(define-key view-mode-map (kbd "p") #'previous-line))
```
If I remove the `with-eval-after-load` and just add the following
```
(setq view-read-only t)
(global-set-key (kbd "<f13>") 'view-mode)
(define-key view-mode-map (kbd "f") #'forward-char)
(define-key view-mode-map (kbd "n") #'next-line)
(define-key view-mode-map (kbd "p") #'previous-line))
```
Then I get: `Symbol's value as variable is void: view-mode-map`.
I'm still a noob so I might be overlooking something really simple here, any help, please? Thanks in advanced!
EDIT: forgot to mention that it that if I go to my init file and `eval-region` on the `define-key` lines after Emacs loads, it works just fine. I’m also overriding other maps like dired and some others in a similar way and they also work fine.
EDIT 2: I am trying to replicate this https://gist.github.com/ivan-krukov/63a586f2121519ca51b201c634402a84 but without `use-package`.
Phil-Hudson · 2022-03-29 16:24
I suspect that you simply aren't loading view-mode. You say that the global binding of `view-mode` is working. Do you not find that after you press F13 (which will implicitly load view.el) the bindings you want start working?
guitmz · 2022-03-29 17:04
So when I press F13 view mode is definitely enabled as I can use it in the buffer. But the bindings that I defined are not present.
Here’s a snipped that relies on use-package and does the same thing (it works) https://gist.github.com/ivan-krukov/63a586f2121519ca51b201c634402a84
Phil-Hudson · 2022-03-29 18:09
In that case your best bet is probably to put the body of the `with-eval-after-load` into a defun and call that defun from the `w-e-a-l` form. The purpose is to be able to use edebug, which you do by placing point anywhere in the body of the defun and typing `C-u C-M-x` before calling it, or by adding the form `(edebug)` at the top of the defun. Have a look at the Emacs documentation for how to use edebug. If you find that edebug is never entered then you know that your `w-e-a-l` form is not being executed for some reason.
guitmz · 2022-03-29 18:43
Oh that’s a good tip, I can try that. Thanks! I did tested without the w-e-a-l too and it also does not work by the way. It gives the error described in the original post…
astoff1 · 2022-03-29 19:25
Check which file defines view-mode, and change the argument to with-eval-after-load accordingly.
guitmz · 2022-03-29 19:29
You mean like if it’s view.el I should use “view” instead of “view-mode”?
https://github.com/emacs-mirror/emacs/blob/973608e35895a8f89a3abcac43dfaf89598b0c82/lisp/view.el#L991
That might be it! Will test soon.
guitmz · 2022-03-29 19:39
It worked! Thank you so much. Now that you mentioned it makes perfect sense. I just blindly assumed it would be called “view-mode” instead. TIL
organic-bookworm · 2022-03-29 19:44
The feature "view.el" library `provide`s is `'view`, not `'view-mode`.
(with-eval-after-load 'view ...)
Your `define-key` forms just never run.
guitmz · 2022-03-29 19:49
Yup that was it! Thanks!
organic-bookworm · 2022-03-29 20:07
Note, that `'view` (a symbol) here means a **feature**, defined in `(provide 'feature)` form, as opposed to the `"view"` (a string), which is a **library** (file) name.
Single library *may* provide multiple features and/or name them differently.
EDIT: Oh, just realized, you've linked exactly that. Sorry.
guitmz · 2022-03-29 20:16
Yeah I’ve been organising my config like this too, for some reason it slipped my mind completely. Now it all makes sense. Thanks again