]> code.delx.au - gnu-emacs/blobdiff - lisp/which-func.el
*** empty log message ***
[gnu-emacs] / lisp / which-func.el
index b043753e68821a37dacfac2af203f7d7e7602f62..c594945d89e43993d6a30b836857928ba75c167b 100644 (file)
@@ -1,6 +1,6 @@
-;;; which-func.el --- Print current function in mode line
+;;; which-func.el --- print current function in mode line
 
-;; Copyright (C) 1994, 1997, 1998 Free Software Foundation, Inc.
+;; Copyright (C) 1994, 1997, 1998, 2001 Free Software Foundation, Inc.
 
 ;; Author:   Alex Rezinsky <alexr@msil.sps.mot.com>
 ;;           (doesn't seem to be responsive any more)
@@ -36,6 +36,8 @@
 ;; point". So if your current point is located after end of function
 ;; FOO but before begin of function BAR, FOO will be displayed in mode
 ;; line.
+;; - if two windows display the same buffer, both windows
+;;   show the same `which-func' information.
 
 ;; TODO LIST
 ;; ---------
@@ -73,8 +75,8 @@
   :version "20.3")
 
 (defcustom which-func-modes
-  '(emacs-lisp-mode c-mode c++-mode perl-mode makefile-mode sh-mode
-                   fortran-mode)
+  '(emacs-lisp-mode c-mode c++-mode perl-mode cperl-mode makefile-mode
+                   sh-mode fortran-mode)
   "List of major modes for which Which Function mode should be used.
 For other modes it is disabled.  If this is equal to t,
 then Which Function mode is enabled in any major mode that supports it."
@@ -91,7 +93,7 @@ Which Function mode doesn't do anything until you use Imenu."
   :group 'which-func
   :type '(repeat (symbol :tag "Major mode")))
 
-(defcustom which-func-maxout 100000
+(defcustom which-func-maxout 500000
   "Don't automatically compute the Imenu menu if buffer is this big or bigger.
 Zero means compute the Imenu menu regardless of size."
   :group 'which-func
@@ -102,18 +104,6 @@ Zero means compute the Imenu menu regardless of size."
   :group 'which-func
   :type 'sexp)
 
-;;;###autoload
-(defcustom which-func-mode-global nil
-  "*Toggle `which-func-mode' globally.
-Setting this variable directly does not take effect;
-use either \\[customize] or the function `which-func-mode'."
-  :set #'(lambda (symbol value)
-          (which-func-mode (if value 1 0)))
-  :initialize 'custom-initialize-default
-  :type    'boolean
-  :group   'which-func
-  :require 'which-func)
-
 (defvar which-func-cleanup-function nil
   "Function to transform a string before displaying it in the mode line.
 The function is called with one argument, the string to display.
@@ -137,7 +127,7 @@ and you want to simplify them for the mode line
 
 (defvar which-func-mode nil
   "Non-nil means display current function name in mode line.
-This makes a difference only if `which-func-mode-global' is non-nil")
+This makes a difference only if `which-function-mode' is non-nil.")
 (make-variable-buffer-local 'which-func-mode)
 ;;(put 'which-func-mode 'permanent-local t)
 
@@ -147,8 +137,9 @@ This makes a difference only if `which-func-mode-global' is non-nil")
   "File find hook for Which Function mode.
 It creates the Imenu index for the buffer, if necessary."
   (setq which-func-mode
-       (and which-func-mode-global
-            (or (eq which-func-modes t) (member major-mode which-func-modes))))
+       (and which-function-mode
+            (or (eq which-func-modes t)
+                (member major-mode which-func-modes))))
 
   (condition-case nil
       (if (and which-func-mode
@@ -162,54 +153,68 @@ It creates the Imenu index for the buffer, if necessary."
      (setq which-func-mode nil))))
 
 (defun which-func-update ()
-  ;; Update the string containing the current function.
-  (when which-func-mode
-    (condition-case info
-       (progn
-         (setq which-func-current (or (which-function) which-func-unknown))
-         (unless (string= which-func-current which-func-previous)
-           (force-mode-line-update)
-           (setq which-func-previous which-func-current)))
-      (error
-       (which-func-mode -1)
-       (error "Error in which-func-update: %s" info)))))
+  "Update the Which-Function mode display for all windows."
+  (walk-windows 'which-func-update-1 nil 'visible))
+
+(defun which-func-update-1 (window)
+  "Update the Which-Function mode display for window WINDOW."
+  (save-selected-window
+    (select-window window)
+    ;; Update the string containing the current function.
+    (when which-func-mode
+      (condition-case info
+         (progn
+           (setq which-func-current (or (which-function) which-func-unknown))
+           (unless (string= which-func-current which-func-previous)
+             (force-mode-line-update)
+             (setq which-func-previous which-func-current)))
+       (error
+        (which-func-mode -1)
+        (error "Error in which-func-update: %s" info))))))
 
