]> git.eshelyaron.com Git - emacs.git/commitdiff
Add 'custom-variable' command
authorPhilip Kaludercic <philipk@posteo.net>
Mon, 12 Feb 2024 17:29:50 +0000 (18:29 +0100)
committerEshel Yaron <me@eshelyaron.com>
Wed, 14 Feb 2024 08:16:36 +0000 (09:16 +0100)
* lisp/cus-edit.el (customize-toggle-option): Add command.
(toggle-option): Add shorter alias for 'customize-toggle-option'.
* etc/NEWS: Document it.  (Bug#69079)

(cherry picked from commit 371ccf09fea26892a2fada028d27fb4b596636df)

etc/NEWS
lisp/cus-edit.el

index d779e5628c60c258b00187bd682731308e58dd31..f7dae2e8a2cf0f1b794afed41cf0a898ad533f0f 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1453,6 +1453,10 @@ in Buffer menu mode.
 *** New command 'customize-dirlocals'.
 This command pops up a buffer to edit the settings in ".dir-locals.el".
 
+---
+** New command 'customize-toggle-option'.
+This command can toggle boolean options for the duration of a session.
+
 ** Calc
 
 +++
index 38b6ec984ab0f74bd48b75d3c32e4b2778ced33a..8fad51dc1166c7ddced32b4698f05903ba1204ad 100644 (file)
@@ -1227,6 +1227,41 @@ If OTHER-WINDOW is non-nil, display in another window."
     (unless (eq symbol basevar)
       (message "`%s' is an alias for `%s'" symbol basevar))))
 
+;;;###autoload
+(defun customize-toggle-option (symbol)
+  "Toggle the value of boolean option SYMBOL for this session."
+  (interactive (let ((prompt "Toggle boolean option: ") opts)
+                 (mapatoms
+                  (lambda (sym)
+                    (when (eq (get sym 'custom-type) 'boolean)
+                      (push sym opts))))
+                 (list (intern (completing-read prompt opts nil nil nil nil
+                                                (symbol-at-point))))))
+  (let* ((setter (or (get symbol 'custom-set) #'set-default))
+         (getter (or (get symbol 'custom-get) #'symbol-value))
+         (value (condition-case nil
+                    (funcall getter symbol)
+                  (void-variable (error "`%s' is not bound" symbol))))
+         (type (get symbol 'custom-type)))
+    (cond
+     ((eq type 'boolean))
+     ((and (null type)
+           (yes-or-no-p
+            (format "`%s' doesn't have a type, and has the value %S.  \
+Proceed to toggle?" symbol value))))
+     ((yes-or-no-p
+       (format "`%s' is of type %s, and has the value %S.  \
+Proceed to toggle?"
+               symbol type value)))
+     ((error "Abort toggling of option `%s'" symbol)))
+    (message "%s user options `%s'."
+             (if (funcall setter symbol (not value))
+                 "Enabled" "Disabled")
+             symbol)))
+
+;;;###autoload
+(defalias 'toggle-option #'customize-toggle-option)
+
 ;;;###autoload
 (defalias 'customize-variable-other-window 'customize-option-other-window)