From 8410b249225da3f6912333a893d49e42918a0d4f Mon Sep 17 00:00:00 2001 From: Nacho Barrientos Date: Tue, 14 Feb 2023 15:37:43 +0100 Subject: [PATCH] Add a button to reset a variable to its standard value --- helpful.el | 28 ++++++++++++++++++++++++++++ test/helpful-unit-test.el | 13 +++++++++++++ 2 files changed, 41 insertions(+) diff --git a/helpful.el b/helpful.el index 474a10b..8563fe6 100644 --- a/helpful.el +++ b/helpful.el @@ -625,6 +625,24 @@ overrides that to include previously opened buffers." (eval-expression expr)) (helpful-update))) +(define-button-type 'helpful-reset-button + 'action #'helpful--reset + 'symbol nil + 'buffer nil + 'follow-link t + 'help-echo "Reset the value of this symbol") + +(defun helpful--reset (button) + "Reset the value of this symbol to its standard value." + (let* ((sym (button-get button 'symbol)) + (buf (button-get button 'buffer)) + (standard-value (car (helpful--original-value sym)))) + (save-current-buffer + (when buf + (set-buffer buf)) + (set sym standard-value)) + (helpful-update))) + (define-button-type 'helpful-view-literal-button 'action #'helpful--view-literal 'help-echo "Toggle viewing as a literal") @@ -1823,6 +1841,14 @@ POSITION-HEADS takes the form ((123 (defun foo)) (456 (defun bar)))." 'symbol sym 'buffer buffer)) +(defun helpful--make-reset-button (sym buffer) + "Make a reset button for SYM in BUFFER." + (helpful--button + "Reset" + 'helpful-reset-button + 'symbol sym + 'buffer buffer)) + (defun helpful--make-toggle-literal-button () "Make set button for SYM in BUFFER." (helpful--button @@ -2315,6 +2341,8 @@ state of the current symbol." (when (memq (helpful--sym-value helpful--sym helpful--associated-buffer) '(nil t)) (insert (helpful--make-toggle-button helpful--sym helpful--associated-buffer) " ")) (insert (helpful--make-set-button helpful--sym helpful--associated-buffer)) + (when (helpful--original-value-differs-p helpful--sym) + (insert " " (helpful--make-reset-button helpful--sym helpful--associated-buffer))) (when (custom-variable-p helpful--sym) (insert " " (helpful--make-customize-button helpful--sym))))) diff --git a/test/helpful-unit-test.el b/test/helpful-unit-test.el index 0f5177b..c18407c 100644 --- a/test/helpful-unit-test.el +++ b/test/helpful-unit-test.el @@ -1066,6 +1066,19 @@ find the source code." (should (s-contains-p "Original Value\n123" (buffer-string)))) +(ert-deftest helpful--reset-only-if-original-value-differs () + "Show the reset button for defcustom variables that have been modified." + (let ((previous-value helpful-test-custom-var)) + (helpful-variable 'helpful-test-custom-var) + (should + (s-contains-p "Set Reset Customize\n" (buffer-string))) + (button-activate (button-at (+ 1 (string-match "Reset" (buffer-string))))) + (should (equal helpful-test-custom-var + (eval (car (get 'helpful-test-custom-var 'standard-value))))) + (should + (s-contains-p "Set Customize\n" (buffer-string))) + (setq helpful-test-custom-var previous-value))) + (ert-deftest helpful--preserve-position () "Show the original value for defcustom variables." (-let [(buf _pos _opened) (helpful--definition 'helpful-test-custom-var nil)]