]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merge from origin/emacs-24
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2015 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
104
105 . try_window
106
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
112
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
117
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
130
131 Desired matrices.
132
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
139
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
146
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
156
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
163
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
175
176 Frame matrices.
177
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
184
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
196
197 Bidirectional display.
198
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
211
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
220
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
239
240 Bidirectional display and character compositions
241
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
246
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
266
267 Moving an iterator in bidirectional text
268 without producing glyphs
269
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
288
289 #include <config.h>
290 #include <stdio.h>
291 #include <limits.h>
292
293 #include "lisp.h"
294 #include "atimer.h"
295 #include "keyboard.h"
296 #include "frame.h"
297 #include "window.h"
298 #include "termchar.h"
299 #include "dispextern.h"
300 #include "character.h"
301 #include "buffer.h"
302 #include "charset.h"
303 #include "indent.h"
304 #include "commands.h"
305 #include "keymap.h"
306 #include "macros.h"
307 #include "disptab.h"
308 #include "termhooks.h"
309 #include "termopts.h"
310 #include "intervals.h"
311 #include "coding.h"
312 #include "process.h"
313 #include "region-cache.h"
314 #include "font.h"
315 #include "fontset.h"
316 #include "blockinput.h"
317 #ifdef HAVE_WINDOW_SYSTEM
318 #include TERM_HEADER
319 #endif /* HAVE_WINDOW_SYSTEM */
320
321 #ifndef FRAME_X_OUTPUT
322 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
323 #endif
324
325 #define INFINITY 10000000
326
327 /* Holds the list (error). */
328 static Lisp_Object list_of_error;
329
330 #ifdef HAVE_WINDOW_SYSTEM
331
332 /* Test if overflow newline into fringe. Called with iterator IT
333 at or past right window margin, and with IT->current_x set. */
334
335 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
336 (!NILP (Voverflow_newline_into_fringe) \
337 && FRAME_WINDOW_P ((IT)->f) \
338 && ((IT)->bidi_it.paragraph_dir == R2L \
339 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
340 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
341 && (IT)->current_x == (IT)->last_visible_x)
342
343 #else /* !HAVE_WINDOW_SYSTEM */
344 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
345 #endif /* HAVE_WINDOW_SYSTEM */
346
347 /* Test if the display element loaded in IT, or the underlying buffer
348 or string character, is a space or a TAB character. This is used
349 to determine where word wrapping can occur. */
350
351 #define IT_DISPLAYING_WHITESPACE(it) \
352 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
353 || ((STRINGP (it->string) \
354 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
355 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
356 || (it->s \
357 && (it->s[IT_BYTEPOS (*it)] == ' ' \
358 || it->s[IT_BYTEPOS (*it)] == '\t')) \
359 || (IT_BYTEPOS (*it) < ZV_BYTE \
360 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
361 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
362
363 /* True means print newline to stdout before next mini-buffer message. */
364
365 bool noninteractive_need_newline;
366
367 /* True means print newline to message log before next message. */
368
369 static bool message_log_need_newline;
370
371 /* Three markers that message_dolog uses.
372 It could allocate them itself, but that causes trouble
373 in handling memory-full errors. */
374 static Lisp_Object message_dolog_marker1;
375 static Lisp_Object message_dolog_marker2;
376 static Lisp_Object message_dolog_marker3;
377 \f
378 /* The buffer position of the first character appearing entirely or
379 partially on the line of the selected window which contains the
380 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
381 redisplay optimization in redisplay_internal. */
382
383 static struct text_pos this_line_start_pos;
384
385 /* Number of characters past the end of the line above, including the
386 terminating newline. */
387
388 static struct text_pos this_line_end_pos;
389
390 /* The vertical positions and the height of this line. */
391
392 static int this_line_vpos;
393 static int this_line_y;
394 static int this_line_pixel_height;
395
396 /* X position at which this display line starts. Usually zero;
397 negative if first character is partially visible. */
398
399 static int this_line_start_x;
400
401 /* The smallest character position seen by move_it_* functions as they
402 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
403 hscrolled lines, see display_line. */
404
405 static struct text_pos this_line_min_pos;
406
407 /* Buffer that this_line_.* variables are referring to. */
408
409 static struct buffer *this_line_buffer;
410
411 /* True if an overlay arrow has been displayed in this window. */
412
413 static bool overlay_arrow_seen;
414
415 /* Vector containing glyphs for an ellipsis `...'. */
416
417 static Lisp_Object default_invis_vector[3];
418
419 /* This is the window where the echo area message was displayed. It
420 is always a mini-buffer window, but it may not be the same window
421 currently active as a mini-buffer. */
422
423 Lisp_Object echo_area_window;
424
425 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
426 pushes the current message and the value of
427 message_enable_multibyte on the stack, the function restore_message
428 pops the stack and displays MESSAGE again. */
429
430 static Lisp_Object Vmessage_stack;
431
432 /* True means multibyte characters were enabled when the echo area
433 message was specified. */
434
435 static bool message_enable_multibyte;
436
437 /* Nonzero if we should redraw the mode lines on the next redisplay.
438 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
439 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
440 (the number used is then only used to track down the cause for this
441 full-redisplay). */
442
443 int update_mode_lines;
444
445 /* Nonzero if window sizes or contents other than selected-window have changed
446 since last redisplay that finished.
447 If it has value REDISPLAY_SOME, then only redisplay the windows where
448 the `redisplay' bit has been set. Otherwise, redisplay all windows
449 (the number used is then only used to track down the cause for this
450 full-redisplay). */
451
452 int windows_or_buffers_changed;
453
454 /* True after display_mode_line if %l was used and it displayed a
455 line number. */
456
457 static bool line_number_displayed;
458
459 /* The name of the *Messages* buffer, a string. */
460
461 static Lisp_Object Vmessages_buffer_name;
462
463 /* Current, index 0, and last displayed echo area message. Either
464 buffers from echo_buffers, or nil to indicate no message. */
465
466 Lisp_Object echo_area_buffer[2];
467
468 /* The buffers referenced from echo_area_buffer. */
469
470 static Lisp_Object echo_buffer[2];
471
472 /* A vector saved used in with_area_buffer to reduce consing. */
473
474 static Lisp_Object Vwith_echo_area_save_vector;
475
476 /* True means display_echo_area should display the last echo area
477 message again. Set by redisplay_preserve_echo_area. */
478
479 static bool display_last_displayed_message_p;
480
481 /* True if echo area is being used by print; false if being used by
482 message. */
483
484 static bool message_buf_print;
485
486 /* Set to true in clear_message to make redisplay_internal aware
487 of an emptied echo area. */
488
489 static bool message_cleared_p;
490
491 /* A scratch glyph row with contents used for generating truncation
492 glyphs. Also used in direct_output_for_insert. */
493
494 #define MAX_SCRATCH_GLYPHS 100
495 static struct glyph_row scratch_glyph_row;
496 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
497
498 /* Ascent and height of the last line processed by move_it_to. */
499
500 static int last_height;
501
502 /* True if there's a help-echo in the echo area. */
503
504 bool help_echo_showing_p;
505
506 /* The maximum distance to look ahead for text properties. Values
507 that are too small let us call compute_char_face and similar
508 functions too often which is expensive. Values that are too large
509 let us call compute_char_face and alike too often because we
510 might not be interested in text properties that far away. */
511
512 #define TEXT_PROP_DISTANCE_LIMIT 100
513
514 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
515 iterator state and later restore it. This is needed because the
516 bidi iterator on bidi.c keeps a stacked cache of its states, which
517 is really a singleton. When we use scratch iterator objects to
518 move around the buffer, we can cause the bidi cache to be pushed or
519 popped, and therefore we need to restore the cache state when we
520 return to the original iterator. */
521 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
522 do { \
523 if (CACHE) \
524 bidi_unshelve_cache (CACHE, true); \
525 ITCOPY = ITORIG; \
526 CACHE = bidi_shelve_cache (); \
527 } while (false)
528
529 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
530 do { \
531 if (pITORIG != pITCOPY) \
532 *(pITORIG) = *(pITCOPY); \
533 bidi_unshelve_cache (CACHE, false); \
534 CACHE = NULL; \
535 } while (false)
536
537 /* Functions to mark elements as needing redisplay. */
538 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
539
540 void
541 redisplay_other_windows (void)
542 {
543 if (!windows_or_buffers_changed)
544 windows_or_buffers_changed = REDISPLAY_SOME;
545 }
546
547 void
548 wset_redisplay (struct window *w)
549 {
550 /* Beware: selected_window can be nil during early stages. */
551 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
552 redisplay_other_windows ();
553 w->redisplay = true;
554 }
555
556 void
557 fset_redisplay (struct frame *f)
558 {
559 redisplay_other_windows ();
560 f->redisplay = true;
561 }
562
563 void
564 bset_redisplay (struct buffer *b)
565 {
566 int count = buffer_window_count (b);
567 if (count > 0)
568 {
569 /* ... it's visible in other window than selected, */
570 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
571 redisplay_other_windows ();
572 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
573 so that if we later set windows_or_buffers_changed, this buffer will
574 not be omitted. */
575 b->text->redisplay = true;
576 }
577 }
578
579 void
580 bset_update_mode_line (struct buffer *b)
581 {
582 if (!update_mode_lines)
583 update_mode_lines = REDISPLAY_SOME;
584 b->text->redisplay = true;
585 }
586
587 #ifdef GLYPH_DEBUG
588
589 /* True means print traces of redisplay if compiled with
590 GLYPH_DEBUG defined. */
591
592 bool trace_redisplay_p;
593
594 #endif /* GLYPH_DEBUG */
595
596 #ifdef DEBUG_TRACE_MOVE
597 /* True means trace with TRACE_MOVE to stderr. */
598 static bool trace_move;
599
600 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
601 #else
602 #define TRACE_MOVE(x) (void) 0
603 #endif
604
605 /* Buffer being redisplayed -- for redisplay_window_error. */
606
607 static struct buffer *displayed_buffer;
608
609 /* Value returned from text property handlers (see below). */
610
611 enum prop_handled
612 {
613 HANDLED_NORMALLY,
614 HANDLED_RECOMPUTE_PROPS,
615 HANDLED_OVERLAY_STRING_CONSUMED,
616 HANDLED_RETURN
617 };
618
619 /* A description of text properties that redisplay is interested
620 in. */
621
622 struct props
623 {
624 /* The symbol index of the name of the property. */
625 short name;
626
627 /* A unique index for the property. */
628 enum prop_idx idx;
629
630 /* A handler function called to set up iterator IT from the property
631 at IT's current position. Value is used to steer handle_stop. */
632 enum prop_handled (*handler) (struct it *it);
633 };
634
635 static enum prop_handled handle_face_prop (struct it *);
636 static enum prop_handled handle_invisible_prop (struct it *);
637 static enum prop_handled handle_display_prop (struct it *);
638 static enum prop_handled handle_composition_prop (struct it *);
639 static enum prop_handled handle_overlay_change (struct it *);
640 static enum prop_handled handle_fontified_prop (struct it *);
641
642 /* Properties handled by iterators. */
643
644 static struct props it_props[] =
645 {
646 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
647 /* Handle `face' before `display' because some sub-properties of
648 `display' need to know the face. */
649 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
650 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
651 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
652 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
653 {0, 0, NULL}
654 };
655
656 /* Value is the position described by X. If X is a marker, value is
657 the marker_position of X. Otherwise, value is X. */
658
659 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
660
661 /* Enumeration returned by some move_it_.* functions internally. */
662
663 enum move_it_result
664 {
665 /* Not used. Undefined value. */
666 MOVE_UNDEFINED,
667
668 /* Move ended at the requested buffer position or ZV. */
669 MOVE_POS_MATCH_OR_ZV,
670
671 /* Move ended at the requested X pixel position. */
672 MOVE_X_REACHED,
673
674 /* Move within a line ended at the end of a line that must be
675 continued. */
676 MOVE_LINE_CONTINUED,
677
678 /* Move within a line ended at the end of a line that would
679 be displayed truncated. */
680 MOVE_LINE_TRUNCATED,
681
682 /* Move within a line ended at a line end. */
683 MOVE_NEWLINE_OR_CR
684 };
685
686 /* This counter is used to clear the face cache every once in a while
687 in redisplay_internal. It is incremented for each redisplay.
688 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
689 cleared. */
690
691 #define CLEAR_FACE_CACHE_COUNT 500
692 static int clear_face_cache_count;
693
694 /* Similarly for the image cache. */
695
696 #ifdef HAVE_WINDOW_SYSTEM
697 #define CLEAR_IMAGE_CACHE_COUNT 101
698 static int clear_image_cache_count;
699
700 /* Null glyph slice */
701 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
702 #endif
703
704 /* True while redisplay_internal is in progress. */
705
706 bool redisplaying_p;
707
708 /* If a string, XTread_socket generates an event to display that string.
709 (The display is done in read_char.) */
710
711 Lisp_Object help_echo_string;
712 Lisp_Object help_echo_window;
713 Lisp_Object help_echo_object;
714 ptrdiff_t help_echo_pos;
715
716 /* Temporary variable for XTread_socket. */
717
718 Lisp_Object previous_help_echo_string;
719
720 /* Platform-independent portion of hourglass implementation. */
721
722 #ifdef HAVE_WINDOW_SYSTEM
723
724 /* True means an hourglass cursor is currently shown. */
725 static bool hourglass_shown_p;
726
727 /* If non-null, an asynchronous timer that, when it expires, displays
728 an hourglass cursor on all frames. */
729 static struct atimer *hourglass_atimer;
730
731 #endif /* HAVE_WINDOW_SYSTEM */
732
733 /* Default number of seconds to wait before displaying an hourglass
734 cursor. */
735 #define DEFAULT_HOURGLASS_DELAY 1
736
737 #ifdef HAVE_WINDOW_SYSTEM
738
739 /* Default pixel width of `thin-space' display method. */
740 #define THIN_SPACE_WIDTH 1
741
742 #endif /* HAVE_WINDOW_SYSTEM */
743
744 /* Function prototypes. */
745
746 static void setup_for_ellipsis (struct it *, int);
747 static void set_iterator_to_next (struct it *, bool);
748 static void mark_window_display_accurate_1 (struct window *, bool);
749 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
750 static bool cursor_row_p (struct glyph_row *);
751 static int redisplay_mode_lines (Lisp_Object, bool);
752
753 static void handle_line_prefix (struct it *);
754
755 static void handle_stop_backwards (struct it *, ptrdiff_t);
756 static void unwind_with_echo_area_buffer (Lisp_Object);
757 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
758 static bool current_message_1 (ptrdiff_t, Lisp_Object);
759 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
760 static void set_message (Lisp_Object);
761 static bool set_message_1 (ptrdiff_t, Lisp_Object);
762 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
763 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
764 static void unwind_redisplay (void);
765 static void extend_face_to_end_of_line (struct it *);
766 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
767 static void push_it (struct it *, struct text_pos *);
768 static void iterate_out_of_display_property (struct it *);
769 static void pop_it (struct it *);
770 static void redisplay_internal (void);
771 static bool echo_area_display (bool);
772 static void redisplay_windows (Lisp_Object);
773 static void redisplay_window (Lisp_Object, bool);
774 static Lisp_Object redisplay_window_error (Lisp_Object);
775 static Lisp_Object redisplay_window_0 (Lisp_Object);
776 static Lisp_Object redisplay_window_1 (Lisp_Object);
777 static bool set_cursor_from_row (struct window *, struct glyph_row *,
778 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
779 int, int);
780 static bool update_menu_bar (struct frame *, bool, bool);
781 static bool try_window_reusing_current_matrix (struct window *);
782 static int try_window_id (struct window *);
783 static bool display_line (struct it *);
784 static int display_mode_lines (struct window *);
785 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
786 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
787 Lisp_Object, bool);
788 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
789 Lisp_Object);
790 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
791 static void display_menu_bar (struct window *);
792 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
793 ptrdiff_t *);
794 static int display_string (const char *, Lisp_Object, Lisp_Object,
795 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
796 static void compute_line_metrics (struct it *);
797 static void run_redisplay_end_trigger_hook (struct it *);
798 static bool get_overlay_strings (struct it *, ptrdiff_t);
799 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
800 static void next_overlay_string (struct it *);
801 static void reseat (struct it *, struct text_pos, bool);
802 static void reseat_1 (struct it *, struct text_pos, bool);
803 static bool next_element_from_display_vector (struct it *);
804 static bool next_element_from_string (struct it *);
805 static bool next_element_from_c_string (struct it *);
806 static bool next_element_from_buffer (struct it *);
807 static bool next_element_from_composition (struct it *);
808 static bool next_element_from_image (struct it *);
809 static bool next_element_from_stretch (struct it *);
810 static void load_overlay_strings (struct it *, ptrdiff_t);
811 static bool get_next_display_element (struct it *);
812 static enum move_it_result
813 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
814 enum move_operation_enum);
815 static void get_visually_first_element (struct it *);
816 static void compute_stop_pos (struct it *);
817 static int face_before_or_after_it_pos (struct it *, bool);
818 static ptrdiff_t next_overlay_change (ptrdiff_t);
819 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
820 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
821 static int handle_single_display_spec (struct it *, Lisp_Object,
822 Lisp_Object, Lisp_Object,
823 struct text_pos *, ptrdiff_t, int, bool);
824 static int underlying_face_id (struct it *);
825
826 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
827 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
828
829 #ifdef HAVE_WINDOW_SYSTEM
830
831 static void update_tool_bar (struct frame *, bool);
832 static void x_draw_bottom_divider (struct window *w);
833 static void notice_overwritten_cursor (struct window *,
834 enum glyph_row_area,
835 int, int, int, int);
836 static void append_stretch_glyph (struct it *, Lisp_Object,
837 int, int, int);
838
839
840 #endif /* HAVE_WINDOW_SYSTEM */
841
842 static void produce_special_glyphs (struct it *, enum display_element_type);
843 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
844 static bool coords_in_mouse_face_p (struct window *, int, int);
845
846
847 \f
848 /***********************************************************************
849 Window display dimensions
850 ***********************************************************************/
851
852 /* Return the bottom boundary y-position for text lines in window W.
853 This is the first y position at which a line cannot start.
854 It is relative to the top of the window.
855
856 This is the height of W minus the height of a mode line, if any. */
857
858 int
859 window_text_bottom_y (struct window *w)
860 {
861 int height = WINDOW_PIXEL_HEIGHT (w);
862
863 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
864
865 if (WINDOW_WANTS_MODELINE_P (w))
866 height -= CURRENT_MODE_LINE_HEIGHT (w);
867
868 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
869
870 return height;
871 }
872
873 /* Return the pixel width of display area AREA of window W.
874 ANY_AREA means return the total width of W, not including
875 fringes to the left and right of the window. */
876
877 int
878 window_box_width (struct window *w, enum glyph_row_area area)
879 {
880 int width = w->pixel_width;
881
882 if (!w->pseudo_window_p)
883 {
884 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
885 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
886
887 if (area == TEXT_AREA)
888 width -= (WINDOW_MARGINS_WIDTH (w)
889 + WINDOW_FRINGES_WIDTH (w));
890 else if (area == LEFT_MARGIN_AREA)
891 width = WINDOW_LEFT_MARGIN_WIDTH (w);
892 else if (area == RIGHT_MARGIN_AREA)
893 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
894 }
895
896 /* With wide margins, fringes, etc. we might end up with a negative
897 width, correct that here. */
898 return max (0, width);
899 }
900
901
902 /* Return the pixel height of the display area of window W, not
903 including mode lines of W, if any. */
904
905 int
906 window_box_height (struct window *w)
907 {
908 struct frame *f = XFRAME (w->frame);
909 int height = WINDOW_PIXEL_HEIGHT (w);
910
911 eassert (height >= 0);
912
913 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
914 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
915
916 /* Note: the code below that determines the mode-line/header-line
917 height is essentially the same as that contained in the macro
918 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
919 the appropriate glyph row has its `mode_line_p' flag set,
920 and if it doesn't, uses estimate_mode_line_height instead. */
921
922 if (WINDOW_WANTS_MODELINE_P (w))
923 {
924 struct glyph_row *ml_row
925 = (w->current_matrix && w->current_matrix->rows
926 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
927 : 0);
928 if (ml_row && ml_row->mode_line_p)
929 height -= ml_row->height;
930 else
931 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
932 }
933
934 if (WINDOW_WANTS_HEADER_LINE_P (w))
935 {
936 struct glyph_row *hl_row
937 = (w->current_matrix && w->current_matrix->rows
938 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
939 : 0);
940 if (hl_row && hl_row->mode_line_p)
941 height -= hl_row->height;
942 else
943 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
944 }
945
946 /* With a very small font and a mode-line that's taller than
947 default, we might end up with a negative height. */
948 return max (0, height);
949 }
950
951 /* Return the window-relative coordinate of the left edge of display
952 area AREA of window W. ANY_AREA means return the left edge of the
953 whole window, to the right of the left fringe of W. */
954
955 int
956 window_box_left_offset (struct window *w, enum glyph_row_area area)
957 {
958 int x;
959
960 if (w->pseudo_window_p)
961 return 0;
962
963 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
964
965 if (area == TEXT_AREA)
966 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
967 + window_box_width (w, LEFT_MARGIN_AREA));
968 else if (area == RIGHT_MARGIN_AREA)
969 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
970 + window_box_width (w, LEFT_MARGIN_AREA)
971 + window_box_width (w, TEXT_AREA)
972 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
973 ? 0
974 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
975 else if (area == LEFT_MARGIN_AREA
976 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
977 x += WINDOW_LEFT_FRINGE_WIDTH (w);
978
979 /* Don't return more than the window's pixel width. */
980 return min (x, w->pixel_width);
981 }
982
983
984 /* Return the window-relative coordinate of the right edge of display
985 area AREA of window W. ANY_AREA means return the right edge of the
986 whole window, to the left of the right fringe of W. */
987
988 static int
989 window_box_right_offset (struct window *w, enum glyph_row_area area)
990 {
991 /* Don't return more than the window's pixel width. */
992 return min (window_box_left_offset (w, area) + window_box_width (w, area),
993 w->pixel_width);
994 }
995
996 /* Return the frame-relative coordinate of the left edge of display
997 area AREA of window W. ANY_AREA means return the left edge of the
998 whole window, to the right of the left fringe of W. */
999
1000 int
1001 window_box_left (struct window *w, enum glyph_row_area area)
1002 {
1003 struct frame *f = XFRAME (w->frame);
1004 int x;
1005
1006 if (w->pseudo_window_p)
1007 return FRAME_INTERNAL_BORDER_WIDTH (f);
1008
1009 x = (WINDOW_LEFT_EDGE_X (w)
1010 + window_box_left_offset (w, area));
1011
1012 return x;
1013 }
1014
1015
1016 /* Return the frame-relative coordinate of the right edge of display
1017 area AREA of window W. ANY_AREA means return the right edge of the
1018 whole window, to the left of the right fringe of W. */
1019
1020 int
1021 window_box_right (struct window *w, enum glyph_row_area area)
1022 {
1023 return window_box_left (w, area) + window_box_width (w, area);
1024 }
1025
1026 /* Get the bounding box of the display area AREA of window W, without
1027 mode lines, in frame-relative coordinates. ANY_AREA means the
1028 whole window, not including the left and right fringes of
1029 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1030 coordinates of the upper-left corner of the box. Return in
1031 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1032
1033 void
1034 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1035 int *box_y, int *box_width, int *box_height)
1036 {
1037 if (box_width)
1038 *box_width = window_box_width (w, area);
1039 if (box_height)
1040 *box_height = window_box_height (w);
1041 if (box_x)
1042 *box_x = window_box_left (w, area);
1043 if (box_y)
1044 {
1045 *box_y = WINDOW_TOP_EDGE_Y (w);
1046 if (WINDOW_WANTS_HEADER_LINE_P (w))
1047 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1048 }
1049 }
1050
1051 #ifdef HAVE_WINDOW_SYSTEM
1052
1053 /* Get the bounding box of the display area AREA of window W, without
1054 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1055 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1056 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1057 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1058 box. */
1059
1060 static void
1061 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1062 int *bottom_right_x, int *bottom_right_y)
1063 {
1064 window_box (w, ANY_AREA, top_left_x, top_left_y,
1065 bottom_right_x, bottom_right_y);
1066 *bottom_right_x += *top_left_x;
1067 *bottom_right_y += *top_left_y;
1068 }
1069
1070 #endif /* HAVE_WINDOW_SYSTEM */
1071
1072 /***********************************************************************
1073 Utilities
1074 ***********************************************************************/
1075
1076 /* Return the bottom y-position of the line the iterator IT is in.
1077 This can modify IT's settings. */
1078
1079 int
1080 line_bottom_y (struct it *it)
1081 {
1082 int line_height = it->max_ascent + it->max_descent;
1083 int line_top_y = it->current_y;
1084
1085 if (line_height == 0)
1086 {
1087 if (last_height)
1088 line_height = last_height;
1089 else if (IT_CHARPOS (*it) < ZV)
1090 {
1091 move_it_by_lines (it, 1);
1092 line_height = (it->max_ascent || it->max_descent
1093 ? it->max_ascent + it->max_descent
1094 : last_height);
1095 }
1096 else
1097 {
1098 struct glyph_row *row = it->glyph_row;
1099
1100 /* Use the default character height. */
1101 it->glyph_row = NULL;
1102 it->what = IT_CHARACTER;
1103 it->c = ' ';
1104 it->len = 1;
1105 PRODUCE_GLYPHS (it);
1106 line_height = it->ascent + it->descent;
1107 it->glyph_row = row;
1108 }
1109 }
1110
1111 return line_top_y + line_height;
1112 }
1113
1114 DEFUN ("line-pixel-height", Fline_pixel_height,
1115 Sline_pixel_height, 0, 0, 0,
1116 doc: /* Return height in pixels of text line in the selected window.
1117
1118 Value is the height in pixels of the line at point. */)
1119 (void)
1120 {
1121 struct it it;
1122 struct text_pos pt;
1123 struct window *w = XWINDOW (selected_window);
1124 struct buffer *old_buffer = NULL;
1125 Lisp_Object result;
1126
1127 if (XBUFFER (w->contents) != current_buffer)
1128 {
1129 old_buffer = current_buffer;
1130 set_buffer_internal_1 (XBUFFER (w->contents));
1131 }
1132 SET_TEXT_POS (pt, PT, PT_BYTE);
1133 start_display (&it, w, pt);
1134 it.vpos = it.current_y = 0;
1135 last_height = 0;
1136 result = make_number (line_bottom_y (&it));
1137 if (old_buffer)
1138 set_buffer_internal_1 (old_buffer);
1139
1140 return result;
1141 }
1142
1143 /* Return the default pixel height of text lines in window W. The
1144 value is the canonical height of the W frame's default font, plus
1145 any extra space required by the line-spacing variable or frame
1146 parameter.
1147
1148 Implementation note: this ignores any line-spacing text properties
1149 put on the newline characters. This is because those properties
1150 only affect the _screen_ line ending in the newline (i.e., in a
1151 continued line, only the last screen line will be affected), which
1152 means only a small number of lines in a buffer can ever use this
1153 feature. Since this function is used to compute the default pixel
1154 equivalent of text lines in a window, we can safely ignore those
1155 few lines. For the same reasons, we ignore the line-height
1156 properties. */
1157 int
1158 default_line_pixel_height (struct window *w)
1159 {
1160 struct frame *f = WINDOW_XFRAME (w);
1161 int height = FRAME_LINE_HEIGHT (f);
1162
1163 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1164 {
1165 struct buffer *b = XBUFFER (w->contents);
1166 Lisp_Object val = BVAR (b, extra_line_spacing);
1167
1168 if (NILP (val))
1169 val = BVAR (&buffer_defaults, extra_line_spacing);
1170 if (!NILP (val))
1171 {
1172 if (RANGED_INTEGERP (0, val, INT_MAX))
1173 height += XFASTINT (val);
1174 else if (FLOATP (val))
1175 {
1176 int addon = XFLOAT_DATA (val) * height + 0.5;
1177
1178 if (addon >= 0)
1179 height += addon;
1180 }
1181 }
1182 else
1183 height += f->extra_line_spacing;
1184 }
1185
1186 return height;
1187 }
1188
1189 /* Subroutine of pos_visible_p below. Extracts a display string, if
1190 any, from the display spec given as its argument. */
1191 static Lisp_Object
1192 string_from_display_spec (Lisp_Object spec)
1193 {
1194 if (CONSP (spec))
1195 {
1196 while (CONSP (spec))
1197 {
1198 if (STRINGP (XCAR (spec)))
1199 return XCAR (spec);
1200 spec = XCDR (spec);
1201 }
1202 }
1203 else if (VECTORP (spec))
1204 {
1205 ptrdiff_t i;
1206
1207 for (i = 0; i < ASIZE (spec); i++)
1208 {
1209 if (STRINGP (AREF (spec, i)))
1210 return AREF (spec, i);
1211 }
1212 return Qnil;
1213 }
1214
1215 return spec;
1216 }
1217
1218
1219 /* Limit insanely large values of W->hscroll on frame F to the largest
1220 value that will still prevent first_visible_x and last_visible_x of
1221 'struct it' from overflowing an int. */
1222 static int
1223 window_hscroll_limited (struct window *w, struct frame *f)
1224 {
1225 ptrdiff_t window_hscroll = w->hscroll;
1226 int window_text_width = window_box_width (w, TEXT_AREA);
1227 int colwidth = FRAME_COLUMN_WIDTH (f);
1228
1229 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1230 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1231
1232 return window_hscroll;
1233 }
1234
1235 /* Return true if position CHARPOS is visible in window W.
1236 CHARPOS < 0 means return info about WINDOW_END position.
1237 If visible, set *X and *Y to pixel coordinates of top left corner.
1238 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1239 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1240
1241 bool
1242 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1243 int *rtop, int *rbot, int *rowh, int *vpos)
1244 {
1245 struct it it;
1246 void *itdata = bidi_shelve_cache ();
1247 struct text_pos top;
1248 bool visible_p = false;
1249 struct buffer *old_buffer = NULL;
1250 bool r2l = false;
1251
1252 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1253 return visible_p;
1254
1255 if (XBUFFER (w->contents) != current_buffer)
1256 {
1257 old_buffer = current_buffer;
1258 set_buffer_internal_1 (XBUFFER (w->contents));
1259 }
1260
1261 SET_TEXT_POS_FROM_MARKER (top, w->start);
1262 /* Scrolling a minibuffer window via scroll bar when the echo area
1263 shows long text sometimes resets the minibuffer contents behind
1264 our backs. */
1265 if (CHARPOS (top) > ZV)
1266 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1267
1268 /* Compute exact mode line heights. */
1269 if (WINDOW_WANTS_MODELINE_P (w))
1270 w->mode_line_height
1271 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1272 BVAR (current_buffer, mode_line_format));
1273
1274 if (WINDOW_WANTS_HEADER_LINE_P (w))
1275 w->header_line_height
1276 = display_mode_line (w, HEADER_LINE_FACE_ID,
1277 BVAR (current_buffer, header_line_format));
1278
1279 start_display (&it, w, top);
1280 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1281 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1282
1283 if (charpos >= 0
1284 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1285 && IT_CHARPOS (it) >= charpos)
1286 /* When scanning backwards under bidi iteration, move_it_to
1287 stops at or _before_ CHARPOS, because it stops at or to
1288 the _right_ of the character at CHARPOS. */
1289 || (it.bidi_p && it.bidi_it.scan_dir == -1
1290 && IT_CHARPOS (it) <= charpos)))
1291 {
1292 /* We have reached CHARPOS, or passed it. How the call to
1293 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1294 or covered by a display property, move_it_to stops at the end
1295 of the invisible text, to the right of CHARPOS. (ii) If
1296 CHARPOS is in a display vector, move_it_to stops on its last
1297 glyph. */
1298 int top_x = it.current_x;
1299 int top_y = it.current_y;
1300 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1301 int bottom_y;
1302 struct it save_it;
1303 void *save_it_data = NULL;
1304
1305 /* Calling line_bottom_y may change it.method, it.position, etc. */
1306 SAVE_IT (save_it, it, save_it_data);
1307 last_height = 0;
1308 bottom_y = line_bottom_y (&it);
1309 if (top_y < window_top_y)
1310 visible_p = bottom_y > window_top_y;
1311 else if (top_y < it.last_visible_y)
1312 visible_p = true;
1313 if (bottom_y >= it.last_visible_y
1314 && it.bidi_p && it.bidi_it.scan_dir == -1
1315 && IT_CHARPOS (it) < charpos)
1316 {
1317 /* When the last line of the window is scanned backwards
1318 under bidi iteration, we could be duped into thinking
1319 that we have passed CHARPOS, when in fact move_it_to
1320 simply stopped short of CHARPOS because it reached
1321 last_visible_y. To see if that's what happened, we call
1322 move_it_to again with a slightly larger vertical limit,
1323 and see if it actually moved vertically; if it did, we
1324 didn't really reach CHARPOS, which is beyond window end. */
1325 /* Why 10? because we don't know how many canonical lines
1326 will the height of the next line(s) be. So we guess. */
1327 int ten_more_lines = 10 * default_line_pixel_height (w);
1328
1329 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1330 MOVE_TO_POS | MOVE_TO_Y);
1331 if (it.current_y > top_y)
1332 visible_p = false;
1333
1334 }
1335 RESTORE_IT (&it, &save_it, save_it_data);
1336 if (visible_p)
1337 {
1338 if (it.method == GET_FROM_DISPLAY_VECTOR)
1339 {
1340 /* We stopped on the last glyph of a display vector.
1341 Try and recompute. Hack alert! */
1342 if (charpos < 2 || top.charpos >= charpos)
1343 top_x = it.glyph_row->x;
1344 else
1345 {
1346 struct it it2, it2_prev;
1347 /* The idea is to get to the previous buffer
1348 position, consume the character there, and use
1349 the pixel coordinates we get after that. But if
1350 the previous buffer position is also displayed
1351 from a display vector, we need to consume all of
1352 the glyphs from that display vector. */
1353 start_display (&it2, w, top);
1354 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1355 /* If we didn't get to CHARPOS - 1, there's some
1356 replacing display property at that position, and
1357 we stopped after it. That is exactly the place
1358 whose coordinates we want. */
1359 if (IT_CHARPOS (it2) != charpos - 1)
1360 it2_prev = it2;
1361 else
1362 {
1363 /* Iterate until we get out of the display
1364 vector that displays the character at
1365 CHARPOS - 1. */
1366 do {
1367 get_next_display_element (&it2);
1368 PRODUCE_GLYPHS (&it2);
1369 it2_prev = it2;
1370 set_iterator_to_next (&it2, true);
1371 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1372 && IT_CHARPOS (it2) < charpos);
1373 }
1374 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1375 || it2_prev.current_x > it2_prev.last_visible_x)
1376 top_x = it.glyph_row->x;
1377 else
1378 {
1379 top_x = it2_prev.current_x;
1380 top_y = it2_prev.current_y;
1381 }
1382 }
1383 }
1384 else if (IT_CHARPOS (it) != charpos)
1385 {
1386 Lisp_Object cpos = make_number (charpos);
1387 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1388 Lisp_Object string = string_from_display_spec (spec);
1389 struct text_pos tpos;
1390 bool newline_in_string
1391 = (STRINGP (string)
1392 && memchr (SDATA (string), '\n', SBYTES (string)));
1393
1394 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1395 bool replacing_spec_p
1396 = (!NILP (spec)
1397 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1398 charpos, FRAME_WINDOW_P (it.f)));
1399 /* The tricky code below is needed because there's a
1400 discrepancy between move_it_to and how we set cursor
1401 when PT is at the beginning of a portion of text
1402 covered by a display property or an overlay with a
1403 display property, or the display line ends in a
1404 newline from a display string. move_it_to will stop
1405 _after_ such display strings, whereas
1406 set_cursor_from_row conspires with cursor_row_p to
1407 place the cursor on the first glyph produced from the
1408 display string. */
1409
1410 /* We have overshoot PT because it is covered by a
1411 display property that replaces the text it covers.
1412 If the string includes embedded newlines, we are also
1413 in the wrong display line. Backtrack to the correct
1414 line, where the display property begins. */
1415 if (replacing_spec_p)
1416 {
1417 Lisp_Object startpos, endpos;
1418 EMACS_INT start, end;
1419 struct it it3;
1420
1421 /* Find the first and the last buffer positions
1422 covered by the display string. */
1423 endpos =
1424 Fnext_single_char_property_change (cpos, Qdisplay,
1425 Qnil, Qnil);
1426 startpos =
1427 Fprevious_single_char_property_change (endpos, Qdisplay,
1428 Qnil, Qnil);
1429 start = XFASTINT (startpos);
1430 end = XFASTINT (endpos);
1431 /* Move to the last buffer position before the
1432 display property. */
1433 start_display (&it3, w, top);
1434 if (start > CHARPOS (top))
1435 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1436 /* Move forward one more line if the position before
1437 the display string is a newline or if it is the
1438 rightmost character on a line that is
1439 continued or word-wrapped. */
1440 if (it3.method == GET_FROM_BUFFER
1441 && (it3.c == '\n'
1442 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1443 move_it_by_lines (&it3, 1);
1444 else if (move_it_in_display_line_to (&it3, -1,
1445 it3.current_x
1446 + it3.pixel_width,
1447 MOVE_TO_X)
1448 == MOVE_LINE_CONTINUED)
1449 {
1450 move_it_by_lines (&it3, 1);
1451 /* When we are under word-wrap, the #$@%!
1452 move_it_by_lines moves 2 lines, so we need to
1453 fix that up. */
1454 if (it3.line_wrap == WORD_WRAP)
1455 move_it_by_lines (&it3, -1);
1456 }
1457
1458 /* Record the vertical coordinate of the display
1459 line where we wound up. */
1460 top_y = it3.current_y;
1461 if (it3.bidi_p)
1462 {
1463 /* When characters are reordered for display,
1464 the character displayed to the left of the
1465 display string could be _after_ the display
1466 property in the logical order. Use the
1467 smallest vertical position of these two. */
1468 start_display (&it3, w, top);
1469 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1470 if (it3.current_y < top_y)
1471 top_y = it3.current_y;
1472 }
1473 /* Move from the top of the window to the beginning
1474 of the display line where the display string
1475 begins. */
1476 start_display (&it3, w, top);
1477 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1478 /* If it3_moved stays false after the 'while' loop
1479 below, that means we already were at a newline
1480 before the loop (e.g., the display string begins
1481 with a newline), so we don't need to (and cannot)
1482 inspect the glyphs of it3.glyph_row, because
1483 PRODUCE_GLYPHS will not produce anything for a
1484 newline, and thus it3.glyph_row stays at its
1485 stale content it got at top of the window. */
1486 bool it3_moved = false;
1487 /* Finally, advance the iterator until we hit the
1488 first display element whose character position is
1489 CHARPOS, or until the first newline from the
1490 display string, which signals the end of the
1491 display line. */
1492 while (get_next_display_element (&it3))
1493 {
1494 PRODUCE_GLYPHS (&it3);
1495 if (IT_CHARPOS (it3) == charpos
1496 || ITERATOR_AT_END_OF_LINE_P (&it3))
1497 break;
1498 it3_moved = true;
1499 set_iterator_to_next (&it3, false);
1500 }
1501 top_x = it3.current_x - it3.pixel_width;
1502 /* Normally, we would exit the above loop because we
1503 found the display element whose character
1504 position is CHARPOS. For the contingency that we
1505 didn't, and stopped at the first newline from the
1506 display string, move back over the glyphs
1507 produced from the string, until we find the
1508 rightmost glyph not from the string. */
1509 if (it3_moved
1510 && newline_in_string
1511 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1512 {
1513 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1514 + it3.glyph_row->used[TEXT_AREA];
1515
1516 while (EQ ((g - 1)->object, string))
1517 {
1518 --g;
1519 top_x -= g->pixel_width;
1520 }
1521 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1522 + it3.glyph_row->used[TEXT_AREA]);
1523 }
1524 }
1525 }
1526
1527 *x = top_x;
1528 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1529 *rtop = max (0, window_top_y - top_y);
1530 *rbot = max (0, bottom_y - it.last_visible_y);
1531 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1532 - max (top_y, window_top_y)));
1533 *vpos = it.vpos;
1534 if (it.bidi_it.paragraph_dir == R2L)
1535 r2l = true;
1536 }
1537 }
1538 else
1539 {
1540 /* Either we were asked to provide info about WINDOW_END, or
1541 CHARPOS is in the partially visible glyph row at end of
1542 window. */
1543 struct it it2;
1544 void *it2data = NULL;
1545
1546 SAVE_IT (it2, it, it2data);
1547 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1548 move_it_by_lines (&it, 1);
1549 if (charpos < IT_CHARPOS (it)
1550 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1551 {
1552 visible_p = true;
1553 RESTORE_IT (&it2, &it2, it2data);
1554 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1555 *x = it2.current_x;
1556 *y = it2.current_y + it2.max_ascent - it2.ascent;
1557 *rtop = max (0, -it2.current_y);
1558 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1559 - it.last_visible_y));
1560 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1561 it.last_visible_y)
1562 - max (it2.current_y,
1563 WINDOW_HEADER_LINE_HEIGHT (w))));
1564 *vpos = it2.vpos;
1565 if (it2.bidi_it.paragraph_dir == R2L)
1566 r2l = true;
1567 }
1568 else
1569 bidi_unshelve_cache (it2data, true);
1570 }
1571 bidi_unshelve_cache (itdata, false);
1572
1573 if (old_buffer)
1574 set_buffer_internal_1 (old_buffer);
1575
1576 if (visible_p)
1577 {
1578 if (w->hscroll > 0)
1579 *x -=
1580 window_hscroll_limited (w, WINDOW_XFRAME (w))
1581 * WINDOW_FRAME_COLUMN_WIDTH (w);
1582 /* For lines in an R2L paragraph, we need to mirror the X pixel
1583 coordinate wrt the text area. For the reasons, see the
1584 commentary in buffer_posn_from_coords and the explanation of
1585 the geometry used by the move_it_* functions at the end of
1586 the large commentary near the beginning of this file. */
1587 if (r2l)
1588 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1589 }
1590
1591 #if false
1592 /* Debugging code. */
1593 if (visible_p)
1594 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1595 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1596 else
1597 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1598 #endif
1599
1600 return visible_p;
1601 }
1602
1603
1604 /* Return the next character from STR. Return in *LEN the length of
1605 the character. This is like STRING_CHAR_AND_LENGTH but never
1606 returns an invalid character. If we find one, we return a `?', but
1607 with the length of the invalid character. */
1608
1609 static int
1610 string_char_and_length (const unsigned char *str, int *len)
1611 {
1612 int c;
1613
1614 c = STRING_CHAR_AND_LENGTH (str, *len);
1615 if (!CHAR_VALID_P (c))
1616 /* We may not change the length here because other places in Emacs
1617 don't use this function, i.e. they silently accept invalid
1618 characters. */
1619 c = '?';
1620
1621 return c;
1622 }
1623
1624
1625
1626 /* Given a position POS containing a valid character and byte position
1627 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1628
1629 static struct text_pos
1630 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1631 {
1632 eassert (STRINGP (string) && nchars >= 0);
1633
1634 if (STRING_MULTIBYTE (string))
1635 {
1636 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1637 int len;
1638
1639 while (nchars--)
1640 {
1641 string_char_and_length (p, &len);
1642 p += len;
1643 CHARPOS (pos) += 1;
1644 BYTEPOS (pos) += len;
1645 }
1646 }
1647 else
1648 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1649
1650 return pos;
1651 }
1652
1653
1654 /* Value is the text position, i.e. character and byte position,
1655 for character position CHARPOS in STRING. */
1656
1657 static struct text_pos
1658 string_pos (ptrdiff_t charpos, Lisp_Object string)
1659 {
1660 struct text_pos pos;
1661 eassert (STRINGP (string));
1662 eassert (charpos >= 0);
1663 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1664 return pos;
1665 }
1666
1667
1668 /* Value is a text position, i.e. character and byte position, for
1669 character position CHARPOS in C string S. MULTIBYTE_P
1670 means recognize multibyte characters. */
1671
1672 static struct text_pos
1673 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1674 {
1675 struct text_pos pos;
1676
1677 eassert (s != NULL);
1678 eassert (charpos >= 0);
1679
1680 if (multibyte_p)
1681 {
1682 int len;
1683
1684 SET_TEXT_POS (pos, 0, 0);
1685 while (charpos--)
1686 {
1687 string_char_and_length ((const unsigned char *) s, &len);
1688 s += len;
1689 CHARPOS (pos) += 1;
1690 BYTEPOS (pos) += len;
1691 }
1692 }
1693 else
1694 SET_TEXT_POS (pos, charpos, charpos);
1695
1696 return pos;
1697 }
1698
1699
1700 /* Value is the number of characters in C string S. MULTIBYTE_P
1701 means recognize multibyte characters. */
1702
1703 static ptrdiff_t
1704 number_of_chars (const char *s, bool multibyte_p)
1705 {
1706 ptrdiff_t nchars;
1707
1708 if (multibyte_p)
1709 {
1710 ptrdiff_t rest = strlen (s);
1711 int len;
1712 const unsigned char *p = (const unsigned char *) s;
1713
1714 for (nchars = 0; rest > 0; ++nchars)
1715 {
1716 string_char_and_length (p, &len);
1717 rest -= len, p += len;
1718 }
1719 }
1720 else
1721 nchars = strlen (s);
1722
1723 return nchars;
1724 }
1725
1726
1727 /* Compute byte position NEWPOS->bytepos corresponding to
1728 NEWPOS->charpos. POS is a known position in string STRING.
1729 NEWPOS->charpos must be >= POS.charpos. */
1730
1731 static void
1732 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1733 {
1734 eassert (STRINGP (string));
1735 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1736
1737 if (STRING_MULTIBYTE (string))
1738 *newpos = string_pos_nchars_ahead (pos, string,
1739 CHARPOS (*newpos) - CHARPOS (pos));
1740 else
1741 BYTEPOS (*newpos) = CHARPOS (*newpos);
1742 }
1743
1744 /* EXPORT:
1745 Return an estimation of the pixel height of mode or header lines on
1746 frame F. FACE_ID specifies what line's height to estimate. */
1747
1748 int
1749 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1750 {
1751 #ifdef HAVE_WINDOW_SYSTEM
1752 if (FRAME_WINDOW_P (f))
1753 {
1754 int height = FONT_HEIGHT (FRAME_FONT (f));
1755
1756 /* This function is called so early when Emacs starts that the face
1757 cache and mode line face are not yet initialized. */
1758 if (FRAME_FACE_CACHE (f))
1759 {
1760 struct face *face = FACE_FROM_ID (f, face_id);
1761 if (face)
1762 {
1763 if (face->font)
1764 height = FONT_HEIGHT (face->font);
1765 if (face->box_line_width > 0)
1766 height += 2 * face->box_line_width;
1767 }
1768 }
1769
1770 return height;
1771 }
1772 #endif
1773
1774 return 1;
1775 }
1776
1777 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1778 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1779 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1780 not force the value into range. */
1781
1782 void
1783 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1784 NativeRectangle *bounds, bool noclip)
1785 {
1786
1787 #ifdef HAVE_WINDOW_SYSTEM
1788 if (FRAME_WINDOW_P (f))
1789 {
1790 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1791 even for negative values. */
1792 if (pix_x < 0)
1793 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1794 if (pix_y < 0)
1795 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1796
1797 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1798 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1799
1800 if (bounds)
1801 STORE_NATIVE_RECT (*bounds,
1802 FRAME_COL_TO_PIXEL_X (f, pix_x),
1803 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1804 FRAME_COLUMN_WIDTH (f) - 1,
1805 FRAME_LINE_HEIGHT (f) - 1);
1806
1807 /* PXW: Should we clip pixels before converting to columns/lines? */
1808 if (!noclip)
1809 {
1810 if (pix_x < 0)
1811 pix_x = 0;
1812 else if (pix_x > FRAME_TOTAL_COLS (f))
1813 pix_x = FRAME_TOTAL_COLS (f);
1814
1815 if (pix_y < 0)
1816 pix_y = 0;
1817 else if (pix_y > FRAME_TOTAL_LINES (f))
1818 pix_y = FRAME_TOTAL_LINES (f);
1819 }
1820 }
1821 #endif
1822
1823 *x = pix_x;
1824 *y = pix_y;
1825 }
1826
1827
1828 /* Find the glyph under window-relative coordinates X/Y in window W.
1829 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1830 strings. Return in *HPOS and *VPOS the row and column number of
1831 the glyph found. Return in *AREA the glyph area containing X.
1832 Value is a pointer to the glyph found or null if X/Y is not on
1833 text, or we can't tell because W's current matrix is not up to
1834 date. */
1835
1836 static struct glyph *
1837 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1838 int *dx, int *dy, int *area)
1839 {
1840 struct glyph *glyph, *end;
1841 struct glyph_row *row = NULL;
1842 int x0, i;
1843
1844 /* Find row containing Y. Give up if some row is not enabled. */
1845 for (i = 0; i < w->current_matrix->nrows; ++i)
1846 {
1847 row = MATRIX_ROW (w->current_matrix, i);
1848 if (!row->enabled_p)
1849 return NULL;
1850 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1851 break;
1852 }
1853
1854 *vpos = i;
1855 *hpos = 0;
1856
1857 /* Give up if Y is not in the window. */
1858 if (i == w->current_matrix->nrows)
1859 return NULL;
1860
1861 /* Get the glyph area containing X. */
1862 if (w->pseudo_window_p)
1863 {
1864 *area = TEXT_AREA;
1865 x0 = 0;
1866 }
1867 else
1868 {
1869 if (x < window_box_left_offset (w, TEXT_AREA))
1870 {
1871 *area = LEFT_MARGIN_AREA;
1872 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1873 }
1874 else if (x < window_box_right_offset (w, TEXT_AREA))
1875 {
1876 *area = TEXT_AREA;
1877 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1878 }
1879 else
1880 {
1881 *area = RIGHT_MARGIN_AREA;
1882 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1883 }
1884 }
1885
1886 /* Find glyph containing X. */
1887 glyph = row->glyphs[*area];
1888 end = glyph + row->used[*area];
1889 x -= x0;
1890 while (glyph < end && x >= glyph->pixel_width)
1891 {
1892 x -= glyph->pixel_width;
1893 ++glyph;
1894 }
1895
1896 if (glyph == end)
1897 return NULL;
1898
1899 if (dx)
1900 {
1901 *dx = x;
1902 *dy = y - (row->y + row->ascent - glyph->ascent);
1903 }
1904
1905 *hpos = glyph - row->glyphs[*area];
1906 return glyph;
1907 }
1908
1909 /* Convert frame-relative x/y to coordinates relative to window W.
1910 Takes pseudo-windows into account. */
1911
1912 static void
1913 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1914 {
1915 if (w->pseudo_window_p)
1916 {
1917 /* A pseudo-window is always full-width, and starts at the
1918 left edge of the frame, plus a frame border. */
1919 struct frame *f = XFRAME (w->frame);
1920 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1921 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1922 }
1923 else
1924 {
1925 *x -= WINDOW_LEFT_EDGE_X (w);
1926 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1927 }
1928 }
1929
1930 #ifdef HAVE_WINDOW_SYSTEM
1931
1932 /* EXPORT:
1933 Return in RECTS[] at most N clipping rectangles for glyph string S.
1934 Return the number of stored rectangles. */
1935
1936 int
1937 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1938 {
1939 XRectangle r;
1940
1941 if (n <= 0)
1942 return 0;
1943
1944 if (s->row->full_width_p)
1945 {
1946 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1947 r.x = WINDOW_LEFT_EDGE_X (s->w);
1948 if (s->row->mode_line_p)
1949 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
1950 else
1951 r.width = WINDOW_PIXEL_WIDTH (s->w);
1952
1953 /* Unless displaying a mode or menu bar line, which are always
1954 fully visible, clip to the visible part of the row. */
1955 if (s->w->pseudo_window_p)
1956 r.height = s->row->visible_height;
1957 else
1958 r.height = s->height;
1959 }
1960 else
1961 {
1962 /* This is a text line that may be partially visible. */
1963 r.x = window_box_left (s->w, s->area);
1964 r.width = window_box_width (s->w, s->area);
1965 r.height = s->row->visible_height;
1966 }
1967
1968 if (s->clip_head)
1969 if (r.x < s->clip_head->x)
1970 {
1971 if (r.width >= s->clip_head->x - r.x)
1972 r.width -= s->clip_head->x - r.x;
1973 else
1974 r.width = 0;
1975 r.x = s->clip_head->x;
1976 }
1977 if (s->clip_tail)
1978 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1979 {
1980 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1981 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1982 else
1983 r.width = 0;
1984 }
1985
1986 /* If S draws overlapping rows, it's sufficient to use the top and
1987 bottom of the window for clipping because this glyph string
1988 intentionally draws over other lines. */
1989 if (s->for_overlaps)
1990 {
1991 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1992 r.height = window_text_bottom_y (s->w) - r.y;
1993
1994 /* Alas, the above simple strategy does not work for the
1995 environments with anti-aliased text: if the same text is
1996 drawn onto the same place multiple times, it gets thicker.
1997 If the overlap we are processing is for the erased cursor, we
1998 take the intersection with the rectangle of the cursor. */
1999 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2000 {
2001 XRectangle rc, r_save = r;
2002
2003 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2004 rc.y = s->w->phys_cursor.y;
2005 rc.width = s->w->phys_cursor_width;
2006 rc.height = s->w->phys_cursor_height;
2007
2008 x_intersect_rectangles (&r_save, &rc, &r);
2009 }
2010 }
2011 else
2012 {
2013 /* Don't use S->y for clipping because it doesn't take partially
2014 visible lines into account. For example, it can be negative for
2015 partially visible lines at the top of a window. */
2016 if (!s->row->full_width_p
2017 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2018 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2019 else
2020 r.y = max (0, s->row->y);
2021 }
2022
2023 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2024
2025 /* If drawing the cursor, don't let glyph draw outside its
2026 advertised boundaries. Cleartype does this under some circumstances. */
2027 if (s->hl == DRAW_CURSOR)
2028 {
2029 struct glyph *glyph = s->first_glyph;
2030 int height, max_y;
2031
2032 if (s->x > r.x)
2033 {
2034 if (r.width >= s->x - r.x)
2035 r.width -= s->x - r.x;
2036 else /* R2L hscrolled row with cursor outside text area */
2037 r.width = 0;
2038 r.x = s->x;
2039 }
2040 r.width = min (r.width, glyph->pixel_width);
2041
2042 /* If r.y is below window bottom, ensure that we still see a cursor. */
2043 height = min (glyph->ascent + glyph->descent,
2044 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2045 max_y = window_text_bottom_y (s->w) - height;
2046 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2047 if (s->ybase - glyph->ascent > max_y)
2048 {
2049 r.y = max_y;
2050 r.height = height;
2051 }
2052 else
2053 {
2054 /* Don't draw cursor glyph taller than our actual glyph. */
2055 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2056 if (height < r.height)
2057 {
2058 max_y = r.y + r.height;
2059 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2060 r.height = min (max_y - r.y, height);
2061 }
2062 }
2063 }
2064
2065 if (s->row->clip)
2066 {
2067 XRectangle r_save = r;
2068
2069 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2070 r.width = 0;
2071 }
2072
2073 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2074 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2075 {
2076 #ifdef CONVERT_FROM_XRECT
2077 CONVERT_FROM_XRECT (r, *rects);
2078 #else
2079 *rects = r;
2080 #endif
2081 return 1;
2082 }
2083 else
2084 {
2085 /* If we are processing overlapping and allowed to return
2086 multiple clipping rectangles, we exclude the row of the glyph
2087 string from the clipping rectangle. This is to avoid drawing
2088 the same text on the environment with anti-aliasing. */
2089 #ifdef CONVERT_FROM_XRECT
2090 XRectangle rs[2];
2091 #else
2092 XRectangle *rs = rects;
2093 #endif
2094 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2095
2096 if (s->for_overlaps & OVERLAPS_PRED)
2097 {
2098 rs[i] = r;
2099 if (r.y + r.height > row_y)
2100 {
2101 if (r.y < row_y)
2102 rs[i].height = row_y - r.y;
2103 else
2104 rs[i].height = 0;
2105 }
2106 i++;
2107 }
2108 if (s->for_overlaps & OVERLAPS_SUCC)
2109 {
2110 rs[i] = r;
2111 if (r.y < row_y + s->row->visible_height)
2112 {
2113 if (r.y + r.height > row_y + s->row->visible_height)
2114 {
2115 rs[i].y = row_y + s->row->visible_height;
2116 rs[i].height = r.y + r.height - rs[i].y;
2117 }
2118 else
2119 rs[i].height = 0;
2120 }
2121 i++;
2122 }
2123
2124 n = i;
2125 #ifdef CONVERT_FROM_XRECT
2126 for (i = 0; i < n; i++)
2127 CONVERT_FROM_XRECT (rs[i], rects[i]);
2128 #endif
2129 return n;
2130 }
2131 }
2132
2133 /* EXPORT:
2134 Return in *NR the clipping rectangle for glyph string S. */
2135
2136 void
2137 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2138 {
2139 get_glyph_string_clip_rects (s, nr, 1);
2140 }
2141
2142
2143 /* EXPORT:
2144 Return the position and height of the phys cursor in window W.
2145 Set w->phys_cursor_width to width of phys cursor.
2146 */
2147
2148 void
2149 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2150 struct glyph *glyph, int *xp, int *yp, int *heightp)
2151 {
2152 struct frame *f = XFRAME (WINDOW_FRAME (w));
2153 int x, y, wd, h, h0, y0;
2154
2155 /* Compute the width of the rectangle to draw. If on a stretch
2156 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2157 rectangle as wide as the glyph, but use a canonical character
2158 width instead. */
2159 wd = glyph->pixel_width;
2160
2161 x = w->phys_cursor.x;
2162 if (x < 0)
2163 {
2164 wd += x;
2165 x = 0;
2166 }
2167
2168 if (glyph->type == STRETCH_GLYPH
2169 && !x_stretch_cursor_p)
2170 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2171 w->phys_cursor_width = wd;
2172
2173 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2174
2175 /* If y is below window bottom, ensure that we still see a cursor. */
2176 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2177
2178 h = max (h0, glyph->ascent + glyph->descent);
2179 h0 = min (h0, glyph->ascent + glyph->descent);
2180
2181 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2182 if (y < y0)
2183 {
2184 h = max (h - (y0 - y) + 1, h0);
2185 y = y0 - 1;
2186 }
2187 else
2188 {
2189 y0 = window_text_bottom_y (w) - h0;
2190 if (y > y0)
2191 {
2192 h += y - y0;
2193 y = y0;
2194 }
2195 }
2196
2197 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2198 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2199 *heightp = h;
2200 }
2201
2202 /*
2203 * Remember which glyph the mouse is over.
2204 */
2205
2206 void
2207 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2208 {
2209 Lisp_Object window;
2210 struct window *w;
2211 struct glyph_row *r, *gr, *end_row;
2212 enum window_part part;
2213 enum glyph_row_area area;
2214 int x, y, width, height;
2215
2216 /* Try to determine frame pixel position and size of the glyph under
2217 frame pixel coordinates X/Y on frame F. */
2218
2219 if (window_resize_pixelwise)
2220 {
2221 width = height = 1;
2222 goto virtual_glyph;
2223 }
2224 else if (!f->glyphs_initialized_p
2225 || (window = window_from_coordinates (f, gx, gy, &part, false),
2226 NILP (window)))
2227 {
2228 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2229 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2230 goto virtual_glyph;
2231 }
2232
2233 w = XWINDOW (window);
2234 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2235 height = WINDOW_FRAME_LINE_HEIGHT (w);
2236
2237 x = window_relative_x_coord (w, part, gx);
2238 y = gy - WINDOW_TOP_EDGE_Y (w);
2239
2240 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2241 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2242
2243 if (w->pseudo_window_p)
2244 {
2245 area = TEXT_AREA;
2246 part = ON_MODE_LINE; /* Don't adjust margin. */
2247 goto text_glyph;
2248 }
2249
2250 switch (part)
2251 {
2252 case ON_LEFT_MARGIN:
2253 area = LEFT_MARGIN_AREA;
2254 goto text_glyph;
2255
2256 case ON_RIGHT_MARGIN:
2257 area = RIGHT_MARGIN_AREA;
2258 goto text_glyph;
2259
2260 case ON_HEADER_LINE:
2261 case ON_MODE_LINE:
2262 gr = (part == ON_HEADER_LINE
2263 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2264 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2265 gy = gr->y;
2266 area = TEXT_AREA;
2267 goto text_glyph_row_found;
2268
2269 case ON_TEXT:
2270 area = TEXT_AREA;
2271
2272 text_glyph:
2273 gr = 0; gy = 0;
2274 for (; r <= end_row && r->enabled_p; ++r)
2275 if (r->y + r->height > y)
2276 {
2277 gr = r; gy = r->y;
2278 break;
2279 }
2280
2281 text_glyph_row_found:
2282 if (gr && gy <= y)
2283 {
2284 struct glyph *g = gr->glyphs[area];
2285 struct glyph *end = g + gr->used[area];
2286
2287 height = gr->height;
2288 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2289 if (gx + g->pixel_width > x)
2290 break;
2291
2292 if (g < end)
2293 {
2294 if (g->type == IMAGE_GLYPH)
2295 {
2296 /* Don't remember when mouse is over image, as
2297 image may have hot-spots. */
2298 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2299 return;
2300 }
2301 width = g->pixel_width;
2302 }
2303 else
2304 {
2305 /* Use nominal char spacing at end of line. */
2306 x -= gx;
2307 gx += (x / width) * width;
2308 }
2309
2310 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2311 {
2312 gx += window_box_left_offset (w, area);
2313 /* Don't expand over the modeline to make sure the vertical
2314 drag cursor is shown early enough. */
2315 height = min (height,
2316 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2317 }
2318 }
2319 else
2320 {
2321 /* Use nominal line height at end of window. */
2322 gx = (x / width) * width;
2323 y -= gy;
2324 gy += (y / height) * height;
2325 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2326 /* See comment above. */
2327 height = min (height,
2328 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2329 }
2330 break;
2331
2332 case ON_LEFT_FRINGE:
2333 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2334 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2335 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2336 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2337 goto row_glyph;
2338
2339 case ON_RIGHT_FRINGE:
2340 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2341 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2342 : window_box_right_offset (w, TEXT_AREA));
2343 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2344 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2345 && !WINDOW_RIGHTMOST_P (w))
2346 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2347 /* Make sure the vertical border can get her own glyph to the
2348 right of the one we build here. */
2349 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2350 else
2351 width = WINDOW_PIXEL_WIDTH (w) - gx;
2352 else
2353 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2354
2355 goto row_glyph;
2356
2357 case ON_VERTICAL_BORDER:
2358 gx = WINDOW_PIXEL_WIDTH (w) - width;
2359 goto row_glyph;
2360
2361 case ON_VERTICAL_SCROLL_BAR:
2362 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2363 ? 0
2364 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2365 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2366 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2367 : 0)));
2368 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2369
2370 row_glyph:
2371 gr = 0, gy = 0;
2372 for (; r <= end_row && r->enabled_p; ++r)
2373 if (r->y + r->height > y)
2374 {
2375 gr = r; gy = r->y;
2376 break;
2377 }
2378
2379 if (gr && gy <= y)
2380 height = gr->height;
2381 else
2382 {
2383 /* Use nominal line height at end of window. */
2384 y -= gy;
2385 gy += (y / height) * height;
2386 }
2387 break;
2388
2389 case ON_RIGHT_DIVIDER:
2390 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2391 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2392 gy = 0;
2393 /* The bottom divider prevails. */
2394 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2395 goto add_edge;
2396
2397 case ON_BOTTOM_DIVIDER:
2398 gx = 0;
2399 width = WINDOW_PIXEL_WIDTH (w);
2400 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2401 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2402 goto add_edge;
2403
2404 default:
2405 ;
2406 virtual_glyph:
2407 /* If there is no glyph under the mouse, then we divide the screen
2408 into a grid of the smallest glyph in the frame, and use that
2409 as our "glyph". */
2410
2411 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2412 round down even for negative values. */
2413 if (gx < 0)
2414 gx -= width - 1;
2415 if (gy < 0)
2416 gy -= height - 1;
2417
2418 gx = (gx / width) * width;
2419 gy = (gy / height) * height;
2420
2421 goto store_rect;
2422 }
2423
2424 add_edge:
2425 gx += WINDOW_LEFT_EDGE_X (w);
2426 gy += WINDOW_TOP_EDGE_Y (w);
2427
2428 store_rect:
2429 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2430
2431 /* Visible feedback for debugging. */
2432 #if false && defined HAVE_X_WINDOWS
2433 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2434 f->output_data.x->normal_gc,
2435 gx, gy, width, height);
2436 #endif
2437 }
2438
2439
2440 #endif /* HAVE_WINDOW_SYSTEM */
2441
2442 static void
2443 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2444 {
2445 eassert (w);
2446 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2447 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2448 w->window_end_vpos
2449 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2450 }
2451
2452 /***********************************************************************
2453 Lisp form evaluation
2454 ***********************************************************************/
2455
2456 /* Error handler for safe_eval and safe_call. */
2457
2458 static Lisp_Object
2459 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2460 {
2461 add_to_log ("Error during redisplay: %S signaled %S",
2462 Flist (nargs, args), arg);
2463 return Qnil;
2464 }
2465
2466 /* Call function FUNC with the rest of NARGS - 1 arguments
2467 following. Return the result, or nil if something went
2468 wrong. Prevent redisplay during the evaluation. */
2469
2470 static Lisp_Object
2471 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2472 {
2473 Lisp_Object val;
2474
2475 if (inhibit_eval_during_redisplay)
2476 val = Qnil;
2477 else
2478 {
2479 ptrdiff_t i;
2480 ptrdiff_t count = SPECPDL_INDEX ();
2481 Lisp_Object *args;
2482 USE_SAFE_ALLOCA;
2483 SAFE_ALLOCA_LISP (args, nargs);
2484
2485 args[0] = func;
2486 for (i = 1; i < nargs; i++)
2487 args[i] = va_arg (ap, Lisp_Object);
2488
2489 specbind (Qinhibit_redisplay, Qt);
2490 if (inhibit_quit)
2491 specbind (Qinhibit_quit, Qt);
2492 /* Use Qt to ensure debugger does not run,
2493 so there is no possibility of wanting to redisplay. */
2494 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2495 safe_eval_handler);
2496 SAFE_FREE ();
2497 val = unbind_to (count, val);
2498 }
2499
2500 return val;
2501 }
2502
2503 Lisp_Object
2504 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2505 {
2506 Lisp_Object retval;
2507 va_list ap;
2508
2509 va_start (ap, func);
2510 retval = safe__call (false, nargs, func, ap);
2511 va_end (ap);
2512 return retval;
2513 }
2514
2515 /* Call function FN with one argument ARG.
2516 Return the result, or nil if something went wrong. */
2517
2518 Lisp_Object
2519 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2520 {
2521 return safe_call (2, fn, arg);
2522 }
2523
2524 static Lisp_Object
2525 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2526 {
2527 Lisp_Object retval;
2528 va_list ap;
2529
2530 va_start (ap, fn);
2531 retval = safe__call (inhibit_quit, 2, fn, ap);
2532 va_end (ap);
2533 return retval;
2534 }
2535
2536 Lisp_Object
2537 safe_eval (Lisp_Object sexpr)
2538 {
2539 return safe__call1 (false, Qeval, sexpr);
2540 }
2541
2542 static Lisp_Object
2543 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2544 {
2545 return safe__call1 (inhibit_quit, Qeval, sexpr);
2546 }
2547
2548 /* Call function FN with two arguments ARG1 and ARG2.
2549 Return the result, or nil if something went wrong. */
2550
2551 Lisp_Object
2552 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2553 {
2554 return safe_call (3, fn, arg1, arg2);
2555 }
2556
2557
2558 \f
2559 /***********************************************************************
2560 Debugging
2561 ***********************************************************************/
2562
2563 /* Define CHECK_IT to perform sanity checks on iterators.
2564 This is for debugging. It is too slow to do unconditionally. */
2565
2566 static void
2567 CHECK_IT (struct it *it)
2568 {
2569 #if false
2570 if (it->method == GET_FROM_STRING)
2571 {
2572 eassert (STRINGP (it->string));
2573 eassert (IT_STRING_CHARPOS (*it) >= 0);
2574 }
2575 else
2576 {
2577 eassert (IT_STRING_CHARPOS (*it) < 0);
2578 if (it->method == GET_FROM_BUFFER)
2579 {
2580 /* Check that character and byte positions agree. */
2581 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2582 }
2583 }
2584
2585 if (it->dpvec)
2586 eassert (it->current.dpvec_index >= 0);
2587 else
2588 eassert (it->current.dpvec_index < 0);
2589 #endif
2590 }
2591
2592
2593 /* Check that the window end of window W is what we expect it
2594 to be---the last row in the current matrix displaying text. */
2595
2596 static void
2597 CHECK_WINDOW_END (struct window *w)
2598 {
2599 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2600 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2601 {
2602 struct glyph_row *row;
2603 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2604 !row->enabled_p
2605 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2606 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2607 }
2608 #endif
2609 }
2610
2611 /***********************************************************************
2612 Iterator initialization
2613 ***********************************************************************/
2614
2615 /* Initialize IT for displaying current_buffer in window W, starting
2616 at character position CHARPOS. CHARPOS < 0 means that no buffer
2617 position is specified which is useful when the iterator is assigned
2618 a position later. BYTEPOS is the byte position corresponding to
2619 CHARPOS.
2620
2621 If ROW is not null, calls to produce_glyphs with IT as parameter
2622 will produce glyphs in that row.
2623
2624 BASE_FACE_ID is the id of a base face to use. It must be one of
2625 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2626 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2627 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2628
2629 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2630 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2631 will be initialized to use the corresponding mode line glyph row of
2632 the desired matrix of W. */
2633
2634 void
2635 init_iterator (struct it *it, struct window *w,
2636 ptrdiff_t charpos, ptrdiff_t bytepos,
2637 struct glyph_row *row, enum face_id base_face_id)
2638 {
2639 enum face_id remapped_base_face_id = base_face_id;
2640
2641 /* Some precondition checks. */
2642 eassert (w != NULL && it != NULL);
2643 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2644 && charpos <= ZV));
2645
2646 /* If face attributes have been changed since the last redisplay,
2647 free realized faces now because they depend on face definitions
2648 that might have changed. Don't free faces while there might be
2649 desired matrices pending which reference these faces. */
2650 if (face_change && !inhibit_free_realized_faces)
2651 {
2652 face_change = false;
2653 free_all_realized_faces (Qnil);
2654 }
2655
2656 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2657 if (! NILP (Vface_remapping_alist))
2658 remapped_base_face_id
2659 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2660
2661 /* Use one of the mode line rows of W's desired matrix if
2662 appropriate. */
2663 if (row == NULL)
2664 {
2665 if (base_face_id == MODE_LINE_FACE_ID
2666 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2667 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2668 else if (base_face_id == HEADER_LINE_FACE_ID)
2669 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2670 }
2671
2672 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2673 Other parts of redisplay rely on that. */
2674 memclear (it, sizeof *it);
2675 it->current.overlay_string_index = -1;
2676 it->current.dpvec_index = -1;
2677 it->base_face_id = remapped_base_face_id;
2678 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2679 it->paragraph_embedding = L2R;
2680 it->bidi_it.w = w;
2681
2682 /* The window in which we iterate over current_buffer: */
2683 XSETWINDOW (it->window, w);
2684 it->w = w;
2685 it->f = XFRAME (w->frame);
2686
2687 it->cmp_it.id = -1;
2688
2689 /* Extra space between lines (on window systems only). */
2690 if (base_face_id == DEFAULT_FACE_ID
2691 && FRAME_WINDOW_P (it->f))
2692 {
2693 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2694 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2695 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2696 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2697 * FRAME_LINE_HEIGHT (it->f));
2698 else if (it->f->extra_line_spacing > 0)
2699 it->extra_line_spacing = it->f->extra_line_spacing;
2700 }
2701
2702 /* If realized faces have been removed, e.g. because of face
2703 attribute changes of named faces, recompute them. When running
2704 in batch mode, the face cache of the initial frame is null. If
2705 we happen to get called, make a dummy face cache. */
2706 if (FRAME_FACE_CACHE (it->f) == NULL)
2707 init_frame_faces (it->f);
2708 if (FRAME_FACE_CACHE (it->f)->used == 0)
2709 recompute_basic_faces (it->f);
2710
2711 it->override_ascent = -1;
2712
2713 /* Are control characters displayed as `^C'? */
2714 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2715
2716 /* -1 means everything between a CR and the following line end
2717 is invisible. >0 means lines indented more than this value are
2718 invisible. */
2719 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2720 ? (clip_to_bounds
2721 (-1, XINT (BVAR (current_buffer, selective_display)),
2722 PTRDIFF_MAX))
2723 : (!NILP (BVAR (current_buffer, selective_display))
2724 ? -1 : 0));
2725 it->selective_display_ellipsis_p
2726 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2727
2728 /* Display table to use. */
2729 it->dp = window_display_table (w);
2730
2731 /* Are multibyte characters enabled in current_buffer? */
2732 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2733
2734 /* Get the position at which the redisplay_end_trigger hook should
2735 be run, if it is to be run at all. */
2736 if (MARKERP (w->redisplay_end_trigger)
2737 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2738 it->redisplay_end_trigger_charpos
2739 = marker_position (w->redisplay_end_trigger);
2740 else if (INTEGERP (w->redisplay_end_trigger))
2741 it->redisplay_end_trigger_charpos
2742 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2743 PTRDIFF_MAX);
2744
2745 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2746
2747 /* Are lines in the display truncated? */
2748 if (TRUNCATE != 0)
2749 it->line_wrap = TRUNCATE;
2750 if (base_face_id == DEFAULT_FACE_ID
2751 && !it->w->hscroll
2752 && (WINDOW_FULL_WIDTH_P (it->w)
2753 || NILP (Vtruncate_partial_width_windows)
2754 || (INTEGERP (Vtruncate_partial_width_windows)
2755 /* PXW: Shall we do something about this? */
2756 && (XINT (Vtruncate_partial_width_windows)
2757 <= WINDOW_TOTAL_COLS (it->w))))
2758 && NILP (BVAR (current_buffer, truncate_lines)))
2759 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2760 ? WINDOW_WRAP : WORD_WRAP;
2761
2762 /* Get dimensions of truncation and continuation glyphs. These are
2763 displayed as fringe bitmaps under X, but we need them for such
2764 frames when the fringes are turned off. But leave the dimensions
2765 zero for tooltip frames, as these glyphs look ugly there and also
2766 sabotage calculations of tooltip dimensions in x-show-tip. */
2767 #ifdef HAVE_WINDOW_SYSTEM
2768 if (!(FRAME_WINDOW_P (it->f)
2769 && FRAMEP (tip_frame)
2770 && it->f == XFRAME (tip_frame)))
2771 #endif
2772 {
2773 if (it->line_wrap == TRUNCATE)
2774 {
2775 /* We will need the truncation glyph. */
2776 eassert (it->glyph_row == NULL);
2777 produce_special_glyphs (it, IT_TRUNCATION);
2778 it->truncation_pixel_width = it->pixel_width;
2779 }
2780 else
2781 {
2782 /* We will need the continuation glyph. */
2783 eassert (it->glyph_row == NULL);
2784 produce_special_glyphs (it, IT_CONTINUATION);
2785 it->continuation_pixel_width = it->pixel_width;
2786 }
2787 }
2788
2789 /* Reset these values to zero because the produce_special_glyphs
2790 above has changed them. */
2791 it->pixel_width = it->ascent = it->descent = 0;
2792 it->phys_ascent = it->phys_descent = 0;
2793
2794 /* Set this after getting the dimensions of truncation and
2795 continuation glyphs, so that we don't produce glyphs when calling
2796 produce_special_glyphs, above. */
2797 it->glyph_row = row;
2798 it->area = TEXT_AREA;
2799
2800 /* Get the dimensions of the display area. The display area
2801 consists of the visible window area plus a horizontally scrolled
2802 part to the left of the window. All x-values are relative to the
2803 start of this total display area. */
2804 if (base_face_id != DEFAULT_FACE_ID)
2805 {
2806 /* Mode lines, menu bar in terminal frames. */
2807 it->first_visible_x = 0;
2808 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2809 }
2810 else
2811 {
2812 it->first_visible_x
2813 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2814 it->last_visible_x = (it->first_visible_x
2815 + window_box_width (w, TEXT_AREA));
2816
2817 /* If we truncate lines, leave room for the truncation glyph(s) at
2818 the right margin. Otherwise, leave room for the continuation
2819 glyph(s). Done only if the window has no right fringe. */
2820 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2821 {
2822 if (it->line_wrap == TRUNCATE)
2823 it->last_visible_x -= it->truncation_pixel_width;
2824 else
2825 it->last_visible_x -= it->continuation_pixel_width;
2826 }
2827
2828 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2829 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2830 }
2831
2832 /* Leave room for a border glyph. */
2833 if (!FRAME_WINDOW_P (it->f)
2834 && !WINDOW_RIGHTMOST_P (it->w))
2835 it->last_visible_x -= 1;
2836
2837 it->last_visible_y = window_text_bottom_y (w);
2838
2839 /* For mode lines and alike, arrange for the first glyph having a
2840 left box line if the face specifies a box. */
2841 if (base_face_id != DEFAULT_FACE_ID)
2842 {
2843 struct face *face;
2844
2845 it->face_id = remapped_base_face_id;
2846
2847 /* If we have a boxed mode line, make the first character appear
2848 with a left box line. */
2849 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2850 if (face && face->box != FACE_NO_BOX)
2851 it->start_of_box_run_p = true;
2852 }
2853
2854 /* If a buffer position was specified, set the iterator there,
2855 getting overlays and face properties from that position. */
2856 if (charpos >= BUF_BEG (current_buffer))
2857 {
2858 it->stop_charpos = charpos;
2859 it->end_charpos = ZV;
2860 eassert (charpos == BYTE_TO_CHAR (bytepos));
2861 IT_CHARPOS (*it) = charpos;
2862 IT_BYTEPOS (*it) = bytepos;
2863
2864 /* We will rely on `reseat' to set this up properly, via
2865 handle_face_prop. */
2866 it->face_id = it->base_face_id;
2867
2868 it->start = it->current;
2869 /* Do we need to reorder bidirectional text? Not if this is a
2870 unibyte buffer: by definition, none of the single-byte
2871 characters are strong R2L, so no reordering is needed. And
2872 bidi.c doesn't support unibyte buffers anyway. Also, don't
2873 reorder while we are loading loadup.el, since the tables of
2874 character properties needed for reordering are not yet
2875 available. */
2876 it->bidi_p =
2877 NILP (Vpurify_flag)
2878 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2879 && it->multibyte_p;
2880
2881 /* If we are to reorder bidirectional text, init the bidi
2882 iterator. */
2883 if (it->bidi_p)
2884 {
2885 /* Since we don't know at this point whether there will be
2886 any R2L lines in the window, we reserve space for
2887 truncation/continuation glyphs even if only the left
2888 fringe is absent. */
2889 if (base_face_id == DEFAULT_FACE_ID
2890 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2891 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2892 {
2893 if (it->line_wrap == TRUNCATE)
2894 it->last_visible_x -= it->truncation_pixel_width;
2895 else
2896 it->last_visible_x -= it->continuation_pixel_width;
2897 }
2898 /* Note the paragraph direction that this buffer wants to
2899 use. */
2900 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2901 Qleft_to_right))
2902 it->paragraph_embedding = L2R;
2903 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2904 Qright_to_left))
2905 it->paragraph_embedding = R2L;
2906 else
2907 it->paragraph_embedding = NEUTRAL_DIR;
2908 bidi_unshelve_cache (NULL, false);
2909 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2910 &it->bidi_it);
2911 }
2912
2913 /* Compute faces etc. */
2914 reseat (it, it->current.pos, true);
2915 }
2916
2917 CHECK_IT (it);
2918 }
2919
2920
2921 /* Initialize IT for the display of window W with window start POS. */
2922
2923 void
2924 start_display (struct it *it, struct window *w, struct text_pos pos)
2925 {
2926 struct glyph_row *row;
2927 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
2928
2929 row = w->desired_matrix->rows + first_vpos;
2930 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2931 it->first_vpos = first_vpos;
2932
2933 /* Don't reseat to previous visible line start if current start
2934 position is in a string or image. */
2935 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2936 {
2937 int first_y = it->current_y;
2938
2939 /* If window start is not at a line start, skip forward to POS to
2940 get the correct continuation lines width. */
2941 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
2942 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2943 if (!start_at_line_beg_p)
2944 {
2945 int new_x;
2946
2947 reseat_at_previous_visible_line_start (it);
2948 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2949
2950 new_x = it->current_x + it->pixel_width;
2951
2952 /* If lines are continued, this line may end in the middle
2953 of a multi-glyph character (e.g. a control character
2954 displayed as \003, or in the middle of an overlay
2955 string). In this case move_it_to above will not have
2956 taken us to the start of the continuation line but to the
2957 end of the continued line. */
2958 if (it->current_x > 0
2959 && it->line_wrap != TRUNCATE /* Lines are continued. */
2960 && (/* And glyph doesn't fit on the line. */
2961 new_x > it->last_visible_x
2962 /* Or it fits exactly and we're on a window
2963 system frame. */
2964 || (new_x == it->last_visible_x
2965 && FRAME_WINDOW_P (it->f)
2966 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2967 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2968 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2969 {
2970 if ((it->current.dpvec_index >= 0
2971 || it->current.overlay_string_index >= 0)
2972 /* If we are on a newline from a display vector or
2973 overlay string, then we are already at the end of
2974 a screen line; no need to go to the next line in
2975 that case, as this line is not really continued.
2976 (If we do go to the next line, C-e will not DTRT.) */
2977 && it->c != '\n')
2978 {
2979 set_iterator_to_next (it, true);
2980 move_it_in_display_line_to (it, -1, -1, 0);
2981 }
2982
2983 it->continuation_lines_width += it->current_x;
2984 }
2985 /* If the character at POS is displayed via a display
2986 vector, move_it_to above stops at the final glyph of
2987 IT->dpvec. To make the caller redisplay that character
2988 again (a.k.a. start at POS), we need to reset the
2989 dpvec_index to the beginning of IT->dpvec. */
2990 else if (it->current.dpvec_index >= 0)
2991 it->current.dpvec_index = 0;
2992
2993 /* We're starting a new display line, not affected by the
2994 height of the continued line, so clear the appropriate
2995 fields in the iterator structure. */
2996 it->max_ascent = it->max_descent = 0;
2997 it->max_phys_ascent = it->max_phys_descent = 0;
2998
2999 it->current_y = first_y;
3000 it->vpos = 0;
3001 it->current_x = it->hpos = 0;
3002 }
3003 }
3004 }
3005
3006
3007 /* Return true if POS is a position in ellipses displayed for invisible
3008 text. W is the window we display, for text property lookup. */
3009
3010 static bool
3011 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3012 {
3013 Lisp_Object prop, window;
3014 bool ellipses_p = false;
3015 ptrdiff_t charpos = CHARPOS (pos->pos);
3016
3017 /* If POS specifies a position in a display vector, this might
3018 be for an ellipsis displayed for invisible text. We won't
3019 get the iterator set up for delivering that ellipsis unless
3020 we make sure that it gets aware of the invisible text. */
3021 if (pos->dpvec_index >= 0
3022 && pos->overlay_string_index < 0
3023 && CHARPOS (pos->string_pos) < 0
3024 && charpos > BEGV
3025 && (XSETWINDOW (window, w),
3026 prop = Fget_char_property (make_number (charpos),
3027 Qinvisible, window),
3028 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3029 {
3030 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3031 window);
3032 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3033 }
3034
3035 return ellipses_p;
3036 }
3037
3038
3039 /* Initialize IT for stepping through current_buffer in window W,
3040 starting at position POS that includes overlay string and display
3041 vector/ control character translation position information. Value
3042 is false if there are overlay strings with newlines at POS. */
3043
3044 static bool
3045 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3046 {
3047 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3048 int i;
3049 bool overlay_strings_with_newlines = false;
3050
3051 /* If POS specifies a position in a display vector, this might
3052 be for an ellipsis displayed for invisible text. We won't
3053 get the iterator set up for delivering that ellipsis unless
3054 we make sure that it gets aware of the invisible text. */
3055 if (in_ellipses_for_invisible_text_p (pos, w))
3056 {
3057 --charpos;
3058 bytepos = 0;
3059 }
3060
3061 /* Keep in mind: the call to reseat in init_iterator skips invisible
3062 text, so we might end up at a position different from POS. This
3063 is only a problem when POS is a row start after a newline and an
3064 overlay starts there with an after-string, and the overlay has an
3065 invisible property. Since we don't skip invisible text in
3066 display_line and elsewhere immediately after consuming the
3067 newline before the row start, such a POS will not be in a string,
3068 but the call to init_iterator below will move us to the
3069 after-string. */
3070 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3071
3072 /* This only scans the current chunk -- it should scan all chunks.
3073 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3074 to 16 in 22.1 to make this a lesser problem. */
3075 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3076 {
3077 const char *s = SSDATA (it->overlay_strings[i]);
3078 const char *e = s + SBYTES (it->overlay_strings[i]);
3079
3080 while (s < e && *s != '\n')
3081 ++s;
3082
3083 if (s < e)
3084 {
3085 overlay_strings_with_newlines = true;
3086 break;
3087 }
3088 }
3089
3090 /* If position is within an overlay string, set up IT to the right
3091 overlay string. */
3092 if (pos->overlay_string_index >= 0)
3093 {
3094 int relative_index;
3095
3096 /* If the first overlay string happens to have a `display'
3097 property for an image, the iterator will be set up for that
3098 image, and we have to undo that setup first before we can
3099 correct the overlay string index. */
3100 if (it->method == GET_FROM_IMAGE)
3101 pop_it (it);
3102
3103 /* We already have the first chunk of overlay strings in
3104 IT->overlay_strings. Load more until the one for
3105 pos->overlay_string_index is in IT->overlay_strings. */
3106 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3107 {
3108 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3109 it->current.overlay_string_index = 0;
3110 while (n--)
3111 {
3112 load_overlay_strings (it, 0);
3113 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3114 }
3115 }
3116
3117 it->current.overlay_string_index = pos->overlay_string_index;
3118 relative_index = (it->current.overlay_string_index
3119 % OVERLAY_STRING_CHUNK_SIZE);
3120 it->string = it->overlay_strings[relative_index];
3121 eassert (STRINGP (it->string));
3122 it->current.string_pos = pos->string_pos;
3123 it->method = GET_FROM_STRING;
3124 it->end_charpos = SCHARS (it->string);
3125 /* Set up the bidi iterator for this overlay string. */
3126 if (it->bidi_p)
3127 {
3128 it->bidi_it.string.lstring = it->string;
3129 it->bidi_it.string.s = NULL;
3130 it->bidi_it.string.schars = SCHARS (it->string);
3131 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3132 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3133 it->bidi_it.string.unibyte = !it->multibyte_p;
3134 it->bidi_it.w = it->w;
3135 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3136 FRAME_WINDOW_P (it->f), &it->bidi_it);
3137
3138 /* Synchronize the state of the bidi iterator with
3139 pos->string_pos. For any string position other than
3140 zero, this will be done automagically when we resume
3141 iteration over the string and get_visually_first_element
3142 is called. But if string_pos is zero, and the string is
3143 to be reordered for display, we need to resync manually,
3144 since it could be that the iteration state recorded in
3145 pos ended at string_pos of 0 moving backwards in string. */
3146 if (CHARPOS (pos->string_pos) == 0)
3147 {
3148 get_visually_first_element (it);
3149 if (IT_STRING_CHARPOS (*it) != 0)
3150 do {
3151 /* Paranoia. */
3152 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3153 bidi_move_to_visually_next (&it->bidi_it);
3154 } while (it->bidi_it.charpos != 0);
3155 }
3156 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3157 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3158 }
3159 }
3160
3161 if (CHARPOS (pos->string_pos) >= 0)
3162 {
3163 /* Recorded position is not in an overlay string, but in another
3164 string. This can only be a string from a `display' property.
3165 IT should already be filled with that string. */
3166 it->current.string_pos = pos->string_pos;
3167 eassert (STRINGP (it->string));
3168 if (it->bidi_p)
3169 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3170 FRAME_WINDOW_P (it->f), &it->bidi_it);
3171 }
3172
3173 /* Restore position in display vector translations, control
3174 character translations or ellipses. */
3175 if (pos->dpvec_index >= 0)
3176 {
3177 if (it->dpvec == NULL)
3178 get_next_display_element (it);
3179 eassert (it->dpvec && it->current.dpvec_index == 0);
3180 it->current.dpvec_index = pos->dpvec_index;
3181 }
3182
3183 CHECK_IT (it);
3184 return !overlay_strings_with_newlines;
3185 }
3186
3187
3188 /* Initialize IT for stepping through current_buffer in window W
3189 starting at ROW->start. */
3190
3191 static void
3192 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3193 {
3194 init_from_display_pos (it, w, &row->start);
3195 it->start = row->start;
3196 it->continuation_lines_width = row->continuation_lines_width;
3197 CHECK_IT (it);
3198 }
3199
3200
3201 /* Initialize IT for stepping through current_buffer in window W
3202 starting in the line following ROW, i.e. starting at ROW->end.
3203 Value is false if there are overlay strings with newlines at ROW's
3204 end position. */
3205
3206 static bool
3207 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3208 {
3209 bool success = false;
3210
3211 if (init_from_display_pos (it, w, &row->end))
3212 {
3213 if (row->continued_p)
3214 it->continuation_lines_width
3215 = row->continuation_lines_width + row->pixel_width;
3216 CHECK_IT (it);
3217 success = true;
3218 }
3219
3220 return success;
3221 }
3222
3223
3224
3225 \f
3226 /***********************************************************************
3227 Text properties
3228 ***********************************************************************/
3229
3230 /* Called when IT reaches IT->stop_charpos. Handle text property and
3231 overlay changes. Set IT->stop_charpos to the next position where
3232 to stop. */
3233
3234 static void
3235 handle_stop (struct it *it)
3236 {
3237 enum prop_handled handled;
3238 bool handle_overlay_change_p;
3239 struct props *p;
3240
3241 it->dpvec = NULL;
3242 it->current.dpvec_index = -1;
3243 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3244 it->ellipsis_p = false;
3245
3246 /* Use face of preceding text for ellipsis (if invisible) */
3247 if (it->selective_display_ellipsis_p)
3248 it->saved_face_id = it->face_id;
3249
3250 /* Here's the description of the semantics of, and the logic behind,
3251 the various HANDLED_* statuses:
3252
3253 HANDLED_NORMALLY means the handler did its job, and the loop
3254 should proceed to calling the next handler in order.
3255
3256 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3257 change in the properties and overlays at current position, so the
3258 loop should be restarted, to re-invoke the handlers that were
3259 already called. This happens when fontification-functions were
3260 called by handle_fontified_prop, and actually fontified
3261 something. Another case where HANDLED_RECOMPUTE_PROPS is
3262 returned is when we discover overlay strings that need to be
3263 displayed right away. The loop below will continue for as long
3264 as the status is HANDLED_RECOMPUTE_PROPS.
3265
3266 HANDLED_RETURN means return immediately to the caller, to
3267 continue iteration without calling any further handlers. This is
3268 used when we need to act on some property right away, for example
3269 when we need to display the ellipsis or a replacing display
3270 property, such as display string or image.
3271
3272 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3273 consumed, and the handler switched to the next overlay string.
3274 This signals the loop below to refrain from looking for more
3275 overlays before all the overlay strings of the current overlay
3276 are processed.
3277
3278 Some of the handlers called by the loop push the iterator state
3279 onto the stack (see 'push_it'), and arrange for the iteration to
3280 continue with another object, such as an image, a display string,
3281 or an overlay string. In most such cases, it->stop_charpos is
3282 set to the first character of the string, so that when the
3283 iteration resumes, this function will immediately be called
3284 again, to examine the properties at the beginning of the string.
3285
3286 When a display or overlay string is exhausted, the iterator state
3287 is popped (see 'pop_it'), and iteration continues with the
3288 previous object. Again, in many such cases this function is
3289 called again to find the next position where properties might
3290 change. */
3291
3292 do
3293 {
3294 handled = HANDLED_NORMALLY;
3295
3296 /* Call text property handlers. */
3297 for (p = it_props; p->handler; ++p)
3298 {
3299 handled = p->handler (it);
3300
3301 if (handled == HANDLED_RECOMPUTE_PROPS)
3302 break;
3303 else if (handled == HANDLED_RETURN)
3304 {
3305 /* We still want to show before and after strings from
3306 overlays even if the actual buffer text is replaced. */
3307 if (!handle_overlay_change_p
3308 || it->sp > 1
3309 /* Don't call get_overlay_strings_1 if we already
3310 have overlay strings loaded, because doing so
3311 will load them again and push the iterator state
3312 onto the stack one more time, which is not
3313 expected by the rest of the code that processes
3314 overlay strings. */
3315 || (it->current.overlay_string_index < 0
3316 && !get_overlay_strings_1 (it, 0, false)))
3317 {
3318 if (it->ellipsis_p)
3319 setup_for_ellipsis (it, 0);
3320 /* When handling a display spec, we might load an
3321 empty string. In that case, discard it here. We
3322 used to discard it in handle_single_display_spec,
3323 but that causes get_overlay_strings_1, above, to
3324 ignore overlay strings that we must check. */
3325 if (STRINGP (it->string) && !SCHARS (it->string))
3326 pop_it (it);
3327 return;
3328 }
3329 else if (STRINGP (it->string) && !SCHARS (it->string))
3330 pop_it (it);
3331 else
3332 {
3333 it->string_from_display_prop_p = false;
3334 it->from_disp_prop_p = false;
3335 handle_overlay_change_p = false;
3336 }
3337 handled = HANDLED_RECOMPUTE_PROPS;
3338 break;
3339 }
3340 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3341 handle_overlay_change_p = false;
3342 }
3343
3344 if (handled != HANDLED_RECOMPUTE_PROPS)
3345 {
3346 /* Don't check for overlay strings below when set to deliver
3347 characters from a display vector. */
3348 if (it->method == GET_FROM_DISPLAY_VECTOR)
3349 handle_overlay_change_p = false;
3350
3351 /* Handle overlay changes.
3352 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3353 if it finds overlays. */
3354 if (handle_overlay_change_p)
3355 handled = handle_overlay_change (it);
3356 }
3357
3358 if (it->ellipsis_p)
3359 {
3360 setup_for_ellipsis (it, 0);
3361 break;
3362 }
3363 }
3364 while (handled == HANDLED_RECOMPUTE_PROPS);
3365
3366 /* Determine where to stop next. */
3367 if (handled == HANDLED_NORMALLY)
3368 compute_stop_pos (it);
3369 }
3370
3371
3372 /* Compute IT->stop_charpos from text property and overlay change
3373 information for IT's current position. */
3374
3375 static void
3376 compute_stop_pos (struct it *it)
3377 {
3378 register INTERVAL iv, next_iv;
3379 Lisp_Object object, limit, position;
3380 ptrdiff_t charpos, bytepos;
3381
3382 if (STRINGP (it->string))
3383 {
3384 /* Strings are usually short, so don't limit the search for
3385 properties. */
3386 it->stop_charpos = it->end_charpos;
3387 object = it->string;
3388 limit = Qnil;
3389 charpos = IT_STRING_CHARPOS (*it);
3390 bytepos = IT_STRING_BYTEPOS (*it);
3391 }
3392 else
3393 {
3394 ptrdiff_t pos;
3395
3396 /* If end_charpos is out of range for some reason, such as a
3397 misbehaving display function, rationalize it (Bug#5984). */
3398 if (it->end_charpos > ZV)
3399 it->end_charpos = ZV;
3400 it->stop_charpos = it->end_charpos;
3401
3402 /* If next overlay change is in front of the current stop pos
3403 (which is IT->end_charpos), stop there. Note: value of
3404 next_overlay_change is point-max if no overlay change
3405 follows. */
3406 charpos = IT_CHARPOS (*it);
3407 bytepos = IT_BYTEPOS (*it);
3408 pos = next_overlay_change (charpos);
3409 if (pos < it->stop_charpos)
3410 it->stop_charpos = pos;
3411
3412 /* Set up variables for computing the stop position from text
3413 property changes. */
3414 XSETBUFFER (object, current_buffer);
3415 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3416 }
3417
3418 /* Get the interval containing IT's position. Value is a null
3419 interval if there isn't such an interval. */
3420 position = make_number (charpos);
3421 iv = validate_interval_range (object, &position, &position, false);
3422 if (iv)
3423 {
3424 Lisp_Object values_here[LAST_PROP_IDX];
3425 struct props *p;
3426
3427 /* Get properties here. */
3428 for (p = it_props; p->handler; ++p)
3429 values_here[p->idx] = textget (iv->plist,
3430 builtin_lisp_symbol (p->name));
3431
3432 /* Look for an interval following iv that has different
3433 properties. */
3434 for (next_iv = next_interval (iv);
3435 (next_iv
3436 && (NILP (limit)
3437 || XFASTINT (limit) > next_iv->position));
3438 next_iv = next_interval (next_iv))
3439 {
3440 for (p = it_props; p->handler; ++p)
3441 {
3442 Lisp_Object new_value = textget (next_iv->plist,
3443 builtin_lisp_symbol (p->name));
3444 if (!EQ (values_here[p->idx], new_value))
3445 break;
3446 }
3447
3448 if (p->handler)
3449 break;
3450 }
3451
3452 if (next_iv)
3453 {
3454 if (INTEGERP (limit)
3455 && next_iv->position >= XFASTINT (limit))
3456 /* No text property change up to limit. */
3457 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3458 else
3459 /* Text properties change in next_iv. */
3460 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3461 }
3462 }
3463
3464 if (it->cmp_it.id < 0)
3465 {
3466 ptrdiff_t stoppos = it->end_charpos;
3467
3468 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3469 stoppos = -1;
3470 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3471 stoppos, it->string);
3472 }
3473
3474 eassert (STRINGP (it->string)
3475 || (it->stop_charpos >= BEGV
3476 && it->stop_charpos >= IT_CHARPOS (*it)));
3477 }
3478
3479
3480 /* Return the position of the next overlay change after POS in
3481 current_buffer. Value is point-max if no overlay change
3482 follows. This is like `next-overlay-change' but doesn't use
3483 xmalloc. */
3484
3485 static ptrdiff_t
3486 next_overlay_change (ptrdiff_t pos)
3487 {
3488 ptrdiff_t i, noverlays;
3489 ptrdiff_t endpos;
3490 Lisp_Object *overlays;
3491 USE_SAFE_ALLOCA;
3492
3493 /* Get all overlays at the given position. */
3494 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3495
3496 /* If any of these overlays ends before endpos,
3497 use its ending point instead. */
3498 for (i = 0; i < noverlays; ++i)
3499 {
3500 Lisp_Object oend;
3501 ptrdiff_t oendpos;
3502
3503 oend = OVERLAY_END (overlays[i]);
3504 oendpos = OVERLAY_POSITION (oend);
3505 endpos = min (endpos, oendpos);
3506 }
3507
3508 SAFE_FREE ();
3509 return endpos;
3510 }
3511
3512 /* How many characters forward to search for a display property or
3513 display string. Searching too far forward makes the bidi display
3514 sluggish, especially in small windows. */
3515 #define MAX_DISP_SCAN 250
3516
3517 /* Return the character position of a display string at or after
3518 position specified by POSITION. If no display string exists at or
3519 after POSITION, return ZV. A display string is either an overlay
3520 with `display' property whose value is a string, or a `display'
3521 text property whose value is a string. STRING is data about the
3522 string to iterate; if STRING->lstring is nil, we are iterating a
3523 buffer. FRAME_WINDOW_P is true when we are displaying a window
3524 on a GUI frame. DISP_PROP is set to zero if we searched
3525 MAX_DISP_SCAN characters forward without finding any display
3526 strings, non-zero otherwise. It is set to 2 if the display string
3527 uses any kind of `(space ...)' spec that will produce a stretch of
3528 white space in the text area. */
3529 ptrdiff_t
3530 compute_display_string_pos (struct text_pos *position,
3531 struct bidi_string_data *string,
3532 struct window *w,
3533 bool frame_window_p, int *disp_prop)
3534 {
3535 /* OBJECT = nil means current buffer. */
3536 Lisp_Object object, object1;
3537 Lisp_Object pos, spec, limpos;
3538 bool string_p = string && (STRINGP (string->lstring) || string->s);
3539 ptrdiff_t eob = string_p ? string->schars : ZV;
3540 ptrdiff_t begb = string_p ? 0 : BEGV;
3541 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3542 ptrdiff_t lim =
3543 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3544 struct text_pos tpos;
3545 int rv = 0;
3546
3547 if (string && STRINGP (string->lstring))
3548 object1 = object = string->lstring;
3549 else if (w && !string_p)
3550 {
3551 XSETWINDOW (object, w);
3552 object1 = Qnil;
3553 }
3554 else
3555 object1 = object = Qnil;
3556
3557 *disp_prop = 1;
3558
3559 if (charpos >= eob
3560 /* We don't support display properties whose values are strings
3561 that have display string properties. */
3562 || string->from_disp_str
3563 /* C strings cannot have display properties. */
3564 || (string->s && !STRINGP (object)))
3565 {
3566 *disp_prop = 0;
3567 return eob;
3568 }
3569
3570 /* If the character at CHARPOS is where the display string begins,
3571 return CHARPOS. */
3572 pos = make_number (charpos);
3573 if (STRINGP (object))
3574 bufpos = string->bufpos;
3575 else
3576 bufpos = charpos;
3577 tpos = *position;
3578 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3579 && (charpos <= begb
3580 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3581 object),
3582 spec))
3583 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3584 frame_window_p)))
3585 {
3586 if (rv == 2)
3587 *disp_prop = 2;
3588 return charpos;
3589 }
3590
3591 /* Look forward for the first character with a `display' property
3592 that will replace the underlying text when displayed. */
3593 limpos = make_number (lim);
3594 do {
3595 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3596 CHARPOS (tpos) = XFASTINT (pos);
3597 if (CHARPOS (tpos) >= lim)
3598 {
3599 *disp_prop = 0;
3600 break;
3601 }
3602 if (STRINGP (object))
3603 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3604 else
3605 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3606 spec = Fget_char_property (pos, Qdisplay, object);
3607 if (!STRINGP (object))
3608 bufpos = CHARPOS (tpos);
3609 } while (NILP (spec)
3610 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3611 bufpos, frame_window_p)));
3612 if (rv == 2)
3613 *disp_prop = 2;
3614
3615 return CHARPOS (tpos);
3616 }
3617
3618 /* Return the character position of the end of the display string that
3619 started at CHARPOS. If there's no display string at CHARPOS,
3620 return -1. A display string is either an overlay with `display'
3621 property whose value is a string or a `display' text property whose
3622 value is a string. */
3623 ptrdiff_t
3624 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3625 {
3626 /* OBJECT = nil means current buffer. */
3627 Lisp_Object object =
3628 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3629 Lisp_Object pos = make_number (charpos);
3630 ptrdiff_t eob =
3631 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3632
3633 if (charpos >= eob || (string->s && !STRINGP (object)))
3634 return eob;
3635
3636 /* It could happen that the display property or overlay was removed
3637 since we found it in compute_display_string_pos above. One way
3638 this can happen is if JIT font-lock was called (through
3639 handle_fontified_prop), and jit-lock-functions remove text
3640 properties or overlays from the portion of buffer that includes
3641 CHARPOS. Muse mode is known to do that, for example. In this
3642 case, we return -1 to the caller, to signal that no display
3643 string is actually present at CHARPOS. See bidi_fetch_char for
3644 how this is handled.
3645
3646 An alternative would be to never look for display properties past
3647 it->stop_charpos. But neither compute_display_string_pos nor
3648 bidi_fetch_char that calls it know or care where the next
3649 stop_charpos is. */
3650 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3651 return -1;
3652
3653 /* Look forward for the first character where the `display' property
3654 changes. */
3655 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3656
3657 return XFASTINT (pos);
3658 }
3659
3660
3661 \f
3662 /***********************************************************************
3663 Fontification
3664 ***********************************************************************/
3665
3666 /* Handle changes in the `fontified' property of the current buffer by
3667 calling hook functions from Qfontification_functions to fontify
3668 regions of text. */
3669
3670 static enum prop_handled
3671 handle_fontified_prop (struct it *it)
3672 {
3673 Lisp_Object prop, pos;
3674 enum prop_handled handled = HANDLED_NORMALLY;
3675
3676 if (!NILP (Vmemory_full))
3677 return handled;
3678
3679 /* Get the value of the `fontified' property at IT's current buffer
3680 position. (The `fontified' property doesn't have a special
3681 meaning in strings.) If the value is nil, call functions from
3682 Qfontification_functions. */
3683 if (!STRINGP (it->string)
3684 && it->s == NULL
3685 && !NILP (Vfontification_functions)
3686 && !NILP (Vrun_hooks)
3687 && (pos = make_number (IT_CHARPOS (*it)),
3688 prop = Fget_char_property (pos, Qfontified, Qnil),
3689 /* Ignore the special cased nil value always present at EOB since
3690 no amount of fontifying will be able to change it. */
3691 NILP (prop) && IT_CHARPOS (*it) < Z))
3692 {
3693 ptrdiff_t count = SPECPDL_INDEX ();
3694 Lisp_Object val;
3695 struct buffer *obuf = current_buffer;
3696 ptrdiff_t begv = BEGV, zv = ZV;
3697 bool old_clip_changed = current_buffer->clip_changed;
3698
3699 val = Vfontification_functions;
3700 specbind (Qfontification_functions, Qnil);
3701
3702 eassert (it->end_charpos == ZV);
3703
3704 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3705 safe_call1 (val, pos);
3706 else
3707 {
3708 Lisp_Object fns, fn;
3709 struct gcpro gcpro1, gcpro2;
3710
3711 fns = Qnil;
3712 GCPRO2 (val, fns);
3713
3714 for (; CONSP (val); val = XCDR (val))
3715 {
3716 fn = XCAR (val);
3717
3718 if (EQ (fn, Qt))
3719 {
3720 /* A value of t indicates this hook has a local
3721 binding; it means to run the global binding too.
3722 In a global value, t should not occur. If it
3723 does, we must ignore it to avoid an endless
3724 loop. */
3725 for (fns = Fdefault_value (Qfontification_functions);
3726 CONSP (fns);
3727 fns = XCDR (fns))
3728 {
3729 fn = XCAR (fns);
3730 if (!EQ (fn, Qt))
3731 safe_call1 (fn, pos);
3732 }
3733 }
3734 else
3735 safe_call1 (fn, pos);
3736 }
3737
3738 UNGCPRO;
3739 }
3740
3741 unbind_to (count, Qnil);
3742
3743 /* Fontification functions routinely call `save-restriction'.
3744 Normally, this tags clip_changed, which can confuse redisplay
3745 (see discussion in Bug#6671). Since we don't perform any
3746 special handling of fontification changes in the case where
3747 `save-restriction' isn't called, there's no point doing so in
3748 this case either. So, if the buffer's restrictions are
3749 actually left unchanged, reset clip_changed. */
3750 if (obuf == current_buffer)
3751 {
3752 if (begv == BEGV && zv == ZV)
3753 current_buffer->clip_changed = old_clip_changed;
3754 }
3755 /* There isn't much we can reasonably do to protect against
3756 misbehaving fontification, but here's a fig leaf. */
3757 else if (BUFFER_LIVE_P (obuf))
3758 set_buffer_internal_1 (obuf);
3759
3760 /* The fontification code may have added/removed text.
3761 It could do even a lot worse, but let's at least protect against
3762 the most obvious case where only the text past `pos' gets changed',
3763 as is/was done in grep.el where some escapes sequences are turned
3764 into face properties (bug#7876). */
3765 it->end_charpos = ZV;
3766
3767 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3768 something. This avoids an endless loop if they failed to
3769 fontify the text for which reason ever. */
3770 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3771 handled = HANDLED_RECOMPUTE_PROPS;
3772 }
3773
3774 return handled;
3775 }
3776
3777
3778 \f
3779 /***********************************************************************
3780 Faces
3781 ***********************************************************************/
3782
3783 /* Set up iterator IT from face properties at its current position.
3784 Called from handle_stop. */
3785
3786 static enum prop_handled
3787 handle_face_prop (struct it *it)
3788 {
3789 int new_face_id;
3790 ptrdiff_t next_stop;
3791
3792 if (!STRINGP (it->string))
3793 {
3794 new_face_id
3795 = face_at_buffer_position (it->w,
3796 IT_CHARPOS (*it),
3797 &next_stop,
3798 (IT_CHARPOS (*it)
3799 + TEXT_PROP_DISTANCE_LIMIT),
3800 false, it->base_face_id);
3801
3802 /* Is this a start of a run of characters with box face?
3803 Caveat: this can be called for a freshly initialized
3804 iterator; face_id is -1 in this case. We know that the new
3805 face will not change until limit, i.e. if the new face has a
3806 box, all characters up to limit will have one. But, as
3807 usual, we don't know whether limit is really the end. */
3808 if (new_face_id != it->face_id)
3809 {
3810 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3811 /* If it->face_id is -1, old_face below will be NULL, see
3812 the definition of FACE_FROM_ID. This will happen if this
3813 is the initial call that gets the face. */
3814 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3815
3816 /* If the value of face_id of the iterator is -1, we have to
3817 look in front of IT's position and see whether there is a
3818 face there that's different from new_face_id. */
3819 if (!old_face && IT_CHARPOS (*it) > BEG)
3820 {
3821 int prev_face_id = face_before_it_pos (it);
3822
3823 old_face = FACE_FROM_ID (it->f, prev_face_id);
3824 }
3825
3826 /* If the new face has a box, but the old face does not,
3827 this is the start of a run of characters with box face,
3828 i.e. this character has a shadow on the left side. */
3829 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3830 && (old_face == NULL || !old_face->box));
3831 it->face_box_p = new_face->box != FACE_NO_BOX;
3832 }
3833 }
3834 else
3835 {
3836 int base_face_id;
3837 ptrdiff_t bufpos;
3838 int i;
3839 Lisp_Object from_overlay
3840 = (it->current.overlay_string_index >= 0
3841 ? it->string_overlays[it->current.overlay_string_index
3842 % OVERLAY_STRING_CHUNK_SIZE]
3843 : Qnil);
3844
3845 /* See if we got to this string directly or indirectly from
3846 an overlay property. That includes the before-string or
3847 after-string of an overlay, strings in display properties
3848 provided by an overlay, their text properties, etc.
3849
3850 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3851 if (! NILP (from_overlay))
3852 for (i = it->sp - 1; i >= 0; i--)
3853 {
3854 if (it->stack[i].current.overlay_string_index >= 0)
3855 from_overlay
3856 = it->string_overlays[it->stack[i].current.overlay_string_index
3857 % OVERLAY_STRING_CHUNK_SIZE];
3858 else if (! NILP (it->stack[i].from_overlay))
3859 from_overlay = it->stack[i].from_overlay;
3860
3861 if (!NILP (from_overlay))
3862 break;
3863 }
3864
3865 if (! NILP (from_overlay))
3866 {
3867 bufpos = IT_CHARPOS (*it);
3868 /* For a string from an overlay, the base face depends
3869 only on text properties and ignores overlays. */
3870 base_face_id
3871 = face_for_overlay_string (it->w,
3872 IT_CHARPOS (*it),
3873 &next_stop,
3874 (IT_CHARPOS (*it)
3875 + TEXT_PROP_DISTANCE_LIMIT),
3876 false,
3877 from_overlay);
3878 }
3879 else
3880 {
3881 bufpos = 0;
3882
3883 /* For strings from a `display' property, use the face at
3884 IT's current buffer position as the base face to merge
3885 with, so that overlay strings appear in the same face as
3886 surrounding text, unless they specify their own faces.
3887 For strings from wrap-prefix and line-prefix properties,
3888 use the default face, possibly remapped via
3889 Vface_remapping_alist. */
3890 /* Note that the fact that we use the face at _buffer_
3891 position means that a 'display' property on an overlay
3892 string will not inherit the face of that overlay string,
3893 but will instead revert to the face of buffer text
3894 covered by the overlay. This is visible, e.g., when the
3895 overlay specifies a box face, but neither the buffer nor
3896 the display string do. This sounds like a design bug,
3897 but Emacs always did that since v21.1, so changing that
3898 might be a big deal. */
3899 base_face_id = it->string_from_prefix_prop_p
3900 ? (!NILP (Vface_remapping_alist)
3901 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3902 : DEFAULT_FACE_ID)
3903 : underlying_face_id (it);
3904 }
3905
3906 new_face_id = face_at_string_position (it->w,
3907 it->string,
3908 IT_STRING_CHARPOS (*it),
3909 bufpos,
3910 &next_stop,
3911 base_face_id, false);
3912
3913 /* Is this a start of a run of characters with box? Caveat:
3914 this can be called for a freshly allocated iterator; face_id
3915 is -1 is this case. We know that the new face will not
3916 change until the next check pos, i.e. if the new face has a
3917 box, all characters up to that position will have a
3918 box. But, as usual, we don't know whether that position
3919 is really the end. */
3920 if (new_face_id != it->face_id)
3921 {
3922 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3923 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3924
3925 /* If new face has a box but old face hasn't, this is the
3926 start of a run of characters with box, i.e. it has a
3927 shadow on the left side. */
3928 it->start_of_box_run_p
3929 = new_face->box && (old_face == NULL || !old_face->box);
3930 it->face_box_p = new_face->box != FACE_NO_BOX;
3931 }
3932 }
3933
3934 it->face_id = new_face_id;
3935 return HANDLED_NORMALLY;
3936 }
3937
3938
3939 /* Return the ID of the face ``underlying'' IT's current position,
3940 which is in a string. If the iterator is associated with a
3941 buffer, return the face at IT's current buffer position.
3942 Otherwise, use the iterator's base_face_id. */
3943
3944 static int
3945 underlying_face_id (struct it *it)
3946 {
3947 int face_id = it->base_face_id, i;
3948
3949 eassert (STRINGP (it->string));
3950
3951 for (i = it->sp - 1; i >= 0; --i)
3952 if (NILP (it->stack[i].string))
3953 face_id = it->stack[i].face_id;
3954
3955 return face_id;
3956 }
3957
3958
3959 /* Compute the face one character before or after the current position
3960 of IT, in the visual order. BEFORE_P means get the face
3961 in front (to the left in L2R paragraphs, to the right in R2L
3962 paragraphs) of IT's screen position. Value is the ID of the face. */
3963
3964 static int
3965 face_before_or_after_it_pos (struct it *it, bool before_p)
3966 {
3967 int face_id, limit;
3968 ptrdiff_t next_check_charpos;
3969 struct it it_copy;
3970 void *it_copy_data = NULL;
3971
3972 eassert (it->s == NULL);
3973
3974 if (STRINGP (it->string))
3975 {
3976 ptrdiff_t bufpos, charpos;
3977 int base_face_id;
3978
3979 /* No face change past the end of the string (for the case
3980 we are padding with spaces). No face change before the
3981 string start. */
3982 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3983 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3984 return it->face_id;
3985
3986 if (!it->bidi_p)
3987 {
3988 /* Set charpos to the position before or after IT's current
3989 position, in the logical order, which in the non-bidi
3990 case is the same as the visual order. */
3991 if (before_p)
3992 charpos = IT_STRING_CHARPOS (*it) - 1;
3993 else if (it->what == IT_COMPOSITION)
3994 /* For composition, we must check the character after the
3995 composition. */
3996 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3997 else
3998 charpos = IT_STRING_CHARPOS (*it) + 1;
3999 }
4000 else
4001 {
4002 if (before_p)
4003 {
4004 /* With bidi iteration, the character before the current
4005 in the visual order cannot be found by simple
4006 iteration, because "reverse" reordering is not
4007 supported. Instead, we need to use the move_it_*
4008 family of functions. */
4009 /* Ignore face changes before the first visible
4010 character on this display line. */
4011 if (it->current_x <= it->first_visible_x)
4012 return it->face_id;
4013 SAVE_IT (it_copy, *it, it_copy_data);
4014 /* Implementation note: Since move_it_in_display_line
4015 works in the iterator geometry, and thinks the first
4016 character is always the leftmost, even in R2L lines,
4017 we don't need to distinguish between the R2L and L2R
4018 cases here. */
4019 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4020 it_copy.current_x - 1, MOVE_TO_X);
4021 charpos = IT_STRING_CHARPOS (it_copy);
4022 RESTORE_IT (it, it, it_copy_data);
4023 }
4024 else
4025 {
4026 /* Set charpos to the string position of the character
4027 that comes after IT's current position in the visual
4028 order. */
4029 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4030
4031 it_copy = *it;
4032 while (n--)
4033 bidi_move_to_visually_next (&it_copy.bidi_it);
4034
4035 charpos = it_copy.bidi_it.charpos;
4036 }
4037 }
4038 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4039
4040 if (it->current.overlay_string_index >= 0)
4041 bufpos = IT_CHARPOS (*it);
4042 else
4043 bufpos = 0;
4044
4045 base_face_id = underlying_face_id (it);
4046
4047 /* Get the face for ASCII, or unibyte. */
4048 face_id = face_at_string_position (it->w,
4049 it->string,
4050 charpos,
4051 bufpos,
4052 &next_check_charpos,
4053 base_face_id, false);
4054
4055 /* Correct the face for charsets different from ASCII. Do it
4056 for the multibyte case only. The face returned above is
4057 suitable for unibyte text if IT->string is unibyte. */
4058 if (STRING_MULTIBYTE (it->string))
4059 {
4060 struct text_pos pos1 = string_pos (charpos, it->string);
4061 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4062 int c, len;
4063 struct face *face = FACE_FROM_ID (it->f, face_id);
4064
4065 c = string_char_and_length (p, &len);
4066 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4067 }
4068 }
4069 else
4070 {
4071 struct text_pos pos;
4072
4073 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4074 || (IT_CHARPOS (*it) <= BEGV && before_p))
4075 return it->face_id;
4076
4077 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4078 pos = it->current.pos;
4079
4080 if (!it->bidi_p)
4081 {
4082 if (before_p)
4083 DEC_TEXT_POS (pos, it->multibyte_p);
4084 else
4085 {
4086 if (it->what == IT_COMPOSITION)
4087 {
4088 /* For composition, we must check the position after
4089 the composition. */
4090 pos.charpos += it->cmp_it.nchars;
4091 pos.bytepos += it->len;
4092 }
4093 else
4094 INC_TEXT_POS (pos, it->multibyte_p);
4095 }
4096 }
4097 else
4098 {
4099 if (before_p)
4100 {
4101 /* With bidi iteration, the character before the current
4102 in the visual order cannot be found by simple
4103 iteration, because "reverse" reordering is not
4104 supported. Instead, we need to use the move_it_*
4105 family of functions. */
4106 /* Ignore face changes before the first visible
4107 character on this display line. */
4108 if (it->current_x <= it->first_visible_x)
4109 return it->face_id;
4110 SAVE_IT (it_copy, *it, it_copy_data);
4111 /* Implementation note: Since move_it_in_display_line
4112 works in the iterator geometry, and thinks the first
4113 character is always the leftmost, even in R2L lines,
4114 we don't need to distinguish between the R2L and L2R
4115 cases here. */
4116 move_it_in_display_line (&it_copy, ZV,
4117 it_copy.current_x - 1, MOVE_TO_X);
4118 pos = it_copy.current.pos;
4119 RESTORE_IT (it, it, it_copy_data);
4120 }
4121 else
4122 {
4123 /* Set charpos to the buffer position of the character
4124 that comes after IT's current position in the visual
4125 order. */
4126 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4127
4128 it_copy = *it;
4129 while (n--)
4130 bidi_move_to_visually_next (&it_copy.bidi_it);
4131
4132 SET_TEXT_POS (pos,
4133 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4134 }
4135 }
4136 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4137
4138 /* Determine face for CHARSET_ASCII, or unibyte. */
4139 face_id = face_at_buffer_position (it->w,
4140 CHARPOS (pos),
4141 &next_check_charpos,
4142 limit, false, -1);
4143
4144 /* Correct the face for charsets different from ASCII. Do it
4145 for the multibyte case only. The face returned above is
4146 suitable for unibyte text if current_buffer is unibyte. */
4147 if (it->multibyte_p)
4148 {
4149 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4150 struct face *face = FACE_FROM_ID (it->f, face_id);
4151 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4152 }
4153 }
4154
4155 return face_id;
4156 }
4157
4158
4159 \f
4160 /***********************************************************************
4161 Invisible text
4162 ***********************************************************************/
4163
4164 /* Set up iterator IT from invisible properties at its current
4165 position. Called from handle_stop. */
4166
4167 static enum prop_handled
4168 handle_invisible_prop (struct it *it)
4169 {
4170 enum prop_handled handled = HANDLED_NORMALLY;
4171 int invis;
4172 Lisp_Object prop;
4173
4174 if (STRINGP (it->string))
4175 {
4176 Lisp_Object end_charpos, limit, charpos;
4177
4178 /* Get the value of the invisible text property at the
4179 current position. Value will be nil if there is no such
4180 property. */
4181 charpos = make_number (IT_STRING_CHARPOS (*it));
4182 prop = Fget_text_property (charpos, Qinvisible, it->string);
4183 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4184
4185 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4186 {
4187 /* Record whether we have to display an ellipsis for the
4188 invisible text. */
4189 bool display_ellipsis_p = (invis == 2);
4190 ptrdiff_t len, endpos;
4191
4192 handled = HANDLED_RECOMPUTE_PROPS;
4193
4194 /* Get the position at which the next visible text can be
4195 found in IT->string, if any. */
4196 endpos = len = SCHARS (it->string);
4197 XSETINT (limit, len);
4198 do
4199 {
4200 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4201 it->string, limit);
4202 if (INTEGERP (end_charpos))
4203 {
4204 endpos = XFASTINT (end_charpos);
4205 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4206 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4207 if (invis == 2)
4208 display_ellipsis_p = true;
4209 }
4210 }
4211 while (invis != 0 && endpos < len);
4212
4213 if (display_ellipsis_p)
4214 it->ellipsis_p = true;
4215
4216 if (endpos < len)
4217 {
4218 /* Text at END_CHARPOS is visible. Move IT there. */
4219 struct text_pos old;
4220 ptrdiff_t oldpos;
4221
4222 old = it->current.string_pos;
4223 oldpos = CHARPOS (old);
4224 if (it->bidi_p)
4225 {
4226 if (it->bidi_it.first_elt
4227 && it->bidi_it.charpos < SCHARS (it->string))
4228 bidi_paragraph_init (it->paragraph_embedding,
4229 &it->bidi_it, true);
4230 /* Bidi-iterate out of the invisible text. */
4231 do
4232 {
4233 bidi_move_to_visually_next (&it->bidi_it);
4234 }
4235 while (oldpos <= it->bidi_it.charpos
4236 && it->bidi_it.charpos < endpos);
4237
4238 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4239 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4240 if (IT_CHARPOS (*it) >= endpos)
4241 it->prev_stop = endpos;
4242 }
4243 else
4244 {
4245 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4246 compute_string_pos (&it->current.string_pos, old, it->string);
4247 }
4248 }
4249 else
4250 {
4251 /* The rest of the string is invisible. If this is an
4252 overlay string, proceed with the next overlay string
4253 or whatever comes and return a character from there. */
4254 if (it->current.overlay_string_index >= 0
4255 && !display_ellipsis_p)
4256 {
4257 next_overlay_string (it);
4258 /* Don't check for overlay strings when we just
4259 finished processing them. */
4260 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4261 }
4262 else
4263 {
4264 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4265 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4266 }
4267 }
4268 }
4269 }
4270 else
4271 {
4272 ptrdiff_t newpos, next_stop, start_charpos, tem;
4273 Lisp_Object pos, overlay;
4274
4275 /* First of all, is there invisible text at this position? */
4276 tem = start_charpos = IT_CHARPOS (*it);
4277 pos = make_number (tem);
4278 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4279 &overlay);
4280 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4281
4282 /* If we are on invisible text, skip over it. */
4283 if (invis != 0 && start_charpos < it->end_charpos)
4284 {
4285 /* Record whether we have to display an ellipsis for the
4286 invisible text. */
4287 bool display_ellipsis_p = invis == 2;
4288
4289 handled = HANDLED_RECOMPUTE_PROPS;
4290
4291 /* Loop skipping over invisible text. The loop is left at
4292 ZV or with IT on the first char being visible again. */
4293 do
4294 {
4295 /* Try to skip some invisible text. Return value is the
4296 position reached which can be equal to where we start
4297 if there is nothing invisible there. This skips both
4298 over invisible text properties and overlays with
4299 invisible property. */
4300 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4301
4302 /* If we skipped nothing at all we weren't at invisible
4303 text in the first place. If everything to the end of
4304 the buffer was skipped, end the loop. */
4305 if (newpos == tem || newpos >= ZV)
4306 invis = 0;
4307 else
4308 {
4309 /* We skipped some characters but not necessarily
4310 all there are. Check if we ended up on visible
4311 text. Fget_char_property returns the property of
4312 the char before the given position, i.e. if we
4313 get invis = 0, this means that the char at
4314 newpos is visible. */
4315 pos = make_number (newpos);
4316 prop = Fget_char_property (pos, Qinvisible, it->window);
4317 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4318 }
4319
4320 /* If we ended up on invisible text, proceed to
4321 skip starting with next_stop. */
4322 if (invis != 0)
4323 tem = next_stop;
4324
4325 /* If there are adjacent invisible texts, don't lose the
4326 second one's ellipsis. */
4327 if (invis == 2)
4328 display_ellipsis_p = true;
4329 }
4330 while (invis != 0);
4331
4332 /* The position newpos is now either ZV or on visible text. */
4333 if (it->bidi_p)
4334 {
4335 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4336 bool on_newline
4337 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4338 bool after_newline
4339 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4340
4341 /* If the invisible text ends on a newline or on a
4342 character after a newline, we can avoid the costly,
4343 character by character, bidi iteration to NEWPOS, and
4344 instead simply reseat the iterator there. That's
4345 because all bidi reordering information is tossed at
4346 the newline. This is a big win for modes that hide
4347 complete lines, like Outline, Org, etc. */
4348 if (on_newline || after_newline)
4349 {
4350 struct text_pos tpos;
4351 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4352
4353 SET_TEXT_POS (tpos, newpos, bpos);
4354 reseat_1 (it, tpos, false);
4355 /* If we reseat on a newline/ZV, we need to prep the
4356 bidi iterator for advancing to the next character
4357 after the newline/EOB, keeping the current paragraph
4358 direction (so that PRODUCE_GLYPHS does TRT wrt
4359 prepending/appending glyphs to a glyph row). */
4360 if (on_newline)
4361 {
4362 it->bidi_it.first_elt = false;
4363 it->bidi_it.paragraph_dir = pdir;
4364 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4365 it->bidi_it.nchars = 1;
4366 it->bidi_it.ch_len = 1;
4367 }
4368 }
4369 else /* Must use the slow method. */
4370 {
4371 /* With bidi iteration, the region of invisible text
4372 could start and/or end in the middle of a
4373 non-base embedding level. Therefore, we need to
4374 skip invisible text using the bidi iterator,
4375 starting at IT's current position, until we find
4376 ourselves outside of the invisible text.
4377 Skipping invisible text _after_ bidi iteration
4378 avoids affecting the visual order of the
4379 displayed text when invisible properties are
4380 added or removed. */
4381 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4382 {
4383 /* If we were `reseat'ed to a new paragraph,
4384 determine the paragraph base direction. We
4385 need to do it now because
4386 next_element_from_buffer may not have a
4387 chance to do it, if we are going to skip any
4388 text at the beginning, which resets the
4389 FIRST_ELT flag. */
4390 bidi_paragraph_init (it->paragraph_embedding,
4391 &it->bidi_it, true);
4392 }
4393 do
4394 {
4395 bidi_move_to_visually_next (&it->bidi_it);
4396 }
4397 while (it->stop_charpos <= it->bidi_it.charpos
4398 && it->bidi_it.charpos < newpos);
4399 IT_CHARPOS (*it) = it->bidi_it.charpos;
4400 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4401 /* If we overstepped NEWPOS, record its position in
4402 the iterator, so that we skip invisible text if
4403 later the bidi iteration lands us in the
4404 invisible region again. */
4405 if (IT_CHARPOS (*it) >= newpos)
4406 it->prev_stop = newpos;
4407 }
4408 }
4409 else
4410 {
4411 IT_CHARPOS (*it) = newpos;
4412 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4413 }
4414
4415 /* If there are before-strings at the start of invisible
4416 text, and the text is invisible because of a text
4417 property, arrange to show before-strings because 20.x did
4418 it that way. (If the text is invisible because of an
4419 overlay property instead of a text property, this is
4420 already handled in the overlay code.) */
4421 if (NILP (overlay)
4422 && get_overlay_strings (it, it->stop_charpos))
4423 {
4424 handled = HANDLED_RECOMPUTE_PROPS;
4425 if (it->sp > 0)
4426 {
4427 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4428 /* The call to get_overlay_strings above recomputes
4429 it->stop_charpos, but it only considers changes
4430 in properties and overlays beyond iterator's
4431 current position. This causes us to miss changes
4432 that happen exactly where the invisible property
4433 ended. So we play it safe here and force the
4434 iterator to check for potential stop positions
4435 immediately after the invisible text. Note that
4436 if get_overlay_strings returns true, it
4437 normally also pushed the iterator stack, so we
4438 need to update the stop position in the slot
4439 below the current one. */
4440 it->stack[it->sp - 1].stop_charpos
4441 = CHARPOS (it->stack[it->sp - 1].current.pos);
4442 }
4443 }
4444 else if (display_ellipsis_p)
4445 {
4446 /* Make sure that the glyphs of the ellipsis will get
4447 correct `charpos' values. If we would not update
4448 it->position here, the glyphs would belong to the
4449 last visible character _before_ the invisible
4450 text, which confuses `set_cursor_from_row'.
4451
4452 We use the last invisible position instead of the
4453 first because this way the cursor is always drawn on
4454 the first "." of the ellipsis, whenever PT is inside
4455 the invisible text. Otherwise the cursor would be
4456 placed _after_ the ellipsis when the point is after the
4457 first invisible character. */
4458 if (!STRINGP (it->object))
4459 {
4460 it->position.charpos = newpos - 1;
4461 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4462 }
4463 it->ellipsis_p = true;
4464 /* Let the ellipsis display before
4465 considering any properties of the following char.
4466 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4467 handled = HANDLED_RETURN;
4468 }
4469 }
4470 }
4471
4472 return handled;
4473 }
4474
4475
4476 /* Make iterator IT return `...' next.
4477 Replaces LEN characters from buffer. */
4478
4479 static void
4480 setup_for_ellipsis (struct it *it, int len)
4481 {
4482 /* Use the display table definition for `...'. Invalid glyphs
4483 will be handled by the method returning elements from dpvec. */
4484 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4485 {
4486 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4487 it->dpvec = v->contents;
4488 it->dpend = v->contents + v->header.size;
4489 }
4490 else
4491 {
4492 /* Default `...'. */
4493 it->dpvec = default_invis_vector;
4494 it->dpend = default_invis_vector + 3;
4495 }
4496
4497 it->dpvec_char_len = len;
4498 it->current.dpvec_index = 0;
4499 it->dpvec_face_id = -1;
4500
4501 /* Remember the current face id in case glyphs specify faces.
4502 IT's face is restored in set_iterator_to_next.
4503 saved_face_id was set to preceding char's face in handle_stop. */
4504 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4505 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4506
4507 it->method = GET_FROM_DISPLAY_VECTOR;
4508 it->ellipsis_p = true;
4509 }
4510
4511
4512 \f
4513 /***********************************************************************
4514 'display' property
4515 ***********************************************************************/
4516
4517 /* Set up iterator IT from `display' property at its current position.
4518 Called from handle_stop.
4519 We return HANDLED_RETURN if some part of the display property
4520 overrides the display of the buffer text itself.
4521 Otherwise we return HANDLED_NORMALLY. */
4522
4523 static enum prop_handled
4524 handle_display_prop (struct it *it)
4525 {
4526 Lisp_Object propval, object, overlay;
4527 struct text_pos *position;
4528 ptrdiff_t bufpos;
4529 /* Nonzero if some property replaces the display of the text itself. */
4530 int display_replaced = 0;
4531
4532 if (STRINGP (it->string))
4533 {
4534 object = it->string;
4535 position = &it->current.string_pos;
4536 bufpos = CHARPOS (it->current.pos);
4537 }
4538 else
4539 {
4540 XSETWINDOW (object, it->w);
4541 position = &it->current.pos;
4542 bufpos = CHARPOS (*position);
4543 }
4544
4545 /* Reset those iterator values set from display property values. */
4546 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4547 it->space_width = Qnil;
4548 it->font_height = Qnil;
4549 it->voffset = 0;
4550
4551 /* We don't support recursive `display' properties, i.e. string
4552 values that have a string `display' property, that have a string
4553 `display' property etc. */
4554 if (!it->string_from_display_prop_p)
4555 it->area = TEXT_AREA;
4556
4557 propval = get_char_property_and_overlay (make_number (position->charpos),
4558 Qdisplay, object, &overlay);
4559 if (NILP (propval))
4560 return HANDLED_NORMALLY;
4561 /* Now OVERLAY is the overlay that gave us this property, or nil
4562 if it was a text property. */
4563
4564 if (!STRINGP (it->string))
4565 object = it->w->contents;
4566
4567 display_replaced = handle_display_spec (it, propval, object, overlay,
4568 position, bufpos,
4569 FRAME_WINDOW_P (it->f));
4570 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4571 }
4572
4573 /* Subroutine of handle_display_prop. Returns non-zero if the display
4574 specification in SPEC is a replacing specification, i.e. it would
4575 replace the text covered by `display' property with something else,
4576 such as an image or a display string. If SPEC includes any kind or
4577 `(space ...) specification, the value is 2; this is used by
4578 compute_display_string_pos, which see.
4579
4580 See handle_single_display_spec for documentation of arguments.
4581 FRAME_WINDOW_P is true if the window being redisplayed is on a
4582 GUI frame; this argument is used only if IT is NULL, see below.
4583
4584 IT can be NULL, if this is called by the bidi reordering code
4585 through compute_display_string_pos, which see. In that case, this
4586 function only examines SPEC, but does not otherwise "handle" it, in
4587 the sense that it doesn't set up members of IT from the display
4588 spec. */
4589 static int
4590 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4591 Lisp_Object overlay, struct text_pos *position,
4592 ptrdiff_t bufpos, bool frame_window_p)
4593 {
4594 int replacing = 0;
4595
4596 if (CONSP (spec)
4597 /* Simple specifications. */
4598 && !EQ (XCAR (spec), Qimage)
4599 && !EQ (XCAR (spec), Qspace)
4600 && !EQ (XCAR (spec), Qwhen)
4601 && !EQ (XCAR (spec), Qslice)
4602 && !EQ (XCAR (spec), Qspace_width)
4603 && !EQ (XCAR (spec), Qheight)
4604 && !EQ (XCAR (spec), Qraise)
4605 /* Marginal area specifications. */
4606 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4607 && !EQ (XCAR (spec), Qleft_fringe)
4608 && !EQ (XCAR (spec), Qright_fringe)
4609 && !NILP (XCAR (spec)))
4610 {
4611 for (; CONSP (spec); spec = XCDR (spec))
4612 {
4613 int rv = handle_single_display_spec (it, XCAR (spec), object,
4614 overlay, position, bufpos,
4615 replacing, frame_window_p);
4616 if (rv != 0)
4617 {
4618 replacing = rv;
4619 /* If some text in a string is replaced, `position' no
4620 longer points to the position of `object'. */
4621 if (!it || STRINGP (object))
4622 break;
4623 }
4624 }
4625 }
4626 else if (VECTORP (spec))
4627 {
4628 ptrdiff_t i;
4629 for (i = 0; i < ASIZE (spec); ++i)
4630 {
4631 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4632 overlay, position, bufpos,
4633 replacing, frame_window_p);
4634 if (rv != 0)
4635 {
4636 replacing = rv;
4637 /* If some text in a string is replaced, `position' no
4638 longer points to the position of `object'. */
4639 if (!it || STRINGP (object))
4640 break;
4641 }
4642 }
4643 }
4644 else
4645 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4646 bufpos, 0, frame_window_p);
4647 return replacing;
4648 }
4649
4650 /* Value is the position of the end of the `display' property starting
4651 at START_POS in OBJECT. */
4652
4653 static struct text_pos
4654 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4655 {
4656 Lisp_Object end;
4657 struct text_pos end_pos;
4658
4659 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4660 Qdisplay, object, Qnil);
4661 CHARPOS (end_pos) = XFASTINT (end);
4662 if (STRINGP (object))
4663 compute_string_pos (&end_pos, start_pos, it->string);
4664 else
4665 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4666
4667 return end_pos;
4668 }
4669
4670
4671 /* Set up IT from a single `display' property specification SPEC. OBJECT
4672 is the object in which the `display' property was found. *POSITION
4673 is the position in OBJECT at which the `display' property was found.
4674 BUFPOS is the buffer position of OBJECT (different from POSITION if
4675 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4676 previously saw a display specification which already replaced text
4677 display with something else, for example an image; we ignore such
4678 properties after the first one has been processed.
4679
4680 OVERLAY is the overlay this `display' property came from,
4681 or nil if it was a text property.
4682
4683 If SPEC is a `space' or `image' specification, and in some other
4684 cases too, set *POSITION to the position where the `display'
4685 property ends.
4686
4687 If IT is NULL, only examine the property specification in SPEC, but
4688 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4689 is intended to be displayed in a window on a GUI frame.
4690
4691 Value is non-zero if something was found which replaces the display
4692 of buffer or string text. */
4693
4694 static int
4695 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4696 Lisp_Object overlay, struct text_pos *position,
4697 ptrdiff_t bufpos, int display_replaced,
4698 bool frame_window_p)
4699 {
4700 Lisp_Object form;
4701 Lisp_Object location, value;
4702 struct text_pos start_pos = *position;
4703
4704 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4705 If the result is non-nil, use VALUE instead of SPEC. */
4706 form = Qt;
4707 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4708 {
4709 spec = XCDR (spec);
4710 if (!CONSP (spec))
4711 return 0;
4712 form = XCAR (spec);
4713 spec = XCDR (spec);
4714 }
4715
4716 if (!NILP (form) && !EQ (form, Qt))
4717 {
4718 ptrdiff_t count = SPECPDL_INDEX ();
4719 struct gcpro gcpro1;
4720
4721 /* Bind `object' to the object having the `display' property, a
4722 buffer or string. Bind `position' to the position in the
4723 object where the property was found, and `buffer-position'
4724 to the current position in the buffer. */
4725
4726 if (NILP (object))
4727 XSETBUFFER (object, current_buffer);
4728 specbind (Qobject, object);
4729 specbind (Qposition, make_number (CHARPOS (*position)));
4730 specbind (Qbuffer_position, make_number (bufpos));
4731 GCPRO1 (form);
4732 form = safe_eval (form);
4733 UNGCPRO;
4734 unbind_to (count, Qnil);
4735 }
4736
4737 if (NILP (form))
4738 return 0;
4739
4740 /* Handle `(height HEIGHT)' specifications. */
4741 if (CONSP (spec)
4742 && EQ (XCAR (spec), Qheight)
4743 && CONSP (XCDR (spec)))
4744 {
4745 if (it)
4746 {
4747 if (!FRAME_WINDOW_P (it->f))
4748 return 0;
4749
4750 it->font_height = XCAR (XCDR (spec));
4751 if (!NILP (it->font_height))
4752 {
4753 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4754 int new_height = -1;
4755
4756 if (CONSP (it->font_height)
4757 && (EQ (XCAR (it->font_height), Qplus)
4758 || EQ (XCAR (it->font_height), Qminus))
4759 && CONSP (XCDR (it->font_height))
4760 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4761 {
4762 /* `(+ N)' or `(- N)' where N is an integer. */
4763 int steps = XINT (XCAR (XCDR (it->font_height)));
4764 if (EQ (XCAR (it->font_height), Qplus))
4765 steps = - steps;
4766 it->face_id = smaller_face (it->f, it->face_id, steps);
4767 }
4768 else if (FUNCTIONP (it->font_height))
4769 {
4770 /* Call function with current height as argument.
4771 Value is the new height. */
4772 Lisp_Object height;
4773 height = safe_call1 (it->font_height,
4774 face->lface[LFACE_HEIGHT_INDEX]);
4775 if (NUMBERP (height))
4776 new_height = XFLOATINT (height);
4777 }
4778 else if (NUMBERP (it->font_height))
4779 {
4780 /* Value is a multiple of the canonical char height. */
4781 struct face *f;
4782
4783 f = FACE_FROM_ID (it->f,
4784 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4785 new_height = (XFLOATINT (it->font_height)
4786 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4787 }
4788 else
4789 {
4790 /* Evaluate IT->font_height with `height' bound to the
4791 current specified height to get the new height. */
4792 ptrdiff_t count = SPECPDL_INDEX ();
4793
4794 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4795 value = safe_eval (it->font_height);
4796 unbind_to (count, Qnil);
4797
4798 if (NUMBERP (value))
4799 new_height = XFLOATINT (value);
4800 }
4801
4802 if (new_height > 0)
4803 it->face_id = face_with_height (it->f, it->face_id, new_height);
4804 }
4805 }
4806
4807 return 0;
4808 }
4809
4810 /* Handle `(space-width WIDTH)'. */
4811 if (CONSP (spec)
4812 && EQ (XCAR (spec), Qspace_width)
4813 && CONSP (XCDR (spec)))
4814 {
4815 if (it)
4816 {
4817 if (!FRAME_WINDOW_P (it->f))
4818 return 0;
4819
4820 value = XCAR (XCDR (spec));
4821 if (NUMBERP (value) && XFLOATINT (value) > 0)
4822 it->space_width = value;
4823 }
4824
4825 return 0;
4826 }
4827
4828 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4829 if (CONSP (spec)
4830 && EQ (XCAR (spec), Qslice))
4831 {
4832 Lisp_Object tem;
4833
4834 if (it)
4835 {
4836 if (!FRAME_WINDOW_P (it->f))
4837 return 0;
4838
4839 if (tem = XCDR (spec), CONSP (tem))
4840 {
4841 it->slice.x = XCAR (tem);
4842 if (tem = XCDR (tem), CONSP (tem))
4843 {
4844 it->slice.y = XCAR (tem);
4845 if (tem = XCDR (tem), CONSP (tem))
4846 {
4847 it->slice.width = XCAR (tem);
4848 if (tem = XCDR (tem), CONSP (tem))
4849 it->slice.height = XCAR (tem);
4850 }
4851 }
4852 }
4853 }
4854
4855 return 0;
4856 }
4857
4858 /* Handle `(raise FACTOR)'. */
4859 if (CONSP (spec)
4860 && EQ (XCAR (spec), Qraise)
4861 && CONSP (XCDR (spec)))
4862 {
4863 if (it)
4864 {
4865 if (!FRAME_WINDOW_P (it->f))
4866 return 0;
4867
4868 #ifdef HAVE_WINDOW_SYSTEM
4869 value = XCAR (XCDR (spec));
4870 if (NUMBERP (value))
4871 {
4872 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4873 it->voffset = - (XFLOATINT (value)
4874 * (FONT_HEIGHT (face->font)));
4875 }
4876 #endif /* HAVE_WINDOW_SYSTEM */
4877 }
4878
4879 return 0;
4880 }
4881
4882 /* Don't handle the other kinds of display specifications
4883 inside a string that we got from a `display' property. */
4884 if (it && it->string_from_display_prop_p)
4885 return 0;
4886
4887 /* Characters having this form of property are not displayed, so
4888 we have to find the end of the property. */
4889 if (it)
4890 {
4891 start_pos = *position;
4892 *position = display_prop_end (it, object, start_pos);
4893 }
4894 value = Qnil;
4895
4896 /* Stop the scan at that end position--we assume that all
4897 text properties change there. */
4898 if (it)
4899 it->stop_charpos = position->charpos;
4900
4901 /* Handle `(left-fringe BITMAP [FACE])'
4902 and `(right-fringe BITMAP [FACE])'. */
4903 if (CONSP (spec)
4904 && (EQ (XCAR (spec), Qleft_fringe)
4905 || EQ (XCAR (spec), Qright_fringe))
4906 && CONSP (XCDR (spec)))
4907 {
4908 int fringe_bitmap;
4909
4910 if (it)
4911 {
4912 if (!FRAME_WINDOW_P (it->f))
4913 /* If we return here, POSITION has been advanced
4914 across the text with this property. */
4915 {
4916 /* Synchronize the bidi iterator with POSITION. This is
4917 needed because we are not going to push the iterator
4918 on behalf of this display property, so there will be
4919 no pop_it call to do this synchronization for us. */
4920 if (it->bidi_p)
4921 {
4922 it->position = *position;
4923 iterate_out_of_display_property (it);
4924 *position = it->position;
4925 }
4926 return 1;
4927 }
4928 }
4929 else if (!frame_window_p)
4930 return 1;
4931
4932 #ifdef HAVE_WINDOW_SYSTEM
4933 value = XCAR (XCDR (spec));
4934 if (!SYMBOLP (value)
4935 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4936 /* If we return here, POSITION has been advanced
4937 across the text with this property. */
4938 {
4939 if (it && it->bidi_p)
4940 {
4941 it->position = *position;
4942 iterate_out_of_display_property (it);
4943 *position = it->position;
4944 }
4945 return 1;
4946 }
4947
4948 if (it)
4949 {
4950 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4951
4952 if (CONSP (XCDR (XCDR (spec))))
4953 {
4954 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4955 int face_id2 = lookup_derived_face (it->f, face_name,
4956 FRINGE_FACE_ID, false);
4957 if (face_id2 >= 0)
4958 face_id = face_id2;
4959 }
4960
4961 /* Save current settings of IT so that we can restore them
4962 when we are finished with the glyph property value. */
4963 push_it (it, position);
4964
4965 it->area = TEXT_AREA;
4966 it->what = IT_IMAGE;
4967 it->image_id = -1; /* no image */
4968 it->position = start_pos;
4969 it->object = NILP (object) ? it->w->contents : object;
4970 it->method = GET_FROM_IMAGE;
4971 it->from_overlay = Qnil;
4972 it->face_id = face_id;
4973 it->from_disp_prop_p = true;
4974
4975 /* Say that we haven't consumed the characters with
4976 `display' property yet. The call to pop_it in
4977 set_iterator_to_next will clean this up. */
4978 *position = start_pos;
4979
4980 if (EQ (XCAR (spec), Qleft_fringe))
4981 {
4982 it->left_user_fringe_bitmap = fringe_bitmap;
4983 it->left_user_fringe_face_id = face_id;
4984 }
4985 else
4986 {
4987 it->right_user_fringe_bitmap = fringe_bitmap;
4988 it->right_user_fringe_face_id = face_id;
4989 }
4990 }
4991 #endif /* HAVE_WINDOW_SYSTEM */
4992 return 1;
4993 }
4994
4995 /* Prepare to handle `((margin left-margin) ...)',
4996 `((margin right-margin) ...)' and `((margin nil) ...)'
4997 prefixes for display specifications. */
4998 location = Qunbound;
4999 if (CONSP (spec) && CONSP (XCAR (spec)))
5000 {
5001 Lisp_Object tem;
5002
5003 value = XCDR (spec);
5004 if (CONSP (value))
5005 value = XCAR (value);
5006
5007 tem = XCAR (spec);
5008 if (EQ (XCAR (tem), Qmargin)
5009 && (tem = XCDR (tem),
5010 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5011 (NILP (tem)
5012 || EQ (tem, Qleft_margin)
5013 || EQ (tem, Qright_margin))))
5014 location = tem;
5015 }
5016
5017 if (EQ (location, Qunbound))
5018 {
5019 location = Qnil;
5020 value = spec;
5021 }
5022
5023 /* After this point, VALUE is the property after any
5024 margin prefix has been stripped. It must be a string,
5025 an image specification, or `(space ...)'.
5026
5027 LOCATION specifies where to display: `left-margin',
5028 `right-margin' or nil. */
5029
5030 bool valid_p = (STRINGP (value)
5031 #ifdef HAVE_WINDOW_SYSTEM
5032 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5033 && valid_image_p (value))
5034 #endif /* not HAVE_WINDOW_SYSTEM */
5035 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5036
5037 if (valid_p && display_replaced == 0)
5038 {
5039 int retval = 1;
5040
5041 if (!it)
5042 {
5043 /* Callers need to know whether the display spec is any kind
5044 of `(space ...)' spec that is about to affect text-area
5045 display. */
5046 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5047 retval = 2;
5048 return retval;
5049 }
5050
5051 /* Save current settings of IT so that we can restore them
5052 when we are finished with the glyph property value. */
5053 push_it (it, position);
5054 it->from_overlay = overlay;
5055 it->from_disp_prop_p = true;
5056
5057 if (NILP (location))
5058 it->area = TEXT_AREA;
5059 else if (EQ (location, Qleft_margin))
5060 it->area = LEFT_MARGIN_AREA;
5061 else
5062 it->area = RIGHT_MARGIN_AREA;
5063
5064 if (STRINGP (value))
5065 {
5066 it->string = value;
5067 it->multibyte_p = STRING_MULTIBYTE (it->string);
5068 it->current.overlay_string_index = -1;
5069 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5070 it->end_charpos = it->string_nchars = SCHARS (it->string);
5071 it->method = GET_FROM_STRING;
5072 it->stop_charpos = 0;
5073 it->prev_stop = 0;
5074 it->base_level_stop = 0;
5075 it->string_from_display_prop_p = true;
5076 /* Say that we haven't consumed the characters with
5077 `display' property yet. The call to pop_it in
5078 set_iterator_to_next will clean this up. */
5079 if (BUFFERP (object))
5080 *position = start_pos;
5081
5082 /* Force paragraph direction to be that of the parent
5083 object. If the parent object's paragraph direction is
5084 not yet determined, default to L2R. */
5085 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5086 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5087 else
5088 it->paragraph_embedding = L2R;
5089
5090 /* Set up the bidi iterator for this display string. */
5091 if (it->bidi_p)
5092 {
5093 it->bidi_it.string.lstring = it->string;
5094 it->bidi_it.string.s = NULL;
5095 it->bidi_it.string.schars = it->end_charpos;
5096 it->bidi_it.string.bufpos = bufpos;
5097 it->bidi_it.string.from_disp_str = true;
5098 it->bidi_it.string.unibyte = !it->multibyte_p;
5099 it->bidi_it.w = it->w;
5100 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5101 }
5102 }
5103 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5104 {
5105 it->method = GET_FROM_STRETCH;
5106 it->object = value;
5107 *position = it->position = start_pos;
5108 retval = 1 + (it->area == TEXT_AREA);
5109 }
5110 #ifdef HAVE_WINDOW_SYSTEM
5111 else
5112 {
5113 it->what = IT_IMAGE;
5114 it->image_id = lookup_image (it->f, value);
5115 it->position = start_pos;
5116 it->object = NILP (object) ? it->w->contents : object;
5117 it->method = GET_FROM_IMAGE;
5118
5119 /* Say that we haven't consumed the characters with
5120 `display' property yet. The call to pop_it in
5121 set_iterator_to_next will clean this up. */
5122 *position = start_pos;
5123 }
5124 #endif /* HAVE_WINDOW_SYSTEM */
5125
5126 return retval;
5127 }
5128
5129 /* Invalid property or property not supported. Restore
5130 POSITION to what it was before. */
5131 *position = start_pos;
5132 return 0;
5133 }
5134
5135 /* Check if PROP is a display property value whose text should be
5136 treated as intangible. OVERLAY is the overlay from which PROP
5137 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5138 specify the buffer position covered by PROP. */
5139
5140 bool
5141 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5142 ptrdiff_t charpos, ptrdiff_t bytepos)
5143 {
5144 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5145 struct text_pos position;
5146
5147 SET_TEXT_POS (position, charpos, bytepos);
5148 return (handle_display_spec (NULL, prop, Qnil, overlay,
5149 &position, charpos, frame_window_p)
5150 != 0);
5151 }
5152
5153
5154 /* Return true if PROP is a display sub-property value containing STRING.
5155
5156 Implementation note: this and the following function are really
5157 special cases of handle_display_spec and
5158 handle_single_display_spec, and should ideally use the same code.
5159 Until they do, these two pairs must be consistent and must be
5160 modified in sync. */
5161
5162 static bool
5163 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5164 {
5165 if (EQ (string, prop))
5166 return true;
5167
5168 /* Skip over `when FORM'. */
5169 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5170 {
5171 prop = XCDR (prop);
5172 if (!CONSP (prop))
5173 return false;
5174 /* Actually, the condition following `when' should be eval'ed,
5175 like handle_single_display_spec does, and we should return
5176 false if it evaluates to nil. However, this function is
5177 called only when the buffer was already displayed and some
5178 glyph in the glyph matrix was found to come from a display
5179 string. Therefore, the condition was already evaluated, and
5180 the result was non-nil, otherwise the display string wouldn't
5181 have been displayed and we would have never been called for
5182 this property. Thus, we can skip the evaluation and assume
5183 its result is non-nil. */
5184 prop = XCDR (prop);
5185 }
5186
5187 if (CONSP (prop))
5188 /* Skip over `margin LOCATION'. */
5189 if (EQ (XCAR (prop), Qmargin))
5190 {
5191 prop = XCDR (prop);
5192 if (!CONSP (prop))
5193 return false;
5194
5195 prop = XCDR (prop);
5196 if (!CONSP (prop))
5197 return false;
5198 }
5199
5200 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5201 }
5202
5203
5204 /* Return true if STRING appears in the `display' property PROP. */
5205
5206 static bool
5207 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5208 {
5209 if (CONSP (prop)
5210 && !EQ (XCAR (prop), Qwhen)
5211 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5212 {
5213 /* A list of sub-properties. */
5214 while (CONSP (prop))
5215 {
5216 if (single_display_spec_string_p (XCAR (prop), string))
5217 return true;
5218 prop = XCDR (prop);
5219 }
5220 }
5221 else if (VECTORP (prop))
5222 {
5223 /* A vector of sub-properties. */
5224 ptrdiff_t i;
5225 for (i = 0; i < ASIZE (prop); ++i)
5226 if (single_display_spec_string_p (AREF (prop, i), string))
5227 return true;
5228 }
5229 else
5230 return single_display_spec_string_p (prop, string);
5231
5232 return false;
5233 }
5234
5235 /* Look for STRING in overlays and text properties in the current
5236 buffer, between character positions FROM and TO (excluding TO).
5237 BACK_P means look back (in this case, TO is supposed to be
5238 less than FROM).
5239 Value is the first character position where STRING was found, or
5240 zero if it wasn't found before hitting TO.
5241
5242 This function may only use code that doesn't eval because it is
5243 called asynchronously from note_mouse_highlight. */
5244
5245 static ptrdiff_t
5246 string_buffer_position_lim (Lisp_Object string,
5247 ptrdiff_t from, ptrdiff_t to, bool back_p)
5248 {
5249 Lisp_Object limit, prop, pos;
5250 bool found = false;
5251
5252 pos = make_number (max (from, BEGV));
5253
5254 if (!back_p) /* looking forward */
5255 {
5256 limit = make_number (min (to, ZV));
5257 while (!found && !EQ (pos, limit))
5258 {
5259 prop = Fget_char_property (pos, Qdisplay, Qnil);
5260 if (!NILP (prop) && display_prop_string_p (prop, string))
5261 found = true;
5262 else
5263 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5264 limit);
5265 }
5266 }
5267 else /* looking back */
5268 {
5269 limit = make_number (max (to, BEGV));
5270 while (!found && !EQ (pos, limit))
5271 {
5272 prop = Fget_char_property (pos, Qdisplay, Qnil);
5273 if (!NILP (prop) && display_prop_string_p (prop, string))
5274 found = true;
5275 else
5276 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5277 limit);
5278 }
5279 }
5280
5281 return found ? XINT (pos) : 0;
5282 }
5283
5284 /* Determine which buffer position in current buffer STRING comes from.
5285 AROUND_CHARPOS is an approximate position where it could come from.
5286 Value is the buffer position or 0 if it couldn't be determined.
5287
5288 This function is necessary because we don't record buffer positions
5289 in glyphs generated from strings (to keep struct glyph small).
5290 This function may only use code that doesn't eval because it is
5291 called asynchronously from note_mouse_highlight. */
5292
5293 static ptrdiff_t
5294 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5295 {
5296 const int MAX_DISTANCE = 1000;
5297 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5298 around_charpos + MAX_DISTANCE,
5299 false);
5300
5301 if (!found)
5302 found = string_buffer_position_lim (string, around_charpos,
5303 around_charpos - MAX_DISTANCE, true);
5304 return found;
5305 }
5306
5307
5308 \f
5309 /***********************************************************************
5310 `composition' property
5311 ***********************************************************************/
5312
5313 /* Set up iterator IT from `composition' property at its current
5314 position. Called from handle_stop. */
5315
5316 static enum prop_handled
5317 handle_composition_prop (struct it *it)
5318 {
5319 Lisp_Object prop, string;
5320 ptrdiff_t pos, pos_byte, start, end;
5321
5322 if (STRINGP (it->string))
5323 {
5324 unsigned char *s;
5325
5326 pos = IT_STRING_CHARPOS (*it);
5327 pos_byte = IT_STRING_BYTEPOS (*it);
5328 string = it->string;
5329 s = SDATA (string) + pos_byte;
5330 it->c = STRING_CHAR (s);
5331 }
5332 else
5333 {
5334 pos = IT_CHARPOS (*it);
5335 pos_byte = IT_BYTEPOS (*it);
5336 string = Qnil;
5337 it->c = FETCH_CHAR (pos_byte);
5338 }
5339
5340 /* If there's a valid composition and point is not inside of the
5341 composition (in the case that the composition is from the current
5342 buffer), draw a glyph composed from the composition components. */
5343 if (find_composition (pos, -1, &start, &end, &prop, string)
5344 && composition_valid_p (start, end, prop)
5345 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5346 {
5347 if (start < pos)
5348 /* As we can't handle this situation (perhaps font-lock added
5349 a new composition), we just return here hoping that next
5350 redisplay will detect this composition much earlier. */
5351 return HANDLED_NORMALLY;
5352 if (start != pos)
5353 {
5354 if (STRINGP (it->string))
5355 pos_byte = string_char_to_byte (it->string, start);
5356 else
5357 pos_byte = CHAR_TO_BYTE (start);
5358 }
5359 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5360 prop, string);
5361
5362 if (it->cmp_it.id >= 0)
5363 {
5364 it->cmp_it.ch = -1;
5365 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5366 it->cmp_it.nglyphs = -1;
5367 }
5368 }
5369
5370 return HANDLED_NORMALLY;
5371 }
5372
5373
5374 \f
5375 /***********************************************************************
5376 Overlay strings
5377 ***********************************************************************/
5378
5379 /* The following structure is used to record overlay strings for
5380 later sorting in load_overlay_strings. */
5381
5382 struct overlay_entry
5383 {
5384 Lisp_Object overlay;
5385 Lisp_Object string;
5386 EMACS_INT priority;
5387 bool after_string_p;
5388 };
5389
5390
5391 /* Set up iterator IT from overlay strings at its current position.
5392 Called from handle_stop. */
5393
5394 static enum prop_handled
5395 handle_overlay_change (struct it *it)
5396 {
5397 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5398 return HANDLED_RECOMPUTE_PROPS;
5399 else
5400 return HANDLED_NORMALLY;
5401 }
5402
5403
5404 /* Set up the next overlay string for delivery by IT, if there is an
5405 overlay string to deliver. Called by set_iterator_to_next when the
5406 end of the current overlay string is reached. If there are more
5407 overlay strings to display, IT->string and
5408 IT->current.overlay_string_index are set appropriately here.
5409 Otherwise IT->string is set to nil. */
5410
5411 static void
5412 next_overlay_string (struct it *it)
5413 {
5414 ++it->current.overlay_string_index;
5415 if (it->current.overlay_string_index == it->n_overlay_strings)
5416 {
5417 /* No more overlay strings. Restore IT's settings to what
5418 they were before overlay strings were processed, and
5419 continue to deliver from current_buffer. */
5420
5421 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5422 pop_it (it);
5423 eassert (it->sp > 0
5424 || (NILP (it->string)
5425 && it->method == GET_FROM_BUFFER
5426 && it->stop_charpos >= BEGV
5427 && it->stop_charpos <= it->end_charpos));
5428 it->current.overlay_string_index = -1;
5429 it->n_overlay_strings = 0;
5430 it->overlay_strings_charpos = -1;
5431 /* If there's an empty display string on the stack, pop the
5432 stack, to resync the bidi iterator with IT's position. Such
5433 empty strings are pushed onto the stack in
5434 get_overlay_strings_1. */
5435 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5436 pop_it (it);
5437
5438 /* Since we've exhausted overlay strings at this buffer
5439 position, set the flag to ignore overlays until we move to
5440 another position. The flag is reset in
5441 next_element_from_buffer. */
5442 it->ignore_overlay_strings_at_pos_p = true;
5443
5444 /* If we're at the end of the buffer, record that we have
5445 processed the overlay strings there already, so that
5446 next_element_from_buffer doesn't try it again. */
5447 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5448 it->overlay_strings_at_end_processed_p = true;
5449 }
5450 else
5451 {
5452 /* There are more overlay strings to process. If
5453 IT->current.overlay_string_index has advanced to a position
5454 where we must load IT->overlay_strings with more strings, do
5455 it. We must load at the IT->overlay_strings_charpos where
5456 IT->n_overlay_strings was originally computed; when invisible
5457 text is present, this might not be IT_CHARPOS (Bug#7016). */
5458 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5459
5460 if (it->current.overlay_string_index && i == 0)
5461 load_overlay_strings (it, it->overlay_strings_charpos);
5462
5463 /* Initialize IT to deliver display elements from the overlay
5464 string. */
5465 it->string = it->overlay_strings[i];
5466 it->multibyte_p = STRING_MULTIBYTE (it->string);
5467 SET_TEXT_POS (it->current.string_pos, 0, 0);
5468 it->method = GET_FROM_STRING;
5469 it->stop_charpos = 0;
5470 it->end_charpos = SCHARS (it->string);
5471 if (it->cmp_it.stop_pos >= 0)
5472 it->cmp_it.stop_pos = 0;
5473 it->prev_stop = 0;
5474 it->base_level_stop = 0;
5475
5476 /* Set up the bidi iterator for this overlay string. */
5477 if (it->bidi_p)
5478 {
5479 it->bidi_it.string.lstring = it->string;
5480 it->bidi_it.string.s = NULL;
5481 it->bidi_it.string.schars = SCHARS (it->string);
5482 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5483 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5484 it->bidi_it.string.unibyte = !it->multibyte_p;
5485 it->bidi_it.w = it->w;
5486 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5487 }
5488 }
5489
5490 CHECK_IT (it);
5491 }
5492
5493
5494 /* Compare two overlay_entry structures E1 and E2. Used as a
5495 comparison function for qsort in load_overlay_strings. Overlay
5496 strings for the same position are sorted so that
5497
5498 1. All after-strings come in front of before-strings, except
5499 when they come from the same overlay.
5500
5501 2. Within after-strings, strings are sorted so that overlay strings
5502 from overlays with higher priorities come first.
5503
5504 2. Within before-strings, strings are sorted so that overlay
5505 strings from overlays with higher priorities come last.
5506
5507 Value is analogous to strcmp. */
5508
5509
5510 static int
5511 compare_overlay_entries (const void *e1, const void *e2)
5512 {
5513 struct overlay_entry const *entry1 = e1;
5514 struct overlay_entry const *entry2 = e2;
5515 int result;
5516
5517 if (entry1->after_string_p != entry2->after_string_p)
5518 {
5519 /* Let after-strings appear in front of before-strings if
5520 they come from different overlays. */
5521 if (EQ (entry1->overlay, entry2->overlay))
5522 result = entry1->after_string_p ? 1 : -1;
5523 else
5524 result = entry1->after_string_p ? -1 : 1;
5525 }
5526 else if (entry1->priority != entry2->priority)
5527 {
5528 if (entry1->after_string_p)
5529 /* After-strings sorted in order of decreasing priority. */
5530 result = entry2->priority < entry1->priority ? -1 : 1;
5531 else
5532 /* Before-strings sorted in order of increasing priority. */
5533 result = entry1->priority < entry2->priority ? -1 : 1;
5534 }
5535 else
5536 result = 0;
5537
5538 return result;
5539 }
5540
5541
5542 /* Load the vector IT->overlay_strings with overlay strings from IT's
5543 current buffer position, or from CHARPOS if that is > 0. Set
5544 IT->n_overlays to the total number of overlay strings found.
5545
5546 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5547 a time. On entry into load_overlay_strings,
5548 IT->current.overlay_string_index gives the number of overlay
5549 strings that have already been loaded by previous calls to this
5550 function.
5551
5552 IT->add_overlay_start contains an additional overlay start
5553 position to consider for taking overlay strings from, if non-zero.
5554 This position comes into play when the overlay has an `invisible'
5555 property, and both before and after-strings. When we've skipped to
5556 the end of the overlay, because of its `invisible' property, we
5557 nevertheless want its before-string to appear.
5558 IT->add_overlay_start will contain the overlay start position
5559 in this case.
5560
5561 Overlay strings are sorted so that after-string strings come in
5562 front of before-string strings. Within before and after-strings,
5563 strings are sorted by overlay priority. See also function
5564 compare_overlay_entries. */
5565
5566 static void
5567 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5568 {
5569 Lisp_Object overlay, window, str, invisible;
5570 struct Lisp_Overlay *ov;
5571 ptrdiff_t start, end;
5572 ptrdiff_t n = 0, i, j;
5573 int invis;
5574 struct overlay_entry entriesbuf[20];
5575 ptrdiff_t size = ARRAYELTS (entriesbuf);
5576 struct overlay_entry *entries = entriesbuf;
5577 USE_SAFE_ALLOCA;
5578
5579 if (charpos <= 0)
5580 charpos = IT_CHARPOS (*it);
5581
5582 /* Append the overlay string STRING of overlay OVERLAY to vector
5583 `entries' which has size `size' and currently contains `n'
5584 elements. AFTER_P means STRING is an after-string of
5585 OVERLAY. */
5586 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5587 do \
5588 { \
5589 Lisp_Object priority; \
5590 \
5591 if (n == size) \
5592 { \
5593 struct overlay_entry *old = entries; \
5594 SAFE_NALLOCA (entries, 2, size); \
5595 memcpy (entries, old, size * sizeof *entries); \
5596 size *= 2; \
5597 } \
5598 \
5599 entries[n].string = (STRING); \
5600 entries[n].overlay = (OVERLAY); \
5601 priority = Foverlay_get ((OVERLAY), Qpriority); \
5602 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5603 entries[n].after_string_p = (AFTER_P); \
5604 ++n; \
5605 } \
5606 while (false)
5607
5608 /* Process overlay before the overlay center. */
5609 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5610 {
5611 XSETMISC (overlay, ov);
5612 eassert (OVERLAYP (overlay));
5613 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5614 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5615
5616 if (end < charpos)
5617 break;
5618
5619 /* Skip this overlay if it doesn't start or end at IT's current
5620 position. */
5621 if (end != charpos && start != charpos)
5622 continue;
5623
5624 /* Skip this overlay if it doesn't apply to IT->w. */
5625 window = Foverlay_get (overlay, Qwindow);
5626 if (WINDOWP (window) && XWINDOW (window) != it->w)
5627 continue;
5628
5629 /* If the text ``under'' the overlay is invisible, both before-
5630 and after-strings from this overlay are visible; start and
5631 end position are indistinguishable. */
5632 invisible = Foverlay_get (overlay, Qinvisible);
5633 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5634
5635 /* If overlay has a non-empty before-string, record it. */
5636 if ((start == charpos || (end == charpos && invis != 0))
5637 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5638 && SCHARS (str))
5639 RECORD_OVERLAY_STRING (overlay, str, false);
5640
5641 /* If overlay has a non-empty after-string, record it. */
5642 if ((end == charpos || (start == charpos && invis != 0))
5643 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5644 && SCHARS (str))
5645 RECORD_OVERLAY_STRING (overlay, str, true);
5646 }
5647
5648 /* Process overlays after the overlay center. */
5649 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5650 {
5651 XSETMISC (overlay, ov);
5652 eassert (OVERLAYP (overlay));
5653 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5654 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5655
5656 if (start > charpos)
5657 break;
5658
5659 /* Skip this overlay if it doesn't start or end at IT's current
5660 position. */
5661 if (end != charpos && start != charpos)
5662 continue;
5663
5664 /* Skip this overlay if it doesn't apply to IT->w. */
5665 window = Foverlay_get (overlay, Qwindow);
5666 if (WINDOWP (window) && XWINDOW (window) != it->w)
5667 continue;
5668
5669 /* If the text ``under'' the overlay is invisible, it has a zero
5670 dimension, and both before- and after-strings apply. */
5671 invisible = Foverlay_get (overlay, Qinvisible);
5672 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5673
5674 /* If overlay has a non-empty before-string, record it. */
5675 if ((start == charpos || (end == charpos && invis != 0))
5676 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5677 && SCHARS (str))
5678 RECORD_OVERLAY_STRING (overlay, str, false);
5679
5680 /* If overlay has a non-empty after-string, record it. */
5681 if ((end == charpos || (start == charpos && invis != 0))
5682 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5683 && SCHARS (str))
5684 RECORD_OVERLAY_STRING (overlay, str, true);
5685 }
5686
5687 #undef RECORD_OVERLAY_STRING
5688
5689 /* Sort entries. */
5690 if (n > 1)
5691 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5692
5693 /* Record number of overlay strings, and where we computed it. */
5694 it->n_overlay_strings = n;
5695 it->overlay_strings_charpos = charpos;
5696
5697 /* IT->current.overlay_string_index is the number of overlay strings
5698 that have already been consumed by IT. Copy some of the
5699 remaining overlay strings to IT->overlay_strings. */
5700 i = 0;
5701 j = it->current.overlay_string_index;
5702 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5703 {
5704 it->overlay_strings[i] = entries[j].string;
5705 it->string_overlays[i++] = entries[j++].overlay;
5706 }
5707
5708 CHECK_IT (it);
5709 SAFE_FREE ();
5710 }
5711
5712
5713 /* Get the first chunk of overlay strings at IT's current buffer
5714 position, or at CHARPOS if that is > 0. Value is true if at
5715 least one overlay string was found. */
5716
5717 static bool
5718 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5719 {
5720 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5721 process. This fills IT->overlay_strings with strings, and sets
5722 IT->n_overlay_strings to the total number of strings to process.
5723 IT->pos.overlay_string_index has to be set temporarily to zero
5724 because load_overlay_strings needs this; it must be set to -1
5725 when no overlay strings are found because a zero value would
5726 indicate a position in the first overlay string. */
5727 it->current.overlay_string_index = 0;
5728 load_overlay_strings (it, charpos);
5729
5730 /* If we found overlay strings, set up IT to deliver display
5731 elements from the first one. Otherwise set up IT to deliver
5732 from current_buffer. */
5733 if (it->n_overlay_strings)
5734 {
5735 /* Make sure we know settings in current_buffer, so that we can
5736 restore meaningful values when we're done with the overlay
5737 strings. */
5738 if (compute_stop_p)
5739 compute_stop_pos (it);
5740 eassert (it->face_id >= 0);
5741
5742 /* Save IT's settings. They are restored after all overlay
5743 strings have been processed. */
5744 eassert (!compute_stop_p || it->sp == 0);
5745
5746 /* When called from handle_stop, there might be an empty display
5747 string loaded. In that case, don't bother saving it. But
5748 don't use this optimization with the bidi iterator, since we
5749 need the corresponding pop_it call to resync the bidi
5750 iterator's position with IT's position, after we are done
5751 with the overlay strings. (The corresponding call to pop_it
5752 in case of an empty display string is in
5753 next_overlay_string.) */
5754 if (!(!it->bidi_p
5755 && STRINGP (it->string) && !SCHARS (it->string)))
5756 push_it (it, NULL);
5757
5758 /* Set up IT to deliver display elements from the first overlay
5759 string. */
5760 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5761 it->string = it->overlay_strings[0];
5762 it->from_overlay = Qnil;
5763 it->stop_charpos = 0;
5764 eassert (STRINGP (it->string));
5765 it->end_charpos = SCHARS (it->string);
5766 it->prev_stop = 0;
5767 it->base_level_stop = 0;
5768 it->multibyte_p = STRING_MULTIBYTE (it->string);
5769 it->method = GET_FROM_STRING;
5770 it->from_disp_prop_p = 0;
5771
5772 /* Force paragraph direction to be that of the parent
5773 buffer. */
5774 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5775 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5776 else
5777 it->paragraph_embedding = L2R;
5778
5779 /* Set up the bidi iterator for this overlay string. */
5780 if (it->bidi_p)
5781 {
5782 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5783
5784 it->bidi_it.string.lstring = it->string;
5785 it->bidi_it.string.s = NULL;
5786 it->bidi_it.string.schars = SCHARS (it->string);
5787 it->bidi_it.string.bufpos = pos;
5788 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5789 it->bidi_it.string.unibyte = !it->multibyte_p;
5790 it->bidi_it.w = it->w;
5791 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5792 }
5793 return true;
5794 }
5795
5796 it->current.overlay_string_index = -1;
5797 return false;
5798 }
5799
5800 static bool
5801 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5802 {
5803 it->string = Qnil;
5804 it->method = GET_FROM_BUFFER;
5805
5806 get_overlay_strings_1 (it, charpos, true);
5807
5808 CHECK_IT (it);
5809
5810 /* Value is true if we found at least one overlay string. */
5811 return STRINGP (it->string);
5812 }
5813
5814
5815 \f
5816 /***********************************************************************
5817 Saving and restoring state
5818 ***********************************************************************/
5819
5820 /* Save current settings of IT on IT->stack. Called, for example,
5821 before setting up IT for an overlay string, to be able to restore
5822 IT's settings to what they were after the overlay string has been
5823 processed. If POSITION is non-NULL, it is the position to save on
5824 the stack instead of IT->position. */
5825
5826 static void
5827 push_it (struct it *it, struct text_pos *position)
5828 {
5829 struct iterator_stack_entry *p;
5830
5831 eassert (it->sp < IT_STACK_SIZE);
5832 p = it->stack + it->sp;
5833
5834 p->stop_charpos = it->stop_charpos;
5835 p->prev_stop = it->prev_stop;
5836 p->base_level_stop = it->base_level_stop;
5837 p->cmp_it = it->cmp_it;
5838 eassert (it->face_id >= 0);
5839 p->face_id = it->face_id;
5840 p->string = it->string;
5841 p->method = it->method;
5842 p->from_overlay = it->from_overlay;
5843 switch (p->method)
5844 {
5845 case GET_FROM_IMAGE:
5846 p->u.image.object = it->object;
5847 p->u.image.image_id = it->image_id;
5848 p->u.image.slice = it->slice;
5849 break;
5850 case GET_FROM_STRETCH:
5851 p->u.stretch.object = it->object;
5852 break;
5853 }
5854 p->position = position ? *position : it->position;
5855 p->current = it->current;
5856 p->end_charpos = it->end_charpos;
5857 p->string_nchars = it->string_nchars;
5858 p->area = it->area;
5859 p->multibyte_p = it->multibyte_p;
5860 p->avoid_cursor_p = it->avoid_cursor_p;
5861 p->space_width = it->space_width;
5862 p->font_height = it->font_height;
5863 p->voffset = it->voffset;
5864 p->string_from_display_prop_p = it->string_from_display_prop_p;
5865 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5866 p->display_ellipsis_p = false;
5867 p->line_wrap = it->line_wrap;
5868 p->bidi_p = it->bidi_p;
5869 p->paragraph_embedding = it->paragraph_embedding;
5870 p->from_disp_prop_p = it->from_disp_prop_p;
5871 ++it->sp;
5872
5873 /* Save the state of the bidi iterator as well. */
5874 if (it->bidi_p)
5875 bidi_push_it (&it->bidi_it);
5876 }
5877
5878 static void
5879 iterate_out_of_display_property (struct it *it)
5880 {
5881 bool buffer_p = !STRINGP (it->string);
5882 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5883 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5884
5885 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5886
5887 /* Maybe initialize paragraph direction. If we are at the beginning
5888 of a new paragraph, next_element_from_buffer may not have a
5889 chance to do that. */
5890 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5891 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
5892 /* prev_stop can be zero, so check against BEGV as well. */
5893 while (it->bidi_it.charpos >= bob
5894 && it->prev_stop <= it->bidi_it.charpos
5895 && it->bidi_it.charpos < CHARPOS (it->position)
5896 && it->bidi_it.charpos < eob)
5897 bidi_move_to_visually_next (&it->bidi_it);
5898 /* Record the stop_pos we just crossed, for when we cross it
5899 back, maybe. */
5900 if (it->bidi_it.charpos > CHARPOS (it->position))
5901 it->prev_stop = CHARPOS (it->position);
5902 /* If we ended up not where pop_it put us, resync IT's
5903 positional members with the bidi iterator. */
5904 if (it->bidi_it.charpos != CHARPOS (it->position))
5905 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5906 if (buffer_p)
5907 it->current.pos = it->position;
5908 else
5909 it->current.string_pos = it->position;
5910 }
5911
5912 /* Restore IT's settings from IT->stack. Called, for example, when no
5913 more overlay strings must be processed, and we return to delivering
5914 display elements from a buffer, or when the end of a string from a
5915 `display' property is reached and we return to delivering display
5916 elements from an overlay string, or from a buffer. */
5917
5918 static void
5919 pop_it (struct it *it)
5920 {
5921 struct iterator_stack_entry *p;
5922 bool from_display_prop = it->from_disp_prop_p;
5923
5924 eassert (it->sp > 0);
5925 --it->sp;
5926 p = it->stack + it->sp;
5927 it->stop_charpos = p->stop_charpos;
5928 it->prev_stop = p->prev_stop;
5929 it->base_level_stop = p->base_level_stop;
5930 it->cmp_it = p->cmp_it;
5931 it->face_id = p->face_id;
5932 it->current = p->current;
5933 it->position = p->position;
5934 it->string = p->string;
5935 it->from_overlay = p->from_overlay;
5936 if (NILP (it->string))
5937 SET_TEXT_POS (it->current.string_pos, -1, -1);
5938 it->method = p->method;
5939 switch (it->method)
5940 {
5941 case GET_FROM_IMAGE:
5942 it->image_id = p->u.image.image_id;
5943 it->object = p->u.image.object;
5944 it->slice = p->u.image.slice;
5945 break;
5946 case GET_FROM_STRETCH:
5947 it->object = p->u.stretch.object;
5948 break;
5949 case GET_FROM_BUFFER:
5950 it->object = it->w->contents;
5951 break;
5952 case GET_FROM_STRING:
5953 {
5954 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5955
5956 /* Restore the face_box_p flag, since it could have been
5957 overwritten by the face of the object that we just finished
5958 displaying. */
5959 if (face)
5960 it->face_box_p = face->box != FACE_NO_BOX;
5961 it->object = it->string;
5962 }
5963 break;
5964 case GET_FROM_DISPLAY_VECTOR:
5965 if (it->s)
5966 it->method = GET_FROM_C_STRING;
5967 else if (STRINGP (it->string))
5968 it->method = GET_FROM_STRING;
5969 else
5970 {
5971 it->method = GET_FROM_BUFFER;
5972 it->object = it->w->contents;
5973 }
5974 }
5975 it->end_charpos = p->end_charpos;
5976 it->string_nchars = p->string_nchars;
5977 it->area = p->area;
5978 it->multibyte_p = p->multibyte_p;
5979 it->avoid_cursor_p = p->avoid_cursor_p;
5980 it->space_width = p->space_width;
5981 it->font_height = p->font_height;
5982 it->voffset = p->voffset;
5983 it->string_from_display_prop_p = p->string_from_display_prop_p;
5984 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5985 it->line_wrap = p->line_wrap;
5986 it->bidi_p = p->bidi_p;
5987 it->paragraph_embedding = p->paragraph_embedding;
5988 it->from_disp_prop_p = p->from_disp_prop_p;
5989 if (it->bidi_p)
5990 {
5991 bidi_pop_it (&it->bidi_it);
5992 /* Bidi-iterate until we get out of the portion of text, if any,
5993 covered by a `display' text property or by an overlay with
5994 `display' property. (We cannot just jump there, because the
5995 internal coherency of the bidi iterator state can not be
5996 preserved across such jumps.) We also must determine the
5997 paragraph base direction if the overlay we just processed is
5998 at the beginning of a new paragraph. */
5999 if (from_display_prop
6000 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6001 iterate_out_of_display_property (it);
6002
6003 eassert ((BUFFERP (it->object)
6004 && IT_CHARPOS (*it) == it->bidi_it.charpos
6005 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6006 || (STRINGP (it->object)
6007 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6008 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6009 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6010 }
6011 }
6012
6013
6014 \f
6015 /***********************************************************************
6016 Moving over lines
6017 ***********************************************************************/
6018
6019 /* Set IT's current position to the previous line start. */
6020
6021 static void
6022 back_to_previous_line_start (struct it *it)
6023 {
6024 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6025
6026 DEC_BOTH (cp, bp);
6027 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6028 }
6029
6030
6031 /* Move IT to the next line start.
6032
6033 Value is true if a newline was found. Set *SKIPPED_P to true if
6034 we skipped over part of the text (as opposed to moving the iterator
6035 continuously over the text). Otherwise, don't change the value
6036 of *SKIPPED_P.
6037
6038 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6039 iterator on the newline, if it was found.
6040
6041 Newlines may come from buffer text, overlay strings, or strings
6042 displayed via the `display' property. That's the reason we can't
6043 simply use find_newline_no_quit.
6044
6045 Note that this function may not skip over invisible text that is so
6046 because of text properties and immediately follows a newline. If
6047 it would, function reseat_at_next_visible_line_start, when called
6048 from set_iterator_to_next, would effectively make invisible
6049 characters following a newline part of the wrong glyph row, which
6050 leads to wrong cursor motion. */
6051
6052 static bool
6053 forward_to_next_line_start (struct it *it, bool *skipped_p,
6054 struct bidi_it *bidi_it_prev)
6055 {
6056 ptrdiff_t old_selective;
6057 bool newline_found_p = false;
6058 int n;
6059 const int MAX_NEWLINE_DISTANCE = 500;
6060
6061 /* If already on a newline, just consume it to avoid unintended
6062 skipping over invisible text below. */
6063 if (it->what == IT_CHARACTER
6064 && it->c == '\n'
6065 && CHARPOS (it->position) == IT_CHARPOS (*it))
6066 {
6067 if (it->bidi_p && bidi_it_prev)
6068 *bidi_it_prev = it->bidi_it;
6069 set_iterator_to_next (it, false);
6070 it->c = 0;
6071 return true;
6072 }
6073
6074 /* Don't handle selective display in the following. It's (a)
6075 unnecessary because it's done by the caller, and (b) leads to an
6076 infinite recursion because next_element_from_ellipsis indirectly
6077 calls this function. */
6078 old_selective = it->selective;
6079 it->selective = 0;
6080
6081 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6082 from buffer text. */
6083 for (n = 0;
6084 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6085 n += !STRINGP (it->string))
6086 {
6087 if (!get_next_display_element (it))
6088 return false;
6089 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6090 if (newline_found_p && it->bidi_p && bidi_it_prev)
6091 *bidi_it_prev = it->bidi_it;
6092 set_iterator_to_next (it, false);
6093 }
6094
6095 /* If we didn't find a newline near enough, see if we can use a
6096 short-cut. */
6097 if (!newline_found_p)
6098 {
6099 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6100 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6101 1, &bytepos);
6102 Lisp_Object pos;
6103
6104 eassert (!STRINGP (it->string));
6105
6106 /* If there isn't any `display' property in sight, and no
6107 overlays, we can just use the position of the newline in
6108 buffer text. */
6109 if (it->stop_charpos >= limit
6110 || ((pos = Fnext_single_property_change (make_number (start),
6111 Qdisplay, Qnil,
6112 make_number (limit)),
6113 NILP (pos))
6114 && next_overlay_change (start) == ZV))
6115 {
6116 if (!it->bidi_p)
6117 {
6118 IT_CHARPOS (*it) = limit;
6119 IT_BYTEPOS (*it) = bytepos;
6120 }
6121 else
6122 {
6123 struct bidi_it bprev;
6124
6125 /* Help bidi.c avoid expensive searches for display
6126 properties and overlays, by telling it that there are
6127 none up to `limit'. */
6128 if (it->bidi_it.disp_pos < limit)
6129 {
6130 it->bidi_it.disp_pos = limit;
6131 it->bidi_it.disp_prop = 0;
6132 }
6133 do {
6134 bprev = it->bidi_it;
6135 bidi_move_to_visually_next (&it->bidi_it);
6136 } while (it->bidi_it.charpos != limit);
6137 IT_CHARPOS (*it) = limit;
6138 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6139 if (bidi_it_prev)
6140 *bidi_it_prev = bprev;
6141 }
6142 *skipped_p = newline_found_p = true;
6143 }
6144 else
6145 {
6146 while (get_next_display_element (it)
6147 && !newline_found_p)
6148 {
6149 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6150 if (newline_found_p && it->bidi_p && bidi_it_prev)
6151 *bidi_it_prev = it->bidi_it;
6152 set_iterator_to_next (it, false);
6153 }
6154 }
6155 }
6156
6157 it->selective = old_selective;
6158 return newline_found_p;
6159 }
6160
6161
6162 /* Set IT's current position to the previous visible line start. Skip
6163 invisible text that is so either due to text properties or due to
6164 selective display. Caution: this does not change IT->current_x and
6165 IT->hpos. */
6166
6167 static void
6168 back_to_previous_visible_line_start (struct it *it)
6169 {
6170 while (IT_CHARPOS (*it) > BEGV)
6171 {
6172 back_to_previous_line_start (it);
6173
6174 if (IT_CHARPOS (*it) <= BEGV)
6175 break;
6176
6177 /* If selective > 0, then lines indented more than its value are
6178 invisible. */
6179 if (it->selective > 0
6180 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6181 it->selective))
6182 continue;
6183
6184 /* Check the newline before point for invisibility. */
6185 {
6186 Lisp_Object prop;
6187 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6188 Qinvisible, it->window);
6189 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6190 continue;
6191 }
6192
6193 if (IT_CHARPOS (*it) <= BEGV)
6194 break;
6195
6196 {
6197 struct it it2;
6198 void *it2data = NULL;
6199 ptrdiff_t pos;
6200 ptrdiff_t beg, end;
6201 Lisp_Object val, overlay;
6202
6203 SAVE_IT (it2, *it, it2data);
6204
6205 /* If newline is part of a composition, continue from start of composition */
6206 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6207 && beg < IT_CHARPOS (*it))
6208 goto replaced;
6209
6210 /* If newline is replaced by a display property, find start of overlay
6211 or interval and continue search from that point. */
6212 pos = --IT_CHARPOS (it2);
6213 --IT_BYTEPOS (it2);
6214 it2.sp = 0;
6215 bidi_unshelve_cache (NULL, false);
6216 it2.string_from_display_prop_p = false;
6217 it2.from_disp_prop_p = false;
6218 if (handle_display_prop (&it2) == HANDLED_RETURN
6219 && !NILP (val = get_char_property_and_overlay
6220 (make_number (pos), Qdisplay, Qnil, &overlay))
6221 && (OVERLAYP (overlay)
6222 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6223 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6224 {
6225 RESTORE_IT (it, it, it2data);
6226 goto replaced;
6227 }
6228
6229 /* Newline is not replaced by anything -- so we are done. */
6230 RESTORE_IT (it, it, it2data);
6231 break;
6232
6233 replaced:
6234 if (beg < BEGV)
6235 beg = BEGV;
6236 IT_CHARPOS (*it) = beg;
6237 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6238 }
6239 }
6240
6241 it->continuation_lines_width = 0;
6242
6243 eassert (IT_CHARPOS (*it) >= BEGV);
6244 eassert (IT_CHARPOS (*it) == BEGV
6245 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6246 CHECK_IT (it);
6247 }
6248
6249
6250 /* Reseat iterator IT at the previous visible line start. Skip
6251 invisible text that is so either due to text properties or due to
6252 selective display. At the end, update IT's overlay information,
6253 face information etc. */
6254
6255 void
6256 reseat_at_previous_visible_line_start (struct it *it)
6257 {
6258 back_to_previous_visible_line_start (it);
6259 reseat (it, it->current.pos, true);
6260 CHECK_IT (it);
6261 }
6262
6263
6264 /* Reseat iterator IT on the next visible line start in the current
6265 buffer. ON_NEWLINE_P means position IT on the newline
6266 preceding the line start. Skip over invisible text that is so
6267 because of selective display. Compute faces, overlays etc at the
6268 new position. Note that this function does not skip over text that
6269 is invisible because of text properties. */
6270
6271 static void
6272 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6273 {
6274 bool skipped_p = false;
6275 struct bidi_it bidi_it_prev;
6276 bool newline_found_p
6277 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6278
6279 /* Skip over lines that are invisible because they are indented
6280 more than the value of IT->selective. */
6281 if (it->selective > 0)
6282 while (IT_CHARPOS (*it) < ZV
6283 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6284 it->selective))
6285 {
6286 eassert (IT_BYTEPOS (*it) == BEGV
6287 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6288 newline_found_p =
6289 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6290 }
6291
6292 /* Position on the newline if that's what's requested. */
6293 if (on_newline_p && newline_found_p)
6294 {
6295 if (STRINGP (it->string))
6296 {
6297 if (IT_STRING_CHARPOS (*it) > 0)
6298 {
6299 if (!it->bidi_p)
6300 {
6301 --IT_STRING_CHARPOS (*it);
6302 --IT_STRING_BYTEPOS (*it);
6303 }
6304 else
6305 {
6306 /* We need to restore the bidi iterator to the state
6307 it had on the newline, and resync the IT's
6308 position with that. */
6309 it->bidi_it = bidi_it_prev;
6310 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6311 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6312 }
6313 }
6314 }
6315 else if (IT_CHARPOS (*it) > BEGV)
6316 {
6317 if (!it->bidi_p)
6318 {
6319 --IT_CHARPOS (*it);
6320 --IT_BYTEPOS (*it);
6321 }
6322 else
6323 {
6324 /* We need to restore the bidi iterator to the state it
6325 had on the newline and resync IT with that. */
6326 it->bidi_it = bidi_it_prev;
6327 IT_CHARPOS (*it) = it->bidi_it.charpos;
6328 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6329 }
6330 reseat (it, it->current.pos, false);
6331 }
6332 }
6333 else if (skipped_p)
6334 reseat (it, it->current.pos, false);
6335
6336 CHECK_IT (it);
6337 }
6338
6339
6340 \f
6341 /***********************************************************************
6342 Changing an iterator's position
6343 ***********************************************************************/
6344
6345 /* Change IT's current position to POS in current_buffer.
6346 If FORCE_P, always check for text properties at the new position.
6347 Otherwise, text properties are only looked up if POS >=
6348 IT->check_charpos of a property. */
6349
6350 static void
6351 reseat (struct it *it, struct text_pos pos, bool force_p)
6352 {
6353 ptrdiff_t original_pos = IT_CHARPOS (*it);
6354
6355 reseat_1 (it, pos, false);
6356
6357 /* Determine where to check text properties. Avoid doing it
6358 where possible because text property lookup is very expensive. */
6359 if (force_p
6360 || CHARPOS (pos) > it->stop_charpos
6361 || CHARPOS (pos) < original_pos)
6362 {
6363 if (it->bidi_p)
6364 {
6365 /* For bidi iteration, we need to prime prev_stop and
6366 base_level_stop with our best estimations. */
6367 /* Implementation note: Of course, POS is not necessarily a
6368 stop position, so assigning prev_pos to it is a lie; we
6369 should have called compute_stop_backwards. However, if
6370 the current buffer does not include any R2L characters,
6371 that call would be a waste of cycles, because the
6372 iterator will never move back, and thus never cross this
6373 "fake" stop position. So we delay that backward search
6374 until the time we really need it, in next_element_from_buffer. */
6375 if (CHARPOS (pos) != it->prev_stop)
6376 it->prev_stop = CHARPOS (pos);
6377 if (CHARPOS (pos) < it->base_level_stop)
6378 it->base_level_stop = 0; /* meaning it's unknown */
6379 handle_stop (it);
6380 }
6381 else
6382 {
6383 handle_stop (it);
6384 it->prev_stop = it->base_level_stop = 0;
6385 }
6386
6387 }
6388
6389 CHECK_IT (it);
6390 }
6391
6392
6393 /* Change IT's buffer position to POS. SET_STOP_P means set
6394 IT->stop_pos to POS, also. */
6395
6396 static void
6397 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6398 {
6399 /* Don't call this function when scanning a C string. */
6400 eassert (it->s == NULL);
6401
6402 /* POS must be a reasonable value. */
6403 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6404
6405 it->current.pos = it->position = pos;
6406 it->end_charpos = ZV;
6407 it->dpvec = NULL;
6408 it->current.dpvec_index = -1;
6409 it->current.overlay_string_index = -1;
6410 IT_STRING_CHARPOS (*it) = -1;
6411 IT_STRING_BYTEPOS (*it) = -1;
6412 it->string = Qnil;
6413 it->method = GET_FROM_BUFFER;
6414 it->object = it->w->contents;
6415 it->area = TEXT_AREA;
6416 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6417 it->sp = 0;
6418 it->string_from_display_prop_p = false;
6419 it->string_from_prefix_prop_p = false;
6420
6421 it->from_disp_prop_p = false;
6422 it->face_before_selective_p = false;
6423 if (it->bidi_p)
6424 {
6425 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6426 &it->bidi_it);
6427 bidi_unshelve_cache (NULL, false);
6428 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6429 it->bidi_it.string.s = NULL;
6430 it->bidi_it.string.lstring = Qnil;
6431 it->bidi_it.string.bufpos = 0;
6432 it->bidi_it.string.from_disp_str = false;
6433 it->bidi_it.string.unibyte = false;
6434 it->bidi_it.w = it->w;
6435 }
6436
6437 if (set_stop_p)
6438 {
6439 it->stop_charpos = CHARPOS (pos);
6440 it->base_level_stop = CHARPOS (pos);
6441 }
6442 /* This make the information stored in it->cmp_it invalidate. */
6443 it->cmp_it.id = -1;
6444 }
6445
6446
6447 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6448 If S is non-null, it is a C string to iterate over. Otherwise,
6449 STRING gives a Lisp string to iterate over.
6450
6451 If PRECISION > 0, don't return more then PRECISION number of
6452 characters from the string.
6453
6454 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6455 characters have been returned. FIELD_WIDTH < 0 means an infinite
6456 field width.
6457
6458 MULTIBYTE = 0 means disable processing of multibyte characters,
6459 MULTIBYTE > 0 means enable it,
6460 MULTIBYTE < 0 means use IT->multibyte_p.
6461
6462 IT must be initialized via a prior call to init_iterator before
6463 calling this function. */
6464
6465 static void
6466 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6467 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6468 int multibyte)
6469 {
6470 /* No text property checks performed by default, but see below. */
6471 it->stop_charpos = -1;
6472
6473 /* Set iterator position and end position. */
6474 memset (&it->current, 0, sizeof it->current);
6475 it->current.overlay_string_index = -1;
6476 it->current.dpvec_index = -1;
6477 eassert (charpos >= 0);
6478
6479 /* If STRING is specified, use its multibyteness, otherwise use the
6480 setting of MULTIBYTE, if specified. */
6481 if (multibyte >= 0)
6482 it->multibyte_p = multibyte > 0;
6483
6484 /* Bidirectional reordering of strings is controlled by the default
6485 value of bidi-display-reordering. Don't try to reorder while
6486 loading loadup.el, as the necessary character property tables are
6487 not yet available. */
6488 it->bidi_p =
6489 NILP (Vpurify_flag)
6490 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6491
6492 if (s == NULL)
6493 {
6494 eassert (STRINGP (string));
6495 it->string = string;
6496 it->s = NULL;
6497 it->end_charpos = it->string_nchars = SCHARS (string);
6498 it->method = GET_FROM_STRING;
6499 it->current.string_pos = string_pos (charpos, string);
6500
6501 if (it->bidi_p)
6502 {
6503 it->bidi_it.string.lstring = string;
6504 it->bidi_it.string.s = NULL;
6505 it->bidi_it.string.schars = it->end_charpos;
6506 it->bidi_it.string.bufpos = 0;
6507 it->bidi_it.string.from_disp_str = false;
6508 it->bidi_it.string.unibyte = !it->multibyte_p;
6509 it->bidi_it.w = it->w;
6510 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6511 FRAME_WINDOW_P (it->f), &it->bidi_it);
6512 }
6513 }
6514 else
6515 {
6516 it->s = (const unsigned char *) s;
6517 it->string = Qnil;
6518
6519 /* Note that we use IT->current.pos, not it->current.string_pos,
6520 for displaying C strings. */
6521 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6522 if (it->multibyte_p)
6523 {
6524 it->current.pos = c_string_pos (charpos, s, true);
6525 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6526 }
6527 else
6528 {
6529 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6530 it->end_charpos = it->string_nchars = strlen (s);
6531 }
6532
6533 if (it->bidi_p)
6534 {
6535 it->bidi_it.string.lstring = Qnil;
6536 it->bidi_it.string.s = (const unsigned char *) s;
6537 it->bidi_it.string.schars = it->end_charpos;
6538 it->bidi_it.string.bufpos = 0;
6539 it->bidi_it.string.from_disp_str = false;
6540 it->bidi_it.string.unibyte = !it->multibyte_p;
6541 it->bidi_it.w = it->w;
6542 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6543 &it->bidi_it);
6544 }
6545 it->method = GET_FROM_C_STRING;
6546 }
6547
6548 /* PRECISION > 0 means don't return more than PRECISION characters
6549 from the string. */
6550 if (precision > 0 && it->end_charpos - charpos > precision)
6551 {
6552 it->end_charpos = it->string_nchars = charpos + precision;
6553 if (it->bidi_p)
6554 it->bidi_it.string.schars = it->end_charpos;
6555 }
6556
6557 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6558 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6559 FIELD_WIDTH < 0 means infinite field width. This is useful for
6560 padding with `-' at the end of a mode line. */
6561 if (field_width < 0)
6562 field_width = INFINITY;
6563 /* Implementation note: We deliberately don't enlarge
6564 it->bidi_it.string.schars here to fit it->end_charpos, because
6565 the bidi iterator cannot produce characters out of thin air. */
6566 if (field_width > it->end_charpos - charpos)
6567 it->end_charpos = charpos + field_width;
6568
6569 /* Use the standard display table for displaying strings. */
6570 if (DISP_TABLE_P (Vstandard_display_table))
6571 it->dp = XCHAR_TABLE (Vstandard_display_table);
6572
6573 it->stop_charpos = charpos;
6574 it->prev_stop = charpos;
6575 it->base_level_stop = 0;
6576 if (it->bidi_p)
6577 {
6578 it->bidi_it.first_elt = true;
6579 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6580 it->bidi_it.disp_pos = -1;
6581 }
6582 if (s == NULL && it->multibyte_p)
6583 {
6584 ptrdiff_t endpos = SCHARS (it->string);
6585 if (endpos > it->end_charpos)
6586 endpos = it->end_charpos;
6587 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6588 it->string);
6589 }
6590 CHECK_IT (it);
6591 }
6592
6593
6594 \f
6595 /***********************************************************************
6596 Iteration
6597 ***********************************************************************/
6598
6599 /* Map enum it_method value to corresponding next_element_from_* function. */
6600
6601 typedef bool (*next_element_function) (struct it *);
6602
6603 static next_element_function const get_next_element[NUM_IT_METHODS] =
6604 {
6605 next_element_from_buffer,
6606 next_element_from_display_vector,
6607 next_element_from_string,
6608 next_element_from_c_string,
6609 next_element_from_image,
6610 next_element_from_stretch
6611 };
6612
6613 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6614
6615
6616 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6617 (possibly with the following characters). */
6618
6619 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6620 ((IT)->cmp_it.id >= 0 \
6621 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6622 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6623 END_CHARPOS, (IT)->w, \
6624 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6625 (IT)->string)))
6626
6627
6628 /* Lookup the char-table Vglyphless_char_display for character C (-1
6629 if we want information for no-font case), and return the display
6630 method symbol. By side-effect, update it->what and
6631 it->glyphless_method. This function is called from
6632 get_next_display_element for each character element, and from
6633 x_produce_glyphs when no suitable font was found. */
6634
6635 Lisp_Object
6636 lookup_glyphless_char_display (int c, struct it *it)
6637 {
6638 Lisp_Object glyphless_method = Qnil;
6639
6640 if (CHAR_TABLE_P (Vglyphless_char_display)
6641 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6642 {
6643 if (c >= 0)
6644 {
6645 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6646 if (CONSP (glyphless_method))
6647 glyphless_method = FRAME_WINDOW_P (it->f)
6648 ? XCAR (glyphless_method)
6649 : XCDR (glyphless_method);
6650 }
6651 else
6652 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6653 }
6654
6655 retry:
6656 if (NILP (glyphless_method))
6657 {
6658 if (c >= 0)
6659 /* The default is to display the character by a proper font. */
6660 return Qnil;
6661 /* The default for the no-font case is to display an empty box. */
6662 glyphless_method = Qempty_box;
6663 }
6664 if (EQ (glyphless_method, Qzero_width))
6665 {
6666 if (c >= 0)
6667 return glyphless_method;
6668 /* This method can't be used for the no-font case. */
6669 glyphless_method = Qempty_box;
6670 }
6671 if (EQ (glyphless_method, Qthin_space))
6672 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6673 else if (EQ (glyphless_method, Qempty_box))
6674 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6675 else if (EQ (glyphless_method, Qhex_code))
6676 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6677 else if (STRINGP (glyphless_method))
6678 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6679 else
6680 {
6681 /* Invalid value. We use the default method. */
6682 glyphless_method = Qnil;
6683 goto retry;
6684 }
6685 it->what = IT_GLYPHLESS;
6686 return glyphless_method;
6687 }
6688
6689 /* Merge escape glyph face and cache the result. */
6690
6691 static struct frame *last_escape_glyph_frame = NULL;
6692 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6693 static int last_escape_glyph_merged_face_id = 0;
6694
6695 static int
6696 merge_escape_glyph_face (struct it *it)
6697 {
6698 int face_id;
6699
6700 if (it->f == last_escape_glyph_frame
6701 && it->face_id == last_escape_glyph_face_id)
6702 face_id = last_escape_glyph_merged_face_id;
6703 else
6704 {
6705 /* Merge the `escape-glyph' face into the current face. */
6706 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6707 last_escape_glyph_frame = it->f;
6708 last_escape_glyph_face_id = it->face_id;
6709 last_escape_glyph_merged_face_id = face_id;
6710 }
6711 return face_id;
6712 }
6713
6714 /* Likewise for glyphless glyph face. */
6715
6716 static struct frame *last_glyphless_glyph_frame = NULL;
6717 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6718 static int last_glyphless_glyph_merged_face_id = 0;
6719
6720 int
6721 merge_glyphless_glyph_face (struct it *it)
6722 {
6723 int face_id;
6724
6725 if (it->f == last_glyphless_glyph_frame
6726 && it->face_id == last_glyphless_glyph_face_id)
6727 face_id = last_glyphless_glyph_merged_face_id;
6728 else
6729 {
6730 /* Merge the `glyphless-char' face into the current face. */
6731 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6732 last_glyphless_glyph_frame = it->f;
6733 last_glyphless_glyph_face_id = it->face_id;
6734 last_glyphless_glyph_merged_face_id = face_id;
6735 }
6736 return face_id;
6737 }
6738
6739 /* Load IT's display element fields with information about the next
6740 display element from the current position of IT. Value is false if
6741 end of buffer (or C string) is reached. */
6742
6743 static bool
6744 get_next_display_element (struct it *it)
6745 {
6746 /* True means that we found a display element. False means that
6747 we hit the end of what we iterate over. Performance note: the
6748 function pointer `method' used here turns out to be faster than
6749 using a sequence of if-statements. */
6750 bool success_p;
6751
6752 get_next:
6753 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6754
6755 if (it->what == IT_CHARACTER)
6756 {
6757 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6758 and only if (a) the resolved directionality of that character
6759 is R..." */
6760 /* FIXME: Do we need an exception for characters from display
6761 tables? */
6762 if (it->bidi_p && it->bidi_it.type == STRONG_R
6763 && !inhibit_bidi_mirroring)
6764 it->c = bidi_mirror_char (it->c);
6765 /* Map via display table or translate control characters.
6766 IT->c, IT->len etc. have been set to the next character by
6767 the function call above. If we have a display table, and it
6768 contains an entry for IT->c, translate it. Don't do this if
6769 IT->c itself comes from a display table, otherwise we could
6770 end up in an infinite recursion. (An alternative could be to
6771 count the recursion depth of this function and signal an
6772 error when a certain maximum depth is reached.) Is it worth
6773 it? */
6774 if (success_p && it->dpvec == NULL)
6775 {
6776 Lisp_Object dv;
6777 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6778 bool nonascii_space_p = false;
6779 bool nonascii_hyphen_p = false;
6780 int c = it->c; /* This is the character to display. */
6781
6782 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6783 {
6784 eassert (SINGLE_BYTE_CHAR_P (c));
6785 if (unibyte_display_via_language_environment)
6786 {
6787 c = DECODE_CHAR (unibyte, c);
6788 if (c < 0)
6789 c = BYTE8_TO_CHAR (it->c);
6790 }
6791 else
6792 c = BYTE8_TO_CHAR (it->c);
6793 }
6794
6795 if (it->dp
6796 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6797 VECTORP (dv)))
6798 {
6799 struct Lisp_Vector *v = XVECTOR (dv);
6800
6801 /* Return the first character from the display table
6802 entry, if not empty. If empty, don't display the
6803 current character. */
6804 if (v->header.size)
6805 {
6806 it->dpvec_char_len = it->len;
6807 it->dpvec = v->contents;
6808 it->dpend = v->contents + v->header.size;
6809 it->current.dpvec_index = 0;
6810 it->dpvec_face_id = -1;
6811 it->saved_face_id = it->face_id;
6812 it->method = GET_FROM_DISPLAY_VECTOR;
6813 it->ellipsis_p = false;
6814 }
6815 else
6816 {
6817 set_iterator_to_next (it, false);
6818 }
6819 goto get_next;
6820 }
6821
6822 if (! NILP (lookup_glyphless_char_display (c, it)))
6823 {
6824 if (it->what == IT_GLYPHLESS)
6825 goto done;
6826 /* Don't display this character. */
6827 set_iterator_to_next (it, false);
6828 goto get_next;
6829 }
6830
6831 /* If `nobreak-char-display' is non-nil, we display
6832 non-ASCII spaces and hyphens specially. */
6833 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6834 {
6835 if (c == 0xA0)
6836 nonascii_space_p = true;
6837 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6838 nonascii_hyphen_p = true;
6839 }
6840
6841 /* Translate control characters into `\003' or `^C' form.
6842 Control characters coming from a display table entry are
6843 currently not translated because we use IT->dpvec to hold
6844 the translation. This could easily be changed but I
6845 don't believe that it is worth doing.
6846
6847 The characters handled by `nobreak-char-display' must be
6848 translated too.
6849
6850 Non-printable characters and raw-byte characters are also
6851 translated to octal form. */
6852 if (((c < ' ' || c == 127) /* ASCII control chars. */
6853 ? (it->area != TEXT_AREA
6854 /* In mode line, treat \n, \t like other crl chars. */
6855 || (c != '\t'
6856 && it->glyph_row
6857 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6858 || (c != '\n' && c != '\t'))
6859 : (nonascii_space_p
6860 || nonascii_hyphen_p
6861 || CHAR_BYTE8_P (c)
6862 || ! CHAR_PRINTABLE_P (c))))
6863 {
6864 /* C is a control character, non-ASCII space/hyphen,
6865 raw-byte, or a non-printable character which must be
6866 displayed either as '\003' or as `^C' where the '\\'
6867 and '^' can be defined in the display table. Fill
6868 IT->ctl_chars with glyphs for what we have to
6869 display. Then, set IT->dpvec to these glyphs. */
6870 Lisp_Object gc;
6871 int ctl_len;
6872 int face_id;
6873 int lface_id = 0;
6874 int escape_glyph;
6875
6876 /* Handle control characters with ^. */
6877
6878 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6879 {
6880 int g;
6881
6882 g = '^'; /* default glyph for Control */
6883 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6884 if (it->dp
6885 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6886 {
6887 g = GLYPH_CODE_CHAR (gc);
6888 lface_id = GLYPH_CODE_FACE (gc);
6889 }
6890
6891 face_id = (lface_id
6892 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6893 : merge_escape_glyph_face (it));
6894
6895 XSETINT (it->ctl_chars[0], g);
6896 XSETINT (it->ctl_chars[1], c ^ 0100);
6897 ctl_len = 2;
6898 goto display_control;
6899 }
6900
6901 /* Handle non-ascii space in the mode where it only gets
6902 highlighting. */
6903
6904 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6905 {
6906 /* Merge `nobreak-space' into the current face. */
6907 face_id = merge_faces (it->f, Qnobreak_space, 0,
6908 it->face_id);
6909 XSETINT (it->ctl_chars[0], ' ');
6910 ctl_len = 1;
6911 goto display_control;
6912 }
6913
6914 /* Handle sequences that start with the "escape glyph". */
6915
6916 /* the default escape glyph is \. */
6917 escape_glyph = '\\';
6918
6919 if (it->dp
6920 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6921 {
6922 escape_glyph = GLYPH_CODE_CHAR (gc);
6923 lface_id = GLYPH_CODE_FACE (gc);
6924 }
6925
6926 face_id = (lface_id
6927 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6928 : merge_escape_glyph_face (it));
6929
6930 /* Draw non-ASCII hyphen with just highlighting: */
6931
6932 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6933 {
6934 XSETINT (it->ctl_chars[0], '-');
6935 ctl_len = 1;
6936 goto display_control;
6937 }
6938
6939 /* Draw non-ASCII space/hyphen with escape glyph: */
6940
6941 if (nonascii_space_p || nonascii_hyphen_p)
6942 {
6943 XSETINT (it->ctl_chars[0], escape_glyph);
6944 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6945 ctl_len = 2;
6946 goto display_control;
6947 }
6948
6949 {
6950 char str[10];
6951 int len, i;
6952
6953 if (CHAR_BYTE8_P (c))
6954 /* Display \200 instead of \17777600. */
6955 c = CHAR_TO_BYTE8 (c);
6956 len = sprintf (str, "%03o", c);
6957
6958 XSETINT (it->ctl_chars[0], escape_glyph);
6959 for (i = 0; i < len; i++)
6960 XSETINT (it->ctl_chars[i + 1], str[i]);
6961 ctl_len = len + 1;
6962 }
6963
6964 display_control:
6965 /* Set up IT->dpvec and return first character from it. */
6966 it->dpvec_char_len = it->len;
6967 it->dpvec = it->ctl_chars;
6968 it->dpend = it->dpvec + ctl_len;
6969 it->current.dpvec_index = 0;
6970 it->dpvec_face_id = face_id;
6971 it->saved_face_id = it->face_id;
6972 it->method = GET_FROM_DISPLAY_VECTOR;
6973 it->ellipsis_p = false;
6974 goto get_next;
6975 }
6976 it->char_to_display = c;
6977 }
6978 else if (success_p)
6979 {
6980 it->char_to_display = it->c;
6981 }
6982 }
6983
6984 #ifdef HAVE_WINDOW_SYSTEM
6985 /* Adjust face id for a multibyte character. There are no multibyte
6986 character in unibyte text. */
6987 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6988 && it->multibyte_p
6989 && success_p
6990 && FRAME_WINDOW_P (it->f))
6991 {
6992 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6993
6994 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6995 {
6996 /* Automatic composition with glyph-string. */
6997 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6998
6999 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7000 }
7001 else
7002 {
7003 ptrdiff_t pos = (it->s ? -1
7004 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7005 : IT_CHARPOS (*it));
7006 int c;
7007
7008 if (it->what == IT_CHARACTER)
7009 c = it->char_to_display;
7010 else
7011 {
7012 struct composition *cmp = composition_table[it->cmp_it.id];
7013 int i;
7014
7015 c = ' ';
7016 for (i = 0; i < cmp->glyph_len; i++)
7017 /* TAB in a composition means display glyphs with
7018 padding space on the left or right. */
7019 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7020 break;
7021 }
7022 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7023 }
7024 }
7025 #endif /* HAVE_WINDOW_SYSTEM */
7026
7027 done:
7028 /* Is this character the last one of a run of characters with
7029 box? If yes, set IT->end_of_box_run_p to true. */
7030 if (it->face_box_p
7031 && it->s == NULL)
7032 {
7033 if (it->method == GET_FROM_STRING && it->sp)
7034 {
7035 int face_id = underlying_face_id (it);
7036 struct face *face = FACE_FROM_ID (it->f, face_id);
7037
7038 if (face)
7039 {
7040 if (face->box == FACE_NO_BOX)
7041 {
7042 /* If the box comes from face properties in a
7043 display string, check faces in that string. */
7044 int string_face_id = face_after_it_pos (it);
7045 it->end_of_box_run_p
7046 = (FACE_FROM_ID (it->f, string_face_id)->box
7047 == FACE_NO_BOX);
7048 }
7049 /* Otherwise, the box comes from the underlying face.
7050 If this is the last string character displayed, check
7051 the next buffer location. */
7052 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7053 /* n_overlay_strings is unreliable unless
7054 overlay_string_index is non-negative. */
7055 && ((it->current.overlay_string_index >= 0
7056 && (it->current.overlay_string_index
7057 == it->n_overlay_strings - 1))
7058 /* A string from display property. */
7059 || it->from_disp_prop_p))
7060 {
7061 ptrdiff_t ignore;
7062 int next_face_id;
7063 struct text_pos pos = it->current.pos;
7064
7065 /* For a string from a display property, the next
7066 buffer position is stored in the 'position'
7067 member of the iteration stack slot below the
7068 current one, see handle_single_display_spec. By
7069 contrast, it->current.pos was is not yet updated
7070 to point to that buffer position; that will
7071 happen in pop_it, after we finish displaying the
7072 current string. Note that we already checked
7073 above that it->sp is positive, so subtracting one
7074 from it is safe. */
7075 if (it->from_disp_prop_p)
7076 pos = (it->stack + it->sp - 1)->position;
7077 else
7078 INC_TEXT_POS (pos, it->multibyte_p);
7079
7080 if (CHARPOS (pos) >= ZV)
7081 it->end_of_box_run_p = true;
7082 else
7083 {
7084 next_face_id = face_at_buffer_position
7085 (it->w, CHARPOS (pos), &ignore,
7086 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, false, -1);
7087 it->end_of_box_run_p
7088 = (FACE_FROM_ID (it->f, next_face_id)->box
7089 == FACE_NO_BOX);
7090 }
7091 }
7092 }
7093 }
7094 /* next_element_from_display_vector sets this flag according to
7095 faces of the display vector glyphs, see there. */
7096 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7097 {
7098 int face_id = face_after_it_pos (it);
7099 it->end_of_box_run_p
7100 = (face_id != it->face_id
7101 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7102 }
7103 }
7104 /* If we reached the end of the object we've been iterating (e.g., a
7105 display string or an overlay string), and there's something on
7106 IT->stack, proceed with what's on the stack. It doesn't make
7107 sense to return false if there's unprocessed stuff on the stack,
7108 because otherwise that stuff will never be displayed. */
7109 if (!success_p && it->sp > 0)
7110 {
7111 set_iterator_to_next (it, false);
7112 success_p = get_next_display_element (it);
7113 }
7114
7115 /* Value is false if end of buffer or string reached. */
7116 return success_p;
7117 }
7118
7119
7120 /* Move IT to the next display element.
7121
7122 RESEAT_P means if called on a newline in buffer text,
7123 skip to the next visible line start.
7124
7125 Functions get_next_display_element and set_iterator_to_next are
7126 separate because I find this arrangement easier to handle than a
7127 get_next_display_element function that also increments IT's
7128 position. The way it is we can first look at an iterator's current
7129 display element, decide whether it fits on a line, and if it does,
7130 increment the iterator position. The other way around we probably
7131 would either need a flag indicating whether the iterator has to be
7132 incremented the next time, or we would have to implement a
7133 decrement position function which would not be easy to write. */
7134
7135 void
7136 set_iterator_to_next (struct it *it, bool reseat_p)
7137 {
7138 /* Reset flags indicating start and end of a sequence of characters
7139 with box. Reset them at the start of this function because
7140 moving the iterator to a new position might set them. */
7141 it->start_of_box_run_p = it->end_of_box_run_p = false;
7142
7143 switch (it->method)
7144 {
7145 case GET_FROM_BUFFER:
7146 /* The current display element of IT is a character from
7147 current_buffer. Advance in the buffer, and maybe skip over
7148 invisible lines that are so because of selective display. */
7149 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7150 reseat_at_next_visible_line_start (it, false);
7151 else if (it->cmp_it.id >= 0)
7152 {
7153 /* We are currently getting glyphs from a composition. */
7154 if (! it->bidi_p)
7155 {
7156 IT_CHARPOS (*it) += it->cmp_it.nchars;
7157 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7158 }
7159 else
7160 {
7161 int i;
7162
7163 /* Update IT's char/byte positions to point to the first
7164 character of the next grapheme cluster, or to the
7165 character visually after the current composition. */
7166 for (i = 0; i < it->cmp_it.nchars; i++)
7167 bidi_move_to_visually_next (&it->bidi_it);
7168 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7169 IT_CHARPOS (*it) = it->bidi_it.charpos;
7170 }
7171
7172 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7173 && it->cmp_it.to < it->cmp_it.nglyphs)
7174 {
7175 /* Composition created while scanning forward. Proceed
7176 to the next grapheme cluster. */
7177 it->cmp_it.from = it->cmp_it.to;
7178 }
7179 else if ((it->bidi_p && it->cmp_it.reversed_p)
7180 && it->cmp_it.from > 0)
7181 {
7182 /* Composition created while scanning backward. Proceed
7183 to the previous grapheme cluster. */
7184 it->cmp_it.to = it->cmp_it.from;
7185 }
7186 else
7187 {
7188 /* No more grapheme clusters in this composition.
7189 Find the next stop position. */
7190 ptrdiff_t stop = it->end_charpos;
7191
7192 if (it->bidi_it.scan_dir < 0)
7193 /* Now we are scanning backward and don't know
7194 where to stop. */
7195 stop = -1;
7196 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7197 IT_BYTEPOS (*it), stop, Qnil);
7198 }
7199 }
7200 else
7201 {
7202 eassert (it->len != 0);
7203
7204 if (!it->bidi_p)
7205 {
7206 IT_BYTEPOS (*it) += it->len;
7207 IT_CHARPOS (*it) += 1;
7208 }
7209 else
7210 {
7211 int prev_scan_dir = it->bidi_it.scan_dir;
7212 /* If this is a new paragraph, determine its base
7213 direction (a.k.a. its base embedding level). */
7214 if (it->bidi_it.new_paragraph)
7215 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7216 false);
7217 bidi_move_to_visually_next (&it->bidi_it);
7218 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7219 IT_CHARPOS (*it) = it->bidi_it.charpos;
7220 if (prev_scan_dir != it->bidi_it.scan_dir)
7221 {
7222 /* As the scan direction was changed, we must
7223 re-compute the stop position for composition. */
7224 ptrdiff_t stop = it->end_charpos;
7225 if (it->bidi_it.scan_dir < 0)
7226 stop = -1;
7227 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7228 IT_BYTEPOS (*it), stop, Qnil);
7229 }
7230 }
7231 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7232 }
7233 break;
7234
7235 case GET_FROM_C_STRING:
7236 /* Current display element of IT is from a C string. */
7237 if (!it->bidi_p
7238 /* If the string position is beyond string's end, it means
7239 next_element_from_c_string is padding the string with
7240 blanks, in which case we bypass the bidi iterator,
7241 because it cannot deal with such virtual characters. */
7242 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7243 {
7244 IT_BYTEPOS (*it) += it->len;
7245 IT_CHARPOS (*it) += 1;
7246 }
7247 else
7248 {
7249 bidi_move_to_visually_next (&it->bidi_it);
7250 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7251 IT_CHARPOS (*it) = it->bidi_it.charpos;
7252 }
7253 break;
7254
7255 case GET_FROM_DISPLAY_VECTOR:
7256 /* Current display element of IT is from a display table entry.
7257 Advance in the display table definition. Reset it to null if
7258 end reached, and continue with characters from buffers/
7259 strings. */
7260 ++it->current.dpvec_index;
7261
7262 /* Restore face of the iterator to what they were before the
7263 display vector entry (these entries may contain faces). */
7264 it->face_id = it->saved_face_id;
7265
7266 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7267 {
7268 bool recheck_faces = it->ellipsis_p;
7269
7270 if (it->s)
7271 it->method = GET_FROM_C_STRING;
7272 else if (STRINGP (it->string))
7273 it->method = GET_FROM_STRING;
7274 else
7275 {
7276 it->method = GET_FROM_BUFFER;
7277 it->object = it->w->contents;
7278 }
7279
7280 it->dpvec = NULL;
7281 it->current.dpvec_index = -1;
7282
7283 /* Skip over characters which were displayed via IT->dpvec. */
7284 if (it->dpvec_char_len < 0)
7285 reseat_at_next_visible_line_start (it, true);
7286 else if (it->dpvec_char_len > 0)
7287 {
7288 it->len = it->dpvec_char_len;
7289 set_iterator_to_next (it, reseat_p);
7290 }
7291
7292 /* Maybe recheck faces after display vector. */
7293 if (recheck_faces)
7294 {
7295 if (it->method == GET_FROM_STRING)
7296 it->stop_charpos = IT_STRING_CHARPOS (*it);
7297 else
7298 it->stop_charpos = IT_CHARPOS (*it);
7299 }
7300 }
7301 break;
7302
7303 case GET_FROM_STRING:
7304 /* Current display element is a character from a Lisp string. */
7305 eassert (it->s == NULL && STRINGP (it->string));
7306 /* Don't advance past string end. These conditions are true
7307 when set_iterator_to_next is called at the end of
7308 get_next_display_element, in which case the Lisp string is
7309 already exhausted, and all we want is pop the iterator
7310 stack. */
7311 if (it->current.overlay_string_index >= 0)
7312 {
7313 /* This is an overlay string, so there's no padding with
7314 spaces, and the number of characters in the string is
7315 where the string ends. */
7316 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7317 goto consider_string_end;
7318 }
7319 else
7320 {
7321 /* Not an overlay string. There could be padding, so test
7322 against it->end_charpos. */
7323 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7324 goto consider_string_end;
7325 }
7326 if (it->cmp_it.id >= 0)
7327 {
7328 /* We are delivering display elements from a composition.
7329 Update the string position past the grapheme cluster
7330 we've just processed. */
7331 if (! it->bidi_p)
7332 {
7333 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7334 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7335 }
7336 else
7337 {
7338 int i;
7339
7340 for (i = 0; i < it->cmp_it.nchars; i++)
7341 bidi_move_to_visually_next (&it->bidi_it);
7342 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7343 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7344 }
7345
7346 /* Did we exhaust all the grapheme clusters of this
7347 composition? */
7348 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7349 && (it->cmp_it.to < it->cmp_it.nglyphs))
7350 {
7351 /* Not all the grapheme clusters were processed yet;
7352 advance to the next cluster. */
7353 it->cmp_it.from = it->cmp_it.to;
7354 }
7355 else if ((it->bidi_p && it->cmp_it.reversed_p)
7356 && it->cmp_it.from > 0)
7357 {
7358 /* Likewise: advance to the next cluster, but going in
7359 the reverse direction. */
7360 it->cmp_it.to = it->cmp_it.from;
7361 }
7362 else
7363 {
7364 /* This composition was fully processed; find the next
7365 candidate place for checking for composed
7366 characters. */
7367 /* Always limit string searches to the string length;
7368 any padding spaces are not part of the string, and
7369 there cannot be any compositions in that padding. */
7370 ptrdiff_t stop = SCHARS (it->string);
7371
7372 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7373 stop = -1;
7374 else if (it->end_charpos < stop)
7375 {
7376 /* Cf. PRECISION in reseat_to_string: we might be
7377 limited in how many of the string characters we
7378 need to deliver. */
7379 stop = it->end_charpos;
7380 }
7381 composition_compute_stop_pos (&it->cmp_it,
7382 IT_STRING_CHARPOS (*it),
7383 IT_STRING_BYTEPOS (*it), stop,
7384 it->string);
7385 }
7386 }
7387 else
7388 {
7389 if (!it->bidi_p
7390 /* If the string position is beyond string's end, it
7391 means next_element_from_string is padding the string
7392 with blanks, in which case we bypass the bidi
7393 iterator, because it cannot deal with such virtual
7394 characters. */
7395 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7396 {
7397 IT_STRING_BYTEPOS (*it) += it->len;
7398 IT_STRING_CHARPOS (*it) += 1;
7399 }
7400 else
7401 {
7402 int prev_scan_dir = it->bidi_it.scan_dir;
7403
7404 bidi_move_to_visually_next (&it->bidi_it);
7405 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7406 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7407 /* If the scan direction changes, we may need to update
7408 the place where to check for composed characters. */
7409 if (prev_scan_dir != it->bidi_it.scan_dir)
7410 {
7411 ptrdiff_t stop = SCHARS (it->string);
7412
7413 if (it->bidi_it.scan_dir < 0)
7414 stop = -1;
7415 else if (it->end_charpos < stop)
7416 stop = it->end_charpos;
7417
7418 composition_compute_stop_pos (&it->cmp_it,
7419 IT_STRING_CHARPOS (*it),
7420 IT_STRING_BYTEPOS (*it), stop,
7421 it->string);
7422 }
7423 }
7424 }
7425
7426 consider_string_end:
7427
7428 if (it->current.overlay_string_index >= 0)
7429 {
7430 /* IT->string is an overlay string. Advance to the
7431 next, if there is one. */
7432 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7433 {
7434 it->ellipsis_p = false;
7435 next_overlay_string (it);
7436 if (it->ellipsis_p)
7437 setup_for_ellipsis (it, 0);
7438 }
7439 }
7440 else
7441 {
7442 /* IT->string is not an overlay string. If we reached
7443 its end, and there is something on IT->stack, proceed
7444 with what is on the stack. This can be either another
7445 string, this time an overlay string, or a buffer. */
7446 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7447 && it->sp > 0)
7448 {
7449 pop_it (it);
7450 if (it->method == GET_FROM_STRING)
7451 goto consider_string_end;
7452 }
7453 }
7454 break;
7455
7456 case GET_FROM_IMAGE:
7457 case GET_FROM_STRETCH:
7458 /* The position etc with which we have to proceed are on
7459 the stack. The position may be at the end of a string,
7460 if the `display' property takes up the whole string. */
7461 eassert (it->sp > 0);
7462 pop_it (it);
7463 if (it->method == GET_FROM_STRING)
7464 goto consider_string_end;
7465 break;
7466
7467 default:
7468 /* There are no other methods defined, so this should be a bug. */
7469 emacs_abort ();
7470 }
7471
7472 eassert (it->method != GET_FROM_STRING
7473 || (STRINGP (it->string)
7474 && IT_STRING_CHARPOS (*it) >= 0));
7475 }
7476
7477 /* Load IT's display element fields with information about the next
7478 display element which comes from a display table entry or from the
7479 result of translating a control character to one of the forms `^C'
7480 or `\003'.
7481
7482 IT->dpvec holds the glyphs to return as characters.
7483 IT->saved_face_id holds the face id before the display vector--it
7484 is restored into IT->face_id in set_iterator_to_next. */
7485
7486 static bool
7487 next_element_from_display_vector (struct it *it)
7488 {
7489 Lisp_Object gc;
7490 int prev_face_id = it->face_id;
7491 int next_face_id;
7492
7493 /* Precondition. */
7494 eassert (it->dpvec && it->current.dpvec_index >= 0);
7495
7496 it->face_id = it->saved_face_id;
7497
7498 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7499 That seemed totally bogus - so I changed it... */
7500 gc = it->dpvec[it->current.dpvec_index];
7501
7502 if (GLYPH_CODE_P (gc))
7503 {
7504 struct face *this_face, *prev_face, *next_face;
7505
7506 it->c = GLYPH_CODE_CHAR (gc);
7507 it->len = CHAR_BYTES (it->c);
7508
7509 /* The entry may contain a face id to use. Such a face id is
7510 the id of a Lisp face, not a realized face. A face id of
7511 zero means no face is specified. */
7512 if (it->dpvec_face_id >= 0)
7513 it->face_id = it->dpvec_face_id;
7514 else
7515 {
7516 int lface_id = GLYPH_CODE_FACE (gc);
7517 if (lface_id > 0)
7518 it->face_id = merge_faces (it->f, Qt, lface_id,
7519 it->saved_face_id);
7520 }
7521
7522 /* Glyphs in the display vector could have the box face, so we
7523 need to set the related flags in the iterator, as
7524 appropriate. */
7525 this_face = FACE_FROM_ID (it->f, it->face_id);
7526 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7527
7528 /* Is this character the first character of a box-face run? */
7529 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7530 && (!prev_face
7531 || prev_face->box == FACE_NO_BOX));
7532
7533 /* For the last character of the box-face run, we need to look
7534 either at the next glyph from the display vector, or at the
7535 face we saw before the display vector. */
7536 next_face_id = it->saved_face_id;
7537 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7538 {
7539 if (it->dpvec_face_id >= 0)
7540 next_face_id = it->dpvec_face_id;
7541 else
7542 {
7543 int lface_id =
7544 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7545
7546 if (lface_id > 0)
7547 next_face_id = merge_faces (it->f, Qt, lface_id,
7548 it->saved_face_id);
7549 }
7550 }
7551 next_face = FACE_FROM_ID (it->f, next_face_id);
7552 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7553 && (!next_face
7554 || next_face->box == FACE_NO_BOX));
7555 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7556 }
7557 else
7558 /* Display table entry is invalid. Return a space. */
7559 it->c = ' ', it->len = 1;
7560
7561 /* Don't change position and object of the iterator here. They are
7562 still the values of the character that had this display table
7563 entry or was translated, and that's what we want. */
7564 it->what = IT_CHARACTER;
7565 return true;
7566 }
7567
7568 /* Get the first element of string/buffer in the visual order, after
7569 being reseated to a new position in a string or a buffer. */
7570 static void
7571 get_visually_first_element (struct it *it)
7572 {
7573 bool string_p = STRINGP (it->string) || it->s;
7574 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7575 ptrdiff_t bob = (string_p ? 0 : BEGV);
7576
7577 if (STRINGP (it->string))
7578 {
7579 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7580 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7581 }
7582 else
7583 {
7584 it->bidi_it.charpos = IT_CHARPOS (*it);
7585 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7586 }
7587
7588 if (it->bidi_it.charpos == eob)
7589 {
7590 /* Nothing to do, but reset the FIRST_ELT flag, like
7591 bidi_paragraph_init does, because we are not going to
7592 call it. */
7593 it->bidi_it.first_elt = false;
7594 }
7595 else if (it->bidi_it.charpos == bob
7596 || (!string_p
7597 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7598 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7599 {
7600 /* If we are at the beginning of a line/string, we can produce
7601 the next element right away. */
7602 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7603 bidi_move_to_visually_next (&it->bidi_it);
7604 }
7605 else
7606 {
7607 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7608
7609 /* We need to prime the bidi iterator starting at the line's or
7610 string's beginning, before we will be able to produce the
7611 next element. */
7612 if (string_p)
7613 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7614 else
7615 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7616 IT_BYTEPOS (*it), -1,
7617 &it->bidi_it.bytepos);
7618 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7619 do
7620 {
7621 /* Now return to buffer/string position where we were asked
7622 to get the next display element, and produce that. */
7623 bidi_move_to_visually_next (&it->bidi_it);
7624 }
7625 while (it->bidi_it.bytepos != orig_bytepos
7626 && it->bidi_it.charpos < eob);
7627 }
7628
7629 /* Adjust IT's position information to where we ended up. */
7630 if (STRINGP (it->string))
7631 {
7632 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7633 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7634 }
7635 else
7636 {
7637 IT_CHARPOS (*it) = it->bidi_it.charpos;
7638 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7639 }
7640
7641 if (STRINGP (it->string) || !it->s)
7642 {
7643 ptrdiff_t stop, charpos, bytepos;
7644
7645 if (STRINGP (it->string))
7646 {
7647 eassert (!it->s);
7648 stop = SCHARS (it->string);
7649 if (stop > it->end_charpos)
7650 stop = it->end_charpos;
7651 charpos = IT_STRING_CHARPOS (*it);
7652 bytepos = IT_STRING_BYTEPOS (*it);
7653 }
7654 else
7655 {
7656 stop = it->end_charpos;
7657 charpos = IT_CHARPOS (*it);
7658 bytepos = IT_BYTEPOS (*it);
7659 }
7660 if (it->bidi_it.scan_dir < 0)
7661 stop = -1;
7662 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7663 it->string);
7664 }
7665 }
7666
7667 /* Load IT with the next display element from Lisp string IT->string.
7668 IT->current.string_pos is the current position within the string.
7669 If IT->current.overlay_string_index >= 0, the Lisp string is an
7670 overlay string. */
7671
7672 static bool
7673 next_element_from_string (struct it *it)
7674 {
7675 struct text_pos position;
7676
7677 eassert (STRINGP (it->string));
7678 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7679 eassert (IT_STRING_CHARPOS (*it) >= 0);
7680 position = it->current.string_pos;
7681
7682 /* With bidi reordering, the character to display might not be the
7683 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7684 that we were reseat()ed to a new string, whose paragraph
7685 direction is not known. */
7686 if (it->bidi_p && it->bidi_it.first_elt)
7687 {
7688 get_visually_first_element (it);
7689 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7690 }
7691
7692 /* Time to check for invisible text? */
7693 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7694 {
7695 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7696 {
7697 if (!(!it->bidi_p
7698 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7699 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7700 {
7701 /* With bidi non-linear iteration, we could find
7702 ourselves far beyond the last computed stop_charpos,
7703 with several other stop positions in between that we
7704 missed. Scan them all now, in buffer's logical
7705 order, until we find and handle the last stop_charpos
7706 that precedes our current position. */
7707 handle_stop_backwards (it, it->stop_charpos);
7708 return GET_NEXT_DISPLAY_ELEMENT (it);
7709 }
7710 else
7711 {
7712 if (it->bidi_p)
7713 {
7714 /* Take note of the stop position we just moved
7715 across, for when we will move back across it. */
7716 it->prev_stop = it->stop_charpos;
7717 /* If we are at base paragraph embedding level, take
7718 note of the last stop position seen at this
7719 level. */
7720 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7721 it->base_level_stop = it->stop_charpos;
7722 }
7723 handle_stop (it);
7724
7725 /* Since a handler may have changed IT->method, we must
7726 recurse here. */
7727 return GET_NEXT_DISPLAY_ELEMENT (it);
7728 }
7729 }
7730 else if (it->bidi_p
7731 /* If we are before prev_stop, we may have overstepped
7732 on our way backwards a stop_pos, and if so, we need
7733 to handle that stop_pos. */
7734 && IT_STRING_CHARPOS (*it) < it->prev_stop
7735 /* We can sometimes back up for reasons that have nothing
7736 to do with bidi reordering. E.g., compositions. The
7737 code below is only needed when we are above the base
7738 embedding level, so test for that explicitly. */
7739 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7740 {
7741 /* If we lost track of base_level_stop, we have no better
7742 place for handle_stop_backwards to start from than string
7743 beginning. This happens, e.g., when we were reseated to
7744 the previous screenful of text by vertical-motion. */
7745 if (it->base_level_stop <= 0
7746 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7747 it->base_level_stop = 0;
7748 handle_stop_backwards (it, it->base_level_stop);
7749 return GET_NEXT_DISPLAY_ELEMENT (it);
7750 }
7751 }
7752
7753 if (it->current.overlay_string_index >= 0)
7754 {
7755 /* Get the next character from an overlay string. In overlay
7756 strings, there is no field width or padding with spaces to
7757 do. */
7758 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7759 {
7760 it->what = IT_EOB;
7761 return false;
7762 }
7763 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7764 IT_STRING_BYTEPOS (*it),
7765 it->bidi_it.scan_dir < 0
7766 ? -1
7767 : SCHARS (it->string))
7768 && next_element_from_composition (it))
7769 {
7770 return true;
7771 }
7772 else if (STRING_MULTIBYTE (it->string))
7773 {
7774 const unsigned char *s = (SDATA (it->string)
7775 + IT_STRING_BYTEPOS (*it));
7776 it->c = string_char_and_length (s, &it->len);
7777 }
7778 else
7779 {
7780 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7781 it->len = 1;
7782 }
7783 }
7784 else
7785 {
7786 /* Get the next character from a Lisp string that is not an
7787 overlay string. Such strings come from the mode line, for
7788 example. We may have to pad with spaces, or truncate the
7789 string. See also next_element_from_c_string. */
7790 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7791 {
7792 it->what = IT_EOB;
7793 return false;
7794 }
7795 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7796 {
7797 /* Pad with spaces. */
7798 it->c = ' ', it->len = 1;
7799 CHARPOS (position) = BYTEPOS (position) = -1;
7800 }
7801 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7802 IT_STRING_BYTEPOS (*it),
7803 it->bidi_it.scan_dir < 0
7804 ? -1
7805 : it->string_nchars)
7806 && next_element_from_composition (it))
7807 {
7808 return true;
7809 }
7810 else if (STRING_MULTIBYTE (it->string))
7811 {
7812 const unsigned char *s = (SDATA (it->string)
7813 + IT_STRING_BYTEPOS (*it));
7814 it->c = string_char_and_length (s, &it->len);
7815 }
7816 else
7817 {
7818 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7819 it->len = 1;
7820 }
7821 }
7822
7823 /* Record what we have and where it came from. */
7824 it->what = IT_CHARACTER;
7825 it->object = it->string;
7826 it->position = position;
7827 return true;
7828 }
7829
7830
7831 /* Load IT with next display element from C string IT->s.
7832 IT->string_nchars is the maximum number of characters to return
7833 from the string. IT->end_charpos may be greater than
7834 IT->string_nchars when this function is called, in which case we
7835 may have to return padding spaces. Value is false if end of string
7836 reached, including padding spaces. */
7837
7838 static bool
7839 next_element_from_c_string (struct it *it)
7840 {
7841 bool success_p = true;
7842
7843 eassert (it->s);
7844 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7845 it->what = IT_CHARACTER;
7846 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7847 it->object = make_number (0);
7848
7849 /* With bidi reordering, the character to display might not be the
7850 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
7851 we were reseated to a new string, whose paragraph direction is
7852 not known. */
7853 if (it->bidi_p && it->bidi_it.first_elt)
7854 get_visually_first_element (it);
7855
7856 /* IT's position can be greater than IT->string_nchars in case a
7857 field width or precision has been specified when the iterator was
7858 initialized. */
7859 if (IT_CHARPOS (*it) >= it->end_charpos)
7860 {
7861 /* End of the game. */
7862 it->what = IT_EOB;
7863 success_p = false;
7864 }
7865 else if (IT_CHARPOS (*it) >= it->string_nchars)
7866 {
7867 /* Pad with spaces. */
7868 it->c = ' ', it->len = 1;
7869 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7870 }
7871 else if (it->multibyte_p)
7872 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7873 else
7874 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7875
7876 return success_p;
7877 }
7878
7879
7880 /* Set up IT to return characters from an ellipsis, if appropriate.
7881 The definition of the ellipsis glyphs may come from a display table
7882 entry. This function fills IT with the first glyph from the
7883 ellipsis if an ellipsis is to be displayed. */
7884
7885 static bool
7886 next_element_from_ellipsis (struct it *it)
7887 {
7888 if (it->selective_display_ellipsis_p)
7889 setup_for_ellipsis (it, it->len);
7890 else
7891 {
7892 /* The face at the current position may be different from the
7893 face we find after the invisible text. Remember what it
7894 was in IT->saved_face_id, and signal that it's there by
7895 setting face_before_selective_p. */
7896 it->saved_face_id = it->face_id;
7897 it->method = GET_FROM_BUFFER;
7898 it->object = it->w->contents;
7899 reseat_at_next_visible_line_start (it, true);
7900 it->face_before_selective_p = true;
7901 }
7902
7903 return GET_NEXT_DISPLAY_ELEMENT (it);
7904 }
7905
7906
7907 /* Deliver an image display element. The iterator IT is already
7908 filled with image information (done in handle_display_prop). Value
7909 is always true. */
7910
7911
7912 static bool
7913 next_element_from_image (struct it *it)
7914 {
7915 it->what = IT_IMAGE;
7916 return true;
7917 }
7918
7919
7920 /* Fill iterator IT with next display element from a stretch glyph
7921 property. IT->object is the value of the text property. Value is
7922 always true. */
7923
7924 static bool
7925 next_element_from_stretch (struct it *it)
7926 {
7927 it->what = IT_STRETCH;
7928 return true;
7929 }
7930
7931 /* Scan backwards from IT's current position until we find a stop
7932 position, or until BEGV. This is called when we find ourself
7933 before both the last known prev_stop and base_level_stop while
7934 reordering bidirectional text. */
7935
7936 static void
7937 compute_stop_pos_backwards (struct it *it)
7938 {
7939 const int SCAN_BACK_LIMIT = 1000;
7940 struct text_pos pos;
7941 struct display_pos save_current = it->current;
7942 struct text_pos save_position = it->position;
7943 ptrdiff_t charpos = IT_CHARPOS (*it);
7944 ptrdiff_t where_we_are = charpos;
7945 ptrdiff_t save_stop_pos = it->stop_charpos;
7946 ptrdiff_t save_end_pos = it->end_charpos;
7947
7948 eassert (NILP (it->string) && !it->s);
7949 eassert (it->bidi_p);
7950 it->bidi_p = false;
7951 do
7952 {
7953 it->end_charpos = min (charpos + 1, ZV);
7954 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7955 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7956 reseat_1 (it, pos, false);
7957 compute_stop_pos (it);
7958 /* We must advance forward, right? */
7959 if (it->stop_charpos <= charpos)
7960 emacs_abort ();
7961 }
7962 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7963
7964 if (it->stop_charpos <= where_we_are)
7965 it->prev_stop = it->stop_charpos;
7966 else
7967 it->prev_stop = BEGV;
7968 it->bidi_p = true;
7969 it->current = save_current;
7970 it->position = save_position;
7971 it->stop_charpos = save_stop_pos;
7972 it->end_charpos = save_end_pos;
7973 }
7974
7975 /* Scan forward from CHARPOS in the current buffer/string, until we
7976 find a stop position > current IT's position. Then handle the stop
7977 position before that. This is called when we bump into a stop
7978 position while reordering bidirectional text. CHARPOS should be
7979 the last previously processed stop_pos (or BEGV/0, if none were
7980 processed yet) whose position is less that IT's current
7981 position. */
7982
7983 static void
7984 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7985 {
7986 bool bufp = !STRINGP (it->string);
7987 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7988 struct display_pos save_current = it->current;
7989 struct text_pos save_position = it->position;
7990 struct text_pos pos1;
7991 ptrdiff_t next_stop;
7992
7993 /* Scan in strict logical order. */
7994 eassert (it->bidi_p);
7995 it->bidi_p = false;
7996 do
7997 {
7998 it->prev_stop = charpos;
7999 if (bufp)
8000 {
8001 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8002 reseat_1 (it, pos1, false);
8003 }
8004 else
8005 it->current.string_pos = string_pos (charpos, it->string);
8006 compute_stop_pos (it);
8007 /* We must advance forward, right? */
8008 if (it->stop_charpos <= it->prev_stop)
8009 emacs_abort ();
8010 charpos = it->stop_charpos;
8011 }
8012 while (charpos <= where_we_are);
8013
8014 it->bidi_p = true;
8015 it->current = save_current;
8016 it->position = save_position;
8017 next_stop = it->stop_charpos;
8018 it->stop_charpos = it->prev_stop;
8019 handle_stop (it);
8020 it->stop_charpos = next_stop;
8021 }
8022
8023 /* Load IT with the next display element from current_buffer. Value
8024 is false if end of buffer reached. IT->stop_charpos is the next
8025 position at which to stop and check for text properties or buffer
8026 end. */
8027
8028 static bool
8029 next_element_from_buffer (struct it *it)
8030 {
8031 bool success_p = true;
8032
8033 eassert (IT_CHARPOS (*it) >= BEGV);
8034 eassert (NILP (it->string) && !it->s);
8035 eassert (!it->bidi_p
8036 || (EQ (it->bidi_it.string.lstring, Qnil)
8037 && it->bidi_it.string.s == NULL));
8038
8039 /* With bidi reordering, the character to display might not be the
8040 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8041 we were reseat()ed to a new buffer position, which is potentially
8042 a different paragraph. */
8043 if (it->bidi_p && it->bidi_it.first_elt)
8044 {
8045 get_visually_first_element (it);
8046 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8047 }
8048
8049 if (IT_CHARPOS (*it) >= it->stop_charpos)
8050 {
8051 if (IT_CHARPOS (*it) >= it->end_charpos)
8052 {
8053 bool overlay_strings_follow_p;
8054
8055 /* End of the game, except when overlay strings follow that
8056 haven't been returned yet. */
8057 if (it->overlay_strings_at_end_processed_p)
8058 overlay_strings_follow_p = false;
8059 else
8060 {
8061 it->overlay_strings_at_end_processed_p = true;
8062 overlay_strings_follow_p = get_overlay_strings (it, 0);
8063 }
8064
8065 if (overlay_strings_follow_p)
8066 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8067 else
8068 {
8069 it->what = IT_EOB;
8070 it->position = it->current.pos;
8071 success_p = false;
8072 }
8073 }
8074 else if (!(!it->bidi_p
8075 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8076 || IT_CHARPOS (*it) == it->stop_charpos))
8077 {
8078 /* With bidi non-linear iteration, we could find ourselves
8079 far beyond the last computed stop_charpos, with several
8080 other stop positions in between that we missed. Scan
8081 them all now, in buffer's logical order, until we find
8082 and handle the last stop_charpos that precedes our
8083 current position. */
8084 handle_stop_backwards (it, it->stop_charpos);
8085 it->ignore_overlay_strings_at_pos_p = false;
8086 return GET_NEXT_DISPLAY_ELEMENT (it);
8087 }
8088 else
8089 {
8090 if (it->bidi_p)
8091 {
8092 /* Take note of the stop position we just moved across,
8093 for when we will move back across it. */
8094 it->prev_stop = it->stop_charpos;
8095 /* If we are at base paragraph embedding level, take
8096 note of the last stop position seen at this
8097 level. */
8098 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8099 it->base_level_stop = it->stop_charpos;
8100 }
8101 handle_stop (it);
8102 it->ignore_overlay_strings_at_pos_p = false;
8103 return GET_NEXT_DISPLAY_ELEMENT (it);
8104 }
8105 }
8106 else if (it->bidi_p
8107 /* If we are before prev_stop, we may have overstepped on
8108 our way backwards a stop_pos, and if so, we need to
8109 handle that stop_pos. */
8110 && IT_CHARPOS (*it) < it->prev_stop
8111 /* We can sometimes back up for reasons that have nothing
8112 to do with bidi reordering. E.g., compositions. The
8113 code below is only needed when we are above the base
8114 embedding level, so test for that explicitly. */
8115 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8116 {
8117 if (it->base_level_stop <= 0
8118 || IT_CHARPOS (*it) < it->base_level_stop)
8119 {
8120 /* If we lost track of base_level_stop, we need to find
8121 prev_stop by looking backwards. This happens, e.g., when
8122 we were reseated to the previous screenful of text by
8123 vertical-motion. */
8124 it->base_level_stop = BEGV;
8125 compute_stop_pos_backwards (it);
8126 handle_stop_backwards (it, it->prev_stop);
8127 }
8128 else
8129 handle_stop_backwards (it, it->base_level_stop);
8130 it->ignore_overlay_strings_at_pos_p = false;
8131 return GET_NEXT_DISPLAY_ELEMENT (it);
8132 }
8133 else
8134 {
8135 /* No face changes, overlays etc. in sight, so just return a
8136 character from current_buffer. */
8137 unsigned char *p;
8138 ptrdiff_t stop;
8139
8140 /* We moved to the next buffer position, so any info about
8141 previously seen overlays is no longer valid. */
8142 it->ignore_overlay_strings_at_pos_p = false;
8143
8144 /* Maybe run the redisplay end trigger hook. Performance note:
8145 This doesn't seem to cost measurable time. */
8146 if (it->redisplay_end_trigger_charpos
8147 && it->glyph_row
8148 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8149 run_redisplay_end_trigger_hook (it);
8150
8151 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8152 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8153 stop)
8154 && next_element_from_composition (it))
8155 {
8156 return true;
8157 }
8158
8159 /* Get the next character, maybe multibyte. */
8160 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8161 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8162 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8163 else
8164 it->c = *p, it->len = 1;
8165
8166 /* Record what we have and where it came from. */
8167 it->what = IT_CHARACTER;
8168 it->object = it->w->contents;
8169 it->position = it->current.pos;
8170
8171 /* Normally we return the character found above, except when we
8172 really want to return an ellipsis for selective display. */
8173 if (it->selective)
8174 {
8175 if (it->c == '\n')
8176 {
8177 /* A value of selective > 0 means hide lines indented more
8178 than that number of columns. */
8179 if (it->selective > 0
8180 && IT_CHARPOS (*it) + 1 < ZV
8181 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8182 IT_BYTEPOS (*it) + 1,
8183 it->selective))
8184 {
8185 success_p = next_element_from_ellipsis (it);
8186 it->dpvec_char_len = -1;
8187 }
8188 }
8189 else if (it->c == '\r' && it->selective == -1)
8190 {
8191 /* A value of selective == -1 means that everything from the
8192 CR to the end of the line is invisible, with maybe an
8193 ellipsis displayed for it. */
8194 success_p = next_element_from_ellipsis (it);
8195 it->dpvec_char_len = -1;
8196 }
8197 }
8198 }
8199
8200 /* Value is false if end of buffer reached. */
8201 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8202 return success_p;
8203 }
8204
8205
8206 /* Run the redisplay end trigger hook for IT. */
8207
8208 static void
8209 run_redisplay_end_trigger_hook (struct it *it)
8210 {
8211 /* IT->glyph_row should be non-null, i.e. we should be actually
8212 displaying something, or otherwise we should not run the hook. */
8213 eassert (it->glyph_row);
8214
8215 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8216 it->redisplay_end_trigger_charpos = 0;
8217
8218 /* Since we are *trying* to run these functions, don't try to run
8219 them again, even if they get an error. */
8220 wset_redisplay_end_trigger (it->w, Qnil);
8221 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8222 make_number (charpos));
8223
8224 /* Notice if it changed the face of the character we are on. */
8225 handle_face_prop (it);
8226 }
8227
8228
8229 /* Deliver a composition display element. Unlike the other
8230 next_element_from_XXX, this function is not registered in the array
8231 get_next_element[]. It is called from next_element_from_buffer and
8232 next_element_from_string when necessary. */
8233
8234 static bool
8235 next_element_from_composition (struct it *it)
8236 {
8237 it->what = IT_COMPOSITION;
8238 it->len = it->cmp_it.nbytes;
8239 if (STRINGP (it->string))
8240 {
8241 if (it->c < 0)
8242 {
8243 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8244 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8245 return false;
8246 }
8247 it->position = it->current.string_pos;
8248 it->object = it->string;
8249 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8250 IT_STRING_BYTEPOS (*it), it->string);
8251 }
8252 else
8253 {
8254 if (it->c < 0)
8255 {
8256 IT_CHARPOS (*it) += it->cmp_it.nchars;
8257 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8258 if (it->bidi_p)
8259 {
8260 if (it->bidi_it.new_paragraph)
8261 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8262 false);
8263 /* Resync the bidi iterator with IT's new position.
8264 FIXME: this doesn't support bidirectional text. */
8265 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8266 bidi_move_to_visually_next (&it->bidi_it);
8267 }
8268 return false;
8269 }
8270 it->position = it->current.pos;
8271 it->object = it->w->contents;
8272 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8273 IT_BYTEPOS (*it), Qnil);
8274 }
8275 return true;
8276 }
8277
8278
8279 \f
8280 /***********************************************************************
8281 Moving an iterator without producing glyphs
8282 ***********************************************************************/
8283
8284 /* Check if iterator is at a position corresponding to a valid buffer
8285 position after some move_it_ call. */
8286
8287 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8288 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8289
8290
8291 /* Move iterator IT to a specified buffer or X position within one
8292 line on the display without producing glyphs.
8293
8294 OP should be a bit mask including some or all of these bits:
8295 MOVE_TO_X: Stop upon reaching x-position TO_X.
8296 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8297 Regardless of OP's value, stop upon reaching the end of the display line.
8298
8299 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8300 This means, in particular, that TO_X includes window's horizontal
8301 scroll amount.
8302
8303 The return value has several possible values that
8304 say what condition caused the scan to stop:
8305
8306 MOVE_POS_MATCH_OR_ZV
8307 - when TO_POS or ZV was reached.
8308
8309 MOVE_X_REACHED
8310 -when TO_X was reached before TO_POS or ZV were reached.
8311
8312 MOVE_LINE_CONTINUED
8313 - when we reached the end of the display area and the line must
8314 be continued.
8315
8316 MOVE_LINE_TRUNCATED
8317 - when we reached the end of the display area and the line is
8318 truncated.
8319
8320 MOVE_NEWLINE_OR_CR
8321 - when we stopped at a line end, i.e. a newline or a CR and selective
8322 display is on. */
8323
8324 static enum move_it_result
8325 move_it_in_display_line_to (struct it *it,
8326 ptrdiff_t to_charpos, int to_x,
8327 enum move_operation_enum op)
8328 {
8329 enum move_it_result result = MOVE_UNDEFINED;
8330 struct glyph_row *saved_glyph_row;
8331 struct it wrap_it, atpos_it, atx_it, ppos_it;
8332 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8333 void *ppos_data = NULL;
8334 bool may_wrap = false;
8335 enum it_method prev_method = it->method;
8336 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8337 bool saw_smaller_pos = prev_pos < to_charpos;
8338
8339 /* Don't produce glyphs in produce_glyphs. */
8340 saved_glyph_row = it->glyph_row;
8341 it->glyph_row = NULL;
8342
8343 /* Use wrap_it to save a copy of IT wherever a word wrap could
8344 occur. Use atpos_it to save a copy of IT at the desired buffer
8345 position, if found, so that we can scan ahead and check if the
8346 word later overshoots the window edge. Use atx_it similarly, for
8347 pixel positions. */
8348 wrap_it.sp = -1;
8349 atpos_it.sp = -1;
8350 atx_it.sp = -1;
8351
8352 /* Use ppos_it under bidi reordering to save a copy of IT for the
8353 initial position. We restore that position in IT when we have
8354 scanned the entire display line without finding a match for
8355 TO_CHARPOS and all the character positions are greater than
8356 TO_CHARPOS. We then restart the scan from the initial position,
8357 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8358 the closest to TO_CHARPOS. */
8359 if (it->bidi_p)
8360 {
8361 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8362 {
8363 SAVE_IT (ppos_it, *it, ppos_data);
8364 closest_pos = IT_CHARPOS (*it);
8365 }
8366 else
8367 closest_pos = ZV;
8368 }
8369
8370 #define BUFFER_POS_REACHED_P() \
8371 ((op & MOVE_TO_POS) != 0 \
8372 && BUFFERP (it->object) \
8373 && (IT_CHARPOS (*it) == to_charpos \
8374 || ((!it->bidi_p \
8375 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8376 && IT_CHARPOS (*it) > to_charpos) \
8377 || (it->what == IT_COMPOSITION \
8378 && ((IT_CHARPOS (*it) > to_charpos \
8379 && to_charpos >= it->cmp_it.charpos) \
8380 || (IT_CHARPOS (*it) < to_charpos \
8381 && to_charpos <= it->cmp_it.charpos)))) \
8382 && (it->method == GET_FROM_BUFFER \
8383 || (it->method == GET_FROM_DISPLAY_VECTOR \
8384 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8385
8386 /* If there's a line-/wrap-prefix, handle it. */
8387 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8388 && it->current_y < it->last_visible_y)
8389 handle_line_prefix (it);
8390
8391 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8392 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8393
8394 while (true)
8395 {
8396 int x, i, ascent = 0, descent = 0;
8397
8398 /* Utility macro to reset an iterator with x, ascent, and descent. */
8399 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8400 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8401 (IT)->max_descent = descent)
8402
8403 /* Stop if we move beyond TO_CHARPOS (after an image or a
8404 display string or stretch glyph). */
8405 if ((op & MOVE_TO_POS) != 0
8406 && BUFFERP (it->object)
8407 && it->method == GET_FROM_BUFFER
8408 && (((!it->bidi_p
8409 /* When the iterator is at base embedding level, we
8410 are guaranteed that characters are delivered for
8411 display in strictly increasing order of their
8412 buffer positions. */
8413 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8414 && IT_CHARPOS (*it) > to_charpos)
8415 || (it->bidi_p
8416 && (prev_method == GET_FROM_IMAGE
8417 || prev_method == GET_FROM_STRETCH
8418 || prev_method == GET_FROM_STRING)
8419 /* Passed TO_CHARPOS from left to right. */
8420 && ((prev_pos < to_charpos
8421 && IT_CHARPOS (*it) > to_charpos)
8422 /* Passed TO_CHARPOS from right to left. */
8423 || (prev_pos > to_charpos
8424 && IT_CHARPOS (*it) < to_charpos)))))
8425 {
8426 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8427 {
8428 result = MOVE_POS_MATCH_OR_ZV;
8429 break;
8430 }
8431 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8432 /* If wrap_it is valid, the current position might be in a
8433 word that is wrapped. So, save the iterator in
8434 atpos_it and continue to see if wrapping happens. */
8435 SAVE_IT (atpos_it, *it, atpos_data);
8436 }
8437
8438 /* Stop when ZV reached.
8439 We used to stop here when TO_CHARPOS reached as well, but that is
8440 too soon if this glyph does not fit on this line. So we handle it
8441 explicitly below. */
8442 if (!get_next_display_element (it))
8443 {
8444 result = MOVE_POS_MATCH_OR_ZV;
8445 break;
8446 }
8447
8448 if (it->line_wrap == TRUNCATE)
8449 {
8450 if (BUFFER_POS_REACHED_P ())
8451 {
8452 result = MOVE_POS_MATCH_OR_ZV;
8453 break;
8454 }
8455 }
8456 else
8457 {
8458 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8459 {
8460 if (IT_DISPLAYING_WHITESPACE (it))
8461 may_wrap = true;
8462 else if (may_wrap)
8463 {
8464 /* We have reached a glyph that follows one or more
8465 whitespace characters. If the position is
8466 already found, we are done. */
8467 if (atpos_it.sp >= 0)
8468 {
8469 RESTORE_IT (it, &atpos_it, atpos_data);
8470 result = MOVE_POS_MATCH_OR_ZV;
8471 goto done;
8472 }
8473 if (atx_it.sp >= 0)
8474 {
8475 RESTORE_IT (it, &atx_it, atx_data);
8476 result = MOVE_X_REACHED;
8477 goto done;
8478 }
8479 /* Otherwise, we can wrap here. */
8480 SAVE_IT (wrap_it, *it, wrap_data);
8481 may_wrap = false;
8482 }
8483 }
8484 }
8485
8486 /* Remember the line height for the current line, in case
8487 the next element doesn't fit on the line. */
8488 ascent = it->max_ascent;
8489 descent = it->max_descent;
8490
8491 /* The call to produce_glyphs will get the metrics of the
8492 display element IT is loaded with. Record the x-position
8493 before this display element, in case it doesn't fit on the
8494 line. */
8495 x = it->current_x;
8496
8497 PRODUCE_GLYPHS (it);
8498
8499 if (it->area != TEXT_AREA)
8500 {
8501 prev_method = it->method;
8502 if (it->method == GET_FROM_BUFFER)
8503 prev_pos = IT_CHARPOS (*it);
8504 set_iterator_to_next (it, true);
8505 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8506 SET_TEXT_POS (this_line_min_pos,
8507 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8508 if (it->bidi_p
8509 && (op & MOVE_TO_POS)
8510 && IT_CHARPOS (*it) > to_charpos
8511 && IT_CHARPOS (*it) < closest_pos)
8512 closest_pos = IT_CHARPOS (*it);
8513 continue;
8514 }
8515
8516 /* The number of glyphs we get back in IT->nglyphs will normally
8517 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8518 character on a terminal frame, or (iii) a line end. For the
8519 second case, IT->nglyphs - 1 padding glyphs will be present.
8520 (On X frames, there is only one glyph produced for a
8521 composite character.)
8522
8523 The behavior implemented below means, for continuation lines,
8524 that as many spaces of a TAB as fit on the current line are
8525 displayed there. For terminal frames, as many glyphs of a
8526 multi-glyph character are displayed in the current line, too.
8527 This is what the old redisplay code did, and we keep it that
8528 way. Under X, the whole shape of a complex character must
8529 fit on the line or it will be completely displayed in the
8530 next line.
8531
8532 Note that both for tabs and padding glyphs, all glyphs have
8533 the same width. */
8534 if (it->nglyphs)
8535 {
8536 /* More than one glyph or glyph doesn't fit on line. All
8537 glyphs have the same width. */
8538 int single_glyph_width = it->pixel_width / it->nglyphs;
8539 int new_x;
8540 int x_before_this_char = x;
8541 int hpos_before_this_char = it->hpos;
8542
8543 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8544 {
8545 new_x = x + single_glyph_width;
8546
8547 /* We want to leave anything reaching TO_X to the caller. */
8548 if ((op & MOVE_TO_X) && new_x > to_x)
8549 {
8550 if (BUFFER_POS_REACHED_P ())
8551 {
8552 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8553 goto buffer_pos_reached;
8554 if (atpos_it.sp < 0)
8555 {
8556 SAVE_IT (atpos_it, *it, atpos_data);
8557 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8558 }
8559 }
8560 else
8561 {
8562 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8563 {
8564 it->current_x = x;
8565 result = MOVE_X_REACHED;
8566 break;
8567 }
8568 if (atx_it.sp < 0)
8569 {
8570 SAVE_IT (atx_it, *it, atx_data);
8571 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8572 }
8573 }
8574 }
8575
8576 if (/* Lines are continued. */
8577 it->line_wrap != TRUNCATE
8578 && (/* And glyph doesn't fit on the line. */
8579 new_x > it->last_visible_x
8580 /* Or it fits exactly and we're on a window
8581 system frame. */
8582 || (new_x == it->last_visible_x
8583 && FRAME_WINDOW_P (it->f)
8584 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8585 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8586 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8587 {
8588 if (/* IT->hpos == 0 means the very first glyph
8589 doesn't fit on the line, e.g. a wide image. */
8590 it->hpos == 0
8591 || (new_x == it->last_visible_x
8592 && FRAME_WINDOW_P (it->f)))
8593 {
8594 ++it->hpos;
8595 it->current_x = new_x;
8596
8597 /* The character's last glyph just barely fits
8598 in this row. */
8599 if (i == it->nglyphs - 1)
8600 {
8601 /* If this is the destination position,
8602 return a position *before* it in this row,
8603 now that we know it fits in this row. */
8604 if (BUFFER_POS_REACHED_P ())
8605 {
8606 if (it->line_wrap != WORD_WRAP
8607 || wrap_it.sp < 0
8608 /* If we've just found whitespace to
8609 wrap, effectively ignore the
8610 previous wrap point -- it is no
8611 longer relevant, but we won't
8612 have an opportunity to update it,
8613 since we've reached the edge of
8614 this screen line. */
8615 || (may_wrap
8616 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8617 {
8618 it->hpos = hpos_before_this_char;
8619 it->current_x = x_before_this_char;
8620 result = MOVE_POS_MATCH_OR_ZV;
8621 break;
8622 }
8623 if (it->line_wrap == WORD_WRAP
8624 && atpos_it.sp < 0)
8625 {
8626 SAVE_IT (atpos_it, *it, atpos_data);
8627 atpos_it.current_x = x_before_this_char;
8628 atpos_it.hpos = hpos_before_this_char;
8629 }
8630 }
8631
8632 prev_method = it->method;
8633 if (it->method == GET_FROM_BUFFER)
8634 prev_pos = IT_CHARPOS (*it);
8635 set_iterator_to_next (it, true);
8636 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8637 SET_TEXT_POS (this_line_min_pos,
8638 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8639 /* On graphical terminals, newlines may
8640 "overflow" into the fringe if
8641 overflow-newline-into-fringe is non-nil.
8642 On text terminals, and on graphical
8643 terminals with no right margin, newlines
8644 may overflow into the last glyph on the
8645 display line.*/
8646 if (!FRAME_WINDOW_P (it->f)
8647 || ((it->bidi_p
8648 && it->bidi_it.paragraph_dir == R2L)
8649 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8650 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8651 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8652 {
8653 if (!get_next_display_element (it))
8654 {
8655 result = MOVE_POS_MATCH_OR_ZV;
8656 break;
8657 }
8658 if (BUFFER_POS_REACHED_P ())
8659 {
8660 if (ITERATOR_AT_END_OF_LINE_P (it))
8661 result = MOVE_POS_MATCH_OR_ZV;
8662 else
8663 result = MOVE_LINE_CONTINUED;
8664 break;
8665 }
8666 if (ITERATOR_AT_END_OF_LINE_P (it)
8667 && (it->line_wrap != WORD_WRAP
8668 || wrap_it.sp < 0
8669 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8670 {
8671 result = MOVE_NEWLINE_OR_CR;
8672 break;
8673 }
8674 }
8675 }
8676 }
8677 else
8678 IT_RESET_X_ASCENT_DESCENT (it);
8679
8680 /* If the screen line ends with whitespace, and we
8681 are under word-wrap, don't use wrap_it: it is no
8682 longer relevant, but we won't have an opportunity
8683 to update it, since we are done with this screen
8684 line. */
8685 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8686 {
8687 /* If we've found TO_X, go back there, as we now
8688 know the last word fits on this screen line. */
8689 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8690 && atx_it.sp >= 0)
8691 {
8692 RESTORE_IT (it, &atx_it, atx_data);
8693 atpos_it.sp = -1;
8694 atx_it.sp = -1;
8695 result = MOVE_X_REACHED;
8696 break;
8697 }
8698 }
8699 else if (wrap_it.sp >= 0)
8700 {
8701 RESTORE_IT (it, &wrap_it, wrap_data);
8702 atpos_it.sp = -1;
8703 atx_it.sp = -1;
8704 }
8705
8706 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8707 IT_CHARPOS (*it)));
8708 result = MOVE_LINE_CONTINUED;
8709 break;
8710 }
8711
8712 if (BUFFER_POS_REACHED_P ())
8713 {
8714 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8715 goto buffer_pos_reached;
8716 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8717 {
8718 SAVE_IT (atpos_it, *it, atpos_data);
8719 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8720 }
8721 }
8722
8723 if (new_x > it->first_visible_x)
8724 {
8725 /* Glyph is visible. Increment number of glyphs that
8726 would be displayed. */
8727 ++it->hpos;
8728 }
8729 }
8730
8731 if (result != MOVE_UNDEFINED)
8732 break;
8733 }
8734 else if (BUFFER_POS_REACHED_P ())
8735 {
8736 buffer_pos_reached:
8737 IT_RESET_X_ASCENT_DESCENT (it);
8738 result = MOVE_POS_MATCH_OR_ZV;
8739 break;
8740 }
8741 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8742 {
8743 /* Stop when TO_X specified and reached. This check is
8744 necessary here because of lines consisting of a line end,
8745 only. The line end will not produce any glyphs and we
8746 would never get MOVE_X_REACHED. */
8747 eassert (it->nglyphs == 0);
8748 result = MOVE_X_REACHED;
8749 break;
8750 }
8751
8752 /* Is this a line end? If yes, we're done. */
8753 if (ITERATOR_AT_END_OF_LINE_P (it))
8754 {
8755 /* If we are past TO_CHARPOS, but never saw any character
8756 positions smaller than TO_CHARPOS, return
8757 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8758 did. */
8759 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8760 {
8761 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8762 {
8763 if (closest_pos < ZV)
8764 {
8765 RESTORE_IT (it, &ppos_it, ppos_data);
8766 /* Don't recurse if closest_pos is equal to
8767 to_charpos, since we have just tried that. */
8768 if (closest_pos != to_charpos)
8769 move_it_in_display_line_to (it, closest_pos, -1,
8770 MOVE_TO_POS);
8771 result = MOVE_POS_MATCH_OR_ZV;
8772 }
8773 else
8774 goto buffer_pos_reached;
8775 }
8776 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8777 && IT_CHARPOS (*it) > to_charpos)
8778 goto buffer_pos_reached;
8779 else
8780 result = MOVE_NEWLINE_OR_CR;
8781 }
8782 else
8783 result = MOVE_NEWLINE_OR_CR;
8784 break;
8785 }
8786
8787 prev_method = it->method;
8788 if (it->method == GET_FROM_BUFFER)
8789 prev_pos = IT_CHARPOS (*it);
8790 /* The current display element has been consumed. Advance
8791 to the next. */
8792 set_iterator_to_next (it, true);
8793 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8794 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8795 if (IT_CHARPOS (*it) < to_charpos)
8796 saw_smaller_pos = true;
8797 if (it->bidi_p
8798 && (op & MOVE_TO_POS)
8799 && IT_CHARPOS (*it) >= to_charpos
8800 && IT_CHARPOS (*it) < closest_pos)
8801 closest_pos = IT_CHARPOS (*it);
8802
8803 /* Stop if lines are truncated and IT's current x-position is
8804 past the right edge of the window now. */
8805 if (it->line_wrap == TRUNCATE
8806 && it->current_x >= it->last_visible_x)
8807 {
8808 if (!FRAME_WINDOW_P (it->f)
8809 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8810 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8811 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8812 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8813 {
8814 bool at_eob_p = false;
8815
8816 if ((at_eob_p = !get_next_display_element (it))
8817 || BUFFER_POS_REACHED_P ()
8818 /* If we are past TO_CHARPOS, but never saw any
8819 character positions smaller than TO_CHARPOS,
8820 return MOVE_POS_MATCH_OR_ZV, like the
8821 unidirectional display did. */
8822 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8823 && !saw_smaller_pos
8824 && IT_CHARPOS (*it) > to_charpos))
8825 {
8826 if (it->bidi_p
8827 && !BUFFER_POS_REACHED_P ()
8828 && !at_eob_p && closest_pos < ZV)
8829 {
8830 RESTORE_IT (it, &ppos_it, ppos_data);
8831 if (closest_pos != to_charpos)
8832 move_it_in_display_line_to (it, closest_pos, -1,
8833 MOVE_TO_POS);
8834 }
8835 result = MOVE_POS_MATCH_OR_ZV;
8836 break;
8837 }
8838 if (ITERATOR_AT_END_OF_LINE_P (it))
8839 {
8840 result = MOVE_NEWLINE_OR_CR;
8841 break;
8842 }
8843 }
8844 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8845 && !saw_smaller_pos
8846 && IT_CHARPOS (*it) > to_charpos)
8847 {
8848 if (closest_pos < ZV)
8849 {
8850 RESTORE_IT (it, &ppos_it, ppos_data);
8851 if (closest_pos != to_charpos)
8852 move_it_in_display_line_to (it, closest_pos, -1,
8853 MOVE_TO_POS);
8854 }
8855 result = MOVE_POS_MATCH_OR_ZV;
8856 break;
8857 }
8858 result = MOVE_LINE_TRUNCATED;
8859 break;
8860 }
8861 #undef IT_RESET_X_ASCENT_DESCENT
8862 }
8863
8864 #undef BUFFER_POS_REACHED_P
8865
8866 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8867 restore the saved iterator. */
8868 if (atpos_it.sp >= 0)
8869 RESTORE_IT (it, &atpos_it, atpos_data);
8870 else if (atx_it.sp >= 0)
8871 RESTORE_IT (it, &atx_it, atx_data);
8872
8873 done:
8874
8875 if (atpos_data)
8876 bidi_unshelve_cache (atpos_data, true);
8877 if (atx_data)
8878 bidi_unshelve_cache (atx_data, true);
8879 if (wrap_data)
8880 bidi_unshelve_cache (wrap_data, true);
8881 if (ppos_data)
8882 bidi_unshelve_cache (ppos_data, true);
8883
8884 /* Restore the iterator settings altered at the beginning of this
8885 function. */
8886 it->glyph_row = saved_glyph_row;
8887 return result;
8888 }
8889
8890 /* For external use. */
8891 void
8892 move_it_in_display_line (struct it *it,
8893 ptrdiff_t to_charpos, int to_x,
8894 enum move_operation_enum op)
8895 {
8896 if (it->line_wrap == WORD_WRAP
8897 && (op & MOVE_TO_X))
8898 {
8899 struct it save_it;
8900 void *save_data = NULL;
8901 int skip;
8902
8903 SAVE_IT (save_it, *it, save_data);
8904 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8905 /* When word-wrap is on, TO_X may lie past the end
8906 of a wrapped line. Then it->current is the
8907 character on the next line, so backtrack to the
8908 space before the wrap point. */
8909 if (skip == MOVE_LINE_CONTINUED)
8910 {
8911 int prev_x = max (it->current_x - 1, 0);
8912 RESTORE_IT (it, &save_it, save_data);
8913 move_it_in_display_line_to
8914 (it, -1, prev_x, MOVE_TO_X);
8915 }
8916 else
8917 bidi_unshelve_cache (save_data, true);
8918 }
8919 else
8920 move_it_in_display_line_to (it, to_charpos, to_x, op);
8921 }
8922
8923
8924 /* Move IT forward until it satisfies one or more of the criteria in
8925 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8926
8927 OP is a bit-mask that specifies where to stop, and in particular,
8928 which of those four position arguments makes a difference. See the
8929 description of enum move_operation_enum.
8930
8931 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8932 screen line, this function will set IT to the next position that is
8933 displayed to the right of TO_CHARPOS on the screen.
8934
8935 Return the maximum pixel length of any line scanned but never more
8936 than it.last_visible_x. */
8937
8938 int
8939 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8940 {
8941 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8942 int line_height, line_start_x = 0, reached = 0;
8943 int max_current_x = 0;
8944 void *backup_data = NULL;
8945
8946 for (;;)
8947 {
8948 if (op & MOVE_TO_VPOS)
8949 {
8950 /* If no TO_CHARPOS and no TO_X specified, stop at the
8951 start of the line TO_VPOS. */
8952 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8953 {
8954 if (it->vpos == to_vpos)
8955 {
8956 reached = 1;
8957 break;
8958 }
8959 else
8960 skip = move_it_in_display_line_to (it, -1, -1, 0);
8961 }
8962 else
8963 {
8964 /* TO_VPOS >= 0 means stop at TO_X in the line at
8965 TO_VPOS, or at TO_POS, whichever comes first. */
8966 if (it->vpos == to_vpos)
8967 {
8968 reached = 2;
8969 break;
8970 }
8971
8972 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8973
8974 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8975 {
8976 reached = 3;
8977 break;
8978 }
8979 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8980 {
8981 /* We have reached TO_X but not in the line we want. */
8982 skip = move_it_in_display_line_to (it, to_charpos,
8983 -1, MOVE_TO_POS);
8984 if (skip == MOVE_POS_MATCH_OR_ZV)
8985 {
8986 reached = 4;
8987 break;
8988 }
8989 }
8990 }
8991 }
8992 else if (op & MOVE_TO_Y)
8993 {
8994 struct it it_backup;
8995
8996 if (it->line_wrap == WORD_WRAP)
8997 SAVE_IT (it_backup, *it, backup_data);
8998
8999 /* TO_Y specified means stop at TO_X in the line containing
9000 TO_Y---or at TO_CHARPOS if this is reached first. The
9001 problem is that we can't really tell whether the line
9002 contains TO_Y before we have completely scanned it, and
9003 this may skip past TO_X. What we do is to first scan to
9004 TO_X.
9005
9006 If TO_X is not specified, use a TO_X of zero. The reason
9007 is to make the outcome of this function more predictable.
9008 If we didn't use TO_X == 0, we would stop at the end of
9009 the line which is probably not what a caller would expect
9010 to happen. */
9011 skip = move_it_in_display_line_to
9012 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9013 (MOVE_TO_X | (op & MOVE_TO_POS)));
9014
9015 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9016 if (skip == MOVE_POS_MATCH_OR_ZV)
9017 reached = 5;
9018 else if (skip == MOVE_X_REACHED)
9019 {
9020 /* If TO_X was reached, we want to know whether TO_Y is
9021 in the line. We know this is the case if the already
9022 scanned glyphs make the line tall enough. Otherwise,
9023 we must check by scanning the rest of the line. */
9024 line_height = it->max_ascent + it->max_descent;
9025 if (to_y >= it->current_y
9026 && to_y < it->current_y + line_height)
9027 {
9028 reached = 6;
9029 break;
9030 }
9031 SAVE_IT (it_backup, *it, backup_data);
9032 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9033 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9034 op & MOVE_TO_POS);
9035 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9036 line_height = it->max_ascent + it->max_descent;
9037 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9038
9039 if (to_y >= it->current_y
9040 && to_y < it->current_y + line_height)
9041 {
9042 /* If TO_Y is in this line and TO_X was reached
9043 above, we scanned too far. We have to restore
9044 IT's settings to the ones before skipping. But
9045 keep the more accurate values of max_ascent and
9046 max_descent we've found while skipping the rest
9047 of the line, for the sake of callers, such as
9048 pos_visible_p, that need to know the line
9049 height. */
9050 int max_ascent = it->max_ascent;
9051 int max_descent = it->max_descent;
9052
9053 RESTORE_IT (it, &it_backup, backup_data);
9054 it->max_ascent = max_ascent;
9055 it->max_descent = max_descent;
9056 reached = 6;
9057 }
9058 else
9059 {
9060 skip = skip2;
9061 if (skip == MOVE_POS_MATCH_OR_ZV)
9062 reached = 7;
9063 }
9064 }
9065 else
9066 {
9067 /* Check whether TO_Y is in this line. */
9068 line_height = it->max_ascent + it->max_descent;
9069 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9070
9071 if (to_y >= it->current_y
9072 && to_y < it->current_y + line_height)
9073 {
9074 if (to_y > it->current_y)
9075 max_current_x = max (it->current_x, max_current_x);
9076
9077 /* When word-wrap is on, TO_X may lie past the end
9078 of a wrapped line. Then it->current is the
9079 character on the next line, so backtrack to the
9080 space before the wrap point. */
9081 if (skip == MOVE_LINE_CONTINUED
9082 && it->line_wrap == WORD_WRAP)
9083 {
9084 int prev_x = max (it->current_x - 1, 0);
9085 RESTORE_IT (it, &it_backup, backup_data);
9086 skip = move_it_in_display_line_to
9087 (it, -1, prev_x, MOVE_TO_X);
9088 }
9089
9090 reached = 6;
9091 }
9092 }
9093
9094 if (reached)
9095 {
9096 max_current_x = max (it->current_x, max_current_x);
9097 break;
9098 }
9099 }
9100 else if (BUFFERP (it->object)
9101 && (it->method == GET_FROM_BUFFER
9102 || it->method == GET_FROM_STRETCH)
9103 && IT_CHARPOS (*it) >= to_charpos
9104 /* Under bidi iteration, a call to set_iterator_to_next
9105 can scan far beyond to_charpos if the initial
9106 portion of the next line needs to be reordered. In
9107 that case, give move_it_in_display_line_to another
9108 chance below. */
9109 && !(it->bidi_p
9110 && it->bidi_it.scan_dir == -1))
9111 skip = MOVE_POS_MATCH_OR_ZV;
9112 else
9113 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9114
9115 switch (skip)
9116 {
9117 case MOVE_POS_MATCH_OR_ZV:
9118 max_current_x = max (it->current_x, max_current_x);
9119 reached = 8;
9120 goto out;
9121
9122 case MOVE_NEWLINE_OR_CR:
9123 max_current_x = max (it->current_x, max_current_x);
9124 set_iterator_to_next (it, true);
9125 it->continuation_lines_width = 0;
9126 break;
9127
9128 case MOVE_LINE_TRUNCATED:
9129 max_current_x = it->last_visible_x;
9130 it->continuation_lines_width = 0;
9131 reseat_at_next_visible_line_start (it, false);
9132 if ((op & MOVE_TO_POS) != 0
9133 && IT_CHARPOS (*it) > to_charpos)
9134 {
9135 reached = 9;
9136 goto out;
9137 }
9138 break;
9139
9140 case MOVE_LINE_CONTINUED:
9141 max_current_x = it->last_visible_x;
9142 /* For continued lines ending in a tab, some of the glyphs
9143 associated with the tab are displayed on the current
9144 line. Since it->current_x does not include these glyphs,
9145 we use it->last_visible_x instead. */
9146 if (it->c == '\t')
9147 {
9148 it->continuation_lines_width += it->last_visible_x;
9149 /* When moving by vpos, ensure that the iterator really
9150 advances to the next line (bug#847, bug#969). Fixme:
9151 do we need to do this in other circumstances? */
9152 if (it->current_x != it->last_visible_x
9153 && (op & MOVE_TO_VPOS)
9154 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9155 {
9156 line_start_x = it->current_x + it->pixel_width
9157 - it->last_visible_x;
9158 if (FRAME_WINDOW_P (it->f))
9159 {
9160 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9161 struct font *face_font = face->font;
9162
9163 /* When display_line produces a continued line
9164 that ends in a TAB, it skips a tab stop that
9165 is closer than the font's space character
9166 width (see x_produce_glyphs where it produces
9167 the stretch glyph which represents a TAB).
9168 We need to reproduce the same logic here. */
9169 eassert (face_font);
9170 if (face_font)
9171 {
9172 if (line_start_x < face_font->space_width)
9173 line_start_x
9174 += it->tab_width * face_font->space_width;
9175 }
9176 }
9177 set_iterator_to_next (it, false);
9178 }
9179 }
9180 else
9181 it->continuation_lines_width += it->current_x;
9182 break;
9183
9184 default:
9185 emacs_abort ();
9186 }
9187
9188 /* Reset/increment for the next run. */
9189 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9190 it->current_x = line_start_x;
9191 line_start_x = 0;
9192 it->hpos = 0;
9193 it->current_y += it->max_ascent + it->max_descent;
9194 ++it->vpos;
9195 last_height = it->max_ascent + it->max_descent;
9196 it->max_ascent = it->max_descent = 0;
9197 }
9198
9199 out:
9200
9201 /* On text terminals, we may stop at the end of a line in the middle
9202 of a multi-character glyph. If the glyph itself is continued,
9203 i.e. it is actually displayed on the next line, don't treat this
9204 stopping point as valid; move to the next line instead (unless
9205 that brings us offscreen). */
9206 if (!FRAME_WINDOW_P (it->f)
9207 && op & MOVE_TO_POS
9208 && IT_CHARPOS (*it) == to_charpos
9209 && it->what == IT_CHARACTER
9210 && it->nglyphs > 1
9211 && it->line_wrap == WINDOW_WRAP
9212 && it->current_x == it->last_visible_x - 1
9213 && it->c != '\n'
9214 && it->c != '\t'
9215 && it->w->window_end_valid
9216 && it->vpos < it->w->window_end_vpos)
9217 {
9218 it->continuation_lines_width += it->current_x;
9219 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9220 it->current_y += it->max_ascent + it->max_descent;
9221 ++it->vpos;
9222 last_height = it->max_ascent + it->max_descent;
9223 }
9224
9225 if (backup_data)
9226 bidi_unshelve_cache (backup_data, true);
9227
9228 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9229
9230 return max_current_x;
9231 }
9232
9233
9234 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9235
9236 If DY > 0, move IT backward at least that many pixels. DY = 0
9237 means move IT backward to the preceding line start or BEGV. This
9238 function may move over more than DY pixels if IT->current_y - DY
9239 ends up in the middle of a line; in this case IT->current_y will be
9240 set to the top of the line moved to. */
9241
9242 void
9243 move_it_vertically_backward (struct it *it, int dy)
9244 {
9245 int nlines, h;
9246 struct it it2, it3;
9247 void *it2data = NULL, *it3data = NULL;
9248 ptrdiff_t start_pos;
9249 int nchars_per_row
9250 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9251 ptrdiff_t pos_limit;
9252
9253 move_further_back:
9254 eassert (dy >= 0);
9255
9256 start_pos = IT_CHARPOS (*it);
9257
9258 /* Estimate how many newlines we must move back. */
9259 nlines = max (1, dy / default_line_pixel_height (it->w));
9260 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9261 pos_limit = BEGV;
9262 else
9263 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9264
9265 /* Set the iterator's position that many lines back. But don't go
9266 back more than NLINES full screen lines -- this wins a day with
9267 buffers which have very long lines. */
9268 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9269 back_to_previous_visible_line_start (it);
9270
9271 /* Reseat the iterator here. When moving backward, we don't want
9272 reseat to skip forward over invisible text, set up the iterator
9273 to deliver from overlay strings at the new position etc. So,
9274 use reseat_1 here. */
9275 reseat_1 (it, it->current.pos, true);
9276
9277 /* We are now surely at a line start. */
9278 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9279 reordering is in effect. */
9280 it->continuation_lines_width = 0;
9281
9282 /* Move forward and see what y-distance we moved. First move to the
9283 start of the next line so that we get its height. We need this
9284 height to be able to tell whether we reached the specified
9285 y-distance. */
9286 SAVE_IT (it2, *it, it2data);
9287 it2.max_ascent = it2.max_descent = 0;
9288 do
9289 {
9290 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9291 MOVE_TO_POS | MOVE_TO_VPOS);
9292 }
9293 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9294 /* If we are in a display string which starts at START_POS,
9295 and that display string includes a newline, and we are
9296 right after that newline (i.e. at the beginning of a
9297 display line), exit the loop, because otherwise we will
9298 infloop, since move_it_to will see that it is already at
9299 START_POS and will not move. */
9300 || (it2.method == GET_FROM_STRING
9301 && IT_CHARPOS (it2) == start_pos
9302 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9303 eassert (IT_CHARPOS (*it) >= BEGV);
9304 SAVE_IT (it3, it2, it3data);
9305
9306 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9307 eassert (IT_CHARPOS (*it) >= BEGV);
9308 /* H is the actual vertical distance from the position in *IT
9309 and the starting position. */
9310 h = it2.current_y - it->current_y;
9311 /* NLINES is the distance in number of lines. */
9312 nlines = it2.vpos - it->vpos;
9313
9314 /* Correct IT's y and vpos position
9315 so that they are relative to the starting point. */
9316 it->vpos -= nlines;
9317 it->current_y -= h;
9318
9319 if (dy == 0)
9320 {
9321 /* DY == 0 means move to the start of the screen line. The
9322 value of nlines is > 0 if continuation lines were involved,
9323 or if the original IT position was at start of a line. */
9324 RESTORE_IT (it, it, it2data);
9325 if (nlines > 0)
9326 move_it_by_lines (it, nlines);
9327 /* The above code moves us to some position NLINES down,
9328 usually to its first glyph (leftmost in an L2R line), but
9329 that's not necessarily the start of the line, under bidi
9330 reordering. We want to get to the character position
9331 that is immediately after the newline of the previous
9332 line. */
9333 if (it->bidi_p
9334 && !it->continuation_lines_width
9335 && !STRINGP (it->string)
9336 && IT_CHARPOS (*it) > BEGV
9337 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9338 {
9339 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9340
9341 DEC_BOTH (cp, bp);
9342 cp = find_newline_no_quit (cp, bp, -1, NULL);
9343 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9344 }
9345 bidi_unshelve_cache (it3data, true);
9346 }
9347 else
9348 {
9349 /* The y-position we try to reach, relative to *IT.
9350 Note that H has been subtracted in front of the if-statement. */
9351 int target_y = it->current_y + h - dy;
9352 int y0 = it3.current_y;
9353 int y1;
9354 int line_height;
9355
9356 RESTORE_IT (&it3, &it3, it3data);
9357 y1 = line_bottom_y (&it3);
9358 line_height = y1 - y0;
9359 RESTORE_IT (it, it, it2data);
9360 /* If we did not reach target_y, try to move further backward if
9361 we can. If we moved too far backward, try to move forward. */
9362 if (target_y < it->current_y
9363 /* This is heuristic. In a window that's 3 lines high, with
9364 a line height of 13 pixels each, recentering with point
9365 on the bottom line will try to move -39/2 = 19 pixels
9366 backward. Try to avoid moving into the first line. */
9367 && (it->current_y - target_y
9368 > min (window_box_height (it->w), line_height * 2 / 3))
9369 && IT_CHARPOS (*it) > BEGV)
9370 {
9371 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9372 target_y - it->current_y));
9373 dy = it->current_y - target_y;
9374 goto move_further_back;
9375 }
9376 else if (target_y >= it->current_y + line_height
9377 && IT_CHARPOS (*it) < ZV)
9378 {
9379 /* Should move forward by at least one line, maybe more.
9380
9381 Note: Calling move_it_by_lines can be expensive on
9382 terminal frames, where compute_motion is used (via
9383 vmotion) to do the job, when there are very long lines
9384 and truncate-lines is nil. That's the reason for
9385 treating terminal frames specially here. */
9386
9387 if (!FRAME_WINDOW_P (it->f))
9388 move_it_vertically (it, target_y - (it->current_y + line_height));
9389 else
9390 {
9391 do
9392 {
9393 move_it_by_lines (it, 1);
9394 }
9395 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9396 }
9397 }
9398 }
9399 }
9400
9401
9402 /* Move IT by a specified amount of pixel lines DY. DY negative means
9403 move backwards. DY = 0 means move to start of screen line. At the
9404 end, IT will be on the start of a screen line. */
9405
9406 void
9407 move_it_vertically (struct it *it, int dy)
9408 {
9409 if (dy <= 0)
9410 move_it_vertically_backward (it, -dy);
9411 else
9412 {
9413 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9414 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9415 MOVE_TO_POS | MOVE_TO_Y);
9416 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9417
9418 /* If buffer ends in ZV without a newline, move to the start of
9419 the line to satisfy the post-condition. */
9420 if (IT_CHARPOS (*it) == ZV
9421 && ZV > BEGV
9422 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9423 move_it_by_lines (it, 0);
9424 }
9425 }
9426
9427
9428 /* Move iterator IT past the end of the text line it is in. */
9429
9430 void
9431 move_it_past_eol (struct it *it)
9432 {
9433 enum move_it_result rc;
9434
9435 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9436 if (rc == MOVE_NEWLINE_OR_CR)
9437 set_iterator_to_next (it, false);
9438 }
9439
9440
9441 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9442 negative means move up. DVPOS == 0 means move to the start of the
9443 screen line.
9444
9445 Optimization idea: If we would know that IT->f doesn't use
9446 a face with proportional font, we could be faster for
9447 truncate-lines nil. */
9448
9449 void
9450 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9451 {
9452
9453 /* The commented-out optimization uses vmotion on terminals. This
9454 gives bad results, because elements like it->what, on which
9455 callers such as pos_visible_p rely, aren't updated. */
9456 /* struct position pos;
9457 if (!FRAME_WINDOW_P (it->f))
9458 {
9459 struct text_pos textpos;
9460
9461 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9462 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9463 reseat (it, textpos, true);
9464 it->vpos += pos.vpos;
9465 it->current_y += pos.vpos;
9466 }
9467 else */
9468
9469 if (dvpos == 0)
9470 {
9471 /* DVPOS == 0 means move to the start of the screen line. */
9472 move_it_vertically_backward (it, 0);
9473 /* Let next call to line_bottom_y calculate real line height. */
9474 last_height = 0;
9475 }
9476 else if (dvpos > 0)
9477 {
9478 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9479 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9480 {
9481 /* Only move to the next buffer position if we ended up in a
9482 string from display property, not in an overlay string
9483 (before-string or after-string). That is because the
9484 latter don't conceal the underlying buffer position, so
9485 we can ask to move the iterator to the exact position we
9486 are interested in. Note that, even if we are already at
9487 IT_CHARPOS (*it), the call below is not a no-op, as it
9488 will detect that we are at the end of the string, pop the
9489 iterator, and compute it->current_x and it->hpos
9490 correctly. */
9491 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9492 -1, -1, -1, MOVE_TO_POS);
9493 }
9494 }
9495 else
9496 {
9497 struct it it2;
9498 void *it2data = NULL;
9499 ptrdiff_t start_charpos, i;
9500 int nchars_per_row
9501 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9502 bool hit_pos_limit = false;
9503 ptrdiff_t pos_limit;
9504
9505 /* Start at the beginning of the screen line containing IT's
9506 position. This may actually move vertically backwards,
9507 in case of overlays, so adjust dvpos accordingly. */
9508 dvpos += it->vpos;
9509 move_it_vertically_backward (it, 0);
9510 dvpos -= it->vpos;
9511
9512 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9513 screen lines, and reseat the iterator there. */
9514 start_charpos = IT_CHARPOS (*it);
9515 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9516 pos_limit = BEGV;
9517 else
9518 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9519
9520 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9521 back_to_previous_visible_line_start (it);
9522 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9523 hit_pos_limit = true;
9524 reseat (it, it->current.pos, true);
9525
9526 /* Move further back if we end up in a string or an image. */
9527 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9528 {
9529 /* First try to move to start of display line. */
9530 dvpos += it->vpos;
9531 move_it_vertically_backward (it, 0);
9532 dvpos -= it->vpos;
9533 if (IT_POS_VALID_AFTER_MOVE_P (it))
9534 break;
9535 /* If start of line is still in string or image,
9536 move further back. */
9537 back_to_previous_visible_line_start (it);
9538 reseat (it, it->current.pos, true);
9539 dvpos--;
9540 }
9541
9542 it->current_x = it->hpos = 0;
9543
9544 /* Above call may have moved too far if continuation lines
9545 are involved. Scan forward and see if it did. */
9546 SAVE_IT (it2, *it, it2data);
9547 it2.vpos = it2.current_y = 0;
9548 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9549 it->vpos -= it2.vpos;
9550 it->current_y -= it2.current_y;
9551 it->current_x = it->hpos = 0;
9552
9553 /* If we moved too far back, move IT some lines forward. */
9554 if (it2.vpos > -dvpos)
9555 {
9556 int delta = it2.vpos + dvpos;
9557
9558 RESTORE_IT (&it2, &it2, it2data);
9559 SAVE_IT (it2, *it, it2data);
9560 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9561 /* Move back again if we got too far ahead. */
9562 if (IT_CHARPOS (*it) >= start_charpos)
9563 RESTORE_IT (it, &it2, it2data);
9564 else
9565 bidi_unshelve_cache (it2data, true);
9566 }
9567 else if (hit_pos_limit && pos_limit > BEGV
9568 && dvpos < 0 && it2.vpos < -dvpos)
9569 {
9570 /* If we hit the limit, but still didn't make it far enough
9571 back, that means there's a display string with a newline
9572 covering a large chunk of text, and that caused
9573 back_to_previous_visible_line_start try to go too far.
9574 Punish those who commit such atrocities by going back
9575 until we've reached DVPOS, after lifting the limit, which
9576 could make it slow for very long lines. "If it hurts,
9577 don't do that!" */
9578 dvpos += it2.vpos;
9579 RESTORE_IT (it, it, it2data);
9580 for (i = -dvpos; i > 0; --i)
9581 {
9582 back_to_previous_visible_line_start (it);
9583 it->vpos--;
9584 }
9585 reseat_1 (it, it->current.pos, true);
9586 }
9587 else
9588 RESTORE_IT (it, it, it2data);
9589 }
9590 }
9591
9592 /* Return true if IT points into the middle of a display vector. */
9593
9594 bool
9595 in_display_vector_p (struct it *it)
9596 {
9597 return (it->method == GET_FROM_DISPLAY_VECTOR
9598 && it->current.dpvec_index > 0
9599 && it->dpvec + it->current.dpvec_index != it->dpend);
9600 }
9601
9602 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9603 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9604 WINDOW must be a live window and defaults to the selected one. The
9605 return value is a cons of the maximum pixel-width of any text line and
9606 the maximum pixel-height of all text lines.
9607
9608 The optional argument FROM, if non-nil, specifies the first text
9609 position and defaults to the minimum accessible position of the buffer.
9610 If FROM is t, use the minimum accessible position that is not a newline
9611 character. TO, if non-nil, specifies the last text position and
9612 defaults to the maximum accessible position of the buffer. If TO is t,
9613 use the maximum accessible position that is not a newline character.
9614
9615 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9616 width that can be returned. X-LIMIT nil or omitted, means to use the
9617 pixel-width of WINDOW's body; use this if you do not intend to change
9618 the width of WINDOW. Use the maximum width WINDOW may assume if you
9619 intend to change WINDOW's width. In any case, text whose x-coordinate
9620 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9621 can take some time, it's always a good idea to make this argument as
9622 small as possible; in particular, if the buffer contains long lines that
9623 shall be truncated anyway.
9624
9625 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9626 height that can be returned. Text lines whose y-coordinate is beyond
9627 Y-LIMIT are ignored. Since calculating the text height of a large
9628 buffer can take some time, it makes sense to specify this argument if
9629 the size of the buffer is unknown.
9630
9631 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9632 include the height of the mode- or header-line of WINDOW in the return
9633 value. If it is either the symbol `mode-line' or `header-line', include
9634 only the height of that line, if present, in the return value. If t,
9635 include the height of both, if present, in the return value. */)
9636 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9637 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9638 {
9639 struct window *w = decode_live_window (window);
9640 Lisp_Object buffer = w->contents;
9641 struct buffer *b;
9642 struct it it;
9643 struct buffer *old_b = NULL;
9644 ptrdiff_t start, end, pos;
9645 struct text_pos startp;
9646 void *itdata = NULL;
9647 int c, max_y = -1, x = 0, y = 0;
9648
9649 CHECK_BUFFER (buffer);
9650 b = XBUFFER (buffer);
9651
9652 if (b != current_buffer)
9653 {
9654 old_b = current_buffer;
9655 set_buffer_internal (b);
9656 }
9657
9658 if (NILP (from))
9659 start = BEGV;
9660 else if (EQ (from, Qt))
9661 {
9662 start = pos = BEGV;
9663 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9664 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9665 start = pos;
9666 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9667 start = pos;
9668 }
9669 else
9670 {
9671 CHECK_NUMBER_COERCE_MARKER (from);
9672 start = min (max (XINT (from), BEGV), ZV);
9673 }
9674
9675 if (NILP (to))
9676 end = ZV;
9677 else if (EQ (to, Qt))
9678 {
9679 end = pos = ZV;
9680 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9681 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9682 end = pos;
9683 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9684 end = pos;
9685 }
9686 else
9687 {
9688 CHECK_NUMBER_COERCE_MARKER (to);
9689 end = max (start, min (XINT (to), ZV));
9690 }
9691
9692 if (!NILP (y_limit))
9693 {
9694 CHECK_NUMBER (y_limit);
9695 max_y = min (XINT (y_limit), INT_MAX);
9696 }
9697
9698 itdata = bidi_shelve_cache ();
9699 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9700 start_display (&it, w, startp);
9701
9702 if (NILP (x_limit))
9703 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9704 else
9705 {
9706 CHECK_NUMBER (x_limit);
9707 it.last_visible_x = min (XINT (x_limit), INFINITY);
9708 /* Actually, we never want move_it_to stop at to_x. But to make
9709 sure that move_it_in_display_line_to always moves far enough,
9710 we set it to INT_MAX and specify MOVE_TO_X. */
9711 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9712 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9713 }
9714
9715 y = it.current_y + it.max_ascent + it.max_descent;
9716
9717 if (!EQ (mode_and_header_line, Qheader_line)
9718 && !EQ (mode_and_header_line, Qt))
9719 /* Do not count the header-line which was counted automatically by
9720 start_display. */
9721 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9722
9723 if (EQ (mode_and_header_line, Qmode_line)
9724 || EQ (mode_and_header_line, Qt))
9725 /* Do count the mode-line which is not included automatically by
9726 start_display. */
9727 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9728
9729 bidi_unshelve_cache (itdata, false);
9730
9731 if (old_b)
9732 set_buffer_internal (old_b);
9733
9734 return Fcons (make_number (x), make_number (y));
9735 }
9736 \f
9737 /***********************************************************************
9738 Messages
9739 ***********************************************************************/
9740
9741
9742 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9743 to *Messages*. */
9744
9745 void
9746 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9747 {
9748 Lisp_Object msg, fmt;
9749 char *buffer;
9750 ptrdiff_t len;
9751 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9752 USE_SAFE_ALLOCA;
9753
9754 fmt = msg = Qnil;
9755 GCPRO4 (fmt, msg, arg1, arg2);
9756
9757 fmt = build_string (format);
9758 msg = CALLN (Fformat, fmt, arg1, arg2);
9759
9760 len = SBYTES (msg) + 1;
9761 buffer = SAFE_ALLOCA (len);
9762 memcpy (buffer, SDATA (msg), len);
9763
9764 message_dolog (buffer, len - 1, true, false);
9765 SAFE_FREE ();
9766
9767 UNGCPRO;
9768 }
9769
9770
9771 /* Output a newline in the *Messages* buffer if "needs" one. */
9772
9773 void
9774 message_log_maybe_newline (void)
9775 {
9776 if (message_log_need_newline)
9777 message_dolog ("", 0, true, false);
9778 }
9779
9780
9781 /* Add a string M of length NBYTES to the message log, optionally
9782 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9783 true, means interpret the contents of M as multibyte. This
9784 function calls low-level routines in order to bypass text property
9785 hooks, etc. which might not be safe to run.
9786
9787 This may GC (insert may run before/after change hooks),
9788 so the buffer M must NOT point to a Lisp string. */
9789
9790 void
9791 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9792 {
9793 const unsigned char *msg = (const unsigned char *) m;
9794
9795 if (!NILP (Vmemory_full))
9796 return;
9797
9798 if (!NILP (Vmessage_log_max))
9799 {
9800 struct buffer *oldbuf;
9801 Lisp_Object oldpoint, oldbegv, oldzv;
9802 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9803 ptrdiff_t point_at_end = 0;
9804 ptrdiff_t zv_at_end = 0;
9805 Lisp_Object old_deactivate_mark;
9806 struct gcpro gcpro1;
9807
9808 old_deactivate_mark = Vdeactivate_mark;
9809 oldbuf = current_buffer;
9810
9811 /* Ensure the Messages buffer exists, and switch to it.
9812 If we created it, set the major-mode. */
9813 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
9814 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9815 if (newbuffer
9816 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9817 call0 (intern ("messages-buffer-mode"));
9818
9819 bset_undo_list (current_buffer, Qt);
9820 bset_cache_long_scans (current_buffer, Qnil);
9821
9822 oldpoint = message_dolog_marker1;
9823 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9824 oldbegv = message_dolog_marker2;
9825 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9826 oldzv = message_dolog_marker3;
9827 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9828 GCPRO1 (old_deactivate_mark);
9829
9830 if (PT == Z)
9831 point_at_end = 1;
9832 if (ZV == Z)
9833 zv_at_end = 1;
9834
9835 BEGV = BEG;
9836 BEGV_BYTE = BEG_BYTE;
9837 ZV = Z;
9838 ZV_BYTE = Z_BYTE;
9839 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9840
9841 /* Insert the string--maybe converting multibyte to single byte
9842 or vice versa, so that all the text fits the buffer. */
9843 if (multibyte
9844 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9845 {
9846 ptrdiff_t i;
9847 int c, char_bytes;
9848 char work[1];
9849
9850 /* Convert a multibyte string to single-byte
9851 for the *Message* buffer. */
9852 for (i = 0; i < nbytes; i += char_bytes)
9853 {
9854 c = string_char_and_length (msg + i, &char_bytes);
9855 work[0] = CHAR_TO_BYTE8 (c);
9856 insert_1_both (work, 1, 1, true, false, false);
9857 }
9858 }
9859 else if (! multibyte
9860 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9861 {
9862 ptrdiff_t i;
9863 int c, char_bytes;
9864 unsigned char str[MAX_MULTIBYTE_LENGTH];
9865 /* Convert a single-byte string to multibyte
9866 for the *Message* buffer. */
9867 for (i = 0; i < nbytes; i++)
9868 {
9869 c = msg[i];
9870 MAKE_CHAR_MULTIBYTE (c);
9871 char_bytes = CHAR_STRING (c, str);
9872 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
9873 }
9874 }
9875 else if (nbytes)
9876 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
9877 true, false, false);
9878
9879 if (nlflag)
9880 {
9881 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9882 printmax_t dups;
9883
9884 insert_1_both ("\n", 1, 1, true, false, false);
9885
9886 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
9887 this_bol = PT;
9888 this_bol_byte = PT_BYTE;
9889
9890 /* See if this line duplicates the previous one.
9891 If so, combine duplicates. */
9892 if (this_bol > BEG)
9893 {
9894 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
9895 prev_bol = PT;
9896 prev_bol_byte = PT_BYTE;
9897
9898 dups = message_log_check_duplicate (prev_bol_byte,
9899 this_bol_byte);
9900 if (dups)
9901 {
9902 del_range_both (prev_bol, prev_bol_byte,
9903 this_bol, this_bol_byte, false);
9904 if (dups > 1)
9905 {
9906 char dupstr[sizeof " [ times]"
9907 + INT_STRLEN_BOUND (printmax_t)];
9908
9909 /* If you change this format, don't forget to also
9910 change message_log_check_duplicate. */
9911 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9912 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9913 insert_1_both (dupstr, duplen, duplen,
9914 true, false, true);
9915 }
9916 }
9917 }
9918
9919 /* If we have more than the desired maximum number of lines
9920 in the *Messages* buffer now, delete the oldest ones.
9921 This is safe because we don't have undo in this buffer. */
9922
9923 if (NATNUMP (Vmessage_log_max))
9924 {
9925 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9926 -XFASTINT (Vmessage_log_max) - 1, false);
9927 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
9928 }
9929 }
9930 BEGV = marker_position (oldbegv);
9931 BEGV_BYTE = marker_byte_position (oldbegv);
9932
9933 if (zv_at_end)
9934 {
9935 ZV = Z;
9936 ZV_BYTE = Z_BYTE;
9937 }
9938 else
9939 {
9940 ZV = marker_position (oldzv);
9941 ZV_BYTE = marker_byte_position (oldzv);
9942 }
9943
9944 if (point_at_end)
9945 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9946 else
9947 /* We can't do Fgoto_char (oldpoint) because it will run some
9948 Lisp code. */
9949 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9950 marker_byte_position (oldpoint));
9951
9952 UNGCPRO;
9953 unchain_marker (XMARKER (oldpoint));
9954 unchain_marker (XMARKER (oldbegv));
9955 unchain_marker (XMARKER (oldzv));
9956
9957 /* We called insert_1_both above with its 5th argument (PREPARE)
9958 false, which prevents insert_1_both from calling
9959 prepare_to_modify_buffer, which in turns prevents us from
9960 incrementing windows_or_buffers_changed even if *Messages* is
9961 shown in some window. So we must manually set
9962 windows_or_buffers_changed here to make up for that. */
9963 windows_or_buffers_changed = old_windows_or_buffers_changed;
9964 bset_redisplay (current_buffer);
9965
9966 set_buffer_internal (oldbuf);
9967
9968 message_log_need_newline = !nlflag;
9969 Vdeactivate_mark = old_deactivate_mark;
9970 }
9971 }
9972
9973
9974 /* We are at the end of the buffer after just having inserted a newline.
9975 (Note: We depend on the fact we won't be crossing the gap.)
9976 Check to see if the most recent message looks a lot like the previous one.
9977 Return 0 if different, 1 if the new one should just replace it, or a
9978 value N > 1 if we should also append " [N times]". */
9979
9980 static intmax_t
9981 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9982 {
9983 ptrdiff_t i;
9984 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9985 bool seen_dots = false;
9986 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9987 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9988
9989 for (i = 0; i < len; i++)
9990 {
9991 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9992 seen_dots = true;
9993 if (p1[i] != p2[i])
9994 return seen_dots;
9995 }
9996 p1 += len;
9997 if (*p1 == '\n')
9998 return 2;
9999 if (*p1++ == ' ' && *p1++ == '[')
10000 {
10001 char *pend;
10002 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10003 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10004 return n + 1;
10005 }
10006 return 0;
10007 }
10008 \f
10009
10010 /* Display an echo area message M with a specified length of NBYTES
10011 bytes. The string may include null characters. If M is not a
10012 string, clear out any existing message, and let the mini-buffer
10013 text show through.
10014
10015 This function cancels echoing. */
10016
10017 void
10018 message3 (Lisp_Object m)
10019 {
10020 struct gcpro gcpro1;
10021
10022 GCPRO1 (m);
10023 clear_message (true, true);
10024 cancel_echoing ();
10025
10026 /* First flush out any partial line written with print. */
10027 message_log_maybe_newline ();
10028 if (STRINGP (m))
10029 {
10030 ptrdiff_t nbytes = SBYTES (m);
10031 bool multibyte = STRING_MULTIBYTE (m);
10032 char *buffer;
10033 USE_SAFE_ALLOCA;
10034 SAFE_ALLOCA_STRING (buffer, m);
10035 message_dolog (buffer, nbytes, true, multibyte);
10036 SAFE_FREE ();
10037 }
10038 message3_nolog (m);
10039
10040 UNGCPRO;
10041 }
10042
10043
10044 /* The non-logging version of message3.
10045 This does not cancel echoing, because it is used for echoing.
10046 Perhaps we need to make a separate function for echoing
10047 and make this cancel echoing. */
10048
10049 void
10050 message3_nolog (Lisp_Object m)
10051 {
10052 struct frame *sf = SELECTED_FRAME ();
10053
10054 if (FRAME_INITIAL_P (sf))
10055 {
10056 if (noninteractive_need_newline)
10057 putc ('\n', stderr);
10058 noninteractive_need_newline = false;
10059 if (STRINGP (m))
10060 {
10061 Lisp_Object s = ENCODE_SYSTEM (m);
10062
10063 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10064 }
10065 if (!cursor_in_echo_area)
10066 fprintf (stderr, "\n");
10067 fflush (stderr);
10068 }
10069 /* Error messages get reported properly by cmd_error, so this must be just an
10070 informative message; if the frame hasn't really been initialized yet, just
10071 toss it. */
10072 else if (INTERACTIVE && sf->glyphs_initialized_p)
10073 {
10074 /* Get the frame containing the mini-buffer
10075 that the selected frame is using. */
10076 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10077 Lisp_Object frame = XWINDOW (mini_window)->frame;
10078 struct frame *f = XFRAME (frame);
10079
10080 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10081 Fmake_frame_visible (frame);
10082
10083 if (STRINGP (m) && SCHARS (m) > 0)
10084 {
10085 set_message (m);
10086 if (minibuffer_auto_raise)
10087 Fraise_frame (frame);
10088 /* Assume we are not echoing.
10089 (If we are, echo_now will override this.) */
10090 echo_message_buffer = Qnil;
10091 }
10092 else
10093 clear_message (true, true);
10094
10095 do_pending_window_change (false);
10096 echo_area_display (true);
10097 do_pending_window_change (false);
10098 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10099 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10100 }
10101 }
10102
10103
10104 /* Display a null-terminated echo area message M. If M is 0, clear
10105 out any existing message, and let the mini-buffer text show through.
10106
10107 The buffer M must continue to exist until after the echo area gets
10108 cleared or some other message gets displayed there. Do not pass
10109 text that is stored in a Lisp string. Do not pass text in a buffer
10110 that was alloca'd. */
10111
10112 void
10113 message1 (const char *m)
10114 {
10115 message3 (m ? build_unibyte_string (m) : Qnil);
10116 }
10117
10118
10119 /* The non-logging counterpart of message1. */
10120
10121 void
10122 message1_nolog (const char *m)
10123 {
10124 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10125 }
10126
10127 /* Display a message M which contains a single %s
10128 which gets replaced with STRING. */
10129
10130 void
10131 message_with_string (const char *m, Lisp_Object string, bool log)
10132 {
10133 CHECK_STRING (string);
10134
10135 if (noninteractive)
10136 {
10137 if (m)
10138 {
10139 /* ENCODE_SYSTEM below can GC and/or relocate the
10140 Lisp data, so make sure we don't use it here. */
10141 eassert (relocatable_string_data_p (m) != 1);
10142
10143 if (noninteractive_need_newline)
10144 putc ('\n', stderr);
10145 noninteractive_need_newline = false;
10146 fprintf (stderr, m, SDATA (ENCODE_SYSTEM (string)));
10147 if (!cursor_in_echo_area)
10148 fprintf (stderr, "\n");
10149 fflush (stderr);
10150 }
10151 }
10152 else if (INTERACTIVE)
10153 {
10154 /* The frame whose minibuffer we're going to display the message on.
10155 It may be larger than the selected frame, so we need
10156 to use its buffer, not the selected frame's buffer. */
10157 Lisp_Object mini_window;
10158 struct frame *f, *sf = SELECTED_FRAME ();
10159
10160 /* Get the frame containing the minibuffer
10161 that the selected frame is using. */
10162 mini_window = FRAME_MINIBUF_WINDOW (sf);
10163 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10164
10165 /* Error messages get reported properly by cmd_error, so this must be
10166 just an informative message; if the frame hasn't really been
10167 initialized yet, just toss it. */
10168 if (f->glyphs_initialized_p)
10169 {
10170 struct gcpro gcpro1, gcpro2;
10171
10172 Lisp_Object fmt = build_string (m);
10173 Lisp_Object msg = string;
10174 GCPRO2 (fmt, msg);
10175
10176 msg = CALLN (Fformat, fmt, msg);
10177
10178 if (log)
10179 message3 (msg);
10180 else
10181 message3_nolog (msg);
10182
10183 UNGCPRO;
10184
10185 /* Print should start at the beginning of the message
10186 buffer next time. */
10187 message_buf_print = false;
10188 }
10189 }
10190 }
10191
10192
10193 /* Dump an informative message to the minibuf. If M is 0, clear out
10194 any existing message, and let the mini-buffer text show through. */
10195
10196 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10197 vmessage (const char *m, va_list ap)
10198 {
10199 if (noninteractive)
10200 {
10201 if (m)
10202 {
10203 if (noninteractive_need_newline)
10204 putc ('\n', stderr);
10205 noninteractive_need_newline = false;
10206 vfprintf (stderr, m, ap);
10207 if (!cursor_in_echo_area)
10208 fprintf (stderr, "\n");
10209 fflush (stderr);
10210 }
10211 }
10212 else if (INTERACTIVE)
10213 {
10214 /* The frame whose mini-buffer we're going to display the message
10215 on. It may be larger than the selected frame, so we need to
10216 use its buffer, not the selected frame's buffer. */
10217 Lisp_Object mini_window;
10218 struct frame *f, *sf = SELECTED_FRAME ();
10219
10220 /* Get the frame containing the mini-buffer
10221 that the selected frame is using. */
10222 mini_window = FRAME_MINIBUF_WINDOW (sf);
10223 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10224
10225 /* Error messages get reported properly by cmd_error, so this must be
10226 just an informative message; if the frame hasn't really been
10227 initialized yet, just toss it. */
10228 if (f->glyphs_initialized_p)
10229 {
10230 if (m)
10231 {
10232 ptrdiff_t len;
10233 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10234 USE_SAFE_ALLOCA;
10235 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10236
10237 len = doprnt (message_buf, maxsize, m, 0, ap);
10238
10239 message3 (make_string (message_buf, len));
10240 SAFE_FREE ();
10241 }
10242 else
10243 message1 (0);
10244
10245 /* Print should start at the beginning of the message
10246 buffer next time. */
10247 message_buf_print = false;
10248 }
10249 }
10250 }
10251
10252 void
10253 message (const char *m, ...)
10254 {
10255 va_list ap;
10256 va_start (ap, m);
10257 vmessage (m, ap);
10258 va_end (ap);
10259 }
10260
10261
10262 #if false
10263 /* The non-logging version of message. */
10264
10265 void
10266 message_nolog (const char *m, ...)
10267 {
10268 Lisp_Object old_log_max;
10269 va_list ap;
10270 va_start (ap, m);
10271 old_log_max = Vmessage_log_max;
10272 Vmessage_log_max = Qnil;
10273 vmessage (m, ap);
10274 Vmessage_log_max = old_log_max;
10275 va_end (ap);
10276 }
10277 #endif
10278
10279
10280 /* Display the current message in the current mini-buffer. This is
10281 only called from error handlers in process.c, and is not time
10282 critical. */
10283
10284 void
10285 update_echo_area (void)
10286 {
10287 if (!NILP (echo_area_buffer[0]))
10288 {
10289 Lisp_Object string;
10290 string = Fcurrent_message ();
10291 message3 (string);
10292 }
10293 }
10294
10295
10296 /* Make sure echo area buffers in `echo_buffers' are live.
10297 If they aren't, make new ones. */
10298
10299 static void
10300 ensure_echo_area_buffers (void)
10301 {
10302 int i;
10303
10304 for (i = 0; i < 2; ++i)
10305 if (!BUFFERP (echo_buffer[i])
10306 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10307 {
10308 char name[30];
10309 Lisp_Object old_buffer;
10310 int j;
10311
10312 old_buffer = echo_buffer[i];
10313 echo_buffer[i] = Fget_buffer_create
10314 (make_formatted_string (name, " *Echo Area %d*", i));
10315 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10316 /* to force word wrap in echo area -
10317 it was decided to postpone this*/
10318 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10319
10320 for (j = 0; j < 2; ++j)
10321 if (EQ (old_buffer, echo_area_buffer[j]))
10322 echo_area_buffer[j] = echo_buffer[i];
10323 }
10324 }
10325
10326
10327 /* Call FN with args A1..A2 with either the current or last displayed
10328 echo_area_buffer as current buffer.
10329
10330 WHICH zero means use the current message buffer
10331 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10332 from echo_buffer[] and clear it.
10333
10334 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10335 suitable buffer from echo_buffer[] and clear it.
10336
10337 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10338 that the current message becomes the last displayed one, make
10339 choose a suitable buffer for echo_area_buffer[0], and clear it.
10340
10341 Value is what FN returns. */
10342
10343 static bool
10344 with_echo_area_buffer (struct window *w, int which,
10345 bool (*fn) (ptrdiff_t, Lisp_Object),
10346 ptrdiff_t a1, Lisp_Object a2)
10347 {
10348 Lisp_Object buffer;
10349 bool this_one, the_other, clear_buffer_p, rc;
10350 ptrdiff_t count = SPECPDL_INDEX ();
10351
10352 /* If buffers aren't live, make new ones. */
10353 ensure_echo_area_buffers ();
10354
10355 clear_buffer_p = false;
10356
10357 if (which == 0)
10358 this_one = false, the_other = true;
10359 else if (which > 0)
10360 this_one = true, the_other = false;
10361 else
10362 {
10363 this_one = false, the_other = true;
10364 clear_buffer_p = true;
10365
10366 /* We need a fresh one in case the current echo buffer equals
10367 the one containing the last displayed echo area message. */
10368 if (!NILP (echo_area_buffer[this_one])
10369 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10370 echo_area_buffer[this_one] = Qnil;
10371 }
10372
10373 /* Choose a suitable buffer from echo_buffer[] is we don't
10374 have one. */
10375 if (NILP (echo_area_buffer[this_one]))
10376 {
10377 echo_area_buffer[this_one]
10378 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10379 ? echo_buffer[the_other]
10380 : echo_buffer[this_one]);
10381 clear_buffer_p = true;
10382 }
10383
10384 buffer = echo_area_buffer[this_one];
10385
10386 /* Don't get confused by reusing the buffer used for echoing
10387 for a different purpose. */
10388 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10389 cancel_echoing ();
10390
10391 record_unwind_protect (unwind_with_echo_area_buffer,
10392 with_echo_area_buffer_unwind_data (w));
10393
10394 /* Make the echo area buffer current. Note that for display
10395 purposes, it is not necessary that the displayed window's buffer
10396 == current_buffer, except for text property lookup. So, let's
10397 only set that buffer temporarily here without doing a full
10398 Fset_window_buffer. We must also change w->pointm, though,
10399 because otherwise an assertions in unshow_buffer fails, and Emacs
10400 aborts. */
10401 set_buffer_internal_1 (XBUFFER (buffer));
10402 if (w)
10403 {
10404 wset_buffer (w, buffer);
10405 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10406 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10407 }
10408
10409 bset_undo_list (current_buffer, Qt);
10410 bset_read_only (current_buffer, Qnil);
10411 specbind (Qinhibit_read_only, Qt);
10412 specbind (Qinhibit_modification_hooks, Qt);
10413
10414 if (clear_buffer_p && Z > BEG)
10415 del_range (BEG, Z);
10416
10417 eassert (BEGV >= BEG);
10418 eassert (ZV <= Z && ZV >= BEGV);
10419
10420 rc = fn (a1, a2);
10421
10422 eassert (BEGV >= BEG);
10423 eassert (ZV <= Z && ZV >= BEGV);
10424
10425 unbind_to (count, Qnil);
10426 return rc;
10427 }
10428
10429
10430 /* Save state that should be preserved around the call to the function
10431 FN called in with_echo_area_buffer. */
10432
10433 static Lisp_Object
10434 with_echo_area_buffer_unwind_data (struct window *w)
10435 {
10436 int i = 0;
10437 Lisp_Object vector, tmp;
10438
10439 /* Reduce consing by keeping one vector in
10440 Vwith_echo_area_save_vector. */
10441 vector = Vwith_echo_area_save_vector;
10442 Vwith_echo_area_save_vector = Qnil;
10443
10444 if (NILP (vector))
10445 vector = Fmake_vector (make_number (11), Qnil);
10446
10447 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10448 ASET (vector, i, Vdeactivate_mark); ++i;
10449 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10450
10451 if (w)
10452 {
10453 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10454 ASET (vector, i, w->contents); ++i;
10455 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10456 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10457 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10458 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10459 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10460 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10461 }
10462 else
10463 {
10464 int end = i + 8;
10465 for (; i < end; ++i)
10466 ASET (vector, i, Qnil);
10467 }
10468
10469 eassert (i == ASIZE (vector));
10470 return vector;
10471 }
10472
10473
10474 /* Restore global state from VECTOR which was created by
10475 with_echo_area_buffer_unwind_data. */
10476
10477 static void
10478 unwind_with_echo_area_buffer (Lisp_Object vector)
10479 {
10480 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10481 Vdeactivate_mark = AREF (vector, 1);
10482 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10483
10484 if (WINDOWP (AREF (vector, 3)))
10485 {
10486 struct window *w;
10487 Lisp_Object buffer;
10488
10489 w = XWINDOW (AREF (vector, 3));
10490 buffer = AREF (vector, 4);
10491
10492 wset_buffer (w, buffer);
10493 set_marker_both (w->pointm, buffer,
10494 XFASTINT (AREF (vector, 5)),
10495 XFASTINT (AREF (vector, 6)));
10496 set_marker_both (w->old_pointm, buffer,
10497 XFASTINT (AREF (vector, 7)),
10498 XFASTINT (AREF (vector, 8)));
10499 set_marker_both (w->start, buffer,
10500 XFASTINT (AREF (vector, 9)),
10501 XFASTINT (AREF (vector, 10)));
10502 }
10503
10504 Vwith_echo_area_save_vector = vector;
10505 }
10506
10507
10508 /* Set up the echo area for use by print functions. MULTIBYTE_P
10509 means we will print multibyte. */
10510
10511 void
10512 setup_echo_area_for_printing (bool multibyte_p)
10513 {
10514 /* If we can't find an echo area any more, exit. */
10515 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10516 Fkill_emacs (Qnil);
10517
10518 ensure_echo_area_buffers ();
10519
10520 if (!message_buf_print)
10521 {
10522 /* A message has been output since the last time we printed.
10523 Choose a fresh echo area buffer. */
10524 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10525 echo_area_buffer[0] = echo_buffer[1];
10526 else
10527 echo_area_buffer[0] = echo_buffer[0];
10528
10529 /* Switch to that buffer and clear it. */
10530 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10531 bset_truncate_lines (current_buffer, Qnil);
10532
10533 if (Z > BEG)
10534 {
10535 ptrdiff_t count = SPECPDL_INDEX ();
10536 specbind (Qinhibit_read_only, Qt);
10537 /* Note that undo recording is always disabled. */
10538 del_range (BEG, Z);
10539 unbind_to (count, Qnil);
10540 }
10541 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10542
10543 /* Set up the buffer for the multibyteness we need. */
10544 if (multibyte_p
10545 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10546 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10547
10548 /* Raise the frame containing the echo area. */
10549 if (minibuffer_auto_raise)
10550 {
10551 struct frame *sf = SELECTED_FRAME ();
10552 Lisp_Object mini_window;
10553 mini_window = FRAME_MINIBUF_WINDOW (sf);
10554 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10555 }
10556
10557 message_log_maybe_newline ();
10558 message_buf_print = true;
10559 }
10560 else
10561 {
10562 if (NILP (echo_area_buffer[0]))
10563 {
10564 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10565 echo_area_buffer[0] = echo_buffer[1];
10566 else
10567 echo_area_buffer[0] = echo_buffer[0];
10568 }
10569
10570 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10571 {
10572 /* Someone switched buffers between print requests. */
10573 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10574 bset_truncate_lines (current_buffer, Qnil);
10575 }
10576 }
10577 }
10578
10579
10580 /* Display an echo area message in window W. Value is true if W's
10581 height is changed. If display_last_displayed_message_p,
10582 display the message that was last displayed, otherwise
10583 display the current message. */
10584
10585 static bool
10586 display_echo_area (struct window *w)
10587 {
10588 bool no_message_p, window_height_changed_p;
10589
10590 /* Temporarily disable garbage collections while displaying the echo
10591 area. This is done because a GC can print a message itself.
10592 That message would modify the echo area buffer's contents while a
10593 redisplay of the buffer is going on, and seriously confuse
10594 redisplay. */
10595 ptrdiff_t count = inhibit_garbage_collection ();
10596
10597 /* If there is no message, we must call display_echo_area_1
10598 nevertheless because it resizes the window. But we will have to
10599 reset the echo_area_buffer in question to nil at the end because
10600 with_echo_area_buffer will sets it to an empty buffer. */
10601 bool i = display_last_displayed_message_p;
10602 no_message_p = NILP (echo_area_buffer[i]);
10603
10604 window_height_changed_p
10605 = with_echo_area_buffer (w, display_last_displayed_message_p,
10606 display_echo_area_1,
10607 (intptr_t) w, Qnil);
10608
10609 if (no_message_p)
10610 echo_area_buffer[i] = Qnil;
10611
10612 unbind_to (count, Qnil);
10613 return window_height_changed_p;
10614 }
10615
10616
10617 /* Helper for display_echo_area. Display the current buffer which
10618 contains the current echo area message in window W, a mini-window,
10619 a pointer to which is passed in A1. A2..A4 are currently not used.
10620 Change the height of W so that all of the message is displayed.
10621 Value is true if height of W was changed. */
10622
10623 static bool
10624 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10625 {
10626 intptr_t i1 = a1;
10627 struct window *w = (struct window *) i1;
10628 Lisp_Object window;
10629 struct text_pos start;
10630
10631 /* Do this before displaying, so that we have a large enough glyph
10632 matrix for the display. If we can't get enough space for the
10633 whole text, display the last N lines. That works by setting w->start. */
10634 bool window_height_changed_p = resize_mini_window (w, false);
10635
10636 /* Use the starting position chosen by resize_mini_window. */
10637 SET_TEXT_POS_FROM_MARKER (start, w->start);
10638
10639 /* Display. */
10640 clear_glyph_matrix (w->desired_matrix);
10641 XSETWINDOW (window, w);
10642 try_window (window, start, 0);
10643
10644 return window_height_changed_p;
10645 }
10646
10647
10648 /* Resize the echo area window to exactly the size needed for the
10649 currently displayed message, if there is one. If a mini-buffer
10650 is active, don't shrink it. */
10651
10652 void
10653 resize_echo_area_exactly (void)
10654 {
10655 if (BUFFERP (echo_area_buffer[0])
10656 && WINDOWP (echo_area_window))
10657 {
10658 struct window *w = XWINDOW (echo_area_window);
10659 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10660 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10661 (intptr_t) w, resize_exactly);
10662 if (resized_p)
10663 {
10664 windows_or_buffers_changed = 42;
10665 update_mode_lines = 30;
10666 redisplay_internal ();
10667 }
10668 }
10669 }
10670
10671
10672 /* Callback function for with_echo_area_buffer, when used from
10673 resize_echo_area_exactly. A1 contains a pointer to the window to
10674 resize, EXACTLY non-nil means resize the mini-window exactly to the
10675 size of the text displayed. A3 and A4 are not used. Value is what
10676 resize_mini_window returns. */
10677
10678 static bool
10679 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10680 {
10681 intptr_t i1 = a1;
10682 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10683 }
10684
10685
10686 /* Resize mini-window W to fit the size of its contents. EXACT_P
10687 means size the window exactly to the size needed. Otherwise, it's
10688 only enlarged until W's buffer is empty.
10689
10690 Set W->start to the right place to begin display. If the whole
10691 contents fit, start at the beginning. Otherwise, start so as
10692 to make the end of the contents appear. This is particularly
10693 important for y-or-n-p, but seems desirable generally.
10694
10695 Value is true if the window height has been changed. */
10696
10697 bool
10698 resize_mini_window (struct window *w, bool exact_p)
10699 {
10700 struct frame *f = XFRAME (w->frame);
10701 bool window_height_changed_p = false;
10702
10703 eassert (MINI_WINDOW_P (w));
10704
10705 /* By default, start display at the beginning. */
10706 set_marker_both (w->start, w->contents,
10707 BUF_BEGV (XBUFFER (w->contents)),
10708 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10709
10710 /* Don't resize windows while redisplaying a window; it would
10711 confuse redisplay functions when the size of the window they are
10712 displaying changes from under them. Such a resizing can happen,
10713 for instance, when which-func prints a long message while
10714 we are running fontification-functions. We're running these
10715 functions with safe_call which binds inhibit-redisplay to t. */
10716 if (!NILP (Vinhibit_redisplay))
10717 return false;
10718
10719 /* Nil means don't try to resize. */
10720 if (NILP (Vresize_mini_windows)
10721 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10722 return false;
10723
10724 if (!FRAME_MINIBUF_ONLY_P (f))
10725 {
10726 struct it it;
10727 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10728 + WINDOW_PIXEL_HEIGHT (w));
10729 int unit = FRAME_LINE_HEIGHT (f);
10730 int height, max_height;
10731 struct text_pos start;
10732 struct buffer *old_current_buffer = NULL;
10733
10734 if (current_buffer != XBUFFER (w->contents))
10735 {
10736 old_current_buffer = current_buffer;
10737 set_buffer_internal (XBUFFER (w->contents));
10738 }
10739
10740 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10741
10742 /* Compute the max. number of lines specified by the user. */
10743 if (FLOATP (Vmax_mini_window_height))
10744 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10745 else if (INTEGERP (Vmax_mini_window_height))
10746 max_height = XINT (Vmax_mini_window_height) * unit;
10747 else
10748 max_height = total_height / 4;
10749
10750 /* Correct that max. height if it's bogus. */
10751 max_height = clip_to_bounds (unit, max_height, total_height);
10752
10753 /* Find out the height of the text in the window. */
10754 if (it.line_wrap == TRUNCATE)
10755 height = unit;
10756 else
10757 {
10758 last_height = 0;
10759 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10760 if (it.max_ascent == 0 && it.max_descent == 0)
10761 height = it.current_y + last_height;
10762 else
10763 height = it.current_y + it.max_ascent + it.max_descent;
10764 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10765 }
10766
10767 /* Compute a suitable window start. */
10768 if (height > max_height)
10769 {
10770 height = (max_height / unit) * unit;
10771 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10772 move_it_vertically_backward (&it, height - unit);
10773 start = it.current.pos;
10774 }
10775 else
10776 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10777 SET_MARKER_FROM_TEXT_POS (w->start, start);
10778
10779 if (EQ (Vresize_mini_windows, Qgrow_only))
10780 {
10781 /* Let it grow only, until we display an empty message, in which
10782 case the window shrinks again. */
10783 if (height > WINDOW_PIXEL_HEIGHT (w))
10784 {
10785 int old_height = WINDOW_PIXEL_HEIGHT (w);
10786
10787 FRAME_WINDOWS_FROZEN (f) = true;
10788 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10789 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10790 }
10791 else if (height < WINDOW_PIXEL_HEIGHT (w)
10792 && (exact_p || BEGV == ZV))
10793 {
10794 int old_height = WINDOW_PIXEL_HEIGHT (w);
10795
10796 FRAME_WINDOWS_FROZEN (f) = false;
10797 shrink_mini_window (w, true);
10798 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10799 }
10800 }
10801 else
10802 {
10803 /* Always resize to exact size needed. */
10804 if (height > WINDOW_PIXEL_HEIGHT (w))
10805 {
10806 int old_height = WINDOW_PIXEL_HEIGHT (w);
10807
10808 FRAME_WINDOWS_FROZEN (f) = true;
10809 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10810 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10811 }
10812 else if (height < WINDOW_PIXEL_HEIGHT (w))
10813 {
10814 int old_height = WINDOW_PIXEL_HEIGHT (w);
10815
10816 FRAME_WINDOWS_FROZEN (f) = false;
10817 shrink_mini_window (w, true);
10818
10819 if (height)
10820 {
10821 FRAME_WINDOWS_FROZEN (f) = true;
10822 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10823 }
10824
10825 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10826 }
10827 }
10828
10829 if (old_current_buffer)
10830 set_buffer_internal (old_current_buffer);
10831 }
10832
10833 return window_height_changed_p;
10834 }
10835
10836
10837 /* Value is the current message, a string, or nil if there is no
10838 current message. */
10839
10840 Lisp_Object
10841 current_message (void)
10842 {
10843 Lisp_Object msg;
10844
10845 if (!BUFFERP (echo_area_buffer[0]))
10846 msg = Qnil;
10847 else
10848 {
10849 with_echo_area_buffer (0, 0, current_message_1,
10850 (intptr_t) &msg, Qnil);
10851 if (NILP (msg))
10852 echo_area_buffer[0] = Qnil;
10853 }
10854
10855 return msg;
10856 }
10857
10858
10859 static bool
10860 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10861 {
10862 intptr_t i1 = a1;
10863 Lisp_Object *msg = (Lisp_Object *) i1;
10864
10865 if (Z > BEG)
10866 *msg = make_buffer_string (BEG, Z, true);
10867 else
10868 *msg = Qnil;
10869 return false;
10870 }
10871
10872
10873 /* Push the current message on Vmessage_stack for later restoration
10874 by restore_message. Value is true if the current message isn't
10875 empty. This is a relatively infrequent operation, so it's not
10876 worth optimizing. */
10877
10878 bool
10879 push_message (void)
10880 {
10881 Lisp_Object msg = current_message ();
10882 Vmessage_stack = Fcons (msg, Vmessage_stack);
10883 return STRINGP (msg);
10884 }
10885
10886
10887 /* Restore message display from the top of Vmessage_stack. */
10888
10889 void
10890 restore_message (void)
10891 {
10892 eassert (CONSP (Vmessage_stack));
10893 message3_nolog (XCAR (Vmessage_stack));
10894 }
10895
10896
10897 /* Handler for unwind-protect calling pop_message. */
10898
10899 void
10900 pop_message_unwind (void)
10901 {
10902 /* Pop the top-most entry off Vmessage_stack. */
10903 eassert (CONSP (Vmessage_stack));
10904 Vmessage_stack = XCDR (Vmessage_stack);
10905 }
10906
10907
10908 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10909 exits. If the stack is not empty, we have a missing pop_message
10910 somewhere. */
10911
10912 void
10913 check_message_stack (void)
10914 {
10915 if (!NILP (Vmessage_stack))
10916 emacs_abort ();
10917 }
10918
10919
10920 /* Truncate to NCHARS what will be displayed in the echo area the next
10921 time we display it---but don't redisplay it now. */
10922
10923 void
10924 truncate_echo_area (ptrdiff_t nchars)
10925 {
10926 if (nchars == 0)
10927 echo_area_buffer[0] = Qnil;
10928 else if (!noninteractive
10929 && INTERACTIVE
10930 && !NILP (echo_area_buffer[0]))
10931 {
10932 struct frame *sf = SELECTED_FRAME ();
10933 /* Error messages get reported properly by cmd_error, so this must be
10934 just an informative message; if the frame hasn't really been
10935 initialized yet, just toss it. */
10936 if (sf->glyphs_initialized_p)
10937 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10938 }
10939 }
10940
10941
10942 /* Helper function for truncate_echo_area. Truncate the current
10943 message to at most NCHARS characters. */
10944
10945 static bool
10946 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10947 {
10948 if (BEG + nchars < Z)
10949 del_range (BEG + nchars, Z);
10950 if (Z == BEG)
10951 echo_area_buffer[0] = Qnil;
10952 return false;
10953 }
10954
10955 /* Set the current message to STRING. */
10956
10957 static void
10958 set_message (Lisp_Object string)
10959 {
10960 eassert (STRINGP (string));
10961
10962 message_enable_multibyte = STRING_MULTIBYTE (string);
10963
10964 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10965 message_buf_print = false;
10966 help_echo_showing_p = false;
10967
10968 if (STRINGP (Vdebug_on_message)
10969 && STRINGP (string)
10970 && fast_string_match (Vdebug_on_message, string) >= 0)
10971 call_debugger (list2 (Qerror, string));
10972 }
10973
10974
10975 /* Helper function for set_message. First argument is ignored and second
10976 argument has the same meaning as for set_message.
10977 This function is called with the echo area buffer being current. */
10978
10979 static bool
10980 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10981 {
10982 eassert (STRINGP (string));
10983
10984 /* Change multibyteness of the echo buffer appropriately. */
10985 if (message_enable_multibyte
10986 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10987 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10988
10989 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10990 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10991 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10992
10993 /* Insert new message at BEG. */
10994 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10995
10996 /* This function takes care of single/multibyte conversion.
10997 We just have to ensure that the echo area buffer has the right
10998 setting of enable_multibyte_characters. */
10999 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11000
11001 return false;
11002 }
11003
11004
11005 /* Clear messages. CURRENT_P means clear the current message.
11006 LAST_DISPLAYED_P means clear the message last displayed. */
11007
11008 void
11009 clear_message (bool current_p, bool last_displayed_p)
11010 {
11011 if (current_p)
11012 {
11013 echo_area_buffer[0] = Qnil;
11014 message_cleared_p = true;
11015 }
11016
11017 if (last_displayed_p)
11018 echo_area_buffer[1] = Qnil;
11019
11020 message_buf_print = false;
11021 }
11022
11023 /* Clear garbaged frames.
11024
11025 This function is used where the old redisplay called
11026 redraw_garbaged_frames which in turn called redraw_frame which in
11027 turn called clear_frame. The call to clear_frame was a source of
11028 flickering. I believe a clear_frame is not necessary. It should
11029 suffice in the new redisplay to invalidate all current matrices,
11030 and ensure a complete redisplay of all windows. */
11031
11032 static void
11033 clear_garbaged_frames (void)
11034 {
11035 if (frame_garbaged)
11036 {
11037 Lisp_Object tail, frame;
11038
11039 FOR_EACH_FRAME (tail, frame)
11040 {
11041 struct frame *f = XFRAME (frame);
11042
11043 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11044 {
11045 if (f->resized_p)
11046 redraw_frame (f);
11047 else
11048 clear_current_matrices (f);
11049 fset_redisplay (f);
11050 f->garbaged = false;
11051 f->resized_p = false;
11052 }
11053 }
11054
11055 frame_garbaged = false;
11056 }
11057 }
11058
11059
11060 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P,
11061 update selected_frame. Value is true if the mini-windows height
11062 has been changed. */
11063
11064 static bool
11065 echo_area_display (bool update_frame_p)
11066 {
11067 Lisp_Object mini_window;
11068 struct window *w;
11069 struct frame *f;
11070 bool window_height_changed_p = false;
11071 struct frame *sf = SELECTED_FRAME ();
11072
11073 mini_window = FRAME_MINIBUF_WINDOW (sf);
11074 w = XWINDOW (mini_window);
11075 f = XFRAME (WINDOW_FRAME (w));
11076
11077 /* Don't display if frame is invisible or not yet initialized. */
11078 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11079 return false;
11080
11081 #ifdef HAVE_WINDOW_SYSTEM
11082 /* When Emacs starts, selected_frame may be the initial terminal
11083 frame. If we let this through, a message would be displayed on
11084 the terminal. */
11085 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11086 return false;
11087 #endif /* HAVE_WINDOW_SYSTEM */
11088
11089 /* Redraw garbaged frames. */
11090 clear_garbaged_frames ();
11091
11092 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11093 {
11094 echo_area_window = mini_window;
11095 window_height_changed_p = display_echo_area (w);
11096 w->must_be_updated_p = true;
11097
11098 /* Update the display, unless called from redisplay_internal.
11099 Also don't update the screen during redisplay itself. The
11100 update will happen at the end of redisplay, and an update
11101 here could cause confusion. */
11102 if (update_frame_p && !redisplaying_p)
11103 {
11104 int n = 0;
11105
11106 /* If the display update has been interrupted by pending
11107 input, update mode lines in the frame. Due to the
11108 pending input, it might have been that redisplay hasn't
11109 been called, so that mode lines above the echo area are
11110 garbaged. This looks odd, so we prevent it here. */
11111 if (!display_completed)
11112 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11113
11114 if (window_height_changed_p
11115 /* Don't do this if Emacs is shutting down. Redisplay
11116 needs to run hooks. */
11117 && !NILP (Vrun_hooks))
11118 {
11119 /* Must update other windows. Likewise as in other
11120 cases, don't let this update be interrupted by
11121 pending input. */
11122 ptrdiff_t count = SPECPDL_INDEX ();
11123 specbind (Qredisplay_dont_pause, Qt);
11124 windows_or_buffers_changed = 44;
11125 redisplay_internal ();
11126 unbind_to (count, Qnil);
11127 }
11128 else if (FRAME_WINDOW_P (f) && n == 0)
11129 {
11130 /* Window configuration is the same as before.
11131 Can do with a display update of the echo area,
11132 unless we displayed some mode lines. */
11133 update_single_window (w);
11134 flush_frame (f);
11135 }
11136 else
11137 update_frame (f, true, true);
11138
11139 /* If cursor is in the echo area, make sure that the next
11140 redisplay displays the minibuffer, so that the cursor will
11141 be replaced with what the minibuffer wants. */
11142 if (cursor_in_echo_area)
11143 wset_redisplay (XWINDOW (mini_window));
11144 }
11145 }
11146 else if (!EQ (mini_window, selected_window))
11147 wset_redisplay (XWINDOW (mini_window));
11148
11149 /* Last displayed message is now the current message. */
11150 echo_area_buffer[1] = echo_area_buffer[0];
11151 /* Inform read_char that we're not echoing. */
11152 echo_message_buffer = Qnil;
11153
11154 /* Prevent redisplay optimization in redisplay_internal by resetting
11155 this_line_start_pos. This is done because the mini-buffer now
11156 displays the message instead of its buffer text. */
11157 if (EQ (mini_window, selected_window))
11158 CHARPOS (this_line_start_pos) = 0;
11159
11160 return window_height_changed_p;
11161 }
11162
11163 /* True if W's buffer was changed but not saved. */
11164
11165 static bool
11166 window_buffer_changed (struct window *w)
11167 {
11168 struct buffer *b = XBUFFER (w->contents);
11169
11170 eassert (BUFFER_LIVE_P (b));
11171
11172 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11173 }
11174
11175 /* True if W has %c in its mode line and mode line should be updated. */
11176
11177 static bool
11178 mode_line_update_needed (struct window *w)
11179 {
11180 return (w->column_number_displayed != -1
11181 && !(PT == w->last_point && !window_outdated (w))
11182 && (w->column_number_displayed != current_column ()));
11183 }
11184
11185 /* True if window start of W is frozen and may not be changed during
11186 redisplay. */
11187
11188 static bool
11189 window_frozen_p (struct window *w)
11190 {
11191 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11192 {
11193 Lisp_Object window;
11194
11195 XSETWINDOW (window, w);
11196 if (MINI_WINDOW_P (w))
11197 return false;
11198 else if (EQ (window, selected_window))
11199 return false;
11200 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11201 && EQ (window, Vminibuf_scroll_window))
11202 /* This special window can't be frozen too. */
11203 return false;
11204 else
11205 return true;
11206 }
11207 return false;
11208 }
11209
11210 /***********************************************************************
11211 Mode Lines and Frame Titles
11212 ***********************************************************************/
11213
11214 /* A buffer for constructing non-propertized mode-line strings and
11215 frame titles in it; allocated from the heap in init_xdisp and
11216 resized as needed in store_mode_line_noprop_char. */
11217
11218 static char *mode_line_noprop_buf;
11219
11220 /* The buffer's end, and a current output position in it. */
11221
11222 static char *mode_line_noprop_buf_end;
11223 static char *mode_line_noprop_ptr;
11224
11225 #define MODE_LINE_NOPROP_LEN(start) \
11226 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11227
11228 static enum {
11229 MODE_LINE_DISPLAY = 0,
11230 MODE_LINE_TITLE,
11231 MODE_LINE_NOPROP,
11232 MODE_LINE_STRING
11233 } mode_line_target;
11234
11235 /* Alist that caches the results of :propertize.
11236 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11237 static Lisp_Object mode_line_proptrans_alist;
11238
11239 /* List of strings making up the mode-line. */
11240 static Lisp_Object mode_line_string_list;
11241
11242 /* Base face property when building propertized mode line string. */
11243 static Lisp_Object mode_line_string_face;
11244 static Lisp_Object mode_line_string_face_prop;
11245
11246
11247 /* Unwind data for mode line strings */
11248
11249 static Lisp_Object Vmode_line_unwind_vector;
11250
11251 static Lisp_Object
11252 format_mode_line_unwind_data (struct frame *target_frame,
11253 struct buffer *obuf,
11254 Lisp_Object owin,
11255 bool save_proptrans)
11256 {
11257 Lisp_Object vector, tmp;
11258
11259 /* Reduce consing by keeping one vector in
11260 Vwith_echo_area_save_vector. */
11261 vector = Vmode_line_unwind_vector;
11262 Vmode_line_unwind_vector = Qnil;
11263
11264 if (NILP (vector))
11265 vector = Fmake_vector (make_number (10), Qnil);
11266
11267 ASET (vector, 0, make_number (mode_line_target));
11268 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11269 ASET (vector, 2, mode_line_string_list);
11270 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11271 ASET (vector, 4, mode_line_string_face);
11272 ASET (vector, 5, mode_line_string_face_prop);
11273
11274 if (obuf)
11275 XSETBUFFER (tmp, obuf);
11276 else
11277 tmp = Qnil;
11278 ASET (vector, 6, tmp);
11279 ASET (vector, 7, owin);
11280 if (target_frame)
11281 {
11282 /* Similarly to `with-selected-window', if the operation selects
11283 a window on another frame, we must restore that frame's
11284 selected window, and (for a tty) the top-frame. */
11285 ASET (vector, 8, target_frame->selected_window);
11286 if (FRAME_TERMCAP_P (target_frame))
11287 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11288 }
11289
11290 return vector;
11291 }
11292
11293 static void
11294 unwind_format_mode_line (Lisp_Object vector)
11295 {
11296 Lisp_Object old_window = AREF (vector, 7);
11297 Lisp_Object target_frame_window = AREF (vector, 8);
11298 Lisp_Object old_top_frame = AREF (vector, 9);
11299
11300 mode_line_target = XINT (AREF (vector, 0));
11301 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11302 mode_line_string_list = AREF (vector, 2);
11303 if (! EQ (AREF (vector, 3), Qt))
11304 mode_line_proptrans_alist = AREF (vector, 3);
11305 mode_line_string_face = AREF (vector, 4);
11306 mode_line_string_face_prop = AREF (vector, 5);
11307
11308 /* Select window before buffer, since it may change the buffer. */
11309 if (!NILP (old_window))
11310 {
11311 /* If the operation that we are unwinding had selected a window
11312 on a different frame, reset its frame-selected-window. For a
11313 text terminal, reset its top-frame if necessary. */
11314 if (!NILP (target_frame_window))
11315 {
11316 Lisp_Object frame
11317 = WINDOW_FRAME (XWINDOW (target_frame_window));
11318
11319 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11320 Fselect_window (target_frame_window, Qt);
11321
11322 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11323 Fselect_frame (old_top_frame, Qt);
11324 }
11325
11326 Fselect_window (old_window, Qt);
11327 }
11328
11329 if (!NILP (AREF (vector, 6)))
11330 {
11331 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11332 ASET (vector, 6, Qnil);
11333 }
11334
11335 Vmode_line_unwind_vector = vector;
11336 }
11337
11338
11339 /* Store a single character C for the frame title in mode_line_noprop_buf.
11340 Re-allocate mode_line_noprop_buf if necessary. */
11341
11342 static void
11343 store_mode_line_noprop_char (char c)
11344 {
11345 /* If output position has reached the end of the allocated buffer,
11346 increase the buffer's size. */
11347 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11348 {
11349 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11350 ptrdiff_t size = len;
11351 mode_line_noprop_buf =
11352 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11353 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11354 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11355 }
11356
11357 *mode_line_noprop_ptr++ = c;
11358 }
11359
11360
11361 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11362 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11363 characters that yield more columns than PRECISION; PRECISION <= 0
11364 means copy the whole string. Pad with spaces until FIELD_WIDTH
11365 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11366 pad. Called from display_mode_element when it is used to build a
11367 frame title. */
11368
11369 static int
11370 store_mode_line_noprop (const char *string, int field_width, int precision)
11371 {
11372 const unsigned char *str = (const unsigned char *) string;
11373 int n = 0;
11374 ptrdiff_t dummy, nbytes;
11375
11376 /* Copy at most PRECISION chars from STR. */
11377 nbytes = strlen (string);
11378 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11379 while (nbytes--)
11380 store_mode_line_noprop_char (*str++);
11381
11382 /* Fill up with spaces until FIELD_WIDTH reached. */
11383 while (field_width > 0
11384 && n < field_width)
11385 {
11386 store_mode_line_noprop_char (' ');
11387 ++n;
11388 }
11389
11390 return n;
11391 }
11392
11393 /***********************************************************************
11394 Frame Titles
11395 ***********************************************************************/
11396
11397 #ifdef HAVE_WINDOW_SYSTEM
11398
11399 /* Set the title of FRAME, if it has changed. The title format is
11400 Vicon_title_format if FRAME is iconified, otherwise it is
11401 frame_title_format. */
11402
11403 static void
11404 x_consider_frame_title (Lisp_Object frame)
11405 {
11406 struct frame *f = XFRAME (frame);
11407
11408 if (FRAME_WINDOW_P (f)
11409 || FRAME_MINIBUF_ONLY_P (f)
11410 || f->explicit_name)
11411 {
11412 /* Do we have more than one visible frame on this X display? */
11413 Lisp_Object tail, other_frame, fmt;
11414 ptrdiff_t title_start;
11415 char *title;
11416 ptrdiff_t len;
11417 struct it it;
11418 ptrdiff_t count = SPECPDL_INDEX ();
11419
11420 FOR_EACH_FRAME (tail, other_frame)
11421 {
11422 struct frame *tf = XFRAME (other_frame);
11423
11424 if (tf != f
11425 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11426 && !FRAME_MINIBUF_ONLY_P (tf)
11427 && !EQ (other_frame, tip_frame)
11428 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11429 break;
11430 }
11431
11432 /* Set global variable indicating that multiple frames exist. */
11433 multiple_frames = CONSP (tail);
11434
11435 /* Switch to the buffer of selected window of the frame. Set up
11436 mode_line_target so that display_mode_element will output into
11437 mode_line_noprop_buf; then display the title. */
11438 record_unwind_protect (unwind_format_mode_line,
11439 format_mode_line_unwind_data
11440 (f, current_buffer, selected_window, false));
11441
11442 Fselect_window (f->selected_window, Qt);
11443 set_buffer_internal_1
11444 (XBUFFER (XWINDOW (f->selected_window)->contents));
11445 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11446
11447 mode_line_target = MODE_LINE_TITLE;
11448 title_start = MODE_LINE_NOPROP_LEN (0);
11449 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11450 NULL, DEFAULT_FACE_ID);
11451 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11452 len = MODE_LINE_NOPROP_LEN (title_start);
11453 title = mode_line_noprop_buf + title_start;
11454 unbind_to (count, Qnil);
11455
11456 /* Set the title only if it's changed. This avoids consing in
11457 the common case where it hasn't. (If it turns out that we've
11458 already wasted too much time by walking through the list with
11459 display_mode_element, then we might need to optimize at a
11460 higher level than this.) */
11461 if (! STRINGP (f->name)
11462 || SBYTES (f->name) != len
11463 || memcmp (title, SDATA (f->name), len) != 0)
11464 x_implicitly_set_name (f, make_string (title, len), Qnil);
11465 }
11466 }
11467
11468 #endif /* not HAVE_WINDOW_SYSTEM */
11469
11470 \f
11471 /***********************************************************************
11472 Menu Bars
11473 ***********************************************************************/
11474
11475 /* True if we will not redisplay all visible windows. */
11476 #define REDISPLAY_SOME_P() \
11477 ((windows_or_buffers_changed == 0 \
11478 || windows_or_buffers_changed == REDISPLAY_SOME) \
11479 && (update_mode_lines == 0 \
11480 || update_mode_lines == REDISPLAY_SOME))
11481
11482 /* Prepare for redisplay by updating menu-bar item lists when
11483 appropriate. This can call eval. */
11484
11485 static void
11486 prepare_menu_bars (void)
11487 {
11488 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11489 bool some_windows = REDISPLAY_SOME_P ();
11490 struct gcpro gcpro1, gcpro2;
11491 Lisp_Object tooltip_frame;
11492
11493 #ifdef HAVE_WINDOW_SYSTEM
11494 tooltip_frame = tip_frame;
11495 #else
11496 tooltip_frame = Qnil;
11497 #endif
11498
11499 if (FUNCTIONP (Vpre_redisplay_function))
11500 {
11501 Lisp_Object windows = all_windows ? Qt : Qnil;
11502 if (all_windows && some_windows)
11503 {
11504 Lisp_Object ws = window_list ();
11505 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11506 {
11507 Lisp_Object this = XCAR (ws);
11508 struct window *w = XWINDOW (this);
11509 if (w->redisplay
11510 || XFRAME (w->frame)->redisplay
11511 || XBUFFER (w->contents)->text->redisplay)
11512 {
11513 windows = Fcons (this, windows);
11514 }
11515 }
11516 }
11517 safe__call1 (true, Vpre_redisplay_function, windows);
11518 }
11519
11520 /* Update all frame titles based on their buffer names, etc. We do
11521 this before the menu bars so that the buffer-menu will show the
11522 up-to-date frame titles. */
11523 #ifdef HAVE_WINDOW_SYSTEM
11524 if (all_windows)
11525 {
11526 Lisp_Object tail, frame;
11527
11528 FOR_EACH_FRAME (tail, frame)
11529 {
11530 struct frame *f = XFRAME (frame);
11531 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11532 if (some_windows
11533 && !f->redisplay
11534 && !w->redisplay
11535 && !XBUFFER (w->contents)->text->redisplay)
11536 continue;
11537
11538 if (!EQ (frame, tooltip_frame)
11539 && (FRAME_ICONIFIED_P (f)
11540 || FRAME_VISIBLE_P (f) == 1
11541 /* Exclude TTY frames that are obscured because they
11542 are not the top frame on their console. This is
11543 because x_consider_frame_title actually switches
11544 to the frame, which for TTY frames means it is
11545 marked as garbaged, and will be completely
11546 redrawn on the next redisplay cycle. This causes
11547 TTY frames to be completely redrawn, when there
11548 are more than one of them, even though nothing
11549 should be changed on display. */
11550 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11551 x_consider_frame_title (frame);
11552 }
11553 }
11554 #endif /* HAVE_WINDOW_SYSTEM */
11555
11556 /* Update the menu bar item lists, if appropriate. This has to be
11557 done before any actual redisplay or generation of display lines. */
11558
11559 if (all_windows)
11560 {
11561 Lisp_Object tail, frame;
11562 ptrdiff_t count = SPECPDL_INDEX ();
11563 /* True means that update_menu_bar has run its hooks
11564 so any further calls to update_menu_bar shouldn't do so again. */
11565 bool menu_bar_hooks_run = false;
11566
11567 record_unwind_save_match_data ();
11568
11569 FOR_EACH_FRAME (tail, frame)
11570 {
11571 struct frame *f = XFRAME (frame);
11572 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11573
11574 /* Ignore tooltip frame. */
11575 if (EQ (frame, tooltip_frame))
11576 continue;
11577
11578 if (some_windows
11579 && !f->redisplay
11580 && !w->redisplay
11581 && !XBUFFER (w->contents)->text->redisplay)
11582 continue;
11583
11584 /* If a window on this frame changed size, report that to
11585 the user and clear the size-change flag. */
11586 if (FRAME_WINDOW_SIZES_CHANGED (f))
11587 {
11588 Lisp_Object functions;
11589
11590 /* Clear flag first in case we get an error below. */
11591 FRAME_WINDOW_SIZES_CHANGED (f) = false;
11592 functions = Vwindow_size_change_functions;
11593 GCPRO2 (tail, functions);
11594
11595 while (CONSP (functions))
11596 {
11597 if (!EQ (XCAR (functions), Qt))
11598 call1 (XCAR (functions), frame);
11599 functions = XCDR (functions);
11600 }
11601 UNGCPRO;
11602 }
11603
11604 GCPRO1 (tail);
11605 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11606 #ifdef HAVE_WINDOW_SYSTEM
11607 update_tool_bar (f, false);
11608 #endif
11609 UNGCPRO;
11610 }
11611
11612 unbind_to (count, Qnil);
11613 }
11614 else
11615 {
11616 struct frame *sf = SELECTED_FRAME ();
11617 update_menu_bar (sf, true, false);
11618 #ifdef HAVE_WINDOW_SYSTEM
11619 update_tool_bar (sf, true);
11620 #endif
11621 }
11622 }
11623
11624
11625 /* Update the menu bar item list for frame F. This has to be done
11626 before we start to fill in any display lines, because it can call
11627 eval.
11628
11629 If SAVE_MATCH_DATA, we must save and restore it here.
11630
11631 If HOOKS_RUN, a previous call to update_menu_bar
11632 already ran the menu bar hooks for this redisplay, so there
11633 is no need to run them again. The return value is the
11634 updated value of this flag, to pass to the next call. */
11635
11636 static bool
11637 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11638 {
11639 Lisp_Object window;
11640 struct window *w;
11641
11642 /* If called recursively during a menu update, do nothing. This can
11643 happen when, for instance, an activate-menubar-hook causes a
11644 redisplay. */
11645 if (inhibit_menubar_update)
11646 return hooks_run;
11647
11648 window = FRAME_SELECTED_WINDOW (f);
11649 w = XWINDOW (window);
11650
11651 if (FRAME_WINDOW_P (f)
11652 ?
11653 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11654 || defined (HAVE_NS) || defined (USE_GTK)
11655 FRAME_EXTERNAL_MENU_BAR (f)
11656 #else
11657 FRAME_MENU_BAR_LINES (f) > 0
11658 #endif
11659 : FRAME_MENU_BAR_LINES (f) > 0)
11660 {
11661 /* If the user has switched buffers or windows, we need to
11662 recompute to reflect the new bindings. But we'll
11663 recompute when update_mode_lines is set too; that means
11664 that people can use force-mode-line-update to request
11665 that the menu bar be recomputed. The adverse effect on
11666 the rest of the redisplay algorithm is about the same as
11667 windows_or_buffers_changed anyway. */
11668 if (windows_or_buffers_changed
11669 /* This used to test w->update_mode_line, but we believe
11670 there is no need to recompute the menu in that case. */
11671 || update_mode_lines
11672 || window_buffer_changed (w))
11673 {
11674 struct buffer *prev = current_buffer;
11675 ptrdiff_t count = SPECPDL_INDEX ();
11676
11677 specbind (Qinhibit_menubar_update, Qt);
11678
11679 set_buffer_internal_1 (XBUFFER (w->contents));
11680 if (save_match_data)
11681 record_unwind_save_match_data ();
11682 if (NILP (Voverriding_local_map_menu_flag))
11683 {
11684 specbind (Qoverriding_terminal_local_map, Qnil);
11685 specbind (Qoverriding_local_map, Qnil);
11686 }
11687
11688 if (!hooks_run)
11689 {
11690 /* Run the Lucid hook. */
11691 safe_run_hooks (Qactivate_menubar_hook);
11692
11693 /* If it has changed current-menubar from previous value,
11694 really recompute the menu-bar from the value. */
11695 if (! NILP (Vlucid_menu_bar_dirty_flag))
11696 call0 (Qrecompute_lucid_menubar);
11697
11698 safe_run_hooks (Qmenu_bar_update_hook);
11699
11700 hooks_run = true;
11701 }
11702
11703 XSETFRAME (Vmenu_updating_frame, f);
11704 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11705
11706 /* Redisplay the menu bar in case we changed it. */
11707 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11708 || defined (HAVE_NS) || defined (USE_GTK)
11709 if (FRAME_WINDOW_P (f))
11710 {
11711 #if defined (HAVE_NS)
11712 /* All frames on Mac OS share the same menubar. So only
11713 the selected frame should be allowed to set it. */
11714 if (f == SELECTED_FRAME ())
11715 #endif
11716 set_frame_menubar (f, false, false);
11717 }
11718 else
11719 /* On a terminal screen, the menu bar is an ordinary screen
11720 line, and this makes it get updated. */
11721 w->update_mode_line = true;
11722 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11723 /* In the non-toolkit version, the menu bar is an ordinary screen
11724 line, and this makes it get updated. */
11725 w->update_mode_line = true;
11726 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11727
11728 unbind_to (count, Qnil);
11729 set_buffer_internal_1 (prev);
11730 }
11731 }
11732
11733 return hooks_run;
11734 }
11735
11736 /***********************************************************************
11737 Tool-bars
11738 ***********************************************************************/
11739
11740 #ifdef HAVE_WINDOW_SYSTEM
11741
11742 /* Select `frame' temporarily without running all the code in
11743 do_switch_frame.
11744 FIXME: Maybe do_switch_frame should be trimmed down similarly
11745 when `norecord' is set. */
11746 static void
11747 fast_set_selected_frame (Lisp_Object frame)
11748 {
11749 if (!EQ (selected_frame, frame))
11750 {
11751 selected_frame = frame;
11752 selected_window = XFRAME (frame)->selected_window;
11753 }
11754 }
11755
11756 /* Update the tool-bar item list for frame F. This has to be done
11757 before we start to fill in any display lines. Called from
11758 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
11759 and restore it here. */
11760
11761 static void
11762 update_tool_bar (struct frame *f, bool save_match_data)
11763 {
11764 #if defined (USE_GTK) || defined (HAVE_NS)
11765 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11766 #else
11767 bool do_update = (WINDOWP (f->tool_bar_window)
11768 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11769 #endif
11770
11771 if (do_update)
11772 {
11773 Lisp_Object window;
11774 struct window *w;
11775
11776 window = FRAME_SELECTED_WINDOW (f);
11777 w = XWINDOW (window);
11778
11779 /* If the user has switched buffers or windows, we need to
11780 recompute to reflect the new bindings. But we'll
11781 recompute when update_mode_lines is set too; that means
11782 that people can use force-mode-line-update to request
11783 that the menu bar be recomputed. The adverse effect on
11784 the rest of the redisplay algorithm is about the same as
11785 windows_or_buffers_changed anyway. */
11786 if (windows_or_buffers_changed
11787 || w->update_mode_line
11788 || update_mode_lines
11789 || window_buffer_changed (w))
11790 {
11791 struct buffer *prev = current_buffer;
11792 ptrdiff_t count = SPECPDL_INDEX ();
11793 Lisp_Object frame, new_tool_bar;
11794 int new_n_tool_bar;
11795 struct gcpro gcpro1;
11796
11797 /* Set current_buffer to the buffer of the selected
11798 window of the frame, so that we get the right local
11799 keymaps. */
11800 set_buffer_internal_1 (XBUFFER (w->contents));
11801
11802 /* Save match data, if we must. */
11803 if (save_match_data)
11804 record_unwind_save_match_data ();
11805
11806 /* Make sure that we don't accidentally use bogus keymaps. */
11807 if (NILP (Voverriding_local_map_menu_flag))
11808 {
11809 specbind (Qoverriding_terminal_local_map, Qnil);
11810 specbind (Qoverriding_local_map, Qnil);
11811 }
11812
11813 GCPRO1 (new_tool_bar);
11814
11815 /* We must temporarily set the selected frame to this frame
11816 before calling tool_bar_items, because the calculation of
11817 the tool-bar keymap uses the selected frame (see
11818 `tool-bar-make-keymap' in tool-bar.el). */
11819 eassert (EQ (selected_window,
11820 /* Since we only explicitly preserve selected_frame,
11821 check that selected_window would be redundant. */
11822 XFRAME (selected_frame)->selected_window));
11823 record_unwind_protect (fast_set_selected_frame, selected_frame);
11824 XSETFRAME (frame, f);
11825 fast_set_selected_frame (frame);
11826
11827 /* Build desired tool-bar items from keymaps. */
11828 new_tool_bar
11829 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11830 &new_n_tool_bar);
11831
11832 /* Redisplay the tool-bar if we changed it. */
11833 if (new_n_tool_bar != f->n_tool_bar_items
11834 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11835 {
11836 /* Redisplay that happens asynchronously due to an expose event
11837 may access f->tool_bar_items. Make sure we update both
11838 variables within BLOCK_INPUT so no such event interrupts. */
11839 block_input ();
11840 fset_tool_bar_items (f, new_tool_bar);
11841 f->n_tool_bar_items = new_n_tool_bar;
11842 w->update_mode_line = true;
11843 unblock_input ();
11844 }
11845
11846 UNGCPRO;
11847
11848 unbind_to (count, Qnil);
11849 set_buffer_internal_1 (prev);
11850 }
11851 }
11852 }
11853
11854 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11855
11856 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11857 F's desired tool-bar contents. F->tool_bar_items must have
11858 been set up previously by calling prepare_menu_bars. */
11859
11860 static void
11861 build_desired_tool_bar_string (struct frame *f)
11862 {
11863 int i, size, size_needed;
11864 struct gcpro gcpro1, gcpro2;
11865 Lisp_Object image, plist;
11866
11867 image = plist = Qnil;
11868 GCPRO2 (image, plist);
11869
11870 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11871 Otherwise, make a new string. */
11872
11873 /* The size of the string we might be able to reuse. */
11874 size = (STRINGP (f->desired_tool_bar_string)
11875 ? SCHARS (f->desired_tool_bar_string)
11876 : 0);
11877
11878 /* We need one space in the string for each image. */
11879 size_needed = f->n_tool_bar_items;
11880
11881 /* Reuse f->desired_tool_bar_string, if possible. */
11882 if (size < size_needed || NILP (f->desired_tool_bar_string))
11883 fset_desired_tool_bar_string
11884 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11885 else
11886 {
11887 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
11888 struct gcpro gcpro1;
11889 GCPRO1 (props);
11890 Fremove_text_properties (make_number (0), make_number (size),
11891 props, f->desired_tool_bar_string);
11892 UNGCPRO;
11893 }
11894
11895 /* Put a `display' property on the string for the images to display,
11896 put a `menu_item' property on tool-bar items with a value that
11897 is the index of the item in F's tool-bar item vector. */
11898 for (i = 0; i < f->n_tool_bar_items; ++i)
11899 {
11900 #define PROP(IDX) \
11901 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11902
11903 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11904 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11905 int hmargin, vmargin, relief, idx, end;
11906
11907 /* If image is a vector, choose the image according to the
11908 button state. */
11909 image = PROP (TOOL_BAR_ITEM_IMAGES);
11910 if (VECTORP (image))
11911 {
11912 if (enabled_p)
11913 idx = (selected_p
11914 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11915 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11916 else
11917 idx = (selected_p
11918 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11919 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11920
11921 eassert (ASIZE (image) >= idx);
11922 image = AREF (image, idx);
11923 }
11924 else
11925 idx = -1;
11926
11927 /* Ignore invalid image specifications. */
11928 if (!valid_image_p (image))
11929 continue;
11930
11931 /* Display the tool-bar button pressed, or depressed. */
11932 plist = Fcopy_sequence (XCDR (image));
11933
11934 /* Compute margin and relief to draw. */
11935 relief = (tool_bar_button_relief >= 0
11936 ? tool_bar_button_relief
11937 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11938 hmargin = vmargin = relief;
11939
11940 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11941 INT_MAX - max (hmargin, vmargin)))
11942 {
11943 hmargin += XFASTINT (Vtool_bar_button_margin);
11944 vmargin += XFASTINT (Vtool_bar_button_margin);
11945 }
11946 else if (CONSP (Vtool_bar_button_margin))
11947 {
11948 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11949 INT_MAX - hmargin))
11950 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11951
11952 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11953 INT_MAX - vmargin))
11954 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11955 }
11956
11957 if (auto_raise_tool_bar_buttons_p)
11958 {
11959 /* Add a `:relief' property to the image spec if the item is
11960 selected. */
11961 if (selected_p)
11962 {
11963 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11964 hmargin -= relief;
11965 vmargin -= relief;
11966 }
11967 }
11968 else
11969 {
11970 /* If image is selected, display it pressed, i.e. with a
11971 negative relief. If it's not selected, display it with a
11972 raised relief. */
11973 plist = Fplist_put (plist, QCrelief,
11974 (selected_p
11975 ? make_number (-relief)
11976 : make_number (relief)));
11977 hmargin -= relief;
11978 vmargin -= relief;
11979 }
11980
11981 /* Put a margin around the image. */
11982 if (hmargin || vmargin)
11983 {
11984 if (hmargin == vmargin)
11985 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11986 else
11987 plist = Fplist_put (plist, QCmargin,
11988 Fcons (make_number (hmargin),
11989 make_number (vmargin)));
11990 }
11991
11992 /* If button is not enabled, and we don't have special images
11993 for the disabled state, make the image appear disabled by
11994 applying an appropriate algorithm to it. */
11995 if (!enabled_p && idx < 0)
11996 plist = Fplist_put (plist, QCconversion, Qdisabled);
11997
11998 /* Put a `display' text property on the string for the image to
11999 display. Put a `menu-item' property on the string that gives
12000 the start of this item's properties in the tool-bar items
12001 vector. */
12002 image = Fcons (Qimage, plist);
12003 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12004 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12005 struct gcpro gcpro1;
12006 GCPRO1 (props);
12007
12008 /* Let the last image hide all remaining spaces in the tool bar
12009 string. The string can be longer than needed when we reuse a
12010 previous string. */
12011 if (i + 1 == f->n_tool_bar_items)
12012 end = SCHARS (f->desired_tool_bar_string);
12013 else
12014 end = i + 1;
12015 Fadd_text_properties (make_number (i), make_number (end),
12016 props, f->desired_tool_bar_string);
12017 UNGCPRO;
12018 #undef PROP
12019 }
12020
12021 UNGCPRO;
12022 }
12023
12024
12025 /* Display one line of the tool-bar of frame IT->f.
12026
12027 HEIGHT specifies the desired height of the tool-bar line.
12028 If the actual height of the glyph row is less than HEIGHT, the
12029 row's height is increased to HEIGHT, and the icons are centered
12030 vertically in the new height.
12031
12032 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12033 count a final empty row in case the tool-bar width exactly matches
12034 the window width.
12035 */
12036
12037 static void
12038 display_tool_bar_line (struct it *it, int height)
12039 {
12040 struct glyph_row *row = it->glyph_row;
12041 int max_x = it->last_visible_x;
12042 struct glyph *last;
12043
12044 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12045 clear_glyph_row (row);
12046 row->enabled_p = true;
12047 row->y = it->current_y;
12048
12049 /* Note that this isn't made use of if the face hasn't a box,
12050 so there's no need to check the face here. */
12051 it->start_of_box_run_p = true;
12052
12053 while (it->current_x < max_x)
12054 {
12055 int x, n_glyphs_before, i, nglyphs;
12056 struct it it_before;
12057
12058 /* Get the next display element. */
12059 if (!get_next_display_element (it))
12060 {
12061 /* Don't count empty row if we are counting needed tool-bar lines. */
12062 if (height < 0 && !it->hpos)
12063 return;
12064 break;
12065 }
12066
12067 /* Produce glyphs. */
12068 n_glyphs_before = row->used[TEXT_AREA];
12069 it_before = *it;
12070
12071 PRODUCE_GLYPHS (it);
12072
12073 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12074 i = 0;
12075 x = it_before.current_x;
12076 while (i < nglyphs)
12077 {
12078 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12079
12080 if (x + glyph->pixel_width > max_x)
12081 {
12082 /* Glyph doesn't fit on line. Backtrack. */
12083 row->used[TEXT_AREA] = n_glyphs_before;
12084 *it = it_before;
12085 /* If this is the only glyph on this line, it will never fit on the
12086 tool-bar, so skip it. But ensure there is at least one glyph,
12087 so we don't accidentally disable the tool-bar. */
12088 if (n_glyphs_before == 0
12089 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12090 break;
12091 goto out;
12092 }
12093
12094 ++it->hpos;
12095 x += glyph->pixel_width;
12096 ++i;
12097 }
12098
12099 /* Stop at line end. */
12100 if (ITERATOR_AT_END_OF_LINE_P (it))
12101 break;
12102
12103 set_iterator_to_next (it, true);
12104 }
12105
12106 out:;
12107
12108 row->displays_text_p = row->used[TEXT_AREA] != 0;
12109
12110 /* Use default face for the border below the tool bar.
12111
12112 FIXME: When auto-resize-tool-bars is grow-only, there is
12113 no additional border below the possibly empty tool-bar lines.
12114 So to make the extra empty lines look "normal", we have to
12115 use the tool-bar face for the border too. */
12116 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12117 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12118 it->face_id = DEFAULT_FACE_ID;
12119
12120 extend_face_to_end_of_line (it);
12121 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12122 last->right_box_line_p = true;
12123 if (last == row->glyphs[TEXT_AREA])
12124 last->left_box_line_p = true;
12125
12126 /* Make line the desired height and center it vertically. */
12127 if ((height -= it->max_ascent + it->max_descent) > 0)
12128 {
12129 /* Don't add more than one line height. */
12130 height %= FRAME_LINE_HEIGHT (it->f);
12131 it->max_ascent += height / 2;
12132 it->max_descent += (height + 1) / 2;
12133 }
12134
12135 compute_line_metrics (it);
12136
12137 /* If line is empty, make it occupy the rest of the tool-bar. */
12138 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12139 {
12140 row->height = row->phys_height = it->last_visible_y - row->y;
12141 row->visible_height = row->height;
12142 row->ascent = row->phys_ascent = 0;
12143 row->extra_line_spacing = 0;
12144 }
12145
12146 row->full_width_p = true;
12147 row->continued_p = false;
12148 row->truncated_on_left_p = false;
12149 row->truncated_on_right_p = false;
12150
12151 it->current_x = it->hpos = 0;
12152 it->current_y += row->height;
12153 ++it->vpos;
12154 ++it->glyph_row;
12155 }
12156
12157
12158 /* Value is the number of pixels needed to make all tool-bar items of
12159 frame F visible. The actual number of glyph rows needed is
12160 returned in *N_ROWS if non-NULL. */
12161 static int
12162 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12163 {
12164 struct window *w = XWINDOW (f->tool_bar_window);
12165 struct it it;
12166 /* tool_bar_height is called from redisplay_tool_bar after building
12167 the desired matrix, so use (unused) mode-line row as temporary row to
12168 avoid destroying the first tool-bar row. */
12169 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12170
12171 /* Initialize an iterator for iteration over
12172 F->desired_tool_bar_string in the tool-bar window of frame F. */
12173 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12174 temp_row->reversed_p = false;
12175 it.first_visible_x = 0;
12176 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12177 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12178 it.paragraph_embedding = L2R;
12179
12180 while (!ITERATOR_AT_END_P (&it))
12181 {
12182 clear_glyph_row (temp_row);
12183 it.glyph_row = temp_row;
12184 display_tool_bar_line (&it, -1);
12185 }
12186 clear_glyph_row (temp_row);
12187
12188 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12189 if (n_rows)
12190 *n_rows = it.vpos > 0 ? it.vpos : -1;
12191
12192 if (pixelwise)
12193 return it.current_y;
12194 else
12195 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12196 }
12197
12198 #endif /* !USE_GTK && !HAVE_NS */
12199
12200 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12201 0, 2, 0,
12202 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12203 If FRAME is nil or omitted, use the selected frame. Optional argument
12204 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12205 (Lisp_Object frame, Lisp_Object pixelwise)
12206 {
12207 int height = 0;
12208
12209 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12210 struct frame *f = decode_any_frame (frame);
12211
12212 if (WINDOWP (f->tool_bar_window)
12213 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12214 {
12215 update_tool_bar (f, true);
12216 if (f->n_tool_bar_items)
12217 {
12218 build_desired_tool_bar_string (f);
12219 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12220 }
12221 }
12222 #endif
12223
12224 return make_number (height);
12225 }
12226
12227
12228 /* Display the tool-bar of frame F. Value is true if tool-bar's
12229 height should be changed. */
12230 static bool
12231 redisplay_tool_bar (struct frame *f)
12232 {
12233 #if defined (USE_GTK) || defined (HAVE_NS)
12234
12235 if (FRAME_EXTERNAL_TOOL_BAR (f))
12236 update_frame_tool_bar (f);
12237 return false;
12238
12239 #else /* !USE_GTK && !HAVE_NS */
12240
12241 struct window *w;
12242 struct it it;
12243 struct glyph_row *row;
12244
12245 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12246 do anything. This means you must start with tool-bar-lines
12247 non-zero to get the auto-sizing effect. Or in other words, you
12248 can turn off tool-bars by specifying tool-bar-lines zero. */
12249 if (!WINDOWP (f->tool_bar_window)
12250 || (w = XWINDOW (f->tool_bar_window),
12251 WINDOW_TOTAL_LINES (w) == 0))
12252 return false;
12253
12254 /* Set up an iterator for the tool-bar window. */
12255 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12256 it.first_visible_x = 0;
12257 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12258 row = it.glyph_row;
12259 row->reversed_p = false;
12260
12261 /* Build a string that represents the contents of the tool-bar. */
12262 build_desired_tool_bar_string (f);
12263 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12264 /* FIXME: This should be controlled by a user option. But it
12265 doesn't make sense to have an R2L tool bar if the menu bar cannot
12266 be drawn also R2L, and making the menu bar R2L is tricky due
12267 toolkit-specific code that implements it. If an R2L tool bar is
12268 ever supported, display_tool_bar_line should also be augmented to
12269 call unproduce_glyphs like display_line and display_string
12270 do. */
12271 it.paragraph_embedding = L2R;
12272
12273 if (f->n_tool_bar_rows == 0)
12274 {
12275 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12276
12277 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12278 {
12279 x_change_tool_bar_height (f, new_height);
12280 frame_default_tool_bar_height = new_height;
12281 /* Always do that now. */
12282 clear_glyph_matrix (w->desired_matrix);
12283 f->fonts_changed = true;
12284 return true;
12285 }
12286 }
12287
12288 /* Display as many lines as needed to display all tool-bar items. */
12289
12290 if (f->n_tool_bar_rows > 0)
12291 {
12292 int border, rows, height, extra;
12293
12294 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12295 border = XINT (Vtool_bar_border);
12296 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12297 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12298 else if (EQ (Vtool_bar_border, Qborder_width))
12299 border = f->border_width;
12300 else
12301 border = 0;
12302 if (border < 0)
12303 border = 0;
12304
12305 rows = f->n_tool_bar_rows;
12306 height = max (1, (it.last_visible_y - border) / rows);
12307 extra = it.last_visible_y - border - height * rows;
12308
12309 while (it.current_y < it.last_visible_y)
12310 {
12311 int h = 0;
12312 if (extra > 0 && rows-- > 0)
12313 {
12314 h = (extra + rows - 1) / rows;
12315 extra -= h;
12316 }
12317 display_tool_bar_line (&it, height + h);
12318 }
12319 }
12320 else
12321 {
12322 while (it.current_y < it.last_visible_y)
12323 display_tool_bar_line (&it, 0);
12324 }
12325
12326 /* It doesn't make much sense to try scrolling in the tool-bar
12327 window, so don't do it. */
12328 w->desired_matrix->no_scrolling_p = true;
12329 w->must_be_updated_p = true;
12330
12331 if (!NILP (Vauto_resize_tool_bars))
12332 {
12333 bool change_height_p = true;
12334
12335 /* If we couldn't display everything, change the tool-bar's
12336 height if there is room for more. */
12337 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12338 change_height_p = true;
12339
12340 /* We subtract 1 because display_tool_bar_line advances the
12341 glyph_row pointer before returning to its caller. We want to
12342 examine the last glyph row produced by
12343 display_tool_bar_line. */
12344 row = it.glyph_row - 1;
12345
12346 /* If there are blank lines at the end, except for a partially
12347 visible blank line at the end that is smaller than
12348 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12349 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12350 && row->height >= FRAME_LINE_HEIGHT (f))
12351 change_height_p = true;
12352
12353 /* If row displays tool-bar items, but is partially visible,
12354 change the tool-bar's height. */
12355 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12356 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12357 change_height_p = true;
12358
12359 /* Resize windows as needed by changing the `tool-bar-lines'
12360 frame parameter. */
12361 if (change_height_p)
12362 {
12363 int nrows;
12364 int new_height = tool_bar_height (f, &nrows, true);
12365
12366 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12367 && !f->minimize_tool_bar_window_p)
12368 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12369 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12370 f->minimize_tool_bar_window_p = false;
12371
12372 if (change_height_p)
12373 {
12374 x_change_tool_bar_height (f, new_height);
12375 frame_default_tool_bar_height = new_height;
12376 clear_glyph_matrix (w->desired_matrix);
12377 f->n_tool_bar_rows = nrows;
12378 f->fonts_changed = true;
12379
12380 return true;
12381 }
12382 }
12383 }
12384
12385 f->minimize_tool_bar_window_p = false;
12386 return false;
12387
12388 #endif /* USE_GTK || HAVE_NS */
12389 }
12390
12391 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12392
12393 /* Get information about the tool-bar item which is displayed in GLYPH
12394 on frame F. Return in *PROP_IDX the index where tool-bar item
12395 properties start in F->tool_bar_items. Value is false if
12396 GLYPH doesn't display a tool-bar item. */
12397
12398 static bool
12399 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12400 {
12401 Lisp_Object prop;
12402 int charpos;
12403
12404 /* This function can be called asynchronously, which means we must
12405 exclude any possibility that Fget_text_property signals an
12406 error. */
12407 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12408 charpos = max (0, charpos);
12409
12410 /* Get the text property `menu-item' at pos. The value of that
12411 property is the start index of this item's properties in
12412 F->tool_bar_items. */
12413 prop = Fget_text_property (make_number (charpos),
12414 Qmenu_item, f->current_tool_bar_string);
12415 if (! INTEGERP (prop))
12416 return false;
12417 *prop_idx = XINT (prop);
12418 return true;
12419 }
12420
12421 \f
12422 /* Get information about the tool-bar item at position X/Y on frame F.
12423 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12424 the current matrix of the tool-bar window of F, or NULL if not
12425 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12426 item in F->tool_bar_items. Value is
12427
12428 -1 if X/Y is not on a tool-bar item
12429 0 if X/Y is on the same item that was highlighted before.
12430 1 otherwise. */
12431
12432 static int
12433 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12434 int *hpos, int *vpos, int *prop_idx)
12435 {
12436 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12437 struct window *w = XWINDOW (f->tool_bar_window);
12438 int area;
12439
12440 /* Find the glyph under X/Y. */
12441 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12442 if (*glyph == NULL)
12443 return -1;
12444
12445 /* Get the start of this tool-bar item's properties in
12446 f->tool_bar_items. */
12447 if (!tool_bar_item_info (f, *glyph, prop_idx))
12448 return -1;
12449
12450 /* Is mouse on the highlighted item? */
12451 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12452 && *vpos >= hlinfo->mouse_face_beg_row
12453 && *vpos <= hlinfo->mouse_face_end_row
12454 && (*vpos > hlinfo->mouse_face_beg_row
12455 || *hpos >= hlinfo->mouse_face_beg_col)
12456 && (*vpos < hlinfo->mouse_face_end_row
12457 || *hpos < hlinfo->mouse_face_end_col
12458 || hlinfo->mouse_face_past_end))
12459 return 0;
12460
12461 return 1;
12462 }
12463
12464
12465 /* EXPORT:
12466 Handle mouse button event on the tool-bar of frame F, at
12467 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12468 false for button release. MODIFIERS is event modifiers for button
12469 release. */
12470
12471 void
12472 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12473 int modifiers)
12474 {
12475 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12476 struct window *w = XWINDOW (f->tool_bar_window);
12477 int hpos, vpos, prop_idx;
12478 struct glyph *glyph;
12479 Lisp_Object enabled_p;
12480 int ts;
12481
12482 /* If not on the highlighted tool-bar item, and mouse-highlight is
12483 non-nil, return. This is so we generate the tool-bar button
12484 click only when the mouse button is released on the same item as
12485 where it was pressed. However, when mouse-highlight is disabled,
12486 generate the click when the button is released regardless of the
12487 highlight, since tool-bar items are not highlighted in that
12488 case. */
12489 frame_to_window_pixel_xy (w, &x, &y);
12490 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12491 if (ts == -1
12492 || (ts != 0 && !NILP (Vmouse_highlight)))
12493 return;
12494
12495 /* When mouse-highlight is off, generate the click for the item
12496 where the button was pressed, disregarding where it was
12497 released. */
12498 if (NILP (Vmouse_highlight) && !down_p)
12499 prop_idx = f->last_tool_bar_item;
12500
12501 /* If item is disabled, do nothing. */
12502 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12503 if (NILP (enabled_p))
12504 return;
12505
12506 if (down_p)
12507 {
12508 /* Show item in pressed state. */
12509 if (!NILP (Vmouse_highlight))
12510 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12511 f->last_tool_bar_item = prop_idx;
12512 }
12513 else
12514 {
12515 Lisp_Object key, frame;
12516 struct input_event event;
12517 EVENT_INIT (event);
12518
12519 /* Show item in released state. */
12520 if (!NILP (Vmouse_highlight))
12521 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12522
12523 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12524
12525 XSETFRAME (frame, f);
12526 event.kind = TOOL_BAR_EVENT;
12527 event.frame_or_window = frame;
12528 event.arg = frame;
12529 kbd_buffer_store_event (&event);
12530
12531 event.kind = TOOL_BAR_EVENT;
12532 event.frame_or_window = frame;
12533 event.arg = key;
12534 event.modifiers = modifiers;
12535 kbd_buffer_store_event (&event);
12536 f->last_tool_bar_item = -1;
12537 }
12538 }
12539
12540
12541 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12542 tool-bar window-relative coordinates X/Y. Called from
12543 note_mouse_highlight. */
12544
12545 static void
12546 note_tool_bar_highlight (struct frame *f, int x, int y)
12547 {
12548 Lisp_Object window = f->tool_bar_window;
12549 struct window *w = XWINDOW (window);
12550 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12551 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12552 int hpos, vpos;
12553 struct glyph *glyph;
12554 struct glyph_row *row;
12555 int i;
12556 Lisp_Object enabled_p;
12557 int prop_idx;
12558 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12559 bool mouse_down_p;
12560 int rc;
12561
12562 /* Function note_mouse_highlight is called with negative X/Y
12563 values when mouse moves outside of the frame. */
12564 if (x <= 0 || y <= 0)
12565 {
12566 clear_mouse_face (hlinfo);
12567 return;
12568 }
12569
12570 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12571 if (rc < 0)
12572 {
12573 /* Not on tool-bar item. */
12574 clear_mouse_face (hlinfo);
12575 return;
12576 }
12577 else if (rc == 0)
12578 /* On same tool-bar item as before. */
12579 goto set_help_echo;
12580
12581 clear_mouse_face (hlinfo);
12582
12583 /* Mouse is down, but on different tool-bar item? */
12584 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12585 && f == dpyinfo->last_mouse_frame);
12586
12587 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12588 return;
12589
12590 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12591
12592 /* If tool-bar item is not enabled, don't highlight it. */
12593 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12594 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12595 {
12596 /* Compute the x-position of the glyph. In front and past the
12597 image is a space. We include this in the highlighted area. */
12598 row = MATRIX_ROW (w->current_matrix, vpos);
12599 for (i = x = 0; i < hpos; ++i)
12600 x += row->glyphs[TEXT_AREA][i].pixel_width;
12601
12602 /* Record this as the current active region. */
12603 hlinfo->mouse_face_beg_col = hpos;
12604 hlinfo->mouse_face_beg_row = vpos;
12605 hlinfo->mouse_face_beg_x = x;
12606 hlinfo->mouse_face_past_end = false;
12607
12608 hlinfo->mouse_face_end_col = hpos + 1;
12609 hlinfo->mouse_face_end_row = vpos;
12610 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12611 hlinfo->mouse_face_window = window;
12612 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12613
12614 /* Display it as active. */
12615 show_mouse_face (hlinfo, draw);
12616 }
12617
12618 set_help_echo:
12619
12620 /* Set help_echo_string to a help string to display for this tool-bar item.
12621 XTread_socket does the rest. */
12622 help_echo_object = help_echo_window = Qnil;
12623 help_echo_pos = -1;
12624 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12625 if (NILP (help_echo_string))
12626 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12627 }
12628
12629 #endif /* !USE_GTK && !HAVE_NS */
12630
12631 #endif /* HAVE_WINDOW_SYSTEM */
12632
12633
12634 \f
12635 /************************************************************************
12636 Horizontal scrolling
12637 ************************************************************************/
12638
12639 /* For all leaf windows in the window tree rooted at WINDOW, set their
12640 hscroll value so that PT is (i) visible in the window, and (ii) so
12641 that it is not within a certain margin at the window's left and
12642 right border. Value is true if any window's hscroll has been
12643 changed. */
12644
12645 static bool
12646 hscroll_window_tree (Lisp_Object window)
12647 {
12648 bool hscrolled_p = false;
12649 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12650 int hscroll_step_abs = 0;
12651 double hscroll_step_rel = 0;
12652
12653 if (hscroll_relative_p)
12654 {
12655 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12656 if (hscroll_step_rel < 0)
12657 {
12658 hscroll_relative_p = false;
12659 hscroll_step_abs = 0;
12660 }
12661 }
12662 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12663 {
12664 hscroll_step_abs = XINT (Vhscroll_step);
12665 if (hscroll_step_abs < 0)
12666 hscroll_step_abs = 0;
12667 }
12668 else
12669 hscroll_step_abs = 0;
12670
12671 while (WINDOWP (window))
12672 {
12673 struct window *w = XWINDOW (window);
12674
12675 if (WINDOWP (w->contents))
12676 hscrolled_p |= hscroll_window_tree (w->contents);
12677 else if (w->cursor.vpos >= 0)
12678 {
12679 int h_margin;
12680 int text_area_width;
12681 struct glyph_row *cursor_row;
12682 struct glyph_row *bottom_row;
12683
12684 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12685 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12686 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12687 else
12688 cursor_row = bottom_row - 1;
12689
12690 if (!cursor_row->enabled_p)
12691 {
12692 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12693 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12694 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12695 else
12696 cursor_row = bottom_row - 1;
12697 }
12698 bool row_r2l_p = cursor_row->reversed_p;
12699
12700 text_area_width = window_box_width (w, TEXT_AREA);
12701
12702 /* Scroll when cursor is inside this scroll margin. */
12703 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12704
12705 /* If the position of this window's point has explicitly
12706 changed, no more suspend auto hscrolling. */
12707 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12708 w->suspend_auto_hscroll = false;
12709
12710 /* Remember window point. */
12711 Fset_marker (w->old_pointm,
12712 ((w == XWINDOW (selected_window))
12713 ? make_number (BUF_PT (XBUFFER (w->contents)))
12714 : Fmarker_position (w->pointm)),
12715 w->contents);
12716
12717 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12718 && !w->suspend_auto_hscroll
12719 /* In some pathological cases, like restoring a window
12720 configuration into a frame that is much smaller than
12721 the one from which the configuration was saved, we
12722 get glyph rows whose start and end have zero buffer
12723 positions, which we cannot handle below. Just skip
12724 such windows. */
12725 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12726 /* For left-to-right rows, hscroll when cursor is either
12727 (i) inside the right hscroll margin, or (ii) if it is
12728 inside the left margin and the window is already
12729 hscrolled. */
12730 && ((!row_r2l_p
12731 && ((w->hscroll && w->cursor.x <= h_margin)
12732 || (cursor_row->enabled_p
12733 && cursor_row->truncated_on_right_p
12734 && (w->cursor.x >= text_area_width - h_margin))))
12735 /* For right-to-left rows, the logic is similar,
12736 except that rules for scrolling to left and right
12737 are reversed. E.g., if cursor.x <= h_margin, we
12738 need to hscroll "to the right" unconditionally,
12739 and that will scroll the screen to the left so as
12740 to reveal the next portion of the row. */
12741 || (row_r2l_p
12742 && ((cursor_row->enabled_p
12743 /* FIXME: It is confusing to set the
12744 truncated_on_right_p flag when R2L rows
12745 are actually truncated on the left. */
12746 && cursor_row->truncated_on_right_p
12747 && w->cursor.x <= h_margin)
12748 || (w->hscroll
12749 && (w->cursor.x >= text_area_width - h_margin))))))
12750 {
12751 struct it it;
12752 ptrdiff_t hscroll;
12753 struct buffer *saved_current_buffer;
12754 ptrdiff_t pt;
12755 int wanted_x;
12756
12757 /* Find point in a display of infinite width. */
12758 saved_current_buffer = current_buffer;
12759 current_buffer = XBUFFER (w->contents);
12760
12761 if (w == XWINDOW (selected_window))
12762 pt = PT;
12763 else
12764 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12765
12766 /* Move iterator to pt starting at cursor_row->start in
12767 a line with infinite width. */
12768 init_to_row_start (&it, w, cursor_row);
12769 it.last_visible_x = INFINITY;
12770 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12771 current_buffer = saved_current_buffer;
12772
12773 /* Position cursor in window. */
12774 if (!hscroll_relative_p && hscroll_step_abs == 0)
12775 hscroll = max (0, (it.current_x
12776 - (ITERATOR_AT_END_OF_LINE_P (&it)
12777 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12778 : (text_area_width / 2))))
12779 / FRAME_COLUMN_WIDTH (it.f);
12780 else if ((!row_r2l_p
12781 && w->cursor.x >= text_area_width - h_margin)
12782 || (row_r2l_p && w->cursor.x <= h_margin))
12783 {
12784 if (hscroll_relative_p)
12785 wanted_x = text_area_width * (1 - hscroll_step_rel)
12786 - h_margin;
12787 else
12788 wanted_x = text_area_width
12789 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12790 - h_margin;
12791 hscroll
12792 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12793 }
12794 else
12795 {
12796 if (hscroll_relative_p)
12797 wanted_x = text_area_width * hscroll_step_rel
12798 + h_margin;
12799 else
12800 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12801 + h_margin;
12802 hscroll
12803 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12804 }
12805 hscroll = max (hscroll, w->min_hscroll);
12806
12807 /* Don't prevent redisplay optimizations if hscroll
12808 hasn't changed, as it will unnecessarily slow down
12809 redisplay. */
12810 if (w->hscroll != hscroll)
12811 {
12812 struct buffer *b = XBUFFER (w->contents);
12813 b->prevent_redisplay_optimizations_p = true;
12814 w->hscroll = hscroll;
12815 hscrolled_p = true;
12816 }
12817 }
12818 }
12819
12820 window = w->next;
12821 }
12822
12823 /* Value is true if hscroll of any leaf window has been changed. */
12824 return hscrolled_p;
12825 }
12826
12827
12828 /* Set hscroll so that cursor is visible and not inside horizontal
12829 scroll margins for all windows in the tree rooted at WINDOW. See
12830 also hscroll_window_tree above. Value is true if any window's
12831 hscroll has been changed. If it has, desired matrices on the frame
12832 of WINDOW are cleared. */
12833
12834 static bool
12835 hscroll_windows (Lisp_Object window)
12836 {
12837 bool hscrolled_p = hscroll_window_tree (window);
12838 if (hscrolled_p)
12839 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12840 return hscrolled_p;
12841 }
12842
12843
12844 \f
12845 /************************************************************************
12846 Redisplay
12847 ************************************************************************/
12848
12849 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
12850 This is sometimes handy to have in a debugger session. */
12851
12852 #ifdef GLYPH_DEBUG
12853
12854 /* First and last unchanged row for try_window_id. */
12855
12856 static int debug_first_unchanged_at_end_vpos;
12857 static int debug_last_unchanged_at_beg_vpos;
12858
12859 /* Delta vpos and y. */
12860
12861 static int debug_dvpos, debug_dy;
12862
12863 /* Delta in characters and bytes for try_window_id. */
12864
12865 static ptrdiff_t debug_delta, debug_delta_bytes;
12866
12867 /* Values of window_end_pos and window_end_vpos at the end of
12868 try_window_id. */
12869
12870 static ptrdiff_t debug_end_vpos;
12871
12872 /* Append a string to W->desired_matrix->method. FMT is a printf
12873 format string. If trace_redisplay_p is true also printf the
12874 resulting string to stderr. */
12875
12876 static void debug_method_add (struct window *, char const *, ...)
12877 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12878
12879 static void
12880 debug_method_add (struct window *w, char const *fmt, ...)
12881 {
12882 void *ptr = w;
12883 char *method = w->desired_matrix->method;
12884 int len = strlen (method);
12885 int size = sizeof w->desired_matrix->method;
12886 int remaining = size - len - 1;
12887 va_list ap;
12888
12889 if (len && remaining)
12890 {
12891 method[len] = '|';
12892 --remaining, ++len;
12893 }
12894
12895 va_start (ap, fmt);
12896 vsnprintf (method + len, remaining + 1, fmt, ap);
12897 va_end (ap);
12898
12899 if (trace_redisplay_p)
12900 fprintf (stderr, "%p (%s): %s\n",
12901 ptr,
12902 ((BUFFERP (w->contents)
12903 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12904 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12905 : "no buffer"),
12906 method + len);
12907 }
12908
12909 #endif /* GLYPH_DEBUG */
12910
12911
12912 /* Value is true if all changes in window W, which displays
12913 current_buffer, are in the text between START and END. START is a
12914 buffer position, END is given as a distance from Z. Used in
12915 redisplay_internal for display optimization. */
12916
12917 static bool
12918 text_outside_line_unchanged_p (struct window *w,
12919 ptrdiff_t start, ptrdiff_t end)
12920 {
12921 bool unchanged_p = true;
12922
12923 /* If text or overlays have changed, see where. */
12924 if (window_outdated (w))
12925 {
12926 /* Gap in the line? */
12927 if (GPT < start || Z - GPT < end)
12928 unchanged_p = false;
12929
12930 /* Changes start in front of the line, or end after it? */
12931 if (unchanged_p
12932 && (BEG_UNCHANGED < start - 1
12933 || END_UNCHANGED < end))
12934 unchanged_p = false;
12935
12936 /* If selective display, can't optimize if changes start at the
12937 beginning of the line. */
12938 if (unchanged_p
12939 && INTEGERP (BVAR (current_buffer, selective_display))
12940 && XINT (BVAR (current_buffer, selective_display)) > 0
12941 && (BEG_UNCHANGED < start || GPT <= start))
12942 unchanged_p = false;
12943
12944 /* If there are overlays at the start or end of the line, these
12945 may have overlay strings with newlines in them. A change at
12946 START, for instance, may actually concern the display of such
12947 overlay strings as well, and they are displayed on different
12948 lines. So, quickly rule out this case. (For the future, it
12949 might be desirable to implement something more telling than
12950 just BEG/END_UNCHANGED.) */
12951 if (unchanged_p)
12952 {
12953 if (BEG + BEG_UNCHANGED == start
12954 && overlay_touches_p (start))
12955 unchanged_p = false;
12956 if (END_UNCHANGED == end
12957 && overlay_touches_p (Z - end))
12958 unchanged_p = false;
12959 }
12960
12961 /* Under bidi reordering, adding or deleting a character in the
12962 beginning of a paragraph, before the first strong directional
12963 character, can change the base direction of the paragraph (unless
12964 the buffer specifies a fixed paragraph direction), which will
12965 require to redisplay the whole paragraph. It might be worthwhile
12966 to find the paragraph limits and widen the range of redisplayed
12967 lines to that, but for now just give up this optimization. */
12968 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12969 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12970 unchanged_p = false;
12971 }
12972
12973 return unchanged_p;
12974 }
12975
12976
12977 /* Do a frame update, taking possible shortcuts into account. This is
12978 the main external entry point for redisplay.
12979
12980 If the last redisplay displayed an echo area message and that message
12981 is no longer requested, we clear the echo area or bring back the
12982 mini-buffer if that is in use. */
12983
12984 void
12985 redisplay (void)
12986 {
12987 redisplay_internal ();
12988 }
12989
12990
12991 static Lisp_Object
12992 overlay_arrow_string_or_property (Lisp_Object var)
12993 {
12994 Lisp_Object val;
12995
12996 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12997 return val;
12998
12999 return Voverlay_arrow_string;
13000 }
13001
13002 /* Return true if there are any overlay-arrows in current_buffer. */
13003 static bool
13004 overlay_arrow_in_current_buffer_p (void)
13005 {
13006 Lisp_Object vlist;
13007
13008 for (vlist = Voverlay_arrow_variable_list;
13009 CONSP (vlist);
13010 vlist = XCDR (vlist))
13011 {
13012 Lisp_Object var = XCAR (vlist);
13013 Lisp_Object val;
13014
13015 if (!SYMBOLP (var))
13016 continue;
13017 val = find_symbol_value (var);
13018 if (MARKERP (val)
13019 && current_buffer == XMARKER (val)->buffer)
13020 return true;
13021 }
13022 return false;
13023 }
13024
13025
13026 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13027 has changed. */
13028
13029 static bool
13030 overlay_arrows_changed_p (void)
13031 {
13032 Lisp_Object vlist;
13033
13034 for (vlist = Voverlay_arrow_variable_list;
13035 CONSP (vlist);
13036 vlist = XCDR (vlist))
13037 {
13038 Lisp_Object var = XCAR (vlist);
13039 Lisp_Object val, pstr;
13040
13041 if (!SYMBOLP (var))
13042 continue;
13043 val = find_symbol_value (var);
13044 if (!MARKERP (val))
13045 continue;
13046 if (! EQ (COERCE_MARKER (val),
13047 Fget (var, Qlast_arrow_position))
13048 || ! (pstr = overlay_arrow_string_or_property (var),
13049 EQ (pstr, Fget (var, Qlast_arrow_string))))
13050 return true;
13051 }
13052 return false;
13053 }
13054
13055 /* Mark overlay arrows to be updated on next redisplay. */
13056
13057 static void
13058 update_overlay_arrows (int up_to_date)
13059 {
13060 Lisp_Object vlist;
13061
13062 for (vlist = Voverlay_arrow_variable_list;
13063 CONSP (vlist);
13064 vlist = XCDR (vlist))
13065 {
13066 Lisp_Object var = XCAR (vlist);
13067
13068 if (!SYMBOLP (var))
13069 continue;
13070
13071 if (up_to_date > 0)
13072 {
13073 Lisp_Object val = find_symbol_value (var);
13074 Fput (var, Qlast_arrow_position,
13075 COERCE_MARKER (val));
13076 Fput (var, Qlast_arrow_string,
13077 overlay_arrow_string_or_property (var));
13078 }
13079 else if (up_to_date < 0
13080 || !NILP (Fget (var, Qlast_arrow_position)))
13081 {
13082 Fput (var, Qlast_arrow_position, Qt);
13083 Fput (var, Qlast_arrow_string, Qt);
13084 }
13085 }
13086 }
13087
13088
13089 /* Return overlay arrow string to display at row.
13090 Return integer (bitmap number) for arrow bitmap in left fringe.
13091 Return nil if no overlay arrow. */
13092
13093 static Lisp_Object
13094 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13095 {
13096 Lisp_Object vlist;
13097
13098 for (vlist = Voverlay_arrow_variable_list;
13099 CONSP (vlist);
13100 vlist = XCDR (vlist))
13101 {
13102 Lisp_Object var = XCAR (vlist);
13103 Lisp_Object val;
13104
13105 if (!SYMBOLP (var))
13106 continue;
13107
13108 val = find_symbol_value (var);
13109
13110 if (MARKERP (val)
13111 && current_buffer == XMARKER (val)->buffer
13112 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13113 {
13114 if (FRAME_WINDOW_P (it->f)
13115 /* FIXME: if ROW->reversed_p is set, this should test
13116 the right fringe, not the left one. */
13117 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13118 {
13119 #ifdef HAVE_WINDOW_SYSTEM
13120 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13121 {
13122 int fringe_bitmap = lookup_fringe_bitmap (val);
13123 if (fringe_bitmap != 0)
13124 return make_number (fringe_bitmap);
13125 }
13126 #endif
13127 return make_number (-1); /* Use default arrow bitmap. */
13128 }
13129 return overlay_arrow_string_or_property (var);
13130 }
13131 }
13132
13133 return Qnil;
13134 }
13135
13136 /* Return true if point moved out of or into a composition. Otherwise
13137 return false. PREV_BUF and PREV_PT are the last point buffer and
13138 position. BUF and PT are the current point buffer and position. */
13139
13140 static bool
13141 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13142 struct buffer *buf, ptrdiff_t pt)
13143 {
13144 ptrdiff_t start, end;
13145 Lisp_Object prop;
13146 Lisp_Object buffer;
13147
13148 XSETBUFFER (buffer, buf);
13149 /* Check a composition at the last point if point moved within the
13150 same buffer. */
13151 if (prev_buf == buf)
13152 {
13153 if (prev_pt == pt)
13154 /* Point didn't move. */
13155 return false;
13156
13157 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13158 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13159 && composition_valid_p (start, end, prop)
13160 && start < prev_pt && end > prev_pt)
13161 /* The last point was within the composition. Return true iff
13162 point moved out of the composition. */
13163 return (pt <= start || pt >= end);
13164 }
13165
13166 /* Check a composition at the current point. */
13167 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13168 && find_composition (pt, -1, &start, &end, &prop, buffer)
13169 && composition_valid_p (start, end, prop)
13170 && start < pt && end > pt);
13171 }
13172
13173 /* Reconsider the clip changes of buffer which is displayed in W. */
13174
13175 static void
13176 reconsider_clip_changes (struct window *w)
13177 {
13178 struct buffer *b = XBUFFER (w->contents);
13179
13180 if (b->clip_changed
13181 && w->window_end_valid
13182 && w->current_matrix->buffer == b
13183 && w->current_matrix->zv == BUF_ZV (b)
13184 && w->current_matrix->begv == BUF_BEGV (b))
13185 b->clip_changed = false;
13186
13187 /* If display wasn't paused, and W is not a tool bar window, see if
13188 point has been moved into or out of a composition. In that case,
13189 set b->clip_changed to force updating the screen. If
13190 b->clip_changed has already been set, skip this check. */
13191 if (!b->clip_changed && w->window_end_valid)
13192 {
13193 ptrdiff_t pt = (w == XWINDOW (selected_window)
13194 ? PT : marker_position (w->pointm));
13195
13196 if ((w->current_matrix->buffer != b || pt != w->last_point)
13197 && check_point_in_composition (w->current_matrix->buffer,
13198 w->last_point, b, pt))
13199 b->clip_changed = true;
13200 }
13201 }
13202
13203 static void
13204 propagate_buffer_redisplay (void)
13205 { /* Resetting b->text->redisplay is problematic!
13206 We can't just reset it in the case that some window that displays
13207 it has not been redisplayed; and such a window can stay
13208 unredisplayed for a long time if it's currently invisible.
13209 But we do want to reset it at the end of redisplay otherwise
13210 its displayed windows will keep being redisplayed over and over
13211 again.
13212 So we copy all b->text->redisplay flags up to their windows here,
13213 such that mark_window_display_accurate can safely reset
13214 b->text->redisplay. */
13215 Lisp_Object ws = window_list ();
13216 for (; CONSP (ws); ws = XCDR (ws))
13217 {
13218 struct window *thisw = XWINDOW (XCAR (ws));
13219 struct buffer *thisb = XBUFFER (thisw->contents);
13220 if (thisb->text->redisplay)
13221 thisw->redisplay = true;
13222 }
13223 }
13224
13225 #define STOP_POLLING \
13226 do { if (! polling_stopped_here) stop_polling (); \
13227 polling_stopped_here = true; } while (false)
13228
13229 #define RESUME_POLLING \
13230 do { if (polling_stopped_here) start_polling (); \
13231 polling_stopped_here = false; } while (false)
13232
13233
13234 /* Perhaps in the future avoid recentering windows if it
13235 is not necessary; currently that causes some problems. */
13236
13237 static void
13238 redisplay_internal (void)
13239 {
13240 struct window *w = XWINDOW (selected_window);
13241 struct window *sw;
13242 struct frame *fr;
13243 bool pending;
13244 bool must_finish = false, match_p;
13245 struct text_pos tlbufpos, tlendpos;
13246 int number_of_visible_frames;
13247 ptrdiff_t count;
13248 struct frame *sf;
13249 bool polling_stopped_here = false;
13250 Lisp_Object tail, frame;
13251
13252 /* True means redisplay has to consider all windows on all
13253 frames. False, only selected_window is considered. */
13254 bool consider_all_windows_p;
13255
13256 /* True means redisplay has to redisplay the miniwindow. */
13257 bool update_miniwindow_p = false;
13258
13259 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13260
13261 /* No redisplay if running in batch mode or frame is not yet fully
13262 initialized, or redisplay is explicitly turned off by setting
13263 Vinhibit_redisplay. */
13264 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13265 || !NILP (Vinhibit_redisplay))
13266 return;
13267
13268 /* Don't examine these until after testing Vinhibit_redisplay.
13269 When Emacs is shutting down, perhaps because its connection to
13270 X has dropped, we should not look at them at all. */
13271 fr = XFRAME (w->frame);
13272 sf = SELECTED_FRAME ();
13273
13274 if (!fr->glyphs_initialized_p)
13275 return;
13276
13277 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13278 if (popup_activated ())
13279 return;
13280 #endif
13281
13282 /* I don't think this happens but let's be paranoid. */
13283 if (redisplaying_p)
13284 return;
13285
13286 /* Record a function that clears redisplaying_p
13287 when we leave this function. */
13288 count = SPECPDL_INDEX ();
13289 record_unwind_protect_void (unwind_redisplay);
13290 redisplaying_p = true;
13291 specbind (Qinhibit_free_realized_faces, Qnil);
13292
13293 /* Record this function, so it appears on the profiler's backtraces. */
13294 record_in_backtrace (Qredisplay_internal, 0, 0);
13295
13296 FOR_EACH_FRAME (tail, frame)
13297 XFRAME (frame)->already_hscrolled_p = false;
13298
13299 retry:
13300 /* Remember the currently selected window. */
13301 sw = w;
13302
13303 pending = false;
13304 last_escape_glyph_frame = NULL;
13305 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13306 last_glyphless_glyph_frame = NULL;
13307 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13308
13309 /* If face_change, init_iterator will free all realized faces, which
13310 includes the faces referenced from current matrices. So, we
13311 can't reuse current matrices in this case. */
13312 if (face_change)
13313 windows_or_buffers_changed = 47;
13314
13315 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13316 && FRAME_TTY (sf)->previous_frame != sf)
13317 {
13318 /* Since frames on a single ASCII terminal share the same
13319 display area, displaying a different frame means redisplay
13320 the whole thing. */
13321 SET_FRAME_GARBAGED (sf);
13322 #ifndef DOS_NT
13323 set_tty_color_mode (FRAME_TTY (sf), sf);
13324 #endif
13325 FRAME_TTY (sf)->previous_frame = sf;
13326 }
13327
13328 /* Set the visible flags for all frames. Do this before checking for
13329 resized or garbaged frames; they want to know if their frames are
13330 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13331 number_of_visible_frames = 0;
13332
13333 FOR_EACH_FRAME (tail, frame)
13334 {
13335 struct frame *f = XFRAME (frame);
13336
13337 if (FRAME_VISIBLE_P (f))
13338 {
13339 ++number_of_visible_frames;
13340 /* Adjust matrices for visible frames only. */
13341 if (f->fonts_changed)
13342 {
13343 adjust_frame_glyphs (f);
13344 f->fonts_changed = false;
13345 }
13346 /* If cursor type has been changed on the frame
13347 other than selected, consider all frames. */
13348 if (f != sf && f->cursor_type_changed)
13349 update_mode_lines = 31;
13350 }
13351 clear_desired_matrices (f);
13352 }
13353
13354 /* Notice any pending interrupt request to change frame size. */
13355 do_pending_window_change (true);
13356
13357 /* do_pending_window_change could change the selected_window due to
13358 frame resizing which makes the selected window too small. */
13359 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13360 sw = w;
13361
13362 /* Clear frames marked as garbaged. */
13363 clear_garbaged_frames ();
13364
13365 /* Build menubar and tool-bar items. */
13366 if (NILP (Vmemory_full))
13367 prepare_menu_bars ();
13368
13369 reconsider_clip_changes (w);
13370
13371 /* In most cases selected window displays current buffer. */
13372 match_p = XBUFFER (w->contents) == current_buffer;
13373 if (match_p)
13374 {
13375 /* Detect case that we need to write or remove a star in the mode line. */
13376 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13377 w->update_mode_line = true;
13378
13379 if (mode_line_update_needed (w))
13380 w->update_mode_line = true;
13381
13382 /* If reconsider_clip_changes above decided that the narrowing
13383 in the current buffer changed, make sure all other windows
13384 showing that buffer will be redisplayed. */
13385 if (current_buffer->clip_changed)
13386 bset_update_mode_line (current_buffer);
13387 }
13388
13389 /* Normally the message* functions will have already displayed and
13390 updated the echo area, but the frame may have been trashed, or
13391 the update may have been preempted, so display the echo area
13392 again here. Checking message_cleared_p captures the case that
13393 the echo area should be cleared. */
13394 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13395 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13396 || (message_cleared_p
13397 && minibuf_level == 0
13398 /* If the mini-window is currently selected, this means the
13399 echo-area doesn't show through. */
13400 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13401 {
13402 bool window_height_changed_p = echo_area_display (false);
13403
13404 if (message_cleared_p)
13405 update_miniwindow_p = true;
13406
13407 must_finish = true;
13408
13409 /* If we don't display the current message, don't clear the
13410 message_cleared_p flag, because, if we did, we wouldn't clear
13411 the echo area in the next redisplay which doesn't preserve
13412 the echo area. */
13413 if (!display_last_displayed_message_p)
13414 message_cleared_p = false;
13415
13416 if (window_height_changed_p)
13417 {
13418 windows_or_buffers_changed = 50;
13419
13420 /* If window configuration was changed, frames may have been
13421 marked garbaged. Clear them or we will experience
13422 surprises wrt scrolling. */
13423 clear_garbaged_frames ();
13424 }
13425 }
13426 else if (EQ (selected_window, minibuf_window)
13427 && (current_buffer->clip_changed || window_outdated (w))
13428 && resize_mini_window (w, false))
13429 {
13430 /* Resized active mini-window to fit the size of what it is
13431 showing if its contents might have changed. */
13432 must_finish = true;
13433
13434 /* If window configuration was changed, frames may have been
13435 marked garbaged. Clear them or we will experience
13436 surprises wrt scrolling. */
13437 clear_garbaged_frames ();
13438 }
13439
13440 if (windows_or_buffers_changed && !update_mode_lines)
13441 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13442 only the windows's contents needs to be refreshed, or whether the
13443 mode-lines also need a refresh. */
13444 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13445 ? REDISPLAY_SOME : 32);
13446
13447 /* If specs for an arrow have changed, do thorough redisplay
13448 to ensure we remove any arrow that should no longer exist. */
13449 if (overlay_arrows_changed_p ())
13450 /* Apparently, this is the only case where we update other windows,
13451 without updating other mode-lines. */
13452 windows_or_buffers_changed = 49;
13453
13454 consider_all_windows_p = (update_mode_lines
13455 || windows_or_buffers_changed);
13456
13457 #define AINC(a,i) \
13458 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13459 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13460
13461 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13462 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13463
13464 /* Optimize the case that only the line containing the cursor in the
13465 selected window has changed. Variables starting with this_ are
13466 set in display_line and record information about the line
13467 containing the cursor. */
13468 tlbufpos = this_line_start_pos;
13469 tlendpos = this_line_end_pos;
13470 if (!consider_all_windows_p
13471 && CHARPOS (tlbufpos) > 0
13472 && !w->update_mode_line
13473 && !current_buffer->clip_changed
13474 && !current_buffer->prevent_redisplay_optimizations_p
13475 && FRAME_VISIBLE_P (XFRAME (w->frame))
13476 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13477 && !XFRAME (w->frame)->cursor_type_changed
13478 /* Make sure recorded data applies to current buffer, etc. */
13479 && this_line_buffer == current_buffer
13480 && match_p
13481 && !w->force_start
13482 && !w->optional_new_start
13483 /* Point must be on the line that we have info recorded about. */
13484 && PT >= CHARPOS (tlbufpos)
13485 && PT <= Z - CHARPOS (tlendpos)
13486 /* All text outside that line, including its final newline,
13487 must be unchanged. */
13488 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13489 CHARPOS (tlendpos)))
13490 {
13491 if (CHARPOS (tlbufpos) > BEGV
13492 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13493 && (CHARPOS (tlbufpos) == ZV
13494 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13495 /* Former continuation line has disappeared by becoming empty. */
13496 goto cancel;
13497 else if (window_outdated (w) || MINI_WINDOW_P (w))
13498 {
13499 /* We have to handle the case of continuation around a
13500 wide-column character (see the comment in indent.c around
13501 line 1340).
13502
13503 For instance, in the following case:
13504
13505 -------- Insert --------
13506 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13507 J_I_ ==> J_I_ `^^' are cursors.
13508 ^^ ^^
13509 -------- --------
13510
13511 As we have to redraw the line above, we cannot use this
13512 optimization. */
13513
13514 struct it it;
13515 int line_height_before = this_line_pixel_height;
13516
13517 /* Note that start_display will handle the case that the
13518 line starting at tlbufpos is a continuation line. */
13519 start_display (&it, w, tlbufpos);
13520
13521 /* Implementation note: It this still necessary? */
13522 if (it.current_x != this_line_start_x)
13523 goto cancel;
13524
13525 TRACE ((stderr, "trying display optimization 1\n"));
13526 w->cursor.vpos = -1;
13527 overlay_arrow_seen = false;
13528 it.vpos = this_line_vpos;
13529 it.current_y = this_line_y;
13530 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13531 display_line (&it);
13532
13533 /* If line contains point, is not continued,
13534 and ends at same distance from eob as before, we win. */
13535 if (w->cursor.vpos >= 0
13536 /* Line is not continued, otherwise this_line_start_pos
13537 would have been set to 0 in display_line. */
13538 && CHARPOS (this_line_start_pos)
13539 /* Line ends as before. */
13540 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13541 /* Line has same height as before. Otherwise other lines
13542 would have to be shifted up or down. */
13543 && this_line_pixel_height == line_height_before)
13544 {
13545 /* If this is not the window's last line, we must adjust
13546 the charstarts of the lines below. */
13547 if (it.current_y < it.last_visible_y)
13548 {
13549 struct glyph_row *row
13550 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13551 ptrdiff_t delta, delta_bytes;
13552
13553 /* We used to distinguish between two cases here,
13554 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13555 when the line ends in a newline or the end of the
13556 buffer's accessible portion. But both cases did
13557 the same, so they were collapsed. */
13558 delta = (Z
13559 - CHARPOS (tlendpos)
13560 - MATRIX_ROW_START_CHARPOS (row));
13561 delta_bytes = (Z_BYTE
13562 - BYTEPOS (tlendpos)
13563 - MATRIX_ROW_START_BYTEPOS (row));
13564
13565 increment_matrix_positions (w->current_matrix,
13566 this_line_vpos + 1,
13567 w->current_matrix->nrows,
13568 delta, delta_bytes);
13569 }
13570
13571 /* If this row displays text now but previously didn't,
13572 or vice versa, w->window_end_vpos may have to be
13573 adjusted. */
13574 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13575 {
13576 if (w->window_end_vpos < this_line_vpos)
13577 w->window_end_vpos = this_line_vpos;
13578 }
13579 else if (w->window_end_vpos == this_line_vpos
13580 && this_line_vpos > 0)
13581 w->window_end_vpos = this_line_vpos - 1;
13582 w->window_end_valid = false;
13583
13584 /* Update hint: No need to try to scroll in update_window. */
13585 w->desired_matrix->no_scrolling_p = true;
13586
13587 #ifdef GLYPH_DEBUG
13588 *w->desired_matrix->method = 0;
13589 debug_method_add (w, "optimization 1");
13590 #endif
13591 #ifdef HAVE_WINDOW_SYSTEM
13592 update_window_fringes (w, false);
13593 #endif
13594 goto update;
13595 }
13596 else
13597 goto cancel;
13598 }
13599 else if (/* Cursor position hasn't changed. */
13600 PT == w->last_point
13601 /* Make sure the cursor was last displayed
13602 in this window. Otherwise we have to reposition it. */
13603
13604 /* PXW: Must be converted to pixels, probably. */
13605 && 0 <= w->cursor.vpos
13606 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13607 {
13608 if (!must_finish)
13609 {
13610 do_pending_window_change (true);
13611 /* If selected_window changed, redisplay again. */
13612 if (WINDOWP (selected_window)
13613 && (w = XWINDOW (selected_window)) != sw)
13614 goto retry;
13615
13616 /* We used to always goto end_of_redisplay here, but this
13617 isn't enough if we have a blinking cursor. */
13618 if (w->cursor_off_p == w->last_cursor_off_p)
13619 goto end_of_redisplay;
13620 }
13621 goto update;
13622 }
13623 /* If highlighting the region, or if the cursor is in the echo area,
13624 then we can't just move the cursor. */
13625 else if (NILP (Vshow_trailing_whitespace)
13626 && !cursor_in_echo_area)
13627 {
13628 struct it it;
13629 struct glyph_row *row;
13630
13631 /* Skip from tlbufpos to PT and see where it is. Note that
13632 PT may be in invisible text. If so, we will end at the
13633 next visible position. */
13634 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13635 NULL, DEFAULT_FACE_ID);
13636 it.current_x = this_line_start_x;
13637 it.current_y = this_line_y;
13638 it.vpos = this_line_vpos;
13639
13640 /* The call to move_it_to stops in front of PT, but
13641 moves over before-strings. */
13642 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13643
13644 if (it.vpos == this_line_vpos
13645 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13646 row->enabled_p))
13647 {
13648 eassert (this_line_vpos == it.vpos);
13649 eassert (this_line_y == it.current_y);
13650 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13651 #ifdef GLYPH_DEBUG
13652 *w->desired_matrix->method = 0;
13653 debug_method_add (w, "optimization 3");
13654 #endif
13655 goto update;
13656 }
13657 else
13658 goto cancel;
13659 }
13660
13661 cancel:
13662 /* Text changed drastically or point moved off of line. */
13663 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13664 }
13665
13666 CHARPOS (this_line_start_pos) = 0;
13667 ++clear_face_cache_count;
13668 #ifdef HAVE_WINDOW_SYSTEM
13669 ++clear_image_cache_count;
13670 #endif
13671
13672 /* Build desired matrices, and update the display. If
13673 consider_all_windows_p, do it for all windows on all frames.
13674 Otherwise do it for selected_window, only. */
13675
13676 if (consider_all_windows_p)
13677 {
13678 FOR_EACH_FRAME (tail, frame)
13679 XFRAME (frame)->updated_p = false;
13680
13681 propagate_buffer_redisplay ();
13682
13683 FOR_EACH_FRAME (tail, frame)
13684 {
13685 struct frame *f = XFRAME (frame);
13686
13687 /* We don't have to do anything for unselected terminal
13688 frames. */
13689 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13690 && !EQ (FRAME_TTY (f)->top_frame, frame))
13691 continue;
13692
13693 retry_frame:
13694
13695 #if defined (HAVE_WINDOW_SYSTEM) && !defined (USE_GTK) && !defined (HAVE_NS)
13696 /* Redisplay internal tool bar if this is the first time so we
13697 can adjust the frame height right now, if necessary. */
13698 if (!f->tool_bar_redisplayed_once)
13699 {
13700 if (redisplay_tool_bar (f))
13701 adjust_frame_glyphs (f);
13702 f->tool_bar_redisplayed_once = true;
13703 }
13704 #endif
13705
13706 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13707 {
13708 bool gcscrollbars
13709 /* Only GC scrollbars when we redisplay the whole frame. */
13710 = f->redisplay || !REDISPLAY_SOME_P ();
13711 /* Mark all the scroll bars to be removed; we'll redeem
13712 the ones we want when we redisplay their windows. */
13713 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13714 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13715
13716 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13717 redisplay_windows (FRAME_ROOT_WINDOW (f));
13718 /* Remember that the invisible frames need to be redisplayed next
13719 time they're visible. */
13720 else if (!REDISPLAY_SOME_P ())
13721 f->redisplay = true;
13722
13723 /* The X error handler may have deleted that frame. */
13724 if (!FRAME_LIVE_P (f))
13725 continue;
13726
13727 /* Any scroll bars which redisplay_windows should have
13728 nuked should now go away. */
13729 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13730 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13731
13732 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13733 {
13734 /* If fonts changed on visible frame, display again. */
13735 if (f->fonts_changed)
13736 {
13737 adjust_frame_glyphs (f);
13738 f->fonts_changed = false;
13739 goto retry_frame;
13740 }
13741
13742 /* See if we have to hscroll. */
13743 if (!f->already_hscrolled_p)
13744 {
13745 f->already_hscrolled_p = true;
13746 if (hscroll_windows (f->root_window))
13747 goto retry_frame;
13748 }
13749
13750 /* Prevent various kinds of signals during display
13751 update. stdio is not robust about handling
13752 signals, which can cause an apparent I/O error. */
13753 if (interrupt_input)
13754 unrequest_sigio ();
13755 STOP_POLLING;
13756
13757 pending |= update_frame (f, false, false);
13758 f->cursor_type_changed = false;
13759 f->updated_p = true;
13760 }
13761 }
13762 }
13763
13764 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13765
13766 if (!pending)
13767 {
13768 /* Do the mark_window_display_accurate after all windows have
13769 been redisplayed because this call resets flags in buffers
13770 which are needed for proper redisplay. */
13771 FOR_EACH_FRAME (tail, frame)
13772 {
13773 struct frame *f = XFRAME (frame);
13774 if (f->updated_p)
13775 {
13776 f->redisplay = false;
13777 mark_window_display_accurate (f->root_window, true);
13778 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13779 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13780 }
13781 }
13782 }
13783 }
13784 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13785 {
13786 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13787 struct frame *mini_frame;
13788
13789 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13790 /* Use list_of_error, not Qerror, so that
13791 we catch only errors and don't run the debugger. */
13792 internal_condition_case_1 (redisplay_window_1, selected_window,
13793 list_of_error,
13794 redisplay_window_error);
13795 if (update_miniwindow_p)
13796 internal_condition_case_1 (redisplay_window_1, mini_window,
13797 list_of_error,
13798 redisplay_window_error);
13799
13800 /* Compare desired and current matrices, perform output. */
13801
13802 update:
13803 /* If fonts changed, display again. */
13804 if (sf->fonts_changed)
13805 goto retry;
13806
13807 /* Prevent various kinds of signals during display update.
13808 stdio is not robust about handling signals,
13809 which can cause an apparent I/O error. */
13810 if (interrupt_input)
13811 unrequest_sigio ();
13812 STOP_POLLING;
13813
13814 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13815 {
13816 if (hscroll_windows (selected_window))
13817 goto retry;
13818
13819 XWINDOW (selected_window)->must_be_updated_p = true;
13820 pending = update_frame (sf, false, false);
13821 sf->cursor_type_changed = false;
13822 }
13823
13824 /* We may have called echo_area_display at the top of this
13825 function. If the echo area is on another frame, that may
13826 have put text on a frame other than the selected one, so the
13827 above call to update_frame would not have caught it. Catch
13828 it here. */
13829 mini_window = FRAME_MINIBUF_WINDOW (sf);
13830 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13831
13832 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13833 {
13834 XWINDOW (mini_window)->must_be_updated_p = true;
13835 pending |= update_frame (mini_frame, false, false);
13836 mini_frame->cursor_type_changed = false;
13837 if (!pending && hscroll_windows (mini_window))
13838 goto retry;
13839 }
13840 }
13841
13842 /* If display was paused because of pending input, make sure we do a
13843 thorough update the next time. */
13844 if (pending)
13845 {
13846 /* Prevent the optimization at the beginning of
13847 redisplay_internal that tries a single-line update of the
13848 line containing the cursor in the selected window. */
13849 CHARPOS (this_line_start_pos) = 0;
13850
13851 /* Let the overlay arrow be updated the next time. */
13852 update_overlay_arrows (0);
13853
13854 /* If we pause after scrolling, some rows in the current
13855 matrices of some windows are not valid. */
13856 if (!WINDOW_FULL_WIDTH_P (w)
13857 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13858 update_mode_lines = 36;
13859 }
13860 else
13861 {
13862 if (!consider_all_windows_p)
13863 {
13864 /* This has already been done above if
13865 consider_all_windows_p is set. */
13866 if (XBUFFER (w->contents)->text->redisplay
13867 && buffer_window_count (XBUFFER (w->contents)) > 1)
13868 /* This can happen if b->text->redisplay was set during
13869 jit-lock. */
13870 propagate_buffer_redisplay ();
13871 mark_window_display_accurate_1 (w, true);
13872
13873 /* Say overlay arrows are up to date. */
13874 update_overlay_arrows (1);
13875
13876 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13877 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13878 }
13879
13880 update_mode_lines = 0;
13881 windows_or_buffers_changed = 0;
13882 }
13883
13884 /* Start SIGIO interrupts coming again. Having them off during the
13885 code above makes it less likely one will discard output, but not
13886 impossible, since there might be stuff in the system buffer here.
13887 But it is much hairier to try to do anything about that. */
13888 if (interrupt_input)
13889 request_sigio ();
13890 RESUME_POLLING;
13891
13892 /* If a frame has become visible which was not before, redisplay
13893 again, so that we display it. Expose events for such a frame
13894 (which it gets when becoming visible) don't call the parts of
13895 redisplay constructing glyphs, so simply exposing a frame won't
13896 display anything in this case. So, we have to display these
13897 frames here explicitly. */
13898 if (!pending)
13899 {
13900 int new_count = 0;
13901
13902 FOR_EACH_FRAME (tail, frame)
13903 {
13904 if (XFRAME (frame)->visible)
13905 new_count++;
13906 }
13907
13908 if (new_count != number_of_visible_frames)
13909 windows_or_buffers_changed = 52;
13910 }
13911
13912 /* Change frame size now if a change is pending. */
13913 do_pending_window_change (true);
13914
13915 /* If we just did a pending size change, or have additional
13916 visible frames, or selected_window changed, redisplay again. */
13917 if ((windows_or_buffers_changed && !pending)
13918 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13919 goto retry;
13920
13921 /* Clear the face and image caches.
13922
13923 We used to do this only if consider_all_windows_p. But the cache
13924 needs to be cleared if a timer creates images in the current
13925 buffer (e.g. the test case in Bug#6230). */
13926
13927 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13928 {
13929 clear_face_cache (false);
13930 clear_face_cache_count = 0;
13931 }
13932
13933 #ifdef HAVE_WINDOW_SYSTEM
13934 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13935 {
13936 clear_image_caches (Qnil);
13937 clear_image_cache_count = 0;
13938 }
13939 #endif /* HAVE_WINDOW_SYSTEM */
13940
13941 end_of_redisplay:
13942 #ifdef HAVE_NS
13943 ns_set_doc_edited ();
13944 #endif
13945 if (interrupt_input && interrupts_deferred)
13946 request_sigio ();
13947
13948 unbind_to (count, Qnil);
13949 RESUME_POLLING;
13950 }
13951
13952
13953 /* Redisplay, but leave alone any recent echo area message unless
13954 another message has been requested in its place.
13955
13956 This is useful in situations where you need to redisplay but no
13957 user action has occurred, making it inappropriate for the message
13958 area to be cleared. See tracking_off and
13959 wait_reading_process_output for examples of these situations.
13960
13961 FROM_WHERE is an integer saying from where this function was
13962 called. This is useful for debugging. */
13963
13964 void
13965 redisplay_preserve_echo_area (int from_where)
13966 {
13967 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13968
13969 if (!NILP (echo_area_buffer[1]))
13970 {
13971 /* We have a previously displayed message, but no current
13972 message. Redisplay the previous message. */
13973 display_last_displayed_message_p = true;
13974 redisplay_internal ();
13975 display_last_displayed_message_p = false;
13976 }
13977 else
13978 redisplay_internal ();
13979
13980 flush_frame (SELECTED_FRAME ());
13981 }
13982
13983
13984 /* Function registered with record_unwind_protect in redisplay_internal. */
13985
13986 static void
13987 unwind_redisplay (void)
13988 {
13989 redisplaying_p = false;
13990 }
13991
13992
13993 /* Mark the display of leaf window W as accurate or inaccurate.
13994 If ACCURATE_P, mark display of W as accurate.
13995 If !ACCURATE_P, arrange for W to be redisplayed the next
13996 time redisplay_internal is called. */
13997
13998 static void
13999 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14000 {
14001 struct buffer *b = XBUFFER (w->contents);
14002
14003 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14004 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14005 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14006
14007 if (accurate_p)
14008 {
14009 b->clip_changed = false;
14010 b->prevent_redisplay_optimizations_p = false;
14011 eassert (buffer_window_count (b) > 0);
14012 /* Resetting b->text->redisplay is problematic!
14013 In order to make it safer to do it here, redisplay_internal must
14014 have copied all b->text->redisplay to their respective windows. */
14015 b->text->redisplay = false;
14016
14017 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14018 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14019 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14020 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14021
14022 w->current_matrix->buffer = b;
14023 w->current_matrix->begv = BUF_BEGV (b);
14024 w->current_matrix->zv = BUF_ZV (b);
14025
14026 w->last_cursor_vpos = w->cursor.vpos;
14027 w->last_cursor_off_p = w->cursor_off_p;
14028
14029 if (w == XWINDOW (selected_window))
14030 w->last_point = BUF_PT (b);
14031 else
14032 w->last_point = marker_position (w->pointm);
14033
14034 w->window_end_valid = true;
14035 w->update_mode_line = false;
14036 }
14037
14038 w->redisplay = !accurate_p;
14039 }
14040
14041
14042 /* Mark the display of windows in the window tree rooted at WINDOW as
14043 accurate or inaccurate. If ACCURATE_P, mark display of
14044 windows as accurate. If !ACCURATE_P, arrange for windows to
14045 be redisplayed the next time redisplay_internal is called. */
14046
14047 void
14048 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14049 {
14050 struct window *w;
14051
14052 for (; !NILP (window); window = w->next)
14053 {
14054 w = XWINDOW (window);
14055 if (WINDOWP (w->contents))
14056 mark_window_display_accurate (w->contents, accurate_p);
14057 else
14058 mark_window_display_accurate_1 (w, accurate_p);
14059 }
14060
14061 if (accurate_p)
14062 update_overlay_arrows (1);
14063 else
14064 /* Force a thorough redisplay the next time by setting
14065 last_arrow_position and last_arrow_string to t, which is
14066 unequal to any useful value of Voverlay_arrow_... */
14067 update_overlay_arrows (-1);
14068 }
14069
14070
14071 /* Return value in display table DP (Lisp_Char_Table *) for character
14072 C. Since a display table doesn't have any parent, we don't have to
14073 follow parent. Do not call this function directly but use the
14074 macro DISP_CHAR_VECTOR. */
14075
14076 Lisp_Object
14077 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14078 {
14079 Lisp_Object val;
14080
14081 if (ASCII_CHAR_P (c))
14082 {
14083 val = dp->ascii;
14084 if (SUB_CHAR_TABLE_P (val))
14085 val = XSUB_CHAR_TABLE (val)->contents[c];
14086 }
14087 else
14088 {
14089 Lisp_Object table;
14090
14091 XSETCHAR_TABLE (table, dp);
14092 val = char_table_ref (table, c);
14093 }
14094 if (NILP (val))
14095 val = dp->defalt;
14096 return val;
14097 }
14098
14099
14100 \f
14101 /***********************************************************************
14102 Window Redisplay
14103 ***********************************************************************/
14104
14105 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14106
14107 static void
14108 redisplay_windows (Lisp_Object window)
14109 {
14110 while (!NILP (window))
14111 {
14112 struct window *w = XWINDOW (window);
14113
14114 if (WINDOWP (w->contents))
14115 redisplay_windows (w->contents);
14116 else if (BUFFERP (w->contents))
14117 {
14118 displayed_buffer = XBUFFER (w->contents);
14119 /* Use list_of_error, not Qerror, so that
14120 we catch only errors and don't run the debugger. */
14121 internal_condition_case_1 (redisplay_window_0, window,
14122 list_of_error,
14123 redisplay_window_error);
14124 }
14125
14126 window = w->next;
14127 }
14128 }
14129
14130 static Lisp_Object
14131 redisplay_window_error (Lisp_Object ignore)
14132 {
14133 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14134 return Qnil;
14135 }
14136
14137 static Lisp_Object
14138 redisplay_window_0 (Lisp_Object window)
14139 {
14140 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14141 redisplay_window (window, false);
14142 return Qnil;
14143 }
14144
14145 static Lisp_Object
14146 redisplay_window_1 (Lisp_Object window)
14147 {
14148 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14149 redisplay_window (window, true);
14150 return Qnil;
14151 }
14152 \f
14153
14154 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14155 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14156 which positions recorded in ROW differ from current buffer
14157 positions.
14158
14159 Return true iff cursor is on this row. */
14160
14161 static bool
14162 set_cursor_from_row (struct window *w, struct glyph_row *row,
14163 struct glyph_matrix *matrix,
14164 ptrdiff_t delta, ptrdiff_t delta_bytes,
14165 int dy, int dvpos)
14166 {
14167 struct glyph *glyph = row->glyphs[TEXT_AREA];
14168 struct glyph *end = glyph + row->used[TEXT_AREA];
14169 struct glyph *cursor = NULL;
14170 /* The last known character position in row. */
14171 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14172 int x = row->x;
14173 ptrdiff_t pt_old = PT - delta;
14174 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14175 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14176 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14177 /* A glyph beyond the edge of TEXT_AREA which we should never
14178 touch. */
14179 struct glyph *glyphs_end = end;
14180 /* True means we've found a match for cursor position, but that
14181 glyph has the avoid_cursor_p flag set. */
14182 bool match_with_avoid_cursor = false;
14183 /* True means we've seen at least one glyph that came from a
14184 display string. */
14185 bool string_seen = false;
14186 /* Largest and smallest buffer positions seen so far during scan of
14187 glyph row. */
14188 ptrdiff_t bpos_max = pos_before;
14189 ptrdiff_t bpos_min = pos_after;
14190 /* Last buffer position covered by an overlay string with an integer
14191 `cursor' property. */
14192 ptrdiff_t bpos_covered = 0;
14193 /* True means the display string on which to display the cursor
14194 comes from a text property, not from an overlay. */
14195 bool string_from_text_prop = false;
14196
14197 /* Don't even try doing anything if called for a mode-line or
14198 header-line row, since the rest of the code isn't prepared to
14199 deal with such calamities. */
14200 eassert (!row->mode_line_p);
14201 if (row->mode_line_p)
14202 return false;
14203
14204 /* Skip over glyphs not having an object at the start and the end of
14205 the row. These are special glyphs like truncation marks on
14206 terminal frames. */
14207 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14208 {
14209 if (!row->reversed_p)
14210 {
14211 while (glyph < end
14212 && NILP (glyph->object)
14213 && glyph->charpos < 0)
14214 {
14215 x += glyph->pixel_width;
14216 ++glyph;
14217 }
14218 while (end > glyph
14219 && NILP ((end - 1)->object)
14220 /* CHARPOS is zero for blanks and stretch glyphs
14221 inserted by extend_face_to_end_of_line. */
14222 && (end - 1)->charpos <= 0)
14223 --end;
14224 glyph_before = glyph - 1;
14225 glyph_after = end;
14226 }
14227 else
14228 {
14229 struct glyph *g;
14230
14231 /* If the glyph row is reversed, we need to process it from back
14232 to front, so swap the edge pointers. */
14233 glyphs_end = end = glyph - 1;
14234 glyph += row->used[TEXT_AREA] - 1;
14235
14236 while (glyph > end + 1
14237 && NILP (glyph->object)
14238 && glyph->charpos < 0)
14239 {
14240 --glyph;
14241 x -= glyph->pixel_width;
14242 }
14243 if (NILP (glyph->object) && glyph->charpos < 0)
14244 --glyph;
14245 /* By default, in reversed rows we put the cursor on the
14246 rightmost (first in the reading order) glyph. */
14247 for (g = end + 1; g < glyph; g++)
14248 x += g->pixel_width;
14249 while (end < glyph
14250 && NILP ((end + 1)->object)
14251 && (end + 1)->charpos <= 0)
14252 ++end;
14253 glyph_before = glyph + 1;
14254 glyph_after = end;
14255 }
14256 }
14257 else if (row->reversed_p)
14258 {
14259 /* In R2L rows that don't display text, put the cursor on the
14260 rightmost glyph. Case in point: an empty last line that is
14261 part of an R2L paragraph. */
14262 cursor = end - 1;
14263 /* Avoid placing the cursor on the last glyph of the row, where
14264 on terminal frames we hold the vertical border between
14265 adjacent windows. */
14266 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14267 && !WINDOW_RIGHTMOST_P (w)
14268 && cursor == row->glyphs[LAST_AREA] - 1)
14269 cursor--;
14270 x = -1; /* will be computed below, at label compute_x */
14271 }
14272
14273 /* Step 1: Try to find the glyph whose character position
14274 corresponds to point. If that's not possible, find 2 glyphs
14275 whose character positions are the closest to point, one before
14276 point, the other after it. */
14277 if (!row->reversed_p)
14278 while (/* not marched to end of glyph row */
14279 glyph < end
14280 /* glyph was not inserted by redisplay for internal purposes */
14281 && !NILP (glyph->object))
14282 {
14283 if (BUFFERP (glyph->object))
14284 {
14285 ptrdiff_t dpos = glyph->charpos - pt_old;
14286
14287 if (glyph->charpos > bpos_max)
14288 bpos_max = glyph->charpos;
14289 if (glyph->charpos < bpos_min)
14290 bpos_min = glyph->charpos;
14291 if (!glyph->avoid_cursor_p)
14292 {
14293 /* If we hit point, we've found the glyph on which to
14294 display the cursor. */
14295 if (dpos == 0)
14296 {
14297 match_with_avoid_cursor = false;
14298 break;
14299 }
14300 /* See if we've found a better approximation to
14301 POS_BEFORE or to POS_AFTER. */
14302 if (0 > dpos && dpos > pos_before - pt_old)
14303 {
14304 pos_before = glyph->charpos;
14305 glyph_before = glyph;
14306 }
14307 else if (0 < dpos && dpos < pos_after - pt_old)
14308 {
14309 pos_after = glyph->charpos;
14310 glyph_after = glyph;
14311 }
14312 }
14313 else if (dpos == 0)
14314 match_with_avoid_cursor = true;
14315 }
14316 else if (STRINGP (glyph->object))
14317 {
14318 Lisp_Object chprop;
14319 ptrdiff_t glyph_pos = glyph->charpos;
14320
14321 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14322 glyph->object);
14323 if (!NILP (chprop))
14324 {
14325 /* If the string came from a `display' text property,
14326 look up the buffer position of that property and
14327 use that position to update bpos_max, as if we
14328 actually saw such a position in one of the row's
14329 glyphs. This helps with supporting integer values
14330 of `cursor' property on the display string in
14331 situations where most or all of the row's buffer
14332 text is completely covered by display properties,
14333 so that no glyph with valid buffer positions is
14334 ever seen in the row. */
14335 ptrdiff_t prop_pos =
14336 string_buffer_position_lim (glyph->object, pos_before,
14337 pos_after, false);
14338
14339 if (prop_pos >= pos_before)
14340 bpos_max = prop_pos;
14341 }
14342 if (INTEGERP (chprop))
14343 {
14344 bpos_covered = bpos_max + XINT (chprop);
14345 /* If the `cursor' property covers buffer positions up
14346 to and including point, we should display cursor on
14347 this glyph. Note that, if a `cursor' property on one
14348 of the string's characters has an integer value, we
14349 will break out of the loop below _before_ we get to
14350 the position match above. IOW, integer values of
14351 the `cursor' property override the "exact match for
14352 point" strategy of positioning the cursor. */
14353 /* Implementation note: bpos_max == pt_old when, e.g.,
14354 we are in an empty line, where bpos_max is set to
14355 MATRIX_ROW_START_CHARPOS, see above. */
14356 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14357 {
14358 cursor = glyph;
14359 break;
14360 }
14361 }
14362
14363 string_seen = true;
14364 }
14365 x += glyph->pixel_width;
14366 ++glyph;
14367 }
14368 else if (glyph > end) /* row is reversed */
14369 while (!NILP (glyph->object))
14370 {
14371 if (BUFFERP (glyph->object))
14372 {
14373 ptrdiff_t dpos = glyph->charpos - pt_old;
14374
14375 if (glyph->charpos > bpos_max)
14376 bpos_max = glyph->charpos;
14377 if (glyph->charpos < bpos_min)
14378 bpos_min = glyph->charpos;
14379 if (!glyph->avoid_cursor_p)
14380 {
14381 if (dpos == 0)
14382 {
14383 match_with_avoid_cursor = false;
14384 break;
14385 }
14386 if (0 > dpos && dpos > pos_before - pt_old)
14387 {
14388 pos_before = glyph->charpos;
14389 glyph_before = glyph;
14390 }
14391 else if (0 < dpos && dpos < pos_after - pt_old)
14392 {
14393 pos_after = glyph->charpos;
14394 glyph_after = glyph;
14395 }
14396 }
14397 else if (dpos == 0)
14398 match_with_avoid_cursor = true;
14399 }
14400 else if (STRINGP (glyph->object))
14401 {
14402 Lisp_Object chprop;
14403 ptrdiff_t glyph_pos = glyph->charpos;
14404
14405 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14406 glyph->object);
14407 if (!NILP (chprop))
14408 {
14409 ptrdiff_t prop_pos =
14410 string_buffer_position_lim (glyph->object, pos_before,
14411 pos_after, false);
14412
14413 if (prop_pos >= pos_before)
14414 bpos_max = prop_pos;
14415 }
14416 if (INTEGERP (chprop))
14417 {
14418 bpos_covered = bpos_max + XINT (chprop);
14419 /* If the `cursor' property covers buffer positions up
14420 to and including point, we should display cursor on
14421 this glyph. */
14422 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14423 {
14424 cursor = glyph;
14425 break;
14426 }
14427 }
14428 string_seen = true;
14429 }
14430 --glyph;
14431 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14432 {
14433 x--; /* can't use any pixel_width */
14434 break;
14435 }
14436 x -= glyph->pixel_width;
14437 }
14438
14439 /* Step 2: If we didn't find an exact match for point, we need to
14440 look for a proper place to put the cursor among glyphs between
14441 GLYPH_BEFORE and GLYPH_AFTER. */
14442 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14443 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14444 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14445 {
14446 /* An empty line has a single glyph whose OBJECT is nil and
14447 whose CHARPOS is the position of a newline on that line.
14448 Note that on a TTY, there are more glyphs after that, which
14449 were produced by extend_face_to_end_of_line, but their
14450 CHARPOS is zero or negative. */
14451 bool empty_line_p =
14452 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14453 && NILP (glyph->object) && glyph->charpos > 0
14454 /* On a TTY, continued and truncated rows also have a glyph at
14455 their end whose OBJECT is nil and whose CHARPOS is
14456 positive (the continuation and truncation glyphs), but such
14457 rows are obviously not "empty". */
14458 && !(row->continued_p || row->truncated_on_right_p));
14459
14460 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14461 {
14462 ptrdiff_t ellipsis_pos;
14463
14464 /* Scan back over the ellipsis glyphs. */
14465 if (!row->reversed_p)
14466 {
14467 ellipsis_pos = (glyph - 1)->charpos;
14468 while (glyph > row->glyphs[TEXT_AREA]
14469 && (glyph - 1)->charpos == ellipsis_pos)
14470 glyph--, x -= glyph->pixel_width;
14471 /* That loop always goes one position too far, including
14472 the glyph before the ellipsis. So scan forward over
14473 that one. */
14474 x += glyph->pixel_width;
14475 glyph++;
14476 }
14477 else /* row is reversed */
14478 {
14479 ellipsis_pos = (glyph + 1)->charpos;
14480 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14481 && (glyph + 1)->charpos == ellipsis_pos)
14482 glyph++, x += glyph->pixel_width;
14483 x -= glyph->pixel_width;
14484 glyph--;
14485 }
14486 }
14487 else if (match_with_avoid_cursor)
14488 {
14489 cursor = glyph_after;
14490 x = -1;
14491 }
14492 else if (string_seen)
14493 {
14494 int incr = row->reversed_p ? -1 : +1;
14495
14496 /* Need to find the glyph that came out of a string which is
14497 present at point. That glyph is somewhere between
14498 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14499 positioned between POS_BEFORE and POS_AFTER in the
14500 buffer. */
14501 struct glyph *start, *stop;
14502 ptrdiff_t pos = pos_before;
14503
14504 x = -1;
14505
14506 /* If the row ends in a newline from a display string,
14507 reordering could have moved the glyphs belonging to the
14508 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14509 in this case we extend the search to the last glyph in
14510 the row that was not inserted by redisplay. */
14511 if (row->ends_in_newline_from_string_p)
14512 {
14513 glyph_after = end;
14514 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14515 }
14516
14517 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14518 correspond to POS_BEFORE and POS_AFTER, respectively. We
14519 need START and STOP in the order that corresponds to the
14520 row's direction as given by its reversed_p flag. If the
14521 directionality of characters between POS_BEFORE and
14522 POS_AFTER is the opposite of the row's base direction,
14523 these characters will have been reordered for display,
14524 and we need to reverse START and STOP. */
14525 if (!row->reversed_p)
14526 {
14527 start = min (glyph_before, glyph_after);
14528 stop = max (glyph_before, glyph_after);
14529 }
14530 else
14531 {
14532 start = max (glyph_before, glyph_after);
14533 stop = min (glyph_before, glyph_after);
14534 }
14535 for (glyph = start + incr;
14536 row->reversed_p ? glyph > stop : glyph < stop; )
14537 {
14538
14539 /* Any glyphs that come from the buffer are here because
14540 of bidi reordering. Skip them, and only pay
14541 attention to glyphs that came from some string. */
14542 if (STRINGP (glyph->object))
14543 {
14544 Lisp_Object str;
14545 ptrdiff_t tem;
14546 /* If the display property covers the newline, we
14547 need to search for it one position farther. */
14548 ptrdiff_t lim = pos_after
14549 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14550
14551 string_from_text_prop = false;
14552 str = glyph->object;
14553 tem = string_buffer_position_lim (str, pos, lim, false);
14554 if (tem == 0 /* from overlay */
14555 || pos <= tem)
14556 {
14557 /* If the string from which this glyph came is
14558 found in the buffer at point, or at position
14559 that is closer to point than pos_after, then
14560 we've found the glyph we've been looking for.
14561 If it comes from an overlay (tem == 0), and
14562 it has the `cursor' property on one of its
14563 glyphs, record that glyph as a candidate for
14564 displaying the cursor. (As in the
14565 unidirectional version, we will display the
14566 cursor on the last candidate we find.) */
14567 if (tem == 0
14568 || tem == pt_old
14569 || (tem - pt_old > 0 && tem < pos_after))
14570 {
14571 /* The glyphs from this string could have
14572 been reordered. Find the one with the
14573 smallest string position. Or there could
14574 be a character in the string with the
14575 `cursor' property, which means display
14576 cursor on that character's glyph. */
14577 ptrdiff_t strpos = glyph->charpos;
14578
14579 if (tem)
14580 {
14581 cursor = glyph;
14582 string_from_text_prop = true;
14583 }
14584 for ( ;
14585 (row->reversed_p ? glyph > stop : glyph < stop)
14586 && EQ (glyph->object, str);
14587 glyph += incr)
14588 {
14589 Lisp_Object cprop;
14590 ptrdiff_t gpos = glyph->charpos;
14591
14592 cprop = Fget_char_property (make_number (gpos),
14593 Qcursor,
14594 glyph->object);
14595 if (!NILP (cprop))
14596 {
14597 cursor = glyph;
14598 break;
14599 }
14600 if (tem && glyph->charpos < strpos)
14601 {
14602 strpos = glyph->charpos;
14603 cursor = glyph;
14604 }
14605 }
14606
14607 if (tem == pt_old
14608 || (tem - pt_old > 0 && tem < pos_after))
14609 goto compute_x;
14610 }
14611 if (tem)
14612 pos = tem + 1; /* don't find previous instances */
14613 }
14614 /* This string is not what we want; skip all of the
14615 glyphs that came from it. */
14616 while ((row->reversed_p ? glyph > stop : glyph < stop)
14617 && EQ (glyph->object, str))
14618 glyph += incr;
14619 }
14620 else
14621 glyph += incr;
14622 }
14623
14624 /* If we reached the end of the line, and END was from a string,
14625 the cursor is not on this line. */
14626 if (cursor == NULL
14627 && (row->reversed_p ? glyph <= end : glyph >= end)
14628 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14629 && STRINGP (end->object)
14630 && row->continued_p)
14631 return false;
14632 }
14633 /* A truncated row may not include PT among its character positions.
14634 Setting the cursor inside the scroll margin will trigger
14635 recalculation of hscroll in hscroll_window_tree. But if a
14636 display string covers point, defer to the string-handling
14637 code below to figure this out. */
14638 else if (row->truncated_on_left_p && pt_old < bpos_min)
14639 {
14640 cursor = glyph_before;
14641 x = -1;
14642 }
14643 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14644 /* Zero-width characters produce no glyphs. */
14645 || (!empty_line_p
14646 && (row->reversed_p
14647 ? glyph_after > glyphs_end
14648 : glyph_after < glyphs_end)))
14649 {
14650 cursor = glyph_after;
14651 x = -1;
14652 }
14653 }
14654
14655 compute_x:
14656 if (cursor != NULL)
14657 glyph = cursor;
14658 else if (glyph == glyphs_end
14659 && pos_before == pos_after
14660 && STRINGP ((row->reversed_p
14661 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14662 : row->glyphs[TEXT_AREA])->object))
14663 {
14664 /* If all the glyphs of this row came from strings, put the
14665 cursor on the first glyph of the row. This avoids having the
14666 cursor outside of the text area in this very rare and hard
14667 use case. */
14668 glyph =
14669 row->reversed_p
14670 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14671 : row->glyphs[TEXT_AREA];
14672 }
14673 if (x < 0)
14674 {
14675 struct glyph *g;
14676
14677 /* Need to compute x that corresponds to GLYPH. */
14678 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14679 {
14680 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14681 emacs_abort ();
14682 x += g->pixel_width;
14683 }
14684 }
14685
14686 /* ROW could be part of a continued line, which, under bidi
14687 reordering, might have other rows whose start and end charpos
14688 occlude point. Only set w->cursor if we found a better
14689 approximation to the cursor position than we have from previously
14690 examined candidate rows belonging to the same continued line. */
14691 if (/* We already have a candidate row. */
14692 w->cursor.vpos >= 0
14693 /* That candidate is not the row we are processing. */
14694 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14695 /* Make sure cursor.vpos specifies a row whose start and end
14696 charpos occlude point, and it is valid candidate for being a
14697 cursor-row. This is because some callers of this function
14698 leave cursor.vpos at the row where the cursor was displayed
14699 during the last redisplay cycle. */
14700 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14701 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14702 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14703 {
14704 struct glyph *g1
14705 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14706
14707 /* Don't consider glyphs that are outside TEXT_AREA. */
14708 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14709 return false;
14710 /* Keep the candidate whose buffer position is the closest to
14711 point or has the `cursor' property. */
14712 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14713 w->cursor.hpos >= 0
14714 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14715 && ((BUFFERP (g1->object)
14716 && (g1->charpos == pt_old /* An exact match always wins. */
14717 || (BUFFERP (glyph->object)
14718 && eabs (g1->charpos - pt_old)
14719 < eabs (glyph->charpos - pt_old))))
14720 /* Previous candidate is a glyph from a string that has
14721 a non-nil `cursor' property. */
14722 || (STRINGP (g1->object)
14723 && (!NILP (Fget_char_property (make_number (g1->charpos),
14724 Qcursor, g1->object))
14725 /* Previous candidate is from the same display
14726 string as this one, and the display string
14727 came from a text property. */
14728 || (EQ (g1->object, glyph->object)
14729 && string_from_text_prop)
14730 /* this candidate is from newline and its
14731 position is not an exact match */
14732 || (NILP (glyph->object)
14733 && glyph->charpos != pt_old)))))
14734 return false;
14735 /* If this candidate gives an exact match, use that. */
14736 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14737 /* If this candidate is a glyph created for the
14738 terminating newline of a line, and point is on that
14739 newline, it wins because it's an exact match. */
14740 || (!row->continued_p
14741 && NILP (glyph->object)
14742 && glyph->charpos == 0
14743 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14744 /* Otherwise, keep the candidate that comes from a row
14745 spanning less buffer positions. This may win when one or
14746 both candidate positions are on glyphs that came from
14747 display strings, for which we cannot compare buffer
14748 positions. */
14749 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14750 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14751 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14752 return false;
14753 }
14754 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14755 w->cursor.x = x;
14756 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14757 w->cursor.y = row->y + dy;
14758
14759 if (w == XWINDOW (selected_window))
14760 {
14761 if (!row->continued_p
14762 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14763 && row->x == 0)
14764 {
14765 this_line_buffer = XBUFFER (w->contents);
14766
14767 CHARPOS (this_line_start_pos)
14768 = MATRIX_ROW_START_CHARPOS (row) + delta;
14769 BYTEPOS (this_line_start_pos)
14770 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14771
14772 CHARPOS (this_line_end_pos)
14773 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14774 BYTEPOS (this_line_end_pos)
14775 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14776
14777 this_line_y = w->cursor.y;
14778 this_line_pixel_height = row->height;
14779 this_line_vpos = w->cursor.vpos;
14780 this_line_start_x = row->x;
14781 }
14782 else
14783 CHARPOS (this_line_start_pos) = 0;
14784 }
14785
14786 return true;
14787 }
14788
14789
14790 /* Run window scroll functions, if any, for WINDOW with new window
14791 start STARTP. Sets the window start of WINDOW to that position.
14792
14793 We assume that the window's buffer is really current. */
14794
14795 static struct text_pos
14796 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14797 {
14798 struct window *w = XWINDOW (window);
14799 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14800
14801 eassert (current_buffer == XBUFFER (w->contents));
14802
14803 if (!NILP (Vwindow_scroll_functions))
14804 {
14805 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14806 make_number (CHARPOS (startp)));
14807 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14808 /* In case the hook functions switch buffers. */
14809 set_buffer_internal (XBUFFER (w->contents));
14810 }
14811
14812 return startp;
14813 }
14814
14815
14816 /* Make sure the line containing the cursor is fully visible.
14817 A value of true means there is nothing to be done.
14818 (Either the line is fully visible, or it cannot be made so,
14819 or we cannot tell.)
14820
14821 If FORCE_P, return false even if partial visible cursor row
14822 is higher than window.
14823
14824 If CURRENT_MATRIX_P, use the information from the
14825 window's current glyph matrix; otherwise use the desired glyph
14826 matrix.
14827
14828 A value of false means the caller should do scrolling
14829 as if point had gone off the screen. */
14830
14831 static bool
14832 cursor_row_fully_visible_p (struct window *w, bool force_p,
14833 bool current_matrix_p)
14834 {
14835 struct glyph_matrix *matrix;
14836 struct glyph_row *row;
14837 int window_height;
14838
14839 if (!make_cursor_line_fully_visible_p)
14840 return true;
14841
14842 /* It's not always possible to find the cursor, e.g, when a window
14843 is full of overlay strings. Don't do anything in that case. */
14844 if (w->cursor.vpos < 0)
14845 return true;
14846
14847 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14848 row = MATRIX_ROW (matrix, w->cursor.vpos);
14849
14850 /* If the cursor row is not partially visible, there's nothing to do. */
14851 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14852 return true;
14853
14854 /* If the row the cursor is in is taller than the window's height,
14855 it's not clear what to do, so do nothing. */
14856 window_height = window_box_height (w);
14857 if (row->height >= window_height)
14858 {
14859 if (!force_p || MINI_WINDOW_P (w)
14860 || w->vscroll || w->cursor.vpos == 0)
14861 return true;
14862 }
14863 return false;
14864 }
14865
14866
14867 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14868 means only WINDOW is redisplayed in redisplay_internal.
14869 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14870 in redisplay_window to bring a partially visible line into view in
14871 the case that only the cursor has moved.
14872
14873 LAST_LINE_MISFIT should be true if we're scrolling because the
14874 last screen line's vertical height extends past the end of the screen.
14875
14876 Value is
14877
14878 1 if scrolling succeeded
14879
14880 0 if scrolling didn't find point.
14881
14882 -1 if new fonts have been loaded so that we must interrupt
14883 redisplay, adjust glyph matrices, and try again. */
14884
14885 enum
14886 {
14887 SCROLLING_SUCCESS,
14888 SCROLLING_FAILED,
14889 SCROLLING_NEED_LARGER_MATRICES
14890 };
14891
14892 /* If scroll-conservatively is more than this, never recenter.
14893
14894 If you change this, don't forget to update the doc string of
14895 `scroll-conservatively' and the Emacs manual. */
14896 #define SCROLL_LIMIT 100
14897
14898 static int
14899 try_scrolling (Lisp_Object window, bool just_this_one_p,
14900 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14901 bool temp_scroll_step, bool last_line_misfit)
14902 {
14903 struct window *w = XWINDOW (window);
14904 struct frame *f = XFRAME (w->frame);
14905 struct text_pos pos, startp;
14906 struct it it;
14907 int this_scroll_margin, scroll_max, rc, height;
14908 int dy = 0, amount_to_scroll = 0;
14909 bool scroll_down_p = false;
14910 int extra_scroll_margin_lines = last_line_misfit;
14911 Lisp_Object aggressive;
14912 /* We will never try scrolling more than this number of lines. */
14913 int scroll_limit = SCROLL_LIMIT;
14914 int frame_line_height = default_line_pixel_height (w);
14915 int window_total_lines
14916 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14917
14918 #ifdef GLYPH_DEBUG
14919 debug_method_add (w, "try_scrolling");
14920 #endif
14921
14922 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14923
14924 /* Compute scroll margin height in pixels. We scroll when point is
14925 within this distance from the top or bottom of the window. */
14926 if (scroll_margin > 0)
14927 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14928 * frame_line_height;
14929 else
14930 this_scroll_margin = 0;
14931
14932 /* Force arg_scroll_conservatively to have a reasonable value, to
14933 avoid scrolling too far away with slow move_it_* functions. Note
14934 that the user can supply scroll-conservatively equal to
14935 `most-positive-fixnum', which can be larger than INT_MAX. */
14936 if (arg_scroll_conservatively > scroll_limit)
14937 {
14938 arg_scroll_conservatively = scroll_limit + 1;
14939 scroll_max = scroll_limit * frame_line_height;
14940 }
14941 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14942 /* Compute how much we should try to scroll maximally to bring
14943 point into view. */
14944 scroll_max = (max (scroll_step,
14945 max (arg_scroll_conservatively, temp_scroll_step))
14946 * frame_line_height);
14947 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14948 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14949 /* We're trying to scroll because of aggressive scrolling but no
14950 scroll_step is set. Choose an arbitrary one. */
14951 scroll_max = 10 * frame_line_height;
14952 else
14953 scroll_max = 0;
14954
14955 too_near_end:
14956
14957 /* Decide whether to scroll down. */
14958 if (PT > CHARPOS (startp))
14959 {
14960 int scroll_margin_y;
14961
14962 /* Compute the pixel ypos of the scroll margin, then move IT to
14963 either that ypos or PT, whichever comes first. */
14964 start_display (&it, w, startp);
14965 scroll_margin_y = it.last_visible_y - this_scroll_margin
14966 - frame_line_height * extra_scroll_margin_lines;
14967 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14968 (MOVE_TO_POS | MOVE_TO_Y));
14969
14970 if (PT > CHARPOS (it.current.pos))
14971 {
14972 int y0 = line_bottom_y (&it);
14973 /* Compute how many pixels below window bottom to stop searching
14974 for PT. This avoids costly search for PT that is far away if
14975 the user limited scrolling by a small number of lines, but
14976 always finds PT if scroll_conservatively is set to a large
14977 number, such as most-positive-fixnum. */
14978 int slack = max (scroll_max, 10 * frame_line_height);
14979 int y_to_move = it.last_visible_y + slack;
14980
14981 /* Compute the distance from the scroll margin to PT or to
14982 the scroll limit, whichever comes first. This should
14983 include the height of the cursor line, to make that line
14984 fully visible. */
14985 move_it_to (&it, PT, -1, y_to_move,
14986 -1, MOVE_TO_POS | MOVE_TO_Y);
14987 dy = line_bottom_y (&it) - y0;
14988
14989 if (dy > scroll_max)
14990 return SCROLLING_FAILED;
14991
14992 if (dy > 0)
14993 scroll_down_p = true;
14994 }
14995 }
14996
14997 if (scroll_down_p)
14998 {
14999 /* Point is in or below the bottom scroll margin, so move the
15000 window start down. If scrolling conservatively, move it just
15001 enough down to make point visible. If scroll_step is set,
15002 move it down by scroll_step. */
15003 if (arg_scroll_conservatively)
15004 amount_to_scroll
15005 = min (max (dy, frame_line_height),
15006 frame_line_height * arg_scroll_conservatively);
15007 else if (scroll_step || temp_scroll_step)
15008 amount_to_scroll = scroll_max;
15009 else
15010 {
15011 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15012 height = WINDOW_BOX_TEXT_HEIGHT (w);
15013 if (NUMBERP (aggressive))
15014 {
15015 double float_amount = XFLOATINT (aggressive) * height;
15016 int aggressive_scroll = float_amount;
15017 if (aggressive_scroll == 0 && float_amount > 0)
15018 aggressive_scroll = 1;
15019 /* Don't let point enter the scroll margin near top of
15020 the window. This could happen if the value of
15021 scroll_up_aggressively is too large and there are
15022 non-zero margins, because scroll_up_aggressively
15023 means put point that fraction of window height
15024 _from_the_bottom_margin_. */
15025 if (aggressive_scroll + 2 * this_scroll_margin > height)
15026 aggressive_scroll = height - 2 * this_scroll_margin;
15027 amount_to_scroll = dy + aggressive_scroll;
15028 }
15029 }
15030
15031 if (amount_to_scroll <= 0)
15032 return SCROLLING_FAILED;
15033
15034 start_display (&it, w, startp);
15035 if (arg_scroll_conservatively <= scroll_limit)
15036 move_it_vertically (&it, amount_to_scroll);
15037 else
15038 {
15039 /* Extra precision for users who set scroll-conservatively
15040 to a large number: make sure the amount we scroll
15041 the window start is never less than amount_to_scroll,
15042 which was computed as distance from window bottom to
15043 point. This matters when lines at window top and lines
15044 below window bottom have different height. */
15045 struct it it1;
15046 void *it1data = NULL;
15047 /* We use a temporary it1 because line_bottom_y can modify
15048 its argument, if it moves one line down; see there. */
15049 int start_y;
15050
15051 SAVE_IT (it1, it, it1data);
15052 start_y = line_bottom_y (&it1);
15053 do {
15054 RESTORE_IT (&it, &it, it1data);
15055 move_it_by_lines (&it, 1);
15056 SAVE_IT (it1, it, it1data);
15057 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15058 }
15059
15060 /* If STARTP is unchanged, move it down another screen line. */
15061 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15062 move_it_by_lines (&it, 1);
15063 startp = it.current.pos;
15064 }
15065 else
15066 {
15067 struct text_pos scroll_margin_pos = startp;
15068 int y_offset = 0;
15069
15070 /* See if point is inside the scroll margin at the top of the
15071 window. */
15072 if (this_scroll_margin)
15073 {
15074 int y_start;
15075
15076 start_display (&it, w, startp);
15077 y_start = it.current_y;
15078 move_it_vertically (&it, this_scroll_margin);
15079 scroll_margin_pos = it.current.pos;
15080 /* If we didn't move enough before hitting ZV, request
15081 additional amount of scroll, to move point out of the
15082 scroll margin. */
15083 if (IT_CHARPOS (it) == ZV
15084 && it.current_y - y_start < this_scroll_margin)
15085 y_offset = this_scroll_margin - (it.current_y - y_start);
15086 }
15087
15088 if (PT < CHARPOS (scroll_margin_pos))
15089 {
15090 /* Point is in the scroll margin at the top of the window or
15091 above what is displayed in the window. */
15092 int y0, y_to_move;
15093
15094 /* Compute the vertical distance from PT to the scroll
15095 margin position. Move as far as scroll_max allows, or
15096 one screenful, or 10 screen lines, whichever is largest.
15097 Give up if distance is greater than scroll_max or if we
15098 didn't reach the scroll margin position. */
15099 SET_TEXT_POS (pos, PT, PT_BYTE);
15100 start_display (&it, w, pos);
15101 y0 = it.current_y;
15102 y_to_move = max (it.last_visible_y,
15103 max (scroll_max, 10 * frame_line_height));
15104 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15105 y_to_move, -1,
15106 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15107 dy = it.current_y - y0;
15108 if (dy > scroll_max
15109 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15110 return SCROLLING_FAILED;
15111
15112 /* Additional scroll for when ZV was too close to point. */
15113 dy += y_offset;
15114
15115 /* Compute new window start. */
15116 start_display (&it, w, startp);
15117
15118 if (arg_scroll_conservatively)
15119 amount_to_scroll = max (dy, frame_line_height
15120 * max (scroll_step, temp_scroll_step));
15121 else if (scroll_step || temp_scroll_step)
15122 amount_to_scroll = scroll_max;
15123 else
15124 {
15125 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15126 height = WINDOW_BOX_TEXT_HEIGHT (w);
15127 if (NUMBERP (aggressive))
15128 {
15129 double float_amount = XFLOATINT (aggressive) * height;
15130 int aggressive_scroll = float_amount;
15131 if (aggressive_scroll == 0 && float_amount > 0)
15132 aggressive_scroll = 1;
15133 /* Don't let point enter the scroll margin near
15134 bottom of the window, if the value of
15135 scroll_down_aggressively happens to be too
15136 large. */
15137 if (aggressive_scroll + 2 * this_scroll_margin > height)
15138 aggressive_scroll = height - 2 * this_scroll_margin;
15139 amount_to_scroll = dy + aggressive_scroll;
15140 }
15141 }
15142
15143 if (amount_to_scroll <= 0)
15144 return SCROLLING_FAILED;
15145
15146 move_it_vertically_backward (&it, amount_to_scroll);
15147 startp = it.current.pos;
15148 }
15149 }
15150
15151 /* Run window scroll functions. */
15152 startp = run_window_scroll_functions (window, startp);
15153
15154 /* Display the window. Give up if new fonts are loaded, or if point
15155 doesn't appear. */
15156 if (!try_window (window, startp, 0))
15157 rc = SCROLLING_NEED_LARGER_MATRICES;
15158 else if (w->cursor.vpos < 0)
15159 {
15160 clear_glyph_matrix (w->desired_matrix);
15161 rc = SCROLLING_FAILED;
15162 }
15163 else
15164 {
15165 /* Maybe forget recorded base line for line number display. */
15166 if (!just_this_one_p
15167 || current_buffer->clip_changed
15168 || BEG_UNCHANGED < CHARPOS (startp))
15169 w->base_line_number = 0;
15170
15171 /* If cursor ends up on a partially visible line,
15172 treat that as being off the bottom of the screen. */
15173 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15174 false)
15175 /* It's possible that the cursor is on the first line of the
15176 buffer, which is partially obscured due to a vscroll
15177 (Bug#7537). In that case, avoid looping forever. */
15178 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15179 {
15180 clear_glyph_matrix (w->desired_matrix);
15181 ++extra_scroll_margin_lines;
15182 goto too_near_end;
15183 }
15184 rc = SCROLLING_SUCCESS;
15185 }
15186
15187 return rc;
15188 }
15189
15190
15191 /* Compute a suitable window start for window W if display of W starts
15192 on a continuation line. Value is true if a new window start
15193 was computed.
15194
15195 The new window start will be computed, based on W's width, starting
15196 from the start of the continued line. It is the start of the
15197 screen line with the minimum distance from the old start W->start. */
15198
15199 static bool
15200 compute_window_start_on_continuation_line (struct window *w)
15201 {
15202 struct text_pos pos, start_pos;
15203 bool window_start_changed_p = false;
15204
15205 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15206
15207 /* If window start is on a continuation line... Window start may be
15208 < BEGV in case there's invisible text at the start of the
15209 buffer (M-x rmail, for example). */
15210 if (CHARPOS (start_pos) > BEGV
15211 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15212 {
15213 struct it it;
15214 struct glyph_row *row;
15215
15216 /* Handle the case that the window start is out of range. */
15217 if (CHARPOS (start_pos) < BEGV)
15218 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15219 else if (CHARPOS (start_pos) > ZV)
15220 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15221
15222 /* Find the start of the continued line. This should be fast
15223 because find_newline is fast (newline cache). */
15224 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15225 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15226 row, DEFAULT_FACE_ID);
15227 reseat_at_previous_visible_line_start (&it);
15228
15229 /* If the line start is "too far" away from the window start,
15230 say it takes too much time to compute a new window start. */
15231 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15232 /* PXW: Do we need upper bounds here? */
15233 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15234 {
15235 int min_distance, distance;
15236
15237 /* Move forward by display lines to find the new window
15238 start. If window width was enlarged, the new start can
15239 be expected to be > the old start. If window width was
15240 decreased, the new window start will be < the old start.
15241 So, we're looking for the display line start with the
15242 minimum distance from the old window start. */
15243 pos = it.current.pos;
15244 min_distance = INFINITY;
15245 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15246 distance < min_distance)
15247 {
15248 min_distance = distance;
15249 pos = it.current.pos;
15250 if (it.line_wrap == WORD_WRAP)
15251 {
15252 /* Under WORD_WRAP, move_it_by_lines is likely to
15253 overshoot and stop not at the first, but the
15254 second character from the left margin. So in
15255 that case, we need a more tight control on the X
15256 coordinate of the iterator than move_it_by_lines
15257 promises in its contract. The method is to first
15258 go to the last (rightmost) visible character of a
15259 line, then move to the leftmost character on the
15260 next line in a separate call. */
15261 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15262 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15263 move_it_to (&it, ZV, 0,
15264 it.current_y + it.max_ascent + it.max_descent, -1,
15265 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15266 }
15267 else
15268 move_it_by_lines (&it, 1);
15269 }
15270
15271 /* Set the window start there. */
15272 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15273 window_start_changed_p = true;
15274 }
15275 }
15276
15277 return window_start_changed_p;
15278 }
15279
15280
15281 /* Try cursor movement in case text has not changed in window WINDOW,
15282 with window start STARTP. Value is
15283
15284 CURSOR_MOVEMENT_SUCCESS if successful
15285
15286 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15287
15288 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15289 display. *SCROLL_STEP is set to true, under certain circumstances, if
15290 we want to scroll as if scroll-step were set to 1. See the code.
15291
15292 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15293 which case we have to abort this redisplay, and adjust matrices
15294 first. */
15295
15296 enum
15297 {
15298 CURSOR_MOVEMENT_SUCCESS,
15299 CURSOR_MOVEMENT_CANNOT_BE_USED,
15300 CURSOR_MOVEMENT_MUST_SCROLL,
15301 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15302 };
15303
15304 static int
15305 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15306 bool *scroll_step)
15307 {
15308 struct window *w = XWINDOW (window);
15309 struct frame *f = XFRAME (w->frame);
15310 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15311
15312 #ifdef GLYPH_DEBUG
15313 if (inhibit_try_cursor_movement)
15314 return rc;
15315 #endif
15316
15317 /* Previously, there was a check for Lisp integer in the
15318 if-statement below. Now, this field is converted to
15319 ptrdiff_t, thus zero means invalid position in a buffer. */
15320 eassert (w->last_point > 0);
15321 /* Likewise there was a check whether window_end_vpos is nil or larger
15322 than the window. Now window_end_vpos is int and so never nil, but
15323 let's leave eassert to check whether it fits in the window. */
15324 eassert (!w->window_end_valid
15325 || w->window_end_vpos < w->current_matrix->nrows);
15326
15327 /* Handle case where text has not changed, only point, and it has
15328 not moved off the frame. */
15329 if (/* Point may be in this window. */
15330 PT >= CHARPOS (startp)
15331 /* Selective display hasn't changed. */
15332 && !current_buffer->clip_changed
15333 /* Function force-mode-line-update is used to force a thorough
15334 redisplay. It sets either windows_or_buffers_changed or
15335 update_mode_lines. So don't take a shortcut here for these
15336 cases. */
15337 && !update_mode_lines
15338 && !windows_or_buffers_changed
15339 && !f->cursor_type_changed
15340 && NILP (Vshow_trailing_whitespace)
15341 /* This code is not used for mini-buffer for the sake of the case
15342 of redisplaying to replace an echo area message; since in
15343 that case the mini-buffer contents per se are usually
15344 unchanged. This code is of no real use in the mini-buffer
15345 since the handling of this_line_start_pos, etc., in redisplay
15346 handles the same cases. */
15347 && !EQ (window, minibuf_window)
15348 && (FRAME_WINDOW_P (f)
15349 || !overlay_arrow_in_current_buffer_p ()))
15350 {
15351 int this_scroll_margin, top_scroll_margin;
15352 struct glyph_row *row = NULL;
15353 int frame_line_height = default_line_pixel_height (w);
15354 int window_total_lines
15355 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15356
15357 #ifdef GLYPH_DEBUG
15358 debug_method_add (w, "cursor movement");
15359 #endif
15360
15361 /* Scroll if point within this distance from the top or bottom
15362 of the window. This is a pixel value. */
15363 if (scroll_margin > 0)
15364 {
15365 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15366 this_scroll_margin *= frame_line_height;
15367 }
15368 else
15369 this_scroll_margin = 0;
15370
15371 top_scroll_margin = this_scroll_margin;
15372 if (WINDOW_WANTS_HEADER_LINE_P (w))
15373 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15374
15375 /* Start with the row the cursor was displayed during the last
15376 not paused redisplay. Give up if that row is not valid. */
15377 if (w->last_cursor_vpos < 0
15378 || w->last_cursor_vpos >= w->current_matrix->nrows)
15379 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15380 else
15381 {
15382 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15383 if (row->mode_line_p)
15384 ++row;
15385 if (!row->enabled_p)
15386 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15387 }
15388
15389 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15390 {
15391 bool scroll_p = false, must_scroll = false;
15392 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15393
15394 if (PT > w->last_point)
15395 {
15396 /* Point has moved forward. */
15397 while (MATRIX_ROW_END_CHARPOS (row) < PT
15398 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15399 {
15400 eassert (row->enabled_p);
15401 ++row;
15402 }
15403
15404 /* If the end position of a row equals the start
15405 position of the next row, and PT is at that position,
15406 we would rather display cursor in the next line. */
15407 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15408 && MATRIX_ROW_END_CHARPOS (row) == PT
15409 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15410 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15411 && !cursor_row_p (row))
15412 ++row;
15413
15414 /* If within the scroll margin, scroll. Note that
15415 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15416 the next line would be drawn, and that
15417 this_scroll_margin can be zero. */
15418 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15419 || PT > MATRIX_ROW_END_CHARPOS (row)
15420 /* Line is completely visible last line in window
15421 and PT is to be set in the next line. */
15422 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15423 && PT == MATRIX_ROW_END_CHARPOS (row)
15424 && !row->ends_at_zv_p
15425 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15426 scroll_p = true;
15427 }
15428 else if (PT < w->last_point)
15429 {
15430 /* Cursor has to be moved backward. Note that PT >=
15431 CHARPOS (startp) because of the outer if-statement. */
15432 while (!row->mode_line_p
15433 && (MATRIX_ROW_START_CHARPOS (row) > PT
15434 || (MATRIX_ROW_START_CHARPOS (row) == PT
15435 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15436 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15437 row > w->current_matrix->rows
15438 && (row-1)->ends_in_newline_from_string_p))))
15439 && (row->y > top_scroll_margin
15440 || CHARPOS (startp) == BEGV))
15441 {
15442 eassert (row->enabled_p);
15443 --row;
15444 }
15445
15446 /* Consider the following case: Window starts at BEGV,
15447 there is invisible, intangible text at BEGV, so that
15448 display starts at some point START > BEGV. It can
15449 happen that we are called with PT somewhere between
15450 BEGV and START. Try to handle that case. */
15451 if (row < w->current_matrix->rows
15452 || row->mode_line_p)
15453 {
15454 row = w->current_matrix->rows;
15455 if (row->mode_line_p)
15456 ++row;
15457 }
15458
15459 /* Due to newlines in overlay strings, we may have to
15460 skip forward over overlay strings. */
15461 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15462 && MATRIX_ROW_END_CHARPOS (row) == PT
15463 && !cursor_row_p (row))
15464 ++row;
15465
15466 /* If within the scroll margin, scroll. */
15467 if (row->y < top_scroll_margin
15468 && CHARPOS (startp) != BEGV)
15469 scroll_p = true;
15470 }
15471 else
15472 {
15473 /* Cursor did not move. So don't scroll even if cursor line
15474 is partially visible, as it was so before. */
15475 rc = CURSOR_MOVEMENT_SUCCESS;
15476 }
15477
15478 if (PT < MATRIX_ROW_START_CHARPOS (row)
15479 || PT > MATRIX_ROW_END_CHARPOS (row))
15480 {
15481 /* if PT is not in the glyph row, give up. */
15482 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15483 must_scroll = true;
15484 }
15485 else if (rc != CURSOR_MOVEMENT_SUCCESS
15486 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15487 {
15488 struct glyph_row *row1;
15489
15490 /* If rows are bidi-reordered and point moved, back up
15491 until we find a row that does not belong to a
15492 continuation line. This is because we must consider
15493 all rows of a continued line as candidates for the
15494 new cursor positioning, since row start and end
15495 positions change non-linearly with vertical position
15496 in such rows. */
15497 /* FIXME: Revisit this when glyph ``spilling'' in
15498 continuation lines' rows is implemented for
15499 bidi-reordered rows. */
15500 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15501 MATRIX_ROW_CONTINUATION_LINE_P (row);
15502 --row)
15503 {
15504 /* If we hit the beginning of the displayed portion
15505 without finding the first row of a continued
15506 line, give up. */
15507 if (row <= row1)
15508 {
15509 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15510 break;
15511 }
15512 eassert (row->enabled_p);
15513 }
15514 }
15515 if (must_scroll)
15516 ;
15517 else if (rc != CURSOR_MOVEMENT_SUCCESS
15518 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15519 /* Make sure this isn't a header line by any chance, since
15520 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15521 && !row->mode_line_p
15522 && make_cursor_line_fully_visible_p)
15523 {
15524 if (PT == MATRIX_ROW_END_CHARPOS (row)
15525 && !row->ends_at_zv_p
15526 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15527 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15528 else if (row->height > window_box_height (w))
15529 {
15530 /* If we end up in a partially visible line, let's
15531 make it fully visible, except when it's taller
15532 than the window, in which case we can't do much
15533 about it. */
15534 *scroll_step = true;
15535 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15536 }
15537 else
15538 {
15539 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15540 if (!cursor_row_fully_visible_p (w, false, true))
15541 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15542 else
15543 rc = CURSOR_MOVEMENT_SUCCESS;
15544 }
15545 }
15546 else if (scroll_p)
15547 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15548 else if (rc != CURSOR_MOVEMENT_SUCCESS
15549 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15550 {
15551 /* With bidi-reordered rows, there could be more than
15552 one candidate row whose start and end positions
15553 occlude point. We need to let set_cursor_from_row
15554 find the best candidate. */
15555 /* FIXME: Revisit this when glyph ``spilling'' in
15556 continuation lines' rows is implemented for
15557 bidi-reordered rows. */
15558 bool rv = false;
15559
15560 do
15561 {
15562 bool at_zv_p = false, exact_match_p = false;
15563
15564 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15565 && PT <= MATRIX_ROW_END_CHARPOS (row)
15566 && cursor_row_p (row))
15567 rv |= set_cursor_from_row (w, row, w->current_matrix,
15568 0, 0, 0, 0);
15569 /* As soon as we've found the exact match for point,
15570 or the first suitable row whose ends_at_zv_p flag
15571 is set, we are done. */
15572 if (rv)
15573 {
15574 at_zv_p = MATRIX_ROW (w->current_matrix,
15575 w->cursor.vpos)->ends_at_zv_p;
15576 if (!at_zv_p
15577 && w->cursor.hpos >= 0
15578 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15579 w->cursor.vpos))
15580 {
15581 struct glyph_row *candidate =
15582 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15583 struct glyph *g =
15584 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15585 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15586
15587 exact_match_p =
15588 (BUFFERP (g->object) && g->charpos == PT)
15589 || (NILP (g->object)
15590 && (g->charpos == PT
15591 || (g->charpos == 0 && endpos - 1 == PT)));
15592 }
15593 if (at_zv_p || exact_match_p)
15594 {
15595 rc = CURSOR_MOVEMENT_SUCCESS;
15596 break;
15597 }
15598 }
15599 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15600 break;
15601 ++row;
15602 }
15603 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15604 || row->continued_p)
15605 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15606 || (MATRIX_ROW_START_CHARPOS (row) == PT
15607 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15608 /* If we didn't find any candidate rows, or exited the
15609 loop before all the candidates were examined, signal
15610 to the caller that this method failed. */
15611 if (rc != CURSOR_MOVEMENT_SUCCESS
15612 && !(rv
15613 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15614 && !row->continued_p))
15615 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15616 else if (rv)
15617 rc = CURSOR_MOVEMENT_SUCCESS;
15618 }
15619 else
15620 {
15621 do
15622 {
15623 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15624 {
15625 rc = CURSOR_MOVEMENT_SUCCESS;
15626 break;
15627 }
15628 ++row;
15629 }
15630 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15631 && MATRIX_ROW_START_CHARPOS (row) == PT
15632 && cursor_row_p (row));
15633 }
15634 }
15635 }
15636
15637 return rc;
15638 }
15639
15640
15641 void
15642 set_vertical_scroll_bar (struct window *w)
15643 {
15644 ptrdiff_t start, end, whole;
15645
15646 /* Calculate the start and end positions for the current window.
15647 At some point, it would be nice to choose between scrollbars
15648 which reflect the whole buffer size, with special markers
15649 indicating narrowing, and scrollbars which reflect only the
15650 visible region.
15651
15652 Note that mini-buffers sometimes aren't displaying any text. */
15653 if (!MINI_WINDOW_P (w)
15654 || (w == XWINDOW (minibuf_window)
15655 && NILP (echo_area_buffer[0])))
15656 {
15657 struct buffer *buf = XBUFFER (w->contents);
15658 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15659 start = marker_position (w->start) - BUF_BEGV (buf);
15660 /* I don't think this is guaranteed to be right. For the
15661 moment, we'll pretend it is. */
15662 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15663
15664 if (end < start)
15665 end = start;
15666 if (whole < (end - start))
15667 whole = end - start;
15668 }
15669 else
15670 start = end = whole = 0;
15671
15672 /* Indicate what this scroll bar ought to be displaying now. */
15673 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15674 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15675 (w, end - start, whole, start);
15676 }
15677
15678
15679 void
15680 set_horizontal_scroll_bar (struct window *w)
15681 {
15682 int start, end, whole, portion;
15683
15684 if (!MINI_WINDOW_P (w)
15685 || (w == XWINDOW (minibuf_window)
15686 && NILP (echo_area_buffer[0])))
15687 {
15688 struct buffer *b = XBUFFER (w->contents);
15689 struct buffer *old_buffer = NULL;
15690 struct it it;
15691 struct text_pos startp;
15692
15693 if (b != current_buffer)
15694 {
15695 old_buffer = current_buffer;
15696 set_buffer_internal (b);
15697 }
15698
15699 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15700 start_display (&it, w, startp);
15701 it.last_visible_x = INT_MAX;
15702 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15703 MOVE_TO_X | MOVE_TO_Y);
15704 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15705 window_box_height (w), -1,
15706 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15707
15708 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15709 end = start + window_box_width (w, TEXT_AREA);
15710 portion = end - start;
15711 /* After enlarging a horizontally scrolled window such that it
15712 gets at least as wide as the text it contains, make sure that
15713 the thumb doesn't fill the entire scroll bar so we can still
15714 drag it back to see the entire text. */
15715 whole = max (whole, end);
15716
15717 if (it.bidi_p)
15718 {
15719 Lisp_Object pdir;
15720
15721 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15722 if (EQ (pdir, Qright_to_left))
15723 {
15724 start = whole - end;
15725 end = start + portion;
15726 }
15727 }
15728
15729 if (old_buffer)
15730 set_buffer_internal (old_buffer);
15731 }
15732 else
15733 start = end = whole = portion = 0;
15734
15735 w->hscroll_whole = whole;
15736
15737 /* Indicate what this scroll bar ought to be displaying now. */
15738 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15739 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15740 (w, portion, whole, start);
15741 }
15742
15743
15744 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
15745 selected_window is redisplayed.
15746
15747 We can return without actually redisplaying the window if fonts has been
15748 changed on window's frame. In that case, redisplay_internal will retry.
15749
15750 As one of the important parts of redisplaying a window, we need to
15751 decide whether the previous window-start position (stored in the
15752 window's w->start marker position) is still valid, and if it isn't,
15753 recompute it. Some details about that:
15754
15755 . The previous window-start could be in a continuation line, in
15756 which case we need to recompute it when the window width
15757 changes. See compute_window_start_on_continuation_line and its
15758 call below.
15759
15760 . The text that changed since last redisplay could include the
15761 previous window-start position. In that case, we try to salvage
15762 what we can from the current glyph matrix by calling
15763 try_scrolling, which see.
15764
15765 . Some Emacs command could force us to use a specific window-start
15766 position by setting the window's force_start flag, or gently
15767 propose doing that by setting the window's optional_new_start
15768 flag. In these cases, we try using the specified start point if
15769 that succeeds (i.e. the window desired matrix is successfully
15770 recomputed, and point location is within the window). In case
15771 of optional_new_start, we first check if the specified start
15772 position is feasible, i.e. if it will allow point to be
15773 displayed in the window. If using the specified start point
15774 fails, e.g., if new fonts are needed to be loaded, we abort the
15775 redisplay cycle and leave it up to the next cycle to figure out
15776 things.
15777
15778 . Note that the window's force_start flag is sometimes set by
15779 redisplay itself, when it decides that the previous window start
15780 point is fine and should be kept. Search for "goto force_start"
15781 below to see the details. Like the values of window-start
15782 specified outside of redisplay, these internally-deduced values
15783 are tested for feasibility, and ignored if found to be
15784 unfeasible.
15785
15786 . Note that the function try_window, used to completely redisplay
15787 a window, accepts the window's start point as its argument.
15788 This is used several times in the redisplay code to control
15789 where the window start will be, according to user options such
15790 as scroll-conservatively, and also to ensure the screen line
15791 showing point will be fully (as opposed to partially) visible on
15792 display. */
15793
15794 static void
15795 redisplay_window (Lisp_Object window, bool just_this_one_p)
15796 {
15797 struct window *w = XWINDOW (window);
15798 struct frame *f = XFRAME (w->frame);
15799 struct buffer *buffer = XBUFFER (w->contents);
15800 struct buffer *old = current_buffer;
15801 struct text_pos lpoint, opoint, startp;
15802 bool update_mode_line;
15803 int tem;
15804 struct it it;
15805 /* Record it now because it's overwritten. */
15806 bool current_matrix_up_to_date_p = false;
15807 bool used_current_matrix_p = false;
15808 /* This is less strict than current_matrix_up_to_date_p.
15809 It indicates that the buffer contents and narrowing are unchanged. */
15810 bool buffer_unchanged_p = false;
15811 bool temp_scroll_step = false;
15812 ptrdiff_t count = SPECPDL_INDEX ();
15813 int rc;
15814 int centering_position = -1;
15815 bool last_line_misfit = false;
15816 ptrdiff_t beg_unchanged, end_unchanged;
15817 int frame_line_height;
15818
15819 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15820 opoint = lpoint;
15821
15822 #ifdef GLYPH_DEBUG
15823 *w->desired_matrix->method = 0;
15824 #endif
15825
15826 if (!just_this_one_p
15827 && REDISPLAY_SOME_P ()
15828 && !w->redisplay
15829 && !w->update_mode_line
15830 && !f->redisplay
15831 && !buffer->text->redisplay
15832 && BUF_PT (buffer) == w->last_point)
15833 return;
15834
15835 /* Make sure that both W's markers are valid. */
15836 eassert (XMARKER (w->start)->buffer == buffer);
15837 eassert (XMARKER (w->pointm)->buffer == buffer);
15838
15839 /* We come here again if we need to run window-text-change-functions
15840 below. */
15841 restart:
15842 reconsider_clip_changes (w);
15843 frame_line_height = default_line_pixel_height (w);
15844
15845 /* Has the mode line to be updated? */
15846 update_mode_line = (w->update_mode_line
15847 || update_mode_lines
15848 || buffer->clip_changed
15849 || buffer->prevent_redisplay_optimizations_p);
15850
15851 if (!just_this_one_p)
15852 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15853 cleverly elsewhere. */
15854 w->must_be_updated_p = true;
15855
15856 if (MINI_WINDOW_P (w))
15857 {
15858 if (w == XWINDOW (echo_area_window)
15859 && !NILP (echo_area_buffer[0]))
15860 {
15861 if (update_mode_line)
15862 /* We may have to update a tty frame's menu bar or a
15863 tool-bar. Example `M-x C-h C-h C-g'. */
15864 goto finish_menu_bars;
15865 else
15866 /* We've already displayed the echo area glyphs in this window. */
15867 goto finish_scroll_bars;
15868 }
15869 else if ((w != XWINDOW (minibuf_window)
15870 || minibuf_level == 0)
15871 /* When buffer is nonempty, redisplay window normally. */
15872 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15873 /* Quail displays non-mini buffers in minibuffer window.
15874 In that case, redisplay the window normally. */
15875 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15876 {
15877 /* W is a mini-buffer window, but it's not active, so clear
15878 it. */
15879 int yb = window_text_bottom_y (w);
15880 struct glyph_row *row;
15881 int y;
15882
15883 for (y = 0, row = w->desired_matrix->rows;
15884 y < yb;
15885 y += row->height, ++row)
15886 blank_row (w, row, y);
15887 goto finish_scroll_bars;
15888 }
15889
15890 clear_glyph_matrix (w->desired_matrix);
15891 }
15892
15893 /* Otherwise set up data on this window; select its buffer and point
15894 value. */
15895 /* Really select the buffer, for the sake of buffer-local
15896 variables. */
15897 set_buffer_internal_1 (XBUFFER (w->contents));
15898
15899 current_matrix_up_to_date_p
15900 = (w->window_end_valid
15901 && !current_buffer->clip_changed
15902 && !current_buffer->prevent_redisplay_optimizations_p
15903 && !window_outdated (w));
15904
15905 /* Run the window-text-change-functions
15906 if it is possible that the text on the screen has changed
15907 (either due to modification of the text, or any other reason). */
15908 if (!current_matrix_up_to_date_p
15909 && !NILP (Vwindow_text_change_functions))
15910 {
15911 safe_run_hooks (Qwindow_text_change_functions);
15912 goto restart;
15913 }
15914
15915 beg_unchanged = BEG_UNCHANGED;
15916 end_unchanged = END_UNCHANGED;
15917
15918 SET_TEXT_POS (opoint, PT, PT_BYTE);
15919
15920 specbind (Qinhibit_point_motion_hooks, Qt);
15921
15922 buffer_unchanged_p
15923 = (w->window_end_valid
15924 && !current_buffer->clip_changed
15925 && !window_outdated (w));
15926
15927 /* When windows_or_buffers_changed is non-zero, we can't rely
15928 on the window end being valid, so set it to zero there. */
15929 if (windows_or_buffers_changed)
15930 {
15931 /* If window starts on a continuation line, maybe adjust the
15932 window start in case the window's width changed. */
15933 if (XMARKER (w->start)->buffer == current_buffer)
15934 compute_window_start_on_continuation_line (w);
15935
15936 w->window_end_valid = false;
15937 /* If so, we also can't rely on current matrix
15938 and should not fool try_cursor_movement below. */
15939 current_matrix_up_to_date_p = false;
15940 }
15941
15942 /* Some sanity checks. */
15943 CHECK_WINDOW_END (w);
15944 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15945 emacs_abort ();
15946 if (BYTEPOS (opoint) < CHARPOS (opoint))
15947 emacs_abort ();
15948
15949 if (mode_line_update_needed (w))
15950 update_mode_line = true;
15951
15952 /* Point refers normally to the selected window. For any other
15953 window, set up appropriate value. */
15954 if (!EQ (window, selected_window))
15955 {
15956 ptrdiff_t new_pt = marker_position (w->pointm);
15957 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15958
15959 if (new_pt < BEGV)
15960 {
15961 new_pt = BEGV;
15962 new_pt_byte = BEGV_BYTE;
15963 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15964 }
15965 else if (new_pt > (ZV - 1))
15966 {
15967 new_pt = ZV;
15968 new_pt_byte = ZV_BYTE;
15969 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15970 }
15971
15972 /* We don't use SET_PT so that the point-motion hooks don't run. */
15973 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15974 }
15975
15976 /* If any of the character widths specified in the display table
15977 have changed, invalidate the width run cache. It's true that
15978 this may be a bit late to catch such changes, but the rest of
15979 redisplay goes (non-fatally) haywire when the display table is
15980 changed, so why should we worry about doing any better? */
15981 if (current_buffer->width_run_cache
15982 || (current_buffer->base_buffer
15983 && current_buffer->base_buffer->width_run_cache))
15984 {
15985 struct Lisp_Char_Table *disptab = buffer_display_table ();
15986
15987 if (! disptab_matches_widthtab
15988 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15989 {
15990 struct buffer *buf = current_buffer;
15991
15992 if (buf->base_buffer)
15993 buf = buf->base_buffer;
15994 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
15995 recompute_width_table (current_buffer, disptab);
15996 }
15997 }
15998
15999 /* If window-start is screwed up, choose a new one. */
16000 if (XMARKER (w->start)->buffer != current_buffer)
16001 goto recenter;
16002
16003 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16004
16005 /* If someone specified a new starting point but did not insist,
16006 check whether it can be used. */
16007 if ((w->optional_new_start || window_frozen_p (w))
16008 && CHARPOS (startp) >= BEGV
16009 && CHARPOS (startp) <= ZV)
16010 {
16011 ptrdiff_t it_charpos;
16012
16013 w->optional_new_start = false;
16014 start_display (&it, w, startp);
16015 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16016 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16017 /* Record IT's position now, since line_bottom_y might change
16018 that. */
16019 it_charpos = IT_CHARPOS (it);
16020 /* Make sure we set the force_start flag only if the cursor row
16021 will be fully visible. Otherwise, the code under force_start
16022 label below will try to move point back into view, which is
16023 not what the code which sets optional_new_start wants. */
16024 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16025 && !w->force_start)
16026 {
16027 if (it_charpos == PT)
16028 w->force_start = true;
16029 /* IT may overshoot PT if text at PT is invisible. */
16030 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16031 w->force_start = true;
16032 #ifdef GLYPH_DEBUG
16033 if (w->force_start)
16034 {
16035 if (window_frozen_p (w))
16036 debug_method_add (w, "set force_start from frozen window start");
16037 else
16038 debug_method_add (w, "set force_start from optional_new_start");
16039 }
16040 #endif
16041 }
16042 }
16043
16044 force_start:
16045
16046 /* Handle case where place to start displaying has been specified,
16047 unless the specified location is outside the accessible range. */
16048 if (w->force_start)
16049 {
16050 /* We set this later on if we have to adjust point. */
16051 int new_vpos = -1;
16052
16053 w->force_start = false;
16054 w->vscroll = 0;
16055 w->window_end_valid = false;
16056
16057 /* Forget any recorded base line for line number display. */
16058 if (!buffer_unchanged_p)
16059 w->base_line_number = 0;
16060
16061 /* Redisplay the mode line. Select the buffer properly for that.
16062 Also, run the hook window-scroll-functions
16063 because we have scrolled. */
16064 /* Note, we do this after clearing force_start because
16065 if there's an error, it is better to forget about force_start
16066 than to get into an infinite loop calling the hook functions
16067 and having them get more errors. */
16068 if (!update_mode_line
16069 || ! NILP (Vwindow_scroll_functions))
16070 {
16071 update_mode_line = true;
16072 w->update_mode_line = true;
16073 startp = run_window_scroll_functions (window, startp);
16074 }
16075
16076 if (CHARPOS (startp) < BEGV)
16077 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16078 else if (CHARPOS (startp) > ZV)
16079 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16080
16081 /* Redisplay, then check if cursor has been set during the
16082 redisplay. Give up if new fonts were loaded. */
16083 /* We used to issue a CHECK_MARGINS argument to try_window here,
16084 but this causes scrolling to fail when point begins inside
16085 the scroll margin (bug#148) -- cyd */
16086 if (!try_window (window, startp, 0))
16087 {
16088 w->force_start = true;
16089 clear_glyph_matrix (w->desired_matrix);
16090 goto need_larger_matrices;
16091 }
16092
16093 if (w->cursor.vpos < 0)
16094 {
16095 /* If point does not appear, try to move point so it does
16096 appear. The desired matrix has been built above, so we
16097 can use it here. */
16098 new_vpos = window_box_height (w) / 2;
16099 }
16100
16101 if (!cursor_row_fully_visible_p (w, false, false))
16102 {
16103 /* Point does appear, but on a line partly visible at end of window.
16104 Move it back to a fully-visible line. */
16105 new_vpos = window_box_height (w);
16106 /* But if window_box_height suggests a Y coordinate that is
16107 not less than we already have, that line will clearly not
16108 be fully visible, so give up and scroll the display.
16109 This can happen when the default face uses a font whose
16110 dimensions are different from the frame's default
16111 font. */
16112 if (new_vpos >= w->cursor.y)
16113 {
16114 w->cursor.vpos = -1;
16115 clear_glyph_matrix (w->desired_matrix);
16116 goto try_to_scroll;
16117 }
16118 }
16119 else if (w->cursor.vpos >= 0)
16120 {
16121 /* Some people insist on not letting point enter the scroll
16122 margin, even though this part handles windows that didn't
16123 scroll at all. */
16124 int window_total_lines
16125 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16126 int margin = min (scroll_margin, window_total_lines / 4);
16127 int pixel_margin = margin * frame_line_height;
16128 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16129
16130 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16131 below, which finds the row to move point to, advances by
16132 the Y coordinate of the _next_ row, see the definition of
16133 MATRIX_ROW_BOTTOM_Y. */
16134 if (w->cursor.vpos < margin + header_line)
16135 {
16136 w->cursor.vpos = -1;
16137 clear_glyph_matrix (w->desired_matrix);
16138 goto try_to_scroll;
16139 }
16140 else
16141 {
16142 int window_height = window_box_height (w);
16143
16144 if (header_line)
16145 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16146 if (w->cursor.y >= window_height - pixel_margin)
16147 {
16148 w->cursor.vpos = -1;
16149 clear_glyph_matrix (w->desired_matrix);
16150 goto try_to_scroll;
16151 }
16152 }
16153 }
16154
16155 /* If we need to move point for either of the above reasons,
16156 now actually do it. */
16157 if (new_vpos >= 0)
16158 {
16159 struct glyph_row *row;
16160
16161 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16162 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16163 ++row;
16164
16165 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16166 MATRIX_ROW_START_BYTEPOS (row));
16167
16168 if (w != XWINDOW (selected_window))
16169 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16170 else if (current_buffer == old)
16171 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16172
16173 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16174
16175 /* Re-run pre-redisplay-function so it can update the region
16176 according to the new position of point. */
16177 /* Other than the cursor, w's redisplay is done so we can set its
16178 redisplay to false. Also the buffer's redisplay can be set to
16179 false, since propagate_buffer_redisplay should have already
16180 propagated its info to `w' anyway. */
16181 w->redisplay = false;
16182 XBUFFER (w->contents)->text->redisplay = false;
16183 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16184
16185 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16186 {
16187 /* pre-redisplay-function made changes (e.g. move the region)
16188 that require another round of redisplay. */
16189 clear_glyph_matrix (w->desired_matrix);
16190 if (!try_window (window, startp, 0))
16191 goto need_larger_matrices;
16192 }
16193 }
16194 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16195 {
16196 clear_glyph_matrix (w->desired_matrix);
16197 goto try_to_scroll;
16198 }
16199
16200 #ifdef GLYPH_DEBUG
16201 debug_method_add (w, "forced window start");
16202 #endif
16203 goto done;
16204 }
16205
16206 /* Handle case where text has not changed, only point, and it has
16207 not moved off the frame, and we are not retrying after hscroll.
16208 (current_matrix_up_to_date_p is true when retrying.) */
16209 if (current_matrix_up_to_date_p
16210 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16211 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16212 {
16213 switch (rc)
16214 {
16215 case CURSOR_MOVEMENT_SUCCESS:
16216 used_current_matrix_p = true;
16217 goto done;
16218
16219 case CURSOR_MOVEMENT_MUST_SCROLL:
16220 goto try_to_scroll;
16221
16222 default:
16223 emacs_abort ();
16224 }
16225 }
16226 /* If current starting point was originally the beginning of a line
16227 but no longer is, find a new starting point. */
16228 else if (w->start_at_line_beg
16229 && !(CHARPOS (startp) <= BEGV
16230 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16231 {
16232 #ifdef GLYPH_DEBUG
16233 debug_method_add (w, "recenter 1");
16234 #endif
16235 goto recenter;
16236 }
16237
16238 /* Try scrolling with try_window_id. Value is > 0 if update has
16239 been done, it is -1 if we know that the same window start will
16240 not work. It is 0 if unsuccessful for some other reason. */
16241 else if ((tem = try_window_id (w)) != 0)
16242 {
16243 #ifdef GLYPH_DEBUG
16244 debug_method_add (w, "try_window_id %d", tem);
16245 #endif
16246
16247 if (f->fonts_changed)
16248 goto need_larger_matrices;
16249 if (tem > 0)
16250 goto done;
16251
16252 /* Otherwise try_window_id has returned -1 which means that we
16253 don't want the alternative below this comment to execute. */
16254 }
16255 else if (CHARPOS (startp) >= BEGV
16256 && CHARPOS (startp) <= ZV
16257 && PT >= CHARPOS (startp)
16258 && (CHARPOS (startp) < ZV
16259 /* Avoid starting at end of buffer. */
16260 || CHARPOS (startp) == BEGV
16261 || !window_outdated (w)))
16262 {
16263 int d1, d2, d5, d6;
16264 int rtop, rbot;
16265
16266 /* If first window line is a continuation line, and window start
16267 is inside the modified region, but the first change is before
16268 current window start, we must select a new window start.
16269
16270 However, if this is the result of a down-mouse event (e.g. by
16271 extending the mouse-drag-overlay), we don't want to select a
16272 new window start, since that would change the position under
16273 the mouse, resulting in an unwanted mouse-movement rather
16274 than a simple mouse-click. */
16275 if (!w->start_at_line_beg
16276 && NILP (do_mouse_tracking)
16277 && CHARPOS (startp) > BEGV
16278 && CHARPOS (startp) > BEG + beg_unchanged
16279 && CHARPOS (startp) <= Z - end_unchanged
16280 /* Even if w->start_at_line_beg is nil, a new window may
16281 start at a line_beg, since that's how set_buffer_window
16282 sets it. So, we need to check the return value of
16283 compute_window_start_on_continuation_line. (See also
16284 bug#197). */
16285 && XMARKER (w->start)->buffer == current_buffer
16286 && compute_window_start_on_continuation_line (w)
16287 /* It doesn't make sense to force the window start like we
16288 do at label force_start if it is already known that point
16289 will not be fully visible in the resulting window, because
16290 doing so will move point from its correct position
16291 instead of scrolling the window to bring point into view.
16292 See bug#9324. */
16293 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16294 /* A very tall row could need more than the window height,
16295 in which case we accept that it is partially visible. */
16296 && (rtop != 0) == (rbot != 0))
16297 {
16298 w->force_start = true;
16299 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16300 #ifdef GLYPH_DEBUG
16301 debug_method_add (w, "recomputed window start in continuation line");
16302 #endif
16303 goto force_start;
16304 }
16305
16306 #ifdef GLYPH_DEBUG
16307 debug_method_add (w, "same window start");
16308 #endif
16309
16310 /* Try to redisplay starting at same place as before.
16311 If point has not moved off frame, accept the results. */
16312 if (!current_matrix_up_to_date_p
16313 /* Don't use try_window_reusing_current_matrix in this case
16314 because a window scroll function can have changed the
16315 buffer. */
16316 || !NILP (Vwindow_scroll_functions)
16317 || MINI_WINDOW_P (w)
16318 || !(used_current_matrix_p
16319 = try_window_reusing_current_matrix (w)))
16320 {
16321 IF_DEBUG (debug_method_add (w, "1"));
16322 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16323 /* -1 means we need to scroll.
16324 0 means we need new matrices, but fonts_changed
16325 is set in that case, so we will detect it below. */
16326 goto try_to_scroll;
16327 }
16328
16329 if (f->fonts_changed)
16330 goto need_larger_matrices;
16331
16332 if (w->cursor.vpos >= 0)
16333 {
16334 if (!just_this_one_p
16335 || current_buffer->clip_changed
16336 || BEG_UNCHANGED < CHARPOS (startp))
16337 /* Forget any recorded base line for line number display. */
16338 w->base_line_number = 0;
16339
16340 if (!cursor_row_fully_visible_p (w, true, false))
16341 {
16342 clear_glyph_matrix (w->desired_matrix);
16343 last_line_misfit = true;
16344 }
16345 /* Drop through and scroll. */
16346 else
16347 goto done;
16348 }
16349 else
16350 clear_glyph_matrix (w->desired_matrix);
16351 }
16352
16353 try_to_scroll:
16354
16355 /* Redisplay the mode line. Select the buffer properly for that. */
16356 if (!update_mode_line)
16357 {
16358 update_mode_line = true;
16359 w->update_mode_line = true;
16360 }
16361
16362 /* Try to scroll by specified few lines. */
16363 if ((scroll_conservatively
16364 || emacs_scroll_step
16365 || temp_scroll_step
16366 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16367 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16368 && CHARPOS (startp) >= BEGV
16369 && CHARPOS (startp) <= ZV)
16370 {
16371 /* The function returns -1 if new fonts were loaded, 1 if
16372 successful, 0 if not successful. */
16373 int ss = try_scrolling (window, just_this_one_p,
16374 scroll_conservatively,
16375 emacs_scroll_step,
16376 temp_scroll_step, last_line_misfit);
16377 switch (ss)
16378 {
16379 case SCROLLING_SUCCESS:
16380 goto done;
16381
16382 case SCROLLING_NEED_LARGER_MATRICES:
16383 goto need_larger_matrices;
16384
16385 case SCROLLING_FAILED:
16386 break;
16387
16388 default:
16389 emacs_abort ();
16390 }
16391 }
16392
16393 /* Finally, just choose a place to start which positions point
16394 according to user preferences. */
16395
16396 recenter:
16397
16398 #ifdef GLYPH_DEBUG
16399 debug_method_add (w, "recenter");
16400 #endif
16401
16402 /* Forget any previously recorded base line for line number display. */
16403 if (!buffer_unchanged_p)
16404 w->base_line_number = 0;
16405
16406 /* Determine the window start relative to point. */
16407 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16408 it.current_y = it.last_visible_y;
16409 if (centering_position < 0)
16410 {
16411 int window_total_lines
16412 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16413 int margin
16414 = scroll_margin > 0
16415 ? min (scroll_margin, window_total_lines / 4)
16416 : 0;
16417 ptrdiff_t margin_pos = CHARPOS (startp);
16418 Lisp_Object aggressive;
16419 bool scrolling_up;
16420
16421 /* If there is a scroll margin at the top of the window, find
16422 its character position. */
16423 if (margin
16424 /* Cannot call start_display if startp is not in the
16425 accessible region of the buffer. This can happen when we
16426 have just switched to a different buffer and/or changed
16427 its restriction. In that case, startp is initialized to
16428 the character position 1 (BEGV) because we did not yet
16429 have chance to display the buffer even once. */
16430 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16431 {
16432 struct it it1;
16433 void *it1data = NULL;
16434
16435 SAVE_IT (it1, it, it1data);
16436 start_display (&it1, w, startp);
16437 move_it_vertically (&it1, margin * frame_line_height);
16438 margin_pos = IT_CHARPOS (it1);
16439 RESTORE_IT (&it, &it, it1data);
16440 }
16441 scrolling_up = PT > margin_pos;
16442 aggressive =
16443 scrolling_up
16444 ? BVAR (current_buffer, scroll_up_aggressively)
16445 : BVAR (current_buffer, scroll_down_aggressively);
16446
16447 if (!MINI_WINDOW_P (w)
16448 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16449 {
16450 int pt_offset = 0;
16451
16452 /* Setting scroll-conservatively overrides
16453 scroll-*-aggressively. */
16454 if (!scroll_conservatively && NUMBERP (aggressive))
16455 {
16456 double float_amount = XFLOATINT (aggressive);
16457
16458 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16459 if (pt_offset == 0 && float_amount > 0)
16460 pt_offset = 1;
16461 if (pt_offset && margin > 0)
16462 margin -= 1;
16463 }
16464 /* Compute how much to move the window start backward from
16465 point so that point will be displayed where the user
16466 wants it. */
16467 if (scrolling_up)
16468 {
16469 centering_position = it.last_visible_y;
16470 if (pt_offset)
16471 centering_position -= pt_offset;
16472 centering_position -=
16473 (frame_line_height * (1 + margin + last_line_misfit)
16474 + WINDOW_HEADER_LINE_HEIGHT (w));
16475 /* Don't let point enter the scroll margin near top of
16476 the window. */
16477 if (centering_position < margin * frame_line_height)
16478 centering_position = margin * frame_line_height;
16479 }
16480 else
16481 centering_position = margin * frame_line_height + pt_offset;
16482 }
16483 else
16484 /* Set the window start half the height of the window backward
16485 from point. */
16486 centering_position = window_box_height (w) / 2;
16487 }
16488 move_it_vertically_backward (&it, centering_position);
16489
16490 eassert (IT_CHARPOS (it) >= BEGV);
16491
16492 /* The function move_it_vertically_backward may move over more
16493 than the specified y-distance. If it->w is small, e.g. a
16494 mini-buffer window, we may end up in front of the window's
16495 display area. Start displaying at the start of the line
16496 containing PT in this case. */
16497 if (it.current_y <= 0)
16498 {
16499 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16500 move_it_vertically_backward (&it, 0);
16501 it.current_y = 0;
16502 }
16503
16504 it.current_x = it.hpos = 0;
16505
16506 /* Set the window start position here explicitly, to avoid an
16507 infinite loop in case the functions in window-scroll-functions
16508 get errors. */
16509 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16510
16511 /* Run scroll hooks. */
16512 startp = run_window_scroll_functions (window, it.current.pos);
16513
16514 /* Redisplay the window. */
16515 if (!current_matrix_up_to_date_p
16516 || windows_or_buffers_changed
16517 || f->cursor_type_changed
16518 /* Don't use try_window_reusing_current_matrix in this case
16519 because it can have changed the buffer. */
16520 || !NILP (Vwindow_scroll_functions)
16521 || !just_this_one_p
16522 || MINI_WINDOW_P (w)
16523 || !(used_current_matrix_p
16524 = try_window_reusing_current_matrix (w)))
16525 try_window (window, startp, 0);
16526
16527 /* If new fonts have been loaded (due to fontsets), give up. We
16528 have to start a new redisplay since we need to re-adjust glyph
16529 matrices. */
16530 if (f->fonts_changed)
16531 goto need_larger_matrices;
16532
16533 /* If cursor did not appear assume that the middle of the window is
16534 in the first line of the window. Do it again with the next line.
16535 (Imagine a window of height 100, displaying two lines of height
16536 60. Moving back 50 from it->last_visible_y will end in the first
16537 line.) */
16538 if (w->cursor.vpos < 0)
16539 {
16540 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16541 {
16542 clear_glyph_matrix (w->desired_matrix);
16543 move_it_by_lines (&it, 1);
16544 try_window (window, it.current.pos, 0);
16545 }
16546 else if (PT < IT_CHARPOS (it))
16547 {
16548 clear_glyph_matrix (w->desired_matrix);
16549 move_it_by_lines (&it, -1);
16550 try_window (window, it.current.pos, 0);
16551 }
16552 else
16553 {
16554 /* Not much we can do about it. */
16555 }
16556 }
16557
16558 /* Consider the following case: Window starts at BEGV, there is
16559 invisible, intangible text at BEGV, so that display starts at
16560 some point START > BEGV. It can happen that we are called with
16561 PT somewhere between BEGV and START. Try to handle that case,
16562 and similar ones. */
16563 if (w->cursor.vpos < 0)
16564 {
16565 /* First, try locating the proper glyph row for PT. */
16566 struct glyph_row *row =
16567 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16568
16569 /* Sometimes point is at the beginning of invisible text that is
16570 before the 1st character displayed in the row. In that case,
16571 row_containing_pos fails to find the row, because no glyphs
16572 with appropriate buffer positions are present in the row.
16573 Therefore, we next try to find the row which shows the 1st
16574 position after the invisible text. */
16575 if (!row)
16576 {
16577 Lisp_Object val =
16578 get_char_property_and_overlay (make_number (PT), Qinvisible,
16579 Qnil, NULL);
16580
16581 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
16582 {
16583 ptrdiff_t alt_pos;
16584 Lisp_Object invis_end =
16585 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16586 Qnil, Qnil);
16587
16588 if (NATNUMP (invis_end))
16589 alt_pos = XFASTINT (invis_end);
16590 else
16591 alt_pos = ZV;
16592 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16593 NULL, 0);
16594 }
16595 }
16596 /* Finally, fall back on the first row of the window after the
16597 header line (if any). This is slightly better than not
16598 displaying the cursor at all. */
16599 if (!row)
16600 {
16601 row = w->current_matrix->rows;
16602 if (row->mode_line_p)
16603 ++row;
16604 }
16605 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16606 }
16607
16608 if (!cursor_row_fully_visible_p (w, false, false))
16609 {
16610 /* If vscroll is enabled, disable it and try again. */
16611 if (w->vscroll)
16612 {
16613 w->vscroll = 0;
16614 clear_glyph_matrix (w->desired_matrix);
16615 goto recenter;
16616 }
16617
16618 /* Users who set scroll-conservatively to a large number want
16619 point just above/below the scroll margin. If we ended up
16620 with point's row partially visible, move the window start to
16621 make that row fully visible and out of the margin. */
16622 if (scroll_conservatively > SCROLL_LIMIT)
16623 {
16624 int window_total_lines
16625 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16626 int margin =
16627 scroll_margin > 0
16628 ? min (scroll_margin, window_total_lines / 4)
16629 : 0;
16630 bool move_down = w->cursor.vpos >= window_total_lines / 2;
16631
16632 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16633 clear_glyph_matrix (w->desired_matrix);
16634 if (1 == try_window (window, it.current.pos,
16635 TRY_WINDOW_CHECK_MARGINS))
16636 goto done;
16637 }
16638
16639 /* If centering point failed to make the whole line visible,
16640 put point at the top instead. That has to make the whole line
16641 visible, if it can be done. */
16642 if (centering_position == 0)
16643 goto done;
16644
16645 clear_glyph_matrix (w->desired_matrix);
16646 centering_position = 0;
16647 goto recenter;
16648 }
16649
16650 done:
16651
16652 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16653 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16654 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16655
16656 /* Display the mode line, if we must. */
16657 if ((update_mode_line
16658 /* If window not full width, must redo its mode line
16659 if (a) the window to its side is being redone and
16660 (b) we do a frame-based redisplay. This is a consequence
16661 of how inverted lines are drawn in frame-based redisplay. */
16662 || (!just_this_one_p
16663 && !FRAME_WINDOW_P (f)
16664 && !WINDOW_FULL_WIDTH_P (w))
16665 /* Line number to display. */
16666 || w->base_line_pos > 0
16667 /* Column number is displayed and different from the one displayed. */
16668 || (w->column_number_displayed != -1
16669 && (w->column_number_displayed != current_column ())))
16670 /* This means that the window has a mode line. */
16671 && (WINDOW_WANTS_MODELINE_P (w)
16672 || WINDOW_WANTS_HEADER_LINE_P (w)))
16673 {
16674
16675 display_mode_lines (w);
16676
16677 /* If mode line height has changed, arrange for a thorough
16678 immediate redisplay using the correct mode line height. */
16679 if (WINDOW_WANTS_MODELINE_P (w)
16680 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16681 {
16682 f->fonts_changed = true;
16683 w->mode_line_height = -1;
16684 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16685 = DESIRED_MODE_LINE_HEIGHT (w);
16686 }
16687
16688 /* If header line height has changed, arrange for a thorough
16689 immediate redisplay using the correct header line height. */
16690 if (WINDOW_WANTS_HEADER_LINE_P (w)
16691 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16692 {
16693 f->fonts_changed = true;
16694 w->header_line_height = -1;
16695 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16696 = DESIRED_HEADER_LINE_HEIGHT (w);
16697 }
16698
16699 if (f->fonts_changed)
16700 goto need_larger_matrices;
16701 }
16702
16703 if (!line_number_displayed && w->base_line_pos != -1)
16704 {
16705 w->base_line_pos = 0;
16706 w->base_line_number = 0;
16707 }
16708
16709 finish_menu_bars:
16710
16711 /* When we reach a frame's selected window, redo the frame's menu bar. */
16712 if (update_mode_line
16713 && EQ (FRAME_SELECTED_WINDOW (f), window))
16714 {
16715 bool redisplay_menu_p;
16716
16717 if (FRAME_WINDOW_P (f))
16718 {
16719 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16720 || defined (HAVE_NS) || defined (USE_GTK)
16721 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16722 #else
16723 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16724 #endif
16725 }
16726 else
16727 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16728
16729 if (redisplay_menu_p)
16730 display_menu_bar (w);
16731
16732 #ifdef HAVE_WINDOW_SYSTEM
16733 if (FRAME_WINDOW_P (f))
16734 {
16735 #if defined (USE_GTK) || defined (HAVE_NS)
16736 if (FRAME_EXTERNAL_TOOL_BAR (f))
16737 redisplay_tool_bar (f);
16738 #else
16739 if (WINDOWP (f->tool_bar_window)
16740 && (FRAME_TOOL_BAR_LINES (f) > 0
16741 || !NILP (Vauto_resize_tool_bars))
16742 && redisplay_tool_bar (f))
16743 ignore_mouse_drag_p = true;
16744 #endif
16745 }
16746 #endif
16747 }
16748
16749 #ifdef HAVE_WINDOW_SYSTEM
16750 if (FRAME_WINDOW_P (f)
16751 && update_window_fringes (w, (just_this_one_p
16752 || (!used_current_matrix_p && !overlay_arrow_seen)
16753 || w->pseudo_window_p)))
16754 {
16755 update_begin (f);
16756 block_input ();
16757 if (draw_window_fringes (w, true))
16758 {
16759 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16760 x_draw_right_divider (w);
16761 else
16762 x_draw_vertical_border (w);
16763 }
16764 unblock_input ();
16765 update_end (f);
16766 }
16767
16768 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16769 x_draw_bottom_divider (w);
16770 #endif /* HAVE_WINDOW_SYSTEM */
16771
16772 /* We go to this label, with fonts_changed set, if it is
16773 necessary to try again using larger glyph matrices.
16774 We have to redeem the scroll bar even in this case,
16775 because the loop in redisplay_internal expects that. */
16776 need_larger_matrices:
16777 ;
16778 finish_scroll_bars:
16779
16780 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16781 {
16782 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16783 /* Set the thumb's position and size. */
16784 set_vertical_scroll_bar (w);
16785
16786 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16787 /* Set the thumb's position and size. */
16788 set_horizontal_scroll_bar (w);
16789
16790 /* Note that we actually used the scroll bar attached to this
16791 window, so it shouldn't be deleted at the end of redisplay. */
16792 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16793 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16794 }
16795
16796 /* Restore current_buffer and value of point in it. The window
16797 update may have changed the buffer, so first make sure `opoint'
16798 is still valid (Bug#6177). */
16799 if (CHARPOS (opoint) < BEGV)
16800 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16801 else if (CHARPOS (opoint) > ZV)
16802 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16803 else
16804 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16805
16806 set_buffer_internal_1 (old);
16807 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16808 shorter. This can be caused by log truncation in *Messages*. */
16809 if (CHARPOS (lpoint) <= ZV)
16810 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16811
16812 unbind_to (count, Qnil);
16813 }
16814
16815
16816 /* Build the complete desired matrix of WINDOW with a window start
16817 buffer position POS.
16818
16819 Value is 1 if successful. It is zero if fonts were loaded during
16820 redisplay which makes re-adjusting glyph matrices necessary, and -1
16821 if point would appear in the scroll margins.
16822 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16823 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16824 set in FLAGS.) */
16825
16826 int
16827 try_window (Lisp_Object window, struct text_pos pos, int flags)
16828 {
16829 struct window *w = XWINDOW (window);
16830 struct it it;
16831 struct glyph_row *last_text_row = NULL;
16832 struct frame *f = XFRAME (w->frame);
16833 int frame_line_height = default_line_pixel_height (w);
16834
16835 /* Make POS the new window start. */
16836 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16837
16838 /* Mark cursor position as unknown. No overlay arrow seen. */
16839 w->cursor.vpos = -1;
16840 overlay_arrow_seen = false;
16841
16842 /* Initialize iterator and info to start at POS. */
16843 start_display (&it, w, pos);
16844 it.glyph_row->reversed_p = false;
16845
16846 /* Display all lines of W. */
16847 while (it.current_y < it.last_visible_y)
16848 {
16849 if (display_line (&it))
16850 last_text_row = it.glyph_row - 1;
16851 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16852 return 0;
16853 }
16854
16855 /* Don't let the cursor end in the scroll margins. */
16856 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16857 && !MINI_WINDOW_P (w))
16858 {
16859 int this_scroll_margin;
16860 int window_total_lines
16861 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16862
16863 if (scroll_margin > 0)
16864 {
16865 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16866 this_scroll_margin *= frame_line_height;
16867 }
16868 else
16869 this_scroll_margin = 0;
16870
16871 if ((w->cursor.y >= 0 /* not vscrolled */
16872 && w->cursor.y < this_scroll_margin
16873 && CHARPOS (pos) > BEGV
16874 && IT_CHARPOS (it) < ZV)
16875 /* rms: considering make_cursor_line_fully_visible_p here
16876 seems to give wrong results. We don't want to recenter
16877 when the last line is partly visible, we want to allow
16878 that case to be handled in the usual way. */
16879 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16880 {
16881 w->cursor.vpos = -1;
16882 clear_glyph_matrix (w->desired_matrix);
16883 return -1;
16884 }
16885 }
16886
16887 /* If bottom moved off end of frame, change mode line percentage. */
16888 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16889 w->update_mode_line = true;
16890
16891 /* Set window_end_pos to the offset of the last character displayed
16892 on the window from the end of current_buffer. Set
16893 window_end_vpos to its row number. */
16894 if (last_text_row)
16895 {
16896 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16897 adjust_window_ends (w, last_text_row, false);
16898 eassert
16899 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16900 w->window_end_vpos)));
16901 }
16902 else
16903 {
16904 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16905 w->window_end_pos = Z - ZV;
16906 w->window_end_vpos = 0;
16907 }
16908
16909 /* But that is not valid info until redisplay finishes. */
16910 w->window_end_valid = false;
16911 return 1;
16912 }
16913
16914
16915 \f
16916 /************************************************************************
16917 Window redisplay reusing current matrix when buffer has not changed
16918 ************************************************************************/
16919
16920 /* Try redisplay of window W showing an unchanged buffer with a
16921 different window start than the last time it was displayed by
16922 reusing its current matrix. Value is true if successful.
16923 W->start is the new window start. */
16924
16925 static bool
16926 try_window_reusing_current_matrix (struct window *w)
16927 {
16928 struct frame *f = XFRAME (w->frame);
16929 struct glyph_row *bottom_row;
16930 struct it it;
16931 struct run run;
16932 struct text_pos start, new_start;
16933 int nrows_scrolled, i;
16934 struct glyph_row *last_text_row;
16935 struct glyph_row *last_reused_text_row;
16936 struct glyph_row *start_row;
16937 int start_vpos, min_y, max_y;
16938
16939 #ifdef GLYPH_DEBUG
16940 if (inhibit_try_window_reusing)
16941 return false;
16942 #endif
16943
16944 if (/* This function doesn't handle terminal frames. */
16945 !FRAME_WINDOW_P (f)
16946 /* Don't try to reuse the display if windows have been split
16947 or such. */
16948 || windows_or_buffers_changed
16949 || f->cursor_type_changed)
16950 return false;
16951
16952 /* Can't do this if showing trailing whitespace. */
16953 if (!NILP (Vshow_trailing_whitespace))
16954 return false;
16955
16956 /* If top-line visibility has changed, give up. */
16957 if (WINDOW_WANTS_HEADER_LINE_P (w)
16958 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16959 return false;
16960
16961 /* Give up if old or new display is scrolled vertically. We could
16962 make this function handle this, but right now it doesn't. */
16963 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16964 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16965 return false;
16966
16967 /* The variable new_start now holds the new window start. The old
16968 start `start' can be determined from the current matrix. */
16969 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16970 start = start_row->minpos;
16971 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16972
16973 /* Clear the desired matrix for the display below. */
16974 clear_glyph_matrix (w->desired_matrix);
16975
16976 if (CHARPOS (new_start) <= CHARPOS (start))
16977 {
16978 /* Don't use this method if the display starts with an ellipsis
16979 displayed for invisible text. It's not easy to handle that case
16980 below, and it's certainly not worth the effort since this is
16981 not a frequent case. */
16982 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16983 return false;
16984
16985 IF_DEBUG (debug_method_add (w, "twu1"));
16986
16987 /* Display up to a row that can be reused. The variable
16988 last_text_row is set to the last row displayed that displays
16989 text. Note that it.vpos == 0 if or if not there is a
16990 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16991 start_display (&it, w, new_start);
16992 w->cursor.vpos = -1;
16993 last_text_row = last_reused_text_row = NULL;
16994
16995 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16996 {
16997 /* If we have reached into the characters in the START row,
16998 that means the line boundaries have changed. So we
16999 can't start copying with the row START. Maybe it will
17000 work to start copying with the following row. */
17001 while (IT_CHARPOS (it) > CHARPOS (start))
17002 {
17003 /* Advance to the next row as the "start". */
17004 start_row++;
17005 start = start_row->minpos;
17006 /* If there are no more rows to try, or just one, give up. */
17007 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17008 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17009 || CHARPOS (start) == ZV)
17010 {
17011 clear_glyph_matrix (w->desired_matrix);
17012 return false;
17013 }
17014
17015 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17016 }
17017 /* If we have reached alignment, we can copy the rest of the
17018 rows. */
17019 if (IT_CHARPOS (it) == CHARPOS (start)
17020 /* Don't accept "alignment" inside a display vector,
17021 since start_row could have started in the middle of
17022 that same display vector (thus their character
17023 positions match), and we have no way of telling if
17024 that is the case. */
17025 && it.current.dpvec_index < 0)
17026 break;
17027
17028 it.glyph_row->reversed_p = false;
17029 if (display_line (&it))
17030 last_text_row = it.glyph_row - 1;
17031
17032 }
17033
17034 /* A value of current_y < last_visible_y means that we stopped
17035 at the previous window start, which in turn means that we
17036 have at least one reusable row. */
17037 if (it.current_y < it.last_visible_y)
17038 {
17039 struct glyph_row *row;
17040
17041 /* IT.vpos always starts from 0; it counts text lines. */
17042 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17043
17044 /* Find PT if not already found in the lines displayed. */
17045 if (w->cursor.vpos < 0)
17046 {
17047 int dy = it.current_y - start_row->y;
17048
17049 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17050 row = row_containing_pos (w, PT, row, NULL, dy);
17051 if (row)
17052 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17053 dy, nrows_scrolled);
17054 else
17055 {
17056 clear_glyph_matrix (w->desired_matrix);
17057 return false;
17058 }
17059 }
17060
17061 /* Scroll the display. Do it before the current matrix is
17062 changed. The problem here is that update has not yet
17063 run, i.e. part of the current matrix is not up to date.
17064 scroll_run_hook will clear the cursor, and use the
17065 current matrix to get the height of the row the cursor is
17066 in. */
17067 run.current_y = start_row->y;
17068 run.desired_y = it.current_y;
17069 run.height = it.last_visible_y - it.current_y;
17070
17071 if (run.height > 0 && run.current_y != run.desired_y)
17072 {
17073 update_begin (f);
17074 FRAME_RIF (f)->update_window_begin_hook (w);
17075 FRAME_RIF (f)->clear_window_mouse_face (w);
17076 FRAME_RIF (f)->scroll_run_hook (w, &run);
17077 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17078 update_end (f);
17079 }
17080
17081 /* Shift current matrix down by nrows_scrolled lines. */
17082 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17083 rotate_matrix (w->current_matrix,
17084 start_vpos,
17085 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17086 nrows_scrolled);
17087
17088 /* Disable lines that must be updated. */
17089 for (i = 0; i < nrows_scrolled; ++i)
17090 (start_row + i)->enabled_p = false;
17091
17092 /* Re-compute Y positions. */
17093 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17094 max_y = it.last_visible_y;
17095 for (row = start_row + nrows_scrolled;
17096 row < bottom_row;
17097 ++row)
17098 {
17099 row->y = it.current_y;
17100 row->visible_height = row->height;
17101
17102 if (row->y < min_y)
17103 row->visible_height -= min_y - row->y;
17104 if (row->y + row->height > max_y)
17105 row->visible_height -= row->y + row->height - max_y;
17106 if (row->fringe_bitmap_periodic_p)
17107 row->redraw_fringe_bitmaps_p = true;
17108
17109 it.current_y += row->height;
17110
17111 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17112 last_reused_text_row = row;
17113 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17114 break;
17115 }
17116
17117 /* Disable lines in the current matrix which are now
17118 below the window. */
17119 for (++row; row < bottom_row; ++row)
17120 row->enabled_p = row->mode_line_p = false;
17121 }
17122
17123 /* Update window_end_pos etc.; last_reused_text_row is the last
17124 reused row from the current matrix containing text, if any.
17125 The value of last_text_row is the last displayed line
17126 containing text. */
17127 if (last_reused_text_row)
17128 adjust_window_ends (w, last_reused_text_row, true);
17129 else if (last_text_row)
17130 adjust_window_ends (w, last_text_row, false);
17131 else
17132 {
17133 /* This window must be completely empty. */
17134 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17135 w->window_end_pos = Z - ZV;
17136 w->window_end_vpos = 0;
17137 }
17138 w->window_end_valid = false;
17139
17140 /* Update hint: don't try scrolling again in update_window. */
17141 w->desired_matrix->no_scrolling_p = true;
17142
17143 #ifdef GLYPH_DEBUG
17144 debug_method_add (w, "try_window_reusing_current_matrix 1");
17145 #endif
17146 return true;
17147 }
17148 else if (CHARPOS (new_start) > CHARPOS (start))
17149 {
17150 struct glyph_row *pt_row, *row;
17151 struct glyph_row *first_reusable_row;
17152 struct glyph_row *first_row_to_display;
17153 int dy;
17154 int yb = window_text_bottom_y (w);
17155
17156 /* Find the row starting at new_start, if there is one. Don't
17157 reuse a partially visible line at the end. */
17158 first_reusable_row = start_row;
17159 while (first_reusable_row->enabled_p
17160 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17161 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17162 < CHARPOS (new_start)))
17163 ++first_reusable_row;
17164
17165 /* Give up if there is no row to reuse. */
17166 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17167 || !first_reusable_row->enabled_p
17168 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17169 != CHARPOS (new_start)))
17170 return false;
17171
17172 /* We can reuse fully visible rows beginning with
17173 first_reusable_row to the end of the window. Set
17174 first_row_to_display to the first row that cannot be reused.
17175 Set pt_row to the row containing point, if there is any. */
17176 pt_row = NULL;
17177 for (first_row_to_display = first_reusable_row;
17178 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17179 ++first_row_to_display)
17180 {
17181 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17182 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17183 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17184 && first_row_to_display->ends_at_zv_p
17185 && pt_row == NULL)))
17186 pt_row = first_row_to_display;
17187 }
17188
17189 /* Start displaying at the start of first_row_to_display. */
17190 eassert (first_row_to_display->y < yb);
17191 init_to_row_start (&it, w, first_row_to_display);
17192
17193 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17194 - start_vpos);
17195 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17196 - nrows_scrolled);
17197 it.current_y = (first_row_to_display->y - first_reusable_row->y
17198 + WINDOW_HEADER_LINE_HEIGHT (w));
17199
17200 /* Display lines beginning with first_row_to_display in the
17201 desired matrix. Set last_text_row to the last row displayed
17202 that displays text. */
17203 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17204 if (pt_row == NULL)
17205 w->cursor.vpos = -1;
17206 last_text_row = NULL;
17207 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17208 if (display_line (&it))
17209 last_text_row = it.glyph_row - 1;
17210
17211 /* If point is in a reused row, adjust y and vpos of the cursor
17212 position. */
17213 if (pt_row)
17214 {
17215 w->cursor.vpos -= nrows_scrolled;
17216 w->cursor.y -= first_reusable_row->y - start_row->y;
17217 }
17218
17219 /* Give up if point isn't in a row displayed or reused. (This
17220 also handles the case where w->cursor.vpos < nrows_scrolled
17221 after the calls to display_line, which can happen with scroll
17222 margins. See bug#1295.) */
17223 if (w->cursor.vpos < 0)
17224 {
17225 clear_glyph_matrix (w->desired_matrix);
17226 return false;
17227 }
17228
17229 /* Scroll the display. */
17230 run.current_y = first_reusable_row->y;
17231 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17232 run.height = it.last_visible_y - run.current_y;
17233 dy = run.current_y - run.desired_y;
17234
17235 if (run.height)
17236 {
17237 update_begin (f);
17238 FRAME_RIF (f)->update_window_begin_hook (w);
17239 FRAME_RIF (f)->clear_window_mouse_face (w);
17240 FRAME_RIF (f)->scroll_run_hook (w, &run);
17241 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17242 update_end (f);
17243 }
17244
17245 /* Adjust Y positions of reused rows. */
17246 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17247 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17248 max_y = it.last_visible_y;
17249 for (row = first_reusable_row; row < first_row_to_display; ++row)
17250 {
17251 row->y -= dy;
17252 row->visible_height = row->height;
17253 if (row->y < min_y)
17254 row->visible_height -= min_y - row->y;
17255 if (row->y + row->height > max_y)
17256 row->visible_height -= row->y + row->height - max_y;
17257 if (row->fringe_bitmap_periodic_p)
17258 row->redraw_fringe_bitmaps_p = true;
17259 }
17260
17261 /* Scroll the current matrix. */
17262 eassert (nrows_scrolled > 0);
17263 rotate_matrix (w->current_matrix,
17264 start_vpos,
17265 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17266 -nrows_scrolled);
17267
17268 /* Disable rows not reused. */
17269 for (row -= nrows_scrolled; row < bottom_row; ++row)
17270 row->enabled_p = false;
17271
17272 /* Point may have moved to a different line, so we cannot assume that
17273 the previous cursor position is valid; locate the correct row. */
17274 if (pt_row)
17275 {
17276 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17277 row < bottom_row
17278 && PT >= MATRIX_ROW_END_CHARPOS (row)
17279 && !row->ends_at_zv_p;
17280 row++)
17281 {
17282 w->cursor.vpos++;
17283 w->cursor.y = row->y;
17284 }
17285 if (row < bottom_row)
17286 {
17287 /* Can't simply scan the row for point with
17288 bidi-reordered glyph rows. Let set_cursor_from_row
17289 figure out where to put the cursor, and if it fails,
17290 give up. */
17291 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17292 {
17293 if (!set_cursor_from_row (w, row, w->current_matrix,
17294 0, 0, 0, 0))
17295 {
17296 clear_glyph_matrix (w->desired_matrix);
17297 return false;
17298 }
17299 }
17300 else
17301 {
17302 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17303 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17304
17305 for (; glyph < end
17306 && (!BUFFERP (glyph->object)
17307 || glyph->charpos < PT);
17308 glyph++)
17309 {
17310 w->cursor.hpos++;
17311 w->cursor.x += glyph->pixel_width;
17312 }
17313 }
17314 }
17315 }
17316
17317 /* Adjust window end. A null value of last_text_row means that
17318 the window end is in reused rows which in turn means that
17319 only its vpos can have changed. */
17320 if (last_text_row)
17321 adjust_window_ends (w, last_text_row, false);
17322 else
17323 w->window_end_vpos -= nrows_scrolled;
17324
17325 w->window_end_valid = false;
17326 w->desired_matrix->no_scrolling_p = true;
17327
17328 #ifdef GLYPH_DEBUG
17329 debug_method_add (w, "try_window_reusing_current_matrix 2");
17330 #endif
17331 return true;
17332 }
17333
17334 return false;
17335 }
17336
17337
17338 \f
17339 /************************************************************************
17340 Window redisplay reusing current matrix when buffer has changed
17341 ************************************************************************/
17342
17343 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17344 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17345 ptrdiff_t *, ptrdiff_t *);
17346 static struct glyph_row *
17347 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17348 struct glyph_row *);
17349
17350
17351 /* Return the last row in MATRIX displaying text. If row START is
17352 non-null, start searching with that row. IT gives the dimensions
17353 of the display. Value is null if matrix is empty; otherwise it is
17354 a pointer to the row found. */
17355
17356 static struct glyph_row *
17357 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17358 struct glyph_row *start)
17359 {
17360 struct glyph_row *row, *row_found;
17361
17362 /* Set row_found to the last row in IT->w's current matrix
17363 displaying text. The loop looks funny but think of partially
17364 visible lines. */
17365 row_found = NULL;
17366 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17367 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17368 {
17369 eassert (row->enabled_p);
17370 row_found = row;
17371 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17372 break;
17373 ++row;
17374 }
17375
17376 return row_found;
17377 }
17378
17379
17380 /* Return the last row in the current matrix of W that is not affected
17381 by changes at the start of current_buffer that occurred since W's
17382 current matrix was built. Value is null if no such row exists.
17383
17384 BEG_UNCHANGED us the number of characters unchanged at the start of
17385 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17386 first changed character in current_buffer. Characters at positions <
17387 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17388 when the current matrix was built. */
17389
17390 static struct glyph_row *
17391 find_last_unchanged_at_beg_row (struct window *w)
17392 {
17393 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17394 struct glyph_row *row;
17395 struct glyph_row *row_found = NULL;
17396 int yb = window_text_bottom_y (w);
17397
17398 /* Find the last row displaying unchanged text. */
17399 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17400 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17401 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17402 ++row)
17403 {
17404 if (/* If row ends before first_changed_pos, it is unchanged,
17405 except in some case. */
17406 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17407 /* When row ends in ZV and we write at ZV it is not
17408 unchanged. */
17409 && !row->ends_at_zv_p
17410 /* When first_changed_pos is the end of a continued line,
17411 row is not unchanged because it may be no longer
17412 continued. */
17413 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17414 && (row->continued_p
17415 || row->exact_window_width_line_p))
17416 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17417 needs to be recomputed, so don't consider this row as
17418 unchanged. This happens when the last line was
17419 bidi-reordered and was killed immediately before this
17420 redisplay cycle. In that case, ROW->end stores the
17421 buffer position of the first visual-order character of
17422 the killed text, which is now beyond ZV. */
17423 && CHARPOS (row->end.pos) <= ZV)
17424 row_found = row;
17425
17426 /* Stop if last visible row. */
17427 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17428 break;
17429 }
17430
17431 return row_found;
17432 }
17433
17434
17435 /* Find the first glyph row in the current matrix of W that is not
17436 affected by changes at the end of current_buffer since the
17437 time W's current matrix was built.
17438
17439 Return in *DELTA the number of chars by which buffer positions in
17440 unchanged text at the end of current_buffer must be adjusted.
17441
17442 Return in *DELTA_BYTES the corresponding number of bytes.
17443
17444 Value is null if no such row exists, i.e. all rows are affected by
17445 changes. */
17446
17447 static struct glyph_row *
17448 find_first_unchanged_at_end_row (struct window *w,
17449 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17450 {
17451 struct glyph_row *row;
17452 struct glyph_row *row_found = NULL;
17453
17454 *delta = *delta_bytes = 0;
17455
17456 /* Display must not have been paused, otherwise the current matrix
17457 is not up to date. */
17458 eassert (w->window_end_valid);
17459
17460 /* A value of window_end_pos >= END_UNCHANGED means that the window
17461 end is in the range of changed text. If so, there is no
17462 unchanged row at the end of W's current matrix. */
17463 if (w->window_end_pos >= END_UNCHANGED)
17464 return NULL;
17465
17466 /* Set row to the last row in W's current matrix displaying text. */
17467 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17468
17469 /* If matrix is entirely empty, no unchanged row exists. */
17470 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17471 {
17472 /* The value of row is the last glyph row in the matrix having a
17473 meaningful buffer position in it. The end position of row
17474 corresponds to window_end_pos. This allows us to translate
17475 buffer positions in the current matrix to current buffer
17476 positions for characters not in changed text. */
17477 ptrdiff_t Z_old =
17478 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17479 ptrdiff_t Z_BYTE_old =
17480 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17481 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17482 struct glyph_row *first_text_row
17483 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17484
17485 *delta = Z - Z_old;
17486 *delta_bytes = Z_BYTE - Z_BYTE_old;
17487
17488 /* Set last_unchanged_pos to the buffer position of the last
17489 character in the buffer that has not been changed. Z is the
17490 index + 1 of the last character in current_buffer, i.e. by
17491 subtracting END_UNCHANGED we get the index of the last
17492 unchanged character, and we have to add BEG to get its buffer
17493 position. */
17494 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17495 last_unchanged_pos_old = last_unchanged_pos - *delta;
17496
17497 /* Search backward from ROW for a row displaying a line that
17498 starts at a minimum position >= last_unchanged_pos_old. */
17499 for (; row > first_text_row; --row)
17500 {
17501 /* This used to abort, but it can happen.
17502 It is ok to just stop the search instead here. KFS. */
17503 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17504 break;
17505
17506 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17507 row_found = row;
17508 }
17509 }
17510
17511 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17512
17513 return row_found;
17514 }
17515
17516
17517 /* Make sure that glyph rows in the current matrix of window W
17518 reference the same glyph memory as corresponding rows in the
17519 frame's frame matrix. This function is called after scrolling W's
17520 current matrix on a terminal frame in try_window_id and
17521 try_window_reusing_current_matrix. */
17522
17523 static void
17524 sync_frame_with_window_matrix_rows (struct window *w)
17525 {
17526 struct frame *f = XFRAME (w->frame);
17527 struct glyph_row *window_row, *window_row_end, *frame_row;
17528
17529 /* Preconditions: W must be a leaf window and full-width. Its frame
17530 must have a frame matrix. */
17531 eassert (BUFFERP (w->contents));
17532 eassert (WINDOW_FULL_WIDTH_P (w));
17533 eassert (!FRAME_WINDOW_P (f));
17534
17535 /* If W is a full-width window, glyph pointers in W's current matrix
17536 have, by definition, to be the same as glyph pointers in the
17537 corresponding frame matrix. Note that frame matrices have no
17538 marginal areas (see build_frame_matrix). */
17539 window_row = w->current_matrix->rows;
17540 window_row_end = window_row + w->current_matrix->nrows;
17541 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17542 while (window_row < window_row_end)
17543 {
17544 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17545 struct glyph *end = window_row->glyphs[LAST_AREA];
17546
17547 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17548 frame_row->glyphs[TEXT_AREA] = start;
17549 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17550 frame_row->glyphs[LAST_AREA] = end;
17551
17552 /* Disable frame rows whose corresponding window rows have
17553 been disabled in try_window_id. */
17554 if (!window_row->enabled_p)
17555 frame_row->enabled_p = false;
17556
17557 ++window_row, ++frame_row;
17558 }
17559 }
17560
17561
17562 /* Find the glyph row in window W containing CHARPOS. Consider all
17563 rows between START and END (not inclusive). END null means search
17564 all rows to the end of the display area of W. Value is the row
17565 containing CHARPOS or null. */
17566
17567 struct glyph_row *
17568 row_containing_pos (struct window *w, ptrdiff_t charpos,
17569 struct glyph_row *start, struct glyph_row *end, int dy)
17570 {
17571 struct glyph_row *row = start;
17572 struct glyph_row *best_row = NULL;
17573 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17574 int last_y;
17575
17576 /* If we happen to start on a header-line, skip that. */
17577 if (row->mode_line_p)
17578 ++row;
17579
17580 if ((end && row >= end) || !row->enabled_p)
17581 return NULL;
17582
17583 last_y = window_text_bottom_y (w) - dy;
17584
17585 while (true)
17586 {
17587 /* Give up if we have gone too far. */
17588 if (end && row >= end)
17589 return NULL;
17590 /* This formerly returned if they were equal.
17591 I think that both quantities are of a "last plus one" type;
17592 if so, when they are equal, the row is within the screen. -- rms. */
17593 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17594 return NULL;
17595
17596 /* If it is in this row, return this row. */
17597 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17598 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17599 /* The end position of a row equals the start
17600 position of the next row. If CHARPOS is there, we
17601 would rather consider it displayed in the next
17602 line, except when this line ends in ZV. */
17603 && !row_for_charpos_p (row, charpos)))
17604 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17605 {
17606 struct glyph *g;
17607
17608 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17609 || (!best_row && !row->continued_p))
17610 return row;
17611 /* In bidi-reordered rows, there could be several rows whose
17612 edges surround CHARPOS, all of these rows belonging to
17613 the same continued line. We need to find the row which
17614 fits CHARPOS the best. */
17615 for (g = row->glyphs[TEXT_AREA];
17616 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17617 g++)
17618 {
17619 if (!STRINGP (g->object))
17620 {
17621 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17622 {
17623 mindif = eabs (g->charpos - charpos);
17624 best_row = row;
17625 /* Exact match always wins. */
17626 if (mindif == 0)
17627 return best_row;
17628 }
17629 }
17630 }
17631 }
17632 else if (best_row && !row->continued_p)
17633 return best_row;
17634 ++row;
17635 }
17636 }
17637
17638
17639 /* Try to redisplay window W by reusing its existing display. W's
17640 current matrix must be up to date when this function is called,
17641 i.e., window_end_valid must be true.
17642
17643 Value is
17644
17645 >= 1 if successful, i.e. display has been updated
17646 specifically:
17647 1 means the changes were in front of a newline that precedes
17648 the window start, and the whole current matrix was reused
17649 2 means the changes were after the last position displayed
17650 in the window, and the whole current matrix was reused
17651 3 means portions of the current matrix were reused, while
17652 some of the screen lines were redrawn
17653 -1 if redisplay with same window start is known not to succeed
17654 0 if otherwise unsuccessful
17655
17656 The following steps are performed:
17657
17658 1. Find the last row in the current matrix of W that is not
17659 affected by changes at the start of current_buffer. If no such row
17660 is found, give up.
17661
17662 2. Find the first row in W's current matrix that is not affected by
17663 changes at the end of current_buffer. Maybe there is no such row.
17664
17665 3. Display lines beginning with the row + 1 found in step 1 to the
17666 row found in step 2 or, if step 2 didn't find a row, to the end of
17667 the window.
17668
17669 4. If cursor is not known to appear on the window, give up.
17670
17671 5. If display stopped at the row found in step 2, scroll the
17672 display and current matrix as needed.
17673
17674 6. Maybe display some lines at the end of W, if we must. This can
17675 happen under various circumstances, like a partially visible line
17676 becoming fully visible, or because newly displayed lines are displayed
17677 in smaller font sizes.
17678
17679 7. Update W's window end information. */
17680
17681 static int
17682 try_window_id (struct window *w)
17683 {
17684 struct frame *f = XFRAME (w->frame);
17685 struct glyph_matrix *current_matrix = w->current_matrix;
17686 struct glyph_matrix *desired_matrix = w->desired_matrix;
17687 struct glyph_row *last_unchanged_at_beg_row;
17688 struct glyph_row *first_unchanged_at_end_row;
17689 struct glyph_row *row;
17690 struct glyph_row *bottom_row;
17691 int bottom_vpos;
17692 struct it it;
17693 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17694 int dvpos, dy;
17695 struct text_pos start_pos;
17696 struct run run;
17697 int first_unchanged_at_end_vpos = 0;
17698 struct glyph_row *last_text_row, *last_text_row_at_end;
17699 struct text_pos start;
17700 ptrdiff_t first_changed_charpos, last_changed_charpos;
17701
17702 #ifdef GLYPH_DEBUG
17703 if (inhibit_try_window_id)
17704 return 0;
17705 #endif
17706
17707 /* This is handy for debugging. */
17708 #if false
17709 #define GIVE_UP(X) \
17710 do { \
17711 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17712 return 0; \
17713 } while (false)
17714 #else
17715 #define GIVE_UP(X) return 0
17716 #endif
17717
17718 SET_TEXT_POS_FROM_MARKER (start, w->start);
17719
17720 /* Don't use this for mini-windows because these can show
17721 messages and mini-buffers, and we don't handle that here. */
17722 if (MINI_WINDOW_P (w))
17723 GIVE_UP (1);
17724
17725 /* This flag is used to prevent redisplay optimizations. */
17726 if (windows_or_buffers_changed || f->cursor_type_changed)
17727 GIVE_UP (2);
17728
17729 /* This function's optimizations cannot be used if overlays have
17730 changed in the buffer displayed by the window, so give up if they
17731 have. */
17732 if (w->last_overlay_modified != OVERLAY_MODIFF)
17733 GIVE_UP (21);
17734
17735 /* Verify that narrowing has not changed.
17736 Also verify that we were not told to prevent redisplay optimizations.
17737 It would be nice to further
17738 reduce the number of cases where this prevents try_window_id. */
17739 if (current_buffer->clip_changed
17740 || current_buffer->prevent_redisplay_optimizations_p)
17741 GIVE_UP (3);
17742
17743 /* Window must either use window-based redisplay or be full width. */
17744 if (!FRAME_WINDOW_P (f)
17745 && (!FRAME_LINE_INS_DEL_OK (f)
17746 || !WINDOW_FULL_WIDTH_P (w)))
17747 GIVE_UP (4);
17748
17749 /* Give up if point is known NOT to appear in W. */
17750 if (PT < CHARPOS (start))
17751 GIVE_UP (5);
17752
17753 /* Another way to prevent redisplay optimizations. */
17754 if (w->last_modified == 0)
17755 GIVE_UP (6);
17756
17757 /* Verify that window is not hscrolled. */
17758 if (w->hscroll != 0)
17759 GIVE_UP (7);
17760
17761 /* Verify that display wasn't paused. */
17762 if (!w->window_end_valid)
17763 GIVE_UP (8);
17764
17765 /* Likewise if highlighting trailing whitespace. */
17766 if (!NILP (Vshow_trailing_whitespace))
17767 GIVE_UP (11);
17768
17769 /* Can't use this if overlay arrow position and/or string have
17770 changed. */
17771 if (overlay_arrows_changed_p ())
17772 GIVE_UP (12);
17773
17774 /* When word-wrap is on, adding a space to the first word of a
17775 wrapped line can change the wrap position, altering the line
17776 above it. It might be worthwhile to handle this more
17777 intelligently, but for now just redisplay from scratch. */
17778 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17779 GIVE_UP (21);
17780
17781 /* Under bidi reordering, adding or deleting a character in the
17782 beginning of a paragraph, before the first strong directional
17783 character, can change the base direction of the paragraph (unless
17784 the buffer specifies a fixed paragraph direction), which will
17785 require to redisplay the whole paragraph. It might be worthwhile
17786 to find the paragraph limits and widen the range of redisplayed
17787 lines to that, but for now just give up this optimization and
17788 redisplay from scratch. */
17789 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17790 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17791 GIVE_UP (22);
17792
17793 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17794 only if buffer has really changed. The reason is that the gap is
17795 initially at Z for freshly visited files. The code below would
17796 set end_unchanged to 0 in that case. */
17797 if (MODIFF > SAVE_MODIFF
17798 /* This seems to happen sometimes after saving a buffer. */
17799 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17800 {
17801 if (GPT - BEG < BEG_UNCHANGED)
17802 BEG_UNCHANGED = GPT - BEG;
17803 if (Z - GPT < END_UNCHANGED)
17804 END_UNCHANGED = Z - GPT;
17805 }
17806
17807 /* The position of the first and last character that has been changed. */
17808 first_changed_charpos = BEG + BEG_UNCHANGED;
17809 last_changed_charpos = Z - END_UNCHANGED;
17810
17811 /* If window starts after a line end, and the last change is in
17812 front of that newline, then changes don't affect the display.
17813 This case happens with stealth-fontification. Note that although
17814 the display is unchanged, glyph positions in the matrix have to
17815 be adjusted, of course. */
17816 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17817 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17818 && ((last_changed_charpos < CHARPOS (start)
17819 && CHARPOS (start) == BEGV)
17820 || (last_changed_charpos < CHARPOS (start) - 1
17821 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17822 {
17823 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17824 struct glyph_row *r0;
17825
17826 /* Compute how many chars/bytes have been added to or removed
17827 from the buffer. */
17828 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17829 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17830 Z_delta = Z - Z_old;
17831 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17832
17833 /* Give up if PT is not in the window. Note that it already has
17834 been checked at the start of try_window_id that PT is not in
17835 front of the window start. */
17836 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17837 GIVE_UP (13);
17838
17839 /* If window start is unchanged, we can reuse the whole matrix
17840 as is, after adjusting glyph positions. No need to compute
17841 the window end again, since its offset from Z hasn't changed. */
17842 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17843 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17844 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17845 /* PT must not be in a partially visible line. */
17846 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17847 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17848 {
17849 /* Adjust positions in the glyph matrix. */
17850 if (Z_delta || Z_delta_bytes)
17851 {
17852 struct glyph_row *r1
17853 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17854 increment_matrix_positions (w->current_matrix,
17855 MATRIX_ROW_VPOS (r0, current_matrix),
17856 MATRIX_ROW_VPOS (r1, current_matrix),
17857 Z_delta, Z_delta_bytes);
17858 }
17859
17860 /* Set the cursor. */
17861 row = row_containing_pos (w, PT, r0, NULL, 0);
17862 if (row)
17863 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17864 return 1;
17865 }
17866 }
17867
17868 /* Handle the case that changes are all below what is displayed in
17869 the window, and that PT is in the window. This shortcut cannot
17870 be taken if ZV is visible in the window, and text has been added
17871 there that is visible in the window. */
17872 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17873 /* ZV is not visible in the window, or there are no
17874 changes at ZV, actually. */
17875 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17876 || first_changed_charpos == last_changed_charpos))
17877 {
17878 struct glyph_row *r0;
17879
17880 /* Give up if PT is not in the window. Note that it already has
17881 been checked at the start of try_window_id that PT is not in
17882 front of the window start. */
17883 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17884 GIVE_UP (14);
17885
17886 /* If window start is unchanged, we can reuse the whole matrix
17887 as is, without changing glyph positions since no text has
17888 been added/removed in front of the window end. */
17889 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17890 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17891 /* PT must not be in a partially visible line. */
17892 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17893 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17894 {
17895 /* We have to compute the window end anew since text
17896 could have been added/removed after it. */
17897 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17898 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17899
17900 /* Set the cursor. */
17901 row = row_containing_pos (w, PT, r0, NULL, 0);
17902 if (row)
17903 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17904 return 2;
17905 }
17906 }
17907
17908 /* Give up if window start is in the changed area.
17909
17910 The condition used to read
17911
17912 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17913
17914 but why that was tested escapes me at the moment. */
17915 if (CHARPOS (start) >= first_changed_charpos
17916 && CHARPOS (start) <= last_changed_charpos)
17917 GIVE_UP (15);
17918
17919 /* Check that window start agrees with the start of the first glyph
17920 row in its current matrix. Check this after we know the window
17921 start is not in changed text, otherwise positions would not be
17922 comparable. */
17923 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17924 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17925 GIVE_UP (16);
17926
17927 /* Give up if the window ends in strings. Overlay strings
17928 at the end are difficult to handle, so don't try. */
17929 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17930 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17931 GIVE_UP (20);
17932
17933 /* Compute the position at which we have to start displaying new
17934 lines. Some of the lines at the top of the window might be
17935 reusable because they are not displaying changed text. Find the
17936 last row in W's current matrix not affected by changes at the
17937 start of current_buffer. Value is null if changes start in the
17938 first line of window. */
17939 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17940 if (last_unchanged_at_beg_row)
17941 {
17942 /* Avoid starting to display in the middle of a character, a TAB
17943 for instance. This is easier than to set up the iterator
17944 exactly, and it's not a frequent case, so the additional
17945 effort wouldn't really pay off. */
17946 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17947 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17948 && last_unchanged_at_beg_row > w->current_matrix->rows)
17949 --last_unchanged_at_beg_row;
17950
17951 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17952 GIVE_UP (17);
17953
17954 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
17955 GIVE_UP (18);
17956 start_pos = it.current.pos;
17957
17958 /* Start displaying new lines in the desired matrix at the same
17959 vpos we would use in the current matrix, i.e. below
17960 last_unchanged_at_beg_row. */
17961 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17962 current_matrix);
17963 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17964 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17965
17966 eassert (it.hpos == 0 && it.current_x == 0);
17967 }
17968 else
17969 {
17970 /* There are no reusable lines at the start of the window.
17971 Start displaying in the first text line. */
17972 start_display (&it, w, start);
17973 it.vpos = it.first_vpos;
17974 start_pos = it.current.pos;
17975 }
17976
17977 /* Find the first row that is not affected by changes at the end of
17978 the buffer. Value will be null if there is no unchanged row, in
17979 which case we must redisplay to the end of the window. delta
17980 will be set to the value by which buffer positions beginning with
17981 first_unchanged_at_end_row have to be adjusted due to text
17982 changes. */
17983 first_unchanged_at_end_row
17984 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17985 IF_DEBUG (debug_delta = delta);
17986 IF_DEBUG (debug_delta_bytes = delta_bytes);
17987
17988 /* Set stop_pos to the buffer position up to which we will have to
17989 display new lines. If first_unchanged_at_end_row != NULL, this
17990 is the buffer position of the start of the line displayed in that
17991 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17992 that we don't stop at a buffer position. */
17993 stop_pos = 0;
17994 if (first_unchanged_at_end_row)
17995 {
17996 eassert (last_unchanged_at_beg_row == NULL
17997 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17998
17999 /* If this is a continuation line, move forward to the next one
18000 that isn't. Changes in lines above affect this line.
18001 Caution: this may move first_unchanged_at_end_row to a row
18002 not displaying text. */
18003 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18004 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18005 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18006 < it.last_visible_y))
18007 ++first_unchanged_at_end_row;
18008
18009 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18010 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18011 >= it.last_visible_y))
18012 first_unchanged_at_end_row = NULL;
18013 else
18014 {
18015 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18016 + delta);
18017 first_unchanged_at_end_vpos
18018 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18019 eassert (stop_pos >= Z - END_UNCHANGED);
18020 }
18021 }
18022 else if (last_unchanged_at_beg_row == NULL)
18023 GIVE_UP (19);
18024
18025
18026 #ifdef GLYPH_DEBUG
18027
18028 /* Either there is no unchanged row at the end, or the one we have
18029 now displays text. This is a necessary condition for the window
18030 end pos calculation at the end of this function. */
18031 eassert (first_unchanged_at_end_row == NULL
18032 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18033
18034 debug_last_unchanged_at_beg_vpos
18035 = (last_unchanged_at_beg_row
18036 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18037 : -1);
18038 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18039
18040 #endif /* GLYPH_DEBUG */
18041
18042
18043 /* Display new lines. Set last_text_row to the last new line
18044 displayed which has text on it, i.e. might end up as being the
18045 line where the window_end_vpos is. */
18046 w->cursor.vpos = -1;
18047 last_text_row = NULL;
18048 overlay_arrow_seen = false;
18049 if (it.current_y < it.last_visible_y
18050 && !f->fonts_changed
18051 && (first_unchanged_at_end_row == NULL
18052 || IT_CHARPOS (it) < stop_pos))
18053 it.glyph_row->reversed_p = false;
18054 while (it.current_y < it.last_visible_y
18055 && !f->fonts_changed
18056 && (first_unchanged_at_end_row == NULL
18057 || IT_CHARPOS (it) < stop_pos))
18058 {
18059 if (display_line (&it))
18060 last_text_row = it.glyph_row - 1;
18061 }
18062
18063 if (f->fonts_changed)
18064 return -1;
18065
18066 /* The redisplay iterations in display_line above could have
18067 triggered font-lock, which could have done something that
18068 invalidates IT->w window's end-point information, on which we
18069 rely below. E.g., one package, which will remain unnamed, used
18070 to install a font-lock-fontify-region-function that called
18071 bury-buffer, whose side effect is to switch the buffer displayed
18072 by IT->w, and that predictably resets IT->w's window_end_valid
18073 flag, which we already tested at the entry to this function.
18074 Amply punish such packages/modes by giving up on this
18075 optimization in those cases. */
18076 if (!w->window_end_valid)
18077 {
18078 clear_glyph_matrix (w->desired_matrix);
18079 return -1;
18080 }
18081
18082 /* Compute differences in buffer positions, y-positions etc. for
18083 lines reused at the bottom of the window. Compute what we can
18084 scroll. */
18085 if (first_unchanged_at_end_row
18086 /* No lines reused because we displayed everything up to the
18087 bottom of the window. */
18088 && it.current_y < it.last_visible_y)
18089 {
18090 dvpos = (it.vpos
18091 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18092 current_matrix));
18093 dy = it.current_y - first_unchanged_at_end_row->y;
18094 run.current_y = first_unchanged_at_end_row->y;
18095 run.desired_y = run.current_y + dy;
18096 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18097 }
18098 else
18099 {
18100 delta = delta_bytes = dvpos = dy
18101 = run.current_y = run.desired_y = run.height = 0;
18102 first_unchanged_at_end_row = NULL;
18103 }
18104 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18105
18106
18107 /* Find the cursor if not already found. We have to decide whether
18108 PT will appear on this window (it sometimes doesn't, but this is
18109 not a very frequent case.) This decision has to be made before
18110 the current matrix is altered. A value of cursor.vpos < 0 means
18111 that PT is either in one of the lines beginning at
18112 first_unchanged_at_end_row or below the window. Don't care for
18113 lines that might be displayed later at the window end; as
18114 mentioned, this is not a frequent case. */
18115 if (w->cursor.vpos < 0)
18116 {
18117 /* Cursor in unchanged rows at the top? */
18118 if (PT < CHARPOS (start_pos)
18119 && last_unchanged_at_beg_row)
18120 {
18121 row = row_containing_pos (w, PT,
18122 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18123 last_unchanged_at_beg_row + 1, 0);
18124 if (row)
18125 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18126 }
18127
18128 /* Start from first_unchanged_at_end_row looking for PT. */
18129 else if (first_unchanged_at_end_row)
18130 {
18131 row = row_containing_pos (w, PT - delta,
18132 first_unchanged_at_end_row, NULL, 0);
18133 if (row)
18134 set_cursor_from_row (w, row, w->current_matrix, delta,
18135 delta_bytes, dy, dvpos);
18136 }
18137
18138 /* Give up if cursor was not found. */
18139 if (w->cursor.vpos < 0)
18140 {
18141 clear_glyph_matrix (w->desired_matrix);
18142 return -1;
18143 }
18144 }
18145
18146 /* Don't let the cursor end in the scroll margins. */
18147 {
18148 int this_scroll_margin, cursor_height;
18149 int frame_line_height = default_line_pixel_height (w);
18150 int window_total_lines
18151 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18152
18153 this_scroll_margin =
18154 max (0, min (scroll_margin, window_total_lines / 4));
18155 this_scroll_margin *= frame_line_height;
18156 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18157
18158 if ((w->cursor.y < this_scroll_margin
18159 && CHARPOS (start) > BEGV)
18160 /* Old redisplay didn't take scroll margin into account at the bottom,
18161 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18162 || (w->cursor.y + (make_cursor_line_fully_visible_p
18163 ? cursor_height + this_scroll_margin
18164 : 1)) > it.last_visible_y)
18165 {
18166 w->cursor.vpos = -1;
18167 clear_glyph_matrix (w->desired_matrix);
18168 return -1;
18169 }
18170 }
18171
18172 /* Scroll the display. Do it before changing the current matrix so
18173 that xterm.c doesn't get confused about where the cursor glyph is
18174 found. */
18175 if (dy && run.height)
18176 {
18177 update_begin (f);
18178
18179 if (FRAME_WINDOW_P (f))
18180 {
18181 FRAME_RIF (f)->update_window_begin_hook (w);
18182 FRAME_RIF (f)->clear_window_mouse_face (w);
18183 FRAME_RIF (f)->scroll_run_hook (w, &run);
18184 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18185 }
18186 else
18187 {
18188 /* Terminal frame. In this case, dvpos gives the number of
18189 lines to scroll by; dvpos < 0 means scroll up. */
18190 int from_vpos
18191 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18192 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18193 int end = (WINDOW_TOP_EDGE_LINE (w)
18194 + WINDOW_WANTS_HEADER_LINE_P (w)
18195 + window_internal_height (w));
18196
18197 #if defined (HAVE_GPM) || defined (MSDOS)
18198 x_clear_window_mouse_face (w);
18199 #endif
18200 /* Perform the operation on the screen. */
18201 if (dvpos > 0)
18202 {
18203 /* Scroll last_unchanged_at_beg_row to the end of the
18204 window down dvpos lines. */
18205 set_terminal_window (f, end);
18206
18207 /* On dumb terminals delete dvpos lines at the end
18208 before inserting dvpos empty lines. */
18209 if (!FRAME_SCROLL_REGION_OK (f))
18210 ins_del_lines (f, end - dvpos, -dvpos);
18211
18212 /* Insert dvpos empty lines in front of
18213 last_unchanged_at_beg_row. */
18214 ins_del_lines (f, from, dvpos);
18215 }
18216 else if (dvpos < 0)
18217 {
18218 /* Scroll up last_unchanged_at_beg_vpos to the end of
18219 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18220 set_terminal_window (f, end);
18221
18222 /* Delete dvpos lines in front of
18223 last_unchanged_at_beg_vpos. ins_del_lines will set
18224 the cursor to the given vpos and emit |dvpos| delete
18225 line sequences. */
18226 ins_del_lines (f, from + dvpos, dvpos);
18227
18228 /* On a dumb terminal insert dvpos empty lines at the
18229 end. */
18230 if (!FRAME_SCROLL_REGION_OK (f))
18231 ins_del_lines (f, end + dvpos, -dvpos);
18232 }
18233
18234 set_terminal_window (f, 0);
18235 }
18236
18237 update_end (f);
18238 }
18239
18240 /* Shift reused rows of the current matrix to the right position.
18241 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18242 text. */
18243 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18244 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18245 if (dvpos < 0)
18246 {
18247 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18248 bottom_vpos, dvpos);
18249 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18250 bottom_vpos);
18251 }
18252 else if (dvpos > 0)
18253 {
18254 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18255 bottom_vpos, dvpos);
18256 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18257 first_unchanged_at_end_vpos + dvpos);
18258 }
18259
18260 /* For frame-based redisplay, make sure that current frame and window
18261 matrix are in sync with respect to glyph memory. */
18262 if (!FRAME_WINDOW_P (f))
18263 sync_frame_with_window_matrix_rows (w);
18264
18265 /* Adjust buffer positions in reused rows. */
18266 if (delta || delta_bytes)
18267 increment_matrix_positions (current_matrix,
18268 first_unchanged_at_end_vpos + dvpos,
18269 bottom_vpos, delta, delta_bytes);
18270
18271 /* Adjust Y positions. */
18272 if (dy)
18273 shift_glyph_matrix (w, current_matrix,
18274 first_unchanged_at_end_vpos + dvpos,
18275 bottom_vpos, dy);
18276
18277 if (first_unchanged_at_end_row)
18278 {
18279 first_unchanged_at_end_row += dvpos;
18280 if (first_unchanged_at_end_row->y >= it.last_visible_y
18281 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18282 first_unchanged_at_end_row = NULL;
18283 }
18284
18285 /* If scrolling up, there may be some lines to display at the end of
18286 the window. */
18287 last_text_row_at_end = NULL;
18288 if (dy < 0)
18289 {
18290 /* Scrolling up can leave for example a partially visible line
18291 at the end of the window to be redisplayed. */
18292 /* Set last_row to the glyph row in the current matrix where the
18293 window end line is found. It has been moved up or down in
18294 the matrix by dvpos. */
18295 int last_vpos = w->window_end_vpos + dvpos;
18296 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18297
18298 /* If last_row is the window end line, it should display text. */
18299 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18300
18301 /* If window end line was partially visible before, begin
18302 displaying at that line. Otherwise begin displaying with the
18303 line following it. */
18304 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18305 {
18306 init_to_row_start (&it, w, last_row);
18307 it.vpos = last_vpos;
18308 it.current_y = last_row->y;
18309 }
18310 else
18311 {
18312 init_to_row_end (&it, w, last_row);
18313 it.vpos = 1 + last_vpos;
18314 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18315 ++last_row;
18316 }
18317
18318 /* We may start in a continuation line. If so, we have to
18319 get the right continuation_lines_width and current_x. */
18320 it.continuation_lines_width = last_row->continuation_lines_width;
18321 it.hpos = it.current_x = 0;
18322
18323 /* Display the rest of the lines at the window end. */
18324 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18325 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18326 {
18327 /* Is it always sure that the display agrees with lines in
18328 the current matrix? I don't think so, so we mark rows
18329 displayed invalid in the current matrix by setting their
18330 enabled_p flag to false. */
18331 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18332 if (display_line (&it))
18333 last_text_row_at_end = it.glyph_row - 1;
18334 }
18335 }
18336
18337 /* Update window_end_pos and window_end_vpos. */
18338 if (first_unchanged_at_end_row && !last_text_row_at_end)
18339 {
18340 /* Window end line if one of the preserved rows from the current
18341 matrix. Set row to the last row displaying text in current
18342 matrix starting at first_unchanged_at_end_row, after
18343 scrolling. */
18344 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18345 row = find_last_row_displaying_text (w->current_matrix, &it,
18346 first_unchanged_at_end_row);
18347 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18348 adjust_window_ends (w, row, true);
18349 eassert (w->window_end_bytepos >= 0);
18350 IF_DEBUG (debug_method_add (w, "A"));
18351 }
18352 else if (last_text_row_at_end)
18353 {
18354 adjust_window_ends (w, last_text_row_at_end, false);
18355 eassert (w->window_end_bytepos >= 0);
18356 IF_DEBUG (debug_method_add (w, "B"));
18357 }
18358 else if (last_text_row)
18359 {
18360 /* We have displayed either to the end of the window or at the
18361 end of the window, i.e. the last row with text is to be found
18362 in the desired matrix. */
18363 adjust_window_ends (w, last_text_row, false);
18364 eassert (w->window_end_bytepos >= 0);
18365 }
18366 else if (first_unchanged_at_end_row == NULL
18367 && last_text_row == NULL
18368 && last_text_row_at_end == NULL)
18369 {
18370 /* Displayed to end of window, but no line containing text was
18371 displayed. Lines were deleted at the end of the window. */
18372 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18373 int vpos = w->window_end_vpos;
18374 struct glyph_row *current_row = current_matrix->rows + vpos;
18375 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18376
18377 for (row = NULL;
18378 row == NULL && vpos >= first_vpos;
18379 --vpos, --current_row, --desired_row)
18380 {
18381 if (desired_row->enabled_p)
18382 {
18383 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18384 row = desired_row;
18385 }
18386 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18387 row = current_row;
18388 }
18389
18390 eassert (row != NULL);
18391 w->window_end_vpos = vpos + 1;
18392 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18393 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18394 eassert (w->window_end_bytepos >= 0);
18395 IF_DEBUG (debug_method_add (w, "C"));
18396 }
18397 else
18398 emacs_abort ();
18399
18400 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18401 debug_end_vpos = w->window_end_vpos));
18402
18403 /* Record that display has not been completed. */
18404 w->window_end_valid = false;
18405 w->desired_matrix->no_scrolling_p = true;
18406 return 3;
18407
18408 #undef GIVE_UP
18409 }
18410
18411
18412 \f
18413 /***********************************************************************
18414 More debugging support
18415 ***********************************************************************/
18416
18417 #ifdef GLYPH_DEBUG
18418
18419 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18420 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18421 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18422
18423
18424 /* Dump the contents of glyph matrix MATRIX on stderr.
18425
18426 GLYPHS 0 means don't show glyph contents.
18427 GLYPHS 1 means show glyphs in short form
18428 GLYPHS > 1 means show glyphs in long form. */
18429
18430 void
18431 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18432 {
18433 int i;
18434 for (i = 0; i < matrix->nrows; ++i)
18435 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18436 }
18437
18438
18439 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18440 the glyph row and area where the glyph comes from. */
18441
18442 void
18443 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18444 {
18445 if (glyph->type == CHAR_GLYPH
18446 || glyph->type == GLYPHLESS_GLYPH)
18447 {
18448 fprintf (stderr,
18449 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18450 glyph - row->glyphs[TEXT_AREA],
18451 (glyph->type == CHAR_GLYPH
18452 ? 'C'
18453 : 'G'),
18454 glyph->charpos,
18455 (BUFFERP (glyph->object)
18456 ? 'B'
18457 : (STRINGP (glyph->object)
18458 ? 'S'
18459 : (NILP (glyph->object)
18460 ? '0'
18461 : '-'))),
18462 glyph->pixel_width,
18463 glyph->u.ch,
18464 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18465 ? glyph->u.ch
18466 : '.'),
18467 glyph->face_id,
18468 glyph->left_box_line_p,
18469 glyph->right_box_line_p);
18470 }
18471 else if (glyph->type == STRETCH_GLYPH)
18472 {
18473 fprintf (stderr,
18474 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18475 glyph - row->glyphs[TEXT_AREA],
18476 'S',
18477 glyph->charpos,
18478 (BUFFERP (glyph->object)
18479 ? 'B'
18480 : (STRINGP (glyph->object)
18481 ? 'S'
18482 : (NILP (glyph->object)
18483 ? '0'
18484 : '-'))),
18485 glyph->pixel_width,
18486 0,
18487 ' ',
18488 glyph->face_id,
18489 glyph->left_box_line_p,
18490 glyph->right_box_line_p);
18491 }
18492 else if (glyph->type == IMAGE_GLYPH)
18493 {
18494 fprintf (stderr,
18495 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18496 glyph - row->glyphs[TEXT_AREA],
18497 'I',
18498 glyph->charpos,
18499 (BUFFERP (glyph->object)
18500 ? 'B'
18501 : (STRINGP (glyph->object)
18502 ? 'S'
18503 : (NILP (glyph->object)
18504 ? '0'
18505 : '-'))),
18506 glyph->pixel_width,
18507 glyph->u.img_id,
18508 '.',
18509 glyph->face_id,
18510 glyph->left_box_line_p,
18511 glyph->right_box_line_p);
18512 }
18513 else if (glyph->type == COMPOSITE_GLYPH)
18514 {
18515 fprintf (stderr,
18516 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18517 glyph - row->glyphs[TEXT_AREA],
18518 '+',
18519 glyph->charpos,
18520 (BUFFERP (glyph->object)
18521 ? 'B'
18522 : (STRINGP (glyph->object)
18523 ? 'S'
18524 : (NILP (glyph->object)
18525 ? '0'
18526 : '-'))),
18527 glyph->pixel_width,
18528 glyph->u.cmp.id);
18529 if (glyph->u.cmp.automatic)
18530 fprintf (stderr,
18531 "[%d-%d]",
18532 glyph->slice.cmp.from, glyph->slice.cmp.to);
18533 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18534 glyph->face_id,
18535 glyph->left_box_line_p,
18536 glyph->right_box_line_p);
18537 }
18538 }
18539
18540
18541 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18542 GLYPHS 0 means don't show glyph contents.
18543 GLYPHS 1 means show glyphs in short form
18544 GLYPHS > 1 means show glyphs in long form. */
18545
18546 void
18547 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18548 {
18549 if (glyphs != 1)
18550 {
18551 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18552 fprintf (stderr, "==============================================================================\n");
18553
18554 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18555 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18556 vpos,
18557 MATRIX_ROW_START_CHARPOS (row),
18558 MATRIX_ROW_END_CHARPOS (row),
18559 row->used[TEXT_AREA],
18560 row->contains_overlapping_glyphs_p,
18561 row->enabled_p,
18562 row->truncated_on_left_p,
18563 row->truncated_on_right_p,
18564 row->continued_p,
18565 MATRIX_ROW_CONTINUATION_LINE_P (row),
18566 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18567 row->ends_at_zv_p,
18568 row->fill_line_p,
18569 row->ends_in_middle_of_char_p,
18570 row->starts_in_middle_of_char_p,
18571 row->mouse_face_p,
18572 row->x,
18573 row->y,
18574 row->pixel_width,
18575 row->height,
18576 row->visible_height,
18577 row->ascent,
18578 row->phys_ascent);
18579 /* The next 3 lines should align to "Start" in the header. */
18580 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18581 row->end.overlay_string_index,
18582 row->continuation_lines_width);
18583 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18584 CHARPOS (row->start.string_pos),
18585 CHARPOS (row->end.string_pos));
18586 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18587 row->end.dpvec_index);
18588 }
18589
18590 if (glyphs > 1)
18591 {
18592 int area;
18593
18594 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18595 {
18596 struct glyph *glyph = row->glyphs[area];
18597 struct glyph *glyph_end = glyph + row->used[area];
18598
18599 /* Glyph for a line end in text. */
18600 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18601 ++glyph_end;
18602
18603 if (glyph < glyph_end)
18604 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18605
18606 for (; glyph < glyph_end; ++glyph)
18607 dump_glyph (row, glyph, area);
18608 }
18609 }
18610 else if (glyphs == 1)
18611 {
18612 int area;
18613 char s[SHRT_MAX + 4];
18614
18615 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18616 {
18617 int i;
18618
18619 for (i = 0; i < row->used[area]; ++i)
18620 {
18621 struct glyph *glyph = row->glyphs[area] + i;
18622 if (i == row->used[area] - 1
18623 && area == TEXT_AREA
18624 && NILP (glyph->object)
18625 && glyph->type == CHAR_GLYPH
18626 && glyph->u.ch == ' ')
18627 {
18628 strcpy (&s[i], "[\\n]");
18629 i += 4;
18630 }
18631 else if (glyph->type == CHAR_GLYPH
18632 && glyph->u.ch < 0x80
18633 && glyph->u.ch >= ' ')
18634 s[i] = glyph->u.ch;
18635 else
18636 s[i] = '.';
18637 }
18638
18639 s[i] = '\0';
18640 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18641 }
18642 }
18643 }
18644
18645
18646 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18647 Sdump_glyph_matrix, 0, 1, "p",
18648 doc: /* Dump the current matrix of the selected window to stderr.
18649 Shows contents of glyph row structures. With non-nil
18650 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18651 glyphs in short form, otherwise show glyphs in long form.
18652
18653 Interactively, no argument means show glyphs in short form;
18654 with numeric argument, its value is passed as the GLYPHS flag. */)
18655 (Lisp_Object glyphs)
18656 {
18657 struct window *w = XWINDOW (selected_window);
18658 struct buffer *buffer = XBUFFER (w->contents);
18659
18660 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18661 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18662 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18663 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18664 fprintf (stderr, "=============================================\n");
18665 dump_glyph_matrix (w->current_matrix,
18666 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18667 return Qnil;
18668 }
18669
18670
18671 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18672 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18673 Only text-mode frames have frame glyph matrices. */)
18674 (void)
18675 {
18676 struct frame *f = XFRAME (selected_frame);
18677
18678 if (f->current_matrix)
18679 dump_glyph_matrix (f->current_matrix, 1);
18680 else
18681 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18682 return Qnil;
18683 }
18684
18685
18686 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18687 doc: /* Dump glyph row ROW to stderr.
18688 GLYPH 0 means don't dump glyphs.
18689 GLYPH 1 means dump glyphs in short form.
18690 GLYPH > 1 or omitted means dump glyphs in long form. */)
18691 (Lisp_Object row, Lisp_Object glyphs)
18692 {
18693 struct glyph_matrix *matrix;
18694 EMACS_INT vpos;
18695
18696 CHECK_NUMBER (row);
18697 matrix = XWINDOW (selected_window)->current_matrix;
18698 vpos = XINT (row);
18699 if (vpos >= 0 && vpos < matrix->nrows)
18700 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18701 vpos,
18702 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18703 return Qnil;
18704 }
18705
18706
18707 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18708 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18709 GLYPH 0 means don't dump glyphs.
18710 GLYPH 1 means dump glyphs in short form.
18711 GLYPH > 1 or omitted means dump glyphs in long form.
18712
18713 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18714 do nothing. */)
18715 (Lisp_Object row, Lisp_Object glyphs)
18716 {
18717 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18718 struct frame *sf = SELECTED_FRAME ();
18719 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18720 EMACS_INT vpos;
18721
18722 CHECK_NUMBER (row);
18723 vpos = XINT (row);
18724 if (vpos >= 0 && vpos < m->nrows)
18725 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18726 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18727 #endif
18728 return Qnil;
18729 }
18730
18731
18732 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18733 doc: /* Toggle tracing of redisplay.
18734 With ARG, turn tracing on if and only if ARG is positive. */)
18735 (Lisp_Object arg)
18736 {
18737 if (NILP (arg))
18738 trace_redisplay_p = !trace_redisplay_p;
18739 else
18740 {
18741 arg = Fprefix_numeric_value (arg);
18742 trace_redisplay_p = XINT (arg) > 0;
18743 }
18744
18745 return Qnil;
18746 }
18747
18748
18749 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18750 doc: /* Like `format', but print result to stderr.
18751 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18752 (ptrdiff_t nargs, Lisp_Object *args)
18753 {
18754 Lisp_Object s = Fformat (nargs, args);
18755 fwrite (SDATA (s), 1, SBYTES (s), stderr);
18756 return Qnil;
18757 }
18758
18759 #endif /* GLYPH_DEBUG */
18760
18761
18762 \f
18763 /***********************************************************************
18764 Building Desired Matrix Rows
18765 ***********************************************************************/
18766
18767 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18768 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18769
18770 static struct glyph_row *
18771 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18772 {
18773 struct frame *f = XFRAME (WINDOW_FRAME (w));
18774 struct buffer *buffer = XBUFFER (w->contents);
18775 struct buffer *old = current_buffer;
18776 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18777 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
18778 const unsigned char *arrow_end = arrow_string + arrow_len;
18779 const unsigned char *p;
18780 struct it it;
18781 bool multibyte_p;
18782 int n_glyphs_before;
18783
18784 set_buffer_temp (buffer);
18785 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18786 scratch_glyph_row.reversed_p = false;
18787 it.glyph_row->used[TEXT_AREA] = 0;
18788 SET_TEXT_POS (it.position, 0, 0);
18789
18790 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18791 p = arrow_string;
18792 while (p < arrow_end)
18793 {
18794 Lisp_Object face, ilisp;
18795
18796 /* Get the next character. */
18797 if (multibyte_p)
18798 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18799 else
18800 {
18801 it.c = it.char_to_display = *p, it.len = 1;
18802 if (! ASCII_CHAR_P (it.c))
18803 it.char_to_display = BYTE8_TO_CHAR (it.c);
18804 }
18805 p += it.len;
18806
18807 /* Get its face. */
18808 ilisp = make_number (p - arrow_string);
18809 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18810 it.face_id = compute_char_face (f, it.char_to_display, face);
18811
18812 /* Compute its width, get its glyphs. */
18813 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18814 SET_TEXT_POS (it.position, -1, -1);
18815 PRODUCE_GLYPHS (&it);
18816
18817 /* If this character doesn't fit any more in the line, we have
18818 to remove some glyphs. */
18819 if (it.current_x > it.last_visible_x)
18820 {
18821 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18822 break;
18823 }
18824 }
18825
18826 set_buffer_temp (old);
18827 return it.glyph_row;
18828 }
18829
18830
18831 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18832 glyphs to insert is determined by produce_special_glyphs. */
18833
18834 static void
18835 insert_left_trunc_glyphs (struct it *it)
18836 {
18837 struct it truncate_it;
18838 struct glyph *from, *end, *to, *toend;
18839
18840 eassert (!FRAME_WINDOW_P (it->f)
18841 || (!it->glyph_row->reversed_p
18842 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18843 || (it->glyph_row->reversed_p
18844 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18845
18846 /* Get the truncation glyphs. */
18847 truncate_it = *it;
18848 truncate_it.current_x = 0;
18849 truncate_it.face_id = DEFAULT_FACE_ID;
18850 truncate_it.glyph_row = &scratch_glyph_row;
18851 truncate_it.area = TEXT_AREA;
18852 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18853 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18854 truncate_it.object = Qnil;
18855 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18856
18857 /* Overwrite glyphs from IT with truncation glyphs. */
18858 if (!it->glyph_row->reversed_p)
18859 {
18860 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18861
18862 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18863 end = from + tused;
18864 to = it->glyph_row->glyphs[TEXT_AREA];
18865 toend = to + it->glyph_row->used[TEXT_AREA];
18866 if (FRAME_WINDOW_P (it->f))
18867 {
18868 /* On GUI frames, when variable-size fonts are displayed,
18869 the truncation glyphs may need more pixels than the row's
18870 glyphs they overwrite. We overwrite more glyphs to free
18871 enough screen real estate, and enlarge the stretch glyph
18872 on the right (see display_line), if there is one, to
18873 preserve the screen position of the truncation glyphs on
18874 the right. */
18875 int w = 0;
18876 struct glyph *g = to;
18877 short used;
18878
18879 /* The first glyph could be partially visible, in which case
18880 it->glyph_row->x will be negative. But we want the left
18881 truncation glyphs to be aligned at the left margin of the
18882 window, so we override the x coordinate at which the row
18883 will begin. */
18884 it->glyph_row->x = 0;
18885 while (g < toend && w < it->truncation_pixel_width)
18886 {
18887 w += g->pixel_width;
18888 ++g;
18889 }
18890 if (g - to - tused > 0)
18891 {
18892 memmove (to + tused, g, (toend - g) * sizeof(*g));
18893 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18894 }
18895 used = it->glyph_row->used[TEXT_AREA];
18896 if (it->glyph_row->truncated_on_right_p
18897 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18898 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18899 == STRETCH_GLYPH)
18900 {
18901 int extra = w - it->truncation_pixel_width;
18902
18903 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18904 }
18905 }
18906
18907 while (from < end)
18908 *to++ = *from++;
18909
18910 /* There may be padding glyphs left over. Overwrite them too. */
18911 if (!FRAME_WINDOW_P (it->f))
18912 {
18913 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18914 {
18915 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18916 while (from < end)
18917 *to++ = *from++;
18918 }
18919 }
18920
18921 if (to > toend)
18922 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18923 }
18924 else
18925 {
18926 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18927
18928 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18929 that back to front. */
18930 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18931 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18932 toend = it->glyph_row->glyphs[TEXT_AREA];
18933 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18934 if (FRAME_WINDOW_P (it->f))
18935 {
18936 int w = 0;
18937 struct glyph *g = to;
18938
18939 while (g >= toend && w < it->truncation_pixel_width)
18940 {
18941 w += g->pixel_width;
18942 --g;
18943 }
18944 if (to - g - tused > 0)
18945 to = g + tused;
18946 if (it->glyph_row->truncated_on_right_p
18947 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18948 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18949 {
18950 int extra = w - it->truncation_pixel_width;
18951
18952 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18953 }
18954 }
18955
18956 while (from >= end && to >= toend)
18957 *to-- = *from--;
18958 if (!FRAME_WINDOW_P (it->f))
18959 {
18960 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18961 {
18962 from =
18963 truncate_it.glyph_row->glyphs[TEXT_AREA]
18964 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18965 while (from >= end && to >= toend)
18966 *to-- = *from--;
18967 }
18968 }
18969 if (from >= end)
18970 {
18971 /* Need to free some room before prepending additional
18972 glyphs. */
18973 int move_by = from - end + 1;
18974 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18975 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18976
18977 for ( ; g >= g0; g--)
18978 g[move_by] = *g;
18979 while (from >= end)
18980 *to-- = *from--;
18981 it->glyph_row->used[TEXT_AREA] += move_by;
18982 }
18983 }
18984 }
18985
18986 /* Compute the hash code for ROW. */
18987 unsigned
18988 row_hash (struct glyph_row *row)
18989 {
18990 int area, k;
18991 unsigned hashval = 0;
18992
18993 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18994 for (k = 0; k < row->used[area]; ++k)
18995 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18996 + row->glyphs[area][k].u.val
18997 + row->glyphs[area][k].face_id
18998 + row->glyphs[area][k].padding_p
18999 + (row->glyphs[area][k].type << 2));
19000
19001 return hashval;
19002 }
19003
19004 /* Compute the pixel height and width of IT->glyph_row.
19005
19006 Most of the time, ascent and height of a display line will be equal
19007 to the max_ascent and max_height values of the display iterator
19008 structure. This is not the case if
19009
19010 1. We hit ZV without displaying anything. In this case, max_ascent
19011 and max_height will be zero.
19012
19013 2. We have some glyphs that don't contribute to the line height.
19014 (The glyph row flag contributes_to_line_height_p is for future
19015 pixmap extensions).
19016
19017 The first case is easily covered by using default values because in
19018 these cases, the line height does not really matter, except that it
19019 must not be zero. */
19020
19021 static void
19022 compute_line_metrics (struct it *it)
19023 {
19024 struct glyph_row *row = it->glyph_row;
19025
19026 if (FRAME_WINDOW_P (it->f))
19027 {
19028 int i, min_y, max_y;
19029
19030 /* The line may consist of one space only, that was added to
19031 place the cursor on it. If so, the row's height hasn't been
19032 computed yet. */
19033 if (row->height == 0)
19034 {
19035 if (it->max_ascent + it->max_descent == 0)
19036 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19037 row->ascent = it->max_ascent;
19038 row->height = it->max_ascent + it->max_descent;
19039 row->phys_ascent = it->max_phys_ascent;
19040 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19041 row->extra_line_spacing = it->max_extra_line_spacing;
19042 }
19043
19044 /* Compute the width of this line. */
19045 row->pixel_width = row->x;
19046 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19047 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19048
19049 eassert (row->pixel_width >= 0);
19050 eassert (row->ascent >= 0 && row->height > 0);
19051
19052 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19053 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19054
19055 /* If first line's physical ascent is larger than its logical
19056 ascent, use the physical ascent, and make the row taller.
19057 This makes accented characters fully visible. */
19058 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19059 && row->phys_ascent > row->ascent)
19060 {
19061 row->height += row->phys_ascent - row->ascent;
19062 row->ascent = row->phys_ascent;
19063 }
19064
19065 /* Compute how much of the line is visible. */
19066 row->visible_height = row->height;
19067
19068 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19069 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19070
19071 if (row->y < min_y)
19072 row->visible_height -= min_y - row->y;
19073 if (row->y + row->height > max_y)
19074 row->visible_height -= row->y + row->height - max_y;
19075 }
19076 else
19077 {
19078 row->pixel_width = row->used[TEXT_AREA];
19079 if (row->continued_p)
19080 row->pixel_width -= it->continuation_pixel_width;
19081 else if (row->truncated_on_right_p)
19082 row->pixel_width -= it->truncation_pixel_width;
19083 row->ascent = row->phys_ascent = 0;
19084 row->height = row->phys_height = row->visible_height = 1;
19085 row->extra_line_spacing = 0;
19086 }
19087
19088 /* Compute a hash code for this row. */
19089 row->hash = row_hash (row);
19090
19091 it->max_ascent = it->max_descent = 0;
19092 it->max_phys_ascent = it->max_phys_descent = 0;
19093 }
19094
19095
19096 /* Append one space to the glyph row of iterator IT if doing a
19097 window-based redisplay. The space has the same face as
19098 IT->face_id. Value is true if a space was added.
19099
19100 This function is called to make sure that there is always one glyph
19101 at the end of a glyph row that the cursor can be set on under
19102 window-systems. (If there weren't such a glyph we would not know
19103 how wide and tall a box cursor should be displayed).
19104
19105 At the same time this space let's a nicely handle clearing to the
19106 end of the line if the row ends in italic text. */
19107
19108 static bool
19109 append_space_for_newline (struct it *it, bool default_face_p)
19110 {
19111 if (FRAME_WINDOW_P (it->f))
19112 {
19113 int n = it->glyph_row->used[TEXT_AREA];
19114
19115 if (it->glyph_row->glyphs[TEXT_AREA] + n
19116 < it->glyph_row->glyphs[1 + TEXT_AREA])
19117 {
19118 /* Save some values that must not be changed.
19119 Must save IT->c and IT->len because otherwise
19120 ITERATOR_AT_END_P wouldn't work anymore after
19121 append_space_for_newline has been called. */
19122 enum display_element_type saved_what = it->what;
19123 int saved_c = it->c, saved_len = it->len;
19124 int saved_char_to_display = it->char_to_display;
19125 int saved_x = it->current_x;
19126 int saved_face_id = it->face_id;
19127 bool saved_box_end = it->end_of_box_run_p;
19128 struct text_pos saved_pos;
19129 Lisp_Object saved_object;
19130 struct face *face;
19131
19132 saved_object = it->object;
19133 saved_pos = it->position;
19134
19135 it->what = IT_CHARACTER;
19136 memset (&it->position, 0, sizeof it->position);
19137 it->object = Qnil;
19138 it->c = it->char_to_display = ' ';
19139 it->len = 1;
19140
19141 /* If the default face was remapped, be sure to use the
19142 remapped face for the appended newline. */
19143 if (default_face_p)
19144 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19145 else if (it->face_before_selective_p)
19146 it->face_id = it->saved_face_id;
19147 face = FACE_FROM_ID (it->f, it->face_id);
19148 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19149 /* In R2L rows, we will prepend a stretch glyph that will
19150 have the end_of_box_run_p flag set for it, so there's no
19151 need for the appended newline glyph to have that flag
19152 set. */
19153 if (it->glyph_row->reversed_p
19154 /* But if the appended newline glyph goes all the way to
19155 the end of the row, there will be no stretch glyph,
19156 so leave the box flag set. */
19157 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19158 it->end_of_box_run_p = false;
19159
19160 PRODUCE_GLYPHS (it);
19161
19162 it->override_ascent = -1;
19163 it->constrain_row_ascent_descent_p = false;
19164 it->current_x = saved_x;
19165 it->object = saved_object;
19166 it->position = saved_pos;
19167 it->what = saved_what;
19168 it->face_id = saved_face_id;
19169 it->len = saved_len;
19170 it->c = saved_c;
19171 it->char_to_display = saved_char_to_display;
19172 it->end_of_box_run_p = saved_box_end;
19173 return true;
19174 }
19175 }
19176
19177 return false;
19178 }
19179
19180
19181 /* Extend the face of the last glyph in the text area of IT->glyph_row
19182 to the end of the display line. Called from display_line. If the
19183 glyph row is empty, add a space glyph to it so that we know the
19184 face to draw. Set the glyph row flag fill_line_p. If the glyph
19185 row is R2L, prepend a stretch glyph to cover the empty space to the
19186 left of the leftmost glyph. */
19187
19188 static void
19189 extend_face_to_end_of_line (struct it *it)
19190 {
19191 struct face *face, *default_face;
19192 struct frame *f = it->f;
19193
19194 /* If line is already filled, do nothing. Non window-system frames
19195 get a grace of one more ``pixel'' because their characters are
19196 1-``pixel'' wide, so they hit the equality too early. This grace
19197 is needed only for R2L rows that are not continued, to produce
19198 one extra blank where we could display the cursor. */
19199 if ((it->current_x >= it->last_visible_x
19200 + (!FRAME_WINDOW_P (f)
19201 && it->glyph_row->reversed_p
19202 && !it->glyph_row->continued_p))
19203 /* If the window has display margins, we will need to extend
19204 their face even if the text area is filled. */
19205 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19206 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19207 return;
19208
19209 /* The default face, possibly remapped. */
19210 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19211
19212 /* Face extension extends the background and box of IT->face_id
19213 to the end of the line. If the background equals the background
19214 of the frame, we don't have to do anything. */
19215 if (it->face_before_selective_p)
19216 face = FACE_FROM_ID (f, it->saved_face_id);
19217 else
19218 face = FACE_FROM_ID (f, it->face_id);
19219
19220 if (FRAME_WINDOW_P (f)
19221 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19222 && face->box == FACE_NO_BOX
19223 && face->background == FRAME_BACKGROUND_PIXEL (f)
19224 #ifdef HAVE_WINDOW_SYSTEM
19225 && !face->stipple
19226 #endif
19227 && !it->glyph_row->reversed_p)
19228 return;
19229
19230 /* Set the glyph row flag indicating that the face of the last glyph
19231 in the text area has to be drawn to the end of the text area. */
19232 it->glyph_row->fill_line_p = true;
19233
19234 /* If current character of IT is not ASCII, make sure we have the
19235 ASCII face. This will be automatically undone the next time
19236 get_next_display_element returns a multibyte character. Note
19237 that the character will always be single byte in unibyte
19238 text. */
19239 if (!ASCII_CHAR_P (it->c))
19240 {
19241 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19242 }
19243
19244 if (FRAME_WINDOW_P (f))
19245 {
19246 /* If the row is empty, add a space with the current face of IT,
19247 so that we know which face to draw. */
19248 if (it->glyph_row->used[TEXT_AREA] == 0)
19249 {
19250 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19251 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19252 it->glyph_row->used[TEXT_AREA] = 1;
19253 }
19254 /* Mode line and the header line don't have margins, and
19255 likewise the frame's tool-bar window, if there is any. */
19256 if (!(it->glyph_row->mode_line_p
19257 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19258 || (WINDOWP (f->tool_bar_window)
19259 && it->w == XWINDOW (f->tool_bar_window))
19260 #endif
19261 ))
19262 {
19263 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19264 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19265 {
19266 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19267 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19268 default_face->id;
19269 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19270 }
19271 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19272 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19273 {
19274 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19275 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19276 default_face->id;
19277 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19278 }
19279 }
19280 #ifdef HAVE_WINDOW_SYSTEM
19281 if (it->glyph_row->reversed_p)
19282 {
19283 /* Prepend a stretch glyph to the row, such that the
19284 rightmost glyph will be drawn flushed all the way to the
19285 right margin of the window. The stretch glyph that will
19286 occupy the empty space, if any, to the left of the
19287 glyphs. */
19288 struct font *font = face->font ? face->font : FRAME_FONT (f);
19289 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19290 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19291 struct glyph *g;
19292 int row_width, stretch_ascent, stretch_width;
19293 struct text_pos saved_pos;
19294 int saved_face_id;
19295 bool saved_avoid_cursor, saved_box_start;
19296
19297 for (row_width = 0, g = row_start; g < row_end; g++)
19298 row_width += g->pixel_width;
19299
19300 /* FIXME: There are various minor display glitches in R2L
19301 rows when only one of the fringes is missing. The
19302 strange condition below produces the least bad effect. */
19303 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19304 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19305 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19306 stretch_width = window_box_width (it->w, TEXT_AREA);
19307 else
19308 stretch_width = it->last_visible_x - it->first_visible_x;
19309 stretch_width -= row_width;
19310
19311 if (stretch_width > 0)
19312 {
19313 stretch_ascent =
19314 (((it->ascent + it->descent)
19315 * FONT_BASE (font)) / FONT_HEIGHT (font));
19316 saved_pos = it->position;
19317 memset (&it->position, 0, sizeof it->position);
19318 saved_avoid_cursor = it->avoid_cursor_p;
19319 it->avoid_cursor_p = true;
19320 saved_face_id = it->face_id;
19321 saved_box_start = it->start_of_box_run_p;
19322 /* The last row's stretch glyph should get the default
19323 face, to avoid painting the rest of the window with
19324 the region face, if the region ends at ZV. */
19325 if (it->glyph_row->ends_at_zv_p)
19326 it->face_id = default_face->id;
19327 else
19328 it->face_id = face->id;
19329 it->start_of_box_run_p = false;
19330 append_stretch_glyph (it, Qnil, stretch_width,
19331 it->ascent + it->descent, stretch_ascent);
19332 it->position = saved_pos;
19333 it->avoid_cursor_p = saved_avoid_cursor;
19334 it->face_id = saved_face_id;
19335 it->start_of_box_run_p = saved_box_start;
19336 }
19337 /* If stretch_width comes out negative, it means that the
19338 last glyph is only partially visible. In R2L rows, we
19339 want the leftmost glyph to be partially visible, so we
19340 need to give the row the corresponding left offset. */
19341 if (stretch_width < 0)
19342 it->glyph_row->x = stretch_width;
19343 }
19344 #endif /* HAVE_WINDOW_SYSTEM */
19345 }
19346 else
19347 {
19348 /* Save some values that must not be changed. */
19349 int saved_x = it->current_x;
19350 struct text_pos saved_pos;
19351 Lisp_Object saved_object;
19352 enum display_element_type saved_what = it->what;
19353 int saved_face_id = it->face_id;
19354
19355 saved_object = it->object;
19356 saved_pos = it->position;
19357
19358 it->what = IT_CHARACTER;
19359 memset (&it->position, 0, sizeof it->position);
19360 it->object = Qnil;
19361 it->c = it->char_to_display = ' ';
19362 it->len = 1;
19363
19364 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19365 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19366 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19367 && !it->glyph_row->mode_line_p
19368 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19369 {
19370 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19371 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19372
19373 for (it->current_x = 0; g < e; g++)
19374 it->current_x += g->pixel_width;
19375
19376 it->area = LEFT_MARGIN_AREA;
19377 it->face_id = default_face->id;
19378 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19379 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19380 {
19381 PRODUCE_GLYPHS (it);
19382 /* term.c:produce_glyphs advances it->current_x only for
19383 TEXT_AREA. */
19384 it->current_x += it->pixel_width;
19385 }
19386
19387 it->current_x = saved_x;
19388 it->area = TEXT_AREA;
19389 }
19390
19391 /* The last row's blank glyphs should get the default face, to
19392 avoid painting the rest of the window with the region face,
19393 if the region ends at ZV. */
19394 if (it->glyph_row->ends_at_zv_p)
19395 it->face_id = default_face->id;
19396 else
19397 it->face_id = face->id;
19398 PRODUCE_GLYPHS (it);
19399
19400 while (it->current_x <= it->last_visible_x)
19401 PRODUCE_GLYPHS (it);
19402
19403 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19404 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19405 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19406 && !it->glyph_row->mode_line_p
19407 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19408 {
19409 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19410 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19411
19412 for ( ; g < e; g++)
19413 it->current_x += g->pixel_width;
19414
19415 it->area = RIGHT_MARGIN_AREA;
19416 it->face_id = default_face->id;
19417 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19418 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19419 {
19420 PRODUCE_GLYPHS (it);
19421 it->current_x += it->pixel_width;
19422 }
19423
19424 it->area = TEXT_AREA;
19425 }
19426
19427 /* Don't count these blanks really. It would let us insert a left
19428 truncation glyph below and make us set the cursor on them, maybe. */
19429 it->current_x = saved_x;
19430 it->object = saved_object;
19431 it->position = saved_pos;
19432 it->what = saved_what;
19433 it->face_id = saved_face_id;
19434 }
19435 }
19436
19437
19438 /* Value is true if text starting at CHARPOS in current_buffer is
19439 trailing whitespace. */
19440
19441 static bool
19442 trailing_whitespace_p (ptrdiff_t charpos)
19443 {
19444 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19445 int c = 0;
19446
19447 while (bytepos < ZV_BYTE
19448 && (c = FETCH_CHAR (bytepos),
19449 c == ' ' || c == '\t'))
19450 ++bytepos;
19451
19452 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19453 {
19454 if (bytepos != PT_BYTE)
19455 return true;
19456 }
19457 return false;
19458 }
19459
19460
19461 /* Highlight trailing whitespace, if any, in ROW. */
19462
19463 static void
19464 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19465 {
19466 int used = row->used[TEXT_AREA];
19467
19468 if (used)
19469 {
19470 struct glyph *start = row->glyphs[TEXT_AREA];
19471 struct glyph *glyph = start + used - 1;
19472
19473 if (row->reversed_p)
19474 {
19475 /* Right-to-left rows need to be processed in the opposite
19476 direction, so swap the edge pointers. */
19477 glyph = start;
19478 start = row->glyphs[TEXT_AREA] + used - 1;
19479 }
19480
19481 /* Skip over glyphs inserted to display the cursor at the
19482 end of a line, for extending the face of the last glyph
19483 to the end of the line on terminals, and for truncation
19484 and continuation glyphs. */
19485 if (!row->reversed_p)
19486 {
19487 while (glyph >= start
19488 && glyph->type == CHAR_GLYPH
19489 && NILP (glyph->object))
19490 --glyph;
19491 }
19492 else
19493 {
19494 while (glyph <= start
19495 && glyph->type == CHAR_GLYPH
19496 && NILP (glyph->object))
19497 ++glyph;
19498 }
19499
19500 /* If last glyph is a space or stretch, and it's trailing
19501 whitespace, set the face of all trailing whitespace glyphs in
19502 IT->glyph_row to `trailing-whitespace'. */
19503 if ((row->reversed_p ? glyph <= start : glyph >= start)
19504 && BUFFERP (glyph->object)
19505 && (glyph->type == STRETCH_GLYPH
19506 || (glyph->type == CHAR_GLYPH
19507 && glyph->u.ch == ' '))
19508 && trailing_whitespace_p (glyph->charpos))
19509 {
19510 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19511 if (face_id < 0)
19512 return;
19513
19514 if (!row->reversed_p)
19515 {
19516 while (glyph >= start
19517 && BUFFERP (glyph->object)
19518 && (glyph->type == STRETCH_GLYPH
19519 || (glyph->type == CHAR_GLYPH
19520 && glyph->u.ch == ' ')))
19521 (glyph--)->face_id = face_id;
19522 }
19523 else
19524 {
19525 while (glyph <= start
19526 && BUFFERP (glyph->object)
19527 && (glyph->type == STRETCH_GLYPH
19528 || (glyph->type == CHAR_GLYPH
19529 && glyph->u.ch == ' ')))
19530 (glyph++)->face_id = face_id;
19531 }
19532 }
19533 }
19534 }
19535
19536
19537 /* Value is true if glyph row ROW should be
19538 considered to hold the buffer position CHARPOS. */
19539
19540 static bool
19541 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19542 {
19543 bool result = true;
19544
19545 if (charpos == CHARPOS (row->end.pos)
19546 || charpos == MATRIX_ROW_END_CHARPOS (row))
19547 {
19548 /* Suppose the row ends on a string.
19549 Unless the row is continued, that means it ends on a newline
19550 in the string. If it's anything other than a display string
19551 (e.g., a before-string from an overlay), we don't want the
19552 cursor there. (This heuristic seems to give the optimal
19553 behavior for the various types of multi-line strings.)
19554 One exception: if the string has `cursor' property on one of
19555 its characters, we _do_ want the cursor there. */
19556 if (CHARPOS (row->end.string_pos) >= 0)
19557 {
19558 if (row->continued_p)
19559 result = true;
19560 else
19561 {
19562 /* Check for `display' property. */
19563 struct glyph *beg = row->glyphs[TEXT_AREA];
19564 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19565 struct glyph *glyph;
19566
19567 result = false;
19568 for (glyph = end; glyph >= beg; --glyph)
19569 if (STRINGP (glyph->object))
19570 {
19571 Lisp_Object prop
19572 = Fget_char_property (make_number (charpos),
19573 Qdisplay, Qnil);
19574 result =
19575 (!NILP (prop)
19576 && display_prop_string_p (prop, glyph->object));
19577 /* If there's a `cursor' property on one of the
19578 string's characters, this row is a cursor row,
19579 even though this is not a display string. */
19580 if (!result)
19581 {
19582 Lisp_Object s = glyph->object;
19583
19584 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19585 {
19586 ptrdiff_t gpos = glyph->charpos;
19587
19588 if (!NILP (Fget_char_property (make_number (gpos),
19589 Qcursor, s)))
19590 {
19591 result = true;
19592 break;
19593 }
19594 }
19595 }
19596 break;
19597 }
19598 }
19599 }
19600 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19601 {
19602 /* If the row ends in middle of a real character,
19603 and the line is continued, we want the cursor here.
19604 That's because CHARPOS (ROW->end.pos) would equal
19605 PT if PT is before the character. */
19606 if (!row->ends_in_ellipsis_p)
19607 result = row->continued_p;
19608 else
19609 /* If the row ends in an ellipsis, then
19610 CHARPOS (ROW->end.pos) will equal point after the
19611 invisible text. We want that position to be displayed
19612 after the ellipsis. */
19613 result = false;
19614 }
19615 /* If the row ends at ZV, display the cursor at the end of that
19616 row instead of at the start of the row below. */
19617 else
19618 result = row->ends_at_zv_p;
19619 }
19620
19621 return result;
19622 }
19623
19624 /* Value is true if glyph row ROW should be
19625 used to hold the cursor. */
19626
19627 static bool
19628 cursor_row_p (struct glyph_row *row)
19629 {
19630 return row_for_charpos_p (row, PT);
19631 }
19632
19633 \f
19634
19635 /* Push the property PROP so that it will be rendered at the current
19636 position in IT. Return true if PROP was successfully pushed, false
19637 otherwise. Called from handle_line_prefix to handle the
19638 `line-prefix' and `wrap-prefix' properties. */
19639
19640 static bool
19641 push_prefix_prop (struct it *it, Lisp_Object prop)
19642 {
19643 struct text_pos pos =
19644 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19645
19646 eassert (it->method == GET_FROM_BUFFER
19647 || it->method == GET_FROM_DISPLAY_VECTOR
19648 || it->method == GET_FROM_STRING);
19649
19650 /* We need to save the current buffer/string position, so it will be
19651 restored by pop_it, because iterate_out_of_display_property
19652 depends on that being set correctly, but some situations leave
19653 it->position not yet set when this function is called. */
19654 push_it (it, &pos);
19655
19656 if (STRINGP (prop))
19657 {
19658 if (SCHARS (prop) == 0)
19659 {
19660 pop_it (it);
19661 return false;
19662 }
19663
19664 it->string = prop;
19665 it->string_from_prefix_prop_p = true;
19666 it->multibyte_p = STRING_MULTIBYTE (it->string);
19667 it->current.overlay_string_index = -1;
19668 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19669 it->end_charpos = it->string_nchars = SCHARS (it->string);
19670 it->method = GET_FROM_STRING;
19671 it->stop_charpos = 0;
19672 it->prev_stop = 0;
19673 it->base_level_stop = 0;
19674
19675 /* Force paragraph direction to be that of the parent
19676 buffer/string. */
19677 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19678 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19679 else
19680 it->paragraph_embedding = L2R;
19681
19682 /* Set up the bidi iterator for this display string. */
19683 if (it->bidi_p)
19684 {
19685 it->bidi_it.string.lstring = it->string;
19686 it->bidi_it.string.s = NULL;
19687 it->bidi_it.string.schars = it->end_charpos;
19688 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19689 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19690 it->bidi_it.string.unibyte = !it->multibyte_p;
19691 it->bidi_it.w = it->w;
19692 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19693 }
19694 }
19695 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19696 {
19697 it->method = GET_FROM_STRETCH;
19698 it->object = prop;
19699 }
19700 #ifdef HAVE_WINDOW_SYSTEM
19701 else if (IMAGEP (prop))
19702 {
19703 it->what = IT_IMAGE;
19704 it->image_id = lookup_image (it->f, prop);
19705 it->method = GET_FROM_IMAGE;
19706 }
19707 #endif /* HAVE_WINDOW_SYSTEM */
19708 else
19709 {
19710 pop_it (it); /* bogus display property, give up */
19711 return false;
19712 }
19713
19714 return true;
19715 }
19716
19717 /* Return the character-property PROP at the current position in IT. */
19718
19719 static Lisp_Object
19720 get_it_property (struct it *it, Lisp_Object prop)
19721 {
19722 Lisp_Object position, object = it->object;
19723
19724 if (STRINGP (object))
19725 position = make_number (IT_STRING_CHARPOS (*it));
19726 else if (BUFFERP (object))
19727 {
19728 position = make_number (IT_CHARPOS (*it));
19729 object = it->window;
19730 }
19731 else
19732 return Qnil;
19733
19734 return Fget_char_property (position, prop, object);
19735 }
19736
19737 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19738
19739 static void
19740 handle_line_prefix (struct it *it)
19741 {
19742 Lisp_Object prefix;
19743
19744 if (it->continuation_lines_width > 0)
19745 {
19746 prefix = get_it_property (it, Qwrap_prefix);
19747 if (NILP (prefix))
19748 prefix = Vwrap_prefix;
19749 }
19750 else
19751 {
19752 prefix = get_it_property (it, Qline_prefix);
19753 if (NILP (prefix))
19754 prefix = Vline_prefix;
19755 }
19756 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19757 {
19758 /* If the prefix is wider than the window, and we try to wrap
19759 it, it would acquire its own wrap prefix, and so on till the
19760 iterator stack overflows. So, don't wrap the prefix. */
19761 it->line_wrap = TRUNCATE;
19762 it->avoid_cursor_p = true;
19763 }
19764 }
19765
19766 \f
19767
19768 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19769 only for R2L lines from display_line and display_string, when they
19770 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19771 the line/string needs to be continued on the next glyph row. */
19772 static void
19773 unproduce_glyphs (struct it *it, int n)
19774 {
19775 struct glyph *glyph, *end;
19776
19777 eassert (it->glyph_row);
19778 eassert (it->glyph_row->reversed_p);
19779 eassert (it->area == TEXT_AREA);
19780 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19781
19782 if (n > it->glyph_row->used[TEXT_AREA])
19783 n = it->glyph_row->used[TEXT_AREA];
19784 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19785 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19786 for ( ; glyph < end; glyph++)
19787 glyph[-n] = *glyph;
19788 }
19789
19790 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19791 and ROW->maxpos. */
19792 static void
19793 find_row_edges (struct it *it, struct glyph_row *row,
19794 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19795 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19796 {
19797 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19798 lines' rows is implemented for bidi-reordered rows. */
19799
19800 /* ROW->minpos is the value of min_pos, the minimal buffer position
19801 we have in ROW, or ROW->start.pos if that is smaller. */
19802 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19803 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19804 else
19805 /* We didn't find buffer positions smaller than ROW->start, or
19806 didn't find _any_ valid buffer positions in any of the glyphs,
19807 so we must trust the iterator's computed positions. */
19808 row->minpos = row->start.pos;
19809 if (max_pos <= 0)
19810 {
19811 max_pos = CHARPOS (it->current.pos);
19812 max_bpos = BYTEPOS (it->current.pos);
19813 }
19814
19815 /* Here are the various use-cases for ending the row, and the
19816 corresponding values for ROW->maxpos:
19817
19818 Line ends in a newline from buffer eol_pos + 1
19819 Line is continued from buffer max_pos + 1
19820 Line is truncated on right it->current.pos
19821 Line ends in a newline from string max_pos + 1(*)
19822 (*) + 1 only when line ends in a forward scan
19823 Line is continued from string max_pos
19824 Line is continued from display vector max_pos
19825 Line is entirely from a string min_pos == max_pos
19826 Line is entirely from a display vector min_pos == max_pos
19827 Line that ends at ZV ZV
19828
19829 If you discover other use-cases, please add them here as
19830 appropriate. */
19831 if (row->ends_at_zv_p)
19832 row->maxpos = it->current.pos;
19833 else if (row->used[TEXT_AREA])
19834 {
19835 bool seen_this_string = false;
19836 struct glyph_row *r1 = row - 1;
19837
19838 /* Did we see the same display string on the previous row? */
19839 if (STRINGP (it->object)
19840 /* this is not the first row */
19841 && row > it->w->desired_matrix->rows
19842 /* previous row is not the header line */
19843 && !r1->mode_line_p
19844 /* previous row also ends in a newline from a string */
19845 && r1->ends_in_newline_from_string_p)
19846 {
19847 struct glyph *start, *end;
19848
19849 /* Search for the last glyph of the previous row that came
19850 from buffer or string. Depending on whether the row is
19851 L2R or R2L, we need to process it front to back or the
19852 other way round. */
19853 if (!r1->reversed_p)
19854 {
19855 start = r1->glyphs[TEXT_AREA];
19856 end = start + r1->used[TEXT_AREA];
19857 /* Glyphs inserted by redisplay have nil as their object. */
19858 while (end > start
19859 && NILP ((end - 1)->object)
19860 && (end - 1)->charpos <= 0)
19861 --end;
19862 if (end > start)
19863 {
19864 if (EQ ((end - 1)->object, it->object))
19865 seen_this_string = true;
19866 }
19867 else
19868 /* If all the glyphs of the previous row were inserted
19869 by redisplay, it means the previous row was
19870 produced from a single newline, which is only
19871 possible if that newline came from the same string
19872 as the one which produced this ROW. */
19873 seen_this_string = true;
19874 }
19875 else
19876 {
19877 end = r1->glyphs[TEXT_AREA] - 1;
19878 start = end + r1->used[TEXT_AREA];
19879 while (end < start
19880 && NILP ((end + 1)->object)
19881 && (end + 1)->charpos <= 0)
19882 ++end;
19883 if (end < start)
19884 {
19885 if (EQ ((end + 1)->object, it->object))
19886 seen_this_string = true;
19887 }
19888 else
19889 seen_this_string = true;
19890 }
19891 }
19892 /* Take note of each display string that covers a newline only
19893 once, the first time we see it. This is for when a display
19894 string includes more than one newline in it. */
19895 if (row->ends_in_newline_from_string_p && !seen_this_string)
19896 {
19897 /* If we were scanning the buffer forward when we displayed
19898 the string, we want to account for at least one buffer
19899 position that belongs to this row (position covered by
19900 the display string), so that cursor positioning will
19901 consider this row as a candidate when point is at the end
19902 of the visual line represented by this row. This is not
19903 required when scanning back, because max_pos will already
19904 have a much larger value. */
19905 if (CHARPOS (row->end.pos) > max_pos)
19906 INC_BOTH (max_pos, max_bpos);
19907 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19908 }
19909 else if (CHARPOS (it->eol_pos) > 0)
19910 SET_TEXT_POS (row->maxpos,
19911 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19912 else if (row->continued_p)
19913 {
19914 /* If max_pos is different from IT's current position, it
19915 means IT->method does not belong to the display element
19916 at max_pos. However, it also means that the display
19917 element at max_pos was displayed in its entirety on this
19918 line, which is equivalent to saying that the next line
19919 starts at the next buffer position. */
19920 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19921 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19922 else
19923 {
19924 INC_BOTH (max_pos, max_bpos);
19925 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19926 }
19927 }
19928 else if (row->truncated_on_right_p)
19929 /* display_line already called reseat_at_next_visible_line_start,
19930 which puts the iterator at the beginning of the next line, in
19931 the logical order. */
19932 row->maxpos = it->current.pos;
19933 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19934 /* A line that is entirely from a string/image/stretch... */
19935 row->maxpos = row->minpos;
19936 else
19937 emacs_abort ();
19938 }
19939 else
19940 row->maxpos = it->current.pos;
19941 }
19942
19943 /* Construct the glyph row IT->glyph_row in the desired matrix of
19944 IT->w from text at the current position of IT. See dispextern.h
19945 for an overview of struct it. Value is true if
19946 IT->glyph_row displays text, as opposed to a line displaying ZV
19947 only. */
19948
19949 static bool
19950 display_line (struct it *it)
19951 {
19952 struct glyph_row *row = it->glyph_row;
19953 Lisp_Object overlay_arrow_string;
19954 struct it wrap_it;
19955 void *wrap_data = NULL;
19956 bool may_wrap = false;
19957 int wrap_x IF_LINT (= 0);
19958 int wrap_row_used = -1;
19959 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19960 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19961 int wrap_row_extra_line_spacing IF_LINT (= 0);
19962 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19963 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19964 int cvpos;
19965 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19966 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19967 bool pending_handle_line_prefix = false;
19968
19969 /* We always start displaying at hpos zero even if hscrolled. */
19970 eassert (it->hpos == 0 && it->current_x == 0);
19971
19972 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19973 >= it->w->desired_matrix->nrows)
19974 {
19975 it->w->nrows_scale_factor++;
19976 it->f->fonts_changed = true;
19977 return false;
19978 }
19979
19980 /* Clear the result glyph row and enable it. */
19981 prepare_desired_row (it->w, row, false);
19982
19983 row->y = it->current_y;
19984 row->start = it->start;
19985 row->continuation_lines_width = it->continuation_lines_width;
19986 row->displays_text_p = true;
19987 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19988 it->starts_in_middle_of_char_p = false;
19989
19990 /* Arrange the overlays nicely for our purposes. Usually, we call
19991 display_line on only one line at a time, in which case this
19992 can't really hurt too much, or we call it on lines which appear
19993 one after another in the buffer, in which case all calls to
19994 recenter_overlay_lists but the first will be pretty cheap. */
19995 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19996
19997 /* Move over display elements that are not visible because we are
19998 hscrolled. This may stop at an x-position < IT->first_visible_x
19999 if the first glyph is partially visible or if we hit a line end. */
20000 if (it->current_x < it->first_visible_x)
20001 {
20002 enum move_it_result move_result;
20003
20004 this_line_min_pos = row->start.pos;
20005 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20006 MOVE_TO_POS | MOVE_TO_X);
20007 /* If we are under a large hscroll, move_it_in_display_line_to
20008 could hit the end of the line without reaching
20009 it->first_visible_x. Pretend that we did reach it. This is
20010 especially important on a TTY, where we will call
20011 extend_face_to_end_of_line, which needs to know how many
20012 blank glyphs to produce. */
20013 if (it->current_x < it->first_visible_x
20014 && (move_result == MOVE_NEWLINE_OR_CR
20015 || move_result == MOVE_POS_MATCH_OR_ZV))
20016 it->current_x = it->first_visible_x;
20017
20018 /* Record the smallest positions seen while we moved over
20019 display elements that are not visible. This is needed by
20020 redisplay_internal for optimizing the case where the cursor
20021 stays inside the same line. The rest of this function only
20022 considers positions that are actually displayed, so
20023 RECORD_MAX_MIN_POS will not otherwise record positions that
20024 are hscrolled to the left of the left edge of the window. */
20025 min_pos = CHARPOS (this_line_min_pos);
20026 min_bpos = BYTEPOS (this_line_min_pos);
20027 }
20028 else if (it->area == TEXT_AREA)
20029 {
20030 /* We only do this when not calling move_it_in_display_line_to
20031 above, because that function calls itself handle_line_prefix. */
20032 handle_line_prefix (it);
20033 }
20034 else
20035 {
20036 /* Line-prefix and wrap-prefix are always displayed in the text
20037 area. But if this is the first call to display_line after
20038 init_iterator, the iterator might have been set up to write
20039 into a marginal area, e.g. if the line begins with some
20040 display property that writes to the margins. So we need to
20041 wait with the call to handle_line_prefix until whatever
20042 writes to the margin has done its job. */
20043 pending_handle_line_prefix = true;
20044 }
20045
20046 /* Get the initial row height. This is either the height of the
20047 text hscrolled, if there is any, or zero. */
20048 row->ascent = it->max_ascent;
20049 row->height = it->max_ascent + it->max_descent;
20050 row->phys_ascent = it->max_phys_ascent;
20051 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20052 row->extra_line_spacing = it->max_extra_line_spacing;
20053
20054 /* Utility macro to record max and min buffer positions seen until now. */
20055 #define RECORD_MAX_MIN_POS(IT) \
20056 do \
20057 { \
20058 bool composition_p \
20059 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20060 ptrdiff_t current_pos = \
20061 composition_p ? (IT)->cmp_it.charpos \
20062 : IT_CHARPOS (*(IT)); \
20063 ptrdiff_t current_bpos = \
20064 composition_p ? CHAR_TO_BYTE (current_pos) \
20065 : IT_BYTEPOS (*(IT)); \
20066 if (current_pos < min_pos) \
20067 { \
20068 min_pos = current_pos; \
20069 min_bpos = current_bpos; \
20070 } \
20071 if (IT_CHARPOS (*it) > max_pos) \
20072 { \
20073 max_pos = IT_CHARPOS (*it); \
20074 max_bpos = IT_BYTEPOS (*it); \
20075 } \
20076 } \
20077 while (false)
20078
20079 /* Loop generating characters. The loop is left with IT on the next
20080 character to display. */
20081 while (true)
20082 {
20083 int n_glyphs_before, hpos_before, x_before;
20084 int x, nglyphs;
20085 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20086
20087 /* Retrieve the next thing to display. Value is false if end of
20088 buffer reached. */
20089 if (!get_next_display_element (it))
20090 {
20091 /* Maybe add a space at the end of this line that is used to
20092 display the cursor there under X. Set the charpos of the
20093 first glyph of blank lines not corresponding to any text
20094 to -1. */
20095 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20096 row->exact_window_width_line_p = true;
20097 else if ((append_space_for_newline (it, true)
20098 && row->used[TEXT_AREA] == 1)
20099 || row->used[TEXT_AREA] == 0)
20100 {
20101 row->glyphs[TEXT_AREA]->charpos = -1;
20102 row->displays_text_p = false;
20103
20104 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20105 && (!MINI_WINDOW_P (it->w)
20106 || (minibuf_level && EQ (it->window, minibuf_window))))
20107 row->indicate_empty_line_p = true;
20108 }
20109
20110 it->continuation_lines_width = 0;
20111 row->ends_at_zv_p = true;
20112 /* A row that displays right-to-left text must always have
20113 its last face extended all the way to the end of line,
20114 even if this row ends in ZV, because we still write to
20115 the screen left to right. We also need to extend the
20116 last face if the default face is remapped to some
20117 different face, otherwise the functions that clear
20118 portions of the screen will clear with the default face's
20119 background color. */
20120 if (row->reversed_p
20121 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20122 extend_face_to_end_of_line (it);
20123 break;
20124 }
20125
20126 /* Now, get the metrics of what we want to display. This also
20127 generates glyphs in `row' (which is IT->glyph_row). */
20128 n_glyphs_before = row->used[TEXT_AREA];
20129 x = it->current_x;
20130
20131 /* Remember the line height so far in case the next element doesn't
20132 fit on the line. */
20133 if (it->line_wrap != TRUNCATE)
20134 {
20135 ascent = it->max_ascent;
20136 descent = it->max_descent;
20137 phys_ascent = it->max_phys_ascent;
20138 phys_descent = it->max_phys_descent;
20139
20140 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20141 {
20142 if (IT_DISPLAYING_WHITESPACE (it))
20143 may_wrap = true;
20144 else if (may_wrap)
20145 {
20146 SAVE_IT (wrap_it, *it, wrap_data);
20147 wrap_x = x;
20148 wrap_row_used = row->used[TEXT_AREA];
20149 wrap_row_ascent = row->ascent;
20150 wrap_row_height = row->height;
20151 wrap_row_phys_ascent = row->phys_ascent;
20152 wrap_row_phys_height = row->phys_height;
20153 wrap_row_extra_line_spacing = row->extra_line_spacing;
20154 wrap_row_min_pos = min_pos;
20155 wrap_row_min_bpos = min_bpos;
20156 wrap_row_max_pos = max_pos;
20157 wrap_row_max_bpos = max_bpos;
20158 may_wrap = false;
20159 }
20160 }
20161 }
20162
20163 PRODUCE_GLYPHS (it);
20164
20165 /* If this display element was in marginal areas, continue with
20166 the next one. */
20167 if (it->area != TEXT_AREA)
20168 {
20169 row->ascent = max (row->ascent, it->max_ascent);
20170 row->height = max (row->height, it->max_ascent + it->max_descent);
20171 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20172 row->phys_height = max (row->phys_height,
20173 it->max_phys_ascent + it->max_phys_descent);
20174 row->extra_line_spacing = max (row->extra_line_spacing,
20175 it->max_extra_line_spacing);
20176 set_iterator_to_next (it, true);
20177 /* If we didn't handle the line/wrap prefix above, and the
20178 call to set_iterator_to_next just switched to TEXT_AREA,
20179 process the prefix now. */
20180 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20181 {
20182 pending_handle_line_prefix = false;
20183 handle_line_prefix (it);
20184 }
20185 continue;
20186 }
20187
20188 /* Does the display element fit on the line? If we truncate
20189 lines, we should draw past the right edge of the window. If
20190 we don't truncate, we want to stop so that we can display the
20191 continuation glyph before the right margin. If lines are
20192 continued, there are two possible strategies for characters
20193 resulting in more than 1 glyph (e.g. tabs): Display as many
20194 glyphs as possible in this line and leave the rest for the
20195 continuation line, or display the whole element in the next
20196 line. Original redisplay did the former, so we do it also. */
20197 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20198 hpos_before = it->hpos;
20199 x_before = x;
20200
20201 if (/* Not a newline. */
20202 nglyphs > 0
20203 /* Glyphs produced fit entirely in the line. */
20204 && it->current_x < it->last_visible_x)
20205 {
20206 it->hpos += nglyphs;
20207 row->ascent = max (row->ascent, it->max_ascent);
20208 row->height = max (row->height, it->max_ascent + it->max_descent);
20209 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20210 row->phys_height = max (row->phys_height,
20211 it->max_phys_ascent + it->max_phys_descent);
20212 row->extra_line_spacing = max (row->extra_line_spacing,
20213 it->max_extra_line_spacing);
20214 if (it->current_x - it->pixel_width < it->first_visible_x
20215 /* In R2L rows, we arrange in extend_face_to_end_of_line
20216 to add a right offset to the line, by a suitable
20217 change to the stretch glyph that is the leftmost
20218 glyph of the line. */
20219 && !row->reversed_p)
20220 row->x = x - it->first_visible_x;
20221 /* Record the maximum and minimum buffer positions seen so
20222 far in glyphs that will be displayed by this row. */
20223 if (it->bidi_p)
20224 RECORD_MAX_MIN_POS (it);
20225 }
20226 else
20227 {
20228 int i, new_x;
20229 struct glyph *glyph;
20230
20231 for (i = 0; i < nglyphs; ++i, x = new_x)
20232 {
20233 /* Identify the glyphs added by the last call to
20234 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20235 the previous glyphs. */
20236 if (!row->reversed_p)
20237 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20238 else
20239 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20240 new_x = x + glyph->pixel_width;
20241
20242 if (/* Lines are continued. */
20243 it->line_wrap != TRUNCATE
20244 && (/* Glyph doesn't fit on the line. */
20245 new_x > it->last_visible_x
20246 /* Or it fits exactly on a window system frame. */
20247 || (new_x == it->last_visible_x
20248 && FRAME_WINDOW_P (it->f)
20249 && (row->reversed_p
20250 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20251 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20252 {
20253 /* End of a continued line. */
20254
20255 if (it->hpos == 0
20256 || (new_x == it->last_visible_x
20257 && FRAME_WINDOW_P (it->f)
20258 && (row->reversed_p
20259 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20260 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20261 {
20262 /* Current glyph is the only one on the line or
20263 fits exactly on the line. We must continue
20264 the line because we can't draw the cursor
20265 after the glyph. */
20266 row->continued_p = true;
20267 it->current_x = new_x;
20268 it->continuation_lines_width += new_x;
20269 ++it->hpos;
20270 if (i == nglyphs - 1)
20271 {
20272 /* If line-wrap is on, check if a previous
20273 wrap point was found. */
20274 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20275 && wrap_row_used > 0
20276 /* Even if there is a previous wrap
20277 point, continue the line here as
20278 usual, if (i) the previous character
20279 was a space or tab AND (ii) the
20280 current character is not. */
20281 && (!may_wrap
20282 || IT_DISPLAYING_WHITESPACE (it)))
20283 goto back_to_wrap;
20284
20285 /* Record the maximum and minimum buffer
20286 positions seen so far in glyphs that will be
20287 displayed by this row. */
20288 if (it->bidi_p)
20289 RECORD_MAX_MIN_POS (it);
20290 set_iterator_to_next (it, true);
20291 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20292 {
20293 if (!get_next_display_element (it))
20294 {
20295 row->exact_window_width_line_p = true;
20296 it->continuation_lines_width = 0;
20297 row->continued_p = false;
20298 row->ends_at_zv_p = true;
20299 }
20300 else if (ITERATOR_AT_END_OF_LINE_P (it))
20301 {
20302 row->continued_p = false;
20303 row->exact_window_width_line_p = true;
20304 }
20305 /* If line-wrap is on, check if a
20306 previous wrap point was found. */
20307 else if (wrap_row_used > 0
20308 /* Even if there is a previous wrap
20309 point, continue the line here as
20310 usual, if (i) the previous character
20311 was a space or tab AND (ii) the
20312 current character is not. */
20313 && (!may_wrap
20314 || IT_DISPLAYING_WHITESPACE (it)))
20315 goto back_to_wrap;
20316
20317 }
20318 }
20319 else if (it->bidi_p)
20320 RECORD_MAX_MIN_POS (it);
20321 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20322 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20323 extend_face_to_end_of_line (it);
20324 }
20325 else if (CHAR_GLYPH_PADDING_P (*glyph)
20326 && !FRAME_WINDOW_P (it->f))
20327 {
20328 /* A padding glyph that doesn't fit on this line.
20329 This means the whole character doesn't fit
20330 on the line. */
20331 if (row->reversed_p)
20332 unproduce_glyphs (it, row->used[TEXT_AREA]
20333 - n_glyphs_before);
20334 row->used[TEXT_AREA] = n_glyphs_before;
20335
20336 /* Fill the rest of the row with continuation
20337 glyphs like in 20.x. */
20338 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20339 < row->glyphs[1 + TEXT_AREA])
20340 produce_special_glyphs (it, IT_CONTINUATION);
20341
20342 row->continued_p = true;
20343 it->current_x = x_before;
20344 it->continuation_lines_width += x_before;
20345
20346 /* Restore the height to what it was before the
20347 element not fitting on the line. */
20348 it->max_ascent = ascent;
20349 it->max_descent = descent;
20350 it->max_phys_ascent = phys_ascent;
20351 it->max_phys_descent = phys_descent;
20352 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20353 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20354 extend_face_to_end_of_line (it);
20355 }
20356 else if (wrap_row_used > 0)
20357 {
20358 back_to_wrap:
20359 if (row->reversed_p)
20360 unproduce_glyphs (it,
20361 row->used[TEXT_AREA] - wrap_row_used);
20362 RESTORE_IT (it, &wrap_it, wrap_data);
20363 it->continuation_lines_width += wrap_x;
20364 row->used[TEXT_AREA] = wrap_row_used;
20365 row->ascent = wrap_row_ascent;
20366 row->height = wrap_row_height;
20367 row->phys_ascent = wrap_row_phys_ascent;
20368 row->phys_height = wrap_row_phys_height;
20369 row->extra_line_spacing = wrap_row_extra_line_spacing;
20370 min_pos = wrap_row_min_pos;
20371 min_bpos = wrap_row_min_bpos;
20372 max_pos = wrap_row_max_pos;
20373 max_bpos = wrap_row_max_bpos;
20374 row->continued_p = true;
20375 row->ends_at_zv_p = false;
20376 row->exact_window_width_line_p = false;
20377 it->continuation_lines_width += x;
20378
20379 /* Make sure that a non-default face is extended
20380 up to the right margin of the window. */
20381 extend_face_to_end_of_line (it);
20382 }
20383 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20384 {
20385 /* A TAB that extends past the right edge of the
20386 window. This produces a single glyph on
20387 window system frames. We leave the glyph in
20388 this row and let it fill the row, but don't
20389 consume the TAB. */
20390 if ((row->reversed_p
20391 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20392 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20393 produce_special_glyphs (it, IT_CONTINUATION);
20394 it->continuation_lines_width += it->last_visible_x;
20395 row->ends_in_middle_of_char_p = true;
20396 row->continued_p = true;
20397 glyph->pixel_width = it->last_visible_x - x;
20398 it->starts_in_middle_of_char_p = true;
20399 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20400 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20401 extend_face_to_end_of_line (it);
20402 }
20403 else
20404 {
20405 /* Something other than a TAB that draws past
20406 the right edge of the window. Restore
20407 positions to values before the element. */
20408 if (row->reversed_p)
20409 unproduce_glyphs (it, row->used[TEXT_AREA]
20410 - (n_glyphs_before + i));
20411 row->used[TEXT_AREA] = n_glyphs_before + i;
20412
20413 /* Display continuation glyphs. */
20414 it->current_x = x_before;
20415 it->continuation_lines_width += x;
20416 if (!FRAME_WINDOW_P (it->f)
20417 || (row->reversed_p
20418 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20419 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20420 produce_special_glyphs (it, IT_CONTINUATION);
20421 row->continued_p = true;
20422
20423 extend_face_to_end_of_line (it);
20424
20425 if (nglyphs > 1 && i > 0)
20426 {
20427 row->ends_in_middle_of_char_p = true;
20428 it->starts_in_middle_of_char_p = true;
20429 }
20430
20431 /* Restore the height to what it was before the
20432 element not fitting on the line. */
20433 it->max_ascent = ascent;
20434 it->max_descent = descent;
20435 it->max_phys_ascent = phys_ascent;
20436 it->max_phys_descent = phys_descent;
20437 }
20438
20439 break;
20440 }
20441 else if (new_x > it->first_visible_x)
20442 {
20443 /* Increment number of glyphs actually displayed. */
20444 ++it->hpos;
20445
20446 /* Record the maximum and minimum buffer positions
20447 seen so far in glyphs that will be displayed by
20448 this row. */
20449 if (it->bidi_p)
20450 RECORD_MAX_MIN_POS (it);
20451
20452 if (x < it->first_visible_x && !row->reversed_p)
20453 /* Glyph is partially visible, i.e. row starts at
20454 negative X position. Don't do that in R2L
20455 rows, where we arrange to add a right offset to
20456 the line in extend_face_to_end_of_line, by a
20457 suitable change to the stretch glyph that is
20458 the leftmost glyph of the line. */
20459 row->x = x - it->first_visible_x;
20460 /* When the last glyph of an R2L row only fits
20461 partially on the line, we need to set row->x to a
20462 negative offset, so that the leftmost glyph is
20463 the one that is partially visible. But if we are
20464 going to produce the truncation glyph, this will
20465 be taken care of in produce_special_glyphs. */
20466 if (row->reversed_p
20467 && new_x > it->last_visible_x
20468 && !(it->line_wrap == TRUNCATE
20469 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20470 {
20471 eassert (FRAME_WINDOW_P (it->f));
20472 row->x = it->last_visible_x - new_x;
20473 }
20474 }
20475 else
20476 {
20477 /* Glyph is completely off the left margin of the
20478 window. This should not happen because of the
20479 move_it_in_display_line at the start of this
20480 function, unless the text display area of the
20481 window is empty. */
20482 eassert (it->first_visible_x <= it->last_visible_x);
20483 }
20484 }
20485 /* Even if this display element produced no glyphs at all,
20486 we want to record its position. */
20487 if (it->bidi_p && nglyphs == 0)
20488 RECORD_MAX_MIN_POS (it);
20489
20490 row->ascent = max (row->ascent, it->max_ascent);
20491 row->height = max (row->height, it->max_ascent + it->max_descent);
20492 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20493 row->phys_height = max (row->phys_height,
20494 it->max_phys_ascent + it->max_phys_descent);
20495 row->extra_line_spacing = max (row->extra_line_spacing,
20496 it->max_extra_line_spacing);
20497
20498 /* End of this display line if row is continued. */
20499 if (row->continued_p || row->ends_at_zv_p)
20500 break;
20501 }
20502
20503 at_end_of_line:
20504 /* Is this a line end? If yes, we're also done, after making
20505 sure that a non-default face is extended up to the right
20506 margin of the window. */
20507 if (ITERATOR_AT_END_OF_LINE_P (it))
20508 {
20509 int used_before = row->used[TEXT_AREA];
20510
20511 row->ends_in_newline_from_string_p = STRINGP (it->object);
20512
20513 /* Add a space at the end of the line that is used to
20514 display the cursor there. */
20515 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20516 append_space_for_newline (it, false);
20517
20518 /* Extend the face to the end of the line. */
20519 extend_face_to_end_of_line (it);
20520
20521 /* Make sure we have the position. */
20522 if (used_before == 0)
20523 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20524
20525 /* Record the position of the newline, for use in
20526 find_row_edges. */
20527 it->eol_pos = it->current.pos;
20528
20529 /* Consume the line end. This skips over invisible lines. */
20530 set_iterator_to_next (it, true);
20531 it->continuation_lines_width = 0;
20532 break;
20533 }
20534
20535 /* Proceed with next display element. Note that this skips
20536 over lines invisible because of selective display. */
20537 set_iterator_to_next (it, true);
20538
20539 /* If we truncate lines, we are done when the last displayed
20540 glyphs reach past the right margin of the window. */
20541 if (it->line_wrap == TRUNCATE
20542 && ((FRAME_WINDOW_P (it->f)
20543 /* Images are preprocessed in produce_image_glyph such
20544 that they are cropped at the right edge of the
20545 window, so an image glyph will always end exactly at
20546 last_visible_x, even if there's no right fringe. */
20547 && ((row->reversed_p
20548 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20549 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20550 || it->what == IT_IMAGE))
20551 ? (it->current_x >= it->last_visible_x)
20552 : (it->current_x > it->last_visible_x)))
20553 {
20554 /* Maybe add truncation glyphs. */
20555 if (!FRAME_WINDOW_P (it->f)
20556 || (row->reversed_p
20557 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20558 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20559 {
20560 int i, n;
20561
20562 if (!row->reversed_p)
20563 {
20564 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20565 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20566 break;
20567 }
20568 else
20569 {
20570 for (i = 0; i < row->used[TEXT_AREA]; i++)
20571 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20572 break;
20573 /* Remove any padding glyphs at the front of ROW, to
20574 make room for the truncation glyphs we will be
20575 adding below. The loop below always inserts at
20576 least one truncation glyph, so also remove the
20577 last glyph added to ROW. */
20578 unproduce_glyphs (it, i + 1);
20579 /* Adjust i for the loop below. */
20580 i = row->used[TEXT_AREA] - (i + 1);
20581 }
20582
20583 /* produce_special_glyphs overwrites the last glyph, so
20584 we don't want that if we want to keep that last
20585 glyph, which means it's an image. */
20586 if (it->current_x > it->last_visible_x)
20587 {
20588 it->current_x = x_before;
20589 if (!FRAME_WINDOW_P (it->f))
20590 {
20591 for (n = row->used[TEXT_AREA]; i < n; ++i)
20592 {
20593 row->used[TEXT_AREA] = i;
20594 produce_special_glyphs (it, IT_TRUNCATION);
20595 }
20596 }
20597 else
20598 {
20599 row->used[TEXT_AREA] = i;
20600 produce_special_glyphs (it, IT_TRUNCATION);
20601 }
20602 it->hpos = hpos_before;
20603 }
20604 }
20605 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20606 {
20607 /* Don't truncate if we can overflow newline into fringe. */
20608 if (!get_next_display_element (it))
20609 {
20610 it->continuation_lines_width = 0;
20611 row->ends_at_zv_p = true;
20612 row->exact_window_width_line_p = true;
20613 break;
20614 }
20615 if (ITERATOR_AT_END_OF_LINE_P (it))
20616 {
20617 row->exact_window_width_line_p = true;
20618 goto at_end_of_line;
20619 }
20620 it->current_x = x_before;
20621 it->hpos = hpos_before;
20622 }
20623
20624 row->truncated_on_right_p = true;
20625 it->continuation_lines_width = 0;
20626 reseat_at_next_visible_line_start (it, false);
20627 /* We insist below that IT's position be at ZV because in
20628 bidi-reordered lines the character at visible line start
20629 might not be the character that follows the newline in
20630 the logical order. */
20631 if (IT_BYTEPOS (*it) > BEG_BYTE)
20632 row->ends_at_zv_p =
20633 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20634 else
20635 row->ends_at_zv_p = false;
20636 break;
20637 }
20638 }
20639
20640 if (wrap_data)
20641 bidi_unshelve_cache (wrap_data, true);
20642
20643 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20644 at the left window margin. */
20645 if (it->first_visible_x
20646 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20647 {
20648 if (!FRAME_WINDOW_P (it->f)
20649 || (((row->reversed_p
20650 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20651 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20652 /* Don't let insert_left_trunc_glyphs overwrite the
20653 first glyph of the row if it is an image. */
20654 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20655 insert_left_trunc_glyphs (it);
20656 row->truncated_on_left_p = true;
20657 }
20658
20659 /* Remember the position at which this line ends.
20660
20661 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20662 cannot be before the call to find_row_edges below, since that is
20663 where these positions are determined. */
20664 row->end = it->current;
20665 if (!it->bidi_p)
20666 {
20667 row->minpos = row->start.pos;
20668 row->maxpos = row->end.pos;
20669 }
20670 else
20671 {
20672 /* ROW->minpos and ROW->maxpos must be the smallest and
20673 `1 + the largest' buffer positions in ROW. But if ROW was
20674 bidi-reordered, these two positions can be anywhere in the
20675 row, so we must determine them now. */
20676 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20677 }
20678
20679 /* If the start of this line is the overlay arrow-position, then
20680 mark this glyph row as the one containing the overlay arrow.
20681 This is clearly a mess with variable size fonts. It would be
20682 better to let it be displayed like cursors under X. */
20683 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20684 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20685 !NILP (overlay_arrow_string)))
20686 {
20687 /* Overlay arrow in window redisplay is a fringe bitmap. */
20688 if (STRINGP (overlay_arrow_string))
20689 {
20690 struct glyph_row *arrow_row
20691 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20692 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20693 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20694 struct glyph *p = row->glyphs[TEXT_AREA];
20695 struct glyph *p2, *end;
20696
20697 /* Copy the arrow glyphs. */
20698 while (glyph < arrow_end)
20699 *p++ = *glyph++;
20700
20701 /* Throw away padding glyphs. */
20702 p2 = p;
20703 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20704 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20705 ++p2;
20706 if (p2 > p)
20707 {
20708 while (p2 < end)
20709 *p++ = *p2++;
20710 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20711 }
20712 }
20713 else
20714 {
20715 eassert (INTEGERP (overlay_arrow_string));
20716 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20717 }
20718 overlay_arrow_seen = true;
20719 }
20720
20721 /* Highlight trailing whitespace. */
20722 if (!NILP (Vshow_trailing_whitespace))
20723 highlight_trailing_whitespace (it->f, it->glyph_row);
20724
20725 /* Compute pixel dimensions of this line. */
20726 compute_line_metrics (it);
20727
20728 /* Implementation note: No changes in the glyphs of ROW or in their
20729 faces can be done past this point, because compute_line_metrics
20730 computes ROW's hash value and stores it within the glyph_row
20731 structure. */
20732
20733 /* Record whether this row ends inside an ellipsis. */
20734 row->ends_in_ellipsis_p
20735 = (it->method == GET_FROM_DISPLAY_VECTOR
20736 && it->ellipsis_p);
20737
20738 /* Save fringe bitmaps in this row. */
20739 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20740 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20741 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20742 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20743
20744 it->left_user_fringe_bitmap = 0;
20745 it->left_user_fringe_face_id = 0;
20746 it->right_user_fringe_bitmap = 0;
20747 it->right_user_fringe_face_id = 0;
20748
20749 /* Maybe set the cursor. */
20750 cvpos = it->w->cursor.vpos;
20751 if ((cvpos < 0
20752 /* In bidi-reordered rows, keep checking for proper cursor
20753 position even if one has been found already, because buffer
20754 positions in such rows change non-linearly with ROW->VPOS,
20755 when a line is continued. One exception: when we are at ZV,
20756 display cursor on the first suitable glyph row, since all
20757 the empty rows after that also have their position set to ZV. */
20758 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20759 lines' rows is implemented for bidi-reordered rows. */
20760 || (it->bidi_p
20761 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20762 && PT >= MATRIX_ROW_START_CHARPOS (row)
20763 && PT <= MATRIX_ROW_END_CHARPOS (row)
20764 && cursor_row_p (row))
20765 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20766
20767 /* Prepare for the next line. This line starts horizontally at (X
20768 HPOS) = (0 0). Vertical positions are incremented. As a
20769 convenience for the caller, IT->glyph_row is set to the next
20770 row to be used. */
20771 it->current_x = it->hpos = 0;
20772 it->current_y += row->height;
20773 SET_TEXT_POS (it->eol_pos, 0, 0);
20774 ++it->vpos;
20775 ++it->glyph_row;
20776 /* The next row should by default use the same value of the
20777 reversed_p flag as this one. set_iterator_to_next decides when
20778 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20779 the flag accordingly. */
20780 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20781 it->glyph_row->reversed_p = row->reversed_p;
20782 it->start = row->end;
20783 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20784
20785 #undef RECORD_MAX_MIN_POS
20786 }
20787
20788 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20789 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20790 doc: /* Return paragraph direction at point in BUFFER.
20791 Value is either `left-to-right' or `right-to-left'.
20792 If BUFFER is omitted or nil, it defaults to the current buffer.
20793
20794 Paragraph direction determines how the text in the paragraph is displayed.
20795 In left-to-right paragraphs, text begins at the left margin of the window
20796 and the reading direction is generally left to right. In right-to-left
20797 paragraphs, text begins at the right margin and is read from right to left.
20798
20799 See also `bidi-paragraph-direction'. */)
20800 (Lisp_Object buffer)
20801 {
20802 struct buffer *buf = current_buffer;
20803 struct buffer *old = buf;
20804
20805 if (! NILP (buffer))
20806 {
20807 CHECK_BUFFER (buffer);
20808 buf = XBUFFER (buffer);
20809 }
20810
20811 if (NILP (BVAR (buf, bidi_display_reordering))
20812 || NILP (BVAR (buf, enable_multibyte_characters))
20813 /* When we are loading loadup.el, the character property tables
20814 needed for bidi iteration are not yet available. */
20815 || !NILP (Vpurify_flag))
20816 return Qleft_to_right;
20817 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20818 return BVAR (buf, bidi_paragraph_direction);
20819 else
20820 {
20821 /* Determine the direction from buffer text. We could try to
20822 use current_matrix if it is up to date, but this seems fast
20823 enough as it is. */
20824 struct bidi_it itb;
20825 ptrdiff_t pos = BUF_PT (buf);
20826 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20827 int c;
20828 void *itb_data = bidi_shelve_cache ();
20829
20830 set_buffer_temp (buf);
20831 /* bidi_paragraph_init finds the base direction of the paragraph
20832 by searching forward from paragraph start. We need the base
20833 direction of the current or _previous_ paragraph, so we need
20834 to make sure we are within that paragraph. To that end, find
20835 the previous non-empty line. */
20836 if (pos >= ZV && pos > BEGV)
20837 DEC_BOTH (pos, bytepos);
20838 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
20839 if (fast_looking_at (trailing_white_space,
20840 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20841 {
20842 while ((c = FETCH_BYTE (bytepos)) == '\n'
20843 || c == ' ' || c == '\t' || c == '\f')
20844 {
20845 if (bytepos <= BEGV_BYTE)
20846 break;
20847 bytepos--;
20848 pos--;
20849 }
20850 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20851 bytepos--;
20852 }
20853 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20854 itb.paragraph_dir = NEUTRAL_DIR;
20855 itb.string.s = NULL;
20856 itb.string.lstring = Qnil;
20857 itb.string.bufpos = 0;
20858 itb.string.from_disp_str = false;
20859 itb.string.unibyte = false;
20860 /* We have no window to use here for ignoring window-specific
20861 overlays. Using NULL for window pointer will cause
20862 compute_display_string_pos to use the current buffer. */
20863 itb.w = NULL;
20864 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
20865 bidi_unshelve_cache (itb_data, false);
20866 set_buffer_temp (old);
20867 switch (itb.paragraph_dir)
20868 {
20869 case L2R:
20870 return Qleft_to_right;
20871 break;
20872 case R2L:
20873 return Qright_to_left;
20874 break;
20875 default:
20876 emacs_abort ();
20877 }
20878 }
20879 }
20880
20881 DEFUN ("bidi-find-overridden-directionality",
20882 Fbidi_find_overridden_directionality,
20883 Sbidi_find_overridden_directionality, 2, 3, 0,
20884 doc: /* Return position between FROM and TO where directionality was overridden.
20885
20886 This function returns the first character position in the specified
20887 region of OBJECT where there is a character whose `bidi-class' property
20888 is `L', but which was forced to display as `R' by a directional
20889 override, and likewise with characters whose `bidi-class' is `R'
20890 or `AL' that were forced to display as `L'.
20891
20892 If no such character is found, the function returns nil.
20893
20894 OBJECT is a Lisp string or buffer to search for overridden
20895 directionality, and defaults to the current buffer if nil or omitted.
20896 OBJECT can also be a window, in which case the function will search
20897 the buffer displayed in that window. Passing the window instead of
20898 a buffer is preferable when the buffer is displayed in some window,
20899 because this function will then be able to correctly account for
20900 window-specific overlays, which can affect the results.
20901
20902 Strong directional characters `L', `R', and `AL' can have their
20903 intrinsic directionality overridden by directional override
20904 control characters RLO \(u+202e) and LRO \(u+202d). See the
20905 function `get-char-code-property' for a way to inquire about
20906 the `bidi-class' property of a character. */)
20907 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
20908 {
20909 struct buffer *buf = current_buffer;
20910 struct buffer *old = buf;
20911 struct window *w = NULL;
20912 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
20913 struct bidi_it itb;
20914 ptrdiff_t from_pos, to_pos, from_bpos;
20915 void *itb_data;
20916
20917 if (!NILP (object))
20918 {
20919 if (BUFFERP (object))
20920 buf = XBUFFER (object);
20921 else if (WINDOWP (object))
20922 {
20923 w = decode_live_window (object);
20924 buf = XBUFFER (w->contents);
20925 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
20926 }
20927 else
20928 CHECK_STRING (object);
20929 }
20930
20931 if (STRINGP (object))
20932 {
20933 /* Characters in unibyte strings are always treated by bidi.c as
20934 strong LTR. */
20935 if (!STRING_MULTIBYTE (object)
20936 /* When we are loading loadup.el, the character property
20937 tables needed for bidi iteration are not yet
20938 available. */
20939 || !NILP (Vpurify_flag))
20940 return Qnil;
20941
20942 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
20943 if (from_pos >= SCHARS (object))
20944 return Qnil;
20945
20946 /* Set up the bidi iterator. */
20947 itb_data = bidi_shelve_cache ();
20948 itb.paragraph_dir = NEUTRAL_DIR;
20949 itb.string.lstring = object;
20950 itb.string.s = NULL;
20951 itb.string.schars = SCHARS (object);
20952 itb.string.bufpos = 0;
20953 itb.string.from_disp_str = false;
20954 itb.string.unibyte = false;
20955 itb.w = w;
20956 bidi_init_it (0, 0, frame_window_p, &itb);
20957 }
20958 else
20959 {
20960 /* Nothing this fancy can happen in unibyte buffers, or in a
20961 buffer that disabled reordering, or if FROM is at EOB. */
20962 if (NILP (BVAR (buf, bidi_display_reordering))
20963 || NILP (BVAR (buf, enable_multibyte_characters))
20964 /* When we are loading loadup.el, the character property
20965 tables needed for bidi iteration are not yet
20966 available. */
20967 || !NILP (Vpurify_flag))
20968 return Qnil;
20969
20970 set_buffer_temp (buf);
20971 validate_region (&from, &to);
20972 from_pos = XINT (from);
20973 to_pos = XINT (to);
20974 if (from_pos >= ZV)
20975 return Qnil;
20976
20977 /* Set up the bidi iterator. */
20978 itb_data = bidi_shelve_cache ();
20979 from_bpos = CHAR_TO_BYTE (from_pos);
20980 if (from_pos == BEGV)
20981 {
20982 itb.charpos = BEGV;
20983 itb.bytepos = BEGV_BYTE;
20984 }
20985 else if (FETCH_CHAR (from_bpos - 1) == '\n')
20986 {
20987 itb.charpos = from_pos;
20988 itb.bytepos = from_bpos;
20989 }
20990 else
20991 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
20992 -1, &itb.bytepos);
20993 itb.paragraph_dir = NEUTRAL_DIR;
20994 itb.string.s = NULL;
20995 itb.string.lstring = Qnil;
20996 itb.string.bufpos = 0;
20997 itb.string.from_disp_str = false;
20998 itb.string.unibyte = false;
20999 itb.w = w;
21000 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21001 }
21002
21003 ptrdiff_t found;
21004 do {
21005 /* For the purposes of this function, the actual base direction of
21006 the paragraph doesn't matter, so just set it to L2R. */
21007 bidi_paragraph_init (L2R, &itb, false);
21008 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21009 ;
21010 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21011
21012 bidi_unshelve_cache (itb_data, false);
21013 set_buffer_temp (old);
21014
21015 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21016 }
21017
21018 DEFUN ("move-point-visually", Fmove_point_visually,
21019 Smove_point_visually, 1, 1, 0,
21020 doc: /* Move point in the visual order in the specified DIRECTION.
21021 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21022 left.
21023
21024 Value is the new character position of point. */)
21025 (Lisp_Object direction)
21026 {
21027 struct window *w = XWINDOW (selected_window);
21028 struct buffer *b = XBUFFER (w->contents);
21029 struct glyph_row *row;
21030 int dir;
21031 Lisp_Object paragraph_dir;
21032
21033 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21034 (!(ROW)->continued_p \
21035 && NILP ((GLYPH)->object) \
21036 && (GLYPH)->type == CHAR_GLYPH \
21037 && (GLYPH)->u.ch == ' ' \
21038 && (GLYPH)->charpos >= 0 \
21039 && !(GLYPH)->avoid_cursor_p)
21040
21041 CHECK_NUMBER (direction);
21042 dir = XINT (direction);
21043 if (dir > 0)
21044 dir = 1;
21045 else
21046 dir = -1;
21047
21048 /* If current matrix is up-to-date, we can use the information
21049 recorded in the glyphs, at least as long as the goal is on the
21050 screen. */
21051 if (w->window_end_valid
21052 && !windows_or_buffers_changed
21053 && b
21054 && !b->clip_changed
21055 && !b->prevent_redisplay_optimizations_p
21056 && !window_outdated (w)
21057 /* We rely below on the cursor coordinates to be up to date, but
21058 we cannot trust them if some command moved point since the
21059 last complete redisplay. */
21060 && w->last_point == BUF_PT (b)
21061 && w->cursor.vpos >= 0
21062 && w->cursor.vpos < w->current_matrix->nrows
21063 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21064 {
21065 struct glyph *g = row->glyphs[TEXT_AREA];
21066 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21067 struct glyph *gpt = g + w->cursor.hpos;
21068
21069 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21070 {
21071 if (BUFFERP (g->object) && g->charpos != PT)
21072 {
21073 SET_PT (g->charpos);
21074 w->cursor.vpos = -1;
21075 return make_number (PT);
21076 }
21077 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21078 {
21079 ptrdiff_t new_pos;
21080
21081 if (BUFFERP (gpt->object))
21082 {
21083 new_pos = PT;
21084 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21085 new_pos += (row->reversed_p ? -dir : dir);
21086 else
21087 new_pos -= (row->reversed_p ? -dir : dir);
21088 }
21089 else if (BUFFERP (g->object))
21090 new_pos = g->charpos;
21091 else
21092 break;
21093 SET_PT (new_pos);
21094 w->cursor.vpos = -1;
21095 return make_number (PT);
21096 }
21097 else if (ROW_GLYPH_NEWLINE_P (row, g))
21098 {
21099 /* Glyphs inserted at the end of a non-empty line for
21100 positioning the cursor have zero charpos, so we must
21101 deduce the value of point by other means. */
21102 if (g->charpos > 0)
21103 SET_PT (g->charpos);
21104 else if (row->ends_at_zv_p && PT != ZV)
21105 SET_PT (ZV);
21106 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21107 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21108 else
21109 break;
21110 w->cursor.vpos = -1;
21111 return make_number (PT);
21112 }
21113 }
21114 if (g == e || NILP (g->object))
21115 {
21116 if (row->truncated_on_left_p || row->truncated_on_right_p)
21117 goto simulate_display;
21118 if (!row->reversed_p)
21119 row += dir;
21120 else
21121 row -= dir;
21122 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21123 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21124 goto simulate_display;
21125
21126 if (dir > 0)
21127 {
21128 if (row->reversed_p && !row->continued_p)
21129 {
21130 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21131 w->cursor.vpos = -1;
21132 return make_number (PT);
21133 }
21134 g = row->glyphs[TEXT_AREA];
21135 e = g + row->used[TEXT_AREA];
21136 for ( ; g < e; g++)
21137 {
21138 if (BUFFERP (g->object)
21139 /* Empty lines have only one glyph, which stands
21140 for the newline, and whose charpos is the
21141 buffer position of the newline. */
21142 || ROW_GLYPH_NEWLINE_P (row, g)
21143 /* When the buffer ends in a newline, the line at
21144 EOB also has one glyph, but its charpos is -1. */
21145 || (row->ends_at_zv_p
21146 && !row->reversed_p
21147 && NILP (g->object)
21148 && g->type == CHAR_GLYPH
21149 && g->u.ch == ' '))
21150 {
21151 if (g->charpos > 0)
21152 SET_PT (g->charpos);
21153 else if (!row->reversed_p
21154 && row->ends_at_zv_p
21155 && PT != ZV)
21156 SET_PT (ZV);
21157 else
21158 continue;
21159 w->cursor.vpos = -1;
21160 return make_number (PT);
21161 }
21162 }
21163 }
21164 else
21165 {
21166 if (!row->reversed_p && !row->continued_p)
21167 {
21168 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21169 w->cursor.vpos = -1;
21170 return make_number (PT);
21171 }
21172 e = row->glyphs[TEXT_AREA];
21173 g = e + row->used[TEXT_AREA] - 1;
21174 for ( ; g >= e; g--)
21175 {
21176 if (BUFFERP (g->object)
21177 || (ROW_GLYPH_NEWLINE_P (row, g)
21178 && g->charpos > 0)
21179 /* Empty R2L lines on GUI frames have the buffer
21180 position of the newline stored in the stretch
21181 glyph. */
21182 || g->type == STRETCH_GLYPH
21183 || (row->ends_at_zv_p
21184 && row->reversed_p
21185 && NILP (g->object)
21186 && g->type == CHAR_GLYPH
21187 && g->u.ch == ' '))
21188 {
21189 if (g->charpos > 0)
21190 SET_PT (g->charpos);
21191 else if (row->reversed_p
21192 && row->ends_at_zv_p
21193 && PT != ZV)
21194 SET_PT (ZV);
21195 else
21196 continue;
21197 w->cursor.vpos = -1;
21198 return make_number (PT);
21199 }
21200 }
21201 }
21202 }
21203 }
21204
21205 simulate_display:
21206
21207 /* If we wind up here, we failed to move by using the glyphs, so we
21208 need to simulate display instead. */
21209
21210 if (b)
21211 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21212 else
21213 paragraph_dir = Qleft_to_right;
21214 if (EQ (paragraph_dir, Qright_to_left))
21215 dir = -dir;
21216 if (PT <= BEGV && dir < 0)
21217 xsignal0 (Qbeginning_of_buffer);
21218 else if (PT >= ZV && dir > 0)
21219 xsignal0 (Qend_of_buffer);
21220 else
21221 {
21222 struct text_pos pt;
21223 struct it it;
21224 int pt_x, target_x, pixel_width, pt_vpos;
21225 bool at_eol_p;
21226 bool overshoot_expected = false;
21227 bool target_is_eol_p = false;
21228
21229 /* Setup the arena. */
21230 SET_TEXT_POS (pt, PT, PT_BYTE);
21231 start_display (&it, w, pt);
21232
21233 if (it.cmp_it.id < 0
21234 && it.method == GET_FROM_STRING
21235 && it.area == TEXT_AREA
21236 && it.string_from_display_prop_p
21237 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21238 overshoot_expected = true;
21239
21240 /* Find the X coordinate of point. We start from the beginning
21241 of this or previous line to make sure we are before point in
21242 the logical order (since the move_it_* functions can only
21243 move forward). */
21244 reseat:
21245 reseat_at_previous_visible_line_start (&it);
21246 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21247 if (IT_CHARPOS (it) != PT)
21248 {
21249 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21250 -1, -1, -1, MOVE_TO_POS);
21251 /* If we missed point because the character there is
21252 displayed out of a display vector that has more than one
21253 glyph, retry expecting overshoot. */
21254 if (it.method == GET_FROM_DISPLAY_VECTOR
21255 && it.current.dpvec_index > 0
21256 && !overshoot_expected)
21257 {
21258 overshoot_expected = true;
21259 goto reseat;
21260 }
21261 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21262 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21263 }
21264 pt_x = it.current_x;
21265 pt_vpos = it.vpos;
21266 if (dir > 0 || overshoot_expected)
21267 {
21268 struct glyph_row *row = it.glyph_row;
21269
21270 /* When point is at beginning of line, we don't have
21271 information about the glyph there loaded into struct
21272 it. Calling get_next_display_element fixes that. */
21273 if (pt_x == 0)
21274 get_next_display_element (&it);
21275 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21276 it.glyph_row = NULL;
21277 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21278 it.glyph_row = row;
21279 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21280 it, lest it will become out of sync with it's buffer
21281 position. */
21282 it.current_x = pt_x;
21283 }
21284 else
21285 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21286 pixel_width = it.pixel_width;
21287 if (overshoot_expected && at_eol_p)
21288 pixel_width = 0;
21289 else if (pixel_width <= 0)
21290 pixel_width = 1;
21291
21292 /* If there's a display string (or something similar) at point,
21293 we are actually at the glyph to the left of point, so we need
21294 to correct the X coordinate. */
21295 if (overshoot_expected)
21296 {
21297 if (it.bidi_p)
21298 pt_x += pixel_width * it.bidi_it.scan_dir;
21299 else
21300 pt_x += pixel_width;
21301 }
21302
21303 /* Compute target X coordinate, either to the left or to the
21304 right of point. On TTY frames, all characters have the same
21305 pixel width of 1, so we can use that. On GUI frames we don't
21306 have an easy way of getting at the pixel width of the
21307 character to the left of point, so we use a different method
21308 of getting to that place. */
21309 if (dir > 0)
21310 target_x = pt_x + pixel_width;
21311 else
21312 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21313
21314 /* Target X coordinate could be one line above or below the line
21315 of point, in which case we need to adjust the target X
21316 coordinate. Also, if moving to the left, we need to begin at
21317 the left edge of the point's screen line. */
21318 if (dir < 0)
21319 {
21320 if (pt_x > 0)
21321 {
21322 start_display (&it, w, pt);
21323 reseat_at_previous_visible_line_start (&it);
21324 it.current_x = it.current_y = it.hpos = 0;
21325 if (pt_vpos != 0)
21326 move_it_by_lines (&it, pt_vpos);
21327 }
21328 else
21329 {
21330 move_it_by_lines (&it, -1);
21331 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21332 target_is_eol_p = true;
21333 /* Under word-wrap, we don't know the x coordinate of
21334 the last character displayed on the previous line,
21335 which immediately precedes the wrap point. To find
21336 out its x coordinate, we try moving to the right
21337 margin of the window, which will stop at the wrap
21338 point, and then reset target_x to point at the
21339 character that precedes the wrap point. This is not
21340 needed on GUI frames, because (see below) there we
21341 move from the left margin one grapheme cluster at a
21342 time, and stop when we hit the wrap point. */
21343 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21344 {
21345 void *it_data = NULL;
21346 struct it it2;
21347
21348 SAVE_IT (it2, it, it_data);
21349 move_it_in_display_line_to (&it, ZV, target_x,
21350 MOVE_TO_POS | MOVE_TO_X);
21351 /* If we arrived at target_x, that _is_ the last
21352 character on the previous line. */
21353 if (it.current_x != target_x)
21354 target_x = it.current_x - 1;
21355 RESTORE_IT (&it, &it2, it_data);
21356 }
21357 }
21358 }
21359 else
21360 {
21361 if (at_eol_p
21362 || (target_x >= it.last_visible_x
21363 && it.line_wrap != TRUNCATE))
21364 {
21365 if (pt_x > 0)
21366 move_it_by_lines (&it, 0);
21367 move_it_by_lines (&it, 1);
21368 target_x = 0;
21369 }
21370 }
21371
21372 /* Move to the target X coordinate. */
21373 #ifdef HAVE_WINDOW_SYSTEM
21374 /* On GUI frames, as we don't know the X coordinate of the
21375 character to the left of point, moving point to the left
21376 requires walking, one grapheme cluster at a time, until we
21377 find ourself at a place immediately to the left of the
21378 character at point. */
21379 if (FRAME_WINDOW_P (it.f) && dir < 0)
21380 {
21381 struct text_pos new_pos;
21382 enum move_it_result rc = MOVE_X_REACHED;
21383
21384 if (it.current_x == 0)
21385 get_next_display_element (&it);
21386 if (it.what == IT_COMPOSITION)
21387 {
21388 new_pos.charpos = it.cmp_it.charpos;
21389 new_pos.bytepos = -1;
21390 }
21391 else
21392 new_pos = it.current.pos;
21393
21394 while (it.current_x + it.pixel_width <= target_x
21395 && (rc == MOVE_X_REACHED
21396 /* Under word-wrap, move_it_in_display_line_to
21397 stops at correct coordinates, but sometimes
21398 returns MOVE_POS_MATCH_OR_ZV. */
21399 || (it.line_wrap == WORD_WRAP
21400 && rc == MOVE_POS_MATCH_OR_ZV)))
21401 {
21402 int new_x = it.current_x + it.pixel_width;
21403
21404 /* For composed characters, we want the position of the
21405 first character in the grapheme cluster (usually, the
21406 composition's base character), whereas it.current
21407 might give us the position of the _last_ one, e.g. if
21408 the composition is rendered in reverse due to bidi
21409 reordering. */
21410 if (it.what == IT_COMPOSITION)
21411 {
21412 new_pos.charpos = it.cmp_it.charpos;
21413 new_pos.bytepos = -1;
21414 }
21415 else
21416 new_pos = it.current.pos;
21417 if (new_x == it.current_x)
21418 new_x++;
21419 rc = move_it_in_display_line_to (&it, ZV, new_x,
21420 MOVE_TO_POS | MOVE_TO_X);
21421 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21422 break;
21423 }
21424 /* The previous position we saw in the loop is the one we
21425 want. */
21426 if (new_pos.bytepos == -1)
21427 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21428 it.current.pos = new_pos;
21429 }
21430 else
21431 #endif
21432 if (it.current_x != target_x)
21433 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21434
21435 /* When lines are truncated, the above loop will stop at the
21436 window edge. But we want to get to the end of line, even if
21437 it is beyond the window edge; automatic hscroll will then
21438 scroll the window to show point as appropriate. */
21439 if (target_is_eol_p && it.line_wrap == TRUNCATE
21440 && get_next_display_element (&it))
21441 {
21442 struct text_pos new_pos = it.current.pos;
21443
21444 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21445 {
21446 set_iterator_to_next (&it, false);
21447 if (it.method == GET_FROM_BUFFER)
21448 new_pos = it.current.pos;
21449 if (!get_next_display_element (&it))
21450 break;
21451 }
21452
21453 it.current.pos = new_pos;
21454 }
21455
21456 /* If we ended up in a display string that covers point, move to
21457 buffer position to the right in the visual order. */
21458 if (dir > 0)
21459 {
21460 while (IT_CHARPOS (it) == PT)
21461 {
21462 set_iterator_to_next (&it, false);
21463 if (!get_next_display_element (&it))
21464 break;
21465 }
21466 }
21467
21468 /* Move point to that position. */
21469 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21470 }
21471
21472 return make_number (PT);
21473
21474 #undef ROW_GLYPH_NEWLINE_P
21475 }
21476
21477 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21478 Sbidi_resolved_levels, 0, 1, 0,
21479 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21480
21481 The resolved levels are produced by the Emacs bidi reordering engine
21482 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21483 read the Unicode Standard Annex 9 (UAX#9) for background information
21484 about these levels.
21485
21486 VPOS is the zero-based number of the current window's screen line
21487 for which to produce the resolved levels. If VPOS is nil or omitted,
21488 it defaults to the screen line of point. If the window displays a
21489 header line, VPOS of zero will report on the header line, and first
21490 line of text in the window will have VPOS of 1.
21491
21492 Value is an array of resolved levels, indexed by glyph number.
21493 Glyphs are numbered from zero starting from the beginning of the
21494 screen line, i.e. the left edge of the window for left-to-right lines
21495 and from the right edge for right-to-left lines. The resolved levels
21496 are produced only for the window's text area; text in display margins
21497 is not included.
21498
21499 If the selected window's display is not up-to-date, or if the specified
21500 screen line does not display text, this function returns nil. It is
21501 highly recommended to bind this function to some simple key, like F8,
21502 in order to avoid these problems.
21503
21504 This function exists mainly for testing the correctness of the
21505 Emacs UBA implementation, in particular with the test suite. */)
21506 (Lisp_Object vpos)
21507 {
21508 struct window *w = XWINDOW (selected_window);
21509 struct buffer *b = XBUFFER (w->contents);
21510 int nrow;
21511 struct glyph_row *row;
21512
21513 if (NILP (vpos))
21514 {
21515 int d1, d2, d3, d4, d5;
21516
21517 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21518 }
21519 else
21520 {
21521 CHECK_NUMBER_COERCE_MARKER (vpos);
21522 nrow = XINT (vpos);
21523 }
21524
21525 /* We require up-to-date glyph matrix for this window. */
21526 if (w->window_end_valid
21527 && !windows_or_buffers_changed
21528 && b
21529 && !b->clip_changed
21530 && !b->prevent_redisplay_optimizations_p
21531 && !window_outdated (w)
21532 && nrow >= 0
21533 && nrow < w->current_matrix->nrows
21534 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21535 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21536 {
21537 struct glyph *g, *e, *g1;
21538 int nglyphs, i;
21539 Lisp_Object levels;
21540
21541 if (!row->reversed_p) /* Left-to-right glyph row. */
21542 {
21543 g = g1 = row->glyphs[TEXT_AREA];
21544 e = g + row->used[TEXT_AREA];
21545
21546 /* Skip over glyphs at the start of the row that was
21547 generated by redisplay for its own needs. */
21548 while (g < e
21549 && NILP (g->object)
21550 && g->charpos < 0)
21551 g++;
21552 g1 = g;
21553
21554 /* Count the "interesting" glyphs in this row. */
21555 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21556 nglyphs++;
21557
21558 /* Create and fill the array. */
21559 levels = make_uninit_vector (nglyphs);
21560 for (i = 0; g1 < g; i++, g1++)
21561 ASET (levels, i, make_number (g1->resolved_level));
21562 }
21563 else /* Right-to-left glyph row. */
21564 {
21565 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21566 e = row->glyphs[TEXT_AREA] - 1;
21567 while (g > e
21568 && NILP (g->object)
21569 && g->charpos < 0)
21570 g--;
21571 g1 = g;
21572 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21573 nglyphs++;
21574 levels = make_uninit_vector (nglyphs);
21575 for (i = 0; g1 > g; i++, g1--)
21576 ASET (levels, i, make_number (g1->resolved_level));
21577 }
21578 return levels;
21579 }
21580 else
21581 return Qnil;
21582 }
21583
21584
21585 \f
21586 /***********************************************************************
21587 Menu Bar
21588 ***********************************************************************/
21589
21590 /* Redisplay the menu bar in the frame for window W.
21591
21592 The menu bar of X frames that don't have X toolkit support is
21593 displayed in a special window W->frame->menu_bar_window.
21594
21595 The menu bar of terminal frames is treated specially as far as
21596 glyph matrices are concerned. Menu bar lines are not part of
21597 windows, so the update is done directly on the frame matrix rows
21598 for the menu bar. */
21599
21600 static void
21601 display_menu_bar (struct window *w)
21602 {
21603 struct frame *f = XFRAME (WINDOW_FRAME (w));
21604 struct it it;
21605 Lisp_Object items;
21606 int i;
21607
21608 /* Don't do all this for graphical frames. */
21609 #ifdef HAVE_NTGUI
21610 if (FRAME_W32_P (f))
21611 return;
21612 #endif
21613 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21614 if (FRAME_X_P (f))
21615 return;
21616 #endif
21617
21618 #ifdef HAVE_NS
21619 if (FRAME_NS_P (f))
21620 return;
21621 #endif /* HAVE_NS */
21622
21623 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21624 eassert (!FRAME_WINDOW_P (f));
21625 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21626 it.first_visible_x = 0;
21627 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21628 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21629 if (FRAME_WINDOW_P (f))
21630 {
21631 /* Menu bar lines are displayed in the desired matrix of the
21632 dummy window menu_bar_window. */
21633 struct window *menu_w;
21634 menu_w = XWINDOW (f->menu_bar_window);
21635 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21636 MENU_FACE_ID);
21637 it.first_visible_x = 0;
21638 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21639 }
21640 else
21641 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21642 {
21643 /* This is a TTY frame, i.e. character hpos/vpos are used as
21644 pixel x/y. */
21645 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21646 MENU_FACE_ID);
21647 it.first_visible_x = 0;
21648 it.last_visible_x = FRAME_COLS (f);
21649 }
21650
21651 /* FIXME: This should be controlled by a user option. See the
21652 comments in redisplay_tool_bar and display_mode_line about
21653 this. */
21654 it.paragraph_embedding = L2R;
21655
21656 /* Clear all rows of the menu bar. */
21657 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21658 {
21659 struct glyph_row *row = it.glyph_row + i;
21660 clear_glyph_row (row);
21661 row->enabled_p = true;
21662 row->full_width_p = true;
21663 row->reversed_p = false;
21664 }
21665
21666 /* Display all items of the menu bar. */
21667 items = FRAME_MENU_BAR_ITEMS (it.f);
21668 for (i = 0; i < ASIZE (items); i += 4)
21669 {
21670 Lisp_Object string;
21671
21672 /* Stop at nil string. */
21673 string = AREF (items, i + 1);
21674 if (NILP (string))
21675 break;
21676
21677 /* Remember where item was displayed. */
21678 ASET (items, i + 3, make_number (it.hpos));
21679
21680 /* Display the item, pad with one space. */
21681 if (it.current_x < it.last_visible_x)
21682 display_string (NULL, string, Qnil, 0, 0, &it,
21683 SCHARS (string) + 1, 0, 0, -1);
21684 }
21685
21686 /* Fill out the line with spaces. */
21687 if (it.current_x < it.last_visible_x)
21688 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21689
21690 /* Compute the total height of the lines. */
21691 compute_line_metrics (&it);
21692 }
21693
21694 /* Deep copy of a glyph row, including the glyphs. */
21695 static void
21696 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21697 {
21698 struct glyph *pointers[1 + LAST_AREA];
21699 int to_used = to->used[TEXT_AREA];
21700
21701 /* Save glyph pointers of TO. */
21702 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21703
21704 /* Do a structure assignment. */
21705 *to = *from;
21706
21707 /* Restore original glyph pointers of TO. */
21708 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21709
21710 /* Copy the glyphs. */
21711 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21712 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21713
21714 /* If we filled only part of the TO row, fill the rest with
21715 space_glyph (which will display as empty space). */
21716 if (to_used > from->used[TEXT_AREA])
21717 fill_up_frame_row_with_spaces (to, to_used);
21718 }
21719
21720 /* Display one menu item on a TTY, by overwriting the glyphs in the
21721 frame F's desired glyph matrix with glyphs produced from the menu
21722 item text. Called from term.c to display TTY drop-down menus one
21723 item at a time.
21724
21725 ITEM_TEXT is the menu item text as a C string.
21726
21727 FACE_ID is the face ID to be used for this menu item. FACE_ID
21728 could specify one of 3 faces: a face for an enabled item, a face
21729 for a disabled item, or a face for a selected item.
21730
21731 X and Y are coordinates of the first glyph in the frame's desired
21732 matrix to be overwritten by the menu item. Since this is a TTY, Y
21733 is the zero-based number of the glyph row and X is the zero-based
21734 glyph number in the row, starting from left, where to start
21735 displaying the item.
21736
21737 SUBMENU means this menu item drops down a submenu, which
21738 should be indicated by displaying a proper visual cue after the
21739 item text. */
21740
21741 void
21742 display_tty_menu_item (const char *item_text, int width, int face_id,
21743 int x, int y, bool submenu)
21744 {
21745 struct it it;
21746 struct frame *f = SELECTED_FRAME ();
21747 struct window *w = XWINDOW (f->selected_window);
21748 struct glyph_row *row;
21749 size_t item_len = strlen (item_text);
21750
21751 eassert (FRAME_TERMCAP_P (f));
21752
21753 /* Don't write beyond the matrix's last row. This can happen for
21754 TTY screens that are not high enough to show the entire menu.
21755 (This is actually a bit of defensive programming, as
21756 tty_menu_display already limits the number of menu items to one
21757 less than the number of screen lines.) */
21758 if (y >= f->desired_matrix->nrows)
21759 return;
21760
21761 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21762 it.first_visible_x = 0;
21763 it.last_visible_x = FRAME_COLS (f) - 1;
21764 row = it.glyph_row;
21765 /* Start with the row contents from the current matrix. */
21766 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21767 bool saved_width = row->full_width_p;
21768 row->full_width_p = true;
21769 bool saved_reversed = row->reversed_p;
21770 row->reversed_p = false;
21771 row->enabled_p = true;
21772
21773 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21774 desired face. */
21775 eassert (x < f->desired_matrix->matrix_w);
21776 it.current_x = it.hpos = x;
21777 it.current_y = it.vpos = y;
21778 int saved_used = row->used[TEXT_AREA];
21779 bool saved_truncated = row->truncated_on_right_p;
21780 row->used[TEXT_AREA] = x;
21781 it.face_id = face_id;
21782 it.line_wrap = TRUNCATE;
21783
21784 /* FIXME: This should be controlled by a user option. See the
21785 comments in redisplay_tool_bar and display_mode_line about this.
21786 Also, if paragraph_embedding could ever be R2L, changes will be
21787 needed to avoid shifting to the right the row characters in
21788 term.c:append_glyph. */
21789 it.paragraph_embedding = L2R;
21790
21791 /* Pad with a space on the left. */
21792 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21793 width--;
21794 /* Display the menu item, pad with spaces to WIDTH. */
21795 if (submenu)
21796 {
21797 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21798 item_len, 0, FRAME_COLS (f) - 1, -1);
21799 width -= item_len;
21800 /* Indicate with " >" that there's a submenu. */
21801 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21802 FRAME_COLS (f) - 1, -1);
21803 }
21804 else
21805 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21806 width, 0, FRAME_COLS (f) - 1, -1);
21807
21808 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21809 row->truncated_on_right_p = saved_truncated;
21810 row->hash = row_hash (row);
21811 row->full_width_p = saved_width;
21812 row->reversed_p = saved_reversed;
21813 }
21814 \f
21815 /***********************************************************************
21816 Mode Line
21817 ***********************************************************************/
21818
21819 /* Redisplay mode lines in the window tree whose root is WINDOW.
21820 If FORCE, redisplay mode lines unconditionally.
21821 Otherwise, redisplay only mode lines that are garbaged. Value is
21822 the number of windows whose mode lines were redisplayed. */
21823
21824 static int
21825 redisplay_mode_lines (Lisp_Object window, bool force)
21826 {
21827 int nwindows = 0;
21828
21829 while (!NILP (window))
21830 {
21831 struct window *w = XWINDOW (window);
21832
21833 if (WINDOWP (w->contents))
21834 nwindows += redisplay_mode_lines (w->contents, force);
21835 else if (force
21836 || FRAME_GARBAGED_P (XFRAME (w->frame))
21837 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21838 {
21839 struct text_pos lpoint;
21840 struct buffer *old = current_buffer;
21841
21842 /* Set the window's buffer for the mode line display. */
21843 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21844 set_buffer_internal_1 (XBUFFER (w->contents));
21845
21846 /* Point refers normally to the selected window. For any
21847 other window, set up appropriate value. */
21848 if (!EQ (window, selected_window))
21849 {
21850 struct text_pos pt;
21851
21852 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21853 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21854 }
21855
21856 /* Display mode lines. */
21857 clear_glyph_matrix (w->desired_matrix);
21858 if (display_mode_lines (w))
21859 ++nwindows;
21860
21861 /* Restore old settings. */
21862 set_buffer_internal_1 (old);
21863 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21864 }
21865
21866 window = w->next;
21867 }
21868
21869 return nwindows;
21870 }
21871
21872
21873 /* Display the mode and/or header line of window W. Value is the
21874 sum number of mode lines and header lines displayed. */
21875
21876 static int
21877 display_mode_lines (struct window *w)
21878 {
21879 Lisp_Object old_selected_window = selected_window;
21880 Lisp_Object old_selected_frame = selected_frame;
21881 Lisp_Object new_frame = w->frame;
21882 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21883 int n = 0;
21884
21885 selected_frame = new_frame;
21886 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21887 or window's point, then we'd need select_window_1 here as well. */
21888 XSETWINDOW (selected_window, w);
21889 XFRAME (new_frame)->selected_window = selected_window;
21890
21891 /* These will be set while the mode line specs are processed. */
21892 line_number_displayed = false;
21893 w->column_number_displayed = -1;
21894
21895 if (WINDOW_WANTS_MODELINE_P (w))
21896 {
21897 struct window *sel_w = XWINDOW (old_selected_window);
21898
21899 /* Select mode line face based on the real selected window. */
21900 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21901 BVAR (current_buffer, mode_line_format));
21902 ++n;
21903 }
21904
21905 if (WINDOW_WANTS_HEADER_LINE_P (w))
21906 {
21907 display_mode_line (w, HEADER_LINE_FACE_ID,
21908 BVAR (current_buffer, header_line_format));
21909 ++n;
21910 }
21911
21912 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21913 selected_frame = old_selected_frame;
21914 selected_window = old_selected_window;
21915 if (n > 0)
21916 w->must_be_updated_p = true;
21917 return n;
21918 }
21919
21920
21921 /* Display mode or header line of window W. FACE_ID specifies which
21922 line to display; it is either MODE_LINE_FACE_ID or
21923 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21924 display. Value is the pixel height of the mode/header line
21925 displayed. */
21926
21927 static int
21928 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21929 {
21930 struct it it;
21931 struct face *face;
21932 ptrdiff_t count = SPECPDL_INDEX ();
21933
21934 init_iterator (&it, w, -1, -1, NULL, face_id);
21935 /* Don't extend on a previously drawn mode-line.
21936 This may happen if called from pos_visible_p. */
21937 it.glyph_row->enabled_p = false;
21938 prepare_desired_row (w, it.glyph_row, true);
21939
21940 it.glyph_row->mode_line_p = true;
21941
21942 /* FIXME: This should be controlled by a user option. But
21943 supporting such an option is not trivial, since the mode line is
21944 made up of many separate strings. */
21945 it.paragraph_embedding = L2R;
21946
21947 record_unwind_protect (unwind_format_mode_line,
21948 format_mode_line_unwind_data (NULL, NULL,
21949 Qnil, false));
21950
21951 mode_line_target = MODE_LINE_DISPLAY;
21952
21953 /* Temporarily make frame's keyboard the current kboard so that
21954 kboard-local variables in the mode_line_format will get the right
21955 values. */
21956 push_kboard (FRAME_KBOARD (it.f));
21957 record_unwind_save_match_data ();
21958 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
21959 pop_kboard ();
21960
21961 unbind_to (count, Qnil);
21962
21963 /* Fill up with spaces. */
21964 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21965
21966 compute_line_metrics (&it);
21967 it.glyph_row->full_width_p = true;
21968 it.glyph_row->continued_p = false;
21969 it.glyph_row->truncated_on_left_p = false;
21970 it.glyph_row->truncated_on_right_p = false;
21971
21972 /* Make a 3D mode-line have a shadow at its right end. */
21973 face = FACE_FROM_ID (it.f, face_id);
21974 extend_face_to_end_of_line (&it);
21975 if (face->box != FACE_NO_BOX)
21976 {
21977 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21978 + it.glyph_row->used[TEXT_AREA] - 1);
21979 last->right_box_line_p = true;
21980 }
21981
21982 return it.glyph_row->height;
21983 }
21984
21985 /* Move element ELT in LIST to the front of LIST.
21986 Return the updated list. */
21987
21988 static Lisp_Object
21989 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21990 {
21991 register Lisp_Object tail, prev;
21992 register Lisp_Object tem;
21993
21994 tail = list;
21995 prev = Qnil;
21996 while (CONSP (tail))
21997 {
21998 tem = XCAR (tail);
21999
22000 if (EQ (elt, tem))
22001 {
22002 /* Splice out the link TAIL. */
22003 if (NILP (prev))
22004 list = XCDR (tail);
22005 else
22006 Fsetcdr (prev, XCDR (tail));
22007
22008 /* Now make it the first. */
22009 Fsetcdr (tail, list);
22010 return tail;
22011 }
22012 else
22013 prev = tail;
22014 tail = XCDR (tail);
22015 QUIT;
22016 }
22017
22018 /* Not found--return unchanged LIST. */
22019 return list;
22020 }
22021
22022 /* Contribute ELT to the mode line for window IT->w. How it
22023 translates into text depends on its data type.
22024
22025 IT describes the display environment in which we display, as usual.
22026
22027 DEPTH is the depth in recursion. It is used to prevent
22028 infinite recursion here.
22029
22030 FIELD_WIDTH is the number of characters the display of ELT should
22031 occupy in the mode line, and PRECISION is the maximum number of
22032 characters to display from ELT's representation. See
22033 display_string for details.
22034
22035 Returns the hpos of the end of the text generated by ELT.
22036
22037 PROPS is a property list to add to any string we encounter.
22038
22039 If RISKY, remove (disregard) any properties in any string
22040 we encounter, and ignore :eval and :propertize.
22041
22042 The global variable `mode_line_target' determines whether the
22043 output is passed to `store_mode_line_noprop',
22044 `store_mode_line_string', or `display_string'. */
22045
22046 static int
22047 display_mode_element (struct it *it, int depth, int field_width, int precision,
22048 Lisp_Object elt, Lisp_Object props, bool risky)
22049 {
22050 int n = 0, field, prec;
22051 bool literal = false;
22052
22053 tail_recurse:
22054 if (depth > 100)
22055 elt = build_string ("*too-deep*");
22056
22057 depth++;
22058
22059 switch (XTYPE (elt))
22060 {
22061 case Lisp_String:
22062 {
22063 /* A string: output it and check for %-constructs within it. */
22064 unsigned char c;
22065 ptrdiff_t offset = 0;
22066
22067 if (SCHARS (elt) > 0
22068 && (!NILP (props) || risky))
22069 {
22070 Lisp_Object oprops, aelt;
22071 oprops = Ftext_properties_at (make_number (0), elt);
22072
22073 /* If the starting string's properties are not what
22074 we want, translate the string. Also, if the string
22075 is risky, do that anyway. */
22076
22077 if (NILP (Fequal (props, oprops)) || risky)
22078 {
22079 /* If the starting string has properties,
22080 merge the specified ones onto the existing ones. */
22081 if (! NILP (oprops) && !risky)
22082 {
22083 Lisp_Object tem;
22084
22085 oprops = Fcopy_sequence (oprops);
22086 tem = props;
22087 while (CONSP (tem))
22088 {
22089 oprops = Fplist_put (oprops, XCAR (tem),
22090 XCAR (XCDR (tem)));
22091 tem = XCDR (XCDR (tem));
22092 }
22093 props = oprops;
22094 }
22095
22096 aelt = Fassoc (elt, mode_line_proptrans_alist);
22097 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22098 {
22099 /* AELT is what we want. Move it to the front
22100 without consing. */
22101 elt = XCAR (aelt);
22102 mode_line_proptrans_alist
22103 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22104 }
22105 else
22106 {
22107 Lisp_Object tem;
22108
22109 /* If AELT has the wrong props, it is useless.
22110 so get rid of it. */
22111 if (! NILP (aelt))
22112 mode_line_proptrans_alist
22113 = Fdelq (aelt, mode_line_proptrans_alist);
22114
22115 elt = Fcopy_sequence (elt);
22116 Fset_text_properties (make_number (0), Flength (elt),
22117 props, elt);
22118 /* Add this item to mode_line_proptrans_alist. */
22119 mode_line_proptrans_alist
22120 = Fcons (Fcons (elt, props),
22121 mode_line_proptrans_alist);
22122 /* Truncate mode_line_proptrans_alist
22123 to at most 50 elements. */
22124 tem = Fnthcdr (make_number (50),
22125 mode_line_proptrans_alist);
22126 if (! NILP (tem))
22127 XSETCDR (tem, Qnil);
22128 }
22129 }
22130 }
22131
22132 offset = 0;
22133
22134 if (literal)
22135 {
22136 prec = precision - n;
22137 switch (mode_line_target)
22138 {
22139 case MODE_LINE_NOPROP:
22140 case MODE_LINE_TITLE:
22141 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22142 break;
22143 case MODE_LINE_STRING:
22144 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22145 break;
22146 case MODE_LINE_DISPLAY:
22147 n += display_string (NULL, elt, Qnil, 0, 0, it,
22148 0, prec, 0, STRING_MULTIBYTE (elt));
22149 break;
22150 }
22151
22152 break;
22153 }
22154
22155 /* Handle the non-literal case. */
22156
22157 while ((precision <= 0 || n < precision)
22158 && SREF (elt, offset) != 0
22159 && (mode_line_target != MODE_LINE_DISPLAY
22160 || it->current_x < it->last_visible_x))
22161 {
22162 ptrdiff_t last_offset = offset;
22163
22164 /* Advance to end of string or next format specifier. */
22165 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22166 ;
22167
22168 if (offset - 1 != last_offset)
22169 {
22170 ptrdiff_t nchars, nbytes;
22171
22172 /* Output to end of string or up to '%'. Field width
22173 is length of string. Don't output more than
22174 PRECISION allows us. */
22175 offset--;
22176
22177 prec = c_string_width (SDATA (elt) + last_offset,
22178 offset - last_offset, precision - n,
22179 &nchars, &nbytes);
22180
22181 switch (mode_line_target)
22182 {
22183 case MODE_LINE_NOPROP:
22184 case MODE_LINE_TITLE:
22185 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22186 break;
22187 case MODE_LINE_STRING:
22188 {
22189 ptrdiff_t bytepos = last_offset;
22190 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22191 ptrdiff_t endpos = (precision <= 0
22192 ? string_byte_to_char (elt, offset)
22193 : charpos + nchars);
22194 Lisp_Object mode_string
22195 = Fsubstring (elt, make_number (charpos),
22196 make_number (endpos));
22197 n += store_mode_line_string (NULL, mode_string, false,
22198 0, 0, Qnil);
22199 }
22200 break;
22201 case MODE_LINE_DISPLAY:
22202 {
22203 ptrdiff_t bytepos = last_offset;
22204 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22205
22206 if (precision <= 0)
22207 nchars = string_byte_to_char (elt, offset) - charpos;
22208 n += display_string (NULL, elt, Qnil, 0, charpos,
22209 it, 0, nchars, 0,
22210 STRING_MULTIBYTE (elt));
22211 }
22212 break;
22213 }
22214 }
22215 else /* c == '%' */
22216 {
22217 ptrdiff_t percent_position = offset;
22218
22219 /* Get the specified minimum width. Zero means
22220 don't pad. */
22221 field = 0;
22222 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22223 field = field * 10 + c - '0';
22224
22225 /* Don't pad beyond the total padding allowed. */
22226 if (field_width - n > 0 && field > field_width - n)
22227 field = field_width - n;
22228
22229 /* Note that either PRECISION <= 0 or N < PRECISION. */
22230 prec = precision - n;
22231
22232 if (c == 'M')
22233 n += display_mode_element (it, depth, field, prec,
22234 Vglobal_mode_string, props,
22235 risky);
22236 else if (c != 0)
22237 {
22238 bool multibyte;
22239 ptrdiff_t bytepos, charpos;
22240 const char *spec;
22241 Lisp_Object string;
22242
22243 bytepos = percent_position;
22244 charpos = (STRING_MULTIBYTE (elt)
22245 ? string_byte_to_char (elt, bytepos)
22246 : bytepos);
22247 spec = decode_mode_spec (it->w, c, field, &string);
22248 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22249
22250 switch (mode_line_target)
22251 {
22252 case MODE_LINE_NOPROP:
22253 case MODE_LINE_TITLE:
22254 n += store_mode_line_noprop (spec, field, prec);
22255 break;
22256 case MODE_LINE_STRING:
22257 {
22258 Lisp_Object tem = build_string (spec);
22259 props = Ftext_properties_at (make_number (charpos), elt);
22260 /* Should only keep face property in props */
22261 n += store_mode_line_string (NULL, tem, false,
22262 field, prec, props);
22263 }
22264 break;
22265 case MODE_LINE_DISPLAY:
22266 {
22267 int nglyphs_before, nwritten;
22268
22269 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22270 nwritten = display_string (spec, string, elt,
22271 charpos, 0, it,
22272 field, prec, 0,
22273 multibyte);
22274
22275 /* Assign to the glyphs written above the
22276 string where the `%x' came from, position
22277 of the `%'. */
22278 if (nwritten > 0)
22279 {
22280 struct glyph *glyph
22281 = (it->glyph_row->glyphs[TEXT_AREA]
22282 + nglyphs_before);
22283 int i;
22284
22285 for (i = 0; i < nwritten; ++i)
22286 {
22287 glyph[i].object = elt;
22288 glyph[i].charpos = charpos;
22289 }
22290
22291 n += nwritten;
22292 }
22293 }
22294 break;
22295 }
22296 }
22297 else /* c == 0 */
22298 break;
22299 }
22300 }
22301 }
22302 break;
22303
22304 case Lisp_Symbol:
22305 /* A symbol: process the value of the symbol recursively
22306 as if it appeared here directly. Avoid error if symbol void.
22307 Special case: if value of symbol is a string, output the string
22308 literally. */
22309 {
22310 register Lisp_Object tem;
22311
22312 /* If the variable is not marked as risky to set
22313 then its contents are risky to use. */
22314 if (NILP (Fget (elt, Qrisky_local_variable)))
22315 risky = true;
22316
22317 tem = Fboundp (elt);
22318 if (!NILP (tem))
22319 {
22320 tem = Fsymbol_value (elt);
22321 /* If value is a string, output that string literally:
22322 don't check for % within it. */
22323 if (STRINGP (tem))
22324 literal = true;
22325
22326 if (!EQ (tem, elt))
22327 {
22328 /* Give up right away for nil or t. */
22329 elt = tem;
22330 goto tail_recurse;
22331 }
22332 }
22333 }
22334 break;
22335
22336 case Lisp_Cons:
22337 {
22338 register Lisp_Object car, tem;
22339
22340 /* A cons cell: five distinct cases.
22341 If first element is :eval or :propertize, do something special.
22342 If first element is a string or a cons, process all the elements
22343 and effectively concatenate them.
22344 If first element is a negative number, truncate displaying cdr to
22345 at most that many characters. If positive, pad (with spaces)
22346 to at least that many characters.
22347 If first element is a symbol, process the cadr or caddr recursively
22348 according to whether the symbol's value is non-nil or nil. */
22349 car = XCAR (elt);
22350 if (EQ (car, QCeval))
22351 {
22352 /* An element of the form (:eval FORM) means evaluate FORM
22353 and use the result as mode line elements. */
22354
22355 if (risky)
22356 break;
22357
22358 if (CONSP (XCDR (elt)))
22359 {
22360 Lisp_Object spec;
22361 spec = safe__eval (true, XCAR (XCDR (elt)));
22362 n += display_mode_element (it, depth, field_width - n,
22363 precision - n, spec, props,
22364 risky);
22365 }
22366 }
22367 else if (EQ (car, QCpropertize))
22368 {
22369 /* An element of the form (:propertize ELT PROPS...)
22370 means display ELT but applying properties PROPS. */
22371
22372 if (risky)
22373 break;
22374
22375 if (CONSP (XCDR (elt)))
22376 n += display_mode_element (it, depth, field_width - n,
22377 precision - n, XCAR (XCDR (elt)),
22378 XCDR (XCDR (elt)), risky);
22379 }
22380 else if (SYMBOLP (car))
22381 {
22382 tem = Fboundp (car);
22383 elt = XCDR (elt);
22384 if (!CONSP (elt))
22385 goto invalid;
22386 /* elt is now the cdr, and we know it is a cons cell.
22387 Use its car if CAR has a non-nil value. */
22388 if (!NILP (tem))
22389 {
22390 tem = Fsymbol_value (car);
22391 if (!NILP (tem))
22392 {
22393 elt = XCAR (elt);
22394 goto tail_recurse;
22395 }
22396 }
22397 /* Symbol's value is nil (or symbol is unbound)
22398 Get the cddr of the original list
22399 and if possible find the caddr and use that. */
22400 elt = XCDR (elt);
22401 if (NILP (elt))
22402 break;
22403 else if (!CONSP (elt))
22404 goto invalid;
22405 elt = XCAR (elt);
22406 goto tail_recurse;
22407 }
22408 else if (INTEGERP (car))
22409 {
22410 register int lim = XINT (car);
22411 elt = XCDR (elt);
22412 if (lim < 0)
22413 {
22414 /* Negative int means reduce maximum width. */
22415 if (precision <= 0)
22416 precision = -lim;
22417 else
22418 precision = min (precision, -lim);
22419 }
22420 else if (lim > 0)
22421 {
22422 /* Padding specified. Don't let it be more than
22423 current maximum. */
22424 if (precision > 0)
22425 lim = min (precision, lim);
22426
22427 /* If that's more padding than already wanted, queue it.
22428 But don't reduce padding already specified even if
22429 that is beyond the current truncation point. */
22430 field_width = max (lim, field_width);
22431 }
22432 goto tail_recurse;
22433 }
22434 else if (STRINGP (car) || CONSP (car))
22435 {
22436 Lisp_Object halftail = elt;
22437 int len = 0;
22438
22439 while (CONSP (elt)
22440 && (precision <= 0 || n < precision))
22441 {
22442 n += display_mode_element (it, depth,
22443 /* Do padding only after the last
22444 element in the list. */
22445 (! CONSP (XCDR (elt))
22446 ? field_width - n
22447 : 0),
22448 precision - n, XCAR (elt),
22449 props, risky);
22450 elt = XCDR (elt);
22451 len++;
22452 if ((len & 1) == 0)
22453 halftail = XCDR (halftail);
22454 /* Check for cycle. */
22455 if (EQ (halftail, elt))
22456 break;
22457 }
22458 }
22459 }
22460 break;
22461
22462 default:
22463 invalid:
22464 elt = build_string ("*invalid*");
22465 goto tail_recurse;
22466 }
22467
22468 /* Pad to FIELD_WIDTH. */
22469 if (field_width > 0 && n < field_width)
22470 {
22471 switch (mode_line_target)
22472 {
22473 case MODE_LINE_NOPROP:
22474 case MODE_LINE_TITLE:
22475 n += store_mode_line_noprop ("", field_width - n, 0);
22476 break;
22477 case MODE_LINE_STRING:
22478 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
22479 Qnil);
22480 break;
22481 case MODE_LINE_DISPLAY:
22482 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22483 0, 0, 0);
22484 break;
22485 }
22486 }
22487
22488 return n;
22489 }
22490
22491 /* Store a mode-line string element in mode_line_string_list.
22492
22493 If STRING is non-null, display that C string. Otherwise, the Lisp
22494 string LISP_STRING is displayed.
22495
22496 FIELD_WIDTH is the minimum number of output glyphs to produce.
22497 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22498 with spaces. FIELD_WIDTH <= 0 means don't pad.
22499
22500 PRECISION is the maximum number of characters to output from
22501 STRING. PRECISION <= 0 means don't truncate the string.
22502
22503 If COPY_STRING, make a copy of LISP_STRING before adding
22504 properties to the string.
22505
22506 PROPS are the properties to add to the string.
22507 The mode_line_string_face face property is always added to the string.
22508 */
22509
22510 static int
22511 store_mode_line_string (const char *string, Lisp_Object lisp_string,
22512 bool copy_string,
22513 int field_width, int precision, Lisp_Object props)
22514 {
22515 ptrdiff_t len;
22516 int n = 0;
22517
22518 if (string != NULL)
22519 {
22520 len = strlen (string);
22521 if (precision > 0 && len > precision)
22522 len = precision;
22523 lisp_string = make_string (string, len);
22524 if (NILP (props))
22525 props = mode_line_string_face_prop;
22526 else if (!NILP (mode_line_string_face))
22527 {
22528 Lisp_Object face = Fplist_get (props, Qface);
22529 props = Fcopy_sequence (props);
22530 if (NILP (face))
22531 face = mode_line_string_face;
22532 else
22533 face = list2 (face, mode_line_string_face);
22534 props = Fplist_put (props, Qface, face);
22535 }
22536 Fadd_text_properties (make_number (0), make_number (len),
22537 props, lisp_string);
22538 }
22539 else
22540 {
22541 len = XFASTINT (Flength (lisp_string));
22542 if (precision > 0 && len > precision)
22543 {
22544 len = precision;
22545 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22546 precision = -1;
22547 }
22548 if (!NILP (mode_line_string_face))
22549 {
22550 Lisp_Object face;
22551 if (NILP (props))
22552 props = Ftext_properties_at (make_number (0), lisp_string);
22553 face = Fplist_get (props, Qface);
22554 if (NILP (face))
22555 face = mode_line_string_face;
22556 else
22557 face = list2 (face, mode_line_string_face);
22558 props = list2 (Qface, face);
22559 if (copy_string)
22560 lisp_string = Fcopy_sequence (lisp_string);
22561 }
22562 if (!NILP (props))
22563 Fadd_text_properties (make_number (0), make_number (len),
22564 props, lisp_string);
22565 }
22566
22567 if (len > 0)
22568 {
22569 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22570 n += len;
22571 }
22572
22573 if (field_width > len)
22574 {
22575 field_width -= len;
22576 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22577 if (!NILP (props))
22578 Fadd_text_properties (make_number (0), make_number (field_width),
22579 props, lisp_string);
22580 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22581 n += field_width;
22582 }
22583
22584 return n;
22585 }
22586
22587
22588 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22589 1, 4, 0,
22590 doc: /* Format a string out of a mode line format specification.
22591 First arg FORMAT specifies the mode line format (see `mode-line-format'
22592 for details) to use.
22593
22594 By default, the format is evaluated for the currently selected window.
22595
22596 Optional second arg FACE specifies the face property to put on all
22597 characters for which no face is specified. The value nil means the
22598 default face. The value t means whatever face the window's mode line
22599 currently uses (either `mode-line' or `mode-line-inactive',
22600 depending on whether the window is the selected window or not).
22601 An integer value means the value string has no text
22602 properties.
22603
22604 Optional third and fourth args WINDOW and BUFFER specify the window
22605 and buffer to use as the context for the formatting (defaults
22606 are the selected window and the WINDOW's buffer). */)
22607 (Lisp_Object format, Lisp_Object face,
22608 Lisp_Object window, Lisp_Object buffer)
22609 {
22610 struct it it;
22611 int len;
22612 struct window *w;
22613 struct buffer *old_buffer = NULL;
22614 int face_id;
22615 bool no_props = INTEGERP (face);
22616 ptrdiff_t count = SPECPDL_INDEX ();
22617 Lisp_Object str;
22618 int string_start = 0;
22619
22620 w = decode_any_window (window);
22621 XSETWINDOW (window, w);
22622
22623 if (NILP (buffer))
22624 buffer = w->contents;
22625 CHECK_BUFFER (buffer);
22626
22627 /* Make formatting the modeline a non-op when noninteractive, otherwise
22628 there will be problems later caused by a partially initialized frame. */
22629 if (NILP (format) || noninteractive)
22630 return empty_unibyte_string;
22631
22632 if (no_props)
22633 face = Qnil;
22634
22635 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22636 : EQ (face, Qt) ? (EQ (window, selected_window)
22637 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22638 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22639 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22640 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22641 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22642 : DEFAULT_FACE_ID;
22643
22644 old_buffer = current_buffer;
22645
22646 /* Save things including mode_line_proptrans_alist,
22647 and set that to nil so that we don't alter the outer value. */
22648 record_unwind_protect (unwind_format_mode_line,
22649 format_mode_line_unwind_data
22650 (XFRAME (WINDOW_FRAME (w)),
22651 old_buffer, selected_window, true));
22652 mode_line_proptrans_alist = Qnil;
22653
22654 Fselect_window (window, Qt);
22655 set_buffer_internal_1 (XBUFFER (buffer));
22656
22657 init_iterator (&it, w, -1, -1, NULL, face_id);
22658
22659 if (no_props)
22660 {
22661 mode_line_target = MODE_LINE_NOPROP;
22662 mode_line_string_face_prop = Qnil;
22663 mode_line_string_list = Qnil;
22664 string_start = MODE_LINE_NOPROP_LEN (0);
22665 }
22666 else
22667 {
22668 mode_line_target = MODE_LINE_STRING;
22669 mode_line_string_list = Qnil;
22670 mode_line_string_face = face;
22671 mode_line_string_face_prop
22672 = NILP (face) ? Qnil : list2 (Qface, face);
22673 }
22674
22675 push_kboard (FRAME_KBOARD (it.f));
22676 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22677 pop_kboard ();
22678
22679 if (no_props)
22680 {
22681 len = MODE_LINE_NOPROP_LEN (string_start);
22682 str = make_string (mode_line_noprop_buf + string_start, len);
22683 }
22684 else
22685 {
22686 mode_line_string_list = Fnreverse (mode_line_string_list);
22687 str = Fmapconcat (Qidentity, mode_line_string_list,
22688 empty_unibyte_string);
22689 }
22690
22691 unbind_to (count, Qnil);
22692 return str;
22693 }
22694
22695 /* Write a null-terminated, right justified decimal representation of
22696 the positive integer D to BUF using a minimal field width WIDTH. */
22697
22698 static void
22699 pint2str (register char *buf, register int width, register ptrdiff_t d)
22700 {
22701 register char *p = buf;
22702
22703 if (d <= 0)
22704 *p++ = '0';
22705 else
22706 {
22707 while (d > 0)
22708 {
22709 *p++ = d % 10 + '0';
22710 d /= 10;
22711 }
22712 }
22713
22714 for (width -= (int) (p - buf); width > 0; --width)
22715 *p++ = ' ';
22716 *p-- = '\0';
22717 while (p > buf)
22718 {
22719 d = *buf;
22720 *buf++ = *p;
22721 *p-- = d;
22722 }
22723 }
22724
22725 /* Write a null-terminated, right justified decimal and "human
22726 readable" representation of the nonnegative integer D to BUF using
22727 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22728
22729 static const char power_letter[] =
22730 {
22731 0, /* no letter */
22732 'k', /* kilo */
22733 'M', /* mega */
22734 'G', /* giga */
22735 'T', /* tera */
22736 'P', /* peta */
22737 'E', /* exa */
22738 'Z', /* zetta */
22739 'Y' /* yotta */
22740 };
22741
22742 static void
22743 pint2hrstr (char *buf, int width, ptrdiff_t d)
22744 {
22745 /* We aim to represent the nonnegative integer D as
22746 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22747 ptrdiff_t quotient = d;
22748 int remainder = 0;
22749 /* -1 means: do not use TENTHS. */
22750 int tenths = -1;
22751 int exponent = 0;
22752
22753 /* Length of QUOTIENT.TENTHS as a string. */
22754 int length;
22755
22756 char * psuffix;
22757 char * p;
22758
22759 if (quotient >= 1000)
22760 {
22761 /* Scale to the appropriate EXPONENT. */
22762 do
22763 {
22764 remainder = quotient % 1000;
22765 quotient /= 1000;
22766 exponent++;
22767 }
22768 while (quotient >= 1000);
22769
22770 /* Round to nearest and decide whether to use TENTHS or not. */
22771 if (quotient <= 9)
22772 {
22773 tenths = remainder / 100;
22774 if (remainder % 100 >= 50)
22775 {
22776 if (tenths < 9)
22777 tenths++;
22778 else
22779 {
22780 quotient++;
22781 if (quotient == 10)
22782 tenths = -1;
22783 else
22784 tenths = 0;
22785 }
22786 }
22787 }
22788 else
22789 if (remainder >= 500)
22790 {
22791 if (quotient < 999)
22792 quotient++;
22793 else
22794 {
22795 quotient = 1;
22796 exponent++;
22797 tenths = 0;
22798 }
22799 }
22800 }
22801
22802 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22803 if (tenths == -1 && quotient <= 99)
22804 if (quotient <= 9)
22805 length = 1;
22806 else
22807 length = 2;
22808 else
22809 length = 3;
22810 p = psuffix = buf + max (width, length);
22811
22812 /* Print EXPONENT. */
22813 *psuffix++ = power_letter[exponent];
22814 *psuffix = '\0';
22815
22816 /* Print TENTHS. */
22817 if (tenths >= 0)
22818 {
22819 *--p = '0' + tenths;
22820 *--p = '.';
22821 }
22822
22823 /* Print QUOTIENT. */
22824 do
22825 {
22826 int digit = quotient % 10;
22827 *--p = '0' + digit;
22828 }
22829 while ((quotient /= 10) != 0);
22830
22831 /* Print leading spaces. */
22832 while (buf < p)
22833 *--p = ' ';
22834 }
22835
22836 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22837 If EOL_FLAG, set also a mnemonic character for end-of-line
22838 type of CODING_SYSTEM. Return updated pointer into BUF. */
22839
22840 static unsigned char invalid_eol_type[] = "(*invalid*)";
22841
22842 static char *
22843 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
22844 {
22845 Lisp_Object val;
22846 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22847 const unsigned char *eol_str;
22848 int eol_str_len;
22849 /* The EOL conversion we are using. */
22850 Lisp_Object eoltype;
22851
22852 val = CODING_SYSTEM_SPEC (coding_system);
22853 eoltype = Qnil;
22854
22855 if (!VECTORP (val)) /* Not yet decided. */
22856 {
22857 *buf++ = multibyte ? '-' : ' ';
22858 if (eol_flag)
22859 eoltype = eol_mnemonic_undecided;
22860 /* Don't mention EOL conversion if it isn't decided. */
22861 }
22862 else
22863 {
22864 Lisp_Object attrs;
22865 Lisp_Object eolvalue;
22866
22867 attrs = AREF (val, 0);
22868 eolvalue = AREF (val, 2);
22869
22870 *buf++ = multibyte
22871 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22872 : ' ';
22873
22874 if (eol_flag)
22875 {
22876 /* The EOL conversion that is normal on this system. */
22877
22878 if (NILP (eolvalue)) /* Not yet decided. */
22879 eoltype = eol_mnemonic_undecided;
22880 else if (VECTORP (eolvalue)) /* Not yet decided. */
22881 eoltype = eol_mnemonic_undecided;
22882 else /* eolvalue is Qunix, Qdos, or Qmac. */
22883 eoltype = (EQ (eolvalue, Qunix)
22884 ? eol_mnemonic_unix
22885 : EQ (eolvalue, Qdos)
22886 ? eol_mnemonic_dos : eol_mnemonic_mac);
22887 }
22888 }
22889
22890 if (eol_flag)
22891 {
22892 /* Mention the EOL conversion if it is not the usual one. */
22893 if (STRINGP (eoltype))
22894 {
22895 eol_str = SDATA (eoltype);
22896 eol_str_len = SBYTES (eoltype);
22897 }
22898 else if (CHARACTERP (eoltype))
22899 {
22900 int c = XFASTINT (eoltype);
22901 return buf + CHAR_STRING (c, (unsigned char *) buf);
22902 }
22903 else
22904 {
22905 eol_str = invalid_eol_type;
22906 eol_str_len = sizeof (invalid_eol_type) - 1;
22907 }
22908 memcpy (buf, eol_str, eol_str_len);
22909 buf += eol_str_len;
22910 }
22911
22912 return buf;
22913 }
22914
22915 /* Return a string for the output of a mode line %-spec for window W,
22916 generated by character C. FIELD_WIDTH > 0 means pad the string
22917 returned with spaces to that value. Return a Lisp string in
22918 *STRING if the resulting string is taken from that Lisp string.
22919
22920 Note we operate on the current buffer for most purposes. */
22921
22922 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22923
22924 static const char *
22925 decode_mode_spec (struct window *w, register int c, int field_width,
22926 Lisp_Object *string)
22927 {
22928 Lisp_Object obj;
22929 struct frame *f = XFRAME (WINDOW_FRAME (w));
22930 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22931 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22932 produce strings from numerical values, so limit preposterously
22933 large values of FIELD_WIDTH to avoid overrunning the buffer's
22934 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22935 bytes plus the terminating null. */
22936 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22937 struct buffer *b = current_buffer;
22938
22939 obj = Qnil;
22940 *string = Qnil;
22941
22942 switch (c)
22943 {
22944 case '*':
22945 if (!NILP (BVAR (b, read_only)))
22946 return "%";
22947 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22948 return "*";
22949 return "-";
22950
22951 case '+':
22952 /* This differs from %* only for a modified read-only buffer. */
22953 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22954 return "*";
22955 if (!NILP (BVAR (b, read_only)))
22956 return "%";
22957 return "-";
22958
22959 case '&':
22960 /* This differs from %* in ignoring read-only-ness. */
22961 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22962 return "*";
22963 return "-";
22964
22965 case '%':
22966 return "%";
22967
22968 case '[':
22969 {
22970 int i;
22971 char *p;
22972
22973 if (command_loop_level > 5)
22974 return "[[[... ";
22975 p = decode_mode_spec_buf;
22976 for (i = 0; i < command_loop_level; i++)
22977 *p++ = '[';
22978 *p = 0;
22979 return decode_mode_spec_buf;
22980 }
22981
22982 case ']':
22983 {
22984 int i;
22985 char *p;
22986
22987 if (command_loop_level > 5)
22988 return " ...]]]";
22989 p = decode_mode_spec_buf;
22990 for (i = 0; i < command_loop_level; i++)
22991 *p++ = ']';
22992 *p = 0;
22993 return decode_mode_spec_buf;
22994 }
22995
22996 case '-':
22997 {
22998 register int i;
22999
23000 /* Let lots_of_dashes be a string of infinite length. */
23001 if (mode_line_target == MODE_LINE_NOPROP
23002 || mode_line_target == MODE_LINE_STRING)
23003 return "--";
23004 if (field_width <= 0
23005 || field_width > sizeof (lots_of_dashes))
23006 {
23007 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23008 decode_mode_spec_buf[i] = '-';
23009 decode_mode_spec_buf[i] = '\0';
23010 return decode_mode_spec_buf;
23011 }
23012 else
23013 return lots_of_dashes;
23014 }
23015
23016 case 'b':
23017 obj = BVAR (b, name);
23018 break;
23019
23020 case 'c':
23021 /* %c and %l are ignored in `frame-title-format'.
23022 (In redisplay_internal, the frame title is drawn _before_ the
23023 windows are updated, so the stuff which depends on actual
23024 window contents (such as %l) may fail to render properly, or
23025 even crash emacs.) */
23026 if (mode_line_target == MODE_LINE_TITLE)
23027 return "";
23028 else
23029 {
23030 ptrdiff_t col = current_column ();
23031 w->column_number_displayed = col;
23032 pint2str (decode_mode_spec_buf, width, col);
23033 return decode_mode_spec_buf;
23034 }
23035
23036 case 'e':
23037 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23038 {
23039 if (NILP (Vmemory_full))
23040 return "";
23041 else
23042 return "!MEM FULL! ";
23043 }
23044 #else
23045 return "";
23046 #endif
23047
23048 case 'F':
23049 /* %F displays the frame name. */
23050 if (!NILP (f->title))
23051 return SSDATA (f->title);
23052 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23053 return SSDATA (f->name);
23054 return "Emacs";
23055
23056 case 'f':
23057 obj = BVAR (b, filename);
23058 break;
23059
23060 case 'i':
23061 {
23062 ptrdiff_t size = ZV - BEGV;
23063 pint2str (decode_mode_spec_buf, width, size);
23064 return decode_mode_spec_buf;
23065 }
23066
23067 case 'I':
23068 {
23069 ptrdiff_t size = ZV - BEGV;
23070 pint2hrstr (decode_mode_spec_buf, width, size);
23071 return decode_mode_spec_buf;
23072 }
23073
23074 case 'l':
23075 {
23076 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23077 ptrdiff_t topline, nlines, height;
23078 ptrdiff_t junk;
23079
23080 /* %c and %l are ignored in `frame-title-format'. */
23081 if (mode_line_target == MODE_LINE_TITLE)
23082 return "";
23083
23084 startpos = marker_position (w->start);
23085 startpos_byte = marker_byte_position (w->start);
23086 height = WINDOW_TOTAL_LINES (w);
23087
23088 /* If we decided that this buffer isn't suitable for line numbers,
23089 don't forget that too fast. */
23090 if (w->base_line_pos == -1)
23091 goto no_value;
23092
23093 /* If the buffer is very big, don't waste time. */
23094 if (INTEGERP (Vline_number_display_limit)
23095 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23096 {
23097 w->base_line_pos = 0;
23098 w->base_line_number = 0;
23099 goto no_value;
23100 }
23101
23102 if (w->base_line_number > 0
23103 && w->base_line_pos > 0
23104 && w->base_line_pos <= startpos)
23105 {
23106 line = w->base_line_number;
23107 linepos = w->base_line_pos;
23108 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23109 }
23110 else
23111 {
23112 line = 1;
23113 linepos = BUF_BEGV (b);
23114 linepos_byte = BUF_BEGV_BYTE (b);
23115 }
23116
23117 /* Count lines from base line to window start position. */
23118 nlines = display_count_lines (linepos_byte,
23119 startpos_byte,
23120 startpos, &junk);
23121
23122 topline = nlines + line;
23123
23124 /* Determine a new base line, if the old one is too close
23125 or too far away, or if we did not have one.
23126 "Too close" means it's plausible a scroll-down would
23127 go back past it. */
23128 if (startpos == BUF_BEGV (b))
23129 {
23130 w->base_line_number = topline;
23131 w->base_line_pos = BUF_BEGV (b);
23132 }
23133 else if (nlines < height + 25 || nlines > height * 3 + 50
23134 || linepos == BUF_BEGV (b))
23135 {
23136 ptrdiff_t limit = BUF_BEGV (b);
23137 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23138 ptrdiff_t position;
23139 ptrdiff_t distance =
23140 (height * 2 + 30) * line_number_display_limit_width;
23141
23142 if (startpos - distance > limit)
23143 {
23144 limit = startpos - distance;
23145 limit_byte = CHAR_TO_BYTE (limit);
23146 }
23147
23148 nlines = display_count_lines (startpos_byte,
23149 limit_byte,
23150 - (height * 2 + 30),
23151 &position);
23152 /* If we couldn't find the lines we wanted within
23153 line_number_display_limit_width chars per line,
23154 give up on line numbers for this window. */
23155 if (position == limit_byte && limit == startpos - distance)
23156 {
23157 w->base_line_pos = -1;
23158 w->base_line_number = 0;
23159 goto no_value;
23160 }
23161
23162 w->base_line_number = topline - nlines;
23163 w->base_line_pos = BYTE_TO_CHAR (position);
23164 }
23165
23166 /* Now count lines from the start pos to point. */
23167 nlines = display_count_lines (startpos_byte,
23168 PT_BYTE, PT, &junk);
23169
23170 /* Record that we did display the line number. */
23171 line_number_displayed = true;
23172
23173 /* Make the string to show. */
23174 pint2str (decode_mode_spec_buf, width, topline + nlines);
23175 return decode_mode_spec_buf;
23176 no_value:
23177 {
23178 char *p = decode_mode_spec_buf;
23179 int pad = width - 2;
23180 while (pad-- > 0)
23181 *p++ = ' ';
23182 *p++ = '?';
23183 *p++ = '?';
23184 *p = '\0';
23185 return decode_mode_spec_buf;
23186 }
23187 }
23188 break;
23189
23190 case 'm':
23191 obj = BVAR (b, mode_name);
23192 break;
23193
23194 case 'n':
23195 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23196 return " Narrow";
23197 break;
23198
23199 case 'p':
23200 {
23201 ptrdiff_t pos = marker_position (w->start);
23202 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23203
23204 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23205 {
23206 if (pos <= BUF_BEGV (b))
23207 return "All";
23208 else
23209 return "Bottom";
23210 }
23211 else if (pos <= BUF_BEGV (b))
23212 return "Top";
23213 else
23214 {
23215 if (total > 1000000)
23216 /* Do it differently for a large value, to avoid overflow. */
23217 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23218 else
23219 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23220 /* We can't normally display a 3-digit number,
23221 so get us a 2-digit number that is close. */
23222 if (total == 100)
23223 total = 99;
23224 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23225 return decode_mode_spec_buf;
23226 }
23227 }
23228
23229 /* Display percentage of size above the bottom of the screen. */
23230 case 'P':
23231 {
23232 ptrdiff_t toppos = marker_position (w->start);
23233 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23234 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23235
23236 if (botpos >= BUF_ZV (b))
23237 {
23238 if (toppos <= BUF_BEGV (b))
23239 return "All";
23240 else
23241 return "Bottom";
23242 }
23243 else
23244 {
23245 if (total > 1000000)
23246 /* Do it differently for a large value, to avoid overflow. */
23247 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23248 else
23249 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23250 /* We can't normally display a 3-digit number,
23251 so get us a 2-digit number that is close. */
23252 if (total == 100)
23253 total = 99;
23254 if (toppos <= BUF_BEGV (b))
23255 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23256 else
23257 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23258 return decode_mode_spec_buf;
23259 }
23260 }
23261
23262 case 's':
23263 /* status of process */
23264 obj = Fget_buffer_process (Fcurrent_buffer ());
23265 if (NILP (obj))
23266 return "no process";
23267 #ifndef MSDOS
23268 obj = Fsymbol_name (Fprocess_status (obj));
23269 #endif
23270 break;
23271
23272 case '@':
23273 {
23274 ptrdiff_t count = inhibit_garbage_collection ();
23275 Lisp_Object curdir = BVAR (current_buffer, directory);
23276 Lisp_Object val = Qnil;
23277
23278 if (STRINGP (curdir))
23279 val = call1 (intern ("file-remote-p"), curdir);
23280
23281 unbind_to (count, Qnil);
23282
23283 if (NILP (val))
23284 return "-";
23285 else
23286 return "@";
23287 }
23288
23289 case 'z':
23290 /* coding-system (not including end-of-line format) */
23291 case 'Z':
23292 /* coding-system (including end-of-line type) */
23293 {
23294 bool eol_flag = (c == 'Z');
23295 char *p = decode_mode_spec_buf;
23296
23297 if (! FRAME_WINDOW_P (f))
23298 {
23299 /* No need to mention EOL here--the terminal never needs
23300 to do EOL conversion. */
23301 p = decode_mode_spec_coding (CODING_ID_NAME
23302 (FRAME_KEYBOARD_CODING (f)->id),
23303 p, false);
23304 p = decode_mode_spec_coding (CODING_ID_NAME
23305 (FRAME_TERMINAL_CODING (f)->id),
23306 p, false);
23307 }
23308 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23309 p, eol_flag);
23310
23311 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23312 #ifdef subprocesses
23313 obj = Fget_buffer_process (Fcurrent_buffer ());
23314 if (PROCESSP (obj))
23315 {
23316 p = decode_mode_spec_coding
23317 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23318 p = decode_mode_spec_coding
23319 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23320 }
23321 #endif /* subprocesses */
23322 #endif /* false */
23323 *p = 0;
23324 return decode_mode_spec_buf;
23325 }
23326 }
23327
23328 if (STRINGP (obj))
23329 {
23330 *string = obj;
23331 return SSDATA (obj);
23332 }
23333 else
23334 return "";
23335 }
23336
23337
23338 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23339 means count lines back from START_BYTE. But don't go beyond
23340 LIMIT_BYTE. Return the number of lines thus found (always
23341 nonnegative).
23342
23343 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23344 either the position COUNT lines after/before START_BYTE, if we
23345 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23346 COUNT lines. */
23347
23348 static ptrdiff_t
23349 display_count_lines (ptrdiff_t start_byte,
23350 ptrdiff_t limit_byte, ptrdiff_t count,
23351 ptrdiff_t *byte_pos_ptr)
23352 {
23353 register unsigned char *cursor;
23354 unsigned char *base;
23355
23356 register ptrdiff_t ceiling;
23357 register unsigned char *ceiling_addr;
23358 ptrdiff_t orig_count = count;
23359
23360 /* If we are not in selective display mode,
23361 check only for newlines. */
23362 bool selective_display
23363 = (!NILP (BVAR (current_buffer, selective_display))
23364 && !INTEGERP (BVAR (current_buffer, selective_display)));
23365
23366 if (count > 0)
23367 {
23368 while (start_byte < limit_byte)
23369 {
23370 ceiling = BUFFER_CEILING_OF (start_byte);
23371 ceiling = min (limit_byte - 1, ceiling);
23372 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23373 base = (cursor = BYTE_POS_ADDR (start_byte));
23374
23375 do
23376 {
23377 if (selective_display)
23378 {
23379 while (*cursor != '\n' && *cursor != 015
23380 && ++cursor != ceiling_addr)
23381 continue;
23382 if (cursor == ceiling_addr)
23383 break;
23384 }
23385 else
23386 {
23387 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23388 if (! cursor)
23389 break;
23390 }
23391
23392 cursor++;
23393
23394 if (--count == 0)
23395 {
23396 start_byte += cursor - base;
23397 *byte_pos_ptr = start_byte;
23398 return orig_count;
23399 }
23400 }
23401 while (cursor < ceiling_addr);
23402
23403 start_byte += ceiling_addr - base;
23404 }
23405 }
23406 else
23407 {
23408 while (start_byte > limit_byte)
23409 {
23410 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23411 ceiling = max (limit_byte, ceiling);
23412 ceiling_addr = BYTE_POS_ADDR (ceiling);
23413 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23414 while (true)
23415 {
23416 if (selective_display)
23417 {
23418 while (--cursor >= ceiling_addr
23419 && *cursor != '\n' && *cursor != 015)
23420 continue;
23421 if (cursor < ceiling_addr)
23422 break;
23423 }
23424 else
23425 {
23426 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23427 if (! cursor)
23428 break;
23429 }
23430
23431 if (++count == 0)
23432 {
23433 start_byte += cursor - base + 1;
23434 *byte_pos_ptr = start_byte;
23435 /* When scanning backwards, we should
23436 not count the newline posterior to which we stop. */
23437 return - orig_count - 1;
23438 }
23439 }
23440 start_byte += ceiling_addr - base;
23441 }
23442 }
23443
23444 *byte_pos_ptr = limit_byte;
23445
23446 if (count < 0)
23447 return - orig_count + count;
23448 return orig_count - count;
23449
23450 }
23451
23452
23453 \f
23454 /***********************************************************************
23455 Displaying strings
23456 ***********************************************************************/
23457
23458 /* Display a NUL-terminated string, starting with index START.
23459
23460 If STRING is non-null, display that C string. Otherwise, the Lisp
23461 string LISP_STRING is displayed. There's a case that STRING is
23462 non-null and LISP_STRING is not nil. It means STRING is a string
23463 data of LISP_STRING. In that case, we display LISP_STRING while
23464 ignoring its text properties.
23465
23466 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23467 FACE_STRING. Display STRING or LISP_STRING with the face at
23468 FACE_STRING_POS in FACE_STRING:
23469
23470 Display the string in the environment given by IT, but use the
23471 standard display table, temporarily.
23472
23473 FIELD_WIDTH is the minimum number of output glyphs to produce.
23474 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23475 with spaces. If STRING has more characters, more than FIELD_WIDTH
23476 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23477
23478 PRECISION is the maximum number of characters to output from
23479 STRING. PRECISION < 0 means don't truncate the string.
23480
23481 This is roughly equivalent to printf format specifiers:
23482
23483 FIELD_WIDTH PRECISION PRINTF
23484 ----------------------------------------
23485 -1 -1 %s
23486 -1 10 %.10s
23487 10 -1 %10s
23488 20 10 %20.10s
23489
23490 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23491 display them, and < 0 means obey the current buffer's value of
23492 enable_multibyte_characters.
23493
23494 Value is the number of columns displayed. */
23495
23496 static int
23497 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23498 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23499 int field_width, int precision, int max_x, int multibyte)
23500 {
23501 int hpos_at_start = it->hpos;
23502 int saved_face_id = it->face_id;
23503 struct glyph_row *row = it->glyph_row;
23504 ptrdiff_t it_charpos;
23505
23506 /* Initialize the iterator IT for iteration over STRING beginning
23507 with index START. */
23508 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23509 precision, field_width, multibyte);
23510 if (string && STRINGP (lisp_string))
23511 /* LISP_STRING is the one returned by decode_mode_spec. We should
23512 ignore its text properties. */
23513 it->stop_charpos = it->end_charpos;
23514
23515 /* If displaying STRING, set up the face of the iterator from
23516 FACE_STRING, if that's given. */
23517 if (STRINGP (face_string))
23518 {
23519 ptrdiff_t endptr;
23520 struct face *face;
23521
23522 it->face_id
23523 = face_at_string_position (it->w, face_string, face_string_pos,
23524 0, &endptr, it->base_face_id, false);
23525 face = FACE_FROM_ID (it->f, it->face_id);
23526 it->face_box_p = face->box != FACE_NO_BOX;
23527 }
23528
23529 /* Set max_x to the maximum allowed X position. Don't let it go
23530 beyond the right edge of the window. */
23531 if (max_x <= 0)
23532 max_x = it->last_visible_x;
23533 else
23534 max_x = min (max_x, it->last_visible_x);
23535
23536 /* Skip over display elements that are not visible. because IT->w is
23537 hscrolled. */
23538 if (it->current_x < it->first_visible_x)
23539 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23540 MOVE_TO_POS | MOVE_TO_X);
23541
23542 row->ascent = it->max_ascent;
23543 row->height = it->max_ascent + it->max_descent;
23544 row->phys_ascent = it->max_phys_ascent;
23545 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23546 row->extra_line_spacing = it->max_extra_line_spacing;
23547
23548 if (STRINGP (it->string))
23549 it_charpos = IT_STRING_CHARPOS (*it);
23550 else
23551 it_charpos = IT_CHARPOS (*it);
23552
23553 /* This condition is for the case that we are called with current_x
23554 past last_visible_x. */
23555 while (it->current_x < max_x)
23556 {
23557 int x_before, x, n_glyphs_before, i, nglyphs;
23558
23559 /* Get the next display element. */
23560 if (!get_next_display_element (it))
23561 break;
23562
23563 /* Produce glyphs. */
23564 x_before = it->current_x;
23565 n_glyphs_before = row->used[TEXT_AREA];
23566 PRODUCE_GLYPHS (it);
23567
23568 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23569 i = 0;
23570 x = x_before;
23571 while (i < nglyphs)
23572 {
23573 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23574
23575 if (it->line_wrap != TRUNCATE
23576 && x + glyph->pixel_width > max_x)
23577 {
23578 /* End of continued line or max_x reached. */
23579 if (CHAR_GLYPH_PADDING_P (*glyph))
23580 {
23581 /* A wide character is unbreakable. */
23582 if (row->reversed_p)
23583 unproduce_glyphs (it, row->used[TEXT_AREA]
23584 - n_glyphs_before);
23585 row->used[TEXT_AREA] = n_glyphs_before;
23586 it->current_x = x_before;
23587 }
23588 else
23589 {
23590 if (row->reversed_p)
23591 unproduce_glyphs (it, row->used[TEXT_AREA]
23592 - (n_glyphs_before + i));
23593 row->used[TEXT_AREA] = n_glyphs_before + i;
23594 it->current_x = x;
23595 }
23596 break;
23597 }
23598 else if (x + glyph->pixel_width >= it->first_visible_x)
23599 {
23600 /* Glyph is at least partially visible. */
23601 ++it->hpos;
23602 if (x < it->first_visible_x)
23603 row->x = x - it->first_visible_x;
23604 }
23605 else
23606 {
23607 /* Glyph is off the left margin of the display area.
23608 Should not happen. */
23609 emacs_abort ();
23610 }
23611
23612 row->ascent = max (row->ascent, it->max_ascent);
23613 row->height = max (row->height, it->max_ascent + it->max_descent);
23614 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23615 row->phys_height = max (row->phys_height,
23616 it->max_phys_ascent + it->max_phys_descent);
23617 row->extra_line_spacing = max (row->extra_line_spacing,
23618 it->max_extra_line_spacing);
23619 x += glyph->pixel_width;
23620 ++i;
23621 }
23622
23623 /* Stop if max_x reached. */
23624 if (i < nglyphs)
23625 break;
23626
23627 /* Stop at line ends. */
23628 if (ITERATOR_AT_END_OF_LINE_P (it))
23629 {
23630 it->continuation_lines_width = 0;
23631 break;
23632 }
23633
23634 set_iterator_to_next (it, true);
23635 if (STRINGP (it->string))
23636 it_charpos = IT_STRING_CHARPOS (*it);
23637 else
23638 it_charpos = IT_CHARPOS (*it);
23639
23640 /* Stop if truncating at the right edge. */
23641 if (it->line_wrap == TRUNCATE
23642 && it->current_x >= it->last_visible_x)
23643 {
23644 /* Add truncation mark, but don't do it if the line is
23645 truncated at a padding space. */
23646 if (it_charpos < it->string_nchars)
23647 {
23648 if (!FRAME_WINDOW_P (it->f))
23649 {
23650 int ii, n;
23651
23652 if (it->current_x > it->last_visible_x)
23653 {
23654 if (!row->reversed_p)
23655 {
23656 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23657 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23658 break;
23659 }
23660 else
23661 {
23662 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23663 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23664 break;
23665 unproduce_glyphs (it, ii + 1);
23666 ii = row->used[TEXT_AREA] - (ii + 1);
23667 }
23668 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23669 {
23670 row->used[TEXT_AREA] = ii;
23671 produce_special_glyphs (it, IT_TRUNCATION);
23672 }
23673 }
23674 produce_special_glyphs (it, IT_TRUNCATION);
23675 }
23676 row->truncated_on_right_p = true;
23677 }
23678 break;
23679 }
23680 }
23681
23682 /* Maybe insert a truncation at the left. */
23683 if (it->first_visible_x
23684 && it_charpos > 0)
23685 {
23686 if (!FRAME_WINDOW_P (it->f)
23687 || (row->reversed_p
23688 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23689 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23690 insert_left_trunc_glyphs (it);
23691 row->truncated_on_left_p = true;
23692 }
23693
23694 it->face_id = saved_face_id;
23695
23696 /* Value is number of columns displayed. */
23697 return it->hpos - hpos_at_start;
23698 }
23699
23700
23701 \f
23702 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23703 appears as an element of LIST or as the car of an element of LIST.
23704 If PROPVAL is a list, compare each element against LIST in that
23705 way, and return 1/2 if any element of PROPVAL is found in LIST.
23706 Otherwise return 0. This function cannot quit.
23707 The return value is 2 if the text is invisible but with an ellipsis
23708 and 1 if it's invisible and without an ellipsis. */
23709
23710 int
23711 invisible_prop (Lisp_Object propval, Lisp_Object list)
23712 {
23713 Lisp_Object tail, proptail;
23714
23715 for (tail = list; CONSP (tail); tail = XCDR (tail))
23716 {
23717 register Lisp_Object tem;
23718 tem = XCAR (tail);
23719 if (EQ (propval, tem))
23720 return 1;
23721 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23722 return NILP (XCDR (tem)) ? 1 : 2;
23723 }
23724
23725 if (CONSP (propval))
23726 {
23727 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23728 {
23729 Lisp_Object propelt;
23730 propelt = XCAR (proptail);
23731 for (tail = list; CONSP (tail); tail = XCDR (tail))
23732 {
23733 register Lisp_Object tem;
23734 tem = XCAR (tail);
23735 if (EQ (propelt, tem))
23736 return 1;
23737 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23738 return NILP (XCDR (tem)) ? 1 : 2;
23739 }
23740 }
23741 }
23742
23743 return 0;
23744 }
23745
23746 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23747 doc: /* Non-nil if the property makes the text invisible.
23748 POS-OR-PROP can be a marker or number, in which case it is taken to be
23749 a position in the current buffer and the value of the `invisible' property
23750 is checked; or it can be some other value, which is then presumed to be the
23751 value of the `invisible' property of the text of interest.
23752 The non-nil value returned can be t for truly invisible text or something
23753 else if the text is replaced by an ellipsis. */)
23754 (Lisp_Object pos_or_prop)
23755 {
23756 Lisp_Object prop
23757 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23758 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23759 : pos_or_prop);
23760 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23761 return (invis == 0 ? Qnil
23762 : invis == 1 ? Qt
23763 : make_number (invis));
23764 }
23765
23766 /* Calculate a width or height in pixels from a specification using
23767 the following elements:
23768
23769 SPEC ::=
23770 NUM - a (fractional) multiple of the default font width/height
23771 (NUM) - specifies exactly NUM pixels
23772 UNIT - a fixed number of pixels, see below.
23773 ELEMENT - size of a display element in pixels, see below.
23774 (NUM . SPEC) - equals NUM * SPEC
23775 (+ SPEC SPEC ...) - add pixel values
23776 (- SPEC SPEC ...) - subtract pixel values
23777 (- SPEC) - negate pixel value
23778
23779 NUM ::=
23780 INT or FLOAT - a number constant
23781 SYMBOL - use symbol's (buffer local) variable binding.
23782
23783 UNIT ::=
23784 in - pixels per inch *)
23785 mm - pixels per 1/1000 meter *)
23786 cm - pixels per 1/100 meter *)
23787 width - width of current font in pixels.
23788 height - height of current font in pixels.
23789
23790 *) using the ratio(s) defined in display-pixels-per-inch.
23791
23792 ELEMENT ::=
23793
23794 left-fringe - left fringe width in pixels
23795 right-fringe - right fringe width in pixels
23796
23797 left-margin - left margin width in pixels
23798 right-margin - right margin width in pixels
23799
23800 scroll-bar - scroll-bar area width in pixels
23801
23802 Examples:
23803
23804 Pixels corresponding to 5 inches:
23805 (5 . in)
23806
23807 Total width of non-text areas on left side of window (if scroll-bar is on left):
23808 '(space :width (+ left-fringe left-margin scroll-bar))
23809
23810 Align to first text column (in header line):
23811 '(space :align-to 0)
23812
23813 Align to middle of text area minus half the width of variable `my-image'
23814 containing a loaded image:
23815 '(space :align-to (0.5 . (- text my-image)))
23816
23817 Width of left margin minus width of 1 character in the default font:
23818 '(space :width (- left-margin 1))
23819
23820 Width of left margin minus width of 2 characters in the current font:
23821 '(space :width (- left-margin (2 . width)))
23822
23823 Center 1 character over left-margin (in header line):
23824 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23825
23826 Different ways to express width of left fringe plus left margin minus one pixel:
23827 '(space :width (- (+ left-fringe left-margin) (1)))
23828 '(space :width (+ left-fringe left-margin (- (1))))
23829 '(space :width (+ left-fringe left-margin (-1)))
23830
23831 */
23832
23833 static bool
23834 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23835 struct font *font, bool width_p, int *align_to)
23836 {
23837 double pixels;
23838
23839 # define OK_PIXELS(val) (*res = (val), true)
23840 # define OK_ALIGN_TO(val) (*align_to = (val), true)
23841
23842 if (NILP (prop))
23843 return OK_PIXELS (0);
23844
23845 eassert (FRAME_LIVE_P (it->f));
23846
23847 if (SYMBOLP (prop))
23848 {
23849 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23850 {
23851 char *unit = SSDATA (SYMBOL_NAME (prop));
23852
23853 if (unit[0] == 'i' && unit[1] == 'n')
23854 pixels = 1.0;
23855 else if (unit[0] == 'm' && unit[1] == 'm')
23856 pixels = 25.4;
23857 else if (unit[0] == 'c' && unit[1] == 'm')
23858 pixels = 2.54;
23859 else
23860 pixels = 0;
23861 if (pixels > 0)
23862 {
23863 double ppi = (width_p ? FRAME_RES_X (it->f)
23864 : FRAME_RES_Y (it->f));
23865
23866 if (ppi > 0)
23867 return OK_PIXELS (ppi / pixels);
23868 return false;
23869 }
23870 }
23871
23872 #ifdef HAVE_WINDOW_SYSTEM
23873 if (EQ (prop, Qheight))
23874 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23875 if (EQ (prop, Qwidth))
23876 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23877 #else
23878 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23879 return OK_PIXELS (1);
23880 #endif
23881
23882 if (EQ (prop, Qtext))
23883 return OK_PIXELS (width_p
23884 ? window_box_width (it->w, TEXT_AREA)
23885 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23886
23887 if (align_to && *align_to < 0)
23888 {
23889 *res = 0;
23890 if (EQ (prop, Qleft))
23891 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23892 if (EQ (prop, Qright))
23893 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23894 if (EQ (prop, Qcenter))
23895 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23896 + window_box_width (it->w, TEXT_AREA) / 2);
23897 if (EQ (prop, Qleft_fringe))
23898 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23899 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23900 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23901 if (EQ (prop, Qright_fringe))
23902 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23903 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23904 : window_box_right_offset (it->w, TEXT_AREA));
23905 if (EQ (prop, Qleft_margin))
23906 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23907 if (EQ (prop, Qright_margin))
23908 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23909 if (EQ (prop, Qscroll_bar))
23910 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23911 ? 0
23912 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23913 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23914 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23915 : 0)));
23916 }
23917 else
23918 {
23919 if (EQ (prop, Qleft_fringe))
23920 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23921 if (EQ (prop, Qright_fringe))
23922 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23923 if (EQ (prop, Qleft_margin))
23924 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23925 if (EQ (prop, Qright_margin))
23926 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23927 if (EQ (prop, Qscroll_bar))
23928 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23929 }
23930
23931 prop = buffer_local_value (prop, it->w->contents);
23932 if (EQ (prop, Qunbound))
23933 prop = Qnil;
23934 }
23935
23936 if (INTEGERP (prop) || FLOATP (prop))
23937 {
23938 int base_unit = (width_p
23939 ? FRAME_COLUMN_WIDTH (it->f)
23940 : FRAME_LINE_HEIGHT (it->f));
23941 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23942 }
23943
23944 if (CONSP (prop))
23945 {
23946 Lisp_Object car = XCAR (prop);
23947 Lisp_Object cdr = XCDR (prop);
23948
23949 if (SYMBOLP (car))
23950 {
23951 #ifdef HAVE_WINDOW_SYSTEM
23952 if (FRAME_WINDOW_P (it->f)
23953 && valid_image_p (prop))
23954 {
23955 ptrdiff_t id = lookup_image (it->f, prop);
23956 struct image *img = IMAGE_FROM_ID (it->f, id);
23957
23958 return OK_PIXELS (width_p ? img->width : img->height);
23959 }
23960 #endif
23961 if (EQ (car, Qplus) || EQ (car, Qminus))
23962 {
23963 bool first = true;
23964 double px;
23965
23966 pixels = 0;
23967 while (CONSP (cdr))
23968 {
23969 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23970 font, width_p, align_to))
23971 return false;
23972 if (first)
23973 pixels = (EQ (car, Qplus) ? px : -px), first = false;
23974 else
23975 pixels += px;
23976 cdr = XCDR (cdr);
23977 }
23978 if (EQ (car, Qminus))
23979 pixels = -pixels;
23980 return OK_PIXELS (pixels);
23981 }
23982
23983 car = buffer_local_value (car, it->w->contents);
23984 if (EQ (car, Qunbound))
23985 car = Qnil;
23986 }
23987
23988 if (INTEGERP (car) || FLOATP (car))
23989 {
23990 double fact;
23991 pixels = XFLOATINT (car);
23992 if (NILP (cdr))
23993 return OK_PIXELS (pixels);
23994 if (calc_pixel_width_or_height (&fact, it, cdr,
23995 font, width_p, align_to))
23996 return OK_PIXELS (pixels * fact);
23997 return false;
23998 }
23999
24000 return false;
24001 }
24002
24003 return false;
24004 }
24005
24006 \f
24007 /***********************************************************************
24008 Glyph Display
24009 ***********************************************************************/
24010
24011 #ifdef HAVE_WINDOW_SYSTEM
24012
24013 #ifdef GLYPH_DEBUG
24014
24015 void
24016 dump_glyph_string (struct glyph_string *s)
24017 {
24018 fprintf (stderr, "glyph string\n");
24019 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24020 s->x, s->y, s->width, s->height);
24021 fprintf (stderr, " ybase = %d\n", s->ybase);
24022 fprintf (stderr, " hl = %d\n", s->hl);
24023 fprintf (stderr, " left overhang = %d, right = %d\n",
24024 s->left_overhang, s->right_overhang);
24025 fprintf (stderr, " nchars = %d\n", s->nchars);
24026 fprintf (stderr, " extends to end of line = %d\n",
24027 s->extends_to_end_of_line_p);
24028 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24029 fprintf (stderr, " bg width = %d\n", s->background_width);
24030 }
24031
24032 #endif /* GLYPH_DEBUG */
24033
24034 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24035 of XChar2b structures for S; it can't be allocated in
24036 init_glyph_string because it must be allocated via `alloca'. W
24037 is the window on which S is drawn. ROW and AREA are the glyph row
24038 and area within the row from which S is constructed. START is the
24039 index of the first glyph structure covered by S. HL is a
24040 face-override for drawing S. */
24041
24042 #ifdef HAVE_NTGUI
24043 #define OPTIONAL_HDC(hdc) HDC hdc,
24044 #define DECLARE_HDC(hdc) HDC hdc;
24045 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24046 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24047 #endif
24048
24049 #ifndef OPTIONAL_HDC
24050 #define OPTIONAL_HDC(hdc)
24051 #define DECLARE_HDC(hdc)
24052 #define ALLOCATE_HDC(hdc, f)
24053 #define RELEASE_HDC(hdc, f)
24054 #endif
24055
24056 static void
24057 init_glyph_string (struct glyph_string *s,
24058 OPTIONAL_HDC (hdc)
24059 XChar2b *char2b, struct window *w, struct glyph_row *row,
24060 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24061 {
24062 memset (s, 0, sizeof *s);
24063 s->w = w;
24064 s->f = XFRAME (w->frame);
24065 #ifdef HAVE_NTGUI
24066 s->hdc = hdc;
24067 #endif
24068 s->display = FRAME_X_DISPLAY (s->f);
24069 s->window = FRAME_X_WINDOW (s->f);
24070 s->char2b = char2b;
24071 s->hl = hl;
24072 s->row = row;
24073 s->area = area;
24074 s->first_glyph = row->glyphs[area] + start;
24075 s->height = row->height;
24076 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24077 s->ybase = s->y + row->ascent;
24078 }
24079
24080
24081 /* Append the list of glyph strings with head H and tail T to the list
24082 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24083
24084 static void
24085 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24086 struct glyph_string *h, struct glyph_string *t)
24087 {
24088 if (h)
24089 {
24090 if (*head)
24091 (*tail)->next = h;
24092 else
24093 *head = h;
24094 h->prev = *tail;
24095 *tail = t;
24096 }
24097 }
24098
24099
24100 /* Prepend the list of glyph strings with head H and tail T to the
24101 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24102 result. */
24103
24104 static void
24105 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24106 struct glyph_string *h, struct glyph_string *t)
24107 {
24108 if (h)
24109 {
24110 if (*head)
24111 (*head)->prev = t;
24112 else
24113 *tail = t;
24114 t->next = *head;
24115 *head = h;
24116 }
24117 }
24118
24119
24120 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24121 Set *HEAD and *TAIL to the resulting list. */
24122
24123 static void
24124 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24125 struct glyph_string *s)
24126 {
24127 s->next = s->prev = NULL;
24128 append_glyph_string_lists (head, tail, s, s);
24129 }
24130
24131
24132 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24133 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24134 make sure that X resources for the face returned are allocated.
24135 Value is a pointer to a realized face that is ready for display if
24136 DISPLAY_P. */
24137
24138 static struct face *
24139 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24140 XChar2b *char2b, bool display_p)
24141 {
24142 struct face *face = FACE_FROM_ID (f, face_id);
24143 unsigned code = 0;
24144
24145 if (face->font)
24146 {
24147 code = face->font->driver->encode_char (face->font, c);
24148
24149 if (code == FONT_INVALID_CODE)
24150 code = 0;
24151 }
24152 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24153
24154 /* Make sure X resources of the face are allocated. */
24155 #ifdef HAVE_X_WINDOWS
24156 if (display_p)
24157 #endif
24158 {
24159 eassert (face != NULL);
24160 prepare_face_for_display (f, face);
24161 }
24162
24163 return face;
24164 }
24165
24166
24167 /* Get face and two-byte form of character glyph GLYPH on frame F.
24168 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24169 a pointer to a realized face that is ready for display. */
24170
24171 static struct face *
24172 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24173 XChar2b *char2b)
24174 {
24175 struct face *face;
24176 unsigned code = 0;
24177
24178 eassert (glyph->type == CHAR_GLYPH);
24179 face = FACE_FROM_ID (f, glyph->face_id);
24180
24181 /* Make sure X resources of the face are allocated. */
24182 eassert (face != NULL);
24183 prepare_face_for_display (f, face);
24184
24185 if (face->font)
24186 {
24187 if (CHAR_BYTE8_P (glyph->u.ch))
24188 code = CHAR_TO_BYTE8 (glyph->u.ch);
24189 else
24190 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24191
24192 if (code == FONT_INVALID_CODE)
24193 code = 0;
24194 }
24195
24196 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24197 return face;
24198 }
24199
24200
24201 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24202 Return true iff FONT has a glyph for C. */
24203
24204 static bool
24205 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24206 {
24207 unsigned code;
24208
24209 if (CHAR_BYTE8_P (c))
24210 code = CHAR_TO_BYTE8 (c);
24211 else
24212 code = font->driver->encode_char (font, c);
24213
24214 if (code == FONT_INVALID_CODE)
24215 return false;
24216 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24217 return true;
24218 }
24219
24220
24221 /* Fill glyph string S with composition components specified by S->cmp.
24222
24223 BASE_FACE is the base face of the composition.
24224 S->cmp_from is the index of the first component for S.
24225
24226 OVERLAPS non-zero means S should draw the foreground only, and use
24227 its physical height for clipping. See also draw_glyphs.
24228
24229 Value is the index of a component not in S. */
24230
24231 static int
24232 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24233 int overlaps)
24234 {
24235 int i;
24236 /* For all glyphs of this composition, starting at the offset
24237 S->cmp_from, until we reach the end of the definition or encounter a
24238 glyph that requires the different face, add it to S. */
24239 struct face *face;
24240
24241 eassert (s);
24242
24243 s->for_overlaps = overlaps;
24244 s->face = NULL;
24245 s->font = NULL;
24246 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24247 {
24248 int c = COMPOSITION_GLYPH (s->cmp, i);
24249
24250 /* TAB in a composition means display glyphs with padding space
24251 on the left or right. */
24252 if (c != '\t')
24253 {
24254 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24255 -1, Qnil);
24256
24257 face = get_char_face_and_encoding (s->f, c, face_id,
24258 s->char2b + i, true);
24259 if (face)
24260 {
24261 if (! s->face)
24262 {
24263 s->face = face;
24264 s->font = s->face->font;
24265 }
24266 else if (s->face != face)
24267 break;
24268 }
24269 }
24270 ++s->nchars;
24271 }
24272 s->cmp_to = i;
24273
24274 if (s->face == NULL)
24275 {
24276 s->face = base_face->ascii_face;
24277 s->font = s->face->font;
24278 }
24279
24280 /* All glyph strings for the same composition has the same width,
24281 i.e. the width set for the first component of the composition. */
24282 s->width = s->first_glyph->pixel_width;
24283
24284 /* If the specified font could not be loaded, use the frame's
24285 default font, but record the fact that we couldn't load it in
24286 the glyph string so that we can draw rectangles for the
24287 characters of the glyph string. */
24288 if (s->font == NULL)
24289 {
24290 s->font_not_found_p = true;
24291 s->font = FRAME_FONT (s->f);
24292 }
24293
24294 /* Adjust base line for subscript/superscript text. */
24295 s->ybase += s->first_glyph->voffset;
24296
24297 return s->cmp_to;
24298 }
24299
24300 static int
24301 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24302 int start, int end, int overlaps)
24303 {
24304 struct glyph *glyph, *last;
24305 Lisp_Object lgstring;
24306 int i;
24307
24308 s->for_overlaps = overlaps;
24309 glyph = s->row->glyphs[s->area] + start;
24310 last = s->row->glyphs[s->area] + end;
24311 s->cmp_id = glyph->u.cmp.id;
24312 s->cmp_from = glyph->slice.cmp.from;
24313 s->cmp_to = glyph->slice.cmp.to + 1;
24314 s->face = FACE_FROM_ID (s->f, face_id);
24315 lgstring = composition_gstring_from_id (s->cmp_id);
24316 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24317 glyph++;
24318 while (glyph < last
24319 && glyph->u.cmp.automatic
24320 && glyph->u.cmp.id == s->cmp_id
24321 && s->cmp_to == glyph->slice.cmp.from)
24322 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24323
24324 for (i = s->cmp_from; i < s->cmp_to; i++)
24325 {
24326 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24327 unsigned code = LGLYPH_CODE (lglyph);
24328
24329 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24330 }
24331 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24332 return glyph - s->row->glyphs[s->area];
24333 }
24334
24335
24336 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24337 See the comment of fill_glyph_string for arguments.
24338 Value is the index of the first glyph not in S. */
24339
24340
24341 static int
24342 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24343 int start, int end, int overlaps)
24344 {
24345 struct glyph *glyph, *last;
24346 int voffset;
24347
24348 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24349 s->for_overlaps = overlaps;
24350 glyph = s->row->glyphs[s->area] + start;
24351 last = s->row->glyphs[s->area] + end;
24352 voffset = glyph->voffset;
24353 s->face = FACE_FROM_ID (s->f, face_id);
24354 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24355 s->nchars = 1;
24356 s->width = glyph->pixel_width;
24357 glyph++;
24358 while (glyph < last
24359 && glyph->type == GLYPHLESS_GLYPH
24360 && glyph->voffset == voffset
24361 && glyph->face_id == face_id)
24362 {
24363 s->nchars++;
24364 s->width += glyph->pixel_width;
24365 glyph++;
24366 }
24367 s->ybase += voffset;
24368 return glyph - s->row->glyphs[s->area];
24369 }
24370
24371
24372 /* Fill glyph string S from a sequence of character glyphs.
24373
24374 FACE_ID is the face id of the string. START is the index of the
24375 first glyph to consider, END is the index of the last + 1.
24376 OVERLAPS non-zero means S should draw the foreground only, and use
24377 its physical height for clipping. See also draw_glyphs.
24378
24379 Value is the index of the first glyph not in S. */
24380
24381 static int
24382 fill_glyph_string (struct glyph_string *s, int face_id,
24383 int start, int end, int overlaps)
24384 {
24385 struct glyph *glyph, *last;
24386 int voffset;
24387 bool glyph_not_available_p;
24388
24389 eassert (s->f == XFRAME (s->w->frame));
24390 eassert (s->nchars == 0);
24391 eassert (start >= 0 && end > start);
24392
24393 s->for_overlaps = overlaps;
24394 glyph = s->row->glyphs[s->area] + start;
24395 last = s->row->glyphs[s->area] + end;
24396 voffset = glyph->voffset;
24397 s->padding_p = glyph->padding_p;
24398 glyph_not_available_p = glyph->glyph_not_available_p;
24399
24400 while (glyph < last
24401 && glyph->type == CHAR_GLYPH
24402 && glyph->voffset == voffset
24403 /* Same face id implies same font, nowadays. */
24404 && glyph->face_id == face_id
24405 && glyph->glyph_not_available_p == glyph_not_available_p)
24406 {
24407 s->face = get_glyph_face_and_encoding (s->f, glyph,
24408 s->char2b + s->nchars);
24409 ++s->nchars;
24410 eassert (s->nchars <= end - start);
24411 s->width += glyph->pixel_width;
24412 if (glyph++->padding_p != s->padding_p)
24413 break;
24414 }
24415
24416 s->font = s->face->font;
24417
24418 /* If the specified font could not be loaded, use the frame's font,
24419 but record the fact that we couldn't load it in
24420 S->font_not_found_p so that we can draw rectangles for the
24421 characters of the glyph string. */
24422 if (s->font == NULL || glyph_not_available_p)
24423 {
24424 s->font_not_found_p = true;
24425 s->font = FRAME_FONT (s->f);
24426 }
24427
24428 /* Adjust base line for subscript/superscript text. */
24429 s->ybase += voffset;
24430
24431 eassert (s->face && s->face->gc);
24432 return glyph - s->row->glyphs[s->area];
24433 }
24434
24435
24436 /* Fill glyph string S from image glyph S->first_glyph. */
24437
24438 static void
24439 fill_image_glyph_string (struct glyph_string *s)
24440 {
24441 eassert (s->first_glyph->type == IMAGE_GLYPH);
24442 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24443 eassert (s->img);
24444 s->slice = s->first_glyph->slice.img;
24445 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24446 s->font = s->face->font;
24447 s->width = s->first_glyph->pixel_width;
24448
24449 /* Adjust base line for subscript/superscript text. */
24450 s->ybase += s->first_glyph->voffset;
24451 }
24452
24453
24454 /* Fill glyph string S from a sequence of stretch glyphs.
24455
24456 START is the index of the first glyph to consider,
24457 END is the index of the last + 1.
24458
24459 Value is the index of the first glyph not in S. */
24460
24461 static int
24462 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24463 {
24464 struct glyph *glyph, *last;
24465 int voffset, face_id;
24466
24467 eassert (s->first_glyph->type == STRETCH_GLYPH);
24468
24469 glyph = s->row->glyphs[s->area] + start;
24470 last = s->row->glyphs[s->area] + end;
24471 face_id = glyph->face_id;
24472 s->face = FACE_FROM_ID (s->f, face_id);
24473 s->font = s->face->font;
24474 s->width = glyph->pixel_width;
24475 s->nchars = 1;
24476 voffset = glyph->voffset;
24477
24478 for (++glyph;
24479 (glyph < last
24480 && glyph->type == STRETCH_GLYPH
24481 && glyph->voffset == voffset
24482 && glyph->face_id == face_id);
24483 ++glyph)
24484 s->width += glyph->pixel_width;
24485
24486 /* Adjust base line for subscript/superscript text. */
24487 s->ybase += voffset;
24488
24489 /* The case that face->gc == 0 is handled when drawing the glyph
24490 string by calling prepare_face_for_display. */
24491 eassert (s->face);
24492 return glyph - s->row->glyphs[s->area];
24493 }
24494
24495 static struct font_metrics *
24496 get_per_char_metric (struct font *font, XChar2b *char2b)
24497 {
24498 static struct font_metrics metrics;
24499 unsigned code;
24500
24501 if (! font)
24502 return NULL;
24503 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24504 if (code == FONT_INVALID_CODE)
24505 return NULL;
24506 font->driver->text_extents (font, &code, 1, &metrics);
24507 return &metrics;
24508 }
24509
24510 /* EXPORT for RIF:
24511 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24512 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24513 assumed to be zero. */
24514
24515 void
24516 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24517 {
24518 *left = *right = 0;
24519
24520 if (glyph->type == CHAR_GLYPH)
24521 {
24522 XChar2b char2b;
24523 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
24524 if (face->font)
24525 {
24526 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
24527 if (pcm)
24528 {
24529 if (pcm->rbearing > pcm->width)
24530 *right = pcm->rbearing - pcm->width;
24531 if (pcm->lbearing < 0)
24532 *left = -pcm->lbearing;
24533 }
24534 }
24535 }
24536 else if (glyph->type == COMPOSITE_GLYPH)
24537 {
24538 if (! glyph->u.cmp.automatic)
24539 {
24540 struct composition *cmp = composition_table[glyph->u.cmp.id];
24541
24542 if (cmp->rbearing > cmp->pixel_width)
24543 *right = cmp->rbearing - cmp->pixel_width;
24544 if (cmp->lbearing < 0)
24545 *left = - cmp->lbearing;
24546 }
24547 else
24548 {
24549 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24550 struct font_metrics metrics;
24551
24552 composition_gstring_width (gstring, glyph->slice.cmp.from,
24553 glyph->slice.cmp.to + 1, &metrics);
24554 if (metrics.rbearing > metrics.width)
24555 *right = metrics.rbearing - metrics.width;
24556 if (metrics.lbearing < 0)
24557 *left = - metrics.lbearing;
24558 }
24559 }
24560 }
24561
24562
24563 /* Return the index of the first glyph preceding glyph string S that
24564 is overwritten by S because of S's left overhang. Value is -1
24565 if no glyphs are overwritten. */
24566
24567 static int
24568 left_overwritten (struct glyph_string *s)
24569 {
24570 int k;
24571
24572 if (s->left_overhang)
24573 {
24574 int x = 0, i;
24575 struct glyph *glyphs = s->row->glyphs[s->area];
24576 int first = s->first_glyph - glyphs;
24577
24578 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24579 x -= glyphs[i].pixel_width;
24580
24581 k = i + 1;
24582 }
24583 else
24584 k = -1;
24585
24586 return k;
24587 }
24588
24589
24590 /* Return the index of the first glyph preceding glyph string S that
24591 is overwriting S because of its right overhang. Value is -1 if no
24592 glyph in front of S overwrites S. */
24593
24594 static int
24595 left_overwriting (struct glyph_string *s)
24596 {
24597 int i, k, x;
24598 struct glyph *glyphs = s->row->glyphs[s->area];
24599 int first = s->first_glyph - glyphs;
24600
24601 k = -1;
24602 x = 0;
24603 for (i = first - 1; i >= 0; --i)
24604 {
24605 int left, right;
24606 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24607 if (x + right > 0)
24608 k = i;
24609 x -= glyphs[i].pixel_width;
24610 }
24611
24612 return k;
24613 }
24614
24615
24616 /* Return the index of the last glyph following glyph string S that is
24617 overwritten by S because of S's right overhang. Value is -1 if
24618 no such glyph is found. */
24619
24620 static int
24621 right_overwritten (struct glyph_string *s)
24622 {
24623 int k = -1;
24624
24625 if (s->right_overhang)
24626 {
24627 int x = 0, i;
24628 struct glyph *glyphs = s->row->glyphs[s->area];
24629 int first = (s->first_glyph - glyphs
24630 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24631 int end = s->row->used[s->area];
24632
24633 for (i = first; i < end && s->right_overhang > x; ++i)
24634 x += glyphs[i].pixel_width;
24635
24636 k = i;
24637 }
24638
24639 return k;
24640 }
24641
24642
24643 /* Return the index of the last glyph following glyph string S that
24644 overwrites S because of its left overhang. Value is negative
24645 if no such glyph is found. */
24646
24647 static int
24648 right_overwriting (struct glyph_string *s)
24649 {
24650 int i, k, x;
24651 int end = s->row->used[s->area];
24652 struct glyph *glyphs = s->row->glyphs[s->area];
24653 int first = (s->first_glyph - glyphs
24654 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24655
24656 k = -1;
24657 x = 0;
24658 for (i = first; i < end; ++i)
24659 {
24660 int left, right;
24661 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24662 if (x - left < 0)
24663 k = i;
24664 x += glyphs[i].pixel_width;
24665 }
24666
24667 return k;
24668 }
24669
24670
24671 /* Set background width of glyph string S. START is the index of the
24672 first glyph following S. LAST_X is the right-most x-position + 1
24673 in the drawing area. */
24674
24675 static void
24676 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24677 {
24678 /* If the face of this glyph string has to be drawn to the end of
24679 the drawing area, set S->extends_to_end_of_line_p. */
24680
24681 if (start == s->row->used[s->area]
24682 && ((s->row->fill_line_p
24683 && (s->hl == DRAW_NORMAL_TEXT
24684 || s->hl == DRAW_IMAGE_RAISED
24685 || s->hl == DRAW_IMAGE_SUNKEN))
24686 || s->hl == DRAW_MOUSE_FACE))
24687 s->extends_to_end_of_line_p = true;
24688
24689 /* If S extends its face to the end of the line, set its
24690 background_width to the distance to the right edge of the drawing
24691 area. */
24692 if (s->extends_to_end_of_line_p)
24693 s->background_width = last_x - s->x + 1;
24694 else
24695 s->background_width = s->width;
24696 }
24697
24698
24699 /* Compute overhangs and x-positions for glyph string S and its
24700 predecessors, or successors. X is the starting x-position for S.
24701 BACKWARD_P means process predecessors. */
24702
24703 static void
24704 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
24705 {
24706 if (backward_p)
24707 {
24708 while (s)
24709 {
24710 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24711 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24712 x -= s->width;
24713 s->x = x;
24714 s = s->prev;
24715 }
24716 }
24717 else
24718 {
24719 while (s)
24720 {
24721 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24722 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24723 s->x = x;
24724 x += s->width;
24725 s = s->next;
24726 }
24727 }
24728 }
24729
24730
24731
24732 /* The following macros are only called from draw_glyphs below.
24733 They reference the following parameters of that function directly:
24734 `w', `row', `area', and `overlap_p'
24735 as well as the following local variables:
24736 `s', `f', and `hdc' (in W32) */
24737
24738 #ifdef HAVE_NTGUI
24739 /* On W32, silently add local `hdc' variable to argument list of
24740 init_glyph_string. */
24741 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24742 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24743 #else
24744 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24745 init_glyph_string (s, char2b, w, row, area, start, hl)
24746 #endif
24747
24748 /* Add a glyph string for a stretch glyph to the list of strings
24749 between HEAD and TAIL. START is the index of the stretch glyph in
24750 row area AREA of glyph row ROW. END is the index of the last glyph
24751 in that glyph row area. X is the current output position assigned
24752 to the new glyph string constructed. HL overrides that face of the
24753 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24754 is the right-most x-position of the drawing area. */
24755
24756 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24757 and below -- keep them on one line. */
24758 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24759 do \
24760 { \
24761 s = alloca (sizeof *s); \
24762 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24763 START = fill_stretch_glyph_string (s, START, END); \
24764 append_glyph_string (&HEAD, &TAIL, s); \
24765 s->x = (X); \
24766 } \
24767 while (false)
24768
24769
24770 /* Add a glyph string for an image glyph to the list of strings
24771 between HEAD and TAIL. START is the index of the image glyph in
24772 row area AREA of glyph row ROW. END is the index of the last glyph
24773 in that glyph row area. X is the current output position assigned
24774 to the new glyph string constructed. HL overrides that face of the
24775 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24776 is the right-most x-position of the drawing area. */
24777
24778 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24779 do \
24780 { \
24781 s = alloca (sizeof *s); \
24782 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24783 fill_image_glyph_string (s); \
24784 append_glyph_string (&HEAD, &TAIL, s); \
24785 ++START; \
24786 s->x = (X); \
24787 } \
24788 while (false)
24789
24790
24791 /* Add a glyph string for a sequence of character glyphs to the list
24792 of strings between HEAD and TAIL. START is the index of the first
24793 glyph in row area AREA of glyph row ROW that is part of the new
24794 glyph string. END is the index of the last glyph in that glyph row
24795 area. X is the current output position assigned to the new glyph
24796 string constructed. HL overrides that face of the glyph; e.g. it
24797 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24798 right-most x-position of the drawing area. */
24799
24800 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24801 do \
24802 { \
24803 int face_id; \
24804 XChar2b *char2b; \
24805 \
24806 face_id = (row)->glyphs[area][START].face_id; \
24807 \
24808 s = alloca (sizeof *s); \
24809 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
24810 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24811 append_glyph_string (&HEAD, &TAIL, s); \
24812 s->x = (X); \
24813 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24814 } \
24815 while (false)
24816
24817
24818 /* Add a glyph string for a composite sequence to the list of strings
24819 between HEAD and TAIL. START is the index of the first glyph in
24820 row area AREA of glyph row ROW that is part of the new glyph
24821 string. END is the index of the last glyph in that glyph row area.
24822 X is the current output position assigned to the new glyph string
24823 constructed. HL overrides that face of the glyph; e.g. it is
24824 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24825 x-position of the drawing area. */
24826
24827 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24828 do { \
24829 int face_id = (row)->glyphs[area][START].face_id; \
24830 struct face *base_face = FACE_FROM_ID (f, face_id); \
24831 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24832 struct composition *cmp = composition_table[cmp_id]; \
24833 XChar2b *char2b; \
24834 struct glyph_string *first_s = NULL; \
24835 int n; \
24836 \
24837 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
24838 \
24839 /* Make glyph_strings for each glyph sequence that is drawable by \
24840 the same face, and append them to HEAD/TAIL. */ \
24841 for (n = 0; n < cmp->glyph_len;) \
24842 { \
24843 s = alloca (sizeof *s); \
24844 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24845 append_glyph_string (&(HEAD), &(TAIL), s); \
24846 s->cmp = cmp; \
24847 s->cmp_from = n; \
24848 s->x = (X); \
24849 if (n == 0) \
24850 first_s = s; \
24851 n = fill_composite_glyph_string (s, base_face, overlaps); \
24852 } \
24853 \
24854 ++START; \
24855 s = first_s; \
24856 } while (false)
24857
24858
24859 /* Add a glyph string for a glyph-string sequence to the list of strings
24860 between HEAD and TAIL. */
24861
24862 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24863 do { \
24864 int face_id; \
24865 XChar2b *char2b; \
24866 Lisp_Object gstring; \
24867 \
24868 face_id = (row)->glyphs[area][START].face_id; \
24869 gstring = (composition_gstring_from_id \
24870 ((row)->glyphs[area][START].u.cmp.id)); \
24871 s = alloca (sizeof *s); \
24872 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
24873 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24874 append_glyph_string (&(HEAD), &(TAIL), s); \
24875 s->x = (X); \
24876 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24877 } while (false)
24878
24879
24880 /* Add a glyph string for a sequence of glyphless character's glyphs
24881 to the list of strings between HEAD and TAIL. The meanings of
24882 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24883
24884 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24885 do \
24886 { \
24887 int face_id; \
24888 \
24889 face_id = (row)->glyphs[area][START].face_id; \
24890 \
24891 s = alloca (sizeof *s); \
24892 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24893 append_glyph_string (&HEAD, &TAIL, s); \
24894 s->x = (X); \
24895 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24896 overlaps); \
24897 } \
24898 while (false)
24899
24900
24901 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24902 of AREA of glyph row ROW on window W between indices START and END.
24903 HL overrides the face for drawing glyph strings, e.g. it is
24904 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24905 x-positions of the drawing area.
24906
24907 This is an ugly monster macro construct because we must use alloca
24908 to allocate glyph strings (because draw_glyphs can be called
24909 asynchronously). */
24910
24911 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24912 do \
24913 { \
24914 HEAD = TAIL = NULL; \
24915 while (START < END) \
24916 { \
24917 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24918 switch (first_glyph->type) \
24919 { \
24920 case CHAR_GLYPH: \
24921 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24922 HL, X, LAST_X); \
24923 break; \
24924 \
24925 case COMPOSITE_GLYPH: \
24926 if (first_glyph->u.cmp.automatic) \
24927 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24928 HL, X, LAST_X); \
24929 else \
24930 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24931 HL, X, LAST_X); \
24932 break; \
24933 \
24934 case STRETCH_GLYPH: \
24935 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24936 HL, X, LAST_X); \
24937 break; \
24938 \
24939 case IMAGE_GLYPH: \
24940 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24941 HL, X, LAST_X); \
24942 break; \
24943 \
24944 case GLYPHLESS_GLYPH: \
24945 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24946 HL, X, LAST_X); \
24947 break; \
24948 \
24949 default: \
24950 emacs_abort (); \
24951 } \
24952 \
24953 if (s) \
24954 { \
24955 set_glyph_string_background_width (s, START, LAST_X); \
24956 (X) += s->width; \
24957 } \
24958 } \
24959 } while (false)
24960
24961
24962 /* Draw glyphs between START and END in AREA of ROW on window W,
24963 starting at x-position X. X is relative to AREA in W. HL is a
24964 face-override with the following meaning:
24965
24966 DRAW_NORMAL_TEXT draw normally
24967 DRAW_CURSOR draw in cursor face
24968 DRAW_MOUSE_FACE draw in mouse face.
24969 DRAW_INVERSE_VIDEO draw in mode line face
24970 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24971 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24972
24973 If OVERLAPS is non-zero, draw only the foreground of characters and
24974 clip to the physical height of ROW. Non-zero value also defines
24975 the overlapping part to be drawn:
24976
24977 OVERLAPS_PRED overlap with preceding rows
24978 OVERLAPS_SUCC overlap with succeeding rows
24979 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24980 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24981
24982 Value is the x-position reached, relative to AREA of W. */
24983
24984 static int
24985 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24986 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24987 enum draw_glyphs_face hl, int overlaps)
24988 {
24989 struct glyph_string *head, *tail;
24990 struct glyph_string *s;
24991 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24992 int i, j, x_reached, last_x, area_left = 0;
24993 struct frame *f = XFRAME (WINDOW_FRAME (w));
24994 DECLARE_HDC (hdc);
24995
24996 ALLOCATE_HDC (hdc, f);
24997
24998 /* Let's rather be paranoid than getting a SEGV. */
24999 end = min (end, row->used[area]);
25000 start = clip_to_bounds (0, start, end);
25001
25002 /* Translate X to frame coordinates. Set last_x to the right
25003 end of the drawing area. */
25004 if (row->full_width_p)
25005 {
25006 /* X is relative to the left edge of W, without scroll bars
25007 or fringes. */
25008 area_left = WINDOW_LEFT_EDGE_X (w);
25009 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25010 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25011 }
25012 else
25013 {
25014 area_left = window_box_left (w, area);
25015 last_x = area_left + window_box_width (w, area);
25016 }
25017 x += area_left;
25018
25019 /* Build a doubly-linked list of glyph_string structures between
25020 head and tail from what we have to draw. Note that the macro
25021 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25022 the reason we use a separate variable `i'. */
25023 i = start;
25024 USE_SAFE_ALLOCA;
25025 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25026 if (tail)
25027 x_reached = tail->x + tail->background_width;
25028 else
25029 x_reached = x;
25030
25031 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25032 the row, redraw some glyphs in front or following the glyph
25033 strings built above. */
25034 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25035 {
25036 struct glyph_string *h, *t;
25037 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25038 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25039 bool check_mouse_face = false;
25040 int dummy_x = 0;
25041
25042 /* If mouse highlighting is on, we may need to draw adjacent
25043 glyphs using mouse-face highlighting. */
25044 if (area == TEXT_AREA && row->mouse_face_p
25045 && hlinfo->mouse_face_beg_row >= 0
25046 && hlinfo->mouse_face_end_row >= 0)
25047 {
25048 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25049
25050 if (row_vpos >= hlinfo->mouse_face_beg_row
25051 && row_vpos <= hlinfo->mouse_face_end_row)
25052 {
25053 check_mouse_face = true;
25054 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25055 ? hlinfo->mouse_face_beg_col : 0;
25056 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25057 ? hlinfo->mouse_face_end_col
25058 : row->used[TEXT_AREA];
25059 }
25060 }
25061
25062 /* Compute overhangs for all glyph strings. */
25063 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25064 for (s = head; s; s = s->next)
25065 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25066
25067 /* Prepend glyph strings for glyphs in front of the first glyph
25068 string that are overwritten because of the first glyph
25069 string's left overhang. The background of all strings
25070 prepended must be drawn because the first glyph string
25071 draws over it. */
25072 i = left_overwritten (head);
25073 if (i >= 0)
25074 {
25075 enum draw_glyphs_face overlap_hl;
25076
25077 /* If this row contains mouse highlighting, attempt to draw
25078 the overlapped glyphs with the correct highlight. This
25079 code fails if the overlap encompasses more than one glyph
25080 and mouse-highlight spans only some of these glyphs.
25081 However, making it work perfectly involves a lot more
25082 code, and I don't know if the pathological case occurs in
25083 practice, so we'll stick to this for now. --- cyd */
25084 if (check_mouse_face
25085 && mouse_beg_col < start && mouse_end_col > i)
25086 overlap_hl = DRAW_MOUSE_FACE;
25087 else
25088 overlap_hl = DRAW_NORMAL_TEXT;
25089
25090 if (hl != overlap_hl)
25091 clip_head = head;
25092 j = i;
25093 BUILD_GLYPH_STRINGS (j, start, h, t,
25094 overlap_hl, dummy_x, last_x);
25095 start = i;
25096 compute_overhangs_and_x (t, head->x, true);
25097 prepend_glyph_string_lists (&head, &tail, h, t);
25098 if (clip_head == NULL)
25099 clip_head = head;
25100 }
25101
25102 /* Prepend glyph strings for glyphs in front of the first glyph
25103 string that overwrite that glyph string because of their
25104 right overhang. For these strings, only the foreground must
25105 be drawn, because it draws over the glyph string at `head'.
25106 The background must not be drawn because this would overwrite
25107 right overhangs of preceding glyphs for which no glyph
25108 strings exist. */
25109 i = left_overwriting (head);
25110 if (i >= 0)
25111 {
25112 enum draw_glyphs_face overlap_hl;
25113
25114 if (check_mouse_face
25115 && mouse_beg_col < start && mouse_end_col > i)
25116 overlap_hl = DRAW_MOUSE_FACE;
25117 else
25118 overlap_hl = DRAW_NORMAL_TEXT;
25119
25120 if (hl == overlap_hl || clip_head == NULL)
25121 clip_head = head;
25122 BUILD_GLYPH_STRINGS (i, start, h, t,
25123 overlap_hl, dummy_x, last_x);
25124 for (s = h; s; s = s->next)
25125 s->background_filled_p = true;
25126 compute_overhangs_and_x (t, head->x, true);
25127 prepend_glyph_string_lists (&head, &tail, h, t);
25128 }
25129
25130 /* Append glyphs strings for glyphs following the last glyph
25131 string tail that are overwritten by tail. The background of
25132 these strings has to be drawn because tail's foreground draws
25133 over it. */
25134 i = right_overwritten (tail);
25135 if (i >= 0)
25136 {
25137 enum draw_glyphs_face overlap_hl;
25138
25139 if (check_mouse_face
25140 && mouse_beg_col < i && mouse_end_col > end)
25141 overlap_hl = DRAW_MOUSE_FACE;
25142 else
25143 overlap_hl = DRAW_NORMAL_TEXT;
25144
25145 if (hl != overlap_hl)
25146 clip_tail = tail;
25147 BUILD_GLYPH_STRINGS (end, i, h, t,
25148 overlap_hl, x, last_x);
25149 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25150 we don't have `end = i;' here. */
25151 compute_overhangs_and_x (h, tail->x + tail->width, false);
25152 append_glyph_string_lists (&head, &tail, h, t);
25153 if (clip_tail == NULL)
25154 clip_tail = tail;
25155 }
25156
25157 /* Append glyph strings for glyphs following the last glyph
25158 string tail that overwrite tail. The foreground of such
25159 glyphs has to be drawn because it writes into the background
25160 of tail. The background must not be drawn because it could
25161 paint over the foreground of following glyphs. */
25162 i = right_overwriting (tail);
25163 if (i >= 0)
25164 {
25165 enum draw_glyphs_face overlap_hl;
25166 if (check_mouse_face
25167 && mouse_beg_col < i && mouse_end_col > end)
25168 overlap_hl = DRAW_MOUSE_FACE;
25169 else
25170 overlap_hl = DRAW_NORMAL_TEXT;
25171
25172 if (hl == overlap_hl || clip_tail == NULL)
25173 clip_tail = tail;
25174 i++; /* We must include the Ith glyph. */
25175 BUILD_GLYPH_STRINGS (end, i, h, t,
25176 overlap_hl, x, last_x);
25177 for (s = h; s; s = s->next)
25178 s->background_filled_p = true;
25179 compute_overhangs_and_x (h, tail->x + tail->width, false);
25180 append_glyph_string_lists (&head, &tail, h, t);
25181 }
25182 if (clip_head || clip_tail)
25183 for (s = head; s; s = s->next)
25184 {
25185 s->clip_head = clip_head;
25186 s->clip_tail = clip_tail;
25187 }
25188 }
25189
25190 /* Draw all strings. */
25191 for (s = head; s; s = s->next)
25192 FRAME_RIF (f)->draw_glyph_string (s);
25193
25194 #ifndef HAVE_NS
25195 /* When focus a sole frame and move horizontally, this clears on_p
25196 causing a failure to erase prev cursor position. */
25197 if (area == TEXT_AREA
25198 && !row->full_width_p
25199 /* When drawing overlapping rows, only the glyph strings'
25200 foreground is drawn, which doesn't erase a cursor
25201 completely. */
25202 && !overlaps)
25203 {
25204 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25205 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25206 : (tail ? tail->x + tail->background_width : x));
25207 x0 -= area_left;
25208 x1 -= area_left;
25209
25210 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25211 row->y, MATRIX_ROW_BOTTOM_Y (row));
25212 }
25213 #endif
25214
25215 /* Value is the x-position up to which drawn, relative to AREA of W.
25216 This doesn't include parts drawn because of overhangs. */
25217 if (row->full_width_p)
25218 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25219 else
25220 x_reached -= area_left;
25221
25222 RELEASE_HDC (hdc, f);
25223
25224 SAFE_FREE ();
25225 return x_reached;
25226 }
25227
25228 /* Expand row matrix if too narrow. Don't expand if area
25229 is not present. */
25230
25231 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25232 { \
25233 if (!it->f->fonts_changed \
25234 && (it->glyph_row->glyphs[area] \
25235 < it->glyph_row->glyphs[area + 1])) \
25236 { \
25237 it->w->ncols_scale_factor++; \
25238 it->f->fonts_changed = true; \
25239 } \
25240 }
25241
25242 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25243 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25244
25245 static void
25246 append_glyph (struct it *it)
25247 {
25248 struct glyph *glyph;
25249 enum glyph_row_area area = it->area;
25250
25251 eassert (it->glyph_row);
25252 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25253
25254 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25255 if (glyph < it->glyph_row->glyphs[area + 1])
25256 {
25257 /* If the glyph row is reversed, we need to prepend the glyph
25258 rather than append it. */
25259 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25260 {
25261 struct glyph *g;
25262
25263 /* Make room for the additional glyph. */
25264 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25265 g[1] = *g;
25266 glyph = it->glyph_row->glyphs[area];
25267 }
25268 glyph->charpos = CHARPOS (it->position);
25269 glyph->object = it->object;
25270 if (it->pixel_width > 0)
25271 {
25272 glyph->pixel_width = it->pixel_width;
25273 glyph->padding_p = false;
25274 }
25275 else
25276 {
25277 /* Assure at least 1-pixel width. Otherwise, cursor can't
25278 be displayed correctly. */
25279 glyph->pixel_width = 1;
25280 glyph->padding_p = true;
25281 }
25282 glyph->ascent = it->ascent;
25283 glyph->descent = it->descent;
25284 glyph->voffset = it->voffset;
25285 glyph->type = CHAR_GLYPH;
25286 glyph->avoid_cursor_p = it->avoid_cursor_p;
25287 glyph->multibyte_p = it->multibyte_p;
25288 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25289 {
25290 /* In R2L rows, the left and the right box edges need to be
25291 drawn in reverse direction. */
25292 glyph->right_box_line_p = it->start_of_box_run_p;
25293 glyph->left_box_line_p = it->end_of_box_run_p;
25294 }
25295 else
25296 {
25297 glyph->left_box_line_p = it->start_of_box_run_p;
25298 glyph->right_box_line_p = it->end_of_box_run_p;
25299 }
25300 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25301 || it->phys_descent > it->descent);
25302 glyph->glyph_not_available_p = it->glyph_not_available_p;
25303 glyph->face_id = it->face_id;
25304 glyph->u.ch = it->char_to_display;
25305 glyph->slice.img = null_glyph_slice;
25306 glyph->font_type = FONT_TYPE_UNKNOWN;
25307 if (it->bidi_p)
25308 {
25309 glyph->resolved_level = it->bidi_it.resolved_level;
25310 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25311 glyph->bidi_type = it->bidi_it.type;
25312 }
25313 else
25314 {
25315 glyph->resolved_level = 0;
25316 glyph->bidi_type = UNKNOWN_BT;
25317 }
25318 ++it->glyph_row->used[area];
25319 }
25320 else
25321 IT_EXPAND_MATRIX_WIDTH (it, area);
25322 }
25323
25324 /* Store one glyph for the composition IT->cmp_it.id in
25325 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25326 non-null. */
25327
25328 static void
25329 append_composite_glyph (struct it *it)
25330 {
25331 struct glyph *glyph;
25332 enum glyph_row_area area = it->area;
25333
25334 eassert (it->glyph_row);
25335
25336 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25337 if (glyph < it->glyph_row->glyphs[area + 1])
25338 {
25339 /* If the glyph row is reversed, we need to prepend the glyph
25340 rather than append it. */
25341 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25342 {
25343 struct glyph *g;
25344
25345 /* Make room for the new glyph. */
25346 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25347 g[1] = *g;
25348 glyph = it->glyph_row->glyphs[it->area];
25349 }
25350 glyph->charpos = it->cmp_it.charpos;
25351 glyph->object = it->object;
25352 glyph->pixel_width = it->pixel_width;
25353 glyph->ascent = it->ascent;
25354 glyph->descent = it->descent;
25355 glyph->voffset = it->voffset;
25356 glyph->type = COMPOSITE_GLYPH;
25357 if (it->cmp_it.ch < 0)
25358 {
25359 glyph->u.cmp.automatic = false;
25360 glyph->u.cmp.id = it->cmp_it.id;
25361 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25362 }
25363 else
25364 {
25365 glyph->u.cmp.automatic = true;
25366 glyph->u.cmp.id = it->cmp_it.id;
25367 glyph->slice.cmp.from = it->cmp_it.from;
25368 glyph->slice.cmp.to = it->cmp_it.to - 1;
25369 }
25370 glyph->avoid_cursor_p = it->avoid_cursor_p;
25371 glyph->multibyte_p = it->multibyte_p;
25372 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25373 {
25374 /* In R2L rows, the left and the right box edges need to be
25375 drawn in reverse direction. */
25376 glyph->right_box_line_p = it->start_of_box_run_p;
25377 glyph->left_box_line_p = it->end_of_box_run_p;
25378 }
25379 else
25380 {
25381 glyph->left_box_line_p = it->start_of_box_run_p;
25382 glyph->right_box_line_p = it->end_of_box_run_p;
25383 }
25384 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25385 || it->phys_descent > it->descent);
25386 glyph->padding_p = false;
25387 glyph->glyph_not_available_p = false;
25388 glyph->face_id = it->face_id;
25389 glyph->font_type = FONT_TYPE_UNKNOWN;
25390 if (it->bidi_p)
25391 {
25392 glyph->resolved_level = it->bidi_it.resolved_level;
25393 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25394 glyph->bidi_type = it->bidi_it.type;
25395 }
25396 ++it->glyph_row->used[area];
25397 }
25398 else
25399 IT_EXPAND_MATRIX_WIDTH (it, area);
25400 }
25401
25402
25403 /* Change IT->ascent and IT->height according to the setting of
25404 IT->voffset. */
25405
25406 static void
25407 take_vertical_position_into_account (struct it *it)
25408 {
25409 if (it->voffset)
25410 {
25411 if (it->voffset < 0)
25412 /* Increase the ascent so that we can display the text higher
25413 in the line. */
25414 it->ascent -= it->voffset;
25415 else
25416 /* Increase the descent so that we can display the text lower
25417 in the line. */
25418 it->descent += it->voffset;
25419 }
25420 }
25421
25422
25423 /* Produce glyphs/get display metrics for the image IT is loaded with.
25424 See the description of struct display_iterator in dispextern.h for
25425 an overview of struct display_iterator. */
25426
25427 static void
25428 produce_image_glyph (struct it *it)
25429 {
25430 struct image *img;
25431 struct face *face;
25432 int glyph_ascent, crop;
25433 struct glyph_slice slice;
25434
25435 eassert (it->what == IT_IMAGE);
25436
25437 face = FACE_FROM_ID (it->f, it->face_id);
25438 eassert (face);
25439 /* Make sure X resources of the face is loaded. */
25440 prepare_face_for_display (it->f, face);
25441
25442 if (it->image_id < 0)
25443 {
25444 /* Fringe bitmap. */
25445 it->ascent = it->phys_ascent = 0;
25446 it->descent = it->phys_descent = 0;
25447 it->pixel_width = 0;
25448 it->nglyphs = 0;
25449 return;
25450 }
25451
25452 img = IMAGE_FROM_ID (it->f, it->image_id);
25453 eassert (img);
25454 /* Make sure X resources of the image is loaded. */
25455 prepare_image_for_display (it->f, img);
25456
25457 slice.x = slice.y = 0;
25458 slice.width = img->width;
25459 slice.height = img->height;
25460
25461 if (INTEGERP (it->slice.x))
25462 slice.x = XINT (it->slice.x);
25463 else if (FLOATP (it->slice.x))
25464 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25465
25466 if (INTEGERP (it->slice.y))
25467 slice.y = XINT (it->slice.y);
25468 else if (FLOATP (it->slice.y))
25469 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25470
25471 if (INTEGERP (it->slice.width))
25472 slice.width = XINT (it->slice.width);
25473 else if (FLOATP (it->slice.width))
25474 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25475
25476 if (INTEGERP (it->slice.height))
25477 slice.height = XINT (it->slice.height);
25478 else if (FLOATP (it->slice.height))
25479 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25480
25481 if (slice.x >= img->width)
25482 slice.x = img->width;
25483 if (slice.y >= img->height)
25484 slice.y = img->height;
25485 if (slice.x + slice.width >= img->width)
25486 slice.width = img->width - slice.x;
25487 if (slice.y + slice.height > img->height)
25488 slice.height = img->height - slice.y;
25489
25490 if (slice.width == 0 || slice.height == 0)
25491 return;
25492
25493 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25494
25495 it->descent = slice.height - glyph_ascent;
25496 if (slice.y == 0)
25497 it->descent += img->vmargin;
25498 if (slice.y + slice.height == img->height)
25499 it->descent += img->vmargin;
25500 it->phys_descent = it->descent;
25501
25502 it->pixel_width = slice.width;
25503 if (slice.x == 0)
25504 it->pixel_width += img->hmargin;
25505 if (slice.x + slice.width == img->width)
25506 it->pixel_width += img->hmargin;
25507
25508 /* It's quite possible for images to have an ascent greater than
25509 their height, so don't get confused in that case. */
25510 if (it->descent < 0)
25511 it->descent = 0;
25512
25513 it->nglyphs = 1;
25514
25515 if (face->box != FACE_NO_BOX)
25516 {
25517 if (face->box_line_width > 0)
25518 {
25519 if (slice.y == 0)
25520 it->ascent += face->box_line_width;
25521 if (slice.y + slice.height == img->height)
25522 it->descent += face->box_line_width;
25523 }
25524
25525 if (it->start_of_box_run_p && slice.x == 0)
25526 it->pixel_width += eabs (face->box_line_width);
25527 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25528 it->pixel_width += eabs (face->box_line_width);
25529 }
25530
25531 take_vertical_position_into_account (it);
25532
25533 /* Automatically crop wide image glyphs at right edge so we can
25534 draw the cursor on same display row. */
25535 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25536 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25537 {
25538 it->pixel_width -= crop;
25539 slice.width -= crop;
25540 }
25541
25542 if (it->glyph_row)
25543 {
25544 struct glyph *glyph;
25545 enum glyph_row_area area = it->area;
25546
25547 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25548 if (it->glyph_row->reversed_p)
25549 {
25550 struct glyph *g;
25551
25552 /* Make room for the new glyph. */
25553 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25554 g[1] = *g;
25555 glyph = it->glyph_row->glyphs[it->area];
25556 }
25557 if (glyph < it->glyph_row->glyphs[area + 1])
25558 {
25559 glyph->charpos = CHARPOS (it->position);
25560 glyph->object = it->object;
25561 glyph->pixel_width = it->pixel_width;
25562 glyph->ascent = glyph_ascent;
25563 glyph->descent = it->descent;
25564 glyph->voffset = it->voffset;
25565 glyph->type = IMAGE_GLYPH;
25566 glyph->avoid_cursor_p = it->avoid_cursor_p;
25567 glyph->multibyte_p = it->multibyte_p;
25568 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25569 {
25570 /* In R2L rows, the left and the right box edges need to be
25571 drawn in reverse direction. */
25572 glyph->right_box_line_p = it->start_of_box_run_p;
25573 glyph->left_box_line_p = it->end_of_box_run_p;
25574 }
25575 else
25576 {
25577 glyph->left_box_line_p = it->start_of_box_run_p;
25578 glyph->right_box_line_p = it->end_of_box_run_p;
25579 }
25580 glyph->overlaps_vertically_p = false;
25581 glyph->padding_p = false;
25582 glyph->glyph_not_available_p = false;
25583 glyph->face_id = it->face_id;
25584 glyph->u.img_id = img->id;
25585 glyph->slice.img = slice;
25586 glyph->font_type = FONT_TYPE_UNKNOWN;
25587 if (it->bidi_p)
25588 {
25589 glyph->resolved_level = it->bidi_it.resolved_level;
25590 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25591 glyph->bidi_type = it->bidi_it.type;
25592 }
25593 ++it->glyph_row->used[area];
25594 }
25595 else
25596 IT_EXPAND_MATRIX_WIDTH (it, area);
25597 }
25598 }
25599
25600
25601 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25602 of the glyph, WIDTH and HEIGHT are the width and height of the
25603 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25604
25605 static void
25606 append_stretch_glyph (struct it *it, Lisp_Object object,
25607 int width, int height, int ascent)
25608 {
25609 struct glyph *glyph;
25610 enum glyph_row_area area = it->area;
25611
25612 eassert (ascent >= 0 && ascent <= height);
25613
25614 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25615 if (glyph < it->glyph_row->glyphs[area + 1])
25616 {
25617 /* If the glyph row is reversed, we need to prepend the glyph
25618 rather than append it. */
25619 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25620 {
25621 struct glyph *g;
25622
25623 /* Make room for the additional glyph. */
25624 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25625 g[1] = *g;
25626 glyph = it->glyph_row->glyphs[area];
25627
25628 /* Decrease the width of the first glyph of the row that
25629 begins before first_visible_x (e.g., due to hscroll).
25630 This is so the overall width of the row becomes smaller
25631 by the scroll amount, and the stretch glyph appended by
25632 extend_face_to_end_of_line will be wider, to shift the
25633 row glyphs to the right. (In L2R rows, the corresponding
25634 left-shift effect is accomplished by setting row->x to a
25635 negative value, which won't work with R2L rows.)
25636
25637 This must leave us with a positive value of WIDTH, since
25638 otherwise the call to move_it_in_display_line_to at the
25639 beginning of display_line would have got past the entire
25640 first glyph, and then it->current_x would have been
25641 greater or equal to it->first_visible_x. */
25642 if (it->current_x < it->first_visible_x)
25643 width -= it->first_visible_x - it->current_x;
25644 eassert (width > 0);
25645 }
25646 glyph->charpos = CHARPOS (it->position);
25647 glyph->object = object;
25648 glyph->pixel_width = width;
25649 glyph->ascent = ascent;
25650 glyph->descent = height - ascent;
25651 glyph->voffset = it->voffset;
25652 glyph->type = STRETCH_GLYPH;
25653 glyph->avoid_cursor_p = it->avoid_cursor_p;
25654 glyph->multibyte_p = it->multibyte_p;
25655 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25656 {
25657 /* In R2L rows, the left and the right box edges need to be
25658 drawn in reverse direction. */
25659 glyph->right_box_line_p = it->start_of_box_run_p;
25660 glyph->left_box_line_p = it->end_of_box_run_p;
25661 }
25662 else
25663 {
25664 glyph->left_box_line_p = it->start_of_box_run_p;
25665 glyph->right_box_line_p = it->end_of_box_run_p;
25666 }
25667 glyph->overlaps_vertically_p = false;
25668 glyph->padding_p = false;
25669 glyph->glyph_not_available_p = false;
25670 glyph->face_id = it->face_id;
25671 glyph->u.stretch.ascent = ascent;
25672 glyph->u.stretch.height = height;
25673 glyph->slice.img = null_glyph_slice;
25674 glyph->font_type = FONT_TYPE_UNKNOWN;
25675 if (it->bidi_p)
25676 {
25677 glyph->resolved_level = it->bidi_it.resolved_level;
25678 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25679 glyph->bidi_type = it->bidi_it.type;
25680 }
25681 else
25682 {
25683 glyph->resolved_level = 0;
25684 glyph->bidi_type = UNKNOWN_BT;
25685 }
25686 ++it->glyph_row->used[area];
25687 }
25688 else
25689 IT_EXPAND_MATRIX_WIDTH (it, area);
25690 }
25691
25692 #endif /* HAVE_WINDOW_SYSTEM */
25693
25694 /* Produce a stretch glyph for iterator IT. IT->object is the value
25695 of the glyph property displayed. The value must be a list
25696 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25697 being recognized:
25698
25699 1. `:width WIDTH' specifies that the space should be WIDTH *
25700 canonical char width wide. WIDTH may be an integer or floating
25701 point number.
25702
25703 2. `:relative-width FACTOR' specifies that the width of the stretch
25704 should be computed from the width of the first character having the
25705 `glyph' property, and should be FACTOR times that width.
25706
25707 3. `:align-to HPOS' specifies that the space should be wide enough
25708 to reach HPOS, a value in canonical character units.
25709
25710 Exactly one of the above pairs must be present.
25711
25712 4. `:height HEIGHT' specifies that the height of the stretch produced
25713 should be HEIGHT, measured in canonical character units.
25714
25715 5. `:relative-height FACTOR' specifies that the height of the
25716 stretch should be FACTOR times the height of the characters having
25717 the glyph property.
25718
25719 Either none or exactly one of 4 or 5 must be present.
25720
25721 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25722 of the stretch should be used for the ascent of the stretch.
25723 ASCENT must be in the range 0 <= ASCENT <= 100. */
25724
25725 void
25726 produce_stretch_glyph (struct it *it)
25727 {
25728 /* (space :width WIDTH :height HEIGHT ...) */
25729 Lisp_Object prop, plist;
25730 int width = 0, height = 0, align_to = -1;
25731 bool zero_width_ok_p = false;
25732 double tem;
25733 struct font *font = NULL;
25734
25735 #ifdef HAVE_WINDOW_SYSTEM
25736 int ascent = 0;
25737 bool zero_height_ok_p = false;
25738
25739 if (FRAME_WINDOW_P (it->f))
25740 {
25741 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25742 font = face->font ? face->font : FRAME_FONT (it->f);
25743 prepare_face_for_display (it->f, face);
25744 }
25745 #endif
25746
25747 /* List should start with `space'. */
25748 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25749 plist = XCDR (it->object);
25750
25751 /* Compute the width of the stretch. */
25752 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25753 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
25754 {
25755 /* Absolute width `:width WIDTH' specified and valid. */
25756 zero_width_ok_p = true;
25757 width = (int)tem;
25758 }
25759 #ifdef HAVE_WINDOW_SYSTEM
25760 else if (FRAME_WINDOW_P (it->f)
25761 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25762 {
25763 /* Relative width `:relative-width FACTOR' specified and valid.
25764 Compute the width of the characters having the `glyph'
25765 property. */
25766 struct it it2;
25767 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25768
25769 it2 = *it;
25770 if (it->multibyte_p)
25771 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25772 else
25773 {
25774 it2.c = it2.char_to_display = *p, it2.len = 1;
25775 if (! ASCII_CHAR_P (it2.c))
25776 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25777 }
25778
25779 it2.glyph_row = NULL;
25780 it2.what = IT_CHARACTER;
25781 x_produce_glyphs (&it2);
25782 width = NUMVAL (prop) * it2.pixel_width;
25783 }
25784 #endif /* HAVE_WINDOW_SYSTEM */
25785 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25786 && calc_pixel_width_or_height (&tem, it, prop, font, true,
25787 &align_to))
25788 {
25789 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25790 align_to = (align_to < 0
25791 ? 0
25792 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25793 else if (align_to < 0)
25794 align_to = window_box_left_offset (it->w, TEXT_AREA);
25795 width = max (0, (int)tem + align_to - it->current_x);
25796 zero_width_ok_p = true;
25797 }
25798 else
25799 /* Nothing specified -> width defaults to canonical char width. */
25800 width = FRAME_COLUMN_WIDTH (it->f);
25801
25802 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25803 width = 1;
25804
25805 #ifdef HAVE_WINDOW_SYSTEM
25806 /* Compute height. */
25807 if (FRAME_WINDOW_P (it->f))
25808 {
25809 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25810 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
25811 {
25812 height = (int)tem;
25813 zero_height_ok_p = true;
25814 }
25815 else if (prop = Fplist_get (plist, QCrelative_height),
25816 NUMVAL (prop) > 0)
25817 height = FONT_HEIGHT (font) * NUMVAL (prop);
25818 else
25819 height = FONT_HEIGHT (font);
25820
25821 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25822 height = 1;
25823
25824 /* Compute percentage of height used for ascent. If
25825 `:ascent ASCENT' is present and valid, use that. Otherwise,
25826 derive the ascent from the font in use. */
25827 if (prop = Fplist_get (plist, QCascent),
25828 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25829 ascent = height * NUMVAL (prop) / 100.0;
25830 else if (!NILP (prop)
25831 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
25832 ascent = min (max (0, (int)tem), height);
25833 else
25834 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25835 }
25836 else
25837 #endif /* HAVE_WINDOW_SYSTEM */
25838 height = 1;
25839
25840 if (width > 0 && it->line_wrap != TRUNCATE
25841 && it->current_x + width > it->last_visible_x)
25842 {
25843 width = it->last_visible_x - it->current_x;
25844 #ifdef HAVE_WINDOW_SYSTEM
25845 /* Subtract one more pixel from the stretch width, but only on
25846 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25847 width -= FRAME_WINDOW_P (it->f);
25848 #endif
25849 }
25850
25851 if (width > 0 && height > 0 && it->glyph_row)
25852 {
25853 Lisp_Object o_object = it->object;
25854 Lisp_Object object = it->stack[it->sp - 1].string;
25855 int n = width;
25856
25857 if (!STRINGP (object))
25858 object = it->w->contents;
25859 #ifdef HAVE_WINDOW_SYSTEM
25860 if (FRAME_WINDOW_P (it->f))
25861 append_stretch_glyph (it, object, width, height, ascent);
25862 else
25863 #endif
25864 {
25865 it->object = object;
25866 it->char_to_display = ' ';
25867 it->pixel_width = it->len = 1;
25868 while (n--)
25869 tty_append_glyph (it);
25870 it->object = o_object;
25871 }
25872 }
25873
25874 it->pixel_width = width;
25875 #ifdef HAVE_WINDOW_SYSTEM
25876 if (FRAME_WINDOW_P (it->f))
25877 {
25878 it->ascent = it->phys_ascent = ascent;
25879 it->descent = it->phys_descent = height - it->ascent;
25880 it->nglyphs = width > 0 && height > 0;
25881 take_vertical_position_into_account (it);
25882 }
25883 else
25884 #endif
25885 it->nglyphs = width;
25886 }
25887
25888 /* Get information about special display element WHAT in an
25889 environment described by IT. WHAT is one of IT_TRUNCATION or
25890 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25891 non-null glyph_row member. This function ensures that fields like
25892 face_id, c, len of IT are left untouched. */
25893
25894 static void
25895 produce_special_glyphs (struct it *it, enum display_element_type what)
25896 {
25897 struct it temp_it;
25898 Lisp_Object gc;
25899 GLYPH glyph;
25900
25901 temp_it = *it;
25902 temp_it.object = Qnil;
25903 memset (&temp_it.current, 0, sizeof temp_it.current);
25904
25905 if (what == IT_CONTINUATION)
25906 {
25907 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25908 if (it->bidi_it.paragraph_dir == R2L)
25909 SET_GLYPH_FROM_CHAR (glyph, '/');
25910 else
25911 SET_GLYPH_FROM_CHAR (glyph, '\\');
25912 if (it->dp
25913 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25914 {
25915 /* FIXME: Should we mirror GC for R2L lines? */
25916 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25917 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25918 }
25919 }
25920 else if (what == IT_TRUNCATION)
25921 {
25922 /* Truncation glyph. */
25923 SET_GLYPH_FROM_CHAR (glyph, '$');
25924 if (it->dp
25925 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25926 {
25927 /* FIXME: Should we mirror GC for R2L lines? */
25928 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25929 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25930 }
25931 }
25932 else
25933 emacs_abort ();
25934
25935 #ifdef HAVE_WINDOW_SYSTEM
25936 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25937 is turned off, we precede the truncation/continuation glyphs by a
25938 stretch glyph whose width is computed such that these special
25939 glyphs are aligned at the window margin, even when very different
25940 fonts are used in different glyph rows. */
25941 if (FRAME_WINDOW_P (temp_it.f)
25942 /* init_iterator calls this with it->glyph_row == NULL, and it
25943 wants only the pixel width of the truncation/continuation
25944 glyphs. */
25945 && temp_it.glyph_row
25946 /* insert_left_trunc_glyphs calls us at the beginning of the
25947 row, and it has its own calculation of the stretch glyph
25948 width. */
25949 && temp_it.glyph_row->used[TEXT_AREA] > 0
25950 && (temp_it.glyph_row->reversed_p
25951 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25952 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25953 {
25954 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25955
25956 if (stretch_width > 0)
25957 {
25958 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25959 struct font *font =
25960 face->font ? face->font : FRAME_FONT (temp_it.f);
25961 int stretch_ascent =
25962 (((temp_it.ascent + temp_it.descent)
25963 * FONT_BASE (font)) / FONT_HEIGHT (font));
25964
25965 append_stretch_glyph (&temp_it, Qnil, stretch_width,
25966 temp_it.ascent + temp_it.descent,
25967 stretch_ascent);
25968 }
25969 }
25970 #endif
25971
25972 temp_it.dp = NULL;
25973 temp_it.what = IT_CHARACTER;
25974 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25975 temp_it.face_id = GLYPH_FACE (glyph);
25976 temp_it.len = CHAR_BYTES (temp_it.c);
25977
25978 PRODUCE_GLYPHS (&temp_it);
25979 it->pixel_width = temp_it.pixel_width;
25980 it->nglyphs = temp_it.nglyphs;
25981 }
25982
25983 #ifdef HAVE_WINDOW_SYSTEM
25984
25985 /* Calculate line-height and line-spacing properties.
25986 An integer value specifies explicit pixel value.
25987 A float value specifies relative value to current face height.
25988 A cons (float . face-name) specifies relative value to
25989 height of specified face font.
25990
25991 Returns height in pixels, or nil. */
25992
25993 static Lisp_Object
25994 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25995 int boff, bool override)
25996 {
25997 Lisp_Object face_name = Qnil;
25998 int ascent, descent, height;
25999
26000 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26001 return val;
26002
26003 if (CONSP (val))
26004 {
26005 face_name = XCAR (val);
26006 val = XCDR (val);
26007 if (!NUMBERP (val))
26008 val = make_number (1);
26009 if (NILP (face_name))
26010 {
26011 height = it->ascent + it->descent;
26012 goto scale;
26013 }
26014 }
26015
26016 if (NILP (face_name))
26017 {
26018 font = FRAME_FONT (it->f);
26019 boff = FRAME_BASELINE_OFFSET (it->f);
26020 }
26021 else if (EQ (face_name, Qt))
26022 {
26023 override = false;
26024 }
26025 else
26026 {
26027 int face_id;
26028 struct face *face;
26029
26030 face_id = lookup_named_face (it->f, face_name, false);
26031 if (face_id < 0)
26032 return make_number (-1);
26033
26034 face = FACE_FROM_ID (it->f, face_id);
26035 font = face->font;
26036 if (font == NULL)
26037 return make_number (-1);
26038 boff = font->baseline_offset;
26039 if (font->vertical_centering)
26040 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26041 }
26042
26043 ascent = FONT_BASE (font) + boff;
26044 descent = FONT_DESCENT (font) - boff;
26045
26046 if (override)
26047 {
26048 it->override_ascent = ascent;
26049 it->override_descent = descent;
26050 it->override_boff = boff;
26051 }
26052
26053 height = ascent + descent;
26054
26055 scale:
26056 if (FLOATP (val))
26057 height = (int)(XFLOAT_DATA (val) * height);
26058 else if (INTEGERP (val))
26059 height *= XINT (val);
26060
26061 return make_number (height);
26062 }
26063
26064
26065 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26066 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26067 and only if this is for a character for which no font was found.
26068
26069 If the display method (it->glyphless_method) is
26070 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26071 length of the acronym or the hexadecimal string, UPPER_XOFF and
26072 UPPER_YOFF are pixel offsets for the upper part of the string,
26073 LOWER_XOFF and LOWER_YOFF are for the lower part.
26074
26075 For the other display methods, LEN through LOWER_YOFF are zero. */
26076
26077 static void
26078 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26079 short upper_xoff, short upper_yoff,
26080 short lower_xoff, short lower_yoff)
26081 {
26082 struct glyph *glyph;
26083 enum glyph_row_area area = it->area;
26084
26085 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26086 if (glyph < it->glyph_row->glyphs[area + 1])
26087 {
26088 /* If the glyph row is reversed, we need to prepend the glyph
26089 rather than append it. */
26090 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26091 {
26092 struct glyph *g;
26093
26094 /* Make room for the additional glyph. */
26095 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26096 g[1] = *g;
26097 glyph = it->glyph_row->glyphs[area];
26098 }
26099 glyph->charpos = CHARPOS (it->position);
26100 glyph->object = it->object;
26101 glyph->pixel_width = it->pixel_width;
26102 glyph->ascent = it->ascent;
26103 glyph->descent = it->descent;
26104 glyph->voffset = it->voffset;
26105 glyph->type = GLYPHLESS_GLYPH;
26106 glyph->u.glyphless.method = it->glyphless_method;
26107 glyph->u.glyphless.for_no_font = for_no_font;
26108 glyph->u.glyphless.len = len;
26109 glyph->u.glyphless.ch = it->c;
26110 glyph->slice.glyphless.upper_xoff = upper_xoff;
26111 glyph->slice.glyphless.upper_yoff = upper_yoff;
26112 glyph->slice.glyphless.lower_xoff = lower_xoff;
26113 glyph->slice.glyphless.lower_yoff = lower_yoff;
26114 glyph->avoid_cursor_p = it->avoid_cursor_p;
26115 glyph->multibyte_p = it->multibyte_p;
26116 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26117 {
26118 /* In R2L rows, the left and the right box edges need to be
26119 drawn in reverse direction. */
26120 glyph->right_box_line_p = it->start_of_box_run_p;
26121 glyph->left_box_line_p = it->end_of_box_run_p;
26122 }
26123 else
26124 {
26125 glyph->left_box_line_p = it->start_of_box_run_p;
26126 glyph->right_box_line_p = it->end_of_box_run_p;
26127 }
26128 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26129 || it->phys_descent > it->descent);
26130 glyph->padding_p = false;
26131 glyph->glyph_not_available_p = false;
26132 glyph->face_id = face_id;
26133 glyph->font_type = FONT_TYPE_UNKNOWN;
26134 if (it->bidi_p)
26135 {
26136 glyph->resolved_level = it->bidi_it.resolved_level;
26137 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26138 glyph->bidi_type = it->bidi_it.type;
26139 }
26140 ++it->glyph_row->used[area];
26141 }
26142 else
26143 IT_EXPAND_MATRIX_WIDTH (it, area);
26144 }
26145
26146
26147 /* Produce a glyph for a glyphless character for iterator IT.
26148 IT->glyphless_method specifies which method to use for displaying
26149 the character. See the description of enum
26150 glyphless_display_method in dispextern.h for the detail.
26151
26152 FOR_NO_FONT is true if and only if this is for a character for
26153 which no font was found. ACRONYM, if non-nil, is an acronym string
26154 for the character. */
26155
26156 static void
26157 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26158 {
26159 int face_id;
26160 struct face *face;
26161 struct font *font;
26162 int base_width, base_height, width, height;
26163 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26164 int len;
26165
26166 /* Get the metrics of the base font. We always refer to the current
26167 ASCII face. */
26168 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26169 font = face->font ? face->font : FRAME_FONT (it->f);
26170 it->ascent = FONT_BASE (font) + font->baseline_offset;
26171 it->descent = FONT_DESCENT (font) - font->baseline_offset;
26172 base_height = it->ascent + it->descent;
26173 base_width = font->average_width;
26174
26175 face_id = merge_glyphless_glyph_face (it);
26176
26177 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26178 {
26179 it->pixel_width = THIN_SPACE_WIDTH;
26180 len = 0;
26181 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26182 }
26183 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26184 {
26185 width = CHAR_WIDTH (it->c);
26186 if (width == 0)
26187 width = 1;
26188 else if (width > 4)
26189 width = 4;
26190 it->pixel_width = base_width * width;
26191 len = 0;
26192 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26193 }
26194 else
26195 {
26196 char buf[7];
26197 const char *str;
26198 unsigned int code[6];
26199 int upper_len;
26200 int ascent, descent;
26201 struct font_metrics metrics_upper, metrics_lower;
26202
26203 face = FACE_FROM_ID (it->f, face_id);
26204 font = face->font ? face->font : FRAME_FONT (it->f);
26205 prepare_face_for_display (it->f, face);
26206
26207 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26208 {
26209 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26210 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26211 if (CONSP (acronym))
26212 acronym = XCAR (acronym);
26213 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26214 }
26215 else
26216 {
26217 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26218 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
26219 str = buf;
26220 }
26221 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26222 code[len] = font->driver->encode_char (font, str[len]);
26223 upper_len = (len + 1) / 2;
26224 font->driver->text_extents (font, code, upper_len,
26225 &metrics_upper);
26226 font->driver->text_extents (font, code + upper_len, len - upper_len,
26227 &metrics_lower);
26228
26229
26230
26231 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26232 width = max (metrics_upper.width, metrics_lower.width) + 4;
26233 upper_xoff = upper_yoff = 2; /* the typical case */
26234 if (base_width >= width)
26235 {
26236 /* Align the upper to the left, the lower to the right. */
26237 it->pixel_width = base_width;
26238 lower_xoff = base_width - 2 - metrics_lower.width;
26239 }
26240 else
26241 {
26242 /* Center the shorter one. */
26243 it->pixel_width = width;
26244 if (metrics_upper.width >= metrics_lower.width)
26245 lower_xoff = (width - metrics_lower.width) / 2;
26246 else
26247 {
26248 /* FIXME: This code doesn't look right. It formerly was
26249 missing the "lower_xoff = 0;", which couldn't have
26250 been right since it left lower_xoff uninitialized. */
26251 lower_xoff = 0;
26252 upper_xoff = (width - metrics_upper.width) / 2;
26253 }
26254 }
26255
26256 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26257 top, bottom, and between upper and lower strings. */
26258 height = (metrics_upper.ascent + metrics_upper.descent
26259 + metrics_lower.ascent + metrics_lower.descent) + 5;
26260 /* Center vertically.
26261 H:base_height, D:base_descent
26262 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26263
26264 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26265 descent = D - H/2 + h/2;
26266 lower_yoff = descent - 2 - ld;
26267 upper_yoff = lower_yoff - la - 1 - ud; */
26268 ascent = - (it->descent - (base_height + height + 1) / 2);
26269 descent = it->descent - (base_height - height) / 2;
26270 lower_yoff = descent - 2 - metrics_lower.descent;
26271 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26272 - metrics_upper.descent);
26273 /* Don't make the height shorter than the base height. */
26274 if (height > base_height)
26275 {
26276 it->ascent = ascent;
26277 it->descent = descent;
26278 }
26279 }
26280
26281 it->phys_ascent = it->ascent;
26282 it->phys_descent = it->descent;
26283 if (it->glyph_row)
26284 append_glyphless_glyph (it, face_id, for_no_font, len,
26285 upper_xoff, upper_yoff,
26286 lower_xoff, lower_yoff);
26287 it->nglyphs = 1;
26288 take_vertical_position_into_account (it);
26289 }
26290
26291
26292 /* RIF:
26293 Produce glyphs/get display metrics for the display element IT is
26294 loaded with. See the description of struct it in dispextern.h
26295 for an overview of struct it. */
26296
26297 void
26298 x_produce_glyphs (struct it *it)
26299 {
26300 int extra_line_spacing = it->extra_line_spacing;
26301
26302 it->glyph_not_available_p = false;
26303
26304 if (it->what == IT_CHARACTER)
26305 {
26306 XChar2b char2b;
26307 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26308 struct font *font = face->font;
26309 struct font_metrics *pcm = NULL;
26310 int boff; /* Baseline offset. */
26311
26312 if (font == NULL)
26313 {
26314 /* When no suitable font is found, display this character by
26315 the method specified in the first extra slot of
26316 Vglyphless_char_display. */
26317 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26318
26319 eassert (it->what == IT_GLYPHLESS);
26320 produce_glyphless_glyph (it, true,
26321 STRINGP (acronym) ? acronym : Qnil);
26322 goto done;
26323 }
26324
26325 boff = font->baseline_offset;
26326 if (font->vertical_centering)
26327 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26328
26329 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26330 {
26331 it->nglyphs = 1;
26332
26333 if (it->override_ascent >= 0)
26334 {
26335 it->ascent = it->override_ascent;
26336 it->descent = it->override_descent;
26337 boff = it->override_boff;
26338 }
26339 else
26340 {
26341 it->ascent = FONT_BASE (font) + boff;
26342 it->descent = FONT_DESCENT (font) - boff;
26343 }
26344
26345 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26346 {
26347 pcm = get_per_char_metric (font, &char2b);
26348 if (pcm->width == 0
26349 && pcm->rbearing == 0 && pcm->lbearing == 0)
26350 pcm = NULL;
26351 }
26352
26353 if (pcm)
26354 {
26355 it->phys_ascent = pcm->ascent + boff;
26356 it->phys_descent = pcm->descent - boff;
26357 it->pixel_width = pcm->width;
26358 }
26359 else
26360 {
26361 it->glyph_not_available_p = true;
26362 it->phys_ascent = it->ascent;
26363 it->phys_descent = it->descent;
26364 it->pixel_width = font->space_width;
26365 }
26366
26367 if (it->constrain_row_ascent_descent_p)
26368 {
26369 if (it->descent > it->max_descent)
26370 {
26371 it->ascent += it->descent - it->max_descent;
26372 it->descent = it->max_descent;
26373 }
26374 if (it->ascent > it->max_ascent)
26375 {
26376 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26377 it->ascent = it->max_ascent;
26378 }
26379 it->phys_ascent = min (it->phys_ascent, it->ascent);
26380 it->phys_descent = min (it->phys_descent, it->descent);
26381 extra_line_spacing = 0;
26382 }
26383
26384 /* If this is a space inside a region of text with
26385 `space-width' property, change its width. */
26386 bool stretched_p
26387 = it->char_to_display == ' ' && !NILP (it->space_width);
26388 if (stretched_p)
26389 it->pixel_width *= XFLOATINT (it->space_width);
26390
26391 /* If face has a box, add the box thickness to the character
26392 height. If character has a box line to the left and/or
26393 right, add the box line width to the character's width. */
26394 if (face->box != FACE_NO_BOX)
26395 {
26396 int thick = face->box_line_width;
26397
26398 if (thick > 0)
26399 {
26400 it->ascent += thick;
26401 it->descent += thick;
26402 }
26403 else
26404 thick = -thick;
26405
26406 if (it->start_of_box_run_p)
26407 it->pixel_width += thick;
26408 if (it->end_of_box_run_p)
26409 it->pixel_width += thick;
26410 }
26411
26412 /* If face has an overline, add the height of the overline
26413 (1 pixel) and a 1 pixel margin to the character height. */
26414 if (face->overline_p)
26415 it->ascent += overline_margin;
26416
26417 if (it->constrain_row_ascent_descent_p)
26418 {
26419 if (it->ascent > it->max_ascent)
26420 it->ascent = it->max_ascent;
26421 if (it->descent > it->max_descent)
26422 it->descent = it->max_descent;
26423 }
26424
26425 take_vertical_position_into_account (it);
26426
26427 /* If we have to actually produce glyphs, do it. */
26428 if (it->glyph_row)
26429 {
26430 if (stretched_p)
26431 {
26432 /* Translate a space with a `space-width' property
26433 into a stretch glyph. */
26434 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26435 / FONT_HEIGHT (font));
26436 append_stretch_glyph (it, it->object, it->pixel_width,
26437 it->ascent + it->descent, ascent);
26438 }
26439 else
26440 append_glyph (it);
26441
26442 /* If characters with lbearing or rbearing are displayed
26443 in this line, record that fact in a flag of the
26444 glyph row. This is used to optimize X output code. */
26445 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26446 it->glyph_row->contains_overlapping_glyphs_p = true;
26447 }
26448 if (! stretched_p && it->pixel_width == 0)
26449 /* We assure that all visible glyphs have at least 1-pixel
26450 width. */
26451 it->pixel_width = 1;
26452 }
26453 else if (it->char_to_display == '\n')
26454 {
26455 /* A newline has no width, but we need the height of the
26456 line. But if previous part of the line sets a height,
26457 don't increase that height. */
26458
26459 Lisp_Object height;
26460 Lisp_Object total_height = Qnil;
26461
26462 it->override_ascent = -1;
26463 it->pixel_width = 0;
26464 it->nglyphs = 0;
26465
26466 height = get_it_property (it, Qline_height);
26467 /* Split (line-height total-height) list. */
26468 if (CONSP (height)
26469 && CONSP (XCDR (height))
26470 && NILP (XCDR (XCDR (height))))
26471 {
26472 total_height = XCAR (XCDR (height));
26473 height = XCAR (height);
26474 }
26475 height = calc_line_height_property (it, height, font, boff, true);
26476
26477 if (it->override_ascent >= 0)
26478 {
26479 it->ascent = it->override_ascent;
26480 it->descent = it->override_descent;
26481 boff = it->override_boff;
26482 }
26483 else
26484 {
26485 it->ascent = FONT_BASE (font) + boff;
26486 it->descent = FONT_DESCENT (font) - boff;
26487 }
26488
26489 if (EQ (height, Qt))
26490 {
26491 if (it->descent > it->max_descent)
26492 {
26493 it->ascent += it->descent - it->max_descent;
26494 it->descent = it->max_descent;
26495 }
26496 if (it->ascent > it->max_ascent)
26497 {
26498 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26499 it->ascent = it->max_ascent;
26500 }
26501 it->phys_ascent = min (it->phys_ascent, it->ascent);
26502 it->phys_descent = min (it->phys_descent, it->descent);
26503 it->constrain_row_ascent_descent_p = true;
26504 extra_line_spacing = 0;
26505 }
26506 else
26507 {
26508 Lisp_Object spacing;
26509
26510 it->phys_ascent = it->ascent;
26511 it->phys_descent = it->descent;
26512
26513 if ((it->max_ascent > 0 || it->max_descent > 0)
26514 && face->box != FACE_NO_BOX
26515 && face->box_line_width > 0)
26516 {
26517 it->ascent += face->box_line_width;
26518 it->descent += face->box_line_width;
26519 }
26520 if (!NILP (height)
26521 && XINT (height) > it->ascent + it->descent)
26522 it->ascent = XINT (height) - it->descent;
26523
26524 if (!NILP (total_height))
26525 spacing = calc_line_height_property (it, total_height, font,
26526 boff, false);
26527 else
26528 {
26529 spacing = get_it_property (it, Qline_spacing);
26530 spacing = calc_line_height_property (it, spacing, font,
26531 boff, false);
26532 }
26533 if (INTEGERP (spacing))
26534 {
26535 extra_line_spacing = XINT (spacing);
26536 if (!NILP (total_height))
26537 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26538 }
26539 }
26540 }
26541 else /* i.e. (it->char_to_display == '\t') */
26542 {
26543 if (font->space_width > 0)
26544 {
26545 int tab_width = it->tab_width * font->space_width;
26546 int x = it->current_x + it->continuation_lines_width;
26547 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26548
26549 /* If the distance from the current position to the next tab
26550 stop is less than a space character width, use the
26551 tab stop after that. */
26552 if (next_tab_x - x < font->space_width)
26553 next_tab_x += tab_width;
26554
26555 it->pixel_width = next_tab_x - x;
26556 it->nglyphs = 1;
26557 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
26558 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
26559
26560 if (it->glyph_row)
26561 {
26562 append_stretch_glyph (it, it->object, it->pixel_width,
26563 it->ascent + it->descent, it->ascent);
26564 }
26565 }
26566 else
26567 {
26568 it->pixel_width = 0;
26569 it->nglyphs = 1;
26570 }
26571 }
26572 }
26573 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26574 {
26575 /* A static composition.
26576
26577 Note: A composition is represented as one glyph in the
26578 glyph matrix. There are no padding glyphs.
26579
26580 Important note: pixel_width, ascent, and descent are the
26581 values of what is drawn by draw_glyphs (i.e. the values of
26582 the overall glyphs composed). */
26583 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26584 int boff; /* baseline offset */
26585 struct composition *cmp = composition_table[it->cmp_it.id];
26586 int glyph_len = cmp->glyph_len;
26587 struct font *font = face->font;
26588
26589 it->nglyphs = 1;
26590
26591 /* If we have not yet calculated pixel size data of glyphs of
26592 the composition for the current face font, calculate them
26593 now. Theoretically, we have to check all fonts for the
26594 glyphs, but that requires much time and memory space. So,
26595 here we check only the font of the first glyph. This may
26596 lead to incorrect display, but it's very rare, and C-l
26597 (recenter-top-bottom) can correct the display anyway. */
26598 if (! cmp->font || cmp->font != font)
26599 {
26600 /* Ascent and descent of the font of the first character
26601 of this composition (adjusted by baseline offset).
26602 Ascent and descent of overall glyphs should not be less
26603 than these, respectively. */
26604 int font_ascent, font_descent, font_height;
26605 /* Bounding box of the overall glyphs. */
26606 int leftmost, rightmost, lowest, highest;
26607 int lbearing, rbearing;
26608 int i, width, ascent, descent;
26609 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26610 XChar2b char2b;
26611 struct font_metrics *pcm;
26612 ptrdiff_t pos;
26613
26614 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26615 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26616 break;
26617 bool right_padded = glyph_len < cmp->glyph_len;
26618 for (i = 0; i < glyph_len; i++)
26619 {
26620 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26621 break;
26622 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26623 }
26624 bool left_padded = i > 0;
26625
26626 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26627 : IT_CHARPOS (*it));
26628 /* If no suitable font is found, use the default font. */
26629 bool font_not_found_p = font == NULL;
26630 if (font_not_found_p)
26631 {
26632 face = face->ascii_face;
26633 font = face->font;
26634 }
26635 boff = font->baseline_offset;
26636 if (font->vertical_centering)
26637 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26638 font_ascent = FONT_BASE (font) + boff;
26639 font_descent = FONT_DESCENT (font) - boff;
26640 font_height = FONT_HEIGHT (font);
26641
26642 cmp->font = font;
26643
26644 pcm = NULL;
26645 if (! font_not_found_p)
26646 {
26647 get_char_face_and_encoding (it->f, c, it->face_id,
26648 &char2b, false);
26649 pcm = get_per_char_metric (font, &char2b);
26650 }
26651
26652 /* Initialize the bounding box. */
26653 if (pcm)
26654 {
26655 width = cmp->glyph_len > 0 ? pcm->width : 0;
26656 ascent = pcm->ascent;
26657 descent = pcm->descent;
26658 lbearing = pcm->lbearing;
26659 rbearing = pcm->rbearing;
26660 }
26661 else
26662 {
26663 width = cmp->glyph_len > 0 ? font->space_width : 0;
26664 ascent = FONT_BASE (font);
26665 descent = FONT_DESCENT (font);
26666 lbearing = 0;
26667 rbearing = width;
26668 }
26669
26670 rightmost = width;
26671 leftmost = 0;
26672 lowest = - descent + boff;
26673 highest = ascent + boff;
26674
26675 if (! font_not_found_p
26676 && font->default_ascent
26677 && CHAR_TABLE_P (Vuse_default_ascent)
26678 && !NILP (Faref (Vuse_default_ascent,
26679 make_number (it->char_to_display))))
26680 highest = font->default_ascent + boff;
26681
26682 /* Draw the first glyph at the normal position. It may be
26683 shifted to right later if some other glyphs are drawn
26684 at the left. */
26685 cmp->offsets[i * 2] = 0;
26686 cmp->offsets[i * 2 + 1] = boff;
26687 cmp->lbearing = lbearing;
26688 cmp->rbearing = rbearing;
26689
26690 /* Set cmp->offsets for the remaining glyphs. */
26691 for (i++; i < glyph_len; i++)
26692 {
26693 int left, right, btm, top;
26694 int ch = COMPOSITION_GLYPH (cmp, i);
26695 int face_id;
26696 struct face *this_face;
26697
26698 if (ch == '\t')
26699 ch = ' ';
26700 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26701 this_face = FACE_FROM_ID (it->f, face_id);
26702 font = this_face->font;
26703
26704 if (font == NULL)
26705 pcm = NULL;
26706 else
26707 {
26708 get_char_face_and_encoding (it->f, ch, face_id,
26709 &char2b, false);
26710 pcm = get_per_char_metric (font, &char2b);
26711 }
26712 if (! pcm)
26713 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26714 else
26715 {
26716 width = pcm->width;
26717 ascent = pcm->ascent;
26718 descent = pcm->descent;
26719 lbearing = pcm->lbearing;
26720 rbearing = pcm->rbearing;
26721 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26722 {
26723 /* Relative composition with or without
26724 alternate chars. */
26725 left = (leftmost + rightmost - width) / 2;
26726 btm = - descent + boff;
26727 if (font->relative_compose
26728 && (! CHAR_TABLE_P (Vignore_relative_composition)
26729 || NILP (Faref (Vignore_relative_composition,
26730 make_number (ch)))))
26731 {
26732
26733 if (- descent >= font->relative_compose)
26734 /* One extra pixel between two glyphs. */
26735 btm = highest + 1;
26736 else if (ascent <= 0)
26737 /* One extra pixel between two glyphs. */
26738 btm = lowest - 1 - ascent - descent;
26739 }
26740 }
26741 else
26742 {
26743 /* A composition rule is specified by an integer
26744 value that encodes global and new reference
26745 points (GREF and NREF). GREF and NREF are
26746 specified by numbers as below:
26747
26748 0---1---2 -- ascent
26749 | |
26750 | |
26751 | |
26752 9--10--11 -- center
26753 | |
26754 ---3---4---5--- baseline
26755 | |
26756 6---7---8 -- descent
26757 */
26758 int rule = COMPOSITION_RULE (cmp, i);
26759 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26760
26761 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26762 grefx = gref % 3, nrefx = nref % 3;
26763 grefy = gref / 3, nrefy = nref / 3;
26764 if (xoff)
26765 xoff = font_height * (xoff - 128) / 256;
26766 if (yoff)
26767 yoff = font_height * (yoff - 128) / 256;
26768
26769 left = (leftmost
26770 + grefx * (rightmost - leftmost) / 2
26771 - nrefx * width / 2
26772 + xoff);
26773
26774 btm = ((grefy == 0 ? highest
26775 : grefy == 1 ? 0
26776 : grefy == 2 ? lowest
26777 : (highest + lowest) / 2)
26778 - (nrefy == 0 ? ascent + descent
26779 : nrefy == 1 ? descent - boff
26780 : nrefy == 2 ? 0
26781 : (ascent + descent) / 2)
26782 + yoff);
26783 }
26784
26785 cmp->offsets[i * 2] = left;
26786 cmp->offsets[i * 2 + 1] = btm + descent;
26787
26788 /* Update the bounding box of the overall glyphs. */
26789 if (width > 0)
26790 {
26791 right = left + width;
26792 if (left < leftmost)
26793 leftmost = left;
26794 if (right > rightmost)
26795 rightmost = right;
26796 }
26797 top = btm + descent + ascent;
26798 if (top > highest)
26799 highest = top;
26800 if (btm < lowest)
26801 lowest = btm;
26802
26803 if (cmp->lbearing > left + lbearing)
26804 cmp->lbearing = left + lbearing;
26805 if (cmp->rbearing < left + rbearing)
26806 cmp->rbearing = left + rbearing;
26807 }
26808 }
26809
26810 /* If there are glyphs whose x-offsets are negative,
26811 shift all glyphs to the right and make all x-offsets
26812 non-negative. */
26813 if (leftmost < 0)
26814 {
26815 for (i = 0; i < cmp->glyph_len; i++)
26816 cmp->offsets[i * 2] -= leftmost;
26817 rightmost -= leftmost;
26818 cmp->lbearing -= leftmost;
26819 cmp->rbearing -= leftmost;
26820 }
26821
26822 if (left_padded && cmp->lbearing < 0)
26823 {
26824 for (i = 0; i < cmp->glyph_len; i++)
26825 cmp->offsets[i * 2] -= cmp->lbearing;
26826 rightmost -= cmp->lbearing;
26827 cmp->rbearing -= cmp->lbearing;
26828 cmp->lbearing = 0;
26829 }
26830 if (right_padded && rightmost < cmp->rbearing)
26831 {
26832 rightmost = cmp->rbearing;
26833 }
26834
26835 cmp->pixel_width = rightmost;
26836 cmp->ascent = highest;
26837 cmp->descent = - lowest;
26838 if (cmp->ascent < font_ascent)
26839 cmp->ascent = font_ascent;
26840 if (cmp->descent < font_descent)
26841 cmp->descent = font_descent;
26842 }
26843
26844 if (it->glyph_row
26845 && (cmp->lbearing < 0
26846 || cmp->rbearing > cmp->pixel_width))
26847 it->glyph_row->contains_overlapping_glyphs_p = true;
26848
26849 it->pixel_width = cmp->pixel_width;
26850 it->ascent = it->phys_ascent = cmp->ascent;
26851 it->descent = it->phys_descent = cmp->descent;
26852 if (face->box != FACE_NO_BOX)
26853 {
26854 int thick = face->box_line_width;
26855
26856 if (thick > 0)
26857 {
26858 it->ascent += thick;
26859 it->descent += thick;
26860 }
26861 else
26862 thick = - thick;
26863
26864 if (it->start_of_box_run_p)
26865 it->pixel_width += thick;
26866 if (it->end_of_box_run_p)
26867 it->pixel_width += thick;
26868 }
26869
26870 /* If face has an overline, add the height of the overline
26871 (1 pixel) and a 1 pixel margin to the character height. */
26872 if (face->overline_p)
26873 it->ascent += overline_margin;
26874
26875 take_vertical_position_into_account (it);
26876 if (it->ascent < 0)
26877 it->ascent = 0;
26878 if (it->descent < 0)
26879 it->descent = 0;
26880
26881 if (it->glyph_row && cmp->glyph_len > 0)
26882 append_composite_glyph (it);
26883 }
26884 else if (it->what == IT_COMPOSITION)
26885 {
26886 /* A dynamic (automatic) composition. */
26887 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26888 Lisp_Object gstring;
26889 struct font_metrics metrics;
26890
26891 it->nglyphs = 1;
26892
26893 gstring = composition_gstring_from_id (it->cmp_it.id);
26894 it->pixel_width
26895 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26896 &metrics);
26897 if (it->glyph_row
26898 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26899 it->glyph_row->contains_overlapping_glyphs_p = true;
26900 it->ascent = it->phys_ascent = metrics.ascent;
26901 it->descent = it->phys_descent = metrics.descent;
26902 if (face->box != FACE_NO_BOX)
26903 {
26904 int thick = face->box_line_width;
26905
26906 if (thick > 0)
26907 {
26908 it->ascent += thick;
26909 it->descent += thick;
26910 }
26911 else
26912 thick = - thick;
26913
26914 if (it->start_of_box_run_p)
26915 it->pixel_width += thick;
26916 if (it->end_of_box_run_p)
26917 it->pixel_width += thick;
26918 }
26919 /* If face has an overline, add the height of the overline
26920 (1 pixel) and a 1 pixel margin to the character height. */
26921 if (face->overline_p)
26922 it->ascent += overline_margin;
26923 take_vertical_position_into_account (it);
26924 if (it->ascent < 0)
26925 it->ascent = 0;
26926 if (it->descent < 0)
26927 it->descent = 0;
26928
26929 if (it->glyph_row)
26930 append_composite_glyph (it);
26931 }
26932 else if (it->what == IT_GLYPHLESS)
26933 produce_glyphless_glyph (it, false, Qnil);
26934 else if (it->what == IT_IMAGE)
26935 produce_image_glyph (it);
26936 else if (it->what == IT_STRETCH)
26937 produce_stretch_glyph (it);
26938
26939 done:
26940 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26941 because this isn't true for images with `:ascent 100'. */
26942 eassert (it->ascent >= 0 && it->descent >= 0);
26943 if (it->area == TEXT_AREA)
26944 it->current_x += it->pixel_width;
26945
26946 if (extra_line_spacing > 0)
26947 {
26948 it->descent += extra_line_spacing;
26949 if (extra_line_spacing > it->max_extra_line_spacing)
26950 it->max_extra_line_spacing = extra_line_spacing;
26951 }
26952
26953 it->max_ascent = max (it->max_ascent, it->ascent);
26954 it->max_descent = max (it->max_descent, it->descent);
26955 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26956 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26957 }
26958
26959 /* EXPORT for RIF:
26960 Output LEN glyphs starting at START at the nominal cursor position.
26961 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26962 being updated, and UPDATED_AREA is the area of that row being updated. */
26963
26964 void
26965 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26966 struct glyph *start, enum glyph_row_area updated_area, int len)
26967 {
26968 int x, hpos, chpos = w->phys_cursor.hpos;
26969
26970 eassert (updated_row);
26971 /* When the window is hscrolled, cursor hpos can legitimately be out
26972 of bounds, but we draw the cursor at the corresponding window
26973 margin in that case. */
26974 if (!updated_row->reversed_p && chpos < 0)
26975 chpos = 0;
26976 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26977 chpos = updated_row->used[TEXT_AREA] - 1;
26978
26979 block_input ();
26980
26981 /* Write glyphs. */
26982
26983 hpos = start - updated_row->glyphs[updated_area];
26984 x = draw_glyphs (w, w->output_cursor.x,
26985 updated_row, updated_area,
26986 hpos, hpos + len,
26987 DRAW_NORMAL_TEXT, 0);
26988
26989 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26990 if (updated_area == TEXT_AREA
26991 && w->phys_cursor_on_p
26992 && w->phys_cursor.vpos == w->output_cursor.vpos
26993 && chpos >= hpos
26994 && chpos < hpos + len)
26995 w->phys_cursor_on_p = false;
26996
26997 unblock_input ();
26998
26999 /* Advance the output cursor. */
27000 w->output_cursor.hpos += len;
27001 w->output_cursor.x = x;
27002 }
27003
27004
27005 /* EXPORT for RIF:
27006 Insert LEN glyphs from START at the nominal cursor position. */
27007
27008 void
27009 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27010 struct glyph *start, enum glyph_row_area updated_area, int len)
27011 {
27012 struct frame *f;
27013 int line_height, shift_by_width, shifted_region_width;
27014 struct glyph_row *row;
27015 struct glyph *glyph;
27016 int frame_x, frame_y;
27017 ptrdiff_t hpos;
27018
27019 eassert (updated_row);
27020 block_input ();
27021 f = XFRAME (WINDOW_FRAME (w));
27022
27023 /* Get the height of the line we are in. */
27024 row = updated_row;
27025 line_height = row->height;
27026
27027 /* Get the width of the glyphs to insert. */
27028 shift_by_width = 0;
27029 for (glyph = start; glyph < start + len; ++glyph)
27030 shift_by_width += glyph->pixel_width;
27031
27032 /* Get the width of the region to shift right. */
27033 shifted_region_width = (window_box_width (w, updated_area)
27034 - w->output_cursor.x
27035 - shift_by_width);
27036
27037 /* Shift right. */
27038 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27039 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27040
27041 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27042 line_height, shift_by_width);
27043
27044 /* Write the glyphs. */
27045 hpos = start - row->glyphs[updated_area];
27046 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27047 hpos, hpos + len,
27048 DRAW_NORMAL_TEXT, 0);
27049
27050 /* Advance the output cursor. */
27051 w->output_cursor.hpos += len;
27052 w->output_cursor.x += shift_by_width;
27053 unblock_input ();
27054 }
27055
27056
27057 /* EXPORT for RIF:
27058 Erase the current text line from the nominal cursor position
27059 (inclusive) to pixel column TO_X (exclusive). The idea is that
27060 everything from TO_X onward is already erased.
27061
27062 TO_X is a pixel position relative to UPDATED_AREA of currently
27063 updated window W. TO_X == -1 means clear to the end of this area. */
27064
27065 void
27066 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27067 enum glyph_row_area updated_area, int to_x)
27068 {
27069 struct frame *f;
27070 int max_x, min_y, max_y;
27071 int from_x, from_y, to_y;
27072
27073 eassert (updated_row);
27074 f = XFRAME (w->frame);
27075
27076 if (updated_row->full_width_p)
27077 max_x = (WINDOW_PIXEL_WIDTH (w)
27078 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27079 else
27080 max_x = window_box_width (w, updated_area);
27081 max_y = window_text_bottom_y (w);
27082
27083 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27084 of window. For TO_X > 0, truncate to end of drawing area. */
27085 if (to_x == 0)
27086 return;
27087 else if (to_x < 0)
27088 to_x = max_x;
27089 else
27090 to_x = min (to_x, max_x);
27091
27092 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27093
27094 /* Notice if the cursor will be cleared by this operation. */
27095 if (!updated_row->full_width_p)
27096 notice_overwritten_cursor (w, updated_area,
27097 w->output_cursor.x, -1,
27098 updated_row->y,
27099 MATRIX_ROW_BOTTOM_Y (updated_row));
27100
27101 from_x = w->output_cursor.x;
27102
27103 /* Translate to frame coordinates. */
27104 if (updated_row->full_width_p)
27105 {
27106 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27107 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27108 }
27109 else
27110 {
27111 int area_left = window_box_left (w, updated_area);
27112 from_x += area_left;
27113 to_x += area_left;
27114 }
27115
27116 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27117 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27118 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27119
27120 /* Prevent inadvertently clearing to end of the X window. */
27121 if (to_x > from_x && to_y > from_y)
27122 {
27123 block_input ();
27124 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27125 to_x - from_x, to_y - from_y);
27126 unblock_input ();
27127 }
27128 }
27129
27130 #endif /* HAVE_WINDOW_SYSTEM */
27131
27132
27133 \f
27134 /***********************************************************************
27135 Cursor types
27136 ***********************************************************************/
27137
27138 /* Value is the internal representation of the specified cursor type
27139 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27140 of the bar cursor. */
27141
27142 static enum text_cursor_kinds
27143 get_specified_cursor_type (Lisp_Object arg, int *width)
27144 {
27145 enum text_cursor_kinds type;
27146
27147 if (NILP (arg))
27148 return NO_CURSOR;
27149
27150 if (EQ (arg, Qbox))
27151 return FILLED_BOX_CURSOR;
27152
27153 if (EQ (arg, Qhollow))
27154 return HOLLOW_BOX_CURSOR;
27155
27156 if (EQ (arg, Qbar))
27157 {
27158 *width = 2;
27159 return BAR_CURSOR;
27160 }
27161
27162 if (CONSP (arg)
27163 && EQ (XCAR (arg), Qbar)
27164 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27165 {
27166 *width = XINT (XCDR (arg));
27167 return BAR_CURSOR;
27168 }
27169
27170 if (EQ (arg, Qhbar))
27171 {
27172 *width = 2;
27173 return HBAR_CURSOR;
27174 }
27175
27176 if (CONSP (arg)
27177 && EQ (XCAR (arg), Qhbar)
27178 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27179 {
27180 *width = XINT (XCDR (arg));
27181 return HBAR_CURSOR;
27182 }
27183
27184 /* Treat anything unknown as "hollow box cursor".
27185 It was bad to signal an error; people have trouble fixing
27186 .Xdefaults with Emacs, when it has something bad in it. */
27187 type = HOLLOW_BOX_CURSOR;
27188
27189 return type;
27190 }
27191
27192 /* Set the default cursor types for specified frame. */
27193 void
27194 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27195 {
27196 int width = 1;
27197 Lisp_Object tem;
27198
27199 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27200 FRAME_CURSOR_WIDTH (f) = width;
27201
27202 /* By default, set up the blink-off state depending on the on-state. */
27203
27204 tem = Fassoc (arg, Vblink_cursor_alist);
27205 if (!NILP (tem))
27206 {
27207 FRAME_BLINK_OFF_CURSOR (f)
27208 = get_specified_cursor_type (XCDR (tem), &width);
27209 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27210 }
27211 else
27212 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27213
27214 /* Make sure the cursor gets redrawn. */
27215 f->cursor_type_changed = true;
27216 }
27217
27218
27219 #ifdef HAVE_WINDOW_SYSTEM
27220
27221 /* Return the cursor we want to be displayed in window W. Return
27222 width of bar/hbar cursor through WIDTH arg. Return with
27223 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
27224 (i.e. if the `system caret' should track this cursor).
27225
27226 In a mini-buffer window, we want the cursor only to appear if we
27227 are reading input from this window. For the selected window, we
27228 want the cursor type given by the frame parameter or buffer local
27229 setting of cursor-type. If explicitly marked off, draw no cursor.
27230 In all other cases, we want a hollow box cursor. */
27231
27232 static enum text_cursor_kinds
27233 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27234 bool *active_cursor)
27235 {
27236 struct frame *f = XFRAME (w->frame);
27237 struct buffer *b = XBUFFER (w->contents);
27238 int cursor_type = DEFAULT_CURSOR;
27239 Lisp_Object alt_cursor;
27240 bool non_selected = false;
27241
27242 *active_cursor = true;
27243
27244 /* Echo area */
27245 if (cursor_in_echo_area
27246 && FRAME_HAS_MINIBUF_P (f)
27247 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27248 {
27249 if (w == XWINDOW (echo_area_window))
27250 {
27251 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27252 {
27253 *width = FRAME_CURSOR_WIDTH (f);
27254 return FRAME_DESIRED_CURSOR (f);
27255 }
27256 else
27257 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27258 }
27259
27260 *active_cursor = false;
27261 non_selected = true;
27262 }
27263
27264 /* Detect a nonselected window or nonselected frame. */
27265 else if (w != XWINDOW (f->selected_window)
27266 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27267 {
27268 *active_cursor = false;
27269
27270 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27271 return NO_CURSOR;
27272
27273 non_selected = true;
27274 }
27275
27276 /* Never display a cursor in a window in which cursor-type is nil. */
27277 if (NILP (BVAR (b, cursor_type)))
27278 return NO_CURSOR;
27279
27280 /* Get the normal cursor type for this window. */
27281 if (EQ (BVAR (b, cursor_type), Qt))
27282 {
27283 cursor_type = FRAME_DESIRED_CURSOR (f);
27284 *width = FRAME_CURSOR_WIDTH (f);
27285 }
27286 else
27287 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27288
27289 /* Use cursor-in-non-selected-windows instead
27290 for non-selected window or frame. */
27291 if (non_selected)
27292 {
27293 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27294 if (!EQ (Qt, alt_cursor))
27295 return get_specified_cursor_type (alt_cursor, width);
27296 /* t means modify the normal cursor type. */
27297 if (cursor_type == FILLED_BOX_CURSOR)
27298 cursor_type = HOLLOW_BOX_CURSOR;
27299 else if (cursor_type == BAR_CURSOR && *width > 1)
27300 --*width;
27301 return cursor_type;
27302 }
27303
27304 /* Use normal cursor if not blinked off. */
27305 if (!w->cursor_off_p)
27306 {
27307 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27308 {
27309 if (cursor_type == FILLED_BOX_CURSOR)
27310 {
27311 /* Using a block cursor on large images can be very annoying.
27312 So use a hollow cursor for "large" images.
27313 If image is not transparent (no mask), also use hollow cursor. */
27314 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27315 if (img != NULL && IMAGEP (img->spec))
27316 {
27317 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27318 where N = size of default frame font size.
27319 This should cover most of the "tiny" icons people may use. */
27320 if (!img->mask
27321 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27322 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27323 cursor_type = HOLLOW_BOX_CURSOR;
27324 }
27325 }
27326 else if (cursor_type != NO_CURSOR)
27327 {
27328 /* Display current only supports BOX and HOLLOW cursors for images.
27329 So for now, unconditionally use a HOLLOW cursor when cursor is
27330 not a solid box cursor. */
27331 cursor_type = HOLLOW_BOX_CURSOR;
27332 }
27333 }
27334 return cursor_type;
27335 }
27336
27337 /* Cursor is blinked off, so determine how to "toggle" it. */
27338
27339 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27340 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27341 return get_specified_cursor_type (XCDR (alt_cursor), width);
27342
27343 /* Then see if frame has specified a specific blink off cursor type. */
27344 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27345 {
27346 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27347 return FRAME_BLINK_OFF_CURSOR (f);
27348 }
27349
27350 #if false
27351 /* Some people liked having a permanently visible blinking cursor,
27352 while others had very strong opinions against it. So it was
27353 decided to remove it. KFS 2003-09-03 */
27354
27355 /* Finally perform built-in cursor blinking:
27356 filled box <-> hollow box
27357 wide [h]bar <-> narrow [h]bar
27358 narrow [h]bar <-> no cursor
27359 other type <-> no cursor */
27360
27361 if (cursor_type == FILLED_BOX_CURSOR)
27362 return HOLLOW_BOX_CURSOR;
27363
27364 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27365 {
27366 *width = 1;
27367 return cursor_type;
27368 }
27369 #endif
27370
27371 return NO_CURSOR;
27372 }
27373
27374
27375 /* Notice when the text cursor of window W has been completely
27376 overwritten by a drawing operation that outputs glyphs in AREA
27377 starting at X0 and ending at X1 in the line starting at Y0 and
27378 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27379 the rest of the line after X0 has been written. Y coordinates
27380 are window-relative. */
27381
27382 static void
27383 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27384 int x0, int x1, int y0, int y1)
27385 {
27386 int cx0, cx1, cy0, cy1;
27387 struct glyph_row *row;
27388
27389 if (!w->phys_cursor_on_p)
27390 return;
27391 if (area != TEXT_AREA)
27392 return;
27393
27394 if (w->phys_cursor.vpos < 0
27395 || w->phys_cursor.vpos >= w->current_matrix->nrows
27396 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27397 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27398 return;
27399
27400 if (row->cursor_in_fringe_p)
27401 {
27402 row->cursor_in_fringe_p = false;
27403 draw_fringe_bitmap (w, row, row->reversed_p);
27404 w->phys_cursor_on_p = false;
27405 return;
27406 }
27407
27408 cx0 = w->phys_cursor.x;
27409 cx1 = cx0 + w->phys_cursor_width;
27410 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27411 return;
27412
27413 /* The cursor image will be completely removed from the
27414 screen if the output area intersects the cursor area in
27415 y-direction. When we draw in [y0 y1[, and some part of
27416 the cursor is at y < y0, that part must have been drawn
27417 before. When scrolling, the cursor is erased before
27418 actually scrolling, so we don't come here. When not
27419 scrolling, the rows above the old cursor row must have
27420 changed, and in this case these rows must have written
27421 over the cursor image.
27422
27423 Likewise if part of the cursor is below y1, with the
27424 exception of the cursor being in the first blank row at
27425 the buffer and window end because update_text_area
27426 doesn't draw that row. (Except when it does, but
27427 that's handled in update_text_area.) */
27428
27429 cy0 = w->phys_cursor.y;
27430 cy1 = cy0 + w->phys_cursor_height;
27431 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27432 return;
27433
27434 w->phys_cursor_on_p = false;
27435 }
27436
27437 #endif /* HAVE_WINDOW_SYSTEM */
27438
27439 \f
27440 /************************************************************************
27441 Mouse Face
27442 ************************************************************************/
27443
27444 #ifdef HAVE_WINDOW_SYSTEM
27445
27446 /* EXPORT for RIF:
27447 Fix the display of area AREA of overlapping row ROW in window W
27448 with respect to the overlapping part OVERLAPS. */
27449
27450 void
27451 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27452 enum glyph_row_area area, int overlaps)
27453 {
27454 int i, x;
27455
27456 block_input ();
27457
27458 x = 0;
27459 for (i = 0; i < row->used[area];)
27460 {
27461 if (row->glyphs[area][i].overlaps_vertically_p)
27462 {
27463 int start = i, start_x = x;
27464
27465 do
27466 {
27467 x += row->glyphs[area][i].pixel_width;
27468 ++i;
27469 }
27470 while (i < row->used[area]
27471 && row->glyphs[area][i].overlaps_vertically_p);
27472
27473 draw_glyphs (w, start_x, row, area,
27474 start, i,
27475 DRAW_NORMAL_TEXT, overlaps);
27476 }
27477 else
27478 {
27479 x += row->glyphs[area][i].pixel_width;
27480 ++i;
27481 }
27482 }
27483
27484 unblock_input ();
27485 }
27486
27487
27488 /* EXPORT:
27489 Draw the cursor glyph of window W in glyph row ROW. See the
27490 comment of draw_glyphs for the meaning of HL. */
27491
27492 void
27493 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27494 enum draw_glyphs_face hl)
27495 {
27496 /* If cursor hpos is out of bounds, don't draw garbage. This can
27497 happen in mini-buffer windows when switching between echo area
27498 glyphs and mini-buffer. */
27499 if ((row->reversed_p
27500 ? (w->phys_cursor.hpos >= 0)
27501 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27502 {
27503 bool on_p = w->phys_cursor_on_p;
27504 int x1;
27505 int hpos = w->phys_cursor.hpos;
27506
27507 /* When the window is hscrolled, cursor hpos can legitimately be
27508 out of bounds, but we draw the cursor at the corresponding
27509 window margin in that case. */
27510 if (!row->reversed_p && hpos < 0)
27511 hpos = 0;
27512 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27513 hpos = row->used[TEXT_AREA] - 1;
27514
27515 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27516 hl, 0);
27517 w->phys_cursor_on_p = on_p;
27518
27519 if (hl == DRAW_CURSOR)
27520 w->phys_cursor_width = x1 - w->phys_cursor.x;
27521 /* When we erase the cursor, and ROW is overlapped by other
27522 rows, make sure that these overlapping parts of other rows
27523 are redrawn. */
27524 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27525 {
27526 w->phys_cursor_width = x1 - w->phys_cursor.x;
27527
27528 if (row > w->current_matrix->rows
27529 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27530 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27531 OVERLAPS_ERASED_CURSOR);
27532
27533 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27534 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27535 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27536 OVERLAPS_ERASED_CURSOR);
27537 }
27538 }
27539 }
27540
27541
27542 /* Erase the image of a cursor of window W from the screen. */
27543
27544 void
27545 erase_phys_cursor (struct window *w)
27546 {
27547 struct frame *f = XFRAME (w->frame);
27548 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27549 int hpos = w->phys_cursor.hpos;
27550 int vpos = w->phys_cursor.vpos;
27551 bool mouse_face_here_p = false;
27552 struct glyph_matrix *active_glyphs = w->current_matrix;
27553 struct glyph_row *cursor_row;
27554 struct glyph *cursor_glyph;
27555 enum draw_glyphs_face hl;
27556
27557 /* No cursor displayed or row invalidated => nothing to do on the
27558 screen. */
27559 if (w->phys_cursor_type == NO_CURSOR)
27560 goto mark_cursor_off;
27561
27562 /* VPOS >= active_glyphs->nrows means that window has been resized.
27563 Don't bother to erase the cursor. */
27564 if (vpos >= active_glyphs->nrows)
27565 goto mark_cursor_off;
27566
27567 /* If row containing cursor is marked invalid, there is nothing we
27568 can do. */
27569 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27570 if (!cursor_row->enabled_p)
27571 goto mark_cursor_off;
27572
27573 /* If line spacing is > 0, old cursor may only be partially visible in
27574 window after split-window. So adjust visible height. */
27575 cursor_row->visible_height = min (cursor_row->visible_height,
27576 window_text_bottom_y (w) - cursor_row->y);
27577
27578 /* If row is completely invisible, don't attempt to delete a cursor which
27579 isn't there. This can happen if cursor is at top of a window, and
27580 we switch to a buffer with a header line in that window. */
27581 if (cursor_row->visible_height <= 0)
27582 goto mark_cursor_off;
27583
27584 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27585 if (cursor_row->cursor_in_fringe_p)
27586 {
27587 cursor_row->cursor_in_fringe_p = false;
27588 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27589 goto mark_cursor_off;
27590 }
27591
27592 /* This can happen when the new row is shorter than the old one.
27593 In this case, either draw_glyphs or clear_end_of_line
27594 should have cleared the cursor. Note that we wouldn't be
27595 able to erase the cursor in this case because we don't have a
27596 cursor glyph at hand. */
27597 if ((cursor_row->reversed_p
27598 ? (w->phys_cursor.hpos < 0)
27599 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27600 goto mark_cursor_off;
27601
27602 /* When the window is hscrolled, cursor hpos can legitimately be out
27603 of bounds, but we draw the cursor at the corresponding window
27604 margin in that case. */
27605 if (!cursor_row->reversed_p && hpos < 0)
27606 hpos = 0;
27607 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27608 hpos = cursor_row->used[TEXT_AREA] - 1;
27609
27610 /* If the cursor is in the mouse face area, redisplay that when
27611 we clear the cursor. */
27612 if (! NILP (hlinfo->mouse_face_window)
27613 && coords_in_mouse_face_p (w, hpos, vpos)
27614 /* Don't redraw the cursor's spot in mouse face if it is at the
27615 end of a line (on a newline). The cursor appears there, but
27616 mouse highlighting does not. */
27617 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27618 mouse_face_here_p = true;
27619
27620 /* Maybe clear the display under the cursor. */
27621 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27622 {
27623 int x, y;
27624 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27625 int width;
27626
27627 cursor_glyph = get_phys_cursor_glyph (w);
27628 if (cursor_glyph == NULL)
27629 goto mark_cursor_off;
27630
27631 width = cursor_glyph->pixel_width;
27632 x = w->phys_cursor.x;
27633 if (x < 0)
27634 {
27635 width += x;
27636 x = 0;
27637 }
27638 width = min (width, window_box_width (w, TEXT_AREA) - x);
27639 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27640 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
27641
27642 if (width > 0)
27643 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27644 }
27645
27646 /* Erase the cursor by redrawing the character underneath it. */
27647 if (mouse_face_here_p)
27648 hl = DRAW_MOUSE_FACE;
27649 else
27650 hl = DRAW_NORMAL_TEXT;
27651 draw_phys_cursor_glyph (w, cursor_row, hl);
27652
27653 mark_cursor_off:
27654 w->phys_cursor_on_p = false;
27655 w->phys_cursor_type = NO_CURSOR;
27656 }
27657
27658
27659 /* Display or clear cursor of window W. If !ON, clear the cursor.
27660 If ON, display the cursor; where to put the cursor is specified by
27661 HPOS, VPOS, X and Y. */
27662
27663 void
27664 display_and_set_cursor (struct window *w, bool on,
27665 int hpos, int vpos, int x, int y)
27666 {
27667 struct frame *f = XFRAME (w->frame);
27668 int new_cursor_type;
27669 int new_cursor_width;
27670 bool active_cursor;
27671 struct glyph_row *glyph_row;
27672 struct glyph *glyph;
27673
27674 /* This is pointless on invisible frames, and dangerous on garbaged
27675 windows and frames; in the latter case, the frame or window may
27676 be in the midst of changing its size, and x and y may be off the
27677 window. */
27678 if (! FRAME_VISIBLE_P (f)
27679 || FRAME_GARBAGED_P (f)
27680 || vpos >= w->current_matrix->nrows
27681 || hpos >= w->current_matrix->matrix_w)
27682 return;
27683
27684 /* If cursor is off and we want it off, return quickly. */
27685 if (!on && !w->phys_cursor_on_p)
27686 return;
27687
27688 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27689 /* If cursor row is not enabled, we don't really know where to
27690 display the cursor. */
27691 if (!glyph_row->enabled_p)
27692 {
27693 w->phys_cursor_on_p = false;
27694 return;
27695 }
27696
27697 glyph = NULL;
27698 if (!glyph_row->exact_window_width_line_p
27699 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27700 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27701
27702 eassert (input_blocked_p ());
27703
27704 /* Set new_cursor_type to the cursor we want to be displayed. */
27705 new_cursor_type = get_window_cursor_type (w, glyph,
27706 &new_cursor_width, &active_cursor);
27707
27708 /* If cursor is currently being shown and we don't want it to be or
27709 it is in the wrong place, or the cursor type is not what we want,
27710 erase it. */
27711 if (w->phys_cursor_on_p
27712 && (!on
27713 || w->phys_cursor.x != x
27714 || w->phys_cursor.y != y
27715 /* HPOS can be negative in R2L rows whose
27716 exact_window_width_line_p flag is set (i.e. their newline
27717 would "overflow into the fringe"). */
27718 || hpos < 0
27719 || new_cursor_type != w->phys_cursor_type
27720 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27721 && new_cursor_width != w->phys_cursor_width)))
27722 erase_phys_cursor (w);
27723
27724 /* Don't check phys_cursor_on_p here because that flag is only set
27725 to false in some cases where we know that the cursor has been
27726 completely erased, to avoid the extra work of erasing the cursor
27727 twice. In other words, phys_cursor_on_p can be true and the cursor
27728 still not be visible, or it has only been partly erased. */
27729 if (on)
27730 {
27731 w->phys_cursor_ascent = glyph_row->ascent;
27732 w->phys_cursor_height = glyph_row->height;
27733
27734 /* Set phys_cursor_.* before x_draw_.* is called because some
27735 of them may need the information. */
27736 w->phys_cursor.x = x;
27737 w->phys_cursor.y = glyph_row->y;
27738 w->phys_cursor.hpos = hpos;
27739 w->phys_cursor.vpos = vpos;
27740 }
27741
27742 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27743 new_cursor_type, new_cursor_width,
27744 on, active_cursor);
27745 }
27746
27747
27748 /* Switch the display of W's cursor on or off, according to the value
27749 of ON. */
27750
27751 static void
27752 update_window_cursor (struct window *w, bool on)
27753 {
27754 /* Don't update cursor in windows whose frame is in the process
27755 of being deleted. */
27756 if (w->current_matrix)
27757 {
27758 int hpos = w->phys_cursor.hpos;
27759 int vpos = w->phys_cursor.vpos;
27760 struct glyph_row *row;
27761
27762 if (vpos >= w->current_matrix->nrows
27763 || hpos >= w->current_matrix->matrix_w)
27764 return;
27765
27766 row = MATRIX_ROW (w->current_matrix, vpos);
27767
27768 /* When the window is hscrolled, cursor hpos can legitimately be
27769 out of bounds, but we draw the cursor at the corresponding
27770 window margin in that case. */
27771 if (!row->reversed_p && hpos < 0)
27772 hpos = 0;
27773 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27774 hpos = row->used[TEXT_AREA] - 1;
27775
27776 block_input ();
27777 display_and_set_cursor (w, on, hpos, vpos,
27778 w->phys_cursor.x, w->phys_cursor.y);
27779 unblock_input ();
27780 }
27781 }
27782
27783
27784 /* Call update_window_cursor with parameter ON_P on all leaf windows
27785 in the window tree rooted at W. */
27786
27787 static void
27788 update_cursor_in_window_tree (struct window *w, bool on_p)
27789 {
27790 while (w)
27791 {
27792 if (WINDOWP (w->contents))
27793 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27794 else
27795 update_window_cursor (w, on_p);
27796
27797 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27798 }
27799 }
27800
27801
27802 /* EXPORT:
27803 Display the cursor on window W, or clear it, according to ON_P.
27804 Don't change the cursor's position. */
27805
27806 void
27807 x_update_cursor (struct frame *f, bool on_p)
27808 {
27809 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27810 }
27811
27812
27813 /* EXPORT:
27814 Clear the cursor of window W to background color, and mark the
27815 cursor as not shown. This is used when the text where the cursor
27816 is about to be rewritten. */
27817
27818 void
27819 x_clear_cursor (struct window *w)
27820 {
27821 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27822 update_window_cursor (w, false);
27823 }
27824
27825 #endif /* HAVE_WINDOW_SYSTEM */
27826
27827 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27828 and MSDOS. */
27829 static void
27830 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27831 int start_hpos, int end_hpos,
27832 enum draw_glyphs_face draw)
27833 {
27834 #ifdef HAVE_WINDOW_SYSTEM
27835 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27836 {
27837 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27838 return;
27839 }
27840 #endif
27841 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27842 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27843 #endif
27844 }
27845
27846 /* Display the active region described by mouse_face_* according to DRAW. */
27847
27848 static void
27849 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27850 {
27851 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27852 struct frame *f = XFRAME (WINDOW_FRAME (w));
27853
27854 if (/* If window is in the process of being destroyed, don't bother
27855 to do anything. */
27856 w->current_matrix != NULL
27857 /* Don't update mouse highlight if hidden. */
27858 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27859 /* Recognize when we are called to operate on rows that don't exist
27860 anymore. This can happen when a window is split. */
27861 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27862 {
27863 bool phys_cursor_on_p = w->phys_cursor_on_p;
27864 struct glyph_row *row, *first, *last;
27865
27866 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27867 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27868
27869 for (row = first; row <= last && row->enabled_p; ++row)
27870 {
27871 int start_hpos, end_hpos, start_x;
27872
27873 /* For all but the first row, the highlight starts at column 0. */
27874 if (row == first)
27875 {
27876 /* R2L rows have BEG and END in reversed order, but the
27877 screen drawing geometry is always left to right. So
27878 we need to mirror the beginning and end of the
27879 highlighted area in R2L rows. */
27880 if (!row->reversed_p)
27881 {
27882 start_hpos = hlinfo->mouse_face_beg_col;
27883 start_x = hlinfo->mouse_face_beg_x;
27884 }
27885 else if (row == last)
27886 {
27887 start_hpos = hlinfo->mouse_face_end_col;
27888 start_x = hlinfo->mouse_face_end_x;
27889 }
27890 else
27891 {
27892 start_hpos = 0;
27893 start_x = 0;
27894 }
27895 }
27896 else if (row->reversed_p && row == last)
27897 {
27898 start_hpos = hlinfo->mouse_face_end_col;
27899 start_x = hlinfo->mouse_face_end_x;
27900 }
27901 else
27902 {
27903 start_hpos = 0;
27904 start_x = 0;
27905 }
27906
27907 if (row == last)
27908 {
27909 if (!row->reversed_p)
27910 end_hpos = hlinfo->mouse_face_end_col;
27911 else if (row == first)
27912 end_hpos = hlinfo->mouse_face_beg_col;
27913 else
27914 {
27915 end_hpos = row->used[TEXT_AREA];
27916 if (draw == DRAW_NORMAL_TEXT)
27917 row->fill_line_p = true; /* Clear to end of line. */
27918 }
27919 }
27920 else if (row->reversed_p && row == first)
27921 end_hpos = hlinfo->mouse_face_beg_col;
27922 else
27923 {
27924 end_hpos = row->used[TEXT_AREA];
27925 if (draw == DRAW_NORMAL_TEXT)
27926 row->fill_line_p = true; /* Clear to end of line. */
27927 }
27928
27929 if (end_hpos > start_hpos)
27930 {
27931 draw_row_with_mouse_face (w, start_x, row,
27932 start_hpos, end_hpos, draw);
27933
27934 row->mouse_face_p
27935 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27936 }
27937 }
27938
27939 #ifdef HAVE_WINDOW_SYSTEM
27940 /* When we've written over the cursor, arrange for it to
27941 be displayed again. */
27942 if (FRAME_WINDOW_P (f)
27943 && phys_cursor_on_p && !w->phys_cursor_on_p)
27944 {
27945 int hpos = w->phys_cursor.hpos;
27946
27947 /* When the window is hscrolled, cursor hpos can legitimately be
27948 out of bounds, but we draw the cursor at the corresponding
27949 window margin in that case. */
27950 if (!row->reversed_p && hpos < 0)
27951 hpos = 0;
27952 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27953 hpos = row->used[TEXT_AREA] - 1;
27954
27955 block_input ();
27956 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
27957 w->phys_cursor.x, w->phys_cursor.y);
27958 unblock_input ();
27959 }
27960 #endif /* HAVE_WINDOW_SYSTEM */
27961 }
27962
27963 #ifdef HAVE_WINDOW_SYSTEM
27964 /* Change the mouse cursor. */
27965 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
27966 {
27967 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27968 if (draw == DRAW_NORMAL_TEXT
27969 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27970 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27971 else
27972 #endif
27973 if (draw == DRAW_MOUSE_FACE)
27974 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27975 else
27976 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27977 }
27978 #endif /* HAVE_WINDOW_SYSTEM */
27979 }
27980
27981 /* EXPORT:
27982 Clear out the mouse-highlighted active region.
27983 Redraw it un-highlighted first. Value is true if mouse
27984 face was actually drawn unhighlighted. */
27985
27986 bool
27987 clear_mouse_face (Mouse_HLInfo *hlinfo)
27988 {
27989 bool cleared
27990 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
27991 if (cleared)
27992 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27993 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27994 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27995 hlinfo->mouse_face_window = Qnil;
27996 hlinfo->mouse_face_overlay = Qnil;
27997 return cleared;
27998 }
27999
28000 /* Return true if the coordinates HPOS and VPOS on windows W are
28001 within the mouse face on that window. */
28002 static bool
28003 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28004 {
28005 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28006
28007 /* Quickly resolve the easy cases. */
28008 if (!(WINDOWP (hlinfo->mouse_face_window)
28009 && XWINDOW (hlinfo->mouse_face_window) == w))
28010 return false;
28011 if (vpos < hlinfo->mouse_face_beg_row
28012 || vpos > hlinfo->mouse_face_end_row)
28013 return false;
28014 if (vpos > hlinfo->mouse_face_beg_row
28015 && vpos < hlinfo->mouse_face_end_row)
28016 return true;
28017
28018 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28019 {
28020 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28021 {
28022 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28023 return true;
28024 }
28025 else if ((vpos == hlinfo->mouse_face_beg_row
28026 && hpos >= hlinfo->mouse_face_beg_col)
28027 || (vpos == hlinfo->mouse_face_end_row
28028 && hpos < hlinfo->mouse_face_end_col))
28029 return true;
28030 }
28031 else
28032 {
28033 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28034 {
28035 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28036 return true;
28037 }
28038 else if ((vpos == hlinfo->mouse_face_beg_row
28039 && hpos <= hlinfo->mouse_face_beg_col)
28040 || (vpos == hlinfo->mouse_face_end_row
28041 && hpos > hlinfo->mouse_face_end_col))
28042 return true;
28043 }
28044 return false;
28045 }
28046
28047
28048 /* EXPORT:
28049 True if physical cursor of window W is within mouse face. */
28050
28051 bool
28052 cursor_in_mouse_face_p (struct window *w)
28053 {
28054 int hpos = w->phys_cursor.hpos;
28055 int vpos = w->phys_cursor.vpos;
28056 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28057
28058 /* When the window is hscrolled, cursor hpos can legitimately be out
28059 of bounds, but we draw the cursor at the corresponding window
28060 margin in that case. */
28061 if (!row->reversed_p && hpos < 0)
28062 hpos = 0;
28063 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28064 hpos = row->used[TEXT_AREA] - 1;
28065
28066 return coords_in_mouse_face_p (w, hpos, vpos);
28067 }
28068
28069
28070 \f
28071 /* Find the glyph rows START_ROW and END_ROW of window W that display
28072 characters between buffer positions START_CHARPOS and END_CHARPOS
28073 (excluding END_CHARPOS). DISP_STRING is a display string that
28074 covers these buffer positions. This is similar to
28075 row_containing_pos, but is more accurate when bidi reordering makes
28076 buffer positions change non-linearly with glyph rows. */
28077 static void
28078 rows_from_pos_range (struct window *w,
28079 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28080 Lisp_Object disp_string,
28081 struct glyph_row **start, struct glyph_row **end)
28082 {
28083 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28084 int last_y = window_text_bottom_y (w);
28085 struct glyph_row *row;
28086
28087 *start = NULL;
28088 *end = NULL;
28089
28090 while (!first->enabled_p
28091 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28092 first++;
28093
28094 /* Find the START row. */
28095 for (row = first;
28096 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28097 row++)
28098 {
28099 /* A row can potentially be the START row if the range of the
28100 characters it displays intersects the range
28101 [START_CHARPOS..END_CHARPOS). */
28102 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28103 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28104 /* See the commentary in row_containing_pos, for the
28105 explanation of the complicated way to check whether
28106 some position is beyond the end of the characters
28107 displayed by a row. */
28108 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28109 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28110 && !row->ends_at_zv_p
28111 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28112 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28113 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28114 && !row->ends_at_zv_p
28115 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28116 {
28117 /* Found a candidate row. Now make sure at least one of the
28118 glyphs it displays has a charpos from the range
28119 [START_CHARPOS..END_CHARPOS).
28120
28121 This is not obvious because bidi reordering could make
28122 buffer positions of a row be 1,2,3,102,101,100, and if we
28123 want to highlight characters in [50..60), we don't want
28124 this row, even though [50..60) does intersect [1..103),
28125 the range of character positions given by the row's start
28126 and end positions. */
28127 struct glyph *g = row->glyphs[TEXT_AREA];
28128 struct glyph *e = g + row->used[TEXT_AREA];
28129
28130 while (g < e)
28131 {
28132 if (((BUFFERP (g->object) || NILP (g->object))
28133 && start_charpos <= g->charpos && g->charpos < end_charpos)
28134 /* A glyph that comes from DISP_STRING is by
28135 definition to be highlighted. */
28136 || EQ (g->object, disp_string))
28137 *start = row;
28138 g++;
28139 }
28140 if (*start)
28141 break;
28142 }
28143 }
28144
28145 /* Find the END row. */
28146 if (!*start
28147 /* If the last row is partially visible, start looking for END
28148 from that row, instead of starting from FIRST. */
28149 && !(row->enabled_p
28150 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28151 row = first;
28152 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28153 {
28154 struct glyph_row *next = row + 1;
28155 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28156
28157 if (!next->enabled_p
28158 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28159 /* The first row >= START whose range of displayed characters
28160 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28161 is the row END + 1. */
28162 || (start_charpos < next_start
28163 && end_charpos < next_start)
28164 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28165 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28166 && !next->ends_at_zv_p
28167 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28168 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28169 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28170 && !next->ends_at_zv_p
28171 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28172 {
28173 *end = row;
28174 break;
28175 }
28176 else
28177 {
28178 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28179 but none of the characters it displays are in the range, it is
28180 also END + 1. */
28181 struct glyph *g = next->glyphs[TEXT_AREA];
28182 struct glyph *s = g;
28183 struct glyph *e = g + next->used[TEXT_AREA];
28184
28185 while (g < e)
28186 {
28187 if (((BUFFERP (g->object) || NILP (g->object))
28188 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28189 /* If the buffer position of the first glyph in
28190 the row is equal to END_CHARPOS, it means
28191 the last character to be highlighted is the
28192 newline of ROW, and we must consider NEXT as
28193 END, not END+1. */
28194 || (((!next->reversed_p && g == s)
28195 || (next->reversed_p && g == e - 1))
28196 && (g->charpos == end_charpos
28197 /* Special case for when NEXT is an
28198 empty line at ZV. */
28199 || (g->charpos == -1
28200 && !row->ends_at_zv_p
28201 && next_start == end_charpos)))))
28202 /* A glyph that comes from DISP_STRING is by
28203 definition to be highlighted. */
28204 || EQ (g->object, disp_string))
28205 break;
28206 g++;
28207 }
28208 if (g == e)
28209 {
28210 *end = row;
28211 break;
28212 }
28213 /* The first row that ends at ZV must be the last to be
28214 highlighted. */
28215 else if (next->ends_at_zv_p)
28216 {
28217 *end = next;
28218 break;
28219 }
28220 }
28221 }
28222 }
28223
28224 /* This function sets the mouse_face_* elements of HLINFO, assuming
28225 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28226 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28227 for the overlay or run of text properties specifying the mouse
28228 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28229 before-string and after-string that must also be highlighted.
28230 DISP_STRING, if non-nil, is a display string that may cover some
28231 or all of the highlighted text. */
28232
28233 static void
28234 mouse_face_from_buffer_pos (Lisp_Object window,
28235 Mouse_HLInfo *hlinfo,
28236 ptrdiff_t mouse_charpos,
28237 ptrdiff_t start_charpos,
28238 ptrdiff_t end_charpos,
28239 Lisp_Object before_string,
28240 Lisp_Object after_string,
28241 Lisp_Object disp_string)
28242 {
28243 struct window *w = XWINDOW (window);
28244 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28245 struct glyph_row *r1, *r2;
28246 struct glyph *glyph, *end;
28247 ptrdiff_t ignore, pos;
28248 int x;
28249
28250 eassert (NILP (disp_string) || STRINGP (disp_string));
28251 eassert (NILP (before_string) || STRINGP (before_string));
28252 eassert (NILP (after_string) || STRINGP (after_string));
28253
28254 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28255 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28256 if (r1 == NULL)
28257 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28258 /* If the before-string or display-string contains newlines,
28259 rows_from_pos_range skips to its last row. Move back. */
28260 if (!NILP (before_string) || !NILP (disp_string))
28261 {
28262 struct glyph_row *prev;
28263 while ((prev = r1 - 1, prev >= first)
28264 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28265 && prev->used[TEXT_AREA] > 0)
28266 {
28267 struct glyph *beg = prev->glyphs[TEXT_AREA];
28268 glyph = beg + prev->used[TEXT_AREA];
28269 while (--glyph >= beg && NILP (glyph->object));
28270 if (glyph < beg
28271 || !(EQ (glyph->object, before_string)
28272 || EQ (glyph->object, disp_string)))
28273 break;
28274 r1 = prev;
28275 }
28276 }
28277 if (r2 == NULL)
28278 {
28279 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28280 hlinfo->mouse_face_past_end = true;
28281 }
28282 else if (!NILP (after_string))
28283 {
28284 /* If the after-string has newlines, advance to its last row. */
28285 struct glyph_row *next;
28286 struct glyph_row *last
28287 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28288
28289 for (next = r2 + 1;
28290 next <= last
28291 && next->used[TEXT_AREA] > 0
28292 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28293 ++next)
28294 r2 = next;
28295 }
28296 /* The rest of the display engine assumes that mouse_face_beg_row is
28297 either above mouse_face_end_row or identical to it. But with
28298 bidi-reordered continued lines, the row for START_CHARPOS could
28299 be below the row for END_CHARPOS. If so, swap the rows and store
28300 them in correct order. */
28301 if (r1->y > r2->y)
28302 {
28303 struct glyph_row *tem = r2;
28304
28305 r2 = r1;
28306 r1 = tem;
28307 }
28308
28309 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28310 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28311
28312 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28313 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28314 could be anywhere in the row and in any order. The strategy
28315 below is to find the leftmost and the rightmost glyph that
28316 belongs to either of these 3 strings, or whose position is
28317 between START_CHARPOS and END_CHARPOS, and highlight all the
28318 glyphs between those two. This may cover more than just the text
28319 between START_CHARPOS and END_CHARPOS if the range of characters
28320 strides the bidi level boundary, e.g. if the beginning is in R2L
28321 text while the end is in L2R text or vice versa. */
28322 if (!r1->reversed_p)
28323 {
28324 /* This row is in a left to right paragraph. Scan it left to
28325 right. */
28326 glyph = r1->glyphs[TEXT_AREA];
28327 end = glyph + r1->used[TEXT_AREA];
28328 x = r1->x;
28329
28330 /* Skip truncation glyphs at the start of the glyph row. */
28331 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28332 for (; glyph < end
28333 && NILP (glyph->object)
28334 && glyph->charpos < 0;
28335 ++glyph)
28336 x += glyph->pixel_width;
28337
28338 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28339 or DISP_STRING, and the first glyph from buffer whose
28340 position is between START_CHARPOS and END_CHARPOS. */
28341 for (; glyph < end
28342 && !NILP (glyph->object)
28343 && !EQ (glyph->object, disp_string)
28344 && !(BUFFERP (glyph->object)
28345 && (glyph->charpos >= start_charpos
28346 && glyph->charpos < end_charpos));
28347 ++glyph)
28348 {
28349 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28350 are present at buffer positions between START_CHARPOS and
28351 END_CHARPOS, or if they come from an overlay. */
28352 if (EQ (glyph->object, before_string))
28353 {
28354 pos = string_buffer_position (before_string,
28355 start_charpos);
28356 /* If pos == 0, it means before_string came from an
28357 overlay, not from a buffer position. */
28358 if (!pos || (pos >= start_charpos && pos < end_charpos))
28359 break;
28360 }
28361 else if (EQ (glyph->object, after_string))
28362 {
28363 pos = string_buffer_position (after_string, end_charpos);
28364 if (!pos || (pos >= start_charpos && pos < end_charpos))
28365 break;
28366 }
28367 x += glyph->pixel_width;
28368 }
28369 hlinfo->mouse_face_beg_x = x;
28370 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28371 }
28372 else
28373 {
28374 /* This row is in a right to left paragraph. Scan it right to
28375 left. */
28376 struct glyph *g;
28377
28378 end = r1->glyphs[TEXT_AREA] - 1;
28379 glyph = end + r1->used[TEXT_AREA];
28380
28381 /* Skip truncation glyphs at the start of the glyph row. */
28382 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28383 for (; glyph > end
28384 && NILP (glyph->object)
28385 && glyph->charpos < 0;
28386 --glyph)
28387 ;
28388
28389 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28390 or DISP_STRING, and the first glyph from buffer whose
28391 position is between START_CHARPOS and END_CHARPOS. */
28392 for (; glyph > end
28393 && !NILP (glyph->object)
28394 && !EQ (glyph->object, disp_string)
28395 && !(BUFFERP (glyph->object)
28396 && (glyph->charpos >= start_charpos
28397 && glyph->charpos < end_charpos));
28398 --glyph)
28399 {
28400 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28401 are present at buffer positions between START_CHARPOS and
28402 END_CHARPOS, or if they come from an overlay. */
28403 if (EQ (glyph->object, before_string))
28404 {
28405 pos = string_buffer_position (before_string, start_charpos);
28406 /* If pos == 0, it means before_string came from an
28407 overlay, not from a buffer position. */
28408 if (!pos || (pos >= start_charpos && pos < end_charpos))
28409 break;
28410 }
28411 else if (EQ (glyph->object, after_string))
28412 {
28413 pos = string_buffer_position (after_string, end_charpos);
28414 if (!pos || (pos >= start_charpos && pos < end_charpos))
28415 break;
28416 }
28417 }
28418
28419 glyph++; /* first glyph to the right of the highlighted area */
28420 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28421 x += g->pixel_width;
28422 hlinfo->mouse_face_beg_x = x;
28423 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28424 }
28425
28426 /* If the highlight ends in a different row, compute GLYPH and END
28427 for the end row. Otherwise, reuse the values computed above for
28428 the row where the highlight begins. */
28429 if (r2 != r1)
28430 {
28431 if (!r2->reversed_p)
28432 {
28433 glyph = r2->glyphs[TEXT_AREA];
28434 end = glyph + r2->used[TEXT_AREA];
28435 x = r2->x;
28436 }
28437 else
28438 {
28439 end = r2->glyphs[TEXT_AREA] - 1;
28440 glyph = end + r2->used[TEXT_AREA];
28441 }
28442 }
28443
28444 if (!r2->reversed_p)
28445 {
28446 /* Skip truncation and continuation glyphs near the end of the
28447 row, and also blanks and stretch glyphs inserted by
28448 extend_face_to_end_of_line. */
28449 while (end > glyph
28450 && NILP ((end - 1)->object))
28451 --end;
28452 /* Scan the rest of the glyph row from the end, looking for the
28453 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28454 DISP_STRING, or whose position is between START_CHARPOS
28455 and END_CHARPOS */
28456 for (--end;
28457 end > glyph
28458 && !NILP (end->object)
28459 && !EQ (end->object, disp_string)
28460 && !(BUFFERP (end->object)
28461 && (end->charpos >= start_charpos
28462 && end->charpos < end_charpos));
28463 --end)
28464 {
28465 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28466 are present at buffer positions between START_CHARPOS and
28467 END_CHARPOS, or if they come from an overlay. */
28468 if (EQ (end->object, before_string))
28469 {
28470 pos = string_buffer_position (before_string, start_charpos);
28471 if (!pos || (pos >= start_charpos && pos < end_charpos))
28472 break;
28473 }
28474 else if (EQ (end->object, after_string))
28475 {
28476 pos = string_buffer_position (after_string, end_charpos);
28477 if (!pos || (pos >= start_charpos && pos < end_charpos))
28478 break;
28479 }
28480 }
28481 /* Find the X coordinate of the last glyph to be highlighted. */
28482 for (; glyph <= end; ++glyph)
28483 x += glyph->pixel_width;
28484
28485 hlinfo->mouse_face_end_x = x;
28486 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28487 }
28488 else
28489 {
28490 /* Skip truncation and continuation glyphs near the end of the
28491 row, and also blanks and stretch glyphs inserted by
28492 extend_face_to_end_of_line. */
28493 x = r2->x;
28494 end++;
28495 while (end < glyph
28496 && NILP (end->object))
28497 {
28498 x += end->pixel_width;
28499 ++end;
28500 }
28501 /* Scan the rest of the glyph row from the end, looking for the
28502 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28503 DISP_STRING, or whose position is between START_CHARPOS
28504 and END_CHARPOS */
28505 for ( ;
28506 end < glyph
28507 && !NILP (end->object)
28508 && !EQ (end->object, disp_string)
28509 && !(BUFFERP (end->object)
28510 && (end->charpos >= start_charpos
28511 && end->charpos < end_charpos));
28512 ++end)
28513 {
28514 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28515 are present at buffer positions between START_CHARPOS and
28516 END_CHARPOS, or if they come from an overlay. */
28517 if (EQ (end->object, before_string))
28518 {
28519 pos = string_buffer_position (before_string, start_charpos);
28520 if (!pos || (pos >= start_charpos && pos < end_charpos))
28521 break;
28522 }
28523 else if (EQ (end->object, after_string))
28524 {
28525 pos = string_buffer_position (after_string, end_charpos);
28526 if (!pos || (pos >= start_charpos && pos < end_charpos))
28527 break;
28528 }
28529 x += end->pixel_width;
28530 }
28531 /* If we exited the above loop because we arrived at the last
28532 glyph of the row, and its buffer position is still not in
28533 range, it means the last character in range is the preceding
28534 newline. Bump the end column and x values to get past the
28535 last glyph. */
28536 if (end == glyph
28537 && BUFFERP (end->object)
28538 && (end->charpos < start_charpos
28539 || end->charpos >= end_charpos))
28540 {
28541 x += end->pixel_width;
28542 ++end;
28543 }
28544 hlinfo->mouse_face_end_x = x;
28545 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28546 }
28547
28548 hlinfo->mouse_face_window = window;
28549 hlinfo->mouse_face_face_id
28550 = face_at_buffer_position (w, mouse_charpos, &ignore,
28551 mouse_charpos + 1,
28552 !hlinfo->mouse_face_hidden, -1);
28553 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28554 }
28555
28556 /* The following function is not used anymore (replaced with
28557 mouse_face_from_string_pos), but I leave it here for the time
28558 being, in case someone would. */
28559
28560 #if false /* not used */
28561
28562 /* Find the position of the glyph for position POS in OBJECT in
28563 window W's current matrix, and return in *X, *Y the pixel
28564 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28565
28566 RIGHT_P means return the position of the right edge of the glyph.
28567 !RIGHT_P means return the left edge position.
28568
28569 If no glyph for POS exists in the matrix, return the position of
28570 the glyph with the next smaller position that is in the matrix, if
28571 RIGHT_P is false. If RIGHT_P, and no glyph for POS
28572 exists in the matrix, return the position of the glyph with the
28573 next larger position in OBJECT.
28574
28575 Value is true if a glyph was found. */
28576
28577 static bool
28578 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28579 int *hpos, int *vpos, int *x, int *y, bool right_p)
28580 {
28581 int yb = window_text_bottom_y (w);
28582 struct glyph_row *r;
28583 struct glyph *best_glyph = NULL;
28584 struct glyph_row *best_row = NULL;
28585 int best_x = 0;
28586
28587 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28588 r->enabled_p && r->y < yb;
28589 ++r)
28590 {
28591 struct glyph *g = r->glyphs[TEXT_AREA];
28592 struct glyph *e = g + r->used[TEXT_AREA];
28593 int gx;
28594
28595 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28596 if (EQ (g->object, object))
28597 {
28598 if (g->charpos == pos)
28599 {
28600 best_glyph = g;
28601 best_x = gx;
28602 best_row = r;
28603 goto found;
28604 }
28605 else if (best_glyph == NULL
28606 || ((eabs (g->charpos - pos)
28607 < eabs (best_glyph->charpos - pos))
28608 && (right_p
28609 ? g->charpos < pos
28610 : g->charpos > pos)))
28611 {
28612 best_glyph = g;
28613 best_x = gx;
28614 best_row = r;
28615 }
28616 }
28617 }
28618
28619 found:
28620
28621 if (best_glyph)
28622 {
28623 *x = best_x;
28624 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28625
28626 if (right_p)
28627 {
28628 *x += best_glyph->pixel_width;
28629 ++*hpos;
28630 }
28631
28632 *y = best_row->y;
28633 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28634 }
28635
28636 return best_glyph != NULL;
28637 }
28638 #endif /* not used */
28639
28640 /* Find the positions of the first and the last glyphs in window W's
28641 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28642 (assumed to be a string), and return in HLINFO's mouse_face_*
28643 members the pixel and column/row coordinates of those glyphs. */
28644
28645 static void
28646 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28647 Lisp_Object object,
28648 ptrdiff_t startpos, ptrdiff_t endpos)
28649 {
28650 int yb = window_text_bottom_y (w);
28651 struct glyph_row *r;
28652 struct glyph *g, *e;
28653 int gx;
28654 bool found = false;
28655
28656 /* Find the glyph row with at least one position in the range
28657 [STARTPOS..ENDPOS), and the first glyph in that row whose
28658 position belongs to that range. */
28659 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28660 r->enabled_p && r->y < yb;
28661 ++r)
28662 {
28663 if (!r->reversed_p)
28664 {
28665 g = r->glyphs[TEXT_AREA];
28666 e = g + r->used[TEXT_AREA];
28667 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28668 if (EQ (g->object, object)
28669 && startpos <= g->charpos && g->charpos < endpos)
28670 {
28671 hlinfo->mouse_face_beg_row
28672 = MATRIX_ROW_VPOS (r, w->current_matrix);
28673 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28674 hlinfo->mouse_face_beg_x = gx;
28675 found = true;
28676 break;
28677 }
28678 }
28679 else
28680 {
28681 struct glyph *g1;
28682
28683 e = r->glyphs[TEXT_AREA];
28684 g = e + r->used[TEXT_AREA];
28685 for ( ; g > e; --g)
28686 if (EQ ((g-1)->object, object)
28687 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28688 {
28689 hlinfo->mouse_face_beg_row
28690 = MATRIX_ROW_VPOS (r, w->current_matrix);
28691 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28692 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28693 gx += g1->pixel_width;
28694 hlinfo->mouse_face_beg_x = gx;
28695 found = true;
28696 break;
28697 }
28698 }
28699 if (found)
28700 break;
28701 }
28702
28703 if (!found)
28704 return;
28705
28706 /* Starting with the next row, look for the first row which does NOT
28707 include any glyphs whose positions are in the range. */
28708 for (++r; r->enabled_p && r->y < yb; ++r)
28709 {
28710 g = r->glyphs[TEXT_AREA];
28711 e = g + r->used[TEXT_AREA];
28712 found = false;
28713 for ( ; g < e; ++g)
28714 if (EQ (g->object, object)
28715 && startpos <= g->charpos && g->charpos < endpos)
28716 {
28717 found = true;
28718 break;
28719 }
28720 if (!found)
28721 break;
28722 }
28723
28724 /* The highlighted region ends on the previous row. */
28725 r--;
28726
28727 /* Set the end row. */
28728 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28729
28730 /* Compute and set the end column and the end column's horizontal
28731 pixel coordinate. */
28732 if (!r->reversed_p)
28733 {
28734 g = r->glyphs[TEXT_AREA];
28735 e = g + r->used[TEXT_AREA];
28736 for ( ; e > g; --e)
28737 if (EQ ((e-1)->object, object)
28738 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28739 break;
28740 hlinfo->mouse_face_end_col = e - g;
28741
28742 for (gx = r->x; g < e; ++g)
28743 gx += g->pixel_width;
28744 hlinfo->mouse_face_end_x = gx;
28745 }
28746 else
28747 {
28748 e = r->glyphs[TEXT_AREA];
28749 g = e + r->used[TEXT_AREA];
28750 for (gx = r->x ; e < g; ++e)
28751 {
28752 if (EQ (e->object, object)
28753 && startpos <= e->charpos && e->charpos < endpos)
28754 break;
28755 gx += e->pixel_width;
28756 }
28757 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28758 hlinfo->mouse_face_end_x = gx;
28759 }
28760 }
28761
28762 #ifdef HAVE_WINDOW_SYSTEM
28763
28764 /* See if position X, Y is within a hot-spot of an image. */
28765
28766 static bool
28767 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28768 {
28769 if (!CONSP (hot_spot))
28770 return false;
28771
28772 if (EQ (XCAR (hot_spot), Qrect))
28773 {
28774 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28775 Lisp_Object rect = XCDR (hot_spot);
28776 Lisp_Object tem;
28777 if (!CONSP (rect))
28778 return false;
28779 if (!CONSP (XCAR (rect)))
28780 return false;
28781 if (!CONSP (XCDR (rect)))
28782 return false;
28783 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28784 return false;
28785 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28786 return false;
28787 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28788 return false;
28789 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28790 return false;
28791 return true;
28792 }
28793 else if (EQ (XCAR (hot_spot), Qcircle))
28794 {
28795 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28796 Lisp_Object circ = XCDR (hot_spot);
28797 Lisp_Object lr, lx0, ly0;
28798 if (CONSP (circ)
28799 && CONSP (XCAR (circ))
28800 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28801 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28802 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28803 {
28804 double r = XFLOATINT (lr);
28805 double dx = XINT (lx0) - x;
28806 double dy = XINT (ly0) - y;
28807 return (dx * dx + dy * dy <= r * r);
28808 }
28809 }
28810 else if (EQ (XCAR (hot_spot), Qpoly))
28811 {
28812 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28813 if (VECTORP (XCDR (hot_spot)))
28814 {
28815 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28816 Lisp_Object *poly = v->contents;
28817 ptrdiff_t n = v->header.size;
28818 ptrdiff_t i;
28819 bool inside = false;
28820 Lisp_Object lx, ly;
28821 int x0, y0;
28822
28823 /* Need an even number of coordinates, and at least 3 edges. */
28824 if (n < 6 || n & 1)
28825 return false;
28826
28827 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28828 If count is odd, we are inside polygon. Pixels on edges
28829 may or may not be included depending on actual geometry of the
28830 polygon. */
28831 if ((lx = poly[n-2], !INTEGERP (lx))
28832 || (ly = poly[n-1], !INTEGERP (lx)))
28833 return false;
28834 x0 = XINT (lx), y0 = XINT (ly);
28835 for (i = 0; i < n; i += 2)
28836 {
28837 int x1 = x0, y1 = y0;
28838 if ((lx = poly[i], !INTEGERP (lx))
28839 || (ly = poly[i+1], !INTEGERP (ly)))
28840 return false;
28841 x0 = XINT (lx), y0 = XINT (ly);
28842
28843 /* Does this segment cross the X line? */
28844 if (x0 >= x)
28845 {
28846 if (x1 >= x)
28847 continue;
28848 }
28849 else if (x1 < x)
28850 continue;
28851 if (y > y0 && y > y1)
28852 continue;
28853 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28854 inside = !inside;
28855 }
28856 return inside;
28857 }
28858 }
28859 return false;
28860 }
28861
28862 Lisp_Object
28863 find_hot_spot (Lisp_Object map, int x, int y)
28864 {
28865 while (CONSP (map))
28866 {
28867 if (CONSP (XCAR (map))
28868 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28869 return XCAR (map);
28870 map = XCDR (map);
28871 }
28872
28873 return Qnil;
28874 }
28875
28876 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28877 3, 3, 0,
28878 doc: /* Lookup in image map MAP coordinates X and Y.
28879 An image map is an alist where each element has the format (AREA ID PLIST).
28880 An AREA is specified as either a rectangle, a circle, or a polygon:
28881 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28882 pixel coordinates of the upper left and bottom right corners.
28883 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28884 and the radius of the circle; r may be a float or integer.
28885 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28886 vector describes one corner in the polygon.
28887 Returns the alist element for the first matching AREA in MAP. */)
28888 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28889 {
28890 if (NILP (map))
28891 return Qnil;
28892
28893 CHECK_NUMBER (x);
28894 CHECK_NUMBER (y);
28895
28896 return find_hot_spot (map,
28897 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28898 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28899 }
28900
28901
28902 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28903 static void
28904 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28905 {
28906 /* Do not change cursor shape while dragging mouse. */
28907 if (!NILP (do_mouse_tracking))
28908 return;
28909
28910 if (!NILP (pointer))
28911 {
28912 if (EQ (pointer, Qarrow))
28913 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28914 else if (EQ (pointer, Qhand))
28915 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28916 else if (EQ (pointer, Qtext))
28917 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28918 else if (EQ (pointer, intern ("hdrag")))
28919 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28920 else if (EQ (pointer, intern ("nhdrag")))
28921 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28922 #ifdef HAVE_X_WINDOWS
28923 else if (EQ (pointer, intern ("vdrag")))
28924 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28925 #endif
28926 else if (EQ (pointer, intern ("hourglass")))
28927 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28928 else if (EQ (pointer, Qmodeline))
28929 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28930 else
28931 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28932 }
28933
28934 if (cursor != No_Cursor)
28935 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28936 }
28937
28938 #endif /* HAVE_WINDOW_SYSTEM */
28939
28940 /* Take proper action when mouse has moved to the mode or header line
28941 or marginal area AREA of window W, x-position X and y-position Y.
28942 X is relative to the start of the text display area of W, so the
28943 width of bitmap areas and scroll bars must be subtracted to get a
28944 position relative to the start of the mode line. */
28945
28946 static void
28947 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28948 enum window_part area)
28949 {
28950 struct window *w = XWINDOW (window);
28951 struct frame *f = XFRAME (w->frame);
28952 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28953 #ifdef HAVE_WINDOW_SYSTEM
28954 Display_Info *dpyinfo;
28955 #endif
28956 Cursor cursor = No_Cursor;
28957 Lisp_Object pointer = Qnil;
28958 int dx, dy, width, height;
28959 ptrdiff_t charpos;
28960 Lisp_Object string, object = Qnil;
28961 Lisp_Object pos IF_LINT (= Qnil), help;
28962
28963 Lisp_Object mouse_face;
28964 int original_x_pixel = x;
28965 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28966 struct glyph_row *row IF_LINT (= 0);
28967
28968 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28969 {
28970 int x0;
28971 struct glyph *end;
28972
28973 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28974 returns them in row/column units! */
28975 string = mode_line_string (w, area, &x, &y, &charpos,
28976 &object, &dx, &dy, &width, &height);
28977
28978 row = (area == ON_MODE_LINE
28979 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28980 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28981
28982 /* Find the glyph under the mouse pointer. */
28983 if (row->mode_line_p && row->enabled_p)
28984 {
28985 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28986 end = glyph + row->used[TEXT_AREA];
28987
28988 for (x0 = original_x_pixel;
28989 glyph < end && x0 >= glyph->pixel_width;
28990 ++glyph)
28991 x0 -= glyph->pixel_width;
28992
28993 if (glyph >= end)
28994 glyph = NULL;
28995 }
28996 }
28997 else
28998 {
28999 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29000 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29001 returns them in row/column units! */
29002 string = marginal_area_string (w, area, &x, &y, &charpos,
29003 &object, &dx, &dy, &width, &height);
29004 }
29005
29006 help = Qnil;
29007
29008 #ifdef HAVE_WINDOW_SYSTEM
29009 if (IMAGEP (object))
29010 {
29011 Lisp_Object image_map, hotspot;
29012 if ((image_map = Fplist_get (XCDR (object), QCmap),
29013 !NILP (image_map))
29014 && (hotspot = find_hot_spot (image_map, dx, dy),
29015 CONSP (hotspot))
29016 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29017 {
29018 Lisp_Object plist;
29019
29020 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29021 If so, we could look for mouse-enter, mouse-leave
29022 properties in PLIST (and do something...). */
29023 hotspot = XCDR (hotspot);
29024 if (CONSP (hotspot)
29025 && (plist = XCAR (hotspot), CONSP (plist)))
29026 {
29027 pointer = Fplist_get (plist, Qpointer);
29028 if (NILP (pointer))
29029 pointer = Qhand;
29030 help = Fplist_get (plist, Qhelp_echo);
29031 if (!NILP (help))
29032 {
29033 help_echo_string = help;
29034 XSETWINDOW (help_echo_window, w);
29035 help_echo_object = w->contents;
29036 help_echo_pos = charpos;
29037 }
29038 }
29039 }
29040 if (NILP (pointer))
29041 pointer = Fplist_get (XCDR (object), QCpointer);
29042 }
29043 #endif /* HAVE_WINDOW_SYSTEM */
29044
29045 if (STRINGP (string))
29046 pos = make_number (charpos);
29047
29048 /* Set the help text and mouse pointer. If the mouse is on a part
29049 of the mode line without any text (e.g. past the right edge of
29050 the mode line text), use the default help text and pointer. */
29051 if (STRINGP (string) || area == ON_MODE_LINE)
29052 {
29053 /* Arrange to display the help by setting the global variables
29054 help_echo_string, help_echo_object, and help_echo_pos. */
29055 if (NILP (help))
29056 {
29057 if (STRINGP (string))
29058 help = Fget_text_property (pos, Qhelp_echo, string);
29059
29060 if (!NILP (help))
29061 {
29062 help_echo_string = help;
29063 XSETWINDOW (help_echo_window, w);
29064 help_echo_object = string;
29065 help_echo_pos = charpos;
29066 }
29067 else if (area == ON_MODE_LINE)
29068 {
29069 Lisp_Object default_help
29070 = buffer_local_value (Qmode_line_default_help_echo,
29071 w->contents);
29072
29073 if (STRINGP (default_help))
29074 {
29075 help_echo_string = default_help;
29076 XSETWINDOW (help_echo_window, w);
29077 help_echo_object = Qnil;
29078 help_echo_pos = -1;
29079 }
29080 }
29081 }
29082
29083 #ifdef HAVE_WINDOW_SYSTEM
29084 /* Change the mouse pointer according to what is under it. */
29085 if (FRAME_WINDOW_P (f))
29086 {
29087 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29088 || minibuf_level
29089 || NILP (Vresize_mini_windows));
29090
29091 dpyinfo = FRAME_DISPLAY_INFO (f);
29092 if (STRINGP (string))
29093 {
29094 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29095
29096 if (NILP (pointer))
29097 pointer = Fget_text_property (pos, Qpointer, string);
29098
29099 /* Change the mouse pointer according to what is under X/Y. */
29100 if (NILP (pointer)
29101 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29102 {
29103 Lisp_Object map;
29104 map = Fget_text_property (pos, Qlocal_map, string);
29105 if (!KEYMAPP (map))
29106 map = Fget_text_property (pos, Qkeymap, string);
29107 if (!KEYMAPP (map) && draggable)
29108 cursor = dpyinfo->vertical_scroll_bar_cursor;
29109 }
29110 }
29111 else if (draggable)
29112 /* Default mode-line pointer. */
29113 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29114 }
29115 #endif
29116 }
29117
29118 /* Change the mouse face according to what is under X/Y. */
29119 if (STRINGP (string))
29120 {
29121 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29122 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29123 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29124 && glyph)
29125 {
29126 Lisp_Object b, e;
29127
29128 struct glyph * tmp_glyph;
29129
29130 int gpos;
29131 int gseq_length;
29132 int total_pixel_width;
29133 ptrdiff_t begpos, endpos, ignore;
29134
29135 int vpos, hpos;
29136
29137 b = Fprevious_single_property_change (make_number (charpos + 1),
29138 Qmouse_face, string, Qnil);
29139 if (NILP (b))
29140 begpos = 0;
29141 else
29142 begpos = XINT (b);
29143
29144 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29145 if (NILP (e))
29146 endpos = SCHARS (string);
29147 else
29148 endpos = XINT (e);
29149
29150 /* Calculate the glyph position GPOS of GLYPH in the
29151 displayed string, relative to the beginning of the
29152 highlighted part of the string.
29153
29154 Note: GPOS is different from CHARPOS. CHARPOS is the
29155 position of GLYPH in the internal string object. A mode
29156 line string format has structures which are converted to
29157 a flattened string by the Emacs Lisp interpreter. The
29158 internal string is an element of those structures. The
29159 displayed string is the flattened string. */
29160 tmp_glyph = row_start_glyph;
29161 while (tmp_glyph < glyph
29162 && (!(EQ (tmp_glyph->object, glyph->object)
29163 && begpos <= tmp_glyph->charpos
29164 && tmp_glyph->charpos < endpos)))
29165 tmp_glyph++;
29166 gpos = glyph - tmp_glyph;
29167
29168 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29169 the highlighted part of the displayed string to which
29170 GLYPH belongs. Note: GSEQ_LENGTH is different from
29171 SCHARS (STRING), because the latter returns the length of
29172 the internal string. */
29173 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29174 tmp_glyph > glyph
29175 && (!(EQ (tmp_glyph->object, glyph->object)
29176 && begpos <= tmp_glyph->charpos
29177 && tmp_glyph->charpos < endpos));
29178 tmp_glyph--)
29179 ;
29180 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29181
29182 /* Calculate the total pixel width of all the glyphs between
29183 the beginning of the highlighted area and GLYPH. */
29184 total_pixel_width = 0;
29185 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29186 total_pixel_width += tmp_glyph->pixel_width;
29187
29188 /* Pre calculation of re-rendering position. Note: X is in
29189 column units here, after the call to mode_line_string or
29190 marginal_area_string. */
29191 hpos = x - gpos;
29192 vpos = (area == ON_MODE_LINE
29193 ? (w->current_matrix)->nrows - 1
29194 : 0);
29195
29196 /* If GLYPH's position is included in the region that is
29197 already drawn in mouse face, we have nothing to do. */
29198 if ( EQ (window, hlinfo->mouse_face_window)
29199 && (!row->reversed_p
29200 ? (hlinfo->mouse_face_beg_col <= hpos
29201 && hpos < hlinfo->mouse_face_end_col)
29202 /* In R2L rows we swap BEG and END, see below. */
29203 : (hlinfo->mouse_face_end_col <= hpos
29204 && hpos < hlinfo->mouse_face_beg_col))
29205 && hlinfo->mouse_face_beg_row == vpos )
29206 return;
29207
29208 if (clear_mouse_face (hlinfo))
29209 cursor = No_Cursor;
29210
29211 if (!row->reversed_p)
29212 {
29213 hlinfo->mouse_face_beg_col = hpos;
29214 hlinfo->mouse_face_beg_x = original_x_pixel
29215 - (total_pixel_width + dx);
29216 hlinfo->mouse_face_end_col = hpos + gseq_length;
29217 hlinfo->mouse_face_end_x = 0;
29218 }
29219 else
29220 {
29221 /* In R2L rows, show_mouse_face expects BEG and END
29222 coordinates to be swapped. */
29223 hlinfo->mouse_face_end_col = hpos;
29224 hlinfo->mouse_face_end_x = original_x_pixel
29225 - (total_pixel_width + dx);
29226 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29227 hlinfo->mouse_face_beg_x = 0;
29228 }
29229
29230 hlinfo->mouse_face_beg_row = vpos;
29231 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29232 hlinfo->mouse_face_past_end = false;
29233 hlinfo->mouse_face_window = window;
29234
29235 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29236 charpos,
29237 0, &ignore,
29238 glyph->face_id,
29239 true);
29240 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29241
29242 if (NILP (pointer))
29243 pointer = Qhand;
29244 }
29245 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29246 clear_mouse_face (hlinfo);
29247 }
29248 #ifdef HAVE_WINDOW_SYSTEM
29249 if (FRAME_WINDOW_P (f))
29250 define_frame_cursor1 (f, cursor, pointer);
29251 #endif
29252 }
29253
29254
29255 /* EXPORT:
29256 Take proper action when the mouse has moved to position X, Y on
29257 frame F with regards to highlighting portions of display that have
29258 mouse-face properties. Also de-highlight portions of display where
29259 the mouse was before, set the mouse pointer shape as appropriate
29260 for the mouse coordinates, and activate help echo (tooltips).
29261 X and Y can be negative or out of range. */
29262
29263 void
29264 note_mouse_highlight (struct frame *f, int x, int y)
29265 {
29266 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29267 enum window_part part = ON_NOTHING;
29268 Lisp_Object window;
29269 struct window *w;
29270 Cursor cursor = No_Cursor;
29271 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29272 struct buffer *b;
29273
29274 /* When a menu is active, don't highlight because this looks odd. */
29275 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29276 if (popup_activated ())
29277 return;
29278 #endif
29279
29280 if (!f->glyphs_initialized_p
29281 || f->pointer_invisible)
29282 return;
29283
29284 hlinfo->mouse_face_mouse_x = x;
29285 hlinfo->mouse_face_mouse_y = y;
29286 hlinfo->mouse_face_mouse_frame = f;
29287
29288 if (hlinfo->mouse_face_defer)
29289 return;
29290
29291 /* Which window is that in? */
29292 window = window_from_coordinates (f, x, y, &part, true);
29293
29294 /* If displaying active text in another window, clear that. */
29295 if (! EQ (window, hlinfo->mouse_face_window)
29296 /* Also clear if we move out of text area in same window. */
29297 || (!NILP (hlinfo->mouse_face_window)
29298 && !NILP (window)
29299 && part != ON_TEXT
29300 && part != ON_MODE_LINE
29301 && part != ON_HEADER_LINE))
29302 clear_mouse_face (hlinfo);
29303
29304 /* Not on a window -> return. */
29305 if (!WINDOWP (window))
29306 return;
29307
29308 /* Reset help_echo_string. It will get recomputed below. */
29309 help_echo_string = Qnil;
29310
29311 /* Convert to window-relative pixel coordinates. */
29312 w = XWINDOW (window);
29313 frame_to_window_pixel_xy (w, &x, &y);
29314
29315 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29316 /* Handle tool-bar window differently since it doesn't display a
29317 buffer. */
29318 if (EQ (window, f->tool_bar_window))
29319 {
29320 note_tool_bar_highlight (f, x, y);
29321 return;
29322 }
29323 #endif
29324
29325 /* Mouse is on the mode, header line or margin? */
29326 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29327 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29328 {
29329 note_mode_line_or_margin_highlight (window, x, y, part);
29330
29331 #ifdef HAVE_WINDOW_SYSTEM
29332 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29333 {
29334 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29335 /* Show non-text cursor (Bug#16647). */
29336 goto set_cursor;
29337 }
29338 else
29339 #endif
29340 return;
29341 }
29342
29343 #ifdef HAVE_WINDOW_SYSTEM
29344 if (part == ON_VERTICAL_BORDER)
29345 {
29346 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29347 help_echo_string = build_string ("drag-mouse-1: resize");
29348 }
29349 else if (part == ON_RIGHT_DIVIDER)
29350 {
29351 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29352 help_echo_string = build_string ("drag-mouse-1: resize");
29353 }
29354 else if (part == ON_BOTTOM_DIVIDER)
29355 if (! WINDOW_BOTTOMMOST_P (w)
29356 || minibuf_level
29357 || NILP (Vresize_mini_windows))
29358 {
29359 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29360 help_echo_string = build_string ("drag-mouse-1: resize");
29361 }
29362 else
29363 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29364 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29365 || part == ON_VERTICAL_SCROLL_BAR
29366 || part == ON_HORIZONTAL_SCROLL_BAR)
29367 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29368 else
29369 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29370 #endif
29371
29372 /* Are we in a window whose display is up to date?
29373 And verify the buffer's text has not changed. */
29374 b = XBUFFER (w->contents);
29375 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29376 {
29377 int hpos, vpos, dx, dy, area = LAST_AREA;
29378 ptrdiff_t pos;
29379 struct glyph *glyph;
29380 Lisp_Object object;
29381 Lisp_Object mouse_face = Qnil, position;
29382 Lisp_Object *overlay_vec = NULL;
29383 ptrdiff_t i, noverlays;
29384 struct buffer *obuf;
29385 ptrdiff_t obegv, ozv;
29386 bool same_region;
29387
29388 /* Find the glyph under X/Y. */
29389 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29390
29391 #ifdef HAVE_WINDOW_SYSTEM
29392 /* Look for :pointer property on image. */
29393 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29394 {
29395 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29396 if (img != NULL && IMAGEP (img->spec))
29397 {
29398 Lisp_Object image_map, hotspot;
29399 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29400 !NILP (image_map))
29401 && (hotspot = find_hot_spot (image_map,
29402 glyph->slice.img.x + dx,
29403 glyph->slice.img.y + dy),
29404 CONSP (hotspot))
29405 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29406 {
29407 Lisp_Object plist;
29408
29409 /* Could check XCAR (hotspot) to see if we enter/leave
29410 this hot-spot.
29411 If so, we could look for mouse-enter, mouse-leave
29412 properties in PLIST (and do something...). */
29413 hotspot = XCDR (hotspot);
29414 if (CONSP (hotspot)
29415 && (plist = XCAR (hotspot), CONSP (plist)))
29416 {
29417 pointer = Fplist_get (plist, Qpointer);
29418 if (NILP (pointer))
29419 pointer = Qhand;
29420 help_echo_string = Fplist_get (plist, Qhelp_echo);
29421 if (!NILP (help_echo_string))
29422 {
29423 help_echo_window = window;
29424 help_echo_object = glyph->object;
29425 help_echo_pos = glyph->charpos;
29426 }
29427 }
29428 }
29429 if (NILP (pointer))
29430 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29431 }
29432 }
29433 #endif /* HAVE_WINDOW_SYSTEM */
29434
29435 /* Clear mouse face if X/Y not over text. */
29436 if (glyph == NULL
29437 || area != TEXT_AREA
29438 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29439 /* Glyph's OBJECT is nil for glyphs inserted by the
29440 display engine for its internal purposes, like truncation
29441 and continuation glyphs and blanks beyond the end of
29442 line's text on text terminals. If we are over such a
29443 glyph, we are not over any text. */
29444 || NILP (glyph->object)
29445 /* R2L rows have a stretch glyph at their front, which
29446 stands for no text, whereas L2R rows have no glyphs at
29447 all beyond the end of text. Treat such stretch glyphs
29448 like we do with NULL glyphs in L2R rows. */
29449 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29450 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29451 && glyph->type == STRETCH_GLYPH
29452 && glyph->avoid_cursor_p))
29453 {
29454 if (clear_mouse_face (hlinfo))
29455 cursor = No_Cursor;
29456 #ifdef HAVE_WINDOW_SYSTEM
29457 if (FRAME_WINDOW_P (f) && NILP (pointer))
29458 {
29459 if (area != TEXT_AREA)
29460 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29461 else
29462 pointer = Vvoid_text_area_pointer;
29463 }
29464 #endif
29465 goto set_cursor;
29466 }
29467
29468 pos = glyph->charpos;
29469 object = glyph->object;
29470 if (!STRINGP (object) && !BUFFERP (object))
29471 goto set_cursor;
29472
29473 /* If we get an out-of-range value, return now; avoid an error. */
29474 if (BUFFERP (object) && pos > BUF_Z (b))
29475 goto set_cursor;
29476
29477 /* Make the window's buffer temporarily current for
29478 overlays_at and compute_char_face. */
29479 obuf = current_buffer;
29480 current_buffer = b;
29481 obegv = BEGV;
29482 ozv = ZV;
29483 BEGV = BEG;
29484 ZV = Z;
29485
29486 /* Is this char mouse-active or does it have help-echo? */
29487 position = make_number (pos);
29488
29489 USE_SAFE_ALLOCA;
29490
29491 if (BUFFERP (object))
29492 {
29493 /* Put all the overlays we want in a vector in overlay_vec. */
29494 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
29495 /* Sort overlays into increasing priority order. */
29496 noverlays = sort_overlays (overlay_vec, noverlays, w);
29497 }
29498 else
29499 noverlays = 0;
29500
29501 if (NILP (Vmouse_highlight))
29502 {
29503 clear_mouse_face (hlinfo);
29504 goto check_help_echo;
29505 }
29506
29507 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29508
29509 if (same_region)
29510 cursor = No_Cursor;
29511
29512 /* Check mouse-face highlighting. */
29513 if (! same_region
29514 /* If there exists an overlay with mouse-face overlapping
29515 the one we are currently highlighting, we have to
29516 check if we enter the overlapping overlay, and then
29517 highlight only that. */
29518 || (OVERLAYP (hlinfo->mouse_face_overlay)
29519 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29520 {
29521 /* Find the highest priority overlay with a mouse-face. */
29522 Lisp_Object overlay = Qnil;
29523 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29524 {
29525 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29526 if (!NILP (mouse_face))
29527 overlay = overlay_vec[i];
29528 }
29529
29530 /* If we're highlighting the same overlay as before, there's
29531 no need to do that again. */
29532 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29533 goto check_help_echo;
29534 hlinfo->mouse_face_overlay = overlay;
29535
29536 /* Clear the display of the old active region, if any. */
29537 if (clear_mouse_face (hlinfo))
29538 cursor = No_Cursor;
29539
29540 /* If no overlay applies, get a text property. */
29541 if (NILP (overlay))
29542 mouse_face = Fget_text_property (position, Qmouse_face, object);
29543
29544 /* Next, compute the bounds of the mouse highlighting and
29545 display it. */
29546 if (!NILP (mouse_face) && STRINGP (object))
29547 {
29548 /* The mouse-highlighting comes from a display string
29549 with a mouse-face. */
29550 Lisp_Object s, e;
29551 ptrdiff_t ignore;
29552
29553 s = Fprevious_single_property_change
29554 (make_number (pos + 1), Qmouse_face, object, Qnil);
29555 e = Fnext_single_property_change
29556 (position, Qmouse_face, object, Qnil);
29557 if (NILP (s))
29558 s = make_number (0);
29559 if (NILP (e))
29560 e = make_number (SCHARS (object));
29561 mouse_face_from_string_pos (w, hlinfo, object,
29562 XINT (s), XINT (e));
29563 hlinfo->mouse_face_past_end = false;
29564 hlinfo->mouse_face_window = window;
29565 hlinfo->mouse_face_face_id
29566 = face_at_string_position (w, object, pos, 0, &ignore,
29567 glyph->face_id, true);
29568 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29569 cursor = No_Cursor;
29570 }
29571 else
29572 {
29573 /* The mouse-highlighting, if any, comes from an overlay
29574 or text property in the buffer. */
29575 Lisp_Object buffer IF_LINT (= Qnil);
29576 Lisp_Object disp_string IF_LINT (= Qnil);
29577
29578 if (STRINGP (object))
29579 {
29580 /* If we are on a display string with no mouse-face,
29581 check if the text under it has one. */
29582 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29583 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29584 pos = string_buffer_position (object, start);
29585 if (pos > 0)
29586 {
29587 mouse_face = get_char_property_and_overlay
29588 (make_number (pos), Qmouse_face, w->contents, &overlay);
29589 buffer = w->contents;
29590 disp_string = object;
29591 }
29592 }
29593 else
29594 {
29595 buffer = object;
29596 disp_string = Qnil;
29597 }
29598
29599 if (!NILP (mouse_face))
29600 {
29601 Lisp_Object before, after;
29602 Lisp_Object before_string, after_string;
29603 /* To correctly find the limits of mouse highlight
29604 in a bidi-reordered buffer, we must not use the
29605 optimization of limiting the search in
29606 previous-single-property-change and
29607 next-single-property-change, because
29608 rows_from_pos_range needs the real start and end
29609 positions to DTRT in this case. That's because
29610 the first row visible in a window does not
29611 necessarily display the character whose position
29612 is the smallest. */
29613 Lisp_Object lim1
29614 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29615 ? Fmarker_position (w->start)
29616 : Qnil;
29617 Lisp_Object lim2
29618 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29619 ? make_number (BUF_Z (XBUFFER (buffer))
29620 - w->window_end_pos)
29621 : Qnil;
29622
29623 if (NILP (overlay))
29624 {
29625 /* Handle the text property case. */
29626 before = Fprevious_single_property_change
29627 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29628 after = Fnext_single_property_change
29629 (make_number (pos), Qmouse_face, buffer, lim2);
29630 before_string = after_string = Qnil;
29631 }
29632 else
29633 {
29634 /* Handle the overlay case. */
29635 before = Foverlay_start (overlay);
29636 after = Foverlay_end (overlay);
29637 before_string = Foverlay_get (overlay, Qbefore_string);
29638 after_string = Foverlay_get (overlay, Qafter_string);
29639
29640 if (!STRINGP (before_string)) before_string = Qnil;
29641 if (!STRINGP (after_string)) after_string = Qnil;
29642 }
29643
29644 mouse_face_from_buffer_pos (window, hlinfo, pos,
29645 NILP (before)
29646 ? 1
29647 : XFASTINT (before),
29648 NILP (after)
29649 ? BUF_Z (XBUFFER (buffer))
29650 : XFASTINT (after),
29651 before_string, after_string,
29652 disp_string);
29653 cursor = No_Cursor;
29654 }
29655 }
29656 }
29657
29658 check_help_echo:
29659
29660 /* Look for a `help-echo' property. */
29661 if (NILP (help_echo_string)) {
29662 Lisp_Object help, overlay;
29663
29664 /* Check overlays first. */
29665 help = overlay = Qnil;
29666 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29667 {
29668 overlay = overlay_vec[i];
29669 help = Foverlay_get (overlay, Qhelp_echo);
29670 }
29671
29672 if (!NILP (help))
29673 {
29674 help_echo_string = help;
29675 help_echo_window = window;
29676 help_echo_object = overlay;
29677 help_echo_pos = pos;
29678 }
29679 else
29680 {
29681 Lisp_Object obj = glyph->object;
29682 ptrdiff_t charpos = glyph->charpos;
29683
29684 /* Try text properties. */
29685 if (STRINGP (obj)
29686 && charpos >= 0
29687 && charpos < SCHARS (obj))
29688 {
29689 help = Fget_text_property (make_number (charpos),
29690 Qhelp_echo, obj);
29691 if (NILP (help))
29692 {
29693 /* If the string itself doesn't specify a help-echo,
29694 see if the buffer text ``under'' it does. */
29695 struct glyph_row *r
29696 = MATRIX_ROW (w->current_matrix, vpos);
29697 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29698 ptrdiff_t p = string_buffer_position (obj, start);
29699 if (p > 0)
29700 {
29701 help = Fget_char_property (make_number (p),
29702 Qhelp_echo, w->contents);
29703 if (!NILP (help))
29704 {
29705 charpos = p;
29706 obj = w->contents;
29707 }
29708 }
29709 }
29710 }
29711 else if (BUFFERP (obj)
29712 && charpos >= BEGV
29713 && charpos < ZV)
29714 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29715 obj);
29716
29717 if (!NILP (help))
29718 {
29719 help_echo_string = help;
29720 help_echo_window = window;
29721 help_echo_object = obj;
29722 help_echo_pos = charpos;
29723 }
29724 }
29725 }
29726
29727 #ifdef HAVE_WINDOW_SYSTEM
29728 /* Look for a `pointer' property. */
29729 if (FRAME_WINDOW_P (f) && NILP (pointer))
29730 {
29731 /* Check overlays first. */
29732 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29733 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29734
29735 if (NILP (pointer))
29736 {
29737 Lisp_Object obj = glyph->object;
29738 ptrdiff_t charpos = glyph->charpos;
29739
29740 /* Try text properties. */
29741 if (STRINGP (obj)
29742 && charpos >= 0
29743 && charpos < SCHARS (obj))
29744 {
29745 pointer = Fget_text_property (make_number (charpos),
29746 Qpointer, obj);
29747 if (NILP (pointer))
29748 {
29749 /* If the string itself doesn't specify a pointer,
29750 see if the buffer text ``under'' it does. */
29751 struct glyph_row *r
29752 = MATRIX_ROW (w->current_matrix, vpos);
29753 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29754 ptrdiff_t p = string_buffer_position (obj, start);
29755 if (p > 0)
29756 pointer = Fget_char_property (make_number (p),
29757 Qpointer, w->contents);
29758 }
29759 }
29760 else if (BUFFERP (obj)
29761 && charpos >= BEGV
29762 && charpos < ZV)
29763 pointer = Fget_text_property (make_number (charpos),
29764 Qpointer, obj);
29765 }
29766 }
29767 #endif /* HAVE_WINDOW_SYSTEM */
29768
29769 BEGV = obegv;
29770 ZV = ozv;
29771 current_buffer = obuf;
29772 SAFE_FREE ();
29773 }
29774
29775 set_cursor:
29776
29777 #ifdef HAVE_WINDOW_SYSTEM
29778 if (FRAME_WINDOW_P (f))
29779 define_frame_cursor1 (f, cursor, pointer);
29780 #else
29781 /* This is here to prevent a compiler error, about "label at end of
29782 compound statement". */
29783 return;
29784 #endif
29785 }
29786
29787
29788 /* EXPORT for RIF:
29789 Clear any mouse-face on window W. This function is part of the
29790 redisplay interface, and is called from try_window_id and similar
29791 functions to ensure the mouse-highlight is off. */
29792
29793 void
29794 x_clear_window_mouse_face (struct window *w)
29795 {
29796 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29797 Lisp_Object window;
29798
29799 block_input ();
29800 XSETWINDOW (window, w);
29801 if (EQ (window, hlinfo->mouse_face_window))
29802 clear_mouse_face (hlinfo);
29803 unblock_input ();
29804 }
29805
29806
29807 /* EXPORT:
29808 Just discard the mouse face information for frame F, if any.
29809 This is used when the size of F is changed. */
29810
29811 void
29812 cancel_mouse_face (struct frame *f)
29813 {
29814 Lisp_Object window;
29815 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29816
29817 window = hlinfo->mouse_face_window;
29818 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29819 reset_mouse_highlight (hlinfo);
29820 }
29821
29822
29823 \f
29824 /***********************************************************************
29825 Exposure Events
29826 ***********************************************************************/
29827
29828 #ifdef HAVE_WINDOW_SYSTEM
29829
29830 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29831 which intersects rectangle R. R is in window-relative coordinates. */
29832
29833 static void
29834 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29835 enum glyph_row_area area)
29836 {
29837 struct glyph *first = row->glyphs[area];
29838 struct glyph *end = row->glyphs[area] + row->used[area];
29839 struct glyph *last;
29840 int first_x, start_x, x;
29841
29842 if (area == TEXT_AREA && row->fill_line_p)
29843 /* If row extends face to end of line write the whole line. */
29844 draw_glyphs (w, 0, row, area,
29845 0, row->used[area],
29846 DRAW_NORMAL_TEXT, 0);
29847 else
29848 {
29849 /* Set START_X to the window-relative start position for drawing glyphs of
29850 AREA. The first glyph of the text area can be partially visible.
29851 The first glyphs of other areas cannot. */
29852 start_x = window_box_left_offset (w, area);
29853 x = start_x;
29854 if (area == TEXT_AREA)
29855 x += row->x;
29856
29857 /* Find the first glyph that must be redrawn. */
29858 while (first < end
29859 && x + first->pixel_width < r->x)
29860 {
29861 x += first->pixel_width;
29862 ++first;
29863 }
29864
29865 /* Find the last one. */
29866 last = first;
29867 first_x = x;
29868 while (last < end
29869 && x < r->x + r->width)
29870 {
29871 x += last->pixel_width;
29872 ++last;
29873 }
29874
29875 /* Repaint. */
29876 if (last > first)
29877 draw_glyphs (w, first_x - start_x, row, area,
29878 first - row->glyphs[area], last - row->glyphs[area],
29879 DRAW_NORMAL_TEXT, 0);
29880 }
29881 }
29882
29883
29884 /* Redraw the parts of the glyph row ROW on window W intersecting
29885 rectangle R. R is in window-relative coordinates. Value is
29886 true if mouse-face was overwritten. */
29887
29888 static bool
29889 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29890 {
29891 eassert (row->enabled_p);
29892
29893 if (row->mode_line_p || w->pseudo_window_p)
29894 draw_glyphs (w, 0, row, TEXT_AREA,
29895 0, row->used[TEXT_AREA],
29896 DRAW_NORMAL_TEXT, 0);
29897 else
29898 {
29899 if (row->used[LEFT_MARGIN_AREA])
29900 expose_area (w, row, r, LEFT_MARGIN_AREA);
29901 if (row->used[TEXT_AREA])
29902 expose_area (w, row, r, TEXT_AREA);
29903 if (row->used[RIGHT_MARGIN_AREA])
29904 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29905 draw_row_fringe_bitmaps (w, row);
29906 }
29907
29908 return row->mouse_face_p;
29909 }
29910
29911
29912 /* Redraw those parts of glyphs rows during expose event handling that
29913 overlap other rows. Redrawing of an exposed line writes over parts
29914 of lines overlapping that exposed line; this function fixes that.
29915
29916 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29917 row in W's current matrix that is exposed and overlaps other rows.
29918 LAST_OVERLAPPING_ROW is the last such row. */
29919
29920 static void
29921 expose_overlaps (struct window *w,
29922 struct glyph_row *first_overlapping_row,
29923 struct glyph_row *last_overlapping_row,
29924 XRectangle *r)
29925 {
29926 struct glyph_row *row;
29927
29928 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29929 if (row->overlapping_p)
29930 {
29931 eassert (row->enabled_p && !row->mode_line_p);
29932
29933 row->clip = r;
29934 if (row->used[LEFT_MARGIN_AREA])
29935 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29936
29937 if (row->used[TEXT_AREA])
29938 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29939
29940 if (row->used[RIGHT_MARGIN_AREA])
29941 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29942 row->clip = NULL;
29943 }
29944 }
29945
29946
29947 /* Return true if W's cursor intersects rectangle R. */
29948
29949 static bool
29950 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29951 {
29952 XRectangle cr, result;
29953 struct glyph *cursor_glyph;
29954 struct glyph_row *row;
29955
29956 if (w->phys_cursor.vpos >= 0
29957 && w->phys_cursor.vpos < w->current_matrix->nrows
29958 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29959 row->enabled_p)
29960 && row->cursor_in_fringe_p)
29961 {
29962 /* Cursor is in the fringe. */
29963 cr.x = window_box_right_offset (w,
29964 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29965 ? RIGHT_MARGIN_AREA
29966 : TEXT_AREA));
29967 cr.y = row->y;
29968 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29969 cr.height = row->height;
29970 return x_intersect_rectangles (&cr, r, &result);
29971 }
29972
29973 cursor_glyph = get_phys_cursor_glyph (w);
29974 if (cursor_glyph)
29975 {
29976 /* r is relative to W's box, but w->phys_cursor.x is relative
29977 to left edge of W's TEXT area. Adjust it. */
29978 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29979 cr.y = w->phys_cursor.y;
29980 cr.width = cursor_glyph->pixel_width;
29981 cr.height = w->phys_cursor_height;
29982 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29983 I assume the effect is the same -- and this is portable. */
29984 return x_intersect_rectangles (&cr, r, &result);
29985 }
29986 /* If we don't understand the format, pretend we're not in the hot-spot. */
29987 return false;
29988 }
29989
29990
29991 /* EXPORT:
29992 Draw a vertical window border to the right of window W if W doesn't
29993 have vertical scroll bars. */
29994
29995 void
29996 x_draw_vertical_border (struct window *w)
29997 {
29998 struct frame *f = XFRAME (WINDOW_FRAME (w));
29999
30000 /* We could do better, if we knew what type of scroll-bar the adjacent
30001 windows (on either side) have... But we don't :-(
30002 However, I think this works ok. ++KFS 2003-04-25 */
30003
30004 /* Redraw borders between horizontally adjacent windows. Don't
30005 do it for frames with vertical scroll bars because either the
30006 right scroll bar of a window, or the left scroll bar of its
30007 neighbor will suffice as a border. */
30008 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30009 return;
30010
30011 /* Note: It is necessary to redraw both the left and the right
30012 borders, for when only this single window W is being
30013 redisplayed. */
30014 if (!WINDOW_RIGHTMOST_P (w)
30015 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30016 {
30017 int x0, x1, y0, y1;
30018
30019 window_box_edges (w, &x0, &y0, &x1, &y1);
30020 y1 -= 1;
30021
30022 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30023 x1 -= 1;
30024
30025 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30026 }
30027
30028 if (!WINDOW_LEFTMOST_P (w)
30029 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30030 {
30031 int x0, x1, y0, y1;
30032
30033 window_box_edges (w, &x0, &y0, &x1, &y1);
30034 y1 -= 1;
30035
30036 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30037 x0 -= 1;
30038
30039 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30040 }
30041 }
30042
30043
30044 /* Draw window dividers for window W. */
30045
30046 void
30047 x_draw_right_divider (struct window *w)
30048 {
30049 struct frame *f = WINDOW_XFRAME (w);
30050
30051 if (w->mini || w->pseudo_window_p)
30052 return;
30053 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30054 {
30055 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30056 int x1 = WINDOW_RIGHT_EDGE_X (w);
30057 int y0 = WINDOW_TOP_EDGE_Y (w);
30058 /* The bottom divider prevails. */
30059 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30060
30061 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30062 }
30063 }
30064
30065 static void
30066 x_draw_bottom_divider (struct window *w)
30067 {
30068 struct frame *f = XFRAME (WINDOW_FRAME (w));
30069
30070 if (w->mini || w->pseudo_window_p)
30071 return;
30072 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30073 {
30074 int x0 = WINDOW_LEFT_EDGE_X (w);
30075 int x1 = WINDOW_RIGHT_EDGE_X (w);
30076 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30077 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30078
30079 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30080 }
30081 }
30082
30083 /* Redraw the part of window W intersection rectangle FR. Pixel
30084 coordinates in FR are frame-relative. Call this function with
30085 input blocked. Value is true if the exposure overwrites
30086 mouse-face. */
30087
30088 static bool
30089 expose_window (struct window *w, XRectangle *fr)
30090 {
30091 struct frame *f = XFRAME (w->frame);
30092 XRectangle wr, r;
30093 bool mouse_face_overwritten_p = false;
30094
30095 /* If window is not yet fully initialized, do nothing. This can
30096 happen when toolkit scroll bars are used and a window is split.
30097 Reconfiguring the scroll bar will generate an expose for a newly
30098 created window. */
30099 if (w->current_matrix == NULL)
30100 return false;
30101
30102 /* When we're currently updating the window, display and current
30103 matrix usually don't agree. Arrange for a thorough display
30104 later. */
30105 if (w->must_be_updated_p)
30106 {
30107 SET_FRAME_GARBAGED (f);
30108 return false;
30109 }
30110
30111 /* Frame-relative pixel rectangle of W. */
30112 wr.x = WINDOW_LEFT_EDGE_X (w);
30113 wr.y = WINDOW_TOP_EDGE_Y (w);
30114 wr.width = WINDOW_PIXEL_WIDTH (w);
30115 wr.height = WINDOW_PIXEL_HEIGHT (w);
30116
30117 if (x_intersect_rectangles (fr, &wr, &r))
30118 {
30119 int yb = window_text_bottom_y (w);
30120 struct glyph_row *row;
30121 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30122
30123 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30124 r.x, r.y, r.width, r.height));
30125
30126 /* Convert to window coordinates. */
30127 r.x -= WINDOW_LEFT_EDGE_X (w);
30128 r.y -= WINDOW_TOP_EDGE_Y (w);
30129
30130 /* Turn off the cursor. */
30131 bool cursor_cleared_p = (!w->pseudo_window_p
30132 && phys_cursor_in_rect_p (w, &r));
30133 if (cursor_cleared_p)
30134 x_clear_cursor (w);
30135
30136 /* If the row containing the cursor extends face to end of line,
30137 then expose_area might overwrite the cursor outside the
30138 rectangle and thus notice_overwritten_cursor might clear
30139 w->phys_cursor_on_p. We remember the original value and
30140 check later if it is changed. */
30141 bool phys_cursor_on_p = w->phys_cursor_on_p;
30142
30143 /* Update lines intersecting rectangle R. */
30144 first_overlapping_row = last_overlapping_row = NULL;
30145 for (row = w->current_matrix->rows;
30146 row->enabled_p;
30147 ++row)
30148 {
30149 int y0 = row->y;
30150 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30151
30152 if ((y0 >= r.y && y0 < r.y + r.height)
30153 || (y1 > r.y && y1 < r.y + r.height)
30154 || (r.y >= y0 && r.y < y1)
30155 || (r.y + r.height > y0 && r.y + r.height < y1))
30156 {
30157 /* A header line may be overlapping, but there is no need
30158 to fix overlapping areas for them. KFS 2005-02-12 */
30159 if (row->overlapping_p && !row->mode_line_p)
30160 {
30161 if (first_overlapping_row == NULL)
30162 first_overlapping_row = row;
30163 last_overlapping_row = row;
30164 }
30165
30166 row->clip = fr;
30167 if (expose_line (w, row, &r))
30168 mouse_face_overwritten_p = true;
30169 row->clip = NULL;
30170 }
30171 else if (row->overlapping_p)
30172 {
30173 /* We must redraw a row overlapping the exposed area. */
30174 if (y0 < r.y
30175 ? y0 + row->phys_height > r.y
30176 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30177 {
30178 if (first_overlapping_row == NULL)
30179 first_overlapping_row = row;
30180 last_overlapping_row = row;
30181 }
30182 }
30183
30184 if (y1 >= yb)
30185 break;
30186 }
30187
30188 /* Display the mode line if there is one. */
30189 if (WINDOW_WANTS_MODELINE_P (w)
30190 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30191 row->enabled_p)
30192 && row->y < r.y + r.height)
30193 {
30194 if (expose_line (w, row, &r))
30195 mouse_face_overwritten_p = true;
30196 }
30197
30198 if (!w->pseudo_window_p)
30199 {
30200 /* Fix the display of overlapping rows. */
30201 if (first_overlapping_row)
30202 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30203 fr);
30204
30205 /* Draw border between windows. */
30206 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30207 x_draw_right_divider (w);
30208 else
30209 x_draw_vertical_border (w);
30210
30211 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30212 x_draw_bottom_divider (w);
30213
30214 /* Turn the cursor on again. */
30215 if (cursor_cleared_p
30216 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30217 update_window_cursor (w, true);
30218 }
30219 }
30220
30221 return mouse_face_overwritten_p;
30222 }
30223
30224
30225
30226 /* Redraw (parts) of all windows in the window tree rooted at W that
30227 intersect R. R contains frame pixel coordinates. Value is
30228 true if the exposure overwrites mouse-face. */
30229
30230 static bool
30231 expose_window_tree (struct window *w, XRectangle *r)
30232 {
30233 struct frame *f = XFRAME (w->frame);
30234 bool mouse_face_overwritten_p = false;
30235
30236 while (w && !FRAME_GARBAGED_P (f))
30237 {
30238 mouse_face_overwritten_p
30239 |= (WINDOWP (w->contents)
30240 ? expose_window_tree (XWINDOW (w->contents), r)
30241 : expose_window (w, r));
30242
30243 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30244 }
30245
30246 return mouse_face_overwritten_p;
30247 }
30248
30249
30250 /* EXPORT:
30251 Redisplay an exposed area of frame F. X and Y are the upper-left
30252 corner of the exposed rectangle. W and H are width and height of
30253 the exposed area. All are pixel values. W or H zero means redraw
30254 the entire frame. */
30255
30256 void
30257 expose_frame (struct frame *f, int x, int y, int w, int h)
30258 {
30259 XRectangle r;
30260 bool mouse_face_overwritten_p = false;
30261
30262 TRACE ((stderr, "expose_frame "));
30263
30264 /* No need to redraw if frame will be redrawn soon. */
30265 if (FRAME_GARBAGED_P (f))
30266 {
30267 TRACE ((stderr, " garbaged\n"));
30268 return;
30269 }
30270
30271 /* If basic faces haven't been realized yet, there is no point in
30272 trying to redraw anything. This can happen when we get an expose
30273 event while Emacs is starting, e.g. by moving another window. */
30274 if (FRAME_FACE_CACHE (f) == NULL
30275 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30276 {
30277 TRACE ((stderr, " no faces\n"));
30278 return;
30279 }
30280
30281 if (w == 0 || h == 0)
30282 {
30283 r.x = r.y = 0;
30284 r.width = FRAME_TEXT_WIDTH (f);
30285 r.height = FRAME_TEXT_HEIGHT (f);
30286 }
30287 else
30288 {
30289 r.x = x;
30290 r.y = y;
30291 r.width = w;
30292 r.height = h;
30293 }
30294
30295 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30296 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30297
30298 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30299 if (WINDOWP (f->tool_bar_window))
30300 mouse_face_overwritten_p
30301 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30302 #endif
30303
30304 #ifdef HAVE_X_WINDOWS
30305 #ifndef MSDOS
30306 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30307 if (WINDOWP (f->menu_bar_window))
30308 mouse_face_overwritten_p
30309 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30310 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30311 #endif
30312 #endif
30313
30314 /* Some window managers support a focus-follows-mouse style with
30315 delayed raising of frames. Imagine a partially obscured frame,
30316 and moving the mouse into partially obscured mouse-face on that
30317 frame. The visible part of the mouse-face will be highlighted,
30318 then the WM raises the obscured frame. With at least one WM, KDE
30319 2.1, Emacs is not getting any event for the raising of the frame
30320 (even tried with SubstructureRedirectMask), only Expose events.
30321 These expose events will draw text normally, i.e. not
30322 highlighted. Which means we must redo the highlight here.
30323 Subsume it under ``we love X''. --gerd 2001-08-15 */
30324 /* Included in Windows version because Windows most likely does not
30325 do the right thing if any third party tool offers
30326 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30327 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30328 {
30329 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30330 if (f == hlinfo->mouse_face_mouse_frame)
30331 {
30332 int mouse_x = hlinfo->mouse_face_mouse_x;
30333 int mouse_y = hlinfo->mouse_face_mouse_y;
30334 clear_mouse_face (hlinfo);
30335 note_mouse_highlight (f, mouse_x, mouse_y);
30336 }
30337 }
30338 }
30339
30340
30341 /* EXPORT:
30342 Determine the intersection of two rectangles R1 and R2. Return
30343 the intersection in *RESULT. Value is true if RESULT is not
30344 empty. */
30345
30346 bool
30347 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30348 {
30349 XRectangle *left, *right;
30350 XRectangle *upper, *lower;
30351 bool intersection_p = false;
30352
30353 /* Rearrange so that R1 is the left-most rectangle. */
30354 if (r1->x < r2->x)
30355 left = r1, right = r2;
30356 else
30357 left = r2, right = r1;
30358
30359 /* X0 of the intersection is right.x0, if this is inside R1,
30360 otherwise there is no intersection. */
30361 if (right->x <= left->x + left->width)
30362 {
30363 result->x = right->x;
30364
30365 /* The right end of the intersection is the minimum of
30366 the right ends of left and right. */
30367 result->width = (min (left->x + left->width, right->x + right->width)
30368 - result->x);
30369
30370 /* Same game for Y. */
30371 if (r1->y < r2->y)
30372 upper = r1, lower = r2;
30373 else
30374 upper = r2, lower = r1;
30375
30376 /* The upper end of the intersection is lower.y0, if this is inside
30377 of upper. Otherwise, there is no intersection. */
30378 if (lower->y <= upper->y + upper->height)
30379 {
30380 result->y = lower->y;
30381
30382 /* The lower end of the intersection is the minimum of the lower
30383 ends of upper and lower. */
30384 result->height = (min (lower->y + lower->height,
30385 upper->y + upper->height)
30386 - result->y);
30387 intersection_p = true;
30388 }
30389 }
30390
30391 return intersection_p;
30392 }
30393
30394 #endif /* HAVE_WINDOW_SYSTEM */
30395
30396 \f
30397 /***********************************************************************
30398 Initialization
30399 ***********************************************************************/
30400
30401 void
30402 syms_of_xdisp (void)
30403 {
30404 Vwith_echo_area_save_vector = Qnil;
30405 staticpro (&Vwith_echo_area_save_vector);
30406
30407 Vmessage_stack = Qnil;
30408 staticpro (&Vmessage_stack);
30409
30410 /* Non-nil means don't actually do any redisplay. */
30411 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30412
30413 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30414
30415 message_dolog_marker1 = Fmake_marker ();
30416 staticpro (&message_dolog_marker1);
30417 message_dolog_marker2 = Fmake_marker ();
30418 staticpro (&message_dolog_marker2);
30419 message_dolog_marker3 = Fmake_marker ();
30420 staticpro (&message_dolog_marker3);
30421
30422 #ifdef GLYPH_DEBUG
30423 defsubr (&Sdump_frame_glyph_matrix);
30424 defsubr (&Sdump_glyph_matrix);
30425 defsubr (&Sdump_glyph_row);
30426 defsubr (&Sdump_tool_bar_row);
30427 defsubr (&Strace_redisplay);
30428 defsubr (&Strace_to_stderr);
30429 #endif
30430 #ifdef HAVE_WINDOW_SYSTEM
30431 defsubr (&Stool_bar_height);
30432 defsubr (&Slookup_image_map);
30433 #endif
30434 defsubr (&Sline_pixel_height);
30435 defsubr (&Sformat_mode_line);
30436 defsubr (&Sinvisible_p);
30437 defsubr (&Scurrent_bidi_paragraph_direction);
30438 defsubr (&Swindow_text_pixel_size);
30439 defsubr (&Smove_point_visually);
30440 defsubr (&Sbidi_find_overridden_directionality);
30441
30442 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30443 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30444 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30445 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30446 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30447 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30448 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30449 DEFSYM (Qeval, "eval");
30450 DEFSYM (QCdata, ":data");
30451
30452 /* Names of text properties relevant for redisplay. */
30453 DEFSYM (Qdisplay, "display");
30454 DEFSYM (Qspace_width, "space-width");
30455 DEFSYM (Qraise, "raise");
30456 DEFSYM (Qslice, "slice");
30457 DEFSYM (Qspace, "space");
30458 DEFSYM (Qmargin, "margin");
30459 DEFSYM (Qpointer, "pointer");
30460 DEFSYM (Qleft_margin, "left-margin");
30461 DEFSYM (Qright_margin, "right-margin");
30462 DEFSYM (Qcenter, "center");
30463 DEFSYM (Qline_height, "line-height");
30464 DEFSYM (QCalign_to, ":align-to");
30465 DEFSYM (QCrelative_width, ":relative-width");
30466 DEFSYM (QCrelative_height, ":relative-height");
30467 DEFSYM (QCeval, ":eval");
30468 DEFSYM (QCpropertize, ":propertize");
30469 DEFSYM (QCfile, ":file");
30470 DEFSYM (Qfontified, "fontified");
30471 DEFSYM (Qfontification_functions, "fontification-functions");
30472
30473 /* Name of the face used to highlight trailing whitespace. */
30474 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30475
30476 /* Name and number of the face used to highlight escape glyphs. */
30477 DEFSYM (Qescape_glyph, "escape-glyph");
30478
30479 /* Name and number of the face used to highlight non-breaking spaces. */
30480 DEFSYM (Qnobreak_space, "nobreak-space");
30481
30482 /* The symbol 'image' which is the car of the lists used to represent
30483 images in Lisp. Also a tool bar style. */
30484 DEFSYM (Qimage, "image");
30485
30486 /* Tool bar styles. */
30487 DEFSYM (Qtext, "text");
30488 DEFSYM (Qboth, "both");
30489 DEFSYM (Qboth_horiz, "both-horiz");
30490 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30491
30492 /* The image map types. */
30493 DEFSYM (QCmap, ":map");
30494 DEFSYM (QCpointer, ":pointer");
30495 DEFSYM (Qrect, "rect");
30496 DEFSYM (Qcircle, "circle");
30497 DEFSYM (Qpoly, "poly");
30498
30499 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
30500 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30501 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
30502
30503 DEFSYM (Qgrow_only, "grow-only");
30504 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30505 DEFSYM (Qposition, "position");
30506 DEFSYM (Qbuffer_position, "buffer-position");
30507 DEFSYM (Qobject, "object");
30508
30509 /* Cursor shapes. */
30510 DEFSYM (Qbar, "bar");
30511 DEFSYM (Qhbar, "hbar");
30512 DEFSYM (Qbox, "box");
30513 DEFSYM (Qhollow, "hollow");
30514
30515 /* Pointer shapes. */
30516 DEFSYM (Qhand, "hand");
30517 DEFSYM (Qarrow, "arrow");
30518 /* also Qtext */
30519
30520 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30521
30522 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
30523 staticpro (&list_of_error);
30524
30525 /* Values of those variables at last redisplay are stored as
30526 properties on 'overlay-arrow-position' symbol. However, if
30527 Voverlay_arrow_position is a marker, last-arrow-position is its
30528 numerical position. */
30529 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30530 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30531
30532 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
30533 properties on a symbol in overlay-arrow-variable-list. */
30534 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30535 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30536
30537 echo_buffer[0] = echo_buffer[1] = Qnil;
30538 staticpro (&echo_buffer[0]);
30539 staticpro (&echo_buffer[1]);
30540
30541 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30542 staticpro (&echo_area_buffer[0]);
30543 staticpro (&echo_area_buffer[1]);
30544
30545 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30546 staticpro (&Vmessages_buffer_name);
30547
30548 mode_line_proptrans_alist = Qnil;
30549 staticpro (&mode_line_proptrans_alist);
30550 mode_line_string_list = Qnil;
30551 staticpro (&mode_line_string_list);
30552 mode_line_string_face = Qnil;
30553 staticpro (&mode_line_string_face);
30554 mode_line_string_face_prop = Qnil;
30555 staticpro (&mode_line_string_face_prop);
30556 Vmode_line_unwind_vector = Qnil;
30557 staticpro (&Vmode_line_unwind_vector);
30558
30559 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30560
30561 help_echo_string = Qnil;
30562 staticpro (&help_echo_string);
30563 help_echo_object = Qnil;
30564 staticpro (&help_echo_object);
30565 help_echo_window = Qnil;
30566 staticpro (&help_echo_window);
30567 previous_help_echo_string = Qnil;
30568 staticpro (&previous_help_echo_string);
30569 help_echo_pos = -1;
30570
30571 DEFSYM (Qright_to_left, "right-to-left");
30572 DEFSYM (Qleft_to_right, "left-to-right");
30573 defsubr (&Sbidi_resolved_levels);
30574
30575 #ifdef HAVE_WINDOW_SYSTEM
30576 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30577 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30578 For example, if a block cursor is over a tab, it will be drawn as
30579 wide as that tab on the display. */);
30580 x_stretch_cursor_p = 0;
30581 #endif
30582
30583 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30584 doc: /* Non-nil means highlight trailing whitespace.
30585 The face used for trailing whitespace is `trailing-whitespace'. */);
30586 Vshow_trailing_whitespace = Qnil;
30587
30588 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30589 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30590 If the value is t, Emacs highlights non-ASCII chars which have the
30591 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30592 or `escape-glyph' face respectively.
30593
30594 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30595 U+2011 (non-breaking hyphen) are affected.
30596
30597 Any other non-nil value means to display these characters as a escape
30598 glyph followed by an ordinary space or hyphen.
30599
30600 A value of nil means no special handling of these characters. */);
30601 Vnobreak_char_display = Qt;
30602
30603 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30604 doc: /* The pointer shape to show in void text areas.
30605 A value of nil means to show the text pointer. Other options are
30606 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
30607 `hourglass'. */);
30608 Vvoid_text_area_pointer = Qarrow;
30609
30610 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30611 doc: /* Non-nil means don't actually do any redisplay.
30612 This is used for internal purposes. */);
30613 Vinhibit_redisplay = Qnil;
30614
30615 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30616 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30617 Vglobal_mode_string = Qnil;
30618
30619 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30620 doc: /* Marker for where to display an arrow on top of the buffer text.
30621 This must be the beginning of a line in order to work.
30622 See also `overlay-arrow-string'. */);
30623 Voverlay_arrow_position = Qnil;
30624
30625 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30626 doc: /* String to display as an arrow in non-window frames.
30627 See also `overlay-arrow-position'. */);
30628 Voverlay_arrow_string = build_pure_c_string ("=>");
30629
30630 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30631 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30632 The symbols on this list are examined during redisplay to determine
30633 where to display overlay arrows. */);
30634 Voverlay_arrow_variable_list
30635 = list1 (intern_c_string ("overlay-arrow-position"));
30636
30637 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30638 doc: /* The number of lines to try scrolling a window by when point moves out.
30639 If that fails to bring point back on frame, point is centered instead.
30640 If this is zero, point is always centered after it moves off frame.
30641 If you want scrolling to always be a line at a time, you should set
30642 `scroll-conservatively' to a large value rather than set this to 1. */);
30643
30644 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30645 doc: /* Scroll up to this many lines, to bring point back on screen.
30646 If point moves off-screen, redisplay will scroll by up to
30647 `scroll-conservatively' lines in order to bring point just barely
30648 onto the screen again. If that cannot be done, then redisplay
30649 recenters point as usual.
30650
30651 If the value is greater than 100, redisplay will never recenter point,
30652 but will always scroll just enough text to bring point into view, even
30653 if you move far away.
30654
30655 A value of zero means always recenter point if it moves off screen. */);
30656 scroll_conservatively = 0;
30657
30658 DEFVAR_INT ("scroll-margin", scroll_margin,
30659 doc: /* Number of lines of margin at the top and bottom of a window.
30660 Recenter the window whenever point gets within this many lines
30661 of the top or bottom of the window. */);
30662 scroll_margin = 0;
30663
30664 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30665 doc: /* Pixels per inch value for non-window system displays.
30666 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30667 Vdisplay_pixels_per_inch = make_float (72.0);
30668
30669 #ifdef GLYPH_DEBUG
30670 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30671 #endif
30672
30673 DEFVAR_LISP ("truncate-partial-width-windows",
30674 Vtruncate_partial_width_windows,
30675 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30676 For an integer value, truncate lines in each window narrower than the
30677 full frame width, provided the window width is less than that integer;
30678 otherwise, respect the value of `truncate-lines'.
30679
30680 For any other non-nil value, truncate lines in all windows that do
30681 not span the full frame width.
30682
30683 A value of nil means to respect the value of `truncate-lines'.
30684
30685 If `word-wrap' is enabled, you might want to reduce this. */);
30686 Vtruncate_partial_width_windows = make_number (50);
30687
30688 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30689 doc: /* Maximum buffer size for which line number should be displayed.
30690 If the buffer is bigger than this, the line number does not appear
30691 in the mode line. A value of nil means no limit. */);
30692 Vline_number_display_limit = Qnil;
30693
30694 DEFVAR_INT ("line-number-display-limit-width",
30695 line_number_display_limit_width,
30696 doc: /* Maximum line width (in characters) for line number display.
30697 If the average length of the lines near point is bigger than this, then the
30698 line number may be omitted from the mode line. */);
30699 line_number_display_limit_width = 200;
30700
30701 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30702 doc: /* Non-nil means highlight region even in nonselected windows. */);
30703 highlight_nonselected_windows = false;
30704
30705 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30706 doc: /* Non-nil if more than one frame is visible on this display.
30707 Minibuffer-only frames don't count, but iconified frames do.
30708 This variable is not guaranteed to be accurate except while processing
30709 `frame-title-format' and `icon-title-format'. */);
30710
30711 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30712 doc: /* Template for displaying the title bar of visible frames.
30713 \(Assuming the window manager supports this feature.)
30714
30715 This variable has the same structure as `mode-line-format', except that
30716 the %c and %l constructs are ignored. It is used only on frames for
30717 which no explicit name has been set \(see `modify-frame-parameters'). */);
30718
30719 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30720 doc: /* Template for displaying the title bar of an iconified frame.
30721 \(Assuming the window manager supports this feature.)
30722 This variable has the same structure as `mode-line-format' (which see),
30723 and is used only on frames for which no explicit name has been set
30724 \(see `modify-frame-parameters'). */);
30725 Vicon_title_format
30726 = Vframe_title_format
30727 = listn (CONSTYPE_PURE, 3,
30728 intern_c_string ("multiple-frames"),
30729 build_pure_c_string ("%b"),
30730 listn (CONSTYPE_PURE, 4,
30731 empty_unibyte_string,
30732 intern_c_string ("invocation-name"),
30733 build_pure_c_string ("@"),
30734 intern_c_string ("system-name")));
30735
30736 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30737 doc: /* Maximum number of lines to keep in the message log buffer.
30738 If nil, disable message logging. If t, log messages but don't truncate
30739 the buffer when it becomes large. */);
30740 Vmessage_log_max = make_number (1000);
30741
30742 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30743 doc: /* Functions called before redisplay, if window sizes have changed.
30744 The value should be a list of functions that take one argument.
30745 Just before redisplay, for each frame, if any of its windows have changed
30746 size since the last redisplay, or have been split or deleted,
30747 all the functions in the list are called, with the frame as argument. */);
30748 Vwindow_size_change_functions = Qnil;
30749
30750 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30751 doc: /* List of functions to call before redisplaying a window with scrolling.
30752 Each function is called with two arguments, the window and its new
30753 display-start position.
30754 These functions are called whenever the `window-start' marker is modified,
30755 either to point into another buffer (e.g. via `set-window-buffer') or another
30756 place in the same buffer.
30757 Note that the value of `window-end' is not valid when these functions are
30758 called.
30759
30760 Warning: Do not use this feature to alter the way the window
30761 is scrolled. It is not designed for that, and such use probably won't
30762 work. */);
30763 Vwindow_scroll_functions = Qnil;
30764
30765 DEFVAR_LISP ("window-text-change-functions",
30766 Vwindow_text_change_functions,
30767 doc: /* Functions to call in redisplay when text in the window might change. */);
30768 Vwindow_text_change_functions = Qnil;
30769
30770 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30771 doc: /* Functions called when redisplay of a window reaches the end trigger.
30772 Each function is called with two arguments, the window and the end trigger value.
30773 See `set-window-redisplay-end-trigger'. */);
30774 Vredisplay_end_trigger_functions = Qnil;
30775
30776 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30777 doc: /* Non-nil means autoselect window with mouse pointer.
30778 If nil, do not autoselect windows.
30779 A positive number means delay autoselection by that many seconds: a
30780 window is autoselected only after the mouse has remained in that
30781 window for the duration of the delay.
30782 A negative number has a similar effect, but causes windows to be
30783 autoselected only after the mouse has stopped moving. \(Because of
30784 the way Emacs compares mouse events, you will occasionally wait twice
30785 that time before the window gets selected.\)
30786 Any other value means to autoselect window instantaneously when the
30787 mouse pointer enters it.
30788
30789 Autoselection selects the minibuffer only if it is active, and never
30790 unselects the minibuffer if it is active.
30791
30792 When customizing this variable make sure that the actual value of
30793 `focus-follows-mouse' matches the behavior of your window manager. */);
30794 Vmouse_autoselect_window = Qnil;
30795
30796 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30797 doc: /* Non-nil means automatically resize tool-bars.
30798 This dynamically changes the tool-bar's height to the minimum height
30799 that is needed to make all tool-bar items visible.
30800 If value is `grow-only', the tool-bar's height is only increased
30801 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30802 Vauto_resize_tool_bars = Qt;
30803
30804 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30805 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30806 auto_raise_tool_bar_buttons_p = true;
30807
30808 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30809 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30810 make_cursor_line_fully_visible_p = true;
30811
30812 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30813 doc: /* Border below tool-bar in pixels.
30814 If an integer, use it as the height of the border.
30815 If it is one of `internal-border-width' or `border-width', use the
30816 value of the corresponding frame parameter.
30817 Otherwise, no border is added below the tool-bar. */);
30818 Vtool_bar_border = Qinternal_border_width;
30819
30820 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30821 doc: /* Margin around tool-bar buttons in pixels.
30822 If an integer, use that for both horizontal and vertical margins.
30823 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30824 HORZ specifying the horizontal margin, and VERT specifying the
30825 vertical margin. */);
30826 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30827
30828 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30829 doc: /* Relief thickness of tool-bar buttons. */);
30830 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30831
30832 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30833 doc: /* Tool bar style to use.
30834 It can be one of
30835 image - show images only
30836 text - show text only
30837 both - show both, text below image
30838 both-horiz - show text to the right of the image
30839 text-image-horiz - show text to the left of the image
30840 any other - use system default or image if no system default.
30841
30842 This variable only affects the GTK+ toolkit version of Emacs. */);
30843 Vtool_bar_style = Qnil;
30844
30845 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30846 doc: /* Maximum number of characters a label can have to be shown.
30847 The tool bar style must also show labels for this to have any effect, see
30848 `tool-bar-style'. */);
30849 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30850
30851 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30852 doc: /* List of functions to call to fontify regions of text.
30853 Each function is called with one argument POS. Functions must
30854 fontify a region starting at POS in the current buffer, and give
30855 fontified regions the property `fontified'. */);
30856 Vfontification_functions = Qnil;
30857 Fmake_variable_buffer_local (Qfontification_functions);
30858
30859 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30860 unibyte_display_via_language_environment,
30861 doc: /* Non-nil means display unibyte text according to language environment.
30862 Specifically, this means that raw bytes in the range 160-255 decimal
30863 are displayed by converting them to the equivalent multibyte characters
30864 according to the current language environment. As a result, they are
30865 displayed according to the current fontset.
30866
30867 Note that this variable affects only how these bytes are displayed,
30868 but does not change the fact they are interpreted as raw bytes. */);
30869 unibyte_display_via_language_environment = false;
30870
30871 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30872 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30873 If a float, it specifies a fraction of the mini-window frame's height.
30874 If an integer, it specifies a number of lines. */);
30875 Vmax_mini_window_height = make_float (0.25);
30876
30877 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30878 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30879 A value of nil means don't automatically resize mini-windows.
30880 A value of t means resize them to fit the text displayed in them.
30881 A value of `grow-only', the default, means let mini-windows grow only;
30882 they return to their normal size when the minibuffer is closed, or the
30883 echo area becomes empty. */);
30884 Vresize_mini_windows = Qgrow_only;
30885
30886 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30887 doc: /* Alist specifying how to blink the cursor off.
30888 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30889 `cursor-type' frame-parameter or variable equals ON-STATE,
30890 comparing using `equal', Emacs uses OFF-STATE to specify
30891 how to blink it off. ON-STATE and OFF-STATE are values for
30892 the `cursor-type' frame parameter.
30893
30894 If a frame's ON-STATE has no entry in this list,
30895 the frame's other specifications determine how to blink the cursor off. */);
30896 Vblink_cursor_alist = Qnil;
30897
30898 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30899 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30900 If non-nil, windows are automatically scrolled horizontally to make
30901 point visible. */);
30902 automatic_hscrolling_p = true;
30903 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30904
30905 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30906 doc: /* How many columns away from the window edge point is allowed to get
30907 before automatic hscrolling will horizontally scroll the window. */);
30908 hscroll_margin = 5;
30909
30910 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30911 doc: /* How many columns to scroll the window when point gets too close to the edge.
30912 When point is less than `hscroll-margin' columns from the window
30913 edge, automatic hscrolling will scroll the window by the amount of columns
30914 determined by this variable. If its value is a positive integer, scroll that
30915 many columns. If it's a positive floating-point number, it specifies the
30916 fraction of the window's width to scroll. If it's nil or zero, point will be
30917 centered horizontally after the scroll. Any other value, including negative
30918 numbers, are treated as if the value were zero.
30919
30920 Automatic hscrolling always moves point outside the scroll margin, so if
30921 point was more than scroll step columns inside the margin, the window will
30922 scroll more than the value given by the scroll step.
30923
30924 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30925 and `scroll-right' overrides this variable's effect. */);
30926 Vhscroll_step = make_number (0);
30927
30928 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30929 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30930 Bind this around calls to `message' to let it take effect. */);
30931 message_truncate_lines = false;
30932
30933 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30934 doc: /* Normal hook run to update the menu bar definitions.
30935 Redisplay runs this hook before it redisplays the menu bar.
30936 This is used to update menus such as Buffers, whose contents depend on
30937 various data. */);
30938 Vmenu_bar_update_hook = Qnil;
30939
30940 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30941 doc: /* Frame for which we are updating a menu.
30942 The enable predicate for a menu binding should check this variable. */);
30943 Vmenu_updating_frame = Qnil;
30944
30945 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30946 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30947 inhibit_menubar_update = false;
30948
30949 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30950 doc: /* Prefix prepended to all continuation lines at display time.
30951 The value may be a string, an image, or a stretch-glyph; it is
30952 interpreted in the same way as the value of a `display' text property.
30953
30954 This variable is overridden by any `wrap-prefix' text or overlay
30955 property.
30956
30957 To add a prefix to non-continuation lines, use `line-prefix'. */);
30958 Vwrap_prefix = Qnil;
30959 DEFSYM (Qwrap_prefix, "wrap-prefix");
30960 Fmake_variable_buffer_local (Qwrap_prefix);
30961
30962 DEFVAR_LISP ("line-prefix", Vline_prefix,
30963 doc: /* Prefix prepended to all non-continuation lines at display time.
30964 The value may be a string, an image, or a stretch-glyph; it is
30965 interpreted in the same way as the value of a `display' text property.
30966
30967 This variable is overridden by any `line-prefix' text or overlay
30968 property.
30969
30970 To add a prefix to continuation lines, use `wrap-prefix'. */);
30971 Vline_prefix = Qnil;
30972 DEFSYM (Qline_prefix, "line-prefix");
30973 Fmake_variable_buffer_local (Qline_prefix);
30974
30975 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30976 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30977 inhibit_eval_during_redisplay = false;
30978
30979 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30980 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30981 inhibit_free_realized_faces = false;
30982
30983 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
30984 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
30985 Intended for use during debugging and for testing bidi display;
30986 see biditest.el in the test suite. */);
30987 inhibit_bidi_mirroring = false;
30988
30989 #ifdef GLYPH_DEBUG
30990 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30991 doc: /* Inhibit try_window_id display optimization. */);
30992 inhibit_try_window_id = false;
30993
30994 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30995 doc: /* Inhibit try_window_reusing display optimization. */);
30996 inhibit_try_window_reusing = false;
30997
30998 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30999 doc: /* Inhibit try_cursor_movement display optimization. */);
31000 inhibit_try_cursor_movement = false;
31001 #endif /* GLYPH_DEBUG */
31002
31003 DEFVAR_INT ("overline-margin", overline_margin,
31004 doc: /* Space between overline and text, in pixels.
31005 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31006 margin to the character height. */);
31007 overline_margin = 2;
31008
31009 DEFVAR_INT ("underline-minimum-offset",
31010 underline_minimum_offset,
31011 doc: /* Minimum distance between baseline and underline.
31012 This can improve legibility of underlined text at small font sizes,
31013 particularly when using variable `x-use-underline-position-properties'
31014 with fonts that specify an UNDERLINE_POSITION relatively close to the
31015 baseline. The default value is 1. */);
31016 underline_minimum_offset = 1;
31017
31018 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31019 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31020 This feature only works when on a window system that can change
31021 cursor shapes. */);
31022 display_hourglass_p = true;
31023
31024 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31025 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31026 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31027
31028 #ifdef HAVE_WINDOW_SYSTEM
31029 hourglass_atimer = NULL;
31030 hourglass_shown_p = false;
31031 #endif /* HAVE_WINDOW_SYSTEM */
31032
31033 /* Name of the face used to display glyphless characters. */
31034 DEFSYM (Qglyphless_char, "glyphless-char");
31035
31036 /* Method symbols for Vglyphless_char_display. */
31037 DEFSYM (Qhex_code, "hex-code");
31038 DEFSYM (Qempty_box, "empty-box");
31039 DEFSYM (Qthin_space, "thin-space");
31040 DEFSYM (Qzero_width, "zero-width");
31041
31042 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31043 doc: /* Function run just before redisplay.
31044 It is called with one argument, which is the set of windows that are to
31045 be redisplayed. This set can be nil (meaning, only the selected window),
31046 or t (meaning all windows). */);
31047 Vpre_redisplay_function = intern ("ignore");
31048
31049 /* Symbol for the purpose of Vglyphless_char_display. */
31050 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31051 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31052
31053 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31054 doc: /* Char-table defining glyphless characters.
31055 Each element, if non-nil, should be one of the following:
31056 an ASCII acronym string: display this string in a box
31057 `hex-code': display the hexadecimal code of a character in a box
31058 `empty-box': display as an empty box
31059 `thin-space': display as 1-pixel width space
31060 `zero-width': don't display
31061 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31062 display method for graphical terminals and text terminals respectively.
31063 GRAPHICAL and TEXT should each have one of the values listed above.
31064
31065 The char-table has one extra slot to control the display of a character for
31066 which no font is found. This slot only takes effect on graphical terminals.
31067 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31068 `thin-space'. The default is `empty-box'.
31069
31070 If a character has a non-nil entry in an active display table, the
31071 display table takes effect; in this case, Emacs does not consult
31072 `glyphless-char-display' at all. */);
31073 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31074 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31075 Qempty_box);
31076
31077 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31078 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31079 Vdebug_on_message = Qnil;
31080
31081 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31082 doc: /* */);
31083 Vredisplay__all_windows_cause
31084 = Fmake_vector (make_number (100), make_number (0));
31085
31086 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31087 doc: /* */);
31088 Vredisplay__mode_lines_cause
31089 = Fmake_vector (make_number (100), make_number (0));
31090 }
31091
31092
31093 /* Initialize this module when Emacs starts. */
31094
31095 void
31096 init_xdisp (void)
31097 {
31098 CHARPOS (this_line_start_pos) = 0;
31099
31100 if (!noninteractive)
31101 {
31102 struct window *m = XWINDOW (minibuf_window);
31103 Lisp_Object frame = m->frame;
31104 struct frame *f = XFRAME (frame);
31105 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31106 struct window *r = XWINDOW (root);
31107 int i;
31108
31109 echo_area_window = minibuf_window;
31110
31111 r->top_line = FRAME_TOP_MARGIN (f);
31112 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31113 r->total_cols = FRAME_COLS (f);
31114 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31115 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31116 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31117
31118 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31119 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31120 m->total_cols = FRAME_COLS (f);
31121 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31122 m->total_lines = 1;
31123 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31124
31125 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31126 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31127 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31128
31129 /* The default ellipsis glyphs `...'. */
31130 for (i = 0; i < 3; ++i)
31131 default_invis_vector[i] = make_number ('.');
31132 }
31133
31134 {
31135 /* Allocate the buffer for frame titles.
31136 Also used for `format-mode-line'. */
31137 int size = 100;
31138 mode_line_noprop_buf = xmalloc (size);
31139 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31140 mode_line_noprop_ptr = mode_line_noprop_buf;
31141 mode_line_target = MODE_LINE_DISPLAY;
31142 }
31143
31144 help_echo_showing_p = false;
31145 }
31146
31147 #ifdef HAVE_WINDOW_SYSTEM
31148
31149 /* Platform-independent portion of hourglass implementation. */
31150
31151 /* Timer function of hourglass_atimer. */
31152
31153 static void
31154 show_hourglass (struct atimer *timer)
31155 {
31156 /* The timer implementation will cancel this timer automatically
31157 after this function has run. Set hourglass_atimer to null
31158 so that we know the timer doesn't have to be canceled. */
31159 hourglass_atimer = NULL;
31160
31161 if (!hourglass_shown_p)
31162 {
31163 Lisp_Object tail, frame;
31164
31165 block_input ();
31166
31167 FOR_EACH_FRAME (tail, frame)
31168 {
31169 struct frame *f = XFRAME (frame);
31170
31171 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31172 && FRAME_RIF (f)->show_hourglass)
31173 FRAME_RIF (f)->show_hourglass (f);
31174 }
31175
31176 hourglass_shown_p = true;
31177 unblock_input ();
31178 }
31179 }
31180
31181 /* Cancel a currently active hourglass timer, and start a new one. */
31182
31183 void
31184 start_hourglass (void)
31185 {
31186 struct timespec delay;
31187
31188 cancel_hourglass ();
31189
31190 if (INTEGERP (Vhourglass_delay)
31191 && XINT (Vhourglass_delay) > 0)
31192 delay = make_timespec (min (XINT (Vhourglass_delay),
31193 TYPE_MAXIMUM (time_t)),
31194 0);
31195 else if (FLOATP (Vhourglass_delay)
31196 && XFLOAT_DATA (Vhourglass_delay) > 0)
31197 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31198 else
31199 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31200
31201 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31202 show_hourglass, NULL);
31203 }
31204
31205 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31206 shown. */
31207
31208 void
31209 cancel_hourglass (void)
31210 {
31211 if (hourglass_atimer)
31212 {
31213 cancel_atimer (hourglass_atimer);
31214 hourglass_atimer = NULL;
31215 }
31216
31217 if (hourglass_shown_p)
31218 {
31219 Lisp_Object tail, frame;
31220
31221 block_input ();
31222
31223 FOR_EACH_FRAME (tail, frame)
31224 {
31225 struct frame *f = XFRAME (frame);
31226
31227 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31228 && FRAME_RIF (f)->hide_hourglass)
31229 FRAME_RIF (f)->hide_hourglass (f);
31230 #ifdef HAVE_NTGUI
31231 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31232 else if (!FRAME_W32_P (f))
31233 w32_arrow_cursor ();
31234 #endif
31235 }
31236
31237 hourglass_shown_p = false;
31238 unblock_input ();
31239 }
31240 }
31241
31242 #endif /* HAVE_WINDOW_SYSTEM */