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.

4
  • As I have mentioned in the previous comment, It seems hard to make a global minor mode major mode specific stackoverflow.com/questions/62080587/… Commented Mar 19, 2022 at 9:55
  • Mhm... Then is there another way to use something like which-func-mode only in Python?
    – buhtz
    Commented Mar 19, 2022 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
    Commented Mar 20, 2022 at 9: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
    Commented Mar 20, 2022 at 9:29

1 Answer 1

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

2
  • 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
    Commented Mar 21, 2022 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
    Commented Mar 21, 2022 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.