From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Unknown Newsgroups: gmane.emacs.bugs Subject: bug#45443: 28.0.50; Can't find definition of compilation--message->loc Date: Sun, 27 Dec 2020 01:51:29 +0100 Message-ID: References: > <83a6u0n8y7.fsf@gnu.org>> Reply-To: =?UTF-8?Q?Reply-to=3A_Daniel_Mart=C3=83=C2=ADn_=3Cmardani29=40yahoo=2Ees?= =?UTF-8?Q?=3E=0A?= Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="32533"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (darwin) Cc: rms@gnu.org, 45443@debbugs.gnu.org To: Drew Adams Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Dec 27 01:52:16 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ktKIC-0008LJ-KK for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 27 Dec 2020 01:52:16 +0100 Original-Received: from localhost ([::1]:57698 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ktKIB-0004s5-2I for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 26 Dec 2020 19:52:15 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58264) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ktKHx-0004ru-TP for bug-gnu-emacs@gnu.org; Sat, 26 Dec 2020 19:52:01 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:48570) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ktKHx-0007uz-LF for bug-gnu-emacs@gnu.org; Sat, 26 Dec 2020 19:52:01 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ktKHx-0005Dd-JF for bug-gnu-emacs@gnu.org; Sat, 26 Dec 2020 19:52:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Daniel =?UTF-8?Q?Mart=C3=ADn?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 27 Dec 2020 00:52:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 45443 X-GNU-PR-Package: emacs Original-Received: via spool by 45443-submit@debbugs.gnu.org id=B45443.160903030320037 (code B ref 45443); Sun, 27 Dec 2020 00:52:01 +0000 Original-Received: (at 45443) by debbugs.gnu.org; 27 Dec 2020 00:51:43 +0000 Original-Received: from localhost ([127.0.0.1]:60116 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ktKHe-0005D6-Tk for submit@debbugs.gnu.org; Sat, 26 Dec 2020 19:51:43 -0500 Original-Received: from sonic304-23.consmr.mail.ir2.yahoo.com ([77.238.179.148]:39911) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ktKHd-0005Cu-8C for 45443@debbugs.gnu.org; Sat, 26 Dec 2020 19:51:41 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.es; s=s2048; t=1609030295; bh=lIBVoY5tQtEmozK9azOEMf8ILdajGzhJkuB3Qf7GoRE=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From:Subject; b=N8ZdBaQ/oZvMtXMn+221QWNGrgGNdyMm993EnFOMNe7OGRzHN8oOFzDUdSg6YGLtlfhQMh6zHeZkJbwZ6JG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wvVjjENRwnACmk6.KTvC8TD.ZJ4nPgxSS4IkCkc0KaRnMjItGiJ RmpKHQjEBjs5lHngsGummlgRjQUFwFj74dAu6pRpF7D37RaRhnXN4i_b7cnqvRNUVw7JQZofwXfY xby0nvBUW.Lh54eoelXYtzAXJf3aE9fP85BgNlUspNWbumrL.R.zVFFjuh.QPzQeQAyoscNRInWt 72e43DLMrwUjYnI_Xc9GG4aqdiLcg_wIEHWgzZCCz12kurfC1wajr0BHTjraChWX9CTpq6Ipk96b xg2XcXe7MU6IIli2JH_7Y1nRuaIS88188QEo9PXrfljMwO.jAMuCUcB9BpB4DU._gavtRkYznR9o mhX6HTkgpLs3EDe0.dbFo9VihyGeFriRF7tjk6epj0xPzfRHq6eH49Ou54n8cvK_10AtZkhFC.8L Th.6BylrhWI_2k4cn2.WB6aF5urhv9WhQ_Ja.e4JArsYZZovGDvgh9r8JUn_SyzE5IPwM.1BGips FGjTDcQYdeAblhfXimDEINgTuqTQ6tfmQQISE_Ecs4 Original-Received: from sonic.gate.mail.ne1.yahoo.com by sonic304.consmr.mail.ir2.yahoo.com with HTTP; Sun, 27 Dec 2020 00:51:35 +0000 Original-Received: by smtp401.mail.ir2.yahoo.com (VZM Hermes SMTP Server) with ESMTPA ID 8ad96f2d5f15e71497548c3a2dec8691; Sun, 27 Dec 2020 00:51:30 +0000 (UTC) In-Reply-To: (Drew Adams's message of "Sat, 26 Dec 2020 10:58:21 -0800 (PST)") X-Mailer: WebService/1.1.17278 mail.backend.jedi.jws.acl:role.jedi.acl.token.atz.jws.hermes.yahoo Apache-HttpAsyncClient/4.1.4 (Java/11.0.8) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Original-From: Xref: news.gmane.io gmane.emacs.bugs:196782 Archived-At: --=-=-= Content-Type: text/plain Drew Adams writes: >> It's a general problem with uses of cl-defstruct and similar >> constructs: they generate functions and macros that the Help functions >> are unable to find. > > Yep. It's a big doc/help problem, IMO. And > we've moved more and more stuff to things like > `cl-defstruct' (which in itself is fine). > > I don't see a good general solution to this > problem. But it is, I think, something > important for the quintessential > "self-documenting" editor. I truly hope that > some smart and ambitious hacker tackles this. One possible approach is, if the regular expression code fails to find a location, we can fall back to expand macros until we find the definition (a defalias in the case of a function, or a defvar in the case of a variable), or we reach the end of the file. I attach a first implementation of this approach, with some tests for the function definition that Richard wanted to find. I'm sure there are still missing cases that are interesting (closures?). Give it a try and see if there are still relevant symbols whose definition Emacs is unable to locate. I'm also open to feedback about the macro expansion logic. Could it be more efficient? Thanks. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Improve-find-definition-in-Help-buffers.patch >From 34662c47b359792e048bb7020d7eb0fbf94e7838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=ADn?= Date: Sun, 27 Dec 2020 01:07:26 +0100 Subject: [PATCH] Improve "find definition" in *Help* buffers * lisp/emacs-lisp/find-func.el (find-function--search-by-expanding-macros): New internal function that searches for a function or variable by expanding macros in a buffer. * lisp/emacs-lisp/find-func.el (find-function-search-for-symbol): If our regexp algorithm could not find a location for the symbol definition, resort to find-function--search-by-expanding-macros. * test/lisp/emacs-lisp/find-func-tests.el: Add a automatic test for a function and variable generated by a macro. * etc/NEWS: Advertise the improved functionality. --- etc/NEWS | 6 +++ lisp/emacs-lisp/find-func.el | 65 ++++++++++++++++++++++++- test/lisp/emacs-lisp/find-func-tests.el | 10 ++++ 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/etc/NEWS b/etc/NEWS index a320acb5fa..2bfaccd2ef 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -114,6 +114,12 @@ choosing a group, or clicking a button in the "*Help*" buffers when looking at the doc string of a function that belongs to one of these groups. +--- +** Improved "find definition" feature of *Help* buffers. +Now clicking on the link to find the definition of functions generated +by 'cl-defstruct', or variables generated by 'define-derived-mode', +for example, will go to the exact place where they are defined. + ** New variable 'redisplay-skip-initial-frame' to enable batch redisplay tests. Setting it to nil forces the redisplay to do its job even in the initial frame used in batch mode. diff --git a/lisp/emacs-lisp/find-func.el b/lisp/emacs-lisp/find-func.el index 074e7db295..7796a72ecf 100644 --- a/lisp/emacs-lisp/find-func.el +++ b/lisp/emacs-lisp/find-func.el @@ -389,7 +389,70 @@ find-function-search-for-symbol (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. diff --git a/test/lisp/emacs-lisp/find-func-tests.el b/test/lisp/emacs-lisp/find-func-tests.el index d77eb6757f..03df4bb9ff 100644 --- a/test/lisp/emacs-lisp/find-func-tests.el +++ b/test/lisp/emacs-lisp/find-func-tests.el @@ -43,5 +43,15 @@ find-func-tests--library-completion (concat data-directory (kbd "n x / TAB RET")) (read-library-name))))) +;; Avoid a byte-compilation warning that may confuse people reading +;; the result of the following test. +(declare-function compilation--message->loc nil "compile") + +(ert-deftest find-func-tests--locate-macro-generated-symbols () ;bug#45443 + (should (cdr (find-function-search-for-symbol + #'compilation--message->loc nil "compile"))) + (should (cdr (find-function-search-for-symbol + 'c-mode-hook 'defvar "cc-mode")))) + (provide 'find-func-tests) ;;; find-func-tests.el ends here -- 2.28.0 --=-=-=--