unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Drew Adams" <drew.adams@oracle.com>
To: <13322@debbugs.gnu.org>
Subject: bug#13322: 24.3.50; `completion-all-sorted-completions': sorting and duplicate deletion
Date: Mon, 31 Dec 2012 13:10:55 -0800	[thread overview]
Message-ID: <64F4026487BC4A83892CBBC7C0BCE7D1@us.oracle.com> (raw)
In-Reply-To: <62F6B49F5A0840038CF7139810B32470@us.oracle.com>

Oops.  Forgot to handle the second sorting occurrence also.
If a user supplies a sort function then we should not
overrule that to re-sort by history.

This code should do it (but the code is just a suggestion, again):

(defun completion-all-sorted-completions (&optional sort-function
dont-remove-dups)
  "Like `c-a-s-c', but with added optional args."
  (or completion-all-sorted-completions
      (let* ((start (field-beginning))
             (end (field-end))
             (string (buffer-substring start end))
             (md (completion--field-metadata start))
             (all (completion-all-completions
                   string
                   minibuffer-completion-table
                   minibuffer-completion-predicate
                   (- (point) start)
                   md))
             (last (last all))
             (base-size (or (cdr last) 0))
             (all-md (completion--metadata (buffer-substring-no-properties
                                            start (point))
                                           base-size md
                                           minibuffer-completion-table
                                           minibuffer-completion-predicate))
             (sort-fun (or sort-function
                           (completion-metadata-get all-md
'cycle-sort-function))))
        (when last
          (setcdr last nil)
          ;; Delete duplicates: do it after setting last's cdr to nil (so
          ;; it's a proper list), and be careful to reset `last' since it
          ;; may be a different cons-cell.
          (unless dont-remove-dups (setq all (delete-dups all)))
          (setq last (last all))
          (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.
          (when (and (minibufferp)  (not sort-fun))
            (let ((hist (symbol-value minibuffer-history-variable)))
              (setq all (sort all (lambda (c1 c2)
                                    (> (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.
          (completion--cache-all-sorted-completions (nconc all base-size))))))






  reply	other threads:[~2012-12-31 21:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-31 19:43 bug#13322: 24.3.50; `completion-all-sorted-completions': sorting and duplicate deletion Drew Adams
2012-12-31 21:10 ` Drew Adams [this message]
2013-01-03 17:02 ` Stefan Monnier
2013-01-03 17:39   ` Drew Adams
2016-04-28 19:15   ` Lars Ingebrigtsen
2016-04-29  0:45     ` Drew Adams

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=64F4026487BC4A83892CBBC7C0BCE7D1@us.oracle.com \
    --to=drew.adams@oracle.com \
    --cc=13322@debbugs.gnu.org \
    /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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).