Skip to content

Commit dbcaef8

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

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
@@ -647,6 +647,24 @@ overrides that to include previously opened buffers."
647647
(eval-expression expr))
648648
(helpful-update)))
649649

650+
(define-button-type 'helpful-reset-button
651+
'action #'helpful--reset
652+
'symbol nil
653+
'buffer nil
654+
'follow-link t
655+
'help-echo "Reset the value of this symbol")
656+
657+
(defun helpful--reset (button)
658+
"Reset the value of this symbol to its standard value."
659+
(let* ((sym (button-get button 'symbol))
660+
(buf (button-get button 'buffer))
661+
(standard-value (car (helpful--original-value sym))))
662+
(save-current-buffer
663+
(when buf
664+
(set-buffer buf))
665+
(set sym standard-value))
666+
(helpful-update)))
667+
650668
(define-button-type 'helpful-view-literal-button
651669
'action #'helpful--view-literal
652670
'help-echo "Toggle viewing as a literal")
@@ -1857,6 +1875,14 @@ POSITION-HEADS takes the form ((123 (defun foo)) (456 (defun bar)))."
18571875
'symbol sym
18581876
'buffer buffer))
18591877

1878+
(defun helpful--make-reset-button (sym buffer)
1879+
"Make a reset button for SYM in BUFFER."
1880+
(helpful--button
1881+
"Reset"
1882+
'helpful-reset-button
1883+
'symbol sym
1884+
'buffer buffer))
1885+
18601886
(defun helpful--make-toggle-literal-button ()
18611887
"Make set button for SYM in BUFFER."
18621888
(helpful--button
@@ -2406,6 +2432,8 @@ state of the current symbol."
24062432
(when (memq (helpful--sym-value helpful--sym helpful--associated-buffer) '(nil t))
24072433
(insert (helpful--make-toggle-button helpful--sym helpful--associated-buffer) " "))
24082434
(insert (helpful--make-set-button helpful--sym helpful--associated-buffer))
2435+
(when (helpful--original-value-differs-p helpful--sym)
2436+
(insert " " (helpful--make-reset-button helpful--sym helpful--associated-buffer)))
24092437
(when (custom-variable-p helpful--sym)
24102438
(insert " " (helpful--make-customize-button helpful--sym)))))
24112439

test/helpful-unit-test.el

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

1088+
(ert-deftest helpful--reset-only-if-original-value-differs ()
1089+
"Show the reset button for defcustom variables that have been modified."
1090+
(let ((previous-value helpful-test-custom-var))
1091+
(helpful-variable 'helpful-test-custom-var)
1092+
(should
1093+
(s-contains-p "Set Reset Customize\n" (buffer-string)))
1094+
(button-activate (button-at (+ 1 (string-match "Reset" (buffer-string)))))
1095+
(should (equal helpful-test-custom-var
1096+
(eval (car (get 'helpful-test-custom-var 'standard-value)))))
1097+
(should
1098+
(s-contains-p "Set Customize\n" (buffer-string)))
1099+
(setq helpful-test-custom-var previous-value)))
1100+
10881101
(ert-deftest helpful--preserve-position ()
10891102
"Show the original value for defcustom variables."
10901103
(-let [(buf _pos _opened) (helpful--definition 'helpful-test-custom-var nil)]

0 commit comments

Comments
 (0)