branch: master
commit 473c5958db9fccf4e3281191e5e878e6ce1d04a6
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>
Facilitate the sorting of cons cell input to ivy-read
* ivy.el (ivy--reset-state): When an alist is passed to `ivy-read', and
a sorting function is associated via 'caller, the sorting function
will receive two cons cells as arguments, instead of two strings as
usual.
Re #554
Example:
(defvar ivy-sorter-data '(("b 1" . 1) ("a 2" . 2) ("d 0" . 0) ("c 5" . 5)))
(defun isn (a b)
(< (cdr a) (cdr b)))
(add-to-list 'ivy-sort-functions-alist '(ivy-sorter . isn))
(ivy-read "string: " ivy-sorter-data
:sort t
:caller 'ivy-sorter)
---
ivy.el | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/ivy.el b/ivy.el
index f4f590d..4fcabbd 100644
--- a/ivy.el
+++ b/ivy.el
@@ -1499,10 +1499,18 @@ This is useful for recursive `ivy-read'."
(setq coll (ivy--buffer-list "" ivy-use-virtual-buffers
predicate)))
(dynamic-collection
(setq coll (funcall collection ivy-text)))
+ ((and (consp collection) (listp (car collection)))
+ (if (and sort (setq sort-fn (cdr (assoc caller
ivy-sort-functions-alist))))
+ (progn
+ (setq sort nil)
+ (setq coll (mapcar #'car
+ (cl-sort
+ (copy-sequence collection)
+ sort-fn))))
+ (setq coll (all-completions "" collection predicate))))
((or (functionp collection)
(byte-code-function-p collection)
(vectorp collection)
- (and (consp collection) (listp (car collection)))
(hash-table-p collection)
(and (listp collection) (symbolp (car collection))))
(setq coll (all-completions "" collection predicate)))