Hello,
I am struggling to get which-function-mode activated in a derived
mode. I have trimmed the problem down successfully to a minimum example
that looks as follows:
(add-to-list 'auto-mode-alist '("\\.mini\\'" . mini-mode))
(defun mini-mode-which-func ()
"XXX")
(add-hook 'which-func-functions #'mini-mode-which-func)
(define-derived-mode mini-mode
text-mode "Mini"
"Major mode for editing .mini files."
(add-hook 'mini-mode-hook #'(lambda ()
(setq-local foo "bar")
(which-function-mode 1)
(setq-local which-function-mode t)
(setq-local which-func-mode t)
(message "Hook has been setup.")))
(run-mode-hooks))
(provide 'mini)
When I start GUN Emacs 27.1 (Devuan Linux) with
emacs -Q -l mini-mode.el foo.mini
it opens an empty foo.mini correcty in mini-mode, as confirmed by C-h m
and the mode line.
C-h v foo RET
shows that foo is locally set to "bar", but void globally. Likewise, which-function-mode (the variable) is set to t, but which-func-mode is
nil, even though I have set it to t in my mode's hook. This hook is
executed, as confirmed by the text message "Hook has been setup"
immediately after opening foo.mini.
However, the dummy "XXX" used as "which-func" is not shown in the mode
line.
Only if I run
M-x mini-mode RET
again or do a
M-: (setq-local which-func-mode t) RET
the "XXX" appears in the mode line, as desired.
What I am doing wrong or how do I get which-func-mode activated in my
derived mode automatically?
Pointers much appreciated!
Axel Reichert <mail@axel-reichert.de> a |-crit :
What I am doing wrong or how do I get which-func-mode activated in my
derived mode automatically?
Pointers much appreciated!
I do not know really the issue, however I think that it has to do with
imenu. It looks like which-functions-mode uses imenu to understand
where begins and ends a function. Trying to use imenu in Text Mode
throws an error.
Thomas Dupond <thomas@dupond.eu> writes:
Axel Reichert <mail@axel-reichert.de> a |-crit :
What I am doing wrong or how do I get which-func-mode activated in my
derived mode automatically?
Pointers much appreciated!
I do not know really the issue, however I think that it has to do with
imenu. It looks like which-functions-mode uses imenu to understand
where begins and ends a function. Trying to use imenu in Text Mode
throws an error.
I had this suspicion as well, but discarded it because the second
M-x mini-mode
succeeds, as does setting which-func-mode to t manually. However, just
for the sake of testing, I have now based my mini-mode not on text-mode,
but on c-mode, and, heureka, the "XXX" is shown in the mode line!
Now I will search the docs about Imenu and try to find out how to teach
it that text-mode is not a no-go. Let's see.
I delved a little bit deeper and apparently you should be able to do
what you want after you have defined the variable
imenu-generic-expression or the variable imenu-create-index-function.
Fortran mode uses the former and Org mode the latter (respectively in fortran-mode.el and org-compat.el).
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 65 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 01:39:31 |
| Calls: | 862 |
| Files: | 1,311 |
| D/L today: |
10 files (20,373K bytes) |
| Messages: | 264,188 |