Subject: Fix the unaligned tab in whitespace mode

From: Young Arto <[email-masked]>
Date: Sat, 13 Jan 2024 12:23:27 +0000

As described in the commit message, this patch is meant to fix the unaligned tab when tab-mark is turned on.
From 595ff1877c750b35077298f00ad1a9a494927ab1 Mon Sep 17 00:00:00 2001
From: Arto Young <[email-masked]>
Date: Sat, 13 Jan 2024 19:52:44 +0800
Subject: [PATCH] Fix the unaligned tab with tab-mark of whitespace mode

There is a bug in whitespace mode which used the display table to
replace a tab with a tab-mark vector.

If we has a line like this:
------
123\t5678
------

If tab-width is 4 and tab-mark is [?> ?\t],
The line will be displayed as following:
(we use '^' to represent tab extra spaces)
------
123>^^^^5678
------

However, it should be displayed as:
------
123>5678
------

If the line was:
------
12\t5678
------

It would be displayed as:
------
12>^5678
------

It happens on the boundary of tab, so we should adjust the 'next_tab_x'
in this case.

* src/term.c (produce_glyphs): Correct the next_tab_x
* src/xdisp.c (gui_produce_glyphs): Correct the next_tab_x
---
 src/term.c  | 7 +++++++
 src/xdisp.c | 7 +++++++
 2 files changed, 14 insertions(+)

diff --git a/src/term.c b/src/term.c
index d3c858c6bf2..e34b2ed99de 100644
--- a/src/term.c
+++ b/src/term.c
@@ -1642,6 +1642,13 @@ produce_glyphs (struct it *it)
 	   * it->tab_width);
       if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 	next_tab_x += it->lnum_pixel_width;
+
+      /* For tab-mark display vector like [?> ?\t] for an
+	 original char '\t', we should not display ">\t"
+	 but only '>' at tab-width boundry, or tab real length
+	 will be tab-width+1, so we decrease tab-width here. */
+      if (it->c == '\t' && it->dpvec && it->current.dpvec_index > 0 && absolute_x % it->tab_width == 0)
+	next_tab_x -= it->tab_width;
       int nspaces;
 
       /* If part of the TAB has been displayed on the previous line
diff --git a/src/xdisp.c b/src/xdisp.c
index 14cf030ca4e..13fb6e5a308 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -32621,6 +32621,13 @@ gui_produce_glyphs (struct it *it)
 		    next_tab_x -= it->stretch_adjust;
 		}
 
+	      /* For tab-mark display vector like [?> ?\t] for an
+		 original char '\t', we should not display ">\t"
+		 but only '>' at tab-width boundry, or tab real length
+		 will be tab-width+1, so we decrease tab-width here. */
+	      if (it->c == '\t' && it->dpvec && it->current.dpvec_index > 0 && x % tab_width == 0)
+		next_tab_x -= tab_width;
+
 	      it->pixel_width = next_tab_x - x0;
 	      it->nglyphs = 1;
 	      if (FONT_TOO_HIGH (font))
-- 
2.43.0