Skip to content

Commit f7defa8

Browse files
committed
Add a button to reset a variable to its standard value
1 parent 94c2533 commit f7defa8

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

helpful.el

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,24 @@ overrides that to include previously opened buffers."
625625
(eval-expression expr))
626626
(helpful-update)))
627627

628+
(define-button-type 'helpful-reset-button
629+
'action #'helpful--reset
630+
'symbol nil
631+
'buffer nil
632+
'follow-link t
633+
'help-echo "Reset the value of this symbol")
634+
635+
(defun helpful--reset (button)
636+
"Reset the value of this symbol to its standard value."
637+
(let* ((sym (button-get button 'symbol))
638+
(buf (button-get button 'buffer))
639+
(standard-value (car (helpful--original-value sym))))
640+
(save-current-buffer
641+
(when buf
642+
(set-buffer buf))
643+
(set sym standard-value))
644+
(helpful-update)))
645+
628646
(define-button-type 'helpful-view-literal-button
629647
'action #'helpful--view-literal
630648
'help-echo "Toggle viewing as a literal")
@@ -1823,6 +1841,14 @@ POSITION-HEADS takes the form ((123 (defun foo)) (456 (defun bar)))."
18231841
'symbol sym
18241842
'buffer buffer))
18251843

1844+
(defun helpful--make-reset-button (sym buffer)
1845+
"Make a reset button for SYM in BUFFER."
1846+
(helpful--button
1847+
"Reset"
1848+
'helpful-reset-button
1849+
'symbol sym
1850+
'buffer buffer))
1851+
18261852
(defun helpful--make-toggle-literal-button ()
18271853
"Make set button for SYM in BUFFER."
18281854
(helpful--button
@@ -2315,6 +2341,8 @@ state of the current symbol."
23152341
(when (memq (helpful--sym-value helpful--sym helpful--associated-buffer) '(nil t))
23162342
(insert (helpful--make-toggle-button helpful--sym helpful--associated-buffer) " "))
23172343
(insert (helpful--make-set-button helpful--sym helpful--associated-buffer))
2344+
(when (helpful--original-value-differs-p helpful--sym)
2345+
(insert " " (helpful--make-reset-button helpful--sym helpful--associated-buffer)))
23182346
(when (custom-variable-p helpful--sym)
23192347
(insert " " (helpful--make-customize-button helpful--sym)))))
23202348

test/helpful-unit-test.el

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,19 @@ find the source code."
10661066
(should
10671067
(s-contains-p "Original Value\n123" (buffer-string))))
10681068

1069+
(ert-deftest helpful--reset-only-if-original-value-differs ()
1070+
"Show the reset button for defcustom variables that have been modified."
1071+
(let ((previous-value helpful-test-custom-var))
1072+
(helpful-variable 'helpful-test-custom-var)
1073+
(should
1074+
(s-contains-p "Set Reset Customize\n" (buffer-string)))
1075+
(setq helpful-test-custom-var
1076+
(eval (car (get 'helpful-test-custom-var 'standard-value))))
1077+
(helpful-variable 'helpful-test-custom-var)
1078+
(should
1079+
(s-contains-p "Set Customize\n" (buffer-string)))
1080+
(setq helpful-test-custom-var previous-value)))
1081+
10691082
(ert-deftest helpful--preserve-position ()
10701083
"Show the original value for defcustom variables."
10711084
(-let [(buf _pos _opened) (helpful--definition 'helpful-test-custom-var nil)]

0 commit comments

Comments
 (0)