Fix bug #9221 with memory leak in bidi display.
[emacs.git] / src / bidi.c
blob00a6aeef9544ecb39417317f7b21e980c9a14d00
1 /* Low-level bidirectional buffer/string-scanning functions for GNU Emacs.
2 Copyright (C) 2000-2001, 2004-2005, 2009-2011
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* Written by Eli Zaretskii <eliz@gnu.org>.
22 A sequential implementation of the Unicode Bidirectional algorithm,
23 (UBA) as per UAX#9, a part of the Unicode Standard.
25 Unlike the reference and most other implementations, this one is
26 designed to be called once for every character in the buffer or
27 string.
29 The main entry point is bidi_move_to_visually_next. Each time it
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
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
36 character by resolving their levels on the fly.
38 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.
43 A few auxiliary entry points are used to initialize the bidi
44 iterator for iterating an object (buffer or string), push and pop
45 the bidi iterator state, and save and restore the state of the bidi
46 cache.
48 If you want to understand the code, you will have to read it
49 together with the relevant portions of UAX#9. The comments include
50 references to UAX#9 rules, for that very reason.
52 A note about references to UAX#9 rules: if the reference says
53 something like "X9/Retaining", it means that you need to refer to
54 rule X9 and to its modifications decribed in the "Implementation
55 Notes" section of UAX#9, under "Retaining Format Codes". */
57 #include <config.h>
58 #include <stdio.h>
59 #include <setjmp.h>
61 #include "lisp.h"
62 #include "buffer.h"
63 #include "character.h"
64 #include "dispextern.h"
66 static int bidi_initialized = 0;
68 static Lisp_Object bidi_type_table, bidi_mirror_table;
70 #define LRM_CHAR 0x200E
71 #define RLM_CHAR 0x200F
72 #define BIDI_EOB -1
74 /* Data type for describing the bidirectional character categories. */
75 typedef enum {
76 UNKNOWN_BC,
77 NEUTRAL,
78 WEAK,
79 STRONG
80 } bidi_category_t;
82 extern int bidi_ignore_explicit_marks_for_paragraph_level EXTERNALLY_VISIBLE;
83 int bidi_ignore_explicit_marks_for_paragraph_level = 1;
85 static Lisp_Object paragraph_start_re, paragraph_separate_re;
86 static Lisp_Object Qparagraph_start, Qparagraph_separate;
89 /***********************************************************************
90 Utilities
91 ***********************************************************************/
93 /* Return the bidi type of a character CH, subject to the current
94 directional OVERRIDE. */
95 static inline bidi_type_t
96 bidi_get_type (int ch, bidi_dir_t override)
98 bidi_type_t default_type;
100 if (ch == BIDI_EOB)
101 return NEUTRAL_B;
102 if (ch < 0 || ch > MAX_CHAR)
103 abort ();
105 default_type = (bidi_type_t) XINT (CHAR_TABLE_REF (bidi_type_table, ch));
107 if (override == NEUTRAL_DIR)
108 return default_type;
110 switch (default_type)
112 /* Although UAX#9 does not tell, it doesn't make sense to
113 override NEUTRAL_B and LRM/RLM characters. */
114 case NEUTRAL_B:
115 case LRE:
116 case LRO:
117 case RLE:
118 case RLO:
119 case PDF:
120 return default_type;
121 default:
122 switch (ch)
124 case LRM_CHAR:
125 case RLM_CHAR:
126 return default_type;
127 default:
128 if (override == L2R) /* X6 */
129 return STRONG_L;
130 else if (override == R2L)
131 return STRONG_R;
132 else
133 abort (); /* can't happen: handled above */
138 static void
139 bidi_check_type (bidi_type_t type)
141 if (type < UNKNOWN_BT || type > NEUTRAL_ON)
142 abort ();
145 /* Given a bidi TYPE of a character, return its category. */
146 static inline bidi_category_t
147 bidi_get_category (bidi_type_t type)
149 switch (type)
151 case UNKNOWN_BT:
152 return UNKNOWN_BC;
153 case STRONG_L:
154 case STRONG_R:
155 case STRONG_AL:
156 case LRE:
157 case LRO:
158 case RLE:
159 case RLO:
160 return STRONG;
161 case PDF: /* ??? really?? */
162 case WEAK_EN:
163 case WEAK_ES:
164 case WEAK_ET:
165 case WEAK_AN:
166 case WEAK_CS:
167 case WEAK_NSM:
168 case WEAK_BN:
169 return WEAK;
170 case NEUTRAL_B:
171 case NEUTRAL_S:
172 case NEUTRAL_WS:
173 case NEUTRAL_ON:
174 return NEUTRAL;
175 default:
176 abort ();
180 /* Return the mirrored character of C, if it has one. If C has no
181 mirrored counterpart, return C.
182 Note: The conditions in UAX#9 clause L4 regarding the surrounding
183 context must be tested by the caller. */
185 bidi_mirror_char (int c)
187 Lisp_Object val;
189 if (c == BIDI_EOB)
190 return c;
191 if (c < 0 || c > MAX_CHAR)
192 abort ();
194 val = CHAR_TABLE_REF (bidi_mirror_table, c);
195 if (INTEGERP (val))
197 int v = XINT (val);
199 if (v < 0 || v > MAX_CHAR)
200 abort ();
202 return v;
205 return c;
208 /* Determine the start-of-run (sor) directional type given the two
209 embedding levels on either side of the run boundary. Also, update
210 the saved info about previously seen characters, since that info is
211 generally valid for a single level run. */
212 static inline void
213 bidi_set_sor_type (struct bidi_it *bidi_it, int level_before, int level_after)
215 int higher_level = level_before > level_after ? level_before : level_after;
217 /* The prev_was_pdf gork is required for when we have several PDFs
218 in a row. In that case, we want to compute the sor type for the
219 next level run only once: when we see the first PDF. That's
220 because the sor type depends only on the higher of the two levels
221 that we find on the two sides of the level boundary (see UAX#9,
222 clause X10), and so we don't need to know the final embedding
223 level to which we descend after processing all the PDFs. */
224 if (!bidi_it->prev_was_pdf || level_before < level_after)
225 /* FIXME: should the default sor direction be user selectable? */
226 bidi_it->sor = (higher_level & 1) != 0 ? R2L : L2R;
227 if (level_before > level_after)
228 bidi_it->prev_was_pdf = 1;
230 bidi_it->prev.type = UNKNOWN_BT;
231 bidi_it->last_strong.type = bidi_it->last_strong.type_after_w1 =
232 bidi_it->last_strong.orig_type = UNKNOWN_BT;
233 bidi_it->prev_for_neutral.type = bidi_it->sor == R2L ? STRONG_R : STRONG_L;
234 bidi_it->prev_for_neutral.charpos = bidi_it->charpos;
235 bidi_it->prev_for_neutral.bytepos = bidi_it->bytepos;
236 bidi_it->next_for_neutral.type = bidi_it->next_for_neutral.type_after_w1 =
237 bidi_it->next_for_neutral.orig_type = UNKNOWN_BT;
238 bidi_it->ignore_bn_limit = -1; /* meaning it's unknown */
241 /* Push the current embedding level and override status; reset the
242 current level to LEVEL and the current override status to OVERRIDE. */
243 static inline void
244 bidi_push_embedding_level (struct bidi_it *bidi_it,
245 int level, bidi_dir_t override)
247 bidi_it->stack_idx++;
248 xassert (bidi_it->stack_idx < BIDI_MAXLEVEL);
249 bidi_it->level_stack[bidi_it->stack_idx].level = level;
250 bidi_it->level_stack[bidi_it->stack_idx].override = override;
253 /* Pop the embedding level and directional override status from the
254 stack, and return the new level. */
255 static inline int
256 bidi_pop_embedding_level (struct bidi_it *bidi_it)
258 /* UAX#9 says to ignore invalid PDFs. */
259 if (bidi_it->stack_idx > 0)
260 bidi_it->stack_idx--;
261 return bidi_it->level_stack[bidi_it->stack_idx].level;
264 /* Record in SAVED_INFO the information about the current character. */
265 static inline void
266 bidi_remember_char (struct bidi_saved_info *saved_info,
267 struct bidi_it *bidi_it)
269 saved_info->charpos = bidi_it->charpos;
270 saved_info->bytepos = bidi_it->bytepos;
271 saved_info->type = bidi_it->type;
272 bidi_check_type (bidi_it->type);
273 saved_info->type_after_w1 = bidi_it->type_after_w1;
274 bidi_check_type (bidi_it->type_after_w1);
275 saved_info->orig_type = bidi_it->orig_type;
276 bidi_check_type (bidi_it->orig_type);
279 /* Copy the bidi iterator from FROM to TO. To save cycles, this only
280 copies the part of the level stack that is actually in use. */
281 static inline void
282 bidi_copy_it (struct bidi_it *to, struct bidi_it *from)
284 int i;
286 /* Copy everything except the level stack and beyond. */
287 memcpy (to, from, offsetof (struct bidi_it, level_stack[0]));
289 /* Copy the active part of the level stack. */
290 to->level_stack[0] = from->level_stack[0]; /* level zero is always in use */
291 for (i = 1; i <= from->stack_idx; i++)
292 to->level_stack[i] = from->level_stack[i];
296 /***********************************************************************
297 Caching the bidi iterator states
298 ***********************************************************************/
300 #define BIDI_CACHE_CHUNK 200
301 static struct bidi_it *bidi_cache;
302 static ptrdiff_t bidi_cache_size = 0;
303 enum { elsz = sizeof (struct bidi_it) };
304 static ptrdiff_t bidi_cache_idx; /* next unused cache slot */
305 static ptrdiff_t bidi_cache_last_idx; /* slot of last cache hit */
306 static ptrdiff_t bidi_cache_start = 0; /* start of cache for this
307 "stack" level */
309 /* Reset the cache state to the empty state. We only reset the part
310 of the cache relevant to iteration of the current object. Previous
311 objects, which are pushed on the display iterator's stack, are left
312 intact. This is called when the cached information is no more
313 useful for the current iteration, e.g. when we were reseated to a
314 new position on the same object. */
315 static inline void
316 bidi_cache_reset (void)
318 bidi_cache_idx = bidi_cache_start;
319 bidi_cache_last_idx = -1;
322 /* Shrink the cache to its minimal size. Called when we init the bidi
323 iterator for reordering a buffer or a string that does not come
324 from display properties, because that means all the previously
325 cached info is of no further use. */
326 static inline void
327 bidi_cache_shrink (void)
329 if (bidi_cache_size > BIDI_CACHE_CHUNK)
331 bidi_cache_size = BIDI_CACHE_CHUNK;
332 bidi_cache =
333 (struct bidi_it *) xrealloc (bidi_cache, bidi_cache_size * elsz);
335 bidi_cache_reset ();
338 static inline void
339 bidi_cache_fetch_state (ptrdiff_t idx, struct bidi_it *bidi_it)
341 int current_scan_dir = bidi_it->scan_dir;
343 if (idx < bidi_cache_start || idx >= bidi_cache_idx)
344 abort ();
346 bidi_copy_it (bidi_it, &bidi_cache[idx]);
347 bidi_it->scan_dir = current_scan_dir;
348 bidi_cache_last_idx = idx;
351 /* Find a cached state with a given CHARPOS and resolved embedding
352 level less or equal to LEVEL. if LEVEL is -1, disregard the
353 resolved levels in cached states. DIR, if non-zero, means search
354 in that direction from the last cache hit. */
355 static inline ptrdiff_t
356 bidi_cache_search (EMACS_INT charpos, int level, int dir)
358 ptrdiff_t i, i_start;
360 if (bidi_cache_idx > bidi_cache_start)
362 if (bidi_cache_last_idx == -1)
363 bidi_cache_last_idx = bidi_cache_idx - 1;
364 if (charpos < bidi_cache[bidi_cache_last_idx].charpos)
366 dir = -1;
367 i_start = bidi_cache_last_idx - 1;
369 else if (charpos > (bidi_cache[bidi_cache_last_idx].charpos
370 + bidi_cache[bidi_cache_last_idx].nchars - 1))
372 dir = 1;
373 i_start = bidi_cache_last_idx + 1;
375 else if (dir)
376 i_start = bidi_cache_last_idx;
377 else
379 dir = -1;
380 i_start = bidi_cache_idx - 1;
383 if (dir < 0)
385 /* Linear search for now; FIXME! */
386 for (i = i_start; i >= bidi_cache_start; i--)
387 if (bidi_cache[i].charpos <= charpos
388 && charpos < bidi_cache[i].charpos + bidi_cache[i].nchars
389 && (level == -1 || bidi_cache[i].resolved_level <= level))
390 return i;
392 else
394 for (i = i_start; i < bidi_cache_idx; i++)
395 if (bidi_cache[i].charpos <= charpos
396 && charpos < bidi_cache[i].charpos + bidi_cache[i].nchars
397 && (level == -1 || bidi_cache[i].resolved_level <= level))
398 return i;
402 return -1;
405 /* Find a cached state where the resolved level changes to a value
406 that is lower than LEVEL, and return its cache slot index. DIR is
407 the direction to search, starting with the last used cache slot.
408 If DIR is zero, we search backwards from the last occupied cache
409 slot. BEFORE, if non-zero, means return the index of the slot that
410 is ``before'' the level change in the search direction. That is,
411 given the cached levels like this:
413 1122333442211
414 AB C
416 and assuming we are at the position cached at the slot marked with
417 C, searching backwards (DIR = -1) for LEVEL = 2 will return the
418 index of slot B or A, depending whether BEFORE is, respectively,
419 non-zero or zero. */
420 static ptrdiff_t
421 bidi_cache_find_level_change (int level, int dir, int before)
423 if (bidi_cache_idx)
425 ptrdiff_t i = dir ? bidi_cache_last_idx : bidi_cache_idx - 1;
426 int incr = before ? 1 : 0;
428 xassert (!dir || bidi_cache_last_idx >= 0);
430 if (!dir)
431 dir = -1;
432 else if (!incr)
433 i += dir;
435 if (dir < 0)
437 while (i >= bidi_cache_start + incr)
439 if (bidi_cache[i - incr].resolved_level >= 0
440 && bidi_cache[i - incr].resolved_level < level)
441 return i;
442 i--;
445 else
447 while (i < bidi_cache_idx - incr)
449 if (bidi_cache[i + incr].resolved_level >= 0
450 && bidi_cache[i + incr].resolved_level < level)
451 return i;
452 i++;
457 return -1;
460 static inline void
461 bidi_cache_ensure_space (ptrdiff_t idx)
463 /* Enlarge the cache as needed. */
464 if (idx >= bidi_cache_size)
466 ptrdiff_t new_size;
468 /* The bidi cache cannot be larger than the largest Lisp string
469 or buffer. */
470 ptrdiff_t string_or_buffer_bound =
471 max (BUF_BYTES_MAX, STRING_BYTES_BOUND);
473 /* Also, it cannot be larger than what C can represent. */
474 ptrdiff_t c_bound = min (PTRDIFF_MAX, SIZE_MAX) / elsz;
476 if (min (string_or_buffer_bound, c_bound) <= idx)
477 memory_full (SIZE_MAX);
478 new_size = idx - idx % BIDI_CACHE_CHUNK + BIDI_CACHE_CHUNK;
479 bidi_cache = (struct bidi_it *) xrealloc (bidi_cache, new_size * elsz);
480 bidi_cache_size = new_size;
484 static inline void
485 bidi_cache_iterator_state (struct bidi_it *bidi_it, int resolved)
487 ptrdiff_t idx;
489 /* We should never cache on backward scans. */
490 if (bidi_it->scan_dir == -1)
491 abort ();
492 idx = bidi_cache_search (bidi_it->charpos, -1, 1);
494 if (idx < 0)
496 idx = bidi_cache_idx;
497 bidi_cache_ensure_space (idx);
498 /* Character positions should correspond to cache positions 1:1.
499 If we are outside the range of cached positions, the cache is
500 useless and must be reset. */
501 if (idx > bidi_cache_start &&
502 (bidi_it->charpos > (bidi_cache[idx - 1].charpos
503 + bidi_cache[idx - 1].nchars)
504 || bidi_it->charpos < bidi_cache[bidi_cache_start].charpos))
506 bidi_cache_reset ();
507 idx = bidi_cache_start;
509 if (bidi_it->nchars <= 0)
510 abort ();
511 bidi_copy_it (&bidi_cache[idx], bidi_it);
512 if (!resolved)
513 bidi_cache[idx].resolved_level = -1;
515 else
517 /* Copy only the members which could have changed, to avoid
518 costly copying of the entire struct. */
519 bidi_cache[idx].type = bidi_it->type;
520 bidi_check_type (bidi_it->type);
521 bidi_cache[idx].type_after_w1 = bidi_it->type_after_w1;
522 bidi_check_type (bidi_it->type_after_w1);
523 if (resolved)
524 bidi_cache[idx].resolved_level = bidi_it->resolved_level;
525 else
526 bidi_cache[idx].resolved_level = -1;
527 bidi_cache[idx].invalid_levels = bidi_it->invalid_levels;
528 bidi_cache[idx].invalid_rl_levels = bidi_it->invalid_rl_levels;
529 bidi_cache[idx].next_for_neutral = bidi_it->next_for_neutral;
530 bidi_cache[idx].next_for_ws = bidi_it->next_for_ws;
531 bidi_cache[idx].ignore_bn_limit = bidi_it->ignore_bn_limit;
534 bidi_cache_last_idx = idx;
535 if (idx >= bidi_cache_idx)
536 bidi_cache_idx = idx + 1;
539 static inline bidi_type_t
540 bidi_cache_find (EMACS_INT charpos, int level, struct bidi_it *bidi_it)
542 ptrdiff_t i = bidi_cache_search (charpos, level, bidi_it->scan_dir);
544 if (i >= bidi_cache_start)
546 bidi_dir_t current_scan_dir = bidi_it->scan_dir;
548 bidi_copy_it (bidi_it, &bidi_cache[i]);
549 bidi_cache_last_idx = i;
550 /* Don't let scan direction from from the cached state override
551 the current scan direction. */
552 bidi_it->scan_dir = current_scan_dir;
553 return bidi_it->type;
556 return UNKNOWN_BT;
559 static inline int
560 bidi_peek_at_next_level (struct bidi_it *bidi_it)
562 if (bidi_cache_idx == bidi_cache_start || bidi_cache_last_idx == -1)
563 abort ();
564 return bidi_cache[bidi_cache_last_idx + bidi_it->scan_dir].resolved_level;
568 /***********************************************************************
569 Pushing and popping the bidi iterator state
570 ***********************************************************************/
571 /* 5-slot stack for saving the start of the previous level of the
572 cache. xdisp.c maintains a 5-slot stack for its iterator state,
573 and we need the same size of our stack. */
574 static ptrdiff_t bidi_cache_start_stack[IT_STACK_SIZE];
575 static int bidi_cache_sp;
577 /* Push the bidi iterator state in preparation for reordering a
578 different object, e.g. display string found at certain buffer
579 position. Pushing the bidi iterator boils down to saving its
580 entire state on the cache and starting a new cache "stacked" on top
581 of the current cache. */
582 void
583 bidi_push_it (struct bidi_it *bidi_it)
585 /* Save the current iterator state in its entirety after the last
586 used cache slot. */
587 bidi_cache_ensure_space (bidi_cache_idx);
588 memcpy (&bidi_cache[bidi_cache_idx++], bidi_it, sizeof (struct bidi_it));
590 /* Push the current cache start onto the stack. */
591 xassert (bidi_cache_sp < IT_STACK_SIZE);
592 bidi_cache_start_stack[bidi_cache_sp++] = bidi_cache_start;
594 /* Start a new level of cache, and make it empty. */
595 bidi_cache_start = bidi_cache_idx;
596 bidi_cache_last_idx = -1;
599 /* Restore the iterator state saved by bidi_push_it and return the
600 cache to the corresponding state. */
601 void
602 bidi_pop_it (struct bidi_it *bidi_it)
604 if (bidi_cache_start <= 0)
605 abort ();
607 /* Reset the next free cache slot index to what it was before the
608 call to bidi_push_it. */
609 bidi_cache_idx = bidi_cache_start - 1;
611 /* Restore the bidi iterator state saved in the cache. */
612 memcpy (bidi_it, &bidi_cache[bidi_cache_idx], sizeof (struct bidi_it));
614 /* Pop the previous cache start from the stack. */
615 if (bidi_cache_sp <= 0)
616 abort ();
617 bidi_cache_start = bidi_cache_start_stack[--bidi_cache_sp];
619 /* Invalidate the last-used cache slot data. */
620 bidi_cache_last_idx = -1;
623 ptrdiff_t bidi_cache_total_alloc;
625 /* Stash away a copy of the cache and its control variables. */
626 void *
627 bidi_shelve_cache (void)
629 unsigned char *databuf;
631 /* Empty cache. */
632 if (bidi_cache_idx == 0)
633 return NULL;
635 databuf = xmalloc (sizeof (bidi_cache_idx)
636 + bidi_cache_idx * sizeof (struct bidi_it)
637 + sizeof (bidi_cache_start_stack)
638 + sizeof (bidi_cache_sp) + sizeof (bidi_cache_start)
639 + sizeof (bidi_cache_last_idx));
640 bidi_cache_total_alloc +=
641 sizeof (bidi_cache_idx) + bidi_cache_idx * sizeof (struct bidi_it)
642 + sizeof (bidi_cache_start_stack)
643 + sizeof (bidi_cache_sp) + sizeof (bidi_cache_start)
644 + sizeof (bidi_cache_last_idx);
646 memcpy (databuf, &bidi_cache_idx, sizeof (bidi_cache_idx));
647 memcpy (databuf + sizeof (bidi_cache_idx),
648 bidi_cache, bidi_cache_idx * sizeof (struct bidi_it));
649 memcpy (databuf + sizeof (bidi_cache_idx)
650 + bidi_cache_idx * sizeof (struct bidi_it),
651 bidi_cache_start_stack, sizeof (bidi_cache_start_stack));
652 memcpy (databuf + sizeof (bidi_cache_idx)
653 + bidi_cache_idx * sizeof (struct bidi_it)
654 + sizeof (bidi_cache_start_stack),
655 &bidi_cache_sp, sizeof (bidi_cache_sp));
656 memcpy (databuf + sizeof (bidi_cache_idx)
657 + bidi_cache_idx * sizeof (struct bidi_it)
658 + sizeof (bidi_cache_start_stack) + sizeof (bidi_cache_sp),
659 &bidi_cache_start, sizeof (bidi_cache_start));
660 memcpy (databuf + sizeof (bidi_cache_idx)
661 + bidi_cache_idx * sizeof (struct bidi_it)
662 + sizeof (bidi_cache_start_stack) + sizeof (bidi_cache_sp)
663 + sizeof (bidi_cache_start),
664 &bidi_cache_last_idx, sizeof (bidi_cache_last_idx));
666 return databuf;
669 /* Restore the cache state from a copy stashed away by bidi_shelve_cache. */
670 void
671 bidi_unshelve_cache (void *databuf, int just_free)
673 unsigned char *p = databuf;
675 if (!p)
677 /* A NULL pointer means an empty cache. */
678 bidi_cache_start = 0;
679 bidi_cache_sp = 0;
680 bidi_cache_reset ();
682 else
684 if (just_free)
686 ptrdiff_t idx;
688 memcpy (&idx, p, sizeof (bidi_cache_idx));
689 bidi_cache_total_alloc -=
690 sizeof (bidi_cache_idx) + idx * sizeof (struct bidi_it)
691 + sizeof (bidi_cache_start_stack) + sizeof (bidi_cache_sp)
692 + sizeof (bidi_cache_start) + sizeof (bidi_cache_last_idx);
694 else
696 memcpy (&bidi_cache_idx, p, sizeof (bidi_cache_idx));
697 bidi_cache_ensure_space (bidi_cache_idx);
698 memcpy (bidi_cache, p + sizeof (bidi_cache_idx),
699 bidi_cache_idx * sizeof (struct bidi_it));
700 memcpy (bidi_cache_start_stack,
701 p + sizeof (bidi_cache_idx)
702 + bidi_cache_idx * sizeof (struct bidi_it),
703 sizeof (bidi_cache_start_stack));
704 memcpy (&bidi_cache_sp,
705 p + sizeof (bidi_cache_idx)
706 + bidi_cache_idx * sizeof (struct bidi_it)
707 + sizeof (bidi_cache_start_stack),
708 sizeof (bidi_cache_sp));
709 memcpy (&bidi_cache_start,
710 p + sizeof (bidi_cache_idx)
711 + bidi_cache_idx * sizeof (struct bidi_it)
712 + sizeof (bidi_cache_start_stack) + sizeof (bidi_cache_sp),
713 sizeof (bidi_cache_start));
714 memcpy (&bidi_cache_last_idx,
715 p + sizeof (bidi_cache_idx)
716 + bidi_cache_idx * sizeof (struct bidi_it)
717 + sizeof (bidi_cache_start_stack) + sizeof (bidi_cache_sp)
718 + sizeof (bidi_cache_start),
719 sizeof (bidi_cache_last_idx));
720 bidi_cache_total_alloc -=
721 sizeof (bidi_cache_idx) + bidi_cache_idx * sizeof (struct bidi_it)
722 + sizeof (bidi_cache_start_stack) + sizeof (bidi_cache_sp)
723 + sizeof (bidi_cache_start) + sizeof (bidi_cache_last_idx);
726 xfree (p);
731 /***********************************************************************
732 Initialization
733 ***********************************************************************/
734 static void
735 bidi_initialize (void)
738 #include "biditype.h"
739 #include "bidimirror.h"
741 int i;
743 bidi_type_table = Fmake_char_table (Qnil, make_number (STRONG_L));
744 staticpro (&bidi_type_table);
746 for (i = 0; i < sizeof bidi_type / sizeof bidi_type[0]; i++)
747 char_table_set_range (bidi_type_table, bidi_type[i].from, bidi_type[i].to,
748 make_number (bidi_type[i].type));
750 bidi_mirror_table = Fmake_char_table (Qnil, Qnil);
751 staticpro (&bidi_mirror_table);
753 for (i = 0; i < sizeof bidi_mirror / sizeof bidi_mirror[0]; i++)
754 char_table_set (bidi_mirror_table, bidi_mirror[i].from,
755 make_number (bidi_mirror[i].to));
757 Qparagraph_start = intern ("paragraph-start");
758 staticpro (&Qparagraph_start);
759 paragraph_start_re = Fsymbol_value (Qparagraph_start);
760 if (!STRINGP (paragraph_start_re))
761 paragraph_start_re = build_string ("\f\\|[ \t]*$");
762 staticpro (&paragraph_start_re);
763 Qparagraph_separate = intern ("paragraph-separate");
764 staticpro (&Qparagraph_separate);
765 paragraph_separate_re = Fsymbol_value (Qparagraph_separate);
766 if (!STRINGP (paragraph_separate_re))
767 paragraph_separate_re = build_string ("[ \t\f]*$");
768 staticpro (&paragraph_separate_re);
770 bidi_cache_sp = 0;
772 bidi_initialized = 1;
775 /* Do whatever UAX#9 clause X8 says should be done at paragraph's
776 end. */
777 static inline void
778 bidi_set_paragraph_end (struct bidi_it *bidi_it)
780 bidi_it->invalid_levels = 0;
781 bidi_it->invalid_rl_levels = -1;
782 bidi_it->stack_idx = 0;
783 bidi_it->resolved_level = bidi_it->level_stack[0].level;
786 /* Initialize the bidi iterator from buffer/string position CHARPOS. */
787 void
788 bidi_init_it (EMACS_INT charpos, EMACS_INT bytepos, int frame_window_p,
789 struct bidi_it *bidi_it)
791 if (! bidi_initialized)
792 bidi_initialize ();
793 if (charpos >= 0)
794 bidi_it->charpos = charpos;
795 if (bytepos >= 0)
796 bidi_it->bytepos = bytepos;
797 bidi_it->frame_window_p = frame_window_p;
798 bidi_it->nchars = -1; /* to be computed in bidi_resolve_explicit_1 */
799 bidi_it->first_elt = 1;
800 bidi_set_paragraph_end (bidi_it);
801 bidi_it->new_paragraph = 1;
802 bidi_it->separator_limit = -1;
803 bidi_it->type = NEUTRAL_B;
804 bidi_it->type_after_w1 = NEUTRAL_B;
805 bidi_it->orig_type = NEUTRAL_B;
806 bidi_it->prev_was_pdf = 0;
807 bidi_it->prev.type = bidi_it->prev.type_after_w1 =
808 bidi_it->prev.orig_type = UNKNOWN_BT;
809 bidi_it->last_strong.type = bidi_it->last_strong.type_after_w1 =
810 bidi_it->last_strong.orig_type = UNKNOWN_BT;
811 bidi_it->next_for_neutral.charpos = -1;
812 bidi_it->next_for_neutral.type =
813 bidi_it->next_for_neutral.type_after_w1 =
814 bidi_it->next_for_neutral.orig_type = UNKNOWN_BT;
815 bidi_it->prev_for_neutral.charpos = -1;
816 bidi_it->prev_for_neutral.type =
817 bidi_it->prev_for_neutral.type_after_w1 =
818 bidi_it->prev_for_neutral.orig_type = UNKNOWN_BT;
819 bidi_it->sor = L2R; /* FIXME: should it be user-selectable? */
820 bidi_it->disp_pos = -1; /* invalid/unknown */
821 bidi_it->disp_prop_p = 0;
822 /* We can only shrink the cache if we are at the bottom level of its
823 "stack". */
824 if (bidi_cache_start == 0)
825 bidi_cache_shrink ();
826 else
827 bidi_cache_reset ();
830 /* Perform initializations for reordering a new line of bidi text. */
831 static void
832 bidi_line_init (struct bidi_it *bidi_it)
834 bidi_it->scan_dir = 1; /* FIXME: do we need to have control on this? */
835 bidi_it->resolved_level = bidi_it->level_stack[0].level;
836 bidi_it->level_stack[0].override = NEUTRAL_DIR; /* X1 */
837 bidi_it->invalid_levels = 0;
838 bidi_it->invalid_rl_levels = -1;
839 bidi_it->next_en_pos = -1;
840 bidi_it->next_for_ws.type = UNKNOWN_BT;
841 bidi_set_sor_type (bidi_it,
842 bidi_it->paragraph_dir == R2L ? 1 : 0,
843 bidi_it->level_stack[0].level); /* X10 */
845 bidi_cache_reset ();
849 /***********************************************************************
850 Fetching characters
851 ***********************************************************************/
853 /* Count bytes in string S between BEG/BEGBYTE and END. BEG and END
854 are zero-based character positions in S, BEGBYTE is byte position
855 corresponding to BEG. UNIBYTE, if non-zero, means S is a unibyte
856 string. */
857 static inline EMACS_INT
858 bidi_count_bytes (const unsigned char *s, const EMACS_INT beg,
859 const EMACS_INT begbyte, const EMACS_INT end, int unibyte)
861 EMACS_INT pos = beg;
862 const unsigned char *p = s + begbyte, *start = p;
864 if (unibyte)
865 p = s + end;
866 else
868 if (!CHAR_HEAD_P (*p))
869 abort ();
871 while (pos < end)
873 p += BYTES_BY_CHAR_HEAD (*p);
874 pos++;
878 return p - start;
881 /* Fetch and returns the character at byte position BYTEPOS. If S is
882 non-NULL, fetch the character from string S; otherwise fetch the
883 character from the current buffer. UNIBYTE non-zero means S is a
884 unibyte string. */
885 static inline int
886 bidi_char_at_pos (EMACS_INT bytepos, const unsigned char *s, int unibyte)
888 if (s)
890 if (unibyte)
891 return s[bytepos];
892 else
893 return STRING_CHAR (s + bytepos);
895 else
896 return FETCH_MULTIBYTE_CHAR (bytepos);
899 /* Fetch and return the character at BYTEPOS/CHARPOS. If that
900 character is covered by a display string, treat the entire run of
901 covered characters as a single character u+FFFC, and return their
902 combined length in CH_LEN and NCHARS. DISP_POS specifies the
903 character position of the next display string, or -1 if not yet
904 computed. DISP_PROP_P non-zero means that there's really a display
905 string at DISP_POS, as opposed to when we searched till DISP_POS
906 without findingone. When the next character is at or beyond that
907 position, the function updates DISP_POS with the position of the
908 next display string. STRING->s is the C string to iterate, or NULL
909 if iterating over a buffer or a Lisp string; in the latter case,
910 STRING->lstring is the Lisp string. */
911 static inline int
912 bidi_fetch_char (EMACS_INT bytepos, EMACS_INT charpos, EMACS_INT *disp_pos,
913 int *disp_prop_p, struct bidi_string_data *string,
914 int frame_window_p, EMACS_INT *ch_len, EMACS_INT *nchars)
916 int ch;
917 EMACS_INT endpos =
918 (string->s || STRINGP (string->lstring)) ? string->schars : ZV;
919 struct text_pos pos;
921 /* If we got past the last known position of display string, compute
922 the position of the next one. That position could be at CHARPOS. */
923 if (charpos < endpos && charpos > *disp_pos)
925 SET_TEXT_POS (pos, charpos, bytepos);
926 *disp_pos = compute_display_string_pos (&pos, string, frame_window_p,
927 disp_prop_p);
930 /* Fetch the character at BYTEPOS. */
931 if (charpos >= endpos)
933 ch = BIDI_EOB;
934 *ch_len = 1;
935 *nchars = 1;
936 *disp_pos = endpos;
937 *disp_prop_p = 0;
939 else if (charpos >= *disp_pos && *disp_prop_p)
941 EMACS_INT disp_end_pos;
943 /* We don't expect to find ourselves in the middle of a display
944 property. Hopefully, it will never be needed. */
945 if (charpos > *disp_pos)
946 abort ();
947 /* Return the Unicode Object Replacement Character to represent
948 the entire run of characters covered by the display string. */
949 ch = 0xFFFC;
950 disp_end_pos = compute_display_string_end (*disp_pos, string);
951 *nchars = disp_end_pos - *disp_pos;
952 if (*nchars <= 0)
953 abort ();
954 if (string->s)
955 *ch_len = bidi_count_bytes (string->s, *disp_pos, bytepos,
956 disp_end_pos, string->unibyte);
957 else if (STRINGP (string->lstring))
958 *ch_len = bidi_count_bytes (SDATA (string->lstring), *disp_pos,
959 bytepos, disp_end_pos, string->unibyte);
960 else
961 *ch_len = CHAR_TO_BYTE (disp_end_pos) - bytepos;
963 else
965 if (string->s)
967 int len;
969 if (!string->unibyte)
971 ch = STRING_CHAR_AND_LENGTH (string->s + bytepos, len);
972 *ch_len = len;
974 else
976 ch = UNIBYTE_TO_CHAR (string->s[bytepos]);
977 *ch_len = 1;
980 else if (STRINGP (string->lstring))
982 int len;
984 if (!string->unibyte)
986 ch = STRING_CHAR_AND_LENGTH (SDATA (string->lstring) + bytepos,
987 len);
988 *ch_len = len;
990 else
992 ch = UNIBYTE_TO_CHAR (SREF (string->lstring, bytepos));
993 *ch_len = 1;
996 else
998 ch = FETCH_MULTIBYTE_CHAR (bytepos);
999 *ch_len = CHAR_BYTES (ch);
1001 *nchars = 1;
1004 /* If we just entered a run of characters covered by a display
1005 string, compute the position of the next display string. */
1006 if (charpos + *nchars <= endpos && charpos + *nchars > *disp_pos
1007 && *disp_prop_p)
1009 SET_TEXT_POS (pos, charpos + *nchars, bytepos + *ch_len);
1010 *disp_pos = compute_display_string_pos (&pos, string, frame_window_p,
1011 disp_prop_p);
1014 return ch;
1018 /***********************************************************************
1019 Determining paragraph direction
1020 ***********************************************************************/
1022 /* Check if buffer position CHARPOS/BYTEPOS is the end of a paragraph.
1023 Value is the non-negative length of the paragraph separator
1024 following the buffer position, -1 if position is at the beginning
1025 of a new paragraph, or -2 if position is neither at beginning nor
1026 at end of a paragraph. */
1027 static EMACS_INT
1028 bidi_at_paragraph_end (EMACS_INT charpos, EMACS_INT bytepos)
1030 Lisp_Object sep_re;
1031 Lisp_Object start_re;
1032 EMACS_INT val;
1034 sep_re = paragraph_separate_re;
1035 start_re = paragraph_start_re;
1037 val = fast_looking_at (sep_re, charpos, bytepos, ZV, ZV_BYTE, Qnil);
1038 if (val < 0)
1040 if (fast_looking_at (start_re, charpos, bytepos, ZV, ZV_BYTE, Qnil) >= 0)
1041 val = -1;
1042 else
1043 val = -2;
1046 return val;
1049 /* Find the beginning of this paragraph by looking back in the buffer.
1050 Value is the byte position of the paragraph's beginning. */
1051 static EMACS_INT
1052 bidi_find_paragraph_start (EMACS_INT pos, EMACS_INT pos_byte)
1054 Lisp_Object re = paragraph_start_re;
1055 EMACS_INT limit = ZV, limit_byte = ZV_BYTE;
1057 while (pos_byte > BEGV_BYTE
1058 && fast_looking_at (re, pos, pos_byte, limit, limit_byte, Qnil) < 0)
1060 /* FIXME: What if the paragraph beginning is covered by a
1061 display string? And what if a display string covering some
1062 of the text over which we scan back includes
1063 paragraph_start_re? */
1064 pos = find_next_newline_no_quit (pos - 1, -1);
1065 pos_byte = CHAR_TO_BYTE (pos);
1067 return pos_byte;
1070 /* Determine the base direction, a.k.a. base embedding level, of the
1071 paragraph we are about to iterate through. If DIR is either L2R or
1072 R2L, just use that. Otherwise, determine the paragraph direction
1073 from the first strong directional character of the paragraph.
1075 NO_DEFAULT_P non-zero means don't default to L2R if the paragraph
1076 has no strong directional characters and both DIR and
1077 bidi_it->paragraph_dir are NEUTRAL_DIR. In that case, search back
1078 in the buffer until a paragraph is found with a strong character,
1079 or until hitting BEGV. In the latter case, fall back to L2R. This
1080 flag is used in current-bidi-paragraph-direction.
1082 Note that this function gives the paragraph separator the same
1083 direction as the preceding paragraph, even though Emacs generally
1084 views the separartor as not belonging to any paragraph. */
1085 void
1086 bidi_paragraph_init (bidi_dir_t dir, struct bidi_it *bidi_it, int no_default_p)
1088 EMACS_INT bytepos = bidi_it->bytepos;
1089 int string_p = bidi_it->string.s != NULL || STRINGP (bidi_it->string.lstring);
1090 EMACS_INT pstartbyte;
1091 /* Note that begbyte is a byte position, while end is a character
1092 position. Yes, this is ugly, but we are trying to avoid costly
1093 calls to BYTE_TO_CHAR and its ilk. */
1094 EMACS_INT begbyte = string_p ? 0 : BEGV_BYTE;
1095 EMACS_INT end = string_p ? bidi_it->string.schars : ZV;
1097 /* Special case for an empty buffer. */
1098 if (bytepos == begbyte && bidi_it->charpos == end)
1099 dir = L2R;
1100 /* We should never be called at EOB or before BEGV. */
1101 else if (bidi_it->charpos >= end || bytepos < begbyte)
1102 abort ();
1104 if (dir == L2R)
1106 bidi_it->paragraph_dir = L2R;
1107 bidi_it->new_paragraph = 0;
1109 else if (dir == R2L)
1111 bidi_it->paragraph_dir = R2L;
1112 bidi_it->new_paragraph = 0;
1114 else if (dir == NEUTRAL_DIR) /* P2 */
1116 int ch;
1117 EMACS_INT ch_len, nchars;
1118 EMACS_INT pos, disp_pos = -1;
1119 int disp_prop_p = 0;
1120 bidi_type_t type;
1121 const unsigned char *s;
1123 if (!bidi_initialized)
1124 bidi_initialize ();
1126 /* If we are inside a paragraph separator, we are just waiting
1127 for the separator to be exhausted; use the previous paragraph
1128 direction. But don't do that if we have been just reseated,
1129 because we need to reinitialize below in that case. */
1130 if (!bidi_it->first_elt
1131 && bidi_it->charpos < bidi_it->separator_limit)
1132 return;
1134 /* If we are on a newline, get past it to where the next
1135 paragraph might start. But don't do that at BEGV since then
1136 we are potentially in a new paragraph that doesn't yet
1137 exist. */
1138 pos = bidi_it->charpos;
1139 s = STRINGP (bidi_it->string.lstring) ?
1140 SDATA (bidi_it->string.lstring) : bidi_it->string.s;
1141 if (bytepos > begbyte
1142 && bidi_char_at_pos (bytepos, s, bidi_it->string.unibyte) == '\n')
1144 bytepos++;
1145 pos++;
1148 /* We are either at the beginning of a paragraph or in the
1149 middle of it. Find where this paragraph starts. */
1150 if (string_p)
1152 /* We don't support changes of paragraph direction inside a
1153 string. It is treated as a single paragraph. */
1154 pstartbyte = 0;
1156 else
1157 pstartbyte = bidi_find_paragraph_start (pos, bytepos);
1158 bidi_it->separator_limit = -1;
1159 bidi_it->new_paragraph = 0;
1161 /* The following loop is run more than once only if NO_DEFAULT_P
1162 is non-zero, and only if we are iterating on a buffer. */
1163 do {
1164 bytepos = pstartbyte;
1165 if (!string_p)
1166 pos = BYTE_TO_CHAR (bytepos);
1167 ch = bidi_fetch_char (bytepos, pos, &disp_pos, &disp_prop_p,
1168 &bidi_it->string,
1169 bidi_it->frame_window_p, &ch_len, &nchars);
1170 type = bidi_get_type (ch, NEUTRAL_DIR);
1172 for (pos += nchars, bytepos += ch_len;
1173 /* NOTE: UAX#9 says to search only for L, AL, or R types
1174 of characters, and ignore RLE, RLO, LRE, and LRO.
1175 However, I'm not sure it makes sense to omit those 4;
1176 should try with and without that to see the effect. */
1177 (bidi_get_category (type) != STRONG)
1178 || (bidi_ignore_explicit_marks_for_paragraph_level
1179 && (type == RLE || type == RLO
1180 || type == LRE || type == LRO));
1181 type = bidi_get_type (ch, NEUTRAL_DIR))
1183 if (pos >= end)
1185 /* Pretend there's a paragraph separator at end of
1186 buffer/string. */
1187 type = NEUTRAL_B;
1188 break;
1190 if (!string_p
1191 && type == NEUTRAL_B
1192 && bidi_at_paragraph_end (pos, bytepos) >= -1)
1193 break;
1194 /* Fetch next character and advance to get past it. */
1195 ch = bidi_fetch_char (bytepos, pos, &disp_pos,
1196 &disp_prop_p, &bidi_it->string,
1197 bidi_it->frame_window_p, &ch_len, &nchars);
1198 pos += nchars;
1199 bytepos += ch_len;
1201 if (type == STRONG_R || type == STRONG_AL) /* P3 */
1202 bidi_it->paragraph_dir = R2L;
1203 else if (type == STRONG_L)
1204 bidi_it->paragraph_dir = L2R;
1205 if (!string_p
1206 && no_default_p && bidi_it->paragraph_dir == NEUTRAL_DIR)
1208 /* If this paragraph is at BEGV, default to L2R. */
1209 if (pstartbyte == BEGV_BYTE)
1210 bidi_it->paragraph_dir = L2R; /* P3 and HL1 */
1211 else
1213 EMACS_INT prevpbyte = pstartbyte;
1214 EMACS_INT p = BYTE_TO_CHAR (pstartbyte), pbyte = pstartbyte;
1216 /* Find the beginning of the previous paragraph, if any. */
1217 while (pbyte > BEGV_BYTE && prevpbyte >= pstartbyte)
1219 /* FXIME: What if p is covered by a display
1220 string? See also a FIXME inside
1221 bidi_find_paragraph_start. */
1222 p--;
1223 pbyte = CHAR_TO_BYTE (p);
1224 prevpbyte = bidi_find_paragraph_start (p, pbyte);
1226 pstartbyte = prevpbyte;
1229 } while (!string_p
1230 && no_default_p && bidi_it->paragraph_dir == NEUTRAL_DIR);
1232 else
1233 abort ();
1235 /* Contrary to UAX#9 clause P3, we only default the paragraph
1236 direction to L2R if we have no previous usable paragraph
1237 direction. This is allowed by the HL1 clause. */
1238 if (bidi_it->paragraph_dir != L2R && bidi_it->paragraph_dir != R2L)
1239 bidi_it->paragraph_dir = L2R; /* P3 and HL1 ``higher-level protocols'' */
1240 if (bidi_it->paragraph_dir == R2L)
1241 bidi_it->level_stack[0].level = 1;
1242 else
1243 bidi_it->level_stack[0].level = 0;
1245 bidi_line_init (bidi_it);
1249 /***********************************************************************
1250 Resolving explicit and implicit levels.
1251 The rest of this file constitutes the core of the UBA implementation.
1252 ***********************************************************************/
1254 static inline int
1255 bidi_explicit_dir_char (int ch)
1257 bidi_type_t ch_type;
1259 if (!bidi_initialized)
1260 abort ();
1261 ch_type = (bidi_type_t) XINT (CHAR_TABLE_REF (bidi_type_table, ch));
1262 return (ch_type == LRE || ch_type == LRO
1263 || ch_type == RLE || ch_type == RLO
1264 || ch_type == PDF);
1267 /* A helper function for bidi_resolve_explicit. It advances to the
1268 next character in logical order and determines the new embedding
1269 level and directional override, but does not take into account
1270 empty embeddings. */
1271 static int
1272 bidi_resolve_explicit_1 (struct bidi_it *bidi_it)
1274 int curchar;
1275 bidi_type_t type;
1276 int current_level;
1277 int new_level;
1278 bidi_dir_t override;
1279 int string_p = bidi_it->string.s != NULL || STRINGP (bidi_it->string.lstring);
1281 /* If reseat()'ed, don't advance, so as to start iteration from the
1282 position where we were reseated. bidi_it->bytepos can be less
1283 than BEGV_BYTE after reseat to BEGV. */
1284 if (bidi_it->bytepos < (string_p ? 0 : BEGV_BYTE)
1285 || bidi_it->first_elt)
1287 bidi_it->first_elt = 0;
1288 if (string_p)
1290 const unsigned char *p =
1291 STRINGP (bidi_it->string.lstring)
1292 ? SDATA (bidi_it->string.lstring) : bidi_it->string.s;
1294 if (bidi_it->charpos < 0)
1295 bidi_it->charpos = 0;
1296 bidi_it->bytepos = bidi_count_bytes (p, 0, 0, bidi_it->charpos,
1297 bidi_it->string.unibyte);
1299 else
1301 if (bidi_it->charpos < BEGV)
1302 bidi_it->charpos = BEGV;
1303 bidi_it->bytepos = CHAR_TO_BYTE (bidi_it->charpos);
1306 /* Don't move at end of buffer/string. */
1307 else if (bidi_it->charpos < (string_p ? bidi_it->string.schars : ZV))
1309 /* Advance to the next character, skipping characters covered by
1310 display strings (nchars > 1). */
1311 if (bidi_it->nchars <= 0)
1312 abort ();
1313 bidi_it->charpos += bidi_it->nchars;
1314 if (bidi_it->ch_len == 0)
1315 abort ();
1316 bidi_it->bytepos += bidi_it->ch_len;
1319 current_level = bidi_it->level_stack[bidi_it->stack_idx].level; /* X1 */
1320 override = bidi_it->level_stack[bidi_it->stack_idx].override;
1321 new_level = current_level;
1323 if (bidi_it->charpos >= (string_p ? bidi_it->string.schars : ZV))
1325 curchar = BIDI_EOB;
1326 bidi_it->ch_len = 1;
1327 bidi_it->nchars = 1;
1328 bidi_it->disp_pos = (string_p ? bidi_it->string.schars : ZV);
1329 bidi_it->disp_prop_p = 0;
1331 else
1333 /* Fetch the character at BYTEPOS. If it is covered by a
1334 display string, treat the entire run of covered characters as
1335 a single character u+FFFC. */
1336 curchar = bidi_fetch_char (bidi_it->bytepos, bidi_it->charpos,
1337 &bidi_it->disp_pos, &bidi_it->disp_prop_p,
1338 &bidi_it->string, bidi_it->frame_window_p,
1339 &bidi_it->ch_len, &bidi_it->nchars);
1341 bidi_it->ch = curchar;
1343 /* Don't apply directional override here, as all the types we handle
1344 below will not be affected by the override anyway, and we need
1345 the original type unaltered. The override will be applied in
1346 bidi_resolve_weak. */
1347 type = bidi_get_type (curchar, NEUTRAL_DIR);
1348 bidi_it->orig_type = type;
1349 bidi_check_type (bidi_it->orig_type);
1351 if (type != PDF)
1352 bidi_it->prev_was_pdf = 0;
1354 bidi_it->type_after_w1 = UNKNOWN_BT;
1356 switch (type)
1358 case RLE: /* X2 */
1359 case RLO: /* X4 */
1360 bidi_it->type_after_w1 = type;
1361 bidi_check_type (bidi_it->type_after_w1);
1362 type = WEAK_BN; /* X9/Retaining */
1363 if (bidi_it->ignore_bn_limit <= -1)
1365 if (current_level <= BIDI_MAXLEVEL - 4)
1367 /* Compute the least odd embedding level greater than
1368 the current level. */
1369 new_level = ((current_level + 1) & ~1) + 1;
1370 if (bidi_it->type_after_w1 == RLE)
1371 override = NEUTRAL_DIR;
1372 else
1373 override = R2L;
1374 if (current_level == BIDI_MAXLEVEL - 4)
1375 bidi_it->invalid_rl_levels = 0;
1376 bidi_push_embedding_level (bidi_it, new_level, override);
1378 else
1380 bidi_it->invalid_levels++;
1381 /* See the commentary about invalid_rl_levels below. */
1382 if (bidi_it->invalid_rl_levels < 0)
1383 bidi_it->invalid_rl_levels = 0;
1384 bidi_it->invalid_rl_levels++;
1387 else if (bidi_it->prev.type_after_w1 == WEAK_EN /* W5/Retaining */
1388 || bidi_it->next_en_pos > bidi_it->charpos)
1389 type = WEAK_EN;
1390 break;
1391 case LRE: /* X3 */
1392 case LRO: /* X5 */
1393 bidi_it->type_after_w1 = type;
1394 bidi_check_type (bidi_it->type_after_w1);
1395 type = WEAK_BN; /* X9/Retaining */
1396 if (bidi_it->ignore_bn_limit <= -1)
1398 if (current_level <= BIDI_MAXLEVEL - 5)
1400 /* Compute the least even embedding level greater than
1401 the current level. */
1402 new_level = ((current_level + 2) & ~1);
1403 if (bidi_it->type_after_w1 == LRE)
1404 override = NEUTRAL_DIR;
1405 else
1406 override = L2R;
1407 bidi_push_embedding_level (bidi_it, new_level, override);
1409 else
1411 bidi_it->invalid_levels++;
1412 /* invalid_rl_levels counts invalid levels encountered
1413 while the embedding level was already too high for
1414 LRE/LRO, but not for RLE/RLO. That is because
1415 there may be exactly one PDF which we should not
1416 ignore even though invalid_levels is non-zero.
1417 invalid_rl_levels helps to know what PDF is
1418 that. */
1419 if (bidi_it->invalid_rl_levels >= 0)
1420 bidi_it->invalid_rl_levels++;
1423 else if (bidi_it->prev.type_after_w1 == WEAK_EN /* W5/Retaining */
1424 || bidi_it->next_en_pos > bidi_it->charpos)
1425 type = WEAK_EN;
1426 break;
1427 case PDF: /* X7 */
1428 bidi_it->type_after_w1 = type;
1429 bidi_check_type (bidi_it->type_after_w1);
1430 type = WEAK_BN; /* X9/Retaining */
1431 if (bidi_it->ignore_bn_limit <= -1)
1433 if (!bidi_it->invalid_rl_levels)
1435 new_level = bidi_pop_embedding_level (bidi_it);
1436 bidi_it->invalid_rl_levels = -1;
1437 if (bidi_it->invalid_levels)
1438 bidi_it->invalid_levels--;
1439 /* else nothing: UAX#9 says to ignore invalid PDFs */
1441 if (!bidi_it->invalid_levels)
1442 new_level = bidi_pop_embedding_level (bidi_it);
1443 else
1445 bidi_it->invalid_levels--;
1446 bidi_it->invalid_rl_levels--;
1449 else if (bidi_it->prev.type_after_w1 == WEAK_EN /* W5/Retaining */
1450 || bidi_it->next_en_pos > bidi_it->charpos)
1451 type = WEAK_EN;
1452 break;
1453 default:
1454 /* Nothing. */
1455 break;
1458 bidi_it->type = type;
1459 bidi_check_type (bidi_it->type);
1461 return new_level;
1464 /* Given an iterator state in BIDI_IT, advance one character position
1465 in the buffer/string to the next character (in the logical order),
1466 resolve any explicit embeddings and directional overrides, and
1467 return the embedding level of the character after resolving
1468 explicit directives and ignoring empty embeddings. */
1469 static int
1470 bidi_resolve_explicit (struct bidi_it *bidi_it)
1472 int prev_level = bidi_it->level_stack[bidi_it->stack_idx].level;
1473 int new_level = bidi_resolve_explicit_1 (bidi_it);
1474 EMACS_INT eob = bidi_it->string.s ? bidi_it->string.schars : ZV;
1475 const unsigned char *s = STRINGP (bidi_it->string.lstring)
1476 ? SDATA (bidi_it->string.lstring) : bidi_it->string.s;
1478 if (prev_level < new_level
1479 && bidi_it->type == WEAK_BN
1480 && bidi_it->ignore_bn_limit == -1 /* only if not already known */
1481 && bidi_it->charpos < eob /* not already at EOB */
1482 && bidi_explicit_dir_char (bidi_char_at_pos (bidi_it->bytepos
1483 + bidi_it->ch_len, s,
1484 bidi_it->string.unibyte)))
1486 /* Avoid pushing and popping embedding levels if the level run
1487 is empty, as this breaks level runs where it shouldn't.
1488 UAX#9 removes all the explicit embedding and override codes,
1489 so empty embeddings disappear without a trace. We need to
1490 behave as if we did the same. */
1491 struct bidi_it saved_it;
1492 int level = prev_level;
1494 bidi_copy_it (&saved_it, bidi_it);
1496 while (bidi_explicit_dir_char (bidi_char_at_pos (bidi_it->bytepos
1497 + bidi_it->ch_len, s,
1498 bidi_it->string.unibyte)))
1500 /* This advances to the next character, skipping any
1501 characters covered by display strings. */
1502 level = bidi_resolve_explicit_1 (bidi_it);
1503 /* If string.lstring was relocated inside bidi_resolve_explicit_1,
1504 a pointer to its data is no longer valid. */
1505 if (STRINGP (bidi_it->string.lstring))
1506 s = SDATA (bidi_it->string.lstring);
1509 if (bidi_it->nchars <= 0)
1510 abort ();
1511 if (level == prev_level) /* empty embedding */
1512 saved_it.ignore_bn_limit = bidi_it->charpos + bidi_it->nchars;
1513 else /* this embedding is non-empty */
1514 saved_it.ignore_bn_limit = -2;
1516 bidi_copy_it (bidi_it, &saved_it);
1517 if (bidi_it->ignore_bn_limit > -1)
1519 /* We pushed a level, but we shouldn't have. Undo that. */
1520 if (!bidi_it->invalid_rl_levels)
1522 new_level = bidi_pop_embedding_level (bidi_it);
1523 bidi_it->invalid_rl_levels = -1;
1524 if (bidi_it->invalid_levels)
1525 bidi_it->invalid_levels--;
1527 if (!bidi_it->invalid_levels)
1528 new_level = bidi_pop_embedding_level (bidi_it);
1529 else
1531 bidi_it->invalid_levels--;
1532 bidi_it->invalid_rl_levels--;
1537 if (bidi_it->type == NEUTRAL_B) /* X8 */
1539 bidi_set_paragraph_end (bidi_it);
1540 /* This is needed by bidi_resolve_weak below, and in L1. */
1541 bidi_it->type_after_w1 = bidi_it->type;
1542 bidi_check_type (bidi_it->type_after_w1);
1545 return new_level;
1548 /* Advance in the buffer/string, resolve weak types and return the
1549 type of the next character after weak type resolution. */
1550 static bidi_type_t
1551 bidi_resolve_weak (struct bidi_it *bidi_it)
1553 bidi_type_t type;
1554 bidi_dir_t override;
1555 int prev_level = bidi_it->level_stack[bidi_it->stack_idx].level;
1556 int new_level = bidi_resolve_explicit (bidi_it);
1557 int next_char;
1558 bidi_type_t type_of_next;
1559 struct bidi_it saved_it;
1560 EMACS_INT eob =
1561 (STRINGP (bidi_it->string.lstring) || bidi_it->string.s)
1562 ? bidi_it->string.schars : ZV;
1564 type = bidi_it->type;
1565 override = bidi_it->level_stack[bidi_it->stack_idx].override;
1567 if (type == UNKNOWN_BT
1568 || type == LRE
1569 || type == LRO
1570 || type == RLE
1571 || type == RLO
1572 || type == PDF)
1573 abort ();
1575 if (new_level != prev_level
1576 || bidi_it->type == NEUTRAL_B)
1578 /* We've got a new embedding level run, compute the directional
1579 type of sor and initialize per-run variables (UAX#9, clause
1580 X10). */
1581 bidi_set_sor_type (bidi_it, prev_level, new_level);
1583 else if (type == NEUTRAL_S || type == NEUTRAL_WS
1584 || type == WEAK_BN || type == STRONG_AL)
1585 bidi_it->type_after_w1 = type; /* needed in L1 */
1586 bidi_check_type (bidi_it->type_after_w1);
1588 /* Level and directional override status are already recorded in
1589 bidi_it, and do not need any change; see X6. */
1590 if (override == R2L) /* X6 */
1591 type = STRONG_R;
1592 else if (override == L2R)
1593 type = STRONG_L;
1594 else
1596 if (type == WEAK_NSM) /* W1 */
1598 /* Note that we don't need to consider the case where the
1599 prev character has its type overridden by an RLO or LRO,
1600 because then either the type of this NSM would have been
1601 also overridden, or the previous character is outside the
1602 current level run, and thus not relevant to this NSM.
1603 This is why NSM gets the type_after_w1 of the previous
1604 character. */
1605 if (bidi_it->prev.type_after_w1 != UNKNOWN_BT
1606 /* if type_after_w1 is NEUTRAL_B, this NSM is at sor */
1607 && bidi_it->prev.type_after_w1 != NEUTRAL_B)
1608 type = bidi_it->prev.type_after_w1;
1609 else if (bidi_it->sor == R2L)
1610 type = STRONG_R;
1611 else if (bidi_it->sor == L2R)
1612 type = STRONG_L;
1613 else /* shouldn't happen! */
1614 abort ();
1616 if (type == WEAK_EN /* W2 */
1617 && bidi_it->last_strong.type_after_w1 == STRONG_AL)
1618 type = WEAK_AN;
1619 else if (type == STRONG_AL) /* W3 */
1620 type = STRONG_R;
1621 else if ((type == WEAK_ES /* W4 */
1622 && bidi_it->prev.type_after_w1 == WEAK_EN
1623 && bidi_it->prev.orig_type == WEAK_EN)
1624 || (type == WEAK_CS
1625 && ((bidi_it->prev.type_after_w1 == WEAK_EN
1626 && bidi_it->prev.orig_type == WEAK_EN)
1627 || bidi_it->prev.type_after_w1 == WEAK_AN)))
1629 const unsigned char *s =
1630 STRINGP (bidi_it->string.lstring)
1631 ? SDATA (bidi_it->string.lstring) : bidi_it->string.s;
1633 next_char =
1634 bidi_it->charpos + bidi_it->nchars >= eob
1635 ? BIDI_EOB
1636 : bidi_char_at_pos (bidi_it->bytepos + bidi_it->ch_len, s,
1637 bidi_it->string.unibyte);
1638 type_of_next = bidi_get_type (next_char, override);
1640 if (type_of_next == WEAK_BN
1641 || bidi_explicit_dir_char (next_char))
1643 bidi_copy_it (&saved_it, bidi_it);
1644 while (bidi_resolve_explicit (bidi_it) == new_level
1645 && bidi_it->type == WEAK_BN)
1647 type_of_next = bidi_it->type;
1648 bidi_copy_it (bidi_it, &saved_it);
1651 /* If the next character is EN, but the last strong-type
1652 character is AL, that next EN will be changed to AN when
1653 we process it in W2 above. So in that case, this ES
1654 should not be changed into EN. */
1655 if (type == WEAK_ES
1656 && type_of_next == WEAK_EN
1657 && bidi_it->last_strong.type_after_w1 != STRONG_AL)
1658 type = WEAK_EN;
1659 else if (type == WEAK_CS)
1661 if (bidi_it->prev.type_after_w1 == WEAK_AN
1662 && (type_of_next == WEAK_AN
1663 /* If the next character is EN, but the last
1664 strong-type character is AL, EN will be later
1665 changed to AN when we process it in W2 above.
1666 So in that case, this ES should not be
1667 changed into EN. */
1668 || (type_of_next == WEAK_EN
1669 && bidi_it->last_strong.type_after_w1 == STRONG_AL)))
1670 type = WEAK_AN;
1671 else if (bidi_it->prev.type_after_w1 == WEAK_EN
1672 && type_of_next == WEAK_EN
1673 && bidi_it->last_strong.type_after_w1 != STRONG_AL)
1674 type = WEAK_EN;
1677 else if (type == WEAK_ET /* W5: ET with EN before or after it */
1678 || type == WEAK_BN) /* W5/Retaining */
1680 if (bidi_it->prev.type_after_w1 == WEAK_EN /* ET/BN w/EN before it */
1681 || bidi_it->next_en_pos > bidi_it->charpos)
1682 type = WEAK_EN;
1683 else /* W5: ET/BN with EN after it. */
1685 EMACS_INT en_pos = bidi_it->charpos + bidi_it->nchars;
1686 const unsigned char *s =
1687 STRINGP (bidi_it->string.lstring)
1688 ? SDATA (bidi_it->string.lstring) : bidi_it->string.s;
1690 if (bidi_it->nchars <= 0)
1691 abort ();
1692 next_char =
1693 bidi_it->charpos + bidi_it->nchars >= eob
1694 ? BIDI_EOB
1695 : bidi_char_at_pos (bidi_it->bytepos + bidi_it->ch_len, s,
1696 bidi_it->string.unibyte);
1697 type_of_next = bidi_get_type (next_char, override);
1699 if (type_of_next == WEAK_ET
1700 || type_of_next == WEAK_BN
1701 || bidi_explicit_dir_char (next_char))
1703 bidi_copy_it (&saved_it, bidi_it);
1704 while (bidi_resolve_explicit (bidi_it) == new_level
1705 && (bidi_it->type == WEAK_BN
1706 || bidi_it->type == WEAK_ET))
1708 type_of_next = bidi_it->type;
1709 en_pos = bidi_it->charpos;
1710 bidi_copy_it (bidi_it, &saved_it);
1712 if (type_of_next == WEAK_EN)
1714 /* If the last strong character is AL, the EN we've
1715 found will become AN when we get to it (W2). */
1716 if (bidi_it->last_strong.type_after_w1 != STRONG_AL)
1718 type = WEAK_EN;
1719 /* Remember this EN position, to speed up processing
1720 of the next ETs. */
1721 bidi_it->next_en_pos = en_pos;
1723 else if (type == WEAK_BN)
1724 type = NEUTRAL_ON; /* W6/Retaining */
1730 if (type == WEAK_ES || type == WEAK_ET || type == WEAK_CS /* W6 */
1731 || (type == WEAK_BN
1732 && (bidi_it->prev.type_after_w1 == WEAK_CS /* W6/Retaining */
1733 || bidi_it->prev.type_after_w1 == WEAK_ES
1734 || bidi_it->prev.type_after_w1 == WEAK_ET)))
1735 type = NEUTRAL_ON;
1737 /* Store the type we've got so far, before we clobber it with strong
1738 types in W7 and while resolving neutral types. But leave alone
1739 the original types that were recorded above, because we will need
1740 them for the L1 clause. */
1741 if (bidi_it->type_after_w1 == UNKNOWN_BT)
1742 bidi_it->type_after_w1 = type;
1743 bidi_check_type (bidi_it->type_after_w1);
1745 if (type == WEAK_EN) /* W7 */
1747 if ((bidi_it->last_strong.type_after_w1 == STRONG_L)
1748 || (bidi_it->last_strong.type == UNKNOWN_BT && bidi_it->sor == L2R))
1749 type = STRONG_L;
1752 bidi_it->type = type;
1753 bidi_check_type (bidi_it->type);
1754 return type;
1757 /* Resolve the type of a neutral character according to the type of
1758 surrounding strong text and the current embedding level. */
1759 static inline bidi_type_t
1760 bidi_resolve_neutral_1 (bidi_type_t prev_type, bidi_type_t next_type, int lev)
1762 /* N1: European and Arabic numbers are treated as though they were R. */
1763 if (next_type == WEAK_EN || next_type == WEAK_AN)
1764 next_type = STRONG_R;
1765 if (prev_type == WEAK_EN || prev_type == WEAK_AN)
1766 prev_type = STRONG_R;
1768 if (next_type == prev_type) /* N1 */
1769 return next_type;
1770 else if ((lev & 1) == 0) /* N2 */
1771 return STRONG_L;
1772 else
1773 return STRONG_R;
1776 static bidi_type_t
1777 bidi_resolve_neutral (struct bidi_it *bidi_it)
1779 int prev_level = bidi_it->level_stack[bidi_it->stack_idx].level;
1780 bidi_type_t type = bidi_resolve_weak (bidi_it);
1781 int current_level = bidi_it->level_stack[bidi_it->stack_idx].level;
1783 if (!(type == STRONG_R
1784 || type == STRONG_L
1785 || type == WEAK_BN
1786 || type == WEAK_EN
1787 || type == WEAK_AN
1788 || type == NEUTRAL_B
1789 || type == NEUTRAL_S
1790 || type == NEUTRAL_WS
1791 || type == NEUTRAL_ON))
1792 abort ();
1794 if (bidi_get_category (type) == NEUTRAL
1795 || (type == WEAK_BN && prev_level == current_level))
1797 if (bidi_it->next_for_neutral.type != UNKNOWN_BT)
1798 type = bidi_resolve_neutral_1 (bidi_it->prev_for_neutral.type,
1799 bidi_it->next_for_neutral.type,
1800 current_level);
1801 else
1803 /* Arrrgh!! The UAX#9 algorithm is too deeply entrenched in
1804 the assumption of batch-style processing; see clauses W4,
1805 W5, and especially N1, which require to look far forward
1806 (as well as back) in the buffer/string. May the fleas of
1807 a thousand camels infest the armpits of those who design
1808 supposedly general-purpose algorithms by looking at their
1809 own implementations, and fail to consider other possible
1810 implementations! */
1811 struct bidi_it saved_it;
1812 bidi_type_t next_type;
1814 if (bidi_it->scan_dir == -1)
1815 abort ();
1817 bidi_copy_it (&saved_it, bidi_it);
1818 /* Scan the text forward until we find the first non-neutral
1819 character, and then use that to resolve the neutral we
1820 are dealing with now. We also cache the scanned iterator
1821 states, to salvage some of the effort later. */
1822 bidi_cache_iterator_state (bidi_it, 0);
1823 do {
1824 /* Record the info about the previous character, so that
1825 it will be cached below with this state. */
1826 if (bidi_it->type_after_w1 != WEAK_BN /* W1/Retaining */
1827 && bidi_it->type != WEAK_BN)
1828 bidi_remember_char (&bidi_it->prev, bidi_it);
1829 type = bidi_resolve_weak (bidi_it);
1830 /* Paragraph separators have their levels fully resolved
1831 at this point, so cache them as resolved. */
1832 bidi_cache_iterator_state (bidi_it, type == NEUTRAL_B);
1833 /* FIXME: implement L1 here, by testing for a newline and
1834 resetting the level for any sequence of whitespace
1835 characters adjacent to it. */
1836 } while (!(type == NEUTRAL_B
1837 || (type != WEAK_BN
1838 && bidi_get_category (type) != NEUTRAL)
1839 /* This is all per level run, so stop when we
1840 reach the end of this level run. */
1841 || bidi_it->level_stack[bidi_it->stack_idx].level !=
1842 current_level));
1844 bidi_remember_char (&saved_it.next_for_neutral, bidi_it);
1846 switch (type)
1848 case STRONG_L:
1849 case STRONG_R:
1850 case STRONG_AL:
1851 next_type = type;
1852 break;
1853 case WEAK_EN:
1854 case WEAK_AN:
1855 /* N1: ``European and Arabic numbers are treated as
1856 though they were R.'' */
1857 next_type = STRONG_R;
1858 saved_it.next_for_neutral.type = STRONG_R;
1859 break;
1860 case WEAK_BN:
1861 if (!bidi_explicit_dir_char (bidi_it->ch))
1862 abort (); /* can't happen: BNs are skipped */
1863 /* FALLTHROUGH */
1864 case NEUTRAL_B:
1865 /* Marched all the way to the end of this level run.
1866 We need to use the eor type, whose information is
1867 stored by bidi_set_sor_type in the prev_for_neutral
1868 member. */
1869 if (saved_it.type != WEAK_BN
1870 || bidi_get_category (bidi_it->prev.type_after_w1) == NEUTRAL)
1872 next_type = bidi_it->prev_for_neutral.type;
1873 saved_it.next_for_neutral.type = next_type;
1874 bidi_check_type (next_type);
1876 else
1878 /* This is a BN which does not adjoin neutrals.
1879 Leave its type alone. */
1880 bidi_copy_it (bidi_it, &saved_it);
1881 return bidi_it->type;
1883 break;
1884 default:
1885 abort ();
1887 type = bidi_resolve_neutral_1 (saved_it.prev_for_neutral.type,
1888 next_type, current_level);
1889 saved_it.type = type;
1890 bidi_check_type (type);
1891 bidi_copy_it (bidi_it, &saved_it);
1894 return type;
1897 /* Given an iterator state in BIDI_IT, advance one character position
1898 in the buffer/string to the next character (in the logical order),
1899 resolve the bidi type of that next character, and return that
1900 type. */
1901 static bidi_type_t
1902 bidi_type_of_next_char (struct bidi_it *bidi_it)
1904 bidi_type_t type;
1906 /* This should always be called during a forward scan. */
1907 if (bidi_it->scan_dir != 1)
1908 abort ();
1910 /* Reset the limit until which to ignore BNs if we step out of the
1911 area where we found only empty levels. */
1912 if ((bidi_it->ignore_bn_limit > -1
1913 && bidi_it->ignore_bn_limit <= bidi_it->charpos)
1914 || (bidi_it->ignore_bn_limit == -2
1915 && !bidi_explicit_dir_char (bidi_it->ch)))
1916 bidi_it->ignore_bn_limit = -1;
1918 type = bidi_resolve_neutral (bidi_it);
1920 return type;
1923 /* Given an iterator state BIDI_IT, advance one character position in
1924 the buffer/string to the next character (in the current scan
1925 direction), resolve the embedding and implicit levels of that next
1926 character, and return the resulting level. */
1927 static int
1928 bidi_level_of_next_char (struct bidi_it *bidi_it)
1930 bidi_type_t type;
1931 int level, prev_level = -1;
1932 struct bidi_saved_info next_for_neutral;
1933 EMACS_INT next_char_pos = -2;
1935 if (bidi_it->scan_dir == 1)
1937 EMACS_INT eob =
1938 (bidi_it->string.s || STRINGP (bidi_it->string.lstring))
1939 ? bidi_it->string.schars : ZV;
1941 /* There's no sense in trying to advance if we hit end of text. */
1942 if (bidi_it->charpos >= eob)
1943 return bidi_it->resolved_level;
1945 /* Record the info about the previous character. */
1946 if (bidi_it->type_after_w1 != WEAK_BN /* W1/Retaining */
1947 && bidi_it->type != WEAK_BN)
1948 bidi_remember_char (&bidi_it->prev, bidi_it);
1949 if (bidi_it->type_after_w1 == STRONG_R
1950 || bidi_it->type_after_w1 == STRONG_L
1951 || bidi_it->type_after_w1 == STRONG_AL)
1952 bidi_remember_char (&bidi_it->last_strong, bidi_it);
1953 /* FIXME: it sounds like we don't need both prev and
1954 prev_for_neutral members, but I'm leaving them both for now. */
1955 if (bidi_it->type == STRONG_R || bidi_it->type == STRONG_L
1956 || bidi_it->type == WEAK_EN || bidi_it->type == WEAK_AN)
1957 bidi_remember_char (&bidi_it->prev_for_neutral, bidi_it);
1959 /* If we overstepped the characters used for resolving neutrals
1960 and whitespace, invalidate their info in the iterator. */
1961 if (bidi_it->charpos >= bidi_it->next_for_neutral.charpos)
1962 bidi_it->next_for_neutral.type = UNKNOWN_BT;
1963 if (bidi_it->next_en_pos >= 0
1964 && bidi_it->charpos >= bidi_it->next_en_pos)
1965 bidi_it->next_en_pos = -1;
1966 if (bidi_it->next_for_ws.type != UNKNOWN_BT
1967 && bidi_it->charpos >= bidi_it->next_for_ws.charpos)
1968 bidi_it->next_for_ws.type = UNKNOWN_BT;
1970 /* This must be taken before we fill the iterator with the info
1971 about the next char. If we scan backwards, the iterator
1972 state must be already cached, so there's no need to know the
1973 embedding level of the previous character, since we will be
1974 returning to our caller shortly. */
1975 prev_level = bidi_it->level_stack[bidi_it->stack_idx].level;
1977 next_for_neutral = bidi_it->next_for_neutral;
1979 /* Perhaps the character we want is already cached. If it is, the
1980 call to bidi_cache_find below will return a type other than
1981 UNKNOWN_BT. */
1982 if (bidi_cache_idx > bidi_cache_start && !bidi_it->first_elt)
1984 int bob =
1985 (bidi_it->string.s || STRINGP (bidi_it->string.lstring)) ? 0 : 1;
1987 if (bidi_it->scan_dir > 0)
1989 if (bidi_it->nchars <= 0)
1990 abort ();
1991 next_char_pos = bidi_it->charpos + bidi_it->nchars;
1993 else if (bidi_it->charpos >= bob)
1994 /* Implementation note: we allow next_char_pos to be as low as
1995 0 for buffers or -1 for strings, and that is okay because
1996 that's the "position" of the sentinel iterator state we
1997 cached at the beginning of the iteration. */
1998 next_char_pos = bidi_it->charpos - 1;
1999 if (next_char_pos >= bob - 1)
2000 type = bidi_cache_find (next_char_pos, -1, bidi_it);
2001 else
2002 type = UNKNOWN_BT;
2004 else
2005 type = UNKNOWN_BT;
2006 if (type != UNKNOWN_BT)
2008 /* Don't lose the information for resolving neutrals! The
2009 cached states could have been cached before their
2010 next_for_neutral member was computed. If we are on our way
2011 forward, we can simply take the info from the previous
2012 state. */
2013 if (bidi_it->scan_dir == 1
2014 && bidi_it->next_for_neutral.type == UNKNOWN_BT)
2015 bidi_it->next_for_neutral = next_for_neutral;
2017 /* If resolved_level is -1, it means this state was cached
2018 before it was completely resolved, so we cannot return
2019 it. */
2020 if (bidi_it->resolved_level != -1)
2021 return bidi_it->resolved_level;
2023 if (bidi_it->scan_dir == -1)
2024 /* If we are going backwards, the iterator state is already cached
2025 from previous scans, and should be fully resolved. */
2026 abort ();
2028 if (type == UNKNOWN_BT)
2029 type = bidi_type_of_next_char (bidi_it);
2031 if (type == NEUTRAL_B)
2032 return bidi_it->resolved_level;
2034 level = bidi_it->level_stack[bidi_it->stack_idx].level;
2035 if ((bidi_get_category (type) == NEUTRAL /* && type != NEUTRAL_B */)
2036 || (type == WEAK_BN && prev_level == level))
2038 if (bidi_it->next_for_neutral.type == UNKNOWN_BT)
2039 abort ();
2041 /* If the cached state shows a neutral character, it was not
2042 resolved by bidi_resolve_neutral, so do it now. */
2043 type = bidi_resolve_neutral_1 (bidi_it->prev_for_neutral.type,
2044 bidi_it->next_for_neutral.type,
2045 level);
2048 if (!(type == STRONG_R
2049 || type == STRONG_L
2050 || type == WEAK_BN
2051 || type == WEAK_EN
2052 || type == WEAK_AN))
2053 abort ();
2054 bidi_it->type = type;
2055 bidi_check_type (bidi_it->type);
2057 /* For L1 below, we need to know, for each WS character, whether
2058 it belongs to a sequence of WS characters preceding a newline
2059 or a TAB or a paragraph separator. */
2060 if (bidi_it->orig_type == NEUTRAL_WS
2061 && bidi_it->next_for_ws.type == UNKNOWN_BT)
2063 int ch;
2064 EMACS_INT clen = bidi_it->ch_len;
2065 EMACS_INT bpos = bidi_it->bytepos;
2066 EMACS_INT cpos = bidi_it->charpos;
2067 EMACS_INT disp_pos = bidi_it->disp_pos;
2068 EMACS_INT nc = bidi_it->nchars;
2069 struct bidi_string_data bs = bidi_it->string;
2070 bidi_type_t chtype;
2071 int fwp = bidi_it->frame_window_p;
2072 int dpp = bidi_it->disp_prop_p;
2074 if (bidi_it->nchars <= 0)
2075 abort ();
2076 do {
2077 ch = bidi_fetch_char (bpos += clen, cpos += nc, &disp_pos, &dpp, &bs,
2078 fwp, &clen, &nc);
2079 if (ch == '\n' || ch == BIDI_EOB /* || ch == LINESEP_CHAR */)
2080 chtype = NEUTRAL_B;
2081 else
2082 chtype = bidi_get_type (ch, NEUTRAL_DIR);
2083 } while (chtype == NEUTRAL_WS || chtype == WEAK_BN
2084 || bidi_explicit_dir_char (ch)); /* L1/Retaining */
2085 bidi_it->next_for_ws.type = chtype;
2086 bidi_check_type (bidi_it->next_for_ws.type);
2087 bidi_it->next_for_ws.charpos = cpos;
2088 bidi_it->next_for_ws.bytepos = bpos;
2091 /* Resolve implicit levels, with a twist: PDFs get the embedding
2092 level of the enbedding they terminate. See below for the
2093 reason. */
2094 if (bidi_it->orig_type == PDF
2095 /* Don't do this if this formatting code didn't change the
2096 embedding level due to invalid or empty embeddings. */
2097 && prev_level != level)
2099 /* Don't look in UAX#9 for the reason for this: it's our own
2100 private quirk. The reason is that we want the formatting
2101 codes to be delivered so that they bracket the text of their
2102 embedding. For example, given the text
2104 {RLO}teST{PDF}
2106 we want it to be displayed as
2108 {PDF}STet{RLO}
2110 not as
2112 STet{RLO}{PDF}
2114 which will result because we bump up the embedding level as
2115 soon as we see the RLO and pop it as soon as we see the PDF,
2116 so RLO itself has the same embedding level as "teST", and
2117 thus would be normally delivered last, just before the PDF.
2118 The switch below fiddles with the level of PDF so that this
2119 ugly side effect does not happen.
2121 (This is, of course, only important if the formatting codes
2122 are actually displayed, but Emacs does need to display them
2123 if the user wants to.) */
2124 level = prev_level;
2126 else if (bidi_it->orig_type == NEUTRAL_B /* L1 */
2127 || bidi_it->orig_type == NEUTRAL_S
2128 || bidi_it->ch == '\n' || bidi_it->ch == BIDI_EOB
2129 /* || bidi_it->ch == LINESEP_CHAR */
2130 || (bidi_it->orig_type == NEUTRAL_WS
2131 && (bidi_it->next_for_ws.type == NEUTRAL_B
2132 || bidi_it->next_for_ws.type == NEUTRAL_S)))
2133 level = bidi_it->level_stack[0].level;
2134 else if ((level & 1) == 0) /* I1 */
2136 if (type == STRONG_R)
2137 level++;
2138 else if (type == WEAK_EN || type == WEAK_AN)
2139 level += 2;
2141 else /* I2 */
2143 if (type == STRONG_L || type == WEAK_EN || type == WEAK_AN)
2144 level++;
2147 bidi_it->resolved_level = level;
2148 return level;
2151 /* Move to the other edge of a level given by LEVEL. If END_FLAG is
2152 non-zero, we are at the end of a level, and we need to prepare to
2153 resume the scan of the lower level.
2155 If this level's other edge is cached, we simply jump to it, filling
2156 the iterator structure with the iterator state on the other edge.
2157 Otherwise, we walk the buffer or string until we come back to the
2158 same level as LEVEL.
2160 Note: we are not talking here about a ``level run'' in the UAX#9
2161 sense of the term, but rather about a ``level'' which includes
2162 all the levels higher than it. In other words, given the levels
2163 like this:
2165 11111112222222333333334443343222222111111112223322111
2166 A B C
2168 and assuming we are at point A scanning left to right, this
2169 function moves to point C, whereas the UAX#9 ``level 2 run'' ends
2170 at point B. */
2171 static void
2172 bidi_find_other_level_edge (struct bidi_it *bidi_it, int level, int end_flag)
2174 int dir = end_flag ? -bidi_it->scan_dir : bidi_it->scan_dir;
2175 ptrdiff_t idx;
2177 /* Try the cache first. */
2178 if ((idx = bidi_cache_find_level_change (level, dir, end_flag))
2179 >= bidi_cache_start)
2180 bidi_cache_fetch_state (idx, bidi_it);
2181 else
2183 int new_level;
2185 if (end_flag)
2186 abort (); /* if we are at end of level, its edges must be cached */
2188 bidi_cache_iterator_state (bidi_it, 1);
2189 do {
2190 new_level = bidi_level_of_next_char (bidi_it);
2191 bidi_cache_iterator_state (bidi_it, 1);
2192 } while (new_level >= level);
2196 void
2197 bidi_move_to_visually_next (struct bidi_it *bidi_it)
2199 int old_level, new_level, next_level;
2200 struct bidi_it sentinel;
2201 struct gcpro gcpro1;
2203 if (bidi_it->charpos < 0 || bidi_it->bytepos < 0)
2204 abort ();
2206 if (bidi_it->scan_dir == 0)
2208 bidi_it->scan_dir = 1; /* default to logical order */
2211 /* The code below can call eval, and thus cause GC. If we are
2212 iterating a Lisp string, make sure it won't be GCed. */
2213 if (STRINGP (bidi_it->string.lstring))
2214 GCPRO1 (bidi_it->string.lstring);
2216 /* If we just passed a newline, initialize for the next line. */
2217 if (!bidi_it->first_elt && bidi_it->orig_type == NEUTRAL_B)
2218 bidi_line_init (bidi_it);
2220 /* Prepare the sentinel iterator state, and cache it. When we bump
2221 into it, scanning backwards, we'll know that the last non-base
2222 level is exhausted. */
2223 if (bidi_cache_idx == bidi_cache_start)
2225 bidi_copy_it (&sentinel, bidi_it);
2226 if (bidi_it->first_elt)
2228 sentinel.charpos--; /* cached charpos needs to be monotonic */
2229 sentinel.bytepos--;
2230 sentinel.ch = '\n'; /* doesn't matter, but why not? */
2231 sentinel.ch_len = 1;
2232 sentinel.nchars = 1;
2234 bidi_cache_iterator_state (&sentinel, 1);
2237 old_level = bidi_it->resolved_level;
2238 new_level = bidi_level_of_next_char (bidi_it);
2240 /* Reordering of resolved levels (clause L2) is implemented by
2241 jumping to the other edge of the level and flipping direction of
2242 scanning the text whenever we find a level change. */
2243 if (new_level != old_level)
2245 int ascending = new_level > old_level;
2246 int level_to_search = ascending ? old_level + 1 : old_level;
2247 int incr = ascending ? 1 : -1;
2248 int expected_next_level = old_level + incr;
2250 /* Jump (or walk) to the other edge of this level. */
2251 bidi_find_other_level_edge (bidi_it, level_to_search, !ascending);
2252 /* Switch scan direction and peek at the next character in the
2253 new direction. */
2254 bidi_it->scan_dir = -bidi_it->scan_dir;
2256 /* The following loop handles the case where the resolved level
2257 jumps by more than one. This is typical for numbers inside a
2258 run of text with left-to-right embedding direction, but can
2259 also happen in other situations. In those cases the decision
2260 where to continue after a level change, and in what direction,
2261 is tricky. For example, given a text like below:
2263 abcdefgh
2264 11336622
2266 (where the numbers below the text show the resolved levels),
2267 the result of reordering according to UAX#9 should be this:
2269 efdcghba
2271 This is implemented by the loop below which flips direction
2272 and jumps to the other edge of the level each time it finds
2273 the new level not to be the expected one. The expected level
2274 is always one more or one less than the previous one. */
2275 next_level = bidi_peek_at_next_level (bidi_it);
2276 while (next_level != expected_next_level)
2278 expected_next_level += incr;
2279 level_to_search += incr;
2280 bidi_find_other_level_edge (bidi_it, level_to_search, !ascending);
2281 bidi_it->scan_dir = -bidi_it->scan_dir;
2282 next_level = bidi_peek_at_next_level (bidi_it);
2285 /* Finally, deliver the next character in the new direction. */
2286 next_level = bidi_level_of_next_char (bidi_it);
2289 /* Take note when we have just processed the newline that precedes
2290 the end of the paragraph. The next time we are about to be
2291 called, set_iterator_to_next will automatically reinit the
2292 paragraph direction, if needed. We do this at the newline before
2293 the paragraph separator, because the next character might not be
2294 the first character of the next paragraph, due to the bidi
2295 reordering, whereas we _must_ know the paragraph base direction
2296 _before_ we process the paragraph's text, since the base
2297 direction affects the reordering. */
2298 if (bidi_it->scan_dir == 1 && bidi_it->orig_type == NEUTRAL_B)
2300 /* The paragraph direction of the entire string, once
2301 determined, is in effect for the entire string. Setting the
2302 separator limit to the end of the string prevents
2303 bidi_paragraph_init from being called automatically on this
2304 string. */
2305 if (bidi_it->string.s || STRINGP (bidi_it->string.lstring))
2306 bidi_it->separator_limit = bidi_it->string.schars;
2307 else if (bidi_it->bytepos < ZV_BYTE)
2309 EMACS_INT sep_len =
2310 bidi_at_paragraph_end (bidi_it->charpos + bidi_it->nchars,
2311 bidi_it->bytepos + bidi_it->ch_len);
2312 if (bidi_it->nchars <= 0)
2313 abort ();
2314 if (sep_len >= 0)
2316 bidi_it->new_paragraph = 1;
2317 /* Record the buffer position of the last character of the
2318 paragraph separator. */
2319 bidi_it->separator_limit =
2320 bidi_it->charpos + bidi_it->nchars + sep_len;
2325 if (bidi_it->scan_dir == 1 && bidi_cache_idx > bidi_cache_start)
2327 /* If we are at paragraph's base embedding level and beyond the
2328 last cached position, the cache's job is done and we can
2329 discard it. */
2330 if (bidi_it->resolved_level == bidi_it->level_stack[0].level
2331 && bidi_it->charpos > (bidi_cache[bidi_cache_idx - 1].charpos
2332 + bidi_cache[bidi_cache_idx - 1].nchars - 1))
2333 bidi_cache_reset ();
2334 /* But as long as we are caching during forward scan, we must
2335 cache each state, or else the cache integrity will be
2336 compromised: it assumes cached states correspond to buffer
2337 positions 1:1. */
2338 else
2339 bidi_cache_iterator_state (bidi_it, 1);
2342 if (STRINGP (bidi_it->string.lstring))
2343 UNGCPRO;
2346 /* This is meant to be called from within the debugger, whenever you
2347 wish to examine the cache contents. */
2348 void bidi_dump_cached_states (void) EXTERNALLY_VISIBLE;
2349 void
2350 bidi_dump_cached_states (void)
2352 ptrdiff_t i;
2353 int ndigits = 1;
2355 if (bidi_cache_idx == 0)
2357 fprintf (stderr, "The cache is empty.\n");
2358 return;
2360 fprintf (stderr, "Total of %"pD"d state%s in cache:\n",
2361 bidi_cache_idx, bidi_cache_idx == 1 ? "" : "s");
2363 for (i = bidi_cache[bidi_cache_idx - 1].charpos; i > 0; i /= 10)
2364 ndigits++;
2365 fputs ("ch ", stderr);
2366 for (i = 0; i < bidi_cache_idx; i++)
2367 fprintf (stderr, "%*c", ndigits, bidi_cache[i].ch);
2368 fputs ("\n", stderr);
2369 fputs ("lvl ", stderr);
2370 for (i = 0; i < bidi_cache_idx; i++)
2371 fprintf (stderr, "%*d", ndigits, bidi_cache[i].resolved_level);
2372 fputs ("\n", stderr);
2373 fputs ("pos ", stderr);
2374 for (i = 0; i < bidi_cache_idx; i++)
2375 fprintf (stderr, "%*"pI"d", ndigits, bidi_cache[i].charpos);
2376 fputs ("\n", stderr);