From 5c4449dc578903314f400461d13c4c08e02a18ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20T=C3=A1vora?= Date: Wed, 25 Oct 2023 22:36:15 +0100 Subject: [PATCH 2/2] Replace lazy hilit hash table with something simpler * lisp/icomplete.el (icomplete-minibuffer-setup): Don't set completion-lazy-hilit. (icomplete-exhibit): Set it here. * lisp/minibuffer.el (completion-lazy-hilit): Rework docstring. (completion-lazy-hilit-fn): Rework from completion--lazy-hilit-re. (completion--lazy-hilit-table): Delete. (completion-pcm--hilit-commonality): Rework. --- lisp/icomplete.el | 4 ++-- lisp/minibuffer.el | 42 +++++++++++++++++++++--------------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/lisp/icomplete.el b/lisp/icomplete.el index a9ac0b3f040..3e888c8b06a 100644 --- a/lisp/icomplete.el +++ b/lisp/icomplete.el @@ -545,7 +545,6 @@ icomplete-minibuffer-setup (setq-local icomplete--initial-input (icomplete--field-string)) (setq-local completion-show-inline-help nil) (setq icomplete--scrolled-completions nil) - (setq completion-lazy-hilit (cl-gensym)) (use-local-map (make-composed-keymap icomplete-minibuffer-map (current-local-map))) (add-hook 'post-command-hook #'icomplete-post-command-hook nil t) @@ -723,7 +722,8 @@ icomplete-exhibit ;; Check if still in the right buffer (bug#61308) (or (window-minibuffer-p) completion-in-region--data) (icomplete-simple-completing-p)) ;Shouldn't be necessary. - (let ((saved-point (point))) + (let ((saved-point (point)) + (completion-lazy-hilit t)) (save-excursion (goto-char (icomplete--field-end)) ;; Insert the match-status information: diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index 4591f1145c8..4a727615afb 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -1234,6 +1234,7 @@ completion-all-completions POINT is the position of point within STRING. The return value is a list of completions and may contain the base-size in the last `cdr'." + (setq completion-lazy-hilit-fn nil) ;; FIXME: We need to additionally return the info needed for the ;; second part of completion-base-position. (completion--nth-completion 2 string table pred point metadata)) @@ -3753,24 +3754,16 @@ completion-lazy-hilit "If non-nil, request completion lazy hilighting. Completion-presenting frontends may opt to bind this variable to -a unique non-nil value in the context of completion-producing -calls (such as `completion-all-sorted-completions'). This hints -the intervening completion styles that they do not need to -propertize completion strings with the `face' property. +non-nil value in the context of completion-producing calls (such +as `completion-all-sorted-completions'). This hints the +intervening completion styles that they do not need to propertize +completion strings with the `face' property. When doing so, it is the frontend -- not the style -- who becomes responsible for `face'-propertizing only the completion strings -that are meant to be displayed to the user. This can be done by -calling the function `completion-lazy-hilit' which returns a -`face'-propertized string. - -The value stored in this variable by the completion frontend -should be unique to each completion attempt or session that -utilizes the same completion style in `completion-styles-alist'. -For frontends using the minibuffer as the locus of completion -calls and display, setting it to a buffer-local value given by -`gensym' is appropriate. For frontends operating entirely in a -single command, let-binding it to `gensym' is appropriate. +that are meant to be displayed to the user. This is done by +calling `completion-lazy-hilit' on each such string, which +produces the suitably propertized string. Note that the optimization enabled by variable is only actually performed some completions styles. To others, it is a harmless @@ -3778,13 +3771,21 @@ completion-lazy-hilit advantage of this, look in the source of `completion-pcm--hilit-commonality'.") +(defvar completion-lazy-hilit-fn nil + "Used by completions styles to honouring `completion-lazy-hilit'. +When a given style wants to enable support for +`completion-lazy-hilit' (which see), that style should set this +variable to a function of one argument, a fresh string to be +displayed to the user. The function is responsible for +destructively highlighting the string.") + (defun completion-lazy-hilit (str) "Return a copy of completion STR that is `face'-propertized. See documentation for variable `completion-lazy-hilit' for more details." - (completion--hilit-from-re - (copy-sequence str) - (gethash completion-lazy-hilit completion--lazy-hilit-table))) + (if (and completion-lazy-hilit completion-lazy-hilit-fn) + (funcall completion-lazy-hilit-fn (copy-sequence str)) + str)) (defun completion--hilit-from-re (string regexp) "Fontify STRING with `completions-common-part' using REGEXP." @@ -3883,8 +3884,6 @@ completion--flex-score-1 last-b b))) (/ score-numerator (* len (1+ score-denominator)) 1.0))) -(defvar completion--lazy-hilit-table (make-hash-table :weakness 'key)) - (defun completion-pcm--hilit-commonality (pattern completions) "Show where and how well PATTERN matches COMPLETIONS. PATTERN, a list of symbols and strings as seen @@ -3911,7 +3910,8 @@ completion-pcm--hilit-commonality (md (cddr (setq last-md (match-data t last-md))))) (completion--flex-score-1 md match-end (length str)))))) (cond (completion-lazy-hilit - (puthash completion-lazy-hilit re completion--lazy-hilit-table) + (setq completion-lazy-hilit-fn + (lambda (str) (completion--hilit-from-re str re))) (mapc (lambda (str) (put-text-property 0 1 'completion-score (funcall score str) str)) completions)) -- 2.39.2