all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Spencer Baugh <sbaugh@janestreet.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: 70217@debbugs.gnu.org
Subject: bug#70217: [PATCH] Add substring-partial-completion style
Date: Wed, 08 May 2024 12:46:32 -0400	[thread overview]
Message-ID: <ieredacqmjb.fsf@janestreet.com> (raw)
In-Reply-To: <ieril0ebswn.fsf@janestreet.com> (Spencer Baugh's message of "Thu, 18 Apr 2024 11:19:04 -0400")

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

Spencer Baugh <sbaugh@janestreet.com> writes:
> But, also, I realized that I basically always want PCM for both the
> substring and emacs22 completion styles.  So what about having two
> customizations, defaulting to nil?
>
> completion-substring-use-pcm
> completion-emacs22-use-pcm

Here is a patch implementing this approach for both substring and
emacs22.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Support-using-partial-completion-in-emacs22-and-subs.patch --]
[-- Type: text/x-patch, Size: 9327 bytes --]

From 1a10582f1d41109a8a84451fe847fd0ab685cacb Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh@janestreet.com>
Date: Wed, 8 May 2024 12:45:19 -0400
Subject: [PATCH] Support using partial-completion in emacs22 and substring
 styles

The partial-completion completion style is useful, and so are the
emacs22 and substring completion styles.  Now they can be used at the
same time.

* lisp/minibuffer.el (completion-emacs22-use-pcm)
(completion-substring-use-pcm): Add. (bug#70217)
(completion-emacs22-try-completion)
(completion-emacs22-all-completions): Check completion-emacs22-use-pcm.
(completion-pcm--string->pattern, completion-pcm--find-all-completions)
(completion-pcm-all-completions, completion-pcm--merge-try)
(completion-pcm-try-completion): Add "startglob" optional argument and
pass through.
(completion-substring-try-completion)
(completion-substring-all-completions): Check
completion-substring-use-pcm and pass startglob=t.
---
 lisp/minibuffer.el | 93 ++++++++++++++++++++++++++++++++--------------
 1 file changed, 65 insertions(+), 28 deletions(-)

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index ad6a0928cda..d80cd91320c 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -3738,9 +3738,25 @@ completion-emacs21-all-completions
    (length string)
    (car (completion-boundaries string table pred ""))))
 
+(defcustom completion-emacs22-use-pcm nil
+  "If non-nil, the emacs22 completion style performs partial-completion.
+
+This means that in addition to ignoring the text after point
+during completion, the text before point is expanded following
+the partial-completion rules.")
+
 (defun completion-emacs22-try-completion (string table pred point)
-  (let ((suffix (substring string point))
-        (completion (try-completion (substring string 0 point) table pred)))
+  (let* ((suffix (substring string point))
+         (prefix (substring string 0 point))
+         (completion
+          (if completion-emacs22-use-pcm
+              (let ((ret (completion-pcm-try-completion prefix table pred point)))
+                (if (consp ret)
+                    ;; Ignore any changes to point; that would change
+                    ;; what text we're ignoring
+                    (car ret)
+                  ret))
+            (try-completion prefix table pred))))
     (cond
      ((eq completion t)
       (if (equal "" suffix)
@@ -3765,10 +3781,12 @@ completion-emacs22-try-completion
 
 (defun completion-emacs22-all-completions (string table pred point)
   (let ((beforepoint (substring string 0 point)))
-    (completion-hilit-commonality
-     (all-completions beforepoint table pred)
-     point
-     (car (completion-boundaries beforepoint table pred "")))))
+    (if completion-emacs22-use-pcm
+        (completion-pcm-all-completions beforepoint table pred point)
+      (completion-hilit-commonality
+       (all-completions beforepoint table pred)
+       point
+       (car (completion-boundaries beforepoint table pred ""))))))
 
 ;;; Basic completion.
 
@@ -3875,10 +3893,13 @@ completion-pcm--pattern-trivial-p
 	     (setq trivial nil)))
 	 trivial)))
 
