From b71b973a0f1d06e0eca50c8b90066e90f22dacd5 Mon Sep 17 00:00:00 2001
From: Eshel Yaron <me@eshelyaron.com>
Date: Thu, 27 Feb 2025 15:34:24 +0100
Subject: [PATCH] ; (completion-preview-complete): Fix bug#76606

* lisp/completion-preview.el (completion-preview-complete):
Dismiss completion preview before calling ':exit-function'.
This eliminates potential flicker with slow exit functions
and avoids the broken preview update reported in bug#76606.

(cherry picked from commit 840c6824589c9a82deedc5112891d8014d134746)
---
 lisp/completion-preview.el | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/lisp/completion-preview.el b/lisp/completion-preview.el
index 6ace3f736d1..a8ff78d775c 100644
--- a/lisp/completion-preview.el
+++ b/lisp/completion-preview.el
@@ -831,6 +831,10 @@ completions list."
             ;; hook update the completion preview in case the candidate
             ;; can be completed further.
             (when (functionp efn)
+              ;; Remove stale preview since `efn' can make arbitrary
+              ;; text and point modifications that might interfere with
+              ;; a subsequent preview update.  See bug#76606.
+              (completion-preview-active-mode -1)
               (funcall efn (concat base com) (if (cdr all) 'exact 'finished)))
           ;; Otherwise, remove the common prefix from the preview.
           (completion-preview--inhibit-update)
-- 
2.39.5