Subject: bug#73882: 31.0.50; global-visual-wrap-prefix-mode misaligns org tables

From: Jim Porter <[email-masked]>
Date: Sat, 19 Oct 2024 16:14:48 -0700

On 10/19/2024 2:52 AM, Arthur Elsenaar wrote:
> | head  | 1 | 2 | 3 | 4 |
> |-------+---+---+---+---|
> | apple |   |   |   |   |
> | orange |   |   |   |   |
> | pear  |   |   |   |   |
> | banana |   |   |   |   |
> 
> Table cells are misaligned.
> When one hits <tab> within a cell, the cells align. However when
> global-visual-wrap-prefix-mode is enabled, the cells get
> misaligned. It appears a character in the first column gets attributes
> added that makes the character span more than one space.

Thanks for noticing. This patch should do the trick, I think.

From 727abc3fbb8d43de78a9acf47aa4fb3a97bbb21a Mon Sep 17 00:00:00 2001
From: Jim Porter <[email-masked]>
Date: Sat, 19 Oct 2024 16:12:43 -0700
Subject: [PATCH] Fix 'min-width' calculation in 'visual-wrap-prefix-mode'

* lisp/visual-wrap.el (visual-wrap--content-prefix): Remove 'min-width'
before computing the pixel width to avoid miscalculation (bug#73882).
---
 lisp/visual-wrap.el | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/lisp/visual-wrap.el b/lisp/visual-wrap.el
index 76276c0f474..fa128d287a4 100644
--- a/lisp/visual-wrap.el
+++ b/lisp/visual-wrap.el
@@ -165,9 +165,12 @@ visual-wrap--content-prefix
     ;; first-line prefix.
     (let ((avg-space (propertize (buffer-substring position (1+ position))
                                  'display '(space :width 1))))
-        (max (string-width prefix)
-             (ceiling (string-pixel-width prefix (current-buffer))
-                      (string-pixel-width avg-space (current-buffer))))))))
+      ;; Remove any `min-width' display specs since we'll replace with
+      ;; our own later in `visual-wrap--apply-to-line' (bug#73882).
+      (add-display-text-property 0 (length prefix) 'min-width nil prefix)
+      (max (string-width prefix)
+           (ceiling (string-pixel-width prefix (current-buffer))
+                    (string-pixel-width avg-space (current-buffer))))))))
 
 (defun visual-wrap-fill-context-prefix (beg end)
   "Compute visual wrap prefix from text between BEG and END.
-- 
2.25.1