all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Ergus <spacibba@aol.com>
To: emacs-devel@gnu.org
Cc: me@eshelyaron.com
Subject: [PATCH] Completion preview common
Date: Sun, 7 Apr 2024 20:38:04 +0200	[thread overview]
Message-ID: <njlevizac6zitj5fulduqcgtfwhac5vmcsz2we562fym7z5u6i@su5oafrms4r7> (raw)
In-Reply-To: njlevizac6zitj5fulduqcgtfwhac5vmcsz2we562fym7z5u6i.ref@su5oafrms4r7

[-- Attachment #1: Type: text/plain, Size: 750 bytes --]

Hi all:

Recently I have been playing with the new completion-preview mode and
I'm very happy to see something like this finally added to emacs with a
simple and clean code.

However, there is a missing detail that I find useful when typing:
common preview completion.

The goal is to preview a common prefix when available and longer than
the prefix already inserted. This is a point in between 1) exact match
with one candidate only and 2) getting the first candidate in the whole
list.

I tried to avoid adding complexity to the code (because the thing I like
the most in the package is actually its simplicity and predictability);
but the extra functionality I think worth the few new lines.

I attach the patch for a first review.

Best,
Ergus


[-- Attachment #2: completion-preview-common.patch --]
[-- Type: text/plain, Size: 4483 bytes --]

diff --git a/lisp/completion-preview.el b/lisp/completion-preview.el
index a86c1ba1cc9..11ac929efcf 100644
--- a/lisp/completion-preview.el
+++ b/lisp/completion-preview.el
@@ -59,16 +59,21 @@ completion-preview
   :group 'completion)
 
 (defcustom completion-preview-exact-match-only nil
-  "Whether to show completion preview only when there is an exact match.
-
-If this option is non-nil, Completion Preview mode only shows the
-preview when there is exactly one completion candidate that
-matches the symbol at point.  Otherwise, if this option is nil,
-when there are multiple matching candidates the preview shows the
-first candidate, and you can cycle between the candidates with
+  "Show completion preview only when there is an exact or common prefix match.
+
+  If this option is t, Completion Preview mode only shows the preview when
+there is exactly one completion candidate that matches the symbol at
+point.
+  If the option is `common', only shows the preview if there is a common
+prefix in all the candidates and it is longer than the current input.
+  Otherwise, if this option is nil, when there are multiple matching
+candidates the preview shows the first candidate, and you can cycle
+between the candidates with
 \\[completion-preview-next-candidate] and
 \\[completion-preview-prev-candidate]."
-  :type 'boolean
+  :type '(choice (const :tag "Yes" t)
+                 (const :tag "No" nil)
+                 (const :tag "Common" common))
   :version "30.1")
 
 (defcustom completion-preview-commands '(self-insert-command
@@ -236,7 +241,10 @@ completion-preview--try-table
          (sort-fn (or (completion-metadata-get md 'cycle-sort-function)
                       (completion-metadata-get md 'display-sort-function)
                       completion-preview-sort-function))
-         (all (let ((completion-lazy-hilit t))
+         (all (let ((completion-lazy-hilit t)
+                    (completion-styles (pcase completion-preview-exact-match-only
+                                         ('common '(basic))
+                                         (_ completion-styles))))
                 (completion-all-completions string table pred
                                             (- (point) beg) md)))
          (last (last all))
@@ -244,17 +252,28 @@ completion-preview--try-table
          (prefix (substring string base)))
     (when last
       (setcdr last nil)
-      (when-let ((sorted (funcall sort-fn
-                                  (delete prefix (all-completions prefix all)))))
-        (unless (and (cdr sorted) completion-preview-exact-match-only)
-          (list (propertize (substring (car sorted) (length prefix))
-                            'face (if (cdr sorted)
-                                      'completion-preview
-                                    'completion-preview-exact)
-                            'mouse-face 'completion-preview-highlight
-                            'keymap completion-preview--mouse-map)
-                (+ beg base) end sorted
-                (substring string 0 base) exit-fn))))))
+      (when-let ((sorted (cond
+                          ((not completion-preview-exact-match-only)
+                           (funcall sort-fn
+                                    (delete prefix (all-completions prefix all))))
+                          ((eq completion-preview-exact-match-only t)
+                           (when-let ((cands (delete prefix (all-completions prefix all))))
+                             (unless (cdr cands) cands)))
+                          ((eq completion-preview-exact-match-only 'common)
+			   (when-let ((cand (try-completion prefix all)))
+                             (when (and (stringp cand)
+                                        (> (length cand) (length prefix)))
+                               (list cand))))
+                          )))
+
+        (list (propertize (substring (car sorted) (length prefix))
+                          'face (if (cdr sorted)
+                                    'completion-preview
+                                  'completion-preview-exact)
+                          'mouse-face 'completion-preview-highlight
+                          'keymap completion-preview--mouse-map)
+              (+ beg base) end sorted
+              (substring string 0 base) exit-fn)))))
 
 (defun completion-preview--capf-wrapper (capf)
   "Translate return value of CAPF to properties for completion preview overlay."

       reply	other threads:[~2024-04-07 18:38 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <njlevizac6zitj5fulduqcgtfwhac5vmcsz2we562fym7z5u6i.ref@su5oafrms4r7>
2024-04-07 18:38 ` Ergus [this message]
2024-04-07 21:12   ` [PATCH] Completion preview common Eshel Yaron
2024-04-07 23:39     ` Ergus
2024-04-09 18:30       ` Eshel Yaron
2024-04-09 23:05         ` Ergus
2024-04-10 11:55           ` Eshel Yaron
2024-04-10 15:54             ` Ergus
2024-04-12  6:18               ` Eshel Yaron
2024-04-07 21:15   ` [External] : " 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

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

  git send-email \
    --in-reply-to=njlevizac6zitj5fulduqcgtfwhac5vmcsz2we562fym7z5u6i@su5oafrms4r7 \
    --to=spacibba@aol.com \
    --cc=emacs-devel@gnu.org \
    --cc=me@eshelyaron.com \
    /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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.