]> code.delx.au - gnu-emacs/blobdiff - lisp/progmodes/which-func.el
Merge from emacs-24; up to 2012-12-17T11:17:34Z!rgm@gnu.org
[gnu-emacs] / lisp / progmodes / which-func.el
index 57dfcba69ac9b14396a26c830a14d35089bd2580..edfe368479ca315bc8c57a9a9b9a1d5efaa9c568 100644 (file)
@@ -1,7 +1,7 @@
 ;;; which-func.el --- print current function in mode line
 
-;; Copyright (C) 1994, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006
-;;   2007, 2008, 2009, 2010, 2011, 2012  Free Software Foundation, Inc.
+;; Copyright (C) 1994, 1997-1998, 2001-2013 Free Software Foundation,
+;; Inc.
 
 ;; Author:   Alex Rezinsky <alexr@msil.sps.mot.com>
 ;;           (doesn't seem to be responsive any more)
   "String to display in the mode line when current function is unknown.")
 
 (defgroup which-func nil
-  "Mode to display the current function name in the modeline."
+  "Display the current function name in the mode line."
   :group 'tools
   :version "20.3")
 
-(defcustom which-func-modes
-  '(emacs-lisp-mode c-mode c++-mode perl-mode cperl-mode python-mode
-                   makefile-mode sh-mode fortran-mode f90-mode ada-mode
-                   diff-mode)
+(defcustom which-func-modes t
+  ;; '(emacs-lisp-mode c-mode c++-mode objc-mode perl-mode cperl-mode python-mode
+  ;;               makefile-mode sh-mode fortran-mode f90-mode ada-mode
+  ;;               diff-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."
   :group 'which-func
+  :version "24.3"                       ; explicit list -> t
   :type '(choice (const :tag "All modes" t)
                 (repeat (symbol :tag "Major mode"))))
 
@@ -144,12 +145,13 @@ Zero means compute the Imenu menu regardless of size."
     (:propertize which-func-current
                 local-map ,which-func-keymap
                 face which-func
-                ;;mouse-face highlight ; currently not evaluated :-(
+                mouse-face mode-line-highlight
                 help-echo "mouse-1: go to beginning\n\
 mouse-2: toggle rest visibility\n\
 mouse-3: go to end")
     "]")
   "Format for displaying the function in the mode line."
+  :version "24.2"                  ; added mouse-face; 24point2 is correct
   :group 'which-func
   :type 'sexp)
 ;;;###autoload (put 'which-func-format 'risky-local-variable t)
@@ -163,7 +165,7 @@ single string, the new name of the item.")
 (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.
-Its return value is displayed in the modeline.
+Its return value is displayed in the mode line.
 If nil, no function is called.  The default value is nil.
 
 This feature can be useful if Imenu is set up to make more
@@ -179,7 +181,10 @@ and you want to simplify them for the mode line
 (defvar which-func-table (make-hash-table :test 'eq :weakness 'key))
 
 (defconst which-func-current
-  '(:eval (gethash (selected-window) which-func-table which-func-unknown)))
+  '(:eval (replace-regexp-in-string
+          "%" "%%"
+          (or (gethash (selected-window) which-func-table)
+               which-func-unknown))))
 ;;;###autoload (put 'which-func-current 'risky-local-variable t)
 
 (defvar which-func-mode nil
@@ -198,7 +203,7 @@ It creates the Imenu index for the buffer, if necessary."
             (or (eq which-func-modes t)
                 (member major-mode which-func-modes))))
 
-  (condition-case nil
+  (condition-case err
       (if (and which-func-mode
               (not (member major-mode which-func-non-auto-modes))
               (or (null which-func-maxout)
@@ -207,6 +212,9 @@ It creates the Imenu index for the buffer, if necessary."
          (setq imenu--index-alist
                (save-excursion (funcall imenu-create-index-function))))
     (error
+     (unless (equal err
+                    '(user-error "This buffer cannot use `imenu-default-create-index-function'"))
+       (message "which-func-ff-hook error: %S" err))
      (setq which-func-mode nil))))
 
 (defun which-func-update ()
@@ -225,22 +233,24 @@ It creates the Imenu index for the buffer, if necessary."
              (force-mode-line-update)))
        (error
         (setq which-func-mode nil)
-        (error "Error in which-func-update: %s" info))))))
+        (error "Error in which-func-update: %S" info))))))
 
 ;;;###autoload
