(add-hook 'after-change-major-mode-hook #'which-func-ff-hook t)
(defun which-func-try-to-enable ()
- (unless (or (not which-function-mode)
- (local-variable-p 'which-func-mode))
- (setq which-func-mode (or (eq which-func-modes t)
- (member major-mode which-func-modes)))
- (setq which-func--use-mode-line
- (member which-func-display '(mode mode-and-header)))
- (setq which-func--use-header-line
- (member which-func-display '(header mode-and-header)))
- (when (and which-func-mode which-func--use-header-line)
+ (when which-function-mode
+ (unless (local-variable-p 'which-func-mode)
+ (setq which-func-mode (or (eq which-func-modes t)
+ (member major-mode which-func-modes)))
+ (setq which-func--use-mode-line
+ (member which-func-display '(mode mode-and-header)))
+ (setq which-func--use-header-line
+ (member which-func-display '(header mode-and-header))))
+ ;; We might need to re-add which-func-format to the header line,
+ ;; if which-function-mode was toggled off and on.
+ (when (and which-func-mode which-func--use-header-line
+ (listp header-line-format))
(add-to-list 'header-line-format '("" which-func-format " ")))))
-(defun which-func--disable ()
- (when (and which-func-mode which-func--use-header-line)
+(defun which-func--header-line-remove ()
+ (when (and which-func-mode which-func--use-header-line
+ (listp header-line-format))
(setq header-line-format
- (delete '("" which-func-format " ") header-line-format)))
+ (delete '("" which-func-format " ") header-line-format))))
+
+(defun which-func--disable ()
+ (which-func--header-line-remove)
(setq which-func-mode nil))
(defun which-func-ff-hook ()
(when which-function-mode
;;Turn it on.
(setq which-func-update-timer
- (run-with-idle-timer idle-update-delay t #'which-func-update))
- (dolist (buf (buffer-list))
- (with-current-buffer buf (which-func-try-to-enable)))))
+ (run-with-idle-timer idle-update-delay t #'which-func-update)))
+ (dolist (buf (buffer-list))
+ (with-current-buffer buf
+ (which-func--header-line-remove)
+ (which-func-ff-hook))))
(defvar which-function-imenu-failed nil
"Locally t in a buffer if `imenu--make-index-alist' found nothing there.")
--- /dev/null
+;;; which-func-tests.el --- tests for which-func -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2023 Free Software Foundation, Inc.
+
+;; Author: Spencer Baugh <sbaugh@catern.com>
+
+;; This file is part of GNU Emacs.
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;; Code:
+(require 'ert)
+(require 'which-func)
+
+(ert-deftest which-func-tests-toggle ()
+ (let ((which-func-display 'mode-and-header) buf-code buf-not)
+ (setq buf-code (find-file-noselect "which-func-tests.el"))
+ (setq buf-not (get-buffer-create "fundamental"))
+ (with-current-buffer buf-code
+ (should-not which-func-mode) (should-not header-line-format))
+ (with-current-buffer buf-not
+ (should-not which-func-mode) (should-not header-line-format))
+ (which-function-mode 1)
+ (with-current-buffer buf-code
+ (should which-func-mode) (should header-line-format))
+ (with-current-buffer buf-not
+ (should-not which-func-mode) (should-not header-line-format))
+ (which-function-mode -1)
+ ;; which-func-mode stays set even when which-function-mode is off.
+ (with-current-buffer buf-code
+ (should which-func-mode) (should-not header-line-format))
+ (with-current-buffer buf-not
+ (should-not which-func-mode) (should-not header-line-format))
+ (kill-buffer buf-code)
+ (kill-buffer buf-not)
+ (which-function-mode 1)
+ (setq buf-code (find-file-noselect "which-func-tests.el"))
+ (setq buf-not (get-buffer-create "fundamental"))
+ (with-current-buffer buf-code
+ (should which-func-mode) (should header-line-format))
+ (with-current-buffer buf-not
+ (should-not which-func-mode) (should-not header-line-format))))
+
+(provide 'which-func-tests)
+;;; which-func-tests.el ends here