Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ivy.el: Allow toggling of sorting in ivy-read #845

Closed
wants to merge 1 commit into from

Conversation

@justbur
Copy link
Contributor

@justbur justbur commented Jan 4, 2017

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

@justbur
Copy link
Contributor Author

@justbur justbur commented Jan 4, 2017

This is just an idea. I'm sure it can be improved.

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
@justbur justbur force-pushed the justbur:toggle-sort branch to e4ad137 Jan 4, 2017
@justbur
Copy link
Contributor Author

@justbur justbur commented Jan 4, 2017

Here's a slightly more interesting example that sorts by position in recentf-list if found.

(defun recentf-lessp (x y)
  (unless recentf-mode
    (recentf-mode))
  (let* ((recent (mapcar 'substring-no-properties recentf-list))
         (xi (cl-position x recent))
         (yi (cl-position y recent)))
    (or (and xi (null yi))
        (and xi (<= xi yi))
        (and (null xi) (null yi)))))

(setq ivy-sort-functions-alist
      '((read-file-name-internal . (recentf-lessp ivy-sort-file-function-default))
        (internal-complete-buffer)
        (counsel-git-grep-function)
        (Man-goto-section)
        (org-refile)
        (t . string-lessp)))
@abo-abo abo-abo closed this in 6cf7feb Jan 5, 2017
@abo-abo
Copy link
Owner

@abo-abo abo-abo commented Jan 5, 2017

Nice idea, thanks. Would be nice to include something sensible in ivy-sort-functions-alist, so that C-c C-s isn't a no-op by default.
Additionally, would be nice to have the feedback in the C-o hydra, like there already is for the current action. Maybe C-o S would toggle the sorting and print the sorting function symbol name somewhere.

@justbur
Copy link
Contributor Author

@justbur justbur commented Jan 5, 2017

Nice idea, thanks. Would be nice to include something sensible in ivy-sort-functions-alist, so that C-c C-s isn't a no-op by default.
Additionally, would be nice to have the feedback in the C-o hydra, like there already is for the current action. Maybe C-o S would toggle the sorting and print the sorting function symbol name somewhere.

I agree. I just wasn't sure of the best way to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants