1 /* Low-level bidirectional buffer-scanning functions for GNU Emacs.
2 Copyright (C) 2000, 2001, 2004, 2005, 2009, 2010
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 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
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 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
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.
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". */
59 #include "character.h"
60 #include "dispextern.h"
62 static int bidi_initialized
= 0;
64 static Lisp_Object bidi_type_table
, bidi_mirror_table
;
66 /* FIXME: Remove these when bidi_explicit_dir_char uses a lookup table. */
67 #define LRM_CHAR 0x200E
68 #define RLM_CHAR 0x200F
69 #define LRE_CHAR 0x202A
70 #define RLE_CHAR 0x202B
71 #define PDF_CHAR 0x202C
72 #define LRO_CHAR 0x202D
73 #define RLO_CHAR 0x202E
76 #define BIDI_BOB -2 /* FIXME: Is this needed? */
78 /* Local data structures. (Look in dispextern.h for the rest.) */
80 /* What we need to know about the current paragraph. */
81 struct bidi_paragraph_info
{
82 EMACS_INT start_bytepos
; /* byte position where it begins */
83 EMACS_INT end_bytepos
; /* byte position where it ends */
84 int embedding_level
; /* its basic embedding level */
85 bidi_dir_t base_dir
; /* its base direction */
88 /* Data type for describing the bidirectional character categories. */
96 int bidi_ignore_explicit_marks_for_paragraph_level
= 1;
98 static Lisp_Object paragraph_start_re
, paragraph_separate_re
;
99 static Lisp_Object Qparagraph_start
, Qparagraph_separate
;
102 bidi_initialize (void)
105 #include "biditype.h"
106 #include "bidimirror.h"
110 bidi_type_table
= Fmake_char_table (Qnil
, make_number (STRONG_L
));
111 staticpro (&bidi_type_table
);
113 for (i
= 0; i
< sizeof bidi_type
/ sizeof bidi_type
[0]; i
++)
114 char_table_set_range (bidi_type_table
, bidi_type
[i
].from
, bidi_type
[i
].to
,
115 make_number (bidi_type
[i
].type
));
117 bidi_mirror_table
= Fmake_char_table (Qnil
, Qnil
);
118 staticpro (&bidi_mirror_table
);
120 for (i
= 0; i
< sizeof bidi_mirror
/ sizeof bidi_mirror
[0]; i
++)
121 char_table_set (bidi_mirror_table
, bidi_mirror
[i
].from
,
122 make_number (bidi_mirror
[i
].to
));
124 Qparagraph_start
= intern ("paragraph-start");
125 staticpro (&Qparagraph_start
);
126 paragraph_start_re
= Fsymbol_value (Qparagraph_start
);
127 if (!STRINGP (paragraph_start_re
))
128 paragraph_start_re
= build_string ("\f\\|[ \t]*$");
129 staticpro (¶graph_start_re
);
130 Qparagraph_separate
= intern ("paragraph-separate");
131 staticpro (&Qparagraph_separate
);
132 paragraph_separate_re
= Fsymbol_value (Qparagraph_separate
);
133 if (!STRINGP (paragraph_separate_re
))
134 paragraph_separate_re
= build_string ("[ \t\f]*$");
135 staticpro (¶graph_separate_re
);
136 bidi_initialized
= 1;
139 /* Return the bidi type of a character CH, subject to the current
140 directional OVERRIDE. */
141 static INLINE bidi_type_t
142 bidi_get_type (int ch
, bidi_dir_t override
)
144 bidi_type_t default_type
;
148 if (ch
< 0 || ch
> MAX_CHAR
)
151 default_type
= (bidi_type_t
) XINT (CHAR_TABLE_REF (bidi_type_table
, ch
));
153 if (override
== NEUTRAL_DIR
)
156 switch (default_type
)
158 /* Although UAX#9 does not tell, it doesn't make sense to
159 override NEUTRAL_B and LRM/RLM characters. */
174 if (override
== L2R
) /* X6 */
176 else if (override
== R2L
)
179 abort (); /* can't happen: handled above */
185 bidi_check_type (bidi_type_t type
)
187 if (type
< UNKNOWN_BT
|| type
> NEUTRAL_ON
)
191 /* Given a bidi TYPE of a character, return its category. */
192 static INLINE bidi_category_t
193 bidi_get_category (bidi_type_t type
)
207 case PDF
: /* ??? really?? */
226 /* Return the mirrored character of C, if it has one. If C has no
227 mirrored counterpart, return C.
228 Note: The conditions in UAX#9 clause L4 regarding the surrounding
229 context must be tested by the caller. */
231 bidi_mirror_char (int c
)
237 if (c
< 0 || c
> MAX_CHAR
)
240 val
= CHAR_TABLE_REF (bidi_mirror_table
, c
);
245 if (v
< 0 || v
> MAX_CHAR
)
254 /* Copy the bidi iterator from FROM to TO. To save cycles, this only
255 copies the part of the level stack that is actually in use. */
257 bidi_copy_it (struct bidi_it
*to
, struct bidi_it
*from
)
261 /* Copy everything except the level stack and beyond. */
262 memcpy (to
, from
, ((size_t)&((struct bidi_it
*)0)->level_stack
[0]));
264 /* Copy the active part of the level stack. */
265 to
->level_stack
[0] = from
->level_stack
[0]; /* level zero is always in use */
266 for (i
= 1; i
<= from
->stack_idx
; i
++)
267 to
->level_stack
[i
] = from
->level_stack
[i
];
270 /* Caching the bidi iterator states. */
272 #define BIDI_CACHE_CHUNK 200
273 static struct bidi_it
*bidi_cache
;
274 static size_t bidi_cache_size
= 0;
275 static size_t elsz
= sizeof (struct bidi_it
);
276 static int bidi_cache_idx
; /* next unused cache slot */
277 static int bidi_cache_last_idx
; /* slot of last cache hit */
280 bidi_cache_reset (void)
283 bidi_cache_last_idx
= -1;
287 bidi_cache_shrink (void)
289 if (bidi_cache_size
> BIDI_CACHE_CHUNK
)
291 bidi_cache_size
= BIDI_CACHE_CHUNK
;
293 (struct bidi_it
*) xrealloc (bidi_cache
, bidi_cache_size
* elsz
);
299 bidi_cache_fetch_state (int idx
, struct bidi_it
*bidi_it
)
301 int current_scan_dir
= bidi_it
->scan_dir
;
303 if (idx
< 0 || idx
>= bidi_cache_idx
)
306 bidi_copy_it (bidi_it
, &bidi_cache
[idx
]);
307 bidi_it
->scan_dir
= current_scan_dir
;
308 bidi_cache_last_idx
= idx
;
311 /* Find a cached state with a given CHARPOS and resolved embedding
312 level less or equal to LEVEL. if LEVEL is -1, disregard the
313 resolved levels in cached states. DIR, if non-zero, means search
314 in that direction from the last cache hit. */
316 bidi_cache_search (EMACS_INT charpos
, int level
, int dir
)
322 if (charpos
< bidi_cache
[bidi_cache_last_idx
].charpos
)
324 else if (charpos
> bidi_cache
[bidi_cache_last_idx
].charpos
)
327 i_start
= bidi_cache_last_idx
;
331 i_start
= bidi_cache_idx
- 1;
336 /* Linear search for now; FIXME! */
337 for (i
= i_start
; i
>= 0; i
--)
338 if (bidi_cache
[i
].charpos
== charpos
339 && (level
== -1 || bidi_cache
[i
].resolved_level
<= level
))
344 for (i
= i_start
; i
< bidi_cache_idx
; i
++)
345 if (bidi_cache
[i
].charpos
== charpos
346 && (level
== -1 || bidi_cache
[i
].resolved_level
<= level
))
354 /* Find a cached state where the resolved level changes to a value
355 that is lower than LEVEL, and return its cache slot index. DIR is
356 the direction to search, starting with the last used cache slot.
357 BEFORE, if non-zero, means return the index of the slot that is
358 ``before'' the level change in the search direction. That is,
359 given the cached levels like this:
364 and assuming we are at the position cached at the slot marked with
365 C, searching backwards (DIR = -1) for LEVEL = 2 will return the
366 index of slot B or A, depending whether BEFORE is, respectively,
369 bidi_cache_find_level_change (int level
, int dir
, int before
)
373 int i
= dir
? bidi_cache_last_idx
: bidi_cache_idx
- 1;
374 int incr
= before
? 1 : 0;
385 if (bidi_cache
[i
- incr
].resolved_level
>= 0
386 && bidi_cache
[i
- incr
].resolved_level
< level
)
393 while (i
< bidi_cache_idx
- incr
)
395 if (bidi_cache
[i
+ incr
].resolved_level
>= 0
396 && bidi_cache
[i
+ incr
].resolved_level
< level
)
407 bidi_cache_iterator_state (struct bidi_it
*bidi_it
, int resolved
)
411 /* We should never cache on backward scans. */
412 if (bidi_it
->scan_dir
== -1)
414 idx
= bidi_cache_search (bidi_it
->charpos
, -1, 1);
418 idx
= bidi_cache_idx
;
419 /* Enlarge the cache as needed. */
420 if (idx
>= bidi_cache_size
)
422 bidi_cache_size
+= BIDI_CACHE_CHUNK
;
424 (struct bidi_it
*) xrealloc (bidi_cache
, bidi_cache_size
* elsz
);
426 /* Character positions should correspond to cache positions 1:1.
427 If we are outside the range of cached positions, the cache is
428 useless and must be reset. */
430 (bidi_it
->charpos
> bidi_cache
[idx
- 1].charpos
+ 1
431 || bidi_it
->charpos
< bidi_cache
[0].charpos
))
436 bidi_copy_it (&bidi_cache
[idx
], bidi_it
);
438 bidi_cache
[idx
].resolved_level
= -1;
442 /* Copy only the members which could have changed, to avoid
443 costly copying of the entire struct. */
444 bidi_cache
[idx
].type
= bidi_it
->type
;
445 bidi_check_type (bidi_it
->type
);
446 bidi_cache
[idx
].type_after_w1
= bidi_it
->type_after_w1
;
447 bidi_check_type (bidi_it
->type_after_w1
);
449 bidi_cache
[idx
].resolved_level
= bidi_it
->resolved_level
;
451 bidi_cache
[idx
].resolved_level
= -1;
452 bidi_cache
[idx
].invalid_levels
= bidi_it
->invalid_levels
;
453 bidi_cache
[idx
].invalid_rl_levels
= bidi_it
->invalid_rl_levels
;
454 bidi_cache
[idx
].next_for_neutral
= bidi_it
->next_for_neutral
;
455 bidi_cache
[idx
].next_for_ws
= bidi_it
->next_for_ws
;
456 bidi_cache
[idx
].ignore_bn_limit
= bidi_it
->ignore_bn_limit
;
459 bidi_cache_last_idx
= idx
;
460 if (idx
>= bidi_cache_idx
)
461 bidi_cache_idx
= idx
+ 1;
464 static INLINE bidi_type_t
465 bidi_cache_find (EMACS_INT charpos
, int level
, struct bidi_it
*bidi_it
)
467 int i
= bidi_cache_search (charpos
, level
, bidi_it
->scan_dir
);
471 bidi_dir_t current_scan_dir
= bidi_it
->scan_dir
;
473 bidi_copy_it (bidi_it
, &bidi_cache
[i
]);
474 bidi_cache_last_idx
= i
;
475 /* Don't let scan direction from from the cached state override
476 the current scan direction. */
477 bidi_it
->scan_dir
= current_scan_dir
;
478 return bidi_it
->type
;
485 bidi_peek_at_next_level (struct bidi_it
*bidi_it
)
487 if (bidi_cache_idx
== 0 || bidi_cache_last_idx
== -1)
489 return bidi_cache
[bidi_cache_last_idx
+ bidi_it
->scan_dir
].resolved_level
;
492 /* Check if buffer position CHARPOS/BYTEPOS is the end of a paragraph.
493 Value is the non-negative length of the paragraph separator
494 following the buffer position, -1 if position is at the beginning
495 of a new paragraph, or -2 if position is neither at beginning nor
496 at end of a paragraph. */
498 bidi_at_paragraph_end (EMACS_INT charpos
, EMACS_INT bytepos
)
501 Lisp_Object start_re
;
504 sep_re
= paragraph_separate_re
;
505 start_re
= paragraph_start_re
;
507 val
= fast_looking_at (sep_re
, charpos
, bytepos
, ZV
, ZV_BYTE
, Qnil
);
510 if (fast_looking_at (start_re
, charpos
, bytepos
, ZV
, ZV_BYTE
, Qnil
) >= 0)
519 /* Determine the start-of-run (sor) directional type given the two
520 embedding levels on either side of the run boundary. Also, update
521 the saved info about previously seen characters, since that info is
522 generally valid for a single level run. */
524 bidi_set_sor_type (struct bidi_it
*bidi_it
, int level_before
, int level_after
)
526 int higher_level
= level_before
> level_after
? level_before
: level_after
;
528 /* The prev_was_pdf gork is required for when we have several PDFs
529 in a row. In that case, we want to compute the sor type for the
530 next level run only once: when we see the first PDF. That's
531 because the sor type depends only on the higher of the two levels
532 that we find on the two sides of the level boundary (see UAX#9,
533 clause X10), and so we don't need to know the final embedding
534 level to which we descend after processing all the PDFs. */
535 if (!bidi_it
->prev_was_pdf
|| level_before
< level_after
)
536 /* FIXME: should the default sor direction be user selectable? */
537 bidi_it
->sor
= (higher_level
& 1) != 0 ? R2L
: L2R
;
538 if (level_before
> level_after
)
539 bidi_it
->prev_was_pdf
= 1;
541 bidi_it
->prev
.type
= UNKNOWN_BT
;
542 bidi_it
->last_strong
.type
= bidi_it
->last_strong
.type_after_w1
=
543 bidi_it
->last_strong
.orig_type
= UNKNOWN_BT
;
544 bidi_it
->prev_for_neutral
.type
= bidi_it
->sor
== R2L
? STRONG_R
: STRONG_L
;
545 bidi_it
->prev_for_neutral
.charpos
= bidi_it
->charpos
;
546 bidi_it
->prev_for_neutral
.bytepos
= bidi_it
->bytepos
;
547 bidi_it
->next_for_neutral
.type
= bidi_it
->next_for_neutral
.type_after_w1
=
548 bidi_it
->next_for_neutral
.orig_type
= UNKNOWN_BT
;
549 bidi_it
->ignore_bn_limit
= 0; /* meaning it's unknown */
553 bidi_line_init (struct bidi_it
*bidi_it
)
555 bidi_it
->scan_dir
= 1; /* FIXME: do we need to have control on this? */
556 bidi_it
->resolved_level
= bidi_it
->level_stack
[0].level
;
557 bidi_it
->level_stack
[0].override
= NEUTRAL_DIR
; /* X1 */
558 bidi_it
->invalid_levels
= 0;
559 bidi_it
->invalid_rl_levels
= -1;
560 bidi_it
->next_en_pos
= -1;
561 bidi_it
->next_for_ws
.type
= UNKNOWN_BT
;
562 bidi_set_sor_type (bidi_it
,
563 bidi_it
->paragraph_dir
== R2L
? 1 : 0,
564 bidi_it
->level_stack
[0].level
); /* X10 */
569 /* Find the beginning of this paragraph by looking back in the buffer.
570 Value is the byte position of the paragraph's beginning. */
572 bidi_find_paragraph_start (EMACS_INT pos
, EMACS_INT pos_byte
)
574 Lisp_Object re
= paragraph_start_re
;
575 EMACS_INT limit
= ZV
, limit_byte
= ZV_BYTE
;
577 while (pos_byte
> BEGV_BYTE
578 && fast_looking_at (re
, pos
, pos_byte
, limit
, limit_byte
, Qnil
) < 0)
580 pos
= find_next_newline_no_quit (pos
- 1, -1);
581 pos_byte
= CHAR_TO_BYTE (pos
);
586 /* Determine the base direction, a.k.a. base embedding level, of the
587 paragraph we are about to iterate through. If DIR is either L2R or
588 R2L, just use that. Otherwise, determine the paragraph direction
589 from the first strong directional character of the paragraph.
591 NO_DEFAULT_P non-nil means don't default to L2R if the paragraph
592 has no strong directional characters and both DIR and
593 bidi_it->paragraph_dir are NEUTRAL_DIR. In that case, search back
594 in the buffer until a paragraph is found with a strong character,
595 or until hitting BEGV. In the latter case, fall back to L2R. This
596 flag is used in current-bidi-paragraph-direction.
598 Note that this function gives the paragraph separator the same
599 direction as the preceding paragraph, even though Emacs generally
600 views the separartor as not belonging to any paragraph. */
602 bidi_paragraph_init (bidi_dir_t dir
, struct bidi_it
*bidi_it
, int no_default_p
)
604 EMACS_INT bytepos
= bidi_it
->bytepos
;
605 EMACS_INT pstartbyte
;
607 /* Special case for an empty buffer. */
608 if (bytepos
== BEGV_BYTE
&& bytepos
== ZV_BYTE
)
610 /* We should never be called at EOB or before BEGV. */
611 else if (bytepos
>= ZV_BYTE
|| bytepos
< BEGV_BYTE
)
616 bidi_it
->paragraph_dir
= L2R
;
617 bidi_it
->new_paragraph
= 0;
621 bidi_it
->paragraph_dir
= R2L
;
622 bidi_it
->new_paragraph
= 0;
624 else if (dir
== NEUTRAL_DIR
) /* P2 */
630 if (!bidi_initialized
)
633 /* If we are inside a paragraph separator, we are just waiting
634 for the separator to be exhausted; use the previous paragraph
635 direction. But don't do that if we have been just reseated,
636 because we need to reinitialize below in that case. */
637 if (!bidi_it
->first_elt
638 && bidi_it
->charpos
< bidi_it
->separator_limit
)
641 /* If we are on a newline, get past it to where the next
642 paragraph might start. But don't do that at BEGV since then
643 we are potentially in a new paragraph that doesn't yet
645 pos
= bidi_it
->charpos
;
646 if (bytepos
> BEGV_BYTE
&& FETCH_CHAR (bytepos
) == '\n')
652 /* We are either at the beginning of a paragraph or in the
653 middle of it. Find where this paragraph starts. */
654 pstartbyte
= bidi_find_paragraph_start (pos
, bytepos
);
655 bidi_it
->separator_limit
= -1;
656 bidi_it
->new_paragraph
= 0;
658 /* The following loop is run more than once only if NO_DEFAULT_P
661 bytepos
= pstartbyte
;
662 ch
= FETCH_CHAR (bytepos
);
663 ch_len
= CHAR_BYTES (ch
);
664 pos
= BYTE_TO_CHAR (bytepos
);
665 type
= bidi_get_type (ch
, NEUTRAL_DIR
);
667 for (pos
++, bytepos
+= ch_len
;
668 /* NOTE: UAX#9 says to search only for L, AL, or R types
669 of characters, and ignore RLE, RLO, LRE, and LRO.
670 However, I'm not sure it makes sense to omit those 4;
671 should try with and without that to see the effect. */
672 (bidi_get_category (type
) != STRONG
)
673 || (bidi_ignore_explicit_marks_for_paragraph_level
674 && (type
== RLE
|| type
== RLO
675 || type
== LRE
|| type
== LRO
));
676 type
= bidi_get_type (ch
, NEUTRAL_DIR
))
678 if (type
== NEUTRAL_B
&& bidi_at_paragraph_end (pos
, bytepos
) >= -1)
680 if (bytepos
>= ZV_BYTE
)
682 /* Pretend there's a paragraph separator at end of
687 FETCH_CHAR_ADVANCE (ch
, pos
, bytepos
);
689 if (type
== STRONG_R
|| type
== STRONG_AL
) /* P3 */
690 bidi_it
->paragraph_dir
= R2L
;
691 else if (type
== STRONG_L
)
692 bidi_it
->paragraph_dir
= L2R
;
693 if (no_default_p
&& bidi_it
->paragraph_dir
== NEUTRAL_DIR
)
695 /* If this paragraph is at BEGV, default to L2R. */
696 if (pstartbyte
== BEGV_BYTE
)
697 bidi_it
->paragraph_dir
= L2R
; /* P3 and HL1 */
700 EMACS_INT prevpbyte
= pstartbyte
;
701 EMACS_INT p
= BYTE_TO_CHAR (pstartbyte
), pbyte
= pstartbyte
;
703 /* Find the beginning of the previous paragraph, if any. */
704 while (pbyte
> BEGV_BYTE
&& prevpbyte
>= pstartbyte
)
707 pbyte
= CHAR_TO_BYTE (p
);
708 prevpbyte
= bidi_find_paragraph_start (p
, pbyte
);
710 pstartbyte
= prevpbyte
;
713 } while (no_default_p
&& bidi_it
->paragraph_dir
== NEUTRAL_DIR
);
718 /* Contrary to UAX#9 clause P3, we only default the paragraph
719 direction to L2R if we have no previous usable paragraph
720 direction. This is allowed by the HL1 clause. */
721 if (bidi_it
->paragraph_dir
!= L2R
&& bidi_it
->paragraph_dir
!= R2L
)
722 bidi_it
->paragraph_dir
= L2R
; /* P3 and HL1 ``higher-level protocols'' */
723 if (bidi_it
->paragraph_dir
== R2L
)
724 bidi_it
->level_stack
[0].level
= 1;
726 bidi_it
->level_stack
[0].level
= 0;
728 bidi_line_init (bidi_it
);
731 /* Do whatever UAX#9 clause X8 says should be done at paragraph's
734 bidi_set_paragraph_end (struct bidi_it
*bidi_it
)
736 bidi_it
->invalid_levels
= 0;
737 bidi_it
->invalid_rl_levels
= -1;
738 bidi_it
->stack_idx
= 0;
739 bidi_it
->resolved_level
= bidi_it
->level_stack
[0].level
;
742 /* Initialize the bidi iterator from buffer position CHARPOS. */
744 bidi_init_it (EMACS_INT charpos
, EMACS_INT bytepos
, struct bidi_it
*bidi_it
)
746 if (! bidi_initialized
)
748 bidi_it
->charpos
= charpos
;
749 bidi_it
->bytepos
= bytepos
;
750 bidi_it
->first_elt
= 1;
751 bidi_set_paragraph_end (bidi_it
);
752 bidi_it
->new_paragraph
= 1;
753 bidi_it
->separator_limit
= -1;
754 bidi_it
->type
= NEUTRAL_B
;
755 bidi_it
->type_after_w1
= NEUTRAL_B
;
756 bidi_it
->orig_type
= NEUTRAL_B
;
757 bidi_it
->prev_was_pdf
= 0;
758 bidi_it
->prev
.type
= bidi_it
->prev
.type_after_w1
=
759 bidi_it
->prev
.orig_type
= UNKNOWN_BT
;
760 bidi_it
->last_strong
.type
= bidi_it
->last_strong
.type_after_w1
=
761 bidi_it
->last_strong
.orig_type
= UNKNOWN_BT
;
762 bidi_it
->next_for_neutral
.charpos
= -1;
763 bidi_it
->next_for_neutral
.type
=
764 bidi_it
->next_for_neutral
.type_after_w1
=
765 bidi_it
->next_for_neutral
.orig_type
= UNKNOWN_BT
;
766 bidi_it
->prev_for_neutral
.charpos
= -1;
767 bidi_it
->prev_for_neutral
.type
=
768 bidi_it
->prev_for_neutral
.type_after_w1
=
769 bidi_it
->prev_for_neutral
.orig_type
= UNKNOWN_BT
;
770 bidi_it
->sor
= L2R
; /* FIXME: should it be user-selectable? */
771 bidi_cache_shrink ();
774 /* Push the current embedding level and override status; reset the
775 current level to LEVEL and the current override status to OVERRIDE. */
777 bidi_push_embedding_level (struct bidi_it
*bidi_it
,
778 int level
, bidi_dir_t override
)
780 bidi_it
->stack_idx
++;
781 if (bidi_it
->stack_idx
>= BIDI_MAXLEVEL
)
783 bidi_it
->level_stack
[bidi_it
->stack_idx
].level
= level
;
784 bidi_it
->level_stack
[bidi_it
->stack_idx
].override
= override
;
787 /* Pop the embedding level and directional override status from the
788 stack, and return the new level. */
790 bidi_pop_embedding_level (struct bidi_it
*bidi_it
)
792 /* UAX#9 says to ignore invalid PDFs. */
793 if (bidi_it
->stack_idx
> 0)
794 bidi_it
->stack_idx
--;
795 return bidi_it
->level_stack
[bidi_it
->stack_idx
].level
;
798 /* Record in SAVED_INFO the information about the current character. */
800 bidi_remember_char (struct bidi_saved_info
*saved_info
,
801 struct bidi_it
*bidi_it
)
803 saved_info
->charpos
= bidi_it
->charpos
;
804 saved_info
->bytepos
= bidi_it
->bytepos
;
805 saved_info
->type
= bidi_it
->type
;
806 bidi_check_type (bidi_it
->type
);
807 saved_info
->type_after_w1
= bidi_it
->type_after_w1
;
808 bidi_check_type (bidi_it
->type_after_w1
);
809 saved_info
->orig_type
= bidi_it
->orig_type
;
810 bidi_check_type (bidi_it
->orig_type
);
813 /* Resolve the type of a neutral character according to the type of
814 surrounding strong text and the current embedding level. */
815 static INLINE bidi_type_t
816 bidi_resolve_neutral_1 (bidi_type_t prev_type
, bidi_type_t next_type
, int lev
)
818 /* N1: European and Arabic numbers are treated as though they were R. */
819 if (next_type
== WEAK_EN
|| next_type
== WEAK_AN
)
820 next_type
= STRONG_R
;
821 if (prev_type
== WEAK_EN
|| prev_type
== WEAK_AN
)
822 prev_type
= STRONG_R
;
824 if (next_type
== prev_type
) /* N1 */
826 else if ((lev
& 1) == 0) /* N2 */
833 bidi_explicit_dir_char (int c
)
835 /* FIXME: this should be replaced with a lookup table with suitable
836 bits set, like standard C ctype macros do. */
837 return (c
== LRE_CHAR
|| c
== LRO_CHAR
838 || c
== RLE_CHAR
|| c
== RLO_CHAR
|| c
== PDF_CHAR
);
841 /* A helper function for bidi_resolve_explicit. It advances to the
842 next character in logical order and determines the new embedding
843 level and directional override, but does not take into account
846 bidi_resolve_explicit_1 (struct bidi_it
*bidi_it
)
854 if (bidi_it
->bytepos
< BEGV_BYTE
/* after reseat to BEGV? */
855 || bidi_it
->first_elt
)
857 bidi_it
->first_elt
= 0;
858 if (bidi_it
->charpos
< BEGV
)
859 bidi_it
->charpos
= BEGV
;
860 bidi_it
->bytepos
= CHAR_TO_BYTE (bidi_it
->charpos
);
862 else if (bidi_it
->bytepos
< ZV_BYTE
) /* don't move at ZV */
865 if (bidi_it
->ch_len
== 0)
867 bidi_it
->bytepos
+= bidi_it
->ch_len
;
870 current_level
= bidi_it
->level_stack
[bidi_it
->stack_idx
].level
; /* X1 */
871 override
= bidi_it
->level_stack
[bidi_it
->stack_idx
].override
;
872 new_level
= current_level
;
874 /* in case it is a unibyte character (not yet implemented) */
875 /* _fetch_multibyte_char_len = 1; */
876 if (bidi_it
->bytepos
>= ZV_BYTE
)
883 curchar
= FETCH_CHAR (bidi_it
->bytepos
);
884 bidi_it
->ch_len
= CHAR_BYTES (curchar
);
886 bidi_it
->ch
= curchar
;
888 /* Don't apply directional override here, as all the types we handle
889 below will not be affected by the override anyway, and we need
890 the original type unaltered. The override will be applied in
891 bidi_resolve_weak. */
892 type
= bidi_get_type (curchar
, NEUTRAL_DIR
);
893 bidi_it
->orig_type
= type
;
894 bidi_check_type (bidi_it
->orig_type
);
897 bidi_it
->prev_was_pdf
= 0;
899 bidi_it
->type_after_w1
= UNKNOWN_BT
;
905 bidi_it
->type_after_w1
= type
;
906 bidi_check_type (bidi_it
->type_after_w1
);
907 type
= WEAK_BN
; /* X9/Retaining */
908 if (bidi_it
->ignore_bn_limit
<= 0)
910 if (current_level
<= BIDI_MAXLEVEL
- 4)
912 /* Compute the least odd embedding level greater than
913 the current level. */
914 new_level
= ((current_level
+ 1) & ~1) + 1;
915 if (bidi_it
->type_after_w1
== RLE
)
916 override
= NEUTRAL_DIR
;
919 if (current_level
== BIDI_MAXLEVEL
- 4)
920 bidi_it
->invalid_rl_levels
= 0;
921 bidi_push_embedding_level (bidi_it
, new_level
, override
);
925 bidi_it
->invalid_levels
++;
926 /* See the commentary about invalid_rl_levels below. */
927 if (bidi_it
->invalid_rl_levels
< 0)
928 bidi_it
->invalid_rl_levels
= 0;
929 bidi_it
->invalid_rl_levels
++;
932 else if (bidi_it
->prev
.type_after_w1
== WEAK_EN
/* W5/Retaining */
933 || bidi_it
->next_en_pos
> bidi_it
->charpos
)
938 bidi_it
->type_after_w1
= type
;
939 bidi_check_type (bidi_it
->type_after_w1
);
940 type
= WEAK_BN
; /* X9/Retaining */
941 if (bidi_it
->ignore_bn_limit
<= 0)
943 if (current_level
<= BIDI_MAXLEVEL
- 5)
945 /* Compute the least even embedding level greater than
946 the current level. */
947 new_level
= ((current_level
+ 2) & ~1);
948 if (bidi_it
->type_after_w1
== LRE
)
949 override
= NEUTRAL_DIR
;
952 bidi_push_embedding_level (bidi_it
, new_level
, override
);
956 bidi_it
->invalid_levels
++;
957 /* invalid_rl_levels counts invalid levels encountered
958 while the embedding level was already too high for
959 LRE/LRO, but not for RLE/RLO. That is because
960 there may be exactly one PDF which we should not
961 ignore even though invalid_levels is non-zero.
962 invalid_rl_levels helps to know what PDF is
964 if (bidi_it
->invalid_rl_levels
>= 0)
965 bidi_it
->invalid_rl_levels
++;
968 else if (bidi_it
->prev
.type_after_w1
== WEAK_EN
/* W5/Retaining */
969 || bidi_it
->next_en_pos
> bidi_it
->charpos
)
973 bidi_it
->type_after_w1
= type
;
974 bidi_check_type (bidi_it
->type_after_w1
);
975 type
= WEAK_BN
; /* X9/Retaining */
976 if (bidi_it
->ignore_bn_limit
<= 0)
978 if (!bidi_it
->invalid_rl_levels
)
980 new_level
= bidi_pop_embedding_level (bidi_it
);
981 bidi_it
->invalid_rl_levels
= -1;
982 if (bidi_it
->invalid_levels
)
983 bidi_it
->invalid_levels
--;
984 /* else nothing: UAX#9 says to ignore invalid PDFs */
986 if (!bidi_it
->invalid_levels
)
987 new_level
= bidi_pop_embedding_level (bidi_it
);
990 bidi_it
->invalid_levels
--;
991 bidi_it
->invalid_rl_levels
--;
994 else if (bidi_it
->prev
.type_after_w1
== WEAK_EN
/* W5/Retaining */
995 || bidi_it
->next_en_pos
> bidi_it
->charpos
)
1003 bidi_it
->type
= type
;
1004 bidi_check_type (bidi_it
->type
);
1009 /* Given an iterator state in BIDI_IT, advance one character position
1010 in the buffer to the next character (in the logical order), resolve
1011 any explicit embeddings and directional overrides, and return the
1012 embedding level of the character after resolving explicit
1013 directives and ignoring empty embeddings. */
1015 bidi_resolve_explicit (struct bidi_it
*bidi_it
)
1017 int prev_level
= bidi_it
->level_stack
[bidi_it
->stack_idx
].level
;
1018 int new_level
= bidi_resolve_explicit_1 (bidi_it
);
1020 if (prev_level
< new_level
1021 && bidi_it
->type
== WEAK_BN
1022 && bidi_it
->ignore_bn_limit
== 0 /* only if not already known */
1023 && bidi_it
->bytepos
< ZV_BYTE
/* not already at EOB */
1024 && bidi_explicit_dir_char (FETCH_CHAR (bidi_it
->bytepos
1025 + bidi_it
->ch_len
)))
1027 /* Avoid pushing and popping embedding levels if the level run
1028 is empty, as this breaks level runs where it shouldn't.
1029 UAX#9 removes all the explicit embedding and override codes,
1030 so empty embeddings disappear without a trace. We need to
1031 behave as if we did the same. */
1032 struct bidi_it saved_it
;
1033 int level
= prev_level
;
1035 bidi_copy_it (&saved_it
, bidi_it
);
1037 while (bidi_explicit_dir_char (FETCH_CHAR (bidi_it
->bytepos
1038 + bidi_it
->ch_len
)))
1040 level
= bidi_resolve_explicit_1 (bidi_it
);
1043 if (level
== prev_level
) /* empty embedding */
1044 saved_it
.ignore_bn_limit
= bidi_it
->charpos
+ 1;
1045 else /* this embedding is non-empty */
1046 saved_it
.ignore_bn_limit
= -1;
1048 bidi_copy_it (bidi_it
, &saved_it
);
1049 if (bidi_it
->ignore_bn_limit
> 0)
1051 /* We pushed a level, but we shouldn't have. Undo that. */
1052 if (!bidi_it
->invalid_rl_levels
)
1054 new_level
= bidi_pop_embedding_level (bidi_it
);
1055 bidi_it
->invalid_rl_levels
= -1;
1056 if (bidi_it
->invalid_levels
)
1057 bidi_it
->invalid_levels
--;
1059 if (!bidi_it
->invalid_levels
)
1060 new_level
= bidi_pop_embedding_level (bidi_it
);
1063 bidi_it
->invalid_levels
--;
1064 bidi_it
->invalid_rl_levels
--;
1069 if (bidi_it
->type
== NEUTRAL_B
) /* X8 */
1071 bidi_set_paragraph_end (bidi_it
);
1072 /* This is needed by bidi_resolve_weak below, and in L1. */
1073 bidi_it
->type_after_w1
= bidi_it
->type
;
1074 bidi_check_type (bidi_it
->type_after_w1
);
1080 /* Advance in the buffer, resolve weak types and return the type of
1081 the next character after weak type resolution. */
1083 bidi_resolve_weak (struct bidi_it
*bidi_it
)
1086 bidi_dir_t override
;
1087 int prev_level
= bidi_it
->level_stack
[bidi_it
->stack_idx
].level
;
1088 int new_level
= bidi_resolve_explicit (bidi_it
);
1090 bidi_type_t type_of_next
;
1091 struct bidi_it saved_it
;
1093 type
= bidi_it
->type
;
1094 override
= bidi_it
->level_stack
[bidi_it
->stack_idx
].override
;
1096 if (type
== UNKNOWN_BT
1104 if (new_level
!= prev_level
1105 || bidi_it
->type
== NEUTRAL_B
)
1107 /* We've got a new embedding level run, compute the directional
1108 type of sor and initialize per-run variables (UAX#9, clause
1110 bidi_set_sor_type (bidi_it
, prev_level
, new_level
);
1112 else if (type
== NEUTRAL_S
|| type
== NEUTRAL_WS
1113 || type
== WEAK_BN
|| type
== STRONG_AL
)
1114 bidi_it
->type_after_w1
= type
; /* needed in L1 */
1115 bidi_check_type (bidi_it
->type_after_w1
);
1117 /* Level and directional override status are already recorded in
1118 bidi_it, and do not need any change; see X6. */
1119 if (override
== R2L
) /* X6 */
1121 else if (override
== L2R
)
1125 if (type
== WEAK_NSM
) /* W1 */
1127 /* Note that we don't need to consider the case where the
1128 prev character has its type overridden by an RLO or LRO,
1129 because then either the type of this NSM would have been
1130 also overridden, or the previous character is outside the
1131 current level run, and thus not relevant to this NSM.
1132 This is why NSM gets the type_after_w1 of the previous
1134 if (bidi_it
->prev
.type_after_w1
!= UNKNOWN_BT
1135 /* if type_after_w1 is NEUTRAL_B, this NSM is at sor */
1136 && bidi_it
->prev
.type_after_w1
!= NEUTRAL_B
)
1137 type
= bidi_it
->prev
.type_after_w1
;
1138 else if (bidi_it
->sor
== R2L
)
1140 else if (bidi_it
->sor
== L2R
)
1142 else /* shouldn't happen! */
1145 if (type
== WEAK_EN
/* W2 */
1146 && bidi_it
->last_strong
.type_after_w1
== STRONG_AL
)
1148 else if (type
== STRONG_AL
) /* W3 */
1150 else if ((type
== WEAK_ES
/* W4 */
1151 && bidi_it
->prev
.type_after_w1
== WEAK_EN
1152 && bidi_it
->prev
.orig_type
== WEAK_EN
)
1154 && ((bidi_it
->prev
.type_after_w1
== WEAK_EN
1155 && bidi_it
->prev
.orig_type
== WEAK_EN
)
1156 || bidi_it
->prev
.type_after_w1
== WEAK_AN
)))
1159 bidi_it
->bytepos
+ bidi_it
->ch_len
>= ZV_BYTE
1160 ? BIDI_EOB
: FETCH_CHAR (bidi_it
->bytepos
+ bidi_it
->ch_len
);
1161 type_of_next
= bidi_get_type (next_char
, override
);
1163 if (type_of_next
== WEAK_BN
1164 || bidi_explicit_dir_char (next_char
))
1166 bidi_copy_it (&saved_it
, bidi_it
);
1167 while (bidi_resolve_explicit (bidi_it
) == new_level
1168 && bidi_it
->type
== WEAK_BN
)
1170 type_of_next
= bidi_it
->type
;
1171 bidi_copy_it (bidi_it
, &saved_it
);
1174 /* If the next character is EN, but the last strong-type
1175 character is AL, that next EN will be changed to AN when
1176 we process it in W2 above. So in that case, this ES
1177 should not be changed into EN. */
1179 && type_of_next
== WEAK_EN
1180 && bidi_it
->last_strong
.type_after_w1
!= STRONG_AL
)
1182 else if (type
== WEAK_CS
)
1184 if (bidi_it
->prev
.type_after_w1
== WEAK_AN
1185 && (type_of_next
== WEAK_AN
1186 /* If the next character is EN, but the last
1187 strong-type character is AL, EN will be later
1188 changed to AN when we process it in W2 above.
1189 So in that case, this ES should not be
1191 || (type_of_next
== WEAK_EN
1192 && bidi_it
->last_strong
.type_after_w1
== STRONG_AL
)))
1194 else if (bidi_it
->prev
.type_after_w1
== WEAK_EN
1195 && type_of_next
== WEAK_EN
1196 && bidi_it
->last_strong
.type_after_w1
!= STRONG_AL
)
1200 else if (type
== WEAK_ET
/* W5: ET with EN before or after it */
1201 || type
== WEAK_BN
) /* W5/Retaining */
1203 if (bidi_it
->prev
.type_after_w1
== WEAK_EN
/* ET/BN w/EN before it */
1204 || bidi_it
->next_en_pos
> bidi_it
->charpos
)
1206 else /* W5: ET/BN with EN after it. */
1208 EMACS_INT en_pos
= bidi_it
->charpos
+ 1;
1211 bidi_it
->bytepos
+ bidi_it
->ch_len
>= ZV_BYTE
1212 ? BIDI_EOB
: FETCH_CHAR (bidi_it
->bytepos
+ bidi_it
->ch_len
);
1213 type_of_next
= bidi_get_type (next_char
, override
);
1215 if (type_of_next
== WEAK_ET
1216 || type_of_next
== WEAK_BN
1217 || bidi_explicit_dir_char (next_char
))
1219 bidi_copy_it (&saved_it
, bidi_it
);
1220 while (bidi_resolve_explicit (bidi_it
) == new_level
1221 && (bidi_it
->type
== WEAK_BN
1222 || bidi_it
->type
== WEAK_ET
))
1224 type_of_next
= bidi_it
->type
;
1225 en_pos
= bidi_it
->charpos
;
1226 bidi_copy_it (bidi_it
, &saved_it
);
1228 if (type_of_next
== WEAK_EN
)
1230 /* If the last strong character is AL, the EN we've
1231 found will become AN when we get to it (W2). */
1232 if (bidi_it
->last_strong
.type_after_w1
!= STRONG_AL
)
1235 /* Remember this EN position, to speed up processing
1237 bidi_it
->next_en_pos
= en_pos
;
1239 else if (type
== WEAK_BN
)
1240 type
= NEUTRAL_ON
; /* W6/Retaining */
1246 if (type
== WEAK_ES
|| type
== WEAK_ET
|| type
== WEAK_CS
/* W6 */
1248 && (bidi_it
->prev
.type_after_w1
== WEAK_CS
/* W6/Retaining */
1249 || bidi_it
->prev
.type_after_w1
== WEAK_ES
1250 || bidi_it
->prev
.type_after_w1
== WEAK_ET
)))
1253 /* Store the type we've got so far, before we clobber it with strong
1254 types in W7 and while resolving neutral types. But leave alone
1255 the original types that were recorded above, because we will need
1256 them for the L1 clause. */
1257 if (bidi_it
->type_after_w1
== UNKNOWN_BT
)
1258 bidi_it
->type_after_w1
= type
;
1259 bidi_check_type (bidi_it
->type_after_w1
);
1261 if (type
== WEAK_EN
) /* W7 */
1263 if ((bidi_it
->last_strong
.type_after_w1
== STRONG_L
)
1264 || (bidi_it
->last_strong
.type
== UNKNOWN_BT
&& bidi_it
->sor
== L2R
))
1268 bidi_it
->type
= type
;
1269 bidi_check_type (bidi_it
->type
);
1274 bidi_resolve_neutral (struct bidi_it
*bidi_it
)
1276 int prev_level
= bidi_it
->level_stack
[bidi_it
->stack_idx
].level
;
1277 bidi_type_t type
= bidi_resolve_weak (bidi_it
);
1278 int current_level
= bidi_it
->level_stack
[bidi_it
->stack_idx
].level
;
1280 if (!(type
== STRONG_R
1285 || type
== NEUTRAL_B
1286 || type
== NEUTRAL_S
1287 || type
== NEUTRAL_WS
1288 || type
== NEUTRAL_ON
))
1291 if (bidi_get_category (type
) == NEUTRAL
1292 || (type
== WEAK_BN
&& prev_level
== current_level
))
1294 if (bidi_it
->next_for_neutral
.type
!= UNKNOWN_BT
)
1295 type
= bidi_resolve_neutral_1 (bidi_it
->prev_for_neutral
.type
,
1296 bidi_it
->next_for_neutral
.type
,
1300 /* Arrrgh!! The UAX#9 algorithm is too deeply entrenched in
1301 the assumption of batch-style processing; see clauses W4,
1302 W5, and especially N1, which require to look far forward
1303 (as well as back) in the buffer. May the fleas of a
1304 thousand camels infest the armpits of those who design
1305 supposedly general-purpose algorithms by looking at their
1306 own implementations, and fail to consider other possible
1308 struct bidi_it saved_it
;
1309 bidi_type_t next_type
;
1311 if (bidi_it
->scan_dir
== -1)
1314 bidi_copy_it (&saved_it
, bidi_it
);
1315 /* Scan the text forward until we find the first non-neutral
1316 character, and then use that to resolve the neutral we
1317 are dealing with now. We also cache the scanned iterator
1318 states, to salvage some of the effort later. */
1319 bidi_cache_iterator_state (bidi_it
, 0);
1321 /* Record the info about the previous character, so that
1322 it will be cached below with this state. */
1323 if (bidi_it
->type_after_w1
!= WEAK_BN
/* W1/Retaining */
1324 && bidi_it
->type
!= WEAK_BN
)
1325 bidi_remember_char (&bidi_it
->prev
, bidi_it
);
1326 type
= bidi_resolve_weak (bidi_it
);
1327 /* Paragraph separators have their levels fully resolved
1328 at this point, so cache them as resolved. */
1329 bidi_cache_iterator_state (bidi_it
, type
== NEUTRAL_B
);
1330 /* FIXME: implement L1 here, by testing for a newline and
1331 resetting the level for any sequence of whitespace
1332 characters adjacent to it. */
1333 } while (!(type
== NEUTRAL_B
1335 && bidi_get_category (type
) != NEUTRAL
)
1336 /* This is all per level run, so stop when we
1337 reach the end of this level run. */
1338 || bidi_it
->level_stack
[bidi_it
->stack_idx
].level
!=
1341 bidi_remember_char (&saved_it
.next_for_neutral
, bidi_it
);
1352 /* N1: ``European and Arabic numbers are treated as
1353 though they were R.'' */
1354 next_type
= STRONG_R
;
1355 saved_it
.next_for_neutral
.type
= STRONG_R
;
1358 if (!bidi_explicit_dir_char (bidi_it
->ch
))
1359 abort (); /* can't happen: BNs are skipped */
1362 /* Marched all the way to the end of this level run.
1363 We need to use the eor type, whose information is
1364 stored by bidi_set_sor_type in the prev_for_neutral
1366 if (saved_it
.type
!= WEAK_BN
1367 || bidi_get_category (bidi_it
->prev
.type_after_w1
) == NEUTRAL
)
1369 next_type
= bidi_it
->prev_for_neutral
.type
;
1370 saved_it
.next_for_neutral
.type
= next_type
;
1371 bidi_check_type (next_type
);
1375 /* This is a BN which does not adjoin neutrals.
1376 Leave its type alone. */
1377 bidi_copy_it (bidi_it
, &saved_it
);
1378 return bidi_it
->type
;
1384 type
= bidi_resolve_neutral_1 (saved_it
.prev_for_neutral
.type
,
1385 next_type
, current_level
);
1386 saved_it
.type
= type
;
1387 bidi_check_type (type
);
1388 bidi_copy_it (bidi_it
, &saved_it
);
1394 /* Given an iterator state in BIDI_IT, advance one character position
1395 in the buffer to the next character (in the logical order), resolve
1396 the bidi type of that next character, and return that type. */
1398 bidi_type_of_next_char (struct bidi_it
*bidi_it
)
1402 /* This should always be called during a forward scan. */
1403 if (bidi_it
->scan_dir
!= 1)
1406 /* Reset the limit until which to ignore BNs if we step out of the
1407 area where we found only empty levels. */
1408 if ((bidi_it
->ignore_bn_limit
> 0
1409 && bidi_it
->ignore_bn_limit
<= bidi_it
->charpos
)
1410 || (bidi_it
->ignore_bn_limit
== -1
1411 && !bidi_explicit_dir_char (bidi_it
->ch
)))
1412 bidi_it
->ignore_bn_limit
= 0;
1414 type
= bidi_resolve_neutral (bidi_it
);
1419 /* Given an iterator state BIDI_IT, advance one character position in
1420 the buffer to the next character (in the logical order), resolve
1421 the embedding and implicit levels of that next character, and
1422 return the resulting level. */
1424 bidi_level_of_next_char (struct bidi_it
*bidi_it
)
1427 int level
, prev_level
= -1;
1428 struct bidi_saved_info next_for_neutral
;
1430 if (bidi_it
->scan_dir
== 1)
1432 /* There's no sense in trying to advance if we hit end of text. */
1433 if (bidi_it
->bytepos
>= ZV_BYTE
)
1434 return bidi_it
->resolved_level
;
1436 /* Record the info about the previous character. */
1437 if (bidi_it
->type_after_w1
!= WEAK_BN
/* W1/Retaining */
1438 && bidi_it
->type
!= WEAK_BN
)
1439 bidi_remember_char (&bidi_it
->prev
, bidi_it
);
1440 if (bidi_it
->type_after_w1
== STRONG_R
1441 || bidi_it
->type_after_w1
== STRONG_L
1442 || bidi_it
->type_after_w1
== STRONG_AL
)
1443 bidi_remember_char (&bidi_it
->last_strong
, bidi_it
);
1444 /* FIXME: it sounds like we don't need both prev and
1445 prev_for_neutral members, but I'm leaving them both for now. */
1446 if (bidi_it
->type
== STRONG_R
|| bidi_it
->type
== STRONG_L
1447 || bidi_it
->type
== WEAK_EN
|| bidi_it
->type
== WEAK_AN
)
1448 bidi_remember_char (&bidi_it
->prev_for_neutral
, bidi_it
);
1450 /* If we overstepped the characters used for resolving neutrals
1451 and whitespace, invalidate their info in the iterator. */
1452 if (bidi_it
->charpos
>= bidi_it
->next_for_neutral
.charpos
)
1453 bidi_it
->next_for_neutral
.type
= UNKNOWN_BT
;
1454 if (bidi_it
->next_en_pos
>= 0
1455 && bidi_it
->charpos
>= bidi_it
->next_en_pos
)
1456 bidi_it
->next_en_pos
= -1;
1457 if (bidi_it
->next_for_ws
.type
!= UNKNOWN_BT
1458 && bidi_it
->charpos
>= bidi_it
->next_for_ws
.charpos
)
1459 bidi_it
->next_for_ws
.type
= UNKNOWN_BT
;
1461 /* This must be taken before we fill the iterator with the info
1462 about the next char. If we scan backwards, the iterator
1463 state must be already cached, so there's no need to know the
1464 embedding level of the previous character, since we will be
1465 returning to our caller shortly. */
1466 prev_level
= bidi_it
->level_stack
[bidi_it
->stack_idx
].level
;
1468 next_for_neutral
= bidi_it
->next_for_neutral
;
1470 /* Perhaps it is already cached. */
1471 type
= bidi_cache_find (bidi_it
->charpos
+ bidi_it
->scan_dir
, -1, bidi_it
);
1472 if (type
!= UNKNOWN_BT
)
1474 /* Don't lose the information for resolving neutrals! The
1475 cached states could have been cached before their
1476 next_for_neutral member was computed. If we are on our way
1477 forward, we can simply take the info from the previous
1479 if (bidi_it
->scan_dir
== 1
1480 && bidi_it
->next_for_neutral
.type
== UNKNOWN_BT
)
1481 bidi_it
->next_for_neutral
= next_for_neutral
;
1483 /* If resolved_level is -1, it means this state was cached
1484 before it was completely resolved, so we cannot return
1486 if (bidi_it
->resolved_level
!= -1)
1487 return bidi_it
->resolved_level
;
1489 if (bidi_it
->scan_dir
== -1)
1490 /* If we are going backwards, the iterator state is already cached
1491 from previous scans, and should be fully resolved. */
1494 if (type
== UNKNOWN_BT
)
1495 type
= bidi_type_of_next_char (bidi_it
);
1497 if (type
== NEUTRAL_B
)
1498 return bidi_it
->resolved_level
;
1500 level
= bidi_it
->level_stack
[bidi_it
->stack_idx
].level
;
1501 if ((bidi_get_category (type
) == NEUTRAL
/* && type != NEUTRAL_B */)
1502 || (type
== WEAK_BN
&& prev_level
== level
))
1504 if (bidi_it
->next_for_neutral
.type
== UNKNOWN_BT
)
1507 /* If the cached state shows a neutral character, it was not
1508 resolved by bidi_resolve_neutral, so do it now. */
1509 type
= bidi_resolve_neutral_1 (bidi_it
->prev_for_neutral
.type
,
1510 bidi_it
->next_for_neutral
.type
,
1514 if (!(type
== STRONG_R
1518 || type
== WEAK_AN
))
1520 bidi_it
->type
= type
;
1521 bidi_check_type (bidi_it
->type
);
1523 /* For L1 below, we need to know, for each WS character, whether
1524 it belongs to a sequence of WS characters preceeding a newline
1525 or a TAB or a paragraph separator. */
1526 if (bidi_it
->orig_type
== NEUTRAL_WS
1527 && bidi_it
->next_for_ws
.type
== UNKNOWN_BT
)
1530 int clen
= bidi_it
->ch_len
;
1531 EMACS_INT bpos
= bidi_it
->bytepos
;
1532 EMACS_INT cpos
= bidi_it
->charpos
;
1536 /*_fetch_multibyte_char_len = 1;*/
1537 ch
= bpos
+ clen
>= ZV_BYTE
? BIDI_EOB
: FETCH_CHAR (bpos
+ clen
);
1540 clen
= (ch
== BIDI_EOB
? 1 : CHAR_BYTES (ch
));
1541 if (ch
== '\n' || ch
== BIDI_EOB
/* || ch == LINESEP_CHAR */)
1544 chtype
= bidi_get_type (ch
, NEUTRAL_DIR
);
1545 } while (chtype
== NEUTRAL_WS
|| chtype
== WEAK_BN
1546 || bidi_explicit_dir_char (ch
)); /* L1/Retaining */
1547 bidi_it
->next_for_ws
.type
= chtype
;
1548 bidi_check_type (bidi_it
->next_for_ws
.type
);
1549 bidi_it
->next_for_ws
.charpos
= cpos
;
1550 bidi_it
->next_for_ws
.bytepos
= bpos
;
1553 /* Resolve implicit levels, with a twist: PDFs get the embedding
1554 level of the enbedding they terminate. See below for the
1556 if (bidi_it
->orig_type
== PDF
1557 /* Don't do this if this formatting code didn't change the
1558 embedding level due to invalid or empty embeddings. */
1559 && prev_level
!= level
)
1561 /* Don't look in UAX#9 for the reason for this: it's our own
1562 private quirk. The reason is that we want the formatting
1563 codes to be delivered so that they bracket the text of their
1564 embedding. For example, given the text
1568 we want it to be displayed as
1576 which will result because we bump up the embedding level as
1577 soon as we see the RLO and pop it as soon as we see the PDF,
1578 so RLO itself has the same embedding level as "teST", and
1579 thus would be normally delivered last, just before the PDF.
1580 The switch below fiddles with the level of PDF so that this
1581 ugly side effect does not happen.
1583 (This is, of course, only important if the formatting codes
1584 are actually displayed, but Emacs does need to display them
1585 if the user wants to.) */
1588 else if (bidi_it
->orig_type
== NEUTRAL_B
/* L1 */
1589 || bidi_it
->orig_type
== NEUTRAL_S
1590 || bidi_it
->ch
== '\n' || bidi_it
->ch
== BIDI_EOB
1591 /* || bidi_it->ch == LINESEP_CHAR */
1592 || (bidi_it
->orig_type
== NEUTRAL_WS
1593 && (bidi_it
->next_for_ws
.type
== NEUTRAL_B
1594 || bidi_it
->next_for_ws
.type
== NEUTRAL_S
)))
1595 level
= bidi_it
->level_stack
[0].level
;
1596 else if ((level
& 1) == 0) /* I1 */
1598 if (type
== STRONG_R
)
1600 else if (type
== WEAK_EN
|| type
== WEAK_AN
)
1605 if (type
== STRONG_L
|| type
== WEAK_EN
|| type
== WEAK_AN
)
1609 bidi_it
->resolved_level
= level
;
1613 /* Move to the other edge of a level given by LEVEL. If END_FLAG is
1614 non-zero, we are at the end of a level, and we need to prepare to
1615 resume the scan of the lower level.
1617 If this level's other edge is cached, we simply jump to it, filling
1618 the iterator structure with the iterator state on the other edge.
1619 Otherwise, we walk the buffer until we come back to the same level
1622 Note: we are not talking here about a ``level run'' in the UAX#9
1623 sense of the term, but rather about a ``level'' which includes
1624 all the levels higher than it. In other words, given the levels
1627 11111112222222333333334443343222222111111112223322111
1630 and assuming we are at point A scanning left to right, this
1631 function moves to point C, whereas the UAX#9 ``level 2 run'' ends
1634 bidi_find_other_level_edge (struct bidi_it
*bidi_it
, int level
, int end_flag
)
1636 int dir
= end_flag
? -bidi_it
->scan_dir
: bidi_it
->scan_dir
;
1639 /* Try the cache first. */
1640 if ((idx
= bidi_cache_find_level_change (level
, dir
, end_flag
)) >= 0)
1641 bidi_cache_fetch_state (idx
, bidi_it
);
1647 abort (); /* if we are at end of level, its edges must be cached */
1649 bidi_cache_iterator_state (bidi_it
, 1);
1651 new_level
= bidi_level_of_next_char (bidi_it
);
1652 bidi_cache_iterator_state (bidi_it
, 1);
1653 } while (new_level
>= level
);
1658 bidi_move_to_visually_next (struct bidi_it
*bidi_it
)
1660 int old_level
, new_level
, next_level
;
1661 struct bidi_it sentinel
;
1663 if (bidi_it
->scan_dir
== 0)
1665 bidi_it
->scan_dir
= 1; /* default to logical order */
1668 /* If we just passed a newline, initialize for the next line. */
1669 if (!bidi_it
->first_elt
&& bidi_it
->orig_type
== NEUTRAL_B
)
1670 bidi_line_init (bidi_it
);
1672 /* Prepare the sentinel iterator state, and cache it. When we bump
1673 into it, scanning backwards, we'll know that the last non-base
1674 level is exhausted. */
1675 if (bidi_cache_idx
== 0)
1677 bidi_copy_it (&sentinel
, bidi_it
);
1678 if (bidi_it
->first_elt
)
1680 sentinel
.charpos
--; /* cached charpos needs to be monotonic */
1682 sentinel
.ch
= '\n'; /* doesn't matter, but why not? */
1683 sentinel
.ch_len
= 1;
1685 bidi_cache_iterator_state (&sentinel
, 1);
1688 old_level
= bidi_it
->resolved_level
;
1689 new_level
= bidi_level_of_next_char (bidi_it
);
1691 /* Reordering of resolved levels (clause L2) is implemented by
1692 jumping to the other edge of the level and flipping direction of
1693 scanning the text whenever we find a level change. */
1694 if (new_level
!= old_level
)
1696 int ascending
= new_level
> old_level
;
1697 int level_to_search
= ascending
? old_level
+ 1 : old_level
;
1698 int incr
= ascending
? 1 : -1;
1699 int expected_next_level
= old_level
+ incr
;
1701 /* Jump (or walk) to the other edge of this level. */
1702 bidi_find_other_level_edge (bidi_it
, level_to_search
, !ascending
);
1703 /* Switch scan direction and peek at the next character in the
1705 bidi_it
->scan_dir
= -bidi_it
->scan_dir
;
1707 /* The following loop handles the case where the resolved level
1708 jumps by more than one. This is typical for numbers inside a
1709 run of text with left-to-right embedding direction, but can
1710 also happen in other situations. In those cases the decision
1711 where to continue after a level change, and in what direction,
1712 is tricky. For example, given a text like below:
1717 (where the numbers below the text show the resolved levels),
1718 the result of reordering according to UAX#9 should be this:
1722 This is implemented by the loop below which flips direction
1723 and jumps to the other edge of the level each time it finds
1724 the new level not to be the expected one. The expected level
1725 is always one more or one less than the previous one. */
1726 next_level
= bidi_peek_at_next_level (bidi_it
);
1727 while (next_level
!= expected_next_level
)
1729 expected_next_level
+= incr
;
1730 level_to_search
+= incr
;
1731 bidi_find_other_level_edge (bidi_it
, level_to_search
, !ascending
);
1732 bidi_it
->scan_dir
= -bidi_it
->scan_dir
;
1733 next_level
= bidi_peek_at_next_level (bidi_it
);
1736 /* Finally, deliver the next character in the new direction. */
1737 next_level
= bidi_level_of_next_char (bidi_it
);
1740 /* Take note when we have just processed the newline that precedes
1741 the end of the paragraph. The next time we are about to be
1742 called, set_iterator_to_next will automatically reinit the
1743 paragraph direction, if needed. We do this at the newline before
1744 the paragraph separator, because the next character might not be
1745 the first character of the next paragraph, due to the bidi
1746 reordering, whereas we _must_ know the paragraph base direction
1747 _before_ we process the paragraph's text, since the base
1748 direction affects the reordering. */
1749 if (bidi_it
->scan_dir
== 1
1750 && bidi_it
->orig_type
== NEUTRAL_B
1751 && bidi_it
->bytepos
< ZV_BYTE
)
1754 bidi_at_paragraph_end (bidi_it
->charpos
+ 1,
1755 bidi_it
->bytepos
+ bidi_it
->ch_len
);
1758 bidi_it
->new_paragraph
= 1;
1759 /* Record the buffer position of the last character of the
1760 paragraph separator. */
1761 bidi_it
->separator_limit
= bidi_it
->charpos
+ 1 + sep_len
;
1765 if (bidi_it
->scan_dir
== 1 && bidi_cache_idx
)
1767 /* If we are at paragraph's base embedding level and beyond the
1768 last cached position, the cache's job is done and we can
1770 if (bidi_it
->resolved_level
== bidi_it
->level_stack
[0].level
1771 && bidi_it
->charpos
> bidi_cache
[bidi_cache_idx
- 1].charpos
)
1772 bidi_cache_reset ();
1773 /* But as long as we are caching during forward scan, we must
1774 cache each state, or else the cache integrity will be
1775 compromised: it assumes cached states correspond to buffer
1778 bidi_cache_iterator_state (bidi_it
, 1);
1782 /* This is meant to be called from within the debugger, whenever you
1783 wish to examine the cache contents. */
1785 bidi_dump_cached_states (void)
1790 if (bidi_cache_idx
== 0)
1792 fprintf (stderr
, "The cache is empty.\n");
1795 fprintf (stderr
, "Total of %d state%s in cache:\n",
1796 bidi_cache_idx
, bidi_cache_idx
== 1 ? "" : "s");
1798 for (i
= bidi_cache
[bidi_cache_idx
- 1].charpos
; i
> 0; i
/= 10)
1800 fputs ("ch ", stderr
);
1801 for (i
= 0; i
< bidi_cache_idx
; i
++)
1802 fprintf (stderr
, "%*c", ndigits
, bidi_cache
[i
].ch
);
1803 fputs ("\n", stderr
);
1804 fputs ("lvl ", stderr
);
1805 for (i
= 0; i
< bidi_cache_idx
; i
++)
1806 fprintf (stderr
, "%*d", ndigits
, bidi_cache
[i
].resolved_level
);
1807 fputs ("\n", stderr
);
1808 fputs ("pos ", stderr
);
1809 for (i
= 0; i
< bidi_cache_idx
; i
++)
1810 fprintf (stderr
, "%*ld", ndigits
, (long)bidi_cache
[i
].charpos
);
1811 fputs ("\n", stderr
);