From 8655f2690f589117ea8677f4e1dfb1bef78ac75b Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Sat, 21 Nov 2015 16:03:06 -0500 Subject: [PATCH 2/3] Add function to trigger debugger on variable write * lisp/emacs-lisp/debug.el (debug-on-set): (debug--variable-list): (cancel-debug-on-set): New functions. --- lisp/emacs-lisp/debug.el | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lisp/emacs-lisp/debug.el b/lisp/emacs-lisp/debug.el index 0e307fa..a9f6978 100644 --- a/lisp/emacs-lisp/debug.el +++ b/lisp/emacs-lisp/debug.el @@ -765,6 +765,45 @@ (defun debug--implement-debug-on-entry (&rest _ignore) (funcall debugger 'debug)))) ;;;###autoload +(defun debug-on-set (variable) + (interactive + (let* ((var-at-point (variable-at-point)) + (var (and (symbolp var-at-point) var-at-point)) + (val (completing-read + (concat "Debug when setting variable" + (if var (format " (default %s): " var) ": ")) + obarray #'boundp + t nil nil (and var (symbol-name var))))) + (list (if (equal val "") var (intern val))))) + (add-variable-watcher variable #'debug--implement-debug-on-entry)) + + +(defun debug--variable-list () + "List of variables currently set for debug on set." + (let ((vars '())) + (mapatoms + (lambda (s) + (when (and (boundp s) (memq #'debug--implement-debug-on-entry + (get s 'watchers))) + (push s vars)))) + vars)) + +;;;###autoload +(defun cancel-debug-on-set (&optional variable) + (interactive + (list (let ((name + (completing-read + "Cancel debug on set for variable (default all variables): " + (mapcar #'symbol-name (debug--variable-list)) nil t))) + (when name + (unless (string= name "") + (intern name)))))) + (if variable + (remove-variable-watcher variable #'debug--implement-debug-on-entry) + (message "Canceling debug-on-set for all variables") + (mapc #'cancel-debug-on-set (debug--variable-list)))) + +;;;###autoload (defun debug-on-entry (function) "Request FUNCTION to invoke debugger each time it is called. -- 2.6.2