-(defun completion-pcm--string->pattern (string &optional point)
+(defun completion-pcm--string->pattern (string &optional point startglob)
   "Split STRING into a pattern.
 A pattern is a list where each element is either a string
-or a symbol, see `completion-pcm--merge-completions'."
+or a symbol, see `completion-pcm--merge-completions'.
+
+If STARTGLOB is non-nil, the pattern will start with the symbol
+`prefix' if it would otherwise start with a string."
   (if (and point (< point (length string)))
       (let ((prefix (substring string 0 point))
             (suffix (substring string point)))
@@ -3925,7 +3946,10 @@ completion-pcm--string->pattern
       (when (> (length string) p0)
         (if pending (push pending pattern))
         (push (substring string p0) pattern))
-      (nreverse pattern))))
+      (setq pattern (nreverse pattern))
+      (when (and startglob (stringp (car pattern)))
+        (push 'prefix pattern))
+      pattern)))
 
 (defun completion-pcm--optimize-pattern (p)
   ;; Remove empty strings in a separate phase since otherwise a ""
@@ -4218,11 +4242,12 @@ completion-pcm--hilit-commonality
    (t completions)))
 
 (defun completion-pcm--find-all-completions (string table pred point
-                                                    &optional filter)
+                                                    &optional filter startglob)
   "Find all completions for STRING at POINT in TABLE, satisfying PRED.
 POINT is a position inside STRING.
 FILTER is a function applied to the return value, that can be used, e.g. to
-filter out additional entries (because TABLE might not obey PRED)."
+filter out additional entries (because TABLE might not obey PRED).
+STARTGLOB controls whether there's a leading glob in the pattern."
   (unless filter (setq filter 'identity))
   (let* ((beforepoint (substring string 0 point))
          (afterpoint (substring string point))
@@ -4233,7 +4258,7 @@ completion-pcm--find-all-completions
     (setq string (substring string (car bounds) (+ point (cdr bounds))))
     (let* ((relpoint (- point (car bounds)))
            (pattern (completion-pcm--optimize-pattern
-                     (completion-pcm--string->pattern string relpoint)))
+                     (completion-pcm--string->pattern string relpoint startglob)))
            (all (condition-case-unless-debug err
                     (funcall filter
                              (completion-pcm--all-completions
@@ -4311,9 +4336,9 @@ completion-pcm--find-all-completions
           (signal (car firsterror) (cdr firsterror))
         (list pattern all prefix suffix)))))
 
-(defun completion-pcm-all-completions (string table pred point)
+(defun completion-pcm-all-completions (string table pred point &optional startglob)
   (pcase-let ((`(,pattern ,all ,prefix ,_suffix)
-               (completion-pcm--find-all-completions string table pred point)))
+               (completion-pcm--find-all-completions string table pred point nil startglob)))
     (when all
       (nconc (completion-pcm--hilit-commonality pattern all)
              (length prefix)))))
@@ -4489,17 +4514,25 @@ completion-pcm--merge-try
                     merged (max 0 (1- (length merged))) suffix))
       (cons (concat prefix merged suffix) (+ newpos (length prefix)))))))
 
