]> git.eshelyaron.com Git - emacs.git/commitdiff
; Show option state (ON/OFF) in 'toggle-option' completions list
authorEshel Yaron <me@eshelyaron.com>
Sun, 18 Feb 2024 21:08:48 +0000 (22:08 +0100)
committerEshel Yaron <me@eshelyaron.com>
Sun, 18 Feb 2024 21:08:48 +0000 (22:08 +0100)
* lisp/cus-edit.el (custom-boolean-option-on)
(custom-boolean-option-off): New faces.
(customize-read-boolean-option-affixation): New function.
(customize-read-boolean-option): Use it.
(customize-toggle-option): Update.

lisp/cus-edit.el

index 004cd2de9951180a0d8bb7015fca30e6e7b7f931..ff634b666ce696b447c430562219dc7a23ce0fd3 100644 (file)
@@ -1231,6 +1231,22 @@ If OTHER-WINDOW is non-nil, display in another window."
     (unless (eq symbol basevar)
       (message "`%s' is an alias for `%s'" symbol basevar))))
 
+(defface custom-boolean-option-on
+  '((t :inherit success))
+  "Face for the \"ON\" indicator of boolean user option.")
+
+(defface custom-boolean-option-off
+  '((t :inherit error))
+  "Face for the \"OFF\" indicator of boolean user option.")
+
+(defun customize-read-boolean-option-affixation (names)
+  (let ((on  (propertize "ON  " 'face 'custom-boolean-option-on))
+        (off (propertize "OFF " 'face 'custom-boolean-option-off)))
+    (mapcar
+     (lambda (name)
+       (list name (if (symbol-value (intern name)) on off) ""))
+     names)))
+
 (defun customize-read-boolean-option (&optional any)
   "Read a boolean user option and return its name as a symbol.
 
@@ -1244,24 +1260,30 @@ ANY is non-nil, accept any user option instead, regardless of its type."
         (val (completing-read
               (format-prompt "Toggle option" default)
               (completion-table-with-metadata
-               obarray '(metadata (category . boolean-option)))
+               obarray '(metadata (category . boolean-option)
+                                   (affixation-function
+                                    . customize-read-boolean-option-affixation)))
               pred t nil 'customize-option-history default)))
     (when (string-empty-p val) (user-error "You didn't specify an option"))
     (intern val)))
 
 ;;;###autoload
-(defun customize-toggle-option (symbol)
-  "Toggle boolean user option SYMBOL.
+(defun customize-toggle-option (option)
+  "Toggle boolean user OPTION.
 
-Interactively, prompt for SYMBOL, with completion.  Normally, completion
+Interactively, prompt for OPTION, with completion.  Normally, completion
 candidates include only option with custom type `boolean'.  With a
 prefix argument, completion candidates include all user options instead."
   (interactive (list (customize-read-boolean-option current-prefix-arg)))
-  (let* ((set (or (get symbol 'custom-set) #'set-default))
-         (get (or (get symbol 'custom-get) #'symbol-value))
-        (new (not (funcall get symbol))))
-    (funcall set symbol new)
-    (message "`%s' is now %s." symbol (if new "ON" "OFF"))))
+  (let* ((sym (if (symbolp option) option (intern option)))
+         (set (or (get sym 'custom-set) #'set-default))
+         (get (or (get sym 'custom-get) #'symbol-value))
+        (new (not (funcall get sym))))
+    (funcall set sym new)
+    (message "`%s' is now %s" sym
+             (if new
+                 (propertize "ON" 'face 'success)
+               (propertize "OFF" 'face 'error)))))
 
 ;;;###autoload
 (defalias 'toggle-option #'customize-toggle-option)