Subject: Re: bug#25393: FEATURE REQUEST: *Backtrace* -- C source code def highlight + jump to def.

From: Gemini Lasswell <[email-masked]>
Date: Wed, 08 Aug 2018 17:11:21 -0700

Hi Keith,

The new backtrace mode, in the master branch as of da0054c307, makes
links for built-in functions.

The attached patch improves on current master by making links to C
functions work even when those functions are advised. See
http://lists.gnu.org/archive/html/emacs-devel/2018-08/msg00025.html for
Clément's report of that problem.

>From 7737f48840d3c07b09506eb4b4743646d27cd2aa Mon Sep 17 00:00:00 2001
From: Gemini Lasswell <[email-masked]>
Date: Tue, 7 Aug 2018 19:39:06 -0700
Subject: [PATCH] Fix links in backtraces to work on advised built-ins
 (Bug#25393)

* lisp/emacs-lisp/backtrace.el (backtrace--print-func-and-args): Make
links to the original definition of advised functions.  Handle the
case when the function slot of the backtrace frame contains the
definition of a built-in function.
---
 lisp/emacs-lisp/backtrace.el | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/lisp/emacs-lisp/backtrace.el b/lisp/emacs-lisp/backtrace.el
index f13b43b465..e82d4f5a5a 100644
--- a/lisp/emacs-lisp/backtrace.el
+++ b/lisp/emacs-lisp/backtrace.el
@@ -34,6 +34,7 @@
 (eval-when-compile (require 'cl-lib))
 (eval-when-compile (require 'pcase))
 (eval-when-compile (require 'subr-x))        ; if-let
+(require 'find-func)
 (require 'help-mode)     ; Define `help-function-def' button type.
 (require 'lisp-mode)
 
@@ -735,11 +736,11 @@ backtrace--print-func-and-args
          (evald (backtrace-frame-evald frame))
          (fun   (backtrace-frame-fun frame))
          (args  (backtrace-frame-args frame))
-         (def   (and (symbolp fun) (fboundp fun) (symbol-function fun)))
+         (def   (find-function-advised-original fun))
          (fun-file (or (symbol-file fun 'defun)
-                            (and (subrp def)
-                                 (not (eq 'unevalled (cdr (subr-arity def))))
-                                 (find-lisp-object-file-name fun def))))
+                       (and (subrp def)
+                            (not (eq 'unevalled (cdr (subr-arity def))))
+                            (find-lisp-object-file-name fun def))))
          (fun-pt (point)))
     (cond
      ((and evald (not debugger-stack-frame-as-list))
@@ -762,7 +763,8 @@ backtrace--print-func-and-args
         (insert (backtrace--print-to-string fun-and-args)))
       (cl-incf fun-pt)))
     (when fun-file
-      (make-text-button fun-pt (+ fun-pt (length (symbol-name fun)))
+      (make-text-button fun-pt (+ fun-pt
+                                  (length (backtrace--print-to-string fun)))
                         :type 'help-function-def
                         'help-args (list fun fun-file)))
     ;; After any frame that uses eval-buffer, insert a comment that
-- 
2.16.4

Keith David Bershatsky <[email-masked]> writes:

> A check should probably be included to see whether the
> `find-function-C-source-directory' contains C source code files before
> creating highlighting/buttons for jumping:

I chose not to do this. I think it's better to make the buttons
and give an error message when they are pressed if
'find-function-C-source-directory' is misconfigured, because then the
error message lets the user know that there is a configuration problem
so that she or he can try to fix it.

Let me know if you are able to give this a try and how it works for you.

Thanks,
Gemini