-(defun completion-pcm-try-completion (string table pred point)
+(defun completion-pcm-try-completion (string table pred point &optional startglob)
   (pcase-let ((`(,pattern ,all ,prefix ,suffix)
                (completion-pcm--find-all-completions
                 string table pred point
                 (if minibuffer-completing-file-name
-                    'completion-pcm--filename-try-filter))))
+                    'completion-pcm--filename-try-filter)
+                startglob)))
     (completion-pcm--merge-try pattern all prefix suffix)))
 
 ;;; Substring completion
 ;; Mostly derived from the code of `basic' completion.
 
+(defcustom completion-substring-use-pcm nil
+  "If non-nil, the substring completion style performs partial-completion.
+
+This means that in addition to expanding at the start of the
+completion region, all text will be expanded following the
+partial-completion rules.")
+
 (defun completion-substring--all-completions
     (string table pred point &optional transform-pattern-fn)
   "Match the presumed substring STRING to the entries in TABLE.
@@ -4524,20 +4557,24 @@ completion-substring--all-completions
     (list all pattern prefix suffix (car bounds))))
 
 (defun completion-substring-try-completion (string table pred point)
-  (pcase-let ((`(,all ,pattern ,prefix ,suffix ,_carbounds)
-               (completion-substring--all-completions
-                string table pred point)))
-    (if minibuffer-completing-file-name
-        (setq all (completion-pcm--filename-try-filter all)))
-    (completion-pcm--merge-try pattern all prefix suffix)))
+  (if completion-substring-use-pcm
+      (completion-pcm-try-completion string table pred point t)
+    (pcase-let ((`(,all ,pattern ,prefix ,suffix ,_carbounds)
+                 (completion-substring--all-completions
+                  string table pred point)))
+      (if minibuffer-completing-file-name
+          (setq all (completion-pcm--filename-try-filter all)))
+      (completion-pcm--merge-try pattern all prefix suffix))))
 
 (defun completion-substring-all-completions (string table pred point)
-  (pcase-let ((`(,all ,pattern ,prefix ,_suffix ,_carbounds)
-               (completion-substring--all-completions
-                string table pred point)))
-    (when all
-      (nconc (completion-pcm--hilit-commonality pattern all)
-             (length prefix)))))
+  (if completion-substring-use-pcm
+      (completion-pcm-all-completions string table pred point t)
+    (pcase-let ((`(,all ,pattern ,prefix ,_suffix ,_carbounds)
+                 (completion-substring--all-completions
+                  string table pred point)))
+      (when all
+        (nconc (completion-pcm--hilit-commonality pattern all)
+               (length prefix))))))
 
 ;;; "flex" completion, also known as flx/fuzzy/scatter completion
 ;; Completes "foo" to "frodo" and "farfromsober"
-- 
2.39.3


  reply	other threads:[~2024-05-08 16:46 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-05 12:41 bug#70217: [PATCH] Add substring-partial-completion style Spencer Baugh
2024-04-05 18:35 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-05 19:46   ` Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-06  8:10     ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-18 15:19   ` Spencer Baugh
2024-05-08 16:46     ` Spencer Baugh [this message]
2024-05-08 17:14       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-16 20:26         ` Spencer Baugh
2024-05-16 22:09           ` Daniel Mendler via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-17  6:23           ` Eli Zaretskii
2024-05-25 21:22             ` Spencer Baugh
2024-05-26  7:56               ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-26 12:49                 ` Spencer Baugh
2024-05-26  9:11               ` Eli Zaretskii
2024-05-26 13:02                 ` Spencer Baugh
2024-05-26 15:51                   ` Eli Zaretskii
2024-05-28 14:39                     ` Spencer Baugh
2024-05-28 15:11                       ` Eli Zaretskii
2024-05-28 18:16                         ` Spencer Baugh
2024-05-28 18:36                           ` Eli Zaretskii
2024-05-28 18:51                             ` Spencer Baugh
2024-05-28 19:21                               ` Eli Zaretskii
2024-05-28 20:01                                 ` Spencer Baugh
2024-06-01 14:20                                   ` Eli Zaretskii
2024-06-02 12:16                                     ` Spencer Baugh
2024-06-02 14:34                                       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-08 13:13                                         ` Spencer Baugh
2024-08-10  8:02                                           ` Eli Zaretskii
2024-08-21 16:01                                             ` Spencer Baugh
2024-08-21 16:16                                               ` Spencer Baugh
2024-08-24  9:23                                                 ` Eli Zaretskii

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=ieredacqmjb.fsf@janestreet.com \
    --to=sbaugh@janestreet.com \
    --cc=70217@debbugs.gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /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.