Subject: bug#38614: 26.3; Info completions in reverse order

From: Howard Melman <[email-masked]>
Date: Sun, 15 Dec 2019 14:39:16 -0500

(Sorry if this is a resend, I don't think I included debbugs in my original reply)

> On Dec 15, 2019, at 11:06 AM, Eli Zaretskii <[email-masked]> wrote:
> 
>> From: Howard Melman <[email-masked]>
>> Date: Sat, 14 Dec 2019 12:18:54 -0500
>> 
>> When using ivy mode, Info-index shows me the list of
>> completions in reverse alphabetical order. Info-menu does too.
>> Both Info-index and Info-menu use completing-read with
>> Info-complete-menu-item as the collections argument. It
>> seems to generate the list in reverse order.
>> 
>> Sorry this isn't a formatted patch, but a one line fix solves it for me.
>> 
>> If after this line in Info-complete-menu-item:
>> 		    (setq completions (delete-dups completions))
>> I add this line:
>> 		    (setq completions (nreverse completions))
>> the index and menus are shown in alphabetical order.
> 
> Sorry, I don't understand: when I type "i SUBJECT" and press TAB in
> Info, I get completions in alphabetical order, so how come with ivy
> you get the reverse order?

I tried that in Emacs 26.3 with -Q and and I see an alphabetical list. I'm not sure what mechanism that's using and I don't particularly know icomplete or ido, though I tried enabling each and saw the same alphabetical list. 

Note that's not quite the same thing as I was describing. Ivy has two sort mechanisms. Ivy displays a list of possible completions (from the collections argument of completing-read) BEFORE you enter any text (SUBJECT in your case). There's no need to hit TAB to see the completions. Then after you enter text it displays completions that are sorted via a different mechanism (because the various matching mechanisms and what you type might indicate a different sort order from the original completions list such as: shorter matches first, prefix matches first, or just alphabetical). AFAIK, Ivy usually doesn't do a sort of the initial list because often it's in a useful order other than alphabetical.

It's clearer in the Info-menu case, where my desired order is the order of the menu items in the info buffer. Using emacs -Q, Info-menu and TAB shows me an alphabetical list. That tells me that whatever mechanism it's using is sorting the list alphabetically and not using the original order of the collection list. 

Ivy shows me the menu items in reverse buffer order because the original collection list presented to ivy (via completing-read) is in reverse order as found in the buffer. Info-complete-menu-item is clearly pushing onto a list and then not nreversing it as is the common idiom. 

I got a bit lost following the elisp docs for Programmed Completion but didn't see any guidelines about the order of the collections intially returned (Info-complete-menu-item doesn't seem to be setting a display-sort-function which I think is the mechanism but I got lost).

Info uses the same function to build the collection list for both Info-menu and Info-index. In the Info-index case the order from the buffer is alphabetical, so Info-complete-menu-item is returning a collection list in reverse alphabetical order. 

Info-complete-menu-item is saving the cost of an nreverse. It would be useful if it returned the items in the order as found in the buffer, because that order isn't always easily recreated after sorting. In the Info-menu case, that's a logical order of the menu items. In the Info-index case, that happens to be alphabetical without the need for an additional sort.

buffer-list returns the buffers in most recently displayed order because it's useful. I think Info-complete-menu-item should return it's items in a logical order too (and not the reverse of one) even if some popular completion mechanisms hide this by always sorting the list alphabetically before display. 

Ivy seems to show other initial completions in a useful order, to me it seems only it's display of Info commands is wrong, because of this missing idomatic nreverse.

Howard