Subject: bug#41506: 28.0.50; RTL problem

From: Pip Cet <[email-masked]>
Date: Sat, 06 Jun 2020 07:58:24 +0000

Eli Zaretskii <[email-masked]> writes:

>> Date: Tue, 02 Jun 2020 19:34:31 +0300
>> From: Eli Zaretskii <[email-masked]>
>> Cc: [email-masked]
>> 
>> So I guess whatever issues I had back then were meanwhile solved "by
>> other means", and you should install this patch.  If there is indeed
>> some subtlety here, it will present itself sooner or later (like, in
>> another 11 years).
>
> Btw, please note that some residual problem remains: after the patch,
> if a buffer begins with 2 newlines and an RTL letter, the first screen
> line is rendered right-to-left, which is wrong.  You can see that it's
> wrong if you insert more newlines at BOB: then only the single empty
> line immediately before the RTL letter is rendered right-to-left.
>
> Of course, this is better than the original problem.

I decided to investigate further, and finally came up with this patch,
which appears to work. I'm not a hundred percent sure it's the right
thing to do, because when we're called with bidi_it->first_elt = true,
it's possible we shouldn't touch bidi_it->new_paragraph at all...

>From b5302d2e89710166cc8540c8fc08a7eaabc341f4 Mon Sep 17 00:00:00 2001
From: Pip Cet <[email-masked]>
Date: Sat, 6 Jun 2020 07:52:13 +0000
Subject: [PATCH] Handle buffers containing two newlines followed by an RTL
 char

* src/bidi.c (bidi_paragraph_init): Correct handling of initial
newlines.  (Bug#41506)
---
 src/bidi.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/bidi.c b/src/bidi.c
index 1017bd2d52..8d2d3c1f07 100644
--- a/src/bidi.c
+++ b/src/bidi.c
@@ -1707,14 +1707,14 @@ 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.  But don't do that for the first
+	 element since this function will be called twice in that
+	 case.  */
       pos = bidi_it->charpos;
       s = (STRINGP (bidi_it->string.lstring)
 	   ? SDATA (bidi_it->string.lstring)
 	   : bidi_it->string.s);
-      if (bytepos > begbyte
+      if (!bidi_it->first_elt
 	  && bidi_char_at_pos (bytepos, s, bidi_it->string.unibyte) == '\n')
 	{
 	  bytepos++;
-- 
2.27.0.rc0

If you have any further comments, I'd be glad to amend the comments in
bidi.c to reflect what we actually do understand.