Function: which-function

which-function is a byte-compiled function defined in which-func.el.gz.

Signature

(which-function)

Documentation

Return current function name based on point.

Uses which-func-functions, add-log-current-defun. or imenu--index-alist If no function name is found, return nil.

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/which-func.el.gz
(defun which-function ()
  "Return current function name based on point.
Uses `which-func-functions', `add-log-current-defun'.
or `imenu--index-alist'
If no function name is found, return nil."
  (let ((name
	 ;; Try the `which-func-functions' functions first.
	 (run-hook-with-args-until-success 'which-func-functions)))
    ;; Try using add-log support.
    (when (null name)
      (setq name (add-log-current-defun)))
    ;; If Imenu is loaded, try to make an index alist with it.
    ;; If `add-log-current-defun' ran and gave nil, accept that.
    (when (and (null name)
               (null add-log-current-defun-function))
      (when (and (null name)
                 (boundp 'imenu--index-alist)
                 (or (null imenu--index-alist)
                     ;; Update if outdated
                     (/= (buffer-chars-modified-tick) imenu-menubar-modified-tick))
                 (null which-function-imenu-failed))
        (ignore-errors (imenu--make-index-alist t))
        (unless imenu--index-alist
          (setq-local which-function-imenu-failed t)))
      ;; If we have an index alist, use it.
      (when (and (null name)
                 (boundp 'imenu--index-alist) imenu--index-alist)
        (let ((alist imenu--index-alist)
              (minoffset (point-max))
              offset pair mark imstack namestack)
          ;; Elements of alist are either ("name" . marker), or
          ;; ("submenu" ("name" . marker) ... ). The list can be
          ;; arbitrarily nested.
          (while (or alist 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)))))))))))))
    ;; Filter the name if requested.
    (when name
      (if which-func-cleanup-function
	  (funcall which-func-cleanup-function name)
	name))))