Subject: bug#49321: 28.0.50; [PATCH] Add option icomplete-matches-format

From: Gabriel <[email-masked]>
Date: Sun, 04 Jul 2021 12:33:26 -0300

João Távora <[email-masked]> writes:

> Thanks Lars,
>
> I think it'd be great to skip the `safe-length` and similar potentially
> costly calls if the user decides he doesn't want this info (which
> I suppose is icomplete-matches-format = nil)
>
> João
>

Hi Joao,

Thanks for your inputs, makes a lot of sense to shortcut this piece of
code in case the user option is nil. Here is an updated version of the
patch:

>From d1e81cf0a31a2f5ac714c3cf6dc2cada527cb7a4 Mon Sep 17 00:00:00 2001
From: Gabriel do Nascimento Ribeiro <[email-masked]>
Date: Sun, 4 Jul 2021 12:31:26 -0300
Subject: [PATCH 1/1] Add option icomplete-matches-format

* lisp/icomplete.el (icomplete-matches-format): New user option.
---
 lisp/icomplete.el | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/lisp/icomplete.el b/lisp/icomplete.el
index 576fced015..ce914c2f24 100644
--- a/lisp/icomplete.el
+++ b/lisp/icomplete.el
@@ -97,6 +97,12 @@ icomplete-with-completion-tables
   :type '(choice (const :tag "All" t)
 		 (repeat function)))
 
+(defcustom icomplete-matches-format "%s/%s "
+  "Format of the current/total number of matches for the prompt prefix."
+  :version "28.1"
+  :type '(choice (const :tag "No prefix" nil)
+                 (string :tag "Prefix format string" "%s/%s ")))
+
 (defface icomplete-first-match '((t :weight bold))
   "Face used by Icomplete for highlighting first match."
   :version "24.4")
@@ -696,12 +702,11 @@ icomplete-exhibit
               (overlay-put
                icomplete-overlay 'before-string
                (and icomplete-scroll
-                    (let ((past (length icomplete--scrolled-past)))
-                      (format
-                       "%s/%s "
-                       (1+ past)
-                       (+ past
-                          (safe-length completion-all-sorted-completions))))))
+                    icomplete-matches-format
+                    (let* ((past (length icomplete--scrolled-past))
+                           (current (1+ past))
+                           (total (+ past (safe-length completion-all-sorted-completions))))
+                      (format icomplete-matches-format current total))))
               (overlay-put icomplete-overlay 'after-string text))))))))
 
 (defun icomplete--affixate (md prospects)
-- 
2.32.0