all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "João Távora" <joaotavora@gmail.com>
To: 34083@debbugs.gnu.org
Cc: monnier@iro.umontreal.ca
Subject: bug#34083: 27.0.50; Default completion, if it exists, should always sort to top
Date: Tue, 15 Jan 2019 13:39:10 +0000	[thread overview]
Message-ID: <jjbimyq82gx.fsf@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1737 bytes --]

Hi maintainers,

In completion-all-sorted-completions, completions are sorted according
to recency of usage, which is a good idea.

However, for calls to completing-read that are given a DEFAULT (actually
DEF) argument, the sort order isn't very useful.  It means that, when
using icomplete for i.e. M-x describe-symbol on top of the symbol sort

   Describe symbol (default sort): { some-other-symbol | yet-another | sort }
                                     ^^^^^^^^^^^^^^^^^
                                        boldface
                                     
This is very confusing in icomplete as "some-other-symbol" in the
previous example is boldface and gives the idea of a default.  It is the
thing that minibuffer-force-complete-and-exit, bound to C-j, will
complete to immediately.  But since no text has been entered, C-m will
make completion consider "sort" instead.

Here's a recipe:

    Emacs -Q
    M-x icomplete-mode
    M-: (setq icomplete-show-matches-on-no-input t) RET
    type "sort"
    C-h o

Verify that "sort" is the default but it doesn't visually in the
"propects list".  Instead "%" is made boldface and C-j and C-M-i will
immediately complete to it.

This inconsistency is easy to fix in minibuffer.el, as attached in a
patch.  After the patch, "sort" is sorted to the top.

I couldn't figure exactly if there is an impact on non-icomplete.el
usage of completion-all-sorted-completions, because I'm not familiar
with that code.  But since it was already using
minibuffer-history-variable, I don't think this disturbs it much more
than that.

Naturally, this could be coded to work in icomplete-mode only, but doing
that patch cleanly is much harder.

João


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Consider-default-completion-in-completion-all-sorted.patch --]
[-- Type: text/x-patch, Size: 2874 bytes --]

From 26acc0bf504a5c3c29abcbdec7af0544cf2aec02 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20T=C3=A1vora?= <joaotavora@gmail.com>
Date: Tue, 15 Jan 2019 13:27:01 +0000
Subject: [PATCH] Consider default completion in
 completion-all-sorted-completions

* lisp/minibuffer.el (minibuffer-completion-default): New variable.
(completion-all-sorted-completions): Sort with the default on top.
(completing-read-default): Set minibuffer-completion-default
---
 lisp/minibuffer.el | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 5760a2e49d..578fd9ab63 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1210,6 +1210,9 @@ completion--metadata
     (if (eq (car bounds) base) md-at-point
       (completion-metadata (substring string 0 base) table pred))))
 
+(defvar minibuffer-completion-default nil
+  "Within call to `completing-read', this holds the DEF argument.")
+
 (defun completion-all-sorted-completions (&optional start end)
   (or completion-all-sorted-completions
       (let* ((start (or start (minibuffer-prompt-end)))
@@ -1242,12 +1245,17 @@ completion-all-sorted-completions
           (setq all (if sort-fun (funcall sort-fun all)
                       ;; Prefer shorter completions, by default.
                       (sort all (lambda (c1 c2) (< (length c1) (length c2))))))
-          ;; Prefer recently used completions.
+          ;; Prefer recently used completions and put the default, if
+          ;; it exists, on top.
           (when (minibufferp)
-            (let ((hist (symbol-value minibuffer-history-variable)))
-              (setq all (sort all (lambda (c1 c2)
-                                    (> (length (member c1 hist))
-                                       (length (member c2 hist))))))))
+            (let ((hist (symbol-value minibuffer-history-variable))
+                  (def minibuffer-completion-default))
+              (setq all (sort all
+                              (lambda (c1 c2)
+                                (cond ((equal c1 def) t)
+                                      ((equal c2 def) nil)
+                                      (t (> (length (member c1 hist))
+                                            (length (member c2 hist))))))))))
           ;; Cache the result.  This is not just for speed, but also so that
           ;; repeated calls to minibuffer-force-complete can cycle through
           ;; all possibilities.
@@ -3422,6 +3430,7 @@ completing-read-default
 
   (let* ((minibuffer-completion-table collection)
          (minibuffer-completion-predicate predicate)
+         (minibuffer-completion-default def)
          (minibuffer-completion-confirm (unless (eq require-match t)
                                           require-match))
          (base-keymap (if require-match
-- 
2.19.2


             reply	other threads:[~2019-01-15 13:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-15 13:39 João Távora [this message]
2019-01-15 17:55 ` bug#34083: 27.0.50; Default completion, if it exists, should always sort to top Drew Adams
2019-01-15 18:25   ` João Távora
2019-01-15 18:45     ` Drew Adams
2019-01-25 22:27       ` João Távora

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jjbimyq82gx.fsf@gmail.com \
    --to=joaotavora@gmail.com \
    --cc=34083@debbugs.gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.