emacs-devel
[Top][All Lists]
Advanced

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

Re: [elpa] externals/vertico a962a8b 2/3: Implement deferred highlightin


From: Daniel Mendler
Subject: Re: [elpa] externals/vertico a962a8b 2/3: Implement deferred highlighting for all completion styles
Date: Sun, 11 Apr 2021 19:34:17 +0200

Hi Stefan!

On 4/11/21 6:37 PM, Stefan Monnier wrote:
+(declare-function orderless-highlight-matches "ext:orderless")
+(defun vertico--all-completions (&rest args)
+  "Compute all completions for ARGS with deferred highlighting."
+  (cl-letf* ((orig-pcm (symbol-function #'completion-pcm--hilit-commonality))
+             (orig-flex (symbol-function #'completion-flex-all-completions))
+             ((symbol-function #'completion-flex-all-completions)
+              (lambda (&rest args)
+                ;; Unfortunately for flex we have to undo the deferred 
highlighting, since flex uses
+                ;; the completion-score for sorting, which is applied during 
highlighting.
+                (cl-letf (((symbol-function 
#'completion-pcm--hilit-commonality) orig-pcm))
+                  (apply orig-flex args))))
+             ;; Defer the following highlighting functions
+             (hl #'identity)
+             ((symbol-function #'completion-hilit-commonality)
+              (lambda (cands prefix &optional base)
+                (setq hl (lambda (x) (nconc (completion-hilit-commonality x 
prefix base) nil)))
+                (and cands (nconc cands base))))
+             ((symbol-function #'completion-pcm--hilit-commonality)
+              (lambda (pattern cands)
+                (setq hl (lambda (x) (completion-pcm--hilit-commonality 
pattern x)))
+                cands))
+             ((symbol-function #'orderless-highlight-matches)
+              (lambda (pattern cands)
+                (setq hl (lambda (x) (orderless-highlight-matches pattern x)))
+                cands)))
+    (cons (apply #'completion-all-completions args) hl)))

Yuck!
I think this should have a bug#NNN attached to make sure it's only used
on a temporary basis (i.e. until Emacs's minibuffer.el provides the
corresponding functionality).

I wasn't aware that you are monitoring this so closely. This is basically the prototype of how the deferred highlighting could look like, instead of my proposal 8, `completion-skip-highlighting`, which can hardly be generalized. It works well for Orderless (`orderless-skip-highlighting`), but that's about it.

One can introduce a function `completion-all-completions-deferred-highlighting` or alternatively add an optional argument to `completion-all-completions`, which signals that the function should return the list of completions and the deferred highlighting function/highlighting data. (One may also consider returning the base directly or is there a particular reason to put it into the last cdr?)

The function `completion-all-completions-deferred-highlighting` can return a special highlighting function, registered in `completion-styles-alist`.

Daniel



reply via email to

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