0

How can I restrict the which-func-mode and the header bar to python-mode buffers only?

I like to use which-func-mode in the headline for Python files/buffers only. In my current config it works that the mode is activated only if a python-mode buffer comes up. But the problem is that after that the which-func-mode and the headline is shown in all buffers.

;; =========
;; Which Function Mode
;; =========
(use-package which-func
  :config
  ;; replace nothing found symbol "???"
  (setq which-func-unknown "∅")
  ;; add to headline
  (setq-default header-line-format
        '((which-func-mode ("" which-func-format " "))))
  ;; remove from modeline
  (setq mode-line-misc-info
    (delete '(which-function-mode (which-func-mode
                       ("" which-func-format " ")))
       mode-line-misc-info))
)

;; ==========
;; Python
;; ==========
; pip3 install python-lsp-server
(use-package eglot)

(use-package python-mode
  :ensure nil  ; in-build!
  :hook
  (python-mode . eglot-ensure)
  (python-mode . which-func-mode)
  :custom
  (python-shell-interpreter "python3")
  )

I also tried that solution. But here is the same problem.

phils
  • 48,657
  • 3
  • 76
  • 115
buhtz
  • 679
  • 4
  • 22
  • As I have mentioned in the previous comment, It seems hard to make a global minor mode major mode specific https://stackoverflow.com/questions/62080587/make-a-global-minor-mode-buffer-local – Tianshu Wang Mar 19 '22 at 09:55
  • Mhm... Then is there another way to use something like which-func-mode only in Python? – buhtz Mar 19 '22 at 14:00
  • To quote that Q&A: "Global modes have to be treated on a case-by-case basis, looking at their specific implementation details." In this particular case, it supports what you're asking for. – phils Mar 20 '22 at 09:05
  • Make sure you get rid of `(python-mode . which-func-mode)` from your config, btw. You don't want to be calling a global mode every time you open a python file. Just call it once. – phils Mar 20 '22 at 09:29

1 Answers1

1

which-func[tion]-mode does support this.

(setq which-func-modes '(python-mode))
(which-func-mode 1)

FYI you would have found this quickly with:

M-x customize-group RET which-func RET

phils
  • 48,657
  • 3
  • 76
  • 115
  • I rejected your proposed edit deleting the note about `customize-group`, which you described as an "unpolite RTFM". It is not. It is a statement of fact explicitly "for your information" intended to *help you* to learn. I added it in part because you posted the same question *no less than three times* (twice here on E.S. and once on reddit), when the solution was there all along had you asked Emacs in the manner I've shown. I hope that you, and anyone else who finds this answer useful in future, will add `customize-group` to your Emacs toolkit and save yourselves time and effort in future. – phils Mar 21 '22 at 10:40
  • Furthermore a "RTFM" response (a) wouldn't provide a solution directly; (b) wouldn't have directed you to a very specific location; and (c) would refer to *documentation* rather than a customization UI; so all in all I found the comment perplexing. You might just trust that helpful information is intended to be helpful. – phils Mar 21 '22 at 23:30