branch: master
commit 1d8b4f8a9d920be29cace30acf72696dea7a87e4
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>
ivy.el (ivy-sort-matches-functions-alist): New sorting for ivy-switch-buffer
* ivy.el (ivy-sort-function-buffer): New defun.
Fixes #595
---
ivy.el | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)
diff --git a/ivy.el b/ivy.el
index 11e764b..49c28bf 100644
--- a/ivy.el
+++ b/ivy.el
@@ -2319,7 +2319,8 @@ CANDIDATES are assumed to be static."
res))))
(setq ivy--all-candidates res)))
-(defcustom ivy-sort-matches-functions-alist '((t . nil))
+(defcustom ivy-sort-matches-functions-alist '((t . nil)
+ (ivy-switch-buffer .
ivy-sort-function-buffer))
"An alist of functions for sorting matching candidates.
Unlike `ivy-sort-functions-alist', which is used to sort the
@@ -2382,6 +2383,25 @@ Prefix matches to NAME are put ahead of the list."
(nreverse res-prefix)
(nreverse res-noprefix)))))
+(defun ivy-sort-function-buffer (name candidates)
+ "Re-sort CANDIDATES.
+Prefer first \"^*NAME\", then \"^NAME\"."
+ (if (or (string-match "^\\^" name) (string= name ""))
+ candidates
+ (let* ((base-re (funcall ivy--regex-function name))
+ (re-prefix (concat "^\\*" base-re))
+ res-prefix
+ res-noprefix)
+ (unless (cl-find-if (lambda (s) (string-match re-prefix s)) candidates)
+ (setq re-prefix (concat "^" base-re)))
+ (dolist (s candidates)
+ (if (string-match re-prefix s)
+ (push s res-prefix)
+ (push s res-noprefix)))
+ (nconc
+ (nreverse res-prefix)
+ (nreverse res-noprefix)))))
+
(defun ivy--recompute-index (name re-str cands)
(let* ((caller (ivy-state-caller ivy-last))
(func (or (and caller (cdr (assoc caller ivy-index-functions-alist)))