aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Mackenzie2020-05-20 18:02:13 +0000
committerAlan Mackenzie2020-05-20 18:02:13 +0000
commitbd64571ef213a4279f897b7446e97ccd76fe4516 (patch)
tree0bacba813451e3470f675883abc1525d0de14f41
parent525df72753acdd0221771a2a298842868f49f4b4 (diff)
downloademacs-bd64571ef213a4279f897b7446e97ccd76fe4516.tar.gz
emacs-bd64571ef213a4279f897b7446e97ccd76fe4516.zip
which-function-mode: put hook function on after-change-major-mode-hook
, rather than find-file-hook. This keeps which-function-mode active should the major mode be reinitialized. Also accept a null result from add-log-current-defun as definitive, should that function have run. This fixes bug #40714. * lisp/progmodes/which-func.el (which-func-ff-hook): Put on after-change-major-mode-hook. (which-function): Enhance the logic to accept a null result from add-log-current-defun.
-rw-r--r--lisp/progmodes/which-func.el97
1 files changed, 50 insertions, 47 deletions
diff --git a/lisp/progmodes/which-func.el b/lisp/progmodes/which-func.el
index 1cee552b0c0..266f40abbae 100644
--- a/lisp/progmodes/which-func.el
+++ b/lisp/progmodes/which-func.el
@@ -186,7 +186,7 @@ and you want to simplify them for the mode line
186 "Non-nil means display current function name in mode line. 186 "Non-nil means display current function name in mode line.
187This makes a difference only if `which-function-mode' is non-nil.") 187This makes a difference only if `which-function-mode' is non-nil.")
188 188
189(add-hook 'find-file-hook 'which-func-ff-hook t) 189(add-hook 'after-change-major-mode-hook 'which-func-ff-hook t)
190 190
191(defun which-func-try-to-enable () 191(defun which-func-try-to-enable ()
192 (unless (or (not which-function-mode) 192 (unless (or (not which-function-mode)
@@ -195,7 +195,7 @@ This makes a difference only if `which-function-mode' is non-nil.")
195 (member major-mode which-func-modes))))) 195 (member major-mode which-func-modes)))))
196 196
197(defun which-func-ff-hook () 197(defun which-func-ff-hook ()
198 "File find hook for Which Function mode. 198 "`after-change-major-mode-hook' for Which Function mode.
199It creates the Imenu index for the buffer, if necessary." 199It creates the Imenu index for the buffer, if necessary."
200 (which-func-try-to-enable) 200 (which-func-try-to-enable)
201 201
@@ -282,52 +282,55 @@ If no function name is found, return nil."
282 (when (null name) 282 (when (null name)
283 (setq name (add-log-current-defun))) 283 (setq name (add-log-current-defun)))
284 ;; If Imenu is loaded, try to make an index alist with it. 284 ;; If Imenu is loaded, try to make an index alist with it.
285 ;; If `add-log-current-defun' ran and gave nil, accept that.
285 (when (and (null name) 286 (when (and (null name)
286 (boundp 'imenu--index-alist) 287 (null add-log-current-defun-function))
287 (or (null imenu--index-alist) 288 (when (and (null name)
288 ;; Update if outdated 289 (boundp 'imenu--index-alist)
289 (/= (buffer-chars-modified-tick) imenu-menubar-modified-tick)) 290 (or (null imenu--index-alist)
290 (null which-function-imenu-failed)) 291 ;; Update if outdated
291 (ignore-errors (imenu--make-index-alist t)) 292 (/= (buffer-chars-modified-tick) imenu-menubar-modified-tick))
292 (unless imenu--index-alist 293 (null which-function-imenu-failed))
293 (set (make-local-variable 'which-function-imenu-failed) t))) 294 (ignore-errors (imenu--make-index-alist t))
294 ;; If we have an index alist, use it. 295 (unless imenu--index-alist
295 (when (and (null name) 296 (set (make-local-variable 'which-function-imenu-failed) t)))
296 (boundp 'imenu--index-alist) imenu--index-alist) 297 ;; If we have an index alist, use it.
297 (let ((alist imenu--index-alist) 298 (when (and (null name)
298 (minoffset (point-max)) 299 (boundp 'imenu--index-alist) imenu--index-alist)
299 offset pair mark imstack namestack) 300 (let ((alist imenu--index-alist)
300 ;; Elements of alist are either ("name" . marker), or 301 (minoffset (point-max))
301 ;; ("submenu" ("name" . marker) ... ). The list can be 302 offset pair mark imstack namestack)
302 ;; arbitrarily nested. 303 ;; Elements of alist are either ("name" . marker), or
303 (while (or alist imstack) 304 ;; ("submenu" ("name" . marker) ... ). The list can be
304 (if (null alist) 305 ;; arbitrarily nested.
305 (setq alist (car imstack) 306 (while (or alist imstack)
306 namestack (cdr namestack) 307 (if (null alist)
307 imstack (cdr imstack)) 308 (setq alist (car imstack)
308 309 namestack (cdr namestack)
309 (setq pair (car-safe alist) 310 imstack (cdr imstack))
310 alist (cdr-safe alist)) 311
311 312 (setq pair (car-safe alist)
312 (cond 313 alist (cdr-safe alist))
313 ((atom pair)) ; Skip anything not a cons. 314
314 315 (cond
315 ((imenu--subalist-p pair) 316 ((atom pair)) ; Skip anything not a cons.
316 (setq imstack (cons alist imstack) 317
317 namestack (cons (car pair) namestack) 318 ((imenu--subalist-p pair)
318 alist (cdr pair))) 319 (setq imstack (cons alist imstack)
319 320 namestack (cons (car pair) namestack)
320 ((or (number-or-marker-p (setq mark (cdr pair))) 321 alist (cdr pair)))
321 (and (overlayp mark) 322
322 (setq mark (overlay-start mark)))) 323 ((or (number-or-marker-p (setq mark (cdr pair)))
323 (when (and (>= (setq offset (- (point) mark)) 0) 324 (and (overlayp mark)
324 (< offset minoffset)) ; Find the closest item. 325 (setq mark (overlay-start mark))))
325 (setq minoffset offset 326 (when (and (>= (setq offset (- (point) mark)) 0)
326 name (if (null which-func-imenu-joiner-function) 327 (< offset minoffset)) ; Find the closest item.
327 (car pair) 328 (setq minoffset offset
328 (funcall 329 name (if (null which-func-imenu-joiner-function)
329 which-func-imenu-joiner-function 330 (car pair)
330 (reverse (cons (car pair) namestack)))))))))))) 331 (funcall
332 which-func-imenu-joiner-function
333 (reverse (cons (car pair) namestack)))))))))))))
331 ;; Filter the name if requested. 334 ;; Filter the name if requested.
332 (when name 335 (when name
333 (if which-func-cleanup-function 336 (if which-func-cleanup-function