aboutsummaryrefslogtreecommitdiffstats
path: root/src/xdisp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/xdisp.c')
-rw-r--r--src/xdisp.c3001
1 files changed, 2342 insertions, 659 deletions
diff --git a/src/xdisp.c b/src/xdisp.c
index 774bc22699a..0d23d72dd7b 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -1,6 +1,6 @@
1/* Display generation from window structure and buffer text. 1/* Display generation from window structure and buffer text.
2 2
3Copyright (C) 1985-1988, 1993-1995, 1997-2011 Free Software Foundation, Inc. 3Copyright (C) 1985-1988, 1993-1995, 1997-2012 Free Software Foundation, Inc.
4 4
5This file is part of GNU Emacs. 5This file is part of GNU Emacs.
6 6
@@ -129,9 +129,13 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
129 argument. 129 argument.
130 130
131 Iteration over things to be displayed is then simple. It is 131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator. 132 started by initializing an iterator with a call to init_iterator,
133 Calls to get_next_display_element fill the iterator structure with 133 passing it the buffer position where to start iteration. For
134 relevant information about the next thing to display. Calls to 134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
135 set_iterator_to_next move the iterator to the next thing. 139 set_iterator_to_next move the iterator to the next thing.
136 140
137 Besides this, an iterator also contains information about the 141 Besides this, an iterator also contains information about the
@@ -590,6 +594,29 @@ int current_mode_line_height, current_header_line_height;
590 594
591#define TEXT_PROP_DISTANCE_LIMIT 100 595#define TEXT_PROP_DISTANCE_LIMIT 100
592 596
597/* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
598 iterator state and later restore it. This is needed because the
599 bidi iterator on bidi.c keeps a stacked cache of its states, which
600 is really a singleton. When we use scratch iterator objects to
601 move around the buffer, we can cause the bidi cache to be pushed or
602 popped, and therefore we need to restore the cache state when we
603 return to the original iterator. */
604#define SAVE_IT(ITCOPY,ITORIG,CACHE) \
605 do { \
606 if (CACHE) \
607 bidi_unshelve_cache (CACHE, 1); \
608 ITCOPY = ITORIG; \
609 CACHE = bidi_shelve_cache (); \
610 } while (0)
611
612#define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
613 do { \
614 if (pITORIG != pITCOPY) \
615 *(pITORIG) = *(pITCOPY); \
616 bidi_unshelve_cache (CACHE, 0); \
617 CACHE = NULL; \
618 } while (0)
619
593#if GLYPH_DEBUG 620#if GLYPH_DEBUG
594 621
595/* Non-zero means print traces of redisplay if compiled with 622/* Non-zero means print traces of redisplay if compiled with
@@ -872,7 +899,7 @@ static void init_to_row_start (struct it *, struct window *,
872static int init_to_row_end (struct it *, struct window *, 899static int init_to_row_end (struct it *, struct window *,
873 struct glyph_row *); 900 struct glyph_row *);
874static void back_to_previous_line_start (struct it *); 901static void back_to_previous_line_start (struct it *);
875static int forward_to_next_line_start (struct it *, int *); 902static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
876static struct text_pos string_pos_nchars_ahead (struct text_pos, 903static struct text_pos string_pos_nchars_ahead (struct text_pos,
877 Lisp_Object, EMACS_INT); 904 Lisp_Object, EMACS_INT);
878static struct text_pos string_pos (EMACS_INT, Lisp_Object); 905static struct text_pos string_pos (EMACS_INT, Lisp_Object);
@@ -927,7 +954,7 @@ static int coords_in_mouse_face_p (struct window *, int, int);
927 954
928 This is the height of W minus the height of a mode line, if any. */ 955 This is the height of W minus the height of a mode line, if any. */
929 956
930inline int 957int
931window_text_bottom_y (struct window *w) 958window_text_bottom_y (struct window *w)
932{ 959{
933 int height = WINDOW_TOTAL_HEIGHT (w); 960 int height = WINDOW_TOTAL_HEIGHT (w);
@@ -941,7 +968,7 @@ window_text_bottom_y (struct window *w)
941 means return the total width of W, not including fringes to 968 means return the total width of W, not including fringes to
942 the left and right of the window. */ 969 the left and right of the window. */
943 970
944inline int 971int
945window_box_width (struct window *w, int area) 972window_box_width (struct window *w, int area)
946{ 973{
947 int cols = XFASTINT (w->total_cols); 974 int cols = XFASTINT (w->total_cols);
@@ -980,7 +1007,7 @@ window_box_width (struct window *w, int area)
980/* Return the pixel height of the display area of window W, not 1007/* Return the pixel height of the display area of window W, not
981 including mode lines of W, if any. */ 1008 including mode lines of W, if any. */
982 1009
983inline int 1010int
984window_box_height (struct window *w) 1011window_box_height (struct window *w)
985{ 1012{
986 struct frame *f = XFRAME (w->frame); 1013 struct frame *f = XFRAME (w->frame);
@@ -1027,7 +1054,7 @@ window_box_height (struct window *w)
1027 area AREA of window W. AREA < 0 means return the left edge of the 1054 area AREA of window W. AREA < 0 means return the left edge of the
1028 whole window, to the right of the left fringe of W. */ 1055 whole window, to the right of the left fringe of W. */
1029 1056
1030inline int 1057int
1031window_box_left_offset (struct window *w, int area) 1058window_box_left_offset (struct window *w, int area)
1032{ 1059{
1033 int x; 1060 int x;
@@ -1059,7 +1086,7 @@ window_box_left_offset (struct window *w, int area)
1059 area AREA of window W. AREA < 0 means return the right edge of the 1086 area AREA of window W. AREA < 0 means return the right edge of the
1060 whole window, to the left of the right fringe of W. */ 1087 whole window, to the left of the right fringe of W. */
1061 1088
1062inline int 1089int
1063window_box_right_offset (struct window *w, int area) 1090window_box_right_offset (struct window *w, int area)
1064{ 1091{
1065 return window_box_left_offset (w, area) + window_box_width (w, area); 1092 return window_box_left_offset (w, area) + window_box_width (w, area);
@@ -1069,7 +1096,7 @@ window_box_right_offset (struct window *w, int area)
1069 area AREA of window W. AREA < 0 means return the left edge of the 1096 area AREA of window W. AREA < 0 means return the left edge of the
1070 whole window, to the right of the left fringe of W. */ 1097 whole window, to the right of the left fringe of W. */
1071 1098
1072inline int 1099int
1073window_box_left (struct window *w, int area) 1100window_box_left (struct window *w, int area)
1074{ 1101{
1075 struct frame *f = XFRAME (w->frame); 1102 struct frame *f = XFRAME (w->frame);
@@ -1089,7 +1116,7 @@ window_box_left (struct window *w, int area)
1089 area AREA of window W. AREA < 0 means return the right edge of the 1116 area AREA of window W. AREA < 0 means return the right edge of the
1090 whole window, to the left of the right fringe of W. */ 1117 whole window, to the left of the right fringe of W. */
1091 1118
1092inline int 1119int
1093window_box_right (struct window *w, int area) 1120window_box_right (struct window *w, int area)
1094{ 1121{
1095 return window_box_left (w, area) + window_box_width (w, area); 1122 return window_box_left (w, area) + window_box_width (w, area);
@@ -1102,7 +1129,7 @@ window_box_right (struct window *w, int area)
1102 coordinates of the upper-left corner of the box. Return in 1129 coordinates of the upper-left corner of the box. Return in
1103 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */ 1130 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1104 1131
1105inline void 1132void
1106window_box (struct window *w, int area, int *box_x, int *box_y, 1133window_box (struct window *w, int area, int *box_x, int *box_y,
1107 int *box_width, int *box_height) 1134 int *box_width, int *box_height)
1108{ 1135{
@@ -1183,6 +1210,34 @@ line_bottom_y (struct it *it)
1183 return line_top_y + line_height; 1210 return line_top_y + line_height;
1184} 1211}
1185 1212
1213/* Subroutine of pos_visible_p below. Extracts a display string, if
1214 any, from the display spec given as its argument. */
1215static Lisp_Object
1216string_from_display_spec (Lisp_Object spec)
1217{
1218 if (CONSP (spec))
1219 {
1220 while (CONSP (spec))
1221 {
1222 if (STRINGP (XCAR (spec)))
1223 return XCAR (spec);
1224 spec = XCDR (spec);
1225 }
1226 }
1227 else if (VECTORP (spec))
1228 {
1229 ptrdiff_t i;
1230
1231 for (i = 0; i < ASIZE (spec); i++)
1232 {
1233 if (STRINGP (AREF (spec, i)))
1234 return AREF (spec, i);
1235 }
1236 return Qnil;
1237 }
1238
1239 return spec;
1240}
1186 1241
1187/* Return 1 if position CHARPOS is visible in window W. 1242/* Return 1 if position CHARPOS is visible in window W.
1188 CHARPOS < 0 means return info about WINDOW_END position. 1243 CHARPOS < 0 means return info about WINDOW_END position.
@@ -1195,6 +1250,7 @@ pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1195 int *rtop, int *rbot, int *rowh, int *vpos) 1250 int *rtop, int *rbot, int *rowh, int *vpos)
1196{ 1251{
1197 struct it it; 1252 struct it it;
1253 void *itdata = bidi_shelve_cache ();
1198 struct text_pos top; 1254 struct text_pos top;
1199 int visible_p = 0; 1255 int visible_p = 0;
1200 struct buffer *old_buffer = NULL; 1256 struct buffer *old_buffer = NULL;
@@ -1225,13 +1281,21 @@ pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1225 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1, 1281 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1226 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y); 1282 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1227 1283
1228 if (charpos >= 0 && IT_CHARPOS (it) >= charpos) 1284 if (charpos >= 0
1285 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1286 && IT_CHARPOS (it) >= charpos)
1287 /* When scanning backwards under bidi iteration, move_it_to
1288 stops at or _before_ CHARPOS, because it stops at or to
1289 the _right_ of the character at CHARPOS. */
1290 || (it.bidi_p && it.bidi_it.scan_dir == -1
1291 && IT_CHARPOS (it) <= charpos)))
1229 { 1292 {
1230 /* We have reached CHARPOS, or passed it. How the call to 1293 /* We have reached CHARPOS, or passed it. How the call to
1231 move_it_to can overshoot: (i) If CHARPOS is on invisible 1294 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1232 text, move_it_to stops at the end of the invisible text, 1295 or covered by a display property, move_it_to stops at the end
1233 after CHARPOS. (ii) If CHARPOS is in a display vector, 1296 of the invisible text, to the right of CHARPOS. (ii) If
1234 move_it_to stops on its last glyph. */ 1297 CHARPOS is in a display vector, move_it_to stops on its last
1298 glyph. */
1235 int top_x = it.current_x; 1299 int top_x = it.current_x;
1236 int top_y = it.current_y; 1300 int top_y = it.current_y;
1237 enum it_method it_method = it.method; 1301 enum it_method it_method = it.method;
@@ -1268,6 +1332,136 @@ pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1268 } 1332 }
1269 } 1333 }
1270 } 1334 }
1335 else if (IT_CHARPOS (it) != charpos)
1336 {
1337 Lisp_Object cpos = make_number (charpos);
1338 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1339 Lisp_Object string = string_from_display_spec (spec);
1340 int newline_in_string = 0;
1341
1342 if (STRINGP (string))
1343 {
1344 const char *s = SSDATA (string);
1345 const char *e = s + SBYTES (string);
1346 while (s < e)
1347 {
1348 if (*s++ == '\n')
1349 {
1350 newline_in_string = 1;
1351 break;
1352 }
1353 }
1354 }
1355 /* The tricky code below is needed because there's a
1356 discrepancy between move_it_to and how we set cursor
1357 when the display line ends in a newline from a
1358 display string. move_it_to will stop _after_ such
1359 display strings, whereas set_cursor_from_row
1360 conspires with cursor_row_p to place the cursor on
1361 the first glyph produced from the display string. */
1362
1363 /* We have overshoot PT because it is covered by a
1364 display property whose value is a string. If the
1365 string includes embedded newlines, we are also in the
1366 wrong display line. Backtrack to the correct line,
1367 where the display string begins. */
1368 if (newline_in_string)
1369 {
1370 Lisp_Object startpos, endpos;
1371 EMACS_INT start, end;
1372 struct it it3;
1373
1374 /* Find the first and the last buffer positions
1375 covered by the display string. */
1376 endpos =
1377 Fnext_single_char_property_change (cpos, Qdisplay,
1378 Qnil, Qnil);
1379 startpos =
1380 Fprevious_single_char_property_change (endpos, Qdisplay,
1381 Qnil, Qnil);
1382 start = XFASTINT (startpos);
1383 end = XFASTINT (endpos);
1384 /* Move to the last buffer position before the
1385 display property. */
1386 start_display (&it3, w, top);
1387 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1388 /* Move forward one more line if the position before
1389 the display string is a newline or if it is the
1390 rightmost character on a line that is
1391 continued or word-wrapped. */
1392 if (it3.method == GET_FROM_BUFFER
1393 && it3.c == '\n')
1394 move_it_by_lines (&it3, 1);
1395 else if (move_it_in_display_line_to (&it3, -1,
1396 it3.current_x
1397 + it3.pixel_width,
1398 MOVE_TO_X)
1399 == MOVE_LINE_CONTINUED)
1400 {
1401 move_it_by_lines (&it3, 1);
1402 /* When we are under word-wrap, the #$@%!
1403 move_it_by_lines moves 2 lines, so we need to
1404 fix that up. */
1405 if (it3.line_wrap == WORD_WRAP)
1406 move_it_by_lines (&it3, -1);
1407 }
1408
1409 /* Record the vertical coordinate of the display
1410 line where we wound up. */
1411 top_y = it3.current_y;
1412 if (it3.bidi_p)
1413 {
1414 /* When characters are reordered for display,
1415 the character displayed to the left of the
1416 display string could be _after_ the display
1417 property in the logical order. Use the
1418 smallest vertical position of these two. */
1419 start_display (&it3, w, top);
1420 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1421 if (it3.current_y < top_y)
1422 top_y = it3.current_y;
1423 }
1424 /* Move from the top of the window to the beginning
1425 of the display line where the display string
1426 begins. */
1427 start_display (&it3, w, top);
1428 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1429 /* Finally, advance the iterator until we hit the
1430 first display element whose character position is
1431 CHARPOS, or until the first newline from the
1432 display string, which signals the end of the
1433 display line. */
1434 while (get_next_display_element (&it3))
1435 {
1436 PRODUCE_GLYPHS (&it3);
1437 if (IT_CHARPOS (it3) == charpos
1438 || ITERATOR_AT_END_OF_LINE_P (&it3))
1439 break;
1440 set_iterator_to_next (&it3, 0);
1441 }
1442 top_x = it3.current_x - it3.pixel_width;
1443 /* Normally, we would exit the above loop because we
1444 found the display element whose character
1445 position is CHARPOS. For the contingency that we
1446 didn't, and stopped at the first newline from the
1447 display string, move back over the glyphs
1448 produced from the string, until we find the
1449 rightmost glyph not from the string. */
1450 if (IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1451 {
1452 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1453 + it3.glyph_row->used[TEXT_AREA];
1454
1455 while (EQ ((g - 1)->object, string))
1456 {
1457 --g;
1458 top_x -= g->pixel_width;
1459 }
1460 xassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1461 + it3.glyph_row->used[TEXT_AREA]);
1462 }
1463 }
1464 }
1271 1465
1272 *x = top_x; 1466 *x = top_x;
1273 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y); 1467 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
@@ -1280,15 +1474,18 @@ pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1280 } 1474 }
1281 else 1475 else
1282 { 1476 {
1477 /* We were asked to provide info about WINDOW_END. */
1283 struct it it2; 1478 struct it it2;
1479 void *it2data = NULL;
1284 1480
1285 it2 = it; 1481 SAVE_IT (it2, it, it2data);
1286 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n') 1482 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1287 move_it_by_lines (&it, 1); 1483 move_it_by_lines (&it, 1);
1288 if (charpos < IT_CHARPOS (it) 1484 if (charpos < IT_CHARPOS (it)
1289 || (it.what == IT_EOB && charpos == IT_CHARPOS (it))) 1485 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1290 { 1486 {
1291 visible_p = 1; 1487 visible_p = 1;
1488 RESTORE_IT (&it2, &it2, it2data);
1292 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS); 1489 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1293 *x = it2.current_x; 1490 *x = it2.current_x;
1294 *y = it2.current_y + it2.max_ascent - it2.ascent; 1491 *y = it2.current_y + it2.max_ascent - it2.ascent;
@@ -1301,7 +1498,10 @@ pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1301 WINDOW_HEADER_LINE_HEIGHT (w)))); 1498 WINDOW_HEADER_LINE_HEIGHT (w))));
1302 *vpos = it2.vpos; 1499 *vpos = it2.vpos;
1303 } 1500 }
1501 else
1502 bidi_unshelve_cache (it2data, 1);
1304 } 1503 }
1504 bidi_unshelve_cache (itdata, 0);
1305 1505
1306 if (old_buffer) 1506 if (old_buffer)
1307 set_buffer_internal_1 (old_buffer); 1507 set_buffer_internal_1 (old_buffer);
@@ -1715,7 +1915,7 @@ get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int
1715 environments with anti-aliased text: if the same text is 1915 environments with anti-aliased text: if the same text is
1716 drawn onto the same place multiple times, it gets thicker. 1916 drawn onto the same place multiple times, it gets thicker.
1717 If the overlap we are processing is for the erased cursor, we 1917 If the overlap we are processing is for the erased cursor, we
1718 take the intersection with the rectagle of the cursor. */ 1918 take the intersection with the rectangle of the cursor. */
1719 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR) 1919 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1720 { 1920 {
1721 XRectangle rc, r_save = r; 1921 XRectangle rc, r_save = r;
@@ -1874,7 +2074,7 @@ get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
1874 rectangle as wide as the glyph, but use a canonical character 2074 rectangle as wide as the glyph, but use a canonical character
1875 width instead. */ 2075 width instead. */
1876 wd = glyph->pixel_width - 1; 2076 wd = glyph->pixel_width - 1;
1877#if defined(HAVE_NTGUI) || defined(HAVE_NS) 2077#if defined (HAVE_NTGUI) || defined (HAVE_NS)
1878 wd++; /* Why? */ 2078 wd++; /* Why? */
1879#endif 2079#endif
1880 2080
@@ -2209,8 +2409,7 @@ safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2209 This is for debugging. It is too slow to do unconditionally. */ 2409 This is for debugging. It is too slow to do unconditionally. */
2210 2410
2211static void 2411static void
2212check_it (it) 2412check_it (struct it *it)
2213 struct it *it;
2214{ 2413{
2215 if (it->method == GET_FROM_STRING) 2414 if (it->method == GET_FROM_STRING)
2216 { 2415 {
@@ -2340,6 +2539,10 @@ init_iterator (struct it *it, struct window *w,
2340 it->base_face_id = remapped_base_face_id; 2539 it->base_face_id = remapped_base_face_id;
2341 it->string = Qnil; 2540 it->string = Qnil;
2342 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1; 2541 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2542 it->paragraph_embedding = L2R;
2543 it->bidi_it.string.lstring = Qnil;
2544 it->bidi_it.string.s = NULL;
2545 it->bidi_it.string.bufpos = 0;
2343 2546
2344 /* The window in which we iterate over current_buffer: */ 2547 /* The window in which we iterate over current_buffer: */
2345 XSETWINDOW (it->window, w); 2548 XSETWINDOW (it->window, w);
@@ -2396,13 +2599,6 @@ init_iterator (struct it *it, struct window *w,
2396 /* Are multibyte characters enabled in current_buffer? */ 2599 /* Are multibyte characters enabled in current_buffer? */
2397 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters)); 2600 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2398 2601
2399 /* Do we need to reorder bidirectional text? Not if this is a
2400 unibyte buffer: by definition, none of the single-byte characters
2401 are strong R2L, so no reordering is needed. And bidi.c doesn't
2402 support unibyte buffers anyway. */
2403 it->bidi_p
2404 = !NILP (BVAR (current_buffer, bidi_display_reordering)) && it->multibyte_p;
2405
2406 /* Non-zero if we should highlight the region. */ 2602 /* Non-zero if we should highlight the region. */
2407 highlight_region_p 2603 highlight_region_p
2408 = (!NILP (Vtransient_mark_mode) 2604 = (!NILP (Vtransient_mark_mode)
@@ -2440,10 +2636,7 @@ init_iterator (struct it *it, struct window *w,
2440 else if (INTEGERP (w->redisplay_end_trigger)) 2636 else if (INTEGERP (w->redisplay_end_trigger))
2441 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger); 2637 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2442 2638
2443 /* Correct bogus values of tab_width. */ 2639 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2444 it->tab_width = XINT (BVAR (current_buffer, tab_width));
2445 if (it->tab_width <= 0 || it->tab_width > 1000)
2446 it->tab_width = 8;
2447 2640
2448 /* Are lines in the display truncated? */ 2641 /* Are lines in the display truncated? */
2449 if (base_face_id != DEFAULT_FACE_ID 2642 if (base_face_id != DEFAULT_FACE_ID
@@ -2552,29 +2745,17 @@ init_iterator (struct it *it, struct window *w,
2552 it->start_of_box_run_p = 1; 2745 it->start_of_box_run_p = 1;
2553 } 2746 }
2554 2747
2555 /* If we are to reorder bidirectional text, init the bidi
2556 iterator. */
2557 if (it->bidi_p)
2558 {
2559 /* Note the paragraph direction that this buffer wants to
2560 use. */
2561 if (EQ (BVAR (current_buffer, bidi_paragraph_direction), Qleft_to_right))
2562 it->paragraph_embedding = L2R;
2563 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction), Qright_to_left))
2564 it->paragraph_embedding = R2L;
2565 else
2566 it->paragraph_embedding = NEUTRAL_DIR;
2567 bidi_init_it (charpos, bytepos, FRAME_WINDOW_P (it->f), &it->bidi_it);
2568 }
2569
2570 /* If a buffer position was specified, set the iterator there, 2748 /* If a buffer position was specified, set the iterator there,
2571 getting overlays and face properties from that position. */ 2749 getting overlays and face properties from that position. */
2572 if (charpos >= BUF_BEG (current_buffer)) 2750 if (charpos >= BUF_BEG (current_buffer))
2573 { 2751 {
2574 it->end_charpos = ZV; 2752 it->end_charpos = ZV;
2575 it->face_id = -1;
2576 IT_CHARPOS (*it) = charpos; 2753 IT_CHARPOS (*it) = charpos;
2577 2754
2755 /* We will rely on `reseat' to set this up properly, via
2756 handle_face_prop. */
2757 it->face_id = it->base_face_id;
2758
2578 /* Compute byte position if not specified. */ 2759 /* Compute byte position if not specified. */
2579 if (bytepos < charpos) 2760 if (bytepos < charpos)
2580 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos); 2761 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
@@ -2582,6 +2763,36 @@ init_iterator (struct it *it, struct window *w,
2582 IT_BYTEPOS (*it) = bytepos; 2763 IT_BYTEPOS (*it) = bytepos;
2583 2764
2584 it->start = it->current; 2765 it->start = it->current;
2766 /* Do we need to reorder bidirectional text? Not if this is a
2767 unibyte buffer: by definition, none of the single-byte
2768 characters are strong R2L, so no reordering is needed. And
2769 bidi.c doesn't support unibyte buffers anyway. Also, don't
2770 reorder while we are loading loadup.el, since the tables of
2771 character properties needed for reordering are not yet
2772 available. */
2773 it->bidi_p =
2774 NILP (Vpurify_flag)
2775 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2776 && it->multibyte_p;
2777
2778 /* If we are to reorder bidirectional text, init the bidi
2779 iterator. */
2780 if (it->bidi_p)
2781 {
2782 /* Note the paragraph direction that this buffer wants to
2783 use. */
2784 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2785 Qleft_to_right))
2786 it->paragraph_embedding = L2R;
2787 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2788 Qright_to_left))
2789 it->paragraph_embedding = R2L;
2790 else
2791 it->paragraph_embedding = NEUTRAL_DIR;
2792 bidi_unshelve_cache (NULL, 0);
2793 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2794 &it->bidi_it);
2795 }
2585 2796
2586 /* Compute faces etc. */ 2797 /* Compute faces etc. */
2587 reseat (it, it->current.pos, 1); 2798 reseat (it, it->current.pos, 1);
@@ -2638,8 +2849,14 @@ start_display (struct it *it, struct window *w, struct text_pos pos)
2638 || (new_x == it->last_visible_x 2849 || (new_x == it->last_visible_x
2639 && FRAME_WINDOW_P (it->f)))) 2850 && FRAME_WINDOW_P (it->f))))
2640 { 2851 {
2641 if (it->current.dpvec_index >= 0 2852 if ((it->current.dpvec_index >= 0
2642 || it->current.overlay_string_index >= 0) 2853 || it->current.overlay_string_index >= 0)
2854 /* If we are on a newline from a display vector or
2855 overlay string, then we are already at the end of
2856 a screen line; no need to go to the next line in
2857 that case, as this line is not really continued.
2858 (If we do go to the next line, C-e will not DTRT.) */
2859 && it->c != '\n')
2643 { 2860 {
2644 set_iterator_to_next (it, 1); 2861 set_iterator_to_next (it, 1);
2645 move_it_in_display_line_to (it, -1, -1, 0); 2862 move_it_in_display_line_to (it, -1, -1, 0);
@@ -2647,6 +2864,13 @@ start_display (struct it *it, struct window *w, struct text_pos pos)
2647 2864
2648 it->continuation_lines_width += it->current_x; 2865 it->continuation_lines_width += it->current_x;
2649 } 2866 }
2867 /* If the character at POS is displayed via a display
2868 vector, move_it_to above stops at the final glyph of
2869 IT->dpvec. To make the caller redisplay that character
2870 again (a.k.a. start at POS), we need to reset the
2871 dpvec_index to the beginning of IT->dpvec. */
2872 else if (it->current.dpvec_index >= 0)
2873 it->current.dpvec_index = 0;
2650 2874
2651 /* We're starting a new display line, not affected by the 2875 /* We're starting a new display line, not affected by the
2652 height of the continued line, so clear the appropriate 2876 height of the continued line, so clear the appropriate
@@ -2903,6 +3127,7 @@ handle_stop (struct it *it)
2903 { 3127 {
2904 it->ignore_overlay_strings_at_pos_p = 1; 3128 it->ignore_overlay_strings_at_pos_p = 1;
2905 it->string_from_display_prop_p = 0; 3129 it->string_from_display_prop_p = 0;
3130 it->from_disp_prop_p = 0;
2906 handle_overlay_change_p = 0; 3131 handle_overlay_change_p = 0;
2907 } 3132 }
2908 handled = HANDLED_RECOMPUTE_PROPS; 3133 handled = HANDLED_RECOMPUTE_PROPS;
@@ -2950,13 +3175,11 @@ compute_stop_pos (struct it *it)
2950 Lisp_Object object, limit, position; 3175 Lisp_Object object, limit, position;
2951 EMACS_INT charpos, bytepos; 3176 EMACS_INT charpos, bytepos;
2952 3177
2953 /* If nowhere else, stop at the end. */
2954 it->stop_charpos = it->end_charpos;
2955
2956 if (STRINGP (it->string)) 3178 if (STRINGP (it->string))
2957 { 3179 {
2958 /* Strings are usually short, so don't limit the search for 3180 /* Strings are usually short, so don't limit the search for
2959 properties. */ 3181 properties. */
3182 it->stop_charpos = it->end_charpos;
2960 object = it->string; 3183 object = it->string;
2961 limit = Qnil; 3184 limit = Qnil;
2962 charpos = IT_STRING_CHARPOS (*it); 3185 charpos = IT_STRING_CHARPOS (*it);
@@ -2966,6 +3189,12 @@ compute_stop_pos (struct it *it)
2966 { 3189 {
2967 EMACS_INT pos; 3190 EMACS_INT pos;
2968 3191
3192 /* If end_charpos is out of range for some reason, such as a
3193 misbehaving display function, rationalize it (Bug#5984). */
3194 if (it->end_charpos > ZV)
3195 it->end_charpos = ZV;
3196 it->stop_charpos = it->end_charpos;
3197
2969 /* If next overlay change is in front of the current stop pos 3198 /* If next overlay change is in front of the current stop pos
2970 (which is IT->end_charpos), stop there. Note: value of 3199 (which is IT->end_charpos), stop there. Note: value of
2971 next_overlay_change is point-max if no overlay change 3200 next_overlay_change is point-max if no overlay change
@@ -3084,74 +3313,136 @@ next_overlay_change (EMACS_INT pos)
3084 return endpos; 3313 return endpos;
3085} 3314}
3086 3315
3087/* Return the character position of a display string at or after CHARPOS. 3316/* How many characters forward to search for a display property or
3088 If no display string exists at or after CHARPOS, return ZV. A 3317 display string. Searching too far forward makes the bidi display
3089 display string is either an overlay with `display' property whose 3318 sluggish, especially in small windows. */
3090 value is a string, or a `display' text property whose value is a 3319#define MAX_DISP_SCAN 250
3091 string. FRAME_WINDOW_P is non-zero when we are displaying a window 3320
3092 on a GUI frame. */ 3321/* Return the character position of a display string at or after
3322 position specified by POSITION. If no display string exists at or
3323 after POSITION, return ZV. A display string is either an overlay
3324 with `display' property whose value is a string, or a `display'
3325 text property whose value is a string. STRING is data about the
3326 string to iterate; if STRING->lstring is nil, we are iterating a
3327 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3328 on a GUI frame. DISP_PROP is set to zero if we searched
3329 MAX_DISP_SCAN characters forward without finding any display
3330 strings, non-zero otherwise. It is set to 2 if the display string
3331 uses any kind of `(space ...)' spec that will produce a stretch of
3332 white space in the text area. */
3093EMACS_INT 3333EMACS_INT
3094compute_display_string_pos (EMACS_INT charpos, int frame_window_p) 3334compute_display_string_pos (struct text_pos *position,
3095{ 3335 struct bidi_string_data *string,
3096 /* FIXME: Support display properties on strings (object = Qnil means 3336 int frame_window_p, int *disp_prop)
3097 current buffer). */ 3337{
3098 Lisp_Object object = Qnil; 3338 /* OBJECT = nil means current buffer. */
3099 Lisp_Object pos, spec; 3339 Lisp_Object object =
3100 struct text_pos position; 3340 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3101 EMACS_INT bufpos; 3341 Lisp_Object pos, spec, limpos;
3102 3342 int string_p = (string && (STRINGP (string->lstring) || string->s));
3103 if (charpos >= ZV) 3343 EMACS_INT eob = string_p ? string->schars : ZV;
3104 return ZV; 3344 EMACS_INT begb = string_p ? 0 : BEGV;
3345 EMACS_INT bufpos, charpos = CHARPOS (*position);
3346 EMACS_INT lim =
3347 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3348 struct text_pos tpos;
3349 int rv = 0;
3350
3351 *disp_prop = 1;
3352
3353 if (charpos >= eob
3354 /* We don't support display properties whose values are strings
3355 that have display string properties. */
3356 || string->from_disp_str
3357 /* C strings cannot have display properties. */
3358 || (string->s && !STRINGP (object)))
3359 {
3360 *disp_prop = 0;
3361 return eob;
3362 }
3105 3363
3106 /* If the character at CHARPOS is where the display string begins, 3364 /* If the character at CHARPOS is where the display string begins,
3107 return CHARPOS. */ 3365 return CHARPOS. */
3108 pos = make_number (charpos); 3366 pos = make_number (charpos);
3109 CHARPOS (position) = charpos; 3367 if (STRINGP (object))
3110 BYTEPOS (position) = CHAR_TO_BYTE (charpos); 3368 bufpos = string->bufpos;
3111 bufpos = charpos; /* FIXME! support strings as well */ 3369 else
3370 bufpos = charpos;
3371 tpos = *position;
3112 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object)) 3372 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3113 && (charpos <= BEGV 3373 && (charpos <= begb
3114 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay, 3374 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3115 object), 3375 object),
3116 spec)) 3376 spec))
3117 && handle_display_spec (NULL, spec, object, Qnil, &position, bufpos, 3377 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3118 frame_window_p)) 3378 frame_window_p)))
3119 return charpos; 3379 {
3380 if (rv == 2)
3381 *disp_prop = 2;
3382 return charpos;
3383 }
3120 3384
3121 /* Look forward for the first character with a `display' property 3385 /* Look forward for the first character with a `display' property
3122 that will replace the underlying text when displayed. */ 3386 that will replace the underlying text when displayed. */
3387 limpos = make_number (lim);
3123 do { 3388 do {
3124 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil); 3389 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3125 CHARPOS (position) = XFASTINT (pos); 3390 CHARPOS (tpos) = XFASTINT (pos);
3126 BYTEPOS (position) = CHAR_TO_BYTE (CHARPOS (position)); 3391 if (CHARPOS (tpos) >= lim)
3127 if (CHARPOS (position) >= ZV) 3392 {
3128 break; 3393 *disp_prop = 0;
3394 break;
3395 }
3396 if (STRINGP (object))
3397 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3398 else
3399 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3129 spec = Fget_char_property (pos, Qdisplay, object); 3400 spec = Fget_char_property (pos, Qdisplay, object);
3130 bufpos = CHARPOS (position); /* FIXME! support strings as well */ 3401 if (!STRINGP (object))
3402 bufpos = CHARPOS (tpos);
3131 } while (NILP (spec) 3403 } while (NILP (spec)
3132 || !handle_display_spec (NULL, spec, object, Qnil, &position, bufpos, 3404 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3133 frame_window_p)); 3405 bufpos, frame_window_p)));
3406 if (rv == 2)
3407 *disp_prop = 2;
3134 3408
3135 return CHARPOS (position); 3409 return CHARPOS (tpos);
3136} 3410}
3137 3411
3138/* Return the character position of the end of the display string that 3412/* Return the character position of the end of the display string that
3139 started at CHARPOS. A display string is either an overlay with 3413 started at CHARPOS. If there's no display string at CHARPOS,
3140 `display' property whose value is a string or a `display' text 3414 return -1. A display string is either an overlay with `display'
3141 property whose value is a string. */ 3415 property whose value is a string or a `display' text property whose
3416 value is a string. */
3142EMACS_INT 3417EMACS_INT
3143compute_display_string_end (EMACS_INT charpos) 3418compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3144{ 3419{
3145 /* FIXME: Support display properties on strings (object = Qnil means 3420 /* OBJECT = nil means current buffer. */
3146 current buffer). */ 3421 Lisp_Object object =
3147 Lisp_Object object = Qnil; 3422 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3148 Lisp_Object pos = make_number (charpos); 3423 Lisp_Object pos = make_number (charpos);
3149 3424 EMACS_INT eob =
3150 if (charpos >= ZV) 3425 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3151 return ZV; 3426
3152 3427 if (charpos >= eob || (string->s && !STRINGP (object)))
3428 return eob;
3429
3430 /* It could happen that the display property or overlay was removed
3431 since we found it in compute_display_string_pos above. One way
3432 this can happen is if JIT font-lock was called (through
3433 handle_fontified_prop), and jit-lock-functions remove text
3434 properties or overlays from the portion of buffer that includes
3435 CHARPOS. Muse mode is known to do that, for example. In this
3436 case, we return -1 to the caller, to signal that no display
3437 string is actually present at CHARPOS. See bidi_fetch_char for
3438 how this is handled.
3439
3440 An alternative would be to never look for display properties past
3441 it->stop_charpos. But neither compute_display_string_pos nor
3442 bidi_fetch_char that calls it know or care where the next
3443 stop_charpos is. */
3153 if (NILP (Fget_char_property (pos, Qdisplay, object))) 3444 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3154 abort (); 3445 return -1;
3155 3446
3156 /* Look forward for the first character where the `display' property 3447 /* Look forward for the first character where the `display' property
3157 changes. */ 3448 changes. */
@@ -3382,7 +3673,9 @@ handle_face_prop (struct it *it)
3382 with, so that overlay strings appear in the same face as 3673 with, so that overlay strings appear in the same face as
3383 surrounding text, unless they specify their own 3674 surrounding text, unless they specify their own
3384 faces. */ 3675 faces. */
3385 base_face_id = underlying_face_id (it); 3676 base_face_id = it->string_from_prefix_prop_p
3677 ? DEFAULT_FACE_ID
3678 : underlying_face_id (it);
3386 } 3679 }
3387 3680
3388 new_face_id = face_at_string_position (it->w, 3681 new_face_id = face_at_string_position (it->w,
@@ -3441,21 +3734,23 @@ underlying_face_id (struct it *it)
3441 3734
3442 3735
3443/* Compute the face one character before or after the current position 3736/* Compute the face one character before or after the current position
3444 of IT. BEFORE_P non-zero means get the face in front of IT's 3737 of IT, in the visual order. BEFORE_P non-zero means get the face
3445 position. Value is the id of the face. */ 3738 in front (to the left in L2R paragraphs, to the right in R2L
3739 paragraphs) of IT's screen position. Value is the ID of the face. */
3446 3740
3447static int 3741static int
3448face_before_or_after_it_pos (struct it *it, int before_p) 3742face_before_or_after_it_pos (struct it *it, int before_p)
3449{ 3743{
3450 int face_id, limit; 3744 int face_id, limit;
3451 EMACS_INT next_check_charpos; 3745 EMACS_INT next_check_charpos;
3452 struct text_pos pos; 3746 struct it it_copy;
3747 void *it_copy_data = NULL;
3453 3748
3454 xassert (it->s == NULL); 3749 xassert (it->s == NULL);
3455 3750
3456 if (STRINGP (it->string)) 3751 if (STRINGP (it->string))
3457 { 3752 {
3458 EMACS_INT bufpos; 3753 EMACS_INT bufpos, charpos;
3459 int base_face_id; 3754 int base_face_id;
3460 3755
3461 /* No face change past the end of the string (for the case 3756 /* No face change past the end of the string (for the case
@@ -3465,16 +3760,59 @@ face_before_or_after_it_pos (struct it *it, int before_p)
3465 || (IT_STRING_CHARPOS (*it) == 0 && before_p)) 3760 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3466 return it->face_id; 3761 return it->face_id;
3467 3762
3468 /* Set pos to the position before or after IT's current position. */ 3763 if (!it->bidi_p)
3469 if (before_p) 3764 {
3470 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string); 3765 /* Set charpos to the position before or after IT's current
3766 position, in the logical order, which in the non-bidi
3767 case is the same as the visual order. */
3768 if (before_p)
3769 charpos = IT_STRING_CHARPOS (*it) - 1;
3770 else if (it->what == IT_COMPOSITION)
3771 /* For composition, we must check the character after the
3772 composition. */
3773 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3774 else
3775 charpos = IT_STRING_CHARPOS (*it) + 1;
3776 }
3471 else 3777 else
3472 /* For composition, we must check the character after the 3778 {
3473 composition. */ 3779 if (before_p)
3474 pos = (it->what == IT_COMPOSITION 3780 {
3475 ? string_pos (IT_STRING_CHARPOS (*it) 3781 /* With bidi iteration, the character before the current
3476 + it->cmp_it.nchars, it->string) 3782 in the visual order cannot be found by simple
3477 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string)); 3783 iteration, because "reverse" reordering is not
3784 supported. Instead, we need to use the move_it_*
3785 family of functions. */
3786 /* Ignore face changes before the first visible
3787 character on this display line. */
3788 if (it->current_x <= it->first_visible_x)
3789 return it->face_id;
3790 SAVE_IT (it_copy, *it, it_copy_data);
3791 /* Implementation note: Since move_it_in_display_line
3792 works in the iterator geometry, and thinks the first
3793 character is always the leftmost, even in R2L lines,
3794 we don't need to distinguish between the R2L and L2R
3795 cases here. */
3796 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3797 it_copy.current_x - 1, MOVE_TO_X);
3798 charpos = IT_STRING_CHARPOS (it_copy);
3799 RESTORE_IT (it, it, it_copy_data);
3800 }
3801 else
3802 {
3803 /* Set charpos to the string position of the character
3804 that comes after IT's current position in the visual
3805 order. */
3806 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3807
3808 it_copy = *it;
3809 while (n--)
3810 bidi_move_to_visually_next (&it_copy.bidi_it);
3811
3812 charpos = it_copy.bidi_it.charpos;
3813 }
3814 }
3815 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3478 3816
3479 if (it->current.overlay_string_index >= 0) 3817 if (it->current.overlay_string_index >= 0)
3480 bufpos = IT_CHARPOS (*it); 3818 bufpos = IT_CHARPOS (*it);
@@ -3486,7 +3824,7 @@ face_before_or_after_it_pos (struct it *it, int before_p)
3486 /* Get the face for ASCII, or unibyte. */ 3824 /* Get the face for ASCII, or unibyte. */
3487 face_id = face_at_string_position (it->w, 3825 face_id = face_at_string_position (it->w,
3488 it->string, 3826 it->string,
3489 CHARPOS (pos), 3827 charpos,
3490 bufpos, 3828 bufpos,
3491 it->region_beg_charpos, 3829 it->region_beg_charpos,
3492 it->region_end_charpos, 3830 it->region_end_charpos,
@@ -3498,16 +3836,19 @@ face_before_or_after_it_pos (struct it *it, int before_p)
3498 suitable for unibyte text if IT->string is unibyte. */ 3836 suitable for unibyte text if IT->string is unibyte. */
3499 if (STRING_MULTIBYTE (it->string)) 3837 if (STRING_MULTIBYTE (it->string))
3500 { 3838 {
3501 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos); 3839 struct text_pos pos1 = string_pos (charpos, it->string);
3840 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3502 int c, len; 3841 int c, len;
3503 struct face *face = FACE_FROM_ID (it->f, face_id); 3842 struct face *face = FACE_FROM_ID (it->f, face_id);
3504 3843
3505 c = string_char_and_length (p, &len); 3844 c = string_char_and_length (p, &len);
3506 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string); 3845 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3507 } 3846 }
3508 } 3847 }
3509 else 3848 else
3510 { 3849 {
3850 struct text_pos pos;
3851
3511 if ((IT_CHARPOS (*it) >= ZV && !before_p) 3852 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3512 || (IT_CHARPOS (*it) <= BEGV && before_p)) 3853 || (IT_CHARPOS (*it) <= BEGV && before_p))
3513 return it->face_id; 3854 return it->face_id;
@@ -3515,17 +3856,63 @@ face_before_or_after_it_pos (struct it *it, int before_p)
3515 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT; 3856 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3516 pos = it->current.pos; 3857 pos = it->current.pos;
3517 3858
3518 if (before_p) 3859 if (!it->bidi_p)
3519 DEC_TEXT_POS (pos, it->multibyte_p); 3860 {
3861 if (before_p)
3862 DEC_TEXT_POS (pos, it->multibyte_p);
3863 else
3864 {
3865 if (it->what == IT_COMPOSITION)
3866 {
3867 /* For composition, we must check the position after
3868 the composition. */
3869 pos.charpos += it->cmp_it.nchars;
3870 pos.bytepos += it->len;
3871 }
3872 else
3873 INC_TEXT_POS (pos, it->multibyte_p);
3874 }
3875 }
3520 else 3876 else
3521 { 3877 {
3522 if (it->what == IT_COMPOSITION) 3878 if (before_p)
3523 /* For composition, we must check the position after the 3879 {
3524 composition. */ 3880 /* With bidi iteration, the character before the current
3525 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len; 3881 in the visual order cannot be found by simple
3882 iteration, because "reverse" reordering is not
3883 supported. Instead, we need to use the move_it_*
3884 family of functions. */
3885 /* Ignore face changes before the first visible
3886 character on this display line. */
3887 if (it->current_x <= it->first_visible_x)
3888 return it->face_id;
3889 SAVE_IT (it_copy, *it, it_copy_data);
3890 /* Implementation note: Since move_it_in_display_line
3891 works in the iterator geometry, and thinks the first
3892 character is always the leftmost, even in R2L lines,
3893 we don't need to distinguish between the R2L and L2R
3894 cases here. */
3895 move_it_in_display_line (&it_copy, ZV,
3896 it_copy.current_x - 1, MOVE_TO_X);
3897 pos = it_copy.current.pos;
3898 RESTORE_IT (it, it, it_copy_data);
3899 }
3526 else 3900 else
3527 INC_TEXT_POS (pos, it->multibyte_p); 3901 {
3902 /* Set charpos to the buffer position of the character
3903 that comes after IT's current position in the visual
3904 order. */
3905 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3906
3907 it_copy = *it;
3908 while (n--)
3909 bidi_move_to_visually_next (&it_copy.bidi_it);
3910
3911 SET_TEXT_POS (pos,
3912 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3913 }
3528 } 3914 }
3915 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3529 3916
3530 /* Determine face for CHARSET_ASCII, or unibyte. */ 3917 /* Determine face for CHARSET_ASCII, or unibyte. */
3531 face_id = face_at_buffer_position (it->w, 3918 face_id = face_at_buffer_position (it->w,
@@ -3576,6 +3963,8 @@ handle_invisible_prop (struct it *it)
3576 if (!NILP (prop) 3963 if (!NILP (prop)
3577 && IT_STRING_CHARPOS (*it) < it->end_charpos) 3964 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3578 { 3965 {
3966 EMACS_INT endpos;
3967
3579 handled = HANDLED_RECOMPUTE_PROPS; 3968 handled = HANDLED_RECOMPUTE_PROPS;
3580 3969
3581 /* Get the position at which the next change of the 3970 /* Get the position at which the next change of the
@@ -3590,12 +3979,37 @@ handle_invisible_prop (struct it *it)
3590 change in the property is at position end_charpos. 3979 change in the property is at position end_charpos.
3591 Move IT's current position to that position. */ 3980 Move IT's current position to that position. */
3592 if (INTEGERP (end_charpos) 3981 if (INTEGERP (end_charpos)
3593 && XFASTINT (end_charpos) < XFASTINT (limit)) 3982 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3594 { 3983 {
3595 struct text_pos old; 3984 struct text_pos old;
3985 EMACS_INT oldpos;
3986
3596 old = it->current.string_pos; 3987 old = it->current.string_pos;
3597 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos); 3988 oldpos = CHARPOS (old);
3598 compute_string_pos (&it->current.string_pos, old, it->string); 3989 if (it->bidi_p)
3990 {
3991 if (it->bidi_it.first_elt
3992 && it->bidi_it.charpos < SCHARS (it->string))
3993 bidi_paragraph_init (it->paragraph_embedding,
3994 &it->bidi_it, 1);
3995 /* Bidi-iterate out of the invisible text. */
3996 do
3997 {
3998 bidi_move_to_visually_next (&it->bidi_it);
3999 }
4000 while (oldpos <= it->bidi_it.charpos
4001 && it->bidi_it.charpos < endpos);
4002
4003 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4004 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4005 if (IT_CHARPOS (*it) >= endpos)
4006 it->prev_stop = endpos;
4007 }
4008 else
4009 {
4010 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4011 compute_string_pos (&it->current.string_pos, old, it->string);
4012 }
3599 } 4013 }
3600 else 4014 else
3601 { 4015 {
@@ -3681,42 +4095,81 @@ handle_invisible_prop (struct it *it)
3681 while (invis_p); 4095 while (invis_p);
3682 4096
3683 /* The position newpos is now either ZV or on visible text. */ 4097 /* The position newpos is now either ZV or on visible text. */
3684 if (it->bidi_p && newpos < ZV) 4098 if (it->bidi_p)
3685 { 4099 {
3686 /* With bidi iteration, the region of invisible text 4100 EMACS_INT bpos = CHAR_TO_BYTE (newpos);
3687 could start and/or end in the middle of a non-base 4101 int on_newline =
3688 embedding level. Therefore, we need to skip 4102 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
3689 invisible text using the bidi iterator, starting at 4103 int after_newline =
3690 IT's current position, until we find ourselves 4104 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
3691 outside the invisible text. Skipping invisible text 4105
3692 _after_ bidi iteration avoids affecting the visual 4106 /* If the invisible text ends on a newline or on a
3693 order of the displayed text when invisible properties 4107 character after a newline, we can avoid the costly,
3694 are added or removed. */ 4108 character by character, bidi iteration to NEWPOS, and
3695 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV) 4109 instead simply reseat the iterator there. That's
4110 because all bidi reordering information is tossed at
4111 the newline. This is a big win for modes that hide
4112 complete lines, like Outline, Org, etc. */
4113 if (on_newline || after_newline)
3696 { 4114 {
3697 /* If we were `reseat'ed to a new paragraph, 4115 struct text_pos tpos;
3698 determine the paragraph base direction. We need 4116 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
3699 to do it now because next_element_from_buffer may 4117
3700 not have a chance to do it, if we are going to 4118 SET_TEXT_POS (tpos, newpos, bpos);
3701 skip any text at the beginning, which resets the 4119 reseat_1 (it, tpos, 0);
3702 FIRST_ELT flag. */ 4120 /* If we reseat on a newline/ZV, we need to prep the
3703 bidi_paragraph_init (it->paragraph_embedding, 4121 bidi iterator for advancing to the next character
3704 &it->bidi_it, 1); 4122 after the newline/EOB, keeping the current paragraph
4123 direction (so that PRODUCE_GLYPHS does TRT wrt
4124 prepending/appending glyphs to a glyph row). */
4125 if (on_newline)
4126 {
4127 it->bidi_it.first_elt = 0;
4128 it->bidi_it.paragraph_dir = pdir;
4129 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4130 it->bidi_it.nchars = 1;
4131 it->bidi_it.ch_len = 1;
4132 }
3705 } 4133 }
3706 do 4134 else /* Must use the slow method. */
3707 { 4135 {
3708 bidi_move_to_visually_next (&it->bidi_it); 4136 /* With bidi iteration, the region of invisible text
4137 could start and/or end in the middle of a
4138 non-base embedding level. Therefore, we need to
4139 skip invisible text using the bidi iterator,
4140 starting at IT's current position, until we find
4141 ourselves outside of the invisible text.
4142 Skipping invisible text _after_ bidi iteration
4143 avoids affecting the visual order of the
4144 displayed text when invisible properties are
4145 added or removed. */
4146 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4147 {
4148 /* If we were `reseat'ed to a new paragraph,
4149 determine the paragraph base direction. We
4150 need to do it now because
4151 next_element_from_buffer may not have a
4152 chance to do it, if we are going to skip any
4153 text at the beginning, which resets the
4154 FIRST_ELT flag. */
4155 bidi_paragraph_init (it->paragraph_embedding,
4156 &it->bidi_it, 1);
4157 }
4158 do
4159 {
4160 bidi_move_to_visually_next (&it->bidi_it);
4161 }
4162 while (it->stop_charpos <= it->bidi_it.charpos
4163 && it->bidi_it.charpos < newpos);
4164 IT_CHARPOS (*it) = it->bidi_it.charpos;
4165 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4166 /* If we overstepped NEWPOS, record its position in
4167 the iterator, so that we skip invisible text if
4168 later the bidi iteration lands us in the
4169 invisible region again. */
4170 if (IT_CHARPOS (*it) >= newpos)
4171 it->prev_stop = newpos;
3709 } 4172 }
3710 while (it->stop_charpos <= it->bidi_it.charpos
3711 && it->bidi_it.charpos < newpos);
3712 IT_CHARPOS (*it) = it->bidi_it.charpos;
3713 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3714 /* If we overstepped NEWPOS, record its position in the
3715 iterator, so that we skip invisible text if later the
3716 bidi iteration lands us in the invisible region
3717 again. */
3718 if (IT_CHARPOS (*it) >= newpos)
3719 it->prev_stop = newpos;
3720 } 4173 }
3721 else 4174 else
3722 { 4175 {
@@ -3869,7 +4322,9 @@ handle_display_prop (struct it *it)
3869/* Subroutine of handle_display_prop. Returns non-zero if the display 4322/* Subroutine of handle_display_prop. Returns non-zero if the display
3870 specification in SPEC is a replacing specification, i.e. it would 4323 specification in SPEC is a replacing specification, i.e. it would
3871 replace the text covered by `display' property with something else, 4324 replace the text covered by `display' property with something else,
3872 such as an image or a display string. 4325 such as an image or a display string. If SPEC includes any kind or
4326 `(space ...) specification, the value is 2; this is used by
4327 compute_display_string_pos, which see.
3873 4328
3874 See handle_single_display_spec for documentation of arguments. 4329 See handle_single_display_spec for documentation of arguments.
3875 frame_window_p is non-zero if the window being redisplayed is on a 4330 frame_window_p is non-zero if the window being redisplayed is on a
@@ -3886,9 +4341,10 @@ handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
3886 EMACS_INT bufpos, int frame_window_p) 4341 EMACS_INT bufpos, int frame_window_p)
3887{ 4342{
3888 int replacing_p = 0; 4343 int replacing_p = 0;
4344 int rv;
3889 4345
3890 if (CONSP (spec) 4346 if (CONSP (spec)
3891 /* Simple specerties. */ 4347 /* Simple specifications. */
3892 && !EQ (XCAR (spec), Qimage) 4348 && !EQ (XCAR (spec), Qimage)
3893 && !EQ (XCAR (spec), Qspace) 4349 && !EQ (XCAR (spec), Qspace)
3894 && !EQ (XCAR (spec), Qwhen) 4350 && !EQ (XCAR (spec), Qwhen)
@@ -3904,11 +4360,11 @@ handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
3904 { 4360 {
3905 for (; CONSP (spec); spec = XCDR (spec)) 4361 for (; CONSP (spec); spec = XCDR (spec))
3906 { 4362 {
3907 if (handle_single_display_spec (it, XCAR (spec), object, overlay, 4363 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
3908 position, bufpos, replacing_p, 4364 overlay, position, bufpos,
3909 frame_window_p)) 4365 replacing_p, frame_window_p)))
3910 { 4366 {
3911 replacing_p = 1; 4367 replacing_p = rv;
3912 /* If some text in a string is replaced, `position' no 4368 /* If some text in a string is replaced, `position' no
3913 longer points to the position of `object'. */ 4369 longer points to the position of `object'. */
3914 if (!it || STRINGP (object)) 4370 if (!it || STRINGP (object))
@@ -3920,11 +4376,11 @@ handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
3920 { 4376 {
3921 int i; 4377 int i;
3922 for (i = 0; i < ASIZE (spec); ++i) 4378 for (i = 0; i < ASIZE (spec); ++i)
3923 if (handle_single_display_spec (it, AREF (spec, i), object, overlay, 4379 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
3924 position, bufpos, replacing_p, 4380 overlay, position, bufpos,
3925 frame_window_p)) 4381 replacing_p, frame_window_p)))
3926 { 4382 {
3927 replacing_p = 1; 4383 replacing_p = rv;
3928 /* If some text in a string is replaced, `position' no 4384 /* If some text in a string is replaced, `position' no
3929 longer points to the position of `object'. */ 4385 longer points to the position of `object'. */
3930 if (!it || STRINGP (object)) 4386 if (!it || STRINGP (object))
@@ -3933,9 +4389,10 @@ handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
3933 } 4389 }
3934 else 4390 else
3935 { 4391 {
3936 if (handle_single_display_spec (it, spec, object, overlay, 4392 if ((rv = handle_single_display_spec (it, spec, object, overlay,
3937 position, bufpos, 0, frame_window_p)) 4393 position, bufpos, 0,
3938 replacing_p = 1; 4394 frame_window_p)))
4395 replacing_p = rv;
3939 } 4396 }
3940 4397
3941 return replacing_p; 4398 return replacing_p;
@@ -4245,6 +4702,7 @@ handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4245 it->method = GET_FROM_IMAGE; 4702 it->method = GET_FROM_IMAGE;
4246 it->from_overlay = Qnil; 4703 it->from_overlay = Qnil;
4247 it->face_id = face_id; 4704 it->face_id = face_id;
4705 it->from_disp_prop_p = 1;
4248 4706
4249 /* Say that we haven't consumed the characters with 4707 /* Say that we haven't consumed the characters with
4250 `display' property yet. The call to pop_it in 4708 `display' property yet. The call to pop_it in
@@ -4310,13 +4768,23 @@ handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4310 4768
4311 if (valid_p && !display_replaced_p) 4769 if (valid_p && !display_replaced_p)
4312 { 4770 {
4771 int retval = 1;
4772
4313 if (!it) 4773 if (!it)
4314 return 1; 4774 {
4775 /* Callers need to know whether the display spec is any kind
4776 of `(space ...)' spec that is about to affect text-area
4777 display. */
4778 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4779 retval = 2;
4780 return retval;
4781 }
4315 4782
4316 /* Save current settings of IT so that we can restore them 4783 /* Save current settings of IT so that we can restore them
4317 when we are finished with the glyph property value. */ 4784 when we are finished with the glyph property value. */
4318 push_it (it, position); 4785 push_it (it, position);
4319 it->from_overlay = overlay; 4786 it->from_overlay = overlay;
4787 it->from_disp_prop_p = 1;
4320 4788
4321 if (NILP (location)) 4789 if (NILP (location))
4322 it->area = TEXT_AREA; 4790 it->area = TEXT_AREA;
@@ -4334,18 +4802,41 @@ handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4334 it->end_charpos = it->string_nchars = SCHARS (it->string); 4802 it->end_charpos = it->string_nchars = SCHARS (it->string);
4335 it->method = GET_FROM_STRING; 4803 it->method = GET_FROM_STRING;
4336 it->stop_charpos = 0; 4804 it->stop_charpos = 0;
4805 it->prev_stop = 0;
4806 it->base_level_stop = 0;
4337 it->string_from_display_prop_p = 1; 4807 it->string_from_display_prop_p = 1;
4338 /* Say that we haven't consumed the characters with 4808 /* Say that we haven't consumed the characters with
4339 `display' property yet. The call to pop_it in 4809 `display' property yet. The call to pop_it in
4340 set_iterator_to_next will clean this up. */ 4810 set_iterator_to_next will clean this up. */
4341 if (BUFFERP (object)) 4811 if (BUFFERP (object))
4342 *position = start_pos; 4812 *position = start_pos;
4813
4814 /* Force paragraph direction to be that of the parent
4815 object. If the parent object's paragraph direction is
4816 not yet determined, default to L2R. */
4817 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4818 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4819 else
4820 it->paragraph_embedding = L2R;
4821
4822 /* Set up the bidi iterator for this display string. */
4823 if (it->bidi_p)
4824 {
4825 it->bidi_it.string.lstring = it->string;
4826 it->bidi_it.string.s = NULL;
4827 it->bidi_it.string.schars = it->end_charpos;
4828 it->bidi_it.string.bufpos = bufpos;
4829 it->bidi_it.string.from_disp_str = 1;
4830 it->bidi_it.string.unibyte = !it->multibyte_p;
4831 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4832 }
4343 } 4833 }
4344 else if (CONSP (value) && EQ (XCAR (value), Qspace)) 4834 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4345 { 4835 {
4346 it->method = GET_FROM_STRETCH; 4836 it->method = GET_FROM_STRETCH;
4347 it->object = value; 4837 it->object = value;
4348 *position = it->position = start_pos; 4838 *position = it->position = start_pos;
4839 retval = 1 + (it->area == TEXT_AREA);
4349 } 4840 }
4350#ifdef HAVE_WINDOW_SYSTEM 4841#ifdef HAVE_WINDOW_SYSTEM
4351 else 4842 else
@@ -4363,7 +4854,7 @@ handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4363 } 4854 }
4364#endif /* HAVE_WINDOW_SYSTEM */ 4855#endif /* HAVE_WINDOW_SYSTEM */
4365 4856
4366 return 1; 4857 return retval;
4367 } 4858 }
4368 4859
4369 /* Invalid property or property not supported. Restore 4860 /* Invalid property or property not supported. Restore
@@ -4667,6 +5158,12 @@ next_overlay_string (struct it *it)
4667 it->current.overlay_string_index = -1; 5158 it->current.overlay_string_index = -1;
4668 it->n_overlay_strings = 0; 5159 it->n_overlay_strings = 0;
4669 it->overlay_strings_charpos = -1; 5160 it->overlay_strings_charpos = -1;
5161 /* If there's an empty display string on the stack, pop the
5162 stack, to resync the bidi iterator with IT's position. Such
5163 empty strings are pushed onto the stack in
5164 get_overlay_strings_1. */
5165 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5166 pop_it (it);
4670 5167
4671 /* If we're at the end of the buffer, record that we have 5168 /* If we're at the end of the buffer, record that we have
4672 processed the overlay strings there already, so that 5169 processed the overlay strings there already, so that
@@ -4696,6 +5193,20 @@ next_overlay_string (struct it *it)
4696 it->stop_charpos = 0; 5193 it->stop_charpos = 0;
4697 if (it->cmp_it.stop_pos >= 0) 5194 if (it->cmp_it.stop_pos >= 0)
4698 it->cmp_it.stop_pos = 0; 5195 it->cmp_it.stop_pos = 0;
5196 it->prev_stop = 0;
5197 it->base_level_stop = 0;
5198
5199 /* Set up the bidi iterator for this overlay string. */
5200 if (it->bidi_p)
5201 {
5202 it->bidi_it.string.lstring = it->string;
5203 it->bidi_it.string.s = NULL;
5204 it->bidi_it.string.schars = SCHARS (it->string);
5205 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5206 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5207 it->bidi_it.string.unibyte = !it->multibyte_p;
5208 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5209 }
4699 } 5210 }
4700 5211
4701 CHECK_IT (it); 5212 CHECK_IT (it);
@@ -4950,8 +5461,15 @@ get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
4950 xassert (!compute_stop_p || it->sp == 0); 5461 xassert (!compute_stop_p || it->sp == 0);
4951 5462
4952 /* When called from handle_stop, there might be an empty display 5463 /* When called from handle_stop, there might be an empty display
4953 string loaded. In that case, don't bother saving it. */ 5464 string loaded. In that case, don't bother saving it. But
4954 if (!STRINGP (it->string) || SCHARS (it->string)) 5465 don't use this optimization with the bidi iterator, since we
5466 need the corresponding pop_it call to resync the bidi
5467 iterator's position with IT's position, after we are done
5468 with the overlay strings. (The corresponding call to pop_it
5469 in case of an empty display string is in
5470 next_overlay_string.) */
5471 if (!(!it->bidi_p
5472 && STRINGP (it->string) && !SCHARS (it->string)))
4955 push_it (it, NULL); 5473 push_it (it, NULL);
4956 5474
4957 /* Set up IT to deliver display elements from the first overlay 5475 /* Set up IT to deliver display elements from the first overlay
@@ -4962,8 +5480,32 @@ get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
4962 it->stop_charpos = 0; 5480 it->stop_charpos = 0;
4963 xassert (STRINGP (it->string)); 5481 xassert (STRINGP (it->string));
4964 it->end_charpos = SCHARS (it->string); 5482 it->end_charpos = SCHARS (it->string);
5483 it->prev_stop = 0;
5484 it->base_level_stop = 0;
4965 it->multibyte_p = STRING_MULTIBYTE (it->string); 5485 it->multibyte_p = STRING_MULTIBYTE (it->string);
4966 it->method = GET_FROM_STRING; 5486 it->method = GET_FROM_STRING;
5487 it->from_disp_prop_p = 0;
5488
5489 /* Force paragraph direction to be that of the parent
5490 buffer. */
5491 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5492 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5493 else
5494 it->paragraph_embedding = L2R;
5495
5496 /* Set up the bidi iterator for this overlay string. */
5497 if (it->bidi_p)
5498 {
5499 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5500
5501 it->bidi_it.string.lstring = it->string;
5502 it->bidi_it.string.s = NULL;
5503 it->bidi_it.string.schars = SCHARS (it->string);
5504 it->bidi_it.string.bufpos = pos;
5505 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5506 it->bidi_it.string.unibyte = !it->multibyte_p;
5507 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5508 }
4967 return 1; 5509 return 1;
4968 } 5510 }
4969 5511
@@ -5036,23 +5578,38 @@ push_it (struct it *it, struct text_pos *position)
5036 p->font_height = it->font_height; 5578 p->font_height = it->font_height;
5037 p->voffset = it->voffset; 5579 p->voffset = it->voffset;
5038 p->string_from_display_prop_p = it->string_from_display_prop_p; 5580 p->string_from_display_prop_p = it->string_from_display_prop_p;
5581 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5039 p->display_ellipsis_p = 0; 5582 p->display_ellipsis_p = 0;
5040 p->line_wrap = it->line_wrap; 5583 p->line_wrap = it->line_wrap;
5584 p->bidi_p = it->bidi_p;
5585 p->paragraph_embedding = it->paragraph_embedding;
5586 p->from_disp_prop_p = it->from_disp_prop_p;
5041 ++it->sp; 5587 ++it->sp;
5588
5589 /* Save the state of the bidi iterator as well. */
5590 if (it->bidi_p)
5591 bidi_push_it (&it->bidi_it);
5042} 5592}
5043 5593
5044static void 5594static void
5045iterate_out_of_display_property (struct it *it) 5595iterate_out_of_display_property (struct it *it)
5046{ 5596{
5597 int buffer_p = BUFFERP (it->object);
5598 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5599 EMACS_INT bob = (buffer_p ? BEGV : 0);
5600
5601 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5602
5047 /* Maybe initialize paragraph direction. If we are at the beginning 5603 /* Maybe initialize paragraph direction. If we are at the beginning
5048 of a new paragraph, next_element_from_buffer may not have a 5604 of a new paragraph, next_element_from_buffer may not have a
5049 chance to do that. */ 5605 chance to do that. */
5050 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV) 5606 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5051 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1); 5607 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5052 /* prev_stop can be zero, so check against BEGV as well. */ 5608 /* prev_stop can be zero, so check against BEGV as well. */
5053 while (it->bidi_it.charpos >= BEGV 5609 while (it->bidi_it.charpos >= bob
5054 && it->prev_stop <= it->bidi_it.charpos 5610 && it->prev_stop <= it->bidi_it.charpos
5055 && it->bidi_it.charpos < CHARPOS (it->position)) 5611 && it->bidi_it.charpos < CHARPOS (it->position)
5612 && it->bidi_it.charpos < eob)
5056 bidi_move_to_visually_next (&it->bidi_it); 5613 bidi_move_to_visually_next (&it->bidi_it);
5057 /* Record the stop_pos we just crossed, for when we cross it 5614 /* Record the stop_pos we just crossed, for when we cross it
5058 back, maybe. */ 5615 back, maybe. */
@@ -5061,11 +5618,11 @@ iterate_out_of_display_property (struct it *it)
5061 /* If we ended up not where pop_it put us, resync IT's 5618 /* If we ended up not where pop_it put us, resync IT's
5062 positional members with the bidi iterator. */ 5619 positional members with the bidi iterator. */
5063 if (it->bidi_it.charpos != CHARPOS (it->position)) 5620 if (it->bidi_it.charpos != CHARPOS (it->position))
5064 { 5621 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5065 SET_TEXT_POS (it->position, 5622 if (buffer_p)
5066 it->bidi_it.charpos, it->bidi_it.bytepos); 5623 it->current.pos = it->position;
5067 it->current.pos = it->position; 5624 else
5068 } 5625 it->current.string_pos = it->position;
5069} 5626}
5070 5627
5071/* Restore IT's settings from IT->stack. Called, for example, when no 5628/* Restore IT's settings from IT->stack. Called, for example, when no
@@ -5078,6 +5635,7 @@ static void
5078pop_it (struct it *it) 5635pop_it (struct it *it)
5079{ 5636{
5080 struct iterator_stack_entry *p; 5637 struct iterator_stack_entry *p;
5638 int from_display_prop = it->from_disp_prop_p;
5081 5639
5082 xassert (it->sp > 0); 5640 xassert (it->sp > 0);
5083 --it->sp; 5641 --it->sp;
@@ -5102,22 +5660,10 @@ pop_it (struct it *it)
5102 it->slice = p->u.image.slice; 5660 it->slice = p->u.image.slice;
5103 break; 5661 break;
5104 case GET_FROM_STRETCH: 5662 case GET_FROM_STRETCH:
5105 it->object = p->u.comp.object; 5663 it->object = p->u.stretch.object;
5106 break; 5664 break;
5107 case GET_FROM_BUFFER: 5665 case GET_FROM_BUFFER:
5108 it->object = it->w->buffer; 5666 it->object = it->w->buffer;
5109 if (it->bidi_p)
5110 {
5111 /* Bidi-iterate until we get out of the portion of text, if
5112 any, covered by a `display' text property or an overlay
5113 with `display' property. (We cannot just jump there,
5114 because the internal coherency of the bidi iterator state
5115 can not be preserved across such jumps.) We also must
5116 determine the paragraph base direction if the overlay we
5117 just processed is at the beginning of a new
5118 paragraph. */
5119 iterate_out_of_display_property (it);
5120 }
5121 break; 5667 break;
5122 case GET_FROM_STRING: 5668 case GET_FROM_STRING:
5123 it->object = it->string; 5669 it->object = it->string;
@@ -5142,7 +5688,33 @@ pop_it (struct it *it)
5142 it->font_height = p->font_height; 5688 it->font_height = p->font_height;
5143 it->voffset = p->voffset; 5689 it->voffset = p->voffset;
5144 it->string_from_display_prop_p = p->string_from_display_prop_p; 5690 it->string_from_display_prop_p = p->string_from_display_prop_p;
5691 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5145 it->line_wrap = p->line_wrap; 5692 it->line_wrap = p->line_wrap;
5693 it->bidi_p = p->bidi_p;
5694 it->paragraph_embedding = p->paragraph_embedding;
5695 it->from_disp_prop_p = p->from_disp_prop_p;
5696 if (it->bidi_p)
5697 {
5698 bidi_pop_it (&it->bidi_it);
5699 /* Bidi-iterate until we get out of the portion of text, if any,
5700 covered by a `display' text property or by an overlay with
5701 `display' property. (We cannot just jump there, because the
5702 internal coherency of the bidi iterator state can not be
5703 preserved across such jumps.) We also must determine the
5704 paragraph base direction if the overlay we just processed is
5705 at the beginning of a new paragraph. */
5706 if (from_display_prop
5707 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5708 iterate_out_of_display_property (it);
5709
5710 xassert ((BUFFERP (it->object)
5711 && IT_CHARPOS (*it) == it->bidi_it.charpos
5712 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5713 || (STRINGP (it->object)
5714 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5715 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5716 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5717 }
5146} 5718}
5147 5719
5148 5720
@@ -5168,6 +5740,9 @@ back_to_previous_line_start (struct it *it)
5168 continuously over the text). Otherwise, don't change the value 5740 continuously over the text). Otherwise, don't change the value
5169 of *SKIPPED_P. 5741 of *SKIPPED_P.
5170 5742
5743 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5744 iterator on the newline, if it was found.
5745
5171 Newlines may come from buffer text, overlay strings, or strings 5746 Newlines may come from buffer text, overlay strings, or strings
5172 displayed via the `display' property. That's the reason we can't 5747 displayed via the `display' property. That's the reason we can't
5173 simply use find_next_newline_no_quit. 5748 simply use find_next_newline_no_quit.
@@ -5180,7 +5755,8 @@ back_to_previous_line_start (struct it *it)
5180 leads to wrong cursor motion. */ 5755 leads to wrong cursor motion. */
5181 5756
5182static int 5757static int
5183forward_to_next_line_start (struct it *it, int *skipped_p) 5758forward_to_next_line_start (struct it *it, int *skipped_p,
5759 struct bidi_it *bidi_it_prev)
5184{ 5760{
5185 EMACS_INT old_selective; 5761 EMACS_INT old_selective;
5186 int newline_found_p, n; 5762 int newline_found_p, n;
@@ -5192,6 +5768,8 @@ forward_to_next_line_start (struct it *it, int *skipped_p)
5192 && it->c == '\n' 5768 && it->c == '\n'
5193 && CHARPOS (it->position) == IT_CHARPOS (*it)) 5769 && CHARPOS (it->position) == IT_CHARPOS (*it))
5194 { 5770 {
5771 if (it->bidi_p && bidi_it_prev)
5772 *bidi_it_prev = it->bidi_it;
5195 set_iterator_to_next (it, 0); 5773 set_iterator_to_next (it, 0);
5196 it->c = 0; 5774 it->c = 0;
5197 return 1; 5775 return 1;
@@ -5213,6 +5791,8 @@ forward_to_next_line_start (struct it *it, int *skipped_p)
5213 if (!get_next_display_element (it)) 5791 if (!get_next_display_element (it))
5214 return 0; 5792 return 0;
5215 newline_found_p = it->what == IT_CHARACTER && it->c == '\n'; 5793 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5794 if (newline_found_p && it->bidi_p && bidi_it_prev)
5795 *bidi_it_prev = it->bidi_it;
5216 set_iterator_to_next (it, 0); 5796 set_iterator_to_next (it, 0);
5217 } 5797 }
5218 5798
@@ -5231,13 +5811,37 @@ forward_to_next_line_start (struct it *it, int *skipped_p)
5231 buffer text. */ 5811 buffer text. */
5232 if (it->stop_charpos >= limit 5812 if (it->stop_charpos >= limit
5233 || ((pos = Fnext_single_property_change (make_number (start), 5813 || ((pos = Fnext_single_property_change (make_number (start),
5234 Qdisplay, 5814 Qdisplay, Qnil,
5235 Qnil, make_number (limit)), 5815 make_number (limit)),
5236 NILP (pos)) 5816 NILP (pos))
5237 && next_overlay_change (start) == ZV)) 5817 && next_overlay_change (start) == ZV))
5238 { 5818 {
5239 IT_CHARPOS (*it) = limit; 5819 if (!it->bidi_p)
5240 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit); 5820 {
5821 IT_CHARPOS (*it) = limit;
5822 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5823 }
5824 else
5825 {
5826 struct bidi_it bprev;
5827
5828 /* Help bidi.c avoid expensive searches for display
5829 properties and overlays, by telling it that there are
5830 none up to `limit'. */
5831 if (it->bidi_it.disp_pos < limit)
5832 {
5833 it->bidi_it.disp_pos = limit;
5834 it->bidi_it.disp_prop = 0;
5835 }
5836 do {
5837 bprev = it->bidi_it;
5838 bidi_move_to_visually_next (&it->bidi_it);
5839 } while (it->bidi_it.charpos != limit);
5840 IT_CHARPOS (*it) = limit;
5841 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5842 if (bidi_it_prev)
5843 *bidi_it_prev = bprev;
5844 }
5241 *skipped_p = newline_found_p = 1; 5845 *skipped_p = newline_found_p = 1;
5242 } 5846 }
5243 else 5847 else
@@ -5246,6 +5850,8 @@ forward_to_next_line_start (struct it *it, int *skipped_p)
5246 && !newline_found_p) 5850 && !newline_found_p)
5247 { 5851 {
5248 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it); 5852 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5853 if (newline_found_p && it->bidi_p && bidi_it_prev)
5854 *bidi_it_prev = it->bidi_it;
5249 set_iterator_to_next (it, 0); 5855 set_iterator_to_next (it, 0);
5250 } 5856 }
5251 } 5857 }
@@ -5292,10 +5898,13 @@ back_to_previous_visible_line_start (struct it *it)
5292 5898
5293 { 5899 {
5294 struct it it2; 5900 struct it it2;
5901 void *it2data = NULL;
5295 EMACS_INT pos; 5902 EMACS_INT pos;
5296 EMACS_INT beg, end; 5903 EMACS_INT beg, end;
5297 Lisp_Object val, overlay; 5904 Lisp_Object val, overlay;
5298 5905
5906 SAVE_IT (it2, *it, it2data);
5907
5299 /* If newline is part of a composition, continue from start of composition */ 5908 /* If newline is part of a composition, continue from start of composition */
5300 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil) 5909 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5301 && beg < IT_CHARPOS (*it)) 5910 && beg < IT_CHARPOS (*it))
@@ -5303,20 +5912,25 @@ back_to_previous_visible_line_start (struct it *it)
5303 5912
5304 /* If newline is replaced by a display property, find start of overlay 5913 /* If newline is replaced by a display property, find start of overlay
5305 or interval and continue search from that point. */ 5914 or interval and continue search from that point. */
5306 it2 = *it;
5307 pos = --IT_CHARPOS (it2); 5915 pos = --IT_CHARPOS (it2);
5308 --IT_BYTEPOS (it2); 5916 --IT_BYTEPOS (it2);
5309 it2.sp = 0; 5917 it2.sp = 0;
5918 bidi_unshelve_cache (NULL, 0);
5310 it2.string_from_display_prop_p = 0; 5919 it2.string_from_display_prop_p = 0;
5920 it2.from_disp_prop_p = 0;
5311 if (handle_display_prop (&it2) == HANDLED_RETURN 5921 if (handle_display_prop (&it2) == HANDLED_RETURN
5312 && !NILP (val = get_char_property_and_overlay 5922 && !NILP (val = get_char_property_and_overlay
5313 (make_number (pos), Qdisplay, Qnil, &overlay)) 5923 (make_number (pos), Qdisplay, Qnil, &overlay))
5314 && (OVERLAYP (overlay) 5924 && (OVERLAYP (overlay)
5315 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay))) 5925 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5316 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil))) 5926 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5317 goto replaced; 5927 {
5928 RESTORE_IT (it, it, it2data);
5929 goto replaced;
5930 }
5318 5931
5319 /* Newline is not replaced by anything -- so we are done. */ 5932 /* Newline is not replaced by anything -- so we are done. */
5933 RESTORE_IT (it, it, it2data);
5320 break; 5934 break;
5321 5935
5322 replaced: 5936 replaced:
@@ -5361,8 +5975,9 @@ static void
5361reseat_at_next_visible_line_start (struct it *it, int on_newline_p) 5975reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5362{ 5976{
5363 int newline_found_p, skipped_p = 0; 5977 int newline_found_p, skipped_p = 0;
5978 struct bidi_it bidi_it_prev;
5364 5979
5365 newline_found_p = forward_to_next_line_start (it, &skipped_p); 5980 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5366 5981
5367 /* Skip over lines that are invisible because they are indented 5982 /* Skip over lines that are invisible because they are indented
5368 more than the value of IT->selective. */ 5983 more than the value of IT->selective. */
@@ -5373,7 +5988,8 @@ reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5373 { 5988 {
5374 xassert (IT_BYTEPOS (*it) == BEGV 5989 xassert (IT_BYTEPOS (*it) == BEGV
5375 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n'); 5990 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5376 newline_found_p = forward_to_next_line_start (it, &skipped_p); 5991 newline_found_p =
5992 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5377 } 5993 }
5378 5994
5379 /* Position on the newline if that's what's requested. */ 5995 /* Position on the newline if that's what's requested. */
@@ -5383,14 +5999,37 @@ reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5383 { 5999 {
5384 if (IT_STRING_CHARPOS (*it) > 0) 6000 if (IT_STRING_CHARPOS (*it) > 0)
5385 { 6001 {
5386 --IT_STRING_CHARPOS (*it); 6002 if (!it->bidi_p)
5387 --IT_STRING_BYTEPOS (*it); 6003 {
6004 --IT_STRING_CHARPOS (*it);
6005 --IT_STRING_BYTEPOS (*it);
6006 }
6007 else
6008 {
6009 /* We need to restore the bidi iterator to the state
6010 it had on the newline, and resync the IT's
6011 position with that. */
6012 it->bidi_it = bidi_it_prev;
6013 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6014 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6015 }
5388 } 6016 }
5389 } 6017 }
5390 else if (IT_CHARPOS (*it) > BEGV) 6018 else if (IT_CHARPOS (*it) > BEGV)
5391 { 6019 {
5392 --IT_CHARPOS (*it); 6020 if (!it->bidi_p)
5393 --IT_BYTEPOS (*it); 6021 {
6022 --IT_CHARPOS (*it);
6023 --IT_BYTEPOS (*it);
6024 }
6025 else
6026 {
6027 /* We need to restore the bidi iterator to the state it
6028 had on the newline and resync IT with that. */
6029 it->bidi_it = bidi_it_prev;
6030 IT_CHARPOS (*it) = it->bidi_it.charpos;
6031 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6032 }
5394 reseat (it, it->current.pos, 0); 6033 reseat (it, it->current.pos, 0);
5395 } 6034 }
5396 } 6035 }
@@ -5428,17 +6067,19 @@ reseat (struct it *it, struct text_pos pos, int force_p)
5428 { 6067 {
5429 /* For bidi iteration, we need to prime prev_stop and 6068 /* For bidi iteration, we need to prime prev_stop and
5430 base_level_stop with our best estimations. */ 6069 base_level_stop with our best estimations. */
5431 if (CHARPOS (pos) < it->prev_stop) 6070 /* Implementation note: Of course, POS is not necessarily a
5432 { 6071 stop position, so assigning prev_pos to it is a lie; we
5433 handle_stop_backwards (it, BEGV); 6072 should have called compute_stop_backwards. However, if
5434 if (CHARPOS (pos) < it->base_level_stop) 6073 the current buffer does not include any R2L characters,
5435 it->base_level_stop = 0; 6074 that call would be a waste of cycles, because the
5436 } 6075 iterator will never move back, and thus never cross this
5437 else if (CHARPOS (pos) > it->stop_charpos 6076 "fake" stop position. So we delay that backward search
5438 && it->stop_charpos >= BEGV) 6077 until the time we really need it, in next_element_from_buffer. */
5439 handle_stop_backwards (it, it->stop_charpos); 6078 if (CHARPOS (pos) != it->prev_stop)
5440 else /* force_p */ 6079 it->prev_stop = CHARPOS (pos);
5441 handle_stop (it); 6080 if (CHARPOS (pos) < it->base_level_stop)
6081 it->base_level_stop = 0; /* meaning it's unknown */
6082 handle_stop (it);
5442 } 6083 }
5443 else 6084 else
5444 { 6085 {
@@ -5472,19 +6113,26 @@ reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5472 IT_STRING_CHARPOS (*it) = -1; 6113 IT_STRING_CHARPOS (*it) = -1;
5473 IT_STRING_BYTEPOS (*it) = -1; 6114 IT_STRING_BYTEPOS (*it) = -1;
5474 it->string = Qnil; 6115 it->string = Qnil;
5475 it->string_from_display_prop_p = 0;
5476 it->method = GET_FROM_BUFFER; 6116 it->method = GET_FROM_BUFFER;
5477 it->object = it->w->buffer; 6117 it->object = it->w->buffer;
5478 it->area = TEXT_AREA; 6118 it->area = TEXT_AREA;
5479 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters)); 6119 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5480 it->sp = 0; 6120 it->sp = 0;
5481 it->string_from_display_prop_p = 0; 6121 it->string_from_display_prop_p = 0;
6122 it->string_from_prefix_prop_p = 0;
6123
6124 it->from_disp_prop_p = 0;
5482 it->face_before_selective_p = 0; 6125 it->face_before_selective_p = 0;
5483 if (it->bidi_p) 6126 if (it->bidi_p)
5484 { 6127 {
5485 it->bidi_it.first_elt = 1; 6128 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6129 &it->bidi_it);
6130 bidi_unshelve_cache (NULL, 0);
5486 it->bidi_it.paragraph_dir = NEUTRAL_DIR; 6131 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5487 it->bidi_it.disp_pos = -1; 6132 it->bidi_it.string.s = NULL;
6133 it->bidi_it.string.lstring = Qnil;
6134 it->bidi_it.string.bufpos = 0;
6135 it->bidi_it.string.unibyte = 0;
5488 } 6136 }
5489 6137
5490 if (set_stop_p) 6138 if (set_stop_p)
@@ -5535,6 +6183,14 @@ reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5535 if (multibyte >= 0) 6183 if (multibyte >= 0)
5536 it->multibyte_p = multibyte > 0; 6184 it->multibyte_p = multibyte > 0;
5537 6185
6186 /* Bidirectional reordering of strings is controlled by the default
6187 value of bidi-display-reordering. Don't try to reorder while
6188 loading loadup.el, as the necessary character property tables are
6189 not yet available. */
6190 it->bidi_p =
6191 NILP (Vpurify_flag)
6192 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6193
5538 if (s == NULL) 6194 if (s == NULL)
5539 { 6195 {
5540 xassert (STRINGP (string)); 6196 xassert (STRINGP (string));
@@ -5543,6 +6199,18 @@ reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5543 it->end_charpos = it->string_nchars = SCHARS (string); 6199 it->end_charpos = it->string_nchars = SCHARS (string);
5544 it->method = GET_FROM_STRING; 6200 it->method = GET_FROM_STRING;
5545 it->current.string_pos = string_pos (charpos, string); 6201 it->current.string_pos = string_pos (charpos, string);
6202
6203 if (it->bidi_p)
6204 {
6205 it->bidi_it.string.lstring = string;
6206 it->bidi_it.string.s = NULL;
6207 it->bidi_it.string.schars = it->end_charpos;
6208 it->bidi_it.string.bufpos = 0;
6209 it->bidi_it.string.from_disp_str = 0;
6210 it->bidi_it.string.unibyte = !it->multibyte_p;
6211 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6212 FRAME_WINDOW_P (it->f), &it->bidi_it);
6213 }
5546 } 6214 }
5547 else 6215 else
5548 { 6216 {
@@ -5563,13 +6231,28 @@ reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5563 it->end_charpos = it->string_nchars = strlen (s); 6231 it->end_charpos = it->string_nchars = strlen (s);
5564 } 6232 }
5565 6233
6234 if (it->bidi_p)
6235 {
6236 it->bidi_it.string.lstring = Qnil;
6237 it->bidi_it.string.s = (const unsigned char *) s;
6238 it->bidi_it.string.schars = it->end_charpos;
6239 it->bidi_it.string.bufpos = 0;
6240 it->bidi_it.string.from_disp_str = 0;
6241 it->bidi_it.string.unibyte = !it->multibyte_p;
6242 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6243 &it->bidi_it);
6244 }
5566 it->method = GET_FROM_C_STRING; 6245 it->method = GET_FROM_C_STRING;
5567 } 6246 }
5568 6247
5569 /* PRECISION > 0 means don't return more than PRECISION characters 6248 /* PRECISION > 0 means don't return more than PRECISION characters
5570 from the string. */ 6249 from the string. */
5571 if (precision > 0 && it->end_charpos - charpos > precision) 6250 if (precision > 0 && it->end_charpos - charpos > precision)
5572 it->end_charpos = it->string_nchars = charpos + precision; 6251 {
6252 it->end_charpos = it->string_nchars = charpos + precision;
6253 if (it->bidi_p)
6254 it->bidi_it.string.schars = it->end_charpos;
6255 }
5573 6256
5574 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH 6257 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5575 characters have been returned. FIELD_WIDTH == 0 means don't pad, 6258 characters have been returned. FIELD_WIDTH == 0 means don't pad,
@@ -5577,6 +6260,9 @@ reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5577 padding with `-' at the end of a mode line. */ 6260 padding with `-' at the end of a mode line. */
5578 if (field_width < 0) 6261 if (field_width < 0)
5579 field_width = INFINITY; 6262 field_width = INFINITY;
6263 /* Implementation note: We deliberately don't enlarge
6264 it->bidi_it.string.schars here to fit it->end_charpos, because
6265 the bidi iterator cannot produce characters out of thin air. */
5580 if (field_width > it->end_charpos - charpos) 6266 if (field_width > it->end_charpos - charpos)
5581 it->end_charpos = charpos + field_width; 6267 it->end_charpos = charpos + field_width;
5582 6268
@@ -5585,6 +6271,14 @@ reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5585 it->dp = XCHAR_TABLE (Vstandard_display_table); 6271 it->dp = XCHAR_TABLE (Vstandard_display_table);
5586 6272
5587 it->stop_charpos = charpos; 6273 it->stop_charpos = charpos;
6274 it->prev_stop = charpos;
6275 it->base_level_stop = 0;
6276 if (it->bidi_p)
6277 {
6278 it->bidi_it.first_elt = 1;
6279 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6280 it->bidi_it.disp_pos = -1;
6281 }
5588 if (s == NULL && it->multibyte_p) 6282 if (s == NULL && it->multibyte_p)
5589 { 6283 {
5590 EMACS_INT endpos = SCHARS (it->string); 6284 EMACS_INT endpos = SCHARS (it->string);
@@ -5736,8 +6430,8 @@ get_next_display_element (struct it *it)
5736 { 6430 {
5737 Lisp_Object dv; 6431 Lisp_Object dv;
5738 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte); 6432 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5739 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen } 6433 int nonascii_space_p = 0;
5740 nbsp_or_shy = char_is_other; 6434 int nonascii_hyphen_p = 0;
5741 int c = it->c; /* This is the character to display. */ 6435 int c = it->c; /* This is the character to display. */
5742 6436
5743 if (! it->multibyte_p && ! ASCII_CHAR_P (c)) 6437 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
@@ -5789,10 +6483,15 @@ get_next_display_element (struct it *it)
5789 goto get_next; 6483 goto get_next;
5790 } 6484 }
5791 6485
6486 /* If `nobreak-char-display' is non-nil, we display
6487 non-ASCII spaces and hyphens specially. */
5792 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display)) 6488 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5793 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp 6489 {
5794 : c == 0xAD ? char_is_soft_hyphen 6490 if (c == 0xA0)
5795 : char_is_other); 6491 nonascii_space_p = 1;
6492 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6493 nonascii_hyphen_p = 1;
6494 }
5796 6495
5797 /* Translate control characters into `\003' or `^C' form. 6496 /* Translate control characters into `\003' or `^C' form.
5798 Control characters coming from a display table entry are 6497 Control characters coming from a display table entry are
@@ -5800,7 +6499,8 @@ get_next_display_element (struct it *it)
5800 the translation. This could easily be changed but I 6499 the translation. This could easily be changed but I
5801 don't believe that it is worth doing. 6500 don't believe that it is worth doing.
5802 6501
5803 NBSP and SOFT-HYPEN are property translated too. 6502 The characters handled by `nobreak-char-display' must be
6503 translated too.
5804 6504
5805 Non-printable characters and raw-byte characters are also 6505 Non-printable characters and raw-byte characters are also
5806 translated to octal form. */ 6506 translated to octal form. */
@@ -5811,14 +6511,15 @@ get_next_display_element (struct it *it)
5811 && it->glyph_row 6511 && it->glyph_row
5812 && (it->glyph_row->mode_line_p || it->avoid_cursor_p)) 6512 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5813 || (c != '\n' && c != '\t')) 6513 || (c != '\n' && c != '\t'))
5814 : (nbsp_or_shy 6514 : (nonascii_space_p
6515 || nonascii_hyphen_p
5815 || CHAR_BYTE8_P (c) 6516 || CHAR_BYTE8_P (c)
5816 || ! CHAR_PRINTABLE_P (c)))) 6517 || ! CHAR_PRINTABLE_P (c))))
5817 { 6518 {
5818 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte, 6519 /* C is a control character, non-ASCII space/hyphen,
5819 or a non-printable character which must be displayed 6520 raw-byte, or a non-printable character which must be
5820 either as '\003' or as `^C' where the '\\' and '^' 6521 displayed either as '\003' or as `^C' where the '\\'
5821 can be defined in the display table. Fill 6522 and '^' can be defined in the display table. Fill
5822 IT->ctl_chars with glyphs for what we have to 6523 IT->ctl_chars with glyphs for what we have to
5823 display. Then, set IT->dpvec to these glyphs. */ 6524 display. Then, set IT->dpvec to these glyphs. */
5824 Lisp_Object gc; 6525 Lisp_Object gc;
@@ -5867,17 +6568,14 @@ get_next_display_element (struct it *it)
5867 goto display_control; 6568 goto display_control;
5868 } 6569 }
5869 6570
5870 /* Handle non-break space in the mode where it only gets 6571 /* Handle non-ascii space in the mode where it only gets
5871 highlighting. */ 6572 highlighting. */
5872 6573
5873 if (EQ (Vnobreak_char_display, Qt) 6574 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
5874 && nbsp_or_shy == char_is_nbsp)
5875 { 6575 {
5876 /* Merge the no-break-space face into the current face. */ 6576 /* Merge `nobreak-space' into the current face. */
5877 face_id = merge_faces (it->f, Qnobreak_space, 0, 6577 face_id = merge_faces (it->f, Qnobreak_space, 0,
5878 it->face_id); 6578 it->face_id);
5879
5880 c = ' ';
5881 XSETINT (it->ctl_chars[0], ' '); 6579 XSETINT (it->ctl_chars[0], ' ');
5882 ctl_len = 1; 6580 ctl_len = 1;
5883 goto display_control; 6581 goto display_control;
@@ -5917,25 +6615,21 @@ get_next_display_element (struct it *it)
5917 last_escape_glyph_merged_face_id = face_id; 6615 last_escape_glyph_merged_face_id = face_id;
5918 } 6616 }
5919 6617
5920 /* Handle soft hyphens in the mode where they only get 6618 /* Draw non-ASCII hyphen with just highlighting: */
5921 highlighting. */
5922 6619
5923 if (EQ (Vnobreak_char_display, Qt) 6620 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
5924 && nbsp_or_shy == char_is_soft_hyphen)
5925 { 6621 {
5926 XSETINT (it->ctl_chars[0], '-'); 6622 XSETINT (it->ctl_chars[0], '-');
5927 ctl_len = 1; 6623 ctl_len = 1;
5928 goto display_control; 6624 goto display_control;
5929 } 6625 }
5930 6626
5931 /* Handle non-break space and soft hyphen 6627 /* Draw non-ASCII space/hyphen with escape glyph: */
5932 with the escape glyph. */
5933 6628
5934 if (nbsp_or_shy) 6629 if (nonascii_space_p || nonascii_hyphen_p)
5935 { 6630 {
5936 XSETINT (it->ctl_chars[0], escape_glyph); 6631 XSETINT (it->ctl_chars[0], escape_glyph);
5937 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-'); 6632 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
5938 XSETINT (it->ctl_chars[1], c);
5939 ctl_len = 2; 6633 ctl_len = 2;
5940 goto display_control; 6634 goto display_control;
5941 } 6635 }
@@ -6007,6 +6701,8 @@ get_next_display_element (struct it *it)
6007 6701
6008 c = ' '; 6702 c = ' ';
6009 for (i = 0; i < cmp->glyph_len; i++) 6703 for (i = 0; i < cmp->glyph_len; i++)
6704 /* TAB in a composition means display glyphs with
6705 padding space on the left or right. */
6010 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t') 6706 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6011 break; 6707 break;
6012 } 6708 }
@@ -6219,8 +6915,22 @@ set_iterator_to_next (struct it *it, int reseat_p)
6219 6915
6220 case GET_FROM_C_STRING: 6916 case GET_FROM_C_STRING:
6221 /* Current display element of IT is from a C string. */ 6917 /* Current display element of IT is from a C string. */
6222 IT_BYTEPOS (*it) += it->len; 6918 if (!it->bidi_p
6223 IT_CHARPOS (*it) += 1; 6919 /* If the string position is beyond string's end, it means
6920 next_element_from_c_string is padding the string with
6921 blanks, in which case we bypass the bidi iterator,
6922 because it cannot deal with such virtual characters. */
6923 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6924 {
6925 IT_BYTEPOS (*it) += it->len;
6926 IT_CHARPOS (*it) += 1;
6927 }
6928 else
6929 {
6930 bidi_move_to_visually_next (&it->bidi_it);
6931 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6932 IT_CHARPOS (*it) = it->bidi_it.charpos;
6933 }
6224 break; 6934 break;
6225 6935
6226 case GET_FROM_DISPLAY_VECTOR: 6936 case GET_FROM_DISPLAY_VECTOR:
@@ -6274,23 +6984,95 @@ set_iterator_to_next (struct it *it, int reseat_p)
6274 xassert (it->s == NULL && STRINGP (it->string)); 6984 xassert (it->s == NULL && STRINGP (it->string));
6275 if (it->cmp_it.id >= 0) 6985 if (it->cmp_it.id >= 0)
6276 { 6986 {
6277 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars; 6987 int i;
6278 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes; 6988
6279 if (it->cmp_it.to < it->cmp_it.nglyphs) 6989 if (! it->bidi_p)
6280 it->cmp_it.from = it->cmp_it.to; 6990 {
6991 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6992 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6993 if (it->cmp_it.to < it->cmp_it.nglyphs)
6994 it->cmp_it.from = it->cmp_it.to;
6995 else
6996 {
6997 it->cmp_it.id = -1;
6998 composition_compute_stop_pos (&it->cmp_it,
6999 IT_STRING_CHARPOS (*it),
7000 IT_STRING_BYTEPOS (*it),
7001 it->end_charpos, it->string);
7002 }
7003 }
7004 else if (! it->cmp_it.reversed_p)
7005 {
7006 for (i = 0; i < it->cmp_it.nchars; i++)
7007 bidi_move_to_visually_next (&it->bidi_it);
7008 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7009 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7010
7011 if (it->cmp_it.to < it->cmp_it.nglyphs)
7012 it->cmp_it.from = it->cmp_it.to;
7013 else
7014 {
7015 EMACS_INT stop = it->end_charpos;
7016 if (it->bidi_it.scan_dir < 0)
7017 stop = -1;
7018 composition_compute_stop_pos (&it->cmp_it,
7019 IT_STRING_CHARPOS (*it),
7020 IT_STRING_BYTEPOS (*it), stop,
7021 it->string);
7022 }
7023 }
6281 else 7024 else
6282 { 7025 {
6283 it->cmp_it.id = -1; 7026 for (i = 0; i < it->cmp_it.nchars; i++)
6284 composition_compute_stop_pos (&it->cmp_it, 7027 bidi_move_to_visually_next (&it->bidi_it);
6285 IT_STRING_CHARPOS (*it), 7028 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6286 IT_STRING_BYTEPOS (*it), 7029 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6287 it->end_charpos, it->string); 7030 if (it->cmp_it.from > 0)
7031 it->cmp_it.to = it->cmp_it.from;
7032 else
7033 {
7034 EMACS_INT stop = it->end_charpos;
7035 if (it->bidi_it.scan_dir < 0)
7036 stop = -1;
7037 composition_compute_stop_pos (&it->cmp_it,
7038 IT_STRING_CHARPOS (*it),
7039 IT_STRING_BYTEPOS (*it), stop,
7040 it->string);
7041 }
6288 } 7042 }
6289 } 7043 }
6290 else 7044 else
6291 { 7045 {
6292 IT_STRING_BYTEPOS (*it) += it->len; 7046 if (!it->bidi_p
6293 IT_STRING_CHARPOS (*it) += 1; 7047 /* If the string position is beyond string's end, it
7048 means next_element_from_string is padding the string
7049 with blanks, in which case we bypass the bidi
7050 iterator, because it cannot deal with such virtual
7051 characters. */
7052 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7053 {
7054 IT_STRING_BYTEPOS (*it) += it->len;
7055 IT_STRING_CHARPOS (*it) += 1;
7056 }
7057 else
7058 {
7059 int prev_scan_dir = it->bidi_it.scan_dir;
7060
7061 bidi_move_to_visually_next (&it->bidi_it);
7062 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7063 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7064 if (prev_scan_dir != it->bidi_it.scan_dir)
7065 {
7066 EMACS_INT stop = it->end_charpos;
7067
7068 if (it->bidi_it.scan_dir < 0)
7069 stop = -1;
7070 composition_compute_stop_pos (&it->cmp_it,
7071 IT_STRING_CHARPOS (*it),
7072 IT_STRING_BYTEPOS (*it), stop,
7073 it->string);
7074 }
7075 }
6294 } 7076 }
6295 7077
6296 consider_string_end: 7078 consider_string_end:
@@ -6396,6 +7178,106 @@ next_element_from_display_vector (struct it *it)
6396 return 1; 7178 return 1;
6397} 7179}
6398 7180
7181/* Get the first element of string/buffer in the visual order, after
7182 being reseated to a new position in a string or a buffer. */
7183static void
7184get_visually_first_element (struct it *it)
7185{
7186 int string_p = STRINGP (it->string) || it->s;
7187 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
7188 EMACS_INT bob = (string_p ? 0 : BEGV);
7189
7190 if (STRINGP (it->string))
7191 {
7192 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7193 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7194 }
7195 else
7196 {
7197 it->bidi_it.charpos = IT_CHARPOS (*it);
7198 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7199 }
7200
7201 if (it->bidi_it.charpos == eob)
7202 {
7203 /* Nothing to do, but reset the FIRST_ELT flag, like
7204 bidi_paragraph_init does, because we are not going to
7205 call it. */
7206 it->bidi_it.first_elt = 0;
7207 }
7208 else if (it->bidi_it.charpos == bob
7209 || (!string_p
7210 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7211 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7212 {
7213 /* If we are at the beginning of a line/string, we can produce
7214 the next element right away. */
7215 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7216 bidi_move_to_visually_next (&it->bidi_it);
7217 }
7218 else
7219 {
7220 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
7221
7222 /* We need to prime the bidi iterator starting at the line's or
7223 string's beginning, before we will be able to produce the
7224 next element. */
7225 if (string_p)
7226 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7227 else
7228 {
7229 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7230 -1);
7231 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7232 }
7233 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7234 do
7235 {
7236 /* Now return to buffer/string position where we were asked
7237 to get the next display element, and produce that. */
7238 bidi_move_to_visually_next (&it->bidi_it);
7239 }
7240 while (it->bidi_it.bytepos != orig_bytepos
7241 && it->bidi_it.charpos < eob);
7242 }
7243
7244 /* Adjust IT's position information to where we ended up. */
7245 if (STRINGP (it->string))
7246 {
7247 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7248 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7249 }
7250 else
7251 {
7252 IT_CHARPOS (*it) = it->bidi_it.charpos;
7253 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7254 }
7255
7256 if (STRINGP (it->string) || !it->s)
7257 {
7258 EMACS_INT stop, charpos, bytepos;
7259
7260 if (STRINGP (it->string))
7261 {
7262 xassert (!it->s);
7263 stop = SCHARS (it->string);
7264 if (stop > it->end_charpos)
7265 stop = it->end_charpos;
7266 charpos = IT_STRING_CHARPOS (*it);
7267 bytepos = IT_STRING_BYTEPOS (*it);
7268 }
7269 else
7270 {
7271 stop = it->end_charpos;
7272 charpos = IT_CHARPOS (*it);
7273 bytepos = IT_BYTEPOS (*it);
7274 }
7275 if (it->bidi_it.scan_dir < 0)
7276 stop = -1;
7277 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7278 it->string);
7279 }
7280}
6399 7281
6400/* Load IT with the next display element from Lisp string IT->string. 7282/* Load IT with the next display element from Lisp string IT->string.
6401 IT->current.string_pos is the current position within the string. 7283 IT->current.string_pos is the current position within the string.
@@ -6408,24 +7290,85 @@ next_element_from_string (struct it *it)
6408 struct text_pos position; 7290 struct text_pos position;
6409 7291
6410 xassert (STRINGP (it->string)); 7292 xassert (STRINGP (it->string));
7293 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
6411 xassert (IT_STRING_CHARPOS (*it) >= 0); 7294 xassert (IT_STRING_CHARPOS (*it) >= 0);
6412 position = it->current.string_pos; 7295 position = it->current.string_pos;
6413 7296
7297 /* With bidi reordering, the character to display might not be the
7298 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7299 that we were reseat()ed to a new string, whose paragraph
7300 direction is not known. */
7301 if (it->bidi_p && it->bidi_it.first_elt)
7302 {
7303 get_visually_first_element (it);
7304 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7305 }
7306
6414 /* Time to check for invisible text? */ 7307 /* Time to check for invisible text? */
6415 if (IT_STRING_CHARPOS (*it) < it->end_charpos 7308 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
6416 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6417 { 7309 {
6418 handle_stop (it); 7310 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7311 {
7312 if (!(!it->bidi_p
7313 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7314 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7315 {
7316 /* With bidi non-linear iteration, we could find
7317 ourselves far beyond the last computed stop_charpos,
7318 with several other stop positions in between that we
7319 missed. Scan them all now, in buffer's logical
7320 order, until we find and handle the last stop_charpos
7321 that precedes our current position. */
7322 handle_stop_backwards (it, it->stop_charpos);
7323 return GET_NEXT_DISPLAY_ELEMENT (it);
7324 }
7325 else
7326 {
7327 if (it->bidi_p)
7328 {
7329 /* Take note of the stop position we just moved
7330 across, for when we will move back across it. */
7331 it->prev_stop = it->stop_charpos;
7332 /* If we are at base paragraph embedding level, take
7333 note of the last stop position seen at this
7334 level. */
7335 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7336 it->base_level_stop = it->stop_charpos;
7337 }
7338 handle_stop (it);
6419 7339
6420 /* Since a handler may have changed IT->method, we must 7340 /* Since a handler may have changed IT->method, we must
6421 recurse here. */ 7341 recurse here. */
6422 return GET_NEXT_DISPLAY_ELEMENT (it); 7342 return GET_NEXT_DISPLAY_ELEMENT (it);
7343 }
7344 }
7345 else if (it->bidi_p
7346 /* If we are before prev_stop, we may have overstepped
7347 on our way backwards a stop_pos, and if so, we need
7348 to handle that stop_pos. */
7349 && IT_STRING_CHARPOS (*it) < it->prev_stop
7350 /* We can sometimes back up for reasons that have nothing
7351 to do with bidi reordering. E.g., compositions. The
7352 code below is only needed when we are above the base
7353 embedding level, so test for that explicitly. */
7354 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7355 {
7356 /* If we lost track of base_level_stop, we have no better
7357 place for handle_stop_backwards to start from than string
7358 beginning. This happens, e.g., when we were reseated to
7359 the previous screenful of text by vertical-motion. */
7360 if (it->base_level_stop <= 0
7361 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7362 it->base_level_stop = 0;
7363 handle_stop_backwards (it, it->base_level_stop);
7364 return GET_NEXT_DISPLAY_ELEMENT (it);
7365 }
6423 } 7366 }
6424 7367
6425 if (it->current.overlay_string_index >= 0) 7368 if (it->current.overlay_string_index >= 0)
6426 { 7369 {
6427 /* Get the next character from an overlay string. In overlay 7370 /* Get the next character from an overlay string. In overlay
6428 strings, There is no field width or padding with spaces to 7371 strings, there is no field width or padding with spaces to
6429 do. */ 7372 do. */
6430 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)) 7373 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6431 { 7374 {
@@ -6433,7 +7376,10 @@ next_element_from_string (struct it *it)
6433 return 0; 7376 return 0;
6434 } 7377 }
6435 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it), 7378 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6436 IT_STRING_BYTEPOS (*it), SCHARS (it->string)) 7379 IT_STRING_BYTEPOS (*it),
7380 it->bidi_it.scan_dir < 0
7381 ? -1
7382 : SCHARS (it->string))
6437 && next_element_from_composition (it)) 7383 && next_element_from_composition (it))
6438 { 7384 {
6439 return 1; 7385 return 1;
@@ -6468,7 +7414,10 @@ next_element_from_string (struct it *it)
6468 CHARPOS (position) = BYTEPOS (position) = -1; 7414 CHARPOS (position) = BYTEPOS (position) = -1;
6469 } 7415 }
6470 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it), 7416 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6471 IT_STRING_BYTEPOS (*it), it->string_nchars) 7417 IT_STRING_BYTEPOS (*it),
7418 it->bidi_it.scan_dir < 0
7419 ? -1
7420 : it->string_nchars)
6472 && next_element_from_composition (it)) 7421 && next_element_from_composition (it))
6473 { 7422 {
6474 return 1; 7423 return 1;
@@ -6507,12 +7456,20 @@ next_element_from_c_string (struct it *it)
6507 int success_p = 1; 7456 int success_p = 1;
6508 7457
6509 xassert (it->s); 7458 xassert (it->s);
7459 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
6510 it->what = IT_CHARACTER; 7460 it->what = IT_CHARACTER;
6511 BYTEPOS (it->position) = CHARPOS (it->position) = 0; 7461 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6512 it->object = Qnil; 7462 it->object = Qnil;
6513 7463
6514 /* IT's position can be greater IT->string_nchars in case a field 7464 /* With bidi reordering, the character to display might not be the
6515 width or precision has been specified when the iterator was 7465 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7466 we were reseated to a new string, whose paragraph direction is
7467 not known. */
7468 if (it->bidi_p && it->bidi_it.first_elt)
7469 get_visually_first_element (it);
7470
7471 /* IT's position can be greater than IT->string_nchars in case a
7472 field width or precision has been specified when the iterator was
6516 initialized. */ 7473 initialized. */
6517 if (IT_CHARPOS (*it) >= it->end_charpos) 7474 if (IT_CHARPOS (*it) >= it->end_charpos)
6518 { 7475 {
@@ -6587,30 +7544,81 @@ next_element_from_stretch (struct it *it)
6587 return 1; 7544 return 1;
6588} 7545}
6589 7546
6590/* Scan forward from CHARPOS in the current buffer, until we find a 7547/* Scan backwards from IT's current position until we find a stop
6591 stop position > current IT's position. Then handle the stop 7548 position, or until BEGV. This is called when we find ourself
7549 before both the last known prev_stop and base_level_stop while
7550 reordering bidirectional text. */
7551
7552static void
7553compute_stop_pos_backwards (struct it *it)
7554{
7555 const int SCAN_BACK_LIMIT = 1000;
7556 struct text_pos pos;
7557 struct display_pos save_current = it->current;
7558 struct text_pos save_position = it->position;
7559 EMACS_INT charpos = IT_CHARPOS (*it);
7560 EMACS_INT where_we_are = charpos;
7561 EMACS_INT save_stop_pos = it->stop_charpos;
7562 EMACS_INT save_end_pos = it->end_charpos;
7563
7564 xassert (NILP (it->string) && !it->s);
7565 xassert (it->bidi_p);
7566 it->bidi_p = 0;
7567 do
7568 {
7569 it->end_charpos = min (charpos + 1, ZV);
7570 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7571 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7572 reseat_1 (it, pos, 0);
7573 compute_stop_pos (it);
7574 /* We must advance forward, right? */
7575 if (it->stop_charpos <= charpos)
7576 abort ();
7577 }
7578 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7579
7580 if (it->stop_charpos <= where_we_are)
7581 it->prev_stop = it->stop_charpos;
7582 else
7583 it->prev_stop = BEGV;
7584 it->bidi_p = 1;
7585 it->current = save_current;
7586 it->position = save_position;
7587 it->stop_charpos = save_stop_pos;
7588 it->end_charpos = save_end_pos;
7589}
7590
7591/* Scan forward from CHARPOS in the current buffer/string, until we
7592 find a stop position > current IT's position. Then handle the stop
6592 position before that. This is called when we bump into a stop 7593 position before that. This is called when we bump into a stop
6593 position while reordering bidirectional text. CHARPOS should be 7594 position while reordering bidirectional text. CHARPOS should be
6594 the last previously processed stop_pos (or BEGV, if none were 7595 the last previously processed stop_pos (or BEGV/0, if none were
6595 processed yet) whose position is less that IT's current 7596 processed yet) whose position is less that IT's current
6596 position. */ 7597 position. */
6597 7598
6598static void 7599static void
6599handle_stop_backwards (struct it *it, EMACS_INT charpos) 7600handle_stop_backwards (struct it *it, EMACS_INT charpos)
6600{ 7601{
6601 EMACS_INT where_we_are = IT_CHARPOS (*it); 7602 int bufp = !STRINGP (it->string);
7603 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
6602 struct display_pos save_current = it->current; 7604 struct display_pos save_current = it->current;
6603 struct text_pos save_position = it->position; 7605 struct text_pos save_position = it->position;
6604 struct text_pos pos1; 7606 struct text_pos pos1;
6605 EMACS_INT next_stop; 7607 EMACS_INT next_stop;
6606 7608
6607 /* Scan in strict logical order. */ 7609 /* Scan in strict logical order. */
7610 xassert (it->bidi_p);
6608 it->bidi_p = 0; 7611 it->bidi_p = 0;
6609 do 7612 do
6610 { 7613 {
6611 it->prev_stop = charpos; 7614 it->prev_stop = charpos;
6612 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos)); 7615 if (bufp)
6613 reseat_1 (it, pos1, 0); 7616 {
7617 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7618 reseat_1 (it, pos1, 0);
7619 }
7620 else
7621 it->current.string_pos = string_pos (charpos, it->string);
6614 compute_stop_pos (it); 7622 compute_stop_pos (it);
6615 /* We must advance forward, right? */ 7623 /* We must advance forward, right? */
6616 if (it->stop_charpos <= it->prev_stop) 7624 if (it->stop_charpos <= it->prev_stop)
@@ -6619,11 +7627,11 @@ handle_stop_backwards (struct it *it, EMACS_INT charpos)
6619 } 7627 }
6620 while (charpos <= where_we_are); 7628 while (charpos <= where_we_are);
6621 7629
6622 next_stop = it->stop_charpos;
6623 it->stop_charpos = it->prev_stop;
6624 it->bidi_p = 1; 7630 it->bidi_p = 1;
6625 it->current = save_current; 7631 it->current = save_current;
6626 it->position = save_position; 7632 it->position = save_position;
7633 next_stop = it->stop_charpos;
7634 it->stop_charpos = it->prev_stop;
6627 handle_stop (it); 7635 handle_stop (it);
6628 it->stop_charpos = next_stop; 7636 it->stop_charpos = next_stop;
6629} 7637}
@@ -6639,6 +7647,10 @@ next_element_from_buffer (struct it *it)
6639 int success_p = 1; 7647 int success_p = 1;
6640 7648
6641 xassert (IT_CHARPOS (*it) >= BEGV); 7649 xassert (IT_CHARPOS (*it) >= BEGV);
7650 xassert (NILP (it->string) && !it->s);
7651 xassert (!it->bidi_p
7652 || (EQ (it->bidi_it.string.lstring, Qnil)
7653 && it->bidi_it.string.s == NULL));
6642 7654
6643 /* With bidi reordering, the character to display might not be the 7655 /* With bidi reordering, the character to display might not be the
6644 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that 7656 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
@@ -6646,59 +7658,8 @@ next_element_from_buffer (struct it *it)
6646 a different paragraph. */ 7658 a different paragraph. */
6647 if (it->bidi_p && it->bidi_it.first_elt) 7659 if (it->bidi_p && it->bidi_it.first_elt)
6648 { 7660 {
6649 it->bidi_it.charpos = IT_CHARPOS (*it); 7661 get_visually_first_element (it);
6650 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6651 if (it->bidi_it.bytepos == ZV_BYTE)
6652 {
6653 /* Nothing to do, but reset the FIRST_ELT flag, like
6654 bidi_paragraph_init does, because we are not going to
6655 call it. */
6656 it->bidi_it.first_elt = 0;
6657 }
6658 else if (it->bidi_it.bytepos == BEGV_BYTE
6659 /* FIXME: Should support all Unicode line separators. */
6660 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6661 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6662 {
6663 /* If we are at the beginning of a line, we can produce the
6664 next element right away. */
6665 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6666 bidi_move_to_visually_next (&it->bidi_it);
6667 }
6668 else
6669 {
6670 EMACS_INT orig_bytepos = IT_BYTEPOS (*it);
6671
6672 /* We need to prime the bidi iterator starting at the line's
6673 beginning, before we will be able to produce the next
6674 element. */
6675 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6676 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6677 it->bidi_it.charpos = IT_CHARPOS (*it);
6678 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6679 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6680 do
6681 {
6682 /* Now return to buffer position where we were asked to
6683 get the next display element, and produce that. */
6684 bidi_move_to_visually_next (&it->bidi_it);
6685 }
6686 while (it->bidi_it.bytepos != orig_bytepos
6687 && it->bidi_it.bytepos < ZV_BYTE);
6688 }
6689
6690 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6691 /* Adjust IT's position information to where we ended up. */
6692 IT_CHARPOS (*it) = it->bidi_it.charpos;
6693 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6694 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it)); 7662 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6695 {
6696 EMACS_INT stop = it->end_charpos;
6697 if (it->bidi_it.scan_dir < 0)
6698 stop = -1;
6699 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6700 IT_BYTEPOS (*it), stop, Qnil);
6701 }
6702 } 7663 }
6703 7664
6704 if (IT_CHARPOS (*it) >= it->stop_charpos) 7665 if (IT_CHARPOS (*it) >= it->stop_charpos)
@@ -6757,18 +7718,29 @@ next_element_from_buffer (struct it *it)
6757 } 7718 }
6758 } 7719 }
6759 else if (it->bidi_p 7720 else if (it->bidi_p
7721 /* If we are before prev_stop, we may have overstepped on
7722 our way backwards a stop_pos, and if so, we need to
7723 handle that stop_pos. */
7724 && IT_CHARPOS (*it) < it->prev_stop
6760 /* We can sometimes back up for reasons that have nothing 7725 /* We can sometimes back up for reasons that have nothing
6761 to do with bidi reordering. E.g., compositions. The 7726 to do with bidi reordering. E.g., compositions. The
6762 code below is only needed when we are above the base 7727 code below is only needed when we are above the base
6763 embedding level, so test for that explicitly. */ 7728 embedding level, so test for that explicitly. */
6764 && !BIDI_AT_BASE_LEVEL (it->bidi_it) 7729 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
6765 && IT_CHARPOS (*it) < it->prev_stop)
6766 { 7730 {
6767 if (it->base_level_stop <= 0) 7731 if (it->base_level_stop <= 0
6768 it->base_level_stop = BEGV; 7732 || IT_CHARPOS (*it) < it->base_level_stop)
6769 if (IT_CHARPOS (*it) < it->base_level_stop) 7733 {
6770 abort (); 7734 /* If we lost track of base_level_stop, we need to find
6771 handle_stop_backwards (it, it->base_level_stop); 7735 prev_stop by looking backwards. This happens, e.g., when
7736 we were reseated to the previous screenful of text by
7737 vertical-motion. */
7738 it->base_level_stop = BEGV;
7739 compute_stop_pos_backwards (it);
7740 handle_stop_backwards (it, it->prev_stop);
7741 }
7742 else
7743 handle_stop_backwards (it, it->base_level_stop);
6772 return GET_NEXT_DISPLAY_ELEMENT (it); 7744 return GET_NEXT_DISPLAY_ELEMENT (it);
6773 } 7745 }
6774 else 7746 else
@@ -6970,10 +7942,13 @@ move_it_in_display_line_to (struct it *it,
6970{ 7942{
6971 enum move_it_result result = MOVE_UNDEFINED; 7943 enum move_it_result result = MOVE_UNDEFINED;
6972 struct glyph_row *saved_glyph_row; 7944 struct glyph_row *saved_glyph_row;
6973 struct it wrap_it, atpos_it, atx_it; 7945 struct it wrap_it, atpos_it, atx_it, ppos_it;
7946 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
7947 void *ppos_data = NULL;
6974 int may_wrap = 0; 7948 int may_wrap = 0;
6975 enum it_method prev_method = it->method; 7949 enum it_method prev_method = it->method;
6976 EMACS_INT prev_pos = IT_CHARPOS (*it); 7950 EMACS_INT prev_pos = IT_CHARPOS (*it);
7951 int saw_smaller_pos = prev_pos < to_charpos;
6977 7952
6978 /* Don't produce glyphs in produce_glyphs. */ 7953 /* Don't produce glyphs in produce_glyphs. */
6979 saved_glyph_row = it->glyph_row; 7954 saved_glyph_row = it->glyph_row;
@@ -6988,11 +7963,31 @@ move_it_in_display_line_to (struct it *it,
6988 atpos_it.sp = -1; 7963 atpos_it.sp = -1;
6989 atx_it.sp = -1; 7964 atx_it.sp = -1;
6990 7965
7966 /* Use ppos_it under bidi reordering to save a copy of IT for the
7967 position > CHARPOS that is the closest to CHARPOS. We restore
7968 that position in IT when we have scanned the entire display line
7969 without finding a match for CHARPOS and all the character
7970 positions are greater than CHARPOS. */
7971 if (it->bidi_p)
7972 {
7973 SAVE_IT (ppos_it, *it, ppos_data);
7974 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
7975 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
7976 SAVE_IT (ppos_it, *it, ppos_data);
7977 }
7978
6991#define BUFFER_POS_REACHED_P() \ 7979#define BUFFER_POS_REACHED_P() \
6992 ((op & MOVE_TO_POS) != 0 \ 7980 ((op & MOVE_TO_POS) != 0 \
6993 && BUFFERP (it->object) \ 7981 && BUFFERP (it->object) \
6994 && (IT_CHARPOS (*it) == to_charpos \ 7982 && (IT_CHARPOS (*it) == to_charpos \
6995 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \ 7983 || ((!it->bidi_p \
7984 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
7985 && IT_CHARPOS (*it) > to_charpos) \
7986 || (it->what == IT_COMPOSITION \
7987 && ((IT_CHARPOS (*it) > to_charpos \
7988 && to_charpos >= it->cmp_it.charpos) \
7989 || (IT_CHARPOS (*it) < to_charpos \
7990 && to_charpos <= it->cmp_it.charpos)))) \
6996 && (it->method == GET_FROM_BUFFER \ 7991 && (it->method == GET_FROM_BUFFER \
6997 || (it->method == GET_FROM_DISPLAY_VECTOR \ 7992 || (it->method == GET_FROM_DISPLAY_VECTOR \
6998 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend))) 7993 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
@@ -7014,15 +8009,22 @@ move_it_in_display_line_to (struct it *it,
7014 ((IT)->current_x = x, (IT)->max_ascent = ascent, \ 8009 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7015 (IT)->max_descent = descent) 8010 (IT)->max_descent = descent)
7016 8011
7017 /* Stop if we move beyond TO_CHARPOS (after an image or stretch 8012 /* Stop if we move beyond TO_CHARPOS (after an image or a
7018 glyph). */ 8013 display string or stretch glyph). */
7019 if ((op & MOVE_TO_POS) != 0 8014 if ((op & MOVE_TO_POS) != 0
7020 && BUFFERP (it->object) 8015 && BUFFERP (it->object)
7021 && it->method == GET_FROM_BUFFER 8016 && it->method == GET_FROM_BUFFER
7022 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos) 8017 && (((!it->bidi_p
8018 /* When the iterator is at base embedding level, we
8019 are guaranteed that characters are delivered for
8020 display in strictly increasing order of their
8021 buffer positions. */
8022 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8023 && IT_CHARPOS (*it) > to_charpos)
7023 || (it->bidi_p 8024 || (it->bidi_p
7024 && (prev_method == GET_FROM_IMAGE 8025 && (prev_method == GET_FROM_IMAGE
7025 || prev_method == GET_FROM_STRETCH) 8026 || prev_method == GET_FROM_STRETCH
8027 || prev_method == GET_FROM_STRING)
7026 /* Passed TO_CHARPOS from left to right. */ 8028 /* Passed TO_CHARPOS from left to right. */
7027 && ((prev_pos < to_charpos 8029 && ((prev_pos < to_charpos
7028 && IT_CHARPOS (*it) > to_charpos) 8030 && IT_CHARPOS (*it) > to_charpos)
@@ -7039,12 +8041,9 @@ move_it_in_display_line_to (struct it *it,
7039 /* If wrap_it is valid, the current position might be in a 8041 /* If wrap_it is valid, the current position might be in a
7040 word that is wrapped. So, save the iterator in 8042 word that is wrapped. So, save the iterator in
7041 atpos_it and continue to see if wrapping happens. */ 8043 atpos_it and continue to see if wrapping happens. */
7042 atpos_it = *it; 8044 SAVE_IT (atpos_it, *it, atpos_data);
7043 } 8045 }
7044 8046
7045 prev_method = it->method;
7046 if (it->method == GET_FROM_BUFFER)
7047 prev_pos = IT_CHARPOS (*it);
7048 /* Stop when ZV reached. 8047 /* Stop when ZV reached.
7049 We used to stop here when TO_CHARPOS reached as well, but that is 8048 We used to stop here when TO_CHARPOS reached as well, but that is
7050 too soon if this glyph does not fit on this line. So we handle it 8049 too soon if this glyph does not fit on this line. So we handle it
@@ -7076,18 +8075,18 @@ move_it_in_display_line_to (struct it *it,
7076 already found, we are done. */ 8075 already found, we are done. */
7077 if (atpos_it.sp >= 0) 8076 if (atpos_it.sp >= 0)
7078 { 8077 {
7079 *it = atpos_it; 8078 RESTORE_IT (it, &atpos_it, atpos_data);
7080 result = MOVE_POS_MATCH_OR_ZV; 8079 result = MOVE_POS_MATCH_OR_ZV;
7081 goto done; 8080 goto done;
7082 } 8081 }
7083 if (atx_it.sp >= 0) 8082 if (atx_it.sp >= 0)
7084 { 8083 {
7085 *it = atx_it; 8084 RESTORE_IT (it, &atx_it, atx_data);
7086 result = MOVE_X_REACHED; 8085 result = MOVE_X_REACHED;
7087 goto done; 8086 goto done;
7088 } 8087 }
7089 /* Otherwise, we can wrap here. */ 8088 /* Otherwise, we can wrap here. */
7090 wrap_it = *it; 8089 SAVE_IT (wrap_it, *it, wrap_data);
7091 may_wrap = 0; 8090 may_wrap = 0;
7092 } 8091 }
7093 } 8092 }
@@ -7108,10 +8107,18 @@ move_it_in_display_line_to (struct it *it,
7108 8107
7109 if (it->area != TEXT_AREA) 8108 if (it->area != TEXT_AREA)
7110 { 8109 {
8110 prev_method = it->method;
8111 if (it->method == GET_FROM_BUFFER)
8112 prev_pos = IT_CHARPOS (*it);
7111 set_iterator_to_next (it, 1); 8113 set_iterator_to_next (it, 1);
7112 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos)) 8114 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7113 SET_TEXT_POS (this_line_min_pos, 8115 SET_TEXT_POS (this_line_min_pos,
7114 IT_CHARPOS (*it), IT_BYTEPOS (*it)); 8116 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8117 if (it->bidi_p
8118 && (op & MOVE_TO_POS)
8119 && IT_CHARPOS (*it) > to_charpos
8120 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8121 SAVE_IT (ppos_it, *it, ppos_data);
7115 continue; 8122 continue;
7116 } 8123 }
7117 8124
@@ -7155,7 +8162,7 @@ move_it_in_display_line_to (struct it *it,
7155 goto buffer_pos_reached; 8162 goto buffer_pos_reached;
7156 if (atpos_it.sp < 0) 8163 if (atpos_it.sp < 0)
7157 { 8164 {
7158 atpos_it = *it; 8165 SAVE_IT (atpos_it, *it, atpos_data);
7159 IT_RESET_X_ASCENT_DESCENT (&atpos_it); 8166 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7160 } 8167 }
7161 } 8168 }
@@ -7169,7 +8176,7 @@ move_it_in_display_line_to (struct it *it,
7169 } 8176 }
7170 if (atx_it.sp < 0) 8177 if (atx_it.sp < 0)
7171 { 8178 {
7172 atx_it = *it; 8179 SAVE_IT (atx_it, *it, atx_data);
7173 IT_RESET_X_ASCENT_DESCENT (&atx_it); 8180 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7174 } 8181 }
7175 } 8182 }
@@ -7213,12 +8220,15 @@ move_it_in_display_line_to (struct it *it,
7213 if (it->line_wrap == WORD_WRAP 8220 if (it->line_wrap == WORD_WRAP
7214 && atpos_it.sp < 0) 8221 && atpos_it.sp < 0)
7215 { 8222 {
7216 atpos_it = *it; 8223 SAVE_IT (atpos_it, *it, atpos_data);
7217 atpos_it.current_x = x_before_this_char; 8224 atpos_it.current_x = x_before_this_char;
7218 atpos_it.hpos = hpos_before_this_char; 8225 atpos_it.hpos = hpos_before_this_char;
7219 } 8226 }
7220 } 8227 }
7221 8228
8229 prev_method = it->method;
8230 if (it->method == GET_FROM_BUFFER)
8231 prev_pos = IT_CHARPOS (*it);
7222 set_iterator_to_next (it, 1); 8232 set_iterator_to_next (it, 1);
7223 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos)) 8233 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7224 SET_TEXT_POS (this_line_min_pos, 8234 SET_TEXT_POS (this_line_min_pos,
@@ -7258,7 +8268,7 @@ move_it_in_display_line_to (struct it *it,
7258 8268
7259 if (wrap_it.sp >= 0) 8269 if (wrap_it.sp >= 0)
7260 { 8270 {
7261 *it = wrap_it; 8271 RESTORE_IT (it, &wrap_it, wrap_data);
7262 atpos_it.sp = -1; 8272 atpos_it.sp = -1;
7263 atx_it.sp = -1; 8273 atx_it.sp = -1;
7264 } 8274 }
@@ -7275,7 +8285,7 @@ move_it_in_display_line_to (struct it *it,
7275 goto buffer_pos_reached; 8285 goto buffer_pos_reached;
7276 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0) 8286 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7277 { 8287 {
7278 atpos_it = *it; 8288 SAVE_IT (atpos_it, *it, atpos_data);
7279 IT_RESET_X_ASCENT_DESCENT (&atpos_it); 8289 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7280 } 8290 }
7281 } 8291 }
@@ -7312,10 +8322,34 @@ move_it_in_display_line_to (struct it *it,
7312 /* Is this a line end? If yes, we're done. */ 8322 /* Is this a line end? If yes, we're done. */
7313 if (ITERATOR_AT_END_OF_LINE_P (it)) 8323 if (ITERATOR_AT_END_OF_LINE_P (it))
7314 { 8324 {
7315 result = MOVE_NEWLINE_OR_CR; 8325 /* If we are past TO_CHARPOS, but never saw any character
8326 positions smaller than TO_CHARPOS, return
8327 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8328 did. */
8329 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8330 {
8331 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8332 {
8333 if (IT_CHARPOS (ppos_it) < ZV)
8334 {
8335 RESTORE_IT (it, &ppos_it, ppos_data);
8336 result = MOVE_POS_MATCH_OR_ZV;
8337 }
8338 else
8339 goto buffer_pos_reached;
8340 }
8341 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8342 && IT_CHARPOS (*it) > to_charpos)
8343 goto buffer_pos_reached;
8344 else
8345 result = MOVE_NEWLINE_OR_CR;
8346 }
8347 else
8348 result = MOVE_NEWLINE_OR_CR;
7316 break; 8349 break;
7317 } 8350 }
7318 8351
8352 prev_method = it->method;
7319 if (it->method == GET_FROM_BUFFER) 8353 if (it->method == GET_FROM_BUFFER)
7320 prev_pos = IT_CHARPOS (*it); 8354 prev_pos = IT_CHARPOS (*it);
7321 /* The current display element has been consumed. Advance 8355 /* The current display element has been consumed. Advance
@@ -7323,6 +8357,13 @@ move_it_in_display_line_to (struct it *it,
7323 set_iterator_to_next (it, 1); 8357 set_iterator_to_next (it, 1);
7324 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos)) 8358 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7325 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it)); 8359 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8360 if (IT_CHARPOS (*it) < to_charpos)
8361 saw_smaller_pos = 1;
8362 if (it->bidi_p
8363 && (op & MOVE_TO_POS)
8364 && IT_CHARPOS (*it) >= to_charpos
8365 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8366 SAVE_IT (ppos_it, *it, ppos_data);
7326 8367
7327 /* Stop if lines are truncated and IT's current x-position is 8368 /* Stop if lines are truncated and IT's current x-position is
7328 past the right edge of the window now. */ 8369 past the right edge of the window now. */
@@ -7332,9 +8373,21 @@ move_it_in_display_line_to (struct it *it,
7332 if (!FRAME_WINDOW_P (it->f) 8373 if (!FRAME_WINDOW_P (it->f)
7333 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)) 8374 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7334 { 8375 {
7335 if (!get_next_display_element (it) 8376 int at_eob_p = 0;
7336 || BUFFER_POS_REACHED_P ()) 8377
8378 if ((at_eob_p = !get_next_display_element (it))
8379 || BUFFER_POS_REACHED_P ()
8380 /* If we are past TO_CHARPOS, but never saw any
8381 character positions smaller than TO_CHARPOS,
8382 return MOVE_POS_MATCH_OR_ZV, like the
8383 unidirectional display did. */
8384 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8385 && !saw_smaller_pos
8386 && IT_CHARPOS (*it) > to_charpos))
7337 { 8387 {
8388 if (it->bidi_p
8389 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8390 RESTORE_IT (it, &ppos_it, ppos_data);
7338 result = MOVE_POS_MATCH_OR_ZV; 8391 result = MOVE_POS_MATCH_OR_ZV;
7339 break; 8392 break;
7340 } 8393 }
@@ -7344,6 +8397,15 @@ move_it_in_display_line_to (struct it *it,
7344 break; 8397 break;
7345 } 8398 }
7346 } 8399 }
8400 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8401 && !saw_smaller_pos
8402 && IT_CHARPOS (*it) > to_charpos)
8403 {
8404 if (IT_CHARPOS (ppos_it) < ZV)
8405 RESTORE_IT (it, &ppos_it, ppos_data);
8406 result = MOVE_POS_MATCH_OR_ZV;
8407 break;
8408 }
7347 result = MOVE_LINE_TRUNCATED; 8409 result = MOVE_LINE_TRUNCATED;
7348 break; 8410 break;
7349 } 8411 }
@@ -7355,12 +8417,21 @@ move_it_in_display_line_to (struct it *it,
7355 /* If we scanned beyond to_pos and didn't find a point to wrap at, 8417 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7356 restore the saved iterator. */ 8418 restore the saved iterator. */
7357 if (atpos_it.sp >= 0) 8419 if (atpos_it.sp >= 0)
7358 *it = atpos_it; 8420 RESTORE_IT (it, &atpos_it, atpos_data);
7359 else if (atx_it.sp >= 0) 8421 else if (atx_it.sp >= 0)
7360 *it = atx_it; 8422 RESTORE_IT (it, &atx_it, atx_data);
7361 8423
7362 done: 8424 done:
7363 8425
8426 if (atpos_data)
8427 bidi_unshelve_cache (atpos_data, 1);
8428 if (atx_data)
8429 bidi_unshelve_cache (atx_data, 1);
8430 if (wrap_data)
8431 bidi_unshelve_cache (wrap_data, 1);
8432 if (ppos_data)
8433 bidi_unshelve_cache (ppos_data, 1);
8434
7364 /* Restore the iterator settings altered at the beginning of this 8435 /* Restore the iterator settings altered at the beginning of this
7365 function. */ 8436 function. */
7366 it->glyph_row = saved_glyph_row; 8437 it->glyph_row = saved_glyph_row;
@@ -7376,8 +8447,12 @@ move_it_in_display_line (struct it *it,
7376 if (it->line_wrap == WORD_WRAP 8447 if (it->line_wrap == WORD_WRAP
7377 && (op & MOVE_TO_X)) 8448 && (op & MOVE_TO_X))
7378 { 8449 {
7379 struct it save_it = *it; 8450 struct it save_it;
7380 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op); 8451 void *save_data = NULL;
8452 int skip;
8453
8454 SAVE_IT (save_it, *it, save_data);
8455 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7381 /* When word-wrap is on, TO_X may lie past the end 8456 /* When word-wrap is on, TO_X may lie past the end
7382 of a wrapped line. Then it->current is the 8457 of a wrapped line. Then it->current is the
7383 character on the next line, so backtrack to the 8458 character on the next line, so backtrack to the
@@ -7385,10 +8460,12 @@ move_it_in_display_line (struct it *it,
7385 if (skip == MOVE_LINE_CONTINUED) 8460 if (skip == MOVE_LINE_CONTINUED)
7386 { 8461 {
7387 int prev_x = max (it->current_x - 1, 0); 8462 int prev_x = max (it->current_x - 1, 0);
7388 *it = save_it; 8463 RESTORE_IT (it, &save_it, save_data);
7389 move_it_in_display_line_to 8464 move_it_in_display_line_to
7390 (it, -1, prev_x, MOVE_TO_X); 8465 (it, -1, prev_x, MOVE_TO_X);
7391 } 8466 }
8467 else
8468 bidi_unshelve_cache (save_data, 1);
7392 } 8469 }
7393 else 8470 else
7394 move_it_in_display_line_to (it, to_charpos, to_x, op); 8471 move_it_in_display_line_to (it, to_charpos, to_x, op);
@@ -7403,14 +8480,15 @@ move_it_in_display_line (struct it *it,
7403 description of enum move_operation_enum. 8480 description of enum move_operation_enum.
7404 8481
7405 If TO_CHARPOS is in invisible text, e.g. a truncated part of a 8482 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7406 screen line, this function will set IT to the next position > 8483 screen line, this function will set IT to the next position that is
7407 TO_CHARPOS. */ 8484 displayed to the right of TO_CHARPOS on the screen. */
7408 8485
7409void 8486void
7410move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op) 8487move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7411{ 8488{
7412 enum move_it_result skip, skip2 = MOVE_X_REACHED; 8489 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7413 int line_height, line_start_x = 0, reached = 0; 8490 int line_height, line_start_x = 0, reached = 0;
8491 void *backup_data = NULL;
7414 8492
7415 for (;;) 8493 for (;;)
7416 { 8494 {
@@ -7463,7 +8541,7 @@ move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos
7463 struct it it_backup; 8541 struct it it_backup;
7464 8542
7465 if (it->line_wrap == WORD_WRAP) 8543 if (it->line_wrap == WORD_WRAP)
7466 it_backup = *it; 8544 SAVE_IT (it_backup, *it, backup_data);
7467 8545
7468 /* TO_Y specified means stop at TO_X in the line containing 8546 /* TO_Y specified means stop at TO_X in the line containing
7469 TO_Y---or at TO_CHARPOS if this is reached first. The 8547 TO_Y---or at TO_CHARPOS if this is reached first. The
@@ -7497,7 +8575,7 @@ move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos
7497 reached = 6; 8575 reached = 6;
7498 break; 8576 break;
7499 } 8577 }
7500 it_backup = *it; 8578 SAVE_IT (it_backup, *it, backup_data);
7501 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it))); 8579 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7502 skip2 = move_it_in_display_line_to (it, to_charpos, -1, 8580 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7503 op & MOVE_TO_POS); 8581 op & MOVE_TO_POS);
@@ -7511,7 +8589,7 @@ move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos
7511 /* If TO_Y is in this line and TO_X was reached 8589 /* If TO_Y is in this line and TO_X was reached
7512 above, we scanned too far. We have to restore 8590 above, we scanned too far. We have to restore
7513 IT's settings to the ones before skipping. */ 8591 IT's settings to the ones before skipping. */
7514 *it = it_backup; 8592 RESTORE_IT (it, &it_backup, backup_data);
7515 reached = 6; 8593 reached = 6;
7516 } 8594 }
7517 else 8595 else
@@ -7538,7 +8616,7 @@ move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos
7538 && it->line_wrap == WORD_WRAP) 8616 && it->line_wrap == WORD_WRAP)
7539 { 8617 {
7540 int prev_x = max (it->current_x - 1, 0); 8618 int prev_x = max (it->current_x - 1, 0);
7541 *it = it_backup; 8619 RESTORE_IT (it, &it_backup, backup_data);
7542 skip = move_it_in_display_line_to 8620 skip = move_it_in_display_line_to
7543 (it, -1, prev_x, MOVE_TO_X); 8621 (it, -1, prev_x, MOVE_TO_X);
7544 } 8622 }
@@ -7552,7 +8630,14 @@ move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos
7552 else if (BUFFERP (it->object) 8630 else if (BUFFERP (it->object)
7553 && (it->method == GET_FROM_BUFFER 8631 && (it->method == GET_FROM_BUFFER
7554 || it->method == GET_FROM_STRETCH) 8632 || it->method == GET_FROM_STRETCH)
7555 && IT_CHARPOS (*it) >= to_charpos) 8633 && IT_CHARPOS (*it) >= to_charpos
8634 /* Under bidi iteration, a call to set_iterator_to_next
8635 can scan far beyond to_charpos if the initial
8636 portion of the next line needs to be reordered. In
8637 that case, give move_it_in_display_line_to another
8638 chance below. */
8639 && !(it->bidi_p
8640 && it->bidi_it.scan_dir == -1))
7556 skip = MOVE_POS_MATCH_OR_ZV; 8641 skip = MOVE_POS_MATCH_OR_ZV;
7557 else 8642 else
7558 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS); 8643 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
@@ -7645,6 +8730,9 @@ move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos
7645 last_max_ascent = it->max_ascent; 8730 last_max_ascent = it->max_ascent;
7646 } 8731 }
7647 8732
8733 if (backup_data)
8734 bidi_unshelve_cache (backup_data, 1);
8735
7648 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached)); 8736 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7649} 8737}
7650 8738
@@ -7662,6 +8750,7 @@ move_it_vertically_backward (struct it *it, int dy)
7662{ 8750{
7663 int nlines, h; 8751 int nlines, h;
7664 struct it it2, it3; 8752 struct it it2, it3;
8753 void *it2data = NULL, *it3data = NULL;
7665 EMACS_INT start_pos; 8754 EMACS_INT start_pos;
7666 8755
7667 move_further_back: 8756 move_further_back:
@@ -7683,23 +8772,33 @@ move_it_vertically_backward (struct it *it, int dy)
7683 reseat_1 (it, it->current.pos, 1); 8772 reseat_1 (it, it->current.pos, 1);
7684 8773
7685 /* We are now surely at a line start. */ 8774 /* We are now surely at a line start. */
7686 it->current_x = it->hpos = 0; 8775 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8776 reordering is in effect. */
7687 it->continuation_lines_width = 0; 8777 it->continuation_lines_width = 0;
7688 8778
7689 /* Move forward and see what y-distance we moved. First move to the 8779 /* Move forward and see what y-distance we moved. First move to the
7690 start of the next line so that we get its height. We need this 8780 start of the next line so that we get its height. We need this
7691 height to be able to tell whether we reached the specified 8781 height to be able to tell whether we reached the specified
7692 y-distance. */ 8782 y-distance. */
7693 it2 = *it; 8783 SAVE_IT (it2, *it, it2data);
7694 it2.max_ascent = it2.max_descent = 0; 8784 it2.max_ascent = it2.max_descent = 0;
7695 do 8785 do
7696 { 8786 {
7697 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1, 8787 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7698 MOVE_TO_POS | MOVE_TO_VPOS); 8788 MOVE_TO_POS | MOVE_TO_VPOS);
7699 } 8789 }
7700 while (!IT_POS_VALID_AFTER_MOVE_P (&it2)); 8790 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8791 /* If we are in a display string which starts at START_POS,
8792 and that display string includes a newline, and we are
8793 right after that newline (i.e. at the beginning of a
8794 display line), exit the loop, because otherwise we will
8795 infloop, since move_it_to will see that it is already at
8796 START_POS and will not move. */
8797 || (it2.method == GET_FROM_STRING
8798 && IT_CHARPOS (it2) == start_pos
8799 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
7701 xassert (IT_CHARPOS (*it) >= BEGV); 8800 xassert (IT_CHARPOS (*it) >= BEGV);
7702 it3 = it2; 8801 SAVE_IT (it3, it2, it3data);
7703 8802
7704 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS); 8803 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7705 xassert (IT_CHARPOS (*it) >= BEGV); 8804 xassert (IT_CHARPOS (*it) >= BEGV);
@@ -7717,9 +8816,29 @@ move_it_vertically_backward (struct it *it, int dy)
7717 if (dy == 0) 8816 if (dy == 0)
7718 { 8817 {
7719 /* DY == 0 means move to the start of the screen line. The 8818 /* DY == 0 means move to the start of the screen line. The
7720 value of nlines is > 0 if continuation lines were involved. */ 8819 value of nlines is > 0 if continuation lines were involved,
8820 or if the original IT position was at start of a line. */
8821 RESTORE_IT (it, it, it2data);
7721 if (nlines > 0) 8822 if (nlines > 0)
7722 move_it_by_lines (it, nlines); 8823 move_it_by_lines (it, nlines);
8824 /* The above code moves us to some position NLINES down,
8825 usually to its first glyph (leftmost in an L2R line), but
8826 that's not necessarily the start of the line, under bidi
8827 reordering. We want to get to the character position
8828 that is immediately after the newline of the previous
8829 line. */
8830 if (it->bidi_p
8831 && !it->continuation_lines_width
8832 && !STRINGP (it->string)
8833 && IT_CHARPOS (*it) > BEGV
8834 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8835 {
8836 EMACS_INT nl_pos =
8837 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8838
8839 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8840 }
8841 bidi_unshelve_cache (it3data, 1);
7723 } 8842 }
7724 else 8843 else
7725 { 8844 {
@@ -7727,9 +8846,13 @@ move_it_vertically_backward (struct it *it, int dy)
7727 Note that H has been subtracted in front of the if-statement. */ 8846 Note that H has been subtracted in front of the if-statement. */
7728 int target_y = it->current_y + h - dy; 8847 int target_y = it->current_y + h - dy;
7729 int y0 = it3.current_y; 8848 int y0 = it3.current_y;
7730 int y1 = line_bottom_y (&it3); 8849 int y1;
7731 int line_height = y1 - y0; 8850 int line_height;
7732 8851
8852 RESTORE_IT (&it3, &it3, it3data);
8853 y1 = line_bottom_y (&it3);
8854 line_height = y1 - y0;
8855 RESTORE_IT (it, it, it2data);
7733 /* If we did not reach target_y, try to move further backward if 8856 /* If we did not reach target_y, try to move further backward if
7734 we can. If we moved too far backward, try to move forward. */ 8857 we can. If we moved too far backward, try to move forward. */
7735 if (target_y < it->current_y 8858 if (target_y < it->current_y
@@ -7856,6 +8979,7 @@ move_it_by_lines (struct it *it, int dvpos)
7856 else 8979 else
7857 { 8980 {
7858 struct it it2; 8981 struct it it2;
8982 void *it2data = NULL;
7859 EMACS_INT start_charpos, i; 8983 EMACS_INT start_charpos, i;
7860 8984
7861 /* Start at the beginning of the screen line containing IT's 8985 /* Start at the beginning of the screen line containing IT's
@@ -7891,7 +9015,7 @@ move_it_by_lines (struct it *it, int dvpos)
7891 9015
7892 /* Above call may have moved too far if continuation lines 9016 /* Above call may have moved too far if continuation lines
7893 are involved. Scan forward and see if it did. */ 9017 are involved. Scan forward and see if it did. */
7894 it2 = *it; 9018 SAVE_IT (it2, *it, it2data);
7895 it2.vpos = it2.current_y = 0; 9019 it2.vpos = it2.current_y = 0;
7896 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS); 9020 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7897 it->vpos -= it2.vpos; 9021 it->vpos -= it2.vpos;
@@ -7902,12 +9026,18 @@ move_it_by_lines (struct it *it, int dvpos)
7902 if (it2.vpos > -dvpos) 9026 if (it2.vpos > -dvpos)
7903 { 9027 {
7904 int delta = it2.vpos + dvpos; 9028 int delta = it2.vpos + dvpos;
7905 it2 = *it; 9029
9030 RESTORE_IT (&it2, &it2, it2data);
9031 SAVE_IT (it2, *it, it2data);
7906 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS); 9032 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7907 /* Move back again if we got too far ahead. */ 9033 /* Move back again if we got too far ahead. */
7908 if (IT_CHARPOS (*it) >= start_charpos) 9034 if (IT_CHARPOS (*it) >= start_charpos)
7909 *it = it2; 9035 RESTORE_IT (it, &it2, it2data);
9036 else
9037 bidi_unshelve_cache (it2data, 1);
7910 } 9038 }
9039 else
9040 RESTORE_IT (it, it, it2data);
7911 } 9041 }
7912} 9042}
7913 9043
@@ -8068,7 +9198,7 @@ message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8068 if (nlflag) 9198 if (nlflag)
8069 { 9199 {
8070 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte; 9200 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8071 intmax_t dups; 9201 printmax_t dups;
8072 insert_1 ("\n", 1, 1, 0, 0); 9202 insert_1 ("\n", 1, 1, 0, 0);
8073 9203
8074 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0); 9204 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
@@ -8092,12 +9222,12 @@ message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8092 if (dups > 1) 9222 if (dups > 1)
8093 { 9223 {
8094 char dupstr[sizeof " [ times]" 9224 char dupstr[sizeof " [ times]"
8095 + INT_STRLEN_BOUND (intmax_t)]; 9225 + INT_STRLEN_BOUND (printmax_t)];
8096 int duplen; 9226 int duplen;
8097 9227
8098 /* If you change this format, don't forget to also 9228 /* If you change this format, don't forget to also
8099 change message_log_check_duplicate. */ 9229 change message_log_check_duplicate. */
8100 sprintf (dupstr, " [%"PRIdMAX" times]", dups); 9230 sprintf (dupstr, " [%"pMd" times]", dups);
8101 duplen = strlen (dupstr); 9231 duplen = strlen (dupstr);
8102 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1); 9232 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8103 insert_1 (dupstr, duplen, 1, 0, 1); 9233 insert_1 (dupstr, duplen, 1, 0, 1);
@@ -8490,7 +9620,7 @@ vmessage (const char *m, va_list ap)
8490 { 9620 {
8491 if (m) 9621 if (m)
8492 { 9622 {
8493 size_t len; 9623 ptrdiff_t len;
8494 9624
8495 len = doprnt (FRAME_MESSAGE_BUF (f), 9625 len = doprnt (FRAME_MESSAGE_BUF (f),
8496 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap); 9626 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
@@ -9127,7 +10257,7 @@ current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9127} 10257}
9128 10258
9129 10259
9130/* Push the current message on Vmessage_stack for later restauration 10260/* Push the current message on Vmessage_stack for later restoration
9131 by restore_message. Value is non-zero if the current message isn't 10261 by restore_message. Value is non-zero if the current message isn't
9132 empty. This is a relatively infrequent operation, so it's not 10262 empty. This is a relatively infrequent operation, so it's not
9133 worth optimizing. */ 10263 worth optimizing. */
@@ -9612,13 +10742,14 @@ static void
9612store_mode_line_noprop_char (char c) 10742store_mode_line_noprop_char (char c)
9613{ 10743{
9614 /* If output position has reached the end of the allocated buffer, 10744 /* If output position has reached the end of the allocated buffer,
9615 double the buffer's size. */ 10745 increase the buffer's size. */
9616 if (mode_line_noprop_ptr == mode_line_noprop_buf_end) 10746 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9617 { 10747 {
9618 int len = MODE_LINE_NOPROP_LEN (0); 10748 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
9619 int new_size = 2 * len * sizeof *mode_line_noprop_buf; 10749 ptrdiff_t size = len;
9620 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size); 10750 mode_line_noprop_buf =
9621 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size; 10751 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10752 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
9622 mode_line_noprop_ptr = mode_line_noprop_buf + len; 10753 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9623 } 10754 }
9624 10755
@@ -9680,9 +10811,9 @@ x_consider_frame_title (Lisp_Object frame)
9680 /* Do we have more than one visible frame on this X display? */ 10811 /* Do we have more than one visible frame on this X display? */
9681 Lisp_Object tail; 10812 Lisp_Object tail;
9682 Lisp_Object fmt; 10813 Lisp_Object fmt;
9683 int title_start; 10814 ptrdiff_t title_start;
9684 char *title; 10815 char *title;
9685 int len; 10816 ptrdiff_t len;
9686 struct it it; 10817 struct it it;
9687 int count = SPECPDL_INDEX (); 10818 int count = SPECPDL_INDEX ();
9688 10819
@@ -10394,7 +11525,7 @@ display_tool_bar_line (struct it *it, int height)
10394 ++i; 11525 ++i;
10395 } 11526 }
10396 11527
10397 /* Stop at line ends. */ 11528 /* Stop at line end. */
10398 if (ITERATOR_AT_END_OF_LINE_P (it)) 11529 if (ITERATOR_AT_END_OF_LINE_P (it))
10399 break; 11530 break;
10400 11531
@@ -10477,6 +11608,7 @@ tool_bar_lines_needed (struct frame *f, int *n_rows)
10477 it.first_visible_x = 0; 11608 it.first_visible_x = 0;
10478 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f); 11609 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10479 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1); 11610 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11611 it.paragraph_embedding = L2R;
10480 11612
10481 while (!ITERATOR_AT_END_P (&it)) 11613 while (!ITERATOR_AT_END_P (&it))
10482 { 11614 {
@@ -10510,7 +11642,7 @@ DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10510 f = XFRAME (frame); 11642 f = XFRAME (frame);
10511 11643
10512 if (WINDOWP (f->tool_bar_window) 11644 if (WINDOWP (f->tool_bar_window)
10513 || (w = XWINDOW (f->tool_bar_window), 11645 && (w = XWINDOW (f->tool_bar_window),
10514 WINDOW_TOTAL_LINES (w) > 0)) 11646 WINDOW_TOTAL_LINES (w) > 0))
10515 { 11647 {
10516 update_tool_bar (f, 1); 11648 update_tool_bar (f, 1);
@@ -10559,6 +11691,14 @@ redisplay_tool_bar (struct frame *f)
10559 /* Build a string that represents the contents of the tool-bar. */ 11691 /* Build a string that represents the contents of the tool-bar. */
10560 build_desired_tool_bar_string (f); 11692 build_desired_tool_bar_string (f);
10561 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1); 11693 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11694 /* FIXME: This should be controlled by a user option. But it
11695 doesn't make sense to have an R2L tool bar if the menu bar cannot
11696 be drawn also R2L, and making the menu bar R2L is tricky due
11697 toolkit-specific code that implements it. If an R2L tool bar is
11698 ever supported, display_tool_bar_line should also be augmented to
11699 call unproduce_glyphs like display_line and display_string
11700 do. */
11701 it.paragraph_embedding = L2R;
10562 11702
10563 if (f->n_tool_bar_rows == 0) 11703 if (f->n_tool_bar_rows == 0)
10564 { 11704 {
@@ -10986,6 +12126,7 @@ hscroll_window_tree (Lisp_Object window)
10986 = (desired_cursor_row->enabled_p 12126 = (desired_cursor_row->enabled_p
10987 ? desired_cursor_row 12127 ? desired_cursor_row
10988 : current_cursor_row); 12128 : current_cursor_row);
12129 int row_r2l_p = cursor_row->reversed_p;
10989 12130
10990 text_area_width = window_box_width (w, TEXT_AREA); 12131 text_area_width = window_box_width (w, TEXT_AREA);
10991 12132
@@ -10993,11 +12134,31 @@ hscroll_window_tree (Lisp_Object window)
10993 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w); 12134 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10994 12135
10995 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer)) 12136 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10996 && ((XFASTINT (w->hscroll) 12137 /* For left-to-right rows, hscroll when cursor is either
10997 && w->cursor.x <= h_margin) 12138 (i) inside the right hscroll margin, or (ii) if it is
10998 || (cursor_row->enabled_p 12139 inside the left margin and the window is already
10999 && cursor_row->truncated_on_right_p 12140 hscrolled. */
11000 && (w->cursor.x >= text_area_width - h_margin)))) 12141 && ((!row_r2l_p
12142 && ((XFASTINT (w->hscroll)
12143 && w->cursor.x <= h_margin)
12144 || (cursor_row->enabled_p
12145 && cursor_row->truncated_on_right_p
12146 && (w->cursor.x >= text_area_width - h_margin))))
12147 /* For right-to-left rows, the logic is similar,
12148 except that rules for scrolling to left and right
12149 are reversed. E.g., if cursor.x <= h_margin, we
12150 need to hscroll "to the right" unconditionally,
12151 and that will scroll the screen to the left so as
12152 to reveal the next portion of the row. */
12153 || (row_r2l_p
12154 && ((cursor_row->enabled_p
12155 /* FIXME: It is confusing to set the
12156 truncated_on_right_p flag when R2L rows
12157 are actually truncated on the left. */
12158 && cursor_row->truncated_on_right_p
12159 && w->cursor.x <= h_margin)
12160 || (XFASTINT (w->hscroll)
12161 && (w->cursor.x >= text_area_width - h_margin))))))
11001 { 12162 {
11002 struct it it; 12163 struct it it;
11003 int hscroll; 12164 int hscroll;
@@ -11032,7 +12193,9 @@ hscroll_window_tree (Lisp_Object window)
11032 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f)) 12193 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11033 : (text_area_width / 2)))) 12194 : (text_area_width / 2))))
11034 / FRAME_COLUMN_WIDTH (it.f); 12195 / FRAME_COLUMN_WIDTH (it.f);
11035 else if (w->cursor.x >= text_area_width - h_margin) 12196 else if ((!row_r2l_p
12197 && w->cursor.x >= text_area_width - h_margin)
12198 || (row_r2l_p && w->cursor.x <= h_margin))
11036 { 12199 {
11037 if (hscroll_relative_p) 12200 if (hscroll_relative_p)
11038 wanted_x = text_area_width * (1 - hscroll_step_rel) 12201 wanted_x = text_area_width * (1 - hscroll_step_rel)
@@ -11057,9 +12220,9 @@ hscroll_window_tree (Lisp_Object window)
11057 } 12220 }
11058 hscroll = max (hscroll, XFASTINT (w->min_hscroll)); 12221 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11059 12222
11060 /* Don't call Fset_window_hscroll if value hasn't 12223 /* Don't prevent redisplay optimizations if hscroll
11061 changed because it will prevent redisplay 12224 hasn't changed, as it will unnecessarily slow down
11062 optimizations. */ 12225 redisplay. */
11063 if (XFASTINT (w->hscroll) != hscroll) 12226 if (XFASTINT (w->hscroll) != hscroll)
11064 { 12227 {
11065 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1; 12228 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
@@ -12504,13 +13667,16 @@ set_cursor_from_row (struct window *w, struct glyph_row *row,
12504 /* Non-zero means we've seen at least one glyph that came from a 13667 /* Non-zero means we've seen at least one glyph that came from a
12505 display string. */ 13668 display string. */
12506 int string_seen = 0; 13669 int string_seen = 0;
12507 /* Largest and smalles buffer positions seen so far during scan of 13670 /* Largest and smallest buffer positions seen so far during scan of
12508 glyph row. */ 13671 glyph row. */
12509 EMACS_INT bpos_max = pos_before; 13672 EMACS_INT bpos_max = pos_before;
12510 EMACS_INT bpos_min = pos_after; 13673 EMACS_INT bpos_min = pos_after;
12511 /* Last buffer position covered by an overlay string with an integer 13674 /* Last buffer position covered by an overlay string with an integer
12512 `cursor' property. */ 13675 `cursor' property. */
12513 EMACS_INT bpos_covered = 0; 13676 EMACS_INT bpos_covered = 0;
13677 /* Non-zero means the display string on which to display the cursor
13678 comes from a text property, not from an overlay. */
13679 int string_from_text_prop = 0;
12514 13680
12515 /* Skip over glyphs not having an object at the start and the end of 13681 /* Skip over glyphs not having an object at the start and the end of
12516 the row. These are special glyphs like truncation marks on 13682 the row. These are special glyphs like truncation marks on
@@ -12769,19 +13935,7 @@ set_cursor_from_row (struct window *w, struct glyph_row *row,
12769 glyph--; 13935 glyph--;
12770 } 13936 }
12771 } 13937 }
12772 else if (match_with_avoid_cursor 13938 else if (match_with_avoid_cursor)
12773 /* A truncated row may not include PT among its
12774 character positions. Setting the cursor inside the
12775 scroll margin will trigger recalculation of hscroll
12776 in hscroll_window_tree. */
12777 || (row->truncated_on_left_p && pt_old < bpos_min)
12778 || (row->truncated_on_right_p && pt_old > bpos_max)
12779 /* Zero-width characters produce no glyphs. */
12780 || (!string_seen
12781 && !empty_line_p
12782 && (row->reversed_p
12783 ? glyph_after > glyphs_end
12784 : glyph_after < glyphs_end)))
12785 { 13939 {
12786 cursor = glyph_after; 13940 cursor = glyph_after;
12787 x = -1; 13941 x = -1;
@@ -12800,6 +13954,17 @@ set_cursor_from_row (struct window *w, struct glyph_row *row,
12800 13954
12801 x = -1; 13955 x = -1;
12802 13956
13957 /* If the row ends in a newline from a display string,
13958 reordering could have moved the glyphs belonging to the
13959 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
13960 in this case we extend the search to the last glyph in
13961 the row that was not inserted by redisplay. */
13962 if (row->ends_in_newline_from_string_p)
13963 {
13964 glyph_after = end;
13965 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13966 }
13967
12803 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that 13968 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
12804 correspond to POS_BEFORE and POS_AFTER, respectively. We 13969 correspond to POS_BEFORE and POS_AFTER, respectively. We
12805 need START and STOP in the order that corresponds to the 13970 need START and STOP in the order that corresponds to the
@@ -12829,9 +13994,14 @@ set_cursor_from_row (struct window *w, struct glyph_row *row,
12829 { 13994 {
12830 Lisp_Object str; 13995 Lisp_Object str;
12831 EMACS_INT tem; 13996 EMACS_INT tem;
13997 /* If the display property covers the newline, we
13998 need to search for it one position farther. */
13999 EMACS_INT lim = pos_after
14000 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
12832 14001
14002 string_from_text_prop = 0;
12833 str = glyph->object; 14003 str = glyph->object;
12834 tem = string_buffer_position_lim (str, pos, pos_after, 0); 14004 tem = string_buffer_position_lim (str, pos, lim, 0);
12835 if (tem == 0 /* from overlay */ 14005 if (tem == 0 /* from overlay */
12836 || pos <= tem) 14006 || pos <= tem)
12837 { 14007 {
@@ -12855,7 +14025,10 @@ set_cursor_from_row (struct window *w, struct glyph_row *row,
12855 EMACS_INT strpos = glyph->charpos; 14025 EMACS_INT strpos = glyph->charpos;
12856 14026
12857 if (tem) 14027 if (tem)
12858 cursor = glyph; 14028 {
14029 cursor = glyph;
14030 string_from_text_prop = 1;
14031 }
12859 for ( ; 14032 for ( ;
12860 (row->reversed_p ? glyph > stop : glyph < stop) 14033 (row->reversed_p ? glyph > stop : glyph < stop)
12861 && EQ (glyph->object, str); 14034 && EQ (glyph->object, str);
@@ -12903,6 +14076,26 @@ set_cursor_from_row (struct window *w, struct glyph_row *row,
12903 && row->continued_p) 14076 && row->continued_p)
12904 return 0; 14077 return 0;
12905 } 14078 }
14079 /* A truncated row may not include PT among its character positions.
14080 Setting the cursor inside the scroll margin will trigger
14081 recalculation of hscroll in hscroll_window_tree. But if a
14082 display string covers point, defer to the string-handling
14083 code below to figure this out. */
14084 else if (row->truncated_on_left_p && pt_old < bpos_min)
14085 {
14086 cursor = glyph_before;
14087 x = -1;
14088 }
14089 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14090 /* Zero-width characters produce no glyphs. */
14091 || (!empty_line_p
14092 && (row->reversed_p
14093 ? glyph_after > glyphs_end
14094 : glyph_after < glyphs_end)))
14095 {
14096 cursor = glyph_after;
14097 x = -1;
14098 }
12906 } 14099 }
12907 14100
12908 compute_x: 14101 compute_x:
@@ -12930,14 +14123,14 @@ set_cursor_from_row (struct window *w, struct glyph_row *row,
12930 w->cursor.vpos >= 0 14123 w->cursor.vpos >= 0
12931 /* that candidate is not the row we are processing */ 14124 /* that candidate is not the row we are processing */
12932 && MATRIX_ROW (matrix, w->cursor.vpos) != row 14125 && MATRIX_ROW (matrix, w->cursor.vpos) != row
12933 /* the row we are processing is part of a continued line */
12934 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
12935 /* Make sure cursor.vpos specifies a row whose start and end 14126 /* Make sure cursor.vpos specifies a row whose start and end
12936 charpos occlude point. This is because some callers of this 14127 charpos occlude point, and it is valid candidate for being a
12937 function leave cursor.vpos at the row where the cursor was 14128 cursor-row. This is because some callers of this function
12938 displayed during the last redisplay cycle. */ 14129 leave cursor.vpos at the row where the cursor was displayed
14130 during the last redisplay cycle. */
12939 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old 14131 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
12940 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))) 14132 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14133 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
12941 { 14134 {
12942 struct glyph *g1 = 14135 struct glyph *g1 =
12943 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos; 14136 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
@@ -12946,18 +14139,39 @@ set_cursor_from_row (struct window *w, struct glyph_row *row,
12946 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)) 14139 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
12947 return 0; 14140 return 0;
12948 /* Keep the candidate whose buffer position is the closest to 14141 /* Keep the candidate whose buffer position is the closest to
12949 point. */ 14142 point or has the `cursor' property. */
12950 if (/* previous candidate is a glyph in TEXT_AREA of that row */ 14143 if (/* previous candidate is a glyph in TEXT_AREA of that row */
12951 w->cursor.hpos >= 0 14144 w->cursor.hpos >= 0
12952 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos) 14145 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
12953 && BUFFERP (g1->object) 14146 && ((BUFFERP (g1->object)
12954 && (g1->charpos == pt_old /* an exact match always wins */ 14147 && (g1->charpos == pt_old /* an exact match always wins */
12955 || (BUFFERP (glyph->object) 14148 || (BUFFERP (glyph->object)
12956 && eabs (g1->charpos - pt_old) 14149 && eabs (g1->charpos - pt_old)
12957 < eabs (glyph->charpos - pt_old)))) 14150 < eabs (glyph->charpos - pt_old))))
14151 /* previous candidate is a glyph from a string that has
14152 a non-nil `cursor' property */
14153 || (STRINGP (g1->object)
14154 && (!NILP (Fget_char_property (make_number (g1->charpos),
14155 Qcursor, g1->object))
14156 /* previous candidate is from the same display
14157 string as this one, and the display string
14158 came from a text property */
14159 || (EQ (g1->object, glyph->object)
14160 && string_from_text_prop)
14161 /* this candidate is from newline and its
14162 position is not an exact match */
14163 || (INTEGERP (glyph->object)
14164 && glyph->charpos != pt_old)))))
12958 return 0; 14165 return 0;
12959 /* If this candidate gives an exact match, use that. */ 14166 /* If this candidate gives an exact match, use that. */
12960 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old) 14167 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14168 /* If this candidate is a glyph created for the
14169 terminating newline of a line, and point is on that
14170 newline, it wins because it's an exact match. */
14171 || (!row->continued_p
14172 && INTEGERP (glyph->object)
14173 && glyph->charpos == 0
14174 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
12961 /* Otherwise, keep the candidate that comes from a row 14175 /* Otherwise, keep the candidate that comes from a row
12962 spanning less buffer positions. This may win when one or 14176 spanning less buffer positions. This may win when one or
12963 both candidate positions are on glyphs that came from 14177 both candidate positions are on glyphs that came from
@@ -13169,7 +14383,7 @@ try_scrolling (Lisp_Object window, int just_this_one_p,
13169 { 14383 {
13170 int scroll_margin_y; 14384 int scroll_margin_y;
13171 14385
13172 /* Compute the pixel ypos of the scroll margin, then move it to 14386 /* Compute the pixel ypos of the scroll margin, then move IT to
13173 either that ypos or PT, whichever comes first. */ 14387 either that ypos or PT, whichever comes first. */
13174 start_display (&it, w, startp); 14388 start_display (&it, w, startp);
13175 scroll_margin_y = it.last_visible_y - this_scroll_margin 14389 scroll_margin_y = it.last_visible_y - this_scroll_margin
@@ -13199,7 +14413,8 @@ try_scrolling (Lisp_Object window, int just_this_one_p,
13199 if (dy > scroll_max) 14413 if (dy > scroll_max)
13200 return SCROLLING_FAILED; 14414 return SCROLLING_FAILED;
13201 14415
13202 scroll_down_p = 1; 14416 if (dy > 0)
14417 scroll_down_p = 1;
13203 } 14418 }
13204 } 14419 }
13205 14420
@@ -13246,14 +14461,18 @@ try_scrolling (Lisp_Object window, int just_this_one_p,
13246 which was computed as distance from window bottom to 14461 which was computed as distance from window bottom to
13247 point. This matters when lines at window top and lines 14462 point. This matters when lines at window top and lines
13248 below window bottom have different height. */ 14463 below window bottom have different height. */
13249 struct it it1 = it; 14464 struct it it1;
14465 void *it1data = NULL;
13250 /* We use a temporary it1 because line_bottom_y can modify 14466 /* We use a temporary it1 because line_bottom_y can modify
13251 its argument, if it moves one line down; see there. */ 14467 its argument, if it moves one line down; see there. */
13252 int start_y = line_bottom_y (&it1); 14468 int start_y;
13253 14469
14470 SAVE_IT (it1, it, it1data);
14471 start_y = line_bottom_y (&it1);
13254 do { 14472 do {
14473 RESTORE_IT (&it, &it, it1data);
13255 move_it_by_lines (&it, 1); 14474 move_it_by_lines (&it, 1);
13256 it1 = it; 14475 SAVE_IT (it1, it, it1data);
13257 } while (line_bottom_y (&it1) - start_y < amount_to_scroll); 14476 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13258 } 14477 }
13259 14478
@@ -13663,8 +14882,6 @@ try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_ste
13663 bidi-reordered rows. */ 14882 bidi-reordered rows. */
13664 while (MATRIX_ROW_CONTINUATION_LINE_P (row)) 14883 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13665 { 14884 {
13666 xassert (row->enabled_p);
13667 --row;
13668 /* If we hit the beginning of the displayed portion 14885 /* If we hit the beginning of the displayed portion
13669 without finding the first row of a continued 14886 without finding the first row of a continued
13670 line, give up. */ 14887 line, give up. */
@@ -13673,7 +14890,8 @@ try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_ste
13673 rc = CURSOR_MOVEMENT_MUST_SCROLL; 14890 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13674 break; 14891 break;
13675 } 14892 }
13676 14893 xassert (row->enabled_p);
14894 --row;
13677 } 14895 }
13678 } 14896 }
13679 if (must_scroll) 14897 if (must_scroll)
@@ -13720,22 +14938,46 @@ try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_ste
13720 14938
13721 do 14939 do
13722 { 14940 {
14941 int at_zv_p = 0, exact_match_p = 0;
14942
13723 if (MATRIX_ROW_START_CHARPOS (row) <= PT 14943 if (MATRIX_ROW_START_CHARPOS (row) <= PT
13724 && PT <= MATRIX_ROW_END_CHARPOS (row) 14944 && PT <= MATRIX_ROW_END_CHARPOS (row)
13725 && cursor_row_p (row)) 14945 && cursor_row_p (row))
13726 rv |= set_cursor_from_row (w, row, w->current_matrix, 14946 rv |= set_cursor_from_row (w, row, w->current_matrix,
13727 0, 0, 0, 0); 14947 0, 0, 0, 0);
13728 /* As soon as we've found the first suitable row 14948 /* As soon as we've found the exact match for point,
13729 whose ends_at_zv_p flag is set, we are done. */ 14949 or the first suitable row whose ends_at_zv_p flag
13730 if (rv 14950 is set, we are done. */
13731 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p) 14951 at_zv_p =
14952 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
14953 if (rv && !at_zv_p
14954 && w->cursor.hpos >= 0
14955 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
14956 w->cursor.vpos))
14957 {
14958 struct glyph_row *candidate =
14959 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14960 struct glyph *g =
14961 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
14962 EMACS_INT endpos = MATRIX_ROW_END_CHARPOS (candidate);
14963
14964 exact_match_p =
14965 (BUFFERP (g->object) && g->charpos == PT)
14966 || (INTEGERP (g->object)
14967 && (g->charpos == PT
14968 || (g->charpos == 0 && endpos - 1 == PT)));
14969 }
14970 if (rv && (at_zv_p || exact_match_p))
13732 { 14971 {
13733 rc = CURSOR_MOVEMENT_SUCCESS; 14972 rc = CURSOR_MOVEMENT_SUCCESS;
13734 break; 14973 break;
13735 } 14974 }
14975 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
14976 break;
13736 ++row; 14977 ++row;
13737 } 14978 }
13738 while ((MATRIX_ROW_CONTINUATION_LINE_P (row) 14979 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
14980 || row->continued_p)
13739 && MATRIX_ROW_BOTTOM_Y (row) <= last_y) 14981 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
13740 || (MATRIX_ROW_START_CHARPOS (row) == PT 14982 || (MATRIX_ROW_START_CHARPOS (row) == PT
13741 && MATRIX_ROW_BOTTOM_Y (row) < last_y)); 14983 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
@@ -13743,7 +14985,9 @@ try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_ste
13743 loop before all the candidates were examined, signal 14985 loop before all the candidates were examined, signal
13744 to the caller that this method failed. */ 14986 to the caller that this method failed. */
13745 if (rc != CURSOR_MOVEMENT_SUCCESS 14987 if (rc != CURSOR_MOVEMENT_SUCCESS
13746 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row))) 14988 && !(rv
14989 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14990 && !row->continued_p))
13747 rc = CURSOR_MOVEMENT_MUST_SCROLL; 14991 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13748 else if (rv) 14992 else if (rv)
13749 rc = CURSOR_MOVEMENT_SUCCESS; 14993 rc = CURSOR_MOVEMENT_SUCCESS;
@@ -13832,7 +15076,7 @@ redisplay_window (Lisp_Object window, int just_this_one_p)
13832 int current_matrix_up_to_date_p = 0; 15076 int current_matrix_up_to_date_p = 0;
13833 int used_current_matrix_p = 0; 15077 int used_current_matrix_p = 0;
13834 /* This is less strict than current_matrix_up_to_date_p. 15078 /* This is less strict than current_matrix_up_to_date_p.
13835 It indictes that the buffer contents and narrowing are unchanged. */ 15079 It indicates that the buffer contents and narrowing are unchanged. */
13836 int buffer_unchanged_p = 0; 15080 int buffer_unchanged_p = 0;
13837 int temp_scroll_step = 0; 15081 int temp_scroll_step = 0;
13838 int count = SPECPDL_INDEX (); 15082 int count = SPECPDL_INDEX ();
@@ -14203,6 +15447,7 @@ redisplay_window (Lisp_Object window, int just_this_one_p)
14203 || (XFASTINT (w->last_modified) >= MODIFF 15447 || (XFASTINT (w->last_modified) >= MODIFF
14204 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF))) 15448 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14205 { 15449 {
15450 int d1, d2, d3, d4, d5, d6;
14206 15451
14207 /* If first window line is a continuation line, and window start 15452 /* If first window line is a continuation line, and window start
14208 is inside the modified region, but the first change is before 15453 is inside the modified region, but the first change is before
@@ -14224,7 +15469,14 @@ redisplay_window (Lisp_Object window, int just_this_one_p)
14224 compute_window_start_on_continuation_line. (See also 15469 compute_window_start_on_continuation_line. (See also
14225 bug#197). */ 15470 bug#197). */
14226 && XMARKER (w->start)->buffer == current_buffer 15471 && XMARKER (w->start)->buffer == current_buffer
14227 && compute_window_start_on_continuation_line (w)) 15472 && compute_window_start_on_continuation_line (w)
15473 /* It doesn't make sense to force the window start like we
15474 do at label force_start if it is already known that point
15475 will not be visible in the resulting window, because
15476 doing so will move point from its correct position
15477 instead of scrolling the window to bring point into view.
15478 See bug#9324. */
15479 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
14228 { 15480 {
14229 w->force_start = Qt; 15481 w->force_start = Qt;
14230 SET_TEXT_POS_FROM_MARKER (startp, w->start); 15482 SET_TEXT_POS_FROM_MARKER (startp, w->start);
@@ -14346,8 +15598,8 @@ redisplay_window (Lisp_Object window, int just_this_one_p)
14346 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4) 15598 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14347 : 0; 15599 : 0;
14348 EMACS_INT margin_pos = CHARPOS (startp); 15600 EMACS_INT margin_pos = CHARPOS (startp);
14349 int scrolling_up;
14350 Lisp_Object aggressive; 15601 Lisp_Object aggressive;
15602 int scrolling_up;
14351 15603
14352 /* If there is a scroll margin at the top of the window, find 15604 /* If there is a scroll margin at the top of the window, find
14353 its character position. */ 15605 its character position. */
@@ -14356,15 +15608,18 @@ redisplay_window (Lisp_Object window, int just_this_one_p)
14356 accessible region of the buffer. This can happen when we 15608 accessible region of the buffer. This can happen when we
14357 have just switched to a different buffer and/or changed 15609 have just switched to a different buffer and/or changed
14358 its restriction. In that case, startp is initialized to 15610 its restriction. In that case, startp is initialized to
14359 the character position 1 (BEG) because we did not yet 15611 the character position 1 (BEGV) because we did not yet
14360 have chance to display the buffer even once. */ 15612 have chance to display the buffer even once. */
14361 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV) 15613 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
14362 { 15614 {
14363 struct it it1; 15615 struct it it1;
15616 void *it1data = NULL;
14364 15617
15618 SAVE_IT (it1, it, it1data);
14365 start_display (&it1, w, startp); 15619 start_display (&it1, w, startp);
14366 move_it_vertically (&it1, margin); 15620 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
14367 margin_pos = IT_CHARPOS (it1); 15621 margin_pos = IT_CHARPOS (it1);
15622 RESTORE_IT (&it, &it, it1data);
14368 } 15623 }
14369 scrolling_up = PT > margin_pos; 15624 scrolling_up = PT > margin_pos;
14370 aggressive = 15625 aggressive =
@@ -14386,7 +15641,7 @@ redisplay_window (Lisp_Object window, int just_this_one_p)
14386 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w); 15641 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
14387 if (pt_offset == 0 && float_amount > 0) 15642 if (pt_offset == 0 && float_amount > 0)
14388 pt_offset = 1; 15643 pt_offset = 1;
14389 if (pt_offset) 15644 if (pt_offset && margin > 0)
14390 margin -= 1; 15645 margin -= 1;
14391 } 15646 }
14392 /* Compute how much to move the window start backward from 15647 /* Compute how much to move the window start backward from
@@ -14398,7 +15653,8 @@ redisplay_window (Lisp_Object window, int just_this_one_p)
14398 if (pt_offset) 15653 if (pt_offset)
14399 centering_position -= pt_offset; 15654 centering_position -= pt_offset;
14400 centering_position -= 15655 centering_position -=
14401 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0)); 15656 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15657 + WINDOW_HEADER_LINE_HEIGHT (w);
14402 /* Don't let point enter the scroll margin near top of 15658 /* Don't let point enter the scroll margin near top of
14403 the window. */ 15659 the window. */
14404 if (centering_position < margin * FRAME_LINE_HEIGHT (f)) 15660 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
@@ -14505,6 +15761,25 @@ redisplay_window (Lisp_Object window, int just_this_one_p)
14505 goto recenter; 15761 goto recenter;
14506 } 15762 }
14507 15763
15764 /* Users who set scroll-conservatively to a large number want
15765 point just above/below the scroll margin. If we ended up
15766 with point's row partially visible, move the window start to
15767 make that row fully visible and out of the margin. */
15768 if (scroll_conservatively > SCROLL_LIMIT)
15769 {
15770 int margin =
15771 scroll_margin > 0
15772 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15773 : 0;
15774 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15775
15776 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15777 clear_glyph_matrix (w->desired_matrix);
15778 if (1 == try_window (window, it.current.pos,
15779 TRY_WINDOW_CHECK_MARGINS))
15780 goto done;
15781 }
15782
14508 /* If centering point failed to make the whole line visible, 15783 /* If centering point failed to make the whole line visible,
14509 put point at the top instead. That has to make the whole line 15784 put point at the top instead. That has to make the whole line
14510 visible, if it can be done. */ 15785 visible, if it can be done. */
@@ -14874,13 +16149,20 @@ try_window_reusing_current_matrix (struct window *w)
14874 16149
14875 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix); 16150 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14876 } 16151 }
14877 /* If we have reached alignment, 16152 /* If we have reached alignment, we can copy the rest of the
14878 we can copy the rest of the rows. */ 16153 rows. */
14879 if (IT_CHARPOS (it) == CHARPOS (start)) 16154 if (IT_CHARPOS (it) == CHARPOS (start)
16155 /* Don't accept "alignment" inside a display vector,
16156 since start_row could have started in the middle of
16157 that same display vector (thus their character
16158 positions match), and we have no way of telling if
16159 that is the case. */
16160 && it.current.dpvec_index < 0)
14880 break; 16161 break;
14881 16162
14882 if (display_line (&it)) 16163 if (display_line (&it))
14883 last_text_row = it.glyph_row - 1; 16164 last_text_row = it.glyph_row - 1;
16165
14884 } 16166 }
14885 16167
14886 /* A value of current_y < last_visible_y means that we stopped 16168 /* A value of current_y < last_visible_y means that we stopped
@@ -15046,7 +16328,10 @@ try_window_reusing_current_matrix (struct window *w)
15046 ++first_row_to_display) 16328 ++first_row_to_display)
15047 { 16329 {
15048 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display) 16330 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15049 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)) 16331 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16332 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16333 && first_row_to_display->ends_at_zv_p
16334 && pt_row == NULL)))
15050 pt_row = first_row_to_display; 16335 pt_row = first_row_to_display;
15051 } 16336 }
15052 16337
@@ -15138,7 +16423,9 @@ try_window_reusing_current_matrix (struct window *w)
15138 if (pt_row) 16423 if (pt_row)
15139 { 16424 {
15140 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos); 16425 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15141 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row); 16426 row < bottom_row
16427 && PT >= MATRIX_ROW_END_CHARPOS (row)
16428 && !row->ends_at_zv_p;
15142 row++) 16429 row++)
15143 { 16430 {
15144 w->cursor.vpos++; 16431 w->cursor.vpos++;
@@ -15147,7 +16434,7 @@ try_window_reusing_current_matrix (struct window *w)
15147 if (row < bottom_row) 16434 if (row < bottom_row)
15148 { 16435 {
15149 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos; 16436 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15150 struct glyph *end = glyph + row->used[TEXT_AREA]; 16437 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15151 16438
15152 /* Can't use this optimization with bidi-reordered glyph 16439 /* Can't use this optimization with bidi-reordered glyph
15153 rows, unless cursor is already at point. */ 16440 rows, unless cursor is already at point. */
@@ -15802,7 +17089,7 @@ try_window_id (struct window *w)
15802 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w); 17089 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15803 if (last_unchanged_at_beg_row) 17090 if (last_unchanged_at_beg_row)
15804 { 17091 {
15805 /* Avoid starting to display in the moddle of a character, a TAB 17092 /* Avoid starting to display in the middle of a character, a TAB
15806 for instance. This is easier than to set up the iterator 17093 for instance. This is easier than to set up the iterator
15807 exactly, and it's not a frequent case, so the additional 17094 exactly, and it's not a frequent case, so the additional
15808 effort wouldn't really pay off. */ 17095 effort wouldn't really pay off. */
@@ -15990,8 +17277,8 @@ try_window_id (struct window *w)
15990 { 17277 {
15991 int this_scroll_margin, cursor_height; 17278 int this_scroll_margin, cursor_height;
15992 17279
15993 this_scroll_margin = max (0, scroll_margin); 17280 this_scroll_margin =
15994 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4); 17281 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
15995 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f); 17282 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15996 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height; 17283 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15997 17284
@@ -16731,6 +18018,23 @@ insert_left_trunc_glyphs (struct it *it)
16731 } 18018 }
16732} 18019}
16733 18020
18021/* Compute the hash code for ROW. */
18022unsigned
18023row_hash (struct glyph_row *row)
18024{
18025 int area, k;
18026 unsigned hashval = 0;
18027
18028 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18029 for (k = 0; k < row->used[area]; ++k)
18030 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18031 + row->glyphs[area][k].u.val
18032 + row->glyphs[area][k].face_id
18033 + row->glyphs[area][k].padding_p
18034 + (row->glyphs[area][k].type << 2));
18035
18036 return hashval;
18037}
16734 18038
16735/* Compute the pixel height and width of IT->glyph_row. 18039/* Compute the pixel height and width of IT->glyph_row.
16736 18040
@@ -16817,17 +18121,7 @@ compute_line_metrics (struct it *it)
16817 } 18121 }
16818 18122
16819 /* Compute a hash code for this row. */ 18123 /* Compute a hash code for this row. */
16820 { 18124 row->hash = row_hash (row);
16821 int area, i;
16822 row->hash = 0;
16823 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16824 for (i = 0; i < row->used[area]; ++i)
16825 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16826 + row->glyphs[area][i].u.val
16827 + row->glyphs[area][i].face_id
16828 + row->glyphs[area][i].padding_p
16829 + (row->glyphs[area][i].type << 2));
16830 }
16831 18125
16832 it->max_ascent = it->max_descent = 0; 18126 it->max_ascent = it->max_descent = 0;
16833 it->max_phys_ascent = it->max_phys_descent = 0; 18127 it->max_phys_ascent = it->max_phys_descent = 0;
@@ -17162,14 +18456,17 @@ cursor_row_p (struct glyph_row *row)
17162{ 18456{
17163 int result = 1; 18457 int result = 1;
17164 18458
17165 if (PT == CHARPOS (row->end.pos)) 18459 if (PT == CHARPOS (row->end.pos)
18460 || PT == MATRIX_ROW_END_CHARPOS (row))
17166 { 18461 {
17167 /* Suppose the row ends on a string. 18462 /* Suppose the row ends on a string.
17168 Unless the row is continued, that means it ends on a newline 18463 Unless the row is continued, that means it ends on a newline
17169 in the string. If it's anything other than a display string 18464 in the string. If it's anything other than a display string
17170 (e.g. a before-string from an overlay), we don't want the 18465 (e.g., a before-string from an overlay), we don't want the
17171 cursor there. (This heuristic seems to give the optimal 18466 cursor there. (This heuristic seems to give the optimal
17172 behavior for the various types of multi-line strings.) */ 18467 behavior for the various types of multi-line strings.)
18468 One exception: if the string has `cursor' property on one of
18469 its characters, we _do_ want the cursor there. */
17173 if (CHARPOS (row->end.string_pos) >= 0) 18470 if (CHARPOS (row->end.string_pos) >= 0)
17174 { 18471 {
17175 if (row->continued_p) 18472 if (row->continued_p)
@@ -17191,6 +18488,25 @@ cursor_row_p (struct glyph_row *row)
17191 result = 18488 result =
17192 (!NILP (prop) 18489 (!NILP (prop)
17193 && display_prop_string_p (prop, glyph->object)); 18490 && display_prop_string_p (prop, glyph->object));
18491 /* If there's a `cursor' property on one of the
18492 string's characters, this row is a cursor row,
18493 even though this is not a display string. */
18494 if (!result)
18495 {
18496 Lisp_Object s = glyph->object;
18497
18498 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18499 {
18500 EMACS_INT gpos = glyph->charpos;
18501
18502 if (!NILP (Fget_char_property (make_number (gpos),
18503 Qcursor, s)))
18504 {
18505 result = 1;
18506 break;
18507 }
18508 }
18509 }
17194 break; 18510 break;
17195 } 18511 }
17196 } 18512 }
@@ -17223,14 +18539,26 @@ cursor_row_p (struct glyph_row *row)
17223 18539
17224 18540
17225 18541
17226/* Push the display property PROP so that it will be rendered at the 18542/* Push the property PROP so that it will be rendered at the current
17227 current position in IT. Return 1 if PROP was successfully pushed, 18543 position in IT. Return 1 if PROP was successfully pushed, 0
17228 0 otherwise. */ 18544 otherwise. Called from handle_line_prefix to handle the
18545 `line-prefix' and `wrap-prefix' properties. */
17229 18546
17230static int 18547static int
17231push_display_prop (struct it *it, Lisp_Object prop) 18548push_prefix_prop (struct it *it, Lisp_Object prop)
17232{ 18549{
17233 push_it (it, NULL); 18550 struct text_pos pos =
18551 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18552
18553 xassert (it->method == GET_FROM_BUFFER
18554 || it->method == GET_FROM_DISPLAY_VECTOR
18555 || it->method == GET_FROM_STRING);
18556
18557 /* We need to save the current buffer/string position, so it will be
18558 restored by pop_it, because iterate_out_of_display_property
18559 depends on that being set correctly, but some situations leave
18560 it->position not yet set when this function is called. */
18561 push_it (it, &pos);
17234 18562
17235 if (STRINGP (prop)) 18563 if (STRINGP (prop))
17236 { 18564 {
@@ -17241,12 +18569,34 @@ push_display_prop (struct it *it, Lisp_Object prop)
17241 } 18569 }
17242 18570
17243 it->string = prop; 18571 it->string = prop;
18572 it->string_from_prefix_prop_p = 1;
17244 it->multibyte_p = STRING_MULTIBYTE (it->string); 18573 it->multibyte_p = STRING_MULTIBYTE (it->string);
17245 it->current.overlay_string_index = -1; 18574 it->current.overlay_string_index = -1;
17246 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0; 18575 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17247 it->end_charpos = it->string_nchars = SCHARS (it->string); 18576 it->end_charpos = it->string_nchars = SCHARS (it->string);
17248 it->method = GET_FROM_STRING; 18577 it->method = GET_FROM_STRING;
17249 it->stop_charpos = 0; 18578 it->stop_charpos = 0;
18579 it->prev_stop = 0;
18580 it->base_level_stop = 0;
18581
18582 /* Force paragraph direction to be that of the parent
18583 buffer/string. */
18584 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18585 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18586 else
18587 it->paragraph_embedding = L2R;
18588
18589 /* Set up the bidi iterator for this display string. */
18590 if (it->bidi_p)
18591 {
18592 it->bidi_it.string.lstring = it->string;
18593 it->bidi_it.string.s = NULL;
18594 it->bidi_it.string.schars = it->end_charpos;
18595 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18596 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18597 it->bidi_it.string.unibyte = !it->multibyte_p;
18598 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18599 }
17250 } 18600 }
17251 else if (CONSP (prop) && EQ (XCAR (prop), Qspace)) 18601 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17252 { 18602 {
@@ -17293,6 +18643,7 @@ static void
17293handle_line_prefix (struct it *it) 18643handle_line_prefix (struct it *it)
17294{ 18644{
17295 Lisp_Object prefix; 18645 Lisp_Object prefix;
18646
17296 if (it->continuation_lines_width > 0) 18647 if (it->continuation_lines_width > 0)
17297 { 18648 {
17298 prefix = get_it_property (it, Qwrap_prefix); 18649 prefix = get_it_property (it, Qwrap_prefix);
@@ -17305,7 +18656,7 @@ handle_line_prefix (struct it *it)
17305 if (NILP (prefix)) 18656 if (NILP (prefix))
17306 prefix = Vline_prefix; 18657 prefix = Vline_prefix;
17307 } 18658 }
17308 if (! NILP (prefix) && push_display_prop (it, prefix)) 18659 if (! NILP (prefix) && push_prefix_prop (it, prefix))
17309 { 18660 {
17310 /* If the prefix is wider than the window, and we try to wrap 18661 /* If the prefix is wider than the window, and we try to wrap
17311 it, it would acquire its own wrap prefix, and so on till the 18662 it, it would acquire its own wrap prefix, and so on till the
@@ -17318,9 +18669,9 @@ handle_line_prefix (struct it *it)
17318 18669
17319 18670
17320/* Remove N glyphs at the start of a reversed IT->glyph_row. Called 18671/* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17321 only for R2L lines from display_line, when it decides that too many 18672 only for R2L lines from display_line and display_string, when they
17322 glyphs were produced by PRODUCE_GLYPHS, and the line needs to be 18673 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
17323 continued. */ 18674 the line/string needs to be continued on the next glyph row. */
17324static void 18675static void
17325unproduce_glyphs (struct it *it, int n) 18676unproduce_glyphs (struct it *it, int n)
17326{ 18677{
@@ -17350,12 +18701,13 @@ find_row_edges (struct it *it, struct glyph_row *row,
17350 lines' rows is implemented for bidi-reordered rows. */ 18701 lines' rows is implemented for bidi-reordered rows. */
17351 18702
17352 /* ROW->minpos is the value of min_pos, the minimal buffer position 18703 /* ROW->minpos is the value of min_pos, the minimal buffer position
17353 we have in ROW. */ 18704 we have in ROW, or ROW->start.pos if that is smaller. */
17354 if (min_pos <= ZV) 18705 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
17355 SET_TEXT_POS (row->minpos, min_pos, min_bpos); 18706 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17356 else 18707 else
17357 /* We didn't find _any_ valid buffer positions in any of the 18708 /* We didn't find buffer positions smaller than ROW->start, or
17358 glyphs, so we must trust the iterator's computed positions. */ 18709 didn't find _any_ valid buffer positions in any of the glyphs,
18710 so we must trust the iterator's computed positions. */
17359 row->minpos = row->start.pos; 18711 row->minpos = row->start.pos;
17360 if (max_pos <= 0) 18712 if (max_pos <= 0)
17361 { 18713 {
@@ -17369,7 +18721,8 @@ find_row_edges (struct it *it, struct glyph_row *row,
17369 Line ends in a newline from buffer eol_pos + 1 18721 Line ends in a newline from buffer eol_pos + 1
17370 Line is continued from buffer max_pos + 1 18722 Line is continued from buffer max_pos + 1
17371 Line is truncated on right it->current.pos 18723 Line is truncated on right it->current.pos
17372 Line ends in a newline from string max_pos 18724 Line ends in a newline from string max_pos + 1(*)
18725 (*) + 1 only when line ends in a forward scan
17373 Line is continued from string max_pos 18726 Line is continued from string max_pos
17374 Line is continued from display vector max_pos 18727 Line is continued from display vector max_pos
17375 Line is entirely from a string min_pos == max_pos 18728 Line is entirely from a string min_pos == max_pos
@@ -17382,8 +18735,81 @@ find_row_edges (struct it *it, struct glyph_row *row,
17382 row->maxpos = it->current.pos; 18735 row->maxpos = it->current.pos;
17383 else if (row->used[TEXT_AREA]) 18736 else if (row->used[TEXT_AREA])
17384 { 18737 {
17385 if (row->ends_in_newline_from_string_p) 18738 int seen_this_string = 0;
17386 SET_TEXT_POS (row->maxpos, max_pos, max_bpos); 18739 struct glyph_row *r1 = row - 1;
18740
18741 /* Did we see the same display string on the previous row? */
18742 if (STRINGP (it->object)
18743 /* this is not the first row */
18744 && row > it->w->desired_matrix->rows
18745 /* previous row is not the header line */
18746 && !r1->mode_line_p
18747 /* previous row also ends in a newline from a string */
18748 && r1->ends_in_newline_from_string_p)
18749 {
18750 struct glyph *start, *end;
18751
18752 /* Search for the last glyph of the previous row that came
18753 from buffer or string. Depending on whether the row is
18754 L2R or R2L, we need to process it front to back or the
18755 other way round. */
18756 if (!r1->reversed_p)
18757 {
18758 start = r1->glyphs[TEXT_AREA];
18759 end = start + r1->used[TEXT_AREA];
18760 /* Glyphs inserted by redisplay have an integer (zero)
18761 as their object. */
18762 while (end > start
18763 && INTEGERP ((end - 1)->object)
18764 && (end - 1)->charpos <= 0)
18765 --end;
18766 if (end > start)
18767 {
18768 if (EQ ((end - 1)->object, it->object))
18769 seen_this_string = 1;
18770 }
18771 else
18772 /* If all the glyphs of the previous row were inserted
18773 by redisplay, it means the previous row was
18774 produced from a single newline, which is only
18775 possible if that newline came from the same string
18776 as the one which produced this ROW. */
18777 seen_this_string = 1;
18778 }
18779 else
18780 {
18781 end = r1->glyphs[TEXT_AREA] - 1;
18782 start = end + r1->used[TEXT_AREA];
18783 while (end < start
18784 && INTEGERP ((end + 1)->object)
18785 && (end + 1)->charpos <= 0)
18786 ++end;
18787 if (end < start)
18788 {
18789 if (EQ ((end + 1)->object, it->object))
18790 seen_this_string = 1;
18791 }
18792 else
18793 seen_this_string = 1;
18794 }
18795 }
18796 /* Take note of each display string that covers a newline only
18797 once, the first time we see it. This is for when a display
18798 string includes more than one newline in it. */
18799 if (row->ends_in_newline_from_string_p && !seen_this_string)
18800 {
18801 /* If we were scanning the buffer forward when we displayed
18802 the string, we want to account for at least one buffer
18803 position that belongs to this row (position covered by
18804 the display string), so that cursor positioning will
18805 consider this row as a candidate when point is at the end
18806 of the visual line represented by this row. This is not
18807 required when scanning back, because max_pos will already
18808 have a much larger value. */
18809 if (CHARPOS (row->end.pos) > max_pos)
18810 INC_BOTH (max_pos, max_bpos);
18811 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18812 }
17387 else if (CHARPOS (it->eol_pos) > 0) 18813 else if (CHARPOS (it->eol_pos) > 0)
17388 SET_TEXT_POS (row->maxpos, 18814 SET_TEXT_POS (row->maxpos,
17389 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1); 18815 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
@@ -17430,6 +18856,7 @@ display_line (struct it *it)
17430 struct glyph_row *row = it->glyph_row; 18856 struct glyph_row *row = it->glyph_row;
17431 Lisp_Object overlay_arrow_string; 18857 Lisp_Object overlay_arrow_string;
17432 struct it wrap_it; 18858 struct it wrap_it;
18859 void *wrap_data = NULL;
17433 int may_wrap = 0, wrap_x IF_LINT (= 0); 18860 int may_wrap = 0, wrap_x IF_LINT (= 0);
17434 int wrap_row_used = -1; 18861 int wrap_row_used = -1;
17435 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0); 18862 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
@@ -17510,15 +18937,23 @@ display_line (struct it *it)
17510#define RECORD_MAX_MIN_POS(IT) \ 18937#define RECORD_MAX_MIN_POS(IT) \
17511 do \ 18938 do \
17512 { \ 18939 { \
17513 if (IT_CHARPOS (*(IT)) < min_pos) \ 18940 int composition_p = !STRINGP ((IT)->string) \
18941 && ((IT)->what == IT_COMPOSITION); \
18942 EMACS_INT current_pos = \
18943 composition_p ? (IT)->cmp_it.charpos \
18944 : IT_CHARPOS (*(IT)); \
18945 EMACS_INT current_bpos = \
18946 composition_p ? CHAR_TO_BYTE (current_pos) \
18947 : IT_BYTEPOS (*(IT)); \
18948 if (current_pos < min_pos) \
17514 { \ 18949 { \
17515 min_pos = IT_CHARPOS (*(IT)); \ 18950 min_pos = current_pos; \
17516 min_bpos = IT_BYTEPOS (*(IT)); \ 18951 min_bpos = current_bpos; \
17517 } \ 18952 } \
17518 if (IT_CHARPOS (*(IT)) > max_pos) \ 18953 if (IT_CHARPOS (*it) > max_pos) \
17519 { \ 18954 { \
17520 max_pos = IT_CHARPOS (*(IT)); \ 18955 max_pos = IT_CHARPOS (*it); \
17521 max_bpos = IT_BYTEPOS (*(IT)); \ 18956 max_bpos = IT_BYTEPOS (*it); \
17522 } \ 18957 } \
17523 } \ 18958 } \
17524 while (0) 18959 while (0)
@@ -17584,7 +19019,7 @@ display_line (struct it *it)
17584 may_wrap = 1; 19019 may_wrap = 1;
17585 else if (may_wrap) 19020 else if (may_wrap)
17586 { 19021 {
17587 wrap_it = *it; 19022 SAVE_IT (wrap_it, *it, wrap_data);
17588 wrap_x = x; 19023 wrap_x = x;
17589 wrap_row_used = row->used[TEXT_AREA]; 19024 wrap_row_used = row->used[TEXT_AREA];
17590 wrap_row_ascent = row->ascent; 19025 wrap_row_ascent = row->ascent;
@@ -17683,11 +19118,6 @@ display_line (struct it *it)
17683 it->current_x = new_x; 19118 it->current_x = new_x;
17684 it->continuation_lines_width += new_x; 19119 it->continuation_lines_width += new_x;
17685 ++it->hpos; 19120 ++it->hpos;
17686 /* Record the maximum and minimum buffer
17687 positions seen so far in glyphs that will be
17688 displayed by this row. */
17689 if (it->bidi_p)
17690 RECORD_MAX_MIN_POS (it);
17691 if (i == nglyphs - 1) 19121 if (i == nglyphs - 1)
17692 { 19122 {
17693 /* If line-wrap is on, check if a previous 19123 /* If line-wrap is on, check if a previous
@@ -17702,6 +19132,11 @@ display_line (struct it *it)
17702 || IT_DISPLAYING_WHITESPACE (it))) 19132 || IT_DISPLAYING_WHITESPACE (it)))
17703 goto back_to_wrap; 19133 goto back_to_wrap;
17704 19134
19135 /* Record the maximum and minimum buffer
19136 positions seen so far in glyphs that will be
19137 displayed by this row. */
19138 if (it->bidi_p)
19139 RECORD_MAX_MIN_POS (it);
17705 set_iterator_to_next (it, 1); 19140 set_iterator_to_next (it, 1);
17706 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)) 19141 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17707 { 19142 {
@@ -17719,6 +19154,8 @@ display_line (struct it *it)
17719 } 19154 }
17720 } 19155 }
17721 } 19156 }
19157 else if (it->bidi_p)
19158 RECORD_MAX_MIN_POS (it);
17722 } 19159 }
17723 else if (CHAR_GLYPH_PADDING_P (*glyph) 19160 else if (CHAR_GLYPH_PADDING_P (*glyph)
17724 && !FRAME_WINDOW_P (it->f)) 19161 && !FRAME_WINDOW_P (it->f))
@@ -17754,7 +19191,7 @@ display_line (struct it *it)
17754 if (row->reversed_p) 19191 if (row->reversed_p)
17755 unproduce_glyphs (it, 19192 unproduce_glyphs (it,
17756 row->used[TEXT_AREA] - wrap_row_used); 19193 row->used[TEXT_AREA] - wrap_row_used);
17757 *it = wrap_it; 19194 RESTORE_IT (it, &wrap_it, wrap_data);
17758 it->continuation_lines_width += wrap_x; 19195 it->continuation_lines_width += wrap_x;
17759 row->used[TEXT_AREA] = wrap_row_used; 19196 row->used[TEXT_AREA] = wrap_row_used;
17760 row->ascent = wrap_row_ascent; 19197 row->ascent = wrap_row_ascent;
@@ -17849,6 +19286,10 @@ display_line (struct it *it)
17849 xassert (it->first_visible_x <= it->last_visible_x); 19286 xassert (it->first_visible_x <= it->last_visible_x);
17850 } 19287 }
17851 } 19288 }
19289 /* Even if this display element produced no glyphs at all,
19290 we want to record its position. */
19291 if (it->bidi_p && nglyphs == 0)
19292 RECORD_MAX_MIN_POS (it);
17852 19293
17853 row->ascent = max (row->ascent, it->max_ascent); 19294 row->ascent = max (row->ascent, it->max_ascent);
17854 row->height = max (row->height, it->max_ascent + it->max_descent); 19295 row->height = max (row->height, it->max_ascent + it->max_descent);
@@ -17965,6 +19406,9 @@ display_line (struct it *it)
17965 } 19406 }
17966 } 19407 }
17967 19408
19409 if (wrap_data)
19410 bidi_unshelve_cache (wrap_data, 1);
19411
17968 /* If line is not empty and hscrolled, maybe insert truncation glyphs 19412 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17969 at the left window margin. */ 19413 at the left window margin. */
17970 if (it->first_visible_x 19414 if (it->first_visible_x
@@ -18037,9 +19481,18 @@ display_line (struct it *it)
18037 overlay_arrow_seen = 1; 19481 overlay_arrow_seen = 1;
18038 } 19482 }
18039 19483
19484 /* Highlight trailing whitespace. */
19485 if (!NILP (Vshow_trailing_whitespace))
19486 highlight_trailing_whitespace (it->f, it->glyph_row);
19487
18040 /* Compute pixel dimensions of this line. */ 19488 /* Compute pixel dimensions of this line. */
18041 compute_line_metrics (it); 19489 compute_line_metrics (it);
18042 19490
19491 /* Implementation note: No changes in the glyphs of ROW or in their
19492 faces can be done past this point, because compute_line_metrics
19493 computes ROW's hash value and stores it within the glyph_row
19494 structure. */
19495
18043 /* Record whether this row ends inside an ellipsis. */ 19496 /* Record whether this row ends inside an ellipsis. */
18044 row->ends_in_ellipsis_p 19497 row->ends_in_ellipsis_p
18045 = (it->method == GET_FROM_DISPLAY_VECTOR 19498 = (it->method == GET_FROM_DISPLAY_VECTOR
@@ -18074,10 +19527,6 @@ display_line (struct it *it)
18074 && cursor_row_p (row)) 19527 && cursor_row_p (row))
18075 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0); 19528 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18076 19529
18077 /* Highlight trailing whitespace. */
18078 if (!NILP (Vshow_trailing_whitespace))
18079 highlight_trailing_whitespace (it->f, it->glyph_row);
18080
18081 /* Prepare for the next line. This line starts horizontally at (X 19530 /* Prepare for the next line. This line starts horizontally at (X
18082 HPOS) = (0 0). Vertical positions are incremented. As a 19531 HPOS) = (0 0). Vertical positions are incremented. As a
18083 convenience for the caller, IT->glyph_row is set to the next 19532 convenience for the caller, IT->glyph_row is set to the next
@@ -18122,7 +19571,11 @@ See also `bidi-paragraph-direction'. */)
18122 buf = XBUFFER (buffer); 19571 buf = XBUFFER (buffer);
18123 } 19572 }
18124 19573
18125 if (NILP (BVAR (buf, bidi_display_reordering))) 19574 if (NILP (BVAR (buf, bidi_display_reordering))
19575 || NILP (BVAR (buf, enable_multibyte_characters))
19576 /* When we are loading loadup.el, the character property tables
19577 needed for bidi iteration are not yet available. */
19578 || !NILP (Vpurify_flag))
18126 return Qleft_to_right; 19579 return Qleft_to_right;
18127 else if (!NILP (BVAR (buf, bidi_paragraph_direction))) 19580 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
18128 return BVAR (buf, bidi_paragraph_direction); 19581 return BVAR (buf, bidi_paragraph_direction);
@@ -18135,6 +19588,7 @@ See also `bidi-paragraph-direction'. */)
18135 EMACS_INT pos = BUF_PT (buf); 19588 EMACS_INT pos = BUF_PT (buf);
18136 EMACS_INT bytepos = BUF_PT_BYTE (buf); 19589 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18137 int c; 19590 int c;
19591 void *itb_data = bidi_shelve_cache ();
18138 19592
18139 set_buffer_temp (buf); 19593 set_buffer_temp (buf);
18140 /* bidi_paragraph_init finds the base direction of the paragraph 19594 /* bidi_paragraph_init finds the base direction of the paragraph
@@ -18147,25 +19601,28 @@ See also `bidi-paragraph-direction'. */)
18147 pos--; 19601 pos--;
18148 bytepos = CHAR_TO_BYTE (pos); 19602 bytepos = CHAR_TO_BYTE (pos);
18149 } 19603 }
18150 while ((c = FETCH_BYTE (bytepos)) == '\n' 19604 if (fast_looking_at (build_string ("[\f\t ]*\n"),
18151 || c == ' ' || c == '\t' || c == '\f') 19605 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
18152 { 19606 {
18153 if (bytepos <= BEGV_BYTE) 19607 while ((c = FETCH_BYTE (bytepos)) == '\n'
18154 break; 19608 || c == ' ' || c == '\t' || c == '\f')
18155 bytepos--; 19609 {
18156 pos--; 19610 if (bytepos <= BEGV_BYTE)
19611 break;
19612 bytepos--;
19613 pos--;
19614 }
19615 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19616 bytepos--;
18157 } 19617 }
18158 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos))) 19618 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
18159 bytepos--;
18160 itb.charpos = pos;
18161 itb.bytepos = bytepos;
18162 itb.nchars = -1;
18163 itb.frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ()); /* guesswork */
18164 itb.first_elt = 1;
18165 itb.separator_limit = -1;
18166 itb.paragraph_dir = NEUTRAL_DIR; 19619 itb.paragraph_dir = NEUTRAL_DIR;
18167 19620 itb.string.s = NULL;
19621 itb.string.lstring = Qnil;
19622 itb.string.bufpos = 0;
19623 itb.string.unibyte = 0;
18168 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1); 19624 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19625 bidi_unshelve_cache (itb_data, 0);
18169 set_buffer_temp (old); 19626 set_buffer_temp (old);
18170 switch (itb.paragraph_dir) 19627 switch (itb.paragraph_dir)
18171 { 19628 {
@@ -18249,6 +19706,11 @@ display_menu_bar (struct window *w)
18249 } 19706 }
18250#endif /* not USE_X_TOOLKIT */ 19707#endif /* not USE_X_TOOLKIT */
18251 19708
19709 /* FIXME: This should be controlled by a user option. See the
19710 comments in redisplay_tool_bar and display_mode_line about
19711 this. */
19712 it.paragraph_embedding = L2R;
19713
18252 if (! mode_line_inverse_video) 19714 if (! mode_line_inverse_video)
18253 /* Force the menu-bar to be displayed in the default face. */ 19715 /* Force the menu-bar to be displayed in the default face. */
18254 it.base_face_id = it.face_id = DEFAULT_FACE_ID; 19716 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
@@ -18426,6 +19888,11 @@ display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18426 /* Force the mode-line to be displayed in the default face. */ 19888 /* Force the mode-line to be displayed in the default face. */
18427 it.base_face_id = it.face_id = DEFAULT_FACE_ID; 19889 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18428 19890
19891 /* FIXME: This should be controlled by a user option. But
19892 supporting such an option is not trivial, since the mode line is
19893 made up of many separate strings. */
19894 it.paragraph_embedding = L2R;
19895
18429 record_unwind_protect (unwind_format_mode_line, 19896 record_unwind_protect (unwind_format_mode_line,
18430 format_mode_line_unwind_data (NULL, Qnil, 0)); 19897 format_mode_line_unwind_data (NULL, Qnil, 0));
18431 19898
@@ -19971,6 +21438,7 @@ display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_st
19971 int hpos_at_start = it->hpos; 21438 int hpos_at_start = it->hpos;
19972 int saved_face_id = it->face_id; 21439 int saved_face_id = it->face_id;
19973 struct glyph_row *row = it->glyph_row; 21440 struct glyph_row *row = it->glyph_row;
21441 EMACS_INT it_charpos;
19974 21442
19975 /* Initialize the iterator IT for iteration over STRING beginning 21443 /* Initialize the iterator IT for iteration over STRING beginning
19976 with index START. */ 21444 with index START. */
@@ -19979,10 +21447,10 @@ display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_st
19979 if (string && STRINGP (lisp_string)) 21447 if (string && STRINGP (lisp_string))
19980 /* LISP_STRING is the one returned by decode_mode_spec. We should 21448 /* LISP_STRING is the one returned by decode_mode_spec. We should
19981 ignore its text properties. */ 21449 ignore its text properties. */
19982 it->stop_charpos = -1; 21450 it->stop_charpos = it->end_charpos;
19983 21451
19984 /* If displaying STRING, set up the face of the iterator 21452 /* If displaying STRING, set up the face of the iterator from
19985 from LISP_STRING, if that's given. */ 21453 FACE_STRING, if that's given. */
19986 if (STRINGP (face_string)) 21454 if (STRINGP (face_string))
19987 { 21455 {
19988 EMACS_INT endptr; 21456 EMACS_INT endptr;
@@ -20016,6 +21484,11 @@ display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_st
20016 row->phys_height = it->max_phys_ascent + it->max_phys_descent; 21484 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20017 row->extra_line_spacing = it->max_extra_line_spacing; 21485 row->extra_line_spacing = it->max_extra_line_spacing;
20018 21486
21487 if (STRINGP (it->string))
21488 it_charpos = IT_STRING_CHARPOS (*it);
21489 else
21490 it_charpos = IT_CHARPOS (*it);
21491
20019 /* This condition is for the case that we are called with current_x 21492 /* This condition is for the case that we are called with current_x
20020 past last_visible_x. */ 21493 past last_visible_x. */
20021 while (it->current_x < max_x) 21494 while (it->current_x < max_x)
@@ -20028,10 +21501,10 @@ display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_st
20028 21501
20029 /* Produce glyphs. */ 21502 /* Produce glyphs. */
20030 x_before = it->current_x; 21503 x_before = it->current_x;
20031 n_glyphs_before = it->glyph_row->used[TEXT_AREA]; 21504 n_glyphs_before = row->used[TEXT_AREA];
20032 PRODUCE_GLYPHS (it); 21505 PRODUCE_GLYPHS (it);
20033 21506
20034 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before; 21507 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20035 i = 0; 21508 i = 0;
20036 x = x_before; 21509 x = x_before;
20037 while (i < nglyphs) 21510 while (i < nglyphs)
@@ -20045,12 +21518,18 @@ display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_st
20045 if (CHAR_GLYPH_PADDING_P (*glyph)) 21518 if (CHAR_GLYPH_PADDING_P (*glyph))
20046 { 21519 {
20047 /* A wide character is unbreakable. */ 21520 /* A wide character is unbreakable. */
20048 it->glyph_row->used[TEXT_AREA] = n_glyphs_before; 21521 if (row->reversed_p)
21522 unproduce_glyphs (it, row->used[TEXT_AREA]
21523 - n_glyphs_before);
21524 row->used[TEXT_AREA] = n_glyphs_before;
20049 it->current_x = x_before; 21525 it->current_x = x_before;
20050 } 21526 }
20051 else 21527 else
20052 { 21528 {
20053 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i; 21529 if (row->reversed_p)
21530 unproduce_glyphs (it, row->used[TEXT_AREA]
21531 - (n_glyphs_before + i));
21532 row->used[TEXT_AREA] = n_glyphs_before + i;
20054 it->current_x = x; 21533 it->current_x = x;
20055 } 21534 }
20056 break; 21535 break;
@@ -20060,7 +21539,7 @@ display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_st
20060 /* Glyph is at least partially visible. */ 21539 /* Glyph is at least partially visible. */
20061 ++it->hpos; 21540 ++it->hpos;
20062 if (x < it->first_visible_x) 21541 if (x < it->first_visible_x)
20063 it->glyph_row->x = x - it->first_visible_x; 21542 row->x = x - it->first_visible_x;
20064 } 21543 }
20065 else 21544 else
20066 { 21545 {
@@ -20092,6 +21571,10 @@ display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_st
20092 } 21571 }
20093 21572
20094 set_iterator_to_next (it, 1); 21573 set_iterator_to_next (it, 1);
21574 if (STRINGP (it->string))
21575 it_charpos = IT_STRING_CHARPOS (*it);
21576 else
21577 it_charpos = IT_CHARPOS (*it);
20095 21578
20096 /* Stop if truncating at the right edge. */ 21579 /* Stop if truncating at the right edge. */
20097 if (it->line_wrap == TRUNCATE 21580 if (it->line_wrap == TRUNCATE
@@ -20099,7 +21582,7 @@ display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_st
20099 { 21582 {
20100 /* Add truncation mark, but don't do it if the line is 21583 /* Add truncation mark, but don't do it if the line is
20101 truncated at a padding space. */ 21584 truncated at a padding space. */
20102 if (IT_CHARPOS (*it) < it->string_nchars) 21585 if (it_charpos < it->string_nchars)
20103 { 21586 {
20104 if (!FRAME_WINDOW_P (it->f)) 21587 if (!FRAME_WINDOW_P (it->f))
20105 { 21588 {
@@ -20107,9 +21590,20 @@ display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_st
20107 21590
20108 if (it->current_x > it->last_visible_x) 21591 if (it->current_x > it->last_visible_x)
20109 { 21592 {
20110 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii) 21593 if (!row->reversed_p)
20111 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii])) 21594 {
20112 break; 21595 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21596 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21597 break;
21598 }
21599 else
21600 {
21601 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21602 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21603 break;
21604 unproduce_glyphs (it, ii + 1);
21605 ii = row->used[TEXT_AREA] - (ii + 1);
21606 }
20113 for (n = row->used[TEXT_AREA]; ii < n; ++ii) 21607 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
20114 { 21608 {
20115 row->used[TEXT_AREA] = ii; 21609 row->used[TEXT_AREA] = ii;
@@ -20118,7 +21612,7 @@ display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_st
20118 } 21612 }
20119 produce_special_glyphs (it, IT_TRUNCATION); 21613 produce_special_glyphs (it, IT_TRUNCATION);
20120 } 21614 }
20121 it->glyph_row->truncated_on_right_p = 1; 21615 row->truncated_on_right_p = 1;
20122 } 21616 }
20123 break; 21617 break;
20124 } 21618 }
@@ -20126,11 +21620,11 @@ display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_st
20126 21620
20127 /* Maybe insert a truncation at the left. */ 21621 /* Maybe insert a truncation at the left. */
20128 if (it->first_visible_x 21622 if (it->first_visible_x
20129 && IT_CHARPOS (*it) > 0) 21623 && it_charpos > 0)
20130 { 21624 {
20131 if (!FRAME_WINDOW_P (it->f)) 21625 if (!FRAME_WINDOW_P (it->f))
20132 insert_left_trunc_glyphs (it); 21626 insert_left_trunc_glyphs (it);
20133 it->glyph_row->truncated_on_left_p = 1; 21627 row->truncated_on_left_p = 1;
20134 } 21628 }
20135 21629
20136 it->face_id = saved_face_id; 21630 it->face_id = saved_face_id;
@@ -20277,7 +21771,7 @@ else if the text is replaced by an ellipsis. */)
20277 ? XFLOATINT (X) \ 21771 ? XFLOATINT (X) \
20278 : - 1) 21772 : - 1)
20279 21773
20280int 21774static int
20281calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop, 21775calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20282 struct font *font, int width_p, int *align_to) 21776 struct font *font, int width_p, int *align_to)
20283{ 21777{
@@ -20410,7 +21904,7 @@ calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20410 if (FRAME_WINDOW_P (it->f) 21904 if (FRAME_WINDOW_P (it->f)
20411 && valid_image_p (prop)) 21905 && valid_image_p (prop))
20412 { 21906 {
20413 int id = lookup_image (it->f, prop); 21907 ptrdiff_t id = lookup_image (it->f, prop);
20414 struct image *img = IMAGE_FROM_ID (it->f, id); 21908 struct image *img = IMAGE_FROM_ID (it->f, id);
20415 21909
20416 return OK_PIXELS (width_p ? img->width : img->height); 21910 return OK_PIXELS (width_p ? img->width : img->height);
@@ -20659,7 +22153,7 @@ get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
20659 22153
20660 22154
20661/* Get glyph code of character C in FONT in the two-byte form CHAR2B. 22155/* Get glyph code of character C in FONT in the two-byte form CHAR2B.
20662 Retunr 1 if FONT has a glyph for C, otherwise return 0. */ 22156 Return 1 if FONT has a glyph for C, otherwise return 0. */
20663 22157
20664static inline int 22158static inline int
20665get_char_glyph_code (int c, struct font *font, XChar2b *char2b) 22159get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
@@ -20707,6 +22201,8 @@ fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
20707 { 22201 {
20708 int c = COMPOSITION_GLYPH (s->cmp, i); 22202 int c = COMPOSITION_GLYPH (s->cmp, i);
20709 22203
22204 /* TAB in a composition means display glyphs with padding space
22205 on the left or right. */
20710 if (c != '\t') 22206 if (c != '\t')
20711 { 22207 {
20712 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c, 22208 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
@@ -20729,6 +22225,12 @@ fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
20729 } 22225 }
20730 s->cmp_to = i; 22226 s->cmp_to = i;
20731 22227
22228 if (s->face == NULL)
22229 {
22230 s->face = base_face->ascii_face;
22231 s->font = s->face->font;
22232 }
22233
20732 /* All glyph strings for the same composition has the same width, 22234 /* All glyph strings for the same composition has the same width,
20733 i.e. the width set for the first component of the composition. */ 22235 i.e. the width set for the first component of the composition. */
20734 s->width = s->first_glyph->pixel_width; 22236 s->width = s->first_glyph->pixel_width;
@@ -21282,10 +22784,10 @@ compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21282 do { \ 22784 do { \
21283 int face_id = (row)->glyphs[area][START].face_id; \ 22785 int face_id = (row)->glyphs[area][START].face_id; \
21284 struct face *base_face = FACE_FROM_ID (f, face_id); \ 22786 struct face *base_face = FACE_FROM_ID (f, face_id); \
21285 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \ 22787 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21286 struct composition *cmp = composition_table[cmp_id]; \ 22788 struct composition *cmp = composition_table[cmp_id]; \
21287 XChar2b *char2b; \ 22789 XChar2b *char2b; \
21288 struct glyph_string *first_s IF_LINT (= NULL); \ 22790 struct glyph_string *first_s = NULL; \
21289 int n; \ 22791 int n; \
21290 \ 22792 \
21291 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \ 22793 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
@@ -22071,6 +23573,7 @@ append_stretch_glyph (struct it *it, Lisp_Object object,
22071 IT_EXPAND_MATRIX_WIDTH (it, area); 23573 IT_EXPAND_MATRIX_WIDTH (it, area);
22072} 23574}
22073 23575
23576#endif /* HAVE_WINDOW_SYSTEM */
22074 23577
22075/* Produce a stretch glyph for iterator IT. IT->object is the value 23578/* Produce a stretch glyph for iterator IT. IT->object is the value
22076 of the glyph property displayed. The value must be a list 23579 of the glyph property displayed. The value must be a list
@@ -22103,19 +23606,28 @@ append_stretch_glyph (struct it *it, Lisp_Object object,
22103 of the stretch should be used for the ascent of the stretch. 23606 of the stretch should be used for the ascent of the stretch.
22104 ASCENT must be in the range 0 <= ASCENT <= 100. */ 23607 ASCENT must be in the range 0 <= ASCENT <= 100. */
22105 23608
22106static void 23609void
22107produce_stretch_glyph (struct it *it) 23610produce_stretch_glyph (struct it *it)
22108{ 23611{
22109 /* (space :width WIDTH :height HEIGHT ...) */ 23612 /* (space :width WIDTH :height HEIGHT ...) */
22110 Lisp_Object prop, plist; 23613 Lisp_Object prop, plist;
22111 int width = 0, height = 0, align_to = -1; 23614 int width = 0, height = 0, align_to = -1;
22112 int zero_width_ok_p = 0, zero_height_ok_p = 0; 23615 int zero_width_ok_p = 0;
22113 int ascent = 0; 23616 int ascent = 0;
22114 double tem; 23617 double tem;
22115 struct face *face = FACE_FROM_ID (it->f, it->face_id); 23618 struct face *face = NULL;
22116 struct font *font = face->font ? face->font : FRAME_FONT (it->f); 23619 struct font *font = NULL;
22117 23620
22118 PREPARE_FACE_FOR_DISPLAY (it->f, face); 23621#ifdef HAVE_WINDOW_SYSTEM
23622 int zero_height_ok_p = 0;
23623
23624 if (FRAME_WINDOW_P (it->f))
23625 {
23626 face = FACE_FROM_ID (it->f, it->face_id);
23627 font = face->font ? face->font : FRAME_FONT (it->f);
23628 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23629 }
23630#endif
22119 23631
22120 /* List should start with `space'. */ 23632 /* List should start with `space'. */
22121 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace)); 23633 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
@@ -22129,8 +23641,9 @@ produce_stretch_glyph (struct it *it)
22129 zero_width_ok_p = 1; 23641 zero_width_ok_p = 1;
22130 width = (int)tem; 23642 width = (int)tem;
22131 } 23643 }
22132 else if (prop = Fplist_get (plist, QCrelative_width), 23644#ifdef HAVE_WINDOW_SYSTEM
22133 NUMVAL (prop) > 0) 23645 else if (FRAME_WINDOW_P (it->f)
23646 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
22134 { 23647 {
22135 /* Relative width `:relative-width FACTOR' specified and valid. 23648 /* Relative width `:relative-width FACTOR' specified and valid.
22136 Compute the width of the characters having the `glyph' 23649 Compute the width of the characters having the `glyph'
@@ -22153,6 +23666,7 @@ produce_stretch_glyph (struct it *it)
22153 x_produce_glyphs (&it2); 23666 x_produce_glyphs (&it2);
22154 width = NUMVAL (prop) * it2.pixel_width; 23667 width = NUMVAL (prop) * it2.pixel_width;
22155 } 23668 }
23669#endif /* HAVE_WINDOW_SYSTEM */
22156 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop)) 23670 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22157 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to)) 23671 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22158 { 23672 {
@@ -22172,54 +23686,91 @@ produce_stretch_glyph (struct it *it)
22172 if (width <= 0 && (width < 0 || !zero_width_ok_p)) 23686 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22173 width = 1; 23687 width = 1;
22174 23688
23689#ifdef HAVE_WINDOW_SYSTEM
22175 /* Compute height. */ 23690 /* Compute height. */
22176 if ((prop = Fplist_get (plist, QCheight), !NILP (prop)) 23691 if (FRAME_WINDOW_P (it->f))
22177 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22178 { 23692 {
22179 height = (int)tem; 23693 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22180 zero_height_ok_p = 1; 23694 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22181 } 23695 {
22182 else if (prop = Fplist_get (plist, QCrelative_height), 23696 height = (int)tem;
22183 NUMVAL (prop) > 0) 23697 zero_height_ok_p = 1;
22184 height = FONT_HEIGHT (font) * NUMVAL (prop); 23698 }
22185 else 23699 else if (prop = Fplist_get (plist, QCrelative_height),
22186 height = FONT_HEIGHT (font); 23700 NUMVAL (prop) > 0)
23701 height = FONT_HEIGHT (font) * NUMVAL (prop);
23702 else
23703 height = FONT_HEIGHT (font);
22187 23704
22188 if (height <= 0 && (height < 0 || !zero_height_ok_p)) 23705 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22189 height = 1; 23706 height = 1;
22190 23707
22191 /* Compute percentage of height used for ascent. If 23708 /* Compute percentage of height used for ascent. If
22192 `:ascent ASCENT' is present and valid, use that. Otherwise, 23709 `:ascent ASCENT' is present and valid, use that. Otherwise,
22193 derive the ascent from the font in use. */ 23710 derive the ascent from the font in use. */
22194 if (prop = Fplist_get (plist, QCascent), 23711 if (prop = Fplist_get (plist, QCascent),
22195 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100) 23712 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22196 ascent = height * NUMVAL (prop) / 100.0; 23713 ascent = height * NUMVAL (prop) / 100.0;
22197 else if (!NILP (prop) 23714 else if (!NILP (prop)
22198 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0)) 23715 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22199 ascent = min (max (0, (int)tem), height); 23716 ascent = min (max (0, (int)tem), height);
23717 else
23718 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23719 }
22200 else 23720 else
22201 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font); 23721#endif /* HAVE_WINDOW_SYSTEM */
23722 height = 1;
22202 23723
22203 if (width > 0 && it->line_wrap != TRUNCATE 23724 if (width > 0 && it->line_wrap != TRUNCATE
22204 && it->current_x + width > it->last_visible_x) 23725 && it->current_x + width > it->last_visible_x)
22205 width = it->last_visible_x - it->current_x - 1; 23726 {
23727 width = it->last_visible_x - it->current_x;
23728#ifdef HAVE_WINDOW_SYSTEM
23729 /* Subtract one more pixel from the stretch width, but only on
23730 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23731 width -= FRAME_WINDOW_P (it->f);
23732#endif
23733 }
22206 23734
22207 if (width > 0 && height > 0 && it->glyph_row) 23735 if (width > 0 && height > 0 && it->glyph_row)
22208 { 23736 {
23737 Lisp_Object o_object = it->object;
22209 Lisp_Object object = it->stack[it->sp - 1].string; 23738 Lisp_Object object = it->stack[it->sp - 1].string;
23739 int n = width;
23740
22210 if (!STRINGP (object)) 23741 if (!STRINGP (object))
22211 object = it->w->buffer; 23742 object = it->w->buffer;
22212 append_stretch_glyph (it, object, width, height, ascent); 23743#ifdef HAVE_WINDOW_SYSTEM
23744 if (FRAME_WINDOW_P (it->f))
23745 append_stretch_glyph (it, object, width, height, ascent);
23746 else
23747#endif
23748 {
23749 it->object = object;
23750 it->char_to_display = ' ';
23751 it->pixel_width = it->len = 1;
23752 while (n--)
23753 tty_append_glyph (it);
23754 it->object = o_object;
23755 }
22213 } 23756 }
22214 23757
22215 it->pixel_width = width; 23758 it->pixel_width = width;
22216 it->ascent = it->phys_ascent = ascent; 23759#ifdef HAVE_WINDOW_SYSTEM
22217 it->descent = it->phys_descent = height - it->ascent; 23760 if (FRAME_WINDOW_P (it->f))
22218 it->nglyphs = width > 0 && height > 0 ? 1 : 0; 23761 {
22219 23762 it->ascent = it->phys_ascent = ascent;
22220 take_vertical_position_into_account (it); 23763 it->descent = it->phys_descent = height - it->ascent;
23764 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23765 take_vertical_position_into_account (it);
23766 }
23767 else
23768#endif
23769 it->nglyphs = width;
22221} 23770}
22222 23771
23772#ifdef HAVE_WINDOW_SYSTEM
23773
22223/* Calculate line-height and line-spacing properties. 23774/* Calculate line-height and line-spacing properties.
22224 An integer value specifies explicit pixel value. 23775 An integer value specifies explicit pixel value.
22225 A float value specifies relative value to current face height. 23776 A float value specifies relative value to current face height.
@@ -22403,7 +23954,7 @@ produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
22403 base_width = font->average_width; 23954 base_width = font->average_width;
22404 23955
22405 /* Get a face ID for the glyph by utilizing a cache (the same way as 23956 /* Get a face ID for the glyph by utilizing a cache (the same way as
22406 doen for `escape-glyph' in get_next_display_element). */ 23957 done for `escape-glyph' in get_next_display_element). */
22407 if (it->f == last_glyphless_glyph_frame 23958 if (it->f == last_glyphless_glyph_frame
22408 && it->face_id == last_glyphless_glyph_face_id) 23959 && it->face_id == last_glyphless_glyph_face_id)
22409 { 23960 {
@@ -22898,7 +24449,7 @@ x_produce_glyphs (struct it *it)
22898 /* Initialize the bounding box. */ 24449 /* Initialize the bounding box. */
22899 if (pcm) 24450 if (pcm)
22900 { 24451 {
22901 width = pcm->width; 24452 width = cmp->glyph_len > 0 ? pcm->width : 0;
22902 ascent = pcm->ascent; 24453 ascent = pcm->ascent;
22903 descent = pcm->descent; 24454 descent = pcm->descent;
22904 lbearing = pcm->lbearing; 24455 lbearing = pcm->lbearing;
@@ -22906,7 +24457,7 @@ x_produce_glyphs (struct it *it)
22906 } 24457 }
22907 else 24458 else
22908 { 24459 {
22909 width = font->space_width; 24460 width = cmp->glyph_len > 0 ? font->space_width : 0;
22910 ascent = FONT_BASE (font); 24461 ascent = FONT_BASE (font);
22911 descent = FONT_DESCENT (font); 24462 descent = FONT_DESCENT (font);
22912 lbearing = 0; 24463 lbearing = 0;
@@ -23124,7 +24675,7 @@ x_produce_glyphs (struct it *it)
23124 if (it->descent < 0) 24675 if (it->descent < 0)
23125 it->descent = 0; 24676 it->descent = 0;
23126 24677
23127 if (it->glyph_row) 24678 if (it->glyph_row && cmp->glyph_len > 0)
23128 append_composite_glyph (it); 24679 append_composite_glyph (it);
23129 } 24680 }
23130 else if (it->what == IT_COMPOSITION) 24681 else if (it->what == IT_COMPOSITION)
@@ -23134,6 +24685,8 @@ x_produce_glyphs (struct it *it)
23134 Lisp_Object gstring; 24685 Lisp_Object gstring;
23135 struct font_metrics metrics; 24686 struct font_metrics metrics;
23136 24687
24688 it->nglyphs = 1;
24689
23137 gstring = composition_gstring_from_id (it->cmp_it.id); 24690 gstring = composition_gstring_from_id (it->cmp_it.id);
23138 it->pixel_width 24691 it->pixel_width
23139 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to, 24692 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
@@ -23210,9 +24763,17 @@ x_produce_glyphs (struct it *it)
23210void 24763void
23211x_write_glyphs (struct glyph *start, int len) 24764x_write_glyphs (struct glyph *start, int len)
23212{ 24765{
23213 int x, hpos; 24766 int x, hpos, chpos = updated_window->phys_cursor.hpos;
23214 24767
23215 xassert (updated_window && updated_row); 24768 xassert (updated_window && updated_row);
24769 /* When the window is hscrolled, cursor hpos can legitimately be out
24770 of bounds, but we draw the cursor at the corresponding window
24771 margin in that case. */
24772 if (!updated_row->reversed_p && chpos < 0)
24773 chpos = 0;
24774 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
24775 chpos = updated_row->used[TEXT_AREA] - 1;
24776
23216 BLOCK_INPUT; 24777 BLOCK_INPUT;
23217 24778
23218 /* Write glyphs. */ 24779 /* Write glyphs. */
@@ -23227,8 +24788,8 @@ x_write_glyphs (struct glyph *start, int len)
23227 if (updated_area == TEXT_AREA 24788 if (updated_area == TEXT_AREA
23228 && updated_window->phys_cursor_on_p 24789 && updated_window->phys_cursor_on_p
23229 && updated_window->phys_cursor.vpos == output_cursor.vpos 24790 && updated_window->phys_cursor.vpos == output_cursor.vpos
23230 && updated_window->phys_cursor.hpos >= hpos 24791 && chpos >= hpos
23231 && updated_window->phys_cursor.hpos < hpos + len) 24792 && chpos < hpos + len)
23232 updated_window->phys_cursor_on_p = 0; 24793 updated_window->phys_cursor_on_p = 0;
23233 24794
23234 UNBLOCK_INPUT; 24795 UNBLOCK_INPUT;
@@ -23738,8 +25299,17 @@ draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
23738 { 25299 {
23739 int on_p = w->phys_cursor_on_p; 25300 int on_p = w->phys_cursor_on_p;
23740 int x1; 25301 int x1;
23741 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, 25302 int hpos = w->phys_cursor.hpos;
23742 w->phys_cursor.hpos, w->phys_cursor.hpos + 1, 25303
25304 /* When the window is hscrolled, cursor hpos can legitimately be
25305 out of bounds, but we draw the cursor at the corresponding
25306 window margin in that case. */
25307 if (!row->reversed_p && hpos < 0)
25308 hpos = 0;
25309 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25310 hpos = row->used[TEXT_AREA] - 1;
25311
25312 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
23743 hl, 0); 25313 hl, 0);
23744 w->phys_cursor_on_p = on_p; 25314 w->phys_cursor_on_p = on_p;
23745 25315
@@ -23827,6 +25397,14 @@ erase_phys_cursor (struct window *w)
23827 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA]))) 25397 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
23828 goto mark_cursor_off; 25398 goto mark_cursor_off;
23829 25399
25400 /* When the window is hscrolled, cursor hpos can legitimately be out
25401 of bounds, but we draw the cursor at the corresponding window
25402 margin in that case. */
25403 if (!cursor_row->reversed_p && hpos < 0)
25404 hpos = 0;
25405 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25406 hpos = cursor_row->used[TEXT_AREA] - 1;
25407
23830 /* If the cursor is in the mouse face area, redisplay that when 25408 /* If the cursor is in the mouse face area, redisplay that when
23831 we clear the cursor. */ 25409 we clear the cursor. */
23832 if (! NILP (hlinfo->mouse_face_window) 25410 if (! NILP (hlinfo->mouse_face_window)
@@ -23970,8 +25548,26 @@ update_window_cursor (struct window *w, int on)
23970 of being deleted. */ 25548 of being deleted. */
23971 if (w->current_matrix) 25549 if (w->current_matrix)
23972 { 25550 {
25551 int hpos = w->phys_cursor.hpos;
25552 int vpos = w->phys_cursor.vpos;
25553 struct glyph_row *row;
25554
25555 if (vpos >= w->current_matrix->nrows
25556 || hpos >= w->current_matrix->matrix_w)
25557 return;
25558
25559 row = MATRIX_ROW (w->current_matrix, vpos);
25560
25561 /* When the window is hscrolled, cursor hpos can legitimately be
25562 out of bounds, but we draw the cursor at the corresponding
25563 window margin in that case. */
25564 if (!row->reversed_p && hpos < 0)
25565 hpos = 0;
25566 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25567 hpos = row->used[TEXT_AREA] - 1;
25568
23973 BLOCK_INPUT; 25569 BLOCK_INPUT;
23974 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos, 25570 display_and_set_cursor (w, on, hpos, vpos,
23975 w->phys_cursor.x, w->phys_cursor.y); 25571 w->phys_cursor.x, w->phys_cursor.y);
23976 UNBLOCK_INPUT; 25572 UNBLOCK_INPUT;
23977 } 25573 }
@@ -24141,9 +25737,18 @@ show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24141 if (FRAME_WINDOW_P (f) 25737 if (FRAME_WINDOW_P (f)
24142 && phys_cursor_on_p && !w->phys_cursor_on_p) 25738 && phys_cursor_on_p && !w->phys_cursor_on_p)
24143 { 25739 {
25740 int hpos = w->phys_cursor.hpos;
25741
25742 /* When the window is hscrolled, cursor hpos can legitimately be
25743 out of bounds, but we draw the cursor at the corresponding
25744 window margin in that case. */
25745 if (!row->reversed_p && hpos < 0)
25746 hpos = 0;
25747 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25748 hpos = row->used[TEXT_AREA] - 1;
25749
24144 BLOCK_INPUT; 25750 BLOCK_INPUT;
24145 display_and_set_cursor (w, 1, 25751 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
24146 w->phys_cursor.hpos, w->phys_cursor.vpos,
24147 w->phys_cursor.x, w->phys_cursor.y); 25752 w->phys_cursor.x, w->phys_cursor.y);
24148 UNBLOCK_INPUT; 25753 UNBLOCK_INPUT;
24149 } 25754 }
@@ -24242,19 +25847,33 @@ coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24242int 25847int
24243cursor_in_mouse_face_p (struct window *w) 25848cursor_in_mouse_face_p (struct window *w)
24244{ 25849{
24245 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos); 25850 int hpos = w->phys_cursor.hpos;
25851 int vpos = w->phys_cursor.vpos;
25852 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
25853
25854 /* When the window is hscrolled, cursor hpos can legitimately be out
25855 of bounds, but we draw the cursor at the corresponding window
25856 margin in that case. */
25857 if (!row->reversed_p && hpos < 0)
25858 hpos = 0;
25859 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25860 hpos = row->used[TEXT_AREA] - 1;
25861
25862 return coords_in_mouse_face_p (w, hpos, vpos);
24246} 25863}
24247 25864
24248 25865
24249 25866
24250/* Find the glyph rows START_ROW and END_ROW of window W that display 25867/* Find the glyph rows START_ROW and END_ROW of window W that display
24251 characters between buffer positions START_CHARPOS and END_CHARPOS 25868 characters between buffer positions START_CHARPOS and END_CHARPOS
24252 (excluding END_CHARPOS). This is similar to row_containing_pos, 25869 (excluding END_CHARPOS). DISP_STRING is a display string that
24253 but is more accurate when bidi reordering makes buffer positions 25870 covers these buffer positions. This is similar to
24254 change non-linearly with glyph rows. */ 25871 row_containing_pos, but is more accurate when bidi reordering makes
25872 buffer positions change non-linearly with glyph rows. */
24255static void 25873static void
24256rows_from_pos_range (struct window *w, 25874rows_from_pos_range (struct window *w,
24257 EMACS_INT start_charpos, EMACS_INT end_charpos, 25875 EMACS_INT start_charpos, EMACS_INT end_charpos,
25876 Lisp_Object disp_string,
24258 struct glyph_row **start, struct glyph_row **end) 25877 struct glyph_row **start, struct glyph_row **end)
24259{ 25878{
24260 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix); 25879 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
@@ -24306,8 +25925,11 @@ rows_from_pos_range (struct window *w,
24306 25925
24307 while (g < e) 25926 while (g < e)
24308 { 25927 {
24309 if (BUFFERP (g->object) 25928 if (((BUFFERP (g->object) || INTEGERP (g->object))
24310 && start_charpos <= g->charpos && g->charpos < end_charpos) 25929 && start_charpos <= g->charpos && g->charpos < end_charpos)
25930 /* A glyph that comes from DISP_STRING is by
25931 definition to be highlighted. */
25932 || EQ (g->object, disp_string))
24311 *start = row; 25933 *start = row;
24312 g++; 25934 g++;
24313 } 25935 }
@@ -24326,14 +25948,15 @@ rows_from_pos_range (struct window *w,
24326 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++) 25948 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
24327 { 25949 {
24328 struct glyph_row *next = row + 1; 25950 struct glyph_row *next = row + 1;
25951 EMACS_INT next_start = MATRIX_ROW_START_CHARPOS (next);
24329 25952
24330 if (!next->enabled_p 25953 if (!next->enabled_p
24331 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w) 25954 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
24332 /* The first row >= START whose range of displayed characters 25955 /* The first row >= START whose range of displayed characters
24333 does NOT intersect the range [START_CHARPOS..END_CHARPOS] 25956 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
24334 is the row END + 1. */ 25957 is the row END + 1. */
24335 || (start_charpos < MATRIX_ROW_START_CHARPOS (next) 25958 || (start_charpos < next_start
24336 && end_charpos < MATRIX_ROW_START_CHARPOS (next)) 25959 && end_charpos < next_start)
24337 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next) 25960 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
24338 || (start_charpos == MATRIX_ROW_END_CHARPOS (next) 25961 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
24339 && !next->ends_at_zv_p 25962 && !next->ends_at_zv_p
@@ -24352,12 +25975,29 @@ rows_from_pos_range (struct window *w,
24352 but none of the characters it displays are in the range, it is 25975 but none of the characters it displays are in the range, it is
24353 also END + 1. */ 25976 also END + 1. */
24354 struct glyph *g = next->glyphs[TEXT_AREA]; 25977 struct glyph *g = next->glyphs[TEXT_AREA];
25978 struct glyph *s = g;
24355 struct glyph *e = g + next->used[TEXT_AREA]; 25979 struct glyph *e = g + next->used[TEXT_AREA];
24356 25980
24357 while (g < e) 25981 while (g < e)
24358 { 25982 {
24359 if (BUFFERP (g->object) 25983 if (((BUFFERP (g->object) || INTEGERP (g->object))
24360 && start_charpos <= g->charpos && g->charpos < end_charpos) 25984 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
25985 /* If the buffer position of the first glyph in
25986 the row is equal to END_CHARPOS, it means
25987 the last character to be highlighted is the
25988 newline of ROW, and we must consider NEXT as
25989 END, not END+1. */
25990 || (((!next->reversed_p && g == s)
25991 || (next->reversed_p && g == e - 1))
25992 && (g->charpos == end_charpos
25993 /* Special case for when NEXT is an
25994 empty line at ZV. */
25995 || (g->charpos == -1
25996 && !row->ends_at_zv_p
25997 && next_start == end_charpos)))))
25998 /* A glyph that comes from DISP_STRING is by
25999 definition to be highlighted. */
26000 || EQ (g->object, disp_string))
24361 break; 26001 break;
24362 g++; 26002 g++;
24363 } 26003 }
@@ -24366,6 +26006,13 @@ rows_from_pos_range (struct window *w,
24366 *end = row; 26006 *end = row;
24367 break; 26007 break;
24368 } 26008 }
26009 /* The first row that ends at ZV must be the last to be
26010 highlighted. */
26011 else if (next->ends_at_zv_p)
26012 {
26013 *end = next;
26014 break;
26015 }
24369 } 26016 }
24370 } 26017 }
24371} 26018}
@@ -24376,7 +26023,7 @@ rows_from_pos_range (struct window *w,
24376 for the overlay or run of text properties specifying the mouse 26023 for the overlay or run of text properties specifying the mouse
24377 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a 26024 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
24378 before-string and after-string that must also be highlighted. 26025 before-string and after-string that must also be highlighted.
24379 COVER_STRING, if non-nil, is a display string that may cover some 26026 DISP_STRING, if non-nil, is a display string that may cover some
24380 or all of the highlighted text. */ 26027 or all of the highlighted text. */
24381 26028
24382static void 26029static void
@@ -24387,7 +26034,7 @@ mouse_face_from_buffer_pos (Lisp_Object window,
24387 EMACS_INT end_charpos, 26034 EMACS_INT end_charpos,
24388 Lisp_Object before_string, 26035 Lisp_Object before_string,
24389 Lisp_Object after_string, 26036 Lisp_Object after_string,
24390 Lisp_Object cover_string) 26037 Lisp_Object disp_string)
24391{ 26038{
24392 struct window *w = XWINDOW (window); 26039 struct window *w = XWINDOW (window);
24393 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix); 26040 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
@@ -24396,17 +26043,17 @@ mouse_face_from_buffer_pos (Lisp_Object window,
24396 EMACS_INT ignore, pos; 26043 EMACS_INT ignore, pos;
24397 int x; 26044 int x;
24398 26045
24399 xassert (NILP (cover_string) || STRINGP (cover_string)); 26046 xassert (NILP (disp_string) || STRINGP (disp_string));
24400 xassert (NILP (before_string) || STRINGP (before_string)); 26047 xassert (NILP (before_string) || STRINGP (before_string));
24401 xassert (NILP (after_string) || STRINGP (after_string)); 26048 xassert (NILP (after_string) || STRINGP (after_string));
24402 26049
24403 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */ 26050 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
24404 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2); 26051 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
24405 if (r1 == NULL) 26052 if (r1 == NULL)
24406 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos)); 26053 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24407 /* If the before-string or display-string contains newlines, 26054 /* If the before-string or display-string contains newlines,
24408 rows_from_pos_range skips to its last row. Move back. */ 26055 rows_from_pos_range skips to its last row. Move back. */
24409 if (!NILP (before_string) || !NILP (cover_string)) 26056 if (!NILP (before_string) || !NILP (disp_string))
24410 { 26057 {
24411 struct glyph_row *prev; 26058 struct glyph_row *prev;
24412 while ((prev = r1 - 1, prev >= first) 26059 while ((prev = r1 - 1, prev >= first)
@@ -24418,7 +26065,7 @@ mouse_face_from_buffer_pos (Lisp_Object window,
24418 while (--glyph >= beg && INTEGERP (glyph->object)); 26065 while (--glyph >= beg && INTEGERP (glyph->object));
24419 if (glyph < beg 26066 if (glyph < beg
24420 || !(EQ (glyph->object, before_string) 26067 || !(EQ (glyph->object, before_string)
24421 || EQ (glyph->object, cover_string))) 26068 || EQ (glyph->object, disp_string)))
24422 break; 26069 break;
24423 r1 = prev; 26070 r1 = prev;
24424 } 26071 }
@@ -24443,10 +26090,10 @@ mouse_face_from_buffer_pos (Lisp_Object window,
24443 r2 = next; 26090 r2 = next;
24444 } 26091 }
24445 /* The rest of the display engine assumes that mouse_face_beg_row is 26092 /* The rest of the display engine assumes that mouse_face_beg_row is
24446 either above below mouse_face_end_row or identical to it. But 26093 either above mouse_face_end_row or identical to it. But with
24447 with bidi-reordered continued lines, the row for START_CHARPOS 26094 bidi-reordered continued lines, the row for START_CHARPOS could
24448 could be below the row for END_CHARPOS. If so, swap the rows and 26095 be below the row for END_CHARPOS. If so, swap the rows and store
24449 store them in correct order. */ 26096 them in correct order. */
24450 if (r1->y > r2->y) 26097 if (r1->y > r2->y)
24451 { 26098 {
24452 struct glyph_row *tem = r2; 26099 struct glyph_row *tem = r2;
@@ -24461,7 +26108,7 @@ mouse_face_from_buffer_pos (Lisp_Object window,
24461 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix); 26108 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
24462 26109
24463 /* For a bidi-reordered row, the positions of BEFORE_STRING, 26110 /* For a bidi-reordered row, the positions of BEFORE_STRING,
24464 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS 26111 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
24465 could be anywhere in the row and in any order. The strategy 26112 could be anywhere in the row and in any order. The strategy
24466 below is to find the leftmost and the rightmost glyph that 26113 below is to find the leftmost and the rightmost glyph that
24467 belongs to either of these 3 strings, or whose position is 26114 belongs to either of these 3 strings, or whose position is
@@ -24487,11 +26134,11 @@ mouse_face_from_buffer_pos (Lisp_Object window,
24487 x += glyph->pixel_width; 26134 x += glyph->pixel_width;
24488 26135
24489 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING, 26136 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24490 or COVER_STRING, and the first glyph from buffer whose 26137 or DISP_STRING, and the first glyph from buffer whose
24491 position is between START_CHARPOS and END_CHARPOS. */ 26138 position is between START_CHARPOS and END_CHARPOS. */
24492 for (; glyph < end 26139 for (; glyph < end
24493 && !INTEGERP (glyph->object) 26140 && !INTEGERP (glyph->object)
24494 && !EQ (glyph->object, cover_string) 26141 && !EQ (glyph->object, disp_string)
24495 && !(BUFFERP (glyph->object) 26142 && !(BUFFERP (glyph->object)
24496 && (glyph->charpos >= start_charpos 26143 && (glyph->charpos >= start_charpos
24497 && glyph->charpos < end_charpos)); 26144 && glyph->charpos < end_charpos));
@@ -24538,11 +26185,11 @@ mouse_face_from_buffer_pos (Lisp_Object window,
24538 ; 26185 ;
24539 26186
24540 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING, 26187 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24541 or COVER_STRING, and the first glyph from buffer whose 26188 or DISP_STRING, and the first glyph from buffer whose
24542 position is between START_CHARPOS and END_CHARPOS. */ 26189 position is between START_CHARPOS and END_CHARPOS. */
24543 for (; glyph > end 26190 for (; glyph > end
24544 && !INTEGERP (glyph->object) 26191 && !INTEGERP (glyph->object)
24545 && !EQ (glyph->object, cover_string) 26192 && !EQ (glyph->object, disp_string)
24546 && !(BUFFERP (glyph->object) 26193 && !(BUFFERP (glyph->object)
24547 && (glyph->charpos >= start_charpos 26194 && (glyph->charpos >= start_charpos
24548 && glyph->charpos < end_charpos)); 26195 && glyph->charpos < end_charpos));
@@ -24598,17 +26245,16 @@ mouse_face_from_buffer_pos (Lisp_Object window,
24598 row, and also blanks and stretch glyphs inserted by 26245 row, and also blanks and stretch glyphs inserted by
24599 extend_face_to_end_of_line. */ 26246 extend_face_to_end_of_line. */
24600 while (end > glyph 26247 while (end > glyph
24601 && INTEGERP ((end - 1)->object) 26248 && INTEGERP ((end - 1)->object))
24602 && (end - 1)->charpos <= 0)
24603 --end; 26249 --end;
24604 /* Scan the rest of the glyph row from the end, looking for the 26250 /* Scan the rest of the glyph row from the end, looking for the
24605 first glyph that comes from BEFORE_STRING, AFTER_STRING, or 26251 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24606 COVER_STRING, or whose position is between START_CHARPOS 26252 DISP_STRING, or whose position is between START_CHARPOS
24607 and END_CHARPOS */ 26253 and END_CHARPOS */
24608 for (--end; 26254 for (--end;
24609 end > glyph 26255 end > glyph
24610 && !INTEGERP (end->object) 26256 && !INTEGERP (end->object)
24611 && !EQ (end->object, cover_string) 26257 && !EQ (end->object, disp_string)
24612 && !(BUFFERP (end->object) 26258 && !(BUFFERP (end->object)
24613 && (end->charpos >= start_charpos 26259 && (end->charpos >= start_charpos
24614 && end->charpos < end_charpos)); 26260 && end->charpos < end_charpos));
@@ -24645,20 +26291,19 @@ mouse_face_from_buffer_pos (Lisp_Object window,
24645 x = r2->x; 26291 x = r2->x;
24646 end++; 26292 end++;
24647 while (end < glyph 26293 while (end < glyph
24648 && INTEGERP (end->object) 26294 && INTEGERP (end->object))
24649 && end->charpos <= 0)
24650 { 26295 {
24651 x += end->pixel_width; 26296 x += end->pixel_width;
24652 ++end; 26297 ++end;
24653 } 26298 }
24654 /* Scan the rest of the glyph row from the end, looking for the 26299 /* Scan the rest of the glyph row from the end, looking for the
24655 first glyph that comes from BEFORE_STRING, AFTER_STRING, or 26300 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24656 COVER_STRING, or whose position is between START_CHARPOS 26301 DISP_STRING, or whose position is between START_CHARPOS
24657 and END_CHARPOS */ 26302 and END_CHARPOS */
24658 for ( ; 26303 for ( ;
24659 end < glyph 26304 end < glyph
24660 && !INTEGERP (end->object) 26305 && !INTEGERP (end->object)
24661 && !EQ (end->object, cover_string) 26306 && !EQ (end->object, disp_string)
24662 && !(BUFFERP (end->object) 26307 && !(BUFFERP (end->object)
24663 && (end->charpos >= start_charpos 26308 && (end->charpos >= start_charpos
24664 && end->charpos < end_charpos)); 26309 && end->charpos < end_charpos));
@@ -24681,6 +26326,19 @@ mouse_face_from_buffer_pos (Lisp_Object window,
24681 } 26326 }
24682 x += end->pixel_width; 26327 x += end->pixel_width;
24683 } 26328 }
26329 /* If we exited the above loop because we arrived at the last
26330 glyph of the row, and its buffer position is still not in
26331 range, it means the last character in range is the preceding
26332 newline. Bump the end column and x values to get past the
26333 last glyph. */
26334 if (end == glyph
26335 && BUFFERP (end->object)
26336 && (end->charpos < start_charpos
26337 || end->charpos >= end_charpos))
26338 {
26339 x += end->pixel_width;
26340 ++end;
26341 }
24684 hlinfo->mouse_face_end_x = x; 26342 hlinfo->mouse_face_end_x = x;
24685 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA]; 26343 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
24686 } 26344 }
@@ -25368,7 +27026,7 @@ void
25368note_mouse_highlight (struct frame *f, int x, int y) 27026note_mouse_highlight (struct frame *f, int x, int y)
25369{ 27027{
25370 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f); 27028 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25371 enum window_part part; 27029 enum window_part part = ON_NOTHING;
25372 Lisp_Object window; 27030 Lisp_Object window;
25373 struct window *w; 27031 struct window *w;
25374 Cursor cursor = No_Cursor; 27032 Cursor cursor = No_Cursor;
@@ -25402,11 +27060,14 @@ note_mouse_highlight (struct frame *f, int x, int y)
25402 /* Which window is that in? */ 27060 /* Which window is that in? */
25403 window = window_from_coordinates (f, x, y, &part, 1); 27061 window = window_from_coordinates (f, x, y, &part, 1);
25404 27062
25405 /* If we were displaying active text in another window, clear that. 27063 /* If displaying active text in another window, clear that. */
25406 Also clear if we move out of text area in same window. */
25407 if (! EQ (window, hlinfo->mouse_face_window) 27064 if (! EQ (window, hlinfo->mouse_face_window)
25408 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE 27065 /* Also clear if we move out of text area in same window. */
25409 && !NILP (hlinfo->mouse_face_window))) 27066 || (!NILP (hlinfo->mouse_face_window)
27067 && !NILP (window)
27068 && part != ON_TEXT
27069 && part != ON_MODE_LINE
27070 && part != ON_HEADER_LINE))
25410 clear_mouse_face (hlinfo); 27071 clear_mouse_face (hlinfo);
25411 27072
25412 /* Not on a window -> return. */ 27073 /* Not on a window -> return. */
@@ -25459,7 +27120,7 @@ note_mouse_highlight (struct frame *f, int x, int y)
25459 && XFASTINT (w->last_modified) == BUF_MODIFF (b) 27120 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
25460 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b)) 27121 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
25461 { 27122 {
25462 int hpos, vpos, dx, dy, area; 27123 int hpos, vpos, dx, dy, area = LAST_AREA;
25463 EMACS_INT pos; 27124 EMACS_INT pos;
25464 struct glyph *glyph; 27125 struct glyph *glyph;
25465 Lisp_Object object; 27126 Lisp_Object object;
@@ -25650,7 +27311,7 @@ note_mouse_highlight (struct frame *f, int x, int y)
25650 /* The mouse-highlighting, if any, comes from an overlay 27311 /* The mouse-highlighting, if any, comes from an overlay
25651 or text property in the buffer. */ 27312 or text property in the buffer. */
25652 Lisp_Object buffer IF_LINT (= Qnil); 27313 Lisp_Object buffer IF_LINT (= Qnil);
25653 Lisp_Object cover_string IF_LINT (= Qnil); 27314 Lisp_Object disp_string IF_LINT (= Qnil);
25654 27315
25655 if (STRINGP (object)) 27316 if (STRINGP (object))
25656 { 27317 {
@@ -25664,13 +27325,13 @@ note_mouse_highlight (struct frame *f, int x, int y)
25664 mouse_face = get_char_property_and_overlay 27325 mouse_face = get_char_property_and_overlay
25665 (make_number (pos), Qmouse_face, w->buffer, &overlay); 27326 (make_number (pos), Qmouse_face, w->buffer, &overlay);
25666 buffer = w->buffer; 27327 buffer = w->buffer;
25667 cover_string = object; 27328 disp_string = object;
25668 } 27329 }
25669 } 27330 }
25670 else 27331 else
25671 { 27332 {
25672 buffer = object; 27333 buffer = object;
25673 cover_string = Qnil; 27334 disp_string = Qnil;
25674 } 27335 }
25675 27336
25676 if (!NILP (mouse_face)) 27337 if (!NILP (mouse_face))
@@ -25719,10 +27380,14 @@ note_mouse_highlight (struct frame *f, int x, int y)
25719 } 27380 }
25720 27381
25721 mouse_face_from_buffer_pos (window, hlinfo, pos, 27382 mouse_face_from_buffer_pos (window, hlinfo, pos,
25722 XFASTINT (before), 27383 NILP (before)
25723 XFASTINT (after), 27384 ? 1
27385 : XFASTINT (before),
27386 NILP (after)
27387 ? BUF_Z (XBUFFER (buffer))
27388 : XFASTINT (after),
25724 before_string, after_string, 27389 before_string, after_string,
25725 cover_string); 27390 disp_string);
25726 cursor = No_Cursor; 27391 cursor = No_Cursor;
25727 } 27392 }
25728 } 27393 }
@@ -26151,7 +27816,7 @@ expose_window (struct window *w, XRectangle *fr)
26151 { 27816 {
26152 int yb = window_text_bottom_y (w); 27817 int yb = window_text_bottom_y (w);
26153 struct glyph_row *row; 27818 struct glyph_row *row;
26154 int cursor_cleared_p; 27819 int cursor_cleared_p, phys_cursor_on_p;
26155 struct glyph_row *first_overlapping_row, *last_overlapping_row; 27820 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26156 27821
26157 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n", 27822 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
@@ -26171,6 +27836,13 @@ expose_window (struct window *w, XRectangle *fr)
26171 else 27836 else
26172 cursor_cleared_p = 0; 27837 cursor_cleared_p = 0;
26173 27838
27839 /* If the row containing the cursor extends face to end of line,
27840 then expose_area might overwrite the cursor outside the
27841 rectangle and thus notice_overwritten_cursor might clear
27842 w->phys_cursor_on_p. We remember the original value and
27843 check later if it is changed. */
27844 phys_cursor_on_p = w->phys_cursor_on_p;
27845
26174 /* Update lines intersecting rectangle R. */ 27846 /* Update lines intersecting rectangle R. */
26175 first_overlapping_row = last_overlapping_row = NULL; 27847 first_overlapping_row = last_overlapping_row = NULL;
26176 for (row = w->current_matrix->rows; 27848 for (row = w->current_matrix->rows;
@@ -26237,7 +27909,8 @@ expose_window (struct window *w, XRectangle *fr)
26237 x_draw_vertical_border (w); 27909 x_draw_vertical_border (w);
26238 27910
26239 /* Turn the cursor on again. */ 27911 /* Turn the cursor on again. */
26240 if (cursor_cleared_p) 27912 if (cursor_cleared_p
27913 || (phys_cursor_on_p && !w->phys_cursor_on_p))
26241 update_window_cursor (w, 1); 27914 update_window_cursor (w, 1);
26242 } 27915 }
26243 } 27916 }
@@ -26512,7 +28185,6 @@ syms_of_xdisp (void)
26512 DEFSYM (Qhollow, "hollow"); 28185 DEFSYM (Qhollow, "hollow");
26513 DEFSYM (Qhand, "hand"); 28186 DEFSYM (Qhand, "hand");
26514 DEFSYM (Qarrow, "arrow"); 28187 DEFSYM (Qarrow, "arrow");
26515 DEFSYM (Qtext, "text");
26516 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces"); 28188 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
26517 28189
26518 list_of_error = Fcons (Fcons (intern_c_string ("error"), 28190 list_of_error = Fcons (Fcons (intern_c_string ("error"),
@@ -26574,12 +28246,18 @@ The face used for trailing whitespace is `trailing-whitespace'. */);
26574 Vshow_trailing_whitespace = Qnil; 28246 Vshow_trailing_whitespace = Qnil;
26575 28247
26576 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display, 28248 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
26577 doc: /* *Control highlighting of nobreak space and soft hyphen. 28249 doc: /* Control highlighting of non-ASCII space and hyphen chars.
26578A value of t means highlight the character itself (for nobreak space, 28250If the value is t, Emacs highlights non-ASCII chars which have the
26579use face `nobreak-space'). 28251same appearance as an ASCII space or hyphen, using the `nobreak-space'
26580A value of nil means no highlighting. 28252or `escape-glyph' face respectively.
26581Other values mean display the escape glyph followed by an ordinary 28253
26582space or ordinary hyphen. */); 28254U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28255U+2011 (non-breaking hyphen) are affected.
28256
28257Any other non-nil value means to display these characters as a escape
28258glyph followed by an ordinary space or hyphen.
28259
28260A value of nil means no special handling of these characters. */);
26583 Vnobreak_char_display = Qt; 28261 Vnobreak_char_display = Qt;
26584 28262
26585 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer, 28263 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
@@ -26739,7 +28417,11 @@ all the functions in the list are called, with the frame as argument. */);
26739Each function is called with two arguments, the window and its new 28417Each function is called with two arguments, the window and its new
26740display-start position. Note that these functions are also called by 28418display-start position. Note that these functions are also called by
26741`set-window-buffer'. Also note that the value of `window-end' is not 28419`set-window-buffer'. Also note that the value of `window-end' is not
26742valid when these functions are called. */); 28420valid when these functions are called.
28421
28422Warning: Do not use this feature to alter the way the window
28423is scrolled. It is not designed for that, and such use probably won't
28424work. */);
26743 Vwindow_scroll_functions = Qnil; 28425 Vwindow_scroll_functions = Qnil;
26744 28426
26745 DEFVAR_LISP ("window-text-change-functions", 28427 DEFVAR_LISP ("window-text-change-functions",
@@ -26847,18 +28529,18 @@ but does not change the fact they are interpreted as raw bytes. */);
26847 unibyte_display_via_language_environment = 0; 28529 unibyte_display_via_language_environment = 0;
26848 28530
26849 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height, 28531 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
26850 doc: /* *Maximum height for resizing mini-windows. 28532 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
26851If a float, it specifies a fraction of the mini-window frame's height. 28533If a float, it specifies a fraction of the mini-window frame's height.
26852If an integer, it specifies a number of lines. */); 28534If an integer, it specifies a number of lines. */);
26853 Vmax_mini_window_height = make_float (0.25); 28535 Vmax_mini_window_height = make_float (0.25);
26854 28536
26855 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows, 28537 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
26856 doc: /* *How to resize mini-windows. 28538 doc: /* How to resize mini-windows (the minibuffer and the echo area).
26857A value of nil means don't automatically resize mini-windows. 28539A value of nil means don't automatically resize mini-windows.
26858A value of t means resize them to fit the text displayed in them. 28540A value of t means resize them to fit the text displayed in them.
26859A value of `grow-only', the default, means let mini-windows grow 28541A value of `grow-only', the default, means let mini-windows grow only;
26860only, until their display becomes empty, at which point the windows 28542they return to their normal size when the minibuffer is closed, or the
26861go back to their normal size. */); 28543echo area becomes empty. */);
26862 Vresize_mini_windows = Qgrow_only; 28544 Vresize_mini_windows = Qgrow_only;
26863 28545
26864 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist, 28546 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
@@ -26975,7 +28657,7 @@ To add a prefix to continuation lines, use `wrap-prefix'. */);
26975 DEFVAR_INT ("overline-margin", overline_margin, 28657 DEFVAR_INT ("overline-margin", overline_margin,
26976 doc: /* *Space between overline and text, in pixels. 28658 doc: /* *Space between overline and text, in pixels.
26977The default value is 2: the height of the overline (1 pixel) plus 1 pixel 28659The default value is 2: the height of the overline (1 pixel) plus 1 pixel
26978margin to the caracter height. */); 28660margin to the character height. */);
26979 overline_margin = 2; 28661 overline_margin = 2;
26980 28662
26981 DEFVAR_INT ("underline-minimum-offset", 28663 DEFVAR_INT ("underline-minimum-offset",
@@ -27090,7 +28772,8 @@ init_xdisp (void)
27090 28772
27091/* Platform-independent portion of hourglass implementation. */ 28773/* Platform-independent portion of hourglass implementation. */
27092 28774
27093/* Return non-zero if houglass timer has been started or hourglass is shown. */ 28775/* Return non-zero if hourglass timer has been started or hourglass is
28776 shown. */
27094int 28777int
27095hourglass_started (void) 28778hourglass_started (void)
27096{ 28779{