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-sort-functions-alist can no longer handle lambda functions #1574

Closed
dieggsy opened this issue May 15, 2018 · 4 comments

Comments

@dieggsy
Copy link
Contributor

commented May 15, 2018

Not sure why, but recent changes have made it so that

(add-to-list 'ivy-sort-functions-alist (lambda (str1 str2) (string< (downcase str1) (downcase str2)))

Results in an error saying lambda is an invalid function.

@basil-conto

This comment has been minimized.

Copy link
Collaborator

commented May 15, 2018

Not only can I not reproduce this (please try to provide a detailed set of steps starting from make plain); but doing this makes no sense. ivy-sort-functions-alist is an alist mapping collection functions (i.e. :callers) to sorting functions:

ivy-sort-functions-alist is a variable defined in ‘ivy.el’.
Its value is
((read-file-name-internal . ivy-sort-file-function-default)
 (internal-complete-buffer)
 (ivy-completion-in-region)
 (counsel-git-grep-function)
 (Man-goto-section)
 (org-refile)
 (t . ivy-string<))

Documentation:
An alist of sorting functions for each collection function.
Interactive functions that call completion fit in here as well.

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’.

So, what you probably want is:

(add-to-list 'ivy-sort-functions-alist
	     (cons t (lambda (x y)
		       (> 0 (compare-strings x nil nil y nil nil t)))))
@dieggsy

This comment has been minimized.

Copy link
Contributor Author

commented May 15, 2018

Yeah, that was me typing it out incorrectly instead of copy pasting. I meant:

(add-to-list 'ivy-sort-functions-alist
             '(ivy-completion-in-region . (lambda (str1 str2)
                                            (string< (downcase str1) (downcase str2)))))

With this, trying to use completion in region results in Invalid function: lambda.

@basil-conto

This comment has been minimized.

Copy link
Collaborator

commented May 15, 2018

Thanks, I can reproduce this now (and was the one who introduced the issue in the first place, while trying to fix another issue).

FWIW, it is inadvisable to quote lambdas, and case-insensitive string comparison is better done with compare-strings:

(add-to-list 'ivy-sort-functions-alist
             (cons #'ivy-completion-in-region
                   (lambda (str1 str2)
                     (> 0 (compare-strings str1 nil nil str2 nil nil t)))))

or

(add-to-list 'ivy-sort-functions-alist
             `(ivy-completion-in-region
               . ,(lambda (str1 str2)
                    (> 0 (compare-strings str1 nil nil str2 nil nil t)))))
basil-conto added a commit to basil-conto/swiper that referenced this issue May 15, 2018
ivy.el (ivy--sort-function): Handle anonymous fns
ivy-test.el (ivy--sort-function): New test.

Fixes abo-abo#1574
@dieggsy

This comment has been minimized.

Copy link
Contributor Author

commented May 15, 2018

Cool, thanks for the tips

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.