bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#54119: 29.0.50; Edebug: Jumping commands in recursive definitions


From: Michael Heerdegen
Subject: bug#54119: 29.0.50; Edebug: Jumping commands in recursive definitions
Date: Fri, 25 Feb 2022 04:36:47 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

Michael Heerdegen <michael_heerdegen@web.de> writes:

> #+begin_src emacs-lisp
> (defun my-factorial (n) (if (< n 2) 1 (* n (my-factorial (1- n)))))
> #+end_src

We could exploit the fact that the function is already instrumented.
Every function call of the definition is then wrapped in an
`edebug-after' call:

#+begin_src emacs-lisp
(symbol-function 'my-factorial)  ==>
 (closure (t) (n)
  (edebug-enter 'my-factorial
                (list n)
                #'(lambda nil
                    (edebug-after
                     (edebug-before 0)
                     12
                     (if
                         (edebug-after
                          (edebug-before 1)
                          3
                          (<
                           (edebug-after 0 2 n)
                           2))
                         1
                       (edebug-after
                        (edebug-before 4)
                        11 (* etc...)))))))
#+end_src

We could solve this bug if we temporarily change the function binding of
`edebug-after'.  This is a bit tricky, though, mainly because it's not
trivial to restore the original setup:

- two places must be updated: (symbol-function 'edebug-after) and the
  entry in `edebug-behavior-alist'
- `edebug-forward-sexp' terminates immediately, it's on the "meta
  level", undoing the change in that function is too early, since we
  must expect recursive calls of `edebug-after'.
- the actual code is evaluated as an argument of `edebug-after', so the
  replacement can also not restore the original function binding

Which means: either we also redefine `edebug-before', or we use
something very different.  Here is a proof of concept using
`post-command-hook' for restoring:

#+begin_src emacs-lisp
(defun my-edebug-forward-sexp--around-ad (f &rest args)
    (cl-macrolet ((after-fun ()
                    '(nth 2 (cdr (assq 'edebug edebug-behavior-alist)))))
      (let ((orig-after-fun (after-fun)))
        (cl-labels ((set-after-fun (f)
                    (setf (symbol-function 'edebug-after)
                          (setf (after-fun) f)))
                    (reset-after-fun ()
                      (remove-hook 'post-command-hook #'reset-after-fun)
                      (set-after-fun orig-after-fun)))
          (set-after-fun #'edebug-fast-after)
          (add-hook 'post-command-hook #'reset-after-fun)
          (apply f args)))))

(advice-add 'edebug-forward-sexp :around #'my-edebug-forward-sexp--around-ad)
(advice-add 'edebug-step-out     :around #'my-edebug-forward-sexp--around-ad)
#+end_src

Seems to do the job.  A patch would not need an advice of course.

Better ideas welcome.


Michael.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]