summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp/find-func.el
diff options
context:
space:
mode:
authorAndrea Corallo <akrl@sdf.org>2020-12-27 17:54:57 +0100
committerAndrea Corallo <akrl@sdf.org>2020-12-27 17:54:57 +0100
commit8fb94630136700aa4e74c7fc212b019d2db380ae (patch)
tree69b3938a89f450509a7001f45ba3acca057fb40d /lisp/emacs-lisp/find-func.el
parent271fb8a269aff924070b188f23355d0c368356dd (diff)
parentdf882c9701755e2ae063f05d3381de14ae09951e (diff)
downloademacs-8fb94630136700aa4e74c7fc212b019d2db380ae.tar.gz
emacs-8fb94630136700aa4e74c7fc212b019d2db380ae.tar.bz2
emacs-8fb94630136700aa4e74c7fc212b019d2db380ae.zip
Merge remote-tracking branch 'savannah/master' into HEAD
Diffstat (limited to 'lisp/emacs-lisp/find-func.el')
-rw-r--r--lisp/emacs-lisp/find-func.el65
1 files changed, 64 insertions, 1 deletions
diff --git a/lisp/emacs-lisp/find-func.el b/lisp/emacs-lisp/find-func.el
index cf927729b6c..aafefe02603 100644
--- a/lisp/emacs-lisp/find-func.el
+++ b/lisp/emacs-lisp/find-func.el
@@ -394,7 +394,70 @@ The search is done in the source for library LIBRARY."
(progn
(beginning-of-line)
(cons (current-buffer) (point)))
- (cons (current-buffer) nil)))))))))
+ ;; If the regexp search didn't find the location of
+ ;; the symbol (for example, because it is generated by
+ ;; a macro), try a slightly more expensive search that
+ ;; expands macros until it finds the symbol.
+ (cons (current-buffer)
+ (find-function--search-by-expanding-macros
+ (current-buffer) symbol type))))))))))
+
+(defun find-function--try-macroexpand (form)
+ "Try to macroexpand FORM in full or partially.
+This is a best-effort operation in which if macroexpansion fails,
+this function returns FORM as is."
+ (ignore-errors
+ (or
+ (macroexpand-all form)
+ (macroexpand-1 form)
+ form)))
+
+(defun find-function--any-subform-p (form pred)
+ "Walk FORM and apply PRED to its subexpressions.
+Return t if any PRED returns t."
+ (cond
+ ((not (consp form)) nil)
+ ((funcall pred form) t)
+ (t
+ (cl-destructuring-bind (left-child . right-child) form
+ (or
+ (find-function--any-subform-p left-child pred)
+ (find-function--any-subform-p right-child pred))))))
+
+(defun find-function--search-by-expanding-macros (buf symbol type)
+ "Expand macros in BUF to search for the definition of SYMBOL of TYPE."
+ (catch 'found
+ (with-current-buffer buf
+ (save-excursion
+ (goto-char (point-min))
+ (condition-case nil
+ (while t
+ (let ((form (read (current-buffer)))
+ (expected-symbol-p
+ (lambda (form)
+ (cond
+ ((null type)
+ ;; Check if a given form is a `defalias' to
+ ;; SYM, the function name we are searching
+ ;; for. All functions in Emacs Lisp
+ ;; ultimately expand to a `defalias' form
+ ;; after several steps of macroexpansion.
+ (and (eq (car-safe form) 'defalias)
+ (equal (car-safe (cdr form))
+ `(quote ,symbol))))
+ ((eq type 'defvar)
+ ;; Variables generated by macros ultimately
+ ;; expand to `defvar'.
+ (and (eq (car-safe form) 'defvar)
+ (eq (car-safe (cdr form)) symbol)))
+ (t nil)))))
+ (when (find-function--any-subform-p
+ (find-function--try-macroexpand form)
+ expected-symbol-p)
+ ;; We want to return the location at the beginning
+ ;; of the macro, so move back one sexp.
+ (throw 'found (progn (backward-sexp) (point))))))
+ (end-of-file nil))))))
(defun find-function-library (function &optional lisp-only verbose)
"Return the pair (ORIG-FUNCTION . LIBRARY) for FUNCTION.