Subject: bug#41506: 28.0.50; RTL problem

From: Pip Cet <[email-masked]>
Date: Tue, 2 Jun 2020 10:17:55 +0000

On Sun, May 24, 2020 at 2:46 PM Eli Zaretskii <[email-masked]> wrote:
> > From: Pip Cet <[email-masked]>
> > Date: Sun, 24 May 2020 13:05:17 +0000
> >
> > I'm surprised by the way vanilla Emacs behaves when given RTL input:
> >
> > Recipe:
> > emacs -Q hebrew.txt
> >
> > hebrew.txt contains (or should contain!) two newlines followed by the
> > Hebrew word Ivri, punctuated, followed by another single newline.
> >
> > Expected result:
> > right-aligned text
> >
> > Actual result:
> > left-aligned text
> >
> > Is that a bug, or is there something I don't understand?
>
> It's a bug, but one that's very tricky to fix, AFAIR.  If you insert
> or delete a single character, the display becomes correct.

The attached patch seems to avoid the problem, but I'm sure I'm
missing something.

The comment says "don't do that at BEGV since then we are potentially
in a new paragraph that doesn't yet exist". I'm failing to make sense
of that, and the commit (5e65aec01a9bc5a147e492f11dd0115c98bedef4)
isn't too helpful either: "Fix bidi iteration near BEGV and ZV."

I suspect what might have been meant is that narrowing an LTR
paragraph to a line containing STRONG_R text shouldn't result in RTL
display, but it does...
diff --git a/src/bidi.c b/src/bidi.c
index 1017bd2d52..e3a5fe7de6 100644
--- a/src/bidi.c
+++ b/src/bidi.c
@@ -1707,15 +1707,12 @@ bidi_paragraph_init (bidi_dir_t dir, struct bidi_it *bidi_it, bool no_default_p)
 	return;
 
       /* If we are on a newline, get past it to where the next
-	 paragraph might start.  But don't do that at BEGV since then
-	 we are potentially in a new paragraph that doesn't yet
-	 exist.  */
+	 paragraph might start.  */
       pos = bidi_it->charpos;
       s = (STRINGP (bidi_it->string.lstring)
 	   ? SDATA (bidi_it->string.lstring)
 	   : bidi_it->string.s);
-      if (bytepos > begbyte
-	  && bidi_char_at_pos (bytepos, s, bidi_it->string.unibyte) == '\n')
+      if (bidi_char_at_pos (bytepos, s, bidi_it->string.unibyte) == '\n')
 	{
 	  bytepos++;
 	  pos++;