Subject: bug#75544: 31.0.50; Completion preview does not work together with variable-pitch-mode by default

From: Terje Larsen <[email-masked]>
Date: Mon, 13 Jan 2025 23:31:57 +0100

- M-x 'variable-pitch-mode'
- M-x 'completion-preview-mode'
- Type 'eval'
- Notice that the preview is inserted in the middle of the text and the
 'l' is in the end and now you cannot get rid of this text without
 reloading the mode.
- You can also see the error that caused this in the '*Messages*'
  buffer:

Error in post-command-hook (completion-preview--post-command):
(excessive-lisp-nesting 1601)

The source of this is the recent commit 81cf42c8af75b9ee2f2cee36254e3286b2077cbc
'Auto-adapt completion preview background color'

The issue stems from the last line in this function and can be mitigated
by dropping the last line.
+(defun completion-preview--bg-color (pos)
+  "Return background color at POS."
+  ;; This takes into account face remappings and multiple overlays that
+  ;; specify the `face' property, unlike `background-color-at-point'.
+  (catch 'found
+    (named-let rec ((spec (seq-keep (lambda (ov) (overlay-get ov 'face))
+                                    (overlays-at pos t)))
+                    (trace nil))
+      (dolist (face (if (face-list-p spec) spec (list spec)))
+        (let (cur)
+          (if (and (setq cur (alist-get face face-remapping-alist))
+                   (not (memq cur trace)))
+              (rec cur (cons face trace))
+            (cond ((and face (symbolp face))
+                   (let ((value (face-attribute face :background nil t)))
+                     (unless (member value '(nil "unspecified-bg" unspecified))
+                       (throw 'found value))))
+                  ((consp face)
+                   (when-let* ((value (or (cdr (memq 'background-color face))
+                                          (cadr (memq :background face)))))
+                     (throw 'found value)))))))
+      (unless trace
+        (save-excursion
+          (goto-char pos)
+          (font-lock-ensure (pos-bol) (pos-eol)))
+        (rec (or (and font-lock-mode
+                      (get-text-property pos 'font-lock-face))
+                 (get-text-property pos 'face))
+             '(nil))
+        (rec 'default '(nil))))))

Another workaround is to drop the feature completely by setting
'completion-preview-adapt-background-color' to 'nil'.

However this solution is not optimal as it drops the feature.

I am not entirely sure why this issue is triggered when using the
'variable-pitch-mode'.

In GNU Emacs 31.0.50 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo
version 1.18.2, Xaw3d scroll bars)
Repository revision: 7dcc7605d58165dac233bcc029509f1ad9807f72
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12401004
System Description: NixOS 25.05 (Warbler)

Configured using:
 'configure
 --prefix=/nix/store/xgk9si0m6zyj0dry77x5vr7zna4kjnfm-emacs-git-20250112.0
 --disable-build-details --with-modules --with-x-toolkit=lucid
 --with-cairo --with-xft --with-compress-install
 --with-toolkit-scroll-bars --with-native-compilation
 --without-imagemagick --with-mailutils --without-small-ja-dic
 --with-tree-sitter --with-xinput2 --without-xwidgets --with-dbus
 --with-selinux'

Configured features:
CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG
LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2 M17N_FLT MODULES NATIVE_COMP NOTIFY
INOTIFY PDUMPER PNG RSVG SECCOMP SOUND SQLITE3 THREADS TIFF
TOOLKIT_SCROLL_BARS TREE_SITTER WEBP X11 XAW3D XDBE XIM XINPUT2 XPM
LUCID ZLIB

Important settings:
  value of $EMACSLOADPATH:
/nix/store/n9mlqz2n55cl707qsvib2jvgxsv024x4-emacs/share/emacs/site-lisp:
  value of $EMACSNATIVELOADPATH:
/nix/store/n9mlqz2n55cl707qsvib2jvgxsv024x4-emacs/share/emacs/native-lisp:/nix/store/ry0g92f55prsvdnkhsbrhdcsygx49mqx-elisp-packages/share/emacs/native-lisp/:/nix/store/xgk9si0m6zyj0dry77x5vr7zna4kjnfm-emacs-git-20250112.0/share/emacs/native-lisp/
  value of $LANG: en_US.UTF-8
  value of $XMODIFIERS: @im=ibus
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  completion-preview-mode: t
  buffer-face-mode: t
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  minibuffer-regexp-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t

Load-path shadows:
/nix/store/bfi1rzjms3hh299z8c844bjwvd453ar7-emacs-git-20250112-0-compat-30.0.2.0/share/emacs/site-lisp/compat
hides /nix/store/xgk9si0m6zyj0dry77x5vr7zna4kjnfm-emacs-git-20250112.0/share/emacs/31.0.50/lisp/emacs-lisp/compat

Features:
(shadow sort mail-extr emacsbug message mailcap yank-media puny dired
dired-loaddefs rfc822 mml mml-sec password-cache epa derived epg rfc6068
epg-config gnus-util text-property-search mm-decode mm-bodies mm-encode
mail-parse rfc2231 mailabbrev gmm-utils mailheader sendmail rfc2047
rfc2045 ietf-drums mm-util mail-prsvr mail-utils thingatpt help-fns
byte-opt gv bytecomp byte-compile radix-tree help-mode
completion-preview face-remap time-date subr-x cl-loaddefs cl-lib rmc
iso-transl tooltip cconv eldoc paren electric uniquify ediff-hook
vc-hooks lisp-float-type elisp-mode mwheel term/x-win x-win
term/common-win x-dnd touch-screen tool-bar dnd fontset image regexp-opt
fringe tabulated-list replace newcomment text-mode lisp-mode prog-mode
register page tab-bar menu-bar rfn-eshadow isearch easymenu timer select
scroll-bar mouse jit-lock font-lock syntax font-core term/tty-colors
frame minibuffer nadvice seq simple cl-generic indonesian philippine
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese composite emoji-zwj charscript
charprop case-table epa-hook jka-cmpr-hook help abbrev obarray oclosure
cl-preloaded button loaddefs theme-loaddefs faces cus-face macroexp
files window text-properties overlay sha1 md5 base64 format env
code-pages mule custom widget keymap hashtable-print-readable backquote
threads dbusbind inotify dynamic-setting system-font-setting
font-render-setting cairo x-toolkit xinput2 x multi-tty move-toolbar
make-network-process tty-child-frames native-compile emacs)

Memory information:
((conses 16 65950 10040) (symbols 48 6373 0) (strings 32 18940 2326)
 (string-bytes 1 668276) (vectors 16 11507)
 (vector-slots 8 157178 4088) (floats 8 31 24) (intervals 56 310 0)
 (buffers 992 10))