GNU bug report logs - #68875
[PATCH] ; Fix mid-symbol updating/cycling completion preview

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: emacs; Reported by: Eshel Yaron <me@HIDDEN>; Keywords: patch; Done: Eshel Yaron <me@HIDDEN>; Maintainer for emacs is bug-gnu-emacs@HIDDEN.
bug marked as fixed in version 30.1, send any further explanations to 68875 <at> debbugs.gnu.org and Eshel Yaron <me@HIDDEN> Request was from Eshel Yaron <me@HIDDEN> to control <at> debbugs.gnu.org. Full text available.

Message received at 68875 <at> debbugs.gnu.org:


Received: (at 68875) by debbugs.gnu.org; 21 Feb 2024 17:25:13 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Feb 21 12:25:13 2024
Received: from localhost ([127.0.0.1]:56118 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1rcqLI-0000Oi-FR
	for submit <at> debbugs.gnu.org; Wed, 21 Feb 2024 12:25:13 -0500
Received: from mail.eshelyaron.com ([107.175.124.16]:34928 helo=eshelyaron.com)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <me@HIDDEN>)
 id 1rcqEv-0008Ui-4W; Wed, 21 Feb 2024 12:18:51 -0500
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eshelyaron.com;
 s=mail; t=1708534788;
 bh=jbGREnJxzzCwQEJg0GNe8xc8TuyR3g+ed7Bi9YC2Rc8=;
 h=From:To:Subject:In-Reply-To:References:Date:From;
 b=C44MNqkh3MTIowhP9BK8tbw3h/v+qS6NFh5V2ncV66IzmIUJCTyh/wLGfLuxzWST4
 lb+8cCjCgvtKFLwW/A7LlEgmY1Zyvt63hduN6eG99tI9PDQJTB0PLGvgTlE+E/ePZ+
 WqjatEQUt++p3244yfUXo1+QcwtBHeNFgPGBtHJl9f4XeNXUt2Tpov15LlMvGH6Vpg
 SKcFbKLri6WMr9+hLDBrEiwC1E+C9YuE/fF5PPqYqi+AvC9avpVoahnNbbLclECdBA
 03WMb0Jck7dv0BOA923c+TCicUkJzeRvhWFeKp/Ka/9gpFfvJcY6AHOKHUBimXPKEk
 Xn05O8mOhVYzA==
From: Eshel Yaron <me@HIDDEN>
To: 68875 <at> debbugs.gnu.org
Subject: Re: bug#68875: [PATCH] ; Fix mid-symbol updating/cycling completion
 preview