-(defalias 'which-func-mode 'which-function-mode)
+(define-obsolete-function-alias 'which-func-mode 'which-function-mode "24.1")
 
 (defvar which-func-update-timer nil)
 
 ;; This is the name people would normally expect.
 ;;;###autoload
 (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 if arg is positive,
-and off otherwise."
+  "Toggle mode line display of current function (Which Function mode).
+With a prefix argument ARG, enable Which Function mode if ARG is
+positive, and disable it otherwise.  If called from Lisp, enable
+the mode if ARG is omitted or nil.
+
+Which Function mode is a global minor mode.  When enabled, the
+current function name is continuously displayed in the mode line,
+in certain major modes."
   :global t :group 'which-func
   (when (timerp which-func-update-timer)
     (cancel-timer which-func-update-timer))
@@ -280,10 +290,9 @@ If no function name is found, return nil."
     (when (and (null name)
               (boundp 'imenu--index-alist) (null imenu--index-alist)
               (null which-function-imenu-failed))
-      (imenu--make-index-alist t)
+      (ignore-errors (imenu--make-index-alist t))
       (unless imenu--index-alist
-       (make-local-variable 'which-function-imenu-failed)
-       (setq which-function-imenu-failed t)))
+        (set (make-local-variable 'which-function-imenu-failed) t)))
     ;; If we have an index alist, use it.
     (when (and (null name)
               (boundp 'imenu--index-alist) imenu--index-alist)
@@ -294,29 +303,33 @@ If no function name is found, return nil."
         ;; ("submenu" ("name" . marker) ... ). The list can be
         ;; arbitrarily nested.
         (while (or alist imstack)
-          (if alist
-              (progn
-                (setq pair (car-safe alist)
-                      alist (cdr-safe alist))
-
-                (cond ((atom pair))     ; skip anything not a cons
-
-                      ((imenu--subalist-p pair)
-                       (setq imstack   (cons alist imstack)
-                             namestack (cons (car pair) namestack)
-                             alist     (cdr pair)))
-
-                      ((number-or-marker-p (setq mark (cdr pair)))
-                       (if (>= (setq offset (- (point) mark)) 0)
-                           (if (< offset minoffset) ; find the closest item
-                               (setq minoffset offset
-                                     name (funcall
-                                           which-func-imenu-joiner-function
-                                          (reverse (cons (car pair)
-                                                         namestack)))))))))
-            (setq alist     (car imstack)
-                  namestack (cdr namestack)
-                  imstack   (cdr imstack))))))
+          (if (null alist)
+              (setq alist     (car imstack)
+                    namestack (cdr namestack)
+                    imstack   (cdr imstack))
+
+            (setq pair (car-safe alist)
+                  alist (cdr-safe alist))
+
+            (cond
+             ((atom pair))              ; Skip anything not a cons.
+
+             ((imenu--subalist-p pair)
+              (setq imstack   (cons alist imstack)
+                    namestack (cons (car pair) namestack)
+                    alist     (cdr pair)))
+
+             ((or (number-or-marker-p (setq mark (cdr pair)))
+                 (and (overlayp mark)
+                      (setq mark (overlay-start mark))))
+              (when (and (>= (setq offset (- (point) mark)) 0)
+                         (< offset minoffset)) ; Find the closest item.
+                (setq minoffset offset
+                      name (if (null which-func-imenu-joiner-function)
+                               (car pair)
+                             (funcall
+                              which-func-imenu-joiner-function
+                              (reverse (cons (car pair) namestack))))))))))))
 
     ;; Try using add-log support.
     (when (null name)
@@ -327,7 +340,22 @@ If no function name is found, return nil."
          (funcall which-func-cleanup-function name)
        name))))
 
+\f
+;;; Integration with other packages
+
+(defun which-func-update-ediff-windows ()
+  "Update Which-Function mode display for Ediff windows.
+This function is meant to be called from `ediff-select-hook'."
+  (when (eq major-mode 'ediff-mode)
+    (when ediff-window-A
+      (which-func-update-1 ediff-window-A))
+    (when ediff-window-B
+      (which-func-update-1 ediff-window-B))
+    (when ediff-window-C
+      (which-func-update-1 ediff-window-C))))
+
+(add-hook 'ediff-select-hook 'which-func-update-ediff-windows)
+
 (provide 'which-func)
 
-;; arch-tag: fa8a55c7-bfe3-4ffc-95ab-01bf21796827
 ;;; which-func.el ends here