edebug-defun
Thanks to @abo-abo for the tip to mark the defun first.
Here is a quick way to enable/disable edebug-defun
. This uses search-backward-regexp
to edebug-instrument or eval the defun or defmacro name correctly, whether it is defined in use-package
wrapper or not.
;; Edebug a defun or defmacro
(defvar modi/fns-in-edebug nil
"List of functions for which `edebug' is instrumented.")
(defconst modi/fns-regexp (concat "(\\s-*"
"\\(defun\\|defmacro\\)\\s-+"
"\\(?1:\\(\\w\\|\\s_\\)+\\)\\_>") ; word or symbol char
"Regexp to find defun or defmacro definition.")
(defun modi/toggle-edebug-defun ()
(interactive)
(let (fn)
(save-excursion
(search-backward-regexp modi/fns-regexp)
(setq fn (match-string 1))
(mark-sexp)
(narrow-to-region (point) (mark))
(if (member fn modi/fns-in-edebug)
;; If the function is already being edebugged, uninstrument it
(progn
(setq modi/fns-in-edebug (delete fn modi/fns-in-edebug))
(eval-region (point) (mark))
(setq-default eval-expression-print-length 12)
(setq-default eval-expression-print-level 4)
(message "Edebug disabled: %s" fn))
;; If the function is not being edebugged, instrument it
(progn
(add-to-list 'modi/fns-in-edebug fn)
(setq-default eval-expression-print-length nil)
(setq-default eval-expression-print-level nil)
(edebug-defun)
(message "Edebug: %s" fn)))
(widen))))
Usage - With the point anywhere in the function/macro body, calling modi/toggle-edebug-defun
will enable or disable edebug
for that entity.
debug-on-entry
Thanks to @glucas and @Drew, I have cooked up a convenient way to toggle debug-on-entry
. This also uses the same approach as above to parse the defun/defmacro name.
;; Debug on entry
(defvar modi/fns-in-debug nil
"List of functions for which `debug-on-entry' is instrumented.")
(defun modi/toggle-debug-defun ()
(interactive)
(let (fn)
(save-excursion
(search-backward-regexp modi/fns-regexp)
(setq fn (match-string 1)))
(if (member fn modi/fns-in-debug)
;; If the function is already being debugged, cancel its debug on entry
(progn
(setq modi/fns-in-debug (delete fn modi/fns-in-debug))
(cancel-debug-on-entry (intern fn))
(message "Debug-on-entry disabled: %s" fn))
;; If the function is not being debugged, debug it on entry
(progn
(add-to-list 'modi/fns-in-debug fn)
(debug-on-entry (intern fn))
(message "Debug-on-entry: %s" fn)))))
Usage - With the point anywhere in the function body, calling modi/toggle-debug-defun
will enable or cancel the debug-on-error
for that function.