2

Recently, when using Ivy and calling completing-read, my output is being sorted (instead of showing up in the order passed in).

How can I prevent sorting the input to completing-read when using Ivy?

Drew
  • 75,699
  • 9
  • 109
  • 225
ideasman42
  • 8,375
  • 1
  • 28
  • 105

2 Answers2

3

You need your completion-table to tell the completion-UI not to perform sorting (typically because the completion-table already arranges for its own sorting to take place).

See C-h f completion-metadata. Here's an example:

(let* ((presorted-completions ...)
      (completion-table
       (lambda (string pred action)
         (if (eq action 'metadata)
             '(metadata (display-sort-function . identity)
                        (cycle-sort-function . identity))
           (complete-with-action
            action presorted-completions string pred))))
  (completing-read "Prompt" completion-table))
Stefan
  • 26,154
  • 3
  • 46
  • 84
  • This stops `ivy` from being used, is there a general way to handle this so ivy keeps working? – ideasman42 Jun 03 '18 at 06:16
  • Sounds like a bug in ivy that you might like to report. – Stefan Jun 03 '18 at 08:56
  • @ideasman42 Stefan's answer works with Ivy. How is it not working for you? If Ivy still performs unwanted sorting, customise `ivy-sort-functions-alist`. See also https://github.com/abo-abo/swiper/issues/1611. – Basil Jun 17 '18 at 17:57
1

Sorting can be disabled by setting (ivy-sort-functions-alist nil), in the scope you're using completing-read (so as not to change defaults everywhere).

See this complete example.

ideasman42
  • 8,375
  • 1
  • 28
  • 105
  • You don't need to set the entire `ivy-sort-functions-alist` to `nil`, only the the entries corresponding to the callers of `completing-read`. – Basil Jun 02 '18 at 17:29