In-Reply-To: <m1cytfm3gq.fsf@HIDDEN> (Eshel Yaron's message of "Fri,
 02 Feb 2024 13:54:13 +0100")
References: <m1msskgllb.fsf@HIDDEN> <m1cytfm3gq.fsf@HIDDEN>
X-Hashcash: 1:20:240221:68875 <at> debbugs.gnu.org::So9h7MH7Ee2/VXcJ:PnH
Date: Wed, 21 Feb 2024 17:59:46 +0100
Message-ID: <m15xyh3g8t.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: text/plain
X-Spam-Score: -1.9 (-)
X-Debbugs-Envelope-To: 68875
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -2.9 (--)

close 68875 30.1
quit

Eshel Yaron <me@HIDDEN> writes:

>> Tags: patch
>>
>> The attached patch fixes an issue where the completion preview overlay
>> (of Completion Preview mode) could at certain cases appear in the wrong
>> place when point is in the middle of a symbol.
>>
>> To observe the effect of this patch:
>>
>> 1. emacs -Q
>> 2. In the *scratch* buffer, say M-x completion-preview-mode RET
>> 3. Type "defaul-di"
>> 4. C-3 C-b to place point before the hyphen
>> 5. Type "t"
>>    The completion preview overlay appears after "-di", showing "rectory".
>>    So far so good.
>> 6. M-x completion-preview-next-candidate RET
>>    Before this patch, the preview now shows "-directory", thus repeating
>>    the existing suffix "-di".  With this patch, the preview shows just
>>    "rectory", as expected.
>
> Sorry, that patch contained a thinko (which was caught by one of the
> existing tests, fortunately).  I'm attaching an updated patch below.

Pushed to master, and closing the bug.




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#68875; Package emacs. Full text available.

Message received at 68875 <at> debbugs.gnu.org:


Received: (at 68875) by debbugs.gnu.org; 2 Feb 2024 12:54:29 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Fri Feb 02 07:54:29 2024
Received: from localhost ([127.0.0.1]:43812 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1rVt3s-0005Ft-Kv
	for submit <at> debbugs.gnu.org; Fri, 02 Feb 2024 07:54:29 -0500
Received: from mail.eshelyaron.com ([107.175.124.16]:53440 helo=eshelyaron.com)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <me@HIDDEN>) id 1rVt3q-0005Fl-T3
 for 68875 <at> debbugs.gnu.org; Fri, 02 Feb 2024 07:54:27 -0500
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eshelyaron.com;
 s=mail; t=1706878455;
 bh=cCN0GWxEAB+HZsqDKf4KMZlpK1XvmqKwQiqSeAyt1dU=;
 h=From:To:Subject:In-Reply-To:References:Date:From;
 b=rUy/T0MnE8C+rb2OneYWoUImPu0k5vStei4pK5/lyaki2BMsCyk7FVA6wWA+Mm1ix
 jDYrtBPZphA6OOzZjT8HEgXLM6WbGkAOGD93mjlcXPyQnU7HaZ7XiCrvArTCIacVno
 EwUj5LECyPiYzsSECc8BPWerMIV2PSuGvsQLg4PTPhLgORVwTh/ZOQqMVlJcW9A3ZG
 v06RJP/+WZZgYeFAb9WEqR+g7XzKHfMF595g02OlN6v6FRh/au33bYacW7Of7dVnB6
 NL28PwjWyU1ExpTmDq7lBe6bc3x13ULireRfPsWm2W5aagQnFsLmNcT87LCvZN+N2C
 aG1yMDhdivqTw==
From: Eshel Yaron <me@HIDDEN>
To: 68875 <at> debbugs.gnu.org
Subject: Re: bug#68875: [PATCH] ; Fix mid-symbol updating/cycling completion
 preview
In-Reply-To: <m1msskgllb.fsf@HIDDEN> (Eshel Yaron via's message of
 "Thu, 01 Feb 2024 18:06:56 +0100")
References: <m1msskgllb.fsf@HIDDEN>
X-Hashcash: 1:20:240202:bug-gnu-emacs@HIDDEN::egfPvVe8c78zsnrQ:1J3D
X-Hashcash: 1:20:240202:68875 <at> debbugs.gnu.org::qy/Kl46Pu0Qnur7F:37kx
Date: Fri, 02 Feb 2024 13:54:13 +0100
Message-ID: <m1cytfm3gq.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
X-Spam-Score: -0.0 (/)
X-Debbugs-Envelope-To: 68875
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

--=-=-=
Content-Type: text/plain

> Tags: patch
>
> The attached patch fixes an issue where the completion preview overlay
> (of Completion Preview mode) could at certain cases appear in the wrong
> place when point is in the middle of a symbol.
>
> To observe the effect of this patch:
>
> 1. emacs -Q
> 2. In the *scratch* buffer, say M-x completion-preview-mode RET
> 3. Type "defaul-di"
> 4. C-3 C-b to place point before the hyphen
> 5. Type "t"
>    The completion preview overlay appears after "-di", showing "rectory".
>    So far so good.
> 6. M-x completion-preview-next-candidate RET
>    Before this patch, the preview now shows "-directory", thus repeating
>    the existing suffix "-di".  With this patch, the preview shows just
>    "rectory", as expected.

Sorry, that patch contained a thinko (which was caught by one of the
existing tests, fortunately).  I'm attaching an updated patch below.


Thanks,

Eshel


--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment;
 filename=v2-0001-Fix-mid-symbol-updating-cycling-completion-previe.patch

From dd7e8339ec705ebc00ad9a839373d3b668fd39ed Mon Sep 17 00:00:00 2001
From: Eshel Yaron <me@HIDDEN>
Date: Thu, 1 Feb 2024 12:30:24 +0100
Subject: [PATCH v2] ; Fix mid-symbol updating/cycling completion preview

This fixes an issue where 'completion-preview-next-candidate' would
fail to take into account the part of the symbol that follows
point (the suffix) when point is at the middle of a symbol, as well as
a similar issue in 'completion-preview--show' that would manifest with
slow 'completion-at-point-functions'.

* lisp/completion-preview.el (completion-preview-next-candidate)
(completion-preview--show): Use recorded 'completion-preview-end'
position instead of current point.

* test/lisp/completion-preview-tests.el (completion-preview-mid-symbol-cycle):
New test.  (Bug#68875)
---
 lisp/completion-preview.el            | 24 ++++++++++++------------
 test/lisp/completion-preview-tests.el | 15 +++++++++++++++
 2 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/lisp/completion-preview.el b/lisp/completion-preview.el
index 6fd60f3c416..e827da43a08 100644
--- a/lisp/completion-preview.el
+++ b/lisp/completion-preview.el
@@ -302,21 +302,21 @@ completion-preview--show
     ;; never display a stale preview and that the preview doesn't
     ;; flicker, even with slow completion backends.
     (let* ((beg (completion-preview--get 'completion-preview-beg))
+           (end (max (point) (overlay-start completion-preview--overlay)))
            (cands (completion-preview--get 'completion-preview-cands))
            (index (completion-preview--get 'completion-preview-index))
            (cand (nth index cands))
-           (len (length cand))
-           (end (+ beg len))
-           (cur (point))
-           (face (get-text-property 0 'face (completion-preview--get 'after-string))))
-      (if (and (< beg cur end) (string-prefix-p (buffer-substring beg cur) cand))
+           (after (completion-preview--get 'after-string))
+           (face (get-text-property 0 'face after)))
+      (if (and (<= beg (point) end (1- (+ beg (length cand))))
+               (string-prefix-p (buffer-substring beg end) cand))
           ;; The previous preview is still applicable, update it.
           (overlay-put (completion-preview--make-overlay
-                        cur (propertize (substring cand (- cur beg))
+                        end (propertize (substring cand (- end beg))
                                         'face face
                                         'mouse-face 'completion-preview-highlight
                                         'keymap completion-preview--mouse-map))
-                       'completion-preview-end cur)
+                       'completion-preview-end end)
         ;; The previous preview is no longer applicable, hide it.
         (completion-preview-active-mode -1))))
   ;; Run `completion-at-point-functions' to get a new candidate.
@@ -366,16 +366,16 @@ completion-preview-next-candidate
   (interactive "p")
   (when completion-preview-active-mode
     (let* ((beg (completion-preview--get 'completion-preview-beg))
+           (end (completion-preview--get 'completion-preview-end))
            (all (completion-preview--get 'completion-preview-cands))
            (cur (completion-preview--get 'completion-preview-index))
            (len (length all))
            (new (mod (+ cur direction) len))
-           (str (nth new all))
-           (pos (point)))
-      (while (or (<= (+ beg (length str)) pos)
-                 (not (string-prefix-p (buffer-substring beg pos) str)))
+           (str (nth new all)))
+      (while (or (<= (+ beg (length str)) end)
+                 (not (string-prefix-p (buffer-substring beg end) str)))
         (setq new (mod (+ new direction) len) str (nth new all)))
-      (let ((aft (propertize (substring str (- pos beg))
+      (let ((aft (propertize (substring str (- end beg))
                              'face (if (< 1 len)
                                        'completion-preview
                                      'completion-preview-exact)
diff --git a/test/lisp/completion-preview-tests.el b/test/lisp/completion-preview-tests.el
index 190764e9125..5b2c28bd3dd 100644
--- a/test/lisp/completion-preview-tests.el
+++ b/test/lisp/completion-preview-tests.el
@@ -181,4 +181,19 @@ completion-preview-capf-errors
       (completion-preview--post-command))
     (completion-preview-tests--check-preview "barbaz" 'exact)))
 
+(ert-deftest completion-preview-mid-symbol-cycle ()
+  "Test cycling the completion preview with point at the middle of a symbol."
+  (with-temp-buffer
+    (setq-local completion-at-point-functions
+                (list
+                 (completion-preview-tests--capf
+                  '("foobar" "foobaz"))))
+    (insert "fooba")
+    (forward-char -2)
+    (let ((this-command 'self-insert-command))
+      (completion-preview--post-command))
+    (completion-preview-tests--check-preview "r")
+    (completion-preview-next-candidate 1)
+    (completion-preview-tests--check-preview "z")))
+
 ;;; completion-preview-tests.el ends here
-- 
2.42.0


--=-=-=--




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#68875; Package emacs. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 1 Feb 2024 17:07:24 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Thu Feb 01 12:07:24 2024
Received: from localhost ([127.0.0.1]:43093 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1rVaX6-0001xx-04
	for submit <at> debbugs.gnu.org; Thu, 01 Feb 2024 12:07:24 -0500
Received: from lists.gnu.org ([2001:470:142::17]:50938)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <me@HIDDEN>) id 1rVaX1-0001xd-VZ
 for submit <at> debbugs.gnu.org; Thu, 01 Feb 2024 12:07:22 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <me@HIDDEN>) id 1rVaWk-0003fv-Eh
 for bug-gnu-emacs@HIDDEN; Thu, 01 Feb 2024 12:07:02 -0500
Received: from mail.eshelyaron.com ([107.175.124.16] helo=eshelyaron.com)
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <me@HIDDEN>) id 1rVaWi-00012v-0m
 for bug-gnu-emacs@HIDDEN; Thu, 01 Feb 2024 12:07:02 -0500
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eshelyaron.com;
 s=mail; t=1706807219;
 bh=US04eHX+O/vulvn8JFl+yXgQ491jY9gJmKZAZb16lb0=;
 h=From:To:Subject:Date:From;
 b=vx+WRCTNfO9OtGe1fKdKvxz+wmzns7v3anZvoBzu85lgqTfx3ghtIhXiV16H86uKp
 8Qj9SFNoupE0UcvTwtPz/Mfph9O7BleY7WSTdK3vg08otJQInIOy9DSADPhBB6O6pZ
 mXC3p6mZbbUmYcrZXCsZNqEBPvOMVV3t6Ao4z9HI7UUWp4HXPJaM4WC2+s0afm86N3
 0mxcEtxfQJ17k9WN9BUyiPKnm7dqkiJRoxaQVPlH/IqJ6bKs9wdjjf45T9vxATxuQU
 F/GmW0udbODszvUxH/pFzdkN+9UC2hw8/cu0fbCA69yZOXepW+h+YCPCrFHOuT4l5R
 MKuY1kSKVoKZA==
From: Eshel Yaron <me@HIDDEN>
To: bug-gnu-emacs@HIDDEN
Subject: [PATCH] ; Fix mid-symbol updating/cycling completion preview
X-Hashcash: 1:20:240201:bug-gnu-emacs@HIDDEN::rnu6FLuIssH18sYZ:1S3Z
Date: Thu, 01 Feb 2024 18:06:56 +0100
Message-ID: <m1msskgllb.fsf@HIDDEN>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
Received-SPF: pass client-ip=107.175.124.16; envelope-from=me@HIDDEN;
 helo=eshelyaron.com
X-Spam_score_int: -20
X-Spam_score: -2.1
X-Spam_bar: --
X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,
 DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001,
 SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Spam-Score: 0.9 (/)
X-Debbugs-Envelope-To: submit
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -0.1 (/)

--=-=-=
Content-Type: text/plain

Tags: patch

The attached patch fixes an issue where the completion preview overlay
(of Completion Preview mode) could at certain cases appear in the wrong
place when point is in the middle of a symbol.

To observe the effect of this patch:

1. emacs -Q
2. In the *scratch* buffer, say M-x completion-preview-mode RET
3. Type "defaul-di"
4. C-3 C-b to place point before the hyphen
5. Type "t"
   The completion preview overlay appears after "-di", showing "rectory".
   So far so good.
6. M-x completion-preview-next-candidate RET
   Before this patch, the preview now shows "-directory", thus repeating
   the existing suffix "-di".  With this patch, the preview shows just
   "rectory", as expected.


--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment;
 filename=0001-Fix-mid-symbol-updating-cycling-completion-preview.patch

From e64361b0fbd78bb48b859658f9d0c1bb36916f50 Mon Sep 17 00:00:00 2001
From: Eshel Yaron <me@HIDDEN>
Date: Thu, 1 Feb 2024 12:30:24 +0100
Subject: [PATCH] ; Fix mid-symbol updating/cycling completion preview

This fixes an issue where 'completion-preview-next-candidate' would
fail to take into account the part of the symbol that follows
point (the suffix) when point is at the middle of a symbol, as well as
a similar issue in 'completion-preview--show' that would manifest with
slow 'completion-at-point-functions'.

* lisp/completion-preview.el (completion-preview-next-candidate)
(completion-preview--show): Use recorded 'completion-preview-end'
position instead of current point.

* test/lisp/completion-preview-tests.el (completion-preview-mid-symbol-cycle):
New test.
---
 lisp/completion-preview.el            | 24 ++++++++++++------------
 test/lisp/completion-preview-tests.el | 15 +++++++++++++++
 2 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/lisp/completion-preview.el b/lisp/completion-preview.el
index 6fd60f3c416..d7d3e9bf9f7 100644
--- a/lisp/completion-preview.el
+++ b/lisp/completion-preview.el
@@ -302,21 +302,21 @@ completion-preview--show
     ;; never display a stale preview and that the preview doesn't
     ;; flicker, even with slow completion backends.
     (let* ((beg (completion-preview--get 'completion-preview-beg))
+           (end (max (point) (completion-preview--get 'completion-preview-end)))
            (cands (completion-preview--get 'completion-preview-cands))
            (index (completion-preview--get 'completion-preview-index))
            (cand (nth index cands))
-           (len (length cand))
-           (end (+ beg len))
-           (cur (point))
-           (face (get-text-property 0 'face (completion-preview--get 'after-string))))
-      (if (and (< beg cur end) (string-prefix-p (buffer-substring beg cur) cand))
+           (after (completion-preview--get 'after-string))
+           (face (get-text-property 0 'face after)))
+      (if (and (<= beg (point) end (1- (+ beg (length cand))))
+               (string-prefix-p (buffer-substring beg end) cand))
           ;; The previous preview is still applicable, update it.
           (overlay-put (completion-preview--make-overlay
-                        cur (propertize (substring cand (- cur beg))
+                        end (propertize (substring cand (- end beg))
                                         'face face
                                         'mouse-face 'completion-preview-highlight
                                         'keymap completion-preview--mouse-map))
-                       'completion-preview-end cur)
+                       'completion-preview-end end)
         ;; The previous preview is no longer applicable, hide it.
         (completion-preview-active-mode -1))))
   ;; Run `completion-at-point-functions' to get a new candidate.
@@ -366,16 +366,16 @@ completion-preview-next-candidate
   (interactive "p")
   (when completion-preview-active-mode
     (let* ((beg (completion-preview--get 'completion-preview-beg))
+           (end (completion-preview--get 'completion-preview-end))
            (all (completion-preview--get 'completion-preview-cands))
            (cur (completion-preview--get 'completion-preview-index))
            (len (length all))
            (new (mod (+ cur direction) len))
-           (str (nth new all))
-           (pos (point)))
-      (while (or (<= (+ beg (length str)) pos)
-                 (not (string-prefix-p (buffer-substring beg pos) str)))
+           (str (nth new all)))
+      (while (or (<= (+ beg (length str)) end)
+                 (not (string-prefix-p (buffer-substring beg end) str)))
         (setq new (mod (+ new direction) len) str (nth new all)))
-      (let ((aft (propertize (substring str (- pos beg))
+      (let ((aft (propertize (substring str (- end beg))
                              'face (if (< 1 len)
                                        'completion-preview
                                      'completion-preview-exact)
diff --git a/test/lisp/completion-preview-tests.el b/test/lisp/completion-preview-tests.el
index 190764e9125..5b2c28bd3dd 100644
--- a/test/lisp/completion-preview-tests.el
+++ b/test/lisp/completion-preview-tests.el
@@ -181,4 +181,19 @@ completion-preview-capf-errors
       (completion-preview--post-command))
     (completion-preview-tests--check-preview "barbaz" 'exact)))
 
+(ert-deftest completion-preview-mid-symbol-cycle ()
+  "Test cycling the completion preview with point at the middle of a symbol."
+  (with-temp-buffer
+    (setq-local completion-at-point-functions
+                (list
+                 (completion-preview-tests--capf
+                  '("foobar" "foobaz"))))
+    (insert "fooba")
+    (forward-char -2)
+    (let ((this-command 'self-insert-command))
+      (completion-preview--post-command))
+    (completion-preview-tests--check-preview "r")
+    (completion-preview-next-candidate 1)
+    (completion-preview-tests--check-preview "z")))
+
 ;;; completion-preview-tests.el ends here
-- 
2.42.0


--=-=-=--




Acknowledgement sent to Eshel Yaron <me@HIDDEN>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs@HIDDEN. Full text available.
Report forwarded to bug-gnu-emacs@HIDDEN:
bug#68875; Package emacs. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Wed, 21 Feb 2024 17:30:02 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.