all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [PATCH] Handle case where `beg` and `end` are strings instead of markers
@ 2022-04-29  0:36 James N. V. Cash
  2022-04-29  1:10 ` James N. V. Cash
  0 siblings, 1 reply; 25+ messages in thread
From: James N. V. Cash @ 2022-04-29  0:36 UTC (permalink / raw)
  To: emacs-devel


With the changes to pass affixes to the completion functions, the
begin and end points passed to the completion function are now
sometimes strings instead of numbers or markers. This handles that
case by searching for said prefix and suffix.

Without this, completion functions that call `completion--replace` error
out -- for example, completing tags in org-mode.

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

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index ef71b4e6be..d75b771044 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1147,35 +1147,46 @@ 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)
+                    (length 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



^ permalink raw reply related	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2022-05-19 18:52 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-04-29  0:36 [PATCH] Handle case where `beg` and `end` are strings instead of markers James N. V. Cash
2022-04-29  1:10 ` James N. V. Cash
2022-04-29  7:05   ` Juri Linkov
2022-04-29 12:10     ` James N. V. Cash
2022-04-29 12:55       ` Stefan Monnier
2022-04-29 14:07         ` James N. V. Cash
2022-04-29 16:18           ` Stefan Monnier
2022-04-29 17:20             ` James N. V. Cash
2022-04-29 17:29             ` Juri Linkov
2022-04-29 21:05         ` Stefan Monnier
2022-04-30 12:41           ` James N. V. Cash
2022-04-30 13:44             ` Stefan Monnier
2022-04-30 15:48               ` James N. V. Cash
2022-05-01 18:06                 ` Juri Linkov
2022-05-02 15:32                   ` James N. V. Cash
2022-05-02 19:11                     ` Juri Linkov
2022-05-04 12:08                       ` James N. V. Cash
2022-05-04 19:27                         ` Juri Linkov
2022-05-04 21:09                           ` James N. V. Cash
2022-05-05 18:16                             ` Juri Linkov
2022-05-01 18:03               ` Juri Linkov
2022-05-01 18:37                 ` Stefan Monnier
2022-05-05 18:19                   ` Juri Linkov
2022-05-05 18:30         ` Exiting completion-in-region-mode (was: Handle case where `beg` and `end` are strings instead of markers) Juri Linkov
2022-05-19 18:52           ` Exiting completion-in-region-mode Juri Linkov

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.