Subject: bug#78474: 31.0.50; Wrong char insertion in rxvt

From: Manuel Giraud <[email-masked]>
Date: Tue, 22 Jul 2025 16:09:33 +0200

Robert Pluim <[email-masked]> writes:

>>>>>> On Tue, 22 Jul 2025 14:10:11 +0200, Samuel Thibault <[email-masked]> said:
>
>     Samuel> Eli Zaretskii, le mar. 22 juil. 2025 15:08:09 +0300, a ecrit:
>     >> > Date: Mon, 21 Jul 2025 18:25:05 +0200
>     >> > From: Samuel Thibault <[email-masked]>
>     >> > Cc: Eli Zaretskii <[email-masked]>,
>     >> > 	Seb Hinderer <[email-masked]>, [email-masked],
>     >> > 	[email-masked]
>     >> > 
>     >> > > > Samuel> It's actually even slower on my box.
>     >> > > 
>     >> > > You benchmarked moving by one character. Moving by tabs can move in
>     >> > > multiples of 8 characters, so I doubt itʼs that cut and dried.
>     >> > 
>     >> > The case which actually poses problem is only that \t\b vs \e[C. Using
>     >> > tab without moving back with \b does not pose problem.
>     >> 
>     >> If that's the only problematic case, then disabling TABs as in the
>     >> proposed patch is not TRT.
>
>     Samuel> I don't think I asked for this, and only confirmed that it's one way to
>     Samuel> do it.
>
> That was my misinterpretation of the request. Itʼs easy enough to
> restrict the change to only affect the '\t\b' case, but weʼll have to
> come up with a new name for the user option :-)

What about this?
>From 002efa4bd1db6efd9f65dc43364b084680906753 Mon Sep 17 00:00:00 2001
From: Manuel Giraud <[email-masked]>
Date: Tue, 22 Jul 2025 15:15:39 +0200
Subject: [PATCH] Do not overshoot and backup for tty motions

---
 src/cm.c | 24 +++---------------------
 1 file changed, 3 insertions(+), 21 deletions(-)

diff --git a/src/cm.c b/src/cm.c
index 150d1c9a580..fb18ef26251 100644
--- a/src/cm.c
+++ b/src/cm.c
@@ -187,9 +187,7 @@ calccost (struct tty_display_info *tty,
                     c,
                     totalcost;
     int     ntabs,
-            n2tabs,
             tabx,
-            tab2x,
             tabcost;
     register const char *p;
 
@@ -229,37 +227,21 @@ calccost (struct tty_display_info *tty,
 	goto olddelta;		/* forget it! */
 
     /*
-     * ntabs is # tabs towards but not past dstx; n2tabs is one more
-     * (ie past dstx), but this is only valid if that is not past the
-     * right edge of the screen.  We can check that at the same time
-     * as we figure out where we would be if we use the tabs (which
-     * we will put into tabx (for ntabs) and tab2x (for n2tabs)).
+     * ntabs is # tabs towards but not past dstx.  tabx is where we
+     * would be if we put those ntabs tabulations.
      */
 
     ntabs = (deltax + srcx % tty->Wcm->cm_tabwidth) / tty->Wcm->cm_tabwidth;
-    n2tabs = ntabs + 1;
     tabx = (srcx / tty->Wcm->cm_tabwidth + ntabs) * tty->Wcm->cm_tabwidth;
-    tab2x = tabx + tty->Wcm->cm_tabwidth;
-
-    if (tab2x >= tty->Wcm->cm_cols)	/* too far (past edge) */
-	n2tabs = 0;
 
     /*
-     * Now set tabcost to the cost for using ntabs, and c to the cost
-     * for using n2tabs, then pick the minimum.
+     * Now set tabcost to the cost for using ntabs.
      */
 
 		   /* cost for ntabs           +    cost for right motion */
     tabcost = ntabs ? ntabs * tty->Wcm->cc_tab + (dstx - tabx) * tty->Wcm->cc_right
 		    : BIG;
 
-		   /* cost for n2tabs          +    cost for left motion */
-    c = n2tabs  ?    n2tabs * tty->Wcm->cc_tab + (tab2x - dstx) * tty->Wcm->cc_left
-		: BIG;
-
-    if (c < tabcost)		/* then cheaper to overshoot & back up */
-	ntabs = n2tabs, tabcost = c, tabx = tab2x;
-
     if (tabcost >= BIG)		/* caint use tabs */
 	goto newdelta;
 
-- 
2.50.1

Words of warning: I'm not a heavy user of tty emacs and don't really
have an idea of what would be the implications of such change (don't
shoot me).  But maybe, Samuel and Sebastien could try this and see if it
solves their issue.  Then, we could decide to guard behind a user option
and what would be the default.
-- 
Manuel Giraud