+
+ /* The following is an optimization for bracketed text that has
+ only one level which is equal to the paragraph's base
+ embedding level. That is, only L2R and weak/neutral
+ characters in a L2R paragraph, or only R2L and weak/neutral
+ characters in a R2L paragraph. Such brackets can be resolved
+ by bidi_resolve_neutral, which has a further shortcut for
+ this case. So we pretend we did not resolve the brackets in
+ this case, set up next_for_neutral for the entire bracketed
+ text, and reset the cache to the character before the opening
+ bracket. The upshot is to allow bidi_move_to_visually_next
+ reset the cache when it returns this opening bracket, thus
+ cutting significantly on the size of the cache, which is
+ important with long lines, especially if word-wrap is non-nil
+ (which requires the display engine to copy the cache back and
+ forth many times). */
+ if (maxlevel == base_level
+ && ((base_level == 0 && !r2l_seen)
+ || (base_level == 1 && !l2r_seen)))
+ {
+ ptrdiff_t eob
+ = ((bidi_it->string.s || STRINGP (bidi_it->string.lstring))
+ ? bidi_it->string.schars : ZV);
+
+ if (retval)
+ pairing_pos = bidi_it->bracket_pairing_pos;
+
+ /* This special value (which cannot possibly happen when
+ brackets are resolved, since there's no character at ZV)
+ will be noticed by bidi_resolve_explicit, and will be
+ copied to the following iterator states, instead of being
+ reset to -1. */
+ bidi_it->bracket_pairing_pos = eob;
+ /* This type value will be used for resolving the outermost
+ closing bracket in bidi_resolve_brackets. */
+ bidi_it->bracket_enclosed_type = embedding_type;
+ /* bidi_cache_last_idx is set to the index of the current
+ state, because we just called bidi_cache_find above.
+ That state describes the outermost opening bracket, the
+ one with which we entered this function. Force the cache
+ to "forget" all the cached states starting from that state. */
+ bidi_cache_reset_to (bidi_cache_last_idx - bidi_cache_start);
+ /* Set up the next_for_neutral member, to help
+ bidi_resolve_neutral. */
+ bidi_it->next_for_neutral.type = embedding_type;
+ bidi_it->next_for_neutral.charpos = pairing_pos;
+ /* Pretend we didn't resolve this bracket. */
+ retval = false;
+ }