Skip to content

Commit ad2121b

Browse files
nbarrientosslotThe
authored andcommitted
Add a button to reset a variable to its standard value
NOTE: This is Wilfred#313
1 parent 331d53e commit ad2121b

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
@@ -638,6 +638,24 @@ overrides that to include previously opened buffers."
638638
(eval-expression expr))
639639
(helpful-update)))
640640

641+
(define-button-type 'helpful-reset-button
642+
'action #'helpful--reset
643+
'symbol nil
644+
'buffer nil
645+
'follow-link t
646+
'help-echo "Reset the value of this symbol")
647+
648+
(defun helpful--reset (button)
649+
"Reset the value of this symbol to its standard value."
650+
(let* ((sym (button-get button 'symbol))
651+
(buf (button-get button 'buffer))
652+
(standard-value (car (helpful--original-value sym))))
653+
(save-current-buffer
654+
(when buf
655+
(set-buffer buf))
656+
(set sym standard-value))
657+
(helpful-update)))
658+
641659
(define-button-type 'helpful-view-literal-button
642660
'action #'helpful--view-literal
643661
'help-echo "Toggle viewing as a literal")
@@ -1844,6 +1862,14 @@ POSITION-HEADS takes the form ((123 (defun foo)) (456 (defun bar)))."
18441862
'symbol sym
18451863
'buffer buffer))
18461864

1865+
(defun helpful--make-reset-button (sym buffer)
1866+
"Make a reset button for SYM in BUFFER."
1867+
(helpful--button
1868+
"Reset"
1869+
'helpful-reset-button
1870+
'symbol sym
1871+
'buffer buffer))
1872+
18471873
(defun helpful--make-toggle-literal-button ()
18481874
"Make set button for SYM in BUFFER."
18491875
(helpful--button
@@ -2393,6 +2419,8 @@ state of the current symbol."
23932419
(when (memq (helpful--sym-value helpful--sym helpful--associated-buffer) '(nil t))
23942420
(insert (helpful--make-toggle-button helpful--sym helpful--associated-buffer) " "))
23952421
(insert (helpful--make-set-button helpful--sym helpful--associated-buffer))
2422+
(when (helpful--original-value-differs-p helpful--sym)
2423+
(insert " " (helpful--make-reset-button helpful--sym helpful--associated-buffer)))
23962424
(when (custom-variable-p helpful--sym)
23972425
(insert " " (helpful--make-customize-button helpful--sym)))))
23982426

test/helpful-unit-test.el

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

1080+
(ert-deftest helpful--reset-only-if-original-value-differs ()
1081+
"Show the reset button for defcustom variables that have been modified."
1082+
(let ((previous-value helpful-test-custom-var))
1083+
(helpful-variable 'helpful-test-custom-var)
1084+
(should
1085+
(s-contains-p "Set Reset Customize\n" (buffer-string)))
1086+
(button-activate (button-at (+ 1 (string-match "Reset" (buffer-string)))))
1087+
(should (equal helpful-test-custom-var
1088+
(eval (car (get 'helpful-test-custom-var 'standard-value)))))
1089+
(should
1090+
(s-contains-p "Set Customize\n" (buffer-string)))
1091+
(setq helpful-test-custom-var previous-value)))
1092+
10801093
(ert-deftest helpful--preserve-position ()
10811094
"Show the original value for defcustom variables."
10821095
(-let [(buf _pos _opened) (helpful--definition 'helpful-test-custom-var nil)]

0 commit comments

Comments
 (0)