[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 6cf7feb 282/348: ivy.el: Allow toggling of sorting in ivy-
From: |
Oleh Krehel |
Subject: |
[elpa] master 6cf7feb 282/348: ivy.el: Allow toggling of sorting in ivy-read |
Date: |
Sat, 8 Apr 2017 11:04:15 -0400 (EDT) |
branch: master
commit 6cf7feb467c46742e19c8fab53fea5206451a598
Author: Justin Burkett <address@hidden>
Commit: Oleh Krehel <address@hidden>
ivy.el: Allow toggling of sorting in ivy-read
Modify ivy-sort-functions-alist to allow a list of sorting functions to
apply to
a collection. The car of this list is the current one. ivy-rotate-sort then
rotates this list through the different possibilities. I bound it to C-c
C-s. Here is a simple example to illustrate.
(setq ivy-sort-functions-alist
'((read-file-name-internal .
(ivy-sort-file-function-default string-lessp string-greaterp))
(internal-complete-buffer . nil)
(counsel-git-grep-function . nil)
(Man-goto-section . nil)
(org-refile . nil)
(t . string-lessp)))
M-x find-file RET C-c C-s now switches from the default sorting to using
string-lessp. C-c C-s again switches to string-greaterp and so on.
ivy.el (ivy-sort-functions-alist): Allow list in cdr of each cell
ivy.el (ivy-rotate-sort): Add
ivy.el (ivy--sort-function): Add and use for
lookup in ivy-sort-functions-alist
Fixes #845
---
ivy.el | 55 +++++++++++++++++++++++++++++++++++--------------------
1 file changed, 35 insertions(+), 20 deletions(-)
diff --git a/ivy.el b/ivy.el
index 2eb5292..78637e8 100644
--- a/ivy.el
+++ b/ivy.el
@@ -301,6 +301,7 @@ action functions.")
(define-key map (kbd "C-M-a") 'ivy-read-action)
(define-key map (kbd "C-c C-o") 'ivy-occur)
(define-key map (kbd "C-c C-a") 'ivy-toggle-ignore)
+ (define-key map (kbd "C-c C-s") 'ivy-rotate-sort)
(define-key map [remap describe-mode] 'ivy-help)
map)
"Keymap used in the minibuffer.")
@@ -1252,6 +1253,10 @@ Nil means no sorting, which is useful to turn off the
sorting for
functions that have candidates in the natural buffer order, like
`org-refile' or `Man-goto-section'.
+A list can be used to associate multiple sorting functions with a
+collection. The car of the list is the current sort
+function. This list can be rotated with `ivy-rotate-sort'.
+
The entry associated with t is used for all fall-through cases.
See also `ivy-sort-max-size'."
@@ -1264,9 +1269,27 @@ See also `ivy-sort-max-size'."
(const :tag "Plain sort" string-lessp)
(const :tag "File sort" ivy-sort-file-function-default)
(const :tag "No sort" nil)
- (function :tag "Custom function")))
+ (function :tag "Custom function")
+ (repeat (function :tag "Custom function"))))
:group 'ivy)
+(defun ivy--sort-function (collection)
+ "Retrieve sort function from `ivy-sort-functions-alist'"
+ (let ((res (cdr (assoc collection ivy-sort-functions-alist))))
+ (or (car-safe res) res)))
+
+(defun ivy-rotate-sort ()
+ "Rotate through sorting functions available for current collection.
+This only has an effect if multiple sorting functions are
+specified for the current collection in
+`ivy-sort-functions-alist'."
+ (interactive)
+ (let ((cell
+ (assoc (ivy-state-collection ivy-last) ivy-sort-functions-alist)))
+ (when (listp (cdr cell))
+ (setcdr cell (append (cddr cell) (list (cadr cell))))
+ (ivy--reset-state ivy-last))))
+
(defvar ivy-index-functions-alist
'((swiper . ivy-recompute-index-swiper)
(swiper-multi . ivy-recompute-index-swiper)
@@ -1334,8 +1357,7 @@ Directories come first."
(if (equal dir "/")
seq
(setq seq (delete "./" (delete "../" seq)))
- (when (eq (setq sort-fn (cdr (assoc 'read-file-name-internal
- ivy-sort-functions-alist)))
+ (when (eq (setq sort-fn (ivy--sort-function 'read-file-name-internal))
#'ivy-sort-file-function-default)
(setq seq (mapcar (lambda (x)
(propertize x 'dirp (string-match-p "/\\'" x)))
@@ -1606,7 +1628,7 @@ This is useful for recursive `ivy-read'."
(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))))
+ (if (and sort (setq sort-fn (ivy--sort-function caller)))
(progn
(setq sort nil)
(setq coll (mapcar #'car
@@ -1633,12 +1655,11 @@ This is useful for recursive `ivy-read'."
(setq coll collection)))
(when sort
(if (and (functionp collection)
- (setq sort-fn (assoc collection ivy-sort-functions-alist)))
- (when (and (setq sort-fn (cdr sort-fn))
- (not (eq collection 'read-file-name-internal)))
+ (setq sort-fn (ivy--sort-function collection)))
+ (when (not (eq collection 'read-file-name-internal))
(setq coll (cl-sort coll sort-fn)))
(unless (eq history 'org-refile-history)
- (if (and (setq sort-fn (cdr (assoc t ivy-sort-functions-alist)))
+ (if (and (setq sort-fn (ivy--sort-function t))
(<= (length coll) ivy-sort-max-size))
(setq coll (cl-sort (copy-sequence coll) sort-fn))))))
(setq coll (ivy--set-candidates coll))
@@ -1749,11 +1770,9 @@ INHERIT-INPUT-METHOD is currently ignored."
:history history
:keymap nil
:sort
- (let ((sort (or (assoc this-command ivy-sort-functions-alist)
- (assoc t ivy-sort-functions-alist))))
- (if sort
- (cdr sort)
- t)))))
+ (let ((sort (or (ivy--sort-function this-command)
+ (ivy--sort-function t))))
+ (or sort t)))))
(defvar ivy-completion-beg nil
"Completion bounds start.")
@@ -2220,13 +2239,9 @@ If SUBEXP is nil, the text properties are applied to the
whole match."
entry)
(if (null sort)
collection
- (let ((sort-fn (cond ((functionp sort)
- sort)
- ((setq entry (assoc (ivy-state-collection ivy-last)
- ivy-sort-functions-alist))
- (cdr entry))
- (t
- (cdr (assoc t ivy-sort-functions-alist))))))
+ (let ((sort-fn (or (and (functionp sort) sort)
+ (ivy--sort-function (ivy-state-collection ivy-last))
+ (ivy--sort-function t))))
(if (functionp sort-fn)
(cl-sort (copy-sequence collection) sort-fn)
collection)))))
- [elpa] master 1304925 239/348: ivy.el (ivy--directory-done): Fix broken "/ssh:", (continued)
- [elpa] master 1304925 239/348: ivy.el (ivy--directory-done): Fix broken "/ssh:", Oleh Krehel, 2017/04/08
- [elpa] master 359a6c8 241/348: Address multiple problems with counsel-rg, Oleh Krehel, 2017/04/08
- [elpa] master 2ed3da4 252/348: counsel.el: Fix byte-compile warning, Oleh Krehel, 2017/04/08
- [elpa] master 09da543 253/348: swiper.el: Ease the creation of swiper-in-region functions, Oleh Krehel, 2017/04/08
- [elpa] master 3940f17 266/348: Fix typos, Oleh Krehel, 2017/04/08
- [elpa] master 25ac29f 079/348: ivy.el (ivy-completion-in-region): Avoid error for 0 candidates, Oleh Krehel, 2017/04/08
- [elpa] master 0572f16 123/348: swiper now respects ivy-re-builders-alist, Oleh Krehel, 2017/04/08
- [elpa] master d528f04 260/348: ivy.el (ivy--virtual-buffers): Don't modify recentfs or bookmarks, Oleh Krehel, 2017/04/08
- [elpa] master ed579dd 274/348: ivy.el: Fix ivy-highlight-functions-alist, Oleh Krehel, 2017/04/08
- [elpa] master 9151862 277/348: ivy.el (ivy--format-minibuffer-line): Fix for ivy-display-style nil, Oleh Krehel, 2017/04/08
- [elpa] master 6cf7feb 282/348: ivy.el: Allow toggling of sorting in ivy-read,
Oleh Krehel <=
- [elpa] master dbcee94 288/348: counsel.el: support universal argument for counsel-unicode-char, Oleh Krehel, 2017/04/08
- [elpa] master 0507841 285/348: Use customizable list of directories when looking for linux apps., Oleh Krehel, 2017/04/08
- [elpa] master 7c73f71 293/348: Allow users to find libraries from counsel-load-library, Oleh Krehel, 2017/04/08
- [elpa] master 07813be 094/348: Spelling fixes, Oleh Krehel, 2017/04/08
- [elpa] master 22623aa 101/348: swiper.el (swiper-avy): Require avy, Oleh Krehel, 2017/04/08
- [elpa] master 9118fa1 093/348: Separate readme into distinct ivy/counsel/swiper sections, Oleh Krehel, 2017/04/08
- [elpa] master 64b385d 107/348: Don't quit counsel-linux-app on bad data, Oleh Krehel, 2017/04/08
- [elpa] master 2be454c 104/348: Remove ivy-height from the example configuration., Oleh Krehel, 2017/04/08
- [elpa] master d83567e 073/348: counsel.el (counsel-locate-options): Remove obsolete var, Oleh Krehel, 2017/04/08
- [elpa] master e89f250 072/348: Add option for swiper line number to be searchable, Oleh Krehel, 2017/04/08