Skip to content

Commit 11649c3

Browse files
committed
Fix counsel-M-x regression with amx/smex
Historically counsel-M-x transformed the amx and smex collections into something that could be manipulated uniformly with the default obarray candidates. Recently this was refactored and opened up via the user option counsel-M-x-collection to arbitrary completion tables. The counsel-M-x :predicate was adapted accordingly, but I forgot to check other relevant actions and key bindings. This patch tries to introduce a bit more uniformity in action argument handling, at least as is relevant to counsel-M-x. * ivy.el (ivy--action-cand-to-str): New convenience function. (ivy--action-insert, ivy--action-copy): * counsel.el (counsel--info-lookup-symbol, counsel--find-symbol): Use it to handle symbol-keyed alists. (counsel--action-cand-to-interned): New convenience function, similar in spirit but opposite to ivy--action-cand-to-str. (counsel--describe-function): Use it to handle alists. (counsel-M-x-action): Handle alists (#3078) and as yet unknown atoms for flexibility. (counsel-descbinds-action-find): Simplify now that counsel--find-symbol takes symbols. (counsel-descbinds-action-info): Simplify; counsel-info-lookup-symbol takes both symbols and strings. Fixes #3078.
1 parent 4defb81 commit 11649c3

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

counsel.el

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,9 @@ Used by commands `counsel-describe-symbol',
477477
(defun counsel--info-lookup-symbol ()
478478
"Lookup the current symbol in the info docs."
479479
(interactive)
480-
(ivy-exit-with-action #'counsel-info-lookup-symbol))
480+
(ivy-exit-with-action
481+
(lambda (x)
482+
(counsel-info-lookup-symbol (ivy--action-cand-to-str x)))))
481483
(ivy--no-M-x #'counsel--info-lookup-symbol #'ivy--minibuffer-p)
482484

483485
(defun counsel--push-xref-marker (&optional m)
@@ -497,10 +499,11 @@ Used by commands `counsel-describe-symbol',
497499
(ring-insert find-tag-marker-ring (or m (point-marker)))))
498500

499501
(defun counsel--find-symbol (x)
500-
"Find symbol definition that corresponds to string X."
502+
"Find symbol definition that corresponds to action candidate X."
501503
(with-ivy-window
502504
(counsel--push-xref-marker)
503-
(let ((full-name (get-text-property 0 'full-name x)))
505+
(let* ((x (ivy--action-cand-to-str x))
506+
(full-name (get-text-property 0 'full-name x)))
504507
(if full-name
505508
(find-library full-name)
506509
(let ((sym (read x)))
@@ -586,9 +589,16 @@ Variables declared using `defcustom' are highlighted according to
586589
(function-item ivy-thing-at-point)
587590
(function-item ivy-function-called-at-point)))
588591

592+
;; FIXME: Use this more in place of `intern'.
593+
(defun counsel--action-cand-to-interned (x)
594+
"Try to return Ivy action argument X as an existing symbol.
595+
Not quite the dual of `ivy--action-cand-to-str'."
596+
(intern-soft (if (consp x) (car x) x)))
597+
589598
(defun counsel--describe-function (candidate)
590599
"Pass string CANDIDATE to `counsel-describe-function-function'."
591-
(funcall counsel-describe-function-function (intern candidate)))
600+
(funcall counsel-describe-function-function
601+
(counsel--action-cand-to-interned candidate)))
592602

593603
;;;###autoload
594604
(defun counsel-describe-function ()
@@ -995,9 +1005,17 @@ that returns a completion table suitable for `ivy-read'."
9951005
"History for `counsel-M-x'.")
9961006

9971007
(defun counsel-M-x-action (cmd)
998-
"Execute CMD."
999-
(setq cmd (intern
1000-
(subst-char-in-string ?\s ?- (string-remove-prefix "^" cmd))))
1008+
"Execute CMD from `counsel-M-x'."
1009+
;; Currently CMD is a string either following `ivy-immediate-done',
1010+
;; or for all collection types but alist, where CMD is the original
1011+
;; cons. There is no harm in allowing other atoms through.
1012+
(setq cmd (cond ((stringp cmd)
1013+
;; For the benefit of `ivy-immediate-done'.
1014+
;; FIXME: Check `intern-soft'?
1015+
(intern (subst-char-in-string
1016+
?\s ?- (string-remove-prefix "^" cmd))))
1017+
((atom cmd) cmd)
1018+
((intern-soft (car cmd)))))
10011019
(counsel--M-x-extern-rank cmd)
10021020
;; As per `execute-extended-command'.
10031021
(setq this-command cmd)
@@ -1237,13 +1255,13 @@ See `execute-extended-command' for further information."
12371255
"Find symbol definition of candidate X.
12381256
See `counsel--find-symbol' for further information."
12391257
(let ((cmd (cddr x)))
1240-
(counsel--find-symbol (symbol-name cmd))))
1258+
(counsel--find-symbol cmd)))
12411259

12421260
(defun counsel-descbinds-action-info (x)
12431261
"Display symbol definition of candidate X, as found in the relevant manual.
12441262
See `info-lookup-symbol' for further information."
12451263
(let ((cmd (cddr x)))
1246-
(counsel-info-lookup-symbol (symbol-name cmd))))
1264+
(counsel-info-lookup-symbol cmd)))
12471265

12481266
;;;###autoload
12491267
(defun counsel-descbinds (&optional prefix buffer)

ivy.el

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4796,25 +4796,32 @@ Otherwise, forward to `ivy-kill-line'."
47964796

47974797
(ivy-set-actions
47984798
t
4799-
'(("i" ivy--action-insert "insert")
4800-
("w" ivy--action-copy "copy")))
4799+
`(("i" ,#'ivy--action-insert "insert")
4800+
("w" ,#'ivy--action-copy "copy")))
48014801

48024802
(defun ivy--trim-grep-line-number (x)
48034803
(if (string-match ":[0-9]+:" x)
48044804
(substring x (match-end 0))
48054805
x))
48064806

4807+
(defun ivy--action-cand-to-str (x)
4808+
"Try to return Ivy action argument X as a string."
4809+
(let ((x (if (consp x) (car x) x)))
4810+
(if (symbolp x) (symbol-name x) x)))
4811+
48074812
(defun ivy--action-insert (x)
4808-
(insert
4809-
(if (stringp x)
4810-
(ivy--trim-grep-line-number x)
4811-
(car x))))
4813+
"Insert completion candidate X into current buffer at point."
4814+
(insert (funcall (if (stringp x)
4815+
#'ivy--trim-grep-line-number
4816+
#'ivy--action-cand-to-str)
4817+
x)))
48124818

48134819
(defun ivy--action-copy (x)
4814-
(kill-new
4815-
(if (stringp x)
4816-
(ivy--trim-grep-line-number x)
4817-
(car x))))
4820+
"Add completion candidate X to the kill ring."
4821+
(kill-new (funcall (if (stringp x)
4822+
#'ivy--trim-grep-line-number
4823+
#'ivy--action-cand-to-str)
4824+
x)))
48184825

48194826
(defun ivy--switch-buffer-matcher (regexp candidates)
48204827
"Return REGEXP matching CANDIDATES.

0 commit comments

Comments
 (0)