-;; This is the name people would normally expect.
 ;;;###autoload
-(defalias 'which-function-mode 'which-func-mode)
+(defalias 'which-func-mode 'which-function-mode)
 
+;; This is the name people would normally expect.
 ;;;###autoload
-(defun which-func-mode (&optional arg)
+(define-minor-mode which-function-mode
   "Toggle Which Function mode, globally.
 When Which Function mode is enabled, the current function name is
 continuously displayed in the mode line, in certain major modes.
 
 With prefix ARG, turn Which Function mode on iff arg is positive,
 and off otherwise."
-  (interactive "P")
-  (setq which-func-mode-global
-       (or (and (null arg) which-func-mode-global)
-           (<= (prefix-numeric-value arg) 0)))
-  (if which-func-mode-global
-      ;; Turn it off
+  :global t :group 'which-func
+  (if which-function-mode
+      ;;Turn it on
       (progn
-       (remove-hook 'post-command-idle-hook 'which-func-update)
-       (dolist (buf (buffer-list))
-         (with-current-buffer buf (setq which-func-mode nil))))
-    ;;Turn it on
-    (add-hook 'post-command-idle-hook 'which-func-update)
+        (add-hook 'post-command-idle-hook 'which-func-update)
+        (dolist (buf (buffer-list))
+          (with-current-buffer buf
+            (setq which-func-mode
+                  (or (eq which-func-modes t)
+                      (member major-mode which-func-modes))))))
+    ;; Turn it off
+    (remove-hook 'post-command-idle-hook 'which-func-update)
     (dolist (buf (buffer-list))
-      (with-current-buffer buf
-       (setq which-func-mode
-             (or (eq which-func-modes t)
-                 (member major-mode which-func-modes)))))))
+      (with-current-buffer buf (setq which-func-mode nil)))))
+
+(defvar which-function-imenu-failed nil
+  "Locally t in a buffer if `imenu--make-index-alist' found nothing there.")
 
 (defun which-function ()
   "Return current function name based on point.
-If `imenu--index-alist' does not exist, or is empty  or if point
-is located before first function, returns nil."
+Uses `imenu--index-alist' or `add-log-current-defun-function'.
+If no function name is found, return nil."
   (let (name)
-    ;; First try using imenu support.
+    ;; If Imenu is loaded, try to make an index alist with it.
+    (when (and (boundp 'imenu--index-alist) (null imenu--index-alist)
+              (null which-function-imenu-failed))
+      (imenu--make-index-alist)
+      (unless imenu--index-alist
+       (make-local-variable 'which-function-imenu-failed)
+       (setq which-function-imenu-failed t)))
+    ;; If we have an index alist, use it.
     (when (and (boundp 'imenu--index-alist) imenu--index-alist)
       (let ((pair (car-safe imenu--index-alist))
            (rest (cdr-safe imenu--index-alist)))
@@ -231,4 +236,4 @@ is located before first function, returns nil."
 
 (provide 'which-func)
 
-;; which-func.el ends here
+;;; which-func.el ends here