branch: master
commit 792b7b7ca87616559fe7d6a5c24da1676e0ad4a6
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>
counsel.el (counsel-set-variable): New command
* counsel.el (counsel-set-variable-history): New defvar.
(counsel-read-setq-expression): New defun.
(counsel--setq-doconst): New defun.
---
Makefile | 4 ++--
counsel.el | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 76 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
index 009086f..ca1d043 100644
--- a/Makefile
+++ b/Makefile
@@ -2,8 +2,6 @@ emacs ?= emacs
LOAD = -l colir.el -l ivy.el -l swiper.el -l counsel.el
-.PHONY: all compile clean
-
all: test
test:
@@ -17,3 +15,5 @@ plain:
clean:
rm -f *.elc
+
+.PHONY: all compile clean test
diff --git a/counsel.el b/counsel.el
index 73ffa21..9fef1da 100644
--- a/counsel.el
+++ b/counsel.el
@@ -474,6 +474,80 @@ Update the minibuffer with the amount of lines collected
every
(intern x)))
:caller 'counsel-describe-function)))
+;;** `counsel-set-variable'
+(defvar counsel-set-variable-history nil
+ "Store history for `counsel-set-variable'.")
+
+(defun counsel-read-setq-expression (sym)
+ "Read and eval a setq expression for SYM."
+ (setq this-command 'eval-expression)
+ (let* ((minibuffer-completing-symbol t)
+ (sym-value (symbol-value sym))
+ (expr (minibuffer-with-setup-hook
+ (lambda ()
+ (add-hook 'completion-at-point-functions
#'elisp-completion-at-point nil t)
+ (run-hooks 'eval-expression-minibuffer-setup-hook)
+ (goto-char (minibuffer-prompt-end))
+ (forward-char 6)
+ (insert (format "%S " sym)))
+ (read-from-minibuffer "Eval: "
+ (format
+ (if (and sym-value (consp sym-value))
+ "(setq '%S)"
+ "(setq %S)")
+ sym-value)
+ read-expression-map t
+ 'read-expression-history))))
+ (eval-expression expr)))
+
+(defun counsel--setq-doconst (x)
+ "Return a cons of description and value for X.
+X is an item of a radio- or choice-type defcustom."
+ (let (y)
+ (when (and (listp x)
+ (consp (setq y (last x))))
+ (setq x (car y))
+ (cons (prin1-to-string x)
+ (if (symbolp x)
+ (list 'quote x)
+ x)))))
+
+;;;###autoload
+(defun counsel-set-variable ()
+ "Set a variable, with completion.
+
+When the selected variable is a `defcustom' with the type boolean
+or radio, offer completion of all possible values.
+
+Otherwise, offer a variant of `eval-expression', with the initial
+input corresponding to the chosen variable."
+ (interactive)
+ (let ((sym (intern
+ (ivy-read "Variable: "
+ (counsel-variable-list)
+ :history 'counsel-set-variable-history)))
+ sym-type
+ cands)
+ (if (and (boundp sym)
+ (setq sym-type (get sym 'custom-type))
+ (cond
+ ((and (consp sym-type)
+ (memq (car sym-type) '(choice radio)))
+ (setq cands (delq nil (mapcar #'counsel--setq-doconst (cdr
sym-type)))))
+ ((eq sym-type 'boolean)
+ (setq cands '(("nil" . nil) ("t" . t))))
+ (t nil)))
+ (let ((res (ivy-read (format "Set (%S): " sym)
+ cands
+ :preselect (symbol-name (symbol-value sym)))))
+ (when res
+ (setq res
+ (if (assoc res cands)
+ (cdr (assoc res cands))
+ (read res)))
+ (eval `(setq ,sym ,res))))
+ (counsel-read-setq-expression sym))))
+
;;** `counsel-info-lookup-symbol'
(defvar info-lookup-mode)
(declare-function info-lookup->completions "info-look")