[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: scoring completions

From: Kevin Rodgers
Subject: Re: scoring completions
Date: Wed, 25 Oct 2000 14:26:58 -0600 (MDT)

John Hunter writes:
>How hard would it be to have a completion function which was passed an
>alist where the car of each cell was a completion string, such as
>required by the TABLE arg to completing-read, and the cdr of each cell
>is a score, so that when the user hits a key, say M-TAB, the
>minibuffer would cycle through the completions which match the entered
>text in the minibuffer in the order determined by the score?
>This is very close to the functionality of completion.el, but I don't
>know if there is an easy way to use that code to handle this case.

It's also close to the functionality of minibuffer-complete-cycle.el,
which I've attached below.  I think to get the behavior you want, you'd
just need to advise completing-read, like this:

(require 'cl)                           ; every

(defadvice completing-read (before sort activate)
  "If TABLE is an alist whose elements' cdr's are numbers, sort it
from largest to smallest."
  (let ((table (ad-get-arg 1)))
    (if (and (listp table)
             (every (lambda (cons)
                      (and (consp cons)
                           (numberp (cdr cons))))
        (ad-set-arg 1 (sort table
                            (lambda (c-1 c-2) (> (cdr c-1) (cdr c-2))))))))

For some reason, though, that doesn't seem to work -- when I evaluate

        (completing-read "Prompt: " '(("a" . 1) ("b" . 2) ("c" . 3)))

I only get "a" in the *Completions* buffer...  And I just noticed that
Emacs apparently sorts the completions themselves; evaluating

        (completing-read "Prompt: " '(("c") ("b") ("a")))

yields a *Completions* buffer listing them in alphabetical order.  So
advising completing-read to sort them in some other order won't work;
you'll have to dig deeper into the guts of the completion code to figure
out how to do it.

Attachment: minibuffer-complete-cycle.el
Description: minibuffer-complete-cycle.el 1.17

Kevin Rodgers <>          Lead Software Engineer
Information Handling Services           Electronic Systems Development
15 Inverness Way East, M/S A114         GO BUFFS!
Englewood CO 80112-5776 USA             1+ (303) 397-2807[voice]/705-4258[fax]

reply via email to

[Prev in Thread] Current Thread [Next in Thread]