Remove kludgey save and restore of IT->position for push_it.
[bpt/emacs.git] / src / bidi.c
CommitLineData
b7b65b15 1/* Low-level bidirectional buffer-scanning functions for GNU Emacs.
73b0cd50 2 Copyright (C) 2000-2001, 2004-2005, 2009-2011
b118e65f 3 Free Software Foundation, Inc.
b7b65b15
EZ
4
5This file is part of GNU Emacs.
6
a8d11bd3 7GNU Emacs is free software: you can redistribute it and/or modify
b7b65b15 8it under the terms of the GNU General Public License as published by
a8d11bd3
EZ
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
b7b65b15
EZ
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
b7b65b15 17You should have received a copy of the GNU General Public License
a8d11bd3 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
b7b65b15 19
2d6e4628
EZ
20/* Written by Eli Zaretskii <eliz@gnu.org>.
21
22 A sequential implementation of the Unicode Bidirectional algorithm,
b7b65b15
EZ
23 as per UAX#9, a part of the Unicode Standard.
24
25 Unlike the reference and most other implementations, this one is
940afb59
EZ
26 designed to be called once for every character in the buffer or
27 string.
b7b65b15 28
4b292a22 29 The main entry point is bidi_move_to_visually_next. Each time it
b7b65b15
EZ
30 is called, it finds the next character in the visual order, and
31 returns its information in a special structure. The caller is then
32 expected to process this character for display or any other
4b292a22
EZ
33 purposes, and call bidi_move_to_visually_next for the next
34 character. See the comments in bidi_move_to_visually_next for more
35 details about its algorithm that finds the next visual-order
b7b65b15
EZ
36 character by resolving their levels on the fly.
37
940afb59
EZ
38 The two other entry points are bidi_paragraph_init and
39 bidi_mirror_char. The first determines the base direction of a
40 paragraph, while the second returns the mirrored version of its
41 argument character.
42
89d3374a
EZ
43 If you want to understand the code, you will have to read it
44 together with the relevant portions of UAX#9. The comments include
45 references to UAX#9 rules, for that very reason.
46
b7b65b15
EZ
47 A note about references to UAX#9 rules: if the reference says
48 something like "X9/Retaining", it means that you need to refer to
49 rule X9 and to its modifications decribed in the "Implementation
50 Notes" section of UAX#9, under "Retaining Format Codes". */
51
b7b65b15 52#include <config.h>
b7b65b15 53#include <stdio.h>
29e3d8d1
EZ
54#include <setjmp.h>
55
b7b65b15
EZ
56#include "lisp.h"
57#include "buffer.h"
58#include "character.h"
59#include "dispextern.h"
60
61static int bidi_initialized = 0;
62
cbc4fd20 63static Lisp_Object bidi_type_table, bidi_mirror_table;
b7b65b15
EZ
64
65#define LRM_CHAR 0x200E
66#define RLM_CHAR 0x200F
b7b65b15 67#define BIDI_EOB -1
b7b65b15 68
b7b65b15
EZ
69/* Local data structures. (Look in dispextern.h for the rest.) */
70
71/* What we need to know about the current paragraph. */
72struct bidi_paragraph_info {
61bfec98
EZ
73 EMACS_INT start_bytepos; /* byte position where it begins */
74 EMACS_INT end_bytepos; /* byte position where it ends */
75 int embedding_level; /* its basic embedding level */
76 bidi_dir_t base_dir; /* its base direction */
b7b65b15
EZ
77};
78
79/* Data type for describing the bidirectional character categories. */
80typedef enum {
81 UNKNOWN_BC,
82 NEUTRAL,
83 WEAK,
84 STRONG
85} bidi_category_t;
86
e78aecca 87extern int bidi_ignore_explicit_marks_for_paragraph_level EXTERNALLY_VISIBLE;
b7b65b15
EZ
88int bidi_ignore_explicit_marks_for_paragraph_level = 1;
89
372b7a95 90static Lisp_Object paragraph_start_re, paragraph_separate_re;
6bff6497 91static Lisp_Object Qparagraph_start, Qparagraph_separate;
b7b65b15 92
b7b65b15 93static void
971de7fb 94bidi_initialize (void)
b7b65b15 95{
317fbf33
EZ
96
97#include "biditype.h"
cbc4fd20 98#include "bidimirror.h"
317fbf33 99
b7b65b15
EZ
100 int i;
101
102 bidi_type_table = Fmake_char_table (Qnil, make_number (STRONG_L));
2d6e4628 103 staticpro (&bidi_type_table);
b7b65b15
EZ
104
105 for (i = 0; i < sizeof bidi_type / sizeof bidi_type[0]; i++)
317fbf33 106 char_table_set_range (bidi_type_table, bidi_type[i].from, bidi_type[i].to,
b7b65b15 107 make_number (bidi_type[i].type));
6bff6497 108
cbc4fd20
EZ
109 bidi_mirror_table = Fmake_char_table (Qnil, Qnil);
110 staticpro (&bidi_mirror_table);
111
112 for (i = 0; i < sizeof bidi_mirror / sizeof bidi_mirror[0]; i++)
113 char_table_set (bidi_mirror_table, bidi_mirror[i].from,
114 make_number (bidi_mirror[i].to));
115
b4bf28b7
SM
116 Qparagraph_start = intern ("paragraph-start");
117 staticpro (&Qparagraph_start);
372b7a95
EZ
118 paragraph_start_re = Fsymbol_value (Qparagraph_start);
119 if (!STRINGP (paragraph_start_re))
120 paragraph_start_re = build_string ("\f\\|[ \t]*$");
121 staticpro (&paragraph_start_re);
b4bf28b7
SM
122 Qparagraph_separate = intern ("paragraph-separate");
123 staticpro (&Qparagraph_separate);
372b7a95
EZ
124 paragraph_separate_re = Fsymbol_value (Qparagraph_separate);
125 if (!STRINGP (paragraph_separate_re))
126 paragraph_separate_re = build_string ("[ \t\f]*$");
127 staticpro (&paragraph_separate_re);
b7b65b15
EZ
128 bidi_initialized = 1;
129}
130
6bff6497
EZ
131/* Return the bidi type of a character CH, subject to the current
132 directional OVERRIDE. */
fd3998ff 133static INLINE bidi_type_t
6bff6497 134bidi_get_type (int ch, bidi_dir_t override)
b7b65b15 135{
6bff6497
EZ
136 bidi_type_t default_type;
137
e7402cb2
EZ
138 if (ch == BIDI_EOB)
139 return NEUTRAL_B;
e342a24d
EZ
140 if (ch < 0 || ch > MAX_CHAR)
141 abort ();
6bff6497
EZ
142
143 default_type = (bidi_type_t) XINT (CHAR_TABLE_REF (bidi_type_table, ch));
144
145 if (override == NEUTRAL_DIR)
146 return default_type;
147
148 switch (default_type)
149 {
150 /* Although UAX#9 does not tell, it doesn't make sense to
151 override NEUTRAL_B and LRM/RLM characters. */
152 case NEUTRAL_B:
153 case LRE:
154 case LRO:
155 case RLE:
156 case RLO:
157 case PDF:
158 return default_type;
159 default:
160 switch (ch)
161 {
162 case LRM_CHAR:
163 case RLM_CHAR:
164 return default_type;
165 default:
166 if (override == L2R) /* X6 */
167 return STRONG_L;
168 else if (override == R2L)
169 return STRONG_R;
170 else
171 abort (); /* can't happen: handled above */
172 }
173 }
b7b65b15
EZ
174}
175
7d3b3862 176static void
2d6e4628
EZ
177bidi_check_type (bidi_type_t type)
178{
179 if (type < UNKNOWN_BT || type > NEUTRAL_ON)
180 abort ();
181}
182
b7b65b15 183/* Given a bidi TYPE of a character, return its category. */
fd3998ff 184static INLINE bidi_category_t
b7b65b15
EZ
185bidi_get_category (bidi_type_t type)
186{
187 switch (type)
188 {
189 case UNKNOWN_BT:
190 return UNKNOWN_BC;
191 case STRONG_L:
192 case STRONG_R:
193 case STRONG_AL:
194 case LRE:
195 case LRO:
196 case RLE:
197 case RLO:
198 return STRONG;
199 case PDF: /* ??? really?? */
200 case WEAK_EN:
201 case WEAK_ES:
202 case WEAK_ET:
203 case WEAK_AN:
204 case WEAK_CS:
205 case WEAK_NSM:
206 case WEAK_BN:
207 return WEAK;
208 case NEUTRAL_B:
209 case NEUTRAL_S:
210 case NEUTRAL_WS:
211 case NEUTRAL_ON:
212 return NEUTRAL;
213 default:
214 abort ();
215 }
216}
217
a6e8d97c
EZ
218/* Return the mirrored character of C, if it has one. If C has no
219 mirrored counterpart, return C.
220 Note: The conditions in UAX#9 clause L4 regarding the surrounding
221 context must be tested by the caller. */
b7b65b15
EZ
222int
223bidi_mirror_char (int c)
224{
cbc4fd20
EZ
225 Lisp_Object val;
226
227 if (c == BIDI_EOB)
228 return c;
229 if (c < 0 || c > MAX_CHAR)
230 abort ();
b7b65b15 231
cbc4fd20
EZ
232 val = CHAR_TABLE_REF (bidi_mirror_table, c);
233 if (INTEGERP (val))
b7b65b15 234 {
cbc4fd20 235 int v = XINT (val);
b7b65b15 236
cbc4fd20
EZ
237 if (v < 0 || v > MAX_CHAR)
238 abort ();
239
240 return v;
b7b65b15 241 }
cbc4fd20 242
b7b65b15
EZ
243 return c;
244}
245
246/* Copy the bidi iterator from FROM to TO. To save cycles, this only
247 copies the part of the level stack that is actually in use. */
fd3998ff 248static INLINE void
b7b65b15
EZ
249bidi_copy_it (struct bidi_it *to, struct bidi_it *from)
250{
251 int i;
252
e69a9370 253 /* Copy everything except the level stack and beyond. */
182ce2d2 254 memcpy (to, from, offsetof (struct bidi_it, level_stack[0]));
b7b65b15
EZ
255
256 /* Copy the active part of the level stack. */
257 to->level_stack[0] = from->level_stack[0]; /* level zero is always in use */
258 for (i = 1; i <= from->stack_idx; i++)
259 to->level_stack[i] = from->level_stack[i];
260}
261
262/* Caching the bidi iterator states. */
263
2fe72643
EZ
264#define BIDI_CACHE_CHUNK 200
265static struct bidi_it *bidi_cache;
266static size_t bidi_cache_size = 0;
0416466c 267static size_t elsz = sizeof (struct bidi_it);
2fe72643
EZ
268static int bidi_cache_idx; /* next unused cache slot */
269static int bidi_cache_last_idx; /* slot of last cache hit */
b7b65b15 270
fd3998ff 271static INLINE void
b7b65b15
EZ
272bidi_cache_reset (void)
273{
274 bidi_cache_idx = 0;
275 bidi_cache_last_idx = -1;
276}
277
2fe72643
EZ
278static INLINE void
279bidi_cache_shrink (void)
280{
281 if (bidi_cache_size > BIDI_CACHE_CHUNK)
282 {
0416466c
EZ
283 bidi_cache_size = BIDI_CACHE_CHUNK;
284 bidi_cache =
285 (struct bidi_it *) xrealloc (bidi_cache, bidi_cache_size * elsz);
2fe72643
EZ
286 }
287 bidi_cache_reset ();
288}
289
fd3998ff 290static INLINE void
b7b65b15
EZ
291bidi_cache_fetch_state (int idx, struct bidi_it *bidi_it)
292{
293 int current_scan_dir = bidi_it->scan_dir;
294
295 if (idx < 0 || idx >= bidi_cache_idx)
296 abort ();
297
298 bidi_copy_it (bidi_it, &bidi_cache[idx]);
299 bidi_it->scan_dir = current_scan_dir;
300 bidi_cache_last_idx = idx;
301}
302
303/* Find a cached state with a given CHARPOS and resolved embedding
304 level less or equal to LEVEL. if LEVEL is -1, disregard the
305 resolved levels in cached states. DIR, if non-zero, means search
306 in that direction from the last cache hit. */
fd3998ff 307static INLINE int
61bfec98 308bidi_cache_search (EMACS_INT charpos, int level, int dir)
b7b65b15
EZ
309{
310 int i, i_start;
311
312 if (bidi_cache_idx)
313 {
314 if (charpos < bidi_cache[bidi_cache_last_idx].charpos)
182ce2d2
EZ
315 {
316 dir = -1;
317 i_start = bidi_cache_last_idx - 1;
318 }
319 else if (charpos > (bidi_cache[bidi_cache_last_idx].charpos
320 + bidi_cache[bidi_cache_last_idx].nchars - 1))
321 {
322 dir = 1;
323 i_start = bidi_cache_last_idx + 1;
324 }
325 else if (dir)
b7b65b15
EZ
326 i_start = bidi_cache_last_idx;
327 else
328 {
329 dir = -1;
330 i_start = bidi_cache_idx - 1;
331 }
332
333 if (dir < 0)
334 {
335 /* Linear search for now; FIXME! */
336 for (i = i_start; i >= 0; i--)
182ce2d2
EZ
337 if (bidi_cache[i].charpos <= charpos
338 && charpos < bidi_cache[i].charpos + bidi_cache[i].nchars
b7b65b15
EZ
339 && (level == -1 || bidi_cache[i].resolved_level <= level))
340 return i;
341 }
342 else
343 {
344 for (i = i_start; i < bidi_cache_idx; i++)
182ce2d2
EZ
345 if (bidi_cache[i].charpos <= charpos
346 && charpos < bidi_cache[i].charpos + bidi_cache[i].nchars
b7b65b15
EZ
347 && (level == -1 || bidi_cache[i].resolved_level <= level))
348 return i;
349 }
350 }
351
352 return -1;
353}
354
355/* Find a cached state where the resolved level changes to a value
356 that is lower than LEVEL, and return its cache slot index. DIR is
357 the direction to search, starting with the last used cache slot.
358 BEFORE, if non-zero, means return the index of the slot that is
359 ``before'' the level change in the search direction. That is,
360 given the cached levels like this:
361
362 1122333442211
363 AB C
364
365 and assuming we are at the position cached at the slot marked with
366 C, searching backwards (DIR = -1) for LEVEL = 2 will return the
367 index of slot B or A, depending whether BEFORE is, respectively,
368 non-zero or zero. */
369static int
370bidi_cache_find_level_change (int level, int dir, int before)
371{
372 if (bidi_cache_idx)
373 {
374 int i = dir ? bidi_cache_last_idx : bidi_cache_idx - 1;
375 int incr = before ? 1 : 0;
376
377 if (!dir)
378 dir = -1;
379 else if (!incr)
380 i += dir;
381
382 if (dir < 0)
383 {
384 while (i >= incr)
385 {
386 if (bidi_cache[i - incr].resolved_level >= 0
387 && bidi_cache[i - incr].resolved_level < level)
388 return i;
389 i--;
390 }
391 }
392 else
393 {
394 while (i < bidi_cache_idx - incr)
395 {
396 if (bidi_cache[i + incr].resolved_level >= 0
397 && bidi_cache[i + incr].resolved_level < level)
398 return i;
399 i++;
400 }
401 }
402 }
403
404 return -1;
405}
406
fd3998ff 407static INLINE void
b7b65b15
EZ
408bidi_cache_iterator_state (struct bidi_it *bidi_it, int resolved)
409{
410 int idx;
411
412 /* We should never cache on backward scans. */
413 if (bidi_it->scan_dir == -1)
414 abort ();
415 idx = bidi_cache_search (bidi_it->charpos, -1, 1);
416
417 if (idx < 0)
418 {
419 idx = bidi_cache_idx;
2fe72643
EZ
420 /* Enlarge the cache as needed. */
421 if (idx >= bidi_cache_size)
422 {
0416466c 423 bidi_cache_size += BIDI_CACHE_CHUNK;
2fe72643 424 bidi_cache =
0416466c 425 (struct bidi_it *) xrealloc (bidi_cache, bidi_cache_size * elsz);
2fe72643 426 }
bd924a5d
EZ
427 /* Character positions should correspond to cache positions 1:1.
428 If we are outside the range of cached positions, the cache is
429 useless and must be reset. */
430 if (idx > 0 &&
182ce2d2
EZ
431 (bidi_it->charpos > (bidi_cache[idx - 1].charpos
432 + bidi_cache[idx - 1].nchars)
bd924a5d
EZ
433 || bidi_it->charpos < bidi_cache[0].charpos))
434 {
435 bidi_cache_reset ();
436 idx = 0;
437 }
102ebb00
EZ
438 if (bidi_it->nchars <= 0)
439 abort ();
b7b65b15
EZ
440 bidi_copy_it (&bidi_cache[idx], bidi_it);
441 if (!resolved)
442 bidi_cache[idx].resolved_level = -1;
443 }
444 else
445 {
446 /* Copy only the members which could have changed, to avoid
447 costly copying of the entire struct. */
448 bidi_cache[idx].type = bidi_it->type;
2d6e4628 449 bidi_check_type (bidi_it->type);
89d3374a
EZ
450 bidi_cache[idx].type_after_w1 = bidi_it->type_after_w1;
451 bidi_check_type (bidi_it->type_after_w1);
b7b65b15
EZ
452 if (resolved)
453 bidi_cache[idx].resolved_level = bidi_it->resolved_level;
454 else
455 bidi_cache[idx].resolved_level = -1;
456 bidi_cache[idx].invalid_levels = bidi_it->invalid_levels;
457 bidi_cache[idx].invalid_rl_levels = bidi_it->invalid_rl_levels;
458 bidi_cache[idx].next_for_neutral = bidi_it->next_for_neutral;
459 bidi_cache[idx].next_for_ws = bidi_it->next_for_ws;
460 bidi_cache[idx].ignore_bn_limit = bidi_it->ignore_bn_limit;
461 }
462
463 bidi_cache_last_idx = idx;
464 if (idx >= bidi_cache_idx)
465 bidi_cache_idx = idx + 1;
466}
467
fd3998ff 468static INLINE bidi_type_t
61bfec98 469bidi_cache_find (EMACS_INT charpos, int level, struct bidi_it *bidi_it)
b7b65b15
EZ
470{
471 int i = bidi_cache_search (charpos, level, bidi_it->scan_dir);
472
473 if (i >= 0)
474 {
475 bidi_dir_t current_scan_dir = bidi_it->scan_dir;
476
5009f85e 477 bidi_copy_it (bidi_it, &bidi_cache[i]);
b7b65b15
EZ
478 bidi_cache_last_idx = i;
479 /* Don't let scan direction from from the cached state override
480 the current scan direction. */
481 bidi_it->scan_dir = current_scan_dir;
482 return bidi_it->type;
483 }
484
485 return UNKNOWN_BT;
486}
487
fd3998ff 488static INLINE int
b7b65b15
EZ
489bidi_peek_at_next_level (struct bidi_it *bidi_it)
490{
491 if (bidi_cache_idx == 0 || bidi_cache_last_idx == -1)
492 abort ();
493 return bidi_cache[bidi_cache_last_idx + bidi_it->scan_dir].resolved_level;
494}
495
be39f003
EZ
496/* Check if buffer position CHARPOS/BYTEPOS is the end of a paragraph.
497 Value is the non-negative length of the paragraph separator
498 following the buffer position, -1 if position is at the beginning
499 of a new paragraph, or -2 if position is neither at beginning nor
500 at end of a paragraph. */
fd3998ff 501static EMACS_INT
6bff6497 502bidi_at_paragraph_end (EMACS_INT charpos, EMACS_INT bytepos)
b7b65b15 503{
372b7a95
EZ
504 Lisp_Object sep_re;
505 Lisp_Object start_re;
be39f003
EZ
506 EMACS_INT val;
507
372b7a95
EZ
508 sep_re = paragraph_separate_re;
509 start_re = paragraph_start_re;
be39f003
EZ
510
511 val = fast_looking_at (sep_re, charpos, bytepos, ZV, ZV_BYTE, Qnil);
512 if (val < 0)
513 {
514 if (fast_looking_at (start_re, charpos, bytepos, ZV, ZV_BYTE, Qnil) >= 0)
515 val = -1;
516 else
517 val = -2;
518 }
b7b65b15 519
be39f003 520 return val;
b7b65b15
EZ
521}
522
523/* Determine the start-of-run (sor) directional type given the two
524 embedding levels on either side of the run boundary. Also, update
525 the saved info about previously seen characters, since that info is
526 generally valid for a single level run. */
fd3998ff 527static INLINE void
b7b65b15
EZ
528bidi_set_sor_type (struct bidi_it *bidi_it, int level_before, int level_after)
529{
530 int higher_level = level_before > level_after ? level_before : level_after;
531
532 /* The prev_was_pdf gork is required for when we have several PDFs
533 in a row. In that case, we want to compute the sor type for the
534 next level run only once: when we see the first PDF. That's
535 because the sor type depends only on the higher of the two levels
536 that we find on the two sides of the level boundary (see UAX#9,
537 clause X10), and so we don't need to know the final embedding
538 level to which we descend after processing all the PDFs. */
e342a24d 539 if (!bidi_it->prev_was_pdf || level_before < level_after)
b7b65b15
EZ
540 /* FIXME: should the default sor direction be user selectable? */
541 bidi_it->sor = (higher_level & 1) != 0 ? R2L : L2R;
542 if (level_before > level_after)
543 bidi_it->prev_was_pdf = 1;
544
545 bidi_it->prev.type = UNKNOWN_BT;
89d3374a
EZ
546 bidi_it->last_strong.type = bidi_it->last_strong.type_after_w1 =
547 bidi_it->last_strong.orig_type = UNKNOWN_BT;
b7b65b15
EZ
548 bidi_it->prev_for_neutral.type = bidi_it->sor == R2L ? STRONG_R : STRONG_L;
549 bidi_it->prev_for_neutral.charpos = bidi_it->charpos;
550 bidi_it->prev_for_neutral.bytepos = bidi_it->bytepos;
89d3374a
EZ
551 bidi_it->next_for_neutral.type = bidi_it->next_for_neutral.type_after_w1 =
552 bidi_it->next_for_neutral.orig_type = UNKNOWN_BT;
b7b65b15
EZ
553 bidi_it->ignore_bn_limit = 0; /* meaning it's unknown */
554}
555
182ce2d2 556/* Perform initializations for reordering a new line of bidi text. */
6bff6497 557static void
be39f003
EZ
558bidi_line_init (struct bidi_it *bidi_it)
559{
560 bidi_it->scan_dir = 1; /* FIXME: do we need to have control on this? */
561 bidi_it->resolved_level = bidi_it->level_stack[0].level;
562 bidi_it->level_stack[0].override = NEUTRAL_DIR; /* X1 */
563 bidi_it->invalid_levels = 0;
564 bidi_it->invalid_rl_levels = -1;
565 bidi_it->next_en_pos = -1;
566 bidi_it->next_for_ws.type = UNKNOWN_BT;
b44d9321
EZ
567 bidi_set_sor_type (bidi_it,
568 bidi_it->paragraph_dir == R2L ? 1 : 0,
be39f003
EZ
569 bidi_it->level_stack[0].level); /* X10 */
570
571 bidi_cache_reset ();
572}
573
102ebb00
EZ
574/* Fetch and return the character at BYTEPOS/CHARPOS. If that
575 character is covered by a display string, treat the entire run of
576 covered characters as a single character u+FFFC, and return their
577 combined length in CH_LEN and NCHARS. DISP_POS specifies the
578 character position of the next display string, or -1 if not yet
579 computed. When the next character is at or beyond that position,
580 the function updates DISP_POS with the position of the next display
581 string. */
182ce2d2 582static INLINE int
102ebb00 583bidi_fetch_char (EMACS_INT bytepos, EMACS_INT charpos, EMACS_INT *disp_pos,
182ce2d2
EZ
584 EMACS_INT *ch_len, EMACS_INT *nchars)
585{
586 int ch;
587
588 /* FIXME: Support strings in addition to buffers. */
589 /* If we got past the last known position of display string, compute
590 the position of the next one. That position could be at BYTEPOS. */
102ebb00
EZ
591 if (charpos < ZV && charpos > *disp_pos)
592 *disp_pos = compute_display_string_pos (charpos);
593
594 /* Fetch the character at BYTEPOS. */
182ce2d2
EZ
595 if (bytepos >= ZV_BYTE)
596 {
597 ch = BIDI_EOB;
598 *ch_len = 1;
599 *nchars = 1;
7b600102 600 *disp_pos = ZV;
182ce2d2 601 }
102ebb00 602 else if (charpos >= *disp_pos)
182ce2d2 603 {
7b600102
EZ
604 EMACS_INT disp_end_pos;
605
606 /* We don't expect to find ourselves in the middle of a display
607 property. Hopefully, it will never be needed. */
608 if (charpos > *disp_pos)
609 abort ();
610 /* Return the Unicode Object Replacement Character to represent
611 the entire run of characters covered by the display
612 string. */
613 ch = 0xFFFC;
614 disp_end_pos = compute_display_string_end (*disp_pos);
615 *nchars = disp_end_pos - *disp_pos;
616 *ch_len = CHAR_TO_BYTE (disp_end_pos) - bytepos;
182ce2d2 617 }
182ce2d2
EZ
618 else
619 {
620 ch = FETCH_MULTIBYTE_CHAR (bytepos);
182ce2d2 621 *nchars = 1;
7b600102 622 *ch_len = CHAR_BYTES (ch);
182ce2d2
EZ
623 }
624
625 /* If we just entered a run of characters covered by a display
626 string, compute the position of the next display string. */
102ebb00
EZ
627 if (charpos + *nchars <= ZV && charpos + *nchars > *disp_pos)
628 *disp_pos = compute_display_string_pos (charpos + *nchars);
182ce2d2
EZ
629
630 return ch;
631}
632
633/* Looks like we won't need this one. */
634#if 0
635/* Fetch character at CHARPOS/BYTEPOS. Return the character, and
636 advance CHARPOS and BYTEPOS to the next character in logical
637 order. */
638static INLINE int
639bidi_fetch_char_advance (EMACS_INT *charpos, EMACS_INT *bytepos)
640{
641 int ch;
642
643 /* FIXME: Support strings in addition to buffers. */
644 FETCH_CHAR_ADVANCE_NO_CHECK (ch, charpos, bytepos);
645
646#if 0
647 if (...)
648 {
649 /* FIXME: Support characters covered by display strings. */
650 ch = 0xFFFC;
651 }
652#endif
653
654 return ch;
655}
656#endif
657
be39f003
EZ
658/* Find the beginning of this paragraph by looking back in the buffer.
659 Value is the byte position of the paragraph's beginning. */
660static EMACS_INT
b44d9321 661bidi_find_paragraph_start (EMACS_INT pos, EMACS_INT pos_byte)
6bff6497 662{
372b7a95 663 Lisp_Object re = paragraph_start_re;
6bff6497
EZ
664 EMACS_INT limit = ZV, limit_byte = ZV_BYTE;
665
6bff6497
EZ
666 while (pos_byte > BEGV_BYTE
667 && fast_looking_at (re, pos, pos_byte, limit, limit_byte, Qnil) < 0)
668 {
182ce2d2
EZ
669 /* FIXME: What if the paragraph beginning is covered by a
670 display string? And what if a display string covering some
671 of the text over which we scan back includes
672 paragraph_start_re? */
be39f003
EZ
673 pos = find_next_newline_no_quit (pos - 1, -1);
674 pos_byte = CHAR_TO_BYTE (pos);
6bff6497 675 }
be39f003 676 return pos_byte;
6bff6497
EZ
677}
678
bea4f10c 679/* Determine the base direction, a.k.a. base embedding level, of the
b44d9321
EZ
680 paragraph we are about to iterate through. If DIR is either L2R or
681 R2L, just use that. Otherwise, determine the paragraph direction
bea4f10c
EZ
682 from the first strong directional character of the paragraph.
683
182ce2d2 684 NO_DEFAULT_P non-zero means don't default to L2R if the paragraph
bea4f10c
EZ
685 has no strong directional characters and both DIR and
686 bidi_it->paragraph_dir are NEUTRAL_DIR. In that case, search back
687 in the buffer until a paragraph is found with a strong character,
688 or until hitting BEGV. In the latter case, fall back to L2R. This
689 flag is used in current-bidi-paragraph-direction.
690
691 Note that this function gives the paragraph separator the same
692 direction as the preceding paragraph, even though Emacs generally
693 views the separartor as not belonging to any paragraph. */
b7b65b15 694void
bea4f10c 695bidi_paragraph_init (bidi_dir_t dir, struct bidi_it *bidi_it, int no_default_p)
b7b65b15 696{
6bff6497 697 EMACS_INT bytepos = bidi_it->bytepos;
bea4f10c 698 EMACS_INT pstartbyte;
e7402cb2 699
5e65aec0
EZ
700 /* Special case for an empty buffer. */
701 if (bytepos == BEGV_BYTE && bytepos == ZV_BYTE)
702 dir = L2R;
9c82e145 703 /* We should never be called at EOB or before BEGV. */
5e65aec0 704 else if (bytepos >= ZV_BYTE || bytepos < BEGV_BYTE)
9c82e145
EZ
705 abort ();
706
be39f003
EZ
707 if (dir == L2R)
708 {
709 bidi_it->paragraph_dir = L2R;
710 bidi_it->new_paragraph = 0;
711 }
712 else if (dir == R2L)
713 {
714 bidi_it->paragraph_dir = R2L;
715 bidi_it->new_paragraph = 0;
716 }
b7b65b15
EZ
717 else if (dir == NEUTRAL_DIR) /* P2 */
718 {
182ce2d2
EZ
719 int ch;
720 EMACS_INT ch_len, nchars;
721 EMACS_INT pos, disp_pos = -1;
6bff6497 722 bidi_type_t type;
be39f003 723
d20e1419
EZ
724 if (!bidi_initialized)
725 bidi_initialize ();
726
be39f003
EZ
727 /* If we are inside a paragraph separator, we are just waiting
728 for the separator to be exhausted; use the previous paragraph
e5a2fec7
EZ
729 direction. But don't do that if we have been just reseated,
730 because we need to reinitialize below in that case. */
731 if (!bidi_it->first_elt
732 && bidi_it->charpos < bidi_it->separator_limit)
be39f003
EZ
733 return;
734
b44d9321 735 /* If we are on a newline, get past it to where the next
5e65aec0
EZ
736 paragraph might start. But don't do that at BEGV since then
737 we are potentially in a new paragraph that doesn't yet
738 exist. */
c143c213 739 pos = bidi_it->charpos;
5e65aec0 740 if (bytepos > BEGV_BYTE && FETCH_CHAR (bytepos) == '\n')
be39f003 741 {
b44d9321 742 bytepos++;
c143c213 743 pos++;
be39f003 744 }
b44d9321
EZ
745
746 /* We are either at the beginning of a paragraph or in the
747 middle of it. Find where this paragraph starts. */
bea4f10c 748 pstartbyte = bidi_find_paragraph_start (pos, bytepos);
be39f003
EZ
749 bidi_it->separator_limit = -1;
750 bidi_it->new_paragraph = 0;
bea4f10c
EZ
751
752 /* The following loop is run more than once only if NO_DEFAULT_P
753 is non-zero. */
754 do {
755 bytepos = pstartbyte;
bea4f10c 756 pos = BYTE_TO_CHAR (bytepos);
102ebb00 757 ch = bidi_fetch_char (bytepos, pos, &disp_pos, &ch_len, &nchars);
bea4f10c
EZ
758 type = bidi_get_type (ch, NEUTRAL_DIR);
759
182ce2d2 760 for (pos += nchars, bytepos += ch_len;
bea4f10c
EZ
761 /* NOTE: UAX#9 says to search only for L, AL, or R types
762 of characters, and ignore RLE, RLO, LRE, and LRO.
763 However, I'm not sure it makes sense to omit those 4;
764 should try with and without that to see the effect. */
765 (bidi_get_category (type) != STRONG)
766 || (bidi_ignore_explicit_marks_for_paragraph_level
767 && (type == RLE || type == RLO
768 || type == LRE || type == LRO));
769 type = bidi_get_type (ch, NEUTRAL_DIR))
770 {
771 if (type == NEUTRAL_B && bidi_at_paragraph_end (pos, bytepos) >= -1)
21fce5ab 772 break;
bea4f10c
EZ
773 if (bytepos >= ZV_BYTE)
774 {
775 /* Pretend there's a paragraph separator at end of
776 buffer. */
777 type = NEUTRAL_B;
778 break;
779 }
102ebb00
EZ
780 /* Fetch next character and advance to get past it. */
781 ch = bidi_fetch_char (bytepos, pos, &disp_pos, &ch_len, &nchars);
182ce2d2
EZ
782 pos += nchars;
783 bytepos += ch_len;
bea4f10c
EZ
784 }
785 if (type == STRONG_R || type == STRONG_AL) /* P3 */
786 bidi_it->paragraph_dir = R2L;
787 else if (type == STRONG_L)
788 bidi_it->paragraph_dir = L2R;
789 if (no_default_p && bidi_it->paragraph_dir == NEUTRAL_DIR)
790 {
791 /* If this paragraph is at BEGV, default to L2R. */
792 if (pstartbyte == BEGV_BYTE)
793 bidi_it->paragraph_dir = L2R; /* P3 and HL1 */
794 else
795 {
796 EMACS_INT prevpbyte = pstartbyte;
797 EMACS_INT p = BYTE_TO_CHAR (pstartbyte), pbyte = pstartbyte;
798
799 /* Find the beginning of the previous paragraph, if any. */
800 while (pbyte > BEGV_BYTE && prevpbyte >= pstartbyte)
801 {
182ce2d2
EZ
802 /* FXIME: What if p is covered by a display
803 string? See also a FIXME inside
804 bidi_find_paragraph_start. */
bea4f10c
EZ
805 p--;
806 pbyte = CHAR_TO_BYTE (p);
807 prevpbyte = bidi_find_paragraph_start (p, pbyte);
808 }
809 pstartbyte = prevpbyte;
810 }
811 }
812 } while (no_default_p && bidi_it->paragraph_dir == NEUTRAL_DIR);
b7b65b15 813 }
be39f003
EZ
814 else
815 abort ();
b7b65b15 816
b44d9321
EZ
817 /* Contrary to UAX#9 clause P3, we only default the paragraph
818 direction to L2R if we have no previous usable paragraph
bea4f10c 819 direction. This is allowed by the HL1 clause. */
d20e1419 820 if (bidi_it->paragraph_dir != L2R && bidi_it->paragraph_dir != R2L)
bea4f10c 821 bidi_it->paragraph_dir = L2R; /* P3 and HL1 ``higher-level protocols'' */
be39f003 822 if (bidi_it->paragraph_dir == R2L)
b44d9321 823 bidi_it->level_stack[0].level = 1;
be39f003 824 else
b44d9321 825 bidi_it->level_stack[0].level = 0;
be39f003
EZ
826
827 bidi_line_init (bidi_it);
b7b65b15
EZ
828}
829
6bff6497
EZ
830/* Do whatever UAX#9 clause X8 says should be done at paragraph's
831 end. */
fd3998ff 832static INLINE void
b7b65b15
EZ
833bidi_set_paragraph_end (struct bidi_it *bidi_it)
834{
835 bidi_it->invalid_levels = 0;
836 bidi_it->invalid_rl_levels = -1;
837 bidi_it->stack_idx = 0;
838 bidi_it->resolved_level = bidi_it->level_stack[0].level;
b7b65b15
EZ
839}
840
182ce2d2 841/* Initialize the bidi iterator from buffer/string position CHARPOS. */
b7b65b15 842void
6bff6497 843bidi_init_it (EMACS_INT charpos, EMACS_INT bytepos, struct bidi_it *bidi_it)
b7b65b15
EZ
844{
845 if (! bidi_initialized)
846 bidi_initialize ();
89d3374a
EZ
847 bidi_it->charpos = charpos;
848 bidi_it->bytepos = bytepos;
182ce2d2 849 bidi_it->nchars = -1; /* to be computed in bidi_resolve_explicit_1 */
6bff6497
EZ
850 bidi_it->first_elt = 1;
851 bidi_set_paragraph_end (bidi_it);
852 bidi_it->new_paragraph = 1;
be39f003 853 bidi_it->separator_limit = -1;
b7b65b15 854 bidi_it->type = NEUTRAL_B;
ebb5722e
EZ
855 bidi_it->type_after_w1 = NEUTRAL_B;
856 bidi_it->orig_type = NEUTRAL_B;
b7b65b15 857 bidi_it->prev_was_pdf = 0;
ebb5722e
EZ
858 bidi_it->prev.type = bidi_it->prev.type_after_w1 =
859 bidi_it->prev.orig_type = UNKNOWN_BT;
89d3374a
EZ
860 bidi_it->last_strong.type = bidi_it->last_strong.type_after_w1 =
861 bidi_it->last_strong.orig_type = UNKNOWN_BT;
b7b65b15
EZ
862 bidi_it->next_for_neutral.charpos = -1;
863 bidi_it->next_for_neutral.type =
89d3374a
EZ
864 bidi_it->next_for_neutral.type_after_w1 =
865 bidi_it->next_for_neutral.orig_type = UNKNOWN_BT;
b7b65b15
EZ
866 bidi_it->prev_for_neutral.charpos = -1;
867 bidi_it->prev_for_neutral.type =
89d3374a
EZ
868 bidi_it->prev_for_neutral.type_after_w1 =
869 bidi_it->prev_for_neutral.orig_type = UNKNOWN_BT;
b7b65b15 870 bidi_it->sor = L2R; /* FIXME: should it be user-selectable? */
182ce2d2 871 bidi_it->disp_pos = -1; /* invalid/unknown */
2fe72643 872 bidi_cache_shrink ();
b7b65b15
EZ
873}
874
875/* Push the current embedding level and override status; reset the
876 current level to LEVEL and the current override status to OVERRIDE. */
fd3998ff 877static INLINE void
b7b65b15
EZ
878bidi_push_embedding_level (struct bidi_it *bidi_it,
879 int level, bidi_dir_t override)
880{
881 bidi_it->stack_idx++;
882 if (bidi_it->stack_idx >= BIDI_MAXLEVEL)
883 abort ();
884 bidi_it->level_stack[bidi_it->stack_idx].level = level;
885 bidi_it->level_stack[bidi_it->stack_idx].override = override;
886}
887
888/* Pop the embedding level and directional override status from the
889 stack, and return the new level. */
fd3998ff 890static INLINE int
b7b65b15
EZ
891bidi_pop_embedding_level (struct bidi_it *bidi_it)
892{
893 /* UAX#9 says to ignore invalid PDFs. */
894 if (bidi_it->stack_idx > 0)
895 bidi_it->stack_idx--;
896 return bidi_it->level_stack[bidi_it->stack_idx].level;
897}
898
899/* Record in SAVED_INFO the information about the current character. */
fd3998ff 900static INLINE void
b7b65b15
EZ
901bidi_remember_char (struct bidi_saved_info *saved_info,
902 struct bidi_it *bidi_it)
903{
904 saved_info->charpos = bidi_it->charpos;
905 saved_info->bytepos = bidi_it->bytepos;
906 saved_info->type = bidi_it->type;
2d6e4628 907 bidi_check_type (bidi_it->type);
89d3374a
EZ
908 saved_info->type_after_w1 = bidi_it->type_after_w1;
909 bidi_check_type (bidi_it->type_after_w1);
b7b65b15 910 saved_info->orig_type = bidi_it->orig_type;
2d6e4628 911 bidi_check_type (bidi_it->orig_type);
b7b65b15
EZ
912}
913
914/* Resolve the type of a neutral character according to the type of
915 surrounding strong text and the current embedding level. */
fd3998ff 916static INLINE bidi_type_t
b7b65b15
EZ
917bidi_resolve_neutral_1 (bidi_type_t prev_type, bidi_type_t next_type, int lev)
918{
919 /* N1: European and Arabic numbers are treated as though they were R. */
920 if (next_type == WEAK_EN || next_type == WEAK_AN)
921 next_type = STRONG_R;
922 if (prev_type == WEAK_EN || prev_type == WEAK_AN)
923 prev_type = STRONG_R;
924
925 if (next_type == prev_type) /* N1 */
926 return next_type;
927 else if ((lev & 1) == 0) /* N2 */
928 return STRONG_L;
929 else
930 return STRONG_R;
931}
932
fd3998ff 933static INLINE int
182ce2d2 934bidi_explicit_dir_char (int ch)
b7b65b15 935{
182ce2d2
EZ
936 bidi_type_t ch_type;
937
938 if (!bidi_initialized)
939 abort ();
940 ch_type = (bidi_type_t) XINT (CHAR_TABLE_REF (bidi_type_table, ch));
941 return (ch_type == LRE || ch_type == LRO
942 || ch_type == RLE || ch_type == RLO
943 || ch_type == PDF);
b7b65b15
EZ
944}
945
946/* A helper function for bidi_resolve_explicit. It advances to the
947 next character in logical order and determines the new embedding
948 level and directional override, but does not take into account
949 empty embeddings. */
950static int
951bidi_resolve_explicit_1 (struct bidi_it *bidi_it)
952{
953 int curchar;
954 bidi_type_t type;
955 int current_level;
956 int new_level;
957 bidi_dir_t override;
958
182ce2d2
EZ
959 /* If reseat()'ed, don't advance, so as to start iteration from the
960 position where we were reseated. bidi_it->bytepos can be less
961 than BEGV_BYTE after reseat to BEGV. */
962 if (bidi_it->bytepos < BEGV_BYTE
9c82e145 963 || bidi_it->first_elt)
e7402cb2 964 {
9c82e145
EZ
965 bidi_it->first_elt = 0;
966 if (bidi_it->charpos < BEGV)
967 bidi_it->charpos = BEGV;
968 bidi_it->bytepos = CHAR_TO_BYTE (bidi_it->charpos);
e7402cb2 969 }
9c82e145 970 else if (bidi_it->bytepos < ZV_BYTE) /* don't move at ZV */
b7b65b15 971 {
182ce2d2
EZ
972 /* Advance to the next character, skipping characters covered by
973 display strings (nchars > 1). */
102ebb00
EZ
974 if (bidi_it->nchars <= 0)
975 abort ();
182ce2d2 976 bidi_it->charpos += bidi_it->nchars;
e7402cb2
EZ
977 if (bidi_it->ch_len == 0)
978 abort ();
b7b65b15
EZ
979 bidi_it->bytepos += bidi_it->ch_len;
980 }
981
982 current_level = bidi_it->level_stack[bidi_it->stack_idx].level; /* X1 */
983 override = bidi_it->level_stack[bidi_it->stack_idx].override;
984 new_level = current_level;
985
e7402cb2
EZ
986 if (bidi_it->bytepos >= ZV_BYTE)
987 {
988 curchar = BIDI_EOB;
989 bidi_it->ch_len = 1;
182ce2d2 990 bidi_it->nchars = 1;
7b600102 991 bidi_it->disp_pos = ZV;
e7402cb2
EZ
992 }
993 else
994 {
182ce2d2
EZ
995 /* Fetch the character at BYTEPOS. If it is covered by a
996 display string, treat the entire run of covered characters as
997 a single character u+FFFC. */
102ebb00
EZ
998 curchar = bidi_fetch_char (bidi_it->bytepos, bidi_it->charpos,
999 &bidi_it->disp_pos,
1000 &bidi_it->ch_len, &bidi_it->nchars);
e7402cb2 1001 }
b7b65b15 1002 bidi_it->ch = curchar;
b7b65b15 1003
6bff6497
EZ
1004 /* Don't apply directional override here, as all the types we handle
1005 below will not be affected by the override anyway, and we need
1006 the original type unaltered. The override will be applied in
1007 bidi_resolve_weak. */
1008 type = bidi_get_type (curchar, NEUTRAL_DIR);
89d3374a
EZ
1009 bidi_it->orig_type = type;
1010 bidi_check_type (bidi_it->orig_type);
b7b65b15
EZ
1011
1012 if (type != PDF)
1013 bidi_it->prev_was_pdf = 0;
1014
89d3374a 1015 bidi_it->type_after_w1 = UNKNOWN_BT;
b7b65b15
EZ
1016
1017 switch (type)
1018 {
1019 case RLE: /* X2 */
1020 case RLO: /* X4 */
89d3374a
EZ
1021 bidi_it->type_after_w1 = type;
1022 bidi_check_type (bidi_it->type_after_w1);
b7b65b15
EZ
1023 type = WEAK_BN; /* X9/Retaining */
1024 if (bidi_it->ignore_bn_limit <= 0)
1025 {
1026 if (current_level <= BIDI_MAXLEVEL - 4)
1027 {
1028 /* Compute the least odd embedding level greater than
1029 the current level. */
1030 new_level = ((current_level + 1) & ~1) + 1;
89d3374a 1031 if (bidi_it->type_after_w1 == RLE)
b7b65b15
EZ
1032 override = NEUTRAL_DIR;
1033 else
1034 override = R2L;
1035 if (current_level == BIDI_MAXLEVEL - 4)
1036 bidi_it->invalid_rl_levels = 0;
1037 bidi_push_embedding_level (bidi_it, new_level, override);
1038 }
1039 else
1040 {
1041 bidi_it->invalid_levels++;
1042 /* See the commentary about invalid_rl_levels below. */
1043 if (bidi_it->invalid_rl_levels < 0)
1044 bidi_it->invalid_rl_levels = 0;
1045 bidi_it->invalid_rl_levels++;
1046 }
1047 }
89d3374a 1048 else if (bidi_it->prev.type_after_w1 == WEAK_EN /* W5/Retaining */
b7b65b15
EZ
1049 || bidi_it->next_en_pos > bidi_it->charpos)
1050 type = WEAK_EN;
1051 break;
1052 case LRE: /* X3 */
1053 case LRO: /* X5 */
89d3374a
EZ
1054 bidi_it->type_after_w1 = type;
1055 bidi_check_type (bidi_it->type_after_w1);
b7b65b15
EZ
1056 type = WEAK_BN; /* X9/Retaining */
1057 if (bidi_it->ignore_bn_limit <= 0)
1058 {
1059 if (current_level <= BIDI_MAXLEVEL - 5)
1060 {
1061 /* Compute the least even embedding level greater than
1062 the current level. */
1063 new_level = ((current_level + 2) & ~1);
89d3374a 1064 if (bidi_it->type_after_w1 == LRE)
b7b65b15
EZ
1065 override = NEUTRAL_DIR;
1066 else
1067 override = L2R;
1068 bidi_push_embedding_level (bidi_it, new_level, override);
1069 }
1070 else
1071 {
1072 bidi_it->invalid_levels++;
1073 /* invalid_rl_levels counts invalid levels encountered
1074 while the embedding level was already too high for
1075 LRE/LRO, but not for RLE/RLO. That is because
1076 there may be exactly one PDF which we should not
1077 ignore even though invalid_levels is non-zero.
1078 invalid_rl_levels helps to know what PDF is
1079 that. */
1080 if (bidi_it->invalid_rl_levels >= 0)
1081 bidi_it->invalid_rl_levels++;
1082 }
1083 }
89d3374a 1084 else if (bidi_it->prev.type_after_w1 == WEAK_EN /* W5/Retaining */
b7b65b15
EZ
1085 || bidi_it->next_en_pos > bidi_it->charpos)
1086 type = WEAK_EN;
1087 break;
1088 case PDF: /* X7 */
89d3374a
EZ
1089 bidi_it->type_after_w1 = type;
1090 bidi_check_type (bidi_it->type_after_w1);
b7b65b15
EZ
1091 type = WEAK_BN; /* X9/Retaining */
1092 if (bidi_it->ignore_bn_limit <= 0)
1093 {
1094 if (!bidi_it->invalid_rl_levels)
1095 {
1096 new_level = bidi_pop_embedding_level (bidi_it);
1097 bidi_it->invalid_rl_levels = -1;
1098 if (bidi_it->invalid_levels)
1099 bidi_it->invalid_levels--;
1100 /* else nothing: UAX#9 says to ignore invalid PDFs */
1101 }
1102 if (!bidi_it->invalid_levels)
1103 new_level = bidi_pop_embedding_level (bidi_it);
1104 else
1105 {
1106 bidi_it->invalid_levels--;
1107 bidi_it->invalid_rl_levels--;
1108 }
1109 }
89d3374a 1110 else if (bidi_it->prev.type_after_w1 == WEAK_EN /* W5/Retaining */
b7b65b15
EZ
1111 || bidi_it->next_en_pos > bidi_it->charpos)
1112 type = WEAK_EN;
1113 break;
1114 default:
1115 /* Nothing. */
1116 break;
1117 }
1118
1119 bidi_it->type = type;
2d6e4628 1120 bidi_check_type (bidi_it->type);
b7b65b15
EZ
1121
1122 return new_level;
1123}
1124
1125/* Given an iterator state in BIDI_IT, advance one character position
182ce2d2
EZ
1126 in the buffer/string to the next character (in the logical order),
1127 resolve any explicit embeddings and directional overrides, and
1128 return the embedding level of the character after resolving
1129 explicit directives and ignoring empty embeddings. */
b7b65b15
EZ
1130static int
1131bidi_resolve_explicit (struct bidi_it *bidi_it)
1132{
1133 int prev_level = bidi_it->level_stack[bidi_it->stack_idx].level;
1134 int new_level = bidi_resolve_explicit_1 (bidi_it);
1135
1136 if (prev_level < new_level
1137 && bidi_it->type == WEAK_BN
1138 && bidi_it->ignore_bn_limit == 0 /* only if not already known */
1502b819 1139 && bidi_it->bytepos < ZV_BYTE /* not already at EOB */
182ce2d2
EZ
1140 && bidi_explicit_dir_char (FETCH_MULTIBYTE_CHAR (bidi_it->bytepos
1141 + bidi_it->ch_len)))
b7b65b15
EZ
1142 {
1143 /* Avoid pushing and popping embedding levels if the level run
1144 is empty, as this breaks level runs where it shouldn't.
1145 UAX#9 removes all the explicit embedding and override codes,
1146 so empty embeddings disappear without a trace. We need to
1147 behave as if we did the same. */
1148 struct bidi_it saved_it;
1149 int level = prev_level;
1150
1151 bidi_copy_it (&saved_it, bidi_it);
1152
182ce2d2
EZ
1153 while (bidi_explicit_dir_char (FETCH_MULTIBYTE_CHAR (bidi_it->bytepos
1154 + bidi_it->ch_len)))
b7b65b15 1155 {
182ce2d2
EZ
1156 /* This advances to the next character, skipping any
1157 characters covered by display strings. */
b7b65b15
EZ
1158 level = bidi_resolve_explicit_1 (bidi_it);
1159 }
1160
102ebb00
EZ
1161 if (bidi_it->nchars <= 0)
1162 abort ();
b7b65b15 1163 if (level == prev_level) /* empty embedding */
182ce2d2 1164 saved_it.ignore_bn_limit = bidi_it->charpos + bidi_it->nchars;
b7b65b15
EZ
1165 else /* this embedding is non-empty */
1166 saved_it.ignore_bn_limit = -1;
1167
1168 bidi_copy_it (bidi_it, &saved_it);
1169 if (bidi_it->ignore_bn_limit > 0)
1170 {
1171 /* We pushed a level, but we shouldn't have. Undo that. */
1172 if (!bidi_it->invalid_rl_levels)
1173 {
1174 new_level = bidi_pop_embedding_level (bidi_it);
1175 bidi_it->invalid_rl_levels = -1;
1176 if (bidi_it->invalid_levels)
1177 bidi_it->invalid_levels--;
1178 }
1179 if (!bidi_it->invalid_levels)
1180 new_level = bidi_pop_embedding_level (bidi_it);
1181 else
1182 {
1183 bidi_it->invalid_levels--;
1184 bidi_it->invalid_rl_levels--;
1185 }
1186 }
1187 }
1188
b7b65b15
EZ
1189 if (bidi_it->type == NEUTRAL_B) /* X8 */
1190 {
21fce5ab 1191 bidi_set_paragraph_end (bidi_it);
6bff6497
EZ
1192 /* This is needed by bidi_resolve_weak below, and in L1. */
1193 bidi_it->type_after_w1 = bidi_it->type;
89d3374a 1194 bidi_check_type (bidi_it->type_after_w1);
b7b65b15
EZ
1195 }
1196
1197 return new_level;
1198}
1199
182ce2d2
EZ
1200/* Advance in the buffer/string, resolve weak types and return the
1201 type of the next character after weak type resolution. */
fd3998ff 1202static bidi_type_t
b7b65b15
EZ
1203bidi_resolve_weak (struct bidi_it *bidi_it)
1204{
1205 bidi_type_t type;
1206 bidi_dir_t override;
1207 int prev_level = bidi_it->level_stack[bidi_it->stack_idx].level;
1208 int new_level = bidi_resolve_explicit (bidi_it);
1209 int next_char;
1210 bidi_type_t type_of_next;
1211 struct bidi_it saved_it;
1212
1213 type = bidi_it->type;
1214 override = bidi_it->level_stack[bidi_it->stack_idx].override;
1215
1216 if (type == UNKNOWN_BT
1217 || type == LRE
1218 || type == LRO
1219 || type == RLE
1220 || type == RLO
1221 || type == PDF)
1222 abort ();
1223
1224 if (new_level != prev_level
1225 || bidi_it->type == NEUTRAL_B)
1226 {
1227 /* We've got a new embedding level run, compute the directional
1228 type of sor and initialize per-run variables (UAX#9, clause
1229 X10). */
1230 bidi_set_sor_type (bidi_it, prev_level, new_level);
1231 }
1232 else if (type == NEUTRAL_S || type == NEUTRAL_WS
1233 || type == WEAK_BN || type == STRONG_AL)
89d3374a
EZ
1234 bidi_it->type_after_w1 = type; /* needed in L1 */
1235 bidi_check_type (bidi_it->type_after_w1);
b7b65b15
EZ
1236
1237 /* Level and directional override status are already recorded in
1238 bidi_it, and do not need any change; see X6. */
1239 if (override == R2L) /* X6 */
1240 type = STRONG_R;
1241 else if (override == L2R)
1242 type = STRONG_L;
bc5a45f3 1243 else
b7b65b15 1244 {
bc5a45f3 1245 if (type == WEAK_NSM) /* W1 */
b7b65b15 1246 {
bc5a45f3 1247 /* Note that we don't need to consider the case where the
5930fe97
EZ
1248 prev character has its type overridden by an RLO or LRO,
1249 because then either the type of this NSM would have been
1250 also overridden, or the previous character is outside the
1251 current level run, and thus not relevant to this NSM.
1252 This is why NSM gets the type_after_w1 of the previous
1253 character. */
ebb5722e
EZ
1254 if (bidi_it->prev.type_after_w1 != UNKNOWN_BT
1255 /* if type_after_w1 is NEUTRAL_B, this NSM is at sor */
1256 && bidi_it->prev.type_after_w1 != NEUTRAL_B)
5930fe97 1257 type = bidi_it->prev.type_after_w1;
bc5a45f3
EZ
1258 else if (bidi_it->sor == R2L)
1259 type = STRONG_R;
1260 else if (bidi_it->sor == L2R)
1261 type = STRONG_L;
1262 else /* shouldn't happen! */
1263 abort ();
b7b65b15 1264 }
bc5a45f3
EZ
1265 if (type == WEAK_EN /* W2 */
1266 && bidi_it->last_strong.type_after_w1 == STRONG_AL)
1267 type = WEAK_AN;
1268 else if (type == STRONG_AL) /* W3 */
1269 type = STRONG_R;
1270 else if ((type == WEAK_ES /* W4 */
1271 && bidi_it->prev.type_after_w1 == WEAK_EN
1272 && bidi_it->prev.orig_type == WEAK_EN)
1273 || (type == WEAK_CS
1274 && ((bidi_it->prev.type_after_w1 == WEAK_EN
1275 && bidi_it->prev.orig_type == WEAK_EN)
1276 || bidi_it->prev.type_after_w1 == WEAK_AN)))
b7b65b15 1277 {
e7402cb2
EZ
1278 next_char =
1279 bidi_it->bytepos + bidi_it->ch_len >= ZV_BYTE
182ce2d2
EZ
1280 ? BIDI_EOB : FETCH_MULTIBYTE_CHAR (bidi_it->bytepos
1281 + bidi_it->ch_len);
6bff6497 1282 type_of_next = bidi_get_type (next_char, override);
b7b65b15 1283
bc5a45f3 1284 if (type_of_next == WEAK_BN
b7b65b15
EZ
1285 || bidi_explicit_dir_char (next_char))
1286 {
1287 bidi_copy_it (&saved_it, bidi_it);
1288 while (bidi_resolve_explicit (bidi_it) == new_level
bc5a45f3 1289 && bidi_it->type == WEAK_BN)
b7b65b15
EZ
1290 ;
1291 type_of_next = bidi_it->type;
b7b65b15
EZ
1292 bidi_copy_it (bidi_it, &saved_it);
1293 }
bc5a45f3
EZ
1294
1295 /* If the next character is EN, but the last strong-type
1296 character is AL, that next EN will be changed to AN when
1297 we process it in W2 above. So in that case, this ES
1298 should not be changed into EN. */
1299 if (type == WEAK_ES
1300 && type_of_next == WEAK_EN
1301 && bidi_it->last_strong.type_after_w1 != STRONG_AL)
1302 type = WEAK_EN;
1303 else if (type == WEAK_CS)
b7b65b15 1304 {
bc5a45f3
EZ
1305 if (bidi_it->prev.type_after_w1 == WEAK_AN
1306 && (type_of_next == WEAK_AN
1307 /* If the next character is EN, but the last
1308 strong-type character is AL, EN will be later
1309 changed to AN when we process it in W2 above.
1310 So in that case, this ES should not be
1311 changed into EN. */
1312 || (type_of_next == WEAK_EN
1313 && bidi_it->last_strong.type_after_w1 == STRONG_AL)))
1314 type = WEAK_AN;
1315 else if (bidi_it->prev.type_after_w1 == WEAK_EN
1316 && type_of_next == WEAK_EN
1317 && bidi_it->last_strong.type_after_w1 != STRONG_AL)
1318 type = WEAK_EN;
1319 }
1320 }
1321 else if (type == WEAK_ET /* W5: ET with EN before or after it */
1322 || type == WEAK_BN) /* W5/Retaining */
1323 {
1324 if (bidi_it->prev.type_after_w1 == WEAK_EN /* ET/BN w/EN before it */
1325 || bidi_it->next_en_pos > bidi_it->charpos)
1326 type = WEAK_EN;
1327 else /* W5: ET/BN with EN after it. */
1328 {
182ce2d2 1329 EMACS_INT en_pos = bidi_it->charpos + bidi_it->nchars;
bc5a45f3 1330
102ebb00
EZ
1331 if (bidi_it->nchars <= 0)
1332 abort ();
bc5a45f3
EZ
1333 next_char =
1334 bidi_it->bytepos + bidi_it->ch_len >= ZV_BYTE
182ce2d2
EZ
1335 ? BIDI_EOB : FETCH_MULTIBYTE_CHAR (bidi_it->bytepos
1336 + bidi_it->ch_len);
bc5a45f3
EZ
1337 type_of_next = bidi_get_type (next_char, override);
1338
1339 if (type_of_next == WEAK_ET
1340 || type_of_next == WEAK_BN
1341 || bidi_explicit_dir_char (next_char))
1342 {
1343 bidi_copy_it (&saved_it, bidi_it);
1344 while (bidi_resolve_explicit (bidi_it) == new_level
1345 && (bidi_it->type == WEAK_BN
1346 || bidi_it->type == WEAK_ET))
1347 ;
1348 type_of_next = bidi_it->type;
1349 en_pos = bidi_it->charpos;
1350 bidi_copy_it (bidi_it, &saved_it);
1351 }
1352 if (type_of_next == WEAK_EN)
b7b65b15 1353 {
bc5a45f3
EZ
1354 /* If the last strong character is AL, the EN we've
1355 found will become AN when we get to it (W2). */
1356 if (bidi_it->last_strong.type_after_w1 != STRONG_AL)
1357 {
1358 type = WEAK_EN;
1359 /* Remember this EN position, to speed up processing
1360 of the next ETs. */
1361 bidi_it->next_en_pos = en_pos;
1362 }
1363 else if (type == WEAK_BN)
1364 type = NEUTRAL_ON; /* W6/Retaining */
b7b65b15 1365 }
b7b65b15
EZ
1366 }
1367 }
1368 }
1369
1370 if (type == WEAK_ES || type == WEAK_ET || type == WEAK_CS /* W6 */
89d3374a
EZ
1371 || (type == WEAK_BN
1372 && (bidi_it->prev.type_after_w1 == WEAK_CS /* W6/Retaining */
1373 || bidi_it->prev.type_after_w1 == WEAK_ES
1374 || bidi_it->prev.type_after_w1 == WEAK_ET)))
b7b65b15
EZ
1375 type = NEUTRAL_ON;
1376
1377 /* Store the type we've got so far, before we clobber it with strong
1378 types in W7 and while resolving neutral types. But leave alone
1379 the original types that were recorded above, because we will need
1380 them for the L1 clause. */
89d3374a
EZ
1381 if (bidi_it->type_after_w1 == UNKNOWN_BT)
1382 bidi_it->type_after_w1 = type;
1383 bidi_check_type (bidi_it->type_after_w1);
b7b65b15
EZ
1384
1385 if (type == WEAK_EN) /* W7 */
1386 {
89d3374a 1387 if ((bidi_it->last_strong.type_after_w1 == STRONG_L)
b7b65b15
EZ
1388 || (bidi_it->last_strong.type == UNKNOWN_BT && bidi_it->sor == L2R))
1389 type = STRONG_L;
1390 }
1391
1392 bidi_it->type = type;
2d6e4628 1393 bidi_check_type (bidi_it->type);
b7b65b15
EZ
1394 return type;
1395}
1396
fd3998ff 1397static bidi_type_t
b7b65b15
EZ
1398bidi_resolve_neutral (struct bidi_it *bidi_it)
1399{
1400 int prev_level = bidi_it->level_stack[bidi_it->stack_idx].level;
1401 bidi_type_t type = bidi_resolve_weak (bidi_it);
1402 int current_level = bidi_it->level_stack[bidi_it->stack_idx].level;
1403
1404 if (!(type == STRONG_R
1405 || type == STRONG_L
1406 || type == WEAK_BN
1407 || type == WEAK_EN
1408 || type == WEAK_AN
1409 || type == NEUTRAL_B
1410 || type == NEUTRAL_S
1411 || type == NEUTRAL_WS
1412 || type == NEUTRAL_ON))
1413 abort ();
1414
1415 if (bidi_get_category (type) == NEUTRAL
1416 || (type == WEAK_BN && prev_level == current_level))
1417 {
1418 if (bidi_it->next_for_neutral.type != UNKNOWN_BT)
1419 type = bidi_resolve_neutral_1 (bidi_it->prev_for_neutral.type,
1420 bidi_it->next_for_neutral.type,
1421 current_level);
1422 else
1423 {
1424 /* Arrrgh!! The UAX#9 algorithm is too deeply entrenched in
1425 the assumption of batch-style processing; see clauses W4,
1426 W5, and especially N1, which require to look far forward
182ce2d2
EZ
1427 (as well as back) in the buffer/string. May the fleas of
1428 a thousand camels infest the armpits of those who design
b7b65b15
EZ
1429 supposedly general-purpose algorithms by looking at their
1430 own implementations, and fail to consider other possible
1431 implementations! */
1432 struct bidi_it saved_it;
1433 bidi_type_t next_type;
1434
1435 if (bidi_it->scan_dir == -1)
1436 abort ();
1437
1438 bidi_copy_it (&saved_it, bidi_it);
1439 /* Scan the text forward until we find the first non-neutral
1440 character, and then use that to resolve the neutral we
1441 are dealing with now. We also cache the scanned iterator
1442 states, to salvage some of the effort later. */
1443 bidi_cache_iterator_state (bidi_it, 0);
1444 do {
1445 /* Record the info about the previous character, so that
1446 it will be cached below with this state. */
89d3374a 1447 if (bidi_it->type_after_w1 != WEAK_BN /* W1/Retaining */
b7b65b15
EZ
1448 && bidi_it->type != WEAK_BN)
1449 bidi_remember_char (&bidi_it->prev, bidi_it);
1450 type = bidi_resolve_weak (bidi_it);
1451 /* Paragraph separators have their levels fully resolved
1452 at this point, so cache them as resolved. */
1453 bidi_cache_iterator_state (bidi_it, type == NEUTRAL_B);
1454 /* FIXME: implement L1 here, by testing for a newline and
1455 resetting the level for any sequence of whitespace
1456 characters adjacent to it. */
1457 } while (!(type == NEUTRAL_B
1458 || (type != WEAK_BN
1459 && bidi_get_category (type) != NEUTRAL)
1460 /* This is all per level run, so stop when we
1461 reach the end of this level run. */
1462 || bidi_it->level_stack[bidi_it->stack_idx].level !=
1463 current_level));
1464
1465 bidi_remember_char (&saved_it.next_for_neutral, bidi_it);
1466
1467 switch (type)
1468 {
1469 case STRONG_L:
1470 case STRONG_R:
1471 case STRONG_AL:
1472 next_type = type;
1473 break;
1474 case WEAK_EN:
1475 case WEAK_AN:
1476 /* N1: ``European and Arabic numbers are treated as
1477 though they were R.'' */
1478 next_type = STRONG_R;
1479 saved_it.next_for_neutral.type = STRONG_R;
1480 break;
1481 case WEAK_BN:
1482 if (!bidi_explicit_dir_char (bidi_it->ch))
1483 abort (); /* can't happen: BNs are skipped */
1484 /* FALLTHROUGH */
1485 case NEUTRAL_B:
1486 /* Marched all the way to the end of this level run.
1487 We need to use the eor type, whose information is
1488 stored by bidi_set_sor_type in the prev_for_neutral
1489 member. */
1490 if (saved_it.type != WEAK_BN
89d3374a 1491 || bidi_get_category (bidi_it->prev.type_after_w1) == NEUTRAL)
b7b65b15
EZ
1492 {
1493 next_type = bidi_it->prev_for_neutral.type;
1494 saved_it.next_for_neutral.type = next_type;
2d6e4628 1495 bidi_check_type (next_type);
b7b65b15
EZ
1496 }
1497 else
1498 {
1499 /* This is a BN which does not adjoin neutrals.
1500 Leave its type alone. */
1501 bidi_copy_it (bidi_it, &saved_it);
1502 return bidi_it->type;
1503 }
1504 break;
1505 default:
1506 abort ();
1507 }
1508 type = bidi_resolve_neutral_1 (saved_it.prev_for_neutral.type,
1509 next_type, current_level);
1510 saved_it.type = type;
2d6e4628 1511 bidi_check_type (type);
b7b65b15
EZ
1512 bidi_copy_it (bidi_it, &saved_it);
1513 }
1514 }
1515 return type;
1516}
1517
1518/* Given an iterator state in BIDI_IT, advance one character position
182ce2d2
EZ
1519 in the buffer/string to the next character (in the logical order),
1520 resolve the bidi type of that next character, and return that
1521 type. */
fd3998ff 1522static bidi_type_t
b7b65b15
EZ
1523bidi_type_of_next_char (struct bidi_it *bidi_it)
1524{
1525 bidi_type_t type;
1526
1527 /* This should always be called during a forward scan. */
1528 if (bidi_it->scan_dir != 1)
1529 abort ();
1530
1531 /* Reset the limit until which to ignore BNs if we step out of the
1532 area where we found only empty levels. */
1533 if ((bidi_it->ignore_bn_limit > 0
1534 && bidi_it->ignore_bn_limit <= bidi_it->charpos)
1535 || (bidi_it->ignore_bn_limit == -1
1536 && !bidi_explicit_dir_char (bidi_it->ch)))
1537 bidi_it->ignore_bn_limit = 0;
1538
1539 type = bidi_resolve_neutral (bidi_it);
1540
1541 return type;
1542}
1543
1544/* Given an iterator state BIDI_IT, advance one character position in
182ce2d2
EZ
1545 the buffer/string to the next character (in the current scan
1546 direction), resolve the embedding and implicit levels of that next
1547 character, and return the resulting level. */
fd3998ff 1548static int
b7b65b15
EZ
1549bidi_level_of_next_char (struct bidi_it *bidi_it)
1550{
1551 bidi_type_t type;
1552 int level, prev_level = -1;
1553 struct bidi_saved_info next_for_neutral;
182ce2d2 1554 EMACS_INT next_char_pos;
b7b65b15
EZ
1555
1556 if (bidi_it->scan_dir == 1)
1557 {
1558 /* There's no sense in trying to advance if we hit end of text. */
1502b819 1559 if (bidi_it->bytepos >= ZV_BYTE)
b7b65b15
EZ
1560 return bidi_it->resolved_level;
1561
1562 /* Record the info about the previous character. */
89d3374a 1563 if (bidi_it->type_after_w1 != WEAK_BN /* W1/Retaining */
b7b65b15
EZ
1564 && bidi_it->type != WEAK_BN)
1565 bidi_remember_char (&bidi_it->prev, bidi_it);
89d3374a
EZ
1566 if (bidi_it->type_after_w1 == STRONG_R
1567 || bidi_it->type_after_w1 == STRONG_L
1568 || bidi_it->type_after_w1 == STRONG_AL)
b7b65b15
EZ
1569 bidi_remember_char (&bidi_it->last_strong, bidi_it);
1570 /* FIXME: it sounds like we don't need both prev and
1571 prev_for_neutral members, but I'm leaving them both for now. */
1572 if (bidi_it->type == STRONG_R || bidi_it->type == STRONG_L
1573 || bidi_it->type == WEAK_EN || bidi_it->type == WEAK_AN)
1574 bidi_remember_char (&bidi_it->prev_for_neutral, bidi_it);
1575
1576 /* If we overstepped the characters used for resolving neutrals
1577 and whitespace, invalidate their info in the iterator. */
1578 if (bidi_it->charpos >= bidi_it->next_for_neutral.charpos)
1579 bidi_it->next_for_neutral.type = UNKNOWN_BT;
1580 if (bidi_it->next_en_pos >= 0
1581 && bidi_it->charpos >= bidi_it->next_en_pos)
1582 bidi_it->next_en_pos = -1;
1583 if (bidi_it->next_for_ws.type != UNKNOWN_BT
1584 && bidi_it->charpos >= bidi_it->next_for_ws.charpos)
1585 bidi_it->next_for_ws.type = UNKNOWN_BT;
1586
1587 /* This must be taken before we fill the iterator with the info
1588 about the next char. If we scan backwards, the iterator
1589 state must be already cached, so there's no need to know the
1590 embedding level of the previous character, since we will be
1591 returning to our caller shortly. */
1592 prev_level = bidi_it->level_stack[bidi_it->stack_idx].level;
1593 }
1594 next_for_neutral = bidi_it->next_for_neutral;
1595
182ce2d2
EZ
1596 /* Perhaps the character we want is already cached. If it is, the
1597 call to bidi_cache_find below will return a type other than
1598 UNKNOWN_BT. */
102ebb00
EZ
1599 if (bidi_cache_idx && !bidi_it->first_elt)
1600 {
1601 if (bidi_it->scan_dir > 0)
1602 {
1603 if (bidi_it->nchars <= 0)
1604 abort ();
1605 next_char_pos = bidi_it->charpos + bidi_it->nchars;
1606 }
1607 else
1608 next_char_pos = bidi_it->charpos - 1;
1609 type = bidi_cache_find (next_char_pos, -1, bidi_it);
1610 }
182ce2d2 1611 else
102ebb00 1612 type = UNKNOWN_BT;
b7b65b15
EZ
1613 if (type != UNKNOWN_BT)
1614 {
1615 /* Don't lose the information for resolving neutrals! The
1616 cached states could have been cached before their
1617 next_for_neutral member was computed. If we are on our way
1618 forward, we can simply take the info from the previous
1619 state. */
1620 if (bidi_it->scan_dir == 1
1621 && bidi_it->next_for_neutral.type == UNKNOWN_BT)
1622 bidi_it->next_for_neutral = next_for_neutral;
1623
1624 /* If resolved_level is -1, it means this state was cached
1625 before it was completely resolved, so we cannot return
1626 it. */
1627 if (bidi_it->resolved_level != -1)
1628 return bidi_it->resolved_level;
1629 }
1630 if (bidi_it->scan_dir == -1)
1631 /* If we are going backwards, the iterator state is already cached
1632 from previous scans, and should be fully resolved. */
1633 abort ();
1634
1635 if (type == UNKNOWN_BT)
1636 type = bidi_type_of_next_char (bidi_it);
1637
1638 if (type == NEUTRAL_B)
1639 return bidi_it->resolved_level;
1640
1641 level = bidi_it->level_stack[bidi_it->stack_idx].level;
1642 if ((bidi_get_category (type) == NEUTRAL /* && type != NEUTRAL_B */)
1643 || (type == WEAK_BN && prev_level == level))
1644 {
1645 if (bidi_it->next_for_neutral.type == UNKNOWN_BT)
1646 abort ();
1647
1648 /* If the cached state shows a neutral character, it was not
1649 resolved by bidi_resolve_neutral, so do it now. */
1650 type = bidi_resolve_neutral_1 (bidi_it->prev_for_neutral.type,
1651 bidi_it->next_for_neutral.type,
1652 level);
1653 }
1654
1655 if (!(type == STRONG_R
1656 || type == STRONG_L
1657 || type == WEAK_BN
1658 || type == WEAK_EN
1659 || type == WEAK_AN))
1660 abort ();
1661 bidi_it->type = type;
2d6e4628 1662 bidi_check_type (bidi_it->type);
b7b65b15
EZ
1663
1664 /* For L1 below, we need to know, for each WS character, whether
0d327994 1665 it belongs to a sequence of WS characters preceding a newline
b7b65b15 1666 or a TAB or a paragraph separator. */
89d3374a 1667 if (bidi_it->orig_type == NEUTRAL_WS
b7b65b15
EZ
1668 && bidi_it->next_for_ws.type == UNKNOWN_BT)
1669 {
1670 int ch;
1671 int clen = bidi_it->ch_len;
6bff6497
EZ
1672 EMACS_INT bpos = bidi_it->bytepos;
1673 EMACS_INT cpos = bidi_it->charpos;
182ce2d2 1674 EMACS_INT disp_pos = bidi_it->disp_pos;
102ebb00 1675 EMACS_INT nc = bidi_it->nchars;
b7b65b15
EZ
1676 bidi_type_t chtype;
1677
102ebb00
EZ
1678 if (bidi_it->nchars <= 0)
1679 abort ();
b7b65b15 1680 do {
102ebb00 1681 ch = bidi_fetch_char (bpos += clen, cpos += nc, &disp_pos, &clen, &nc);
e7402cb2 1682 if (ch == '\n' || ch == BIDI_EOB /* || ch == LINESEP_CHAR */)
b7b65b15
EZ
1683 chtype = NEUTRAL_B;
1684 else
6bff6497 1685 chtype = bidi_get_type (ch, NEUTRAL_DIR);
b7b65b15
EZ
1686 } while (chtype == NEUTRAL_WS || chtype == WEAK_BN
1687 || bidi_explicit_dir_char (ch)); /* L1/Retaining */
1688 bidi_it->next_for_ws.type = chtype;
2d6e4628 1689 bidi_check_type (bidi_it->next_for_ws.type);
b7b65b15
EZ
1690 bidi_it->next_for_ws.charpos = cpos;
1691 bidi_it->next_for_ws.bytepos = bpos;
1692 }
1693
1694 /* Resolve implicit levels, with a twist: PDFs get the embedding
1695 level of the enbedding they terminate. See below for the
1696 reason. */
89d3374a 1697 if (bidi_it->orig_type == PDF
b7b65b15
EZ
1698 /* Don't do this if this formatting code didn't change the
1699 embedding level due to invalid or empty embeddings. */
1700 && prev_level != level)
1701 {
1702 /* Don't look in UAX#9 for the reason for this: it's our own
1703 private quirk. The reason is that we want the formatting
1704 codes to be delivered so that they bracket the text of their
1705 embedding. For example, given the text
1706
1707 {RLO}teST{PDF}
1708
1709 we want it to be displayed as
1710
0d68907d 1711 {PDF}STet{RLO}
b7b65b15
EZ
1712
1713 not as
1714
1715 STet{RLO}{PDF}
1716
1717 which will result because we bump up the embedding level as
1718 soon as we see the RLO and pop it as soon as we see the PDF,
1719 so RLO itself has the same embedding level as "teST", and
1720 thus would be normally delivered last, just before the PDF.
1721 The switch below fiddles with the level of PDF so that this
1722 ugly side effect does not happen.
1723
1724 (This is, of course, only important if the formatting codes
e7402cb2
EZ
1725 are actually displayed, but Emacs does need to display them
1726 if the user wants to.) */
b7b65b15
EZ
1727 level = prev_level;
1728 }
89d3374a
EZ
1729 else if (bidi_it->orig_type == NEUTRAL_B /* L1 */
1730 || bidi_it->orig_type == NEUTRAL_S
e7402cb2
EZ
1731 || bidi_it->ch == '\n' || bidi_it->ch == BIDI_EOB
1732 /* || bidi_it->ch == LINESEP_CHAR */
89d3374a 1733 || (bidi_it->orig_type == NEUTRAL_WS
b7b65b15
EZ
1734 && (bidi_it->next_for_ws.type == NEUTRAL_B
1735 || bidi_it->next_for_ws.type == NEUTRAL_S)))
1736 level = bidi_it->level_stack[0].level;
1737 else if ((level & 1) == 0) /* I1 */
1738 {
1739 if (type == STRONG_R)
1740 level++;
1741 else if (type == WEAK_EN || type == WEAK_AN)
1742 level += 2;
1743 }
1744 else /* I2 */
1745 {
1746 if (type == STRONG_L || type == WEAK_EN || type == WEAK_AN)
1747 level++;
1748 }
1749
1750 bidi_it->resolved_level = level;
1751 return level;
1752}
1753
1754/* Move to the other edge of a level given by LEVEL. If END_FLAG is
1755 non-zero, we are at the end of a level, and we need to prepare to
1756 resume the scan of the lower level.
1757
1758 If this level's other edge is cached, we simply jump to it, filling
1759 the iterator structure with the iterator state on the other edge.
182ce2d2
EZ
1760 Otherwise, we walk the buffer or string until we come back to the
1761 same level as LEVEL.
b7b65b15
EZ
1762
1763 Note: we are not talking here about a ``level run'' in the UAX#9
1764 sense of the term, but rather about a ``level'' which includes
1765 all the levels higher than it. In other words, given the levels
1766 like this:
1767
1768 11111112222222333333334443343222222111111112223322111
1769 A B C
1770
1771 and assuming we are at point A scanning left to right, this
1772 function moves to point C, whereas the UAX#9 ``level 2 run'' ends
1773 at point B. */
1774static void
1775bidi_find_other_level_edge (struct bidi_it *bidi_it, int level, int end_flag)
1776{
1777 int dir = end_flag ? -bidi_it->scan_dir : bidi_it->scan_dir;
1778 int idx;
1779
1780 /* Try the cache first. */
1781 if ((idx = bidi_cache_find_level_change (level, dir, end_flag)) >= 0)
1782 bidi_cache_fetch_state (idx, bidi_it);
1783 else
1784 {
1785 int new_level;
1786
1787 if (end_flag)
1788 abort (); /* if we are at end of level, its edges must be cached */
1789
1790 bidi_cache_iterator_state (bidi_it, 1);
1791 do {
1792 new_level = bidi_level_of_next_char (bidi_it);
1793 bidi_cache_iterator_state (bidi_it, 1);
1794 } while (new_level >= level);
1795 }
1796}
1797
1798void
4b292a22 1799bidi_move_to_visually_next (struct bidi_it *bidi_it)
b7b65b15
EZ
1800{
1801 int old_level, new_level, next_level;
9c82e145 1802 struct bidi_it sentinel;
b7b65b15
EZ
1803
1804 if (bidi_it->scan_dir == 0)
1805 {
1806 bidi_it->scan_dir = 1; /* default to logical order */
1807 }
1808
be39f003
EZ
1809 /* If we just passed a newline, initialize for the next line. */
1810 if (!bidi_it->first_elt && bidi_it->orig_type == NEUTRAL_B)
1811 bidi_line_init (bidi_it);
1812
6dcfd253
EZ
1813 /* Prepare the sentinel iterator state, and cache it. When we bump
1814 into it, scanning backwards, we'll know that the last non-base
1815 level is exhausted. */
b7b65b15 1816 if (bidi_cache_idx == 0)
9c82e145
EZ
1817 {
1818 bidi_copy_it (&sentinel, bidi_it);
1819 if (bidi_it->first_elt)
1820 {
1821 sentinel.charpos--; /* cached charpos needs to be monotonic */
1822 sentinel.bytepos--;
1823 sentinel.ch = '\n'; /* doesn't matter, but why not? */
1824 sentinel.ch_len = 1;
182ce2d2 1825 sentinel.nchars = 1;
9c82e145 1826 }
6dcfd253 1827 bidi_cache_iterator_state (&sentinel, 1);
9c82e145 1828 }
b7b65b15
EZ
1829
1830 old_level = bidi_it->resolved_level;
1831 new_level = bidi_level_of_next_char (bidi_it);
b7b65b15
EZ
1832
1833 /* Reordering of resolved levels (clause L2) is implemented by
1834 jumping to the other edge of the level and flipping direction of
c0546589 1835 scanning the text whenever we find a level change. */
b7b65b15
EZ
1836 if (new_level != old_level)
1837 {
1838 int ascending = new_level > old_level;
1839 int level_to_search = ascending ? old_level + 1 : old_level;
1840 int incr = ascending ? 1 : -1;
1841 int expected_next_level = old_level + incr;
1842
b7b65b15
EZ
1843 /* Jump (or walk) to the other edge of this level. */
1844 bidi_find_other_level_edge (bidi_it, level_to_search, !ascending);
1845 /* Switch scan direction and peek at the next character in the
1846 new direction. */
1847 bidi_it->scan_dir = -bidi_it->scan_dir;
1848
1849 /* The following loop handles the case where the resolved level
1850 jumps by more than one. This is typical for numbers inside a
1851 run of text with left-to-right embedding direction, but can
1852 also happen in other situations. In those cases the decision
1853 where to continue after a level change, and in what direction,
1854 is tricky. For example, given a text like below:
1855
1856 abcdefgh
1857 11336622
1858
1859 (where the numbers below the text show the resolved levels),
1860 the result of reordering according to UAX#9 should be this:
1861
1862 efdcghba
1863
1864 This is implemented by the loop below which flips direction
1865 and jumps to the other edge of the level each time it finds
1866 the new level not to be the expected one. The expected level
1867 is always one more or one less than the previous one. */
1868 next_level = bidi_peek_at_next_level (bidi_it);
1869 while (next_level != expected_next_level)
1870 {
1871 expected_next_level += incr;
1872 level_to_search += incr;
1873 bidi_find_other_level_edge (bidi_it, level_to_search, !ascending);
1874 bidi_it->scan_dir = -bidi_it->scan_dir;
1875 next_level = bidi_peek_at_next_level (bidi_it);
1876 }
1877
1878 /* Finally, deliver the next character in the new direction. */
1879 next_level = bidi_level_of_next_char (bidi_it);
1880 }
1881
b44d9321
EZ
1882 /* Take note when we have just processed the newline that precedes
1883 the end of the paragraph. The next time we are about to be
1884 called, set_iterator_to_next will automatically reinit the
1885 paragraph direction, if needed. We do this at the newline before
1886 the paragraph separator, because the next character might not be
1887 the first character of the next paragraph, due to the bidi
c0546589
EZ
1888 reordering, whereas we _must_ know the paragraph base direction
1889 _before_ we process the paragraph's text, since the base
1890 direction affects the reordering. */
6bff6497 1891 if (bidi_it->scan_dir == 1
be39f003
EZ
1892 && bidi_it->orig_type == NEUTRAL_B
1893 && bidi_it->bytepos < ZV_BYTE)
1894 {
1895 EMACS_INT sep_len =
182ce2d2 1896 bidi_at_paragraph_end (bidi_it->charpos + bidi_it->nchars,
be39f003 1897 bidi_it->bytepos + bidi_it->ch_len);
102ebb00
EZ
1898 if (bidi_it->nchars <= 0)
1899 abort ();
be39f003
EZ
1900 if (sep_len >= 0)
1901 {
1902 bidi_it->new_paragraph = 1;
b44d9321
EZ
1903 /* Record the buffer position of the last character of the
1904 paragraph separator. */
182ce2d2
EZ
1905 bidi_it->separator_limit =
1906 bidi_it->charpos + bidi_it->nchars + sep_len;
be39f003
EZ
1907 }
1908 }
6bff6497 1909
b7b65b15
EZ
1910 if (bidi_it->scan_dir == 1 && bidi_cache_idx)
1911 {
1912 /* If we are at paragraph's base embedding level and beyond the
1913 last cached position, the cache's job is done and we can
1914 discard it. */
1915 if (bidi_it->resolved_level == bidi_it->level_stack[0].level
182ce2d2
EZ
1916 && bidi_it->charpos > (bidi_cache[bidi_cache_idx - 1].charpos
1917 + bidi_cache[bidi_cache_idx - 1].nchars - 1))
b7b65b15
EZ
1918 bidi_cache_reset ();
1919 /* But as long as we are caching during forward scan, we must
1920 cache each state, or else the cache integrity will be
1921 compromised: it assumes cached states correspond to buffer
1922 positions 1:1. */
1923 else
1924 bidi_cache_iterator_state (bidi_it, 1);
1925 }
1926}
1927
1928/* This is meant to be called from within the debugger, whenever you
1929 wish to examine the cache contents. */
e78aecca 1930void bidi_dump_cached_states (void) EXTERNALLY_VISIBLE;
b7b65b15
EZ
1931void
1932bidi_dump_cached_states (void)
1933{
1934 int i;
1935 int ndigits = 1;
1936
1937 if (bidi_cache_idx == 0)
1938 {
1939 fprintf (stderr, "The cache is empty.\n");
1940 return;
1941 }
1942 fprintf (stderr, "Total of %d state%s in cache:\n",
1943 bidi_cache_idx, bidi_cache_idx == 1 ? "" : "s");
1944
1945 for (i = bidi_cache[bidi_cache_idx - 1].charpos; i > 0; i /= 10)
1946 ndigits++;
1947 fputs ("ch ", stderr);
1948 for (i = 0; i < bidi_cache_idx; i++)
1949 fprintf (stderr, "%*c", ndigits, bidi_cache[i].ch);
1950 fputs ("\n", stderr);
1951 fputs ("lvl ", stderr);
1952 for (i = 0; i < bidi_cache_idx; i++)
1953 fprintf (stderr, "%*d", ndigits, bidi_cache[i].resolved_level);
1954 fputs ("\n", stderr);
1955 fputs ("pos ", stderr);
1956 for (i = 0; i < bidi_cache_idx; i++)
c2982e87 1957 fprintf (stderr, "%*"pI"d", ndigits, bidi_cache[i].charpos);
b7b65b15
EZ
1958 fputs ("\n", stderr);
1959}