From 55cc8b040b0e3c5f97fd1386d1e9c5a120be6340 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Mon, 8 Aug 2022 14:31:54 +0200 Subject: Make which-func-mode output less junk * lisp/emacs-lisp/lisp-mode.el (lisp-current-defun-name): Use edebug specs to find the name (if they exist), and default to returning the top-level symbol if there isn't a define-like form (bug#49592). --- lisp/emacs-lisp/lisp-mode.el | 64 +++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 18 deletions(-) (limited to 'lisp/emacs-lisp/lisp-mode.el') diff --git a/lisp/emacs-lisp/lisp-mode.el b/lisp/emacs-lisp/lisp-mode.el index c906ee6e31d..2e7f019aa9e 100644 --- a/lisp/emacs-lisp/lisp-mode.el +++ b/lisp/emacs-lisp/lisp-mode.el @@ -728,30 +728,58 @@ font-lock keywords will not be case sensitive." len)))) (defun lisp-current-defun-name () - "Return the name of the defun at point, or nil." + "Return the name of the defun at point. +If there is no defun at point, return the first symbol from the +top-level form. If there is no top-level form, return nil. + +(\"defun\" here means \"form that defines something\", and is +decided heuristically.)" (save-excursion - (let ((location (point))) + (let ((location (point)) + name) ;; If we are now precisely at the beginning of a defun, make sure ;; beginning-of-defun finds that one rather than the previous one. - (or (eobp) (forward-char 1)) + (unless (eobp) + (forward-char 1)) (beginning-of-defun) ;; Make sure we are really inside the defun found, not after it. - (when (and (looking-at "\\s(") - (progn (end-of-defun) - (< location (point))) - (progn (forward-sexp -1) - (>= location (point)))) - (if (looking-at "\\s(") - (forward-char 1)) - ;; Skip the defining construct name, typically "defun" or + (when (and (looking-at "(") + (progn + (end-of-defun) + (< location (point))) + (progn + (forward-sexp -1) + (>= location (point)))) + (when (looking-at "(") + (forward-char 1)) + ;; Read the defining construct name, typically "defun" or ;; "defvar". - (forward-sexp 1) - ;; The second element is usually a symbol being defined. If it - ;; is not, use the first symbol in it. - (skip-chars-forward " \t\n'(") - (buffer-substring-no-properties (point) - (progn (forward-sexp 1) - (point))))))) + (let ((symbol (ignore-errors (read (current-buffer))))) + (when (and symbol (not (symbolp symbol))) + (setq symbol nil)) + ;; If there's an edebug spec, use that to determine what the + ;; name is. + (when symbol + (let ((spec (get symbol 'edebug-form-spec))) + (save-excursion + (when (and (eq (car spec) '&define) + (memq 'name spec)) + (pop spec) + (while (and spec (not name)) + (let ((candidate (ignore-errors (read (current-buffer))))) + (when (eq (pop spec) 'name) + (setq name candidate + spec nil)))))))) + ;; We didn't have an edebug spec (or couldn't find the + ;; name). If the symbol starts with \"def\", then it's + ;; likely that the next symbol is the name. + (when (and (not name) + (string-match-p "\\`def" (symbol-name symbol))) + (when-let ((candidate (ignore-errors (read (current-buffer))))) + (when (symbolp candidate) + (setq name candidate)))) + (when-let ((result (or name symbol))) + (symbol-name result))))))) (defvar-keymap lisp-mode-shared-map :doc "Keymap for commands shared by all sorts of Lisp modes." -- cgit v1.2.3 From 909931cb9ad7251393a81076e9020225fe82845e Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Mon, 8 Aug 2022 15:52:53 +0200 Subject: Further lisp-current-defun-name tweaks * lisp/emacs-lisp/lisp-mode.el (lisp-current-defun-name): Further tweaks to finding the symbol being defined (defalias). --- lisp/emacs-lisp/lisp-mode.el | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lisp/emacs-lisp/lisp-mode.el') diff --git a/lisp/emacs-lisp/lisp-mode.el b/lisp/emacs-lisp/lisp-mode.el index 2e7f019aa9e..82afa31ef12 100644 --- a/lisp/emacs-lisp/lisp-mode.el +++ b/lisp/emacs-lisp/lisp-mode.el @@ -776,8 +776,12 @@ decided heuristically.)" (when (and (not name) (string-match-p "\\`def" (symbol-name symbol))) (when-let ((candidate (ignore-errors (read (current-buffer))))) - (when (symbolp candidate) - (setq name candidate)))) + (cond + ((symbolp candidate) + (setq name candidate)) + ((and (consp candidate) + (symbolp (car (delete 'quote candidate)))) + (setq name (car (delete 'quote candidate))))))) (when-let ((result (or name symbol))) (symbol-name result))))))) -- cgit v1.2.3 From fe4fd160a20e2935b9a6aba4dc5dfbb5e26fdfe1 Mon Sep 17 00:00:00 2001 From: Michael Heerdegen Date: Tue, 9 Aug 2022 03:55:14 +0200 Subject: Another lisp-current-defun-name tweak * lisp/emacs-lisp/lisp-mode.el (lisp-current-defun-name): Avoid error when edebug spec is the symbol t. --- lisp/emacs-lisp/lisp-mode.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lisp/emacs-lisp/lisp-mode.el') diff --git a/lisp/emacs-lisp/lisp-mode.el b/lisp/emacs-lisp/lisp-mode.el index 82afa31ef12..1bc2c0ece6d 100644 --- a/lisp/emacs-lisp/lisp-mode.el +++ b/lisp/emacs-lisp/lisp-mode.el @@ -762,7 +762,7 @@ decided heuristically.)" (when symbol (let ((spec (get symbol 'edebug-form-spec))) (save-excursion - (when (and (eq (car spec) '&define) + (when (and (eq (car-safe spec) '&define) (memq 'name spec)) (pop spec) (while (and spec (not name)) -- cgit v1.2.3 From b2bf91003db3bc2171c566ed710ec34d4a55c064 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Tue, 9 Aug 2022 19:19:29 +0200 Subject: Further lisp-current-defun-name tweaks * lisp/emacs-lisp/lisp-mode.el (lisp-current-defun-name): Tweak so that cl-defmethod and friends work again. --- lisp/emacs-lisp/lisp-mode.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lisp/emacs-lisp/lisp-mode.el') diff --git a/lisp/emacs-lisp/lisp-mode.el b/lisp/emacs-lisp/lisp-mode.el index 1bc2c0ece6d..c31fbec640c 100644 --- a/lisp/emacs-lisp/lisp-mode.el +++ b/lisp/emacs-lisp/lisp-mode.el @@ -774,7 +774,7 @@ decided heuristically.)" ;; name). If the symbol starts with \"def\", then it's ;; likely that the next symbol is the name. (when (and (not name) - (string-match-p "\\`def" (symbol-name symbol))) + (string-match-p "\\(\\`\\|-\\)def" (symbol-name symbol))) (when-let ((candidate (ignore-errors (read (current-buffer))))) (cond ((symbolp candidate) -- cgit v1.2.3