emacs-devel
[Top][All Lists]
Advanced

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

Re: [PATCH] Handle case where `beg` and `end` are strings instead of mar


From: James N . V . Cash
Subject: Re: [PATCH] Handle case where `beg` and `end` are strings instead of markers
Date: Fri, 29 Apr 2022 08:10:38 -0400

Juri Linkov <juri@linkov.net> writes:

> Please describe how do you expect this should behave from a UI point of view.
> I see that when using on a simple call:
>
>   (completing-read-multiple "Tags: " '(("tag1") ("tag2") ("tag3")))
>
> subsequent completions can be selected properly
> after replacing these lines in your patch
>
> +                 (+ (search-forward beg)
> +                    (length beg)))))
>
> with
>
> +                 (search-forward beg))))

Ah, that seems to do it -- I was seeing subsequent completions only
partially inserting, but that change you suggest fixes that and works as
expected.

New patch as follows then:

---
 lisp/minibuffer.el | 68 ++++++++++++++++++++++++++--------------------
 1 file changed, 39 insertions(+), 29 deletions(-)

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index ef71b4e6be..08e2dc5a37 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1147,35 +1147,45 @@ completion--replace
                               newtext)
     ;; Remove all text properties.
     (set-text-properties 0 (length newtext) nil newtext))
-  ;; Maybe this should be in subr.el.
-  ;; You'd think this is trivial to do, but details matter if you want
-  ;; to keep markers "at the right place" and be robust in the face of
-  ;; after-change-functions that may themselves modify the buffer.
-  (let ((prefix-len 0))
-    ;; Don't touch markers in the shared prefix (if any).
-    (while (and (< prefix-len (length newtext))
-                (< (+ beg prefix-len) end)
-                (eq (char-after (+ beg prefix-len))
-                    (aref newtext prefix-len)))
-      (setq prefix-len (1+ prefix-len)))
-    (unless (zerop prefix-len)
-      (setq beg (+ beg prefix-len))
-      (setq newtext (substring newtext prefix-len))))
-  (let ((suffix-len 0))
-    ;; Don't touch markers in the shared suffix (if any).
-    (while (and (< suffix-len (length newtext))
-                (< beg (- end suffix-len))
-                (eq (char-before (- end suffix-len))
-                    (aref newtext (- (length newtext) suffix-len 1))))
-      (setq suffix-len (1+ suffix-len)))
-    (unless (zerop suffix-len)
-      (setq end (- end suffix-len))
-      (setq newtext (substring newtext 0 (- suffix-len))))
-    (goto-char beg)
-    (let ((length (- end beg)))         ;Read `end' before we insert the text.
-      (insert-and-inherit newtext)
-      (delete-region (point) (+ (point) length)))
-    (forward-char suffix-len)))
+  (let ((beg (if (number-or-marker-p beg)
+                 beg
+               (save-excursion
+                 (goto-char (minibuffer-prompt-end))
+                 (search-forward beg))))
+        (end (if (number-or-marker-p end)
+                 end
+               (save-excursion
+                 (goto-char (point-max))
+                 (search-backward end)))))
+    ;; Maybe this should be in subr.el.
+    ;; You'd think this is trivial to do, but details matter if you want
+    ;; to keep markers "at the right place" and be robust in the face of
+    ;; after-change-functions that may themselves modify the buffer.
+    (let ((prefix-len 0))
+      ;; Don't touch markers in the shared prefix (if any).
+      (while (and (< prefix-len (length newtext))
+                  (< (+ beg prefix-len) end)
+                  (eq (char-after (+ beg prefix-len))
+                      (aref newtext prefix-len)))
+        (setq prefix-len (1+ prefix-len)))
+      (unless (zerop prefix-len)
+        (setq beg (+ beg prefix-len))
+        (setq newtext (substring newtext prefix-len))))
+    (let ((suffix-len 0))
+      ;; Don't touch markers in the shared suffix (if any).
+      (while (and (< suffix-len (length newtext))
+                  (< beg (- end suffix-len))
+                  (eq (char-before (- end suffix-len))
+                      (aref newtext (- (length newtext) suffix-len 1))))
+        (setq suffix-len (1+ suffix-len)))
+      (unless (zerop suffix-len)
+        (setq end (- end suffix-len))
+        (setq newtext (substring newtext 0 (- suffix-len))))
+      (goto-char beg)
+      (let ((length (- end beg)))         ;Read `end' before we insert the 
text.
+        (insert-and-inherit newtext)
+        (delete-region (point) (+ (point) length)))
+      (forward-char suffix-len))))

 (defcustom completion-cycle-threshold nil
   "Number of completion candidates below which cycling is used.
--
2.25.1



reply via email to

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