From 53bdb1fa6e0993fdaca09fd26e0e9f791c8b554a Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Sun, 18 Feb 2024 22:08:48 +0100 Subject: [PATCH] ; Show option state (ON/OFF) in 'toggle-option' completions list * 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 | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/lisp/cus-edit.el b/lisp/cus-edit.el index 004cd2de995..ff634b666ce 100644 --- a/lisp/cus-edit.el +++ b/lisp/cus-edit.el @@ -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) -- 2.39.5