]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merge from origin/emacs-25
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2016 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 "composite.h"
296 #include "keyboard.h"
297 #include "systime.h"
298 #include "frame.h"
299 #include "window.h"
300 #include "termchar.h"
301 #include "dispextern.h"
302 #include "character.h"
303 #include "buffer.h"
304 #include "charset.h"
305 #include "indent.h"
306 #include "commands.h"
307 #include "keymap.h"
308 #include "disptab.h"
309 #include "termhooks.h"
310 #include "termopts.h"
311 #include "intervals.h"
312 #include "coding.h"
313 #include "region-cache.h"
314 #include "font.h"
315 #include "fontset.h"
316 #include "blockinput.h"
317 #include "xwidget.h"
318 #ifdef HAVE_WINDOW_SYSTEM
319 #include TERM_HEADER
320 #endif /* HAVE_WINDOW_SYSTEM */
321
322 #ifndef FRAME_X_OUTPUT
323 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
324 #endif
325
326 #define INFINITY 10000000
327
328 /* Holds the list (error). */
329 static Lisp_Object list_of_error;
330
331 #ifdef HAVE_WINDOW_SYSTEM
332
333 /* Test if overflow newline into fringe. Called with iterator IT
334 at or past right window margin, and with IT->current_x set. */
335
336 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
337 (!NILP (Voverflow_newline_into_fringe) \
338 && FRAME_WINDOW_P ((IT)->f) \
339 && ((IT)->bidi_it.paragraph_dir == R2L \
340 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
341 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
342 && (IT)->current_x == (IT)->last_visible_x)
343
344 #else /* !HAVE_WINDOW_SYSTEM */
345 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
346 #endif /* HAVE_WINDOW_SYSTEM */
347
348 /* Test if the display element loaded in IT, or the underlying buffer
349 or string character, is a space or a TAB character. This is used
350 to determine where word wrapping can occur. */
351
352 #define IT_DISPLAYING_WHITESPACE(it) \
353 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
354 || ((STRINGP (it->string) \
355 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
356 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
357 || (it->s \
358 && (it->s[IT_BYTEPOS (*it)] == ' ' \
359 || it->s[IT_BYTEPOS (*it)] == '\t')) \
360 || (IT_BYTEPOS (*it) < ZV_BYTE \
361 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
362 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
363
364 /* True means print newline to stdout before next mini-buffer message. */
365
366 bool noninteractive_need_newline;
367
368 /* True means print newline to message log before next message. */
369
370 static bool message_log_need_newline;
371
372 /* Three markers that message_dolog uses.
373 It could allocate them itself, but that causes trouble
374 in handling memory-full errors. */
375 static Lisp_Object message_dolog_marker1;
376 static Lisp_Object message_dolog_marker2;
377 static Lisp_Object message_dolog_marker3;
378 \f
379 /* The buffer position of the first character appearing entirely or
380 partially on the line of the selected window which contains the
381 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
382 redisplay optimization in redisplay_internal. */
383
384 static struct text_pos this_line_start_pos;
385
386 /* Number of characters past the end of the line above, including the
387 terminating newline. */
388
389 static struct text_pos this_line_end_pos;
390
391 /* The vertical positions and the height of this line. */
392
393 static int this_line_vpos;
394 static int this_line_y;
395 static int this_line_pixel_height;
396
397 /* X position at which this display line starts. Usually zero;
398 negative if first character is partially visible. */
399
400 static int this_line_start_x;
401
402 /* The smallest character position seen by move_it_* functions as they
403 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
404 hscrolled lines, see display_line. */
405
406 static struct text_pos this_line_min_pos;
407
408 /* Buffer that this_line_.* variables are referring to. */
409
410 static struct buffer *this_line_buffer;
411
412 /* True if an overlay arrow has been displayed in this window. */
413
414 static bool overlay_arrow_seen;
415
416 /* Vector containing glyphs for an ellipsis `...'. */
417
418 static Lisp_Object default_invis_vector[3];
419
420 /* This is the window where the echo area message was displayed. It
421 is always a mini-buffer window, but it may not be the same window
422 currently active as a mini-buffer. */
423
424 Lisp_Object echo_area_window;
425
426 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
427 pushes the current message and the value of
428 message_enable_multibyte on the stack, the function restore_message
429 pops the stack and displays MESSAGE again. */
430
431 static Lisp_Object Vmessage_stack;
432
433 /* True means multibyte characters were enabled when the echo area
434 message was specified. */
435
436 static bool message_enable_multibyte;
437
438 /* At each redisplay cycle, we should refresh everything there is to refresh.
439 To do that efficiently, we use many optimizations that try to make sure we
440 don't waste too much time updating things that haven't changed.
441 The coarsest such optimization is that, in the most common cases, we only
442 look at the selected-window.
443
444 To know whether other windows should be considered for redisplay, we use the
445 variable windows_or_buffers_changed: as long as it is 0, it means that we
446 have not noticed anything that should require updating anything else than
447 the selected-window. If it is set to REDISPLAY_SOME, it means that since
448 last redisplay, some changes have been made which could impact other
449 windows. To know which ones need redisplay, every buffer, window, and frame
450 has a `redisplay' bit, which (if true) means that this object needs to be
451 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
452 looking for those `redisplay' bits (actually, there might be some such bits
453 set, but then only on objects which aren't displayed anyway).
454
455 OTOH if it's non-zero we wil have to loop through all windows and then check
456 the `redisplay' bit of the corresponding window, frame, and buffer, in order
457 to decide whether that window needs attention or not. Note that we can't
458 just look at the frame's redisplay bit to decide that the whole frame can be
459 skipped, since even if the frame's redisplay bit is unset, some of its
460 windows's redisplay bits may be set.
461
462 Mostly for historical reasons, windows_or_buffers_changed can also take
463 other non-zero values. In that case, the precise value doesn't matter (it
464 encodes the cause of the setting but is only used for debugging purposes),
465 and what it means is that we shouldn't pay attention to any `redisplay' bits
466 and we should simply try and redisplay every window out there. */
467
468 int windows_or_buffers_changed;
469
470 /* Nonzero if we should redraw the mode lines on the next redisplay.
471 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
472 then only redisplay the mode lines in those buffers/windows/frames where the
473 `redisplay' bit has been set.
474 For any other value, redisplay all mode lines (the number used is then only
475 used to track down the cause for this full-redisplay).
476
477 Since the frame title uses the same %-constructs as the mode line
478 (except %c and %l), if this variable is non-zero, we also consider
479 redisplaying the title of each frame, see x_consider_frame_title.
480
481 The `redisplay' bits are the same as those used for
482 windows_or_buffers_changed, and setting windows_or_buffers_changed also
483 causes recomputation of the mode lines of all those windows. IOW this
484 variable only has an effect if windows_or_buffers_changed is zero, in which
485 case we should only need to redisplay the mode-line of those objects with
486 a `redisplay' bit set but not the window's text content (tho we may still
487 need to refresh the text content of the selected-window). */
488
489 int update_mode_lines;
490
491 /* True after display_mode_line if %l was used and it displayed a
492 line number. */
493
494 static bool line_number_displayed;
495
496 /* The name of the *Messages* buffer, a string. */
497
498 static Lisp_Object Vmessages_buffer_name;
499
500 /* Current, index 0, and last displayed echo area message. Either
501 buffers from echo_buffers, or nil to indicate no message. */
502
503 Lisp_Object echo_area_buffer[2];
504
505 /* The buffers referenced from echo_area_buffer. */
506
507 static Lisp_Object echo_buffer[2];
508
509 /* A vector saved used in with_area_buffer to reduce consing. */
510
511 static Lisp_Object Vwith_echo_area_save_vector;
512
513 /* True means display_echo_area should display the last echo area
514 message again. Set by redisplay_preserve_echo_area. */
515
516 static bool display_last_displayed_message_p;
517
518 /* True if echo area is being used by print; false if being used by
519 message. */
520
521 static bool message_buf_print;
522
523 /* Set to true in clear_message to make redisplay_internal aware
524 of an emptied echo area. */
525
526 static bool message_cleared_p;
527
528 /* A scratch glyph row with contents used for generating truncation
529 glyphs. Also used in direct_output_for_insert. */
530
531 #define MAX_SCRATCH_GLYPHS 100
532 static struct glyph_row scratch_glyph_row;
533 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
534
535 /* Ascent and height of the last line processed by move_it_to. */
536
537 static int last_height;
538
539 /* True if there's a help-echo in the echo area. */
540
541 bool help_echo_showing_p;
542
543 /* The maximum distance to look ahead for text properties. Values
544 that are too small let us call compute_char_face and similar
545 functions too often which is expensive. Values that are too large
546 let us call compute_char_face and alike too often because we
547 might not be interested in text properties that far away. */
548
549 #define TEXT_PROP_DISTANCE_LIMIT 100
550
551 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
552 iterator state and later restore it. This is needed because the
553 bidi iterator on bidi.c keeps a stacked cache of its states, which
554 is really a singleton. When we use scratch iterator objects to
555 move around the buffer, we can cause the bidi cache to be pushed or
556 popped, and therefore we need to restore the cache state when we
557 return to the original iterator. */
558 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
559 do { \
560 if (CACHE) \
561 bidi_unshelve_cache (CACHE, true); \
562 ITCOPY = ITORIG; \
563 CACHE = bidi_shelve_cache (); \
564 } while (false)
565
566 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
567 do { \
568 if (pITORIG != pITCOPY) \
569 *(pITORIG) = *(pITCOPY); \
570 bidi_unshelve_cache (CACHE, false); \
571 CACHE = NULL; \
572 } while (false)
573
574 /* Functions to mark elements as needing redisplay. */
575 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
576
577 void
578 redisplay_other_windows (void)
579 {
580 if (!windows_or_buffers_changed)
581 windows_or_buffers_changed = REDISPLAY_SOME;
582 }
583
584 void
585 wset_redisplay (struct window *w)
586 {
587 /* Beware: selected_window can be nil during early stages. */
588 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
589 redisplay_other_windows ();
590 w->redisplay = true;
591 }
592
593 void
594 fset_redisplay (struct frame *f)
595 {
596 redisplay_other_windows ();
597 f->redisplay = true;
598 }
599
600 void
601 bset_redisplay (struct buffer *b)
602 {
603 int count = buffer_window_count (b);
604 if (count > 0)
605 {
606 /* ... it's visible in other window than selected, */
607 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
608 redisplay_other_windows ();
609 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
610 so that if we later set windows_or_buffers_changed, this buffer will
611 not be omitted. */
612 b->text->redisplay = true;
613 }
614 }
615
616 void
617 bset_update_mode_line (struct buffer *b)
618 {
619 if (!update_mode_lines)
620 update_mode_lines = REDISPLAY_SOME;
621 b->text->redisplay = true;
622 }
623
624 void
625 maybe_set_redisplay (Lisp_Object symbol)
626 {
627 if (HASH_TABLE_P (Vredisplay__variables)
628 && hash_lookup (XHASH_TABLE (Vredisplay__variables), symbol, NULL) >= 0)
629 {
630 bset_update_mode_line (current_buffer);
631 current_buffer->prevent_redisplay_optimizations_p = true;
632 }
633 }
634
635 #ifdef GLYPH_DEBUG
636
637 /* True means print traces of redisplay if compiled with
638 GLYPH_DEBUG defined. */
639
640 bool trace_redisplay_p;
641
642 #endif /* GLYPH_DEBUG */
643
644 #ifdef DEBUG_TRACE_MOVE
645 /* True means trace with TRACE_MOVE to stderr. */
646 static bool trace_move;
647
648 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
649 #else
650 #define TRACE_MOVE(x) (void) 0
651 #endif
652
653 /* Buffer being redisplayed -- for redisplay_window_error. */
654
655 static struct buffer *displayed_buffer;
656
657 /* Value returned from text property handlers (see below). */
658
659 enum prop_handled
660 {
661 HANDLED_NORMALLY,
662 HANDLED_RECOMPUTE_PROPS,
663 HANDLED_OVERLAY_STRING_CONSUMED,
664 HANDLED_RETURN
665 };
666
667 /* A description of text properties that redisplay is interested
668 in. */
669
670 struct props
671 {
672 /* The symbol index of the name of the property. */
673 short name;
674
675 /* A unique index for the property. */
676 enum prop_idx idx;
677
678 /* A handler function called to set up iterator IT from the property
679 at IT's current position. Value is used to steer handle_stop. */
680 enum prop_handled (*handler) (struct it *it);
681 };
682
683 static enum prop_handled handle_face_prop (struct it *);
684 static enum prop_handled handle_invisible_prop (struct it *);
685 static enum prop_handled handle_display_prop (struct it *);
686 static enum prop_handled handle_composition_prop (struct it *);
687 static enum prop_handled handle_overlay_change (struct it *);
688 static enum prop_handled handle_fontified_prop (struct it *);
689
690 /* Properties handled by iterators. */
691
692 static struct props it_props[] =
693 {
694 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
695 /* Handle `face' before `display' because some sub-properties of
696 `display' need to know the face. */
697 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
698 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
699 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
700 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
701 {0, 0, NULL}
702 };
703
704 /* Value is the position described by X. If X is a marker, value is
705 the marker_position of X. Otherwise, value is X. */
706
707 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
708
709 /* Enumeration returned by some move_it_.* functions internally. */
710
711 enum move_it_result
712 {
713 /* Not used. Undefined value. */
714 MOVE_UNDEFINED,
715
716 /* Move ended at the requested buffer position or ZV. */
717 MOVE_POS_MATCH_OR_ZV,
718
719 /* Move ended at the requested X pixel position. */
720 MOVE_X_REACHED,
721
722 /* Move within a line ended at the end of a line that must be
723 continued. */
724 MOVE_LINE_CONTINUED,
725
726 /* Move within a line ended at the end of a line that would
727 be displayed truncated. */
728 MOVE_LINE_TRUNCATED,
729
730 /* Move within a line ended at a line end. */
731 MOVE_NEWLINE_OR_CR
732 };
733
734 /* This counter is used to clear the face cache every once in a while
735 in redisplay_internal. It is incremented for each redisplay.
736 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
737 cleared. */
738
739 #define CLEAR_FACE_CACHE_COUNT 500
740 static int clear_face_cache_count;
741
742 /* Similarly for the image cache. */
743
744 #ifdef HAVE_WINDOW_SYSTEM
745 #define CLEAR_IMAGE_CACHE_COUNT 101
746 static int clear_image_cache_count;
747
748 /* Null glyph slice */
749 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
750 #endif
751
752 /* True while redisplay_internal is in progress. */
753
754 bool redisplaying_p;
755
756 /* If a string, XTread_socket generates an event to display that string.
757 (The display is done in read_char.) */
758
759 Lisp_Object help_echo_string;
760 Lisp_Object help_echo_window;
761 Lisp_Object help_echo_object;
762 ptrdiff_t help_echo_pos;
763
764 /* Temporary variable for XTread_socket. */
765
766 Lisp_Object previous_help_echo_string;
767
768 /* Platform-independent portion of hourglass implementation. */
769
770 #ifdef HAVE_WINDOW_SYSTEM
771
772 /* True means an hourglass cursor is currently shown. */
773 static bool hourglass_shown_p;
774
775 /* If non-null, an asynchronous timer that, when it expires, displays
776 an hourglass cursor on all frames. */
777 static struct atimer *hourglass_atimer;
778
779 #endif /* HAVE_WINDOW_SYSTEM */
780
781 /* Default number of seconds to wait before displaying an hourglass
782 cursor. */
783 #define DEFAULT_HOURGLASS_DELAY 1
784
785 #ifdef HAVE_WINDOW_SYSTEM
786
787 /* Default pixel width of `thin-space' display method. */
788 #define THIN_SPACE_WIDTH 1
789
790 #endif /* HAVE_WINDOW_SYSTEM */
791
792 /* Function prototypes. */
793
794 static void setup_for_ellipsis (struct it *, int);
795 static void set_iterator_to_next (struct it *, bool);
796 static void mark_window_display_accurate_1 (struct window *, bool);
797 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
798 static bool cursor_row_p (struct glyph_row *);
799 static int redisplay_mode_lines (Lisp_Object, bool);
800
801 static void handle_line_prefix (struct it *);
802
803 static void handle_stop_backwards (struct it *, ptrdiff_t);
804 static void unwind_with_echo_area_buffer (Lisp_Object);
805 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
806 static bool current_message_1 (ptrdiff_t, Lisp_Object);
807 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
808 static void set_message (Lisp_Object);
809 static bool set_message_1 (ptrdiff_t, Lisp_Object);
810 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
811 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
812 static void unwind_redisplay (void);
813 static void extend_face_to_end_of_line (struct it *);
814 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
815 static void push_it (struct it *, struct text_pos *);
816 static void iterate_out_of_display_property (struct it *);
817 static void pop_it (struct it *);
818 static void redisplay_internal (void);
819 static void echo_area_display (bool);
820 static void redisplay_windows (Lisp_Object);
821 static void redisplay_window (Lisp_Object, bool);
822 static Lisp_Object redisplay_window_error (Lisp_Object);
823 static Lisp_Object redisplay_window_0 (Lisp_Object);
824 static Lisp_Object redisplay_window_1 (Lisp_Object);
825 static bool set_cursor_from_row (struct window *, struct glyph_row *,
826 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
827 int, int);
828 static bool update_menu_bar (struct frame *, bool, bool);
829 static bool try_window_reusing_current_matrix (struct window *);
830 static int try_window_id (struct window *);
831 static bool display_line (struct it *);
832 static int display_mode_lines (struct window *);
833 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
834 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
835 Lisp_Object, bool);
836 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
837 Lisp_Object);
838 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
839 static void display_menu_bar (struct window *);
840 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
841 ptrdiff_t *);
842 static int display_string (const char *, Lisp_Object, Lisp_Object,
843 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
844 static void compute_line_metrics (struct it *);
845 static void run_redisplay_end_trigger_hook (struct it *);
846 static bool get_overlay_strings (struct it *, ptrdiff_t);
847 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
848 static void next_overlay_string (struct it *);
849 static void reseat (struct it *, struct text_pos, bool);
850 static void reseat_1 (struct it *, struct text_pos, bool);
851 static bool next_element_from_display_vector (struct it *);
852 static bool next_element_from_string (struct it *);
853 static bool next_element_from_c_string (struct it *);
854 static bool next_element_from_buffer (struct it *);
855 static bool next_element_from_composition (struct it *);
856 static bool next_element_from_image (struct it *);
857 static bool next_element_from_stretch (struct it *);
858 static bool next_element_from_xwidget (struct it *);
859 static void load_overlay_strings (struct it *, ptrdiff_t);
860 static bool get_next_display_element (struct it *);
861 static enum move_it_result
862 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
863 enum move_operation_enum);
864 static void get_visually_first_element (struct it *);
865 static void compute_stop_pos (struct it *);
866 static int face_before_or_after_it_pos (struct it *, bool);
867 static ptrdiff_t next_overlay_change (ptrdiff_t);
868 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
869 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
870 static int handle_single_display_spec (struct it *, Lisp_Object,
871 Lisp_Object, Lisp_Object,
872 struct text_pos *, ptrdiff_t, int, bool);
873 static int underlying_face_id (struct it *);
874
875 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
876 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
877
878 #ifdef HAVE_WINDOW_SYSTEM
879
880 static void update_tool_bar (struct frame *, bool);
881 static void x_draw_bottom_divider (struct window *w);
882 static void notice_overwritten_cursor (struct window *,
883 enum glyph_row_area,
884 int, int, int, int);
885 static int normal_char_height (struct font *, int);
886 static void normal_char_ascent_descent (struct font *, int, int *, int *);
887
888 static void append_stretch_glyph (struct it *, Lisp_Object,
889 int, int, int);
890
891 static Lisp_Object get_it_property (struct it *, Lisp_Object);
892 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
893 struct font *, int, bool);
894
895 #endif /* HAVE_WINDOW_SYSTEM */
896
897 static void produce_special_glyphs (struct it *, enum display_element_type);
898 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
899 static bool coords_in_mouse_face_p (struct window *, int, int);
900
901
902 \f
903 /***********************************************************************
904 Window display dimensions
905 ***********************************************************************/
906
907 /* Return the bottom boundary y-position for text lines in window W.
908 This is the first y position at which a line cannot start.
909 It is relative to the top of the window.
910
911 This is the height of W minus the height of a mode line, if any. */
912
913 int
914 window_text_bottom_y (struct window *w)
915 {
916 int height = WINDOW_PIXEL_HEIGHT (w);
917
918 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
919
920 if (WINDOW_WANTS_MODELINE_P (w))
921 height -= CURRENT_MODE_LINE_HEIGHT (w);
922
923 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
924
925 return height;
926 }
927
928 /* Return the pixel width of display area AREA of window W.
929 ANY_AREA means return the total width of W, not including
930 fringes to the left and right of the window. */
931
932 int
933 window_box_width (struct window *w, enum glyph_row_area area)
934 {
935 int width = w->pixel_width;
936
937 if (!w->pseudo_window_p)
938 {
939 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
940 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
941
942 if (area == TEXT_AREA)
943 width -= (WINDOW_MARGINS_WIDTH (w)
944 + WINDOW_FRINGES_WIDTH (w));
945 else if (area == LEFT_MARGIN_AREA)
946 width = WINDOW_LEFT_MARGIN_WIDTH (w);
947 else if (area == RIGHT_MARGIN_AREA)
948 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
949 }
950
951 /* With wide margins, fringes, etc. we might end up with a negative
952 width, correct that here. */
953 return max (0, width);
954 }
955
956
957 /* Return the pixel height of the display area of window W, not
958 including mode lines of W, if any. */
959
960 int
961 window_box_height (struct window *w)
962 {
963 struct frame *f = XFRAME (w->frame);
964 int height = WINDOW_PIXEL_HEIGHT (w);
965
966 eassert (height >= 0);
967
968 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
969 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
970
971 /* Note: the code below that determines the mode-line/header-line
972 height is essentially the same as that contained in the macro
973 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
974 the appropriate glyph row has its `mode_line_p' flag set,
975 and if it doesn't, uses estimate_mode_line_height instead. */
976
977 if (WINDOW_WANTS_MODELINE_P (w))
978 {
979 struct glyph_row *ml_row
980 = (w->current_matrix && w->current_matrix->rows
981 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
982 : 0);
983 if (ml_row && ml_row->mode_line_p)
984 height -= ml_row->height;
985 else
986 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
987 }
988
989 if (WINDOW_WANTS_HEADER_LINE_P (w))
990 {
991 struct glyph_row *hl_row
992 = (w->current_matrix && w->current_matrix->rows
993 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
994 : 0);
995 if (hl_row && hl_row->mode_line_p)
996 height -= hl_row->height;
997 else
998 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
999 }
1000
1001 /* With a very small font and a mode-line that's taller than
1002 default, we might end up with a negative height. */
1003 return max (0, height);
1004 }
1005
1006 /* Return the window-relative coordinate of the left edge of display
1007 area AREA of window W. ANY_AREA means return the left edge of the
1008 whole window, to the right of the left fringe of W. */
1009
1010 int
1011 window_box_left_offset (struct window *w, enum glyph_row_area area)
1012 {
1013 int x;
1014
1015 if (w->pseudo_window_p)
1016 return 0;
1017
1018 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1019
1020 if (area == TEXT_AREA)
1021 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1022 + window_box_width (w, LEFT_MARGIN_AREA));
1023 else if (area == RIGHT_MARGIN_AREA)
1024 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1025 + window_box_width (w, LEFT_MARGIN_AREA)
1026 + window_box_width (w, TEXT_AREA)
1027 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1028 ? 0
1029 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1030 else if (area == LEFT_MARGIN_AREA
1031 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1032 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1033
1034 /* Don't return more than the window's pixel width. */
1035 return min (x, w->pixel_width);
1036 }
1037
1038
1039 /* Return the window-relative coordinate of the right edge of display
1040 area AREA of window W. ANY_AREA means return the right edge of the
1041 whole window, to the left of the right fringe of W. */
1042
1043 static int
1044 window_box_right_offset (struct window *w, enum glyph_row_area area)
1045 {
1046 /* Don't return more than the window's pixel width. */
1047 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1048 w->pixel_width);
1049 }
1050
1051 /* Return the frame-relative coordinate of the left edge of display
1052 area AREA of window W. ANY_AREA means return the left edge of the
1053 whole window, to the right of the left fringe of W. */
1054
1055 int
1056 window_box_left (struct window *w, enum glyph_row_area area)
1057 {
1058 struct frame *f = XFRAME (w->frame);
1059 int x;
1060
1061 if (w->pseudo_window_p)
1062 return FRAME_INTERNAL_BORDER_WIDTH (f);
1063
1064 x = (WINDOW_LEFT_EDGE_X (w)
1065 + window_box_left_offset (w, area));
1066
1067 return x;
1068 }
1069
1070
1071 /* Return the frame-relative coordinate of the right edge of display
1072 area AREA of window W. ANY_AREA means return the right edge of the
1073 whole window, to the left of the right fringe of W. */
1074
1075 int
1076 window_box_right (struct window *w, enum glyph_row_area area)
1077 {
1078 return window_box_left (w, area) + window_box_width (w, area);
1079 }
1080
1081 /* Get the bounding box of the display area AREA of window W, without
1082 mode lines, in frame-relative coordinates. ANY_AREA means the
1083 whole window, not including the left and right fringes of
1084 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1085 coordinates of the upper-left corner of the box. Return in
1086 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1087
1088 void
1089 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1090 int *box_y, int *box_width, int *box_height)
1091 {
1092 if (box_width)
1093 *box_width = window_box_width (w, area);
1094 if (box_height)
1095 *box_height = window_box_height (w);
1096 if (box_x)
1097 *box_x = window_box_left (w, area);
1098 if (box_y)
1099 {
1100 *box_y = WINDOW_TOP_EDGE_Y (w);
1101 if (WINDOW_WANTS_HEADER_LINE_P (w))
1102 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1103 }
1104 }
1105
1106 #ifdef HAVE_WINDOW_SYSTEM
1107
1108 /* Get the bounding box of the display area AREA of window W, without
1109 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1110 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1111 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1112 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1113 box. */
1114
1115 static void
1116 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1117 int *bottom_right_x, int *bottom_right_y)
1118 {
1119 window_box (w, ANY_AREA, top_left_x, top_left_y,
1120 bottom_right_x, bottom_right_y);
1121 *bottom_right_x += *top_left_x;
1122 *bottom_right_y += *top_left_y;
1123 }
1124
1125 #endif /* HAVE_WINDOW_SYSTEM */
1126
1127 /***********************************************************************
1128 Utilities
1129 ***********************************************************************/
1130
1131 /* Return the bottom y-position of the line the iterator IT is in.
1132 This can modify IT's settings. */
1133
1134 int
1135 line_bottom_y (struct it *it)
1136 {
1137 int line_height = it->max_ascent + it->max_descent;
1138 int line_top_y = it->current_y;
1139
1140 if (line_height == 0)
1141 {
1142 if (last_height)
1143 line_height = last_height;
1144 else if (IT_CHARPOS (*it) < ZV)
1145 {
1146 move_it_by_lines (it, 1);
1147 line_height = (it->max_ascent || it->max_descent
1148 ? it->max_ascent + it->max_descent
1149 : last_height);
1150 }
1151 else
1152 {
1153 struct glyph_row *row = it->glyph_row;
1154
1155 /* Use the default character height. */
1156 it->glyph_row = NULL;
1157 it->what = IT_CHARACTER;
1158 it->c = ' ';
1159 it->len = 1;
1160 PRODUCE_GLYPHS (it);
1161 line_height = it->ascent + it->descent;
1162 it->glyph_row = row;
1163 }
1164 }
1165
1166 return line_top_y + line_height;
1167 }
1168
1169 DEFUN ("line-pixel-height", Fline_pixel_height,
1170 Sline_pixel_height, 0, 0, 0,
1171 doc: /* Return height in pixels of text line in the selected window.
1172
1173 Value is the height in pixels of the line at point. */)
1174 (void)
1175 {
1176 struct it it;
1177 struct text_pos pt;
1178 struct window *w = XWINDOW (selected_window);
1179 struct buffer *old_buffer = NULL;
1180 Lisp_Object result;
1181
1182 if (XBUFFER (w->contents) != current_buffer)
1183 {
1184 old_buffer = current_buffer;
1185 set_buffer_internal_1 (XBUFFER (w->contents));
1186 }
1187 SET_TEXT_POS (pt, PT, PT_BYTE);
1188 start_display (&it, w, pt);
1189 it.vpos = it.current_y = 0;
1190 last_height = 0;
1191 result = make_number (line_bottom_y (&it));
1192 if (old_buffer)
1193 set_buffer_internal_1 (old_buffer);
1194
1195 return result;
1196 }
1197
1198 /* Return the default pixel height of text lines in window W. The
1199 value is the canonical height of the W frame's default font, plus
1200 any extra space required by the line-spacing variable or frame
1201 parameter.
1202
1203 Implementation note: this ignores any line-spacing text properties
1204 put on the newline characters. This is because those properties
1205 only affect the _screen_ line ending in the newline (i.e., in a
1206 continued line, only the last screen line will be affected), which
1207 means only a small number of lines in a buffer can ever use this
1208 feature. Since this function is used to compute the default pixel
1209 equivalent of text lines in a window, we can safely ignore those
1210 few lines. For the same reasons, we ignore the line-height
1211 properties. */
1212 int
1213 default_line_pixel_height (struct window *w)
1214 {
1215 struct frame *f = WINDOW_XFRAME (w);
1216 int height = FRAME_LINE_HEIGHT (f);
1217
1218 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1219 {
1220 struct buffer *b = XBUFFER (w->contents);
1221 Lisp_Object val = BVAR (b, extra_line_spacing);
1222
1223 if (NILP (val))
1224 val = BVAR (&buffer_defaults, extra_line_spacing);
1225 if (!NILP (val))
1226 {
1227 if (RANGED_INTEGERP (0, val, INT_MAX))
1228 height += XFASTINT (val);
1229 else if (FLOATP (val))
1230 {
1231 int addon = XFLOAT_DATA (val) * height + 0.5;
1232
1233 if (addon >= 0)
1234 height += addon;
1235 }
1236 }
1237 else
1238 height += f->extra_line_spacing;
1239 }
1240
1241 return height;
1242 }
1243
1244 /* Subroutine of pos_visible_p below. Extracts a display string, if
1245 any, from the display spec given as its argument. */
1246 static Lisp_Object
1247 string_from_display_spec (Lisp_Object spec)
1248 {
1249 if (CONSP (spec))
1250 {
1251 while (CONSP (spec))
1252 {
1253 if (STRINGP (XCAR (spec)))
1254 return XCAR (spec);
1255 spec = XCDR (spec);
1256 }
1257 }
1258 else if (VECTORP (spec))
1259 {
1260 ptrdiff_t i;
1261
1262 for (i = 0; i < ASIZE (spec); i++)
1263 {
1264 if (STRINGP (AREF (spec, i)))
1265 return AREF (spec, i);
1266 }
1267 return Qnil;
1268 }
1269
1270 return spec;
1271 }
1272
1273
1274 /* Limit insanely large values of W->hscroll on frame F to the largest
1275 value that will still prevent first_visible_x and last_visible_x of
1276 'struct it' from overflowing an int. */
1277 static int
1278 window_hscroll_limited (struct window *w, struct frame *f)
1279 {
1280 ptrdiff_t window_hscroll = w->hscroll;
1281 int window_text_width = window_box_width (w, TEXT_AREA);
1282 int colwidth = FRAME_COLUMN_WIDTH (f);
1283
1284 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1285 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1286
1287 return window_hscroll;
1288 }
1289
1290 /* Return true if position CHARPOS is visible in window W.
1291 CHARPOS < 0 means return info about WINDOW_END position.
1292 If visible, set *X and *Y to pixel coordinates of top left corner.
1293 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1294 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1295
1296 bool
1297 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1298 int *rtop, int *rbot, int *rowh, int *vpos)
1299 {
1300 struct it it;
1301 void *itdata = bidi_shelve_cache ();
1302 struct text_pos top;
1303 bool visible_p = false;
1304 struct buffer *old_buffer = NULL;
1305 bool r2l = false;
1306
1307 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1308 return visible_p;
1309
1310 if (XBUFFER (w->contents) != current_buffer)
1311 {
1312 old_buffer = current_buffer;
1313 set_buffer_internal_1 (XBUFFER (w->contents));
1314 }
1315
1316 SET_TEXT_POS_FROM_MARKER (top, w->start);
1317 /* Scrolling a minibuffer window via scroll bar when the echo area
1318 shows long text sometimes resets the minibuffer contents behind
1319 our backs. */
1320 if (CHARPOS (top) > ZV)
1321 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1322
1323 /* Compute exact mode line heights. */
1324 if (WINDOW_WANTS_MODELINE_P (w))
1325 w->mode_line_height
1326 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1327 BVAR (current_buffer, mode_line_format));
1328
1329 if (WINDOW_WANTS_HEADER_LINE_P (w))
1330 w->header_line_height
1331 = display_mode_line (w, HEADER_LINE_FACE_ID,
1332 BVAR (current_buffer, header_line_format));
1333
1334 start_display (&it, w, top);
1335 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1336 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1337
1338 if (charpos >= 0
1339 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1340 && IT_CHARPOS (it) >= charpos)
1341 /* When scanning backwards under bidi iteration, move_it_to
1342 stops at or _before_ CHARPOS, because it stops at or to
1343 the _right_ of the character at CHARPOS. */
1344 || (it.bidi_p && it.bidi_it.scan_dir == -1
1345 && IT_CHARPOS (it) <= charpos)))
1346 {
1347 /* We have reached CHARPOS, or passed it. How the call to
1348 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1349 or covered by a display property, move_it_to stops at the end
1350 of the invisible text, to the right of CHARPOS. (ii) If
1351 CHARPOS is in a display vector, move_it_to stops on its last
1352 glyph. */
1353 int top_x = it.current_x;
1354 int top_y = it.current_y;
1355 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1356 int bottom_y;
1357 struct it save_it;
1358 void *save_it_data = NULL;
1359
1360 /* Calling line_bottom_y may change it.method, it.position, etc. */
1361 SAVE_IT (save_it, it, save_it_data);
1362 last_height = 0;
1363 bottom_y = line_bottom_y (&it);
1364 if (top_y < window_top_y)
1365 visible_p = bottom_y > window_top_y;
1366 else if (top_y < it.last_visible_y)
1367 visible_p = true;
1368 if (bottom_y >= it.last_visible_y
1369 && it.bidi_p && it.bidi_it.scan_dir == -1
1370 && IT_CHARPOS (it) < charpos)
1371 {
1372 /* When the last line of the window is scanned backwards
1373 under bidi iteration, we could be duped into thinking
1374 that we have passed CHARPOS, when in fact move_it_to
1375 simply stopped short of CHARPOS because it reached
1376 last_visible_y. To see if that's what happened, we call
1377 move_it_to again with a slightly larger vertical limit,
1378 and see if it actually moved vertically; if it did, we
1379 didn't really reach CHARPOS, which is beyond window end. */
1380 /* Why 10? because we don't know how many canonical lines
1381 will the height of the next line(s) be. So we guess. */
1382 int ten_more_lines = 10 * default_line_pixel_height (w);
1383
1384 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1385 MOVE_TO_POS | MOVE_TO_Y);
1386 if (it.current_y > top_y)
1387 visible_p = false;
1388
1389 }
1390 RESTORE_IT (&it, &save_it, save_it_data);
1391 if (visible_p)
1392 {
1393 if (it.method == GET_FROM_DISPLAY_VECTOR)
1394 {
1395 /* We stopped on the last glyph of a display vector.
1396 Try and recompute. Hack alert! */
1397 if (charpos < 2 || top.charpos >= charpos)
1398 top_x = it.glyph_row->x;
1399 else
1400 {
1401 struct it it2, it2_prev;
1402 /* The idea is to get to the previous buffer
1403 position, consume the character there, and use
1404 the pixel coordinates we get after that. But if
1405 the previous buffer position is also displayed
1406 from a display vector, we need to consume all of
1407 the glyphs from that display vector. */
1408 start_display (&it2, w, top);
1409 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1410 /* If we didn't get to CHARPOS - 1, there's some
1411 replacing display property at that position, and
1412 we stopped after it. That is exactly the place
1413 whose coordinates we want. */
1414 if (IT_CHARPOS (it2) != charpos - 1)
1415 it2_prev = it2;
1416 else
1417 {
1418 /* Iterate until we get out of the display
1419 vector that displays the character at
1420 CHARPOS - 1. */
1421 do {
1422 get_next_display_element (&it2);
1423 PRODUCE_GLYPHS (&it2);
1424 it2_prev = it2;
1425 set_iterator_to_next (&it2, true);
1426 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1427 && IT_CHARPOS (it2) < charpos);
1428 }
1429 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1430 || it2_prev.current_x > it2_prev.last_visible_x)
1431 top_x = it.glyph_row->x;
1432 else
1433 {
1434 top_x = it2_prev.current_x;
1435 top_y = it2_prev.current_y;
1436 }
1437 }
1438 }
1439 else if (IT_CHARPOS (it) != charpos)
1440 {
1441 Lisp_Object cpos = make_number (charpos);
1442 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1443 Lisp_Object string = string_from_display_spec (spec);
1444 struct text_pos tpos;
1445 bool newline_in_string
1446 = (STRINGP (string)
1447 && memchr (SDATA (string), '\n', SBYTES (string)));
1448
1449 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1450 bool replacing_spec_p
1451 = (!NILP (spec)
1452 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1453 charpos, FRAME_WINDOW_P (it.f)));
1454 /* The tricky code below is needed because there's a
1455 discrepancy between move_it_to and how we set cursor
1456 when PT is at the beginning of a portion of text
1457 covered by a display property or an overlay with a
1458 display property, or the display line ends in a
1459 newline from a display string. move_it_to will stop
1460 _after_ such display strings, whereas
1461 set_cursor_from_row conspires with cursor_row_p to
1462 place the cursor on the first glyph produced from the
1463 display string. */
1464
1465 /* We have overshoot PT because it is covered by a
1466 display property that replaces the text it covers.
1467 If the string includes embedded newlines, we are also
1468 in the wrong display line. Backtrack to the correct
1469 line, where the display property begins. */
1470 if (replacing_spec_p)
1471 {
1472 Lisp_Object startpos, endpos;
1473 EMACS_INT start, end;
1474 struct it it3;
1475
1476 /* Find the first and the last buffer positions
1477 covered by the display string. */
1478 endpos =
1479 Fnext_single_char_property_change (cpos, Qdisplay,
1480 Qnil, Qnil);
1481 startpos =
1482 Fprevious_single_char_property_change (endpos, Qdisplay,
1483 Qnil, Qnil);
1484 start = XFASTINT (startpos);
1485 end = XFASTINT (endpos);
1486 /* Move to the last buffer position before the
1487 display property. */
1488 start_display (&it3, w, top);
1489 if (start > CHARPOS (top))
1490 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1491 /* Move forward one more line if the position before
1492 the display string is a newline or if it is the
1493 rightmost character on a line that is
1494 continued or word-wrapped. */
1495 if (it3.method == GET_FROM_BUFFER
1496 && (it3.c == '\n'
1497 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1498 move_it_by_lines (&it3, 1);
1499 else if (move_it_in_display_line_to (&it3, -1,
1500 it3.current_x
1501 + it3.pixel_width,
1502 MOVE_TO_X)
1503 == MOVE_LINE_CONTINUED)
1504 {
1505 move_it_by_lines (&it3, 1);
1506 /* When we are under word-wrap, the #$@%!
1507 move_it_by_lines moves 2 lines, so we need to
1508 fix that up. */
1509 if (it3.line_wrap == WORD_WRAP)
1510 move_it_by_lines (&it3, -1);
1511 }
1512
1513 /* Record the vertical coordinate of the display
1514 line where we wound up. */
1515 top_y = it3.current_y;
1516 if (it3.bidi_p)
1517 {
1518 /* When characters are reordered for display,
1519 the character displayed to the left of the
1520 display string could be _after_ the display
1521 property in the logical order. Use the
1522 smallest vertical position of these two. */
1523 start_display (&it3, w, top);
1524 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1525 if (it3.current_y < top_y)
1526 top_y = it3.current_y;
1527 }
1528 /* Move from the top of the window to the beginning
1529 of the display line where the display string
1530 begins. */
1531 start_display (&it3, w, top);
1532 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1533 /* If it3_moved stays false after the 'while' loop
1534 below, that means we already were at a newline
1535 before the loop (e.g., the display string begins
1536 with a newline), so we don't need to (and cannot)
1537 inspect the glyphs of it3.glyph_row, because
1538 PRODUCE_GLYPHS will not produce anything for a
1539 newline, and thus it3.glyph_row stays at its
1540 stale content it got at top of the window. */
1541 bool it3_moved = false;
1542 /* Finally, advance the iterator until we hit the
1543 first display element whose character position is
1544 CHARPOS, or until the first newline from the
1545 display string, which signals the end of the
1546 display line. */
1547 while (get_next_display_element (&it3))
1548 {
1549 PRODUCE_GLYPHS (&it3);
1550 if (IT_CHARPOS (it3) == charpos
1551 || ITERATOR_AT_END_OF_LINE_P (&it3))
1552 break;
1553 it3_moved = true;
1554 set_iterator_to_next (&it3, false);
1555 }
1556 top_x = it3.current_x - it3.pixel_width;
1557 /* Normally, we would exit the above loop because we
1558 found the display element whose character
1559 position is CHARPOS. For the contingency that we
1560 didn't, and stopped at the first newline from the
1561 display string, move back over the glyphs
1562 produced from the string, until we find the
1563 rightmost glyph not from the string. */
1564 if (it3_moved
1565 && newline_in_string
1566 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1567 {
1568 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1569 + it3.glyph_row->used[TEXT_AREA];
1570
1571 while (EQ ((g - 1)->object, string))
1572 {
1573 --g;
1574 top_x -= g->pixel_width;
1575 }
1576 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1577 + it3.glyph_row->used[TEXT_AREA]);
1578 }
1579 }
1580 }
1581
1582 *x = top_x;
1583 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1584 *rtop = max (0, window_top_y - top_y);
1585 *rbot = max (0, bottom_y - it.last_visible_y);
1586 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1587 - max (top_y, window_top_y)));
1588 *vpos = it.vpos;
1589 if (it.bidi_it.paragraph_dir == R2L)
1590 r2l = true;
1591 }
1592 }
1593 else
1594 {
1595 /* Either we were asked to provide info about WINDOW_END, or
1596 CHARPOS is in the partially visible glyph row at end of
1597 window. */
1598 struct it it2;
1599 void *it2data = NULL;
1600
1601 SAVE_IT (it2, it, it2data);
1602 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1603 move_it_by_lines (&it, 1);
1604 if (charpos < IT_CHARPOS (it)
1605 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1606 {
1607 visible_p = true;
1608 RESTORE_IT (&it2, &it2, it2data);
1609 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1610 *x = it2.current_x;
1611 *y = it2.current_y + it2.max_ascent - it2.ascent;
1612 *rtop = max (0, -it2.current_y);
1613 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1614 - it.last_visible_y));
1615 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1616 it.last_visible_y)
1617 - max (it2.current_y,
1618 WINDOW_HEADER_LINE_HEIGHT (w))));
1619 *vpos = it2.vpos;
1620 if (it2.bidi_it.paragraph_dir == R2L)
1621 r2l = true;
1622 }
1623 else
1624 bidi_unshelve_cache (it2data, true);
1625 }
1626 bidi_unshelve_cache (itdata, false);
1627
1628 if (old_buffer)
1629 set_buffer_internal_1 (old_buffer);
1630
1631 if (visible_p)
1632 {
1633 if (w->hscroll > 0)
1634 *x -=
1635 window_hscroll_limited (w, WINDOW_XFRAME (w))
1636 * WINDOW_FRAME_COLUMN_WIDTH (w);
1637 /* For lines in an R2L paragraph, we need to mirror the X pixel
1638 coordinate wrt the text area. For the reasons, see the
1639 commentary in buffer_posn_from_coords and the explanation of
1640 the geometry used by the move_it_* functions at the end of
1641 the large commentary near the beginning of this file. */
1642 if (r2l)
1643 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1644 }
1645
1646 #if false
1647 /* Debugging code. */
1648 if (visible_p)
1649 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1650 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1651 else
1652 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1653 #endif
1654
1655 return visible_p;
1656 }
1657
1658
1659 /* Return the next character from STR. Return in *LEN the length of
1660 the character. This is like STRING_CHAR_AND_LENGTH but never
1661 returns an invalid character. If we find one, we return a `?', but
1662 with the length of the invalid character. */
1663
1664 static int
1665 string_char_and_length (const unsigned char *str, int *len)
1666 {
1667 int c;
1668
1669 c = STRING_CHAR_AND_LENGTH (str, *len);
1670 if (!CHAR_VALID_P (c))
1671 /* We may not change the length here because other places in Emacs
1672 don't use this function, i.e. they silently accept invalid
1673 characters. */
1674 c = '?';
1675
1676 return c;
1677 }
1678
1679
1680
1681 /* Given a position POS containing a valid character and byte position
1682 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1683
1684 static struct text_pos
1685 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1686 {
1687 eassert (STRINGP (string) && nchars >= 0);
1688
1689 if (STRING_MULTIBYTE (string))
1690 {
1691 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1692 int len;
1693
1694 while (nchars--)
1695 {
1696 string_char_and_length (p, &len);
1697 p += len;
1698 CHARPOS (pos) += 1;
1699 BYTEPOS (pos) += len;
1700 }
1701 }
1702 else
1703 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1704
1705 return pos;
1706 }
1707
1708
1709 /* Value is the text position, i.e. character and byte position,
1710 for character position CHARPOS in STRING. */
1711
1712 static struct text_pos
1713 string_pos (ptrdiff_t charpos, Lisp_Object string)
1714 {
1715 struct text_pos pos;
1716 eassert (STRINGP (string));
1717 eassert (charpos >= 0);
1718 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1719 return pos;
1720 }
1721
1722
1723 /* Value is a text position, i.e. character and byte position, for
1724 character position CHARPOS in C string S. MULTIBYTE_P
1725 means recognize multibyte characters. */
1726
1727 static struct text_pos
1728 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1729 {
1730 struct text_pos pos;
1731
1732 eassert (s != NULL);
1733 eassert (charpos >= 0);
1734
1735 if (multibyte_p)
1736 {
1737 int len;
1738
1739 SET_TEXT_POS (pos, 0, 0);
1740 while (charpos--)
1741 {
1742 string_char_and_length ((const unsigned char *) s, &len);
1743 s += len;
1744 CHARPOS (pos) += 1;
1745 BYTEPOS (pos) += len;
1746 }
1747 }
1748 else
1749 SET_TEXT_POS (pos, charpos, charpos);
1750
1751 return pos;
1752 }
1753
1754
1755 /* Value is the number of characters in C string S. MULTIBYTE_P
1756 means recognize multibyte characters. */
1757
1758 static ptrdiff_t
1759 number_of_chars (const char *s, bool multibyte_p)
1760 {
1761 ptrdiff_t nchars;
1762
1763 if (multibyte_p)
1764 {
1765 ptrdiff_t rest = strlen (s);
1766 int len;
1767 const unsigned char *p = (const unsigned char *) s;
1768
1769 for (nchars = 0; rest > 0; ++nchars)
1770 {
1771 string_char_and_length (p, &len);
1772 rest -= len, p += len;
1773 }
1774 }
1775 else
1776 nchars = strlen (s);
1777
1778 return nchars;
1779 }
1780
1781
1782 /* Compute byte position NEWPOS->bytepos corresponding to
1783 NEWPOS->charpos. POS is a known position in string STRING.
1784 NEWPOS->charpos must be >= POS.charpos. */
1785
1786 static void
1787 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1788 {
1789 eassert (STRINGP (string));
1790 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1791
1792 if (STRING_MULTIBYTE (string))
1793 *newpos = string_pos_nchars_ahead (pos, string,
1794 CHARPOS (*newpos) - CHARPOS (pos));
1795 else
1796 BYTEPOS (*newpos) = CHARPOS (*newpos);
1797 }
1798
1799 /* EXPORT:
1800 Return an estimation of the pixel height of mode or header lines on
1801 frame F. FACE_ID specifies what line's height to estimate. */
1802
1803 int
1804 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1805 {
1806 #ifdef HAVE_WINDOW_SYSTEM
1807 if (FRAME_WINDOW_P (f))
1808 {
1809 int height = FONT_HEIGHT (FRAME_FONT (f));
1810
1811 /* This function is called so early when Emacs starts that the face
1812 cache and mode line face are not yet initialized. */
1813 if (FRAME_FACE_CACHE (f))
1814 {
1815 struct face *face = FACE_FROM_ID (f, face_id);
1816 if (face)
1817 {
1818 if (face->font)
1819 height = normal_char_height (face->font, -1);
1820 if (face->box_line_width > 0)
1821 height += 2 * face->box_line_width;
1822 }
1823 }
1824
1825 return height;
1826 }
1827 #endif
1828
1829 return 1;
1830 }
1831
1832 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1833 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1834 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1835 not force the value into range. */
1836
1837 void
1838 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1839 NativeRectangle *bounds, bool noclip)
1840 {
1841
1842 #ifdef HAVE_WINDOW_SYSTEM
1843 if (FRAME_WINDOW_P (f))
1844 {
1845 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1846 even for negative values. */
1847 if (pix_x < 0)
1848 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1849 if (pix_y < 0)
1850 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1851
1852 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1853 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1854
1855 if (bounds)
1856 STORE_NATIVE_RECT (*bounds,
1857 FRAME_COL_TO_PIXEL_X (f, pix_x),
1858 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1859 FRAME_COLUMN_WIDTH (f) - 1,
1860 FRAME_LINE_HEIGHT (f) - 1);
1861
1862 /* PXW: Should we clip pixels before converting to columns/lines? */
1863 if (!noclip)
1864 {
1865 if (pix_x < 0)
1866 pix_x = 0;
1867 else if (pix_x > FRAME_TOTAL_COLS (f))
1868 pix_x = FRAME_TOTAL_COLS (f);
1869
1870 if (pix_y < 0)
1871 pix_y = 0;
1872 else if (pix_y > FRAME_TOTAL_LINES (f))
1873 pix_y = FRAME_TOTAL_LINES (f);
1874 }
1875 }
1876 #endif
1877
1878 *x = pix_x;
1879 *y = pix_y;
1880 }
1881
1882
1883 /* Find the glyph under window-relative coordinates X/Y in window W.
1884 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1885 strings. Return in *HPOS and *VPOS the row and column number of
1886 the glyph found. Return in *AREA the glyph area containing X.
1887 Value is a pointer to the glyph found or null if X/Y is not on
1888 text, or we can't tell because W's current matrix is not up to
1889 date. */
1890
1891 static struct glyph *
1892 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1893 int *dx, int *dy, int *area)
1894 {
1895 struct glyph *glyph, *end;
1896 struct glyph_row *row = NULL;
1897 int x0, i;
1898
1899 /* Find row containing Y. Give up if some row is not enabled. */
1900 for (i = 0; i < w->current_matrix->nrows; ++i)
1901 {
1902 row = MATRIX_ROW (w->current_matrix, i);
1903 if (!row->enabled_p)
1904 return NULL;
1905 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1906 break;
1907 }
1908
1909 *vpos = i;
1910 *hpos = 0;
1911
1912 /* Give up if Y is not in the window. */
1913 if (i == w->current_matrix->nrows)
1914 return NULL;
1915
1916 /* Get the glyph area containing X. */
1917 if (w->pseudo_window_p)
1918 {
1919 *area = TEXT_AREA;
1920 x0 = 0;
1921 }
1922 else
1923 {
1924 if (x < window_box_left_offset (w, TEXT_AREA))
1925 {
1926 *area = LEFT_MARGIN_AREA;
1927 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1928 }
1929 else if (x < window_box_right_offset (w, TEXT_AREA))
1930 {
1931 *area = TEXT_AREA;
1932 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1933 }
1934 else
1935 {
1936 *area = RIGHT_MARGIN_AREA;
1937 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1938 }
1939 }
1940
1941 /* Find glyph containing X. */
1942 glyph = row->glyphs[*area];
1943 end = glyph + row->used[*area];
1944 x -= x0;
1945 while (glyph < end && x >= glyph->pixel_width)
1946 {
1947 x -= glyph->pixel_width;
1948 ++glyph;
1949 }
1950
1951 if (glyph == end)
1952 return NULL;
1953
1954 if (dx)
1955 {
1956 *dx = x;
1957 *dy = y - (row->y + row->ascent - glyph->ascent);
1958 }
1959
1960 *hpos = glyph - row->glyphs[*area];
1961 return glyph;
1962 }
1963
1964 /* Convert frame-relative x/y to coordinates relative to window W.
1965 Takes pseudo-windows into account. */
1966
1967 static void
1968 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1969 {
1970 if (w->pseudo_window_p)
1971 {
1972 /* A pseudo-window is always full-width, and starts at the
1973 left edge of the frame, plus a frame border. */
1974 struct frame *f = XFRAME (w->frame);
1975 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1976 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1977 }
1978 else
1979 {
1980 *x -= WINDOW_LEFT_EDGE_X (w);
1981 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1982 }
1983 }
1984
1985 #ifdef HAVE_WINDOW_SYSTEM
1986
1987 /* EXPORT:
1988 Return in RECTS[] at most N clipping rectangles for glyph string S.
1989 Return the number of stored rectangles. */
1990
1991 int
1992 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1993 {
1994 XRectangle r;
1995
1996 if (n <= 0)
1997 return 0;
1998
1999 if (s->row->full_width_p)
2000 {
2001 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2002 r.x = WINDOW_LEFT_EDGE_X (s->w);
2003 if (s->row->mode_line_p)
2004 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2005 else
2006 r.width = WINDOW_PIXEL_WIDTH (s->w);
2007
2008 /* Unless displaying a mode or menu bar line, which are always
2009 fully visible, clip to the visible part of the row. */
2010 if (s->w->pseudo_window_p)
2011 r.height = s->row->visible_height;
2012 else
2013 r.height = s->height;
2014 }
2015 else
2016 {
2017 /* This is a text line that may be partially visible. */
2018 r.x = window_box_left (s->w, s->area);
2019 r.width = window_box_width (s->w, s->area);
2020 r.height = s->row->visible_height;
2021 }
2022
2023 if (s->clip_head)
2024 if (r.x < s->clip_head->x)
2025 {
2026 if (r.width >= s->clip_head->x - r.x)
2027 r.width -= s->clip_head->x - r.x;
2028 else
2029 r.width = 0;
2030 r.x = s->clip_head->x;
2031 }
2032 if (s->clip_tail)
2033 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2034 {
2035 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2036 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2037 else
2038 r.width = 0;
2039 }
2040
2041 /* If S draws overlapping rows, it's sufficient to use the top and
2042 bottom of the window for clipping because this glyph string
2043 intentionally draws over other lines. */
2044 if (s->for_overlaps)
2045 {
2046 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2047 r.height = window_text_bottom_y (s->w) - r.y;
2048
2049 /* Alas, the above simple strategy does not work for the
2050 environments with anti-aliased text: if the same text is
2051 drawn onto the same place multiple times, it gets thicker.
2052 If the overlap we are processing is for the erased cursor, we
2053 take the intersection with the rectangle of the cursor. */
2054 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2055 {
2056 XRectangle rc, r_save = r;
2057
2058 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2059 rc.y = s->w->phys_cursor.y;
2060 rc.width = s->w->phys_cursor_width;
2061 rc.height = s->w->phys_cursor_height;
2062
2063 x_intersect_rectangles (&r_save, &rc, &r);
2064 }
2065 }
2066 else
2067 {
2068 /* Don't use S->y for clipping because it doesn't take partially
2069 visible lines into account. For example, it can be negative for
2070 partially visible lines at the top of a window. */
2071 if (!s->row->full_width_p
2072 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2073 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2074 else
2075 r.y = max (0, s->row->y);
2076 }
2077
2078 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2079
2080 /* If drawing the cursor, don't let glyph draw outside its
2081 advertised boundaries. Cleartype does this under some circumstances. */
2082 if (s->hl == DRAW_CURSOR)
2083 {
2084 struct glyph *glyph = s->first_glyph;
2085 int height, max_y;
2086
2087 if (s->x > r.x)
2088 {
2089 if (r.width >= s->x - r.x)
2090 r.width -= s->x - r.x;
2091 else /* R2L hscrolled row with cursor outside text area */
2092 r.width = 0;
2093 r.x = s->x;
2094 }
2095 r.width = min (r.width, glyph->pixel_width);
2096
2097 /* If r.y is below window bottom, ensure that we still see a cursor. */
2098 height = min (glyph->ascent + glyph->descent,
2099 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2100 max_y = window_text_bottom_y (s->w) - height;
2101 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2102 if (s->ybase - glyph->ascent > max_y)
2103 {
2104 r.y = max_y;
2105 r.height = height;
2106 }
2107 else
2108 {
2109 /* Don't draw cursor glyph taller than our actual glyph. */
2110 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2111 if (height < r.height)
2112 {
2113 max_y = r.y + r.height;
2114 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2115 r.height = min (max_y - r.y, height);
2116 }
2117 }
2118 }
2119
2120 if (s->row->clip)
2121 {
2122 XRectangle r_save = r;
2123
2124 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2125 r.width = 0;
2126 }
2127
2128 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2129 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2130 {
2131 #ifdef CONVERT_FROM_XRECT
2132 CONVERT_FROM_XRECT (r, *rects);
2133 #else
2134 *rects = r;
2135 #endif
2136 return 1;
2137 }
2138 else
2139 {
2140 /* If we are processing overlapping and allowed to return
2141 multiple clipping rectangles, we exclude the row of the glyph
2142 string from the clipping rectangle. This is to avoid drawing
2143 the same text on the environment with anti-aliasing. */
2144 #ifdef CONVERT_FROM_XRECT
2145 XRectangle rs[2];
2146 #else
2147 XRectangle *rs = rects;
2148 #endif
2149 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2150
2151 if (s->for_overlaps & OVERLAPS_PRED)
2152 {
2153 rs[i] = r;
2154 if (r.y + r.height > row_y)
2155 {
2156 if (r.y < row_y)
2157 rs[i].height = row_y - r.y;
2158 else
2159 rs[i].height = 0;
2160 }
2161 i++;
2162 }
2163 if (s->for_overlaps & OVERLAPS_SUCC)
2164 {
2165 rs[i] = r;
2166 if (r.y < row_y + s->row->visible_height)
2167 {
2168 if (r.y + r.height > row_y + s->row->visible_height)
2169 {
2170 rs[i].y = row_y + s->row->visible_height;
2171 rs[i].height = r.y + r.height - rs[i].y;
2172 }
2173 else
2174 rs[i].height = 0;
2175 }
2176 i++;
2177 }
2178
2179 n = i;
2180 #ifdef CONVERT_FROM_XRECT
2181 for (i = 0; i < n; i++)
2182 CONVERT_FROM_XRECT (rs[i], rects[i]);
2183 #endif
2184 return n;
2185 }
2186 }
2187
2188 /* EXPORT:
2189 Return in *NR the clipping rectangle for glyph string S. */
2190
2191 void
2192 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2193 {
2194 get_glyph_string_clip_rects (s, nr, 1);
2195 }
2196
2197
2198 /* EXPORT:
2199 Return the position and height of the phys cursor in window W.
2200 Set w->phys_cursor_width to width of phys cursor.
2201 */
2202
2203 void
2204 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2205 struct glyph *glyph, int *xp, int *yp, int *heightp)
2206 {
2207 struct frame *f = XFRAME (WINDOW_FRAME (w));
2208 int x, y, wd, h, h0, y0, ascent;
2209
2210 /* Compute the width of the rectangle to draw. If on a stretch
2211 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2212 rectangle as wide as the glyph, but use a canonical character
2213 width instead. */
2214 wd = glyph->pixel_width;
2215
2216 x = w->phys_cursor.x;
2217 if (x < 0)
2218 {
2219 wd += x;
2220 x = 0;
2221 }
2222
2223 if (glyph->type == STRETCH_GLYPH
2224 && !x_stretch_cursor_p)
2225 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2226 w->phys_cursor_width = wd;
2227
2228 /* Don't let the hollow cursor glyph descend below the glyph row's
2229 ascent value, lest the hollow cursor looks funny. */
2230 y = w->phys_cursor.y;
2231 ascent = row->ascent;
2232 if (row->ascent < glyph->ascent)
2233 {
2234 y =- glyph->ascent - row->ascent;
2235 ascent = glyph->ascent;
2236 }
2237
2238 /* If y is below window bottom, ensure that we still see a cursor. */
2239 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2240
2241 h = max (h0, ascent + glyph->descent);
2242 h0 = min (h0, ascent + glyph->descent);
2243
2244 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2245 if (y < y0)
2246 {
2247 h = max (h - (y0 - y) + 1, h0);
2248 y = y0 - 1;
2249 }
2250 else
2251 {
2252 y0 = window_text_bottom_y (w) - h0;
2253 if (y > y0)
2254 {
2255 h += y - y0;
2256 y = y0;
2257 }
2258 }
2259
2260 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2261 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2262 *heightp = h;
2263 }
2264
2265 /*
2266 * Remember which glyph the mouse is over.
2267 */
2268
2269 void
2270 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2271 {
2272 Lisp_Object window;
2273 struct window *w;
2274 struct glyph_row *r, *gr, *end_row;
2275 enum window_part part;
2276 enum glyph_row_area area;
2277 int x, y, width, height;
2278
2279 /* Try to determine frame pixel position and size of the glyph under
2280 frame pixel coordinates X/Y on frame F. */
2281
2282 if (window_resize_pixelwise)
2283 {
2284 width = height = 1;
2285 goto virtual_glyph;
2286 }
2287 else if (!f->glyphs_initialized_p
2288 || (window = window_from_coordinates (f, gx, gy, &part, false),
2289 NILP (window)))
2290 {
2291 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2292 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2293 goto virtual_glyph;
2294 }
2295
2296 w = XWINDOW (window);
2297 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2298 height = WINDOW_FRAME_LINE_HEIGHT (w);
2299
2300 x = window_relative_x_coord (w, part, gx);
2301 y = gy - WINDOW_TOP_EDGE_Y (w);
2302
2303 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2304 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2305
2306 if (w->pseudo_window_p)
2307 {
2308 area = TEXT_AREA;
2309 part = ON_MODE_LINE; /* Don't adjust margin. */
2310 goto text_glyph;
2311 }
2312
2313 switch (part)
2314 {
2315 case ON_LEFT_MARGIN:
2316 area = LEFT_MARGIN_AREA;
2317 goto text_glyph;
2318
2319 case ON_RIGHT_MARGIN:
2320 area = RIGHT_MARGIN_AREA;
2321 goto text_glyph;
2322
2323 case ON_HEADER_LINE:
2324 case ON_MODE_LINE:
2325 gr = (part == ON_HEADER_LINE
2326 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2327 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2328 gy = gr->y;
2329 area = TEXT_AREA;
2330 goto text_glyph_row_found;
2331
2332 case ON_TEXT:
2333 area = TEXT_AREA;
2334
2335 text_glyph:
2336 gr = 0; gy = 0;
2337 for (; r <= end_row && r->enabled_p; ++r)
2338 if (r->y + r->height > y)
2339 {
2340 gr = r; gy = r->y;
2341 break;
2342 }
2343
2344 text_glyph_row_found:
2345 if (gr && gy <= y)
2346 {
2347 struct glyph *g = gr->glyphs[area];
2348 struct glyph *end = g + gr->used[area];
2349
2350 height = gr->height;
2351 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2352 if (gx + g->pixel_width > x)
2353 break;
2354
2355 if (g < end)
2356 {
2357 if (g->type == IMAGE_GLYPH)
2358 {
2359 /* Don't remember when mouse is over image, as
2360 image may have hot-spots. */
2361 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2362 return;
2363 }
2364 width = g->pixel_width;
2365 }
2366 else
2367 {
2368 /* Use nominal char spacing at end of line. */
2369 x -= gx;
2370 gx += (x / width) * width;
2371 }
2372
2373 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2374 {
2375 gx += window_box_left_offset (w, area);
2376 /* Don't expand over the modeline to make sure the vertical
2377 drag cursor is shown early enough. */
2378 height = min (height,
2379 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2380 }
2381 }
2382 else
2383 {
2384 /* Use nominal line height at end of window. */
2385 gx = (x / width) * width;
2386 y -= gy;
2387 gy += (y / height) * height;
2388 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2389 /* See comment above. */
2390 height = min (height,
2391 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2392 }
2393 break;
2394
2395 case ON_LEFT_FRINGE:
2396 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2397 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2398 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2399 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2400 goto row_glyph;
2401
2402 case ON_RIGHT_FRINGE:
2403 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2404 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2405 : window_box_right_offset (w, TEXT_AREA));
2406 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2407 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2408 && !WINDOW_RIGHTMOST_P (w))
2409 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2410 /* Make sure the vertical border can get her own glyph to the
2411 right of the one we build here. */
2412 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2413 else
2414 width = WINDOW_PIXEL_WIDTH (w) - gx;
2415 else
2416 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2417
2418 goto row_glyph;
2419
2420 case ON_VERTICAL_BORDER:
2421 gx = WINDOW_PIXEL_WIDTH (w) - width;
2422 goto row_glyph;
2423
2424 case ON_VERTICAL_SCROLL_BAR:
2425 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2426 ? 0
2427 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2428 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2429 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2430 : 0)));
2431 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2432
2433 row_glyph:
2434 gr = 0, gy = 0;
2435 for (; r <= end_row && r->enabled_p; ++r)
2436 if (r->y + r->height > y)
2437 {
2438 gr = r; gy = r->y;
2439 break;
2440 }
2441
2442 if (gr && gy <= y)
2443 height = gr->height;
2444 else
2445 {
2446 /* Use nominal line height at end of window. */
2447 y -= gy;
2448 gy += (y / height) * height;
2449 }
2450 break;
2451
2452 case ON_RIGHT_DIVIDER:
2453 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2454 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2455 gy = 0;
2456 /* The bottom divider prevails. */
2457 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2458 goto add_edge;
2459
2460 case ON_BOTTOM_DIVIDER:
2461 gx = 0;
2462 width = WINDOW_PIXEL_WIDTH (w);
2463 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2464 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2465 goto add_edge;
2466
2467 default:
2468 ;
2469 virtual_glyph:
2470 /* If there is no glyph under the mouse, then we divide the screen
2471 into a grid of the smallest glyph in the frame, and use that
2472 as our "glyph". */
2473
2474 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2475 round down even for negative values. */
2476 if (gx < 0)
2477 gx -= width - 1;
2478 if (gy < 0)
2479 gy -= height - 1;
2480
2481 gx = (gx / width) * width;
2482 gy = (gy / height) * height;
2483
2484 goto store_rect;
2485 }
2486
2487 add_edge:
2488 gx += WINDOW_LEFT_EDGE_X (w);
2489 gy += WINDOW_TOP_EDGE_Y (w);
2490
2491 store_rect:
2492 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2493
2494 /* Visible feedback for debugging. */
2495 #if false && defined HAVE_X_WINDOWS
2496 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2497 f->output_data.x->normal_gc,
2498 gx, gy, width, height);
2499 #endif
2500 }
2501
2502
2503 #endif /* HAVE_WINDOW_SYSTEM */
2504
2505 static void
2506 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2507 {
2508 eassert (w);
2509 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2510 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2511 w->window_end_vpos
2512 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2513 }
2514
2515 /***********************************************************************
2516 Lisp form evaluation
2517 ***********************************************************************/
2518
2519 /* Error handler for safe_eval and safe_call. */
2520
2521 static Lisp_Object
2522 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2523 {
2524 add_to_log ("Error during redisplay: %S signaled %S",
2525 Flist (nargs, args), arg);
2526 return Qnil;
2527 }
2528
2529 /* Call function FUNC with the rest of NARGS - 1 arguments
2530 following. Return the result, or nil if something went
2531 wrong. Prevent redisplay during the evaluation. */
2532
2533 static Lisp_Object
2534 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2535 {
2536 Lisp_Object val;
2537
2538 if (inhibit_eval_during_redisplay)
2539 val = Qnil;
2540 else
2541 {
2542 ptrdiff_t i;
2543 ptrdiff_t count = SPECPDL_INDEX ();
2544 Lisp_Object *args;
2545 USE_SAFE_ALLOCA;
2546 SAFE_ALLOCA_LISP (args, nargs);
2547
2548 args[0] = func;
2549 for (i = 1; i < nargs; i++)
2550 args[i] = va_arg (ap, Lisp_Object);
2551
2552 specbind (Qinhibit_redisplay, Qt);
2553 if (inhibit_quit)
2554 specbind (Qinhibit_quit, Qt);
2555 /* Use Qt to ensure debugger does not run,
2556 so there is no possibility of wanting to redisplay. */
2557 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2558 safe_eval_handler);
2559 SAFE_FREE ();
2560 val = unbind_to (count, val);
2561 }
2562
2563 return val;
2564 }
2565
2566 Lisp_Object
2567 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2568 {
2569 Lisp_Object retval;
2570 va_list ap;
2571
2572 va_start (ap, func);
2573 retval = safe__call (false, nargs, func, ap);
2574 va_end (ap);
2575 return retval;
2576 }
2577
2578 /* Call function FN with one argument ARG.
2579 Return the result, or nil if something went wrong. */
2580
2581 Lisp_Object
2582 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2583 {
2584 return safe_call (2, fn, arg);
2585 }
2586
2587 static Lisp_Object
2588 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2589 {
2590 Lisp_Object retval;
2591 va_list ap;
2592
2593 va_start (ap, fn);
2594 retval = safe__call (inhibit_quit, 2, fn, ap);
2595 va_end (ap);
2596 return retval;
2597 }
2598
2599 Lisp_Object
2600 safe_eval (Lisp_Object sexpr)
2601 {
2602 return safe__call1 (false, Qeval, sexpr);
2603 }
2604
2605 static Lisp_Object
2606 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2607 {
2608 return safe__call1 (inhibit_quit, Qeval, sexpr);
2609 }
2610
2611 /* Call function FN with two arguments ARG1 and ARG2.
2612 Return the result, or nil if something went wrong. */
2613
2614 Lisp_Object
2615 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2616 {
2617 return safe_call (3, fn, arg1, arg2);
2618 }
2619
2620
2621 \f
2622 /***********************************************************************
2623 Debugging
2624 ***********************************************************************/
2625
2626 /* Define CHECK_IT to perform sanity checks on iterators.
2627 This is for debugging. It is too slow to do unconditionally. */
2628
2629 static void
2630 CHECK_IT (struct it *it)
2631 {
2632 #if false
2633 if (it->method == GET_FROM_STRING)
2634 {
2635 eassert (STRINGP (it->string));
2636 eassert (IT_STRING_CHARPOS (*it) >= 0);
2637 }
2638 else
2639 {
2640 eassert (IT_STRING_CHARPOS (*it) < 0);
2641 if (it->method == GET_FROM_BUFFER)
2642 {
2643 /* Check that character and byte positions agree. */
2644 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2645 }
2646 }
2647
2648 if (it->dpvec)
2649 eassert (it->current.dpvec_index >= 0);
2650 else
2651 eassert (it->current.dpvec_index < 0);
2652 #endif
2653 }
2654
2655
2656 /* Check that the window end of window W is what we expect it
2657 to be---the last row in the current matrix displaying text. */
2658
2659 static void
2660 CHECK_WINDOW_END (struct window *w)
2661 {
2662 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2663 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2664 {
2665 struct glyph_row *row;
2666 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2667 !row->enabled_p
2668 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2669 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2670 }
2671 #endif
2672 }
2673
2674 /***********************************************************************
2675 Iterator initialization
2676 ***********************************************************************/
2677
2678 /* Initialize IT for displaying current_buffer in window W, starting
2679 at character position CHARPOS. CHARPOS < 0 means that no buffer
2680 position is specified which is useful when the iterator is assigned
2681 a position later. BYTEPOS is the byte position corresponding to
2682 CHARPOS.
2683
2684 If ROW is not null, calls to produce_glyphs with IT as parameter
2685 will produce glyphs in that row.
2686
2687 BASE_FACE_ID is the id of a base face to use. It must be one of
2688 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2689 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2690 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2691
2692 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2693 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2694 will be initialized to use the corresponding mode line glyph row of
2695 the desired matrix of W. */
2696
2697 void
2698 init_iterator (struct it *it, struct window *w,
2699 ptrdiff_t charpos, ptrdiff_t bytepos,
2700 struct glyph_row *row, enum face_id base_face_id)
2701 {
2702 enum face_id remapped_base_face_id = base_face_id;
2703
2704 /* Some precondition checks. */
2705 eassert (w != NULL && it != NULL);
2706 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2707 && charpos <= ZV));
2708
2709 /* If face attributes have been changed since the last redisplay,
2710 free realized faces now because they depend on face definitions
2711 that might have changed. Don't free faces while there might be
2712 desired matrices pending which reference these faces. */
2713 if (!inhibit_free_realized_faces)
2714 {
2715 if (face_change)
2716 {
2717 face_change = false;
2718 free_all_realized_faces (Qnil);
2719 }
2720 else if (XFRAME (w->frame)->face_change)
2721 {
2722 XFRAME (w->frame)->face_change = 0;
2723 free_all_realized_faces (w->frame);
2724 }
2725 }
2726
2727 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2728 if (! NILP (Vface_remapping_alist))
2729 remapped_base_face_id
2730 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2731
2732 /* Use one of the mode line rows of W's desired matrix if
2733 appropriate. */
2734 if (row == NULL)
2735 {
2736 if (base_face_id == MODE_LINE_FACE_ID
2737 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2738 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2739 else if (base_face_id == HEADER_LINE_FACE_ID)
2740 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2741 }
2742
2743 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2744 Other parts of redisplay rely on that. */
2745 memclear (it, sizeof *it);
2746 it->current.overlay_string_index = -1;
2747 it->current.dpvec_index = -1;
2748 it->base_face_id = remapped_base_face_id;
2749 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2750 it->paragraph_embedding = L2R;
2751 it->bidi_it.w = w;
2752
2753 /* The window in which we iterate over current_buffer: */
2754 XSETWINDOW (it->window, w);
2755 it->w = w;
2756 it->f = XFRAME (w->frame);
2757
2758 it->cmp_it.id = -1;
2759
2760 /* Extra space between lines (on window systems only). */
2761 if (base_face_id == DEFAULT_FACE_ID
2762 && FRAME_WINDOW_P (it->f))
2763 {
2764 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2765 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2766 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2767 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2768 * FRAME_LINE_HEIGHT (it->f));
2769 else if (it->f->extra_line_spacing > 0)
2770 it->extra_line_spacing = it->f->extra_line_spacing;
2771 }
2772
2773 /* If realized faces have been removed, e.g. because of face
2774 attribute changes of named faces, recompute them. When running
2775 in batch mode, the face cache of the initial frame is null. If
2776 we happen to get called, make a dummy face cache. */
2777 if (FRAME_FACE_CACHE (it->f) == NULL)
2778 init_frame_faces (it->f);
2779 if (FRAME_FACE_CACHE (it->f)->used == 0)
2780 recompute_basic_faces (it->f);
2781
2782 it->override_ascent = -1;
2783
2784 /* Are control characters displayed as `^C'? */
2785 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2786
2787 /* -1 means everything between a CR and the following line end
2788 is invisible. >0 means lines indented more than this value are
2789 invisible. */
2790 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2791 ? (clip_to_bounds
2792 (-1, XINT (BVAR (current_buffer, selective_display)),
2793 PTRDIFF_MAX))
2794 : (!NILP (BVAR (current_buffer, selective_display))
2795 ? -1 : 0));
2796 it->selective_display_ellipsis_p
2797 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2798
2799 /* Display table to use. */
2800 it->dp = window_display_table (w);
2801
2802 /* Are multibyte characters enabled in current_buffer? */
2803 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2804
2805 /* Get the position at which the redisplay_end_trigger hook should
2806 be run, if it is to be run at all. */
2807 if (MARKERP (w->redisplay_end_trigger)
2808 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2809 it->redisplay_end_trigger_charpos
2810 = marker_position (w->redisplay_end_trigger);
2811 else if (INTEGERP (w->redisplay_end_trigger))
2812 it->redisplay_end_trigger_charpos
2813 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2814 PTRDIFF_MAX);
2815
2816 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2817
2818 /* Are lines in the display truncated? */
2819 if (TRUNCATE != 0)
2820 it->line_wrap = TRUNCATE;
2821 if (base_face_id == DEFAULT_FACE_ID
2822 && !it->w->hscroll
2823 && (WINDOW_FULL_WIDTH_P (it->w)
2824 || NILP (Vtruncate_partial_width_windows)
2825 || (INTEGERP (Vtruncate_partial_width_windows)
2826 /* PXW: Shall we do something about this? */
2827 && (XINT (Vtruncate_partial_width_windows)
2828 <= WINDOW_TOTAL_COLS (it->w))))
2829 && NILP (BVAR (current_buffer, truncate_lines)))
2830 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2831 ? WINDOW_WRAP : WORD_WRAP;
2832
2833 /* Get dimensions of truncation and continuation glyphs. These are
2834 displayed as fringe bitmaps under X, but we need them for such
2835 frames when the fringes are turned off. But leave the dimensions
2836 zero for tooltip frames, as these glyphs look ugly there and also
2837 sabotage calculations of tooltip dimensions in x-show-tip. */
2838 #ifdef HAVE_WINDOW_SYSTEM
2839 if (!(FRAME_WINDOW_P (it->f)
2840 && FRAMEP (tip_frame)
2841 && it->f == XFRAME (tip_frame)))
2842 #endif
2843 {
2844 if (it->line_wrap == TRUNCATE)
2845 {
2846 /* We will need the truncation glyph. */
2847 eassert (it->glyph_row == NULL);
2848 produce_special_glyphs (it, IT_TRUNCATION);
2849 it->truncation_pixel_width = it->pixel_width;
2850 }
2851 else
2852 {
2853 /* We will need the continuation glyph. */
2854 eassert (it->glyph_row == NULL);
2855 produce_special_glyphs (it, IT_CONTINUATION);
2856 it->continuation_pixel_width = it->pixel_width;
2857 }
2858 }
2859
2860 /* Reset these values to zero because the produce_special_glyphs
2861 above has changed them. */
2862 it->pixel_width = it->ascent = it->descent = 0;
2863 it->phys_ascent = it->phys_descent = 0;
2864
2865 /* Set this after getting the dimensions of truncation and
2866 continuation glyphs, so that we don't produce glyphs when calling
2867 produce_special_glyphs, above. */
2868 it->glyph_row = row;
2869 it->area = TEXT_AREA;
2870
2871 /* Get the dimensions of the display area. The display area
2872 consists of the visible window area plus a horizontally scrolled
2873 part to the left of the window. All x-values are relative to the
2874 start of this total display area. */
2875 if (base_face_id != DEFAULT_FACE_ID)
2876 {
2877 /* Mode lines, menu bar in terminal frames. */
2878 it->first_visible_x = 0;
2879 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2880 }
2881 else
2882 {
2883 it->first_visible_x
2884 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2885 it->last_visible_x = (it->first_visible_x
2886 + window_box_width (w, TEXT_AREA));
2887
2888 /* If we truncate lines, leave room for the truncation glyph(s) at
2889 the right margin. Otherwise, leave room for the continuation
2890 glyph(s). Done only if the window has no right fringe. */
2891 if (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
2899 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2900 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2901 }
2902
2903 /* Leave room for a border glyph. */
2904 if (!FRAME_WINDOW_P (it->f)
2905 && !WINDOW_RIGHTMOST_P (it->w))
2906 it->last_visible_x -= 1;
2907
2908 it->last_visible_y = window_text_bottom_y (w);
2909
2910 /* For mode lines and alike, arrange for the first glyph having a
2911 left box line if the face specifies a box. */
2912 if (base_face_id != DEFAULT_FACE_ID)
2913 {
2914 struct face *face;
2915
2916 it->face_id = remapped_base_face_id;
2917
2918 /* If we have a boxed mode line, make the first character appear
2919 with a left box line. */
2920 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2921 if (face && face->box != FACE_NO_BOX)
2922 it->start_of_box_run_p = true;
2923 }
2924
2925 /* If a buffer position was specified, set the iterator there,
2926 getting overlays and face properties from that position. */
2927 if (charpos >= BUF_BEG (current_buffer))
2928 {
2929 it->stop_charpos = charpos;
2930 it->end_charpos = ZV;
2931 eassert (charpos == BYTE_TO_CHAR (bytepos));
2932 IT_CHARPOS (*it) = charpos;
2933 IT_BYTEPOS (*it) = bytepos;
2934
2935 /* We will rely on `reseat' to set this up properly, via
2936 handle_face_prop. */
2937 it->face_id = it->base_face_id;
2938
2939 it->start = it->current;
2940 /* Do we need to reorder bidirectional text? Not if this is a
2941 unibyte buffer: by definition, none of the single-byte
2942 characters are strong R2L, so no reordering is needed. And
2943 bidi.c doesn't support unibyte buffers anyway. Also, don't
2944 reorder while we are loading loadup.el, since the tables of
2945 character properties needed for reordering are not yet
2946 available. */
2947 it->bidi_p =
2948 NILP (Vpurify_flag)
2949 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2950 && it->multibyte_p;
2951
2952 /* If we are to reorder bidirectional text, init the bidi
2953 iterator. */
2954 if (it->bidi_p)
2955 {
2956 /* Since we don't know at this point whether there will be
2957 any R2L lines in the window, we reserve space for
2958 truncation/continuation glyphs even if only the left
2959 fringe is absent. */
2960 if (base_face_id == DEFAULT_FACE_ID
2961 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2962 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2963 {
2964 if (it->line_wrap == TRUNCATE)
2965 it->last_visible_x -= it->truncation_pixel_width;
2966 else
2967 it->last_visible_x -= it->continuation_pixel_width;
2968 }
2969 /* Note the paragraph direction that this buffer wants to
2970 use. */
2971 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2972 Qleft_to_right))
2973 it->paragraph_embedding = L2R;
2974 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2975 Qright_to_left))
2976 it->paragraph_embedding = R2L;
2977 else
2978 it->paragraph_embedding = NEUTRAL_DIR;
2979 bidi_unshelve_cache (NULL, false);
2980 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2981 &it->bidi_it);
2982 }
2983
2984 /* Compute faces etc. */
2985 reseat (it, it->current.pos, true);
2986 }
2987
2988 CHECK_IT (it);
2989 }
2990
2991
2992 /* Initialize IT for the display of window W with window start POS. */
2993
2994 void
2995 start_display (struct it *it, struct window *w, struct text_pos pos)
2996 {
2997 struct glyph_row *row;
2998 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
2999
3000 row = w->desired_matrix->rows + first_vpos;
3001 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3002 it->first_vpos = first_vpos;
3003
3004 /* Don't reseat to previous visible line start if current start
3005 position is in a string or image. */
3006 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3007 {
3008 int first_y = it->current_y;
3009
3010 /* If window start is not at a line start, skip forward to POS to
3011 get the correct continuation lines width. */
3012 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3013 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3014 if (!start_at_line_beg_p)
3015 {
3016 int new_x;
3017
3018 reseat_at_previous_visible_line_start (it);
3019 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3020
3021 new_x = it->current_x + it->pixel_width;
3022
3023 /* If lines are continued, this line may end in the middle
3024 of a multi-glyph character (e.g. a control character
3025 displayed as \003, or in the middle of an overlay
3026 string). In this case move_it_to above will not have
3027 taken us to the start of the continuation line but to the
3028 end of the continued line. */
3029 if (it->current_x > 0
3030 && it->line_wrap != TRUNCATE /* Lines are continued. */
3031 && (/* And glyph doesn't fit on the line. */
3032 new_x > it->last_visible_x
3033 /* Or it fits exactly and we're on a window
3034 system frame. */
3035 || (new_x == it->last_visible_x
3036 && FRAME_WINDOW_P (it->f)
3037 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3038 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3039 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3040 {
3041 if ((it->current.dpvec_index >= 0
3042 || it->current.overlay_string_index >= 0)
3043 /* If we are on a newline from a display vector or
3044 overlay string, then we are already at the end of
3045 a screen line; no need to go to the next line in
3046 that case, as this line is not really continued.
3047 (If we do go to the next line, C-e will not DTRT.) */
3048 && it->c != '\n')
3049 {
3050 set_iterator_to_next (it, true);
3051 move_it_in_display_line_to (it, -1, -1, 0);
3052 }
3053
3054 it->continuation_lines_width += it->current_x;
3055 }
3056 /* If the character at POS is displayed via a display
3057 vector, move_it_to above stops at the final glyph of
3058 IT->dpvec. To make the caller redisplay that character
3059 again (a.k.a. start at POS), we need to reset the
3060 dpvec_index to the beginning of IT->dpvec. */
3061 else if (it->current.dpvec_index >= 0)
3062 it->current.dpvec_index = 0;
3063
3064 /* We're starting a new display line, not affected by the
3065 height of the continued line, so clear the appropriate
3066 fields in the iterator structure. */
3067 it->max_ascent = it->max_descent = 0;
3068 it->max_phys_ascent = it->max_phys_descent = 0;
3069
3070 it->current_y = first_y;
3071 it->vpos = 0;
3072 it->current_x = it->hpos = 0;
3073 }
3074 }
3075 }
3076
3077
3078 /* Return true if POS is a position in ellipses displayed for invisible
3079 text. W is the window we display, for text property lookup. */
3080
3081 static bool
3082 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3083 {
3084 Lisp_Object prop, window;
3085 bool ellipses_p = false;
3086 ptrdiff_t charpos = CHARPOS (pos->pos);
3087
3088 /* If POS specifies a position in a display vector, this might
3089 be for an ellipsis displayed for invisible text. We won't
3090 get the iterator set up for delivering that ellipsis unless
3091 we make sure that it gets aware of the invisible text. */
3092 if (pos->dpvec_index >= 0
3093 && pos->overlay_string_index < 0
3094 && CHARPOS (pos->string_pos) < 0
3095 && charpos > BEGV
3096 && (XSETWINDOW (window, w),
3097 prop = Fget_char_property (make_number (charpos),
3098 Qinvisible, window),
3099 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3100 {
3101 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3102 window);
3103 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3104 }
3105
3106 return ellipses_p;
3107 }
3108
3109
3110 /* Initialize IT for stepping through current_buffer in window W,
3111 starting at position POS that includes overlay string and display
3112 vector/ control character translation position information. Value
3113 is false if there are overlay strings with newlines at POS. */
3114
3115 static bool
3116 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3117 {
3118 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3119 int i;
3120 bool overlay_strings_with_newlines = false;
3121
3122 /* If POS specifies a position in a display vector, this might
3123 be for an ellipsis displayed for invisible text. We won't
3124 get the iterator set up for delivering that ellipsis unless
3125 we make sure that it gets aware of the invisible text. */
3126 if (in_ellipses_for_invisible_text_p (pos, w))
3127 {
3128 --charpos;
3129 bytepos = 0;
3130 }
3131
3132 /* Keep in mind: the call to reseat in init_iterator skips invisible
3133 text, so we might end up at a position different from POS. This
3134 is only a problem when POS is a row start after a newline and an
3135 overlay starts there with an after-string, and the overlay has an
3136 invisible property. Since we don't skip invisible text in
3137 display_line and elsewhere immediately after consuming the
3138 newline before the row start, such a POS will not be in a string,
3139 but the call to init_iterator below will move us to the
3140 after-string. */
3141 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3142
3143 /* This only scans the current chunk -- it should scan all chunks.
3144 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3145 to 16 in 22.1 to make this a lesser problem. */
3146 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3147 {
3148 const char *s = SSDATA (it->overlay_strings[i]);
3149 const char *e = s + SBYTES (it->overlay_strings[i]);
3150
3151 while (s < e && *s != '\n')
3152 ++s;
3153
3154 if (s < e)
3155 {
3156 overlay_strings_with_newlines = true;
3157 break;
3158 }
3159 }
3160
3161 /* If position is within an overlay string, set up IT to the right
3162 overlay string. */
3163 if (pos->overlay_string_index >= 0)
3164 {
3165 int relative_index;
3166
3167 /* If the first overlay string happens to have a `display'
3168 property for an image, the iterator will be set up for that
3169 image, and we have to undo that setup first before we can
3170 correct the overlay string index. */
3171 if (it->method == GET_FROM_IMAGE)
3172 pop_it (it);
3173
3174 /* We already have the first chunk of overlay strings in
3175 IT->overlay_strings. Load more until the one for
3176 pos->overlay_string_index is in IT->overlay_strings. */
3177 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3178 {
3179 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3180 it->current.overlay_string_index = 0;
3181 while (n--)
3182 {
3183 load_overlay_strings (it, 0);
3184 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3185 }
3186 }
3187
3188 it->current.overlay_string_index = pos->overlay_string_index;
3189 relative_index = (it->current.overlay_string_index
3190 % OVERLAY_STRING_CHUNK_SIZE);
3191 it->string = it->overlay_strings[relative_index];
3192 eassert (STRINGP (it->string));
3193 it->current.string_pos = pos->string_pos;
3194 it->method = GET_FROM_STRING;
3195 it->end_charpos = SCHARS (it->string);
3196 /* Set up the bidi iterator for this overlay string. */
3197 if (it->bidi_p)
3198 {
3199 it->bidi_it.string.lstring = it->string;
3200 it->bidi_it.string.s = NULL;
3201 it->bidi_it.string.schars = SCHARS (it->string);
3202 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3203 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3204 it->bidi_it.string.unibyte = !it->multibyte_p;
3205 it->bidi_it.w = it->w;
3206 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3207 FRAME_WINDOW_P (it->f), &it->bidi_it);
3208
3209 /* Synchronize the state of the bidi iterator with
3210 pos->string_pos. For any string position other than
3211 zero, this will be done automagically when we resume
3212 iteration over the string and get_visually_first_element
3213 is called. But if string_pos is zero, and the string is
3214 to be reordered for display, we need to resync manually,
3215 since it could be that the iteration state recorded in
3216 pos ended at string_pos of 0 moving backwards in string. */
3217 if (CHARPOS (pos->string_pos) == 0)
3218 {
3219 get_visually_first_element (it);
3220 if (IT_STRING_CHARPOS (*it) != 0)
3221 do {
3222 /* Paranoia. */
3223 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3224 bidi_move_to_visually_next (&it->bidi_it);
3225 } while (it->bidi_it.charpos != 0);
3226 }
3227 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3228 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3229 }
3230 }
3231
3232 if (CHARPOS (pos->string_pos) >= 0)
3233 {
3234 /* Recorded position is not in an overlay string, but in another
3235 string. This can only be a string from a `display' property.
3236 IT should already be filled with that string. */
3237 it->current.string_pos = pos->string_pos;
3238 eassert (STRINGP (it->string));
3239 if (it->bidi_p)
3240 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3241 FRAME_WINDOW_P (it->f), &it->bidi_it);
3242 }
3243
3244 /* Restore position in display vector translations, control
3245 character translations or ellipses. */
3246 if (pos->dpvec_index >= 0)
3247 {
3248 if (it->dpvec == NULL)
3249 get_next_display_element (it);
3250 eassert (it->dpvec && it->current.dpvec_index == 0);
3251 it->current.dpvec_index = pos->dpvec_index;
3252 }
3253
3254 CHECK_IT (it);
3255 return !overlay_strings_with_newlines;
3256 }
3257
3258
3259 /* Initialize IT for stepping through current_buffer in window W
3260 starting at ROW->start. */
3261
3262 static void
3263 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3264 {
3265 init_from_display_pos (it, w, &row->start);
3266 it->start = row->start;
3267 it->continuation_lines_width = row->continuation_lines_width;
3268 CHECK_IT (it);
3269 }
3270
3271
3272 /* Initialize IT for stepping through current_buffer in window W
3273 starting in the line following ROW, i.e. starting at ROW->end.
3274 Value is false if there are overlay strings with newlines at ROW's
3275 end position. */
3276
3277 static bool
3278 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3279 {
3280 bool success = false;
3281
3282 if (init_from_display_pos (it, w, &row->end))
3283 {
3284 if (row->continued_p)
3285 it->continuation_lines_width
3286 = row->continuation_lines_width + row->pixel_width;
3287 CHECK_IT (it);
3288 success = true;
3289 }
3290
3291 return success;
3292 }
3293
3294
3295
3296 \f
3297 /***********************************************************************
3298 Text properties
3299 ***********************************************************************/
3300
3301 /* Called when IT reaches IT->stop_charpos. Handle text property and
3302 overlay changes. Set IT->stop_charpos to the next position where
3303 to stop. */
3304
3305 static void
3306 handle_stop (struct it *it)
3307 {
3308 enum prop_handled handled;
3309 bool handle_overlay_change_p;
3310 struct props *p;
3311
3312 it->dpvec = NULL;
3313 it->current.dpvec_index = -1;
3314 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3315 it->ellipsis_p = false;
3316
3317 /* Use face of preceding text for ellipsis (if invisible) */
3318 if (it->selective_display_ellipsis_p)
3319 it->saved_face_id = it->face_id;
3320
3321 /* Here's the description of the semantics of, and the logic behind,
3322 the various HANDLED_* statuses:
3323
3324 HANDLED_NORMALLY means the handler did its job, and the loop
3325 should proceed to calling the next handler in order.
3326
3327 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3328 change in the properties and overlays at current position, so the
3329 loop should be restarted, to re-invoke the handlers that were
3330 already called. This happens when fontification-functions were
3331 called by handle_fontified_prop, and actually fontified
3332 something. Another case where HANDLED_RECOMPUTE_PROPS is
3333 returned is when we discover overlay strings that need to be
3334 displayed right away. The loop below will continue for as long
3335 as the status is HANDLED_RECOMPUTE_PROPS.
3336
3337 HANDLED_RETURN means return immediately to the caller, to
3338 continue iteration without calling any further handlers. This is
3339 used when we need to act on some property right away, for example
3340 when we need to display the ellipsis or a replacing display
3341 property, such as display string or image.
3342
3343 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3344 consumed, and the handler switched to the next overlay string.
3345 This signals the loop below to refrain from looking for more
3346 overlays before all the overlay strings of the current overlay
3347 are processed.
3348
3349 Some of the handlers called by the loop push the iterator state
3350 onto the stack (see 'push_it'), and arrange for the iteration to
3351 continue with another object, such as an image, a display string,
3352 or an overlay string. In most such cases, it->stop_charpos is
3353 set to the first character of the string, so that when the
3354 iteration resumes, this function will immediately be called
3355 again, to examine the properties at the beginning of the string.
3356
3357 When a display or overlay string is exhausted, the iterator state
3358 is popped (see 'pop_it'), and iteration continues with the
3359 previous object. Again, in many such cases this function is
3360 called again to find the next position where properties might
3361 change. */
3362
3363 do
3364 {
3365 handled = HANDLED_NORMALLY;
3366
3367 /* Call text property handlers. */
3368 for (p = it_props; p->handler; ++p)
3369 {
3370 handled = p->handler (it);
3371
3372 if (handled == HANDLED_RECOMPUTE_PROPS)
3373 break;
3374 else if (handled == HANDLED_RETURN)
3375 {
3376 /* We still want to show before and after strings from
3377 overlays even if the actual buffer text is replaced. */
3378 if (!handle_overlay_change_p
3379 || it->sp > 1
3380 /* Don't call get_overlay_strings_1 if we already
3381 have overlay strings loaded, because doing so
3382 will load them again and push the iterator state
3383 onto the stack one more time, which is not
3384 expected by the rest of the code that processes
3385 overlay strings. */
3386 || (it->current.overlay_string_index < 0
3387 && !get_overlay_strings_1 (it, 0, false)))
3388 {
3389 if (it->ellipsis_p)
3390 setup_for_ellipsis (it, 0);
3391 /* When handling a display spec, we might load an
3392 empty string. In that case, discard it here. We
3393 used to discard it in handle_single_display_spec,
3394 but that causes get_overlay_strings_1, above, to
3395 ignore overlay strings that we must check. */
3396 if (STRINGP (it->string) && !SCHARS (it->string))
3397 pop_it (it);
3398 return;
3399 }
3400 else if (STRINGP (it->string) && !SCHARS (it->string))
3401 pop_it (it);
3402 else
3403 {
3404 it->string_from_display_prop_p = false;
3405 it->from_disp_prop_p = false;
3406 handle_overlay_change_p = false;
3407 }
3408 handled = HANDLED_RECOMPUTE_PROPS;
3409 break;
3410 }
3411 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3412 handle_overlay_change_p = false;
3413 }
3414
3415 if (handled != HANDLED_RECOMPUTE_PROPS)
3416 {
3417 /* Don't check for overlay strings below when set to deliver
3418 characters from a display vector. */
3419 if (it->method == GET_FROM_DISPLAY_VECTOR)
3420 handle_overlay_change_p = false;
3421
3422 /* Handle overlay changes.
3423 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3424 if it finds overlays. */
3425 if (handle_overlay_change_p)
3426 handled = handle_overlay_change (it);
3427 }
3428
3429 if (it->ellipsis_p)
3430 {
3431 setup_for_ellipsis (it, 0);
3432 break;
3433 }
3434 }
3435 while (handled == HANDLED_RECOMPUTE_PROPS);
3436
3437 /* Determine where to stop next. */
3438 if (handled == HANDLED_NORMALLY)
3439 compute_stop_pos (it);
3440 }
3441
3442
3443 /* Compute IT->stop_charpos from text property and overlay change
3444 information for IT's current position. */
3445
3446 static void
3447 compute_stop_pos (struct it *it)
3448 {
3449 register INTERVAL iv, next_iv;
3450 Lisp_Object object, limit, position;
3451 ptrdiff_t charpos, bytepos;
3452
3453 if (STRINGP (it->string))
3454 {
3455 /* Strings are usually short, so don't limit the search for
3456 properties. */
3457 it->stop_charpos = it->end_charpos;
3458 object = it->string;
3459 limit = Qnil;
3460 charpos = IT_STRING_CHARPOS (*it);
3461 bytepos = IT_STRING_BYTEPOS (*it);
3462 }
3463 else
3464 {
3465 ptrdiff_t pos;
3466
3467 /* If end_charpos is out of range for some reason, such as a
3468 misbehaving display function, rationalize it (Bug#5984). */
3469 if (it->end_charpos > ZV)
3470 it->end_charpos = ZV;
3471 it->stop_charpos = it->end_charpos;
3472
3473 /* If next overlay change is in front of the current stop pos
3474 (which is IT->end_charpos), stop there. Note: value of
3475 next_overlay_change is point-max if no overlay change
3476 follows. */
3477 charpos = IT_CHARPOS (*it);
3478 bytepos = IT_BYTEPOS (*it);
3479 pos = next_overlay_change (charpos);
3480 if (pos < it->stop_charpos)
3481 it->stop_charpos = pos;
3482
3483 /* Set up variables for computing the stop position from text
3484 property changes. */
3485 XSETBUFFER (object, current_buffer);
3486 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3487 }
3488
3489 /* Get the interval containing IT's position. Value is a null
3490 interval if there isn't such an interval. */
3491 position = make_number (charpos);
3492 iv = validate_interval_range (object, &position, &position, false);
3493 if (iv)
3494 {
3495 Lisp_Object values_here[LAST_PROP_IDX];
3496 struct props *p;
3497
3498 /* Get properties here. */
3499 for (p = it_props; p->handler; ++p)
3500 values_here[p->idx] = textget (iv->plist,
3501 builtin_lisp_symbol (p->name));
3502
3503 /* Look for an interval following iv that has different
3504 properties. */
3505 for (next_iv = next_interval (iv);
3506 (next_iv
3507 && (NILP (limit)
3508 || XFASTINT (limit) > next_iv->position));
3509 next_iv = next_interval (next_iv))
3510 {
3511 for (p = it_props; p->handler; ++p)
3512 {
3513 Lisp_Object new_value = textget (next_iv->plist,
3514 builtin_lisp_symbol (p->name));
3515 if (!EQ (values_here[p->idx], new_value))
3516 break;
3517 }
3518
3519 if (p->handler)
3520 break;
3521 }
3522
3523 if (next_iv)
3524 {
3525 if (INTEGERP (limit)
3526 && next_iv->position >= XFASTINT (limit))
3527 /* No text property change up to limit. */
3528 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3529 else
3530 /* Text properties change in next_iv. */
3531 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3532 }
3533 }
3534
3535 if (it->cmp_it.id < 0)
3536 {
3537 ptrdiff_t stoppos = it->end_charpos;
3538
3539 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3540 stoppos = -1;
3541 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3542 stoppos, it->string);
3543 }
3544
3545 eassert (STRINGP (it->string)
3546 || (it->stop_charpos >= BEGV
3547 && it->stop_charpos >= IT_CHARPOS (*it)));
3548 }
3549
3550
3551 /* Return the position of the next overlay change after POS in
3552 current_buffer. Value is point-max if no overlay change
3553 follows. This is like `next-overlay-change' but doesn't use
3554 xmalloc. */
3555
3556 static ptrdiff_t
3557 next_overlay_change (ptrdiff_t pos)
3558 {
3559 ptrdiff_t i, noverlays;
3560 ptrdiff_t endpos;
3561 Lisp_Object *overlays;
3562 USE_SAFE_ALLOCA;
3563
3564 /* Get all overlays at the given position. */
3565 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3566
3567 /* If any of these overlays ends before endpos,
3568 use its ending point instead. */
3569 for (i = 0; i < noverlays; ++i)
3570 {
3571 Lisp_Object oend;
3572 ptrdiff_t oendpos;
3573
3574 oend = OVERLAY_END (overlays[i]);
3575 oendpos = OVERLAY_POSITION (oend);
3576 endpos = min (endpos, oendpos);
3577 }
3578
3579 SAFE_FREE ();
3580 return endpos;
3581 }
3582
3583 /* How many characters forward to search for a display property or
3584 display string. Searching too far forward makes the bidi display
3585 sluggish, especially in small windows. */
3586 #define MAX_DISP_SCAN 250
3587
3588 /* Return the character position of a display string at or after
3589 position specified by POSITION. If no display string exists at or
3590 after POSITION, return ZV. A display string is either an overlay
3591 with `display' property whose value is a string, or a `display'
3592 text property whose value is a string. STRING is data about the
3593 string to iterate; if STRING->lstring is nil, we are iterating a
3594 buffer. FRAME_WINDOW_P is true when we are displaying a window
3595 on a GUI frame. DISP_PROP is set to zero if we searched
3596 MAX_DISP_SCAN characters forward without finding any display
3597 strings, non-zero otherwise. It is set to 2 if the display string
3598 uses any kind of `(space ...)' spec that will produce a stretch of
3599 white space in the text area. */
3600 ptrdiff_t
3601 compute_display_string_pos (struct text_pos *position,
3602 struct bidi_string_data *string,
3603 struct window *w,
3604 bool frame_window_p, int *disp_prop)
3605 {
3606 /* OBJECT = nil means current buffer. */
3607 Lisp_Object object, object1;
3608 Lisp_Object pos, spec, limpos;
3609 bool string_p = string && (STRINGP (string->lstring) || string->s);
3610 ptrdiff_t eob = string_p ? string->schars : ZV;
3611 ptrdiff_t begb = string_p ? 0 : BEGV;
3612 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3613 ptrdiff_t lim =
3614 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3615 struct text_pos tpos;
3616 int rv = 0;
3617
3618 if (string && STRINGP (string->lstring))
3619 object1 = object = string->lstring;
3620 else if (w && !string_p)
3621 {
3622 XSETWINDOW (object, w);
3623 object1 = Qnil;
3624 }
3625 else
3626 object1 = object = Qnil;
3627
3628 *disp_prop = 1;
3629
3630 if (charpos >= eob
3631 /* We don't support display properties whose values are strings
3632 that have display string properties. */
3633 || string->from_disp_str
3634 /* C strings cannot have display properties. */
3635 || (string->s && !STRINGP (object)))
3636 {
3637 *disp_prop = 0;
3638 return eob;
3639 }
3640
3641 /* If the character at CHARPOS is where the display string begins,
3642 return CHARPOS. */
3643 pos = make_number (charpos);
3644 if (STRINGP (object))
3645 bufpos = string->bufpos;
3646 else
3647 bufpos = charpos;
3648 tpos = *position;
3649 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3650 && (charpos <= begb
3651 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3652 object),
3653 spec))
3654 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3655 frame_window_p)))
3656 {
3657 if (rv == 2)
3658 *disp_prop = 2;
3659 return charpos;
3660 }
3661
3662 /* Look forward for the first character with a `display' property
3663 that will replace the underlying text when displayed. */
3664 limpos = make_number (lim);
3665 do {
3666 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3667 CHARPOS (tpos) = XFASTINT (pos);
3668 if (CHARPOS (tpos) >= lim)
3669 {
3670 *disp_prop = 0;
3671 break;
3672 }
3673 if (STRINGP (object))
3674 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3675 else
3676 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3677 spec = Fget_char_property (pos, Qdisplay, object);
3678 if (!STRINGP (object))
3679 bufpos = CHARPOS (tpos);
3680 } while (NILP (spec)
3681 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3682 bufpos, frame_window_p)));
3683 if (rv == 2)
3684 *disp_prop = 2;
3685
3686 return CHARPOS (tpos);
3687 }
3688
3689 /* Return the character position of the end of the display string that
3690 started at CHARPOS. If there's no display string at CHARPOS,
3691 return -1. A display string is either an overlay with `display'
3692 property whose value is a string or a `display' text property whose
3693 value is a string. */
3694 ptrdiff_t
3695 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3696 {
3697 /* OBJECT = nil means current buffer. */
3698 Lisp_Object object =
3699 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3700 Lisp_Object pos = make_number (charpos);
3701 ptrdiff_t eob =
3702 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3703
3704 if (charpos >= eob || (string->s && !STRINGP (object)))
3705 return eob;
3706
3707 /* It could happen that the display property or overlay was removed
3708 since we found it in compute_display_string_pos above. One way
3709 this can happen is if JIT font-lock was called (through
3710 handle_fontified_prop), and jit-lock-functions remove text
3711 properties or overlays from the portion of buffer that includes
3712 CHARPOS. Muse mode is known to do that, for example. In this
3713 case, we return -1 to the caller, to signal that no display
3714 string is actually present at CHARPOS. See bidi_fetch_char for
3715 how this is handled.
3716
3717 An alternative would be to never look for display properties past
3718 it->stop_charpos. But neither compute_display_string_pos nor
3719 bidi_fetch_char that calls it know or care where the next
3720 stop_charpos is. */
3721 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3722 return -1;
3723
3724 /* Look forward for the first character where the `display' property
3725 changes. */
3726 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3727
3728 return XFASTINT (pos);
3729 }
3730
3731
3732 \f
3733 /***********************************************************************
3734 Fontification
3735 ***********************************************************************/
3736
3737 /* Handle changes in the `fontified' property of the current buffer by
3738 calling hook functions from Qfontification_functions to fontify
3739 regions of text. */
3740
3741 static enum prop_handled
3742 handle_fontified_prop (struct it *it)
3743 {
3744 Lisp_Object prop, pos;
3745 enum prop_handled handled = HANDLED_NORMALLY;
3746
3747 if (!NILP (Vmemory_full))
3748 return handled;
3749
3750 /* Get the value of the `fontified' property at IT's current buffer
3751 position. (The `fontified' property doesn't have a special
3752 meaning in strings.) If the value is nil, call functions from
3753 Qfontification_functions. */
3754 if (!STRINGP (it->string)
3755 && it->s == NULL
3756 && !NILP (Vfontification_functions)
3757 && !NILP (Vrun_hooks)
3758 && (pos = make_number (IT_CHARPOS (*it)),
3759 prop = Fget_char_property (pos, Qfontified, Qnil),
3760 /* Ignore the special cased nil value always present at EOB since
3761 no amount of fontifying will be able to change it. */
3762 NILP (prop) && IT_CHARPOS (*it) < Z))
3763 {
3764 ptrdiff_t count = SPECPDL_INDEX ();
3765 Lisp_Object val;
3766 struct buffer *obuf = current_buffer;
3767 ptrdiff_t begv = BEGV, zv = ZV;
3768 bool old_clip_changed = current_buffer->clip_changed;
3769
3770 val = Vfontification_functions;
3771 specbind (Qfontification_functions, Qnil);
3772
3773 eassert (it->end_charpos == ZV);
3774
3775 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3776 safe_call1 (val, pos);
3777 else
3778 {
3779 Lisp_Object fns, fn;
3780
3781 fns = Qnil;
3782
3783 for (; CONSP (val); val = XCDR (val))
3784 {
3785 fn = XCAR (val);
3786
3787 if (EQ (fn, Qt))
3788 {
3789 /* A value of t indicates this hook has a local
3790 binding; it means to run the global binding too.
3791 In a global value, t should not occur. If it
3792 does, we must ignore it to avoid an endless
3793 loop. */
3794 for (fns = Fdefault_value (Qfontification_functions);
3795 CONSP (fns);
3796 fns = XCDR (fns))
3797 {
3798 fn = XCAR (fns);
3799 if (!EQ (fn, Qt))
3800 safe_call1 (fn, pos);
3801 }
3802 }
3803 else
3804 safe_call1 (fn, pos);
3805 }
3806 }
3807
3808 unbind_to (count, Qnil);
3809
3810 /* Fontification functions routinely call `save-restriction'.
3811 Normally, this tags clip_changed, which can confuse redisplay
3812 (see discussion in Bug#6671). Since we don't perform any
3813 special handling of fontification changes in the case where
3814 `save-restriction' isn't called, there's no point doing so in
3815 this case either. So, if the buffer's restrictions are
3816 actually left unchanged, reset clip_changed. */
3817 if (obuf == current_buffer)
3818 {
3819 if (begv == BEGV && zv == ZV)
3820 current_buffer->clip_changed = old_clip_changed;
3821 }
3822 /* There isn't much we can reasonably do to protect against
3823 misbehaving fontification, but here's a fig leaf. */
3824 else if (BUFFER_LIVE_P (obuf))
3825 set_buffer_internal_1 (obuf);
3826
3827 /* The fontification code may have added/removed text.
3828 It could do even a lot worse, but let's at least protect against
3829 the most obvious case where only the text past `pos' gets changed',
3830 as is/was done in grep.el where some escapes sequences are turned
3831 into face properties (bug#7876). */
3832 it->end_charpos = ZV;
3833
3834 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3835 something. This avoids an endless loop if they failed to
3836 fontify the text for which reason ever. */
3837 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3838 handled = HANDLED_RECOMPUTE_PROPS;
3839 }
3840
3841 return handled;
3842 }
3843
3844
3845 \f
3846 /***********************************************************************
3847 Faces
3848 ***********************************************************************/
3849
3850 /* Set up iterator IT from face properties at its current position.
3851 Called from handle_stop. */
3852
3853 static enum prop_handled
3854 handle_face_prop (struct it *it)
3855 {
3856 int new_face_id;
3857 ptrdiff_t next_stop;
3858
3859 if (!STRINGP (it->string))
3860 {
3861 new_face_id
3862 = face_at_buffer_position (it->w,
3863 IT_CHARPOS (*it),
3864 &next_stop,
3865 (IT_CHARPOS (*it)
3866 + TEXT_PROP_DISTANCE_LIMIT),
3867 false, it->base_face_id);
3868
3869 /* Is this a start of a run of characters with box face?
3870 Caveat: this can be called for a freshly initialized
3871 iterator; face_id is -1 in this case. We know that the new
3872 face will not change until limit, i.e. if the new face has a
3873 box, all characters up to limit will have one. But, as
3874 usual, we don't know whether limit is really the end. */
3875 if (new_face_id != it->face_id)
3876 {
3877 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3878 /* If it->face_id is -1, old_face below will be NULL, see
3879 the definition of FACE_FROM_ID. This will happen if this
3880 is the initial call that gets the face. */
3881 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3882
3883 /* If the value of face_id of the iterator is -1, we have to
3884 look in front of IT's position and see whether there is a
3885 face there that's different from new_face_id. */
3886 if (!old_face && IT_CHARPOS (*it) > BEG)
3887 {
3888 int prev_face_id = face_before_it_pos (it);
3889
3890 old_face = FACE_FROM_ID (it->f, prev_face_id);
3891 }
3892
3893 /* If the new face has a box, but the old face does not,
3894 this is the start of a run of characters with box face,
3895 i.e. this character has a shadow on the left side. */
3896 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3897 && (old_face == NULL || !old_face->box));
3898 it->face_box_p = new_face->box != FACE_NO_BOX;
3899 }
3900 }
3901 else
3902 {
3903 int base_face_id;
3904 ptrdiff_t bufpos;
3905 int i;
3906 Lisp_Object from_overlay
3907 = (it->current.overlay_string_index >= 0
3908 ? it->string_overlays[it->current.overlay_string_index
3909 % OVERLAY_STRING_CHUNK_SIZE]
3910 : Qnil);
3911
3912 /* See if we got to this string directly or indirectly from
3913 an overlay property. That includes the before-string or
3914 after-string of an overlay, strings in display properties
3915 provided by an overlay, their text properties, etc.
3916
3917 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3918 if (! NILP (from_overlay))
3919 for (i = it->sp - 1; i >= 0; i--)
3920 {
3921 if (it->stack[i].current.overlay_string_index >= 0)
3922 from_overlay
3923 = it->string_overlays[it->stack[i].current.overlay_string_index
3924 % OVERLAY_STRING_CHUNK_SIZE];
3925 else if (! NILP (it->stack[i].from_overlay))
3926 from_overlay = it->stack[i].from_overlay;
3927
3928 if (!NILP (from_overlay))
3929 break;
3930 }
3931
3932 if (! NILP (from_overlay))
3933 {
3934 bufpos = IT_CHARPOS (*it);
3935 /* For a string from an overlay, the base face depends
3936 only on text properties and ignores overlays. */
3937 base_face_id
3938 = face_for_overlay_string (it->w,
3939 IT_CHARPOS (*it),
3940 &next_stop,
3941 (IT_CHARPOS (*it)
3942 + TEXT_PROP_DISTANCE_LIMIT),
3943 false,
3944 from_overlay);
3945 }
3946 else
3947 {
3948 bufpos = 0;
3949
3950 /* For strings from a `display' property, use the face at
3951 IT's current buffer position as the base face to merge
3952 with, so that overlay strings appear in the same face as
3953 surrounding text, unless they specify their own faces.
3954 For strings from wrap-prefix and line-prefix properties,
3955 use the default face, possibly remapped via
3956 Vface_remapping_alist. */
3957 /* Note that the fact that we use the face at _buffer_
3958 position means that a 'display' property on an overlay
3959 string will not inherit the face of that overlay string,
3960 but will instead revert to the face of buffer text
3961 covered by the overlay. This is visible, e.g., when the
3962 overlay specifies a box face, but neither the buffer nor
3963 the display string do. This sounds like a design bug,
3964 but Emacs always did that since v21.1, so changing that
3965 might be a big deal. */
3966 base_face_id = it->string_from_prefix_prop_p
3967 ? (!NILP (Vface_remapping_alist)
3968 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3969 : DEFAULT_FACE_ID)
3970 : underlying_face_id (it);
3971 }
3972
3973 new_face_id = face_at_string_position (it->w,
3974 it->string,
3975 IT_STRING_CHARPOS (*it),
3976 bufpos,
3977 &next_stop,
3978 base_face_id, false);
3979
3980 /* Is this a start of a run of characters with box? Caveat:
3981 this can be called for a freshly allocated iterator; face_id
3982 is -1 is this case. We know that the new face will not
3983 change until the next check pos, i.e. if the new face has a
3984 box, all characters up to that position will have a
3985 box. But, as usual, we don't know whether that position
3986 is really the end. */
3987 if (new_face_id != it->face_id)
3988 {
3989 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3990 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3991
3992 /* If new face has a box but old face hasn't, this is the
3993 start of a run of characters with box, i.e. it has a
3994 shadow on the left side. */
3995 it->start_of_box_run_p
3996 = new_face->box && (old_face == NULL || !old_face->box);
3997 it->face_box_p = new_face->box != FACE_NO_BOX;
3998 }
3999 }
4000
4001 it->face_id = new_face_id;
4002 return HANDLED_NORMALLY;
4003 }
4004
4005
4006 /* Return the ID of the face ``underlying'' IT's current position,
4007 which is in a string. If the iterator is associated with a
4008 buffer, return the face at IT's current buffer position.
4009 Otherwise, use the iterator's base_face_id. */
4010
4011 static int
4012 underlying_face_id (struct it *it)
4013 {
4014 int face_id = it->base_face_id, i;
4015
4016 eassert (STRINGP (it->string));
4017
4018 for (i = it->sp - 1; i >= 0; --i)
4019 if (NILP (it->stack[i].string))
4020 face_id = it->stack[i].face_id;
4021
4022 return face_id;
4023 }
4024
4025
4026 /* Compute the face one character before or after the current position
4027 of IT, in the visual order. BEFORE_P means get the face
4028 in front (to the left in L2R paragraphs, to the right in R2L
4029 paragraphs) of IT's screen position. Value is the ID of the face. */
4030
4031 static int
4032 face_before_or_after_it_pos (struct it *it, bool before_p)
4033 {
4034 int face_id, limit;
4035 ptrdiff_t next_check_charpos;
4036 struct it it_copy;
4037 void *it_copy_data = NULL;
4038
4039 eassert (it->s == NULL);
4040
4041 if (STRINGP (it->string))
4042 {
4043 ptrdiff_t bufpos, charpos;
4044 int base_face_id;
4045
4046 /* No face change past the end of the string (for the case
4047 we are padding with spaces). No face change before the
4048 string start. */
4049 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4050 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4051 return it->face_id;
4052
4053 if (!it->bidi_p)
4054 {
4055 /* Set charpos to the position before or after IT's current
4056 position, in the logical order, which in the non-bidi
4057 case is the same as the visual order. */
4058 if (before_p)
4059 charpos = IT_STRING_CHARPOS (*it) - 1;
4060 else if (it->what == IT_COMPOSITION)
4061 /* For composition, we must check the character after the
4062 composition. */
4063 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4064 else
4065 charpos = IT_STRING_CHARPOS (*it) + 1;
4066 }
4067 else
4068 {
4069 if (before_p)
4070 {
4071 /* With bidi iteration, the character before the current
4072 in the visual order cannot be found by simple
4073 iteration, because "reverse" reordering is not
4074 supported. Instead, we need to start from the string
4075 beginning and go all the way to the current string
4076 position, remembering the previous position. */
4077 /* Ignore face changes before the first visible
4078 character on this display line. */
4079 if (it->current_x <= it->first_visible_x)
4080 return it->face_id;
4081 SAVE_IT (it_copy, *it, it_copy_data);
4082 IT_STRING_CHARPOS (it_copy) = 0;
4083 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4084
4085 do
4086 {
4087 charpos = IT_STRING_CHARPOS (it_copy);
4088 if (charpos >= SCHARS (it->string))
4089 break;
4090 bidi_move_to_visually_next (&it_copy.bidi_it);
4091 }
4092 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4093
4094 RESTORE_IT (it, it, it_copy_data);
4095 }
4096 else
4097 {
4098 /* Set charpos to the string position of the character
4099 that comes after IT's current position in the visual
4100 order. */
4101 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4102
4103 it_copy = *it;
4104 while (n--)
4105 bidi_move_to_visually_next (&it_copy.bidi_it);
4106
4107 charpos = it_copy.bidi_it.charpos;
4108 }
4109 }
4110 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4111
4112 if (it->current.overlay_string_index >= 0)
4113 bufpos = IT_CHARPOS (*it);
4114 else
4115 bufpos = 0;
4116
4117 base_face_id = underlying_face_id (it);
4118
4119 /* Get the face for ASCII, or unibyte. */
4120 face_id = face_at_string_position (it->w,
4121 it->string,
4122 charpos,
4123 bufpos,
4124 &next_check_charpos,
4125 base_face_id, false);
4126
4127 /* Correct the face for charsets different from ASCII. Do it
4128 for the multibyte case only. The face returned above is
4129 suitable for unibyte text if IT->string is unibyte. */
4130 if (STRING_MULTIBYTE (it->string))
4131 {
4132 struct text_pos pos1 = string_pos (charpos, it->string);
4133 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4134 int c, len;
4135 struct face *face = FACE_FROM_ID (it->f, face_id);
4136
4137 c = string_char_and_length (p, &len);
4138 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4139 }
4140 }
4141 else
4142 {
4143 struct text_pos pos;
4144
4145 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4146 || (IT_CHARPOS (*it) <= BEGV && before_p))
4147 return it->face_id;
4148
4149 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4150 pos = it->current.pos;
4151
4152 if (!it->bidi_p)
4153 {
4154 if (before_p)
4155 DEC_TEXT_POS (pos, it->multibyte_p);
4156 else
4157 {
4158 if (it->what == IT_COMPOSITION)
4159 {
4160 /* For composition, we must check the position after
4161 the composition. */
4162 pos.charpos += it->cmp_it.nchars;
4163 pos.bytepos += it->len;
4164 }
4165 else
4166 INC_TEXT_POS (pos, it->multibyte_p);
4167 }
4168 }
4169 else
4170 {
4171 if (before_p)
4172 {
4173 int current_x;
4174
4175 /* With bidi iteration, the character before the current
4176 in the visual order cannot be found by simple
4177 iteration, because "reverse" reordering is not
4178 supported. Instead, we need to use the move_it_*
4179 family of functions, and move to the previous
4180 character starting from the beginning of the visual
4181 line. */
4182 /* Ignore face changes before the first visible
4183 character on this display line. */
4184 if (it->current_x <= it->first_visible_x)
4185 return it->face_id;
4186 SAVE_IT (it_copy, *it, it_copy_data);
4187 /* Implementation note: Since move_it_in_display_line
4188 works in the iterator geometry, and thinks the first
4189 character is always the leftmost, even in R2L lines,
4190 we don't need to distinguish between the R2L and L2R
4191 cases here. */
4192 current_x = it_copy.current_x;
4193 move_it_vertically_backward (&it_copy, 0);
4194 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4195 pos = it_copy.current.pos;
4196 RESTORE_IT (it, it, it_copy_data);
4197 }
4198 else
4199 {
4200 /* Set charpos to the buffer position of the character
4201 that comes after IT's current position in the visual
4202 order. */
4203 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4204
4205 it_copy = *it;
4206 while (n--)
4207 bidi_move_to_visually_next (&it_copy.bidi_it);
4208
4209 SET_TEXT_POS (pos,
4210 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4211 }
4212 }
4213 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4214
4215 /* Determine face for CHARSET_ASCII, or unibyte. */
4216 face_id = face_at_buffer_position (it->w,
4217 CHARPOS (pos),
4218 &next_check_charpos,
4219 limit, false, -1);
4220
4221 /* Correct the face for charsets different from ASCII. Do it
4222 for the multibyte case only. The face returned above is
4223 suitable for unibyte text if current_buffer is unibyte. */
4224 if (it->multibyte_p)
4225 {
4226 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4227 struct face *face = FACE_FROM_ID (it->f, face_id);
4228 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4229 }
4230 }
4231
4232 return face_id;
4233 }
4234
4235
4236 \f
4237 /***********************************************************************
4238 Invisible text
4239 ***********************************************************************/
4240
4241 /* Set up iterator IT from invisible properties at its current
4242 position. Called from handle_stop. */
4243
4244 static enum prop_handled
4245 handle_invisible_prop (struct it *it)
4246 {
4247 enum prop_handled handled = HANDLED_NORMALLY;
4248 int invis;
4249 Lisp_Object prop;
4250
4251 if (STRINGP (it->string))
4252 {
4253 Lisp_Object end_charpos, limit;
4254
4255 /* Get the value of the invisible text property at the
4256 current position. Value will be nil if there is no such
4257 property. */
4258 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4259 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4260 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4261
4262 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4263 {
4264 /* Record whether we have to display an ellipsis for the
4265 invisible text. */
4266 bool display_ellipsis_p = (invis == 2);
4267 ptrdiff_t len, endpos;
4268
4269 handled = HANDLED_RECOMPUTE_PROPS;
4270
4271 /* Get the position at which the next visible text can be
4272 found in IT->string, if any. */
4273 endpos = len = SCHARS (it->string);
4274 XSETINT (limit, len);
4275 do
4276 {
4277 end_charpos
4278 = Fnext_single_property_change (end_charpos, Qinvisible,
4279 it->string, limit);
4280 /* Since LIMIT is always an integer, so should be the
4281 value returned by Fnext_single_property_change. */
4282 eassert (INTEGERP (end_charpos));
4283 if (INTEGERP (end_charpos))
4284 {
4285 endpos = XFASTINT (end_charpos);
4286 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4287 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4288 if (invis == 2)
4289 display_ellipsis_p = true;
4290 }
4291 else /* Should never happen; but if it does, exit the loop. */
4292 endpos = len;
4293 }
4294 while (invis != 0 && endpos < len);
4295
4296 if (display_ellipsis_p)
4297 it->ellipsis_p = true;
4298
4299 if (endpos < len)
4300 {
4301 /* Text at END_CHARPOS is visible. Move IT there. */
4302 struct text_pos old;
4303 ptrdiff_t oldpos;
4304
4305 old = it->current.string_pos;
4306 oldpos = CHARPOS (old);
4307 if (it->bidi_p)
4308 {
4309 if (it->bidi_it.first_elt
4310 && it->bidi_it.charpos < SCHARS (it->string))
4311 bidi_paragraph_init (it->paragraph_embedding,
4312 &it->bidi_it, true);
4313 /* Bidi-iterate out of the invisible text. */
4314 do
4315 {
4316 bidi_move_to_visually_next (&it->bidi_it);
4317 }
4318 while (oldpos <= it->bidi_it.charpos
4319 && it->bidi_it.charpos < endpos);
4320
4321 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4322 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4323 if (IT_CHARPOS (*it) >= endpos)
4324 it->prev_stop = endpos;
4325 }
4326 else
4327 {
4328 IT_STRING_CHARPOS (*it) = endpos;
4329 compute_string_pos (&it->current.string_pos, old, it->string);
4330 }
4331 }
4332 else
4333 {
4334 /* The rest of the string is invisible. If this is an
4335 overlay string, proceed with the next overlay string
4336 or whatever comes and return a character from there. */
4337 if (it->current.overlay_string_index >= 0
4338 && !display_ellipsis_p)
4339 {
4340 next_overlay_string (it);
4341 /* Don't check for overlay strings when we just
4342 finished processing them. */
4343 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4344 }
4345 else
4346 {
4347 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4348 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4349 }
4350 }
4351 }
4352 }
4353 else
4354 {
4355 ptrdiff_t newpos, next_stop, start_charpos, tem;
4356 Lisp_Object pos, overlay;
4357
4358 /* First of all, is there invisible text at this position? */
4359 tem = start_charpos = IT_CHARPOS (*it);
4360 pos = make_number (tem);
4361 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4362 &overlay);
4363 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4364
4365 /* If we are on invisible text, skip over it. */
4366 if (invis != 0 && start_charpos < it->end_charpos)
4367 {
4368 /* Record whether we have to display an ellipsis for the
4369 invisible text. */
4370 bool display_ellipsis_p = invis == 2;
4371
4372 handled = HANDLED_RECOMPUTE_PROPS;
4373
4374 /* Loop skipping over invisible text. The loop is left at
4375 ZV or with IT on the first char being visible again. */
4376 do
4377 {
4378 /* Try to skip some invisible text. Return value is the
4379 position reached which can be equal to where we start
4380 if there is nothing invisible there. This skips both
4381 over invisible text properties and overlays with
4382 invisible property. */
4383 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4384
4385 /* If we skipped nothing at all we weren't at invisible
4386 text in the first place. If everything to the end of
4387 the buffer was skipped, end the loop. */
4388 if (newpos == tem || newpos >= ZV)
4389 invis = 0;
4390 else
4391 {
4392 /* We skipped some characters but not necessarily
4393 all there are. Check if we ended up on visible
4394 text. Fget_char_property returns the property of
4395 the char before the given position, i.e. if we
4396 get invis = 0, this means that the char at
4397 newpos is visible. */
4398 pos = make_number (newpos);
4399 prop = Fget_char_property (pos, Qinvisible, it->window);
4400 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4401 }
4402
4403 /* If we ended up on invisible text, proceed to
4404 skip starting with next_stop. */
4405 if (invis != 0)
4406 tem = next_stop;
4407
4408 /* If there are adjacent invisible texts, don't lose the
4409 second one's ellipsis. */
4410 if (invis == 2)
4411 display_ellipsis_p = true;
4412 }
4413 while (invis != 0);
4414
4415 /* The position newpos is now either ZV or on visible text. */
4416 if (it->bidi_p)
4417 {
4418 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4419 bool on_newline
4420 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4421 bool after_newline
4422 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4423
4424 /* If the invisible text ends on a newline or on a
4425 character after a newline, we can avoid the costly,
4426 character by character, bidi iteration to NEWPOS, and
4427 instead simply reseat the iterator there. That's
4428 because all bidi reordering information is tossed at
4429 the newline. This is a big win for modes that hide
4430 complete lines, like Outline, Org, etc. */
4431 if (on_newline || after_newline)
4432 {
4433 struct text_pos tpos;
4434 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4435
4436 SET_TEXT_POS (tpos, newpos, bpos);
4437 reseat_1 (it, tpos, false);
4438 /* If we reseat on a newline/ZV, we need to prep the
4439 bidi iterator for advancing to the next character
4440 after the newline/EOB, keeping the current paragraph
4441 direction (so that PRODUCE_GLYPHS does TRT wrt
4442 prepending/appending glyphs to a glyph row). */
4443 if (on_newline)
4444 {
4445 it->bidi_it.first_elt = false;
4446 it->bidi_it.paragraph_dir = pdir;
4447 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4448 it->bidi_it.nchars = 1;
4449 it->bidi_it.ch_len = 1;
4450 }
4451 }
4452 else /* Must use the slow method. */
4453 {
4454 /* With bidi iteration, the region of invisible text
4455 could start and/or end in the middle of a
4456 non-base embedding level. Therefore, we need to
4457 skip invisible text using the bidi iterator,
4458 starting at IT's current position, until we find
4459 ourselves outside of the invisible text.
4460 Skipping invisible text _after_ bidi iteration
4461 avoids affecting the visual order of the
4462 displayed text when invisible properties are
4463 added or removed. */
4464 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4465 {
4466 /* If we were `reseat'ed to a new paragraph,
4467 determine the paragraph base direction. We
4468 need to do it now because
4469 next_element_from_buffer may not have a
4470 chance to do it, if we are going to skip any
4471 text at the beginning, which resets the
4472 FIRST_ELT flag. */
4473 bidi_paragraph_init (it->paragraph_embedding,
4474 &it->bidi_it, true);
4475 }
4476 do
4477 {
4478 bidi_move_to_visually_next (&it->bidi_it);
4479 }
4480 while (it->stop_charpos <= it->bidi_it.charpos
4481 && it->bidi_it.charpos < newpos);
4482 IT_CHARPOS (*it) = it->bidi_it.charpos;
4483 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4484 /* If we overstepped NEWPOS, record its position in
4485 the iterator, so that we skip invisible text if
4486 later the bidi iteration lands us in the
4487 invisible region again. */
4488 if (IT_CHARPOS (*it) >= newpos)
4489 it->prev_stop = newpos;
4490 }
4491 }
4492 else
4493 {
4494 IT_CHARPOS (*it) = newpos;
4495 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4496 }
4497
4498 if (display_ellipsis_p)
4499 {
4500 /* Make sure that the glyphs of the ellipsis will get
4501 correct `charpos' values. If we would not update
4502 it->position here, the glyphs would belong to the
4503 last visible character _before_ the invisible
4504 text, which confuses `set_cursor_from_row'.
4505
4506 We use the last invisible position instead of the
4507 first because this way the cursor is always drawn on
4508 the first "." of the ellipsis, whenever PT is inside
4509 the invisible text. Otherwise the cursor would be
4510 placed _after_ the ellipsis when the point is after the
4511 first invisible character. */
4512 if (!STRINGP (it->object))
4513 {
4514 it->position.charpos = newpos - 1;
4515 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4516 }
4517 }
4518
4519 /* If there are before-strings at the start of invisible
4520 text, and the text is invisible because of a text
4521 property, arrange to show before-strings because 20.x did
4522 it that way. (If the text is invisible because of an
4523 overlay property instead of a text property, this is
4524 already handled in the overlay code.) */
4525 if (NILP (overlay)
4526 && get_overlay_strings (it, it->stop_charpos))
4527 {
4528 handled = HANDLED_RECOMPUTE_PROPS;
4529 if (it->sp > 0)
4530 {
4531 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4532 /* The call to get_overlay_strings above recomputes
4533 it->stop_charpos, but it only considers changes
4534 in properties and overlays beyond iterator's
4535 current position. This causes us to miss changes
4536 that happen exactly where the invisible property
4537 ended. So we play it safe here and force the
4538 iterator to check for potential stop positions
4539 immediately after the invisible text. Note that
4540 if get_overlay_strings returns true, it
4541 normally also pushed the iterator stack, so we
4542 need to update the stop position in the slot
4543 below the current one. */
4544 it->stack[it->sp - 1].stop_charpos
4545 = CHARPOS (it->stack[it->sp - 1].current.pos);
4546 }
4547 }
4548 else if (display_ellipsis_p)
4549 {
4550 it->ellipsis_p = true;
4551 /* Let the ellipsis display before
4552 considering any properties of the following char.
4553 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4554 handled = HANDLED_RETURN;
4555 }
4556 }
4557 }
4558
4559 return handled;
4560 }
4561
4562
4563 /* Make iterator IT return `...' next.
4564 Replaces LEN characters from buffer. */
4565
4566 static void
4567 setup_for_ellipsis (struct it *it, int len)
4568 {
4569 /* Use the display table definition for `...'. Invalid glyphs
4570 will be handled by the method returning elements from dpvec. */
4571 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4572 {
4573 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4574 it->dpvec = v->contents;
4575 it->dpend = v->contents + v->header.size;
4576 }
4577 else
4578 {
4579 /* Default `...'. */
4580 it->dpvec = default_invis_vector;
4581 it->dpend = default_invis_vector + 3;
4582 }
4583
4584 it->dpvec_char_len = len;
4585 it->current.dpvec_index = 0;
4586 it->dpvec_face_id = -1;
4587
4588 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4589 face as the preceding text. IT->saved_face_id was set in
4590 handle_stop to the face of the preceding character, and will be
4591 different from IT->face_id only if the invisible text skipped in
4592 handle_invisible_prop has some non-default face on its first
4593 character. We thus ignore the face of the invisible text when we
4594 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4595 if (it->saved_face_id >= 0)
4596 it->face_id = it->saved_face_id;
4597
4598 /* If the ellipsis represents buffer text, it means we advanced in
4599 the buffer, so we should no longer ignore overlay strings. */
4600 if (it->method == GET_FROM_BUFFER)
4601 it->ignore_overlay_strings_at_pos_p = false;
4602
4603 it->method = GET_FROM_DISPLAY_VECTOR;
4604 it->ellipsis_p = true;
4605 }
4606
4607
4608 \f
4609 /***********************************************************************
4610 'display' property
4611 ***********************************************************************/
4612
4613 /* Set up iterator IT from `display' property at its current position.
4614 Called from handle_stop.
4615 We return HANDLED_RETURN if some part of the display property
4616 overrides the display of the buffer text itself.
4617 Otherwise we return HANDLED_NORMALLY. */
4618
4619 static enum prop_handled
4620 handle_display_prop (struct it *it)
4621 {
4622 Lisp_Object propval, object, overlay;
4623 struct text_pos *position;
4624 ptrdiff_t bufpos;
4625 /* Nonzero if some property replaces the display of the text itself. */
4626 int display_replaced = 0;
4627
4628 if (STRINGP (it->string))
4629 {
4630 object = it->string;
4631 position = &it->current.string_pos;
4632 bufpos = CHARPOS (it->current.pos);
4633 }
4634 else
4635 {
4636 XSETWINDOW (object, it->w);
4637 position = &it->current.pos;
4638 bufpos = CHARPOS (*position);
4639 }
4640
4641 /* Reset those iterator values set from display property values. */
4642 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4643 it->space_width = Qnil;
4644 it->font_height = Qnil;
4645 it->voffset = 0;
4646
4647 /* We don't support recursive `display' properties, i.e. string
4648 values that have a string `display' property, that have a string
4649 `display' property etc. */
4650 if (!it->string_from_display_prop_p)
4651 it->area = TEXT_AREA;
4652
4653 propval = get_char_property_and_overlay (make_number (position->charpos),
4654 Qdisplay, object, &overlay);
4655 if (NILP (propval))
4656 return HANDLED_NORMALLY;
4657 /* Now OVERLAY is the overlay that gave us this property, or nil
4658 if it was a text property. */
4659
4660 if (!STRINGP (it->string))
4661 object = it->w->contents;
4662
4663 display_replaced = handle_display_spec (it, propval, object, overlay,
4664 position, bufpos,
4665 FRAME_WINDOW_P (it->f));
4666 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4667 }
4668
4669 /* Subroutine of handle_display_prop. Returns non-zero if the display
4670 specification in SPEC is a replacing specification, i.e. it would
4671 replace the text covered by `display' property with something else,
4672 such as an image or a display string. If SPEC includes any kind or
4673 `(space ...) specification, the value is 2; this is used by
4674 compute_display_string_pos, which see.
4675
4676 See handle_single_display_spec for documentation of arguments.
4677 FRAME_WINDOW_P is true if the window being redisplayed is on a
4678 GUI frame; this argument is used only if IT is NULL, see below.
4679
4680 IT can be NULL, if this is called by the bidi reordering code
4681 through compute_display_string_pos, which see. In that case, this
4682 function only examines SPEC, but does not otherwise "handle" it, in
4683 the sense that it doesn't set up members of IT from the display
4684 spec. */
4685 static int
4686 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4687 Lisp_Object overlay, struct text_pos *position,
4688 ptrdiff_t bufpos, bool frame_window_p)
4689 {
4690 int replacing = 0;
4691
4692 if (CONSP (spec)
4693 /* Simple specifications. */
4694 && !EQ (XCAR (spec), Qimage)
4695 #ifdef HAVE_XWIDGETS
4696 && !EQ (XCAR (spec), Qxwidget)
4697 #endif
4698 && !EQ (XCAR (spec), Qspace)
4699 && !EQ (XCAR (spec), Qwhen)
4700 && !EQ (XCAR (spec), Qslice)
4701 && !EQ (XCAR (spec), Qspace_width)
4702 && !EQ (XCAR (spec), Qheight)
4703 && !EQ (XCAR (spec), Qraise)
4704 /* Marginal area specifications. */
4705 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4706 && !EQ (XCAR (spec), Qleft_fringe)
4707 && !EQ (XCAR (spec), Qright_fringe)
4708 && !NILP (XCAR (spec)))
4709 {
4710 for (; CONSP (spec); spec = XCDR (spec))
4711 {
4712 int rv = handle_single_display_spec (it, XCAR (spec), object,
4713 overlay, position, bufpos,
4714 replacing, frame_window_p);
4715 if (rv != 0)
4716 {
4717 replacing = rv;
4718 /* If some text in a string is replaced, `position' no
4719 longer points to the position of `object'. */
4720 if (!it || STRINGP (object))
4721 break;
4722 }
4723 }
4724 }
4725 else if (VECTORP (spec))
4726 {
4727 ptrdiff_t i;
4728 for (i = 0; i < ASIZE (spec); ++i)
4729 {
4730 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4731 overlay, position, bufpos,
4732 replacing, frame_window_p);
4733 if (rv != 0)
4734 {
4735 replacing = rv;
4736 /* If some text in a string is replaced, `position' no
4737 longer points to the position of `object'. */
4738 if (!it || STRINGP (object))
4739 break;
4740 }
4741 }
4742 }
4743 else
4744 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4745 bufpos, 0, frame_window_p);
4746 return replacing;
4747 }
4748
4749 /* Value is the position of the end of the `display' property starting
4750 at START_POS in OBJECT. */
4751
4752 static struct text_pos
4753 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4754 {
4755 Lisp_Object end;
4756 struct text_pos end_pos;
4757
4758 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4759 Qdisplay, object, Qnil);
4760 CHARPOS (end_pos) = XFASTINT (end);
4761 if (STRINGP (object))
4762 compute_string_pos (&end_pos, start_pos, it->string);
4763 else
4764 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4765
4766 return end_pos;
4767 }
4768
4769
4770 /* Set up IT from a single `display' property specification SPEC. OBJECT
4771 is the object in which the `display' property was found. *POSITION
4772 is the position in OBJECT at which the `display' property was found.
4773 BUFPOS is the buffer position of OBJECT (different from POSITION if
4774 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4775 previously saw a display specification which already replaced text
4776 display with something else, for example an image; we ignore such
4777 properties after the first one has been processed.
4778
4779 OVERLAY is the overlay this `display' property came from,
4780 or nil if it was a text property.
4781
4782 If SPEC is a `space' or `image' specification, and in some other
4783 cases too, set *POSITION to the position where the `display'
4784 property ends.
4785
4786 If IT is NULL, only examine the property specification in SPEC, but
4787 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4788 is intended to be displayed in a window on a GUI frame.
4789
4790 Value is non-zero if something was found which replaces the display
4791 of buffer or string text. */
4792
4793 static int
4794 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4795 Lisp_Object overlay, struct text_pos *position,
4796 ptrdiff_t bufpos, int display_replaced,
4797 bool frame_window_p)
4798 {
4799 Lisp_Object form;
4800 Lisp_Object location, value;
4801 struct text_pos start_pos = *position;
4802
4803 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4804 If the result is non-nil, use VALUE instead of SPEC. */
4805 form = Qt;
4806 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4807 {
4808 spec = XCDR (spec);
4809 if (!CONSP (spec))
4810 return 0;
4811 form = XCAR (spec);
4812 spec = XCDR (spec);
4813 }
4814
4815 if (!NILP (form) && !EQ (form, Qt))
4816 {
4817 ptrdiff_t count = SPECPDL_INDEX ();
4818
4819 /* Bind `object' to the object having the `display' property, a
4820 buffer or string. Bind `position' to the position in the
4821 object where the property was found, and `buffer-position'
4822 to the current position in the buffer. */
4823
4824 if (NILP (object))
4825 XSETBUFFER (object, current_buffer);
4826 specbind (Qobject, object);
4827 specbind (Qposition, make_number (CHARPOS (*position)));
4828 specbind (Qbuffer_position, make_number (bufpos));
4829 form = safe_eval (form);
4830 unbind_to (count, Qnil);
4831 }
4832
4833 if (NILP (form))
4834 return 0;
4835
4836 /* Handle `(height HEIGHT)' specifications. */
4837 if (CONSP (spec)
4838 && EQ (XCAR (spec), Qheight)
4839 && CONSP (XCDR (spec)))
4840 {
4841 if (it)
4842 {
4843 if (!FRAME_WINDOW_P (it->f))
4844 return 0;
4845
4846 it->font_height = XCAR (XCDR (spec));
4847 if (!NILP (it->font_height))
4848 {
4849 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4850 int new_height = -1;
4851
4852 if (CONSP (it->font_height)
4853 && (EQ (XCAR (it->font_height), Qplus)
4854 || EQ (XCAR (it->font_height), Qminus))
4855 && CONSP (XCDR (it->font_height))
4856 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4857 {
4858 /* `(+ N)' or `(- N)' where N is an integer. */
4859 int steps = XINT (XCAR (XCDR (it->font_height)));
4860 if (EQ (XCAR (it->font_height), Qplus))
4861 steps = - steps;
4862 it->face_id = smaller_face (it->f, it->face_id, steps);
4863 }
4864 else if (FUNCTIONP (it->font_height))
4865 {
4866 /* Call function with current height as argument.
4867 Value is the new height. */
4868 Lisp_Object height;
4869 height = safe_call1 (it->font_height,
4870 face->lface[LFACE_HEIGHT_INDEX]);
4871 if (NUMBERP (height))
4872 new_height = XFLOATINT (height);
4873 }
4874 else if (NUMBERP (it->font_height))
4875 {
4876 /* Value is a multiple of the canonical char height. */
4877 struct face *f;
4878
4879 f = FACE_FROM_ID (it->f,
4880 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4881 new_height = (XFLOATINT (it->font_height)
4882 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4883 }
4884 else
4885 {
4886 /* Evaluate IT->font_height with `height' bound to the
4887 current specified height to get the new height. */
4888 ptrdiff_t count = SPECPDL_INDEX ();
4889
4890 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4891 value = safe_eval (it->font_height);
4892 unbind_to (count, Qnil);
4893
4894 if (NUMBERP (value))
4895 new_height = XFLOATINT (value);
4896 }
4897
4898 if (new_height > 0)
4899 it->face_id = face_with_height (it->f, it->face_id, new_height);
4900 }
4901 }
4902
4903 return 0;
4904 }
4905
4906 /* Handle `(space-width WIDTH)'. */
4907 if (CONSP (spec)
4908 && EQ (XCAR (spec), Qspace_width)
4909 && CONSP (XCDR (spec)))
4910 {
4911 if (it)
4912 {
4913 if (!FRAME_WINDOW_P (it->f))
4914 return 0;
4915
4916 value = XCAR (XCDR (spec));
4917 if (NUMBERP (value) && XFLOATINT (value) > 0)
4918 it->space_width = value;
4919 }
4920
4921 return 0;
4922 }
4923
4924 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4925 if (CONSP (spec)
4926 && EQ (XCAR (spec), Qslice))
4927 {
4928 Lisp_Object tem;
4929
4930 if (it)
4931 {
4932 if (!FRAME_WINDOW_P (it->f))
4933 return 0;
4934
4935 if (tem = XCDR (spec), CONSP (tem))
4936 {
4937 it->slice.x = XCAR (tem);
4938 if (tem = XCDR (tem), CONSP (tem))
4939 {
4940 it->slice.y = XCAR (tem);
4941 if (tem = XCDR (tem), CONSP (tem))
4942 {
4943 it->slice.width = XCAR (tem);
4944 if (tem = XCDR (tem), CONSP (tem))
4945 it->slice.height = XCAR (tem);
4946 }
4947 }
4948 }
4949 }
4950
4951 return 0;
4952 }
4953
4954 /* Handle `(raise FACTOR)'. */
4955 if (CONSP (spec)
4956 && EQ (XCAR (spec), Qraise)
4957 && CONSP (XCDR (spec)))
4958 {
4959 if (it)
4960 {
4961 if (!FRAME_WINDOW_P (it->f))
4962 return 0;
4963
4964 #ifdef HAVE_WINDOW_SYSTEM
4965 value = XCAR (XCDR (spec));
4966 if (NUMBERP (value))
4967 {
4968 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4969 it->voffset = - (XFLOATINT (value)
4970 * (normal_char_height (face->font, -1)));
4971 }
4972 #endif /* HAVE_WINDOW_SYSTEM */
4973 }
4974
4975 return 0;
4976 }
4977
4978 /* Don't handle the other kinds of display specifications
4979 inside a string that we got from a `display' property. */
4980 if (it && it->string_from_display_prop_p)
4981 return 0;
4982
4983 /* Characters having this form of property are not displayed, so
4984 we have to find the end of the property. */
4985 if (it)
4986 {
4987 start_pos = *position;
4988 *position = display_prop_end (it, object, start_pos);
4989 /* If the display property comes from an overlay, don't consider
4990 any potential stop_charpos values before the end of that
4991 overlay. Since display_prop_end will happily find another
4992 'display' property coming from some other overlay or text
4993 property on buffer positions before this overlay's end, we
4994 need to ignore them, or else we risk displaying this
4995 overlay's display string/image twice. */
4996 if (!NILP (overlay))
4997 {
4998 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4999
5000 if (ovendpos > CHARPOS (*position))
5001 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5002 }
5003 }
5004 value = Qnil;
5005
5006 /* Stop the scan at that end position--we assume that all
5007 text properties change there. */
5008 if (it)
5009 it->stop_charpos = position->charpos;
5010
5011 /* Handle `(left-fringe BITMAP [FACE])'
5012 and `(right-fringe BITMAP [FACE])'. */
5013 if (CONSP (spec)
5014 && (EQ (XCAR (spec), Qleft_fringe)
5015 || EQ (XCAR (spec), Qright_fringe))
5016 && CONSP (XCDR (spec)))
5017 {
5018 int fringe_bitmap;
5019
5020 if (it)
5021 {
5022 if (!FRAME_WINDOW_P (it->f))
5023 /* If we return here, POSITION has been advanced
5024 across the text with this property. */
5025 {
5026 /* Synchronize the bidi iterator with POSITION. This is
5027 needed because we are not going to push the iterator
5028 on behalf of this display property, so there will be
5029 no pop_it call to do this synchronization for us. */
5030 if (it->bidi_p)
5031 {
5032 it->position = *position;
5033 iterate_out_of_display_property (it);
5034 *position = it->position;
5035 }
5036 return 1;
5037 }
5038 }
5039 else if (!frame_window_p)
5040 return 1;
5041
5042 #ifdef HAVE_WINDOW_SYSTEM
5043 value = XCAR (XCDR (spec));
5044 if (!SYMBOLP (value)
5045 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
5046 /* If we return here, POSITION has been advanced
5047 across the text with this property. */
5048 {
5049 if (it && it->bidi_p)
5050 {
5051 it->position = *position;
5052 iterate_out_of_display_property (it);
5053 *position = it->position;
5054 }
5055 return 1;
5056 }
5057
5058 if (it)
5059 {
5060 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5061
5062 if (CONSP (XCDR (XCDR (spec))))
5063 {
5064 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5065 int face_id2 = lookup_derived_face (it->f, face_name,
5066 FRINGE_FACE_ID, false);
5067 if (face_id2 >= 0)
5068 face_id = face_id2;
5069 }
5070
5071 /* Save current settings of IT so that we can restore them
5072 when we are finished with the glyph property value. */
5073 push_it (it, position);
5074
5075 it->area = TEXT_AREA;
5076 it->what = IT_IMAGE;
5077 it->image_id = -1; /* no image */
5078 it->position = start_pos;
5079 it->object = NILP (object) ? it->w->contents : object;
5080 it->method = GET_FROM_IMAGE;
5081 it->from_overlay = Qnil;
5082 it->face_id = face_id;
5083 it->from_disp_prop_p = true;
5084
5085 /* Say that we haven't consumed the characters with
5086 `display' property yet. The call to pop_it in
5087 set_iterator_to_next will clean this up. */
5088 *position = start_pos;
5089
5090 if (EQ (XCAR (spec), Qleft_fringe))
5091 {
5092 it->left_user_fringe_bitmap = fringe_bitmap;
5093 it->left_user_fringe_face_id = face_id;
5094 }
5095 else
5096 {
5097 it->right_user_fringe_bitmap = fringe_bitmap;
5098 it->right_user_fringe_face_id = face_id;
5099 }
5100 }
5101 #endif /* HAVE_WINDOW_SYSTEM */
5102 return 1;
5103 }
5104
5105 /* Prepare to handle `((margin left-margin) ...)',
5106 `((margin right-margin) ...)' and `((margin nil) ...)'
5107 prefixes for display specifications. */
5108 location = Qunbound;
5109 if (CONSP (spec) && CONSP (XCAR (spec)))
5110 {
5111 Lisp_Object tem;
5112
5113 value = XCDR (spec);
5114 if (CONSP (value))
5115 value = XCAR (value);
5116
5117 tem = XCAR (spec);
5118 if (EQ (XCAR (tem), Qmargin)
5119 && (tem = XCDR (tem),
5120 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5121 (NILP (tem)
5122 || EQ (tem, Qleft_margin)
5123 || EQ (tem, Qright_margin))))
5124 location = tem;
5125 }
5126
5127 if (EQ (location, Qunbound))
5128 {
5129 location = Qnil;
5130 value = spec;
5131 }
5132
5133 /* After this point, VALUE is the property after any
5134 margin prefix has been stripped. It must be a string,
5135 an image specification, or `(space ...)'.
5136
5137 LOCATION specifies where to display: `left-margin',
5138 `right-margin' or nil. */
5139
5140 bool valid_p = (STRINGP (value)
5141 #ifdef HAVE_WINDOW_SYSTEM
5142 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5143 && valid_image_p (value))
5144 #endif /* not HAVE_WINDOW_SYSTEM */
5145 || (CONSP (value) && EQ (XCAR (value), Qspace))
5146 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5147 && valid_xwidget_spec_p (value)));
5148
5149 if (valid_p && display_replaced == 0)
5150 {
5151 int retval = 1;
5152
5153 if (!it)
5154 {
5155 /* Callers need to know whether the display spec is any kind
5156 of `(space ...)' spec that is about to affect text-area
5157 display. */
5158 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5159 retval = 2;
5160 return retval;
5161 }
5162
5163 /* Save current settings of IT so that we can restore them
5164 when we are finished with the glyph property value. */
5165 push_it (it, position);
5166 it->from_overlay = overlay;
5167 it->from_disp_prop_p = true;
5168
5169 if (NILP (location))
5170 it->area = TEXT_AREA;
5171 else if (EQ (location, Qleft_margin))
5172 it->area = LEFT_MARGIN_AREA;
5173 else
5174 it->area = RIGHT_MARGIN_AREA;
5175
5176 if (STRINGP (value))
5177 {
5178 it->string = value;
5179 it->multibyte_p = STRING_MULTIBYTE (it->string);
5180 it->current.overlay_string_index = -1;
5181 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5182 it->end_charpos = it->string_nchars = SCHARS (it->string);
5183 it->method = GET_FROM_STRING;
5184 it->stop_charpos = 0;
5185 it->prev_stop = 0;
5186 it->base_level_stop = 0;
5187 it->string_from_display_prop_p = true;
5188 /* Say that we haven't consumed the characters with
5189 `display' property yet. The call to pop_it in
5190 set_iterator_to_next will clean this up. */
5191 if (BUFFERP (object))
5192 *position = start_pos;
5193
5194 /* Force paragraph direction to be that of the parent
5195 object. If the parent object's paragraph direction is
5196 not yet determined, default to L2R. */
5197 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5198 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5199 else
5200 it->paragraph_embedding = L2R;
5201
5202 /* Set up the bidi iterator for this display string. */
5203 if (it->bidi_p)
5204 {
5205 it->bidi_it.string.lstring = it->string;
5206 it->bidi_it.string.s = NULL;
5207 it->bidi_it.string.schars = it->end_charpos;
5208 it->bidi_it.string.bufpos = bufpos;
5209 it->bidi_it.string.from_disp_str = true;
5210 it->bidi_it.string.unibyte = !it->multibyte_p;
5211 it->bidi_it.w = it->w;
5212 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5213 }
5214 }
5215 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5216 {
5217 it->method = GET_FROM_STRETCH;
5218 it->object = value;
5219 *position = it->position = start_pos;
5220 retval = 1 + (it->area == TEXT_AREA);
5221 }
5222 else if (valid_xwidget_spec_p (value))
5223 {
5224 it->what = IT_XWIDGET;
5225 it->method = GET_FROM_XWIDGET;
5226 it->position = start_pos;
5227 it->object = NILP (object) ? it->w->contents : object;
5228 *position = start_pos;
5229 it->xwidget = lookup_xwidget (value);
5230 }
5231 #ifdef HAVE_WINDOW_SYSTEM
5232 else
5233 {
5234 it->what = IT_IMAGE;
5235 it->image_id = lookup_image (it->f, value);
5236 it->position = start_pos;
5237 it->object = NILP (object) ? it->w->contents : object;
5238 it->method = GET_FROM_IMAGE;
5239
5240 /* Say that we haven't consumed the characters with
5241 `display' property yet. The call to pop_it in
5242 set_iterator_to_next will clean this up. */
5243 *position = start_pos;
5244 }
5245 #endif /* HAVE_WINDOW_SYSTEM */
5246
5247 return retval;
5248 }
5249
5250 /* Invalid property or property not supported. Restore
5251 POSITION to what it was before. */
5252 *position = start_pos;
5253 return 0;
5254 }
5255
5256 /* Check if PROP is a display property value whose text should be
5257 treated as intangible. OVERLAY is the overlay from which PROP
5258 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5259 specify the buffer position covered by PROP. */
5260
5261 bool
5262 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5263 ptrdiff_t charpos, ptrdiff_t bytepos)
5264 {
5265 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5266 struct text_pos position;
5267
5268 SET_TEXT_POS (position, charpos, bytepos);
5269 return (handle_display_spec (NULL, prop, Qnil, overlay,
5270 &position, charpos, frame_window_p)
5271 != 0);
5272 }
5273
5274
5275 /* Return true if PROP is a display sub-property value containing STRING.
5276
5277 Implementation note: this and the following function are really
5278 special cases of handle_display_spec and
5279 handle_single_display_spec, and should ideally use the same code.
5280 Until they do, these two pairs must be consistent and must be
5281 modified in sync. */
5282
5283 static bool
5284 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5285 {
5286 if (EQ (string, prop))
5287 return true;
5288
5289 /* Skip over `when FORM'. */
5290 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5291 {
5292 prop = XCDR (prop);
5293 if (!CONSP (prop))
5294 return false;
5295 /* Actually, the condition following `when' should be eval'ed,
5296 like handle_single_display_spec does, and we should return
5297 false if it evaluates to nil. However, this function is
5298 called only when the buffer was already displayed and some
5299 glyph in the glyph matrix was found to come from a display
5300 string. Therefore, the condition was already evaluated, and
5301 the result was non-nil, otherwise the display string wouldn't
5302 have been displayed and we would have never been called for
5303 this property. Thus, we can skip the evaluation and assume
5304 its result is non-nil. */
5305 prop = XCDR (prop);
5306 }
5307
5308 if (CONSP (prop))
5309 /* Skip over `margin LOCATION'. */
5310 if (EQ (XCAR (prop), Qmargin))
5311 {
5312 prop = XCDR (prop);
5313 if (!CONSP (prop))
5314 return false;
5315
5316 prop = XCDR (prop);
5317 if (!CONSP (prop))
5318 return false;
5319 }
5320
5321 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5322 }
5323
5324
5325 /* Return true if STRING appears in the `display' property PROP. */
5326
5327 static bool
5328 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5329 {
5330 if (CONSP (prop)
5331 && !EQ (XCAR (prop), Qwhen)
5332 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5333 {
5334 /* A list of sub-properties. */
5335 while (CONSP (prop))
5336 {
5337 if (single_display_spec_string_p (XCAR (prop), string))
5338 return true;
5339 prop = XCDR (prop);
5340 }
5341 }
5342 else if (VECTORP (prop))
5343 {
5344 /* A vector of sub-properties. */
5345 ptrdiff_t i;
5346 for (i = 0; i < ASIZE (prop); ++i)
5347 if (single_display_spec_string_p (AREF (prop, i), string))
5348 return true;
5349 }
5350 else
5351 return single_display_spec_string_p (prop, string);
5352
5353 return false;
5354 }
5355
5356 /* Look for STRING in overlays and text properties in the current
5357 buffer, between character positions FROM and TO (excluding TO).
5358 BACK_P means look back (in this case, TO is supposed to be
5359 less than FROM).
5360 Value is the first character position where STRING was found, or
5361 zero if it wasn't found before hitting TO.
5362
5363 This function may only use code that doesn't eval because it is
5364 called asynchronously from note_mouse_highlight. */
5365
5366 static ptrdiff_t
5367 string_buffer_position_lim (Lisp_Object string,
5368 ptrdiff_t from, ptrdiff_t to, bool back_p)
5369 {
5370 Lisp_Object limit, prop, pos;
5371 bool found = false;
5372
5373 pos = make_number (max (from, BEGV));
5374
5375 if (!back_p) /* looking forward */
5376 {
5377 limit = make_number (min (to, ZV));
5378 while (!found && !EQ (pos, limit))
5379 {
5380 prop = Fget_char_property (pos, Qdisplay, Qnil);
5381 if (!NILP (prop) && display_prop_string_p (prop, string))
5382 found = true;
5383 else
5384 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5385 limit);
5386 }
5387 }
5388 else /* looking back */
5389 {
5390 limit = make_number (max (to, BEGV));
5391 while (!found && !EQ (pos, limit))
5392 {
5393 prop = Fget_char_property (pos, Qdisplay, Qnil);
5394 if (!NILP (prop) && display_prop_string_p (prop, string))
5395 found = true;
5396 else
5397 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5398 limit);
5399 }
5400 }
5401
5402 return found ? XINT (pos) : 0;
5403 }
5404
5405 /* Determine which buffer position in current buffer STRING comes from.
5406 AROUND_CHARPOS is an approximate position where it could come from.
5407 Value is the buffer position or 0 if it couldn't be determined.
5408
5409 This function is necessary because we don't record buffer positions
5410 in glyphs generated from strings (to keep struct glyph small).
5411 This function may only use code that doesn't eval because it is
5412 called asynchronously from note_mouse_highlight. */
5413
5414 static ptrdiff_t
5415 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5416 {
5417 const int MAX_DISTANCE = 1000;
5418 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5419 around_charpos + MAX_DISTANCE,
5420 false);
5421
5422 if (!found)
5423 found = string_buffer_position_lim (string, around_charpos,
5424 around_charpos - MAX_DISTANCE, true);
5425 return found;
5426 }
5427
5428
5429 \f
5430 /***********************************************************************
5431 `composition' property
5432 ***********************************************************************/
5433
5434 /* Set up iterator IT from `composition' property at its current
5435 position. Called from handle_stop. */
5436
5437 static enum prop_handled
5438 handle_composition_prop (struct it *it)
5439 {
5440 Lisp_Object prop, string;
5441 ptrdiff_t pos, pos_byte, start, end;
5442
5443 if (STRINGP (it->string))
5444 {
5445 unsigned char *s;
5446
5447 pos = IT_STRING_CHARPOS (*it);
5448 pos_byte = IT_STRING_BYTEPOS (*it);
5449 string = it->string;
5450 s = SDATA (string) + pos_byte;
5451 it->c = STRING_CHAR (s);
5452 }
5453 else
5454 {
5455 pos = IT_CHARPOS (*it);
5456 pos_byte = IT_BYTEPOS (*it);
5457 string = Qnil;
5458 it->c = FETCH_CHAR (pos_byte);
5459 }
5460
5461 /* If there's a valid composition and point is not inside of the
5462 composition (in the case that the composition is from the current
5463 buffer), draw a glyph composed from the composition components. */
5464 if (find_composition (pos, -1, &start, &end, &prop, string)
5465 && composition_valid_p (start, end, prop)
5466 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5467 {
5468 if (start < pos)
5469 /* As we can't handle this situation (perhaps font-lock added
5470 a new composition), we just return here hoping that next
5471 redisplay will detect this composition much earlier. */
5472 return HANDLED_NORMALLY;
5473 if (start != pos)
5474 {
5475 if (STRINGP (it->string))
5476 pos_byte = string_char_to_byte (it->string, start);
5477 else
5478 pos_byte = CHAR_TO_BYTE (start);
5479 }
5480 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5481 prop, string);
5482
5483 if (it->cmp_it.id >= 0)
5484 {
5485 it->cmp_it.ch = -1;
5486 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5487 it->cmp_it.nglyphs = -1;
5488 }
5489 }
5490
5491 return HANDLED_NORMALLY;
5492 }
5493
5494
5495 \f
5496 /***********************************************************************
5497 Overlay strings
5498 ***********************************************************************/
5499
5500 /* The following structure is used to record overlay strings for
5501 later sorting in load_overlay_strings. */
5502
5503 struct overlay_entry
5504 {
5505 Lisp_Object overlay;
5506 Lisp_Object string;
5507 EMACS_INT priority;
5508 bool after_string_p;
5509 };
5510
5511
5512 /* Set up iterator IT from overlay strings at its current position.
5513 Called from handle_stop. */
5514
5515 static enum prop_handled
5516 handle_overlay_change (struct it *it)
5517 {
5518 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5519 return HANDLED_RECOMPUTE_PROPS;
5520 else
5521 return HANDLED_NORMALLY;
5522 }
5523
5524
5525 /* Set up the next overlay string for delivery by IT, if there is an
5526 overlay string to deliver. Called by set_iterator_to_next when the
5527 end of the current overlay string is reached. If there are more
5528 overlay strings to display, IT->string and
5529 IT->current.overlay_string_index are set appropriately here.
5530 Otherwise IT->string is set to nil. */
5531
5532 static void
5533 next_overlay_string (struct it *it)
5534 {
5535 ++it->current.overlay_string_index;
5536 if (it->current.overlay_string_index == it->n_overlay_strings)
5537 {
5538 /* No more overlay strings. Restore IT's settings to what
5539 they were before overlay strings were processed, and
5540 continue to deliver from current_buffer. */
5541
5542 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5543 pop_it (it);
5544 eassert (it->sp > 0
5545 || (NILP (it->string)
5546 && it->method == GET_FROM_BUFFER
5547 && it->stop_charpos >= BEGV
5548 && it->stop_charpos <= it->end_charpos));
5549 it->current.overlay_string_index = -1;
5550 it->n_overlay_strings = 0;
5551 /* If there's an empty display string on the stack, pop the
5552 stack, to resync the bidi iterator with IT's position. Such
5553 empty strings are pushed onto the stack in
5554 get_overlay_strings_1. */
5555 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5556 pop_it (it);
5557
5558 /* Since we've exhausted overlay strings at this buffer
5559 position, set the flag to ignore overlays until we move to
5560 another position. The flag is reset in
5561 next_element_from_buffer. */
5562 it->ignore_overlay_strings_at_pos_p = true;
5563
5564 /* If we're at the end of the buffer, record that we have
5565 processed the overlay strings there already, so that
5566 next_element_from_buffer doesn't try it again. */
5567 if (NILP (it->string)
5568 && IT_CHARPOS (*it) >= it->end_charpos
5569 && it->overlay_strings_charpos >= it->end_charpos)
5570 it->overlay_strings_at_end_processed_p = true;
5571 /* Note: we reset overlay_strings_charpos only here, to make
5572 sure the just-processed overlays were indeed at EOB.
5573 Otherwise, overlays on text with invisible text property,
5574 which are processed with IT's position past the invisible
5575 text, might fool us into thinking the overlays at EOB were
5576 already processed (linum-mode can cause this, for
5577 example). */
5578 it->overlay_strings_charpos = -1;
5579 }
5580 else
5581 {
5582 /* There are more overlay strings to process. If
5583 IT->current.overlay_string_index has advanced to a position
5584 where we must load IT->overlay_strings with more strings, do
5585 it. We must load at the IT->overlay_strings_charpos where
5586 IT->n_overlay_strings was originally computed; when invisible
5587 text is present, this might not be IT_CHARPOS (Bug#7016). */
5588 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5589
5590 if (it->current.overlay_string_index && i == 0)
5591 load_overlay_strings (it, it->overlay_strings_charpos);
5592
5593 /* Initialize IT to deliver display elements from the overlay
5594 string. */
5595 it->string = it->overlay_strings[i];
5596 it->multibyte_p = STRING_MULTIBYTE (it->string);
5597 SET_TEXT_POS (it->current.string_pos, 0, 0);
5598 it->method = GET_FROM_STRING;
5599 it->stop_charpos = 0;
5600 it->end_charpos = SCHARS (it->string);
5601 if (it->cmp_it.stop_pos >= 0)
5602 it->cmp_it.stop_pos = 0;
5603 it->prev_stop = 0;
5604 it->base_level_stop = 0;
5605
5606 /* Set up the bidi iterator for this overlay string. */
5607 if (it->bidi_p)
5608 {
5609 it->bidi_it.string.lstring = it->string;
5610 it->bidi_it.string.s = NULL;
5611 it->bidi_it.string.schars = SCHARS (it->string);
5612 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5613 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5614 it->bidi_it.string.unibyte = !it->multibyte_p;
5615 it->bidi_it.w = it->w;
5616 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5617 }
5618 }
5619
5620 CHECK_IT (it);
5621 }
5622
5623
5624 /* Compare two overlay_entry structures E1 and E2. Used as a
5625 comparison function for qsort in load_overlay_strings. Overlay
5626 strings for the same position are sorted so that
5627
5628 1. All after-strings come in front of before-strings, except
5629 when they come from the same overlay.
5630
5631 2. Within after-strings, strings are sorted so that overlay strings
5632 from overlays with higher priorities come first.
5633
5634 2. Within before-strings, strings are sorted so that overlay
5635 strings from overlays with higher priorities come last.
5636
5637 Value is analogous to strcmp. */
5638
5639
5640 static int
5641 compare_overlay_entries (const void *e1, const void *e2)
5642 {
5643 struct overlay_entry const *entry1 = e1;
5644 struct overlay_entry const *entry2 = e2;
5645 int result;
5646
5647 if (entry1->after_string_p != entry2->after_string_p)
5648 {
5649 /* Let after-strings appear in front of before-strings if
5650 they come from different overlays. */
5651 if (EQ (entry1->overlay, entry2->overlay))
5652 result = entry1->after_string_p ? 1 : -1;
5653 else
5654 result = entry1->after_string_p ? -1 : 1;
5655 }
5656 else if (entry1->priority != entry2->priority)
5657 {
5658 if (entry1->after_string_p)
5659 /* After-strings sorted in order of decreasing priority. */
5660 result = entry2->priority < entry1->priority ? -1 : 1;
5661 else
5662 /* Before-strings sorted in order of increasing priority. */
5663 result = entry1->priority < entry2->priority ? -1 : 1;
5664 }
5665 else
5666 result = 0;
5667
5668 return result;
5669 }
5670
5671
5672 /* Load the vector IT->overlay_strings with overlay strings from IT's
5673 current buffer position, or from CHARPOS if that is > 0. Set
5674 IT->n_overlays to the total number of overlay strings found.
5675
5676 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5677 a time. On entry into load_overlay_strings,
5678 IT->current.overlay_string_index gives the number of overlay
5679 strings that have already been loaded by previous calls to this
5680 function.
5681
5682 IT->add_overlay_start contains an additional overlay start
5683 position to consider for taking overlay strings from, if non-zero.
5684 This position comes into play when the overlay has an `invisible'
5685 property, and both before and after-strings. When we've skipped to
5686 the end of the overlay, because of its `invisible' property, we
5687 nevertheless want its before-string to appear.
5688 IT->add_overlay_start will contain the overlay start position
5689 in this case.
5690
5691 Overlay strings are sorted so that after-string strings come in
5692 front of before-string strings. Within before and after-strings,
5693 strings are sorted by overlay priority. See also function
5694 compare_overlay_entries. */
5695
5696 static void
5697 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5698 {
5699 Lisp_Object overlay, window, str, invisible;
5700 struct Lisp_Overlay *ov;
5701 ptrdiff_t start, end;
5702 ptrdiff_t n = 0, i, j;
5703 int invis;
5704 struct overlay_entry entriesbuf[20];
5705 ptrdiff_t size = ARRAYELTS (entriesbuf);
5706 struct overlay_entry *entries = entriesbuf;
5707 USE_SAFE_ALLOCA;
5708
5709 if (charpos <= 0)
5710 charpos = IT_CHARPOS (*it);
5711
5712 /* Append the overlay string STRING of overlay OVERLAY to vector
5713 `entries' which has size `size' and currently contains `n'
5714 elements. AFTER_P means STRING is an after-string of
5715 OVERLAY. */
5716 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5717 do \
5718 { \
5719 Lisp_Object priority; \
5720 \
5721 if (n == size) \
5722 { \
5723 struct overlay_entry *old = entries; \
5724 SAFE_NALLOCA (entries, 2, size); \
5725 memcpy (entries, old, size * sizeof *entries); \
5726 size *= 2; \
5727 } \
5728 \
5729 entries[n].string = (STRING); \
5730 entries[n].overlay = (OVERLAY); \
5731 priority = Foverlay_get ((OVERLAY), Qpriority); \
5732 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5733 entries[n].after_string_p = (AFTER_P); \
5734 ++n; \
5735 } \
5736 while (false)
5737
5738 /* Process overlay before the overlay center. */
5739 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5740 {
5741 XSETMISC (overlay, ov);
5742 eassert (OVERLAYP (overlay));
5743 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5744 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5745
5746 if (end < charpos)
5747 break;
5748
5749 /* Skip this overlay if it doesn't start or end at IT's current
5750 position. */
5751 if (end != charpos && start != charpos)
5752 continue;
5753
5754 /* Skip this overlay if it doesn't apply to IT->w. */
5755 window = Foverlay_get (overlay, Qwindow);
5756 if (WINDOWP (window) && XWINDOW (window) != it->w)
5757 continue;
5758
5759 /* If the text ``under'' the overlay is invisible, both before-
5760 and after-strings from this overlay are visible; start and
5761 end position are indistinguishable. */
5762 invisible = Foverlay_get (overlay, Qinvisible);
5763 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5764
5765 /* If overlay has a non-empty before-string, record it. */
5766 if ((start == charpos || (end == charpos && invis != 0))
5767 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5768 && SCHARS (str))
5769 RECORD_OVERLAY_STRING (overlay, str, false);
5770
5771 /* If overlay has a non-empty after-string, record it. */
5772 if ((end == charpos || (start == charpos && invis != 0))
5773 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5774 && SCHARS (str))
5775 RECORD_OVERLAY_STRING (overlay, str, true);
5776 }
5777
5778 /* Process overlays after the overlay center. */
5779 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5780 {
5781 XSETMISC (overlay, ov);
5782 eassert (OVERLAYP (overlay));
5783 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5784 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5785
5786 if (start > charpos)
5787 break;
5788
5789 /* Skip this overlay if it doesn't start or end at IT's current
5790 position. */
5791 if (end != charpos && start != charpos)
5792 continue;
5793
5794 /* Skip this overlay if it doesn't apply to IT->w. */
5795 window = Foverlay_get (overlay, Qwindow);
5796 if (WINDOWP (window) && XWINDOW (window) != it->w)
5797 continue;
5798
5799 /* If the text ``under'' the overlay is invisible, it has a zero
5800 dimension, and both before- and after-strings apply. */
5801 invisible = Foverlay_get (overlay, Qinvisible);
5802 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5803
5804 /* If overlay has a non-empty before-string, record it. */
5805 if ((start == charpos || (end == charpos && invis != 0))
5806 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5807 && SCHARS (str))
5808 RECORD_OVERLAY_STRING (overlay, str, false);
5809
5810 /* If overlay has a non-empty after-string, record it. */
5811 if ((end == charpos || (start == charpos && invis != 0))
5812 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5813 && SCHARS (str))
5814 RECORD_OVERLAY_STRING (overlay, str, true);
5815 }
5816
5817 #undef RECORD_OVERLAY_STRING
5818
5819 /* Sort entries. */
5820 if (n > 1)
5821 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5822
5823 /* Record number of overlay strings, and where we computed it. */
5824 it->n_overlay_strings = n;
5825 it->overlay_strings_charpos = charpos;
5826
5827 /* IT->current.overlay_string_index is the number of overlay strings
5828 that have already been consumed by IT. Copy some of the
5829 remaining overlay strings to IT->overlay_strings. */
5830 i = 0;
5831 j = it->current.overlay_string_index;
5832 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5833 {
5834 it->overlay_strings[i] = entries[j].string;
5835 it->string_overlays[i++] = entries[j++].overlay;
5836 }
5837
5838 CHECK_IT (it);
5839 SAFE_FREE ();
5840 }
5841
5842
5843 /* Get the first chunk of overlay strings at IT's current buffer
5844 position, or at CHARPOS if that is > 0. Value is true if at
5845 least one overlay string was found. */
5846
5847 static bool
5848 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5849 {
5850 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5851 process. This fills IT->overlay_strings with strings, and sets
5852 IT->n_overlay_strings to the total number of strings to process.
5853 IT->pos.overlay_string_index has to be set temporarily to zero
5854 because load_overlay_strings needs this; it must be set to -1
5855 when no overlay strings are found because a zero value would
5856 indicate a position in the first overlay string. */
5857 it->current.overlay_string_index = 0;
5858 load_overlay_strings (it, charpos);
5859
5860 /* If we found overlay strings, set up IT to deliver display
5861 elements from the first one. Otherwise set up IT to deliver
5862 from current_buffer. */
5863 if (it->n_overlay_strings)
5864 {
5865 /* Make sure we know settings in current_buffer, so that we can
5866 restore meaningful values when we're done with the overlay
5867 strings. */
5868 if (compute_stop_p)
5869 compute_stop_pos (it);
5870 eassert (it->face_id >= 0);
5871
5872 /* Save IT's settings. They are restored after all overlay
5873 strings have been processed. */
5874 eassert (!compute_stop_p || it->sp == 0);
5875
5876 /* When called from handle_stop, there might be an empty display
5877 string loaded. In that case, don't bother saving it. But
5878 don't use this optimization with the bidi iterator, since we
5879 need the corresponding pop_it call to resync the bidi
5880 iterator's position with IT's position, after we are done
5881 with the overlay strings. (The corresponding call to pop_it
5882 in case of an empty display string is in
5883 next_overlay_string.) */
5884 if (!(!it->bidi_p
5885 && STRINGP (it->string) && !SCHARS (it->string)))
5886 push_it (it, NULL);
5887
5888 /* Set up IT to deliver display elements from the first overlay
5889 string. */
5890 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5891 it->string = it->overlay_strings[0];
5892 it->from_overlay = Qnil;
5893 it->stop_charpos = 0;
5894 eassert (STRINGP (it->string));
5895 it->end_charpos = SCHARS (it->string);
5896 it->prev_stop = 0;
5897 it->base_level_stop = 0;
5898 it->multibyte_p = STRING_MULTIBYTE (it->string);
5899 it->method = GET_FROM_STRING;
5900 it->from_disp_prop_p = 0;
5901
5902 /* Force paragraph direction to be that of the parent
5903 buffer. */
5904 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5905 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5906 else
5907 it->paragraph_embedding = L2R;
5908
5909 /* Set up the bidi iterator for this overlay string. */
5910 if (it->bidi_p)
5911 {
5912 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5913
5914 it->bidi_it.string.lstring = it->string;
5915 it->bidi_it.string.s = NULL;
5916 it->bidi_it.string.schars = SCHARS (it->string);
5917 it->bidi_it.string.bufpos = pos;
5918 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5919 it->bidi_it.string.unibyte = !it->multibyte_p;
5920 it->bidi_it.w = it->w;
5921 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5922 }
5923 return true;
5924 }
5925
5926 it->current.overlay_string_index = -1;
5927 return false;
5928 }
5929
5930 static bool
5931 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5932 {
5933 it->string = Qnil;
5934 it->method = GET_FROM_BUFFER;
5935
5936 get_overlay_strings_1 (it, charpos, true);
5937
5938 CHECK_IT (it);
5939
5940 /* Value is true if we found at least one overlay string. */
5941 return STRINGP (it->string);
5942 }
5943
5944
5945 \f
5946 /***********************************************************************
5947 Saving and restoring state
5948 ***********************************************************************/
5949
5950 /* Save current settings of IT on IT->stack. Called, for example,
5951 before setting up IT for an overlay string, to be able to restore
5952 IT's settings to what they were after the overlay string has been
5953 processed. If POSITION is non-NULL, it is the position to save on
5954 the stack instead of IT->position. */
5955
5956 static void
5957 push_it (struct it *it, struct text_pos *position)
5958 {
5959 struct iterator_stack_entry *p;
5960
5961 eassert (it->sp < IT_STACK_SIZE);
5962 p = it->stack + it->sp;
5963
5964 p->stop_charpos = it->stop_charpos;
5965 p->prev_stop = it->prev_stop;
5966 p->base_level_stop = it->base_level_stop;
5967 p->cmp_it = it->cmp_it;
5968 eassert (it->face_id >= 0);
5969 p->face_id = it->face_id;
5970 p->string = it->string;
5971 p->method = it->method;
5972 p->from_overlay = it->from_overlay;
5973 switch (p->method)
5974 {
5975 case GET_FROM_IMAGE:
5976 p->u.image.object = it->object;
5977 p->u.image.image_id = it->image_id;
5978 p->u.image.slice = it->slice;
5979 break;
5980 case GET_FROM_STRETCH:
5981 p->u.stretch.object = it->object;
5982 break;
5983 case GET_FROM_XWIDGET:
5984 p->u.xwidget.object = it->object;
5985 break;
5986 case GET_FROM_BUFFER:
5987 case GET_FROM_DISPLAY_VECTOR:
5988 case GET_FROM_STRING:
5989 case GET_FROM_C_STRING:
5990 break;
5991 default:
5992 emacs_abort ();
5993 }
5994 p->position = position ? *position : it->position;
5995 p->current = it->current;
5996 p->end_charpos = it->end_charpos;
5997 p->string_nchars = it->string_nchars;
5998 p->area = it->area;
5999 p->multibyte_p = it->multibyte_p;
6000 p->avoid_cursor_p = it->avoid_cursor_p;
6001 p->space_width = it->space_width;
6002 p->font_height = it->font_height;
6003 p->voffset = it->voffset;
6004 p->string_from_display_prop_p = it->string_from_display_prop_p;
6005 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6006 p->display_ellipsis_p = false;
6007 p->line_wrap = it->line_wrap;
6008 p->bidi_p = it->bidi_p;
6009 p->paragraph_embedding = it->paragraph_embedding;
6010 p->from_disp_prop_p = it->from_disp_prop_p;
6011 ++it->sp;
6012
6013 /* Save the state of the bidi iterator as well. */
6014 if (it->bidi_p)
6015 bidi_push_it (&it->bidi_it);
6016 }
6017
6018 static void
6019 iterate_out_of_display_property (struct it *it)
6020 {
6021 bool buffer_p = !STRINGP (it->string);
6022 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6023 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6024
6025 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6026
6027 /* Maybe initialize paragraph direction. If we are at the beginning
6028 of a new paragraph, next_element_from_buffer may not have a
6029 chance to do that. */
6030 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6031 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6032 /* prev_stop can be zero, so check against BEGV as well. */
6033 while (it->bidi_it.charpos >= bob
6034 && it->prev_stop <= it->bidi_it.charpos
6035 && it->bidi_it.charpos < CHARPOS (it->position)
6036 && it->bidi_it.charpos < eob)
6037 bidi_move_to_visually_next (&it->bidi_it);
6038 /* Record the stop_pos we just crossed, for when we cross it
6039 back, maybe. */
6040 if (it->bidi_it.charpos > CHARPOS (it->position))
6041 it->prev_stop = CHARPOS (it->position);
6042 /* If we ended up not where pop_it put us, resync IT's
6043 positional members with the bidi iterator. */
6044 if (it->bidi_it.charpos != CHARPOS (it->position))
6045 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6046 if (buffer_p)
6047 it->current.pos = it->position;
6048 else
6049 it->current.string_pos = it->position;
6050 }
6051
6052 /* Restore IT's settings from IT->stack. Called, for example, when no
6053 more overlay strings must be processed, and we return to delivering
6054 display elements from a buffer, or when the end of a string from a
6055 `display' property is reached and we return to delivering display
6056 elements from an overlay string, or from a buffer. */
6057
6058 static void
6059 pop_it (struct it *it)
6060 {
6061 struct iterator_stack_entry *p;
6062 bool from_display_prop = it->from_disp_prop_p;
6063 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6064
6065 eassert (it->sp > 0);
6066 --it->sp;
6067 p = it->stack + it->sp;
6068 it->stop_charpos = p->stop_charpos;
6069 it->prev_stop = p->prev_stop;
6070 it->base_level_stop = p->base_level_stop;
6071 it->cmp_it = p->cmp_it;
6072 it->face_id = p->face_id;
6073 it->current = p->current;
6074 it->position = p->position;
6075 it->string = p->string;
6076 it->from_overlay = p->from_overlay;
6077 if (NILP (it->string))
6078 SET_TEXT_POS (it->current.string_pos, -1, -1);
6079 it->method = p->method;
6080 switch (it->method)
6081 {
6082 case GET_FROM_IMAGE:
6083 it->image_id = p->u.image.image_id;
6084 it->object = p->u.image.object;
6085 it->slice = p->u.image.slice;
6086 break;
6087 case GET_FROM_XWIDGET:
6088 it->object = p->u.xwidget.object;
6089 break;
6090 case GET_FROM_STRETCH:
6091 it->object = p->u.stretch.object;
6092 break;
6093 case GET_FROM_BUFFER:
6094 it->object = it->w->contents;
6095 break;
6096 case GET_FROM_STRING:
6097 {
6098 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6099
6100 /* Restore the face_box_p flag, since it could have been
6101 overwritten by the face of the object that we just finished
6102 displaying. */
6103 if (face)
6104 it->face_box_p = face->box != FACE_NO_BOX;
6105 it->object = it->string;
6106 }
6107 break;
6108 case GET_FROM_DISPLAY_VECTOR:
6109 if (it->s)
6110 it->method = GET_FROM_C_STRING;
6111 else if (STRINGP (it->string))
6112 it->method = GET_FROM_STRING;
6113 else
6114 {
6115 it->method = GET_FROM_BUFFER;
6116 it->object = it->w->contents;
6117 }
6118 break;
6119 case GET_FROM_C_STRING:
6120 break;
6121 default:
6122 emacs_abort ();
6123 }
6124 it->end_charpos = p->end_charpos;
6125 it->string_nchars = p->string_nchars;
6126 it->area = p->area;
6127 it->multibyte_p = p->multibyte_p;
6128 it->avoid_cursor_p = p->avoid_cursor_p;
6129 it->space_width = p->space_width;
6130 it->font_height = p->font_height;
6131 it->voffset = p->voffset;
6132 it->string_from_display_prop_p = p->string_from_display_prop_p;
6133 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6134 it->line_wrap = p->line_wrap;
6135 it->bidi_p = p->bidi_p;
6136 it->paragraph_embedding = p->paragraph_embedding;
6137 it->from_disp_prop_p = p->from_disp_prop_p;
6138 if (it->bidi_p)
6139 {
6140 bidi_pop_it (&it->bidi_it);
6141 /* Bidi-iterate until we get out of the portion of text, if any,
6142 covered by a `display' text property or by an overlay with
6143 `display' property. (We cannot just jump there, because the
6144 internal coherency of the bidi iterator state can not be
6145 preserved across such jumps.) We also must determine the
6146 paragraph base direction if the overlay we just processed is
6147 at the beginning of a new paragraph. */
6148 if (from_display_prop
6149 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6150 iterate_out_of_display_property (it);
6151
6152 eassert ((BUFFERP (it->object)
6153 && IT_CHARPOS (*it) == it->bidi_it.charpos
6154 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6155 || (STRINGP (it->object)
6156 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6157 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6158 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6159 }
6160 /* If we move the iterator over text covered by a display property
6161 to a new buffer position, any info about previously seen overlays
6162 is no longer valid. */
6163 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6164 it->ignore_overlay_strings_at_pos_p = false;
6165 }
6166
6167
6168 \f
6169 /***********************************************************************
6170 Moving over lines
6171 ***********************************************************************/
6172
6173 /* Set IT's current position to the previous line start. */
6174
6175 static void
6176 back_to_previous_line_start (struct it *it)
6177 {
6178 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6179
6180 DEC_BOTH (cp, bp);
6181 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6182 }
6183
6184
6185 /* Move IT to the next line start.
6186
6187 Value is true if a newline was found. Set *SKIPPED_P to true if
6188 we skipped over part of the text (as opposed to moving the iterator
6189 continuously over the text). Otherwise, don't change the value
6190 of *SKIPPED_P.
6191
6192 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6193 iterator on the newline, if it was found.
6194
6195 Newlines may come from buffer text, overlay strings, or strings
6196 displayed via the `display' property. That's the reason we can't
6197 simply use find_newline_no_quit.
6198
6199 Note that this function may not skip over invisible text that is so
6200 because of text properties and immediately follows a newline. If
6201 it would, function reseat_at_next_visible_line_start, when called
6202 from set_iterator_to_next, would effectively make invisible
6203 characters following a newline part of the wrong glyph row, which
6204 leads to wrong cursor motion. */
6205
6206 static bool
6207 forward_to_next_line_start (struct it *it, bool *skipped_p,
6208 struct bidi_it *bidi_it_prev)
6209 {
6210 ptrdiff_t old_selective;
6211 bool newline_found_p = false;
6212 int n;
6213 const int MAX_NEWLINE_DISTANCE = 500;
6214
6215 /* If already on a newline, just consume it to avoid unintended
6216 skipping over invisible text below. */
6217 if (it->what == IT_CHARACTER
6218 && it->c == '\n'
6219 && CHARPOS (it->position) == IT_CHARPOS (*it))
6220 {
6221 if (it->bidi_p && bidi_it_prev)
6222 *bidi_it_prev = it->bidi_it;
6223 set_iterator_to_next (it, false);
6224 it->c = 0;
6225 return true;
6226 }
6227
6228 /* Don't handle selective display in the following. It's (a)
6229 unnecessary because it's done by the caller, and (b) leads to an
6230 infinite recursion because next_element_from_ellipsis indirectly
6231 calls this function. */
6232 old_selective = it->selective;
6233 it->selective = 0;
6234
6235 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6236 from buffer text. */
6237 for (n = 0;
6238 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6239 n += !STRINGP (it->string))
6240 {
6241 if (!get_next_display_element (it))
6242 return false;
6243 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6244 if (newline_found_p && it->bidi_p && bidi_it_prev)
6245 *bidi_it_prev = it->bidi_it;
6246 set_iterator_to_next (it, false);
6247 }
6248
6249 /* If we didn't find a newline near enough, see if we can use a
6250 short-cut. */
6251 if (!newline_found_p)
6252 {
6253 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6254 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6255 1, &bytepos);
6256 Lisp_Object pos;
6257
6258 eassert (!STRINGP (it->string));
6259
6260 /* If there isn't any `display' property in sight, and no
6261 overlays, we can just use the position of the newline in
6262 buffer text. */
6263 if (it->stop_charpos >= limit
6264 || ((pos = Fnext_single_property_change (make_number (start),
6265 Qdisplay, Qnil,
6266 make_number (limit)),
6267 NILP (pos))
6268 && next_overlay_change (start) == ZV))
6269 {
6270 if (!it->bidi_p)
6271 {
6272 IT_CHARPOS (*it) = limit;
6273 IT_BYTEPOS (*it) = bytepos;
6274 }
6275 else
6276 {
6277 struct bidi_it bprev;
6278
6279 /* Help bidi.c avoid expensive searches for display
6280 properties and overlays, by telling it that there are
6281 none up to `limit'. */
6282 if (it->bidi_it.disp_pos < limit)
6283 {
6284 it->bidi_it.disp_pos = limit;
6285 it->bidi_it.disp_prop = 0;
6286 }
6287 do {
6288 bprev = it->bidi_it;
6289 bidi_move_to_visually_next (&it->bidi_it);
6290 } while (it->bidi_it.charpos != limit);
6291 IT_CHARPOS (*it) = limit;
6292 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6293 if (bidi_it_prev)
6294 *bidi_it_prev = bprev;
6295 }
6296 *skipped_p = newline_found_p = true;
6297 }
6298 else
6299 {
6300 while (get_next_display_element (it)
6301 && !newline_found_p)
6302 {
6303 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6304 if (newline_found_p && it->bidi_p && bidi_it_prev)
6305 *bidi_it_prev = it->bidi_it;
6306 set_iterator_to_next (it, false);
6307 }
6308 }
6309 }
6310
6311 it->selective = old_selective;
6312 return newline_found_p;
6313 }
6314
6315
6316 /* Set IT's current position to the previous visible line start. Skip
6317 invisible text that is so either due to text properties or due to
6318 selective display. Caution: this does not change IT->current_x and
6319 IT->hpos. */
6320
6321 static void
6322 back_to_previous_visible_line_start (struct it *it)
6323 {
6324 while (IT_CHARPOS (*it) > BEGV)
6325 {
6326 back_to_previous_line_start (it);
6327
6328 if (IT_CHARPOS (*it) <= BEGV)
6329 break;
6330
6331 /* If selective > 0, then lines indented more than its value are
6332 invisible. */
6333 if (it->selective > 0
6334 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6335 it->selective))
6336 continue;
6337
6338 /* Check the newline before point for invisibility. */
6339 {
6340 Lisp_Object prop;
6341 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6342 Qinvisible, it->window);
6343 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6344 continue;
6345 }
6346
6347 if (IT_CHARPOS (*it) <= BEGV)
6348 break;
6349
6350 {
6351 struct it it2;
6352 void *it2data = NULL;
6353 ptrdiff_t pos;
6354 ptrdiff_t beg, end;
6355 Lisp_Object val, overlay;
6356
6357 SAVE_IT (it2, *it, it2data);
6358
6359 /* If newline is part of a composition, continue from start of composition */
6360 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6361 && beg < IT_CHARPOS (*it))
6362 goto replaced;
6363
6364 /* If newline is replaced by a display property, find start of overlay
6365 or interval and continue search from that point. */
6366 pos = --IT_CHARPOS (it2);
6367 --IT_BYTEPOS (it2);
6368 it2.sp = 0;
6369 bidi_unshelve_cache (NULL, false);
6370 it2.string_from_display_prop_p = false;
6371 it2.from_disp_prop_p = false;
6372 if (handle_display_prop (&it2) == HANDLED_RETURN
6373 && !NILP (val = get_char_property_and_overlay
6374 (make_number (pos), Qdisplay, Qnil, &overlay))
6375 && (OVERLAYP (overlay)
6376 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6377 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6378 {
6379 RESTORE_IT (it, it, it2data);
6380 goto replaced;
6381 }
6382
6383 /* Newline is not replaced by anything -- so we are done. */
6384 RESTORE_IT (it, it, it2data);
6385 break;
6386
6387 replaced:
6388 if (beg < BEGV)
6389 beg = BEGV;
6390 IT_CHARPOS (*it) = beg;
6391 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6392 }
6393 }
6394
6395 it->continuation_lines_width = 0;
6396
6397 eassert (IT_CHARPOS (*it) >= BEGV);
6398 eassert (IT_CHARPOS (*it) == BEGV
6399 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6400 CHECK_IT (it);
6401 }
6402
6403
6404 /* Reseat iterator IT at the previous visible line start. Skip
6405 invisible text that is so either due to text properties or due to
6406 selective display. At the end, update IT's overlay information,
6407 face information etc. */
6408
6409 void
6410 reseat_at_previous_visible_line_start (struct it *it)
6411 {
6412 back_to_previous_visible_line_start (it);
6413 reseat (it, it->current.pos, true);
6414 CHECK_IT (it);
6415 }
6416
6417
6418 /* Reseat iterator IT on the next visible line start in the current
6419 buffer. ON_NEWLINE_P means position IT on the newline
6420 preceding the line start. Skip over invisible text that is so
6421 because of selective display. Compute faces, overlays etc at the
6422 new position. Note that this function does not skip over text that
6423 is invisible because of text properties. */
6424
6425 static void
6426 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6427 {
6428 bool skipped_p = false;
6429 struct bidi_it bidi_it_prev;
6430 bool newline_found_p
6431 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6432
6433 /* Skip over lines that are invisible because they are indented
6434 more than the value of IT->selective. */
6435 if (it->selective > 0)
6436 while (IT_CHARPOS (*it) < ZV
6437 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6438 it->selective))
6439 {
6440 eassert (IT_BYTEPOS (*it) == BEGV
6441 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6442 newline_found_p =
6443 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6444 }
6445
6446 /* Position on the newline if that's what's requested. */
6447 if (on_newline_p && newline_found_p)
6448 {
6449 if (STRINGP (it->string))
6450 {
6451 if (IT_STRING_CHARPOS (*it) > 0)
6452 {
6453 if (!it->bidi_p)
6454 {
6455 --IT_STRING_CHARPOS (*it);
6456 --IT_STRING_BYTEPOS (*it);
6457 }
6458 else
6459 {
6460 /* We need to restore the bidi iterator to the state
6461 it had on the newline, and resync the IT's
6462 position with that. */
6463 it->bidi_it = bidi_it_prev;
6464 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6465 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6466 }
6467 }
6468 }
6469 else if (IT_CHARPOS (*it) > BEGV)
6470 {
6471 if (!it->bidi_p)
6472 {
6473 --IT_CHARPOS (*it);
6474 --IT_BYTEPOS (*it);
6475 }
6476 else
6477 {
6478 /* We need to restore the bidi iterator to the state it
6479 had on the newline and resync IT with that. */
6480 it->bidi_it = bidi_it_prev;
6481 IT_CHARPOS (*it) = it->bidi_it.charpos;
6482 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6483 }
6484 reseat (it, it->current.pos, false);
6485 }
6486 }
6487 else if (skipped_p)
6488 reseat (it, it->current.pos, false);
6489
6490 CHECK_IT (it);
6491 }
6492
6493
6494 \f
6495 /***********************************************************************
6496 Changing an iterator's position
6497 ***********************************************************************/
6498
6499 /* Change IT's current position to POS in current_buffer.
6500 If FORCE_P, always check for text properties at the new position.
6501 Otherwise, text properties are only looked up if POS >=
6502 IT->check_charpos of a property. */
6503
6504 static void
6505 reseat (struct it *it, struct text_pos pos, bool force_p)
6506 {
6507 ptrdiff_t original_pos = IT_CHARPOS (*it);
6508
6509 reseat_1 (it, pos, false);
6510
6511 /* Determine where to check text properties. Avoid doing it
6512 where possible because text property lookup is very expensive. */
6513 if (force_p
6514 || CHARPOS (pos) > it->stop_charpos
6515 || CHARPOS (pos) < original_pos)
6516 {
6517 if (it->bidi_p)
6518 {
6519 /* For bidi iteration, we need to prime prev_stop and
6520 base_level_stop with our best estimations. */
6521 /* Implementation note: Of course, POS is not necessarily a
6522 stop position, so assigning prev_pos to it is a lie; we
6523 should have called compute_stop_backwards. However, if
6524 the current buffer does not include any R2L characters,
6525 that call would be a waste of cycles, because the
6526 iterator will never move back, and thus never cross this
6527 "fake" stop position. So we delay that backward search
6528 until the time we really need it, in next_element_from_buffer. */
6529 if (CHARPOS (pos) != it->prev_stop)
6530 it->prev_stop = CHARPOS (pos);
6531 if (CHARPOS (pos) < it->base_level_stop)
6532 it->base_level_stop = 0; /* meaning it's unknown */
6533 handle_stop (it);
6534 }
6535 else
6536 {
6537 handle_stop (it);
6538 it->prev_stop = it->base_level_stop = 0;
6539 }
6540
6541 }
6542
6543 CHECK_IT (it);
6544 }
6545
6546
6547 /* Change IT's buffer position to POS. SET_STOP_P means set
6548 IT->stop_pos to POS, also. */
6549
6550 static void
6551 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6552 {
6553 /* Don't call this function when scanning a C string. */
6554 eassert (it->s == NULL);
6555
6556 /* POS must be a reasonable value. */
6557 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6558
6559 it->current.pos = it->position = pos;
6560 it->end_charpos = ZV;
6561 it->dpvec = NULL;
6562 it->current.dpvec_index = -1;
6563 it->current.overlay_string_index = -1;
6564 IT_STRING_CHARPOS (*it) = -1;
6565 IT_STRING_BYTEPOS (*it) = -1;
6566 it->string = Qnil;
6567 it->method = GET_FROM_BUFFER;
6568 it->object = it->w->contents;
6569 it->area = TEXT_AREA;
6570 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6571 it->sp = 0;
6572 it->string_from_display_prop_p = false;
6573 it->string_from_prefix_prop_p = false;
6574
6575 it->from_disp_prop_p = false;
6576 it->face_before_selective_p = false;
6577 if (it->bidi_p)
6578 {
6579 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6580 &it->bidi_it);
6581 bidi_unshelve_cache (NULL, false);
6582 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6583 it->bidi_it.string.s = NULL;
6584 it->bidi_it.string.lstring = Qnil;
6585 it->bidi_it.string.bufpos = 0;
6586 it->bidi_it.string.from_disp_str = false;
6587 it->bidi_it.string.unibyte = false;
6588 it->bidi_it.w = it->w;
6589 }
6590
6591 if (set_stop_p)
6592 {
6593 it->stop_charpos = CHARPOS (pos);
6594 it->base_level_stop = CHARPOS (pos);
6595 }
6596 /* This make the information stored in it->cmp_it invalidate. */
6597 it->cmp_it.id = -1;
6598 }
6599
6600
6601 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6602 If S is non-null, it is a C string to iterate over. Otherwise,
6603 STRING gives a Lisp string to iterate over.
6604
6605 If PRECISION > 0, don't return more then PRECISION number of
6606 characters from the string.
6607
6608 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6609 characters have been returned. FIELD_WIDTH < 0 means an infinite
6610 field width.
6611
6612 MULTIBYTE = 0 means disable processing of multibyte characters,
6613 MULTIBYTE > 0 means enable it,
6614 MULTIBYTE < 0 means use IT->multibyte_p.
6615
6616 IT must be initialized via a prior call to init_iterator before
6617 calling this function. */
6618
6619 static void
6620 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6621 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6622 int multibyte)
6623 {
6624 /* No text property checks performed by default, but see below. */
6625 it->stop_charpos = -1;
6626
6627 /* Set iterator position and end position. */
6628 memset (&it->current, 0, sizeof it->current);
6629 it->current.overlay_string_index = -1;
6630 it->current.dpvec_index = -1;
6631 eassert (charpos >= 0);
6632
6633 /* If STRING is specified, use its multibyteness, otherwise use the
6634 setting of MULTIBYTE, if specified. */
6635 if (multibyte >= 0)
6636 it->multibyte_p = multibyte > 0;
6637
6638 /* Bidirectional reordering of strings is controlled by the default
6639 value of bidi-display-reordering. Don't try to reorder while
6640 loading loadup.el, as the necessary character property tables are
6641 not yet available. */
6642 it->bidi_p =
6643 NILP (Vpurify_flag)
6644 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6645
6646 if (s == NULL)
6647 {
6648 eassert (STRINGP (string));
6649 it->string = string;
6650 it->s = NULL;
6651 it->end_charpos = it->string_nchars = SCHARS (string);
6652 it->method = GET_FROM_STRING;
6653 it->current.string_pos = string_pos (charpos, string);
6654
6655 if (it->bidi_p)
6656 {
6657 it->bidi_it.string.lstring = string;
6658 it->bidi_it.string.s = NULL;
6659 it->bidi_it.string.schars = it->end_charpos;
6660 it->bidi_it.string.bufpos = 0;
6661 it->bidi_it.string.from_disp_str = false;
6662 it->bidi_it.string.unibyte = !it->multibyte_p;
6663 it->bidi_it.w = it->w;
6664 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6665 FRAME_WINDOW_P (it->f), &it->bidi_it);
6666 }
6667 }
6668 else
6669 {
6670 it->s = (const unsigned char *) s;
6671 it->string = Qnil;
6672
6673 /* Note that we use IT->current.pos, not it->current.string_pos,
6674 for displaying C strings. */
6675 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6676 if (it->multibyte_p)
6677 {
6678 it->current.pos = c_string_pos (charpos, s, true);
6679 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6680 }
6681 else
6682 {
6683 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6684 it->end_charpos = it->string_nchars = strlen (s);
6685 }
6686
6687 if (it->bidi_p)
6688 {
6689 it->bidi_it.string.lstring = Qnil;
6690 it->bidi_it.string.s = (const unsigned char *) s;
6691 it->bidi_it.string.schars = it->end_charpos;
6692 it->bidi_it.string.bufpos = 0;
6693 it->bidi_it.string.from_disp_str = false;
6694 it->bidi_it.string.unibyte = !it->multibyte_p;
6695 it->bidi_it.w = it->w;
6696 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6697 &it->bidi_it);
6698 }
6699 it->method = GET_FROM_C_STRING;
6700 }
6701
6702 /* PRECISION > 0 means don't return more than PRECISION characters
6703 from the string. */
6704 if (precision > 0 && it->end_charpos - charpos > precision)
6705 {
6706 it->end_charpos = it->string_nchars = charpos + precision;
6707 if (it->bidi_p)
6708 it->bidi_it.string.schars = it->end_charpos;
6709 }
6710
6711 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6712 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6713 FIELD_WIDTH < 0 means infinite field width. This is useful for
6714 padding with `-' at the end of a mode line. */
6715 if (field_width < 0)
6716 field_width = INFINITY;
6717 /* Implementation note: We deliberately don't enlarge
6718 it->bidi_it.string.schars here to fit it->end_charpos, because
6719 the bidi iterator cannot produce characters out of thin air. */
6720 if (field_width > it->end_charpos - charpos)
6721 it->end_charpos = charpos + field_width;
6722
6723 /* Use the standard display table for displaying strings. */
6724 if (DISP_TABLE_P (Vstandard_display_table))
6725 it->dp = XCHAR_TABLE (Vstandard_display_table);
6726
6727 it->stop_charpos = charpos;
6728 it->prev_stop = charpos;
6729 it->base_level_stop = 0;
6730 if (it->bidi_p)
6731 {
6732 it->bidi_it.first_elt = true;
6733 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6734 it->bidi_it.disp_pos = -1;
6735 }
6736 if (s == NULL && it->multibyte_p)
6737 {
6738 ptrdiff_t endpos = SCHARS (it->string);
6739 if (endpos > it->end_charpos)
6740 endpos = it->end_charpos;
6741 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6742 it->string);
6743 }
6744 CHECK_IT (it);
6745 }
6746
6747
6748 \f
6749 /***********************************************************************
6750 Iteration
6751 ***********************************************************************/
6752
6753 /* Map enum it_method value to corresponding next_element_from_* function. */
6754
6755 typedef bool (*next_element_function) (struct it *);
6756
6757 static next_element_function const get_next_element[NUM_IT_METHODS] =
6758 {
6759 next_element_from_buffer,
6760 next_element_from_display_vector,
6761 next_element_from_string,
6762 next_element_from_c_string,
6763 next_element_from_image,
6764 next_element_from_stretch,
6765 next_element_from_xwidget,
6766 };
6767
6768 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6769
6770
6771 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6772 (possibly with the following characters). */
6773
6774 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6775 ((IT)->cmp_it.id >= 0 \
6776 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6777 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6778 END_CHARPOS, (IT)->w, \
6779 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6780 (IT)->string)))
6781
6782
6783 /* Lookup the char-table Vglyphless_char_display for character C (-1
6784 if we want information for no-font case), and return the display
6785 method symbol. By side-effect, update it->what and
6786 it->glyphless_method. This function is called from
6787 get_next_display_element for each character element, and from
6788 x_produce_glyphs when no suitable font was found. */
6789
6790 Lisp_Object
6791 lookup_glyphless_char_display (int c, struct it *it)
6792 {
6793 Lisp_Object glyphless_method = Qnil;
6794
6795 if (CHAR_TABLE_P (Vglyphless_char_display)
6796 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6797 {
6798 if (c >= 0)
6799 {
6800 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6801 if (CONSP (glyphless_method))
6802 glyphless_method = FRAME_WINDOW_P (it->f)
6803 ? XCAR (glyphless_method)
6804 : XCDR (glyphless_method);
6805 }
6806 else
6807 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6808 }
6809
6810 retry:
6811 if (NILP (glyphless_method))
6812 {
6813 if (c >= 0)
6814 /* The default is to display the character by a proper font. */
6815 return Qnil;
6816 /* The default for the no-font case is to display an empty box. */
6817 glyphless_method = Qempty_box;
6818 }
6819 if (EQ (glyphless_method, Qzero_width))
6820 {
6821 if (c >= 0)
6822 return glyphless_method;
6823 /* This method can't be used for the no-font case. */
6824 glyphless_method = Qempty_box;
6825 }
6826 if (EQ (glyphless_method, Qthin_space))
6827 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6828 else if (EQ (glyphless_method, Qempty_box))
6829 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6830 else if (EQ (glyphless_method, Qhex_code))
6831 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6832 else if (STRINGP (glyphless_method))
6833 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6834 else
6835 {
6836 /* Invalid value. We use the default method. */
6837 glyphless_method = Qnil;
6838 goto retry;
6839 }
6840 it->what = IT_GLYPHLESS;
6841 return glyphless_method;
6842 }
6843
6844 /* Merge escape glyph face and cache the result. */
6845
6846 static struct frame *last_escape_glyph_frame = NULL;
6847 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6848 static int last_escape_glyph_merged_face_id = 0;
6849
6850 static int
6851 merge_escape_glyph_face (struct it *it)
6852 {
6853 int face_id;
6854
6855 if (it->f == last_escape_glyph_frame
6856 && it->face_id == last_escape_glyph_face_id)
6857 face_id = last_escape_glyph_merged_face_id;
6858 else
6859 {
6860 /* Merge the `escape-glyph' face into the current face. */
6861 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6862 last_escape_glyph_frame = it->f;
6863 last_escape_glyph_face_id = it->face_id;
6864 last_escape_glyph_merged_face_id = face_id;
6865 }
6866 return face_id;
6867 }
6868
6869 /* Likewise for glyphless glyph face. */
6870
6871 static struct frame *last_glyphless_glyph_frame = NULL;
6872 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6873 static int last_glyphless_glyph_merged_face_id = 0;
6874
6875 int
6876 merge_glyphless_glyph_face (struct it *it)
6877 {
6878 int face_id;
6879
6880 if (it->f == last_glyphless_glyph_frame
6881 && it->face_id == last_glyphless_glyph_face_id)
6882 face_id = last_glyphless_glyph_merged_face_id;
6883 else
6884 {
6885 /* Merge the `glyphless-char' face into the current face. */
6886 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6887 last_glyphless_glyph_frame = it->f;
6888 last_glyphless_glyph_face_id = it->face_id;
6889 last_glyphless_glyph_merged_face_id = face_id;
6890 }
6891 return face_id;
6892 }
6893
6894 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6895 be called before redisplaying windows, and when the frame's face
6896 cache is freed. */
6897 void
6898 forget_escape_and_glyphless_faces (void)
6899 {
6900 last_escape_glyph_frame = NULL;
6901 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6902 last_glyphless_glyph_frame = NULL;
6903 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6904 }
6905
6906 /* Load IT's display element fields with information about the next
6907 display element from the current position of IT. Value is false if
6908 end of buffer (or C string) is reached. */
6909
6910 static bool
6911 get_next_display_element (struct it *it)
6912 {
6913 /* True means that we found a display element. False means that
6914 we hit the end of what we iterate over. Performance note: the
6915 function pointer `method' used here turns out to be faster than
6916 using a sequence of if-statements. */
6917 bool success_p;
6918
6919 get_next:
6920 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6921
6922 if (it->what == IT_CHARACTER)
6923 {
6924 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6925 and only if (a) the resolved directionality of that character
6926 is R..." */
6927 /* FIXME: Do we need an exception for characters from display
6928 tables? */
6929 if (it->bidi_p && it->bidi_it.type == STRONG_R
6930 && !inhibit_bidi_mirroring)
6931 it->c = bidi_mirror_char (it->c);
6932 /* Map via display table or translate control characters.
6933 IT->c, IT->len etc. have been set to the next character by
6934 the function call above. If we have a display table, and it
6935 contains an entry for IT->c, translate it. Don't do this if
6936 IT->c itself comes from a display table, otherwise we could
6937 end up in an infinite recursion. (An alternative could be to
6938 count the recursion depth of this function and signal an
6939 error when a certain maximum depth is reached.) Is it worth
6940 it? */
6941 if (success_p && it->dpvec == NULL)
6942 {
6943 Lisp_Object dv;
6944 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6945 bool nonascii_space_p = false;
6946 bool nonascii_hyphen_p = false;
6947 int c = it->c; /* This is the character to display. */
6948
6949 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6950 {
6951 eassert (SINGLE_BYTE_CHAR_P (c));
6952 if (unibyte_display_via_language_environment)
6953 {
6954 c = DECODE_CHAR (unibyte, c);
6955 if (c < 0)
6956 c = BYTE8_TO_CHAR (it->c);
6957 }
6958 else
6959 c = BYTE8_TO_CHAR (it->c);
6960 }
6961
6962 if (it->dp
6963 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6964 VECTORP (dv)))
6965 {
6966 struct Lisp_Vector *v = XVECTOR (dv);
6967
6968 /* Return the first character from the display table
6969 entry, if not empty. If empty, don't display the
6970 current character. */
6971 if (v->header.size)
6972 {
6973 it->dpvec_char_len = it->len;
6974 it->dpvec = v->contents;
6975 it->dpend = v->contents + v->header.size;
6976 it->current.dpvec_index = 0;
6977 it->dpvec_face_id = -1;
6978 it->saved_face_id = it->face_id;
6979 it->method = GET_FROM_DISPLAY_VECTOR;
6980 it->ellipsis_p = false;
6981 }
6982 else
6983 {
6984 set_iterator_to_next (it, false);
6985 }
6986 goto get_next;
6987 }
6988
6989 if (! NILP (lookup_glyphless_char_display (c, it)))
6990 {
6991 if (it->what == IT_GLYPHLESS)
6992 goto done;
6993 /* Don't display this character. */
6994 set_iterator_to_next (it, false);
6995 goto get_next;
6996 }
6997
6998 /* If `nobreak-char-display' is non-nil, we display
6999 non-ASCII spaces and hyphens specially. */
7000 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7001 {
7002 if (c == NO_BREAK_SPACE)
7003 nonascii_space_p = true;
7004 else if (c == SOFT_HYPHEN || c == HYPHEN
7005 || c == NON_BREAKING_HYPHEN)
7006 nonascii_hyphen_p = true;
7007 }
7008
7009 /* Translate control characters into `\003' or `^C' form.
7010 Control characters coming from a display table entry are
7011 currently not translated because we use IT->dpvec to hold
7012 the translation. This could easily be changed but I
7013 don't believe that it is worth doing.
7014
7015 The characters handled by `nobreak-char-display' must be
7016 translated too.
7017
7018 Non-printable characters and raw-byte characters are also
7019 translated to octal form. */
7020 if (((c < ' ' || c == 127) /* ASCII control chars. */
7021 ? (it->area != TEXT_AREA
7022 /* In mode line, treat \n, \t like other crl chars. */
7023 || (c != '\t'
7024 && it->glyph_row
7025 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7026 || (c != '\n' && c != '\t'))
7027 : (nonascii_space_p
7028 || nonascii_hyphen_p
7029 || CHAR_BYTE8_P (c)
7030 || ! CHAR_PRINTABLE_P (c))))
7031 {
7032 /* C is a control character, non-ASCII space/hyphen,
7033 raw-byte, or a non-printable character which must be
7034 displayed either as '\003' or as `^C' where the '\\'
7035 and '^' can be defined in the display table. Fill
7036 IT->ctl_chars with glyphs for what we have to
7037 display. Then, set IT->dpvec to these glyphs. */
7038 Lisp_Object gc;
7039 int ctl_len;
7040 int face_id;
7041 int lface_id = 0;
7042 int escape_glyph;
7043
7044 /* Handle control characters with ^. */
7045
7046 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7047 {
7048 int g;
7049
7050 g = '^'; /* default glyph for Control */
7051 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7052 if (it->dp
7053 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7054 {
7055 g = GLYPH_CODE_CHAR (gc);
7056 lface_id = GLYPH_CODE_FACE (gc);
7057 }
7058
7059 face_id = (lface_id
7060 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7061 : merge_escape_glyph_face (it));
7062
7063 XSETINT (it->ctl_chars[0], g);
7064 XSETINT (it->ctl_chars[1], c ^ 0100);
7065 ctl_len = 2;
7066 goto display_control;
7067 }
7068
7069 /* Handle non-ascii space in the mode where it only gets
7070 highlighting. */
7071
7072 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7073 {
7074 /* Merge `nobreak-space' into the current face. */
7075 face_id = merge_faces (it->f, Qnobreak_space, 0,
7076 it->face_id);
7077 XSETINT (it->ctl_chars[0], ' ');
7078 ctl_len = 1;
7079 goto display_control;
7080 }
7081
7082 /* Handle sequences that start with the "escape glyph". */
7083
7084 /* the default escape glyph is \. */
7085 escape_glyph = '\\';
7086
7087 if (it->dp
7088 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7089 {
7090 escape_glyph = GLYPH_CODE_CHAR (gc);
7091 lface_id = GLYPH_CODE_FACE (gc);
7092 }
7093
7094 face_id = (lface_id
7095 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7096 : merge_escape_glyph_face (it));
7097
7098 /* Draw non-ASCII hyphen with just highlighting: */
7099
7100 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7101 {
7102 XSETINT (it->ctl_chars[0], '-');
7103 ctl_len = 1;
7104 goto display_control;
7105 }
7106
7107 /* Draw non-ASCII space/hyphen with escape glyph: */
7108
7109 if (nonascii_space_p || nonascii_hyphen_p)
7110 {
7111 XSETINT (it->ctl_chars[0], escape_glyph);
7112 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7113 ctl_len = 2;
7114 goto display_control;
7115 }
7116
7117 {
7118 char str[10];
7119 int len, i;
7120
7121 if (CHAR_BYTE8_P (c))
7122 /* Display \200 instead of \17777600. */
7123 c = CHAR_TO_BYTE8 (c);
7124 len = sprintf (str, "%03o", c + 0u);
7125
7126 XSETINT (it->ctl_chars[0], escape_glyph);
7127 for (i = 0; i < len; i++)
7128 XSETINT (it->ctl_chars[i + 1], str[i]);
7129 ctl_len = len + 1;
7130 }
7131
7132 display_control:
7133 /* Set up IT->dpvec and return first character from it. */
7134 it->dpvec_char_len = it->len;
7135 it->dpvec = it->ctl_chars;
7136 it->dpend = it->dpvec + ctl_len;
7137 it->current.dpvec_index = 0;
7138 it->dpvec_face_id = face_id;
7139 it->saved_face_id = it->face_id;
7140 it->method = GET_FROM_DISPLAY_VECTOR;
7141 it->ellipsis_p = false;
7142 goto get_next;
7143 }
7144 it->char_to_display = c;
7145 }
7146 else if (success_p)
7147 {
7148 it->char_to_display = it->c;
7149 }
7150 }
7151
7152 #ifdef HAVE_WINDOW_SYSTEM
7153 /* Adjust face id for a multibyte character. There are no multibyte
7154 character in unibyte text. */
7155 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7156 && it->multibyte_p
7157 && success_p
7158 && FRAME_WINDOW_P (it->f))
7159 {
7160 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7161
7162 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7163 {
7164 /* Automatic composition with glyph-string. */
7165 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7166
7167 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7168 }
7169 else
7170 {
7171 ptrdiff_t pos = (it->s ? -1
7172 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7173 : IT_CHARPOS (*it));
7174 int c;
7175
7176 if (it->what == IT_CHARACTER)
7177 c = it->char_to_display;
7178 else
7179 {
7180 struct composition *cmp = composition_table[it->cmp_it.id];
7181 int i;
7182
7183 c = ' ';
7184 for (i = 0; i < cmp->glyph_len; i++)
7185 /* TAB in a composition means display glyphs with
7186 padding space on the left or right. */
7187 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7188 break;
7189 }
7190 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7191 }
7192 }
7193 #endif /* HAVE_WINDOW_SYSTEM */
7194
7195 done:
7196 /* Is this character the last one of a run of characters with
7197 box? If yes, set IT->end_of_box_run_p to true. */
7198 if (it->face_box_p
7199 && it->s == NULL)
7200 {
7201 if (it->method == GET_FROM_STRING && it->sp)
7202 {
7203 int face_id = underlying_face_id (it);
7204 struct face *face = FACE_FROM_ID (it->f, face_id);
7205
7206 if (face)
7207 {
7208 if (face->box == FACE_NO_BOX)
7209 {
7210 /* If the box comes from face properties in a
7211 display string, check faces in that string. */
7212 int string_face_id = face_after_it_pos (it);
7213 it->end_of_box_run_p
7214 = (FACE_FROM_ID (it->f, string_face_id)->box
7215 == FACE_NO_BOX);
7216 }
7217 /* Otherwise, the box comes from the underlying face.
7218 If this is the last string character displayed, check
7219 the next buffer location. */
7220 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7221 /* n_overlay_strings is unreliable unless
7222 overlay_string_index is non-negative. */
7223 && ((it->current.overlay_string_index >= 0
7224 && (it->current.overlay_string_index
7225 == it->n_overlay_strings - 1))
7226 /* A string from display property. */
7227 || it->from_disp_prop_p))
7228 {
7229 ptrdiff_t ignore;
7230 int next_face_id;
7231 struct text_pos pos = it->current.pos;
7232
7233 /* For a string from a display property, the next
7234 buffer position is stored in the 'position'
7235 member of the iteration stack slot below the
7236 current one, see handle_single_display_spec. By
7237 contrast, it->current.pos was not yet updated
7238 to point to that buffer position; that will
7239 happen in pop_it, after we finish displaying the
7240 current string. Note that we already checked
7241 above that it->sp is positive, so subtracting one
7242 from it is safe. */
7243 if (it->from_disp_prop_p)
7244 {
7245 int stackp = it->sp - 1;
7246
7247 /* Find the stack level with data from buffer. */
7248 while (stackp >= 0
7249 && STRINGP ((it->stack + stackp)->string))
7250 stackp--;
7251 eassert (stackp >= 0);
7252 pos = (it->stack + stackp)->position;
7253 }
7254 else
7255 INC_TEXT_POS (pos, it->multibyte_p);
7256
7257 if (CHARPOS (pos) >= ZV)
7258 it->end_of_box_run_p = true;
7259 else
7260 {
7261 next_face_id = face_at_buffer_position
7262 (it->w, CHARPOS (pos), &ignore,
7263 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, false, -1);
7264 it->end_of_box_run_p
7265 = (FACE_FROM_ID (it->f, next_face_id)->box
7266 == FACE_NO_BOX);
7267 }
7268 }
7269 }
7270 }
7271 /* next_element_from_display_vector sets this flag according to
7272 faces of the display vector glyphs, see there. */
7273 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7274 {
7275 int face_id = face_after_it_pos (it);
7276 it->end_of_box_run_p
7277 = (face_id != it->face_id
7278 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7279 }
7280 }
7281 /* If we reached the end of the object we've been iterating (e.g., a
7282 display string or an overlay string), and there's something on
7283 IT->stack, proceed with what's on the stack. It doesn't make
7284 sense to return false if there's unprocessed stuff on the stack,
7285 because otherwise that stuff will never be displayed. */
7286 if (!success_p && it->sp > 0)
7287 {
7288 set_iterator_to_next (it, false);
7289 success_p = get_next_display_element (it);
7290 }
7291
7292 /* Value is false if end of buffer or string reached. */
7293 return success_p;
7294 }
7295
7296
7297 /* Move IT to the next display element.
7298
7299 RESEAT_P means if called on a newline in buffer text,
7300 skip to the next visible line start.
7301
7302 Functions get_next_display_element and set_iterator_to_next are
7303 separate because I find this arrangement easier to handle than a
7304 get_next_display_element function that also increments IT's
7305 position. The way it is we can first look at an iterator's current
7306 display element, decide whether it fits on a line, and if it does,
7307 increment the iterator position. The other way around we probably
7308 would either need a flag indicating whether the iterator has to be
7309 incremented the next time, or we would have to implement a
7310 decrement position function which would not be easy to write. */
7311
7312 void
7313 set_iterator_to_next (struct it *it, bool reseat_p)
7314 {
7315 /* Reset flags indicating start and end of a sequence of characters
7316 with box. Reset them at the start of this function because
7317 moving the iterator to a new position might set them. */
7318 it->start_of_box_run_p = it->end_of_box_run_p = false;
7319
7320 switch (it->method)
7321 {
7322 case GET_FROM_BUFFER:
7323 /* The current display element of IT is a character from
7324 current_buffer. Advance in the buffer, and maybe skip over
7325 invisible lines that are so because of selective display. */
7326 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7327 reseat_at_next_visible_line_start (it, false);
7328 else if (it->cmp_it.id >= 0)
7329 {
7330 /* We are currently getting glyphs from a composition. */
7331 if (! it->bidi_p)
7332 {
7333 IT_CHARPOS (*it) += it->cmp_it.nchars;
7334 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7335 }
7336 else
7337 {
7338 int i;
7339
7340 /* Update IT's char/byte positions to point to the first
7341 character of the next grapheme cluster, or to the
7342 character visually after the current composition. */
7343 for (i = 0; i < it->cmp_it.nchars; i++)
7344 bidi_move_to_visually_next (&it->bidi_it);
7345 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7346 IT_CHARPOS (*it) = it->bidi_it.charpos;
7347 }
7348
7349 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7350 && it->cmp_it.to < it->cmp_it.nglyphs)
7351 {
7352 /* Composition created while scanning forward. Proceed
7353 to the next grapheme cluster. */
7354 it->cmp_it.from = it->cmp_it.to;
7355 }
7356 else if ((it->bidi_p && it->cmp_it.reversed_p)
7357 && it->cmp_it.from > 0)
7358 {
7359 /* Composition created while scanning backward. Proceed
7360 to the previous grapheme cluster. */
7361 it->cmp_it.to = it->cmp_it.from;
7362 }
7363 else
7364 {
7365 /* No more grapheme clusters in this composition.
7366 Find the next stop position. */
7367 ptrdiff_t stop = it->end_charpos;
7368
7369 if (it->bidi_it.scan_dir < 0)
7370 /* Now we are scanning backward and don't know
7371 where to stop. */
7372 stop = -1;
7373 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7374 IT_BYTEPOS (*it), stop, Qnil);
7375 }
7376 }
7377 else
7378 {
7379 eassert (it->len != 0);
7380
7381 if (!it->bidi_p)
7382 {
7383 IT_BYTEPOS (*it) += it->len;
7384 IT_CHARPOS (*it) += 1;
7385 }
7386 else
7387 {
7388 int prev_scan_dir = it->bidi_it.scan_dir;
7389 /* If this is a new paragraph, determine its base
7390 direction (a.k.a. its base embedding level). */
7391 if (it->bidi_it.new_paragraph)
7392 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7393 false);
7394 bidi_move_to_visually_next (&it->bidi_it);
7395 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7396 IT_CHARPOS (*it) = it->bidi_it.charpos;
7397 if (prev_scan_dir != it->bidi_it.scan_dir)
7398 {
7399 /* As the scan direction was changed, we must
7400 re-compute the stop position for composition. */
7401 ptrdiff_t stop = it->end_charpos;
7402 if (it->bidi_it.scan_dir < 0)
7403 stop = -1;
7404 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7405 IT_BYTEPOS (*it), stop, Qnil);
7406 }
7407 }
7408 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7409 }
7410 break;
7411
7412 case GET_FROM_C_STRING:
7413 /* Current display element of IT is from a C string. */
7414 if (!it->bidi_p
7415 /* If the string position is beyond string's end, it means
7416 next_element_from_c_string is padding the string with
7417 blanks, in which case we bypass the bidi iterator,
7418 because it cannot deal with such virtual characters. */
7419 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7420 {
7421 IT_BYTEPOS (*it) += it->len;
7422 IT_CHARPOS (*it) += 1;
7423 }
7424 else
7425 {
7426 bidi_move_to_visually_next (&it->bidi_it);
7427 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7428 IT_CHARPOS (*it) = it->bidi_it.charpos;
7429 }
7430 break;
7431
7432 case GET_FROM_DISPLAY_VECTOR:
7433 /* Current display element of IT is from a display table entry.
7434 Advance in the display table definition. Reset it to null if
7435 end reached, and continue with characters from buffers/
7436 strings. */
7437 ++it->current.dpvec_index;
7438
7439 /* Restore face of the iterator to what they were before the
7440 display vector entry (these entries may contain faces). */
7441 it->face_id = it->saved_face_id;
7442
7443 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7444 {
7445 bool recheck_faces = it->ellipsis_p;
7446
7447 if (it->s)
7448 it->method = GET_FROM_C_STRING;
7449 else if (STRINGP (it->string))
7450 it->method = GET_FROM_STRING;
7451 else
7452 {
7453 it->method = GET_FROM_BUFFER;
7454 it->object = it->w->contents;
7455 }
7456
7457 it->dpvec = NULL;
7458 it->current.dpvec_index = -1;
7459
7460 /* Skip over characters which were displayed via IT->dpvec. */
7461 if (it->dpvec_char_len < 0)
7462 reseat_at_next_visible_line_start (it, true);
7463 else if (it->dpvec_char_len > 0)
7464 {
7465 it->len = it->dpvec_char_len;
7466 set_iterator_to_next (it, reseat_p);
7467 }
7468
7469 /* Maybe recheck faces after display vector. */
7470 if (recheck_faces)
7471 {
7472 if (it->method == GET_FROM_STRING)
7473 it->stop_charpos = IT_STRING_CHARPOS (*it);
7474 else
7475 it->stop_charpos = IT_CHARPOS (*it);
7476 }
7477 }
7478 break;
7479
7480 case GET_FROM_STRING:
7481 /* Current display element is a character from a Lisp string. */
7482 eassert (it->s == NULL && STRINGP (it->string));
7483 /* Don't advance past string end. These conditions are true
7484 when set_iterator_to_next is called at the end of
7485 get_next_display_element, in which case the Lisp string is
7486 already exhausted, and all we want is pop the iterator
7487 stack. */
7488 if (it->current.overlay_string_index >= 0)
7489 {
7490 /* This is an overlay string, so there's no padding with
7491 spaces, and the number of characters in the string is
7492 where the string ends. */
7493 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7494 goto consider_string_end;
7495 }
7496 else
7497 {
7498 /* Not an overlay string. There could be padding, so test
7499 against it->end_charpos. */
7500 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7501 goto consider_string_end;
7502 }
7503 if (it->cmp_it.id >= 0)
7504 {
7505 /* We are delivering display elements from a composition.
7506 Update the string position past the grapheme cluster
7507 we've just processed. */
7508 if (! it->bidi_p)
7509 {
7510 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7511 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7512 }
7513 else
7514 {
7515 int i;
7516
7517 for (i = 0; i < it->cmp_it.nchars; i++)
7518 bidi_move_to_visually_next (&it->bidi_it);
7519 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7520 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7521 }
7522
7523 /* Did we exhaust all the grapheme clusters of this
7524 composition? */
7525 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7526 && (it->cmp_it.to < it->cmp_it.nglyphs))
7527 {
7528 /* Not all the grapheme clusters were processed yet;
7529 advance to the next cluster. */
7530 it->cmp_it.from = it->cmp_it.to;
7531 }
7532 else if ((it->bidi_p && it->cmp_it.reversed_p)
7533 && it->cmp_it.from > 0)
7534 {
7535 /* Likewise: advance to the next cluster, but going in
7536 the reverse direction. */
7537 it->cmp_it.to = it->cmp_it.from;
7538 }
7539 else
7540 {
7541 /* This composition was fully processed; find the next
7542 candidate place for checking for composed
7543 characters. */
7544 /* Always limit string searches to the string length;
7545 any padding spaces are not part of the string, and
7546 there cannot be any compositions in that padding. */
7547 ptrdiff_t stop = SCHARS (it->string);
7548
7549 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7550 stop = -1;
7551 else if (it->end_charpos < stop)
7552 {
7553 /* Cf. PRECISION in reseat_to_string: we might be
7554 limited in how many of the string characters we
7555 need to deliver. */
7556 stop = it->end_charpos;
7557 }
7558 composition_compute_stop_pos (&it->cmp_it,
7559 IT_STRING_CHARPOS (*it),
7560 IT_STRING_BYTEPOS (*it), stop,
7561 it->string);
7562 }
7563 }
7564 else
7565 {
7566 if (!it->bidi_p
7567 /* If the string position is beyond string's end, it
7568 means next_element_from_string is padding the string
7569 with blanks, in which case we bypass the bidi
7570 iterator, because it cannot deal with such virtual
7571 characters. */
7572 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7573 {
7574 IT_STRING_BYTEPOS (*it) += it->len;
7575 IT_STRING_CHARPOS (*it) += 1;
7576 }
7577 else
7578 {
7579 int prev_scan_dir = it->bidi_it.scan_dir;
7580
7581 bidi_move_to_visually_next (&it->bidi_it);
7582 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7583 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7584 /* If the scan direction changes, we may need to update
7585 the place where to check for composed characters. */
7586 if (prev_scan_dir != it->bidi_it.scan_dir)
7587 {
7588 ptrdiff_t stop = SCHARS (it->string);
7589
7590 if (it->bidi_it.scan_dir < 0)
7591 stop = -1;
7592 else if (it->end_charpos < stop)
7593 stop = it->end_charpos;
7594
7595 composition_compute_stop_pos (&it->cmp_it,
7596 IT_STRING_CHARPOS (*it),
7597 IT_STRING_BYTEPOS (*it), stop,
7598 it->string);
7599 }
7600 }
7601 }
7602
7603 consider_string_end:
7604
7605 if (it->current.overlay_string_index >= 0)
7606 {
7607 /* IT->string is an overlay string. Advance to the
7608 next, if there is one. */
7609 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7610 {
7611 it->ellipsis_p = false;
7612 next_overlay_string (it);
7613 if (it->ellipsis_p)
7614 setup_for_ellipsis (it, 0);
7615 }
7616 }
7617 else
7618 {
7619 /* IT->string is not an overlay string. If we reached
7620 its end, and there is something on IT->stack, proceed
7621 with what is on the stack. This can be either another
7622 string, this time an overlay string, or a buffer. */
7623 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7624 && it->sp > 0)
7625 {
7626 pop_it (it);
7627 if (it->method == GET_FROM_STRING)
7628 goto consider_string_end;
7629 }
7630 }
7631 break;
7632
7633 case GET_FROM_IMAGE:
7634 case GET_FROM_STRETCH:
7635 case GET_FROM_XWIDGET:
7636
7637 /* The position etc with which we have to proceed are on
7638 the stack. The position may be at the end of a string,
7639 if the `display' property takes up the whole string. */
7640 eassert (it->sp > 0);
7641 pop_it (it);
7642 if (it->method == GET_FROM_STRING)
7643 goto consider_string_end;
7644 break;
7645
7646 default:
7647 /* There are no other methods defined, so this should be a bug. */
7648 emacs_abort ();
7649 }
7650
7651 eassert (it->method != GET_FROM_STRING
7652 || (STRINGP (it->string)
7653 && IT_STRING_CHARPOS (*it) >= 0));
7654 }
7655
7656 /* Load IT's display element fields with information about the next
7657 display element which comes from a display table entry or from the
7658 result of translating a control character to one of the forms `^C'
7659 or `\003'.
7660
7661 IT->dpvec holds the glyphs to return as characters.
7662 IT->saved_face_id holds the face id before the display vector--it
7663 is restored into IT->face_id in set_iterator_to_next. */
7664
7665 static bool
7666 next_element_from_display_vector (struct it *it)
7667 {
7668 Lisp_Object gc;
7669 int prev_face_id = it->face_id;
7670 int next_face_id;
7671
7672 /* Precondition. */
7673 eassert (it->dpvec && it->current.dpvec_index >= 0);
7674
7675 it->face_id = it->saved_face_id;
7676
7677 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7678 That seemed totally bogus - so I changed it... */
7679 gc = it->dpvec[it->current.dpvec_index];
7680
7681 if (GLYPH_CODE_P (gc))
7682 {
7683 struct face *this_face, *prev_face, *next_face;
7684
7685 it->c = GLYPH_CODE_CHAR (gc);
7686 it->len = CHAR_BYTES (it->c);
7687
7688 /* The entry may contain a face id to use. Such a face id is
7689 the id of a Lisp face, not a realized face. A face id of
7690 zero means no face is specified. */
7691 if (it->dpvec_face_id >= 0)
7692 it->face_id = it->dpvec_face_id;
7693 else
7694 {
7695 int lface_id = GLYPH_CODE_FACE (gc);
7696 if (lface_id > 0)
7697 it->face_id = merge_faces (it->f, Qt, lface_id,
7698 it->saved_face_id);
7699 }
7700
7701 /* Glyphs in the display vector could have the box face, so we
7702 need to set the related flags in the iterator, as
7703 appropriate. */
7704 this_face = FACE_FROM_ID (it->f, it->face_id);
7705 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7706
7707 /* Is this character the first character of a box-face run? */
7708 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7709 && (!prev_face
7710 || prev_face->box == FACE_NO_BOX));
7711
7712 /* For the last character of the box-face run, we need to look
7713 either at the next glyph from the display vector, or at the
7714 face we saw before the display vector. */
7715 next_face_id = it->saved_face_id;
7716 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7717 {
7718 if (it->dpvec_face_id >= 0)
7719 next_face_id = it->dpvec_face_id;
7720 else
7721 {
7722 int lface_id =
7723 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7724
7725 if (lface_id > 0)
7726 next_face_id = merge_faces (it->f, Qt, lface_id,
7727 it->saved_face_id);
7728 }
7729 }
7730 next_face = FACE_FROM_ID (it->f, next_face_id);
7731 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7732 && (!next_face
7733 || next_face->box == FACE_NO_BOX));
7734 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7735 }
7736 else
7737 /* Display table entry is invalid. Return a space. */
7738 it->c = ' ', it->len = 1;
7739
7740 /* Don't change position and object of the iterator here. They are
7741 still the values of the character that had this display table
7742 entry or was translated, and that's what we want. */
7743 it->what = IT_CHARACTER;
7744 return true;
7745 }
7746
7747 /* Get the first element of string/buffer in the visual order, after
7748 being reseated to a new position in a string or a buffer. */
7749 static void
7750 get_visually_first_element (struct it *it)
7751 {
7752 bool string_p = STRINGP (it->string) || it->s;
7753 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7754 ptrdiff_t bob = (string_p ? 0 : BEGV);
7755
7756 if (STRINGP (it->string))
7757 {
7758 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7759 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7760 }
7761 else
7762 {
7763 it->bidi_it.charpos = IT_CHARPOS (*it);
7764 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7765 }
7766
7767 if (it->bidi_it.charpos == eob)
7768 {
7769 /* Nothing to do, but reset the FIRST_ELT flag, like
7770 bidi_paragraph_init does, because we are not going to
7771 call it. */
7772 it->bidi_it.first_elt = false;
7773 }
7774 else if (it->bidi_it.charpos == bob
7775 || (!string_p
7776 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7777 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7778 {
7779 /* If we are at the beginning of a line/string, we can produce
7780 the next element right away. */
7781 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7782 bidi_move_to_visually_next (&it->bidi_it);
7783 }
7784 else
7785 {
7786 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7787
7788 /* We need to prime the bidi iterator starting at the line's or
7789 string's beginning, before we will be able to produce the
7790 next element. */
7791 if (string_p)
7792 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7793 else
7794 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7795 IT_BYTEPOS (*it), -1,
7796 &it->bidi_it.bytepos);
7797 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7798 do
7799 {
7800 /* Now return to buffer/string position where we were asked
7801 to get the next display element, and produce that. */
7802 bidi_move_to_visually_next (&it->bidi_it);
7803 }
7804 while (it->bidi_it.bytepos != orig_bytepos
7805 && it->bidi_it.charpos < eob);
7806 }
7807
7808 /* Adjust IT's position information to where we ended up. */
7809 if (STRINGP (it->string))
7810 {
7811 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7812 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7813 }
7814 else
7815 {
7816 IT_CHARPOS (*it) = it->bidi_it.charpos;
7817 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7818 }
7819
7820 if (STRINGP (it->string) || !it->s)
7821 {
7822 ptrdiff_t stop, charpos, bytepos;
7823
7824 if (STRINGP (it->string))
7825 {
7826 eassert (!it->s);
7827 stop = SCHARS (it->string);
7828 if (stop > it->end_charpos)
7829 stop = it->end_charpos;
7830 charpos = IT_STRING_CHARPOS (*it);
7831 bytepos = IT_STRING_BYTEPOS (*it);
7832 }
7833 else
7834 {
7835 stop = it->end_charpos;
7836 charpos = IT_CHARPOS (*it);
7837 bytepos = IT_BYTEPOS (*it);
7838 }
7839 if (it->bidi_it.scan_dir < 0)
7840 stop = -1;
7841 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7842 it->string);
7843 }
7844 }
7845
7846 /* Load IT with the next display element from Lisp string IT->string.
7847 IT->current.string_pos is the current position within the string.
7848 If IT->current.overlay_string_index >= 0, the Lisp string is an
7849 overlay string. */
7850
7851 static bool
7852 next_element_from_string (struct it *it)
7853 {
7854 struct text_pos position;
7855
7856 eassert (STRINGP (it->string));
7857 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7858 eassert (IT_STRING_CHARPOS (*it) >= 0);
7859 position = it->current.string_pos;
7860
7861 /* With bidi reordering, the character to display might not be the
7862 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7863 that we were reseat()ed to a new string, whose paragraph
7864 direction is not known. */
7865 if (it->bidi_p && it->bidi_it.first_elt)
7866 {
7867 get_visually_first_element (it);
7868 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7869 }
7870
7871 /* Time to check for invisible text? */
7872 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7873 {
7874 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7875 {
7876 if (!(!it->bidi_p
7877 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7878 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7879 {
7880 /* With bidi non-linear iteration, we could find
7881 ourselves far beyond the last computed stop_charpos,
7882 with several other stop positions in between that we
7883 missed. Scan them all now, in buffer's logical
7884 order, until we find and handle the last stop_charpos
7885 that precedes our current position. */
7886 handle_stop_backwards (it, it->stop_charpos);
7887 return GET_NEXT_DISPLAY_ELEMENT (it);
7888 }
7889 else
7890 {
7891 if (it->bidi_p)
7892 {
7893 /* Take note of the stop position we just moved
7894 across, for when we will move back across it. */
7895 it->prev_stop = it->stop_charpos;
7896 /* If we are at base paragraph embedding level, take
7897 note of the last stop position seen at this
7898 level. */
7899 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7900 it->base_level_stop = it->stop_charpos;
7901 }
7902 handle_stop (it);
7903
7904 /* Since a handler may have changed IT->method, we must
7905 recurse here. */
7906 return GET_NEXT_DISPLAY_ELEMENT (it);
7907 }
7908 }
7909 else if (it->bidi_p
7910 /* If we are before prev_stop, we may have overstepped
7911 on our way backwards a stop_pos, and if so, we need
7912 to handle that stop_pos. */
7913 && IT_STRING_CHARPOS (*it) < it->prev_stop
7914 /* We can sometimes back up for reasons that have nothing
7915 to do with bidi reordering. E.g., compositions. The
7916 code below is only needed when we are above the base
7917 embedding level, so test for that explicitly. */
7918 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7919 {
7920 /* If we lost track of base_level_stop, we have no better
7921 place for handle_stop_backwards to start from than string
7922 beginning. This happens, e.g., when we were reseated to
7923 the previous screenful of text by vertical-motion. */
7924 if (it->base_level_stop <= 0
7925 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7926 it->base_level_stop = 0;
7927 handle_stop_backwards (it, it->base_level_stop);
7928 return GET_NEXT_DISPLAY_ELEMENT (it);
7929 }
7930 }
7931
7932 if (it->current.overlay_string_index >= 0)
7933 {
7934 /* Get the next character from an overlay string. In overlay
7935 strings, there is no field width or padding with spaces to
7936 do. */
7937 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7938 {
7939 it->what = IT_EOB;
7940 return false;
7941 }
7942 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7943 IT_STRING_BYTEPOS (*it),
7944 it->bidi_it.scan_dir < 0
7945 ? -1
7946 : SCHARS (it->string))
7947 && next_element_from_composition (it))
7948 {
7949 return true;
7950 }
7951 else if (STRING_MULTIBYTE (it->string))
7952 {
7953 const unsigned char *s = (SDATA (it->string)
7954 + IT_STRING_BYTEPOS (*it));
7955 it->c = string_char_and_length (s, &it->len);
7956 }
7957 else
7958 {
7959 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7960 it->len = 1;
7961 }
7962 }
7963 else
7964 {
7965 /* Get the next character from a Lisp string that is not an
7966 overlay string. Such strings come from the mode line, for
7967 example. We may have to pad with spaces, or truncate the
7968 string. See also next_element_from_c_string. */
7969 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7970 {
7971 it->what = IT_EOB;
7972 return false;
7973 }
7974 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7975 {
7976 /* Pad with spaces. */
7977 it->c = ' ', it->len = 1;
7978 CHARPOS (position) = BYTEPOS (position) = -1;
7979 }
7980 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7981 IT_STRING_BYTEPOS (*it),
7982 it->bidi_it.scan_dir < 0
7983 ? -1
7984 : it->string_nchars)
7985 && next_element_from_composition (it))
7986 {
7987 return true;
7988 }
7989 else if (STRING_MULTIBYTE (it->string))
7990 {
7991 const unsigned char *s = (SDATA (it->string)
7992 + IT_STRING_BYTEPOS (*it));
7993 it->c = string_char_and_length (s, &it->len);
7994 }
7995 else
7996 {
7997 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7998 it->len = 1;
7999 }
8000 }
8001
8002 /* Record what we have and where it came from. */
8003 it->what = IT_CHARACTER;
8004 it->object = it->string;
8005 it->position = position;
8006 return true;
8007 }
8008
8009
8010 /* Load IT with next display element from C string IT->s.
8011 IT->string_nchars is the maximum number of characters to return
8012 from the string. IT->end_charpos may be greater than
8013 IT->string_nchars when this function is called, in which case we
8014 may have to return padding spaces. Value is false if end of string
8015 reached, including padding spaces. */
8016
8017 static bool
8018 next_element_from_c_string (struct it *it)
8019 {
8020 bool success_p = true;
8021
8022 eassert (it->s);
8023 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8024 it->what = IT_CHARACTER;
8025 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8026 it->object = make_number (0);
8027
8028 /* With bidi reordering, the character to display might not be the
8029 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8030 we were reseated to a new string, whose paragraph direction is
8031 not known. */
8032 if (it->bidi_p && it->bidi_it.first_elt)
8033 get_visually_first_element (it);
8034
8035 /* IT's position can be greater than IT->string_nchars in case a
8036 field width or precision has been specified when the iterator was
8037 initialized. */
8038 if (IT_CHARPOS (*it) >= it->end_charpos)
8039 {
8040 /* End of the game. */
8041 it->what = IT_EOB;
8042 success_p = false;
8043 }
8044 else if (IT_CHARPOS (*it) >= it->string_nchars)
8045 {
8046 /* Pad with spaces. */
8047 it->c = ' ', it->len = 1;
8048 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8049 }
8050 else if (it->multibyte_p)
8051 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8052 else
8053 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8054
8055 return success_p;
8056 }
8057
8058
8059 /* Set up IT to return characters from an ellipsis, if appropriate.
8060 The definition of the ellipsis glyphs may come from a display table
8061 entry. This function fills IT with the first glyph from the
8062 ellipsis if an ellipsis is to be displayed. */
8063
8064 static bool
8065 next_element_from_ellipsis (struct it *it)
8066 {
8067 if (it->selective_display_ellipsis_p)
8068 setup_for_ellipsis (it, it->len);
8069 else
8070 {
8071 /* The face at the current position may be different from the
8072 face we find after the invisible text. Remember what it
8073 was in IT->saved_face_id, and signal that it's there by
8074 setting face_before_selective_p. */
8075 it->saved_face_id = it->face_id;
8076 it->method = GET_FROM_BUFFER;
8077 it->object = it->w->contents;
8078 reseat_at_next_visible_line_start (it, true);
8079 it->face_before_selective_p = true;
8080 }
8081
8082 return GET_NEXT_DISPLAY_ELEMENT (it);
8083 }
8084
8085
8086 /* Deliver an image display element. The iterator IT is already
8087 filled with image information (done in handle_display_prop). Value
8088 is always true. */
8089
8090
8091 static bool
8092 next_element_from_image (struct it *it)
8093 {
8094 it->what = IT_IMAGE;
8095 return true;
8096 }
8097
8098 static bool
8099 next_element_from_xwidget (struct it *it)
8100 {
8101 it->what = IT_XWIDGET;
8102 return true;
8103 }
8104
8105
8106 /* Fill iterator IT with next display element from a stretch glyph
8107 property. IT->object is the value of the text property. Value is
8108 always true. */
8109
8110 static bool
8111 next_element_from_stretch (struct it *it)
8112 {
8113 it->what = IT_STRETCH;
8114 return true;
8115 }
8116
8117 /* Scan backwards from IT's current position until we find a stop
8118 position, or until BEGV. This is called when we find ourself
8119 before both the last known prev_stop and base_level_stop while
8120 reordering bidirectional text. */
8121
8122 static void
8123 compute_stop_pos_backwards (struct it *it)
8124 {
8125 const int SCAN_BACK_LIMIT = 1000;
8126 struct text_pos pos;
8127 struct display_pos save_current = it->current;
8128 struct text_pos save_position = it->position;
8129 ptrdiff_t charpos = IT_CHARPOS (*it);
8130 ptrdiff_t where_we_are = charpos;
8131 ptrdiff_t save_stop_pos = it->stop_charpos;
8132 ptrdiff_t save_end_pos = it->end_charpos;
8133
8134 eassert (NILP (it->string) && !it->s);
8135 eassert (it->bidi_p);
8136 it->bidi_p = false;
8137 do
8138 {
8139 it->end_charpos = min (charpos + 1, ZV);
8140 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8141 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8142 reseat_1 (it, pos, false);
8143 compute_stop_pos (it);
8144 /* We must advance forward, right? */
8145 if (it->stop_charpos <= charpos)
8146 emacs_abort ();
8147 }
8148 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8149
8150 if (it->stop_charpos <= where_we_are)
8151 it->prev_stop = it->stop_charpos;
8152 else
8153 it->prev_stop = BEGV;
8154 it->bidi_p = true;
8155 it->current = save_current;
8156 it->position = save_position;
8157 it->stop_charpos = save_stop_pos;
8158 it->end_charpos = save_end_pos;
8159 }
8160
8161 /* Scan forward from CHARPOS in the current buffer/string, until we
8162 find a stop position > current IT's position. Then handle the stop
8163 position before that. This is called when we bump into a stop
8164 position while reordering bidirectional text. CHARPOS should be
8165 the last previously processed stop_pos (or BEGV/0, if none were
8166 processed yet) whose position is less that IT's current
8167 position. */
8168
8169 static void
8170 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8171 {
8172 bool bufp = !STRINGP (it->string);
8173 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8174 struct display_pos save_current = it->current;
8175 struct text_pos save_position = it->position;
8176 struct text_pos pos1;
8177 ptrdiff_t next_stop;
8178
8179 /* Scan in strict logical order. */
8180 eassert (it->bidi_p);
8181 it->bidi_p = false;
8182 do
8183 {
8184 it->prev_stop = charpos;
8185 if (bufp)
8186 {
8187 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8188 reseat_1 (it, pos1, false);
8189 }
8190 else
8191 it->current.string_pos = string_pos (charpos, it->string);
8192 compute_stop_pos (it);
8193 /* We must advance forward, right? */
8194 if (it->stop_charpos <= it->prev_stop)
8195 emacs_abort ();
8196 charpos = it->stop_charpos;
8197 }
8198 while (charpos <= where_we_are);
8199
8200 it->bidi_p = true;
8201 it->current = save_current;
8202 it->position = save_position;
8203 next_stop = it->stop_charpos;
8204 it->stop_charpos = it->prev_stop;
8205 handle_stop (it);
8206 it->stop_charpos = next_stop;
8207 }
8208
8209 /* Load IT with the next display element from current_buffer. Value
8210 is false if end of buffer reached. IT->stop_charpos is the next
8211 position at which to stop and check for text properties or buffer
8212 end. */
8213
8214 static bool
8215 next_element_from_buffer (struct it *it)
8216 {
8217 bool success_p = true;
8218
8219 eassert (IT_CHARPOS (*it) >= BEGV);
8220 eassert (NILP (it->string) && !it->s);
8221 eassert (!it->bidi_p
8222 || (EQ (it->bidi_it.string.lstring, Qnil)
8223 && it->bidi_it.string.s == NULL));
8224
8225 /* With bidi reordering, the character to display might not be the
8226 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8227 we were reseat()ed to a new buffer position, which is potentially
8228 a different paragraph. */
8229 if (it->bidi_p && it->bidi_it.first_elt)
8230 {
8231 get_visually_first_element (it);
8232 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8233 }
8234
8235 if (IT_CHARPOS (*it) >= it->stop_charpos)
8236 {
8237 if (IT_CHARPOS (*it) >= it->end_charpos)
8238 {
8239 bool overlay_strings_follow_p;
8240
8241 /* End of the game, except when overlay strings follow that
8242 haven't been returned yet. */
8243 if (it->overlay_strings_at_end_processed_p)
8244 overlay_strings_follow_p = false;
8245 else
8246 {
8247 it->overlay_strings_at_end_processed_p = true;
8248 overlay_strings_follow_p = get_overlay_strings (it, 0);
8249 }
8250
8251 if (overlay_strings_follow_p)
8252 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8253 else
8254 {
8255 it->what = IT_EOB;
8256 it->position = it->current.pos;
8257 success_p = false;
8258 }
8259 }
8260 else if (!(!it->bidi_p
8261 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8262 || IT_CHARPOS (*it) == it->stop_charpos))
8263 {
8264 /* With bidi non-linear iteration, we could find ourselves
8265 far beyond the last computed stop_charpos, with several
8266 other stop positions in between that we missed. Scan
8267 them all now, in buffer's logical order, until we find
8268 and handle the last stop_charpos that precedes our
8269 current position. */
8270 handle_stop_backwards (it, it->stop_charpos);
8271 it->ignore_overlay_strings_at_pos_p = false;
8272 return GET_NEXT_DISPLAY_ELEMENT (it);
8273 }
8274 else
8275 {
8276 if (it->bidi_p)
8277 {
8278 /* Take note of the stop position we just moved across,
8279 for when we will move back across it. */
8280 it->prev_stop = it->stop_charpos;
8281 /* If we are at base paragraph embedding level, take
8282 note of the last stop position seen at this
8283 level. */
8284 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8285 it->base_level_stop = it->stop_charpos;
8286 }
8287 handle_stop (it);
8288 it->ignore_overlay_strings_at_pos_p = false;
8289 return GET_NEXT_DISPLAY_ELEMENT (it);
8290 }
8291 }
8292 else if (it->bidi_p
8293 /* If we are before prev_stop, we may have overstepped on
8294 our way backwards a stop_pos, and if so, we need to
8295 handle that stop_pos. */
8296 && IT_CHARPOS (*it) < it->prev_stop
8297 /* We can sometimes back up for reasons that have nothing
8298 to do with bidi reordering. E.g., compositions. The
8299 code below is only needed when we are above the base
8300 embedding level, so test for that explicitly. */
8301 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8302 {
8303 if (it->base_level_stop <= 0
8304 || IT_CHARPOS (*it) < it->base_level_stop)
8305 {
8306 /* If we lost track of base_level_stop, we need to find
8307 prev_stop by looking backwards. This happens, e.g., when
8308 we were reseated to the previous screenful of text by
8309 vertical-motion. */
8310 it->base_level_stop = BEGV;
8311 compute_stop_pos_backwards (it);
8312 handle_stop_backwards (it, it->prev_stop);
8313 }
8314 else
8315 handle_stop_backwards (it, it->base_level_stop);
8316 it->ignore_overlay_strings_at_pos_p = false;
8317 return GET_NEXT_DISPLAY_ELEMENT (it);
8318 }
8319 else
8320 {
8321 /* No face changes, overlays etc. in sight, so just return a
8322 character from current_buffer. */
8323 unsigned char *p;
8324 ptrdiff_t stop;
8325
8326 /* We moved to the next buffer position, so any info about
8327 previously seen overlays is no longer valid. */
8328 it->ignore_overlay_strings_at_pos_p = false;
8329
8330 /* Maybe run the redisplay end trigger hook. Performance note:
8331 This doesn't seem to cost measurable time. */
8332 if (it->redisplay_end_trigger_charpos
8333 && it->glyph_row
8334 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8335 run_redisplay_end_trigger_hook (it);
8336
8337 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8338 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8339 stop)
8340 && next_element_from_composition (it))
8341 {
8342 return true;
8343 }
8344
8345 /* Get the next character, maybe multibyte. */
8346 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8347 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8348 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8349 else
8350 it->c = *p, it->len = 1;
8351
8352 /* Record what we have and where it came from. */
8353 it->what = IT_CHARACTER;
8354 it->object = it->w->contents;
8355 it->position = it->current.pos;
8356
8357 /* Normally we return the character found above, except when we
8358 really want to return an ellipsis for selective display. */
8359 if (it->selective)
8360 {
8361 if (it->c == '\n')
8362 {
8363 /* A value of selective > 0 means hide lines indented more
8364 than that number of columns. */
8365 if (it->selective > 0
8366 && IT_CHARPOS (*it) + 1 < ZV
8367 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8368 IT_BYTEPOS (*it) + 1,
8369 it->selective))
8370 {
8371 success_p = next_element_from_ellipsis (it);
8372 it->dpvec_char_len = -1;
8373 }
8374 }
8375 else if (it->c == '\r' && it->selective == -1)
8376 {
8377 /* A value of selective == -1 means that everything from the
8378 CR to the end of the line is invisible, with maybe an
8379 ellipsis displayed for it. */
8380 success_p = next_element_from_ellipsis (it);
8381 it->dpvec_char_len = -1;
8382 }
8383 }
8384 }
8385
8386 /* Value is false if end of buffer reached. */
8387 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8388 return success_p;
8389 }
8390
8391
8392 /* Run the redisplay end trigger hook for IT. */
8393
8394 static void
8395 run_redisplay_end_trigger_hook (struct it *it)
8396 {
8397 /* IT->glyph_row should be non-null, i.e. we should be actually
8398 displaying something, or otherwise we should not run the hook. */
8399 eassert (it->glyph_row);
8400
8401 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8402 it->redisplay_end_trigger_charpos = 0;
8403
8404 /* Since we are *trying* to run these functions, don't try to run
8405 them again, even if they get an error. */
8406 wset_redisplay_end_trigger (it->w, Qnil);
8407 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8408 make_number (charpos));
8409
8410 /* Notice if it changed the face of the character we are on. */
8411 handle_face_prop (it);
8412 }
8413
8414
8415 /* Deliver a composition display element. Unlike the other
8416 next_element_from_XXX, this function is not registered in the array
8417 get_next_element[]. It is called from next_element_from_buffer and
8418 next_element_from_string when necessary. */
8419
8420 static bool
8421 next_element_from_composition (struct it *it)
8422 {
8423 it->what = IT_COMPOSITION;
8424 it->len = it->cmp_it.nbytes;
8425 if (STRINGP (it->string))
8426 {
8427 if (it->c < 0)
8428 {
8429 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8430 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8431 return false;
8432 }
8433 it->position = it->current.string_pos;
8434 it->object = it->string;
8435 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8436 IT_STRING_BYTEPOS (*it), it->string);
8437 }
8438 else
8439 {
8440 if (it->c < 0)
8441 {
8442 IT_CHARPOS (*it) += it->cmp_it.nchars;
8443 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8444 if (it->bidi_p)
8445 {
8446 if (it->bidi_it.new_paragraph)
8447 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8448 false);
8449 /* Resync the bidi iterator with IT's new position.
8450 FIXME: this doesn't support bidirectional text. */
8451 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8452 bidi_move_to_visually_next (&it->bidi_it);
8453 }
8454 return false;
8455 }
8456 it->position = it->current.pos;
8457 it->object = it->w->contents;
8458 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8459 IT_BYTEPOS (*it), Qnil);
8460 }
8461 return true;
8462 }
8463
8464
8465 \f
8466 /***********************************************************************
8467 Moving an iterator without producing glyphs
8468 ***********************************************************************/
8469
8470 /* Check if iterator is at a position corresponding to a valid buffer
8471 position after some move_it_ call. */
8472
8473 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8474 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8475
8476
8477 /* Move iterator IT to a specified buffer or X position within one
8478 line on the display without producing glyphs.
8479
8480 OP should be a bit mask including some or all of these bits:
8481 MOVE_TO_X: Stop upon reaching x-position TO_X.
8482 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8483 Regardless of OP's value, stop upon reaching the end of the display line.
8484
8485 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8486 This means, in particular, that TO_X includes window's horizontal
8487 scroll amount.
8488
8489 The return value has several possible values that
8490 say what condition caused the scan to stop:
8491
8492 MOVE_POS_MATCH_OR_ZV
8493 - when TO_POS or ZV was reached.
8494
8495 MOVE_X_REACHED
8496 -when TO_X was reached before TO_POS or ZV were reached.
8497
8498 MOVE_LINE_CONTINUED
8499 - when we reached the end of the display area and the line must
8500 be continued.
8501
8502 MOVE_LINE_TRUNCATED
8503 - when we reached the end of the display area and the line is
8504 truncated.
8505
8506 MOVE_NEWLINE_OR_CR
8507 - when we stopped at a line end, i.e. a newline or a CR and selective
8508 display is on. */
8509
8510 static enum move_it_result
8511 move_it_in_display_line_to (struct it *it,
8512 ptrdiff_t to_charpos, int to_x,
8513 enum move_operation_enum op)
8514 {
8515 enum move_it_result result = MOVE_UNDEFINED;
8516 struct glyph_row *saved_glyph_row;
8517 struct it wrap_it, atpos_it, atx_it, ppos_it;
8518 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8519 void *ppos_data = NULL;
8520 bool may_wrap = false;
8521 enum it_method prev_method = it->method;
8522 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8523 bool saw_smaller_pos = prev_pos < to_charpos;
8524
8525 /* Don't produce glyphs in produce_glyphs. */
8526 saved_glyph_row = it->glyph_row;
8527 it->glyph_row = NULL;
8528
8529 /* Use wrap_it to save a copy of IT wherever a word wrap could
8530 occur. Use atpos_it to save a copy of IT at the desired buffer
8531 position, if found, so that we can scan ahead and check if the
8532 word later overshoots the window edge. Use atx_it similarly, for
8533 pixel positions. */
8534 wrap_it.sp = -1;
8535 atpos_it.sp = -1;
8536 atx_it.sp = -1;
8537
8538 /* Use ppos_it under bidi reordering to save a copy of IT for the
8539 initial position. We restore that position in IT when we have
8540 scanned the entire display line without finding a match for
8541 TO_CHARPOS and all the character positions are greater than
8542 TO_CHARPOS. We then restart the scan from the initial position,
8543 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8544 the closest to TO_CHARPOS. */
8545 if (it->bidi_p)
8546 {
8547 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8548 {
8549 SAVE_IT (ppos_it, *it, ppos_data);
8550 closest_pos = IT_CHARPOS (*it);
8551 }
8552 else
8553 closest_pos = ZV;
8554 }
8555
8556 #define BUFFER_POS_REACHED_P() \
8557 ((op & MOVE_TO_POS) != 0 \
8558 && BUFFERP (it->object) \
8559 && (IT_CHARPOS (*it) == to_charpos \
8560 || ((!it->bidi_p \
8561 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8562 && IT_CHARPOS (*it) > to_charpos) \
8563 || (it->what == IT_COMPOSITION \
8564 && ((IT_CHARPOS (*it) > to_charpos \
8565 && to_charpos >= it->cmp_it.charpos) \
8566 || (IT_CHARPOS (*it) < to_charpos \
8567 && to_charpos <= it->cmp_it.charpos)))) \
8568 && (it->method == GET_FROM_BUFFER \
8569 || (it->method == GET_FROM_DISPLAY_VECTOR \
8570 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8571
8572 /* If there's a line-/wrap-prefix, handle it. */
8573 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8574 && it->current_y < it->last_visible_y)
8575 handle_line_prefix (it);
8576
8577 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8578 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8579
8580 while (true)
8581 {
8582 int x, i, ascent = 0, descent = 0;
8583
8584 /* Utility macro to reset an iterator with x, ascent, and descent. */
8585 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8586 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8587 (IT)->max_descent = descent)
8588
8589 /* Stop if we move beyond TO_CHARPOS (after an image or a
8590 display string or stretch glyph). */
8591 if ((op & MOVE_TO_POS) != 0
8592 && BUFFERP (it->object)
8593 && it->method == GET_FROM_BUFFER
8594 && (((!it->bidi_p
8595 /* When the iterator is at base embedding level, we
8596 are guaranteed that characters are delivered for
8597 display in strictly increasing order of their
8598 buffer positions. */
8599 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8600 && IT_CHARPOS (*it) > to_charpos)
8601 || (it->bidi_p
8602 && (prev_method == GET_FROM_IMAGE
8603 || prev_method == GET_FROM_STRETCH
8604 || prev_method == GET_FROM_STRING)
8605 /* Passed TO_CHARPOS from left to right. */
8606 && ((prev_pos < to_charpos
8607 && IT_CHARPOS (*it) > to_charpos)
8608 /* Passed TO_CHARPOS from right to left. */
8609 || (prev_pos > to_charpos
8610 && IT_CHARPOS (*it) < to_charpos)))))
8611 {
8612 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8613 {
8614 result = MOVE_POS_MATCH_OR_ZV;
8615 break;
8616 }
8617 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8618 /* If wrap_it is valid, the current position might be in a
8619 word that is wrapped. So, save the iterator in
8620 atpos_it and continue to see if wrapping happens. */
8621 SAVE_IT (atpos_it, *it, atpos_data);
8622 }
8623
8624 /* Stop when ZV reached.
8625 We used to stop here when TO_CHARPOS reached as well, but that is
8626 too soon if this glyph does not fit on this line. So we handle it
8627 explicitly below. */
8628 if (!get_next_display_element (it))
8629 {
8630 result = MOVE_POS_MATCH_OR_ZV;
8631 break;
8632 }
8633
8634 if (it->line_wrap == TRUNCATE)
8635 {
8636 if (BUFFER_POS_REACHED_P ())
8637 {
8638 result = MOVE_POS_MATCH_OR_ZV;
8639 break;
8640 }
8641 }
8642 else
8643 {
8644 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8645 {
8646 if (IT_DISPLAYING_WHITESPACE (it))
8647 may_wrap = true;
8648 else if (may_wrap)
8649 {
8650 /* We have reached a glyph that follows one or more
8651 whitespace characters. If the position is
8652 already found, we are done. */
8653 if (atpos_it.sp >= 0)
8654 {
8655 RESTORE_IT (it, &atpos_it, atpos_data);
8656 result = MOVE_POS_MATCH_OR_ZV;
8657 goto done;
8658 }
8659 if (atx_it.sp >= 0)
8660 {
8661 RESTORE_IT (it, &atx_it, atx_data);
8662 result = MOVE_X_REACHED;
8663 goto done;
8664 }
8665 /* Otherwise, we can wrap here. */
8666 SAVE_IT (wrap_it, *it, wrap_data);
8667 may_wrap = false;
8668 }
8669 }
8670 }
8671
8672 /* Remember the line height for the current line, in case
8673 the next element doesn't fit on the line. */
8674 ascent = it->max_ascent;
8675 descent = it->max_descent;
8676
8677 /* The call to produce_glyphs will get the metrics of the
8678 display element IT is loaded with. Record the x-position
8679 before this display element, in case it doesn't fit on the
8680 line. */
8681 x = it->current_x;
8682
8683 PRODUCE_GLYPHS (it);
8684
8685 if (it->area != TEXT_AREA)
8686 {
8687 prev_method = it->method;
8688 if (it->method == GET_FROM_BUFFER)
8689 prev_pos = IT_CHARPOS (*it);
8690 set_iterator_to_next (it, true);
8691 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8692 SET_TEXT_POS (this_line_min_pos,
8693 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8694 if (it->bidi_p
8695 && (op & MOVE_TO_POS)
8696 && IT_CHARPOS (*it) > to_charpos
8697 && IT_CHARPOS (*it) < closest_pos)
8698 closest_pos = IT_CHARPOS (*it);
8699 continue;
8700 }
8701
8702 /* The number of glyphs we get back in IT->nglyphs will normally
8703 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8704 character on a terminal frame, or (iii) a line end. For the
8705 second case, IT->nglyphs - 1 padding glyphs will be present.
8706 (On X frames, there is only one glyph produced for a
8707 composite character.)
8708
8709 The behavior implemented below means, for continuation lines,
8710 that as many spaces of a TAB as fit on the current line are
8711 displayed there. For terminal frames, as many glyphs of a
8712 multi-glyph character are displayed in the current line, too.
8713 This is what the old redisplay code did, and we keep it that
8714 way. Under X, the whole shape of a complex character must
8715 fit on the line or it will be completely displayed in the
8716 next line.
8717
8718 Note that both for tabs and padding glyphs, all glyphs have
8719 the same width. */
8720 if (it->nglyphs)
8721 {
8722 /* More than one glyph or glyph doesn't fit on line. All
8723 glyphs have the same width. */
8724 int single_glyph_width = it->pixel_width / it->nglyphs;
8725 int new_x;
8726 int x_before_this_char = x;
8727 int hpos_before_this_char = it->hpos;
8728
8729 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8730 {
8731 new_x = x + single_glyph_width;
8732
8733 /* We want to leave anything reaching TO_X to the caller. */
8734 if ((op & MOVE_TO_X) && new_x > to_x)
8735 {
8736 if (BUFFER_POS_REACHED_P ())
8737 {
8738 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8739 goto buffer_pos_reached;
8740 if (atpos_it.sp < 0)
8741 {
8742 SAVE_IT (atpos_it, *it, atpos_data);
8743 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8744 }
8745 }
8746 else
8747 {
8748 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8749 {
8750 it->current_x = x;
8751 result = MOVE_X_REACHED;
8752 break;
8753 }
8754 if (atx_it.sp < 0)
8755 {
8756 SAVE_IT (atx_it, *it, atx_data);
8757 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8758 }
8759 }
8760 }
8761
8762 if (/* Lines are continued. */
8763 it->line_wrap != TRUNCATE
8764 && (/* And glyph doesn't fit on the line. */
8765 new_x > it->last_visible_x
8766 /* Or it fits exactly and we're on a window
8767 system frame. */
8768 || (new_x == it->last_visible_x
8769 && FRAME_WINDOW_P (it->f)
8770 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8771 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8772 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8773 {
8774 if (/* IT->hpos == 0 means the very first glyph
8775 doesn't fit on the line, e.g. a wide image. */
8776 it->hpos == 0
8777 || (new_x == it->last_visible_x
8778 && FRAME_WINDOW_P (it->f)))
8779 {
8780 ++it->hpos;
8781 it->current_x = new_x;
8782
8783 /* The character's last glyph just barely fits
8784 in this row. */
8785 if (i == it->nglyphs - 1)
8786 {
8787 /* If this is the destination position,
8788 return a position *before* it in this row,
8789 now that we know it fits in this row. */
8790 if (BUFFER_POS_REACHED_P ())
8791 {
8792 if (it->line_wrap != WORD_WRAP
8793 || wrap_it.sp < 0
8794 /* If we've just found whitespace to
8795 wrap, effectively ignore the
8796 previous wrap point -- it is no
8797 longer relevant, but we won't
8798 have an opportunity to update it,
8799 since we've reached the edge of
8800 this screen line. */
8801 || (may_wrap
8802 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8803 {
8804 it->hpos = hpos_before_this_char;
8805 it->current_x = x_before_this_char;
8806 result = MOVE_POS_MATCH_OR_ZV;
8807 break;
8808 }
8809 if (it->line_wrap == WORD_WRAP
8810 && atpos_it.sp < 0)
8811 {
8812 SAVE_IT (atpos_it, *it, atpos_data);
8813 atpos_it.current_x = x_before_this_char;
8814 atpos_it.hpos = hpos_before_this_char;
8815 }
8816 }
8817
8818 prev_method = it->method;
8819 if (it->method == GET_FROM_BUFFER)
8820 prev_pos = IT_CHARPOS (*it);
8821 set_iterator_to_next (it, true);
8822 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8823 SET_TEXT_POS (this_line_min_pos,
8824 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8825 /* On graphical terminals, newlines may
8826 "overflow" into the fringe if
8827 overflow-newline-into-fringe is non-nil.
8828 On text terminals, and on graphical
8829 terminals with no right margin, newlines
8830 may overflow into the last glyph on the
8831 display line.*/
8832 if (!FRAME_WINDOW_P (it->f)
8833 || ((it->bidi_p
8834 && it->bidi_it.paragraph_dir == R2L)
8835 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8836 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8837 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8838 {
8839 if (!get_next_display_element (it))
8840 {
8841 result = MOVE_POS_MATCH_OR_ZV;
8842 break;
8843 }
8844 if (BUFFER_POS_REACHED_P ())
8845 {
8846 if (ITERATOR_AT_END_OF_LINE_P (it))
8847 result = MOVE_POS_MATCH_OR_ZV;
8848 else
8849 result = MOVE_LINE_CONTINUED;
8850 break;
8851 }
8852 if (ITERATOR_AT_END_OF_LINE_P (it)
8853 && (it->line_wrap != WORD_WRAP
8854 || wrap_it.sp < 0
8855 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8856 {
8857 result = MOVE_NEWLINE_OR_CR;
8858 break;
8859 }
8860 }
8861 }
8862 }
8863 else
8864 IT_RESET_X_ASCENT_DESCENT (it);
8865
8866 /* If the screen line ends with whitespace, and we
8867 are under word-wrap, don't use wrap_it: it is no
8868 longer relevant, but we won't have an opportunity
8869 to update it, since we are done with this screen
8870 line. */
8871 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8872 {
8873 /* If we've found TO_X, go back there, as we now
8874 know the last word fits on this screen line. */
8875 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8876 && atx_it.sp >= 0)
8877 {
8878 RESTORE_IT (it, &atx_it, atx_data);
8879 atpos_it.sp = -1;
8880 atx_it.sp = -1;
8881 result = MOVE_X_REACHED;
8882 break;
8883 }
8884 }
8885 else if (wrap_it.sp >= 0)
8886 {
8887 RESTORE_IT (it, &wrap_it, wrap_data);
8888 atpos_it.sp = -1;
8889 atx_it.sp = -1;
8890 }
8891
8892 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8893 IT_CHARPOS (*it)));
8894 result = MOVE_LINE_CONTINUED;
8895 break;
8896 }
8897
8898 if (BUFFER_POS_REACHED_P ())
8899 {
8900 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8901 goto buffer_pos_reached;
8902 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8903 {
8904 SAVE_IT (atpos_it, *it, atpos_data);
8905 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8906 }
8907 }
8908
8909 if (new_x > it->first_visible_x)
8910 {
8911 /* Glyph is visible. Increment number of glyphs that
8912 would be displayed. */
8913 ++it->hpos;
8914 }
8915 }
8916
8917 if (result != MOVE_UNDEFINED)
8918 break;
8919 }
8920 else if (BUFFER_POS_REACHED_P ())
8921 {
8922 buffer_pos_reached:
8923 IT_RESET_X_ASCENT_DESCENT (it);
8924 result = MOVE_POS_MATCH_OR_ZV;
8925 break;
8926 }
8927 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8928 {
8929 /* Stop when TO_X specified and reached. This check is
8930 necessary here because of lines consisting of a line end,
8931 only. The line end will not produce any glyphs and we
8932 would never get MOVE_X_REACHED. */
8933 eassert (it->nglyphs == 0);
8934 result = MOVE_X_REACHED;
8935 break;
8936 }
8937
8938 /* Is this a line end? If yes, we're done. */
8939 if (ITERATOR_AT_END_OF_LINE_P (it))
8940 {
8941 /* If we are past TO_CHARPOS, but never saw any character
8942 positions smaller than TO_CHARPOS, return
8943 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8944 did. */
8945 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8946 {
8947 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8948 {
8949 if (closest_pos < ZV)
8950 {
8951 RESTORE_IT (it, &ppos_it, ppos_data);
8952 /* Don't recurse if closest_pos is equal to
8953 to_charpos, since we have just tried that. */
8954 if (closest_pos != to_charpos)
8955 move_it_in_display_line_to (it, closest_pos, -1,
8956 MOVE_TO_POS);
8957 result = MOVE_POS_MATCH_OR_ZV;
8958 }
8959 else
8960 goto buffer_pos_reached;
8961 }
8962 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8963 && IT_CHARPOS (*it) > to_charpos)
8964 goto buffer_pos_reached;
8965 else
8966 result = MOVE_NEWLINE_OR_CR;
8967 }
8968 else
8969 result = MOVE_NEWLINE_OR_CR;
8970 break;
8971 }
8972
8973 prev_method = it->method;
8974 if (it->method == GET_FROM_BUFFER)
8975 prev_pos = IT_CHARPOS (*it);
8976 /* The current display element has been consumed. Advance
8977 to the next. */
8978 set_iterator_to_next (it, true);
8979 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8980 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8981 if (IT_CHARPOS (*it) < to_charpos)
8982 saw_smaller_pos = true;
8983 if (it->bidi_p
8984 && (op & MOVE_TO_POS)
8985 && IT_CHARPOS (*it) >= to_charpos
8986 && IT_CHARPOS (*it) < closest_pos)
8987 closest_pos = IT_CHARPOS (*it);
8988
8989 /* Stop if lines are truncated and IT's current x-position is
8990 past the right edge of the window now. */
8991 if (it->line_wrap == TRUNCATE
8992 && it->current_x >= it->last_visible_x)
8993 {
8994 if (!FRAME_WINDOW_P (it->f)
8995 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8996 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8997 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8998 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8999 {
9000 bool at_eob_p = false;
9001
9002 if ((at_eob_p = !get_next_display_element (it))
9003 || BUFFER_POS_REACHED_P ()
9004 /* If we are past TO_CHARPOS, but never saw any
9005 character positions smaller than TO_CHARPOS,
9006 return MOVE_POS_MATCH_OR_ZV, like the
9007 unidirectional display did. */
9008 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9009 && !saw_smaller_pos
9010 && IT_CHARPOS (*it) > to_charpos))
9011 {
9012 if (it->bidi_p
9013 && !BUFFER_POS_REACHED_P ()
9014 && !at_eob_p && closest_pos < ZV)
9015 {
9016 RESTORE_IT (it, &ppos_it, ppos_data);
9017 if (closest_pos != to_charpos)
9018 move_it_in_display_line_to (it, closest_pos, -1,
9019 MOVE_TO_POS);
9020 }
9021 result = MOVE_POS_MATCH_OR_ZV;
9022 break;
9023 }
9024 if (ITERATOR_AT_END_OF_LINE_P (it))
9025 {
9026 result = MOVE_NEWLINE_OR_CR;
9027 break;
9028 }
9029 }
9030 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9031 && !saw_smaller_pos
9032 && IT_CHARPOS (*it) > to_charpos)
9033 {
9034 if (closest_pos < ZV)
9035 {
9036 RESTORE_IT (it, &ppos_it, ppos_data);
9037 if (closest_pos != to_charpos)
9038 move_it_in_display_line_to (it, closest_pos, -1,
9039 MOVE_TO_POS);
9040 }
9041 result = MOVE_POS_MATCH_OR_ZV;
9042 break;
9043 }
9044 result = MOVE_LINE_TRUNCATED;
9045 break;
9046 }
9047 #undef IT_RESET_X_ASCENT_DESCENT
9048 }
9049
9050 #undef BUFFER_POS_REACHED_P
9051
9052 /* If we scanned beyond to_pos and didn't find a point to wrap at,
9053 restore the saved iterator. */
9054 if (atpos_it.sp >= 0)
9055 RESTORE_IT (it, &atpos_it, atpos_data);
9056 else if (atx_it.sp >= 0)
9057 RESTORE_IT (it, &atx_it, atx_data);
9058
9059 done:
9060
9061 if (atpos_data)
9062 bidi_unshelve_cache (atpos_data, true);
9063 if (atx_data)
9064 bidi_unshelve_cache (atx_data, true);
9065 if (wrap_data)
9066 bidi_unshelve_cache (wrap_data, true);
9067 if (ppos_data)
9068 bidi_unshelve_cache (ppos_data, true);
9069
9070 /* Restore the iterator settings altered at the beginning of this
9071 function. */
9072 it->glyph_row = saved_glyph_row;
9073 return result;
9074 }
9075
9076 /* For external use. */
9077 void
9078 move_it_in_display_line (struct it *it,
9079 ptrdiff_t to_charpos, int to_x,
9080 enum move_operation_enum op)
9081 {
9082 if (it->line_wrap == WORD_WRAP
9083 && (op & MOVE_TO_X))
9084 {
9085 struct it save_it;
9086 void *save_data = NULL;
9087 int skip;
9088
9089 SAVE_IT (save_it, *it, save_data);
9090 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9091 /* When word-wrap is on, TO_X may lie past the end
9092 of a wrapped line. Then it->current is the
9093 character on the next line, so backtrack to the
9094 space before the wrap point. */
9095 if (skip == MOVE_LINE_CONTINUED)
9096 {
9097 int prev_x = max (it->current_x - 1, 0);
9098 RESTORE_IT (it, &save_it, save_data);
9099 move_it_in_display_line_to
9100 (it, -1, prev_x, MOVE_TO_X);
9101 }
9102 else
9103 bidi_unshelve_cache (save_data, true);
9104 }
9105 else
9106 move_it_in_display_line_to (it, to_charpos, to_x, op);
9107 }
9108
9109
9110 /* Move IT forward until it satisfies one or more of the criteria in
9111 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9112
9113 OP is a bit-mask that specifies where to stop, and in particular,
9114 which of those four position arguments makes a difference. See the
9115 description of enum move_operation_enum.
9116
9117 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9118 screen line, this function will set IT to the next position that is
9119 displayed to the right of TO_CHARPOS on the screen.
9120
9121 Return the maximum pixel length of any line scanned but never more
9122 than it.last_visible_x. */
9123
9124 int
9125 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9126 {
9127 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9128 int line_height, line_start_x = 0, reached = 0;
9129 int max_current_x = 0;
9130 void *backup_data = NULL;
9131
9132 for (;;)
9133 {
9134 if (op & MOVE_TO_VPOS)
9135 {
9136 /* If no TO_CHARPOS and no TO_X specified, stop at the
9137 start of the line TO_VPOS. */
9138 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9139 {
9140 if (it->vpos == to_vpos)
9141 {
9142 reached = 1;
9143 break;
9144 }
9145 else
9146 skip = move_it_in_display_line_to (it, -1, -1, 0);
9147 }
9148 else
9149 {
9150 /* TO_VPOS >= 0 means stop at TO_X in the line at
9151 TO_VPOS, or at TO_POS, whichever comes first. */
9152 if (it->vpos == to_vpos)
9153 {
9154 reached = 2;
9155 break;
9156 }
9157
9158 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9159
9160 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9161 {
9162 reached = 3;
9163 break;
9164 }
9165 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9166 {
9167 /* We have reached TO_X but not in the line we want. */
9168 skip = move_it_in_display_line_to (it, to_charpos,
9169 -1, MOVE_TO_POS);
9170 if (skip == MOVE_POS_MATCH_OR_ZV)
9171 {
9172 reached = 4;
9173 break;
9174 }
9175 }
9176 }
9177 }
9178 else if (op & MOVE_TO_Y)
9179 {
9180 struct it it_backup;
9181
9182 if (it->line_wrap == WORD_WRAP)
9183 SAVE_IT (it_backup, *it, backup_data);
9184
9185 /* TO_Y specified means stop at TO_X in the line containing
9186 TO_Y---or at TO_CHARPOS if this is reached first. The
9187 problem is that we can't really tell whether the line
9188 contains TO_Y before we have completely scanned it, and
9189 this may skip past TO_X. What we do is to first scan to
9190 TO_X.
9191
9192 If TO_X is not specified, use a TO_X of zero. The reason
9193 is to make the outcome of this function more predictable.
9194 If we didn't use TO_X == 0, we would stop at the end of
9195 the line which is probably not what a caller would expect
9196 to happen. */
9197 skip = move_it_in_display_line_to
9198 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9199 (MOVE_TO_X | (op & MOVE_TO_POS)));
9200
9201 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9202 if (skip == MOVE_POS_MATCH_OR_ZV)
9203 reached = 5;
9204 else if (skip == MOVE_X_REACHED)
9205 {
9206 /* If TO_X was reached, we want to know whether TO_Y is
9207 in the line. We know this is the case if the already
9208 scanned glyphs make the line tall enough. Otherwise,
9209 we must check by scanning the rest of the line. */
9210 line_height = it->max_ascent + it->max_descent;
9211 if (to_y >= it->current_y
9212 && to_y < it->current_y + line_height)
9213 {
9214 reached = 6;
9215 break;
9216 }
9217 SAVE_IT (it_backup, *it, backup_data);
9218 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9219 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9220 op & MOVE_TO_POS);
9221 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9222 line_height = it->max_ascent + it->max_descent;
9223 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9224
9225 if (to_y >= it->current_y
9226 && to_y < it->current_y + line_height)
9227 {
9228 /* If TO_Y is in this line and TO_X was reached
9229 above, we scanned too far. We have to restore
9230 IT's settings to the ones before skipping. But
9231 keep the more accurate values of max_ascent and
9232 max_descent we've found while skipping the rest
9233 of the line, for the sake of callers, such as
9234 pos_visible_p, that need to know the line
9235 height. */
9236 int max_ascent = it->max_ascent;
9237 int max_descent = it->max_descent;
9238
9239 RESTORE_IT (it, &it_backup, backup_data);
9240 it->max_ascent = max_ascent;
9241 it->max_descent = max_descent;
9242 reached = 6;
9243 }
9244 else
9245 {
9246 skip = skip2;
9247 if (skip == MOVE_POS_MATCH_OR_ZV)
9248 reached = 7;
9249 }
9250 }
9251 else
9252 {
9253 /* Check whether TO_Y is in this line. */
9254 line_height = it->max_ascent + it->max_descent;
9255 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9256
9257 if (to_y >= it->current_y
9258 && to_y < it->current_y + line_height)
9259 {
9260 if (to_y > it->current_y)
9261 max_current_x = max (it->current_x, max_current_x);
9262
9263 /* When word-wrap is on, TO_X may lie past the end
9264 of a wrapped line. Then it->current is the
9265 character on the next line, so backtrack to the
9266 space before the wrap point. */
9267 if (skip == MOVE_LINE_CONTINUED
9268 && it->line_wrap == WORD_WRAP)
9269 {
9270 int prev_x = max (it->current_x - 1, 0);
9271 RESTORE_IT (it, &it_backup, backup_data);
9272 skip = move_it_in_display_line_to
9273 (it, -1, prev_x, MOVE_TO_X);
9274 }
9275
9276 reached = 6;
9277 }
9278 }
9279
9280 if (reached)
9281 {
9282 max_current_x = max (it->current_x, max_current_x);
9283 break;
9284 }
9285 }
9286 else if (BUFFERP (it->object)
9287 && (it->method == GET_FROM_BUFFER
9288 || it->method == GET_FROM_STRETCH)
9289 && IT_CHARPOS (*it) >= to_charpos
9290 /* Under bidi iteration, a call to set_iterator_to_next
9291 can scan far beyond to_charpos if the initial
9292 portion of the next line needs to be reordered. In
9293 that case, give move_it_in_display_line_to another
9294 chance below. */
9295 && !(it->bidi_p
9296 && it->bidi_it.scan_dir == -1))
9297 skip = MOVE_POS_MATCH_OR_ZV;
9298 else
9299 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9300
9301 switch (skip)
9302 {
9303 case MOVE_POS_MATCH_OR_ZV:
9304 max_current_x = max (it->current_x, max_current_x);
9305 reached = 8;
9306 goto out;
9307
9308 case MOVE_NEWLINE_OR_CR:
9309 max_current_x = max (it->current_x, max_current_x);
9310 set_iterator_to_next (it, true);
9311 it->continuation_lines_width = 0;
9312 break;
9313
9314 case MOVE_LINE_TRUNCATED:
9315 max_current_x = it->last_visible_x;
9316 it->continuation_lines_width = 0;
9317 reseat_at_next_visible_line_start (it, false);
9318 if ((op & MOVE_TO_POS) != 0
9319 && IT_CHARPOS (*it) > to_charpos)
9320 {
9321 reached = 9;
9322 goto out;
9323 }
9324 break;
9325
9326 case MOVE_LINE_CONTINUED:
9327 max_current_x = it->last_visible_x;
9328 /* For continued lines ending in a tab, some of the glyphs
9329 associated with the tab are displayed on the current
9330 line. Since it->current_x does not include these glyphs,
9331 we use it->last_visible_x instead. */
9332 if (it->c == '\t')
9333 {
9334 it->continuation_lines_width += it->last_visible_x;
9335 /* When moving by vpos, ensure that the iterator really
9336 advances to the next line (bug#847, bug#969). Fixme:
9337 do we need to do this in other circumstances? */
9338 if (it->current_x != it->last_visible_x
9339 && (op & MOVE_TO_VPOS)
9340 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9341 {
9342 line_start_x = it->current_x + it->pixel_width
9343 - it->last_visible_x;
9344 if (FRAME_WINDOW_P (it->f))
9345 {
9346 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9347 struct font *face_font = face->font;
9348
9349 /* When display_line produces a continued line
9350 that ends in a TAB, it skips a tab stop that
9351 is closer than the font's space character
9352 width (see x_produce_glyphs where it produces
9353 the stretch glyph which represents a TAB).
9354 We need to reproduce the same logic here. */
9355 eassert (face_font);
9356 if (face_font)
9357 {
9358 if (line_start_x < face_font->space_width)
9359 line_start_x
9360 += it->tab_width * face_font->space_width;
9361 }
9362 }
9363 set_iterator_to_next (it, false);
9364 }
9365 }
9366 else
9367 it->continuation_lines_width += it->current_x;
9368 break;
9369
9370 default:
9371 emacs_abort ();
9372 }
9373
9374 /* Reset/increment for the next run. */
9375 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9376 it->current_x = line_start_x;
9377 line_start_x = 0;
9378 it->hpos = 0;
9379 it->current_y += it->max_ascent + it->max_descent;
9380 ++it->vpos;
9381 last_height = it->max_ascent + it->max_descent;
9382 it->max_ascent = it->max_descent = 0;
9383 }
9384
9385 out:
9386
9387 /* On text terminals, we may stop at the end of a line in the middle
9388 of a multi-character glyph. If the glyph itself is continued,
9389 i.e. it is actually displayed on the next line, don't treat this
9390 stopping point as valid; move to the next line instead (unless
9391 that brings us offscreen). */
9392 if (!FRAME_WINDOW_P (it->f)
9393 && op & MOVE_TO_POS
9394 && IT_CHARPOS (*it) == to_charpos
9395 && it->what == IT_CHARACTER
9396 && it->nglyphs > 1
9397 && it->line_wrap == WINDOW_WRAP
9398 && it->current_x == it->last_visible_x - 1
9399 && it->c != '\n'
9400 && it->c != '\t'
9401 && it->w->window_end_valid
9402 && it->vpos < it->w->window_end_vpos)
9403 {
9404 it->continuation_lines_width += it->current_x;
9405 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9406 it->current_y += it->max_ascent + it->max_descent;
9407 ++it->vpos;
9408 last_height = it->max_ascent + it->max_descent;
9409 }
9410
9411 if (backup_data)
9412 bidi_unshelve_cache (backup_data, true);
9413
9414 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9415
9416 return max_current_x;
9417 }
9418
9419
9420 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9421
9422 If DY > 0, move IT backward at least that many pixels. DY = 0
9423 means move IT backward to the preceding line start or BEGV. This
9424 function may move over more than DY pixels if IT->current_y - DY
9425 ends up in the middle of a line; in this case IT->current_y will be
9426 set to the top of the line moved to. */
9427
9428 void
9429 move_it_vertically_backward (struct it *it, int dy)
9430 {
9431 int nlines, h;
9432 struct it it2, it3;
9433 void *it2data = NULL, *it3data = NULL;
9434 ptrdiff_t start_pos;
9435 int nchars_per_row
9436 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9437 ptrdiff_t pos_limit;
9438
9439 move_further_back:
9440 eassert (dy >= 0);
9441
9442 start_pos = IT_CHARPOS (*it);
9443
9444 /* Estimate how many newlines we must move back. */
9445 nlines = max (1, dy / default_line_pixel_height (it->w));
9446 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9447 pos_limit = BEGV;
9448 else
9449 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9450
9451 /* Set the iterator's position that many lines back. But don't go
9452 back more than NLINES full screen lines -- this wins a day with
9453 buffers which have very long lines. */
9454 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9455 back_to_previous_visible_line_start (it);
9456
9457 /* Reseat the iterator here. When moving backward, we don't want
9458 reseat to skip forward over invisible text, set up the iterator
9459 to deliver from overlay strings at the new position etc. So,
9460 use reseat_1 here. */
9461 reseat_1 (it, it->current.pos, true);
9462
9463 /* We are now surely at a line start. */
9464 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9465 reordering is in effect. */
9466 it->continuation_lines_width = 0;
9467
9468 /* Move forward and see what y-distance we moved. First move to the
9469 start of the next line so that we get its height. We need this
9470 height to be able to tell whether we reached the specified
9471 y-distance. */
9472 SAVE_IT (it2, *it, it2data);
9473 it2.max_ascent = it2.max_descent = 0;
9474 do
9475 {
9476 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9477 MOVE_TO_POS | MOVE_TO_VPOS);
9478 }
9479 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9480 /* If we are in a display string which starts at START_POS,
9481 and that display string includes a newline, and we are
9482 right after that newline (i.e. at the beginning of a
9483 display line), exit the loop, because otherwise we will
9484 infloop, since move_it_to will see that it is already at
9485 START_POS and will not move. */
9486 || (it2.method == GET_FROM_STRING
9487 && IT_CHARPOS (it2) == start_pos
9488 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9489 eassert (IT_CHARPOS (*it) >= BEGV);
9490 SAVE_IT (it3, it2, it3data);
9491
9492 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9493 eassert (IT_CHARPOS (*it) >= BEGV);
9494 /* H is the actual vertical distance from the position in *IT
9495 and the starting position. */
9496 h = it2.current_y - it->current_y;
9497 /* NLINES is the distance in number of lines. */
9498 nlines = it2.vpos - it->vpos;
9499
9500 /* Correct IT's y and vpos position
9501 so that they are relative to the starting point. */
9502 it->vpos -= nlines;
9503 it->current_y -= h;
9504
9505 if (dy == 0)
9506 {
9507 /* DY == 0 means move to the start of the screen line. The
9508 value of nlines is > 0 if continuation lines were involved,
9509 or if the original IT position was at start of a line. */
9510 RESTORE_IT (it, it, it2data);
9511 if (nlines > 0)
9512 move_it_by_lines (it, nlines);
9513 /* The above code moves us to some position NLINES down,
9514 usually to its first glyph (leftmost in an L2R line), but
9515 that's not necessarily the start of the line, under bidi
9516 reordering. We want to get to the character position
9517 that is immediately after the newline of the previous
9518 line. */
9519 if (it->bidi_p
9520 && !it->continuation_lines_width
9521 && !STRINGP (it->string)
9522 && IT_CHARPOS (*it) > BEGV
9523 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9524 {
9525 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9526
9527 DEC_BOTH (cp, bp);
9528 cp = find_newline_no_quit (cp, bp, -1, NULL);
9529 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9530 }
9531 bidi_unshelve_cache (it3data, true);
9532 }
9533 else
9534 {
9535 /* The y-position we try to reach, relative to *IT.
9536 Note that H has been subtracted in front of the if-statement. */
9537 int target_y = it->current_y + h - dy;
9538 int y0 = it3.current_y;
9539 int y1;
9540 int line_height;
9541
9542 RESTORE_IT (&it3, &it3, it3data);
9543 y1 = line_bottom_y (&it3);
9544 line_height = y1 - y0;
9545 RESTORE_IT (it, it, it2data);
9546 /* If we did not reach target_y, try to move further backward if
9547 we can. If we moved too far backward, try to move forward. */
9548 if (target_y < it->current_y
9549 /* This is heuristic. In a window that's 3 lines high, with
9550 a line height of 13 pixels each, recentering with point
9551 on the bottom line will try to move -39/2 = 19 pixels
9552 backward. Try to avoid moving into the first line. */
9553 && (it->current_y - target_y
9554 > min (window_box_height (it->w), line_height * 2 / 3))
9555 && IT_CHARPOS (*it) > BEGV)
9556 {
9557 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9558 target_y - it->current_y));
9559 dy = it->current_y - target_y;
9560 goto move_further_back;
9561 }
9562 else if (target_y >= it->current_y + line_height
9563 && IT_CHARPOS (*it) < ZV)
9564 {
9565 /* Should move forward by at least one line, maybe more.
9566
9567 Note: Calling move_it_by_lines can be expensive on
9568 terminal frames, where compute_motion is used (via
9569 vmotion) to do the job, when there are very long lines
9570 and truncate-lines is nil. That's the reason for
9571 treating terminal frames specially here. */
9572
9573 if (!FRAME_WINDOW_P (it->f))
9574 move_it_vertically (it, target_y - it->current_y);
9575 else
9576 {
9577 do
9578 {
9579 move_it_by_lines (it, 1);
9580 }
9581 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9582 }
9583 }
9584 }
9585 }
9586
9587
9588 /* Move IT by a specified amount of pixel lines DY. DY negative means
9589 move backwards. DY = 0 means move to start of screen line. At the
9590 end, IT will be on the start of a screen line. */
9591
9592 void
9593 move_it_vertically (struct it *it, int dy)
9594 {
9595 if (dy <= 0)
9596 move_it_vertically_backward (it, -dy);
9597 else
9598 {
9599 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9600 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9601 MOVE_TO_POS | MOVE_TO_Y);
9602 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9603
9604 /* If buffer ends in ZV without a newline, move to the start of
9605 the line to satisfy the post-condition. */
9606 if (IT_CHARPOS (*it) == ZV
9607 && ZV > BEGV
9608 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9609 move_it_by_lines (it, 0);
9610 }
9611 }
9612
9613
9614 /* Move iterator IT past the end of the text line it is in. */
9615
9616 void
9617 move_it_past_eol (struct it *it)
9618 {
9619 enum move_it_result rc;
9620
9621 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9622 if (rc == MOVE_NEWLINE_OR_CR)
9623 set_iterator_to_next (it, false);
9624 }
9625
9626
9627 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9628 negative means move up. DVPOS == 0 means move to the start of the
9629 screen line.
9630
9631 Optimization idea: If we would know that IT->f doesn't use
9632 a face with proportional font, we could be faster for
9633 truncate-lines nil. */
9634
9635 void
9636 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9637 {
9638
9639 /* The commented-out optimization uses vmotion on terminals. This
9640 gives bad results, because elements like it->what, on which
9641 callers such as pos_visible_p rely, aren't updated. */
9642 /* struct position pos;
9643 if (!FRAME_WINDOW_P (it->f))
9644 {
9645 struct text_pos textpos;
9646
9647 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9648 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9649 reseat (it, textpos, true);
9650 it->vpos += pos.vpos;
9651 it->current_y += pos.vpos;
9652 }
9653 else */
9654
9655 if (dvpos == 0)
9656 {
9657 /* DVPOS == 0 means move to the start of the screen line. */
9658 move_it_vertically_backward (it, 0);
9659 /* Let next call to line_bottom_y calculate real line height. */
9660 last_height = 0;
9661 }
9662 else if (dvpos > 0)
9663 {
9664 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9665 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9666 {
9667 /* Only move to the next buffer position if we ended up in a
9668 string from display property, not in an overlay string
9669 (before-string or after-string). That is because the
9670 latter don't conceal the underlying buffer position, so
9671 we can ask to move the iterator to the exact position we
9672 are interested in. Note that, even if we are already at
9673 IT_CHARPOS (*it), the call below is not a no-op, as it
9674 will detect that we are at the end of the string, pop the
9675 iterator, and compute it->current_x and it->hpos
9676 correctly. */
9677 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9678 -1, -1, -1, MOVE_TO_POS);
9679 }
9680 }
9681 else
9682 {
9683 struct it it2;
9684 void *it2data = NULL;
9685 ptrdiff_t start_charpos, i;
9686 int nchars_per_row
9687 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9688 bool hit_pos_limit = false;
9689 ptrdiff_t pos_limit;
9690
9691 /* Start at the beginning of the screen line containing IT's
9692 position. This may actually move vertically backwards,
9693 in case of overlays, so adjust dvpos accordingly. */
9694 dvpos += it->vpos;
9695 move_it_vertically_backward (it, 0);
9696 dvpos -= it->vpos;
9697
9698 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9699 screen lines, and reseat the iterator there. */
9700 start_charpos = IT_CHARPOS (*it);
9701 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9702 pos_limit = BEGV;
9703 else
9704 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9705
9706 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9707 back_to_previous_visible_line_start (it);
9708 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9709 hit_pos_limit = true;
9710 reseat (it, it->current.pos, true);
9711
9712 /* Move further back if we end up in a string or an image. */
9713 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9714 {
9715 /* First try to move to start of display line. */
9716 dvpos += it->vpos;
9717 move_it_vertically_backward (it, 0);
9718 dvpos -= it->vpos;
9719 if (IT_POS_VALID_AFTER_MOVE_P (it))
9720 break;
9721 /* If start of line is still in string or image,
9722 move further back. */
9723 back_to_previous_visible_line_start (it);
9724 reseat (it, it->current.pos, true);
9725 dvpos--;
9726 }
9727
9728 it->current_x = it->hpos = 0;
9729
9730 /* Above call may have moved too far if continuation lines
9731 are involved. Scan forward and see if it did. */
9732 SAVE_IT (it2, *it, it2data);
9733 it2.vpos = it2.current_y = 0;
9734 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9735 it->vpos -= it2.vpos;
9736 it->current_y -= it2.current_y;
9737 it->current_x = it->hpos = 0;
9738
9739 /* If we moved too far back, move IT some lines forward. */
9740 if (it2.vpos > -dvpos)
9741 {
9742 int delta = it2.vpos + dvpos;
9743
9744 RESTORE_IT (&it2, &it2, it2data);
9745 SAVE_IT (it2, *it, it2data);
9746 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9747 /* Move back again if we got too far ahead. */
9748 if (IT_CHARPOS (*it) >= start_charpos)
9749 RESTORE_IT (it, &it2, it2data);
9750 else
9751 bidi_unshelve_cache (it2data, true);
9752 }
9753 else if (hit_pos_limit && pos_limit > BEGV
9754 && dvpos < 0 && it2.vpos < -dvpos)
9755 {
9756 /* If we hit the limit, but still didn't make it far enough
9757 back, that means there's a display string with a newline
9758 covering a large chunk of text, and that caused
9759 back_to_previous_visible_line_start try to go too far.
9760 Punish those who commit such atrocities by going back
9761 until we've reached DVPOS, after lifting the limit, which
9762 could make it slow for very long lines. "If it hurts,
9763 don't do that!" */
9764 dvpos += it2.vpos;
9765 RESTORE_IT (it, it, it2data);
9766 for (i = -dvpos; i > 0; --i)
9767 {
9768 back_to_previous_visible_line_start (it);
9769 it->vpos--;
9770 }
9771 reseat_1 (it, it->current.pos, true);
9772 }
9773 else
9774 RESTORE_IT (it, it, it2data);
9775 }
9776 }
9777
9778 /* Return true if IT points into the middle of a display vector. */
9779
9780 bool
9781 in_display_vector_p (struct it *it)
9782 {
9783 return (it->method == GET_FROM_DISPLAY_VECTOR
9784 && it->current.dpvec_index > 0
9785 && it->dpvec + it->current.dpvec_index != it->dpend);
9786 }
9787
9788 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9789 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9790 WINDOW must be a live window and defaults to the selected one. The
9791 return value is a cons of the maximum pixel-width of any text line and
9792 the maximum pixel-height of all text lines.
9793
9794 The optional argument FROM, if non-nil, specifies the first text
9795 position and defaults to the minimum accessible position of the buffer.
9796 If FROM is t, use the minimum accessible position that is not a newline
9797 character. TO, if non-nil, specifies the last text position and
9798 defaults to the maximum accessible position of the buffer. If TO is t,
9799 use the maximum accessible position that is not a newline character.
9800
9801 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9802 width that can be returned. X-LIMIT nil or omitted, means to use the
9803 pixel-width of WINDOW's body; use this if you do not intend to change
9804 the width of WINDOW. Use the maximum width WINDOW may assume if you
9805 intend to change WINDOW's width. In any case, text whose x-coordinate
9806 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9807 can take some time, it's always a good idea to make this argument as
9808 small as possible; in particular, if the buffer contains long lines that
9809 shall be truncated anyway.
9810
9811 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9812 height that can be returned. Text lines whose y-coordinate is beyond
9813 Y-LIMIT are ignored. Since calculating the text height of a large
9814 buffer can take some time, it makes sense to specify this argument if
9815 the size of the buffer is unknown.
9816
9817 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9818 include the height of the mode- or header-line of WINDOW in the return
9819 value. If it is either the symbol `mode-line' or `header-line', include
9820 only the height of that line, if present, in the return value. If t,
9821 include the height of both, if present, in the return value. */)
9822 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9823 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9824 {
9825 struct window *w = decode_live_window (window);
9826 Lisp_Object buffer = w->contents;
9827 struct buffer *b;
9828 struct it it;
9829 struct buffer *old_b = NULL;
9830 ptrdiff_t start, end, pos;
9831 struct text_pos startp;
9832 void *itdata = NULL;
9833 int c, max_y = -1, x = 0, y = 0;
9834
9835 CHECK_BUFFER (buffer);
9836 b = XBUFFER (buffer);
9837
9838 if (b != current_buffer)
9839 {
9840 old_b = current_buffer;
9841 set_buffer_internal (b);
9842 }
9843
9844 if (NILP (from))
9845 start = BEGV;
9846 else if (EQ (from, Qt))
9847 {
9848 start = pos = BEGV;
9849 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9850 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9851 start = pos;
9852 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9853 start = pos;
9854 }
9855 else
9856 {
9857 CHECK_NUMBER_COERCE_MARKER (from);
9858 start = min (max (XINT (from), BEGV), ZV);
9859 }
9860
9861 if (NILP (to))
9862 end = ZV;
9863 else if (EQ (to, Qt))
9864 {
9865 end = pos = ZV;
9866 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9867 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9868 end = pos;
9869 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9870 end = pos;
9871 }
9872 else
9873 {
9874 CHECK_NUMBER_COERCE_MARKER (to);
9875 end = max (start, min (XINT (to), ZV));
9876 }
9877
9878 if (!NILP (y_limit))
9879 {
9880 CHECK_NUMBER (y_limit);
9881 max_y = min (XINT (y_limit), INT_MAX);
9882 }
9883
9884 itdata = bidi_shelve_cache ();
9885 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9886 start_display (&it, w, startp);
9887
9888 if (NILP (x_limit))
9889 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9890 else
9891 {
9892 CHECK_NUMBER (x_limit);
9893 it.last_visible_x = min (XINT (x_limit), INFINITY);
9894 /* Actually, we never want move_it_to stop at to_x. But to make
9895 sure that move_it_in_display_line_to always moves far enough,
9896 we set it to INT_MAX and specify MOVE_TO_X. */
9897 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9898 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9899 }
9900
9901 y = it.current_y + it.max_ascent + it.max_descent;
9902
9903 if (!EQ (mode_and_header_line, Qheader_line)
9904 && !EQ (mode_and_header_line, Qt))
9905 /* Do not count the header-line which was counted automatically by
9906 start_display. */
9907 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9908
9909 if (EQ (mode_and_header_line, Qmode_line)
9910 || EQ (mode_and_header_line, Qt))
9911 /* Do count the mode-line which is not included automatically by
9912 start_display. */
9913 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9914
9915 bidi_unshelve_cache (itdata, false);
9916
9917 if (old_b)
9918 set_buffer_internal (old_b);
9919
9920 return Fcons (make_number (x), make_number (y));
9921 }
9922 \f
9923 /***********************************************************************
9924 Messages
9925 ***********************************************************************/
9926
9927 /* Return the number of arguments the format string FORMAT needs. */
9928
9929 static ptrdiff_t
9930 format_nargs (char const *format)
9931 {
9932 ptrdiff_t nargs = 0;
9933 for (char const *p = format; (p = strchr (p, '%')); p++)
9934 if (p[1] == '%')
9935 p++;
9936 else
9937 nargs++;
9938 return nargs;
9939 }
9940
9941 /* Add a message with format string FORMAT and formatted arguments
9942 to *Messages*. */
9943
9944 void
9945 add_to_log (const char *format, ...)
9946 {
9947 va_list ap;
9948 va_start (ap, format);
9949 vadd_to_log (format, ap);
9950 va_end (ap);
9951 }
9952
9953 void
9954 vadd_to_log (char const *format, va_list ap)
9955 {
9956 ptrdiff_t form_nargs = format_nargs (format);
9957 ptrdiff_t nargs = 1 + form_nargs;
9958 Lisp_Object args[10];
9959 eassert (nargs <= ARRAYELTS (args));
9960 AUTO_STRING (args0, format);
9961 args[0] = args0;
9962 for (ptrdiff_t i = 1; i <= nargs; i++)
9963 args[i] = va_arg (ap, Lisp_Object);
9964 Lisp_Object msg = Qnil;
9965 msg = Fformat_message (nargs, args);
9966
9967 ptrdiff_t len = SBYTES (msg) + 1;
9968 USE_SAFE_ALLOCA;
9969 char *buffer = SAFE_ALLOCA (len);
9970 memcpy (buffer, SDATA (msg), len);
9971
9972 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
9973 SAFE_FREE ();
9974 }
9975
9976
9977 /* Output a newline in the *Messages* buffer if "needs" one. */
9978
9979 void
9980 message_log_maybe_newline (void)
9981 {
9982 if (message_log_need_newline)
9983 message_dolog ("", 0, true, false);
9984 }
9985
9986
9987 /* Add a string M of length NBYTES to the message log, optionally
9988 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9989 true, means interpret the contents of M as multibyte. This
9990 function calls low-level routines in order to bypass text property
9991 hooks, etc. which might not be safe to run.
9992
9993 This may GC (insert may run before/after change hooks),
9994 so the buffer M must NOT point to a Lisp string. */
9995
9996 void
9997 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9998 {
9999 const unsigned char *msg = (const unsigned char *) m;
10000
10001 if (!NILP (Vmemory_full))
10002 return;
10003
10004 if (!NILP (Vmessage_log_max))
10005 {
10006 struct buffer *oldbuf;
10007 Lisp_Object oldpoint, oldbegv, oldzv;
10008 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10009 ptrdiff_t point_at_end = 0;
10010 ptrdiff_t zv_at_end = 0;
10011 Lisp_Object old_deactivate_mark;
10012
10013 old_deactivate_mark = Vdeactivate_mark;
10014 oldbuf = current_buffer;
10015
10016 /* Ensure the Messages buffer exists, and switch to it.
10017 If we created it, set the major-mode. */
10018 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10019 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10020 if (newbuffer
10021 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10022 call0 (intern ("messages-buffer-mode"));
10023
10024 bset_undo_list (current_buffer, Qt);
10025 bset_cache_long_scans (current_buffer, Qnil);
10026
10027 oldpoint = message_dolog_marker1;
10028 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10029 oldbegv = message_dolog_marker2;
10030 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10031 oldzv = message_dolog_marker3;
10032 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10033
10034 if (PT == Z)
10035 point_at_end = 1;
10036 if (ZV == Z)
10037 zv_at_end = 1;
10038
10039 BEGV = BEG;
10040 BEGV_BYTE = BEG_BYTE;
10041 ZV = Z;
10042 ZV_BYTE = Z_BYTE;
10043 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10044
10045 /* Insert the string--maybe converting multibyte to single byte
10046 or vice versa, so that all the text fits the buffer. */
10047 if (multibyte
10048 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10049 {
10050 ptrdiff_t i;
10051 int c, char_bytes;
10052 char work[1];
10053
10054 /* Convert a multibyte string to single-byte
10055 for the *Message* buffer. */
10056 for (i = 0; i < nbytes; i += char_bytes)
10057 {
10058 c = string_char_and_length (msg + i, &char_bytes);
10059 work[0] = CHAR_TO_BYTE8 (c);
10060 insert_1_both (work, 1, 1, true, false, false);
10061 }
10062 }
10063 else if (! multibyte
10064 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10065 {
10066 ptrdiff_t i;
10067 int c, char_bytes;
10068 unsigned char str[MAX_MULTIBYTE_LENGTH];
10069 /* Convert a single-byte string to multibyte
10070 for the *Message* buffer. */
10071 for (i = 0; i < nbytes; i++)
10072 {
10073 c = msg[i];
10074 MAKE_CHAR_MULTIBYTE (c);
10075 char_bytes = CHAR_STRING (c, str);
10076 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10077 }
10078 }
10079 else if (nbytes)
10080 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10081 true, false, false);
10082
10083 if (nlflag)
10084 {
10085 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10086 printmax_t dups;
10087
10088 insert_1_both ("\n", 1, 1, true, false, false);
10089
10090 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10091 this_bol = PT;
10092 this_bol_byte = PT_BYTE;
10093
10094 /* See if this line duplicates the previous one.
10095 If so, combine duplicates. */
10096 if (this_bol > BEG)
10097 {
10098 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10099 prev_bol = PT;
10100 prev_bol_byte = PT_BYTE;
10101
10102 dups = message_log_check_duplicate (prev_bol_byte,
10103 this_bol_byte);
10104 if (dups)
10105 {
10106 del_range_both (prev_bol, prev_bol_byte,
10107 this_bol, this_bol_byte, false);
10108 if (dups > 1)
10109 {
10110 char dupstr[sizeof " [ times]"
10111 + INT_STRLEN_BOUND (printmax_t)];
10112
10113 /* If you change this format, don't forget to also
10114 change message_log_check_duplicate. */
10115 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10116 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10117 insert_1_both (dupstr, duplen, duplen,
10118 true, false, true);
10119 }
10120 }
10121 }
10122
10123 /* If we have more than the desired maximum number of lines
10124 in the *Messages* buffer now, delete the oldest ones.
10125 This is safe because we don't have undo in this buffer. */
10126
10127 if (NATNUMP (Vmessage_log_max))
10128 {
10129 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10130 -XFASTINT (Vmessage_log_max) - 1, false);
10131 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10132 }
10133 }
10134 BEGV = marker_position (oldbegv);
10135 BEGV_BYTE = marker_byte_position (oldbegv);
10136
10137 if (zv_at_end)
10138 {
10139 ZV = Z;
10140 ZV_BYTE = Z_BYTE;
10141 }
10142 else
10143 {
10144 ZV = marker_position (oldzv);
10145 ZV_BYTE = marker_byte_position (oldzv);
10146 }
10147
10148 if (point_at_end)
10149 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10150 else
10151 /* We can't do Fgoto_char (oldpoint) because it will run some
10152 Lisp code. */
10153 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10154 marker_byte_position (oldpoint));
10155
10156 unchain_marker (XMARKER (oldpoint));
10157 unchain_marker (XMARKER (oldbegv));
10158 unchain_marker (XMARKER (oldzv));
10159
10160 /* We called insert_1_both above with its 5th argument (PREPARE)
10161 false, which prevents insert_1_both from calling
10162 prepare_to_modify_buffer, which in turns prevents us from
10163 incrementing windows_or_buffers_changed even if *Messages* is
10164 shown in some window. So we must manually set
10165 windows_or_buffers_changed here to make up for that. */
10166 windows_or_buffers_changed = old_windows_or_buffers_changed;
10167 bset_redisplay (current_buffer);
10168
10169 set_buffer_internal (oldbuf);
10170
10171 message_log_need_newline = !nlflag;
10172 Vdeactivate_mark = old_deactivate_mark;
10173 }
10174 }
10175
10176
10177 /* We are at the end of the buffer after just having inserted a newline.
10178 (Note: We depend on the fact we won't be crossing the gap.)
10179 Check to see if the most recent message looks a lot like the previous one.
10180 Return 0 if different, 1 if the new one should just replace it, or a
10181 value N > 1 if we should also append " [N times]". */
10182
10183 static intmax_t
10184 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10185 {
10186 ptrdiff_t i;
10187 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10188 bool seen_dots = false;
10189 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10190 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10191
10192 for (i = 0; i < len; i++)
10193 {
10194 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10195 seen_dots = true;
10196 if (p1[i] != p2[i])
10197 return seen_dots;
10198 }
10199 p1 += len;
10200 if (*p1 == '\n')
10201 return 2;
10202 if (*p1++ == ' ' && *p1++ == '[')
10203 {
10204 char *pend;
10205 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10206 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10207 return n + 1;
10208 }
10209 return 0;
10210 }
10211 \f
10212
10213 /* Display an echo area message M with a specified length of NBYTES
10214 bytes. The string may include null characters. If M is not a
10215 string, clear out any existing message, and let the mini-buffer
10216 text show through.
10217
10218 This function cancels echoing. */
10219
10220 void
10221 message3 (Lisp_Object m)
10222 {
10223 clear_message (true, true);
10224 cancel_echoing ();
10225
10226 /* First flush out any partial line written with print. */
10227 message_log_maybe_newline ();
10228 if (STRINGP (m))
10229 {
10230 ptrdiff_t nbytes = SBYTES (m);
10231 bool multibyte = STRING_MULTIBYTE (m);
10232 char *buffer;
10233 USE_SAFE_ALLOCA;
10234 SAFE_ALLOCA_STRING (buffer, m);
10235 message_dolog (buffer, nbytes, true, multibyte);
10236 SAFE_FREE ();
10237 }
10238 if (! inhibit_message)
10239 message3_nolog (m);
10240 }
10241
10242 /* Log the message M to stderr. Log an empty line if M is not a string. */
10243
10244 static void
10245 message_to_stderr (Lisp_Object m)
10246 {
10247 if (noninteractive_need_newline)
10248 {
10249 noninteractive_need_newline = false;
10250 fputc ('\n', stderr);
10251 }
10252 if (STRINGP (m))
10253 {
10254 Lisp_Object coding_system = Vlocale_coding_system;
10255 Lisp_Object s;
10256
10257 if (!NILP (Vcoding_system_for_write))
10258 coding_system = Vcoding_system_for_write;
10259 if (!NILP (coding_system))
10260 s = code_convert_string_norecord (m, coding_system, true);
10261 else
10262 s = m;
10263
10264 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10265 }
10266 if (!cursor_in_echo_area)
10267 fputc ('\n', stderr);
10268 fflush (stderr);
10269 }
10270
10271 /* The non-logging version of message3.
10272 This does not cancel echoing, because it is used for echoing.
10273 Perhaps we need to make a separate function for echoing
10274 and make this cancel echoing. */
10275
10276 void
10277 message3_nolog (Lisp_Object m)
10278 {
10279 struct frame *sf = SELECTED_FRAME ();
10280
10281 if (FRAME_INITIAL_P (sf))
10282 message_to_stderr (m);
10283 /* Error messages get reported properly by cmd_error, so this must be just an
10284 informative message; if the frame hasn't really been initialized yet, just
10285 toss it. */
10286 else if (INTERACTIVE && sf->glyphs_initialized_p)
10287 {
10288 /* Get the frame containing the mini-buffer
10289 that the selected frame is using. */
10290 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10291 Lisp_Object frame = XWINDOW (mini_window)->frame;
10292 struct frame *f = XFRAME (frame);
10293
10294 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10295 Fmake_frame_visible (frame);
10296
10297 if (STRINGP (m) && SCHARS (m) > 0)
10298 {
10299 set_message (m);
10300 if (minibuffer_auto_raise)
10301 Fraise_frame (frame);
10302 /* Assume we are not echoing.
10303 (If we are, echo_now will override this.) */
10304 echo_message_buffer = Qnil;
10305 }
10306 else
10307 clear_message (true, true);
10308
10309 do_pending_window_change (false);
10310 echo_area_display (true);
10311 do_pending_window_change (false);
10312 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10313 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10314 }
10315 }
10316
10317
10318 /* Display a null-terminated echo area message M. If M is 0, clear
10319 out any existing message, and let the mini-buffer text show through.
10320
10321 The buffer M must continue to exist until after the echo area gets
10322 cleared or some other message gets displayed there. Do not pass
10323 text that is stored in a Lisp string. Do not pass text in a buffer
10324 that was alloca'd. */
10325
10326 void
10327 message1 (const char *m)
10328 {
10329 message3 (m ? build_unibyte_string (m) : Qnil);
10330 }
10331
10332
10333 /* The non-logging counterpart of message1. */
10334
10335 void
10336 message1_nolog (const char *m)
10337 {
10338 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10339 }
10340
10341 /* Display a message M which contains a single %s
10342 which gets replaced with STRING. */
10343
10344 void
10345 message_with_string (const char *m, Lisp_Object string, bool log)
10346 {
10347 CHECK_STRING (string);
10348
10349 bool need_message;
10350 if (noninteractive)
10351 need_message = !!m;
10352 else if (!INTERACTIVE)
10353 need_message = false;
10354 else
10355 {
10356 /* The frame whose minibuffer we're going to display the message on.
10357 It may be larger than the selected frame, so we need
10358 to use its buffer, not the selected frame's buffer. */
10359 Lisp_Object mini_window;
10360 struct frame *f, *sf = SELECTED_FRAME ();
10361
10362 /* Get the frame containing the minibuffer
10363 that the selected frame is using. */
10364 mini_window = FRAME_MINIBUF_WINDOW (sf);
10365 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10366
10367 /* Error messages get reported properly by cmd_error, so this must be
10368 just an informative message; if the frame hasn't really been
10369 initialized yet, just toss it. */
10370 need_message = f->glyphs_initialized_p;
10371 }
10372
10373 if (need_message)
10374 {
10375 AUTO_STRING (fmt, m);
10376 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10377
10378 if (noninteractive)
10379 message_to_stderr (msg);
10380 else
10381 {
10382 if (log)
10383 message3 (msg);
10384 else
10385 message3_nolog (msg);
10386
10387 /* Print should start at the beginning of the message
10388 buffer next time. */
10389 message_buf_print = false;
10390 }
10391 }
10392 }
10393
10394
10395 /* Dump an informative message to the minibuf. If M is 0, clear out
10396 any existing message, and let the mini-buffer text show through.
10397
10398 The message must be safe ASCII and the format must not contain ` or
10399 '. If your message and format do not fit into this category,
10400 convert your arguments to Lisp objects and use Fmessage instead. */
10401
10402 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10403 vmessage (const char *m, va_list ap)
10404 {
10405 if (noninteractive)
10406 {
10407 if (m)
10408 {
10409 if (noninteractive_need_newline)
10410 putc ('\n', stderr);
10411 noninteractive_need_newline = false;
10412 vfprintf (stderr, m, ap);
10413 if (!cursor_in_echo_area)
10414 fprintf (stderr, "\n");
10415 fflush (stderr);
10416 }
10417 }
10418 else if (INTERACTIVE)
10419 {
10420 /* The frame whose mini-buffer we're going to display the message
10421 on. It may be larger than the selected frame, so we need to
10422 use its buffer, not the selected frame's buffer. */
10423 Lisp_Object mini_window;
10424 struct frame *f, *sf = SELECTED_FRAME ();
10425
10426 /* Get the frame containing the mini-buffer
10427 that the selected frame is using. */
10428 mini_window = FRAME_MINIBUF_WINDOW (sf);
10429 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10430
10431 /* Error messages get reported properly by cmd_error, so this must be
10432 just an informative message; if the frame hasn't really been
10433 initialized yet, just toss it. */
10434 if (f->glyphs_initialized_p)
10435 {
10436 if (m)
10437 {
10438 ptrdiff_t len;
10439 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10440 USE_SAFE_ALLOCA;
10441 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10442
10443 len = doprnt (message_buf, maxsize, m, 0, ap);
10444
10445 message3 (make_string (message_buf, len));
10446 SAFE_FREE ();
10447 }
10448 else
10449 message1 (0);
10450
10451 /* Print should start at the beginning of the message
10452 buffer next time. */
10453 message_buf_print = false;
10454 }
10455 }
10456 }
10457
10458 void
10459 message (const char *m, ...)
10460 {
10461 va_list ap;
10462 va_start (ap, m);
10463 vmessage (m, ap);
10464 va_end (ap);
10465 }
10466
10467
10468 /* Display the current message in the current mini-buffer. This is
10469 only called from error handlers in process.c, and is not time
10470 critical. */
10471
10472 void
10473 update_echo_area (void)
10474 {
10475 if (!NILP (echo_area_buffer[0]))
10476 {
10477 Lisp_Object string;
10478 string = Fcurrent_message ();
10479 message3 (string);
10480 }
10481 }
10482
10483
10484 /* Make sure echo area buffers in `echo_buffers' are live.
10485 If they aren't, make new ones. */
10486
10487 static void
10488 ensure_echo_area_buffers (void)
10489 {
10490 int i;
10491
10492 for (i = 0; i < 2; ++i)
10493 if (!BUFFERP (echo_buffer[i])
10494 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10495 {
10496 char name[30];
10497 Lisp_Object old_buffer;
10498 int j;
10499
10500 old_buffer = echo_buffer[i];
10501 echo_buffer[i] = Fget_buffer_create
10502 (make_formatted_string (name, " *Echo Area %d*", i));
10503 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10504 /* to force word wrap in echo area -
10505 it was decided to postpone this*/
10506 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10507
10508 for (j = 0; j < 2; ++j)
10509 if (EQ (old_buffer, echo_area_buffer[j]))
10510 echo_area_buffer[j] = echo_buffer[i];
10511 }
10512 }
10513
10514
10515 /* Call FN with args A1..A2 with either the current or last displayed
10516 echo_area_buffer as current buffer.
10517
10518 WHICH zero means use the current message buffer
10519 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10520 from echo_buffer[] and clear it.
10521
10522 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10523 suitable buffer from echo_buffer[] and clear it.
10524
10525 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10526 that the current message becomes the last displayed one, make
10527 choose a suitable buffer for echo_area_buffer[0], and clear it.
10528
10529 Value is what FN returns. */
10530
10531 static bool
10532 with_echo_area_buffer (struct window *w, int which,
10533 bool (*fn) (ptrdiff_t, Lisp_Object),
10534 ptrdiff_t a1, Lisp_Object a2)
10535 {
10536 Lisp_Object buffer;
10537 bool this_one, the_other, clear_buffer_p, rc;
10538 ptrdiff_t count = SPECPDL_INDEX ();
10539
10540 /* If buffers aren't live, make new ones. */
10541 ensure_echo_area_buffers ();
10542
10543 clear_buffer_p = false;
10544
10545 if (which == 0)
10546 this_one = false, the_other = true;
10547 else if (which > 0)
10548 this_one = true, the_other = false;
10549 else
10550 {
10551 this_one = false, the_other = true;
10552 clear_buffer_p = true;
10553
10554 /* We need a fresh one in case the current echo buffer equals
10555 the one containing the last displayed echo area message. */
10556 if (!NILP (echo_area_buffer[this_one])
10557 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10558 echo_area_buffer[this_one] = Qnil;
10559 }
10560
10561 /* Choose a suitable buffer from echo_buffer[] is we don't
10562 have one. */
10563 if (NILP (echo_area_buffer[this_one]))
10564 {
10565 echo_area_buffer[this_one]
10566 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10567 ? echo_buffer[the_other]
10568 : echo_buffer[this_one]);
10569 clear_buffer_p = true;
10570 }
10571
10572 buffer = echo_area_buffer[this_one];
10573
10574 /* Don't get confused by reusing the buffer used for echoing
10575 for a different purpose. */
10576 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10577 cancel_echoing ();
10578
10579 record_unwind_protect (unwind_with_echo_area_buffer,
10580 with_echo_area_buffer_unwind_data (w));
10581
10582 /* Make the echo area buffer current. Note that for display
10583 purposes, it is not necessary that the displayed window's buffer
10584 == current_buffer, except for text property lookup. So, let's
10585 only set that buffer temporarily here without doing a full
10586 Fset_window_buffer. We must also change w->pointm, though,
10587 because otherwise an assertions in unshow_buffer fails, and Emacs
10588 aborts. */
10589 set_buffer_internal_1 (XBUFFER (buffer));
10590 if (w)
10591 {
10592 wset_buffer (w, buffer);
10593 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10594 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10595 }
10596
10597 bset_undo_list (current_buffer, Qt);
10598 bset_read_only (current_buffer, Qnil);
10599 specbind (Qinhibit_read_only, Qt);
10600 specbind (Qinhibit_modification_hooks, Qt);
10601
10602 if (clear_buffer_p && Z > BEG)
10603 del_range (BEG, Z);
10604
10605 eassert (BEGV >= BEG);
10606 eassert (ZV <= Z && ZV >= BEGV);
10607
10608 rc = fn (a1, a2);
10609
10610 eassert (BEGV >= BEG);
10611 eassert (ZV <= Z && ZV >= BEGV);
10612
10613 unbind_to (count, Qnil);
10614 return rc;
10615 }
10616
10617
10618 /* Save state that should be preserved around the call to the function
10619 FN called in with_echo_area_buffer. */
10620
10621 static Lisp_Object
10622 with_echo_area_buffer_unwind_data (struct window *w)
10623 {
10624 int i = 0;
10625 Lisp_Object vector, tmp;
10626
10627 /* Reduce consing by keeping one vector in
10628 Vwith_echo_area_save_vector. */
10629 vector = Vwith_echo_area_save_vector;
10630 Vwith_echo_area_save_vector = Qnil;
10631
10632 if (NILP (vector))
10633 vector = Fmake_vector (make_number (11), Qnil);
10634
10635 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10636 ASET (vector, i, Vdeactivate_mark); ++i;
10637 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10638
10639 if (w)
10640 {
10641 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10642 ASET (vector, i, w->contents); ++i;
10643 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10644 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10645 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10646 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10647 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10648 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10649 }
10650 else
10651 {
10652 int end = i + 8;
10653 for (; i < end; ++i)
10654 ASET (vector, i, Qnil);
10655 }
10656
10657 eassert (i == ASIZE (vector));
10658 return vector;
10659 }
10660
10661
10662 /* Restore global state from VECTOR which was created by
10663 with_echo_area_buffer_unwind_data. */
10664
10665 static void
10666 unwind_with_echo_area_buffer (Lisp_Object vector)
10667 {
10668 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10669 Vdeactivate_mark = AREF (vector, 1);
10670 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10671
10672 if (WINDOWP (AREF (vector, 3)))
10673 {
10674 struct window *w;
10675 Lisp_Object buffer;
10676
10677 w = XWINDOW (AREF (vector, 3));
10678 buffer = AREF (vector, 4);
10679
10680 wset_buffer (w, buffer);
10681 set_marker_both (w->pointm, buffer,
10682 XFASTINT (AREF (vector, 5)),
10683 XFASTINT (AREF (vector, 6)));
10684 set_marker_both (w->old_pointm, buffer,
10685 XFASTINT (AREF (vector, 7)),
10686 XFASTINT (AREF (vector, 8)));
10687 set_marker_both (w->start, buffer,
10688 XFASTINT (AREF (vector, 9)),
10689 XFASTINT (AREF (vector, 10)));
10690 }
10691
10692 Vwith_echo_area_save_vector = vector;
10693 }
10694
10695
10696 /* Set up the echo area for use by print functions. MULTIBYTE_P
10697 means we will print multibyte. */
10698
10699 void
10700 setup_echo_area_for_printing (bool multibyte_p)
10701 {
10702 /* If we can't find an echo area any more, exit. */
10703 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10704 Fkill_emacs (Qnil);
10705
10706 ensure_echo_area_buffers ();
10707
10708 if (!message_buf_print)
10709 {
10710 /* A message has been output since the last time we printed.
10711 Choose a fresh echo area buffer. */
10712 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10713 echo_area_buffer[0] = echo_buffer[1];
10714 else
10715 echo_area_buffer[0] = echo_buffer[0];
10716
10717 /* Switch to that buffer and clear it. */
10718 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10719 bset_truncate_lines (current_buffer, Qnil);
10720
10721 if (Z > BEG)
10722 {
10723 ptrdiff_t count = SPECPDL_INDEX ();
10724 specbind (Qinhibit_read_only, Qt);
10725 /* Note that undo recording is always disabled. */
10726 del_range (BEG, Z);
10727 unbind_to (count, Qnil);
10728 }
10729 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10730
10731 /* Set up the buffer for the multibyteness we need. */
10732 if (multibyte_p
10733 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10734 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10735
10736 /* Raise the frame containing the echo area. */
10737 if (minibuffer_auto_raise)
10738 {
10739 struct frame *sf = SELECTED_FRAME ();
10740 Lisp_Object mini_window;
10741 mini_window = FRAME_MINIBUF_WINDOW (sf);
10742 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10743 }
10744
10745 message_log_maybe_newline ();
10746 message_buf_print = true;
10747 }
10748 else
10749 {
10750 if (NILP (echo_area_buffer[0]))
10751 {
10752 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10753 echo_area_buffer[0] = echo_buffer[1];
10754 else
10755 echo_area_buffer[0] = echo_buffer[0];
10756 }
10757
10758 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10759 {
10760 /* Someone switched buffers between print requests. */
10761 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10762 bset_truncate_lines (current_buffer, Qnil);
10763 }
10764 }
10765 }
10766
10767
10768 /* Display an echo area message in window W. Value is true if W's
10769 height is changed. If display_last_displayed_message_p,
10770 display the message that was last displayed, otherwise
10771 display the current message. */
10772
10773 static bool
10774 display_echo_area (struct window *w)
10775 {
10776 bool no_message_p, window_height_changed_p;
10777
10778 /* Temporarily disable garbage collections while displaying the echo
10779 area. This is done because a GC can print a message itself.
10780 That message would modify the echo area buffer's contents while a
10781 redisplay of the buffer is going on, and seriously confuse
10782 redisplay. */
10783 ptrdiff_t count = inhibit_garbage_collection ();
10784
10785 /* If there is no message, we must call display_echo_area_1
10786 nevertheless because it resizes the window. But we will have to
10787 reset the echo_area_buffer in question to nil at the end because
10788 with_echo_area_buffer will sets it to an empty buffer. */
10789 bool i = display_last_displayed_message_p;
10790 /* According to the C99, C11 and C++11 standards, the integral value
10791 of a "bool" is always 0 or 1, so this array access is safe here,
10792 if oddly typed. */
10793 no_message_p = NILP (echo_area_buffer[i]);
10794
10795 window_height_changed_p
10796 = with_echo_area_buffer (w, display_last_displayed_message_p,
10797 display_echo_area_1,
10798 (intptr_t) w, Qnil);
10799
10800 if (no_message_p)
10801 echo_area_buffer[i] = Qnil;
10802
10803 unbind_to (count, Qnil);
10804 return window_height_changed_p;
10805 }
10806
10807
10808 /* Helper for display_echo_area. Display the current buffer which
10809 contains the current echo area message in window W, a mini-window,
10810 a pointer to which is passed in A1. A2..A4 are currently not used.
10811 Change the height of W so that all of the message is displayed.
10812 Value is true if height of W was changed. */
10813
10814 static bool
10815 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10816 {
10817 intptr_t i1 = a1;
10818 struct window *w = (struct window *) i1;
10819 Lisp_Object window;
10820 struct text_pos start;
10821
10822 /* We are about to enter redisplay without going through
10823 redisplay_internal, so we need to forget these faces by hand
10824 here. */
10825 forget_escape_and_glyphless_faces ();
10826
10827 /* Do this before displaying, so that we have a large enough glyph
10828 matrix for the display. If we can't get enough space for the
10829 whole text, display the last N lines. That works by setting w->start. */
10830 bool window_height_changed_p = resize_mini_window (w, false);
10831
10832 /* Use the starting position chosen by resize_mini_window. */
10833 SET_TEXT_POS_FROM_MARKER (start, w->start);
10834
10835 /* Display. */
10836 clear_glyph_matrix (w->desired_matrix);
10837 XSETWINDOW (window, w);
10838 try_window (window, start, 0);
10839
10840 return window_height_changed_p;
10841 }
10842
10843
10844 /* Resize the echo area window to exactly the size needed for the
10845 currently displayed message, if there is one. If a mini-buffer
10846 is active, don't shrink it. */
10847
10848 void
10849 resize_echo_area_exactly (void)
10850 {
10851 if (BUFFERP (echo_area_buffer[0])
10852 && WINDOWP (echo_area_window))
10853 {
10854 struct window *w = XWINDOW (echo_area_window);
10855 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10856 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10857 (intptr_t) w, resize_exactly);
10858 if (resized_p)
10859 {
10860 windows_or_buffers_changed = 42;
10861 update_mode_lines = 30;
10862 redisplay_internal ();
10863 }
10864 }
10865 }
10866
10867
10868 /* Callback function for with_echo_area_buffer, when used from
10869 resize_echo_area_exactly. A1 contains a pointer to the window to
10870 resize, EXACTLY non-nil means resize the mini-window exactly to the
10871 size of the text displayed. A3 and A4 are not used. Value is what
10872 resize_mini_window returns. */
10873
10874 static bool
10875 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10876 {
10877 intptr_t i1 = a1;
10878 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10879 }
10880
10881
10882 /* Resize mini-window W to fit the size of its contents. EXACT_P
10883 means size the window exactly to the size needed. Otherwise, it's
10884 only enlarged until W's buffer is empty.
10885
10886 Set W->start to the right place to begin display. If the whole
10887 contents fit, start at the beginning. Otherwise, start so as
10888 to make the end of the contents appear. This is particularly
10889 important for y-or-n-p, but seems desirable generally.
10890
10891 Value is true if the window height has been changed. */
10892
10893 bool
10894 resize_mini_window (struct window *w, bool exact_p)
10895 {
10896 struct frame *f = XFRAME (w->frame);
10897 bool window_height_changed_p = false;
10898
10899 eassert (MINI_WINDOW_P (w));
10900
10901 /* By default, start display at the beginning. */
10902 set_marker_both (w->start, w->contents,
10903 BUF_BEGV (XBUFFER (w->contents)),
10904 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10905
10906 /* Don't resize windows while redisplaying a window; it would
10907 confuse redisplay functions when the size of the window they are
10908 displaying changes from under them. Such a resizing can happen,
10909 for instance, when which-func prints a long message while
10910 we are running fontification-functions. We're running these
10911 functions with safe_call which binds inhibit-redisplay to t. */
10912 if (!NILP (Vinhibit_redisplay))
10913 return false;
10914
10915 /* Nil means don't try to resize. */
10916 if (NILP (Vresize_mini_windows)
10917 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10918 return false;
10919
10920 if (!FRAME_MINIBUF_ONLY_P (f))
10921 {
10922 struct it it;
10923 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10924 + WINDOW_PIXEL_HEIGHT (w));
10925 int unit = FRAME_LINE_HEIGHT (f);
10926 int height, max_height;
10927 struct text_pos start;
10928 struct buffer *old_current_buffer = NULL;
10929
10930 if (current_buffer != XBUFFER (w->contents))
10931 {
10932 old_current_buffer = current_buffer;
10933 set_buffer_internal (XBUFFER (w->contents));
10934 }
10935
10936 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10937
10938 /* Compute the max. number of lines specified by the user. */
10939 if (FLOATP (Vmax_mini_window_height))
10940 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10941 else if (INTEGERP (Vmax_mini_window_height))
10942 max_height = XINT (Vmax_mini_window_height) * unit;
10943 else
10944 max_height = total_height / 4;
10945
10946 /* Correct that max. height if it's bogus. */
10947 max_height = clip_to_bounds (unit, max_height, total_height);
10948
10949 /* Find out the height of the text in the window. */
10950 if (it.line_wrap == TRUNCATE)
10951 height = unit;
10952 else
10953 {
10954 last_height = 0;
10955 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10956 if (it.max_ascent == 0 && it.max_descent == 0)
10957 height = it.current_y + last_height;
10958 else
10959 height = it.current_y + it.max_ascent + it.max_descent;
10960 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10961 }
10962
10963 /* Compute a suitable window start. */
10964 if (height > max_height)
10965 {
10966 height = (max_height / unit) * unit;
10967 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10968 move_it_vertically_backward (&it, height - unit);
10969 start = it.current.pos;
10970 }
10971 else
10972 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10973 SET_MARKER_FROM_TEXT_POS (w->start, start);
10974
10975 if (EQ (Vresize_mini_windows, Qgrow_only))
10976 {
10977 /* Let it grow only, until we display an empty message, in which
10978 case the window shrinks again. */
10979 if (height > WINDOW_PIXEL_HEIGHT (w))
10980 {
10981 int old_height = WINDOW_PIXEL_HEIGHT (w);
10982
10983 FRAME_WINDOWS_FROZEN (f) = true;
10984 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10985 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10986 }
10987 else if (height < WINDOW_PIXEL_HEIGHT (w)
10988 && (exact_p || BEGV == ZV))
10989 {
10990 int old_height = WINDOW_PIXEL_HEIGHT (w);
10991
10992 FRAME_WINDOWS_FROZEN (f) = false;
10993 shrink_mini_window (w, true);
10994 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10995 }
10996 }
10997 else
10998 {
10999 /* Always resize to exact size needed. */
11000 if (height > WINDOW_PIXEL_HEIGHT (w))
11001 {
11002 int old_height = WINDOW_PIXEL_HEIGHT (w);
11003
11004 FRAME_WINDOWS_FROZEN (f) = true;
11005 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11006 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11007 }
11008 else if (height < WINDOW_PIXEL_HEIGHT (w))
11009 {
11010 int old_height = WINDOW_PIXEL_HEIGHT (w);
11011
11012 FRAME_WINDOWS_FROZEN (f) = false;
11013 shrink_mini_window (w, true);
11014
11015 if (height)
11016 {
11017 FRAME_WINDOWS_FROZEN (f) = true;
11018 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11019 }
11020
11021 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11022 }
11023 }
11024
11025 if (old_current_buffer)
11026 set_buffer_internal (old_current_buffer);
11027 }
11028
11029 return window_height_changed_p;
11030 }
11031
11032
11033 /* Value is the current message, a string, or nil if there is no
11034 current message. */
11035
11036 Lisp_Object
11037 current_message (void)
11038 {
11039 Lisp_Object msg;
11040
11041 if (!BUFFERP (echo_area_buffer[0]))
11042 msg = Qnil;
11043 else
11044 {
11045 with_echo_area_buffer (0, 0, current_message_1,
11046 (intptr_t) &msg, Qnil);
11047 if (NILP (msg))
11048 echo_area_buffer[0] = Qnil;
11049 }
11050
11051 return msg;
11052 }
11053
11054
11055 static bool
11056 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11057 {
11058 intptr_t i1 = a1;
11059 Lisp_Object *msg = (Lisp_Object *) i1;
11060
11061 if (Z > BEG)
11062 *msg = make_buffer_string (BEG, Z, true);
11063 else
11064 *msg = Qnil;
11065 return false;
11066 }
11067
11068
11069 /* Push the current message on Vmessage_stack for later restoration
11070 by restore_message. Value is true if the current message isn't
11071 empty. This is a relatively infrequent operation, so it's not
11072 worth optimizing. */
11073
11074 bool
11075 push_message (void)
11076 {
11077 Lisp_Object msg = current_message ();
11078 Vmessage_stack = Fcons (msg, Vmessage_stack);
11079 return STRINGP (msg);
11080 }
11081
11082
11083 /* Restore message display from the top of Vmessage_stack. */
11084
11085 void
11086 restore_message (void)
11087 {
11088 eassert (CONSP (Vmessage_stack));
11089 message3_nolog (XCAR (Vmessage_stack));
11090 }
11091
11092
11093 /* Handler for unwind-protect calling pop_message. */
11094
11095 void
11096 pop_message_unwind (void)
11097 {
11098 /* Pop the top-most entry off Vmessage_stack. */
11099 eassert (CONSP (Vmessage_stack));
11100 Vmessage_stack = XCDR (Vmessage_stack);
11101 }
11102
11103
11104 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11105 exits. If the stack is not empty, we have a missing pop_message
11106 somewhere. */
11107
11108 void
11109 check_message_stack (void)
11110 {
11111 if (!NILP (Vmessage_stack))
11112 emacs_abort ();
11113 }
11114
11115
11116 /* Truncate to NCHARS what will be displayed in the echo area the next
11117 time we display it---but don't redisplay it now. */
11118
11119 void
11120 truncate_echo_area (ptrdiff_t nchars)
11121 {
11122 if (nchars == 0)
11123 echo_area_buffer[0] = Qnil;
11124 else if (!noninteractive
11125 && INTERACTIVE
11126 && !NILP (echo_area_buffer[0]))
11127 {
11128 struct frame *sf = SELECTED_FRAME ();
11129 /* Error messages get reported properly by cmd_error, so this must be
11130 just an informative message; if the frame hasn't really been
11131 initialized yet, just toss it. */
11132 if (sf->glyphs_initialized_p)
11133 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11134 }
11135 }
11136
11137
11138 /* Helper function for truncate_echo_area. Truncate the current
11139 message to at most NCHARS characters. */
11140
11141 static bool
11142 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11143 {
11144 if (BEG + nchars < Z)
11145 del_range (BEG + nchars, Z);
11146 if (Z == BEG)
11147 echo_area_buffer[0] = Qnil;
11148 return false;
11149 }
11150
11151 /* Set the current message to STRING. */
11152
11153 static void
11154 set_message (Lisp_Object string)
11155 {
11156 eassert (STRINGP (string));
11157
11158 message_enable_multibyte = STRING_MULTIBYTE (string);
11159
11160 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11161 message_buf_print = false;
11162 help_echo_showing_p = false;
11163
11164 if (STRINGP (Vdebug_on_message)
11165 && STRINGP (string)
11166 && fast_string_match (Vdebug_on_message, string) >= 0)
11167 call_debugger (list2 (Qerror, string));
11168 }
11169
11170
11171 /* Helper function for set_message. First argument is ignored and second
11172 argument has the same meaning as for set_message.
11173 This function is called with the echo area buffer being current. */
11174
11175 static bool
11176 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11177 {
11178 eassert (STRINGP (string));
11179
11180 /* Change multibyteness of the echo buffer appropriately. */
11181 if (message_enable_multibyte
11182 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11183 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11184
11185 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11186 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11187 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11188
11189 /* Insert new message at BEG. */
11190 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11191
11192 /* This function takes care of single/multibyte conversion.
11193 We just have to ensure that the echo area buffer has the right
11194 setting of enable_multibyte_characters. */
11195 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11196
11197 return false;
11198 }
11199
11200
11201 /* Clear messages. CURRENT_P means clear the current message.
11202 LAST_DISPLAYED_P means clear the message last displayed. */
11203
11204 void
11205 clear_message (bool current_p, bool last_displayed_p)
11206 {
11207 if (current_p)
11208 {
11209 echo_area_buffer[0] = Qnil;
11210 message_cleared_p = true;
11211 }
11212
11213 if (last_displayed_p)
11214 echo_area_buffer[1] = Qnil;
11215
11216 message_buf_print = false;
11217 }
11218
11219 /* Clear garbaged frames.
11220
11221 This function is used where the old redisplay called
11222 redraw_garbaged_frames which in turn called redraw_frame which in
11223 turn called clear_frame. The call to clear_frame was a source of
11224 flickering. I believe a clear_frame is not necessary. It should
11225 suffice in the new redisplay to invalidate all current matrices,
11226 and ensure a complete redisplay of all windows. */
11227
11228 static void
11229 clear_garbaged_frames (void)
11230 {
11231 if (frame_garbaged)
11232 {
11233 Lisp_Object tail, frame;
11234
11235 FOR_EACH_FRAME (tail, frame)
11236 {
11237 struct frame *f = XFRAME (frame);
11238
11239 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11240 {
11241 if (f->resized_p)
11242 redraw_frame (f);
11243 else
11244 clear_current_matrices (f);
11245 fset_redisplay (f);
11246 f->garbaged = false;
11247 f->resized_p = false;
11248 }
11249 }
11250
11251 frame_garbaged = false;
11252 }
11253 }
11254
11255
11256 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11257 selected_frame. */
11258
11259 static void
11260 echo_area_display (bool update_frame_p)
11261 {
11262 Lisp_Object mini_window;
11263 struct window *w;
11264 struct frame *f;
11265 bool window_height_changed_p = false;
11266 struct frame *sf = SELECTED_FRAME ();
11267
11268 mini_window = FRAME_MINIBUF_WINDOW (sf);
11269 w = XWINDOW (mini_window);
11270 f = XFRAME (WINDOW_FRAME (w));
11271
11272 /* Don't display if frame is invisible or not yet initialized. */
11273 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11274 return;
11275
11276 #ifdef HAVE_WINDOW_SYSTEM
11277 /* When Emacs starts, selected_frame may be the initial terminal
11278 frame. If we let this through, a message would be displayed on
11279 the terminal. */
11280 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11281 return;
11282 #endif /* HAVE_WINDOW_SYSTEM */
11283
11284 /* Redraw garbaged frames. */
11285 clear_garbaged_frames ();
11286
11287 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11288 {
11289 echo_area_window = mini_window;
11290 window_height_changed_p = display_echo_area (w);
11291 w->must_be_updated_p = true;
11292
11293 /* Update the display, unless called from redisplay_internal.
11294 Also don't update the screen during redisplay itself. The
11295 update will happen at the end of redisplay, and an update
11296 here could cause confusion. */
11297 if (update_frame_p && !redisplaying_p)
11298 {
11299 int n = 0;
11300
11301 /* If the display update has been interrupted by pending
11302 input, update mode lines in the frame. Due to the
11303 pending input, it might have been that redisplay hasn't
11304 been called, so that mode lines above the echo area are
11305 garbaged. This looks odd, so we prevent it here. */
11306 if (!display_completed)
11307 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11308
11309 if (window_height_changed_p
11310 /* Don't do this if Emacs is shutting down. Redisplay
11311 needs to run hooks. */
11312 && !NILP (Vrun_hooks))
11313 {
11314 /* Must update other windows. Likewise as in other
11315 cases, don't let this update be interrupted by
11316 pending input. */
11317 ptrdiff_t count = SPECPDL_INDEX ();
11318 specbind (Qredisplay_dont_pause, Qt);
11319 fset_redisplay (f);
11320 redisplay_internal ();
11321 unbind_to (count, Qnil);
11322 }
11323 else if (FRAME_WINDOW_P (f) && n == 0)
11324 {
11325 /* Window configuration is the same as before.
11326 Can do with a display update of the echo area,
11327 unless we displayed some mode lines. */
11328 update_single_window (w);
11329 flush_frame (f);
11330 }
11331 else
11332 update_frame (f, true, true);
11333
11334 /* If cursor is in the echo area, make sure that the next
11335 redisplay displays the minibuffer, so that the cursor will
11336 be replaced with what the minibuffer wants. */
11337 if (cursor_in_echo_area)
11338 wset_redisplay (XWINDOW (mini_window));
11339 }
11340 }
11341 else if (!EQ (mini_window, selected_window))
11342 wset_redisplay (XWINDOW (mini_window));
11343
11344 /* Last displayed message is now the current message. */
11345 echo_area_buffer[1] = echo_area_buffer[0];
11346 /* Inform read_char that we're not echoing. */
11347 echo_message_buffer = Qnil;
11348
11349 /* Prevent redisplay optimization in redisplay_internal by resetting
11350 this_line_start_pos. This is done because the mini-buffer now
11351 displays the message instead of its buffer text. */
11352 if (EQ (mini_window, selected_window))
11353 CHARPOS (this_line_start_pos) = 0;
11354
11355 if (window_height_changed_p)
11356 {
11357 fset_redisplay (f);
11358
11359 /* If window configuration was changed, frames may have been
11360 marked garbaged. Clear them or we will experience
11361 surprises wrt scrolling.
11362 FIXME: How/why/when? */
11363 clear_garbaged_frames ();
11364 }
11365 }
11366
11367 /* True if W's buffer was changed but not saved. */
11368
11369 static bool
11370 window_buffer_changed (struct window *w)
11371 {
11372 struct buffer *b = XBUFFER (w->contents);
11373
11374 eassert (BUFFER_LIVE_P (b));
11375
11376 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11377 }
11378
11379 /* True if W has %c in its mode line and mode line should be updated. */
11380
11381 static bool
11382 mode_line_update_needed (struct window *w)
11383 {
11384 return (w->column_number_displayed != -1
11385 && !(PT == w->last_point && !window_outdated (w))
11386 && (w->column_number_displayed != current_column ()));
11387 }
11388
11389 /* True if window start of W is frozen and may not be changed during
11390 redisplay. */
11391
11392 static bool
11393 window_frozen_p (struct window *w)
11394 {
11395 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11396 {
11397 Lisp_Object window;
11398
11399 XSETWINDOW (window, w);
11400 if (MINI_WINDOW_P (w))
11401 return false;
11402 else if (EQ (window, selected_window))
11403 return false;
11404 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11405 && EQ (window, Vminibuf_scroll_window))
11406 /* This special window can't be frozen too. */
11407 return false;
11408 else
11409 return true;
11410 }
11411 return false;
11412 }
11413
11414 /***********************************************************************
11415 Mode Lines and Frame Titles
11416 ***********************************************************************/
11417
11418 /* A buffer for constructing non-propertized mode-line strings and
11419 frame titles in it; allocated from the heap in init_xdisp and
11420 resized as needed in store_mode_line_noprop_char. */
11421
11422 static char *mode_line_noprop_buf;
11423
11424 /* The buffer's end, and a current output position in it. */
11425
11426 static char *mode_line_noprop_buf_end;
11427 static char *mode_line_noprop_ptr;
11428
11429 #define MODE_LINE_NOPROP_LEN(start) \
11430 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11431
11432 static enum {
11433 MODE_LINE_DISPLAY = 0,
11434 MODE_LINE_TITLE,
11435 MODE_LINE_NOPROP,
11436 MODE_LINE_STRING
11437 } mode_line_target;
11438
11439 /* Alist that caches the results of :propertize.
11440 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11441 static Lisp_Object mode_line_proptrans_alist;
11442
11443 /* List of strings making up the mode-line. */
11444 static Lisp_Object mode_line_string_list;
11445
11446 /* Base face property when building propertized mode line string. */
11447 static Lisp_Object mode_line_string_face;
11448 static Lisp_Object mode_line_string_face_prop;
11449
11450
11451 /* Unwind data for mode line strings */
11452
11453 static Lisp_Object Vmode_line_unwind_vector;
11454
11455 static Lisp_Object
11456 format_mode_line_unwind_data (struct frame *target_frame,
11457 struct buffer *obuf,
11458 Lisp_Object owin,
11459 bool save_proptrans)
11460 {
11461 Lisp_Object vector, tmp;
11462
11463 /* Reduce consing by keeping one vector in
11464 Vwith_echo_area_save_vector. */
11465 vector = Vmode_line_unwind_vector;
11466 Vmode_line_unwind_vector = Qnil;
11467
11468 if (NILP (vector))
11469 vector = Fmake_vector (make_number (10), Qnil);
11470
11471 ASET (vector, 0, make_number (mode_line_target));
11472 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11473 ASET (vector, 2, mode_line_string_list);
11474 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11475 ASET (vector, 4, mode_line_string_face);
11476 ASET (vector, 5, mode_line_string_face_prop);
11477
11478 if (obuf)
11479 XSETBUFFER (tmp, obuf);
11480 else
11481 tmp = Qnil;
11482 ASET (vector, 6, tmp);
11483 ASET (vector, 7, owin);
11484 if (target_frame)
11485 {
11486 /* Similarly to `with-selected-window', if the operation selects
11487 a window on another frame, we must restore that frame's
11488 selected window, and (for a tty) the top-frame. */
11489 ASET (vector, 8, target_frame->selected_window);
11490 if (FRAME_TERMCAP_P (target_frame))
11491 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11492 }
11493
11494 return vector;
11495 }
11496
11497 static void
11498 unwind_format_mode_line (Lisp_Object vector)
11499 {
11500 Lisp_Object old_window = AREF (vector, 7);
11501 Lisp_Object target_frame_window = AREF (vector, 8);
11502 Lisp_Object old_top_frame = AREF (vector, 9);
11503
11504 mode_line_target = XINT (AREF (vector, 0));
11505 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11506 mode_line_string_list = AREF (vector, 2);
11507 if (! EQ (AREF (vector, 3), Qt))
11508 mode_line_proptrans_alist = AREF (vector, 3);
11509 mode_line_string_face = AREF (vector, 4);
11510 mode_line_string_face_prop = AREF (vector, 5);
11511
11512 /* Select window before buffer, since it may change the buffer. */
11513 if (!NILP (old_window))
11514 {
11515 /* If the operation that we are unwinding had selected a window
11516 on a different frame, reset its frame-selected-window. For a
11517 text terminal, reset its top-frame if necessary. */
11518 if (!NILP (target_frame_window))
11519 {
11520 Lisp_Object frame
11521 = WINDOW_FRAME (XWINDOW (target_frame_window));
11522
11523 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11524 Fselect_window (target_frame_window, Qt);
11525
11526 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11527 Fselect_frame (old_top_frame, Qt);
11528 }
11529
11530 Fselect_window (old_window, Qt);
11531 }
11532
11533 if (!NILP (AREF (vector, 6)))
11534 {
11535 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11536 ASET (vector, 6, Qnil);
11537 }
11538
11539 Vmode_line_unwind_vector = vector;
11540 }
11541
11542
11543 /* Store a single character C for the frame title in mode_line_noprop_buf.
11544 Re-allocate mode_line_noprop_buf if necessary. */
11545
11546 static void
11547 store_mode_line_noprop_char (char c)
11548 {
11549 /* If output position has reached the end of the allocated buffer,
11550 increase the buffer's size. */
11551 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11552 {
11553 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11554 ptrdiff_t size = len;
11555 mode_line_noprop_buf =
11556 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11557 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11558 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11559 }
11560
11561 *mode_line_noprop_ptr++ = c;
11562 }
11563
11564
11565 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11566 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11567 characters that yield more columns than PRECISION; PRECISION <= 0
11568 means copy the whole string. Pad with spaces until FIELD_WIDTH
11569 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11570 pad. Called from display_mode_element when it is used to build a
11571 frame title. */
11572
11573 static int
11574 store_mode_line_noprop (const char *string, int field_width, int precision)
11575 {
11576 const unsigned char *str = (const unsigned char *) string;
11577 int n = 0;
11578 ptrdiff_t dummy, nbytes;
11579
11580 /* Copy at most PRECISION chars from STR. */
11581 nbytes = strlen (string);
11582 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11583 while (nbytes--)
11584 store_mode_line_noprop_char (*str++);
11585
11586 /* Fill up with spaces until FIELD_WIDTH reached. */
11587 while (field_width > 0
11588 && n < field_width)
11589 {
11590 store_mode_line_noprop_char (' ');
11591 ++n;
11592 }
11593
11594 return n;
11595 }
11596
11597 /***********************************************************************
11598 Frame Titles
11599 ***********************************************************************/
11600
11601 #ifdef HAVE_WINDOW_SYSTEM
11602
11603 /* Set the title of FRAME, if it has changed. The title format is
11604 Vicon_title_format if FRAME is iconified, otherwise it is
11605 frame_title_format. */
11606
11607 static void
11608 x_consider_frame_title (Lisp_Object frame)
11609 {
11610 struct frame *f = XFRAME (frame);
11611
11612 if ((FRAME_WINDOW_P (f)
11613 || FRAME_MINIBUF_ONLY_P (f)
11614 || f->explicit_name)
11615 && NILP (Fframe_parameter (frame, Qtooltip)))
11616 {
11617 /* Do we have more than one visible frame on this X display? */
11618 Lisp_Object tail, other_frame, fmt;
11619 ptrdiff_t title_start;
11620 char *title;
11621 ptrdiff_t len;
11622 struct it it;
11623 ptrdiff_t count = SPECPDL_INDEX ();
11624
11625 FOR_EACH_FRAME (tail, other_frame)
11626 {
11627 struct frame *tf = XFRAME (other_frame);
11628
11629 if (tf != f
11630 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11631 && !FRAME_MINIBUF_ONLY_P (tf)
11632 && !EQ (other_frame, tip_frame)
11633 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11634 break;
11635 }
11636
11637 /* Set global variable indicating that multiple frames exist. */
11638 multiple_frames = CONSP (tail);
11639
11640 /* Switch to the buffer of selected window of the frame. Set up
11641 mode_line_target so that display_mode_element will output into
11642 mode_line_noprop_buf; then display the title. */
11643 record_unwind_protect (unwind_format_mode_line,
11644 format_mode_line_unwind_data
11645 (f, current_buffer, selected_window, false));
11646
11647 Fselect_window (f->selected_window, Qt);
11648 set_buffer_internal_1
11649 (XBUFFER (XWINDOW (f->selected_window)->contents));
11650 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11651
11652 mode_line_target = MODE_LINE_TITLE;
11653 title_start = MODE_LINE_NOPROP_LEN (0);
11654 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11655 NULL, DEFAULT_FACE_ID);
11656 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11657 len = MODE_LINE_NOPROP_LEN (title_start);
11658 title = mode_line_noprop_buf + title_start;
11659 unbind_to (count, Qnil);
11660
11661 /* Set the title only if it's changed. This avoids consing in
11662 the common case where it hasn't. (If it turns out that we've
11663 already wasted too much time by walking through the list with
11664 display_mode_element, then we might need to optimize at a
11665 higher level than this.) */
11666 if (! STRINGP (f->name)
11667 || SBYTES (f->name) != len
11668 || memcmp (title, SDATA (f->name), len) != 0)
11669 x_implicitly_set_name (f, make_string (title, len), Qnil);
11670 }
11671 }
11672
11673 #endif /* not HAVE_WINDOW_SYSTEM */
11674
11675 \f
11676 /***********************************************************************
11677 Menu Bars
11678 ***********************************************************************/
11679
11680 /* True if we will not redisplay all visible windows. */
11681 #define REDISPLAY_SOME_P() \
11682 ((windows_or_buffers_changed == 0 \
11683 || windows_or_buffers_changed == REDISPLAY_SOME) \
11684 && (update_mode_lines == 0 \
11685 || update_mode_lines == REDISPLAY_SOME))
11686
11687 /* Prepare for redisplay by updating menu-bar item lists when
11688 appropriate. This can call eval. */
11689
11690 static void
11691 prepare_menu_bars (void)
11692 {
11693 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11694 bool some_windows = REDISPLAY_SOME_P ();
11695 Lisp_Object tooltip_frame;
11696
11697 #ifdef HAVE_WINDOW_SYSTEM
11698 tooltip_frame = tip_frame;
11699 #else
11700 tooltip_frame = Qnil;
11701 #endif
11702
11703 if (FUNCTIONP (Vpre_redisplay_function))
11704 {
11705 Lisp_Object windows = all_windows ? Qt : Qnil;
11706 if (all_windows && some_windows)
11707 {
11708 Lisp_Object ws = window_list ();
11709 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11710 {
11711 Lisp_Object this = XCAR (ws);
11712 struct window *w = XWINDOW (this);
11713 if (w->redisplay
11714 || XFRAME (w->frame)->redisplay
11715 || XBUFFER (w->contents)->text->redisplay)
11716 {
11717 windows = Fcons (this, windows);
11718 }
11719 }
11720 }
11721 safe__call1 (true, Vpre_redisplay_function, windows);
11722 }
11723
11724 /* Update all frame titles based on their buffer names, etc. We do
11725 this before the menu bars so that the buffer-menu will show the
11726 up-to-date frame titles. */
11727 #ifdef HAVE_WINDOW_SYSTEM
11728 if (all_windows)
11729 {
11730 Lisp_Object tail, frame;
11731
11732 FOR_EACH_FRAME (tail, frame)
11733 {
11734 struct frame *f = XFRAME (frame);
11735 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11736 if (some_windows
11737 && !f->redisplay
11738 && !w->redisplay
11739 && !XBUFFER (w->contents)->text->redisplay)
11740 continue;
11741
11742 if (!EQ (frame, tooltip_frame)
11743 && (FRAME_ICONIFIED_P (f)
11744 || FRAME_VISIBLE_P (f) == 1
11745 /* Exclude TTY frames that are obscured because they
11746 are not the top frame on their console. This is
11747 because x_consider_frame_title actually switches
11748 to the frame, which for TTY frames means it is
11749 marked as garbaged, and will be completely
11750 redrawn on the next redisplay cycle. This causes
11751 TTY frames to be completely redrawn, when there
11752 are more than one of them, even though nothing
11753 should be changed on display. */
11754 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11755 x_consider_frame_title (frame);
11756 }
11757 }
11758 #endif /* HAVE_WINDOW_SYSTEM */
11759
11760 /* Update the menu bar item lists, if appropriate. This has to be
11761 done before any actual redisplay or generation of display lines. */
11762
11763 if (all_windows)
11764 {
11765 Lisp_Object tail, frame;
11766 ptrdiff_t count = SPECPDL_INDEX ();
11767 /* True means that update_menu_bar has run its hooks
11768 so any further calls to update_menu_bar shouldn't do so again. */
11769 bool menu_bar_hooks_run = false;
11770
11771 record_unwind_save_match_data ();
11772
11773 FOR_EACH_FRAME (tail, frame)
11774 {
11775 struct frame *f = XFRAME (frame);
11776 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11777
11778 /* Ignore tooltip frame. */
11779 if (EQ (frame, tooltip_frame))
11780 continue;
11781
11782 if (some_windows
11783 && !f->redisplay
11784 && !w->redisplay
11785 && !XBUFFER (w->contents)->text->redisplay)
11786 continue;
11787
11788 /* If a window on this frame changed size, report that to
11789 the user and clear the size-change flag. */
11790 if (FRAME_WINDOW_SIZES_CHANGED (f))
11791 {
11792 Lisp_Object functions;
11793
11794 /* Clear flag first in case we get an error below. */
11795 FRAME_WINDOW_SIZES_CHANGED (f) = false;
11796 functions = Vwindow_size_change_functions;
11797
11798 while (CONSP (functions))
11799 {
11800 if (!EQ (XCAR (functions), Qt))
11801 call1 (XCAR (functions), frame);
11802 functions = XCDR (functions);
11803 }
11804 }
11805
11806 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11807 #ifdef HAVE_WINDOW_SYSTEM
11808 update_tool_bar (f, false);
11809 #endif
11810 }
11811
11812 unbind_to (count, Qnil);
11813 }
11814 else
11815 {
11816 struct frame *sf = SELECTED_FRAME ();
11817 update_menu_bar (sf, true, false);
11818 #ifdef HAVE_WINDOW_SYSTEM
11819 update_tool_bar (sf, true);
11820 #endif
11821 }
11822 }
11823
11824
11825 /* Update the menu bar item list for frame F. This has to be done
11826 before we start to fill in any display lines, because it can call
11827 eval.
11828
11829 If SAVE_MATCH_DATA, we must save and restore it here.
11830
11831 If HOOKS_RUN, a previous call to update_menu_bar
11832 already ran the menu bar hooks for this redisplay, so there
11833 is no need to run them again. The return value is the
11834 updated value of this flag, to pass to the next call. */
11835
11836 static bool
11837 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11838 {
11839 Lisp_Object window;
11840 struct window *w;
11841
11842 /* If called recursively during a menu update, do nothing. This can
11843 happen when, for instance, an activate-menubar-hook causes a
11844 redisplay. */
11845 if (inhibit_menubar_update)
11846 return hooks_run;
11847
11848 window = FRAME_SELECTED_WINDOW (f);
11849 w = XWINDOW (window);
11850
11851 if (FRAME_WINDOW_P (f)
11852 ?
11853 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11854 || defined (HAVE_NS) || defined (USE_GTK)
11855 FRAME_EXTERNAL_MENU_BAR (f)
11856 #else
11857 FRAME_MENU_BAR_LINES (f) > 0
11858 #endif
11859 : FRAME_MENU_BAR_LINES (f) > 0)
11860 {
11861 /* If the user has switched buffers or windows, we need to
11862 recompute to reflect the new bindings. But we'll
11863 recompute when update_mode_lines is set too; that means
11864 that people can use force-mode-line-update to request
11865 that the menu bar be recomputed. The adverse effect on
11866 the rest of the redisplay algorithm is about the same as
11867 windows_or_buffers_changed anyway. */
11868 if (windows_or_buffers_changed
11869 /* This used to test w->update_mode_line, but we believe
11870 there is no need to recompute the menu in that case. */
11871 || update_mode_lines
11872 || window_buffer_changed (w))
11873 {
11874 struct buffer *prev = current_buffer;
11875 ptrdiff_t count = SPECPDL_INDEX ();
11876
11877 specbind (Qinhibit_menubar_update, Qt);
11878
11879 set_buffer_internal_1 (XBUFFER (w->contents));
11880 if (save_match_data)
11881 record_unwind_save_match_data ();
11882 if (NILP (Voverriding_local_map_menu_flag))
11883 {
11884 specbind (Qoverriding_terminal_local_map, Qnil);
11885 specbind (Qoverriding_local_map, Qnil);
11886 }
11887
11888 if (!hooks_run)
11889 {
11890 /* Run the Lucid hook. */
11891 safe_run_hooks (Qactivate_menubar_hook);
11892
11893 /* If it has changed current-menubar from previous value,
11894 really recompute the menu-bar from the value. */
11895 if (! NILP (Vlucid_menu_bar_dirty_flag))
11896 call0 (Qrecompute_lucid_menubar);
11897
11898 safe_run_hooks (Qmenu_bar_update_hook);
11899
11900 hooks_run = true;
11901 }
11902
11903 XSETFRAME (Vmenu_updating_frame, f);
11904 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11905
11906 /* Redisplay the menu bar in case we changed it. */
11907 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11908 || defined (HAVE_NS) || defined (USE_GTK)
11909 if (FRAME_WINDOW_P (f))
11910 {
11911 #if defined (HAVE_NS)
11912 /* All frames on Mac OS share the same menubar. So only
11913 the selected frame should be allowed to set it. */
11914 if (f == SELECTED_FRAME ())
11915 #endif
11916 set_frame_menubar (f, false, false);
11917 }
11918 else
11919 /* On a terminal screen, the menu bar is an ordinary screen
11920 line, and this makes it get updated. */
11921 w->update_mode_line = true;
11922 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11923 /* In the non-toolkit version, the menu bar is an ordinary screen
11924 line, and this makes it get updated. */
11925 w->update_mode_line = true;
11926 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11927
11928 unbind_to (count, Qnil);
11929 set_buffer_internal_1 (prev);
11930 }
11931 }
11932
11933 return hooks_run;
11934 }
11935
11936 /***********************************************************************
11937 Tool-bars
11938 ***********************************************************************/
11939
11940 #ifdef HAVE_WINDOW_SYSTEM
11941
11942 /* Select `frame' temporarily without running all the code in
11943 do_switch_frame.
11944 FIXME: Maybe do_switch_frame should be trimmed down similarly
11945 when `norecord' is set. */
11946 static void
11947 fast_set_selected_frame (Lisp_Object frame)
11948 {
11949 if (!EQ (selected_frame, frame))
11950 {
11951 selected_frame = frame;
11952 selected_window = XFRAME (frame)->selected_window;
11953 }
11954 }
11955
11956 /* Update the tool-bar item list for frame F. This has to be done
11957 before we start to fill in any display lines. Called from
11958 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
11959 and restore it here. */
11960
11961 static void
11962 update_tool_bar (struct frame *f, bool save_match_data)
11963 {
11964 #if defined (USE_GTK) || defined (HAVE_NS)
11965 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11966 #else
11967 bool do_update = (WINDOWP (f->tool_bar_window)
11968 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11969 #endif
11970
11971 if (do_update)
11972 {
11973 Lisp_Object window;
11974 struct window *w;
11975
11976 window = FRAME_SELECTED_WINDOW (f);
11977 w = XWINDOW (window);
11978
11979 /* If the user has switched buffers or windows, we need to
11980 recompute to reflect the new bindings. But we'll
11981 recompute when update_mode_lines is set too; that means
11982 that people can use force-mode-line-update to request
11983 that the menu bar be recomputed. The adverse effect on
11984 the rest of the redisplay algorithm is about the same as
11985 windows_or_buffers_changed anyway. */
11986 if (windows_or_buffers_changed
11987 || w->update_mode_line
11988 || update_mode_lines
11989 || window_buffer_changed (w))
11990 {
11991 struct buffer *prev = current_buffer;
11992 ptrdiff_t count = SPECPDL_INDEX ();
11993 Lisp_Object frame, new_tool_bar;
11994 int new_n_tool_bar;
11995
11996 /* Set current_buffer to the buffer of the selected
11997 window of the frame, so that we get the right local
11998 keymaps. */
11999 set_buffer_internal_1 (XBUFFER (w->contents));
12000
12001 /* Save match data, if we must. */
12002 if (save_match_data)
12003 record_unwind_save_match_data ();
12004
12005 /* Make sure that we don't accidentally use bogus keymaps. */
12006 if (NILP (Voverriding_local_map_menu_flag))
12007 {
12008 specbind (Qoverriding_terminal_local_map, Qnil);
12009 specbind (Qoverriding_local_map, Qnil);
12010 }
12011
12012 /* We must temporarily set the selected frame to this frame
12013 before calling tool_bar_items, because the calculation of
12014 the tool-bar keymap uses the selected frame (see
12015 `tool-bar-make-keymap' in tool-bar.el). */
12016 eassert (EQ (selected_window,
12017 /* Since we only explicitly preserve selected_frame,
12018 check that selected_window would be redundant. */
12019 XFRAME (selected_frame)->selected_window));
12020 record_unwind_protect (fast_set_selected_frame, selected_frame);
12021 XSETFRAME (frame, f);
12022 fast_set_selected_frame (frame);
12023
12024 /* Build desired tool-bar items from keymaps. */
12025 new_tool_bar
12026 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12027 &new_n_tool_bar);
12028
12029 /* Redisplay the tool-bar if we changed it. */
12030 if (new_n_tool_bar != f->n_tool_bar_items
12031 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12032 {
12033 /* Redisplay that happens asynchronously due to an expose event
12034 may access f->tool_bar_items. Make sure we update both
12035 variables within BLOCK_INPUT so no such event interrupts. */
12036 block_input ();
12037 fset_tool_bar_items (f, new_tool_bar);
12038 f->n_tool_bar_items = new_n_tool_bar;
12039 w->update_mode_line = true;
12040 unblock_input ();
12041 }
12042
12043 unbind_to (count, Qnil);
12044 set_buffer_internal_1 (prev);
12045 }
12046 }
12047 }
12048
12049 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12050
12051 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12052 F's desired tool-bar contents. F->tool_bar_items must have
12053 been set up previously by calling prepare_menu_bars. */
12054
12055 static void
12056 build_desired_tool_bar_string (struct frame *f)
12057 {
12058 int i, size, size_needed;
12059 Lisp_Object image, plist;
12060
12061 image = plist = Qnil;
12062
12063 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12064 Otherwise, make a new string. */
12065
12066 /* The size of the string we might be able to reuse. */
12067 size = (STRINGP (f->desired_tool_bar_string)
12068 ? SCHARS (f->desired_tool_bar_string)
12069 : 0);
12070
12071 /* We need one space in the string for each image. */
12072 size_needed = f->n_tool_bar_items;
12073
12074 /* Reuse f->desired_tool_bar_string, if possible. */
12075 if (size < size_needed || NILP (f->desired_tool_bar_string))
12076 fset_desired_tool_bar_string
12077 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12078 else
12079 {
12080 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12081 Fremove_text_properties (make_number (0), make_number (size),
12082 props, f->desired_tool_bar_string);
12083 }
12084
12085 /* Put a `display' property on the string for the images to display,
12086 put a `menu_item' property on tool-bar items with a value that
12087 is the index of the item in F's tool-bar item vector. */
12088 for (i = 0; i < f->n_tool_bar_items; ++i)
12089 {
12090 #define PROP(IDX) \
12091 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12092
12093 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12094 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12095 int hmargin, vmargin, relief, idx, end;
12096
12097 /* If image is a vector, choose the image according to the
12098 button state. */
12099 image = PROP (TOOL_BAR_ITEM_IMAGES);
12100 if (VECTORP (image))
12101 {
12102 if (enabled_p)
12103 idx = (selected_p
12104 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12105 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12106 else
12107 idx = (selected_p
12108 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12109 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12110
12111 eassert (ASIZE (image) >= idx);
12112 image = AREF (image, idx);
12113 }
12114 else
12115 idx = -1;
12116
12117 /* Ignore invalid image specifications. */
12118 if (!valid_image_p (image))
12119 continue;
12120
12121 /* Display the tool-bar button pressed, or depressed. */
12122 plist = Fcopy_sequence (XCDR (image));
12123
12124 /* Compute margin and relief to draw. */
12125 relief = (tool_bar_button_relief >= 0
12126 ? tool_bar_button_relief
12127 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12128 hmargin = vmargin = relief;
12129
12130 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12131 INT_MAX - max (hmargin, vmargin)))
12132 {
12133 hmargin += XFASTINT (Vtool_bar_button_margin);
12134 vmargin += XFASTINT (Vtool_bar_button_margin);
12135 }
12136 else if (CONSP (Vtool_bar_button_margin))
12137 {
12138 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12139 INT_MAX - hmargin))
12140 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12141
12142 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12143 INT_MAX - vmargin))
12144 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12145 }
12146
12147 if (auto_raise_tool_bar_buttons_p)
12148 {
12149 /* Add a `:relief' property to the image spec if the item is
12150 selected. */
12151 if (selected_p)
12152 {
12153 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12154 hmargin -= relief;
12155 vmargin -= relief;
12156 }
12157 }
12158 else
12159 {
12160 /* If image is selected, display it pressed, i.e. with a
12161 negative relief. If it's not selected, display it with a
12162 raised relief. */
12163 plist = Fplist_put (plist, QCrelief,
12164 (selected_p
12165 ? make_number (-relief)
12166 : make_number (relief)));
12167 hmargin -= relief;
12168 vmargin -= relief;
12169 }
12170
12171 /* Put a margin around the image. */
12172 if (hmargin || vmargin)
12173 {
12174 if (hmargin == vmargin)
12175 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12176 else
12177 plist = Fplist_put (plist, QCmargin,
12178 Fcons (make_number (hmargin),
12179 make_number (vmargin)));
12180 }
12181
12182 /* If button is not enabled, and we don't have special images
12183 for the disabled state, make the image appear disabled by
12184 applying an appropriate algorithm to it. */
12185 if (!enabled_p && idx < 0)
12186 plist = Fplist_put (plist, QCconversion, Qdisabled);
12187
12188 /* Put a `display' text property on the string for the image to
12189 display. Put a `menu-item' property on the string that gives
12190 the start of this item's properties in the tool-bar items
12191 vector. */
12192 image = Fcons (Qimage, plist);
12193 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12194 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12195
12196 /* Let the last image hide all remaining spaces in the tool bar
12197 string. The string can be longer than needed when we reuse a
12198 previous string. */
12199 if (i + 1 == f->n_tool_bar_items)
12200 end = SCHARS (f->desired_tool_bar_string);
12201 else
12202 end = i + 1;
12203 Fadd_text_properties (make_number (i), make_number (end),
12204 props, f->desired_tool_bar_string);
12205 #undef PROP
12206 }
12207 }
12208
12209
12210 /* Display one line of the tool-bar of frame IT->f.
12211
12212 HEIGHT specifies the desired height of the tool-bar line.
12213 If the actual height of the glyph row is less than HEIGHT, the
12214 row's height is increased to HEIGHT, and the icons are centered
12215 vertically in the new height.
12216
12217 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12218 count a final empty row in case the tool-bar width exactly matches
12219 the window width.
12220 */
12221
12222 static void
12223 display_tool_bar_line (struct it *it, int height)
12224 {
12225 struct glyph_row *row = it->glyph_row;
12226 int max_x = it->last_visible_x;
12227 struct glyph *last;
12228
12229 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12230 clear_glyph_row (row);
12231 row->enabled_p = true;
12232 row->y = it->current_y;
12233
12234 /* Note that this isn't made use of if the face hasn't a box,
12235 so there's no need to check the face here. */
12236 it->start_of_box_run_p = true;
12237
12238 while (it->current_x < max_x)
12239 {
12240 int x, n_glyphs_before, i, nglyphs;
12241 struct it it_before;
12242
12243 /* Get the next display element. */
12244 if (!get_next_display_element (it))
12245 {
12246 /* Don't count empty row if we are counting needed tool-bar lines. */
12247 if (height < 0 && !it->hpos)
12248 return;
12249 break;
12250 }
12251
12252 /* Produce glyphs. */
12253 n_glyphs_before = row->used[TEXT_AREA];
12254 it_before = *it;
12255
12256 PRODUCE_GLYPHS (it);
12257
12258 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12259 i = 0;
12260 x = it_before.current_x;
12261 while (i < nglyphs)
12262 {
12263 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12264
12265 if (x + glyph->pixel_width > max_x)
12266 {
12267 /* Glyph doesn't fit on line. Backtrack. */
12268 row->used[TEXT_AREA] = n_glyphs_before;
12269 *it = it_before;
12270 /* If this is the only glyph on this line, it will never fit on the
12271 tool-bar, so skip it. But ensure there is at least one glyph,
12272 so we don't accidentally disable the tool-bar. */
12273 if (n_glyphs_before == 0
12274 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12275 break;
12276 goto out;
12277 }
12278
12279 ++it->hpos;
12280 x += glyph->pixel_width;
12281 ++i;
12282 }
12283
12284 /* Stop at line end. */
12285 if (ITERATOR_AT_END_OF_LINE_P (it))
12286 break;
12287
12288 set_iterator_to_next (it, true);
12289 }
12290
12291 out:;
12292
12293 row->displays_text_p = row->used[TEXT_AREA] != 0;
12294
12295 /* Use default face for the border below the tool bar.
12296
12297 FIXME: When auto-resize-tool-bars is grow-only, there is
12298 no additional border below the possibly empty tool-bar lines.
12299 So to make the extra empty lines look "normal", we have to
12300 use the tool-bar face for the border too. */
12301 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12302 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12303 it->face_id = DEFAULT_FACE_ID;
12304
12305 extend_face_to_end_of_line (it);
12306 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12307 last->right_box_line_p = true;
12308 if (last == row->glyphs[TEXT_AREA])
12309 last->left_box_line_p = true;
12310
12311 /* Make line the desired height and center it vertically. */
12312 if ((height -= it->max_ascent + it->max_descent) > 0)
12313 {
12314 /* Don't add more than one line height. */
12315 height %= FRAME_LINE_HEIGHT (it->f);
12316 it->max_ascent += height / 2;
12317 it->max_descent += (height + 1) / 2;
12318 }
12319
12320 compute_line_metrics (it);
12321
12322 /* If line is empty, make it occupy the rest of the tool-bar. */
12323 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12324 {
12325 row->height = row->phys_height = it->last_visible_y - row->y;
12326 row->visible_height = row->height;
12327 row->ascent = row->phys_ascent = 0;
12328 row->extra_line_spacing = 0;
12329 }
12330
12331 row->full_width_p = true;
12332 row->continued_p = false;
12333 row->truncated_on_left_p = false;
12334 row->truncated_on_right_p = false;
12335
12336 it->current_x = it->hpos = 0;
12337 it->current_y += row->height;
12338 ++it->vpos;
12339 ++it->glyph_row;
12340 }
12341
12342
12343 /* Value is the number of pixels needed to make all tool-bar items of
12344 frame F visible. The actual number of glyph rows needed is
12345 returned in *N_ROWS if non-NULL. */
12346 static int
12347 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12348 {
12349 struct window *w = XWINDOW (f->tool_bar_window);
12350 struct it it;
12351 /* tool_bar_height is called from redisplay_tool_bar after building
12352 the desired matrix, so use (unused) mode-line row as temporary row to
12353 avoid destroying the first tool-bar row. */
12354 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12355
12356 /* Initialize an iterator for iteration over
12357 F->desired_tool_bar_string in the tool-bar window of frame F. */
12358 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12359 temp_row->reversed_p = false;
12360 it.first_visible_x = 0;
12361 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12362 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12363 it.paragraph_embedding = L2R;
12364
12365 while (!ITERATOR_AT_END_P (&it))
12366 {
12367 clear_glyph_row (temp_row);
12368 it.glyph_row = temp_row;
12369 display_tool_bar_line (&it, -1);
12370 }
12371 clear_glyph_row (temp_row);
12372
12373 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12374 if (n_rows)
12375 *n_rows = it.vpos > 0 ? it.vpos : -1;
12376
12377 if (pixelwise)
12378 return it.current_y;
12379 else
12380 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12381 }
12382
12383 #endif /* !USE_GTK && !HAVE_NS */
12384
12385 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12386 0, 2, 0,
12387 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12388 If FRAME is nil or omitted, use the selected frame. Optional argument
12389 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12390 (Lisp_Object frame, Lisp_Object pixelwise)
12391 {
12392 int height = 0;
12393
12394 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12395 struct frame *f = decode_any_frame (frame);
12396
12397 if (WINDOWP (f->tool_bar_window)
12398 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12399 {
12400 update_tool_bar (f, true);
12401 if (f->n_tool_bar_items)
12402 {
12403 build_desired_tool_bar_string (f);
12404 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12405 }
12406 }
12407 #endif
12408
12409 return make_number (height);
12410 }
12411
12412
12413 /* Display the tool-bar of frame F. Value is true if tool-bar's
12414 height should be changed. */
12415 static bool
12416 redisplay_tool_bar (struct frame *f)
12417 {
12418 f->tool_bar_redisplayed = true;
12419 #if defined (USE_GTK) || defined (HAVE_NS)
12420
12421 if (FRAME_EXTERNAL_TOOL_BAR (f))
12422 update_frame_tool_bar (f);
12423 return false;
12424
12425 #else /* !USE_GTK && !HAVE_NS */
12426
12427 struct window *w;
12428 struct it it;
12429 struct glyph_row *row;
12430
12431 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12432 do anything. This means you must start with tool-bar-lines
12433 non-zero to get the auto-sizing effect. Or in other words, you
12434 can turn off tool-bars by specifying tool-bar-lines zero. */
12435 if (!WINDOWP (f->tool_bar_window)
12436 || (w = XWINDOW (f->tool_bar_window),
12437 WINDOW_TOTAL_LINES (w) == 0))
12438 return false;
12439
12440 /* Set up an iterator for the tool-bar window. */
12441 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12442 it.first_visible_x = 0;
12443 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12444 row = it.glyph_row;
12445 row->reversed_p = false;
12446
12447 /* Build a string that represents the contents of the tool-bar. */
12448 build_desired_tool_bar_string (f);
12449 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12450 /* FIXME: This should be controlled by a user option. But it
12451 doesn't make sense to have an R2L tool bar if the menu bar cannot
12452 be drawn also R2L, and making the menu bar R2L is tricky due
12453 toolkit-specific code that implements it. If an R2L tool bar is
12454 ever supported, display_tool_bar_line should also be augmented to
12455 call unproduce_glyphs like display_line and display_string
12456 do. */
12457 it.paragraph_embedding = L2R;
12458
12459 if (f->n_tool_bar_rows == 0)
12460 {
12461 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12462
12463 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12464 {
12465 x_change_tool_bar_height (f, new_height);
12466 frame_default_tool_bar_height = new_height;
12467 /* Always do that now. */
12468 clear_glyph_matrix (w->desired_matrix);
12469 f->fonts_changed = true;
12470 return true;
12471 }
12472 }
12473
12474 /* Display as many lines as needed to display all tool-bar items. */
12475
12476 if (f->n_tool_bar_rows > 0)
12477 {
12478 int border, rows, height, extra;
12479
12480 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12481 border = XINT (Vtool_bar_border);
12482 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12483 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12484 else if (EQ (Vtool_bar_border, Qborder_width))
12485 border = f->border_width;
12486 else
12487 border = 0;
12488 if (border < 0)
12489 border = 0;
12490
12491 rows = f->n_tool_bar_rows;
12492 height = max (1, (it.last_visible_y - border) / rows);
12493 extra = it.last_visible_y - border - height * rows;
12494
12495 while (it.current_y < it.last_visible_y)
12496 {
12497 int h = 0;
12498 if (extra > 0 && rows-- > 0)
12499 {
12500 h = (extra + rows - 1) / rows;
12501 extra -= h;
12502 }
12503 display_tool_bar_line (&it, height + h);
12504 }
12505 }
12506 else
12507 {
12508 while (it.current_y < it.last_visible_y)
12509 display_tool_bar_line (&it, 0);
12510 }
12511
12512 /* It doesn't make much sense to try scrolling in the tool-bar
12513 window, so don't do it. */
12514 w->desired_matrix->no_scrolling_p = true;
12515 w->must_be_updated_p = true;
12516
12517 if (!NILP (Vauto_resize_tool_bars))
12518 {
12519 bool change_height_p = true;
12520
12521 /* If we couldn't display everything, change the tool-bar's
12522 height if there is room for more. */
12523 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12524 change_height_p = true;
12525
12526 /* We subtract 1 because display_tool_bar_line advances the
12527 glyph_row pointer before returning to its caller. We want to
12528 examine the last glyph row produced by
12529 display_tool_bar_line. */
12530 row = it.glyph_row - 1;
12531
12532 /* If there are blank lines at the end, except for a partially
12533 visible blank line at the end that is smaller than
12534 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12535 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12536 && row->height >= FRAME_LINE_HEIGHT (f))
12537 change_height_p = true;
12538
12539 /* If row displays tool-bar items, but is partially visible,
12540 change the tool-bar's height. */
12541 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12542 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12543 change_height_p = true;
12544
12545 /* Resize windows as needed by changing the `tool-bar-lines'
12546 frame parameter. */
12547 if (change_height_p)
12548 {
12549 int nrows;
12550 int new_height = tool_bar_height (f, &nrows, true);
12551
12552 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12553 && !f->minimize_tool_bar_window_p)
12554 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12555 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12556 f->minimize_tool_bar_window_p = false;
12557
12558 if (change_height_p)
12559 {
12560 x_change_tool_bar_height (f, new_height);
12561 frame_default_tool_bar_height = new_height;
12562 clear_glyph_matrix (w->desired_matrix);
12563 f->n_tool_bar_rows = nrows;
12564 f->fonts_changed = true;
12565
12566 return true;
12567 }
12568 }
12569 }
12570
12571 f->minimize_tool_bar_window_p = false;
12572 return false;
12573
12574 #endif /* USE_GTK || HAVE_NS */
12575 }
12576
12577 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12578
12579 /* Get information about the tool-bar item which is displayed in GLYPH
12580 on frame F. Return in *PROP_IDX the index where tool-bar item
12581 properties start in F->tool_bar_items. Value is false if
12582 GLYPH doesn't display a tool-bar item. */
12583
12584 static bool
12585 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12586 {
12587 Lisp_Object prop;
12588 int charpos;
12589
12590 /* This function can be called asynchronously, which means we must
12591 exclude any possibility that Fget_text_property signals an
12592 error. */
12593 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12594 charpos = max (0, charpos);
12595
12596 /* Get the text property `menu-item' at pos. The value of that
12597 property is the start index of this item's properties in
12598 F->tool_bar_items. */
12599 prop = Fget_text_property (make_number (charpos),
12600 Qmenu_item, f->current_tool_bar_string);
12601 if (! INTEGERP (prop))
12602 return false;
12603 *prop_idx = XINT (prop);
12604 return true;
12605 }
12606
12607 \f
12608 /* Get information about the tool-bar item at position X/Y on frame F.
12609 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12610 the current matrix of the tool-bar window of F, or NULL if not
12611 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12612 item in F->tool_bar_items. Value is
12613
12614 -1 if X/Y is not on a tool-bar item
12615 0 if X/Y is on the same item that was highlighted before.
12616 1 otherwise. */
12617
12618 static int
12619 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12620 int *hpos, int *vpos, int *prop_idx)
12621 {
12622 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12623 struct window *w = XWINDOW (f->tool_bar_window);
12624 int area;
12625
12626 /* Find the glyph under X/Y. */
12627 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12628 if (*glyph == NULL)
12629 return -1;
12630
12631 /* Get the start of this tool-bar item's properties in
12632 f->tool_bar_items. */
12633 if (!tool_bar_item_info (f, *glyph, prop_idx))
12634 return -1;
12635
12636 /* Is mouse on the highlighted item? */
12637 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12638 && *vpos >= hlinfo->mouse_face_beg_row
12639 && *vpos <= hlinfo->mouse_face_end_row
12640 && (*vpos > hlinfo->mouse_face_beg_row
12641 || *hpos >= hlinfo->mouse_face_beg_col)
12642 && (*vpos < hlinfo->mouse_face_end_row
12643 || *hpos < hlinfo->mouse_face_end_col
12644 || hlinfo->mouse_face_past_end))
12645 return 0;
12646
12647 return 1;
12648 }
12649
12650
12651 /* EXPORT:
12652 Handle mouse button event on the tool-bar of frame F, at
12653 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12654 false for button release. MODIFIERS is event modifiers for button
12655 release. */
12656
12657 void
12658 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12659 int modifiers)
12660 {
12661 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12662 struct window *w = XWINDOW (f->tool_bar_window);
12663 int hpos, vpos, prop_idx;
12664 struct glyph *glyph;
12665 Lisp_Object enabled_p;
12666 int ts;
12667
12668 /* If not on the highlighted tool-bar item, and mouse-highlight is
12669 non-nil, return. This is so we generate the tool-bar button
12670 click only when the mouse button is released on the same item as
12671 where it was pressed. However, when mouse-highlight is disabled,
12672 generate the click when the button is released regardless of the
12673 highlight, since tool-bar items are not highlighted in that
12674 case. */
12675 frame_to_window_pixel_xy (w, &x, &y);
12676 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12677 if (ts == -1
12678 || (ts != 0 && !NILP (Vmouse_highlight)))
12679 return;
12680
12681 /* When mouse-highlight is off, generate the click for the item
12682 where the button was pressed, disregarding where it was
12683 released. */
12684 if (NILP (Vmouse_highlight) && !down_p)
12685 prop_idx = f->last_tool_bar_item;
12686
12687 /* If item is disabled, do nothing. */
12688 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12689 if (NILP (enabled_p))
12690 return;
12691
12692 if (down_p)
12693 {
12694 /* Show item in pressed state. */
12695 if (!NILP (Vmouse_highlight))
12696 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12697 f->last_tool_bar_item = prop_idx;
12698 }
12699 else
12700 {
12701 Lisp_Object key, frame;
12702 struct input_event event;
12703 EVENT_INIT (event);
12704
12705 /* Show item in released state. */
12706 if (!NILP (Vmouse_highlight))
12707 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12708
12709 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12710
12711 XSETFRAME (frame, f);
12712 event.kind = TOOL_BAR_EVENT;
12713 event.frame_or_window = frame;
12714 event.arg = frame;
12715 kbd_buffer_store_event (&event);
12716
12717 event.kind = TOOL_BAR_EVENT;
12718 event.frame_or_window = frame;
12719 event.arg = key;
12720 event.modifiers = modifiers;
12721 kbd_buffer_store_event (&event);
12722 f->last_tool_bar_item = -1;
12723 }
12724 }
12725
12726
12727 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12728 tool-bar window-relative coordinates X/Y. Called from
12729 note_mouse_highlight. */
12730
12731 static void
12732 note_tool_bar_highlight (struct frame *f, int x, int y)
12733 {
12734 Lisp_Object window = f->tool_bar_window;
12735 struct window *w = XWINDOW (window);
12736 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12737 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12738 int hpos, vpos;
12739 struct glyph *glyph;
12740 struct glyph_row *row;
12741 int i;
12742 Lisp_Object enabled_p;
12743 int prop_idx;
12744 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12745 bool mouse_down_p;
12746 int rc;
12747
12748 /* Function note_mouse_highlight is called with negative X/Y
12749 values when mouse moves outside of the frame. */
12750 if (x <= 0 || y <= 0)
12751 {
12752 clear_mouse_face (hlinfo);
12753 return;
12754 }
12755
12756 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12757 if (rc < 0)
12758 {
12759 /* Not on tool-bar item. */
12760 clear_mouse_face (hlinfo);
12761 return;
12762 }
12763 else if (rc == 0)
12764 /* On same tool-bar item as before. */
12765 goto set_help_echo;
12766
12767 clear_mouse_face (hlinfo);
12768
12769 /* Mouse is down, but on different tool-bar item? */
12770 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12771 && f == dpyinfo->last_mouse_frame);
12772
12773 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12774 return;
12775
12776 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12777
12778 /* If tool-bar item is not enabled, don't highlight it. */
12779 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12780 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12781 {
12782 /* Compute the x-position of the glyph. In front and past the
12783 image is a space. We include this in the highlighted area. */
12784 row = MATRIX_ROW (w->current_matrix, vpos);
12785 for (i = x = 0; i < hpos; ++i)
12786 x += row->glyphs[TEXT_AREA][i].pixel_width;
12787
12788 /* Record this as the current active region. */
12789 hlinfo->mouse_face_beg_col = hpos;
12790 hlinfo->mouse_face_beg_row = vpos;
12791 hlinfo->mouse_face_beg_x = x;
12792 hlinfo->mouse_face_past_end = false;
12793
12794 hlinfo->mouse_face_end_col = hpos + 1;
12795 hlinfo->mouse_face_end_row = vpos;
12796 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12797 hlinfo->mouse_face_window = window;
12798 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12799
12800 /* Display it as active. */
12801 show_mouse_face (hlinfo, draw);
12802 }
12803
12804 set_help_echo:
12805
12806 /* Set help_echo_string to a help string to display for this tool-bar item.
12807 XTread_socket does the rest. */
12808 help_echo_object = help_echo_window = Qnil;
12809 help_echo_pos = -1;
12810 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12811 if (NILP (help_echo_string))
12812 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12813 }
12814
12815 #endif /* !USE_GTK && !HAVE_NS */
12816
12817 #endif /* HAVE_WINDOW_SYSTEM */
12818
12819
12820 \f
12821 /************************************************************************
12822 Horizontal scrolling
12823 ************************************************************************/
12824
12825 /* For all leaf windows in the window tree rooted at WINDOW, set their
12826 hscroll value so that PT is (i) visible in the window, and (ii) so
12827 that it is not within a certain margin at the window's left and
12828 right border. Value is true if any window's hscroll has been
12829 changed. */
12830
12831 static bool
12832 hscroll_window_tree (Lisp_Object window)
12833 {
12834 bool hscrolled_p = false;
12835 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12836 int hscroll_step_abs = 0;
12837 double hscroll_step_rel = 0;
12838
12839 if (hscroll_relative_p)
12840 {
12841 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12842 if (hscroll_step_rel < 0)
12843 {
12844 hscroll_relative_p = false;
12845 hscroll_step_abs = 0;
12846 }
12847 }
12848 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12849 {
12850 hscroll_step_abs = XINT (Vhscroll_step);
12851 if (hscroll_step_abs < 0)
12852 hscroll_step_abs = 0;
12853 }
12854 else
12855 hscroll_step_abs = 0;
12856
12857 while (WINDOWP (window))
12858 {
12859 struct window *w = XWINDOW (window);
12860
12861 if (WINDOWP (w->contents))
12862 hscrolled_p |= hscroll_window_tree (w->contents);
12863 else if (w->cursor.vpos >= 0)
12864 {
12865 int h_margin;
12866 int text_area_width;
12867 struct glyph_row *cursor_row;
12868 struct glyph_row *bottom_row;
12869
12870 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12871 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12872 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12873 else
12874 cursor_row = bottom_row - 1;
12875
12876 if (!cursor_row->enabled_p)
12877 {
12878 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12879 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12880 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12881 else
12882 cursor_row = bottom_row - 1;
12883 }
12884 bool row_r2l_p = cursor_row->reversed_p;
12885
12886 text_area_width = window_box_width (w, TEXT_AREA);
12887
12888 /* Scroll when cursor is inside this scroll margin. */
12889 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12890
12891 /* If the position of this window's point has explicitly
12892 changed, no more suspend auto hscrolling. */
12893 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12894 w->suspend_auto_hscroll = false;
12895
12896 /* Remember window point. */
12897 Fset_marker (w->old_pointm,
12898 ((w == XWINDOW (selected_window))
12899 ? make_number (BUF_PT (XBUFFER (w->contents)))
12900 : Fmarker_position (w->pointm)),
12901 w->contents);
12902
12903 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12904 && !w->suspend_auto_hscroll
12905 /* In some pathological cases, like restoring a window
12906 configuration into a frame that is much smaller than
12907 the one from which the configuration was saved, we
12908 get glyph rows whose start and end have zero buffer
12909 positions, which we cannot handle below. Just skip
12910 such windows. */
12911 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12912 /* For left-to-right rows, hscroll when cursor is either
12913 (i) inside the right hscroll margin, or (ii) if it is
12914 inside the left margin and the window is already
12915 hscrolled. */
12916 && ((!row_r2l_p
12917 && ((w->hscroll && w->cursor.x <= h_margin)
12918 || (cursor_row->enabled_p
12919 && cursor_row->truncated_on_right_p
12920 && (w->cursor.x >= text_area_width - h_margin))))
12921 /* For right-to-left rows, the logic is similar,
12922 except that rules for scrolling to left and right
12923 are reversed. E.g., if cursor.x <= h_margin, we
12924 need to hscroll "to the right" unconditionally,
12925 and that will scroll the screen to the left so as
12926 to reveal the next portion of the row. */
12927 || (row_r2l_p
12928 && ((cursor_row->enabled_p
12929 /* FIXME: It is confusing to set the
12930 truncated_on_right_p flag when R2L rows
12931 are actually truncated on the left. */
12932 && cursor_row->truncated_on_right_p
12933 && w->cursor.x <= h_margin)
12934 || (w->hscroll
12935 && (w->cursor.x >= text_area_width - h_margin))))))
12936 {
12937 struct it it;
12938 ptrdiff_t hscroll;
12939 struct buffer *saved_current_buffer;
12940 ptrdiff_t pt;
12941 int wanted_x;
12942
12943 /* Find point in a display of infinite width. */
12944 saved_current_buffer = current_buffer;
12945 current_buffer = XBUFFER (w->contents);
12946
12947 if (w == XWINDOW (selected_window))
12948 pt = PT;
12949 else
12950 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12951
12952 /* Move iterator to pt starting at cursor_row->start in
12953 a line with infinite width. */
12954 init_to_row_start (&it, w, cursor_row);
12955 it.last_visible_x = INFINITY;
12956 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12957 current_buffer = saved_current_buffer;
12958
12959 /* Position cursor in window. */
12960 if (!hscroll_relative_p && hscroll_step_abs == 0)
12961 hscroll = max (0, (it.current_x
12962 - (ITERATOR_AT_END_OF_LINE_P (&it)
12963 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12964 : (text_area_width / 2))))
12965 / FRAME_COLUMN_WIDTH (it.f);
12966 else if ((!row_r2l_p
12967 && w->cursor.x >= text_area_width - h_margin)
12968 || (row_r2l_p && w->cursor.x <= h_margin))
12969 {
12970 if (hscroll_relative_p)
12971 wanted_x = text_area_width * (1 - hscroll_step_rel)
12972 - h_margin;
12973 else
12974 wanted_x = text_area_width
12975 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12976 - h_margin;
12977 hscroll
12978 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12979 }
12980 else
12981 {
12982 if (hscroll_relative_p)
12983 wanted_x = text_area_width * hscroll_step_rel
12984 + h_margin;
12985 else
12986 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12987 + h_margin;
12988 hscroll
12989 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12990 }
12991 hscroll = max (hscroll, w->min_hscroll);
12992
12993 /* Don't prevent redisplay optimizations if hscroll
12994 hasn't changed, as it will unnecessarily slow down
12995 redisplay. */
12996 if (w->hscroll != hscroll)
12997 {
12998 struct buffer *b = XBUFFER (w->contents);
12999 b->prevent_redisplay_optimizations_p = true;
13000 w->hscroll = hscroll;
13001 hscrolled_p = true;
13002 }
13003 }
13004 }
13005
13006 window = w->next;
13007 }
13008
13009 /* Value is true if hscroll of any leaf window has been changed. */
13010 return hscrolled_p;
13011 }
13012
13013
13014 /* Set hscroll so that cursor is visible and not inside horizontal
13015 scroll margins for all windows in the tree rooted at WINDOW. See
13016 also hscroll_window_tree above. Value is true if any window's
13017 hscroll has been changed. If it has, desired matrices on the frame
13018 of WINDOW are cleared. */
13019
13020 static bool
13021 hscroll_windows (Lisp_Object window)
13022 {
13023 bool hscrolled_p = hscroll_window_tree (window);
13024 if (hscrolled_p)
13025 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13026 return hscrolled_p;
13027 }
13028
13029
13030 \f
13031 /************************************************************************
13032 Redisplay
13033 ************************************************************************/
13034
13035 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13036 This is sometimes handy to have in a debugger session. */
13037
13038 #ifdef GLYPH_DEBUG
13039
13040 /* First and last unchanged row for try_window_id. */
13041
13042 static int debug_first_unchanged_at_end_vpos;
13043 static int debug_last_unchanged_at_beg_vpos;
13044
13045 /* Delta vpos and y. */
13046
13047 static int debug_dvpos, debug_dy;
13048
13049 /* Delta in characters and bytes for try_window_id. */
13050
13051 static ptrdiff_t debug_delta, debug_delta_bytes;
13052
13053 /* Values of window_end_pos and window_end_vpos at the end of
13054 try_window_id. */
13055
13056 static ptrdiff_t debug_end_vpos;
13057
13058 /* Append a string to W->desired_matrix->method. FMT is a printf
13059 format string. If trace_redisplay_p is true also printf the
13060 resulting string to stderr. */
13061
13062 static void debug_method_add (struct window *, char const *, ...)
13063 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13064
13065 static void
13066 debug_method_add (struct window *w, char const *fmt, ...)
13067 {
13068 void *ptr = w;
13069 char *method = w->desired_matrix->method;
13070 int len = strlen (method);
13071 int size = sizeof w->desired_matrix->method;
13072 int remaining = size - len - 1;
13073 va_list ap;
13074
13075 if (len && remaining)
13076 {
13077 method[len] = '|';
13078 --remaining, ++len;
13079 }
13080
13081 va_start (ap, fmt);
13082 vsnprintf (method + len, remaining + 1, fmt, ap);
13083 va_end (ap);
13084
13085 if (trace_redisplay_p)
13086 fprintf (stderr, "%p (%s): %s\n",
13087 ptr,
13088 ((BUFFERP (w->contents)
13089 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13090 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13091 : "no buffer"),
13092 method + len);
13093 }
13094
13095 #endif /* GLYPH_DEBUG */
13096
13097
13098 /* Value is true if all changes in window W, which displays
13099 current_buffer, are in the text between START and END. START is a
13100 buffer position, END is given as a distance from Z. Used in
13101 redisplay_internal for display optimization. */
13102
13103 static bool
13104 text_outside_line_unchanged_p (struct window *w,
13105 ptrdiff_t start, ptrdiff_t end)
13106 {
13107 bool unchanged_p = true;
13108
13109 /* If text or overlays have changed, see where. */
13110 if (window_outdated (w))
13111 {
13112 /* Gap in the line? */
13113 if (GPT < start || Z - GPT < end)
13114 unchanged_p = false;
13115
13116 /* Changes start in front of the line, or end after it? */
13117 if (unchanged_p
13118 && (BEG_UNCHANGED < start - 1
13119 || END_UNCHANGED < end))
13120 unchanged_p = false;
13121
13122 /* If selective display, can't optimize if changes start at the
13123 beginning of the line. */
13124 if (unchanged_p
13125 && INTEGERP (BVAR (current_buffer, selective_display))
13126 && XINT (BVAR (current_buffer, selective_display)) > 0
13127 && (BEG_UNCHANGED < start || GPT <= start))
13128 unchanged_p = false;
13129
13130 /* If there are overlays at the start or end of the line, these
13131 may have overlay strings with newlines in them. A change at
13132 START, for instance, may actually concern the display of such
13133 overlay strings as well, and they are displayed on different
13134 lines. So, quickly rule out this case. (For the future, it
13135 might be desirable to implement something more telling than
13136 just BEG/END_UNCHANGED.) */
13137 if (unchanged_p)
13138 {
13139 if (BEG + BEG_UNCHANGED == start
13140 && overlay_touches_p (start))
13141 unchanged_p = false;
13142 if (END_UNCHANGED == end
13143 && overlay_touches_p (Z - end))
13144 unchanged_p = false;
13145 }
13146
13147 /* Under bidi reordering, adding or deleting a character in the
13148 beginning of a paragraph, before the first strong directional
13149 character, can change the base direction of the paragraph (unless
13150 the buffer specifies a fixed paragraph direction), which will
13151 require redisplaying the whole paragraph. It might be worthwhile
13152 to find the paragraph limits and widen the range of redisplayed
13153 lines to that, but for now just give up this optimization. */
13154 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13155 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13156 unchanged_p = false;
13157 }
13158
13159 return unchanged_p;
13160 }
13161
13162
13163 /* Do a frame update, taking possible shortcuts into account. This is
13164 the main external entry point for redisplay.
13165
13166 If the last redisplay displayed an echo area message and that message
13167 is no longer requested, we clear the echo area or bring back the
13168 mini-buffer if that is in use. */
13169
13170 void
13171 redisplay (void)
13172 {
13173 redisplay_internal ();
13174 }
13175
13176
13177 static Lisp_Object
13178 overlay_arrow_string_or_property (Lisp_Object var)
13179 {
13180 Lisp_Object val;
13181
13182 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13183 return val;
13184
13185 return Voverlay_arrow_string;
13186 }
13187
13188 /* Return true if there are any overlay-arrows in current_buffer. */
13189 static bool
13190 overlay_arrow_in_current_buffer_p (void)
13191 {
13192 Lisp_Object vlist;
13193
13194 for (vlist = Voverlay_arrow_variable_list;
13195 CONSP (vlist);
13196 vlist = XCDR (vlist))
13197 {
13198 Lisp_Object var = XCAR (vlist);
13199 Lisp_Object val;
13200
13201 if (!SYMBOLP (var))
13202 continue;
13203 val = find_symbol_value (var);
13204 if (MARKERP (val)
13205 && current_buffer == XMARKER (val)->buffer)
13206 return true;
13207 }
13208 return false;
13209 }
13210
13211
13212 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13213 has changed. */
13214
13215 static bool
13216 overlay_arrows_changed_p (void)
13217 {
13218 Lisp_Object vlist;
13219
13220 for (vlist = Voverlay_arrow_variable_list;
13221 CONSP (vlist);
13222 vlist = XCDR (vlist))
13223 {
13224 Lisp_Object var = XCAR (vlist);
13225 Lisp_Object val, pstr;
13226
13227 if (!SYMBOLP (var))
13228 continue;
13229 val = find_symbol_value (var);
13230 if (!MARKERP (val))
13231 continue;
13232 if (! EQ (COERCE_MARKER (val),
13233 Fget (var, Qlast_arrow_position))
13234 || ! (pstr = overlay_arrow_string_or_property (var),
13235 EQ (pstr, Fget (var, Qlast_arrow_string))))
13236 return true;
13237 }
13238 return false;
13239 }
13240
13241 /* Mark overlay arrows to be updated on next redisplay. */
13242
13243 static void
13244 update_overlay_arrows (int up_to_date)
13245 {
13246 Lisp_Object vlist;
13247
13248 for (vlist = Voverlay_arrow_variable_list;
13249 CONSP (vlist);
13250 vlist = XCDR (vlist))
13251 {
13252 Lisp_Object var = XCAR (vlist);
13253
13254 if (!SYMBOLP (var))
13255 continue;
13256
13257 if (up_to_date > 0)
13258 {
13259 Lisp_Object val = find_symbol_value (var);
13260 Fput (var, Qlast_arrow_position,
13261 COERCE_MARKER (val));
13262 Fput (var, Qlast_arrow_string,
13263 overlay_arrow_string_or_property (var));
13264 }
13265 else if (up_to_date < 0
13266 || !NILP (Fget (var, Qlast_arrow_position)))
13267 {
13268 Fput (var, Qlast_arrow_position, Qt);
13269 Fput (var, Qlast_arrow_string, Qt);
13270 }
13271 }
13272 }
13273
13274
13275 /* Return overlay arrow string to display at row.
13276 Return integer (bitmap number) for arrow bitmap in left fringe.
13277 Return nil if no overlay arrow. */
13278
13279 static Lisp_Object
13280 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13281 {
13282 Lisp_Object vlist;
13283
13284 for (vlist = Voverlay_arrow_variable_list;
13285 CONSP (vlist);
13286 vlist = XCDR (vlist))
13287 {
13288 Lisp_Object var = XCAR (vlist);
13289 Lisp_Object val;
13290
13291 if (!SYMBOLP (var))
13292 continue;
13293
13294 val = find_symbol_value (var);
13295
13296 if (MARKERP (val)
13297 && current_buffer == XMARKER (val)->buffer
13298 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13299 {
13300 if (FRAME_WINDOW_P (it->f)
13301 /* FIXME: if ROW->reversed_p is set, this should test
13302 the right fringe, not the left one. */
13303 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13304 {
13305 #ifdef HAVE_WINDOW_SYSTEM
13306 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13307 {
13308 int fringe_bitmap = lookup_fringe_bitmap (val);
13309 if (fringe_bitmap != 0)
13310 return make_number (fringe_bitmap);
13311 }
13312 #endif
13313 return make_number (-1); /* Use default arrow bitmap. */
13314 }
13315 return overlay_arrow_string_or_property (var);
13316 }
13317 }
13318
13319 return Qnil;
13320 }
13321
13322 /* Return true if point moved out of or into a composition. Otherwise
13323 return false. PREV_BUF and PREV_PT are the last point buffer and
13324 position. BUF and PT are the current point buffer and position. */
13325
13326 static bool
13327 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13328 struct buffer *buf, ptrdiff_t pt)
13329 {
13330 ptrdiff_t start, end;
13331 Lisp_Object prop;
13332 Lisp_Object buffer;
13333
13334 XSETBUFFER (buffer, buf);
13335 /* Check a composition at the last point if point moved within the
13336 same buffer. */
13337 if (prev_buf == buf)
13338 {
13339 if (prev_pt == pt)
13340 /* Point didn't move. */
13341 return false;
13342
13343 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13344 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13345 && composition_valid_p (start, end, prop)
13346 && start < prev_pt && end > prev_pt)
13347 /* The last point was within the composition. Return true iff
13348 point moved out of the composition. */
13349 return (pt <= start || pt >= end);
13350 }
13351
13352 /* Check a composition at the current point. */
13353 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13354 && find_composition (pt, -1, &start, &end, &prop, buffer)
13355 && composition_valid_p (start, end, prop)
13356 && start < pt && end > pt);
13357 }
13358
13359 /* Reconsider the clip changes of buffer which is displayed in W. */
13360
13361 static void
13362 reconsider_clip_changes (struct window *w)
13363 {
13364 struct buffer *b = XBUFFER (w->contents);
13365
13366 if (b->clip_changed
13367 && w->window_end_valid
13368 && w->current_matrix->buffer == b
13369 && w->current_matrix->zv == BUF_ZV (b)
13370 && w->current_matrix->begv == BUF_BEGV (b))
13371 b->clip_changed = false;
13372
13373 /* If display wasn't paused, and W is not a tool bar window, see if
13374 point has been moved into or out of a composition. In that case,
13375 set b->clip_changed to force updating the screen. If
13376 b->clip_changed has already been set, skip this check. */
13377 if (!b->clip_changed && w->window_end_valid)
13378 {
13379 ptrdiff_t pt = (w == XWINDOW (selected_window)
13380 ? PT : marker_position (w->pointm));
13381
13382 if ((w->current_matrix->buffer != b || pt != w->last_point)
13383 && check_point_in_composition (w->current_matrix->buffer,
13384 w->last_point, b, pt))
13385 b->clip_changed = true;
13386 }
13387 }
13388
13389 static void
13390 propagate_buffer_redisplay (void)
13391 { /* Resetting b->text->redisplay is problematic!
13392 We can't just reset it in the case that some window that displays
13393 it has not been redisplayed; and such a window can stay
13394 unredisplayed for a long time if it's currently invisible.
13395 But we do want to reset it at the end of redisplay otherwise
13396 its displayed windows will keep being redisplayed over and over
13397 again.
13398 So we copy all b->text->redisplay flags up to their windows here,
13399 such that mark_window_display_accurate can safely reset
13400 b->text->redisplay. */
13401 Lisp_Object ws = window_list ();
13402 for (; CONSP (ws); ws = XCDR (ws))
13403 {
13404 struct window *thisw = XWINDOW (XCAR (ws));
13405 struct buffer *thisb = XBUFFER (thisw->contents);
13406 if (thisb->text->redisplay)
13407 thisw->redisplay = true;
13408 }
13409 }
13410
13411 #define STOP_POLLING \
13412 do { if (! polling_stopped_here) stop_polling (); \
13413 polling_stopped_here = true; } while (false)
13414
13415 #define RESUME_POLLING \
13416 do { if (polling_stopped_here) start_polling (); \
13417 polling_stopped_here = false; } while (false)
13418
13419
13420 /* Perhaps in the future avoid recentering windows if it
13421 is not necessary; currently that causes some problems. */
13422
13423 static void
13424 redisplay_internal (void)
13425 {
13426 struct window *w = XWINDOW (selected_window);
13427 struct window *sw;
13428 struct frame *fr;
13429 bool pending;
13430 bool must_finish = false, match_p;
13431 struct text_pos tlbufpos, tlendpos;
13432 int number_of_visible_frames;
13433 ptrdiff_t count;
13434 struct frame *sf;
13435 bool polling_stopped_here = false;
13436 Lisp_Object tail, frame;
13437
13438 /* True means redisplay has to consider all windows on all
13439 frames. False, only selected_window is considered. */
13440 bool consider_all_windows_p;
13441
13442 /* True means redisplay has to redisplay the miniwindow. */
13443 bool update_miniwindow_p = false;
13444
13445 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13446
13447 /* No redisplay if running in batch mode or frame is not yet fully
13448 initialized, or redisplay is explicitly turned off by setting
13449 Vinhibit_redisplay. */
13450 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13451 || !NILP (Vinhibit_redisplay))
13452 return;
13453
13454 /* Don't examine these until after testing Vinhibit_redisplay.
13455 When Emacs is shutting down, perhaps because its connection to
13456 X has dropped, we should not look at them at all. */
13457 fr = XFRAME (w->frame);
13458 sf = SELECTED_FRAME ();
13459
13460 if (!fr->glyphs_initialized_p)
13461 return;
13462
13463 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13464 if (popup_activated ())
13465 return;
13466 #endif
13467
13468 /* I don't think this happens but let's be paranoid. */
13469 if (redisplaying_p)
13470 return;
13471
13472 /* Record a function that clears redisplaying_p
13473 when we leave this function. */
13474 count = SPECPDL_INDEX ();
13475 record_unwind_protect_void (unwind_redisplay);
13476 redisplaying_p = true;
13477 specbind (Qinhibit_free_realized_faces, Qnil);
13478
13479 /* Record this function, so it appears on the profiler's backtraces. */
13480 record_in_backtrace (Qredisplay_internal, 0, 0);
13481
13482 FOR_EACH_FRAME (tail, frame)
13483 XFRAME (frame)->already_hscrolled_p = false;
13484
13485 retry:
13486 /* Remember the currently selected window. */
13487 sw = w;
13488
13489 pending = false;
13490 forget_escape_and_glyphless_faces ();
13491
13492 inhibit_free_realized_faces = false;
13493
13494 /* If face_change, init_iterator will free all realized faces, which
13495 includes the faces referenced from current matrices. So, we
13496 can't reuse current matrices in this case. */
13497 if (face_change)
13498 windows_or_buffers_changed = 47;
13499
13500 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13501 && FRAME_TTY (sf)->previous_frame != sf)
13502 {
13503 /* Since frames on a single ASCII terminal share the same
13504 display area, displaying a different frame means redisplay
13505 the whole thing. */
13506 SET_FRAME_GARBAGED (sf);
13507 #ifndef DOS_NT
13508 set_tty_color_mode (FRAME_TTY (sf), sf);
13509 #endif
13510 FRAME_TTY (sf)->previous_frame = sf;
13511 }
13512
13513 /* Set the visible flags for all frames. Do this before checking for
13514 resized or garbaged frames; they want to know if their frames are
13515 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13516 number_of_visible_frames = 0;
13517
13518 FOR_EACH_FRAME (tail, frame)
13519 {
13520 struct frame *f = XFRAME (frame);
13521
13522 if (FRAME_VISIBLE_P (f))
13523 {
13524 ++number_of_visible_frames;
13525 /* Adjust matrices for visible frames only. */
13526 if (f->fonts_changed)
13527 {
13528 adjust_frame_glyphs (f);
13529 /* Disable all redisplay optimizations for this frame.
13530 This is because adjust_frame_glyphs resets the
13531 enabled_p flag for all glyph rows of all windows, so
13532 many optimizations will fail anyway, and some might
13533 fail to test that flag and do bogus things as
13534 result. */
13535 SET_FRAME_GARBAGED (f);
13536 f->fonts_changed = false;
13537 }
13538 /* If cursor type has been changed on the frame
13539 other than selected, consider all frames. */
13540 if (f != sf && f->cursor_type_changed)
13541 fset_redisplay (f);
13542 }
13543 clear_desired_matrices (f);
13544 }
13545
13546 /* Notice any pending interrupt request to change frame size. */
13547 do_pending_window_change (true);
13548
13549 /* do_pending_window_change could change the selected_window due to
13550 frame resizing which makes the selected window too small. */
13551 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13552 sw = w;
13553
13554 /* Clear frames marked as garbaged. */
13555 clear_garbaged_frames ();
13556
13557 /* Build menubar and tool-bar items. */
13558 if (NILP (Vmemory_full))
13559 prepare_menu_bars ();
13560
13561 reconsider_clip_changes (w);
13562
13563 /* In most cases selected window displays current buffer. */
13564 match_p = XBUFFER (w->contents) == current_buffer;
13565 if (match_p)
13566 {
13567 /* Detect case that we need to write or remove a star in the mode line. */
13568 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13569 w->update_mode_line = true;
13570
13571 if (mode_line_update_needed (w))
13572 w->update_mode_line = true;
13573
13574 /* If reconsider_clip_changes above decided that the narrowing
13575 in the current buffer changed, make sure all other windows
13576 showing that buffer will be redisplayed. */
13577 if (current_buffer->clip_changed)
13578 bset_update_mode_line (current_buffer);
13579 }
13580
13581 /* Normally the message* functions will have already displayed and
13582 updated the echo area, but the frame may have been trashed, or
13583 the update may have been preempted, so display the echo area
13584 again here. Checking message_cleared_p captures the case that
13585 the echo area should be cleared. */
13586 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13587 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13588 || (message_cleared_p
13589 && minibuf_level == 0
13590 /* If the mini-window is currently selected, this means the
13591 echo-area doesn't show through. */
13592 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13593 {
13594 echo_area_display (false);
13595
13596 /* If echo_area_display resizes the mini-window, the redisplay and
13597 window_sizes_changed flags of the selected frame are set, but
13598 it's too late for the hooks in window-size-change-functions,
13599 which have been examined already in prepare_menu_bars. So in
13600 that case we call the hooks here only for the selected frame. */
13601 if (sf->redisplay && FRAME_WINDOW_SIZES_CHANGED (sf))
13602 {
13603 Lisp_Object functions;
13604 ptrdiff_t count1 = SPECPDL_INDEX ();
13605
13606 record_unwind_save_match_data ();
13607
13608 /* Clear flag first in case we get an error below. */
13609 FRAME_WINDOW_SIZES_CHANGED (sf) = false;
13610 functions = Vwindow_size_change_functions;
13611
13612 while (CONSP (functions))
13613 {
13614 if (!EQ (XCAR (functions), Qt))
13615 call1 (XCAR (functions), selected_frame);
13616 functions = XCDR (functions);
13617 }
13618
13619 unbind_to (count1, Qnil);
13620 }
13621
13622 if (message_cleared_p)
13623 update_miniwindow_p = true;
13624
13625 must_finish = true;
13626
13627 /* If we don't display the current message, don't clear the
13628 message_cleared_p flag, because, if we did, we wouldn't clear
13629 the echo area in the next redisplay which doesn't preserve
13630 the echo area. */
13631 if (!display_last_displayed_message_p)
13632 message_cleared_p = false;
13633 }
13634 else if (EQ (selected_window, minibuf_window)
13635 && (current_buffer->clip_changed || window_outdated (w))
13636 && resize_mini_window (w, false))
13637 {
13638 if (sf->redisplay)
13639 {
13640 Lisp_Object functions;
13641 ptrdiff_t count1 = SPECPDL_INDEX ();
13642
13643 record_unwind_save_match_data ();
13644
13645 /* Clear flag first in case we get an error below. */
13646 FRAME_WINDOW_SIZES_CHANGED (sf) = false;
13647 functions = Vwindow_size_change_functions;
13648
13649 while (CONSP (functions))
13650 {
13651 if (!EQ (XCAR (functions), Qt))
13652 call1 (XCAR (functions), selected_frame);
13653 functions = XCDR (functions);
13654 }
13655
13656 unbind_to (count1, Qnil);
13657 }
13658
13659 /* Resized active mini-window to fit the size of what it is
13660 showing if its contents might have changed. */
13661 must_finish = true;
13662
13663 /* If window configuration was changed, frames may have been
13664 marked garbaged. Clear them or we will experience
13665 surprises wrt scrolling. */
13666 clear_garbaged_frames ();
13667 }
13668
13669 if (windows_or_buffers_changed && !update_mode_lines)
13670 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13671 only the windows's contents needs to be refreshed, or whether the
13672 mode-lines also need a refresh. */
13673 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13674 ? REDISPLAY_SOME : 32);
13675
13676 /* If specs for an arrow have changed, do thorough redisplay
13677 to ensure we remove any arrow that should no longer exist. */
13678 if (overlay_arrows_changed_p ())
13679 /* Apparently, this is the only case where we update other windows,
13680 without updating other mode-lines. */
13681 windows_or_buffers_changed = 49;
13682
13683 consider_all_windows_p = (update_mode_lines
13684 || windows_or_buffers_changed);
13685
13686 #define AINC(a,i) \
13687 { \
13688 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13689 if (INTEGERP (entry)) \
13690 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13691 }
13692
13693 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13694 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13695
13696 /* Optimize the case that only the line containing the cursor in the
13697 selected window has changed. Variables starting with this_ are
13698 set in display_line and record information about the line
13699 containing the cursor. */
13700 tlbufpos = this_line_start_pos;
13701 tlendpos = this_line_end_pos;
13702 if (!consider_all_windows_p
13703 && CHARPOS (tlbufpos) > 0
13704 && !w->update_mode_line
13705 && !current_buffer->clip_changed
13706 && !current_buffer->prevent_redisplay_optimizations_p
13707 && FRAME_VISIBLE_P (XFRAME (w->frame))
13708 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13709 && !XFRAME (w->frame)->cursor_type_changed
13710 && !XFRAME (w->frame)->face_change
13711 /* Make sure recorded data applies to current buffer, etc. */
13712 && this_line_buffer == current_buffer
13713 && match_p
13714 && !w->force_start
13715 && !w->optional_new_start
13716 /* Point must be on the line that we have info recorded about. */
13717 && PT >= CHARPOS (tlbufpos)
13718 && PT <= Z - CHARPOS (tlendpos)
13719 /* All text outside that line, including its final newline,
13720 must be unchanged. */
13721 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13722 CHARPOS (tlendpos)))
13723 {
13724 if (CHARPOS (tlbufpos) > BEGV
13725 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13726 && (CHARPOS (tlbufpos) == ZV
13727 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13728 /* Former continuation line has disappeared by becoming empty. */
13729 goto cancel;
13730 else if (window_outdated (w) || MINI_WINDOW_P (w))
13731 {
13732 /* We have to handle the case of continuation around a
13733 wide-column character (see the comment in indent.c around
13734 line 1340).
13735
13736 For instance, in the following case:
13737
13738 -------- Insert --------
13739 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13740 J_I_ ==> J_I_ `^^' are cursors.
13741 ^^ ^^
13742 -------- --------
13743
13744 As we have to redraw the line above, we cannot use this
13745 optimization. */
13746
13747 struct it it;
13748 int line_height_before = this_line_pixel_height;
13749
13750 /* Note that start_display will handle the case that the
13751 line starting at tlbufpos is a continuation line. */
13752 start_display (&it, w, tlbufpos);
13753
13754 /* Implementation note: It this still necessary? */
13755 if (it.current_x != this_line_start_x)
13756 goto cancel;
13757
13758 TRACE ((stderr, "trying display optimization 1\n"));
13759 w->cursor.vpos = -1;
13760 overlay_arrow_seen = false;
13761 it.vpos = this_line_vpos;
13762 it.current_y = this_line_y;
13763 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13764 display_line (&it);
13765
13766 /* If line contains point, is not continued,
13767 and ends at same distance from eob as before, we win. */
13768 if (w->cursor.vpos >= 0
13769 /* Line is not continued, otherwise this_line_start_pos
13770 would have been set to 0 in display_line. */
13771 && CHARPOS (this_line_start_pos)
13772 /* Line ends as before. */
13773 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13774 /* Line has same height as before. Otherwise other lines
13775 would have to be shifted up or down. */
13776 && this_line_pixel_height == line_height_before)
13777 {
13778 /* If this is not the window's last line, we must adjust
13779 the charstarts of the lines below. */
13780 if (it.current_y < it.last_visible_y)
13781 {
13782 struct glyph_row *row
13783 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13784 ptrdiff_t delta, delta_bytes;
13785
13786 /* We used to distinguish between two cases here,
13787 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13788 when the line ends in a newline or the end of the
13789 buffer's accessible portion. But both cases did
13790 the same, so they were collapsed. */
13791 delta = (Z
13792 - CHARPOS (tlendpos)
13793 - MATRIX_ROW_START_CHARPOS (row));
13794 delta_bytes = (Z_BYTE
13795 - BYTEPOS (tlendpos)
13796 - MATRIX_ROW_START_BYTEPOS (row));
13797
13798 increment_matrix_positions (w->current_matrix,
13799 this_line_vpos + 1,
13800 w->current_matrix->nrows,
13801 delta, delta_bytes);
13802 }
13803
13804 /* If this row displays text now but previously didn't,
13805 or vice versa, w->window_end_vpos may have to be
13806 adjusted. */
13807 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13808 {
13809 if (w->window_end_vpos < this_line_vpos)
13810 w->window_end_vpos = this_line_vpos;
13811 }
13812 else if (w->window_end_vpos == this_line_vpos
13813 && this_line_vpos > 0)
13814 w->window_end_vpos = this_line_vpos - 1;
13815 w->window_end_valid = false;
13816
13817 /* Update hint: No need to try to scroll in update_window. */
13818 w->desired_matrix->no_scrolling_p = true;
13819
13820 #ifdef GLYPH_DEBUG
13821 *w->desired_matrix->method = 0;
13822 debug_method_add (w, "optimization 1");
13823 #endif
13824 #ifdef HAVE_WINDOW_SYSTEM
13825 update_window_fringes (w, false);
13826 #endif
13827 goto update;
13828 }
13829 else
13830 goto cancel;
13831 }
13832 else if (/* Cursor position hasn't changed. */
13833 PT == w->last_point
13834 /* Make sure the cursor was last displayed
13835 in this window. Otherwise we have to reposition it. */
13836
13837 /* PXW: Must be converted to pixels, probably. */
13838 && 0 <= w->cursor.vpos
13839 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13840 {
13841 if (!must_finish)
13842 {
13843 do_pending_window_change (true);
13844 /* If selected_window changed, redisplay again. */
13845 if (WINDOWP (selected_window)
13846 && (w = XWINDOW (selected_window)) != sw)
13847 goto retry;
13848
13849 /* We used to always goto end_of_redisplay here, but this
13850 isn't enough if we have a blinking cursor. */
13851 if (w->cursor_off_p == w->last_cursor_off_p)
13852 goto end_of_redisplay;
13853 }
13854 goto update;
13855 }
13856 /* If highlighting the region, or if the cursor is in the echo area,
13857 then we can't just move the cursor. */
13858 else if (NILP (Vshow_trailing_whitespace)
13859 && !cursor_in_echo_area)
13860 {
13861 struct it it;
13862 struct glyph_row *row;
13863
13864 /* Skip from tlbufpos to PT and see where it is. Note that
13865 PT may be in invisible text. If so, we will end at the
13866 next visible position. */
13867 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13868 NULL, DEFAULT_FACE_ID);
13869 it.current_x = this_line_start_x;
13870 it.current_y = this_line_y;
13871 it.vpos = this_line_vpos;
13872
13873 /* The call to move_it_to stops in front of PT, but
13874 moves over before-strings. */
13875 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13876
13877 if (it.vpos == this_line_vpos
13878 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13879 row->enabled_p))
13880 {
13881 eassert (this_line_vpos == it.vpos);
13882 eassert (this_line_y == it.current_y);
13883 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13884 #ifdef GLYPH_DEBUG
13885 *w->desired_matrix->method = 0;
13886 debug_method_add (w, "optimization 3");
13887 #endif
13888 goto update;
13889 }
13890 else
13891 goto cancel;
13892 }
13893
13894 cancel:
13895 /* Text changed drastically or point moved off of line. */
13896 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13897 }
13898
13899 CHARPOS (this_line_start_pos) = 0;
13900 ++clear_face_cache_count;
13901 #ifdef HAVE_WINDOW_SYSTEM
13902 ++clear_image_cache_count;
13903 #endif
13904
13905 /* Build desired matrices, and update the display. If
13906 consider_all_windows_p, do it for all windows on all frames that
13907 require redisplay, as specified by their 'redisplay' flag.
13908 Otherwise do it for selected_window, only. */
13909
13910 if (consider_all_windows_p)
13911 {
13912 FOR_EACH_FRAME (tail, frame)
13913 XFRAME (frame)->updated_p = false;
13914
13915 propagate_buffer_redisplay ();
13916
13917 FOR_EACH_FRAME (tail, frame)
13918 {
13919 struct frame *f = XFRAME (frame);
13920
13921 /* We don't have to do anything for unselected terminal
13922 frames. */
13923 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13924 && !EQ (FRAME_TTY (f)->top_frame, frame))
13925 continue;
13926
13927 retry_frame:
13928 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13929 {
13930 bool gcscrollbars
13931 /* Only GC scrollbars when we redisplay the whole frame. */
13932 = f->redisplay || !REDISPLAY_SOME_P ();
13933 bool f_redisplay_flag = f->redisplay;
13934 /* Mark all the scroll bars to be removed; we'll redeem
13935 the ones we want when we redisplay their windows. */
13936 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13937 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13938
13939 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13940 redisplay_windows (FRAME_ROOT_WINDOW (f));
13941 /* Remember that the invisible frames need to be redisplayed next
13942 time they're visible. */
13943 else if (!REDISPLAY_SOME_P ())
13944 f->redisplay = true;
13945
13946 /* The X error handler may have deleted that frame. */
13947 if (!FRAME_LIVE_P (f))
13948 continue;
13949
13950 /* Any scroll bars which redisplay_windows should have
13951 nuked should now go away. */
13952 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13953 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13954
13955 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13956 {
13957 /* If fonts changed on visible frame, display again. */
13958 if (f->fonts_changed)
13959 {
13960 adjust_frame_glyphs (f);
13961 /* Disable all redisplay optimizations for this
13962 frame. For the reasons, see the comment near
13963 the previous call to adjust_frame_glyphs above. */
13964 SET_FRAME_GARBAGED (f);
13965 f->fonts_changed = false;
13966 goto retry_frame;
13967 }
13968
13969 /* See if we have to hscroll. */
13970 if (!f->already_hscrolled_p)
13971 {
13972 f->already_hscrolled_p = true;
13973 if (hscroll_windows (f->root_window))
13974 goto retry_frame;
13975 }
13976
13977 /* If the frame's redisplay flag was not set before
13978 we went about redisplaying its windows, but it is
13979 set now, that means we employed some redisplay
13980 optimizations inside redisplay_windows, and
13981 bypassed producing some screen lines. But if
13982 f->redisplay is now set, it might mean the old
13983 faces are no longer valid (e.g., if redisplaying
13984 some window called some Lisp which defined a new
13985 face or redefined an existing face), so trying to
13986 use them in update_frame will segfault.
13987 Therefore, we must redisplay this frame. */
13988 if (!f_redisplay_flag && f->redisplay)
13989 goto retry_frame;
13990
13991 /* Prevent various kinds of signals during display
13992 update. stdio is not robust about handling
13993 signals, which can cause an apparent I/O error. */
13994 if (interrupt_input)
13995 unrequest_sigio ();
13996 STOP_POLLING;
13997
13998 pending |= update_frame (f, false, false);
13999 f->cursor_type_changed = false;
14000 f->updated_p = true;
14001 }
14002 }
14003 }
14004
14005 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14006
14007 if (!pending)
14008 {
14009 /* Do the mark_window_display_accurate after all windows have
14010 been redisplayed because this call resets flags in buffers
14011 which are needed for proper redisplay. */
14012 FOR_EACH_FRAME (tail, frame)
14013 {
14014 struct frame *f = XFRAME (frame);
14015 if (f->updated_p)
14016 {
14017 f->redisplay = false;
14018 mark_window_display_accurate (f->root_window, true);
14019 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14020 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14021 }
14022 }
14023 }
14024 }
14025 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14026 {
14027 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14028 struct frame *mini_frame;
14029
14030 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14031 /* Use list_of_error, not Qerror, so that
14032 we catch only errors and don't run the debugger. */
14033 internal_condition_case_1 (redisplay_window_1, selected_window,
14034 list_of_error,
14035 redisplay_window_error);
14036 if (update_miniwindow_p)
14037 internal_condition_case_1 (redisplay_window_1, mini_window,
14038 list_of_error,
14039 redisplay_window_error);
14040
14041 /* Compare desired and current matrices, perform output. */
14042
14043 update:
14044 /* If fonts changed, display again. Likewise if redisplay_window_1
14045 above caused some change (e.g., a change in faces) that requires
14046 considering the entire frame again. */
14047 if (sf->fonts_changed || sf->redisplay)
14048 {
14049 if (sf->redisplay)
14050 {
14051 /* Set this to force a more thorough redisplay.
14052 Otherwise, we might immediately loop back to the
14053 above "else-if" clause (since all the conditions that
14054 led here might still be true), and we will then
14055 infloop, because the selected-frame's redisplay flag
14056 is not (and cannot be) reset. */
14057 windows_or_buffers_changed = 50;
14058 }
14059 goto retry;
14060 }
14061
14062 /* Prevent freeing of realized faces, since desired matrices are
14063 pending that reference the faces we computed and cached. */
14064 inhibit_free_realized_faces = true;
14065
14066 /* Prevent various kinds of signals during display update.
14067 stdio is not robust about handling signals,
14068 which can cause an apparent I/O error. */
14069 if (interrupt_input)
14070 unrequest_sigio ();
14071 STOP_POLLING;
14072
14073 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14074 {
14075 if (hscroll_windows (selected_window))
14076 goto retry;
14077
14078 XWINDOW (selected_window)->must_be_updated_p = true;
14079 pending = update_frame (sf, false, false);
14080 sf->cursor_type_changed = false;
14081 }
14082
14083 /* We may have called echo_area_display at the top of this
14084 function. If the echo area is on another frame, that may
14085 have put text on a frame other than the selected one, so the
14086 above call to update_frame would not have caught it. Catch
14087 it here. */
14088 mini_window = FRAME_MINIBUF_WINDOW (sf);
14089 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14090
14091 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14092 {
14093 XWINDOW (mini_window)->must_be_updated_p = true;
14094 pending |= update_frame (mini_frame, false, false);
14095 mini_frame->cursor_type_changed = false;
14096 if (!pending && hscroll_windows (mini_window))
14097 goto retry;
14098 }
14099 }
14100
14101 /* If display was paused because of pending input, make sure we do a
14102 thorough update the next time. */
14103 if (pending)
14104 {
14105 /* Prevent the optimization at the beginning of
14106 redisplay_internal that tries a single-line update of the
14107 line containing the cursor in the selected window. */
14108 CHARPOS (this_line_start_pos) = 0;
14109
14110 /* Let the overlay arrow be updated the next time. */
14111 update_overlay_arrows (0);
14112
14113 /* If we pause after scrolling, some rows in the current
14114 matrices of some windows are not valid. */
14115 if (!WINDOW_FULL_WIDTH_P (w)
14116 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14117 update_mode_lines = 36;
14118 }
14119 else
14120 {
14121 if (!consider_all_windows_p)
14122 {
14123 /* This has already been done above if
14124 consider_all_windows_p is set. */
14125 if (XBUFFER (w->contents)->text->redisplay
14126 && buffer_window_count (XBUFFER (w->contents)) > 1)
14127 /* This can happen if b->text->redisplay was set during
14128 jit-lock. */
14129 propagate_buffer_redisplay ();
14130 mark_window_display_accurate_1 (w, true);
14131
14132 /* Say overlay arrows are up to date. */
14133 update_overlay_arrows (1);
14134
14135 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14136 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14137 }
14138
14139 update_mode_lines = 0;
14140 windows_or_buffers_changed = 0;
14141 }
14142
14143 /* Start SIGIO interrupts coming again. Having them off during the
14144 code above makes it less likely one will discard output, but not
14145 impossible, since there might be stuff in the system buffer here.
14146 But it is much hairier to try to do anything about that. */
14147 if (interrupt_input)
14148 request_sigio ();
14149 RESUME_POLLING;
14150
14151 /* If a frame has become visible which was not before, redisplay
14152 again, so that we display it. Expose events for such a frame
14153 (which it gets when becoming visible) don't call the parts of
14154 redisplay constructing glyphs, so simply exposing a frame won't
14155 display anything in this case. So, we have to display these
14156 frames here explicitly. */
14157 if (!pending)
14158 {
14159 int new_count = 0;
14160
14161 FOR_EACH_FRAME (tail, frame)
14162 {
14163 if (XFRAME (frame)->visible)
14164 new_count++;
14165 }
14166
14167 if (new_count != number_of_visible_frames)
14168 windows_or_buffers_changed = 52;
14169 }
14170
14171 /* Change frame size now if a change is pending. */
14172 do_pending_window_change (true);
14173
14174 /* If we just did a pending size change, or have additional
14175 visible frames, or selected_window changed, redisplay again. */
14176 if ((windows_or_buffers_changed && !pending)
14177 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14178 goto retry;
14179
14180 /* Clear the face and image caches.
14181
14182 We used to do this only if consider_all_windows_p. But the cache
14183 needs to be cleared if a timer creates images in the current
14184 buffer (e.g. the test case in Bug#6230). */
14185
14186 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14187 {
14188 clear_face_cache (false);
14189 clear_face_cache_count = 0;
14190 }
14191
14192 #ifdef HAVE_WINDOW_SYSTEM
14193 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14194 {
14195 clear_image_caches (Qnil);
14196 clear_image_cache_count = 0;
14197 }
14198 #endif /* HAVE_WINDOW_SYSTEM */
14199
14200 end_of_redisplay:
14201 #ifdef HAVE_NS
14202 ns_set_doc_edited ();
14203 #endif
14204 if (interrupt_input && interrupts_deferred)
14205 request_sigio ();
14206
14207 unbind_to (count, Qnil);
14208 RESUME_POLLING;
14209 }
14210
14211
14212 /* Redisplay, but leave alone any recent echo area message unless
14213 another message has been requested in its place.
14214
14215 This is useful in situations where you need to redisplay but no
14216 user action has occurred, making it inappropriate for the message
14217 area to be cleared. See tracking_off and
14218 wait_reading_process_output for examples of these situations.
14219
14220 FROM_WHERE is an integer saying from where this function was
14221 called. This is useful for debugging. */
14222
14223 void
14224 redisplay_preserve_echo_area (int from_where)
14225 {
14226 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14227
14228 if (!NILP (echo_area_buffer[1]))
14229 {
14230 /* We have a previously displayed message, but no current
14231 message. Redisplay the previous message. */
14232 display_last_displayed_message_p = true;
14233 redisplay_internal ();
14234 display_last_displayed_message_p = false;
14235 }
14236 else
14237 redisplay_internal ();
14238
14239 flush_frame (SELECTED_FRAME ());
14240 }
14241
14242
14243 /* Function registered with record_unwind_protect in redisplay_internal. */
14244
14245 static void
14246 unwind_redisplay (void)
14247 {
14248 redisplaying_p = false;
14249 }
14250
14251
14252 /* Mark the display of leaf window W as accurate or inaccurate.
14253 If ACCURATE_P, mark display of W as accurate.
14254 If !ACCURATE_P, arrange for W to be redisplayed the next
14255 time redisplay_internal is called. */
14256
14257 static void
14258 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14259 {
14260 struct buffer *b = XBUFFER (w->contents);
14261
14262 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14263 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14264 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14265
14266 if (accurate_p)
14267 {
14268 b->clip_changed = false;
14269 b->prevent_redisplay_optimizations_p = false;
14270 eassert (buffer_window_count (b) > 0);
14271 /* Resetting b->text->redisplay is problematic!
14272 In order to make it safer to do it here, redisplay_internal must
14273 have copied all b->text->redisplay to their respective windows. */
14274 b->text->redisplay = false;
14275
14276 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14277 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14278 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14279 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14280
14281 w->current_matrix->buffer = b;
14282 w->current_matrix->begv = BUF_BEGV (b);
14283 w->current_matrix->zv = BUF_ZV (b);
14284
14285 w->last_cursor_vpos = w->cursor.vpos;
14286 w->last_cursor_off_p = w->cursor_off_p;
14287
14288 if (w == XWINDOW (selected_window))
14289 w->last_point = BUF_PT (b);
14290 else
14291 w->last_point = marker_position (w->pointm);
14292
14293 w->window_end_valid = true;
14294 w->update_mode_line = false;
14295 }
14296
14297 w->redisplay = !accurate_p;
14298 }
14299
14300
14301 /* Mark the display of windows in the window tree rooted at WINDOW as
14302 accurate or inaccurate. If ACCURATE_P, mark display of
14303 windows as accurate. If !ACCURATE_P, arrange for windows to
14304 be redisplayed the next time redisplay_internal is called. */
14305
14306 void
14307 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14308 {
14309 struct window *w;
14310
14311 for (; !NILP (window); window = w->next)
14312 {
14313 w = XWINDOW (window);
14314 if (WINDOWP (w->contents))
14315 mark_window_display_accurate (w->contents, accurate_p);
14316 else
14317 mark_window_display_accurate_1 (w, accurate_p);
14318 }
14319
14320 if (accurate_p)
14321 update_overlay_arrows (1);
14322 else
14323 /* Force a thorough redisplay the next time by setting
14324 last_arrow_position and last_arrow_string to t, which is
14325 unequal to any useful value of Voverlay_arrow_... */
14326 update_overlay_arrows (-1);
14327 }
14328
14329
14330 /* Return value in display table DP (Lisp_Char_Table *) for character
14331 C. Since a display table doesn't have any parent, we don't have to
14332 follow parent. Do not call this function directly but use the
14333 macro DISP_CHAR_VECTOR. */
14334
14335 Lisp_Object
14336 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14337 {
14338 Lisp_Object val;
14339
14340 if (ASCII_CHAR_P (c))
14341 {
14342 val = dp->ascii;
14343 if (SUB_CHAR_TABLE_P (val))
14344 val = XSUB_CHAR_TABLE (val)->contents[c];
14345 }
14346 else
14347 {
14348 Lisp_Object table;
14349
14350 XSETCHAR_TABLE (table, dp);
14351 val = char_table_ref (table, c);
14352 }
14353 if (NILP (val))
14354 val = dp->defalt;
14355 return val;
14356 }
14357
14358
14359 \f
14360 /***********************************************************************
14361 Window Redisplay
14362 ***********************************************************************/
14363
14364 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14365
14366 static void
14367 redisplay_windows (Lisp_Object window)
14368 {
14369 while (!NILP (window))
14370 {
14371 struct window *w = XWINDOW (window);
14372
14373 if (WINDOWP (w->contents))
14374 redisplay_windows (w->contents);
14375 else if (BUFFERP (w->contents))
14376 {
14377 displayed_buffer = XBUFFER (w->contents);
14378 /* Use list_of_error, not Qerror, so that
14379 we catch only errors and don't run the debugger. */
14380 internal_condition_case_1 (redisplay_window_0, window,
14381 list_of_error,
14382 redisplay_window_error);
14383 }
14384
14385 window = w->next;
14386 }
14387 }
14388
14389 static Lisp_Object
14390 redisplay_window_error (Lisp_Object ignore)
14391 {
14392 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14393 return Qnil;
14394 }
14395
14396 static Lisp_Object
14397 redisplay_window_0 (Lisp_Object window)
14398 {
14399 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14400 redisplay_window (window, false);
14401 return Qnil;
14402 }
14403
14404 static Lisp_Object
14405 redisplay_window_1 (Lisp_Object window)
14406 {
14407 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14408 redisplay_window (window, true);
14409 return Qnil;
14410 }
14411 \f
14412
14413 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14414 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14415 which positions recorded in ROW differ from current buffer
14416 positions.
14417
14418 Return true iff cursor is on this row. */
14419
14420 static bool
14421 set_cursor_from_row (struct window *w, struct glyph_row *row,
14422 struct glyph_matrix *matrix,
14423 ptrdiff_t delta, ptrdiff_t delta_bytes,
14424 int dy, int dvpos)
14425 {
14426 struct glyph *glyph = row->glyphs[TEXT_AREA];
14427 struct glyph *end = glyph + row->used[TEXT_AREA];
14428 struct glyph *cursor = NULL;
14429 /* The last known character position in row. */
14430 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14431 int x = row->x;
14432 ptrdiff_t pt_old = PT - delta;
14433 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14434 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14435 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14436 /* A glyph beyond the edge of TEXT_AREA which we should never
14437 touch. */
14438 struct glyph *glyphs_end = end;
14439 /* True means we've found a match for cursor position, but that
14440 glyph has the avoid_cursor_p flag set. */
14441 bool match_with_avoid_cursor = false;
14442 /* True means we've seen at least one glyph that came from a
14443 display string. */
14444 bool string_seen = false;
14445 /* Largest and smallest buffer positions seen so far during scan of
14446 glyph row. */
14447 ptrdiff_t bpos_max = pos_before;
14448 ptrdiff_t bpos_min = pos_after;
14449 /* Last buffer position covered by an overlay string with an integer
14450 `cursor' property. */
14451 ptrdiff_t bpos_covered = 0;
14452 /* True means the display string on which to display the cursor
14453 comes from a text property, not from an overlay. */
14454 bool string_from_text_prop = false;
14455
14456 /* Don't even try doing anything if called for a mode-line or
14457 header-line row, since the rest of the code isn't prepared to
14458 deal with such calamities. */
14459 eassert (!row->mode_line_p);
14460 if (row->mode_line_p)
14461 return false;
14462
14463 /* Skip over glyphs not having an object at the start and the end of
14464 the row. These are special glyphs like truncation marks on
14465 terminal frames. */
14466 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14467 {
14468 if (!row->reversed_p)
14469 {
14470 while (glyph < end
14471 && NILP (glyph->object)
14472 && glyph->charpos < 0)
14473 {
14474 x += glyph->pixel_width;
14475 ++glyph;
14476 }
14477 while (end > glyph
14478 && NILP ((end - 1)->object)
14479 /* CHARPOS is zero for blanks and stretch glyphs
14480 inserted by extend_face_to_end_of_line. */
14481 && (end - 1)->charpos <= 0)
14482 --end;
14483 glyph_before = glyph - 1;
14484 glyph_after = end;
14485 }
14486 else
14487 {
14488 struct glyph *g;
14489
14490 /* If the glyph row is reversed, we need to process it from back
14491 to front, so swap the edge pointers. */
14492 glyphs_end = end = glyph - 1;
14493 glyph += row->used[TEXT_AREA] - 1;
14494
14495 while (glyph > end + 1
14496 && NILP (glyph->object)
14497 && glyph->charpos < 0)
14498 {
14499 --glyph;
14500 x -= glyph->pixel_width;
14501 }
14502 if (NILP (glyph->object) && glyph->charpos < 0)
14503 --glyph;
14504 /* By default, in reversed rows we put the cursor on the
14505 rightmost (first in the reading order) glyph. */
14506 for (g = end + 1; g < glyph; g++)
14507 x += g->pixel_width;
14508 while (end < glyph
14509 && NILP ((end + 1)->object)
14510 && (end + 1)->charpos <= 0)
14511 ++end;
14512 glyph_before = glyph + 1;
14513 glyph_after = end;
14514 }
14515 }
14516 else if (row->reversed_p)
14517 {
14518 /* In R2L rows that don't display text, put the cursor on the
14519 rightmost glyph. Case in point: an empty last line that is
14520 part of an R2L paragraph. */
14521 cursor = end - 1;
14522 /* Avoid placing the cursor on the last glyph of the row, where
14523 on terminal frames we hold the vertical border between
14524 adjacent windows. */
14525 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14526 && !WINDOW_RIGHTMOST_P (w)
14527 && cursor == row->glyphs[LAST_AREA] - 1)
14528 cursor--;
14529 x = -1; /* will be computed below, at label compute_x */
14530 }
14531
14532 /* Step 1: Try to find the glyph whose character position
14533 corresponds to point. If that's not possible, find 2 glyphs
14534 whose character positions are the closest to point, one before
14535 point, the other after it. */
14536 if (!row->reversed_p)
14537 while (/* not marched to end of glyph row */
14538 glyph < end
14539 /* glyph was not inserted by redisplay for internal purposes */
14540 && !NILP (glyph->object))
14541 {
14542 if (BUFFERP (glyph->object))
14543 {
14544 ptrdiff_t dpos = glyph->charpos - pt_old;
14545
14546 if (glyph->charpos > bpos_max)
14547 bpos_max = glyph->charpos;
14548 if (glyph->charpos < bpos_min)
14549 bpos_min = glyph->charpos;
14550 if (!glyph->avoid_cursor_p)
14551 {
14552 /* If we hit point, we've found the glyph on which to
14553 display the cursor. */
14554 if (dpos == 0)
14555 {
14556 match_with_avoid_cursor = false;
14557 break;
14558 }
14559 /* See if we've found a better approximation to
14560 POS_BEFORE or to POS_AFTER. */
14561 if (0 > dpos && dpos > pos_before - pt_old)
14562 {
14563 pos_before = glyph->charpos;
14564 glyph_before = glyph;
14565 }
14566 else if (0 < dpos && dpos < pos_after - pt_old)
14567 {
14568 pos_after = glyph->charpos;
14569 glyph_after = glyph;
14570 }
14571 }
14572 else if (dpos == 0)
14573 match_with_avoid_cursor = true;
14574 }
14575 else if (STRINGP (glyph->object))
14576 {
14577 Lisp_Object chprop;
14578 ptrdiff_t glyph_pos = glyph->charpos;
14579
14580 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14581 glyph->object);
14582 if (!NILP (chprop))
14583 {
14584 /* If the string came from a `display' text property,
14585 look up the buffer position of that property and
14586 use that position to update bpos_max, as if we
14587 actually saw such a position in one of the row's
14588 glyphs. This helps with supporting integer values
14589 of `cursor' property on the display string in
14590 situations where most or all of the row's buffer
14591 text is completely covered by display properties,
14592 so that no glyph with valid buffer positions is
14593 ever seen in the row. */
14594 ptrdiff_t prop_pos =
14595 string_buffer_position_lim (glyph->object, pos_before,
14596 pos_after, false);
14597
14598 if (prop_pos >= pos_before)
14599 bpos_max = prop_pos;
14600 }
14601 if (INTEGERP (chprop))
14602 {
14603 bpos_covered = bpos_max + XINT (chprop);
14604 /* If the `cursor' property covers buffer positions up
14605 to and including point, we should display cursor on
14606 this glyph. Note that, if a `cursor' property on one
14607 of the string's characters has an integer value, we
14608 will break out of the loop below _before_ we get to
14609 the position match above. IOW, integer values of
14610 the `cursor' property override the "exact match for
14611 point" strategy of positioning the cursor. */
14612 /* Implementation note: bpos_max == pt_old when, e.g.,
14613 we are in an empty line, where bpos_max is set to
14614 MATRIX_ROW_START_CHARPOS, see above. */
14615 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14616 {
14617 cursor = glyph;
14618 break;
14619 }
14620 }
14621
14622 string_seen = true;
14623 }
14624 x += glyph->pixel_width;
14625 ++glyph;
14626 }
14627 else if (glyph > end) /* row is reversed */
14628 while (!NILP (glyph->object))
14629 {
14630 if (BUFFERP (glyph->object))
14631 {
14632 ptrdiff_t dpos = glyph->charpos - pt_old;
14633
14634 if (glyph->charpos > bpos_max)
14635 bpos_max = glyph->charpos;
14636 if (glyph->charpos < bpos_min)
14637 bpos_min = glyph->charpos;
14638 if (!glyph->avoid_cursor_p)
14639 {
14640 if (dpos == 0)
14641 {
14642 match_with_avoid_cursor = false;
14643 break;
14644 }
14645 if (0 > dpos && dpos > pos_before - pt_old)
14646 {
14647 pos_before = glyph->charpos;
14648 glyph_before = glyph;
14649 }
14650 else if (0 < dpos && dpos < pos_after - pt_old)
14651 {
14652 pos_after = glyph->charpos;
14653 glyph_after = glyph;
14654 }
14655 }
14656 else if (dpos == 0)
14657 match_with_avoid_cursor = true;
14658 }
14659 else if (STRINGP (glyph->object))
14660 {
14661 Lisp_Object chprop;
14662 ptrdiff_t glyph_pos = glyph->charpos;
14663
14664 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14665 glyph->object);
14666 if (!NILP (chprop))
14667 {
14668 ptrdiff_t prop_pos =
14669 string_buffer_position_lim (glyph->object, pos_before,
14670 pos_after, false);
14671
14672 if (prop_pos >= pos_before)
14673 bpos_max = prop_pos;
14674 }
14675 if (INTEGERP (chprop))
14676 {
14677 bpos_covered = bpos_max + XINT (chprop);
14678 /* If the `cursor' property covers buffer positions up
14679 to and including point, we should display cursor on
14680 this glyph. */
14681 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14682 {
14683 cursor = glyph;
14684 break;
14685 }
14686 }
14687 string_seen = true;
14688 }
14689 --glyph;
14690 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14691 {
14692 x--; /* can't use any pixel_width */
14693 break;
14694 }
14695 x -= glyph->pixel_width;
14696 }
14697
14698 /* Step 2: If we didn't find an exact match for point, we need to
14699 look for a proper place to put the cursor among glyphs between
14700 GLYPH_BEFORE and GLYPH_AFTER. */
14701 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14702 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14703 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14704 {
14705 /* An empty line has a single glyph whose OBJECT is nil and
14706 whose CHARPOS is the position of a newline on that line.
14707 Note that on a TTY, there are more glyphs after that, which
14708 were produced by extend_face_to_end_of_line, but their
14709 CHARPOS is zero or negative. */
14710 bool empty_line_p =
14711 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14712 && NILP (glyph->object) && glyph->charpos > 0
14713 /* On a TTY, continued and truncated rows also have a glyph at
14714 their end whose OBJECT is nil and whose CHARPOS is
14715 positive (the continuation and truncation glyphs), but such
14716 rows are obviously not "empty". */
14717 && !(row->continued_p || row->truncated_on_right_p));
14718
14719 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14720 {
14721 ptrdiff_t ellipsis_pos;
14722
14723 /* Scan back over the ellipsis glyphs. */
14724 if (!row->reversed_p)
14725 {
14726 ellipsis_pos = (glyph - 1)->charpos;
14727 while (glyph > row->glyphs[TEXT_AREA]
14728 && (glyph - 1)->charpos == ellipsis_pos)
14729 glyph--, x -= glyph->pixel_width;
14730 /* That loop always goes one position too far, including
14731 the glyph before the ellipsis. So scan forward over
14732 that one. */
14733 x += glyph->pixel_width;
14734 glyph++;
14735 }
14736 else /* row is reversed */
14737 {
14738 ellipsis_pos = (glyph + 1)->charpos;
14739 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14740 && (glyph + 1)->charpos == ellipsis_pos)
14741 glyph++, x += glyph->pixel_width;
14742 x -= glyph->pixel_width;
14743 glyph--;
14744 }
14745 }
14746 else if (match_with_avoid_cursor)
14747 {
14748 cursor = glyph_after;
14749 x = -1;
14750 }
14751 else if (string_seen)
14752 {
14753 int incr = row->reversed_p ? -1 : +1;
14754
14755 /* Need to find the glyph that came out of a string which is
14756 present at point. That glyph is somewhere between
14757 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14758 positioned between POS_BEFORE and POS_AFTER in the
14759 buffer. */
14760 struct glyph *start, *stop;
14761 ptrdiff_t pos = pos_before;
14762
14763 x = -1;
14764
14765 /* If the row ends in a newline from a display string,
14766 reordering could have moved the glyphs belonging to the
14767 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14768 in this case we extend the search to the last glyph in
14769 the row that was not inserted by redisplay. */
14770 if (row->ends_in_newline_from_string_p)
14771 {
14772 glyph_after = end;
14773 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14774 }
14775
14776 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14777 correspond to POS_BEFORE and POS_AFTER, respectively. We
14778 need START and STOP in the order that corresponds to the
14779 row's direction as given by its reversed_p flag. If the
14780 directionality of characters between POS_BEFORE and
14781 POS_AFTER is the opposite of the row's base direction,
14782 these characters will have been reordered for display,
14783 and we need to reverse START and STOP. */
14784 if (!row->reversed_p)
14785 {
14786 start = min (glyph_before, glyph_after);
14787 stop = max (glyph_before, glyph_after);
14788 }
14789 else
14790 {
14791 start = max (glyph_before, glyph_after);
14792 stop = min (glyph_before, glyph_after);
14793 }
14794 for (glyph = start + incr;
14795 row->reversed_p ? glyph > stop : glyph < stop; )
14796 {
14797
14798 /* Any glyphs that come from the buffer are here because
14799 of bidi reordering. Skip them, and only pay
14800 attention to glyphs that came from some string. */
14801 if (STRINGP (glyph->object))
14802 {
14803 Lisp_Object str;
14804 ptrdiff_t tem;
14805 /* If the display property covers the newline, we
14806 need to search for it one position farther. */
14807 ptrdiff_t lim = pos_after
14808 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14809
14810 string_from_text_prop = false;
14811 str = glyph->object;
14812 tem = string_buffer_position_lim (str, pos, lim, false);
14813 if (tem == 0 /* from overlay */
14814 || pos <= tem)
14815 {
14816 /* If the string from which this glyph came is
14817 found in the buffer at point, or at position
14818 that is closer to point than pos_after, then
14819 we've found the glyph we've been looking for.
14820 If it comes from an overlay (tem == 0), and
14821 it has the `cursor' property on one of its
14822 glyphs, record that glyph as a candidate for
14823 displaying the cursor. (As in the
14824 unidirectional version, we will display the
14825 cursor on the last candidate we find.) */
14826 if (tem == 0
14827 || tem == pt_old
14828 || (tem - pt_old > 0 && tem < pos_after))
14829 {
14830 /* The glyphs from this string could have
14831 been reordered. Find the one with the
14832 smallest string position. Or there could
14833 be a character in the string with the
14834 `cursor' property, which means display
14835 cursor on that character's glyph. */
14836 ptrdiff_t strpos = glyph->charpos;
14837
14838 if (tem)
14839 {
14840 cursor = glyph;
14841 string_from_text_prop = true;
14842 }
14843 for ( ;
14844 (row->reversed_p ? glyph > stop : glyph < stop)
14845 && EQ (glyph->object, str);
14846 glyph += incr)
14847 {
14848 Lisp_Object cprop;
14849 ptrdiff_t gpos = glyph->charpos;
14850
14851 cprop = Fget_char_property (make_number (gpos),
14852 Qcursor,
14853 glyph->object);
14854 if (!NILP (cprop))
14855 {
14856 cursor = glyph;
14857 break;
14858 }
14859 if (tem && glyph->charpos < strpos)
14860 {
14861 strpos = glyph->charpos;
14862 cursor = glyph;
14863 }
14864 }
14865
14866 if (tem == pt_old
14867 || (tem - pt_old > 0 && tem < pos_after))
14868 goto compute_x;
14869 }
14870 if (tem)
14871 pos = tem + 1; /* don't find previous instances */
14872 }
14873 /* This string is not what we want; skip all of the
14874 glyphs that came from it. */
14875 while ((row->reversed_p ? glyph > stop : glyph < stop)
14876 && EQ (glyph->object, str))
14877 glyph += incr;
14878 }
14879 else
14880 glyph += incr;
14881 }
14882
14883 /* If we reached the end of the line, and END was from a string,
14884 the cursor is not on this line. */
14885 if (cursor == NULL
14886 && (row->reversed_p ? glyph <= end : glyph >= end)
14887 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14888 && STRINGP (end->object)
14889 && row->continued_p)
14890 return false;
14891 }
14892 /* A truncated row may not include PT among its character positions.
14893 Setting the cursor inside the scroll margin will trigger
14894 recalculation of hscroll in hscroll_window_tree. But if a
14895 display string covers point, defer to the string-handling
14896 code below to figure this out. */
14897 else if (row->truncated_on_left_p && pt_old < bpos_min)
14898 {
14899 cursor = glyph_before;
14900 x = -1;
14901 }
14902 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14903 /* Zero-width characters produce no glyphs. */
14904 || (!empty_line_p
14905 && (row->reversed_p
14906 ? glyph_after > glyphs_end
14907 : glyph_after < glyphs_end)))
14908 {
14909 cursor = glyph_after;
14910 x = -1;
14911 }
14912 }
14913
14914 compute_x:
14915 if (cursor != NULL)
14916 glyph = cursor;
14917 else if (glyph == glyphs_end
14918 && pos_before == pos_after
14919 && STRINGP ((row->reversed_p
14920 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14921 : row->glyphs[TEXT_AREA])->object))
14922 {
14923 /* If all the glyphs of this row came from strings, put the
14924 cursor on the first glyph of the row. This avoids having the
14925 cursor outside of the text area in this very rare and hard
14926 use case. */
14927 glyph =
14928 row->reversed_p
14929 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14930 : row->glyphs[TEXT_AREA];
14931 }
14932 if (x < 0)
14933 {
14934 struct glyph *g;
14935
14936 /* Need to compute x that corresponds to GLYPH. */
14937 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14938 {
14939 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14940 emacs_abort ();
14941 x += g->pixel_width;
14942 }
14943 }
14944
14945 /* ROW could be part of a continued line, which, under bidi
14946 reordering, might have other rows whose start and end charpos
14947 occlude point. Only set w->cursor if we found a better
14948 approximation to the cursor position than we have from previously
14949 examined candidate rows belonging to the same continued line. */
14950 if (/* We already have a candidate row. */
14951 w->cursor.vpos >= 0
14952 /* That candidate is not the row we are processing. */
14953 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14954 /* Make sure cursor.vpos specifies a row whose start and end
14955 charpos occlude point, and it is valid candidate for being a
14956 cursor-row. This is because some callers of this function
14957 leave cursor.vpos at the row where the cursor was displayed
14958 during the last redisplay cycle. */
14959 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14960 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14961 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14962 {
14963 struct glyph *g1
14964 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14965
14966 /* Don't consider glyphs that are outside TEXT_AREA. */
14967 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14968 return false;
14969 /* Keep the candidate whose buffer position is the closest to
14970 point or has the `cursor' property. */
14971 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14972 w->cursor.hpos >= 0
14973 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14974 && ((BUFFERP (g1->object)
14975 && (g1->charpos == pt_old /* An exact match always wins. */
14976 || (BUFFERP (glyph->object)
14977 && eabs (g1->charpos - pt_old)
14978 < eabs (glyph->charpos - pt_old))))
14979 /* Previous candidate is a glyph from a string that has
14980 a non-nil `cursor' property. */
14981 || (STRINGP (g1->object)
14982 && (!NILP (Fget_char_property (make_number (g1->charpos),
14983 Qcursor, g1->object))
14984 /* Previous candidate is from the same display
14985 string as this one, and the display string
14986 came from a text property. */
14987 || (EQ (g1->object, glyph->object)
14988 && string_from_text_prop)
14989 /* this candidate is from newline and its
14990 position is not an exact match */
14991 || (NILP (glyph->object)
14992 && glyph->charpos != pt_old)))))
14993 return false;
14994 /* If this candidate gives an exact match, use that. */
14995 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14996 /* If this candidate is a glyph created for the
14997 terminating newline of a line, and point is on that
14998 newline, it wins because it's an exact match. */
14999 || (!row->continued_p
15000 && NILP (glyph->object)
15001 && glyph->charpos == 0
15002 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15003 /* Otherwise, keep the candidate that comes from a row
15004 spanning less buffer positions. This may win when one or
15005 both candidate positions are on glyphs that came from
15006 display strings, for which we cannot compare buffer
15007 positions. */
15008 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15009 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15010 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15011 return false;
15012 }
15013 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15014 w->cursor.x = x;
15015 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15016 w->cursor.y = row->y + dy;
15017
15018 if (w == XWINDOW (selected_window))
15019 {
15020 if (!row->continued_p
15021 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15022 && row->x == 0)
15023 {
15024 this_line_buffer = XBUFFER (w->contents);
15025
15026 CHARPOS (this_line_start_pos)
15027 = MATRIX_ROW_START_CHARPOS (row) + delta;
15028 BYTEPOS (this_line_start_pos)
15029 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15030
15031 CHARPOS (this_line_end_pos)
15032 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15033 BYTEPOS (this_line_end_pos)
15034 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15035
15036 this_line_y = w->cursor.y;
15037 this_line_pixel_height = row->height;
15038 this_line_vpos = w->cursor.vpos;
15039 this_line_start_x = row->x;
15040 }
15041 else
15042 CHARPOS (this_line_start_pos) = 0;
15043 }
15044
15045 return true;
15046 }
15047
15048
15049 /* Run window scroll functions, if any, for WINDOW with new window
15050 start STARTP. Sets the window start of WINDOW to that position.
15051
15052 We assume that the window's buffer is really current. */
15053
15054 static struct text_pos
15055 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15056 {
15057 struct window *w = XWINDOW (window);
15058 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15059
15060 eassert (current_buffer == XBUFFER (w->contents));
15061
15062 if (!NILP (Vwindow_scroll_functions))
15063 {
15064 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15065 make_number (CHARPOS (startp)));
15066 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15067 /* In case the hook functions switch buffers. */
15068 set_buffer_internal (XBUFFER (w->contents));
15069 }
15070
15071 return startp;
15072 }
15073
15074
15075 /* Make sure the line containing the cursor is fully visible.
15076 A value of true means there is nothing to be done.
15077 (Either the line is fully visible, or it cannot be made so,
15078 or we cannot tell.)
15079
15080 If FORCE_P, return false even if partial visible cursor row
15081 is higher than window.
15082
15083 If CURRENT_MATRIX_P, use the information from the
15084 window's current glyph matrix; otherwise use the desired glyph
15085 matrix.
15086
15087 A value of false means the caller should do scrolling
15088 as if point had gone off the screen. */
15089
15090 static bool
15091 cursor_row_fully_visible_p (struct window *w, bool force_p,
15092 bool current_matrix_p)
15093 {
15094 struct glyph_matrix *matrix;
15095 struct glyph_row *row;
15096 int window_height;
15097
15098 if (!make_cursor_line_fully_visible_p)
15099 return true;
15100
15101 /* It's not always possible to find the cursor, e.g, when a window
15102 is full of overlay strings. Don't do anything in that case. */
15103 if (w->cursor.vpos < 0)
15104 return true;
15105
15106 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15107 row = MATRIX_ROW (matrix, w->cursor.vpos);
15108
15109 /* If the cursor row is not partially visible, there's nothing to do. */
15110 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15111 return true;
15112
15113 /* If the row the cursor is in is taller than the window's height,
15114 it's not clear what to do, so do nothing. */
15115 window_height = window_box_height (w);
15116 if (row->height >= window_height)
15117 {
15118 if (!force_p || MINI_WINDOW_P (w)
15119 || w->vscroll || w->cursor.vpos == 0)
15120 return true;
15121 }
15122 return false;
15123 }
15124
15125
15126 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15127 means only WINDOW is redisplayed in redisplay_internal.
15128 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15129 in redisplay_window to bring a partially visible line into view in
15130 the case that only the cursor has moved.
15131
15132 LAST_LINE_MISFIT should be true if we're scrolling because the
15133 last screen line's vertical height extends past the end of the screen.
15134
15135 Value is
15136
15137 1 if scrolling succeeded
15138
15139 0 if scrolling didn't find point.
15140
15141 -1 if new fonts have been loaded so that we must interrupt
15142 redisplay, adjust glyph matrices, and try again. */
15143
15144 enum
15145 {
15146 SCROLLING_SUCCESS,
15147 SCROLLING_FAILED,
15148 SCROLLING_NEED_LARGER_MATRICES
15149 };
15150
15151 /* If scroll-conservatively is more than this, never recenter.
15152
15153 If you change this, don't forget to update the doc string of
15154 `scroll-conservatively' and the Emacs manual. */
15155 #define SCROLL_LIMIT 100
15156
15157 static int
15158 try_scrolling (Lisp_Object window, bool just_this_one_p,
15159 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15160 bool temp_scroll_step, bool last_line_misfit)
15161 {
15162 struct window *w = XWINDOW (window);
15163 struct frame *f = XFRAME (w->frame);
15164 struct text_pos pos, startp;
15165 struct it it;
15166 int this_scroll_margin, scroll_max, rc, height;
15167 int dy = 0, amount_to_scroll = 0;
15168 bool scroll_down_p = false;
15169 int extra_scroll_margin_lines = last_line_misfit;
15170 Lisp_Object aggressive;
15171 /* We will never try scrolling more than this number of lines. */
15172 int scroll_limit = SCROLL_LIMIT;
15173 int frame_line_height = default_line_pixel_height (w);
15174 int window_total_lines
15175 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15176
15177 #ifdef GLYPH_DEBUG
15178 debug_method_add (w, "try_scrolling");
15179 #endif
15180
15181 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15182
15183 /* Compute scroll margin height in pixels. We scroll when point is
15184 within this distance from the top or bottom of the window. */
15185 if (scroll_margin > 0)
15186 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15187 * frame_line_height;
15188 else
15189 this_scroll_margin = 0;
15190
15191 /* Force arg_scroll_conservatively to have a reasonable value, to
15192 avoid scrolling too far away with slow move_it_* functions. Note
15193 that the user can supply scroll-conservatively equal to
15194 `most-positive-fixnum', which can be larger than INT_MAX. */
15195 if (arg_scroll_conservatively > scroll_limit)
15196 {
15197 arg_scroll_conservatively = scroll_limit + 1;
15198 scroll_max = scroll_limit * frame_line_height;
15199 }
15200 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15201 /* Compute how much we should try to scroll maximally to bring
15202 point into view. */
15203 scroll_max = (max (scroll_step,
15204 max (arg_scroll_conservatively, temp_scroll_step))
15205 * frame_line_height);
15206 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15207 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15208 /* We're trying to scroll because of aggressive scrolling but no
15209 scroll_step is set. Choose an arbitrary one. */
15210 scroll_max = 10 * frame_line_height;
15211 else
15212 scroll_max = 0;
15213
15214 too_near_end:
15215
15216 /* Decide whether to scroll down. */
15217 if (PT > CHARPOS (startp))
15218 {
15219 int scroll_margin_y;
15220
15221 /* Compute the pixel ypos of the scroll margin, then move IT to
15222 either that ypos or PT, whichever comes first. */
15223 start_display (&it, w, startp);
15224 scroll_margin_y = it.last_visible_y - this_scroll_margin
15225 - frame_line_height * extra_scroll_margin_lines;
15226 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15227 (MOVE_TO_POS | MOVE_TO_Y));
15228
15229 if (PT > CHARPOS (it.current.pos))
15230 {
15231 int y0 = line_bottom_y (&it);
15232 /* Compute how many pixels below window bottom to stop searching
15233 for PT. This avoids costly search for PT that is far away if
15234 the user limited scrolling by a small number of lines, but
15235 always finds PT if scroll_conservatively is set to a large
15236 number, such as most-positive-fixnum. */
15237 int slack = max (scroll_max, 10 * frame_line_height);
15238 int y_to_move = it.last_visible_y + slack;
15239
15240 /* Compute the distance from the scroll margin to PT or to
15241 the scroll limit, whichever comes first. This should
15242 include the height of the cursor line, to make that line
15243 fully visible. */
15244 move_it_to (&it, PT, -1, y_to_move,
15245 -1, MOVE_TO_POS | MOVE_TO_Y);
15246 dy = line_bottom_y (&it) - y0;
15247
15248 if (dy > scroll_max)
15249 return SCROLLING_FAILED;
15250
15251 if (dy > 0)
15252 scroll_down_p = true;
15253 }
15254 }
15255
15256 if (scroll_down_p)
15257 {
15258 /* Point is in or below the bottom scroll margin, so move the
15259 window start down. If scrolling conservatively, move it just
15260 enough down to make point visible. If scroll_step is set,
15261 move it down by scroll_step. */
15262 if (arg_scroll_conservatively)
15263 amount_to_scroll
15264 = min (max (dy, frame_line_height),
15265 frame_line_height * arg_scroll_conservatively);
15266 else if (scroll_step || temp_scroll_step)
15267 amount_to_scroll = scroll_max;
15268 else
15269 {
15270 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15271 height = WINDOW_BOX_TEXT_HEIGHT (w);
15272 if (NUMBERP (aggressive))
15273 {
15274 double float_amount = XFLOATINT (aggressive) * height;
15275 int aggressive_scroll = float_amount;
15276 if (aggressive_scroll == 0 && float_amount > 0)
15277 aggressive_scroll = 1;
15278 /* Don't let point enter the scroll margin near top of
15279 the window. This could happen if the value of
15280 scroll_up_aggressively is too large and there are
15281 non-zero margins, because scroll_up_aggressively
15282 means put point that fraction of window height
15283 _from_the_bottom_margin_. */
15284 if (aggressive_scroll + 2 * this_scroll_margin > height)
15285 aggressive_scroll = height - 2 * this_scroll_margin;
15286 amount_to_scroll = dy + aggressive_scroll;
15287 }
15288 }
15289
15290 if (amount_to_scroll <= 0)
15291 return SCROLLING_FAILED;
15292
15293 start_display (&it, w, startp);
15294 if (arg_scroll_conservatively <= scroll_limit)
15295 move_it_vertically (&it, amount_to_scroll);
15296 else
15297 {
15298 /* Extra precision for users who set scroll-conservatively
15299 to a large number: make sure the amount we scroll
15300 the window start is never less than amount_to_scroll,
15301 which was computed as distance from window bottom to
15302 point. This matters when lines at window top and lines
15303 below window bottom have different height. */
15304 struct it it1;
15305 void *it1data = NULL;
15306 /* We use a temporary it1 because line_bottom_y can modify
15307 its argument, if it moves one line down; see there. */
15308 int start_y;
15309
15310 SAVE_IT (it1, it, it1data);
15311 start_y = line_bottom_y (&it1);
15312 do {
15313 RESTORE_IT (&it, &it, it1data);
15314 move_it_by_lines (&it, 1);
15315 SAVE_IT (it1, it, it1data);
15316 } while (IT_CHARPOS (it) < ZV
15317 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15318 bidi_unshelve_cache (it1data, true);
15319 }
15320
15321 /* If STARTP is unchanged, move it down another screen line. */
15322 if (IT_CHARPOS (it) == CHARPOS (startp))
15323 move_it_by_lines (&it, 1);
15324 startp = it.current.pos;
15325 }
15326 else
15327 {
15328 struct text_pos scroll_margin_pos = startp;
15329 int y_offset = 0;
15330
15331 /* See if point is inside the scroll margin at the top of the
15332 window. */
15333 if (this_scroll_margin)
15334 {
15335 int y_start;
15336
15337 start_display (&it, w, startp);
15338 y_start = it.current_y;
15339 move_it_vertically (&it, this_scroll_margin);
15340 scroll_margin_pos = it.current.pos;
15341 /* If we didn't move enough before hitting ZV, request
15342 additional amount of scroll, to move point out of the
15343 scroll margin. */
15344 if (IT_CHARPOS (it) == ZV
15345 && it.current_y - y_start < this_scroll_margin)
15346 y_offset = this_scroll_margin - (it.current_y - y_start);
15347 }
15348
15349 if (PT < CHARPOS (scroll_margin_pos))
15350 {
15351 /* Point is in the scroll margin at the top of the window or
15352 above what is displayed in the window. */
15353 int y0, y_to_move;
15354
15355 /* Compute the vertical distance from PT to the scroll
15356 margin position. Move as far as scroll_max allows, or
15357 one screenful, or 10 screen lines, whichever is largest.
15358 Give up if distance is greater than scroll_max or if we
15359 didn't reach the scroll margin position. */
15360 SET_TEXT_POS (pos, PT, PT_BYTE);
15361 start_display (&it, w, pos);
15362 y0 = it.current_y;
15363 y_to_move = max (it.last_visible_y,
15364 max (scroll_max, 10 * frame_line_height));
15365 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15366 y_to_move, -1,
15367 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15368 dy = it.current_y - y0;
15369 if (dy > scroll_max
15370 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15371 return SCROLLING_FAILED;
15372
15373 /* Additional scroll for when ZV was too close to point. */
15374 dy += y_offset;
15375
15376 /* Compute new window start. */
15377 start_display (&it, w, startp);
15378
15379 if (arg_scroll_conservatively)
15380 amount_to_scroll = max (dy, frame_line_height
15381 * max (scroll_step, temp_scroll_step));
15382 else if (scroll_step || temp_scroll_step)
15383 amount_to_scroll = scroll_max;
15384 else
15385 {
15386 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15387 height = WINDOW_BOX_TEXT_HEIGHT (w);
15388 if (NUMBERP (aggressive))
15389 {
15390 double float_amount = XFLOATINT (aggressive) * height;
15391 int aggressive_scroll = float_amount;
15392 if (aggressive_scroll == 0 && float_amount > 0)
15393 aggressive_scroll = 1;
15394 /* Don't let point enter the scroll margin near
15395 bottom of the window, if the value of
15396 scroll_down_aggressively happens to be too
15397 large. */
15398 if (aggressive_scroll + 2 * this_scroll_margin > height)
15399 aggressive_scroll = height - 2 * this_scroll_margin;
15400 amount_to_scroll = dy + aggressive_scroll;
15401 }
15402 }
15403
15404 if (amount_to_scroll <= 0)
15405 return SCROLLING_FAILED;
15406
15407 move_it_vertically_backward (&it, amount_to_scroll);
15408 startp = it.current.pos;
15409 }
15410 }
15411
15412 /* Run window scroll functions. */
15413 startp = run_window_scroll_functions (window, startp);
15414
15415 /* Display the window. Give up if new fonts are loaded, or if point
15416 doesn't appear. */
15417 if (!try_window (window, startp, 0))
15418 rc = SCROLLING_NEED_LARGER_MATRICES;
15419 else if (w->cursor.vpos < 0)
15420 {
15421 clear_glyph_matrix (w->desired_matrix);
15422 rc = SCROLLING_FAILED;
15423 }
15424 else
15425 {
15426 /* Maybe forget recorded base line for line number display. */
15427 if (!just_this_one_p
15428 || current_buffer->clip_changed
15429 || BEG_UNCHANGED < CHARPOS (startp))
15430 w->base_line_number = 0;
15431
15432 /* If cursor ends up on a partially visible line,
15433 treat that as being off the bottom of the screen. */
15434 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15435 false)
15436 /* It's possible that the cursor is on the first line of the
15437 buffer, which is partially obscured due to a vscroll
15438 (Bug#7537). In that case, avoid looping forever. */
15439 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15440 {
15441 clear_glyph_matrix (w->desired_matrix);
15442 ++extra_scroll_margin_lines;
15443 goto too_near_end;
15444 }
15445 rc = SCROLLING_SUCCESS;
15446 }
15447
15448 return rc;
15449 }
15450
15451
15452 /* Compute a suitable window start for window W if display of W starts
15453 on a continuation line. Value is true if a new window start
15454 was computed.
15455
15456 The new window start will be computed, based on W's width, starting
15457 from the start of the continued line. It is the start of the
15458 screen line with the minimum distance from the old start W->start. */
15459
15460 static bool
15461 compute_window_start_on_continuation_line (struct window *w)
15462 {
15463 struct text_pos pos, start_pos;
15464 bool window_start_changed_p = false;
15465
15466 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15467
15468 /* If window start is on a continuation line... Window start may be
15469 < BEGV in case there's invisible text at the start of the
15470 buffer (M-x rmail, for example). */
15471 if (CHARPOS (start_pos) > BEGV
15472 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15473 {
15474 struct it it;
15475 struct glyph_row *row;
15476
15477 /* Handle the case that the window start is out of range. */
15478 if (CHARPOS (start_pos) < BEGV)
15479 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15480 else if (CHARPOS (start_pos) > ZV)
15481 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15482
15483 /* Find the start of the continued line. This should be fast
15484 because find_newline is fast (newline cache). */
15485 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15486 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15487 row, DEFAULT_FACE_ID);
15488 reseat_at_previous_visible_line_start (&it);
15489
15490 /* If the line start is "too far" away from the window start,
15491 say it takes too much time to compute a new window start. */
15492 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15493 /* PXW: Do we need upper bounds here? */
15494 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15495 {
15496 int min_distance, distance;
15497
15498 /* Move forward by display lines to find the new window
15499 start. If window width was enlarged, the new start can
15500 be expected to be > the old start. If window width was
15501 decreased, the new window start will be < the old start.
15502 So, we're looking for the display line start with the
15503 minimum distance from the old window start. */
15504 pos = it.current.pos;
15505 min_distance = INFINITY;
15506 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15507 distance < min_distance)
15508 {
15509 min_distance = distance;
15510 pos = it.current.pos;
15511 if (it.line_wrap == WORD_WRAP)
15512 {
15513 /* Under WORD_WRAP, move_it_by_lines is likely to
15514 overshoot and stop not at the first, but the
15515 second character from the left margin. So in
15516 that case, we need a more tight control on the X
15517 coordinate of the iterator than move_it_by_lines
15518 promises in its contract. The method is to first
15519 go to the last (rightmost) visible character of a
15520 line, then move to the leftmost character on the
15521 next line in a separate call. */
15522 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15523 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15524 move_it_to (&it, ZV, 0,
15525 it.current_y + it.max_ascent + it.max_descent, -1,
15526 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15527 }
15528 else
15529 move_it_by_lines (&it, 1);
15530 }
15531
15532 /* Set the window start there. */
15533 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15534 window_start_changed_p = true;
15535 }
15536 }
15537
15538 return window_start_changed_p;
15539 }
15540
15541
15542 /* Try cursor movement in case text has not changed in window WINDOW,
15543 with window start STARTP. Value is
15544
15545 CURSOR_MOVEMENT_SUCCESS if successful
15546
15547 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15548
15549 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15550 display. *SCROLL_STEP is set to true, under certain circumstances, if
15551 we want to scroll as if scroll-step were set to 1. See the code.
15552
15553 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15554 which case we have to abort this redisplay, and adjust matrices
15555 first. */
15556
15557 enum
15558 {
15559 CURSOR_MOVEMENT_SUCCESS,
15560 CURSOR_MOVEMENT_CANNOT_BE_USED,
15561 CURSOR_MOVEMENT_MUST_SCROLL,
15562 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15563 };
15564
15565 static int
15566 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15567 bool *scroll_step)
15568 {
15569 struct window *w = XWINDOW (window);
15570 struct frame *f = XFRAME (w->frame);
15571 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15572
15573 #ifdef GLYPH_DEBUG
15574 if (inhibit_try_cursor_movement)
15575 return rc;
15576 #endif
15577
15578 /* Previously, there was a check for Lisp integer in the
15579 if-statement below. Now, this field is converted to
15580 ptrdiff_t, thus zero means invalid position in a buffer. */
15581 eassert (w->last_point > 0);
15582 /* Likewise there was a check whether window_end_vpos is nil or larger
15583 than the window. Now window_end_vpos is int and so never nil, but
15584 let's leave eassert to check whether it fits in the window. */
15585 eassert (!w->window_end_valid
15586 || w->window_end_vpos < w->current_matrix->nrows);
15587
15588 /* Handle case where text has not changed, only point, and it has
15589 not moved off the frame. */
15590 if (/* Point may be in this window. */
15591 PT >= CHARPOS (startp)
15592 /* Selective display hasn't changed. */
15593 && !current_buffer->clip_changed
15594 /* Function force-mode-line-update is used to force a thorough
15595 redisplay. It sets either windows_or_buffers_changed or
15596 update_mode_lines. So don't take a shortcut here for these
15597 cases. */
15598 && !update_mode_lines
15599 && !windows_or_buffers_changed
15600 && !f->cursor_type_changed
15601 && NILP (Vshow_trailing_whitespace)
15602 /* This code is not used for mini-buffer for the sake of the case
15603 of redisplaying to replace an echo area message; since in
15604 that case the mini-buffer contents per se are usually
15605 unchanged. This code is of no real use in the mini-buffer
15606 since the handling of this_line_start_pos, etc., in redisplay
15607 handles the same cases. */
15608 && !EQ (window, minibuf_window)
15609 && (FRAME_WINDOW_P (f)
15610 || !overlay_arrow_in_current_buffer_p ()))
15611 {
15612 int this_scroll_margin, top_scroll_margin;
15613 struct glyph_row *row = NULL;
15614 int frame_line_height = default_line_pixel_height (w);
15615 int window_total_lines
15616 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15617
15618 #ifdef GLYPH_DEBUG
15619 debug_method_add (w, "cursor movement");
15620 #endif
15621
15622 /* Scroll if point within this distance from the top or bottom
15623 of the window. This is a pixel value. */
15624 if (scroll_margin > 0)
15625 {
15626 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15627 this_scroll_margin *= frame_line_height;
15628 }
15629 else
15630 this_scroll_margin = 0;
15631
15632 top_scroll_margin = this_scroll_margin;
15633 if (WINDOW_WANTS_HEADER_LINE_P (w))
15634 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15635
15636 /* Start with the row the cursor was displayed during the last
15637 not paused redisplay. Give up if that row is not valid. */
15638 if (w->last_cursor_vpos < 0
15639 || w->last_cursor_vpos >= w->current_matrix->nrows)
15640 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15641 else
15642 {
15643 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15644 if (row->mode_line_p)
15645 ++row;
15646 if (!row->enabled_p)
15647 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15648 }
15649
15650 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15651 {
15652 bool scroll_p = false, must_scroll = false;
15653 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15654
15655 if (PT > w->last_point)
15656 {
15657 /* Point has moved forward. */
15658 while (MATRIX_ROW_END_CHARPOS (row) < PT
15659 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15660 {
15661 eassert (row->enabled_p);
15662 ++row;
15663 }
15664
15665 /* If the end position of a row equals the start
15666 position of the next row, and PT is at that position,
15667 we would rather display cursor in the next line. */
15668 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15669 && MATRIX_ROW_END_CHARPOS (row) == PT
15670 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15671 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15672 && !cursor_row_p (row))
15673 ++row;
15674
15675 /* If within the scroll margin, scroll. Note that
15676 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15677 the next line would be drawn, and that
15678 this_scroll_margin can be zero. */
15679 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15680 || PT > MATRIX_ROW_END_CHARPOS (row)
15681 /* Line is completely visible last line in window
15682 and PT is to be set in the next line. */
15683 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15684 && PT == MATRIX_ROW_END_CHARPOS (row)
15685 && !row->ends_at_zv_p
15686 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15687 scroll_p = true;
15688 }
15689 else if (PT < w->last_point)
15690 {
15691 /* Cursor has to be moved backward. Note that PT >=
15692 CHARPOS (startp) because of the outer if-statement. */
15693 while (!row->mode_line_p
15694 && (MATRIX_ROW_START_CHARPOS (row) > PT
15695 || (MATRIX_ROW_START_CHARPOS (row) == PT
15696 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15697 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15698 row > w->current_matrix->rows
15699 && (row-1)->ends_in_newline_from_string_p))))
15700 && (row->y > top_scroll_margin
15701 || CHARPOS (startp) == BEGV))
15702 {
15703 eassert (row->enabled_p);
15704 --row;
15705 }
15706
15707 /* Consider the following case: Window starts at BEGV,
15708 there is invisible, intangible text at BEGV, so that
15709 display starts at some point START > BEGV. It can
15710 happen that we are called with PT somewhere between
15711 BEGV and START. Try to handle that case. */
15712 if (row < w->current_matrix->rows
15713 || row->mode_line_p)
15714 {
15715 row = w->current_matrix->rows;
15716 if (row->mode_line_p)
15717 ++row;
15718 }
15719
15720 /* Due to newlines in overlay strings, we may have to
15721 skip forward over overlay strings. */
15722 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15723 && MATRIX_ROW_END_CHARPOS (row) == PT
15724 && !cursor_row_p (row))
15725 ++row;
15726
15727 /* If within the scroll margin, scroll. */
15728 if (row->y < top_scroll_margin
15729 && CHARPOS (startp) != BEGV)
15730 scroll_p = true;
15731 }
15732 else
15733 {
15734 /* Cursor did not move. So don't scroll even if cursor line
15735 is partially visible, as it was so before. */
15736 rc = CURSOR_MOVEMENT_SUCCESS;
15737 }
15738
15739 if (PT < MATRIX_ROW_START_CHARPOS (row)
15740 || PT > MATRIX_ROW_END_CHARPOS (row))
15741 {
15742 /* if PT is not in the glyph row, give up. */
15743 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15744 must_scroll = true;
15745 }
15746 else if (rc != CURSOR_MOVEMENT_SUCCESS
15747 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15748 {
15749 struct glyph_row *row1;
15750
15751 /* If rows are bidi-reordered and point moved, back up
15752 until we find a row that does not belong to a
15753 continuation line. This is because we must consider
15754 all rows of a continued line as candidates for the
15755 new cursor positioning, since row start and end
15756 positions change non-linearly with vertical position
15757 in such rows. */
15758 /* FIXME: Revisit this when glyph ``spilling'' in
15759 continuation lines' rows is implemented for
15760 bidi-reordered rows. */
15761 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15762 MATRIX_ROW_CONTINUATION_LINE_P (row);
15763 --row)
15764 {
15765 /* If we hit the beginning of the displayed portion
15766 without finding the first row of a continued
15767 line, give up. */
15768 if (row <= row1)
15769 {
15770 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15771 break;
15772 }
15773 eassert (row->enabled_p);
15774 }
15775 }
15776 if (must_scroll)
15777 ;
15778 else if (rc != CURSOR_MOVEMENT_SUCCESS
15779 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15780 /* Make sure this isn't a header line by any chance, since
15781 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15782 && !row->mode_line_p
15783 && make_cursor_line_fully_visible_p)
15784 {
15785 if (PT == MATRIX_ROW_END_CHARPOS (row)
15786 && !row->ends_at_zv_p
15787 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15788 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15789 else if (row->height > window_box_height (w))
15790 {
15791 /* If we end up in a partially visible line, let's
15792 make it fully visible, except when it's taller
15793 than the window, in which case we can't do much
15794 about it. */
15795 *scroll_step = true;
15796 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15797 }
15798 else
15799 {
15800 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15801 if (!cursor_row_fully_visible_p (w, false, true))
15802 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15803 else
15804 rc = CURSOR_MOVEMENT_SUCCESS;
15805 }
15806 }
15807 else if (scroll_p)
15808 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15809 else if (rc != CURSOR_MOVEMENT_SUCCESS
15810 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15811 {
15812 /* With bidi-reordered rows, there could be more than
15813 one candidate row whose start and end positions
15814 occlude point. We need to let set_cursor_from_row
15815 find the best candidate. */
15816 /* FIXME: Revisit this when glyph ``spilling'' in
15817 continuation lines' rows is implemented for
15818 bidi-reordered rows. */
15819 bool rv = false;
15820
15821 do
15822 {
15823 bool at_zv_p = false, exact_match_p = false;
15824
15825 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15826 && PT <= MATRIX_ROW_END_CHARPOS (row)
15827 && cursor_row_p (row))
15828 rv |= set_cursor_from_row (w, row, w->current_matrix,
15829 0, 0, 0, 0);
15830 /* As soon as we've found the exact match for point,
15831 or the first suitable row whose ends_at_zv_p flag
15832 is set, we are done. */
15833 if (rv)
15834 {
15835 at_zv_p = MATRIX_ROW (w->current_matrix,
15836 w->cursor.vpos)->ends_at_zv_p;
15837 if (!at_zv_p
15838 && w->cursor.hpos >= 0
15839 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15840 w->cursor.vpos))
15841 {
15842 struct glyph_row *candidate =
15843 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15844 struct glyph *g =
15845 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15846 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15847
15848 exact_match_p =
15849 (BUFFERP (g->object) && g->charpos == PT)
15850 || (NILP (g->object)
15851 && (g->charpos == PT
15852 || (g->charpos == 0 && endpos - 1 == PT)));
15853 }
15854 if (at_zv_p || exact_match_p)
15855 {
15856 rc = CURSOR_MOVEMENT_SUCCESS;
15857 break;
15858 }
15859 }
15860 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15861 break;
15862 ++row;
15863 }
15864 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15865 || row->continued_p)
15866 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15867 || (MATRIX_ROW_START_CHARPOS (row) == PT
15868 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15869 /* If we didn't find any candidate rows, or exited the
15870 loop before all the candidates were examined, signal
15871 to the caller that this method failed. */
15872 if (rc != CURSOR_MOVEMENT_SUCCESS
15873 && !(rv
15874 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15875 && !row->continued_p))
15876 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15877 else if (rv)
15878 rc = CURSOR_MOVEMENT_SUCCESS;
15879 }
15880 else
15881 {
15882 do
15883 {
15884 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15885 {
15886 rc = CURSOR_MOVEMENT_SUCCESS;
15887 break;
15888 }
15889 ++row;
15890 }
15891 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15892 && MATRIX_ROW_START_CHARPOS (row) == PT
15893 && cursor_row_p (row));
15894 }
15895 }
15896 }
15897
15898 return rc;
15899 }
15900
15901
15902 void
15903 set_vertical_scroll_bar (struct window *w)
15904 {
15905 ptrdiff_t start, end, whole;
15906
15907 /* Calculate the start and end positions for the current window.
15908 At some point, it would be nice to choose between scrollbars
15909 which reflect the whole buffer size, with special markers
15910 indicating narrowing, and scrollbars which reflect only the
15911 visible region.
15912
15913 Note that mini-buffers sometimes aren't displaying any text. */
15914 if (!MINI_WINDOW_P (w)
15915 || (w == XWINDOW (minibuf_window)
15916 && NILP (echo_area_buffer[0])))
15917 {
15918 struct buffer *buf = XBUFFER (w->contents);
15919 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15920 start = marker_position (w->start) - BUF_BEGV (buf);
15921 /* I don't think this is guaranteed to be right. For the
15922 moment, we'll pretend it is. */
15923 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15924
15925 if (end < start)
15926 end = start;
15927 if (whole < (end - start))
15928 whole = end - start;
15929 }
15930 else
15931 start = end = whole = 0;
15932
15933 /* Indicate what this scroll bar ought to be displaying now. */
15934 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15935 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15936 (w, end - start, whole, start);
15937 }
15938
15939
15940 void
15941 set_horizontal_scroll_bar (struct window *w)
15942 {
15943 int start, end, whole, portion;
15944
15945 if (!MINI_WINDOW_P (w)
15946 || (w == XWINDOW (minibuf_window)
15947 && NILP (echo_area_buffer[0])))
15948 {
15949 struct buffer *b = XBUFFER (w->contents);
15950 struct buffer *old_buffer = NULL;
15951 struct it it;
15952 struct text_pos startp;
15953
15954 if (b != current_buffer)
15955 {
15956 old_buffer = current_buffer;
15957 set_buffer_internal (b);
15958 }
15959
15960 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15961 start_display (&it, w, startp);
15962 it.last_visible_x = INT_MAX;
15963 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15964 MOVE_TO_X | MOVE_TO_Y);
15965 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15966 window_box_height (w), -1,
15967 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15968
15969 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15970 end = start + window_box_width (w, TEXT_AREA);
15971 portion = end - start;
15972 /* After enlarging a horizontally scrolled window such that it
15973 gets at least as wide as the text it contains, make sure that
15974 the thumb doesn't fill the entire scroll bar so we can still
15975 drag it back to see the entire text. */
15976 whole = max (whole, end);
15977
15978 if (it.bidi_p)
15979 {
15980 Lisp_Object pdir;
15981
15982 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15983 if (EQ (pdir, Qright_to_left))
15984 {
15985 start = whole - end;
15986 end = start + portion;
15987 }
15988 }
15989
15990 if (old_buffer)
15991 set_buffer_internal (old_buffer);
15992 }
15993 else
15994 start = end = whole = portion = 0;
15995
15996 w->hscroll_whole = whole;
15997
15998 /* Indicate what this scroll bar ought to be displaying now. */
15999 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16000 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16001 (w, portion, whole, start);
16002 }
16003
16004
16005 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16006 selected_window is redisplayed.
16007
16008 We can return without actually redisplaying the window if fonts has been
16009 changed on window's frame. In that case, redisplay_internal will retry.
16010
16011 As one of the important parts of redisplaying a window, we need to
16012 decide whether the previous window-start position (stored in the
16013 window's w->start marker position) is still valid, and if it isn't,
16014 recompute it. Some details about that:
16015
16016 . The previous window-start could be in a continuation line, in
16017 which case we need to recompute it when the window width
16018 changes. See compute_window_start_on_continuation_line and its
16019 call below.
16020
16021 . The text that changed since last redisplay could include the
16022 previous window-start position. In that case, we try to salvage
16023 what we can from the current glyph matrix by calling
16024 try_scrolling, which see.
16025
16026 . Some Emacs command could force us to use a specific window-start
16027 position by setting the window's force_start flag, or gently
16028 propose doing that by setting the window's optional_new_start
16029 flag. In these cases, we try using the specified start point if
16030 that succeeds (i.e. the window desired matrix is successfully
16031 recomputed, and point location is within the window). In case
16032 of optional_new_start, we first check if the specified start
16033 position is feasible, i.e. if it will allow point to be
16034 displayed in the window. If using the specified start point
16035 fails, e.g., if new fonts are needed to be loaded, we abort the
16036 redisplay cycle and leave it up to the next cycle to figure out
16037 things.
16038
16039 . Note that the window's force_start flag is sometimes set by
16040 redisplay itself, when it decides that the previous window start
16041 point is fine and should be kept. Search for "goto force_start"
16042 below to see the details. Like the values of window-start
16043 specified outside of redisplay, these internally-deduced values
16044 are tested for feasibility, and ignored if found to be
16045 unfeasible.
16046
16047 . Note that the function try_window, used to completely redisplay
16048 a window, accepts the window's start point as its argument.
16049 This is used several times in the redisplay code to control
16050 where the window start will be, according to user options such
16051 as scroll-conservatively, and also to ensure the screen line
16052 showing point will be fully (as opposed to partially) visible on
16053 display. */
16054
16055 static void
16056 redisplay_window (Lisp_Object window, bool just_this_one_p)
16057 {
16058 struct window *w = XWINDOW (window);
16059 struct frame *f = XFRAME (w->frame);
16060 struct buffer *buffer = XBUFFER (w->contents);
16061 struct buffer *old = current_buffer;
16062 struct text_pos lpoint, opoint, startp;
16063 bool update_mode_line;
16064 int tem;
16065 struct it it;
16066 /* Record it now because it's overwritten. */
16067 bool current_matrix_up_to_date_p = false;
16068 bool used_current_matrix_p = false;
16069 /* This is less strict than current_matrix_up_to_date_p.
16070 It indicates that the buffer contents and narrowing are unchanged. */
16071 bool buffer_unchanged_p = false;
16072 bool temp_scroll_step = false;
16073 ptrdiff_t count = SPECPDL_INDEX ();
16074 int rc;
16075 int centering_position = -1;
16076 bool last_line_misfit = false;
16077 ptrdiff_t beg_unchanged, end_unchanged;
16078 int frame_line_height;
16079
16080 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16081 opoint = lpoint;
16082
16083 #ifdef GLYPH_DEBUG
16084 *w->desired_matrix->method = 0;
16085 #endif
16086
16087 if (!just_this_one_p
16088 && REDISPLAY_SOME_P ()
16089 && !w->redisplay
16090 && !w->update_mode_line
16091 && !f->face_change
16092 && !f->redisplay
16093 && !buffer->text->redisplay
16094 && BUF_PT (buffer) == w->last_point)
16095 return;
16096
16097 /* Make sure that both W's markers are valid. */
16098 eassert (XMARKER (w->start)->buffer == buffer);
16099 eassert (XMARKER (w->pointm)->buffer == buffer);
16100
16101 /* We come here again if we need to run window-text-change-functions
16102 below. */
16103 restart:
16104 reconsider_clip_changes (w);
16105 frame_line_height = default_line_pixel_height (w);
16106
16107 /* Has the mode line to be updated? */
16108 update_mode_line = (w->update_mode_line
16109 || update_mode_lines
16110 || buffer->clip_changed
16111 || buffer->prevent_redisplay_optimizations_p);
16112
16113 if (!just_this_one_p)
16114 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16115 cleverly elsewhere. */
16116 w->must_be_updated_p = true;
16117
16118 if (MINI_WINDOW_P (w))
16119 {
16120 if (w == XWINDOW (echo_area_window)
16121 && !NILP (echo_area_buffer[0]))
16122 {
16123 if (update_mode_line)
16124 /* We may have to update a tty frame's menu bar or a
16125 tool-bar. Example `M-x C-h C-h C-g'. */
16126 goto finish_menu_bars;
16127 else
16128 /* We've already displayed the echo area glyphs in this window. */
16129 goto finish_scroll_bars;
16130 }
16131 else if ((w != XWINDOW (minibuf_window)
16132 || minibuf_level == 0)
16133 /* When buffer is nonempty, redisplay window normally. */
16134 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16135 /* Quail displays non-mini buffers in minibuffer window.
16136 In that case, redisplay the window normally. */
16137 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16138 {
16139 /* W is a mini-buffer window, but it's not active, so clear
16140 it. */
16141 int yb = window_text_bottom_y (w);
16142 struct glyph_row *row;
16143 int y;
16144
16145 for (y = 0, row = w->desired_matrix->rows;
16146 y < yb;
16147 y += row->height, ++row)
16148 blank_row (w, row, y);
16149 goto finish_scroll_bars;
16150 }
16151
16152 clear_glyph_matrix (w->desired_matrix);
16153 }
16154
16155 /* Otherwise set up data on this window; select its buffer and point
16156 value. */
16157 /* Really select the buffer, for the sake of buffer-local
16158 variables. */
16159 set_buffer_internal_1 (XBUFFER (w->contents));
16160
16161 current_matrix_up_to_date_p
16162 = (w->window_end_valid
16163 && !current_buffer->clip_changed
16164 && !current_buffer->prevent_redisplay_optimizations_p
16165 && !window_outdated (w));
16166
16167 /* Run the window-text-change-functions
16168 if it is possible that the text on the screen has changed
16169 (either due to modification of the text, or any other reason). */
16170 if (!current_matrix_up_to_date_p
16171 && !NILP (Vwindow_text_change_functions))
16172 {
16173 safe_run_hooks (Qwindow_text_change_functions);
16174 goto restart;
16175 }
16176
16177 beg_unchanged = BEG_UNCHANGED;
16178 end_unchanged = END_UNCHANGED;
16179
16180 SET_TEXT_POS (opoint, PT, PT_BYTE);
16181
16182 specbind (Qinhibit_point_motion_hooks, Qt);
16183
16184 buffer_unchanged_p
16185 = (w->window_end_valid
16186 && !current_buffer->clip_changed
16187 && !window_outdated (w));
16188
16189 /* When windows_or_buffers_changed is non-zero, we can't rely
16190 on the window end being valid, so set it to zero there. */
16191 if (windows_or_buffers_changed)
16192 {
16193 /* If window starts on a continuation line, maybe adjust the
16194 window start in case the window's width changed. */
16195 if (XMARKER (w->start)->buffer == current_buffer)
16196 compute_window_start_on_continuation_line (w);
16197
16198 w->window_end_valid = false;
16199 /* If so, we also can't rely on current matrix
16200 and should not fool try_cursor_movement below. */
16201 current_matrix_up_to_date_p = false;
16202 }
16203
16204 /* Some sanity checks. */
16205 CHECK_WINDOW_END (w);
16206 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16207 emacs_abort ();
16208 if (BYTEPOS (opoint) < CHARPOS (opoint))
16209 emacs_abort ();
16210
16211 if (mode_line_update_needed (w))
16212 update_mode_line = true;
16213
16214 /* Point refers normally to the selected window. For any other
16215 window, set up appropriate value. */
16216 if (!EQ (window, selected_window))
16217 {
16218 ptrdiff_t new_pt = marker_position (w->pointm);
16219 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16220
16221 if (new_pt < BEGV)
16222 {
16223 new_pt = BEGV;
16224 new_pt_byte = BEGV_BYTE;
16225 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16226 }
16227 else if (new_pt > (ZV - 1))
16228 {
16229 new_pt = ZV;
16230 new_pt_byte = ZV_BYTE;
16231 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16232 }
16233
16234 /* We don't use SET_PT so that the point-motion hooks don't run. */
16235 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16236 }
16237
16238 /* If any of the character widths specified in the display table
16239 have changed, invalidate the width run cache. It's true that
16240 this may be a bit late to catch such changes, but the rest of
16241 redisplay goes (non-fatally) haywire when the display table is
16242 changed, so why should we worry about doing any better? */
16243 if (current_buffer->width_run_cache
16244 || (current_buffer->base_buffer
16245 && current_buffer->base_buffer->width_run_cache))
16246 {
16247 struct Lisp_Char_Table *disptab = buffer_display_table ();
16248
16249 if (! disptab_matches_widthtab
16250 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16251 {
16252 struct buffer *buf = current_buffer;
16253
16254 if (buf->base_buffer)
16255 buf = buf->base_buffer;
16256 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16257 recompute_width_table (current_buffer, disptab);
16258 }
16259 }
16260
16261 /* If window-start is screwed up, choose a new one. */
16262 if (XMARKER (w->start)->buffer != current_buffer)
16263 goto recenter;
16264
16265 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16266
16267 /* If someone specified a new starting point but did not insist,
16268 check whether it can be used. */
16269 if ((w->optional_new_start || window_frozen_p (w))
16270 && CHARPOS (startp) >= BEGV
16271 && CHARPOS (startp) <= ZV)
16272 {
16273 ptrdiff_t it_charpos;
16274
16275 w->optional_new_start = false;
16276 start_display (&it, w, startp);
16277 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16278 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16279 /* Record IT's position now, since line_bottom_y might change
16280 that. */
16281 it_charpos = IT_CHARPOS (it);
16282 /* Make sure we set the force_start flag only if the cursor row
16283 will be fully visible. Otherwise, the code under force_start
16284 label below will try to move point back into view, which is
16285 not what the code which sets optional_new_start wants. */
16286 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16287 && !w->force_start)
16288 {
16289 if (it_charpos == PT)
16290 w->force_start = true;
16291 /* IT may overshoot PT if text at PT is invisible. */
16292 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16293 w->force_start = true;
16294 #ifdef GLYPH_DEBUG
16295 if (w->force_start)
16296 {
16297 if (window_frozen_p (w))
16298 debug_method_add (w, "set force_start from frozen window start");
16299 else
16300 debug_method_add (w, "set force_start from optional_new_start");
16301 }
16302 #endif
16303 }
16304 }
16305
16306 force_start:
16307
16308 /* Handle case where place to start displaying has been specified,
16309 unless the specified location is outside the accessible range. */
16310 if (w->force_start)
16311 {
16312 /* We set this later on if we have to adjust point. */
16313 int new_vpos = -1;
16314
16315 w->force_start = false;
16316 w->vscroll = 0;
16317 w->window_end_valid = false;
16318
16319 /* Forget any recorded base line for line number display. */
16320 if (!buffer_unchanged_p)
16321 w->base_line_number = 0;
16322
16323 /* Redisplay the mode line. Select the buffer properly for that.
16324 Also, run the hook window-scroll-functions
16325 because we have scrolled. */
16326 /* Note, we do this after clearing force_start because
16327 if there's an error, it is better to forget about force_start
16328 than to get into an infinite loop calling the hook functions
16329 and having them get more errors. */
16330 if (!update_mode_line
16331 || ! NILP (Vwindow_scroll_functions))
16332 {
16333 update_mode_line = true;
16334 w->update_mode_line = true;
16335 startp = run_window_scroll_functions (window, startp);
16336 }
16337
16338 if (CHARPOS (startp) < BEGV)
16339 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16340 else if (CHARPOS (startp) > ZV)
16341 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16342
16343 /* Redisplay, then check if cursor has been set during the
16344 redisplay. Give up if new fonts were loaded. */
16345 /* We used to issue a CHECK_MARGINS argument to try_window here,
16346 but this causes scrolling to fail when point begins inside
16347 the scroll margin (bug#148) -- cyd */
16348 if (!try_window (window, startp, 0))
16349 {
16350 w->force_start = true;
16351 clear_glyph_matrix (w->desired_matrix);
16352 goto need_larger_matrices;
16353 }
16354
16355 if (w->cursor.vpos < 0)
16356 {
16357 /* If point does not appear, try to move point so it does
16358 appear. The desired matrix has been built above, so we
16359 can use it here. First see if point is in invisible
16360 text, and if so, move it to the first visible buffer
16361 position past that. */
16362 struct glyph_row *r = NULL;
16363 Lisp_Object invprop =
16364 get_char_property_and_overlay (make_number (PT), Qinvisible,
16365 Qnil, NULL);
16366
16367 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16368 {
16369 ptrdiff_t alt_pt;
16370 Lisp_Object invprop_end =
16371 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16372 Qnil, Qnil);
16373
16374 if (NATNUMP (invprop_end))
16375 alt_pt = XFASTINT (invprop_end);
16376 else
16377 alt_pt = ZV;
16378 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16379 NULL, 0);
16380 }
16381 if (r)
16382 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16383 else /* Give up and just move to the middle of the window. */
16384 new_vpos = window_box_height (w) / 2;
16385 }
16386
16387 if (!cursor_row_fully_visible_p (w, false, false))
16388 {
16389 /* Point does appear, but on a line partly visible at end of window.
16390 Move it back to a fully-visible line. */
16391 new_vpos = window_box_height (w);
16392 /* But if window_box_height suggests a Y coordinate that is
16393 not less than we already have, that line will clearly not
16394 be fully visible, so give up and scroll the display.
16395 This can happen when the default face uses a font whose
16396 dimensions are different from the frame's default
16397 font. */
16398 if (new_vpos >= w->cursor.y)
16399 {
16400 w->cursor.vpos = -1;
16401 clear_glyph_matrix (w->desired_matrix);
16402 goto try_to_scroll;
16403 }
16404 }
16405 else if (w->cursor.vpos >= 0)
16406 {
16407 /* Some people insist on not letting point enter the scroll
16408 margin, even though this part handles windows that didn't
16409 scroll at all. */
16410 int window_total_lines
16411 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16412 int margin = min (scroll_margin, window_total_lines / 4);
16413 int pixel_margin = margin * frame_line_height;
16414 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16415
16416 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16417 below, which finds the row to move point to, advances by
16418 the Y coordinate of the _next_ row, see the definition of
16419 MATRIX_ROW_BOTTOM_Y. */
16420 if (w->cursor.vpos < margin + header_line)
16421 {
16422 w->cursor.vpos = -1;
16423 clear_glyph_matrix (w->desired_matrix);
16424 goto try_to_scroll;
16425 }
16426 else
16427 {
16428 int window_height = window_box_height (w);
16429
16430 if (header_line)
16431 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16432 if (w->cursor.y >= window_height - pixel_margin)
16433 {
16434 w->cursor.vpos = -1;
16435 clear_glyph_matrix (w->desired_matrix);
16436 goto try_to_scroll;
16437 }
16438 }
16439 }
16440
16441 /* If we need to move point for either of the above reasons,
16442 now actually do it. */
16443 if (new_vpos >= 0)
16444 {
16445 struct glyph_row *row;
16446
16447 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16448 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16449 ++row;
16450
16451 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16452 MATRIX_ROW_START_BYTEPOS (row));
16453
16454 if (w != XWINDOW (selected_window))
16455 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16456 else if (current_buffer == old)
16457 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16458
16459 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16460
16461 /* Re-run pre-redisplay-function so it can update the region
16462 according to the new position of point. */
16463 /* Other than the cursor, w's redisplay is done so we can set its
16464 redisplay to false. Also the buffer's redisplay can be set to
16465 false, since propagate_buffer_redisplay should have already
16466 propagated its info to `w' anyway. */
16467 w->redisplay = false;
16468 XBUFFER (w->contents)->text->redisplay = false;
16469 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16470
16471 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16472 {
16473 /* pre-redisplay-function made changes (e.g. move the region)
16474 that require another round of redisplay. */
16475 clear_glyph_matrix (w->desired_matrix);
16476 if (!try_window (window, startp, 0))
16477 goto need_larger_matrices;
16478 }
16479 }
16480 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16481 {
16482 clear_glyph_matrix (w->desired_matrix);
16483 goto try_to_scroll;
16484 }
16485
16486 #ifdef GLYPH_DEBUG
16487 debug_method_add (w, "forced window start");
16488 #endif
16489 goto done;
16490 }
16491
16492 /* Handle case where text has not changed, only point, and it has
16493 not moved off the frame, and we are not retrying after hscroll.
16494 (current_matrix_up_to_date_p is true when retrying.) */
16495 if (current_matrix_up_to_date_p
16496 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16497 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16498 {
16499 switch (rc)
16500 {
16501 case CURSOR_MOVEMENT_SUCCESS:
16502 used_current_matrix_p = true;
16503 goto done;
16504
16505 case CURSOR_MOVEMENT_MUST_SCROLL:
16506 goto try_to_scroll;
16507
16508 default:
16509 emacs_abort ();
16510 }
16511 }
16512 /* If current starting point was originally the beginning of a line
16513 but no longer is, find a new starting point. */
16514 else if (w->start_at_line_beg
16515 && !(CHARPOS (startp) <= BEGV
16516 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16517 {
16518 #ifdef GLYPH_DEBUG
16519 debug_method_add (w, "recenter 1");
16520 #endif
16521 goto recenter;
16522 }
16523
16524 /* Try scrolling with try_window_id. Value is > 0 if update has
16525 been done, it is -1 if we know that the same window start will
16526 not work. It is 0 if unsuccessful for some other reason. */
16527 else if ((tem = try_window_id (w)) != 0)
16528 {
16529 #ifdef GLYPH_DEBUG
16530 debug_method_add (w, "try_window_id %d", tem);
16531 #endif
16532
16533 if (f->fonts_changed)
16534 goto need_larger_matrices;
16535 if (tem > 0)
16536 goto done;
16537
16538 /* Otherwise try_window_id has returned -1 which means that we
16539 don't want the alternative below this comment to execute. */
16540 }
16541 else if (CHARPOS (startp) >= BEGV
16542 && CHARPOS (startp) <= ZV
16543 && PT >= CHARPOS (startp)
16544 && (CHARPOS (startp) < ZV
16545 /* Avoid starting at end of buffer. */
16546 || CHARPOS (startp) == BEGV
16547 || !window_outdated (w)))
16548 {
16549 int d1, d2, d5, d6;
16550 int rtop, rbot;
16551
16552 /* If first window line is a continuation line, and window start
16553 is inside the modified region, but the first change is before
16554 current window start, we must select a new window start.
16555
16556 However, if this is the result of a down-mouse event (e.g. by
16557 extending the mouse-drag-overlay), we don't want to select a
16558 new window start, since that would change the position under
16559 the mouse, resulting in an unwanted mouse-movement rather
16560 than a simple mouse-click. */
16561 if (!w->start_at_line_beg
16562 && NILP (do_mouse_tracking)
16563 && CHARPOS (startp) > BEGV
16564 && CHARPOS (startp) > BEG + beg_unchanged
16565 && CHARPOS (startp) <= Z - end_unchanged
16566 /* Even if w->start_at_line_beg is nil, a new window may
16567 start at a line_beg, since that's how set_buffer_window
16568 sets it. So, we need to check the return value of
16569 compute_window_start_on_continuation_line. (See also
16570 bug#197). */
16571 && XMARKER (w->start)->buffer == current_buffer
16572 && compute_window_start_on_continuation_line (w)
16573 /* It doesn't make sense to force the window start like we
16574 do at label force_start if it is already known that point
16575 will not be fully visible in the resulting window, because
16576 doing so will move point from its correct position
16577 instead of scrolling the window to bring point into view.
16578 See bug#9324. */
16579 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16580 /* A very tall row could need more than the window height,
16581 in which case we accept that it is partially visible. */
16582 && (rtop != 0) == (rbot != 0))
16583 {
16584 w->force_start = true;
16585 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16586 #ifdef GLYPH_DEBUG
16587 debug_method_add (w, "recomputed window start in continuation line");
16588 #endif
16589 goto force_start;
16590 }
16591
16592 #ifdef GLYPH_DEBUG
16593 debug_method_add (w, "same window start");
16594 #endif
16595
16596 /* Try to redisplay starting at same place as before.
16597 If point has not moved off frame, accept the results. */
16598 if (!current_matrix_up_to_date_p
16599 /* Don't use try_window_reusing_current_matrix in this case
16600 because a window scroll function can have changed the
16601 buffer. */
16602 || !NILP (Vwindow_scroll_functions)
16603 || MINI_WINDOW_P (w)
16604 || !(used_current_matrix_p
16605 = try_window_reusing_current_matrix (w)))
16606 {
16607 IF_DEBUG (debug_method_add (w, "1"));
16608 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16609 /* -1 means we need to scroll.
16610 0 means we need new matrices, but fonts_changed
16611 is set in that case, so we will detect it below. */
16612 goto try_to_scroll;
16613 }
16614
16615 if (f->fonts_changed)
16616 goto need_larger_matrices;
16617
16618 if (w->cursor.vpos >= 0)
16619 {
16620 if (!just_this_one_p
16621 || current_buffer->clip_changed
16622 || BEG_UNCHANGED < CHARPOS (startp))
16623 /* Forget any recorded base line for line number display. */
16624 w->base_line_number = 0;
16625
16626 if (!cursor_row_fully_visible_p (w, true, false))
16627 {
16628 clear_glyph_matrix (w->desired_matrix);
16629 last_line_misfit = true;
16630 }
16631 /* Drop through and scroll. */
16632 else
16633 goto done;
16634 }
16635 else
16636 clear_glyph_matrix (w->desired_matrix);
16637 }
16638
16639 try_to_scroll:
16640
16641 /* Redisplay the mode line. Select the buffer properly for that. */
16642 if (!update_mode_line)
16643 {
16644 update_mode_line = true;
16645 w->update_mode_line = true;
16646 }
16647
16648 /* Try to scroll by specified few lines. */
16649 if ((scroll_conservatively
16650 || emacs_scroll_step
16651 || temp_scroll_step
16652 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16653 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16654 && CHARPOS (startp) >= BEGV
16655 && CHARPOS (startp) <= ZV)
16656 {
16657 /* The function returns -1 if new fonts were loaded, 1 if
16658 successful, 0 if not successful. */
16659 int ss = try_scrolling (window, just_this_one_p,
16660 scroll_conservatively,
16661 emacs_scroll_step,
16662 temp_scroll_step, last_line_misfit);
16663 switch (ss)
16664 {
16665 case SCROLLING_SUCCESS:
16666 goto done;
16667
16668 case SCROLLING_NEED_LARGER_MATRICES:
16669 goto need_larger_matrices;
16670
16671 case SCROLLING_FAILED:
16672 break;
16673
16674 default:
16675 emacs_abort ();
16676 }
16677 }
16678
16679 /* Finally, just choose a place to start which positions point
16680 according to user preferences. */
16681
16682 recenter:
16683
16684 #ifdef GLYPH_DEBUG
16685 debug_method_add (w, "recenter");
16686 #endif
16687
16688 /* Forget any previously recorded base line for line number display. */
16689 if (!buffer_unchanged_p)
16690 w->base_line_number = 0;
16691
16692 /* Determine the window start relative to point. */
16693 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16694 it.current_y = it.last_visible_y;
16695 if (centering_position < 0)
16696 {
16697 int window_total_lines
16698 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16699 int margin
16700 = scroll_margin > 0
16701 ? min (scroll_margin, window_total_lines / 4)
16702 : 0;
16703 ptrdiff_t margin_pos = CHARPOS (startp);
16704 Lisp_Object aggressive;
16705 bool scrolling_up;
16706
16707 /* If there is a scroll margin at the top of the window, find
16708 its character position. */
16709 if (margin
16710 /* Cannot call start_display if startp is not in the
16711 accessible region of the buffer. This can happen when we
16712 have just switched to a different buffer and/or changed
16713 its restriction. In that case, startp is initialized to
16714 the character position 1 (BEGV) because we did not yet
16715 have chance to display the buffer even once. */
16716 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16717 {
16718 struct it it1;
16719 void *it1data = NULL;
16720
16721 SAVE_IT (it1, it, it1data);
16722 start_display (&it1, w, startp);
16723 move_it_vertically (&it1, margin * frame_line_height);
16724 margin_pos = IT_CHARPOS (it1);
16725 RESTORE_IT (&it, &it, it1data);
16726 }
16727 scrolling_up = PT > margin_pos;
16728 aggressive =
16729 scrolling_up
16730 ? BVAR (current_buffer, scroll_up_aggressively)
16731 : BVAR (current_buffer, scroll_down_aggressively);
16732
16733 if (!MINI_WINDOW_P (w)
16734 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16735 {
16736 int pt_offset = 0;
16737
16738 /* Setting scroll-conservatively overrides
16739 scroll-*-aggressively. */
16740 if (!scroll_conservatively && NUMBERP (aggressive))
16741 {
16742 double float_amount = XFLOATINT (aggressive);
16743
16744 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16745 if (pt_offset == 0 && float_amount > 0)
16746 pt_offset = 1;
16747 if (pt_offset && margin > 0)
16748 margin -= 1;
16749 }
16750 /* Compute how much to move the window start backward from
16751 point so that point will be displayed where the user
16752 wants it. */
16753 if (scrolling_up)
16754 {
16755 centering_position = it.last_visible_y;
16756 if (pt_offset)
16757 centering_position -= pt_offset;
16758 centering_position -=
16759 (frame_line_height * (1 + margin + last_line_misfit)
16760 + WINDOW_HEADER_LINE_HEIGHT (w));
16761 /* Don't let point enter the scroll margin near top of
16762 the window. */
16763 if (centering_position < margin * frame_line_height)
16764 centering_position = margin * frame_line_height;
16765 }
16766 else
16767 centering_position = margin * frame_line_height + pt_offset;
16768 }
16769 else
16770 /* Set the window start half the height of the window backward
16771 from point. */
16772 centering_position = window_box_height (w) / 2;
16773 }
16774 move_it_vertically_backward (&it, centering_position);
16775
16776 eassert (IT_CHARPOS (it) >= BEGV);
16777
16778 /* The function move_it_vertically_backward may move over more
16779 than the specified y-distance. If it->w is small, e.g. a
16780 mini-buffer window, we may end up in front of the window's
16781 display area. Start displaying at the start of the line
16782 containing PT in this case. */
16783 if (it.current_y <= 0)
16784 {
16785 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16786 move_it_vertically_backward (&it, 0);
16787 it.current_y = 0;
16788 }
16789
16790 it.current_x = it.hpos = 0;
16791
16792 /* Set the window start position here explicitly, to avoid an
16793 infinite loop in case the functions in window-scroll-functions
16794 get errors. */
16795 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16796
16797 /* Run scroll hooks. */
16798 startp = run_window_scroll_functions (window, it.current.pos);
16799
16800 /* Redisplay the window. */
16801 bool use_desired_matrix = false;
16802 if (!current_matrix_up_to_date_p
16803 || windows_or_buffers_changed
16804 || f->cursor_type_changed
16805 /* Don't use try_window_reusing_current_matrix in this case
16806 because it can have changed the buffer. */
16807 || !NILP (Vwindow_scroll_functions)
16808 || !just_this_one_p
16809 || MINI_WINDOW_P (w)
16810 || !(used_current_matrix_p
16811 = try_window_reusing_current_matrix (w)))
16812 use_desired_matrix = (try_window (window, startp, 0) == 1);
16813
16814 /* If new fonts have been loaded (due to fontsets), give up. We
16815 have to start a new redisplay since we need to re-adjust glyph
16816 matrices. */
16817 if (f->fonts_changed)
16818 goto need_larger_matrices;
16819
16820 /* If cursor did not appear assume that the middle of the window is
16821 in the first line of the window. Do it again with the next line.
16822 (Imagine a window of height 100, displaying two lines of height
16823 60. Moving back 50 from it->last_visible_y will end in the first
16824 line.) */
16825 if (w->cursor.vpos < 0)
16826 {
16827 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16828 {
16829 clear_glyph_matrix (w->desired_matrix);
16830 move_it_by_lines (&it, 1);
16831 try_window (window, it.current.pos, 0);
16832 }
16833 else if (PT < IT_CHARPOS (it))
16834 {
16835 clear_glyph_matrix (w->desired_matrix);
16836 move_it_by_lines (&it, -1);
16837 try_window (window, it.current.pos, 0);
16838 }
16839 else
16840 {
16841 /* Not much we can do about it. */
16842 }
16843 }
16844
16845 /* Consider the following case: Window starts at BEGV, there is
16846 invisible, intangible text at BEGV, so that display starts at
16847 some point START > BEGV. It can happen that we are called with
16848 PT somewhere between BEGV and START. Try to handle that case,
16849 and similar ones. */
16850 if (w->cursor.vpos < 0)
16851 {
16852 /* Prefer the desired matrix to the current matrix, if possible,
16853 in the fallback calculations below. This is because using
16854 the current matrix might completely goof, e.g. if its first
16855 row is after point. */
16856 struct glyph_matrix *matrix =
16857 use_desired_matrix ? w->desired_matrix : w->current_matrix;
16858 /* First, try locating the proper glyph row for PT. */
16859 struct glyph_row *row =
16860 row_containing_pos (w, PT, matrix->rows, NULL, 0);
16861
16862 /* Sometimes point is at the beginning of invisible text that is
16863 before the 1st character displayed in the row. In that case,
16864 row_containing_pos fails to find the row, because no glyphs
16865 with appropriate buffer positions are present in the row.
16866 Therefore, we next try to find the row which shows the 1st
16867 position after the invisible text. */
16868 if (!row)
16869 {
16870 Lisp_Object val =
16871 get_char_property_and_overlay (make_number (PT), Qinvisible,
16872 Qnil, NULL);
16873
16874 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
16875 {
16876 ptrdiff_t alt_pos;
16877 Lisp_Object invis_end =
16878 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16879 Qnil, Qnil);
16880
16881 if (NATNUMP (invis_end))
16882 alt_pos = XFASTINT (invis_end);
16883 else
16884 alt_pos = ZV;
16885 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
16886 }
16887 }
16888 /* Finally, fall back on the first row of the window after the
16889 header line (if any). This is slightly better than not
16890 displaying the cursor at all. */
16891 if (!row)
16892 {
16893 row = matrix->rows;
16894 if (row->mode_line_p)
16895 ++row;
16896 }
16897 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
16898 }
16899
16900 if (!cursor_row_fully_visible_p (w, false, false))
16901 {
16902 /* If vscroll is enabled, disable it and try again. */
16903 if (w->vscroll)
16904 {
16905 w->vscroll = 0;
16906 clear_glyph_matrix (w->desired_matrix);
16907 goto recenter;
16908 }
16909
16910 /* Users who set scroll-conservatively to a large number want
16911 point just above/below the scroll margin. If we ended up
16912 with point's row partially visible, move the window start to
16913 make that row fully visible and out of the margin. */
16914 if (scroll_conservatively > SCROLL_LIMIT)
16915 {
16916 int window_total_lines
16917 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16918 int margin =
16919 scroll_margin > 0
16920 ? min (scroll_margin, window_total_lines / 4)
16921 : 0;
16922 bool move_down = w->cursor.vpos >= window_total_lines / 2;
16923
16924 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16925 clear_glyph_matrix (w->desired_matrix);
16926 if (1 == try_window (window, it.current.pos,
16927 TRY_WINDOW_CHECK_MARGINS))
16928 goto done;
16929 }
16930
16931 /* If centering point failed to make the whole line visible,
16932 put point at the top instead. That has to make the whole line
16933 visible, if it can be done. */
16934 if (centering_position == 0)
16935 goto done;
16936
16937 clear_glyph_matrix (w->desired_matrix);
16938 centering_position = 0;
16939 goto recenter;
16940 }
16941
16942 done:
16943
16944 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16945 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16946 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16947
16948 /* Display the mode line, if we must. */
16949 if ((update_mode_line
16950 /* If window not full width, must redo its mode line
16951 if (a) the window to its side is being redone and
16952 (b) we do a frame-based redisplay. This is a consequence
16953 of how inverted lines are drawn in frame-based redisplay. */
16954 || (!just_this_one_p
16955 && !FRAME_WINDOW_P (f)
16956 && !WINDOW_FULL_WIDTH_P (w))
16957 /* Line number to display. */
16958 || w->base_line_pos > 0
16959 /* Column number is displayed and different from the one displayed. */
16960 || (w->column_number_displayed != -1
16961 && (w->column_number_displayed != current_column ())))
16962 /* This means that the window has a mode line. */
16963 && (WINDOW_WANTS_MODELINE_P (w)
16964 || WINDOW_WANTS_HEADER_LINE_P (w)))
16965 {
16966
16967 display_mode_lines (w);
16968
16969 /* If mode line height has changed, arrange for a thorough
16970 immediate redisplay using the correct mode line height. */
16971 if (WINDOW_WANTS_MODELINE_P (w)
16972 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16973 {
16974 f->fonts_changed = true;
16975 w->mode_line_height = -1;
16976 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16977 = DESIRED_MODE_LINE_HEIGHT (w);
16978 }
16979
16980 /* If header line height has changed, arrange for a thorough
16981 immediate redisplay using the correct header line height. */
16982 if (WINDOW_WANTS_HEADER_LINE_P (w)
16983 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16984 {
16985 f->fonts_changed = true;
16986 w->header_line_height = -1;
16987 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16988 = DESIRED_HEADER_LINE_HEIGHT (w);
16989 }
16990
16991 if (f->fonts_changed)
16992 goto need_larger_matrices;
16993 }
16994
16995 if (!line_number_displayed && w->base_line_pos != -1)
16996 {
16997 w->base_line_pos = 0;
16998 w->base_line_number = 0;
16999 }
17000
17001 finish_menu_bars:
17002
17003 /* When we reach a frame's selected window, redo the frame's menu
17004 bar and the frame's title. */
17005 if (update_mode_line
17006 && EQ (FRAME_SELECTED_WINDOW (f), window))
17007 {
17008 bool redisplay_menu_p;
17009
17010 if (FRAME_WINDOW_P (f))
17011 {
17012 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17013 || defined (HAVE_NS) || defined (USE_GTK)
17014 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17015 #else
17016 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17017 #endif
17018 }
17019 else
17020 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17021
17022 if (redisplay_menu_p)
17023 display_menu_bar (w);
17024
17025 #ifdef HAVE_WINDOW_SYSTEM
17026 if (FRAME_WINDOW_P (f))
17027 {
17028 #if defined (USE_GTK) || defined (HAVE_NS)
17029 if (FRAME_EXTERNAL_TOOL_BAR (f))
17030 redisplay_tool_bar (f);
17031 #else
17032 if (WINDOWP (f->tool_bar_window)
17033 && (FRAME_TOOL_BAR_LINES (f) > 0
17034 || !NILP (Vauto_resize_tool_bars))
17035 && redisplay_tool_bar (f))
17036 ignore_mouse_drag_p = true;
17037 #endif
17038 }
17039 x_consider_frame_title (w->frame);
17040 #endif
17041 }
17042
17043 #ifdef HAVE_WINDOW_SYSTEM
17044 if (FRAME_WINDOW_P (f)
17045 && update_window_fringes (w, (just_this_one_p
17046 || (!used_current_matrix_p && !overlay_arrow_seen)
17047 || w->pseudo_window_p)))
17048 {
17049 update_begin (f);
17050 block_input ();
17051 if (draw_window_fringes (w, true))
17052 {
17053 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17054 x_draw_right_divider (w);
17055 else
17056 x_draw_vertical_border (w);
17057 }
17058 unblock_input ();
17059 update_end (f);
17060 }
17061
17062 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17063 x_draw_bottom_divider (w);
17064 #endif /* HAVE_WINDOW_SYSTEM */
17065
17066 /* We go to this label, with fonts_changed set, if it is
17067 necessary to try again using larger glyph matrices.
17068 We have to redeem the scroll bar even in this case,
17069 because the loop in redisplay_internal expects that. */
17070 need_larger_matrices:
17071 ;
17072 finish_scroll_bars:
17073
17074 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17075 {
17076 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17077 /* Set the thumb's position and size. */
17078 set_vertical_scroll_bar (w);
17079
17080 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17081 /* Set the thumb's position and size. */
17082 set_horizontal_scroll_bar (w);
17083
17084 /* Note that we actually used the scroll bar attached to this
17085 window, so it shouldn't be deleted at the end of redisplay. */
17086 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17087 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17088 }
17089
17090 /* Restore current_buffer and value of point in it. The window
17091 update may have changed the buffer, so first make sure `opoint'
17092 is still valid (Bug#6177). */
17093 if (CHARPOS (opoint) < BEGV)
17094 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17095 else if (CHARPOS (opoint) > ZV)
17096 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17097 else
17098 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17099
17100 set_buffer_internal_1 (old);
17101 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17102 shorter. This can be caused by log truncation in *Messages*. */
17103 if (CHARPOS (lpoint) <= ZV)
17104 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17105
17106 unbind_to (count, Qnil);
17107 }
17108
17109
17110 /* Build the complete desired matrix of WINDOW with a window start
17111 buffer position POS.
17112
17113 Value is 1 if successful. It is zero if fonts were loaded during
17114 redisplay which makes re-adjusting glyph matrices necessary, and -1
17115 if point would appear in the scroll margins.
17116 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17117 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17118 set in FLAGS.) */
17119
17120 int
17121 try_window (Lisp_Object window, struct text_pos pos, int flags)
17122 {
17123 struct window *w = XWINDOW (window);
17124 struct it it;
17125 struct glyph_row *last_text_row = NULL;
17126 struct frame *f = XFRAME (w->frame);
17127 int frame_line_height = default_line_pixel_height (w);
17128
17129 /* Make POS the new window start. */
17130 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17131
17132 /* Mark cursor position as unknown. No overlay arrow seen. */
17133 w->cursor.vpos = -1;
17134 overlay_arrow_seen = false;
17135
17136 /* Initialize iterator and info to start at POS. */
17137 start_display (&it, w, pos);
17138 it.glyph_row->reversed_p = false;
17139
17140 /* Display all lines of W. */
17141 while (it.current_y < it.last_visible_y)
17142 {
17143 if (display_line (&it))
17144 last_text_row = it.glyph_row - 1;
17145 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17146 return 0;
17147 }
17148
17149 /* Don't let the cursor end in the scroll margins. */
17150 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17151 && !MINI_WINDOW_P (w))
17152 {
17153 int this_scroll_margin;
17154 int window_total_lines
17155 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17156
17157 if (scroll_margin > 0)
17158 {
17159 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17160 this_scroll_margin *= frame_line_height;
17161 }
17162 else
17163 this_scroll_margin = 0;
17164
17165 if ((w->cursor.y >= 0 /* not vscrolled */
17166 && w->cursor.y < this_scroll_margin
17167 && CHARPOS (pos) > BEGV
17168 && IT_CHARPOS (it) < ZV)
17169 /* rms: considering make_cursor_line_fully_visible_p here
17170 seems to give wrong results. We don't want to recenter
17171 when the last line is partly visible, we want to allow
17172 that case to be handled in the usual way. */
17173 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17174 {
17175 w->cursor.vpos = -1;
17176 clear_glyph_matrix (w->desired_matrix);
17177 return -1;
17178 }
17179 }
17180
17181 /* If bottom moved off end of frame, change mode line percentage. */
17182 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17183 w->update_mode_line = true;
17184
17185 /* Set window_end_pos to the offset of the last character displayed
17186 on the window from the end of current_buffer. Set
17187 window_end_vpos to its row number. */
17188 if (last_text_row)
17189 {
17190 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17191 adjust_window_ends (w, last_text_row, false);
17192 eassert
17193 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17194 w->window_end_vpos)));
17195 }
17196 else
17197 {
17198 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17199 w->window_end_pos = Z - ZV;
17200 w->window_end_vpos = 0;
17201 }
17202
17203 /* But that is not valid info until redisplay finishes. */
17204 w->window_end_valid = false;
17205 return 1;
17206 }
17207
17208
17209 \f
17210 /************************************************************************
17211 Window redisplay reusing current matrix when buffer has not changed
17212 ************************************************************************/
17213
17214 /* Try redisplay of window W showing an unchanged buffer with a
17215 different window start than the last time it was displayed by
17216 reusing its current matrix. Value is true if successful.
17217 W->start is the new window start. */
17218
17219 static bool
17220 try_window_reusing_current_matrix (struct window *w)
17221 {
17222 struct frame *f = XFRAME (w->frame);
17223 struct glyph_row *bottom_row;
17224 struct it it;
17225 struct run run;
17226 struct text_pos start, new_start;
17227 int nrows_scrolled, i;
17228 struct glyph_row *last_text_row;
17229 struct glyph_row *last_reused_text_row;
17230 struct glyph_row *start_row;
17231 int start_vpos, min_y, max_y;
17232
17233 #ifdef GLYPH_DEBUG
17234 if (inhibit_try_window_reusing)
17235 return false;
17236 #endif
17237
17238 if (/* This function doesn't handle terminal frames. */
17239 !FRAME_WINDOW_P (f)
17240 /* Don't try to reuse the display if windows have been split
17241 or such. */
17242 || windows_or_buffers_changed
17243 || f->cursor_type_changed)
17244 return false;
17245
17246 /* Can't do this if showing trailing whitespace. */
17247 if (!NILP (Vshow_trailing_whitespace))
17248 return false;
17249
17250 /* If top-line visibility has changed, give up. */
17251 if (WINDOW_WANTS_HEADER_LINE_P (w)
17252 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17253 return false;
17254
17255 /* Give up if old or new display is scrolled vertically. We could
17256 make this function handle this, but right now it doesn't. */
17257 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17258 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17259 return false;
17260
17261 /* The variable new_start now holds the new window start. The old
17262 start `start' can be determined from the current matrix. */
17263 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17264 start = start_row->minpos;
17265 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17266
17267 /* Clear the desired matrix for the display below. */
17268 clear_glyph_matrix (w->desired_matrix);
17269
17270 if (CHARPOS (new_start) <= CHARPOS (start))
17271 {
17272 /* Don't use this method if the display starts with an ellipsis
17273 displayed for invisible text. It's not easy to handle that case
17274 below, and it's certainly not worth the effort since this is
17275 not a frequent case. */
17276 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17277 return false;
17278
17279 IF_DEBUG (debug_method_add (w, "twu1"));
17280
17281 /* Display up to a row that can be reused. The variable
17282 last_text_row is set to the last row displayed that displays
17283 text. Note that it.vpos == 0 if or if not there is a
17284 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17285 start_display (&it, w, new_start);
17286 w->cursor.vpos = -1;
17287 last_text_row = last_reused_text_row = NULL;
17288
17289 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17290 {
17291 /* If we have reached into the characters in the START row,
17292 that means the line boundaries have changed. So we
17293 can't start copying with the row START. Maybe it will
17294 work to start copying with the following row. */
17295 while (IT_CHARPOS (it) > CHARPOS (start))
17296 {
17297 /* Advance to the next row as the "start". */
17298 start_row++;
17299 start = start_row->minpos;
17300 /* If there are no more rows to try, or just one, give up. */
17301 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17302 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17303 || CHARPOS (start) == ZV)
17304 {
17305 clear_glyph_matrix (w->desired_matrix);
17306 return false;
17307 }
17308
17309 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17310 }
17311 /* If we have reached alignment, we can copy the rest of the
17312 rows. */
17313 if (IT_CHARPOS (it) == CHARPOS (start)
17314 /* Don't accept "alignment" inside a display vector,
17315 since start_row could have started in the middle of
17316 that same display vector (thus their character
17317 positions match), and we have no way of telling if
17318 that is the case. */
17319 && it.current.dpvec_index < 0)
17320 break;
17321
17322 it.glyph_row->reversed_p = false;
17323 if (display_line (&it))
17324 last_text_row = it.glyph_row - 1;
17325
17326 }
17327
17328 /* A value of current_y < last_visible_y means that we stopped
17329 at the previous window start, which in turn means that we
17330 have at least one reusable row. */
17331 if (it.current_y < it.last_visible_y)
17332 {
17333 struct glyph_row *row;
17334
17335 /* IT.vpos always starts from 0; it counts text lines. */
17336 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17337
17338 /* Find PT if not already found in the lines displayed. */
17339 if (w->cursor.vpos < 0)
17340 {
17341 int dy = it.current_y - start_row->y;
17342
17343 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17344 row = row_containing_pos (w, PT, row, NULL, dy);
17345 if (row)
17346 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17347 dy, nrows_scrolled);
17348 else
17349 {
17350 clear_glyph_matrix (w->desired_matrix);
17351 return false;
17352 }
17353 }
17354
17355 /* Scroll the display. Do it before the current matrix is
17356 changed. The problem here is that update has not yet
17357 run, i.e. part of the current matrix is not up to date.
17358 scroll_run_hook will clear the cursor, and use the
17359 current matrix to get the height of the row the cursor is
17360 in. */
17361 run.current_y = start_row->y;
17362 run.desired_y = it.current_y;
17363 run.height = it.last_visible_y - it.current_y;
17364
17365 if (run.height > 0 && run.current_y != run.desired_y)
17366 {
17367 update_begin (f);
17368 FRAME_RIF (f)->update_window_begin_hook (w);
17369 FRAME_RIF (f)->clear_window_mouse_face (w);
17370 FRAME_RIF (f)->scroll_run_hook (w, &run);
17371 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17372 update_end (f);
17373 }
17374
17375 /* Shift current matrix down by nrows_scrolled lines. */
17376 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17377 rotate_matrix (w->current_matrix,
17378 start_vpos,
17379 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17380 nrows_scrolled);
17381
17382 /* Disable lines that must be updated. */
17383 for (i = 0; i < nrows_scrolled; ++i)
17384 (start_row + i)->enabled_p = false;
17385
17386 /* Re-compute Y positions. */
17387 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17388 max_y = it.last_visible_y;
17389 for (row = start_row + nrows_scrolled;
17390 row < bottom_row;
17391 ++row)
17392 {
17393 row->y = it.current_y;
17394 row->visible_height = row->height;
17395
17396 if (row->y < min_y)
17397 row->visible_height -= min_y - row->y;
17398 if (row->y + row->height > max_y)
17399 row->visible_height -= row->y + row->height - max_y;
17400 if (row->fringe_bitmap_periodic_p)
17401 row->redraw_fringe_bitmaps_p = true;
17402
17403 it.current_y += row->height;
17404
17405 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17406 last_reused_text_row = row;
17407 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17408 break;
17409 }
17410
17411 /* Disable lines in the current matrix which are now
17412 below the window. */
17413 for (++row; row < bottom_row; ++row)
17414 row->enabled_p = row->mode_line_p = false;
17415 }
17416
17417 /* Update window_end_pos etc.; last_reused_text_row is the last
17418 reused row from the current matrix containing text, if any.
17419 The value of last_text_row is the last displayed line
17420 containing text. */
17421 if (last_reused_text_row)
17422 adjust_window_ends (w, last_reused_text_row, true);
17423 else if (last_text_row)
17424 adjust_window_ends (w, last_text_row, false);
17425 else
17426 {
17427 /* This window must be completely empty. */
17428 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17429 w->window_end_pos = Z - ZV;
17430 w->window_end_vpos = 0;
17431 }
17432 w->window_end_valid = false;
17433
17434 /* Update hint: don't try scrolling again in update_window. */
17435 w->desired_matrix->no_scrolling_p = true;
17436
17437 #ifdef GLYPH_DEBUG
17438 debug_method_add (w, "try_window_reusing_current_matrix 1");
17439 #endif
17440 return true;
17441 }
17442 else if (CHARPOS (new_start) > CHARPOS (start))
17443 {
17444 struct glyph_row *pt_row, *row;
17445 struct glyph_row *first_reusable_row;
17446 struct glyph_row *first_row_to_display;
17447 int dy;
17448 int yb = window_text_bottom_y (w);
17449
17450 /* Find the row starting at new_start, if there is one. Don't
17451 reuse a partially visible line at the end. */
17452 first_reusable_row = start_row;
17453 while (first_reusable_row->enabled_p
17454 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17455 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17456 < CHARPOS (new_start)))
17457 ++first_reusable_row;
17458
17459 /* Give up if there is no row to reuse. */
17460 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17461 || !first_reusable_row->enabled_p
17462 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17463 != CHARPOS (new_start)))
17464 return false;
17465
17466 /* We can reuse fully visible rows beginning with
17467 first_reusable_row to the end of the window. Set
17468 first_row_to_display to the first row that cannot be reused.
17469 Set pt_row to the row containing point, if there is any. */
17470 pt_row = NULL;
17471 for (first_row_to_display = first_reusable_row;
17472 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17473 ++first_row_to_display)
17474 {
17475 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17476 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17477 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17478 && first_row_to_display->ends_at_zv_p
17479 && pt_row == NULL)))
17480 pt_row = first_row_to_display;
17481 }
17482
17483 /* Start displaying at the start of first_row_to_display. */
17484 eassert (first_row_to_display->y < yb);
17485 init_to_row_start (&it, w, first_row_to_display);
17486
17487 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17488 - start_vpos);
17489 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17490 - nrows_scrolled);
17491 it.current_y = (first_row_to_display->y - first_reusable_row->y
17492 + WINDOW_HEADER_LINE_HEIGHT (w));
17493
17494 /* Display lines beginning with first_row_to_display in the
17495 desired matrix. Set last_text_row to the last row displayed
17496 that displays text. */
17497 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17498 if (pt_row == NULL)
17499 w->cursor.vpos = -1;
17500 last_text_row = NULL;
17501 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17502 if (display_line (&it))
17503 last_text_row = it.glyph_row - 1;
17504
17505 /* If point is in a reused row, adjust y and vpos of the cursor
17506 position. */
17507 if (pt_row)
17508 {
17509 w->cursor.vpos -= nrows_scrolled;
17510 w->cursor.y -= first_reusable_row->y - start_row->y;
17511 }
17512
17513 /* Give up if point isn't in a row displayed or reused. (This
17514 also handles the case where w->cursor.vpos < nrows_scrolled
17515 after the calls to display_line, which can happen with scroll
17516 margins. See bug#1295.) */
17517 if (w->cursor.vpos < 0)
17518 {
17519 clear_glyph_matrix (w->desired_matrix);
17520 return false;
17521 }
17522
17523 /* Scroll the display. */
17524 run.current_y = first_reusable_row->y;
17525 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17526 run.height = it.last_visible_y - run.current_y;
17527 dy = run.current_y - run.desired_y;
17528
17529 if (run.height)
17530 {
17531 update_begin (f);
17532 FRAME_RIF (f)->update_window_begin_hook (w);
17533 FRAME_RIF (f)->clear_window_mouse_face (w);
17534 FRAME_RIF (f)->scroll_run_hook (w, &run);
17535 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17536 update_end (f);
17537 }
17538
17539 /* Adjust Y positions of reused rows. */
17540 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17541 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17542 max_y = it.last_visible_y;
17543 for (row = first_reusable_row; row < first_row_to_display; ++row)
17544 {
17545 row->y -= dy;
17546 row->visible_height = row->height;
17547 if (row->y < min_y)
17548 row->visible_height -= min_y - row->y;
17549 if (row->y + row->height > max_y)
17550 row->visible_height -= row->y + row->height - max_y;
17551 if (row->fringe_bitmap_periodic_p)
17552 row->redraw_fringe_bitmaps_p = true;
17553 }
17554
17555 /* Scroll the current matrix. */
17556 eassert (nrows_scrolled > 0);
17557 rotate_matrix (w->current_matrix,
17558 start_vpos,
17559 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17560 -nrows_scrolled);
17561
17562 /* Disable rows not reused. */
17563 for (row -= nrows_scrolled; row < bottom_row; ++row)
17564 row->enabled_p = false;
17565
17566 /* Point may have moved to a different line, so we cannot assume that
17567 the previous cursor position is valid; locate the correct row. */
17568 if (pt_row)
17569 {
17570 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17571 row < bottom_row
17572 && PT >= MATRIX_ROW_END_CHARPOS (row)
17573 && !row->ends_at_zv_p;
17574 row++)
17575 {
17576 w->cursor.vpos++;
17577 w->cursor.y = row->y;
17578 }
17579 if (row < bottom_row)
17580 {
17581 /* Can't simply scan the row for point with
17582 bidi-reordered glyph rows. Let set_cursor_from_row
17583 figure out where to put the cursor, and if it fails,
17584 give up. */
17585 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17586 {
17587 if (!set_cursor_from_row (w, row, w->current_matrix,
17588 0, 0, 0, 0))
17589 {
17590 clear_glyph_matrix (w->desired_matrix);
17591 return false;
17592 }
17593 }
17594 else
17595 {
17596 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17597 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17598
17599 for (; glyph < end
17600 && (!BUFFERP (glyph->object)
17601 || glyph->charpos < PT);
17602 glyph++)
17603 {
17604 w->cursor.hpos++;
17605 w->cursor.x += glyph->pixel_width;
17606 }
17607 }
17608 }
17609 }
17610
17611 /* Adjust window end. A null value of last_text_row means that
17612 the window end is in reused rows which in turn means that
17613 only its vpos can have changed. */
17614 if (last_text_row)
17615 adjust_window_ends (w, last_text_row, false);
17616 else
17617 w->window_end_vpos -= nrows_scrolled;
17618
17619 w->window_end_valid = false;
17620 w->desired_matrix->no_scrolling_p = true;
17621
17622 #ifdef GLYPH_DEBUG
17623 debug_method_add (w, "try_window_reusing_current_matrix 2");
17624 #endif
17625 return true;
17626 }
17627
17628 return false;
17629 }
17630
17631
17632 \f
17633 /************************************************************************
17634 Window redisplay reusing current matrix when buffer has changed
17635 ************************************************************************/
17636
17637 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17638 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17639 ptrdiff_t *, ptrdiff_t *);
17640 static struct glyph_row *
17641 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17642 struct glyph_row *);
17643
17644
17645 /* Return the last row in MATRIX displaying text. If row START is
17646 non-null, start searching with that row. IT gives the dimensions
17647 of the display. Value is null if matrix is empty; otherwise it is
17648 a pointer to the row found. */
17649
17650 static struct glyph_row *
17651 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17652 struct glyph_row *start)
17653 {
17654 struct glyph_row *row, *row_found;
17655
17656 /* Set row_found to the last row in IT->w's current matrix
17657 displaying text. The loop looks funny but think of partially
17658 visible lines. */
17659 row_found = NULL;
17660 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17661 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17662 {
17663 eassert (row->enabled_p);
17664 row_found = row;
17665 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17666 break;
17667 ++row;
17668 }
17669
17670 return row_found;
17671 }
17672
17673
17674 /* Return the last row in the current matrix of W that is not affected
17675 by changes at the start of current_buffer that occurred since W's
17676 current matrix was built. Value is null if no such row exists.
17677
17678 BEG_UNCHANGED us the number of characters unchanged at the start of
17679 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17680 first changed character in current_buffer. Characters at positions <
17681 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17682 when the current matrix was built. */
17683
17684 static struct glyph_row *
17685 find_last_unchanged_at_beg_row (struct window *w)
17686 {
17687 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17688 struct glyph_row *row;
17689 struct glyph_row *row_found = NULL;
17690 int yb = window_text_bottom_y (w);
17691
17692 /* Find the last row displaying unchanged text. */
17693 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17694 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17695 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17696 ++row)
17697 {
17698 if (/* If row ends before first_changed_pos, it is unchanged,
17699 except in some case. */
17700 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17701 /* When row ends in ZV and we write at ZV it is not
17702 unchanged. */
17703 && !row->ends_at_zv_p
17704 /* When first_changed_pos is the end of a continued line,
17705 row is not unchanged because it may be no longer
17706 continued. */
17707 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17708 && (row->continued_p
17709 || row->exact_window_width_line_p))
17710 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17711 needs to be recomputed, so don't consider this row as
17712 unchanged. This happens when the last line was
17713 bidi-reordered and was killed immediately before this
17714 redisplay cycle. In that case, ROW->end stores the
17715 buffer position of the first visual-order character of
17716 the killed text, which is now beyond ZV. */
17717 && CHARPOS (row->end.pos) <= ZV)
17718 row_found = row;
17719
17720 /* Stop if last visible row. */
17721 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17722 break;
17723 }
17724
17725 return row_found;
17726 }
17727
17728
17729 /* Find the first glyph row in the current matrix of W that is not
17730 affected by changes at the end of current_buffer since the
17731 time W's current matrix was built.
17732
17733 Return in *DELTA the number of chars by which buffer positions in
17734 unchanged text at the end of current_buffer must be adjusted.
17735
17736 Return in *DELTA_BYTES the corresponding number of bytes.
17737
17738 Value is null if no such row exists, i.e. all rows are affected by
17739 changes. */
17740
17741 static struct glyph_row *
17742 find_first_unchanged_at_end_row (struct window *w,
17743 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17744 {
17745 struct glyph_row *row;
17746 struct glyph_row *row_found = NULL;
17747
17748 *delta = *delta_bytes = 0;
17749
17750 /* Display must not have been paused, otherwise the current matrix
17751 is not up to date. */
17752 eassert (w->window_end_valid);
17753
17754 /* A value of window_end_pos >= END_UNCHANGED means that the window
17755 end is in the range of changed text. If so, there is no
17756 unchanged row at the end of W's current matrix. */
17757 if (w->window_end_pos >= END_UNCHANGED)
17758 return NULL;
17759
17760 /* Set row to the last row in W's current matrix displaying text. */
17761 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17762
17763 /* If matrix is entirely empty, no unchanged row exists. */
17764 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17765 {
17766 /* The value of row is the last glyph row in the matrix having a
17767 meaningful buffer position in it. The end position of row
17768 corresponds to window_end_pos. This allows us to translate
17769 buffer positions in the current matrix to current buffer
17770 positions for characters not in changed text. */
17771 ptrdiff_t Z_old =
17772 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17773 ptrdiff_t Z_BYTE_old =
17774 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17775 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17776 struct glyph_row *first_text_row
17777 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17778
17779 *delta = Z - Z_old;
17780 *delta_bytes = Z_BYTE - Z_BYTE_old;
17781
17782 /* Set last_unchanged_pos to the buffer position of the last
17783 character in the buffer that has not been changed. Z is the
17784 index + 1 of the last character in current_buffer, i.e. by
17785 subtracting END_UNCHANGED we get the index of the last
17786 unchanged character, and we have to add BEG to get its buffer
17787 position. */
17788 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17789 last_unchanged_pos_old = last_unchanged_pos - *delta;
17790
17791 /* Search backward from ROW for a row displaying a line that
17792 starts at a minimum position >= last_unchanged_pos_old. */
17793 for (; row > first_text_row; --row)
17794 {
17795 /* This used to abort, but it can happen.
17796 It is ok to just stop the search instead here. KFS. */
17797 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17798 break;
17799
17800 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17801 row_found = row;
17802 }
17803 }
17804
17805 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17806
17807 return row_found;
17808 }
17809
17810
17811 /* Make sure that glyph rows in the current matrix of window W
17812 reference the same glyph memory as corresponding rows in the
17813 frame's frame matrix. This function is called after scrolling W's
17814 current matrix on a terminal frame in try_window_id and
17815 try_window_reusing_current_matrix. */
17816
17817 static void
17818 sync_frame_with_window_matrix_rows (struct window *w)
17819 {
17820 struct frame *f = XFRAME (w->frame);
17821 struct glyph_row *window_row, *window_row_end, *frame_row;
17822
17823 /* Preconditions: W must be a leaf window and full-width. Its frame
17824 must have a frame matrix. */
17825 eassert (BUFFERP (w->contents));
17826 eassert (WINDOW_FULL_WIDTH_P (w));
17827 eassert (!FRAME_WINDOW_P (f));
17828
17829 /* If W is a full-width window, glyph pointers in W's current matrix
17830 have, by definition, to be the same as glyph pointers in the
17831 corresponding frame matrix. Note that frame matrices have no
17832 marginal areas (see build_frame_matrix). */
17833 window_row = w->current_matrix->rows;
17834 window_row_end = window_row + w->current_matrix->nrows;
17835 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17836 while (window_row < window_row_end)
17837 {
17838 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17839 struct glyph *end = window_row->glyphs[LAST_AREA];
17840
17841 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17842 frame_row->glyphs[TEXT_AREA] = start;
17843 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17844 frame_row->glyphs[LAST_AREA] = end;
17845
17846 /* Disable frame rows whose corresponding window rows have
17847 been disabled in try_window_id. */
17848 if (!window_row->enabled_p)
17849 frame_row->enabled_p = false;
17850
17851 ++window_row, ++frame_row;
17852 }
17853 }
17854
17855
17856 /* Find the glyph row in window W containing CHARPOS. Consider all
17857 rows between START and END (not inclusive). END null means search
17858 all rows to the end of the display area of W. Value is the row
17859 containing CHARPOS or null. */
17860
17861 struct glyph_row *
17862 row_containing_pos (struct window *w, ptrdiff_t charpos,
17863 struct glyph_row *start, struct glyph_row *end, int dy)
17864 {
17865 struct glyph_row *row = start;
17866 struct glyph_row *best_row = NULL;
17867 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17868 int last_y;
17869
17870 /* If we happen to start on a header-line, skip that. */
17871 if (row->mode_line_p)
17872 ++row;
17873
17874 if ((end && row >= end) || !row->enabled_p)
17875 return NULL;
17876
17877 last_y = window_text_bottom_y (w) - dy;
17878
17879 while (true)
17880 {
17881 /* Give up if we have gone too far. */
17882 if ((end && row >= end) || !row->enabled_p)
17883 return NULL;
17884 /* This formerly returned if they were equal.
17885 I think that both quantities are of a "last plus one" type;
17886 if so, when they are equal, the row is within the screen. -- rms. */
17887 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17888 return NULL;
17889
17890 /* If it is in this row, return this row. */
17891 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17892 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17893 /* The end position of a row equals the start
17894 position of the next row. If CHARPOS is there, we
17895 would rather consider it displayed in the next
17896 line, except when this line ends in ZV. */
17897 && !row_for_charpos_p (row, charpos)))
17898 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17899 {
17900 struct glyph *g;
17901
17902 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17903 || (!best_row && !row->continued_p))
17904 return row;
17905 /* In bidi-reordered rows, there could be several rows whose
17906 edges surround CHARPOS, all of these rows belonging to
17907 the same continued line. We need to find the row which
17908 fits CHARPOS the best. */
17909 for (g = row->glyphs[TEXT_AREA];
17910 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17911 g++)
17912 {
17913 if (!STRINGP (g->object))
17914 {
17915 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17916 {
17917 mindif = eabs (g->charpos - charpos);
17918 best_row = row;
17919 /* Exact match always wins. */
17920 if (mindif == 0)
17921 return best_row;
17922 }
17923 }
17924 }
17925 }
17926 else if (best_row && !row->continued_p)
17927 return best_row;
17928 ++row;
17929 }
17930 }
17931
17932
17933 /* Try to redisplay window W by reusing its existing display. W's
17934 current matrix must be up to date when this function is called,
17935 i.e., window_end_valid must be true.
17936
17937 Value is
17938
17939 >= 1 if successful, i.e. display has been updated
17940 specifically:
17941 1 means the changes were in front of a newline that precedes
17942 the window start, and the whole current matrix was reused
17943 2 means the changes were after the last position displayed
17944 in the window, and the whole current matrix was reused
17945 3 means portions of the current matrix were reused, while
17946 some of the screen lines were redrawn
17947 -1 if redisplay with same window start is known not to succeed
17948 0 if otherwise unsuccessful
17949
17950 The following steps are performed:
17951
17952 1. Find the last row in the current matrix of W that is not
17953 affected by changes at the start of current_buffer. If no such row
17954 is found, give up.
17955
17956 2. Find the first row in W's current matrix that is not affected by
17957 changes at the end of current_buffer. Maybe there is no such row.
17958
17959 3. Display lines beginning with the row + 1 found in step 1 to the
17960 row found in step 2 or, if step 2 didn't find a row, to the end of
17961 the window.
17962
17963 4. If cursor is not known to appear on the window, give up.
17964
17965 5. If display stopped at the row found in step 2, scroll the
17966 display and current matrix as needed.
17967
17968 6. Maybe display some lines at the end of W, if we must. This can
17969 happen under various circumstances, like a partially visible line
17970 becoming fully visible, or because newly displayed lines are displayed
17971 in smaller font sizes.
17972
17973 7. Update W's window end information. */
17974
17975 static int
17976 try_window_id (struct window *w)
17977 {
17978 struct frame *f = XFRAME (w->frame);
17979 struct glyph_matrix *current_matrix = w->current_matrix;
17980 struct glyph_matrix *desired_matrix = w->desired_matrix;
17981 struct glyph_row *last_unchanged_at_beg_row;
17982 struct glyph_row *first_unchanged_at_end_row;
17983 struct glyph_row *row;
17984 struct glyph_row *bottom_row;
17985 int bottom_vpos;
17986 struct it it;
17987 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17988 int dvpos, dy;
17989 struct text_pos start_pos;
17990 struct run run;
17991 int first_unchanged_at_end_vpos = 0;
17992 struct glyph_row *last_text_row, *last_text_row_at_end;
17993 struct text_pos start;
17994 ptrdiff_t first_changed_charpos, last_changed_charpos;
17995
17996 #ifdef GLYPH_DEBUG
17997 if (inhibit_try_window_id)
17998 return 0;
17999 #endif
18000
18001 /* This is handy for debugging. */
18002 #if false
18003 #define GIVE_UP(X) \
18004 do { \
18005 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18006 return 0; \
18007 } while (false)
18008 #else
18009 #define GIVE_UP(X) return 0
18010 #endif
18011
18012 SET_TEXT_POS_FROM_MARKER (start, w->start);
18013
18014 /* Don't use this for mini-windows because these can show
18015 messages and mini-buffers, and we don't handle that here. */
18016 if (MINI_WINDOW_P (w))
18017 GIVE_UP (1);
18018
18019 /* This flag is used to prevent redisplay optimizations. */
18020 if (windows_or_buffers_changed || f->cursor_type_changed)
18021 GIVE_UP (2);
18022
18023 /* This function's optimizations cannot be used if overlays have
18024 changed in the buffer displayed by the window, so give up if they
18025 have. */
18026 if (w->last_overlay_modified != OVERLAY_MODIFF)
18027 GIVE_UP (200);
18028
18029 /* Verify that narrowing has not changed.
18030 Also verify that we were not told to prevent redisplay optimizations.
18031 It would be nice to further
18032 reduce the number of cases where this prevents try_window_id. */
18033 if (current_buffer->clip_changed
18034 || current_buffer->prevent_redisplay_optimizations_p)
18035 GIVE_UP (3);
18036
18037 /* Window must either use window-based redisplay or be full width. */
18038 if (!FRAME_WINDOW_P (f)
18039 && (!FRAME_LINE_INS_DEL_OK (f)
18040 || !WINDOW_FULL_WIDTH_P (w)))
18041 GIVE_UP (4);
18042
18043 /* Give up if point is known NOT to appear in W. */
18044 if (PT < CHARPOS (start))
18045 GIVE_UP (5);
18046
18047 /* Another way to prevent redisplay optimizations. */
18048 if (w->last_modified == 0)
18049 GIVE_UP (6);
18050
18051 /* Verify that window is not hscrolled. */
18052 if (w->hscroll != 0)
18053 GIVE_UP (7);
18054
18055 /* Verify that display wasn't paused. */
18056 if (!w->window_end_valid)
18057 GIVE_UP (8);
18058
18059 /* Likewise if highlighting trailing whitespace. */
18060 if (!NILP (Vshow_trailing_whitespace))
18061 GIVE_UP (11);
18062
18063 /* Can't use this if overlay arrow position and/or string have
18064 changed. */
18065 if (overlay_arrows_changed_p ())
18066 GIVE_UP (12);
18067
18068 /* When word-wrap is on, adding a space to the first word of a
18069 wrapped line can change the wrap position, altering the line
18070 above it. It might be worthwhile to handle this more
18071 intelligently, but for now just redisplay from scratch. */
18072 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18073 GIVE_UP (21);
18074
18075 /* Under bidi reordering, adding or deleting a character in the
18076 beginning of a paragraph, before the first strong directional
18077 character, can change the base direction of the paragraph (unless
18078 the buffer specifies a fixed paragraph direction), which will
18079 require redisplaying the whole paragraph. It might be worthwhile
18080 to find the paragraph limits and widen the range of redisplayed
18081 lines to that, but for now just give up this optimization and
18082 redisplay from scratch. */
18083 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18084 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18085 GIVE_UP (22);
18086
18087 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18088 to that variable require thorough redisplay. */
18089 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18090 GIVE_UP (23);
18091
18092 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18093 only if buffer has really changed. The reason is that the gap is
18094 initially at Z for freshly visited files. The code below would
18095 set end_unchanged to 0 in that case. */
18096 if (MODIFF > SAVE_MODIFF
18097 /* This seems to happen sometimes after saving a buffer. */
18098 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18099 {
18100 if (GPT - BEG < BEG_UNCHANGED)
18101 BEG_UNCHANGED = GPT - BEG;
18102 if (Z - GPT < END_UNCHANGED)
18103 END_UNCHANGED = Z - GPT;
18104 }
18105
18106 /* The position of the first and last character that has been changed. */
18107 first_changed_charpos = BEG + BEG_UNCHANGED;
18108 last_changed_charpos = Z - END_UNCHANGED;
18109
18110 /* If window starts after a line end, and the last change is in
18111 front of that newline, then changes don't affect the display.
18112 This case happens with stealth-fontification. Note that although
18113 the display is unchanged, glyph positions in the matrix have to
18114 be adjusted, of course. */
18115 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18116 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18117 && ((last_changed_charpos < CHARPOS (start)
18118 && CHARPOS (start) == BEGV)
18119 || (last_changed_charpos < CHARPOS (start) - 1
18120 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18121 {
18122 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18123 struct glyph_row *r0;
18124
18125 /* Compute how many chars/bytes have been added to or removed
18126 from the buffer. */
18127 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18128 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18129 Z_delta = Z - Z_old;
18130 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18131
18132 /* Give up if PT is not in the window. Note that it already has
18133 been checked at the start of try_window_id that PT is not in
18134 front of the window start. */
18135 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18136 GIVE_UP (13);
18137
18138 /* If window start is unchanged, we can reuse the whole matrix
18139 as is, after adjusting glyph positions. No need to compute
18140 the window end again, since its offset from Z hasn't changed. */
18141 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18142 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18143 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18144 /* PT must not be in a partially visible line. */
18145 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18146 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18147 {
18148 /* Adjust positions in the glyph matrix. */
18149 if (Z_delta || Z_delta_bytes)
18150 {
18151 struct glyph_row *r1
18152 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18153 increment_matrix_positions (w->current_matrix,
18154 MATRIX_ROW_VPOS (r0, current_matrix),
18155 MATRIX_ROW_VPOS (r1, current_matrix),
18156 Z_delta, Z_delta_bytes);
18157 }
18158
18159 /* Set the cursor. */
18160 row = row_containing_pos (w, PT, r0, NULL, 0);
18161 if (row)
18162 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18163 return 1;
18164 }
18165 }
18166
18167 /* Handle the case that changes are all below what is displayed in
18168 the window, and that PT is in the window. This shortcut cannot
18169 be taken if ZV is visible in the window, and text has been added
18170 there that is visible in the window. */
18171 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18172 /* ZV is not visible in the window, or there are no
18173 changes at ZV, actually. */
18174 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18175 || first_changed_charpos == last_changed_charpos))
18176 {
18177 struct glyph_row *r0;
18178
18179 /* Give up if PT is not in the window. Note that it already has
18180 been checked at the start of try_window_id that PT is not in
18181 front of the window start. */
18182 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18183 GIVE_UP (14);
18184
18185 /* If window start is unchanged, we can reuse the whole matrix
18186 as is, without changing glyph positions since no text has
18187 been added/removed in front of the window end. */
18188 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18189 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18190 /* PT must not be in a partially visible line. */
18191 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18192 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18193 {
18194 /* We have to compute the window end anew since text
18195 could have been added/removed after it. */
18196 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18197 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18198
18199 /* Set the cursor. */
18200 row = row_containing_pos (w, PT, r0, NULL, 0);
18201 if (row)
18202 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18203 return 2;
18204 }
18205 }
18206
18207 /* Give up if window start is in the changed area.
18208
18209 The condition used to read
18210
18211 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18212
18213 but why that was tested escapes me at the moment. */
18214 if (CHARPOS (start) >= first_changed_charpos
18215 && CHARPOS (start) <= last_changed_charpos)
18216 GIVE_UP (15);
18217
18218 /* Check that window start agrees with the start of the first glyph
18219 row in its current matrix. Check this after we know the window
18220 start is not in changed text, otherwise positions would not be
18221 comparable. */
18222 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18223 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18224 GIVE_UP (16);
18225
18226 /* Give up if the window ends in strings. Overlay strings
18227 at the end are difficult to handle, so don't try. */
18228 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18229 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18230 GIVE_UP (20);
18231
18232 /* Compute the position at which we have to start displaying new
18233 lines. Some of the lines at the top of the window might be
18234 reusable because they are not displaying changed text. Find the
18235 last row in W's current matrix not affected by changes at the
18236 start of current_buffer. Value is null if changes start in the
18237 first line of window. */
18238 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18239 if (last_unchanged_at_beg_row)
18240 {
18241 /* Avoid starting to display in the middle of a character, a TAB
18242 for instance. This is easier than to set up the iterator
18243 exactly, and it's not a frequent case, so the additional
18244 effort wouldn't really pay off. */
18245 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18246 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18247 && last_unchanged_at_beg_row > w->current_matrix->rows)
18248 --last_unchanged_at_beg_row;
18249
18250 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18251 GIVE_UP (17);
18252
18253 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18254 GIVE_UP (18);
18255 start_pos = it.current.pos;
18256
18257 /* Start displaying new lines in the desired matrix at the same
18258 vpos we would use in the current matrix, i.e. below
18259 last_unchanged_at_beg_row. */
18260 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18261 current_matrix);
18262 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18263 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18264
18265 eassert (it.hpos == 0 && it.current_x == 0);
18266 }
18267 else
18268 {
18269 /* There are no reusable lines at the start of the window.
18270 Start displaying in the first text line. */
18271 start_display (&it, w, start);
18272 it.vpos = it.first_vpos;
18273 start_pos = it.current.pos;
18274 }
18275
18276 /* Find the first row that is not affected by changes at the end of
18277 the buffer. Value will be null if there is no unchanged row, in
18278 which case we must redisplay to the end of the window. delta
18279 will be set to the value by which buffer positions beginning with
18280 first_unchanged_at_end_row have to be adjusted due to text
18281 changes. */
18282 first_unchanged_at_end_row
18283 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18284 IF_DEBUG (debug_delta = delta);
18285 IF_DEBUG (debug_delta_bytes = delta_bytes);
18286
18287 /* Set stop_pos to the buffer position up to which we will have to
18288 display new lines. If first_unchanged_at_end_row != NULL, this
18289 is the buffer position of the start of the line displayed in that
18290 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18291 that we don't stop at a buffer position. */
18292 stop_pos = 0;
18293 if (first_unchanged_at_end_row)
18294 {
18295 eassert (last_unchanged_at_beg_row == NULL
18296 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18297
18298 /* If this is a continuation line, move forward to the next one
18299 that isn't. Changes in lines above affect this line.
18300 Caution: this may move first_unchanged_at_end_row to a row
18301 not displaying text. */
18302 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18303 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18304 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18305 < it.last_visible_y))
18306 ++first_unchanged_at_end_row;
18307
18308 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18309 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18310 >= it.last_visible_y))
18311 first_unchanged_at_end_row = NULL;
18312 else
18313 {
18314 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18315 + delta);
18316 first_unchanged_at_end_vpos
18317 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18318 eassert (stop_pos >= Z - END_UNCHANGED);
18319 }
18320 }
18321 else if (last_unchanged_at_beg_row == NULL)
18322 GIVE_UP (19);
18323
18324
18325 #ifdef GLYPH_DEBUG
18326
18327 /* Either there is no unchanged row at the end, or the one we have
18328 now displays text. This is a necessary condition for the window
18329 end pos calculation at the end of this function. */
18330 eassert (first_unchanged_at_end_row == NULL
18331 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18332
18333 debug_last_unchanged_at_beg_vpos
18334 = (last_unchanged_at_beg_row
18335 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18336 : -1);
18337 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18338
18339 #endif /* GLYPH_DEBUG */
18340
18341
18342 /* Display new lines. Set last_text_row to the last new line
18343 displayed which has text on it, i.e. might end up as being the
18344 line where the window_end_vpos is. */
18345 w->cursor.vpos = -1;
18346 last_text_row = NULL;
18347 overlay_arrow_seen = false;
18348 if (it.current_y < it.last_visible_y
18349 && !f->fonts_changed
18350 && (first_unchanged_at_end_row == NULL
18351 || IT_CHARPOS (it) < stop_pos))
18352 it.glyph_row->reversed_p = false;
18353 while (it.current_y < it.last_visible_y
18354 && !f->fonts_changed
18355 && (first_unchanged_at_end_row == NULL
18356 || IT_CHARPOS (it) < stop_pos))
18357 {
18358 if (display_line (&it))
18359 last_text_row = it.glyph_row - 1;
18360 }
18361
18362 if (f->fonts_changed)
18363 return -1;
18364
18365 /* The redisplay iterations in display_line above could have
18366 triggered font-lock, which could have done something that
18367 invalidates IT->w window's end-point information, on which we
18368 rely below. E.g., one package, which will remain unnamed, used
18369 to install a font-lock-fontify-region-function that called
18370 bury-buffer, whose side effect is to switch the buffer displayed
18371 by IT->w, and that predictably resets IT->w's window_end_valid
18372 flag, which we already tested at the entry to this function.
18373 Amply punish such packages/modes by giving up on this
18374 optimization in those cases. */
18375 if (!w->window_end_valid)
18376 {
18377 clear_glyph_matrix (w->desired_matrix);
18378 return -1;
18379 }
18380
18381 /* Compute differences in buffer positions, y-positions etc. for
18382 lines reused at the bottom of the window. Compute what we can
18383 scroll. */
18384 if (first_unchanged_at_end_row
18385 /* No lines reused because we displayed everything up to the
18386 bottom of the window. */
18387 && it.current_y < it.last_visible_y)
18388 {
18389 dvpos = (it.vpos
18390 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18391 current_matrix));
18392 dy = it.current_y - first_unchanged_at_end_row->y;
18393 run.current_y = first_unchanged_at_end_row->y;
18394 run.desired_y = run.current_y + dy;
18395 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18396 }
18397 else
18398 {
18399 delta = delta_bytes = dvpos = dy
18400 = run.current_y = run.desired_y = run.height = 0;
18401 first_unchanged_at_end_row = NULL;
18402 }
18403 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18404
18405
18406 /* Find the cursor if not already found. We have to decide whether
18407 PT will appear on this window (it sometimes doesn't, but this is
18408 not a very frequent case.) This decision has to be made before
18409 the current matrix is altered. A value of cursor.vpos < 0 means
18410 that PT is either in one of the lines beginning at
18411 first_unchanged_at_end_row or below the window. Don't care for
18412 lines that might be displayed later at the window end; as
18413 mentioned, this is not a frequent case. */
18414 if (w->cursor.vpos < 0)
18415 {
18416 /* Cursor in unchanged rows at the top? */
18417 if (PT < CHARPOS (start_pos)
18418 && last_unchanged_at_beg_row)
18419 {
18420 row = row_containing_pos (w, PT,
18421 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18422 last_unchanged_at_beg_row + 1, 0);
18423 if (row)
18424 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18425 }
18426
18427 /* Start from first_unchanged_at_end_row looking for PT. */
18428 else if (first_unchanged_at_end_row)
18429 {
18430 row = row_containing_pos (w, PT - delta,
18431 first_unchanged_at_end_row, NULL, 0);
18432 if (row)
18433 set_cursor_from_row (w, row, w->current_matrix, delta,
18434 delta_bytes, dy, dvpos);
18435 }
18436
18437 /* Give up if cursor was not found. */
18438 if (w->cursor.vpos < 0)
18439 {
18440 clear_glyph_matrix (w->desired_matrix);
18441 return -1;
18442 }
18443 }
18444
18445 /* Don't let the cursor end in the scroll margins. */
18446 {
18447 int this_scroll_margin, cursor_height;
18448 int frame_line_height = default_line_pixel_height (w);
18449 int window_total_lines
18450 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18451
18452 this_scroll_margin =
18453 max (0, min (scroll_margin, window_total_lines / 4));
18454 this_scroll_margin *= frame_line_height;
18455 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18456
18457 if ((w->cursor.y < this_scroll_margin
18458 && CHARPOS (start) > BEGV)
18459 /* Old redisplay didn't take scroll margin into account at the bottom,
18460 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18461 || (w->cursor.y + (make_cursor_line_fully_visible_p
18462 ? cursor_height + this_scroll_margin
18463 : 1)) > it.last_visible_y)
18464 {
18465 w->cursor.vpos = -1;
18466 clear_glyph_matrix (w->desired_matrix);
18467 return -1;
18468 }
18469 }
18470
18471 /* Scroll the display. Do it before changing the current matrix so
18472 that xterm.c doesn't get confused about where the cursor glyph is
18473 found. */
18474 if (dy && run.height)
18475 {
18476 update_begin (f);
18477
18478 if (FRAME_WINDOW_P (f))
18479 {
18480 FRAME_RIF (f)->update_window_begin_hook (w);
18481 FRAME_RIF (f)->clear_window_mouse_face (w);
18482 FRAME_RIF (f)->scroll_run_hook (w, &run);
18483 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18484 }
18485 else
18486 {
18487 /* Terminal frame. In this case, dvpos gives the number of
18488 lines to scroll by; dvpos < 0 means scroll up. */
18489 int from_vpos
18490 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18491 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18492 int end = (WINDOW_TOP_EDGE_LINE (w)
18493 + WINDOW_WANTS_HEADER_LINE_P (w)
18494 + window_internal_height (w));
18495
18496 #if defined (HAVE_GPM) || defined (MSDOS)
18497 x_clear_window_mouse_face (w);
18498 #endif
18499 /* Perform the operation on the screen. */
18500 if (dvpos > 0)
18501 {
18502 /* Scroll last_unchanged_at_beg_row to the end of the
18503 window down dvpos lines. */
18504 set_terminal_window (f, end);
18505
18506 /* On dumb terminals delete dvpos lines at the end
18507 before inserting dvpos empty lines. */
18508 if (!FRAME_SCROLL_REGION_OK (f))
18509 ins_del_lines (f, end - dvpos, -dvpos);
18510
18511 /* Insert dvpos empty lines in front of
18512 last_unchanged_at_beg_row. */
18513 ins_del_lines (f, from, dvpos);
18514 }
18515 else if (dvpos < 0)
18516 {
18517 /* Scroll up last_unchanged_at_beg_vpos to the end of
18518 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18519 set_terminal_window (f, end);
18520
18521 /* Delete dvpos lines in front of
18522 last_unchanged_at_beg_vpos. ins_del_lines will set
18523 the cursor to the given vpos and emit |dvpos| delete
18524 line sequences. */
18525 ins_del_lines (f, from + dvpos, dvpos);
18526
18527 /* On a dumb terminal insert dvpos empty lines at the
18528 end. */
18529 if (!FRAME_SCROLL_REGION_OK (f))
18530 ins_del_lines (f, end + dvpos, -dvpos);
18531 }
18532
18533 set_terminal_window (f, 0);
18534 }
18535
18536 update_end (f);
18537 }
18538
18539 /* Shift reused rows of the current matrix to the right position.
18540 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18541 text. */
18542 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18543 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18544 if (dvpos < 0)
18545 {
18546 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18547 bottom_vpos, dvpos);
18548 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18549 bottom_vpos);
18550 }
18551 else if (dvpos > 0)
18552 {
18553 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18554 bottom_vpos, dvpos);
18555 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18556 first_unchanged_at_end_vpos + dvpos);
18557 }
18558
18559 /* For frame-based redisplay, make sure that current frame and window
18560 matrix are in sync with respect to glyph memory. */
18561 if (!FRAME_WINDOW_P (f))
18562 sync_frame_with_window_matrix_rows (w);
18563
18564 /* Adjust buffer positions in reused rows. */
18565 if (delta || delta_bytes)
18566 increment_matrix_positions (current_matrix,
18567 first_unchanged_at_end_vpos + dvpos,
18568 bottom_vpos, delta, delta_bytes);
18569
18570 /* Adjust Y positions. */
18571 if (dy)
18572 shift_glyph_matrix (w, current_matrix,
18573 first_unchanged_at_end_vpos + dvpos,
18574 bottom_vpos, dy);
18575
18576 if (first_unchanged_at_end_row)
18577 {
18578 first_unchanged_at_end_row += dvpos;
18579 if (first_unchanged_at_end_row->y >= it.last_visible_y
18580 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18581 first_unchanged_at_end_row = NULL;
18582 }
18583
18584 /* If scrolling up, there may be some lines to display at the end of
18585 the window. */
18586 last_text_row_at_end = NULL;
18587 if (dy < 0)
18588 {
18589 /* Scrolling up can leave for example a partially visible line
18590 at the end of the window to be redisplayed. */
18591 /* Set last_row to the glyph row in the current matrix where the
18592 window end line is found. It has been moved up or down in
18593 the matrix by dvpos. */
18594 int last_vpos = w->window_end_vpos + dvpos;
18595 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18596
18597 /* If last_row is the window end line, it should display text. */
18598 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18599
18600 /* If window end line was partially visible before, begin
18601 displaying at that line. Otherwise begin displaying with the
18602 line following it. */
18603 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18604 {
18605 init_to_row_start (&it, w, last_row);
18606 it.vpos = last_vpos;
18607 it.current_y = last_row->y;
18608 }
18609 else
18610 {
18611 init_to_row_end (&it, w, last_row);
18612 it.vpos = 1 + last_vpos;
18613 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18614 ++last_row;
18615 }
18616
18617 /* We may start in a continuation line. If so, we have to
18618 get the right continuation_lines_width and current_x. */
18619 it.continuation_lines_width = last_row->continuation_lines_width;
18620 it.hpos = it.current_x = 0;
18621
18622 /* Display the rest of the lines at the window end. */
18623 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18624 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18625 {
18626 /* Is it always sure that the display agrees with lines in
18627 the current matrix? I don't think so, so we mark rows
18628 displayed invalid in the current matrix by setting their
18629 enabled_p flag to false. */
18630 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18631 if (display_line (&it))
18632 last_text_row_at_end = it.glyph_row - 1;
18633 }
18634 }
18635
18636 /* Update window_end_pos and window_end_vpos. */
18637 if (first_unchanged_at_end_row && !last_text_row_at_end)
18638 {
18639 /* Window end line if one of the preserved rows from the current
18640 matrix. Set row to the last row displaying text in current
18641 matrix starting at first_unchanged_at_end_row, after
18642 scrolling. */
18643 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18644 row = find_last_row_displaying_text (w->current_matrix, &it,
18645 first_unchanged_at_end_row);
18646 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18647 adjust_window_ends (w, row, true);
18648 eassert (w->window_end_bytepos >= 0);
18649 IF_DEBUG (debug_method_add (w, "A"));
18650 }
18651 else if (last_text_row_at_end)
18652 {
18653 adjust_window_ends (w, last_text_row_at_end, false);
18654 eassert (w->window_end_bytepos >= 0);
18655 IF_DEBUG (debug_method_add (w, "B"));
18656 }
18657 else if (last_text_row)
18658 {
18659 /* We have displayed either to the end of the window or at the
18660 end of the window, i.e. the last row with text is to be found
18661 in the desired matrix. */
18662 adjust_window_ends (w, last_text_row, false);
18663 eassert (w->window_end_bytepos >= 0);
18664 }
18665 else if (first_unchanged_at_end_row == NULL
18666 && last_text_row == NULL
18667 && last_text_row_at_end == NULL)
18668 {
18669 /* Displayed to end of window, but no line containing text was
18670 displayed. Lines were deleted at the end of the window. */
18671 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18672 int vpos = w->window_end_vpos;
18673 struct glyph_row *current_row = current_matrix->rows + vpos;
18674 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18675
18676 for (row = NULL;
18677 row == NULL && vpos >= first_vpos;
18678 --vpos, --current_row, --desired_row)
18679 {
18680 if (desired_row->enabled_p)
18681 {
18682 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18683 row = desired_row;
18684 }
18685 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18686 row = current_row;
18687 }
18688
18689 eassert (row != NULL);
18690 w->window_end_vpos = vpos + 1;
18691 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18692 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18693 eassert (w->window_end_bytepos >= 0);
18694 IF_DEBUG (debug_method_add (w, "C"));
18695 }
18696 else
18697 emacs_abort ();
18698
18699 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18700 debug_end_vpos = w->window_end_vpos));
18701
18702 /* Record that display has not been completed. */
18703 w->window_end_valid = false;
18704 w->desired_matrix->no_scrolling_p = true;
18705 return 3;
18706
18707 #undef GIVE_UP
18708 }
18709
18710
18711 \f
18712 /***********************************************************************
18713 More debugging support
18714 ***********************************************************************/
18715
18716 #ifdef GLYPH_DEBUG
18717
18718 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18719 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18720 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18721
18722
18723 /* Dump the contents of glyph matrix MATRIX on stderr.
18724
18725 GLYPHS 0 means don't show glyph contents.
18726 GLYPHS 1 means show glyphs in short form
18727 GLYPHS > 1 means show glyphs in long form. */
18728
18729 void
18730 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18731 {
18732 int i;
18733 for (i = 0; i < matrix->nrows; ++i)
18734 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18735 }
18736
18737
18738 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18739 the glyph row and area where the glyph comes from. */
18740
18741 void
18742 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18743 {
18744 if (glyph->type == CHAR_GLYPH
18745 || glyph->type == GLYPHLESS_GLYPH)
18746 {
18747 fprintf (stderr,
18748 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18749 glyph - row->glyphs[TEXT_AREA],
18750 (glyph->type == CHAR_GLYPH
18751 ? 'C'
18752 : 'G'),
18753 glyph->charpos,
18754 (BUFFERP (glyph->object)
18755 ? 'B'
18756 : (STRINGP (glyph->object)
18757 ? 'S'
18758 : (NILP (glyph->object)
18759 ? '0'
18760 : '-'))),
18761 glyph->pixel_width,
18762 glyph->u.ch,
18763 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18764 ? glyph->u.ch
18765 : '.'),
18766 glyph->face_id,
18767 glyph->left_box_line_p,
18768 glyph->right_box_line_p);
18769 }
18770 else if (glyph->type == STRETCH_GLYPH)
18771 {
18772 fprintf (stderr,
18773 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18774 glyph - row->glyphs[TEXT_AREA],
18775 'S',
18776 glyph->charpos,
18777 (BUFFERP (glyph->object)
18778 ? 'B'
18779 : (STRINGP (glyph->object)
18780 ? 'S'
18781 : (NILP (glyph->object)
18782 ? '0'
18783 : '-'))),
18784 glyph->pixel_width,
18785 0,
18786 ' ',
18787 glyph->face_id,
18788 glyph->left_box_line_p,
18789 glyph->right_box_line_p);
18790 }
18791 else if (glyph->type == IMAGE_GLYPH)
18792 {
18793 fprintf (stderr,
18794 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18795 glyph - row->glyphs[TEXT_AREA],
18796 'I',
18797 glyph->charpos,
18798 (BUFFERP (glyph->object)
18799 ? 'B'
18800 : (STRINGP (glyph->object)
18801 ? 'S'
18802 : (NILP (glyph->object)
18803 ? '0'
18804 : '-'))),
18805 glyph->pixel_width,
18806 glyph->u.img_id,
18807 '.',
18808 glyph->face_id,
18809 glyph->left_box_line_p,
18810 glyph->right_box_line_p);
18811 }
18812 else if (glyph->type == COMPOSITE_GLYPH)
18813 {
18814 fprintf (stderr,
18815 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18816 glyph - row->glyphs[TEXT_AREA],
18817 '+',
18818 glyph->charpos,
18819 (BUFFERP (glyph->object)
18820 ? 'B'
18821 : (STRINGP (glyph->object)
18822 ? 'S'
18823 : (NILP (glyph->object)
18824 ? '0'
18825 : '-'))),
18826 glyph->pixel_width,
18827 glyph->u.cmp.id);
18828 if (glyph->u.cmp.automatic)
18829 fprintf (stderr,
18830 "[%d-%d]",
18831 glyph->slice.cmp.from, glyph->slice.cmp.to);
18832 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18833 glyph->face_id,
18834 glyph->left_box_line_p,
18835 glyph->right_box_line_p);
18836 }
18837 else if (glyph->type == XWIDGET_GLYPH)
18838 {
18839 #ifndef HAVE_XWIDGETS
18840 eassume (false);
18841 #else
18842 fprintf (stderr,
18843 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18844 glyph - row->glyphs[TEXT_AREA],
18845 'X',
18846 glyph->charpos,
18847 (BUFFERP (glyph->object)
18848 ? 'B'
18849 : (STRINGP (glyph->object)
18850 ? 'S'
18851 : '-')),
18852 glyph->pixel_width,
18853 glyph->u.xwidget,
18854 '.',
18855 glyph->face_id,
18856 glyph->left_box_line_p,
18857 glyph->right_box_line_p);
18858 #endif
18859 }
18860 }
18861
18862
18863 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18864 GLYPHS 0 means don't show glyph contents.
18865 GLYPHS 1 means show glyphs in short form
18866 GLYPHS > 1 means show glyphs in long form. */
18867
18868 void
18869 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18870 {
18871 if (glyphs != 1)
18872 {
18873 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18874 fprintf (stderr, "==============================================================================\n");
18875
18876 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18877 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18878 vpos,
18879 MATRIX_ROW_START_CHARPOS (row),
18880 MATRIX_ROW_END_CHARPOS (row),
18881 row->used[TEXT_AREA],
18882 row->contains_overlapping_glyphs_p,
18883 row->enabled_p,
18884 row->truncated_on_left_p,
18885 row->truncated_on_right_p,
18886 row->continued_p,
18887 MATRIX_ROW_CONTINUATION_LINE_P (row),
18888 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18889 row->ends_at_zv_p,
18890 row->fill_line_p,
18891 row->ends_in_middle_of_char_p,
18892 row->starts_in_middle_of_char_p,
18893 row->mouse_face_p,
18894 row->x,
18895 row->y,
18896 row->pixel_width,
18897 row->height,
18898 row->visible_height,
18899 row->ascent,
18900 row->phys_ascent);
18901 /* The next 3 lines should align to "Start" in the header. */
18902 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18903 row->end.overlay_string_index,
18904 row->continuation_lines_width);
18905 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18906 CHARPOS (row->start.string_pos),
18907 CHARPOS (row->end.string_pos));
18908 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18909 row->end.dpvec_index);
18910 }
18911
18912 if (glyphs > 1)
18913 {
18914 int area;
18915
18916 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18917 {
18918 struct glyph *glyph = row->glyphs[area];
18919 struct glyph *glyph_end = glyph + row->used[area];
18920
18921 /* Glyph for a line end in text. */
18922 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18923 ++glyph_end;
18924
18925 if (glyph < glyph_end)
18926 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18927
18928 for (; glyph < glyph_end; ++glyph)
18929 dump_glyph (row, glyph, area);
18930 }
18931 }
18932 else if (glyphs == 1)
18933 {
18934 int area;
18935 char s[SHRT_MAX + 4];
18936
18937 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18938 {
18939 int i;
18940
18941 for (i = 0; i < row->used[area]; ++i)
18942 {
18943 struct glyph *glyph = row->glyphs[area] + i;
18944 if (i == row->used[area] - 1
18945 && area == TEXT_AREA
18946 && NILP (glyph->object)
18947 && glyph->type == CHAR_GLYPH
18948 && glyph->u.ch == ' ')
18949 {
18950 strcpy (&s[i], "[\\n]");
18951 i += 4;
18952 }
18953 else if (glyph->type == CHAR_GLYPH
18954 && glyph->u.ch < 0x80
18955 && glyph->u.ch >= ' ')
18956 s[i] = glyph->u.ch;
18957 else
18958 s[i] = '.';
18959 }
18960
18961 s[i] = '\0';
18962 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18963 }
18964 }
18965 }
18966
18967
18968 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18969 Sdump_glyph_matrix, 0, 1, "p",
18970 doc: /* Dump the current matrix of the selected window to stderr.
18971 Shows contents of glyph row structures. With non-nil
18972 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18973 glyphs in short form, otherwise show glyphs in long form.
18974
18975 Interactively, no argument means show glyphs in short form;
18976 with numeric argument, its value is passed as the GLYPHS flag. */)
18977 (Lisp_Object glyphs)
18978 {
18979 struct window *w = XWINDOW (selected_window);
18980 struct buffer *buffer = XBUFFER (w->contents);
18981
18982 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18983 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18984 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18985 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18986 fprintf (stderr, "=============================================\n");
18987 dump_glyph_matrix (w->current_matrix,
18988 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18989 return Qnil;
18990 }
18991
18992
18993 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18994 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18995 Only text-mode frames have frame glyph matrices. */)
18996 (void)
18997 {
18998 struct frame *f = XFRAME (selected_frame);
18999
19000 if (f->current_matrix)
19001 dump_glyph_matrix (f->current_matrix, 1);
19002 else
19003 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19004 return Qnil;
19005 }
19006
19007
19008 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19009 doc: /* Dump glyph row ROW to stderr.
19010 GLYPH 0 means don't dump glyphs.
19011 GLYPH 1 means dump glyphs in short form.
19012 GLYPH > 1 or omitted means dump glyphs in long form. */)
19013 (Lisp_Object row, Lisp_Object glyphs)
19014 {
19015 struct glyph_matrix *matrix;
19016 EMACS_INT vpos;
19017
19018 CHECK_NUMBER (row);
19019 matrix = XWINDOW (selected_window)->current_matrix;
19020 vpos = XINT (row);
19021 if (vpos >= 0 && vpos < matrix->nrows)
19022 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19023 vpos,
19024 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19025 return Qnil;
19026 }
19027
19028
19029 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19030 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19031 GLYPH 0 means don't dump glyphs.
19032 GLYPH 1 means dump glyphs in short form.
19033 GLYPH > 1 or omitted means dump glyphs in long form.
19034
19035 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19036 do nothing. */)
19037 (Lisp_Object row, Lisp_Object glyphs)
19038 {
19039 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19040 struct frame *sf = SELECTED_FRAME ();
19041 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19042 EMACS_INT vpos;
19043
19044 CHECK_NUMBER (row);
19045 vpos = XINT (row);
19046 if (vpos >= 0 && vpos < m->nrows)
19047 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19048 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19049 #endif
19050 return Qnil;
19051 }
19052
19053
19054 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19055 doc: /* Toggle tracing of redisplay.
19056 With ARG, turn tracing on if and only if ARG is positive. */)
19057 (Lisp_Object arg)
19058 {
19059 if (NILP (arg))
19060 trace_redisplay_p = !trace_redisplay_p;
19061 else
19062 {
19063 arg = Fprefix_numeric_value (arg);
19064 trace_redisplay_p = XINT (arg) > 0;
19065 }
19066
19067 return Qnil;
19068 }
19069
19070
19071 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19072 doc: /* Like `format', but print result to stderr.
19073 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19074 (ptrdiff_t nargs, Lisp_Object *args)
19075 {
19076 Lisp_Object s = Fformat (nargs, args);
19077 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19078 return Qnil;
19079 }
19080
19081 #endif /* GLYPH_DEBUG */
19082
19083
19084 \f
19085 /***********************************************************************
19086 Building Desired Matrix Rows
19087 ***********************************************************************/
19088
19089 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19090 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19091
19092 static struct glyph_row *
19093 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19094 {
19095 struct frame *f = XFRAME (WINDOW_FRAME (w));
19096 struct buffer *buffer = XBUFFER (w->contents);
19097 struct buffer *old = current_buffer;
19098 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19099 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19100 const unsigned char *arrow_end = arrow_string + arrow_len;
19101 const unsigned char *p;
19102 struct it it;
19103 bool multibyte_p;
19104 int n_glyphs_before;
19105
19106 set_buffer_temp (buffer);
19107 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19108 scratch_glyph_row.reversed_p = false;
19109 it.glyph_row->used[TEXT_AREA] = 0;
19110 SET_TEXT_POS (it.position, 0, 0);
19111
19112 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19113 p = arrow_string;
19114 while (p < arrow_end)
19115 {
19116 Lisp_Object face, ilisp;
19117
19118 /* Get the next character. */
19119 if (multibyte_p)
19120 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19121 else
19122 {
19123 it.c = it.char_to_display = *p, it.len = 1;
19124 if (! ASCII_CHAR_P (it.c))
19125 it.char_to_display = BYTE8_TO_CHAR (it.c);
19126 }
19127 p += it.len;
19128
19129 /* Get its face. */
19130 ilisp = make_number (p - arrow_string);
19131 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19132 it.face_id = compute_char_face (f, it.char_to_display, face);
19133
19134 /* Compute its width, get its glyphs. */
19135 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19136 SET_TEXT_POS (it.position, -1, -1);
19137 PRODUCE_GLYPHS (&it);
19138
19139 /* If this character doesn't fit any more in the line, we have
19140 to remove some glyphs. */
19141 if (it.current_x > it.last_visible_x)
19142 {
19143 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19144 break;
19145 }
19146 }
19147
19148 set_buffer_temp (old);
19149 return it.glyph_row;
19150 }
19151
19152
19153 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19154 glyphs to insert is determined by produce_special_glyphs. */
19155
19156 static void
19157 insert_left_trunc_glyphs (struct it *it)
19158 {
19159 struct it truncate_it;
19160 struct glyph *from, *end, *to, *toend;
19161
19162 eassert (!FRAME_WINDOW_P (it->f)
19163 || (!it->glyph_row->reversed_p
19164 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19165 || (it->glyph_row->reversed_p
19166 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19167
19168 /* Get the truncation glyphs. */
19169 truncate_it = *it;
19170 truncate_it.current_x = 0;
19171 truncate_it.face_id = DEFAULT_FACE_ID;
19172 truncate_it.glyph_row = &scratch_glyph_row;
19173 truncate_it.area = TEXT_AREA;
19174 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19175 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19176 truncate_it.object = Qnil;
19177 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19178
19179 /* Overwrite glyphs from IT with truncation glyphs. */
19180 if (!it->glyph_row->reversed_p)
19181 {
19182 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19183
19184 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19185 end = from + tused;
19186 to = it->glyph_row->glyphs[TEXT_AREA];
19187 toend = to + it->glyph_row->used[TEXT_AREA];
19188 if (FRAME_WINDOW_P (it->f))
19189 {
19190 /* On GUI frames, when variable-size fonts are displayed,
19191 the truncation glyphs may need more pixels than the row's
19192 glyphs they overwrite. We overwrite more glyphs to free
19193 enough screen real estate, and enlarge the stretch glyph
19194 on the right (see display_line), if there is one, to
19195 preserve the screen position of the truncation glyphs on
19196 the right. */
19197 int w = 0;
19198 struct glyph *g = to;
19199 short used;
19200
19201 /* The first glyph could be partially visible, in which case
19202 it->glyph_row->x will be negative. But we want the left
19203 truncation glyphs to be aligned at the left margin of the
19204 window, so we override the x coordinate at which the row
19205 will begin. */
19206 it->glyph_row->x = 0;
19207 while (g < toend && w < it->truncation_pixel_width)
19208 {
19209 w += g->pixel_width;
19210 ++g;
19211 }
19212 if (g - to - tused > 0)
19213 {
19214 memmove (to + tused, g, (toend - g) * sizeof(*g));
19215 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19216 }
19217 used = it->glyph_row->used[TEXT_AREA];
19218 if (it->glyph_row->truncated_on_right_p
19219 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19220 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19221 == STRETCH_GLYPH)
19222 {
19223 int extra = w - it->truncation_pixel_width;
19224
19225 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19226 }
19227 }
19228
19229 while (from < end)
19230 *to++ = *from++;
19231
19232 /* There may be padding glyphs left over. Overwrite them too. */
19233 if (!FRAME_WINDOW_P (it->f))
19234 {
19235 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19236 {
19237 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19238 while (from < end)
19239 *to++ = *from++;
19240 }
19241 }
19242
19243 if (to > toend)
19244 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19245 }
19246 else
19247 {
19248 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19249
19250 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19251 that back to front. */
19252 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19253 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19254 toend = it->glyph_row->glyphs[TEXT_AREA];
19255 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19256 if (FRAME_WINDOW_P (it->f))
19257 {
19258 int w = 0;
19259 struct glyph *g = to;
19260
19261 while (g >= toend && w < it->truncation_pixel_width)
19262 {
19263 w += g->pixel_width;
19264 --g;
19265 }
19266 if (to - g - tused > 0)
19267 to = g + tused;
19268 if (it->glyph_row->truncated_on_right_p
19269 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19270 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19271 {
19272 int extra = w - it->truncation_pixel_width;
19273
19274 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19275 }
19276 }
19277
19278 while (from >= end && to >= toend)
19279 *to-- = *from--;
19280 if (!FRAME_WINDOW_P (it->f))
19281 {
19282 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19283 {
19284 from =
19285 truncate_it.glyph_row->glyphs[TEXT_AREA]
19286 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19287 while (from >= end && to >= toend)
19288 *to-- = *from--;
19289 }
19290 }
19291 if (from >= end)
19292 {
19293 /* Need to free some room before prepending additional
19294 glyphs. */
19295 int move_by = from - end + 1;
19296 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19297 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19298
19299 for ( ; g >= g0; g--)
19300 g[move_by] = *g;
19301 while (from >= end)
19302 *to-- = *from--;
19303 it->glyph_row->used[TEXT_AREA] += move_by;
19304 }
19305 }
19306 }
19307
19308 /* Compute the hash code for ROW. */
19309 unsigned
19310 row_hash (struct glyph_row *row)
19311 {
19312 int area, k;
19313 unsigned hashval = 0;
19314
19315 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19316 for (k = 0; k < row->used[area]; ++k)
19317 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19318 + row->glyphs[area][k].u.val
19319 + row->glyphs[area][k].face_id
19320 + row->glyphs[area][k].padding_p
19321 + (row->glyphs[area][k].type << 2));
19322
19323 return hashval;
19324 }
19325
19326 /* Compute the pixel height and width of IT->glyph_row.
19327
19328 Most of the time, ascent and height of a display line will be equal
19329 to the max_ascent and max_height values of the display iterator
19330 structure. This is not the case if
19331
19332 1. We hit ZV without displaying anything. In this case, max_ascent
19333 and max_height will be zero.
19334
19335 2. We have some glyphs that don't contribute to the line height.
19336 (The glyph row flag contributes_to_line_height_p is for future
19337 pixmap extensions).
19338
19339 The first case is easily covered by using default values because in
19340 these cases, the line height does not really matter, except that it
19341 must not be zero. */
19342
19343 static void
19344 compute_line_metrics (struct it *it)
19345 {
19346 struct glyph_row *row = it->glyph_row;
19347
19348 if (FRAME_WINDOW_P (it->f))
19349 {
19350 int i, min_y, max_y;
19351
19352 /* The line may consist of one space only, that was added to
19353 place the cursor on it. If so, the row's height hasn't been
19354 computed yet. */
19355 if (row->height == 0)
19356 {
19357 if (it->max_ascent + it->max_descent == 0)
19358 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19359 row->ascent = it->max_ascent;
19360 row->height = it->max_ascent + it->max_descent;
19361 row->phys_ascent = it->max_phys_ascent;
19362 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19363 row->extra_line_spacing = it->max_extra_line_spacing;
19364 }
19365
19366 /* Compute the width of this line. */
19367 row->pixel_width = row->x;
19368 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19369 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19370
19371 eassert (row->pixel_width >= 0);
19372 eassert (row->ascent >= 0 && row->height > 0);
19373
19374 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19375 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19376
19377 /* If first line's physical ascent is larger than its logical
19378 ascent, use the physical ascent, and make the row taller.
19379 This makes accented characters fully visible. */
19380 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19381 && row->phys_ascent > row->ascent)
19382 {
19383 row->height += row->phys_ascent - row->ascent;
19384 row->ascent = row->phys_ascent;
19385 }
19386
19387 /* Compute how much of the line is visible. */
19388 row->visible_height = row->height;
19389
19390 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19391 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19392
19393 if (row->y < min_y)
19394 row->visible_height -= min_y - row->y;
19395 if (row->y + row->height > max_y)
19396 row->visible_height -= row->y + row->height - max_y;
19397 }
19398 else
19399 {
19400 row->pixel_width = row->used[TEXT_AREA];
19401 if (row->continued_p)
19402 row->pixel_width -= it->continuation_pixel_width;
19403 else if (row->truncated_on_right_p)
19404 row->pixel_width -= it->truncation_pixel_width;
19405 row->ascent = row->phys_ascent = 0;
19406 row->height = row->phys_height = row->visible_height = 1;
19407 row->extra_line_spacing = 0;
19408 }
19409
19410 /* Compute a hash code for this row. */
19411 row->hash = row_hash (row);
19412
19413 it->max_ascent = it->max_descent = 0;
19414 it->max_phys_ascent = it->max_phys_descent = 0;
19415 }
19416
19417
19418 /* Append one space to the glyph row of iterator IT if doing a
19419 window-based redisplay. The space has the same face as
19420 IT->face_id. Value is true if a space was added.
19421
19422 This function is called to make sure that there is always one glyph
19423 at the end of a glyph row that the cursor can be set on under
19424 window-systems. (If there weren't such a glyph we would not know
19425 how wide and tall a box cursor should be displayed).
19426
19427 At the same time this space let's a nicely handle clearing to the
19428 end of the line if the row ends in italic text. */
19429
19430 static bool
19431 append_space_for_newline (struct it *it, bool default_face_p)
19432 {
19433 if (FRAME_WINDOW_P (it->f))
19434 {
19435 int n = it->glyph_row->used[TEXT_AREA];
19436
19437 if (it->glyph_row->glyphs[TEXT_AREA] + n
19438 < it->glyph_row->glyphs[1 + TEXT_AREA])
19439 {
19440 /* Save some values that must not be changed.
19441 Must save IT->c and IT->len because otherwise
19442 ITERATOR_AT_END_P wouldn't work anymore after
19443 append_space_for_newline has been called. */
19444 enum display_element_type saved_what = it->what;
19445 int saved_c = it->c, saved_len = it->len;
19446 int saved_char_to_display = it->char_to_display;
19447 int saved_x = it->current_x;
19448 int saved_face_id = it->face_id;
19449 bool saved_box_end = it->end_of_box_run_p;
19450 struct text_pos saved_pos;
19451 Lisp_Object saved_object;
19452 struct face *face;
19453 struct glyph *g;
19454
19455 saved_object = it->object;
19456 saved_pos = it->position;
19457
19458 it->what = IT_CHARACTER;
19459 memset (&it->position, 0, sizeof it->position);
19460 it->object = Qnil;
19461 it->c = it->char_to_display = ' ';
19462 it->len = 1;
19463
19464 /* If the default face was remapped, be sure to use the
19465 remapped face for the appended newline. */
19466 if (default_face_p)
19467 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19468 else if (it->face_before_selective_p)
19469 it->face_id = it->saved_face_id;
19470 face = FACE_FROM_ID (it->f, it->face_id);
19471 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19472 /* In R2L rows, we will prepend a stretch glyph that will
19473 have the end_of_box_run_p flag set for it, so there's no
19474 need for the appended newline glyph to have that flag
19475 set. */
19476 if (it->glyph_row->reversed_p
19477 /* But if the appended newline glyph goes all the way to
19478 the end of the row, there will be no stretch glyph,
19479 so leave the box flag set. */
19480 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19481 it->end_of_box_run_p = false;
19482
19483 PRODUCE_GLYPHS (it);
19484
19485 #ifdef HAVE_WINDOW_SYSTEM
19486 /* Make sure this space glyph has the right ascent and
19487 descent values, or else cursor at end of line will look
19488 funny, and height of empty lines will be incorrect. */
19489 g = it->glyph_row->glyphs[TEXT_AREA] + n;
19490 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19491 if (n == 0)
19492 {
19493 Lisp_Object height, total_height;
19494 int extra_line_spacing = it->extra_line_spacing;
19495 int boff = font->baseline_offset;
19496
19497 if (font->vertical_centering)
19498 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19499
19500 it->object = saved_object; /* get_it_property needs this */
19501 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19502 /* Must do a subset of line height processing from
19503 x_produce_glyph for newline characters. */
19504 height = get_it_property (it, Qline_height);
19505 if (CONSP (height)
19506 && CONSP (XCDR (height))
19507 && NILP (XCDR (XCDR (height))))
19508 {
19509 total_height = XCAR (XCDR (height));
19510 height = XCAR (height);
19511 }
19512 else
19513 total_height = Qnil;
19514 height = calc_line_height_property (it, height, font, boff, true);
19515
19516 if (it->override_ascent >= 0)
19517 {
19518 it->ascent = it->override_ascent;
19519 it->descent = it->override_descent;
19520 boff = it->override_boff;
19521 }
19522 if (EQ (height, Qt))
19523 extra_line_spacing = 0;
19524 else
19525 {
19526 Lisp_Object spacing;
19527
19528 it->phys_ascent = it->ascent;
19529 it->phys_descent = it->descent;
19530 if (!NILP (height)
19531 && XINT (height) > it->ascent + it->descent)
19532 it->ascent = XINT (height) - it->descent;
19533
19534 if (!NILP (total_height))
19535 spacing = calc_line_height_property (it, total_height, font,
19536 boff, false);
19537 else
19538 {
19539 spacing = get_it_property (it, Qline_spacing);
19540 spacing = calc_line_height_property (it, spacing, font,
19541 boff, false);
19542 }
19543 if (INTEGERP (spacing))
19544 {
19545 extra_line_spacing = XINT (spacing);
19546 if (!NILP (total_height))
19547 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19548 }
19549 }
19550 if (extra_line_spacing > 0)
19551 {
19552 it->descent += extra_line_spacing;
19553 if (extra_line_spacing > it->max_extra_line_spacing)
19554 it->max_extra_line_spacing = extra_line_spacing;
19555 }
19556 it->max_ascent = it->ascent;
19557 it->max_descent = it->descent;
19558 /* Make sure compute_line_metrics recomputes the row height. */
19559 it->glyph_row->height = 0;
19560 }
19561
19562 g->ascent = it->max_ascent;
19563 g->descent = it->max_descent;
19564 #endif
19565
19566 it->override_ascent = -1;
19567 it->constrain_row_ascent_descent_p = false;
19568 it->current_x = saved_x;
19569 it->object = saved_object;
19570 it->position = saved_pos;
19571 it->what = saved_what;
19572 it->face_id = saved_face_id;
19573 it->len = saved_len;
19574 it->c = saved_c;
19575 it->char_to_display = saved_char_to_display;
19576 it->end_of_box_run_p = saved_box_end;
19577 return true;
19578 }
19579 }
19580
19581 return false;
19582 }
19583
19584
19585 /* Extend the face of the last glyph in the text area of IT->glyph_row
19586 to the end of the display line. Called from display_line. If the
19587 glyph row is empty, add a space glyph to it so that we know the
19588 face to draw. Set the glyph row flag fill_line_p. If the glyph
19589 row is R2L, prepend a stretch glyph to cover the empty space to the
19590 left of the leftmost glyph. */
19591
19592 static void
19593 extend_face_to_end_of_line (struct it *it)
19594 {
19595 struct face *face, *default_face;
19596 struct frame *f = it->f;
19597
19598 /* If line is already filled, do nothing. Non window-system frames
19599 get a grace of one more ``pixel'' because their characters are
19600 1-``pixel'' wide, so they hit the equality too early. This grace
19601 is needed only for R2L rows that are not continued, to produce
19602 one extra blank where we could display the cursor. */
19603 if ((it->current_x >= it->last_visible_x
19604 + (!FRAME_WINDOW_P (f)
19605 && it->glyph_row->reversed_p
19606 && !it->glyph_row->continued_p))
19607 /* If the window has display margins, we will need to extend
19608 their face even if the text area is filled. */
19609 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19610 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19611 return;
19612
19613 /* The default face, possibly remapped. */
19614 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19615
19616 /* Face extension extends the background and box of IT->face_id
19617 to the end of the line. If the background equals the background
19618 of the frame, we don't have to do anything. */
19619 if (it->face_before_selective_p)
19620 face = FACE_FROM_ID (f, it->saved_face_id);
19621 else
19622 face = FACE_FROM_ID (f, it->face_id);
19623
19624 if (FRAME_WINDOW_P (f)
19625 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19626 && face->box == FACE_NO_BOX
19627 && face->background == FRAME_BACKGROUND_PIXEL (f)
19628 #ifdef HAVE_WINDOW_SYSTEM
19629 && !face->stipple
19630 #endif
19631 && !it->glyph_row->reversed_p)
19632 return;
19633
19634 /* Set the glyph row flag indicating that the face of the last glyph
19635 in the text area has to be drawn to the end of the text area. */
19636 it->glyph_row->fill_line_p = true;
19637
19638 /* If current character of IT is not ASCII, make sure we have the
19639 ASCII face. This will be automatically undone the next time
19640 get_next_display_element returns a multibyte character. Note
19641 that the character will always be single byte in unibyte
19642 text. */
19643 if (!ASCII_CHAR_P (it->c))
19644 {
19645 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19646 }
19647
19648 if (FRAME_WINDOW_P (f))
19649 {
19650 /* If the row is empty, add a space with the current face of IT,
19651 so that we know which face to draw. */
19652 if (it->glyph_row->used[TEXT_AREA] == 0)
19653 {
19654 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19655 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19656 it->glyph_row->used[TEXT_AREA] = 1;
19657 }
19658 /* Mode line and the header line don't have margins, and
19659 likewise the frame's tool-bar window, if there is any. */
19660 if (!(it->glyph_row->mode_line_p
19661 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19662 || (WINDOWP (f->tool_bar_window)
19663 && it->w == XWINDOW (f->tool_bar_window))
19664 #endif
19665 ))
19666 {
19667 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19668 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19669 {
19670 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19671 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19672 default_face->id;
19673 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19674 }
19675 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19676 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19677 {
19678 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19679 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19680 default_face->id;
19681 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19682 }
19683 }
19684 #ifdef HAVE_WINDOW_SYSTEM
19685 if (it->glyph_row->reversed_p)
19686 {
19687 /* Prepend a stretch glyph to the row, such that the
19688 rightmost glyph will be drawn flushed all the way to the
19689 right margin of the window. The stretch glyph that will
19690 occupy the empty space, if any, to the left of the
19691 glyphs. */
19692 struct font *font = face->font ? face->font : FRAME_FONT (f);
19693 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19694 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19695 struct glyph *g;
19696 int row_width, stretch_ascent, stretch_width;
19697 struct text_pos saved_pos;
19698 int saved_face_id;
19699 bool saved_avoid_cursor, saved_box_start;
19700
19701 for (row_width = 0, g = row_start; g < row_end; g++)
19702 row_width += g->pixel_width;
19703
19704 /* FIXME: There are various minor display glitches in R2L
19705 rows when only one of the fringes is missing. The
19706 strange condition below produces the least bad effect. */
19707 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19708 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19709 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19710 stretch_width = window_box_width (it->w, TEXT_AREA);
19711 else
19712 stretch_width = it->last_visible_x - it->first_visible_x;
19713 stretch_width -= row_width;
19714
19715 if (stretch_width > 0)
19716 {
19717 stretch_ascent =
19718 (((it->ascent + it->descent)
19719 * FONT_BASE (font)) / FONT_HEIGHT (font));
19720 saved_pos = it->position;
19721 memset (&it->position, 0, sizeof it->position);
19722 saved_avoid_cursor = it->avoid_cursor_p;
19723 it->avoid_cursor_p = true;
19724 saved_face_id = it->face_id;
19725 saved_box_start = it->start_of_box_run_p;
19726 /* The last row's stretch glyph should get the default
19727 face, to avoid painting the rest of the window with
19728 the region face, if the region ends at ZV. */
19729 if (it->glyph_row->ends_at_zv_p)
19730 it->face_id = default_face->id;
19731 else
19732 it->face_id = face->id;
19733 it->start_of_box_run_p = false;
19734 append_stretch_glyph (it, Qnil, stretch_width,
19735 it->ascent + it->descent, stretch_ascent);
19736 it->position = saved_pos;
19737 it->avoid_cursor_p = saved_avoid_cursor;
19738 it->face_id = saved_face_id;
19739 it->start_of_box_run_p = saved_box_start;
19740 }
19741 /* If stretch_width comes out negative, it means that the
19742 last glyph is only partially visible. In R2L rows, we
19743 want the leftmost glyph to be partially visible, so we
19744 need to give the row the corresponding left offset. */
19745 if (stretch_width < 0)
19746 it->glyph_row->x = stretch_width;
19747 }
19748 #endif /* HAVE_WINDOW_SYSTEM */
19749 }
19750 else
19751 {
19752 /* Save some values that must not be changed. */
19753 int saved_x = it->current_x;
19754 struct text_pos saved_pos;
19755 Lisp_Object saved_object;
19756 enum display_element_type saved_what = it->what;
19757 int saved_face_id = it->face_id;
19758
19759 saved_object = it->object;
19760 saved_pos = it->position;
19761
19762 it->what = IT_CHARACTER;
19763 memset (&it->position, 0, sizeof it->position);
19764 it->object = Qnil;
19765 it->c = it->char_to_display = ' ';
19766 it->len = 1;
19767
19768 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19769 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19770 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19771 && !it->glyph_row->mode_line_p
19772 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19773 {
19774 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19775 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19776
19777 for (it->current_x = 0; g < e; g++)
19778 it->current_x += g->pixel_width;
19779
19780 it->area = LEFT_MARGIN_AREA;
19781 it->face_id = default_face->id;
19782 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19783 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19784 {
19785 PRODUCE_GLYPHS (it);
19786 /* term.c:produce_glyphs advances it->current_x only for
19787 TEXT_AREA. */
19788 it->current_x += it->pixel_width;
19789 }
19790
19791 it->current_x = saved_x;
19792 it->area = TEXT_AREA;
19793 }
19794
19795 /* The last row's blank glyphs should get the default face, to
19796 avoid painting the rest of the window with the region face,
19797 if the region ends at ZV. */
19798 if (it->glyph_row->ends_at_zv_p)
19799 it->face_id = default_face->id;
19800 else
19801 it->face_id = face->id;
19802 PRODUCE_GLYPHS (it);
19803
19804 while (it->current_x <= it->last_visible_x)
19805 PRODUCE_GLYPHS (it);
19806
19807 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19808 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19809 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19810 && !it->glyph_row->mode_line_p
19811 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19812 {
19813 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19814 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19815
19816 for ( ; g < e; g++)
19817 it->current_x += g->pixel_width;
19818
19819 it->area = RIGHT_MARGIN_AREA;
19820 it->face_id = default_face->id;
19821 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19822 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19823 {
19824 PRODUCE_GLYPHS (it);
19825 it->current_x += it->pixel_width;
19826 }
19827
19828 it->area = TEXT_AREA;
19829 }
19830
19831 /* Don't count these blanks really. It would let us insert a left
19832 truncation glyph below and make us set the cursor on them, maybe. */
19833 it->current_x = saved_x;
19834 it->object = saved_object;
19835 it->position = saved_pos;
19836 it->what = saved_what;
19837 it->face_id = saved_face_id;
19838 }
19839 }
19840
19841
19842 /* Value is true if text starting at CHARPOS in current_buffer is
19843 trailing whitespace. */
19844
19845 static bool
19846 trailing_whitespace_p (ptrdiff_t charpos)
19847 {
19848 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19849 int c = 0;
19850
19851 while (bytepos < ZV_BYTE
19852 && (c = FETCH_CHAR (bytepos),
19853 c == ' ' || c == '\t'))
19854 ++bytepos;
19855
19856 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19857 {
19858 if (bytepos != PT_BYTE)
19859 return true;
19860 }
19861 return false;
19862 }
19863
19864
19865 /* Highlight trailing whitespace, if any, in ROW. */
19866
19867 static void
19868 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19869 {
19870 int used = row->used[TEXT_AREA];
19871
19872 if (used)
19873 {
19874 struct glyph *start = row->glyphs[TEXT_AREA];
19875 struct glyph *glyph = start + used - 1;
19876
19877 if (row->reversed_p)
19878 {
19879 /* Right-to-left rows need to be processed in the opposite
19880 direction, so swap the edge pointers. */
19881 glyph = start;
19882 start = row->glyphs[TEXT_AREA] + used - 1;
19883 }
19884
19885 /* Skip over glyphs inserted to display the cursor at the
19886 end of a line, for extending the face of the last glyph
19887 to the end of the line on terminals, and for truncation
19888 and continuation glyphs. */
19889 if (!row->reversed_p)
19890 {
19891 while (glyph >= start
19892 && glyph->type == CHAR_GLYPH
19893 && NILP (glyph->object))
19894 --glyph;
19895 }
19896 else
19897 {
19898 while (glyph <= start
19899 && glyph->type == CHAR_GLYPH
19900 && NILP (glyph->object))
19901 ++glyph;
19902 }
19903
19904 /* If last glyph is a space or stretch, and it's trailing
19905 whitespace, set the face of all trailing whitespace glyphs in
19906 IT->glyph_row to `trailing-whitespace'. */
19907 if ((row->reversed_p ? glyph <= start : glyph >= start)
19908 && BUFFERP (glyph->object)
19909 && (glyph->type == STRETCH_GLYPH
19910 || (glyph->type == CHAR_GLYPH
19911 && glyph->u.ch == ' '))
19912 && trailing_whitespace_p (glyph->charpos))
19913 {
19914 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19915 if (face_id < 0)
19916 return;
19917
19918 if (!row->reversed_p)
19919 {
19920 while (glyph >= start
19921 && BUFFERP (glyph->object)
19922 && (glyph->type == STRETCH_GLYPH
19923 || (glyph->type == CHAR_GLYPH
19924 && glyph->u.ch == ' ')))
19925 (glyph--)->face_id = face_id;
19926 }
19927 else
19928 {
19929 while (glyph <= start
19930 && BUFFERP (glyph->object)
19931 && (glyph->type == STRETCH_GLYPH
19932 || (glyph->type == CHAR_GLYPH
19933 && glyph->u.ch == ' ')))
19934 (glyph++)->face_id = face_id;
19935 }
19936 }
19937 }
19938 }
19939
19940
19941 /* Value is true if glyph row ROW should be
19942 considered to hold the buffer position CHARPOS. */
19943
19944 static bool
19945 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19946 {
19947 bool result = true;
19948
19949 if (charpos == CHARPOS (row->end.pos)
19950 || charpos == MATRIX_ROW_END_CHARPOS (row))
19951 {
19952 /* Suppose the row ends on a string.
19953 Unless the row is continued, that means it ends on a newline
19954 in the string. If it's anything other than a display string
19955 (e.g., a before-string from an overlay), we don't want the
19956 cursor there. (This heuristic seems to give the optimal
19957 behavior for the various types of multi-line strings.)
19958 One exception: if the string has `cursor' property on one of
19959 its characters, we _do_ want the cursor there. */
19960 if (CHARPOS (row->end.string_pos) >= 0)
19961 {
19962 if (row->continued_p)
19963 result = true;
19964 else
19965 {
19966 /* Check for `display' property. */
19967 struct glyph *beg = row->glyphs[TEXT_AREA];
19968 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19969 struct glyph *glyph;
19970
19971 result = false;
19972 for (glyph = end; glyph >= beg; --glyph)
19973 if (STRINGP (glyph->object))
19974 {
19975 Lisp_Object prop
19976 = Fget_char_property (make_number (charpos),
19977 Qdisplay, Qnil);
19978 result =
19979 (!NILP (prop)
19980 && display_prop_string_p (prop, glyph->object));
19981 /* If there's a `cursor' property on one of the
19982 string's characters, this row is a cursor row,
19983 even though this is not a display string. */
19984 if (!result)
19985 {
19986 Lisp_Object s = glyph->object;
19987
19988 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19989 {
19990 ptrdiff_t gpos = glyph->charpos;
19991
19992 if (!NILP (Fget_char_property (make_number (gpos),
19993 Qcursor, s)))
19994 {
19995 result = true;
19996 break;
19997 }
19998 }
19999 }
20000 break;
20001 }
20002 }
20003 }
20004 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20005 {
20006 /* If the row ends in middle of a real character,
20007 and the line is continued, we want the cursor here.
20008 That's because CHARPOS (ROW->end.pos) would equal
20009 PT if PT is before the character. */
20010 if (!row->ends_in_ellipsis_p)
20011 result = row->continued_p;
20012 else
20013 /* If the row ends in an ellipsis, then
20014 CHARPOS (ROW->end.pos) will equal point after the
20015 invisible text. We want that position to be displayed
20016 after the ellipsis. */
20017 result = false;
20018 }
20019 /* If the row ends at ZV, display the cursor at the end of that
20020 row instead of at the start of the row below. */
20021 else
20022 result = row->ends_at_zv_p;
20023 }
20024
20025 return result;
20026 }
20027
20028 /* Value is true if glyph row ROW should be
20029 used to hold the cursor. */
20030
20031 static bool
20032 cursor_row_p (struct glyph_row *row)
20033 {
20034 return row_for_charpos_p (row, PT);
20035 }
20036
20037 \f
20038
20039 /* Push the property PROP so that it will be rendered at the current
20040 position in IT. Return true if PROP was successfully pushed, false
20041 otherwise. Called from handle_line_prefix to handle the
20042 `line-prefix' and `wrap-prefix' properties. */
20043
20044 static bool
20045 push_prefix_prop (struct it *it, Lisp_Object prop)
20046 {
20047 struct text_pos pos =
20048 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20049
20050 eassert (it->method == GET_FROM_BUFFER
20051 || it->method == GET_FROM_DISPLAY_VECTOR
20052 || it->method == GET_FROM_STRING
20053 || it->method == GET_FROM_IMAGE);
20054
20055 /* We need to save the current buffer/string position, so it will be
20056 restored by pop_it, because iterate_out_of_display_property
20057 depends on that being set correctly, but some situations leave
20058 it->position not yet set when this function is called. */
20059 push_it (it, &pos);
20060
20061 if (STRINGP (prop))
20062 {
20063 if (SCHARS (prop) == 0)
20064 {
20065 pop_it (it);
20066 return false;
20067 }
20068
20069 it->string = prop;
20070 it->string_from_prefix_prop_p = true;
20071 it->multibyte_p = STRING_MULTIBYTE (it->string);
20072 it->current.overlay_string_index = -1;
20073 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20074 it->end_charpos = it->string_nchars = SCHARS (it->string);
20075 it->method = GET_FROM_STRING;
20076 it->stop_charpos = 0;
20077 it->prev_stop = 0;
20078 it->base_level_stop = 0;
20079
20080 /* Force paragraph direction to be that of the parent
20081 buffer/string. */
20082 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20083 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20084 else
20085 it->paragraph_embedding = L2R;
20086
20087 /* Set up the bidi iterator for this display string. */
20088 if (it->bidi_p)
20089 {
20090 it->bidi_it.string.lstring = it->string;
20091 it->bidi_it.string.s = NULL;
20092 it->bidi_it.string.schars = it->end_charpos;
20093 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20094 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20095 it->bidi_it.string.unibyte = !it->multibyte_p;
20096 it->bidi_it.w = it->w;
20097 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20098 }
20099 }
20100 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20101 {
20102 it->method = GET_FROM_STRETCH;
20103 it->object = prop;
20104 }
20105 #ifdef HAVE_WINDOW_SYSTEM
20106 else if (IMAGEP (prop))
20107 {
20108 it->what = IT_IMAGE;
20109 it->image_id = lookup_image (it->f, prop);
20110 it->method = GET_FROM_IMAGE;
20111 }
20112 #endif /* HAVE_WINDOW_SYSTEM */
20113 else
20114 {
20115 pop_it (it); /* bogus display property, give up */
20116 return false;
20117 }
20118
20119 return true;
20120 }
20121
20122 /* Return the character-property PROP at the current position in IT. */
20123
20124 static Lisp_Object
20125 get_it_property (struct it *it, Lisp_Object prop)
20126 {
20127 Lisp_Object position, object = it->object;
20128
20129 if (STRINGP (object))
20130 position = make_number (IT_STRING_CHARPOS (*it));
20131 else if (BUFFERP (object))
20132 {
20133 position = make_number (IT_CHARPOS (*it));
20134 object = it->window;
20135 }
20136 else
20137 return Qnil;
20138
20139 return Fget_char_property (position, prop, object);
20140 }
20141
20142 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20143
20144 static void
20145 handle_line_prefix (struct it *it)
20146 {
20147 Lisp_Object prefix;
20148
20149 if (it->continuation_lines_width > 0)
20150 {
20151 prefix = get_it_property (it, Qwrap_prefix);
20152 if (NILP (prefix))
20153 prefix = Vwrap_prefix;
20154 }
20155 else
20156 {
20157 prefix = get_it_property (it, Qline_prefix);
20158 if (NILP (prefix))
20159 prefix = Vline_prefix;
20160 }
20161 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20162 {
20163 /* If the prefix is wider than the window, and we try to wrap
20164 it, it would acquire its own wrap prefix, and so on till the
20165 iterator stack overflows. So, don't wrap the prefix. */
20166 it->line_wrap = TRUNCATE;
20167 it->avoid_cursor_p = true;
20168 }
20169 }
20170
20171 \f
20172
20173 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20174 only for R2L lines from display_line and display_string, when they
20175 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20176 the line/string needs to be continued on the next glyph row. */
20177 static void
20178 unproduce_glyphs (struct it *it, int n)
20179 {
20180 struct glyph *glyph, *end;
20181
20182 eassert (it->glyph_row);
20183 eassert (it->glyph_row->reversed_p);
20184 eassert (it->area == TEXT_AREA);
20185 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20186
20187 if (n > it->glyph_row->used[TEXT_AREA])
20188 n = it->glyph_row->used[TEXT_AREA];
20189 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20190 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20191 for ( ; glyph < end; glyph++)
20192 glyph[-n] = *glyph;
20193 }
20194
20195 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20196 and ROW->maxpos. */
20197 static void
20198 find_row_edges (struct it *it, struct glyph_row *row,
20199 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20200 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20201 {
20202 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20203 lines' rows is implemented for bidi-reordered rows. */
20204
20205 /* ROW->minpos is the value of min_pos, the minimal buffer position
20206 we have in ROW, or ROW->start.pos if that is smaller. */
20207 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20208 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20209 else
20210 /* We didn't find buffer positions smaller than ROW->start, or
20211 didn't find _any_ valid buffer positions in any of the glyphs,
20212 so we must trust the iterator's computed positions. */
20213 row->minpos = row->start.pos;
20214 if (max_pos <= 0)
20215 {
20216 max_pos = CHARPOS (it->current.pos);
20217 max_bpos = BYTEPOS (it->current.pos);
20218 }
20219
20220 /* Here are the various use-cases for ending the row, and the
20221 corresponding values for ROW->maxpos:
20222
20223 Line ends in a newline from buffer eol_pos + 1
20224 Line is continued from buffer max_pos + 1
20225 Line is truncated on right it->current.pos
20226 Line ends in a newline from string max_pos + 1(*)
20227 (*) + 1 only when line ends in a forward scan
20228 Line is continued from string max_pos
20229 Line is continued from display vector max_pos
20230 Line is entirely from a string min_pos == max_pos
20231 Line is entirely from a display vector min_pos == max_pos
20232 Line that ends at ZV ZV
20233
20234 If you discover other use-cases, please add them here as
20235 appropriate. */
20236 if (row->ends_at_zv_p)
20237 row->maxpos = it->current.pos;
20238 else if (row->used[TEXT_AREA])
20239 {
20240 bool seen_this_string = false;
20241 struct glyph_row *r1 = row - 1;
20242
20243 /* Did we see the same display string on the previous row? */
20244 if (STRINGP (it->object)
20245 /* this is not the first row */
20246 && row > it->w->desired_matrix->rows
20247 /* previous row is not the header line */
20248 && !r1->mode_line_p
20249 /* previous row also ends in a newline from a string */
20250 && r1->ends_in_newline_from_string_p)
20251 {
20252 struct glyph *start, *end;
20253
20254 /* Search for the last glyph of the previous row that came
20255 from buffer or string. Depending on whether the row is
20256 L2R or R2L, we need to process it front to back or the
20257 other way round. */
20258 if (!r1->reversed_p)
20259 {
20260 start = r1->glyphs[TEXT_AREA];
20261 end = start + r1->used[TEXT_AREA];
20262 /* Glyphs inserted by redisplay have nil as their object. */
20263 while (end > start
20264 && NILP ((end - 1)->object)
20265 && (end - 1)->charpos <= 0)
20266 --end;
20267 if (end > start)
20268 {
20269 if (EQ ((end - 1)->object, it->object))
20270 seen_this_string = true;
20271 }
20272 else
20273 /* If all the glyphs of the previous row were inserted
20274 by redisplay, it means the previous row was
20275 produced from a single newline, which is only
20276 possible if that newline came from the same string
20277 as the one which produced this ROW. */
20278 seen_this_string = true;
20279 }
20280 else
20281 {
20282 end = r1->glyphs[TEXT_AREA] - 1;
20283 start = end + r1->used[TEXT_AREA];
20284 while (end < start
20285 && NILP ((end + 1)->object)
20286 && (end + 1)->charpos <= 0)
20287 ++end;
20288 if (end < start)
20289 {
20290 if (EQ ((end + 1)->object, it->object))
20291 seen_this_string = true;
20292 }
20293 else
20294 seen_this_string = true;
20295 }
20296 }
20297 /* Take note of each display string that covers a newline only
20298 once, the first time we see it. This is for when a display
20299 string includes more than one newline in it. */
20300 if (row->ends_in_newline_from_string_p && !seen_this_string)
20301 {
20302 /* If we were scanning the buffer forward when we displayed
20303 the string, we want to account for at least one buffer
20304 position that belongs to this row (position covered by
20305 the display string), so that cursor positioning will
20306 consider this row as a candidate when point is at the end
20307 of the visual line represented by this row. This is not
20308 required when scanning back, because max_pos will already
20309 have a much larger value. */
20310 if (CHARPOS (row->end.pos) > max_pos)
20311 INC_BOTH (max_pos, max_bpos);
20312 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20313 }
20314 else if (CHARPOS (it->eol_pos) > 0)
20315 SET_TEXT_POS (row->maxpos,
20316 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20317 else if (row->continued_p)
20318 {
20319 /* If max_pos is different from IT's current position, it
20320 means IT->method does not belong to the display element
20321 at max_pos. However, it also means that the display
20322 element at max_pos was displayed in its entirety on this
20323 line, which is equivalent to saying that the next line
20324 starts at the next buffer position. */
20325 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20326 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20327 else
20328 {
20329 INC_BOTH (max_pos, max_bpos);
20330 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20331 }
20332 }
20333 else if (row->truncated_on_right_p)
20334 /* display_line already called reseat_at_next_visible_line_start,
20335 which puts the iterator at the beginning of the next line, in
20336 the logical order. */
20337 row->maxpos = it->current.pos;
20338 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20339 /* A line that is entirely from a string/image/stretch... */
20340 row->maxpos = row->minpos;
20341 else
20342 emacs_abort ();
20343 }
20344 else
20345 row->maxpos = it->current.pos;
20346 }
20347
20348 /* Construct the glyph row IT->glyph_row in the desired matrix of
20349 IT->w from text at the current position of IT. See dispextern.h
20350 for an overview of struct it. Value is true if
20351 IT->glyph_row displays text, as opposed to a line displaying ZV
20352 only. */
20353
20354 static bool
20355 display_line (struct it *it)
20356 {
20357 struct glyph_row *row = it->glyph_row;
20358 Lisp_Object overlay_arrow_string;
20359 struct it wrap_it;
20360 void *wrap_data = NULL;
20361 bool may_wrap = false;
20362 int wrap_x IF_LINT (= 0);
20363 int wrap_row_used = -1;
20364 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20365 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20366 int wrap_row_extra_line_spacing IF_LINT (= 0);
20367 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20368 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20369 int cvpos;
20370 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20371 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20372 bool pending_handle_line_prefix = false;
20373
20374 /* We always start displaying at hpos zero even if hscrolled. */
20375 eassert (it->hpos == 0 && it->current_x == 0);
20376
20377 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20378 >= it->w->desired_matrix->nrows)
20379 {
20380 it->w->nrows_scale_factor++;
20381 it->f->fonts_changed = true;
20382 return false;
20383 }
20384
20385 /* Clear the result glyph row and enable it. */
20386 prepare_desired_row (it->w, row, false);
20387
20388 row->y = it->current_y;
20389 row->start = it->start;
20390 row->continuation_lines_width = it->continuation_lines_width;
20391 row->displays_text_p = true;
20392 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20393 it->starts_in_middle_of_char_p = false;
20394
20395 /* Arrange the overlays nicely for our purposes. Usually, we call
20396 display_line on only one line at a time, in which case this
20397 can't really hurt too much, or we call it on lines which appear
20398 one after another in the buffer, in which case all calls to
20399 recenter_overlay_lists but the first will be pretty cheap. */
20400 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20401
20402 /* Move over display elements that are not visible because we are
20403 hscrolled. This may stop at an x-position < IT->first_visible_x
20404 if the first glyph is partially visible or if we hit a line end. */
20405 if (it->current_x < it->first_visible_x)
20406 {
20407 enum move_it_result move_result;
20408
20409 this_line_min_pos = row->start.pos;
20410 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20411 MOVE_TO_POS | MOVE_TO_X);
20412 /* If we are under a large hscroll, move_it_in_display_line_to
20413 could hit the end of the line without reaching
20414 it->first_visible_x. Pretend that we did reach it. This is
20415 especially important on a TTY, where we will call
20416 extend_face_to_end_of_line, which needs to know how many
20417 blank glyphs to produce. */
20418 if (it->current_x < it->first_visible_x
20419 && (move_result == MOVE_NEWLINE_OR_CR
20420 || move_result == MOVE_POS_MATCH_OR_ZV))
20421 it->current_x = it->first_visible_x;
20422
20423 /* Record the smallest positions seen while we moved over
20424 display elements that are not visible. This is needed by
20425 redisplay_internal for optimizing the case where the cursor
20426 stays inside the same line. The rest of this function only
20427 considers positions that are actually displayed, so
20428 RECORD_MAX_MIN_POS will not otherwise record positions that
20429 are hscrolled to the left of the left edge of the window. */
20430 min_pos = CHARPOS (this_line_min_pos);
20431 min_bpos = BYTEPOS (this_line_min_pos);
20432 }
20433 else if (it->area == TEXT_AREA)
20434 {
20435 /* We only do this when not calling move_it_in_display_line_to
20436 above, because that function calls itself handle_line_prefix. */
20437 handle_line_prefix (it);
20438 }
20439 else
20440 {
20441 /* Line-prefix and wrap-prefix are always displayed in the text
20442 area. But if this is the first call to display_line after
20443 init_iterator, the iterator might have been set up to write
20444 into a marginal area, e.g. if the line begins with some
20445 display property that writes to the margins. So we need to
20446 wait with the call to handle_line_prefix until whatever
20447 writes to the margin has done its job. */
20448 pending_handle_line_prefix = true;
20449 }
20450
20451 /* Get the initial row height. This is either the height of the
20452 text hscrolled, if there is any, or zero. */
20453 row->ascent = it->max_ascent;
20454 row->height = it->max_ascent + it->max_descent;
20455 row->phys_ascent = it->max_phys_ascent;
20456 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20457 row->extra_line_spacing = it->max_extra_line_spacing;
20458
20459 /* Utility macro to record max and min buffer positions seen until now. */
20460 #define RECORD_MAX_MIN_POS(IT) \
20461 do \
20462 { \
20463 bool composition_p \
20464 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20465 ptrdiff_t current_pos = \
20466 composition_p ? (IT)->cmp_it.charpos \
20467 : IT_CHARPOS (*(IT)); \
20468 ptrdiff_t current_bpos = \
20469 composition_p ? CHAR_TO_BYTE (current_pos) \
20470 : IT_BYTEPOS (*(IT)); \
20471 if (current_pos < min_pos) \
20472 { \
20473 min_pos = current_pos; \
20474 min_bpos = current_bpos; \
20475 } \
20476 if (IT_CHARPOS (*it) > max_pos) \
20477 { \
20478 max_pos = IT_CHARPOS (*it); \
20479 max_bpos = IT_BYTEPOS (*it); \
20480 } \
20481 } \
20482 while (false)
20483
20484 /* Loop generating characters. The loop is left with IT on the next
20485 character to display. */
20486 while (true)
20487 {
20488 int n_glyphs_before, hpos_before, x_before;
20489 int x, nglyphs;
20490 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20491
20492 /* Retrieve the next thing to display. Value is false if end of
20493 buffer reached. */
20494 if (!get_next_display_element (it))
20495 {
20496 /* Maybe add a space at the end of this line that is used to
20497 display the cursor there under X. Set the charpos of the
20498 first glyph of blank lines not corresponding to any text
20499 to -1. */
20500 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20501 row->exact_window_width_line_p = true;
20502 else if ((append_space_for_newline (it, true)
20503 && row->used[TEXT_AREA] == 1)
20504 || row->used[TEXT_AREA] == 0)
20505 {
20506 row->glyphs[TEXT_AREA]->charpos = -1;
20507 row->displays_text_p = false;
20508
20509 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20510 && (!MINI_WINDOW_P (it->w)
20511 || (minibuf_level && EQ (it->window, minibuf_window))))
20512 row->indicate_empty_line_p = true;
20513 }
20514
20515 it->continuation_lines_width = 0;
20516 row->ends_at_zv_p = true;
20517 /* A row that displays right-to-left text must always have
20518 its last face extended all the way to the end of line,
20519 even if this row ends in ZV, because we still write to
20520 the screen left to right. We also need to extend the
20521 last face if the default face is remapped to some
20522 different face, otherwise the functions that clear
20523 portions of the screen will clear with the default face's
20524 background color. */
20525 if (row->reversed_p
20526 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20527 extend_face_to_end_of_line (it);
20528 break;
20529 }
20530
20531 /* Now, get the metrics of what we want to display. This also
20532 generates glyphs in `row' (which is IT->glyph_row). */
20533 n_glyphs_before = row->used[TEXT_AREA];
20534 x = it->current_x;
20535
20536 /* Remember the line height so far in case the next element doesn't
20537 fit on the line. */
20538 if (it->line_wrap != TRUNCATE)
20539 {
20540 ascent = it->max_ascent;
20541 descent = it->max_descent;
20542 phys_ascent = it->max_phys_ascent;
20543 phys_descent = it->max_phys_descent;
20544
20545 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20546 {
20547 if (IT_DISPLAYING_WHITESPACE (it))
20548 may_wrap = true;
20549 else if (may_wrap)
20550 {
20551 SAVE_IT (wrap_it, *it, wrap_data);
20552 wrap_x = x;
20553 wrap_row_used = row->used[TEXT_AREA];
20554 wrap_row_ascent = row->ascent;
20555 wrap_row_height = row->height;
20556 wrap_row_phys_ascent = row->phys_ascent;
20557 wrap_row_phys_height = row->phys_height;
20558 wrap_row_extra_line_spacing = row->extra_line_spacing;
20559 wrap_row_min_pos = min_pos;
20560 wrap_row_min_bpos = min_bpos;
20561 wrap_row_max_pos = max_pos;
20562 wrap_row_max_bpos = max_bpos;
20563 may_wrap = false;
20564 }
20565 }
20566 }
20567
20568 PRODUCE_GLYPHS (it);
20569
20570 /* If this display element was in marginal areas, continue with
20571 the next one. */
20572 if (it->area != TEXT_AREA)
20573 {
20574 row->ascent = max (row->ascent, it->max_ascent);
20575 row->height = max (row->height, it->max_ascent + it->max_descent);
20576 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20577 row->phys_height = max (row->phys_height,
20578 it->max_phys_ascent + it->max_phys_descent);
20579 row->extra_line_spacing = max (row->extra_line_spacing,
20580 it->max_extra_line_spacing);
20581 set_iterator_to_next (it, true);
20582 /* If we didn't handle the line/wrap prefix above, and the
20583 call to set_iterator_to_next just switched to TEXT_AREA,
20584 process the prefix now. */
20585 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20586 {
20587 pending_handle_line_prefix = false;
20588 handle_line_prefix (it);
20589 }
20590 continue;
20591 }
20592
20593 /* Does the display element fit on the line? If we truncate
20594 lines, we should draw past the right edge of the window. If
20595 we don't truncate, we want to stop so that we can display the
20596 continuation glyph before the right margin. If lines are
20597 continued, there are two possible strategies for characters
20598 resulting in more than 1 glyph (e.g. tabs): Display as many
20599 glyphs as possible in this line and leave the rest for the
20600 continuation line, or display the whole element in the next
20601 line. Original redisplay did the former, so we do it also. */
20602 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20603 hpos_before = it->hpos;
20604 x_before = x;
20605
20606 if (/* Not a newline. */
20607 nglyphs > 0
20608 /* Glyphs produced fit entirely in the line. */
20609 && it->current_x < it->last_visible_x)
20610 {
20611 it->hpos += nglyphs;
20612 row->ascent = max (row->ascent, it->max_ascent);
20613 row->height = max (row->height, it->max_ascent + it->max_descent);
20614 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20615 row->phys_height = max (row->phys_height,
20616 it->max_phys_ascent + it->max_phys_descent);
20617 row->extra_line_spacing = max (row->extra_line_spacing,
20618 it->max_extra_line_spacing);
20619 if (it->current_x - it->pixel_width < it->first_visible_x
20620 /* In R2L rows, we arrange in extend_face_to_end_of_line
20621 to add a right offset to the line, by a suitable
20622 change to the stretch glyph that is the leftmost
20623 glyph of the line. */
20624 && !row->reversed_p)
20625 row->x = x - it->first_visible_x;
20626 /* Record the maximum and minimum buffer positions seen so
20627 far in glyphs that will be displayed by this row. */
20628 if (it->bidi_p)
20629 RECORD_MAX_MIN_POS (it);
20630 }
20631 else
20632 {
20633 int i, new_x;
20634 struct glyph *glyph;
20635
20636 for (i = 0; i < nglyphs; ++i, x = new_x)
20637 {
20638 /* Identify the glyphs added by the last call to
20639 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20640 the previous glyphs. */
20641 if (!row->reversed_p)
20642 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20643 else
20644 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20645 new_x = x + glyph->pixel_width;
20646
20647 if (/* Lines are continued. */
20648 it->line_wrap != TRUNCATE
20649 && (/* Glyph doesn't fit on the line. */
20650 new_x > it->last_visible_x
20651 /* Or it fits exactly on a window system frame. */
20652 || (new_x == it->last_visible_x
20653 && FRAME_WINDOW_P (it->f)
20654 && (row->reversed_p
20655 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20656 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20657 {
20658 /* End of a continued line. */
20659
20660 if (it->hpos == 0
20661 || (new_x == it->last_visible_x
20662 && FRAME_WINDOW_P (it->f)
20663 && (row->reversed_p
20664 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20665 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20666 {
20667 /* Current glyph is the only one on the line or
20668 fits exactly on the line. We must continue
20669 the line because we can't draw the cursor
20670 after the glyph. */
20671 row->continued_p = true;
20672 it->current_x = new_x;
20673 it->continuation_lines_width += new_x;
20674 ++it->hpos;
20675 if (i == nglyphs - 1)
20676 {
20677 /* If line-wrap is on, check if a previous
20678 wrap point was found. */
20679 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20680 && wrap_row_used > 0
20681 /* Even if there is a previous wrap
20682 point, continue the line here as
20683 usual, if (i) the previous character
20684 was a space or tab AND (ii) the
20685 current character is not. */
20686 && (!may_wrap
20687 || IT_DISPLAYING_WHITESPACE (it)))
20688 goto back_to_wrap;
20689
20690 /* Record the maximum and minimum buffer
20691 positions seen so far in glyphs that will be
20692 displayed by this row. */
20693 if (it->bidi_p)
20694 RECORD_MAX_MIN_POS (it);
20695 set_iterator_to_next (it, true);
20696 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20697 {
20698 if (!get_next_display_element (it))
20699 {
20700 row->exact_window_width_line_p = true;
20701 it->continuation_lines_width = 0;
20702 row->continued_p = false;
20703 row->ends_at_zv_p = true;
20704 }
20705 else if (ITERATOR_AT_END_OF_LINE_P (it))
20706 {
20707 row->continued_p = false;
20708 row->exact_window_width_line_p = true;
20709 }
20710 /* If line-wrap is on, check if a
20711 previous wrap point was found. */
20712 else if (wrap_row_used > 0
20713 /* Even if there is a previous wrap
20714 point, continue the line here as
20715 usual, if (i) the previous character
20716 was a space or tab AND (ii) the
20717 current character is not. */
20718 && (!may_wrap
20719 || IT_DISPLAYING_WHITESPACE (it)))
20720 goto back_to_wrap;
20721
20722 }
20723 }
20724 else if (it->bidi_p)
20725 RECORD_MAX_MIN_POS (it);
20726 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20727 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20728 extend_face_to_end_of_line (it);
20729 }
20730 else if (CHAR_GLYPH_PADDING_P (*glyph)
20731 && !FRAME_WINDOW_P (it->f))
20732 {
20733 /* A padding glyph that doesn't fit on this line.
20734 This means the whole character doesn't fit
20735 on the line. */
20736 if (row->reversed_p)
20737 unproduce_glyphs (it, row->used[TEXT_AREA]
20738 - n_glyphs_before);
20739 row->used[TEXT_AREA] = n_glyphs_before;
20740
20741 /* Fill the rest of the row with continuation
20742 glyphs like in 20.x. */
20743 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20744 < row->glyphs[1 + TEXT_AREA])
20745 produce_special_glyphs (it, IT_CONTINUATION);
20746
20747 row->continued_p = true;
20748 it->current_x = x_before;
20749 it->continuation_lines_width += x_before;
20750
20751 /* Restore the height to what it was before the
20752 element not fitting on the line. */
20753 it->max_ascent = ascent;
20754 it->max_descent = descent;
20755 it->max_phys_ascent = phys_ascent;
20756 it->max_phys_descent = phys_descent;
20757 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20758 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20759 extend_face_to_end_of_line (it);
20760 }
20761 else if (wrap_row_used > 0)
20762 {
20763 back_to_wrap:
20764 if (row->reversed_p)
20765 unproduce_glyphs (it,
20766 row->used[TEXT_AREA] - wrap_row_used);
20767 RESTORE_IT (it, &wrap_it, wrap_data);
20768 it->continuation_lines_width += wrap_x;
20769 row->used[TEXT_AREA] = wrap_row_used;
20770 row->ascent = wrap_row_ascent;
20771 row->height = wrap_row_height;
20772 row->phys_ascent = wrap_row_phys_ascent;
20773 row->phys_height = wrap_row_phys_height;
20774 row->extra_line_spacing = wrap_row_extra_line_spacing;
20775 min_pos = wrap_row_min_pos;
20776 min_bpos = wrap_row_min_bpos;
20777 max_pos = wrap_row_max_pos;
20778 max_bpos = wrap_row_max_bpos;
20779 row->continued_p = true;
20780 row->ends_at_zv_p = false;
20781 row->exact_window_width_line_p = false;
20782 it->continuation_lines_width += x;
20783
20784 /* Make sure that a non-default face is extended
20785 up to the right margin of the window. */
20786 extend_face_to_end_of_line (it);
20787 }
20788 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20789 {
20790 /* A TAB that extends past the right edge of the
20791 window. This produces a single glyph on
20792 window system frames. We leave the glyph in
20793 this row and let it fill the row, but don't
20794 consume the TAB. */
20795 if ((row->reversed_p
20796 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20797 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20798 produce_special_glyphs (it, IT_CONTINUATION);
20799 it->continuation_lines_width += it->last_visible_x;
20800 row->ends_in_middle_of_char_p = true;
20801 row->continued_p = true;
20802 glyph->pixel_width = it->last_visible_x - x;
20803 it->starts_in_middle_of_char_p = true;
20804 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20805 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20806 extend_face_to_end_of_line (it);
20807 }
20808 else
20809 {
20810 /* Something other than a TAB that draws past
20811 the right edge of the window. Restore
20812 positions to values before the element. */
20813 if (row->reversed_p)
20814 unproduce_glyphs (it, row->used[TEXT_AREA]
20815 - (n_glyphs_before + i));
20816 row->used[TEXT_AREA] = n_glyphs_before + i;
20817
20818 /* Display continuation glyphs. */
20819 it->current_x = x_before;
20820 it->continuation_lines_width += x;
20821 if (!FRAME_WINDOW_P (it->f)
20822 || (row->reversed_p
20823 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20824 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20825 produce_special_glyphs (it, IT_CONTINUATION);
20826 row->continued_p = true;
20827
20828 extend_face_to_end_of_line (it);
20829
20830 if (nglyphs > 1 && i > 0)
20831 {
20832 row->ends_in_middle_of_char_p = true;
20833 it->starts_in_middle_of_char_p = true;
20834 }
20835
20836 /* Restore the height to what it was before the
20837 element not fitting on the line. */
20838 it->max_ascent = ascent;
20839 it->max_descent = descent;
20840 it->max_phys_ascent = phys_ascent;
20841 it->max_phys_descent = phys_descent;
20842 }
20843
20844 break;
20845 }
20846 else if (new_x > it->first_visible_x)
20847 {
20848 /* Increment number of glyphs actually displayed. */
20849 ++it->hpos;
20850
20851 /* Record the maximum and minimum buffer positions
20852 seen so far in glyphs that will be displayed by
20853 this row. */
20854 if (it->bidi_p)
20855 RECORD_MAX_MIN_POS (it);
20856
20857 if (x < it->first_visible_x && !row->reversed_p)
20858 /* Glyph is partially visible, i.e. row starts at
20859 negative X position. Don't do that in R2L
20860 rows, where we arrange to add a right offset to
20861 the line in extend_face_to_end_of_line, by a
20862 suitable change to the stretch glyph that is
20863 the leftmost glyph of the line. */
20864 row->x = x - it->first_visible_x;
20865 /* When the last glyph of an R2L row only fits
20866 partially on the line, we need to set row->x to a
20867 negative offset, so that the leftmost glyph is
20868 the one that is partially visible. But if we are
20869 going to produce the truncation glyph, this will
20870 be taken care of in produce_special_glyphs. */
20871 if (row->reversed_p
20872 && new_x > it->last_visible_x
20873 && !(it->line_wrap == TRUNCATE
20874 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20875 {
20876 eassert (FRAME_WINDOW_P (it->f));
20877 row->x = it->last_visible_x - new_x;
20878 }
20879 }
20880 else
20881 {
20882 /* Glyph is completely off the left margin of the
20883 window. This should not happen because of the
20884 move_it_in_display_line at the start of this
20885 function, unless the text display area of the
20886 window is empty. */
20887 eassert (it->first_visible_x <= it->last_visible_x);
20888 }
20889 }
20890 /* Even if this display element produced no glyphs at all,
20891 we want to record its position. */
20892 if (it->bidi_p && nglyphs == 0)
20893 RECORD_MAX_MIN_POS (it);
20894
20895 row->ascent = max (row->ascent, it->max_ascent);
20896 row->height = max (row->height, it->max_ascent + it->max_descent);
20897 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20898 row->phys_height = max (row->phys_height,
20899 it->max_phys_ascent + it->max_phys_descent);
20900 row->extra_line_spacing = max (row->extra_line_spacing,
20901 it->max_extra_line_spacing);
20902
20903 /* End of this display line if row is continued. */
20904 if (row->continued_p || row->ends_at_zv_p)
20905 break;
20906 }
20907
20908 at_end_of_line:
20909 /* Is this a line end? If yes, we're also done, after making
20910 sure that a non-default face is extended up to the right
20911 margin of the window. */
20912 if (ITERATOR_AT_END_OF_LINE_P (it))
20913 {
20914 int used_before = row->used[TEXT_AREA];
20915
20916 row->ends_in_newline_from_string_p = STRINGP (it->object);
20917
20918 /* Add a space at the end of the line that is used to
20919 display the cursor there. */
20920 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20921 append_space_for_newline (it, false);
20922
20923 /* Extend the face to the end of the line. */
20924 extend_face_to_end_of_line (it);
20925
20926 /* Make sure we have the position. */
20927 if (used_before == 0)
20928 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20929
20930 /* Record the position of the newline, for use in
20931 find_row_edges. */
20932 it->eol_pos = it->current.pos;
20933
20934 /* Consume the line end. This skips over invisible lines. */
20935 set_iterator_to_next (it, true);
20936 it->continuation_lines_width = 0;
20937 break;
20938 }
20939
20940 /* Proceed with next display element. Note that this skips
20941 over lines invisible because of selective display. */
20942 set_iterator_to_next (it, true);
20943
20944 /* If we truncate lines, we are done when the last displayed
20945 glyphs reach past the right margin of the window. */
20946 if (it->line_wrap == TRUNCATE
20947 && ((FRAME_WINDOW_P (it->f)
20948 /* Images are preprocessed in produce_image_glyph such
20949 that they are cropped at the right edge of the
20950 window, so an image glyph will always end exactly at
20951 last_visible_x, even if there's no right fringe. */
20952 && ((row->reversed_p
20953 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20954 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20955 || it->what == IT_IMAGE))
20956 ? (it->current_x >= it->last_visible_x)
20957 : (it->current_x > it->last_visible_x)))
20958 {
20959 /* Maybe add truncation glyphs. */
20960 if (!FRAME_WINDOW_P (it->f)
20961 || (row->reversed_p
20962 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20963 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20964 {
20965 int i, n;
20966
20967 if (!row->reversed_p)
20968 {
20969 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20970 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20971 break;
20972 }
20973 else
20974 {
20975 for (i = 0; i < row->used[TEXT_AREA]; i++)
20976 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20977 break;
20978 /* Remove any padding glyphs at the front of ROW, to
20979 make room for the truncation glyphs we will be
20980 adding below. The loop below always inserts at
20981 least one truncation glyph, so also remove the
20982 last glyph added to ROW. */
20983 unproduce_glyphs (it, i + 1);
20984 /* Adjust i for the loop below. */
20985 i = row->used[TEXT_AREA] - (i + 1);
20986 }
20987
20988 /* produce_special_glyphs overwrites the last glyph, so
20989 we don't want that if we want to keep that last
20990 glyph, which means it's an image. */
20991 if (it->current_x > it->last_visible_x)
20992 {
20993 it->current_x = x_before;
20994 if (!FRAME_WINDOW_P (it->f))
20995 {
20996 for (n = row->used[TEXT_AREA]; i < n; ++i)
20997 {
20998 row->used[TEXT_AREA] = i;
20999 produce_special_glyphs (it, IT_TRUNCATION);
21000 }
21001 }
21002 else
21003 {
21004 row->used[TEXT_AREA] = i;
21005 produce_special_glyphs (it, IT_TRUNCATION);
21006 }
21007 it->hpos = hpos_before;
21008 }
21009 }
21010 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21011 {
21012 /* Don't truncate if we can overflow newline into fringe. */
21013 if (!get_next_display_element (it))
21014 {
21015 it->continuation_lines_width = 0;
21016 row->ends_at_zv_p = true;
21017 row->exact_window_width_line_p = true;
21018 break;
21019 }
21020 if (ITERATOR_AT_END_OF_LINE_P (it))
21021 {
21022 row->exact_window_width_line_p = true;
21023 goto at_end_of_line;
21024 }
21025 it->current_x = x_before;
21026 it->hpos = hpos_before;
21027 }
21028
21029 row->truncated_on_right_p = true;
21030 it->continuation_lines_width = 0;
21031 reseat_at_next_visible_line_start (it, false);
21032 /* We insist below that IT's position be at ZV because in
21033 bidi-reordered lines the character at visible line start
21034 might not be the character that follows the newline in
21035 the logical order. */
21036 if (IT_BYTEPOS (*it) > BEG_BYTE)
21037 row->ends_at_zv_p =
21038 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21039 else
21040 row->ends_at_zv_p = false;
21041 break;
21042 }
21043 }
21044
21045 if (wrap_data)
21046 bidi_unshelve_cache (wrap_data, true);
21047
21048 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21049 at the left window margin. */
21050 if (it->first_visible_x
21051 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21052 {
21053 if (!FRAME_WINDOW_P (it->f)
21054 || (((row->reversed_p
21055 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21056 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21057 /* Don't let insert_left_trunc_glyphs overwrite the
21058 first glyph of the row if it is an image. */
21059 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21060 insert_left_trunc_glyphs (it);
21061 row->truncated_on_left_p = true;
21062 }
21063
21064 /* Remember the position at which this line ends.
21065
21066 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21067 cannot be before the call to find_row_edges below, since that is
21068 where these positions are determined. */
21069 row->end = it->current;
21070 if (!it->bidi_p)
21071 {
21072 row->minpos = row->start.pos;
21073 row->maxpos = row->end.pos;
21074 }
21075 else
21076 {
21077 /* ROW->minpos and ROW->maxpos must be the smallest and
21078 `1 + the largest' buffer positions in ROW. But if ROW was
21079 bidi-reordered, these two positions can be anywhere in the
21080 row, so we must determine them now. */
21081 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21082 }
21083
21084 /* If the start of this line is the overlay arrow-position, then
21085 mark this glyph row as the one containing the overlay arrow.
21086 This is clearly a mess with variable size fonts. It would be
21087 better to let it be displayed like cursors under X. */
21088 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21089 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21090 !NILP (overlay_arrow_string)))
21091 {
21092 /* Overlay arrow in window redisplay is a fringe bitmap. */
21093 if (STRINGP (overlay_arrow_string))
21094 {
21095 struct glyph_row *arrow_row
21096 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21097 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21098 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21099 struct glyph *p = row->glyphs[TEXT_AREA];
21100 struct glyph *p2, *end;
21101
21102 /* Copy the arrow glyphs. */
21103 while (glyph < arrow_end)
21104 *p++ = *glyph++;
21105
21106 /* Throw away padding glyphs. */
21107 p2 = p;
21108 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21109 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21110 ++p2;
21111 if (p2 > p)
21112 {
21113 while (p2 < end)
21114 *p++ = *p2++;
21115 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21116 }
21117 }
21118 else
21119 {
21120 eassert (INTEGERP (overlay_arrow_string));
21121 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21122 }
21123 overlay_arrow_seen = true;
21124 }
21125
21126 /* Highlight trailing whitespace. */
21127 if (!NILP (Vshow_trailing_whitespace))
21128 highlight_trailing_whitespace (it->f, it->glyph_row);
21129
21130 /* Compute pixel dimensions of this line. */
21131 compute_line_metrics (it);
21132
21133 /* Implementation note: No changes in the glyphs of ROW or in their
21134 faces can be done past this point, because compute_line_metrics
21135 computes ROW's hash value and stores it within the glyph_row
21136 structure. */
21137
21138 /* Record whether this row ends inside an ellipsis. */
21139 row->ends_in_ellipsis_p
21140 = (it->method == GET_FROM_DISPLAY_VECTOR
21141 && it->ellipsis_p);
21142
21143 /* Save fringe bitmaps in this row. */
21144 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21145 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21146 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21147 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21148
21149 it->left_user_fringe_bitmap = 0;
21150 it->left_user_fringe_face_id = 0;
21151 it->right_user_fringe_bitmap = 0;
21152 it->right_user_fringe_face_id = 0;
21153
21154 /* Maybe set the cursor. */
21155 cvpos = it->w->cursor.vpos;
21156 if ((cvpos < 0
21157 /* In bidi-reordered rows, keep checking for proper cursor
21158 position even if one has been found already, because buffer
21159 positions in such rows change non-linearly with ROW->VPOS,
21160 when a line is continued. One exception: when we are at ZV,
21161 display cursor on the first suitable glyph row, since all
21162 the empty rows after that also have their position set to ZV. */
21163 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21164 lines' rows is implemented for bidi-reordered rows. */
21165 || (it->bidi_p
21166 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21167 && PT >= MATRIX_ROW_START_CHARPOS (row)
21168 && PT <= MATRIX_ROW_END_CHARPOS (row)
21169 && cursor_row_p (row))
21170 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21171
21172 /* Prepare for the next line. This line starts horizontally at (X
21173 HPOS) = (0 0). Vertical positions are incremented. As a
21174 convenience for the caller, IT->glyph_row is set to the next
21175 row to be used. */
21176 it->current_x = it->hpos = 0;
21177 it->current_y += row->height;
21178 SET_TEXT_POS (it->eol_pos, 0, 0);
21179 ++it->vpos;
21180 ++it->glyph_row;
21181 /* The next row should by default use the same value of the
21182 reversed_p flag as this one. set_iterator_to_next decides when
21183 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21184 the flag accordingly. */
21185 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21186 it->glyph_row->reversed_p = row->reversed_p;
21187 it->start = row->end;
21188 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21189
21190 #undef RECORD_MAX_MIN_POS
21191 }
21192
21193 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21194 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21195 doc: /* Return paragraph direction at point in BUFFER.
21196 Value is either `left-to-right' or `right-to-left'.
21197 If BUFFER is omitted or nil, it defaults to the current buffer.
21198
21199 Paragraph direction determines how the text in the paragraph is displayed.
21200 In left-to-right paragraphs, text begins at the left margin of the window
21201 and the reading direction is generally left to right. In right-to-left
21202 paragraphs, text begins at the right margin and is read from right to left.
21203
21204 See also `bidi-paragraph-direction'. */)
21205 (Lisp_Object buffer)
21206 {
21207 struct buffer *buf = current_buffer;
21208 struct buffer *old = buf;
21209
21210 if (! NILP (buffer))
21211 {
21212 CHECK_BUFFER (buffer);
21213 buf = XBUFFER (buffer);
21214 }
21215
21216 if (NILP (BVAR (buf, bidi_display_reordering))
21217 || NILP (BVAR (buf, enable_multibyte_characters))
21218 /* When we are loading loadup.el, the character property tables
21219 needed for bidi iteration are not yet available. */
21220 || !NILP (Vpurify_flag))
21221 return Qleft_to_right;
21222 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21223 return BVAR (buf, bidi_paragraph_direction);
21224 else
21225 {
21226 /* Determine the direction from buffer text. We could try to
21227 use current_matrix if it is up to date, but this seems fast
21228 enough as it is. */
21229 struct bidi_it itb;
21230 ptrdiff_t pos = BUF_PT (buf);
21231 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21232 int c;
21233 void *itb_data = bidi_shelve_cache ();
21234
21235 set_buffer_temp (buf);
21236 /* bidi_paragraph_init finds the base direction of the paragraph
21237 by searching forward from paragraph start. We need the base
21238 direction of the current or _previous_ paragraph, so we need
21239 to make sure we are within that paragraph. To that end, find
21240 the previous non-empty line. */
21241 if (pos >= ZV && pos > BEGV)
21242 DEC_BOTH (pos, bytepos);
21243 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21244 if (fast_looking_at (trailing_white_space,
21245 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21246 {
21247 while ((c = FETCH_BYTE (bytepos)) == '\n'
21248 || c == ' ' || c == '\t' || c == '\f')
21249 {
21250 if (bytepos <= BEGV_BYTE)
21251 break;
21252 bytepos--;
21253 pos--;
21254 }
21255 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21256 bytepos--;
21257 }
21258 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21259 itb.paragraph_dir = NEUTRAL_DIR;
21260 itb.string.s = NULL;
21261 itb.string.lstring = Qnil;
21262 itb.string.bufpos = 0;
21263 itb.string.from_disp_str = false;
21264 itb.string.unibyte = false;
21265 /* We have no window to use here for ignoring window-specific
21266 overlays. Using NULL for window pointer will cause
21267 compute_display_string_pos to use the current buffer. */
21268 itb.w = NULL;
21269 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21270 bidi_unshelve_cache (itb_data, false);
21271 set_buffer_temp (old);
21272 switch (itb.paragraph_dir)
21273 {
21274 case L2R:
21275 return Qleft_to_right;
21276 break;
21277 case R2L:
21278 return Qright_to_left;
21279 break;
21280 default:
21281 emacs_abort ();
21282 }
21283 }
21284 }
21285
21286 DEFUN ("bidi-find-overridden-directionality",
21287 Fbidi_find_overridden_directionality,
21288 Sbidi_find_overridden_directionality, 2, 3, 0,
21289 doc: /* Return position between FROM and TO where directionality was overridden.
21290
21291 This function returns the first character position in the specified
21292 region of OBJECT where there is a character whose `bidi-class' property
21293 is `L', but which was forced to display as `R' by a directional
21294 override, and likewise with characters whose `bidi-class' is `R'
21295 or `AL' that were forced to display as `L'.
21296
21297 If no such character is found, the function returns nil.
21298
21299 OBJECT is a Lisp string or buffer to search for overridden
21300 directionality, and defaults to the current buffer if nil or omitted.
21301 OBJECT can also be a window, in which case the function will search
21302 the buffer displayed in that window. Passing the window instead of
21303 a buffer is preferable when the buffer is displayed in some window,
21304 because this function will then be able to correctly account for
21305 window-specific overlays, which can affect the results.
21306
21307 Strong directional characters `L', `R', and `AL' can have their
21308 intrinsic directionality overridden by directional override
21309 control characters RLO (u+202e) and LRO (u+202d). See the
21310 function `get-char-code-property' for a way to inquire about
21311 the `bidi-class' property of a character. */)
21312 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21313 {
21314 struct buffer *buf = current_buffer;
21315 struct buffer *old = buf;
21316 struct window *w = NULL;
21317 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21318 struct bidi_it itb;
21319 ptrdiff_t from_pos, to_pos, from_bpos;
21320 void *itb_data;
21321
21322 if (!NILP (object))
21323 {
21324 if (BUFFERP (object))
21325 buf = XBUFFER (object);
21326 else if (WINDOWP (object))
21327 {
21328 w = decode_live_window (object);
21329 buf = XBUFFER (w->contents);
21330 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21331 }
21332 else
21333 CHECK_STRING (object);
21334 }
21335
21336 if (STRINGP (object))
21337 {
21338 /* Characters in unibyte strings are always treated by bidi.c as
21339 strong LTR. */
21340 if (!STRING_MULTIBYTE (object)
21341 /* When we are loading loadup.el, the character property
21342 tables needed for bidi iteration are not yet
21343 available. */
21344 || !NILP (Vpurify_flag))
21345 return Qnil;
21346
21347 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21348 if (from_pos >= SCHARS (object))
21349 return Qnil;
21350
21351 /* Set up the bidi iterator. */
21352 itb_data = bidi_shelve_cache ();
21353 itb.paragraph_dir = NEUTRAL_DIR;
21354 itb.string.lstring = object;
21355 itb.string.s = NULL;
21356 itb.string.schars = SCHARS (object);
21357 itb.string.bufpos = 0;
21358 itb.string.from_disp_str = false;
21359 itb.string.unibyte = false;
21360 itb.w = w;
21361 bidi_init_it (0, 0, frame_window_p, &itb);
21362 }
21363 else
21364 {
21365 /* Nothing this fancy can happen in unibyte buffers, or in a
21366 buffer that disabled reordering, or if FROM is at EOB. */
21367 if (NILP (BVAR (buf, bidi_display_reordering))
21368 || NILP (BVAR (buf, enable_multibyte_characters))
21369 /* When we are loading loadup.el, the character property
21370 tables needed for bidi iteration are not yet
21371 available. */
21372 || !NILP (Vpurify_flag))
21373 return Qnil;
21374
21375 set_buffer_temp (buf);
21376 validate_region (&from, &to);
21377 from_pos = XINT (from);
21378 to_pos = XINT (to);
21379 if (from_pos >= ZV)
21380 return Qnil;
21381
21382 /* Set up the bidi iterator. */
21383 itb_data = bidi_shelve_cache ();
21384 from_bpos = CHAR_TO_BYTE (from_pos);
21385 if (from_pos == BEGV)
21386 {
21387 itb.charpos = BEGV;
21388 itb.bytepos = BEGV_BYTE;
21389 }
21390 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21391 {
21392 itb.charpos = from_pos;
21393 itb.bytepos = from_bpos;
21394 }
21395 else
21396 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21397 -1, &itb.bytepos);
21398 itb.paragraph_dir = NEUTRAL_DIR;
21399 itb.string.s = NULL;
21400 itb.string.lstring = Qnil;
21401 itb.string.bufpos = 0;
21402 itb.string.from_disp_str = false;
21403 itb.string.unibyte = false;
21404 itb.w = w;
21405 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21406 }
21407
21408 ptrdiff_t found;
21409 do {
21410 /* For the purposes of this function, the actual base direction of
21411 the paragraph doesn't matter, so just set it to L2R. */
21412 bidi_paragraph_init (L2R, &itb, false);
21413 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21414 ;
21415 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21416
21417 bidi_unshelve_cache (itb_data, false);
21418 set_buffer_temp (old);
21419
21420 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21421 }
21422
21423 DEFUN ("move-point-visually", Fmove_point_visually,
21424 Smove_point_visually, 1, 1, 0,
21425 doc: /* Move point in the visual order in the specified DIRECTION.
21426 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21427 left.
21428
21429 Value is the new character position of point. */)
21430 (Lisp_Object direction)
21431 {
21432 struct window *w = XWINDOW (selected_window);
21433 struct buffer *b = XBUFFER (w->contents);
21434 struct glyph_row *row;
21435 int dir;
21436 Lisp_Object paragraph_dir;
21437
21438 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21439 (!(ROW)->continued_p \
21440 && NILP ((GLYPH)->object) \
21441 && (GLYPH)->type == CHAR_GLYPH \
21442 && (GLYPH)->u.ch == ' ' \
21443 && (GLYPH)->charpos >= 0 \
21444 && !(GLYPH)->avoid_cursor_p)
21445
21446 CHECK_NUMBER (direction);
21447 dir = XINT (direction);
21448 if (dir > 0)
21449 dir = 1;
21450 else
21451 dir = -1;
21452
21453 /* If current matrix is up-to-date, we can use the information
21454 recorded in the glyphs, at least as long as the goal is on the
21455 screen. */
21456 if (w->window_end_valid
21457 && !windows_or_buffers_changed
21458 && b
21459 && !b->clip_changed
21460 && !b->prevent_redisplay_optimizations_p
21461 && !window_outdated (w)
21462 /* We rely below on the cursor coordinates to be up to date, but
21463 we cannot trust them if some command moved point since the
21464 last complete redisplay. */
21465 && w->last_point == BUF_PT (b)
21466 && w->cursor.vpos >= 0
21467 && w->cursor.vpos < w->current_matrix->nrows
21468 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21469 {
21470 struct glyph *g = row->glyphs[TEXT_AREA];
21471 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21472 struct glyph *gpt = g + w->cursor.hpos;
21473
21474 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21475 {
21476 if (BUFFERP (g->object) && g->charpos != PT)
21477 {
21478 SET_PT (g->charpos);
21479 w->cursor.vpos = -1;
21480 return make_number (PT);
21481 }
21482 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21483 {
21484 ptrdiff_t new_pos;
21485
21486 if (BUFFERP (gpt->object))
21487 {
21488 new_pos = PT;
21489 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21490 new_pos += (row->reversed_p ? -dir : dir);
21491 else
21492 new_pos -= (row->reversed_p ? -dir : dir);
21493 }
21494 else if (BUFFERP (g->object))
21495 new_pos = g->charpos;
21496 else
21497 break;
21498 SET_PT (new_pos);
21499 w->cursor.vpos = -1;
21500 return make_number (PT);
21501 }
21502 else if (ROW_GLYPH_NEWLINE_P (row, g))
21503 {
21504 /* Glyphs inserted at the end of a non-empty line for
21505 positioning the cursor have zero charpos, so we must
21506 deduce the value of point by other means. */
21507 if (g->charpos > 0)
21508 SET_PT (g->charpos);
21509 else if (row->ends_at_zv_p && PT != ZV)
21510 SET_PT (ZV);
21511 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21512 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21513 else
21514 break;
21515 w->cursor.vpos = -1;
21516 return make_number (PT);
21517 }
21518 }
21519 if (g == e || NILP (g->object))
21520 {
21521 if (row->truncated_on_left_p || row->truncated_on_right_p)
21522 goto simulate_display;
21523 if (!row->reversed_p)
21524 row += dir;
21525 else
21526 row -= dir;
21527 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21528 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21529 goto simulate_display;
21530
21531 if (dir > 0)
21532 {
21533 if (row->reversed_p && !row->continued_p)
21534 {
21535 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21536 w->cursor.vpos = -1;
21537 return make_number (PT);
21538 }
21539 g = row->glyphs[TEXT_AREA];
21540 e = g + row->used[TEXT_AREA];
21541 for ( ; g < e; g++)
21542 {
21543 if (BUFFERP (g->object)
21544 /* Empty lines have only one glyph, which stands
21545 for the newline, and whose charpos is the
21546 buffer position of the newline. */
21547 || ROW_GLYPH_NEWLINE_P (row, g)
21548 /* When the buffer ends in a newline, the line at
21549 EOB also has one glyph, but its charpos is -1. */
21550 || (row->ends_at_zv_p
21551 && !row->reversed_p
21552 && NILP (g->object)
21553 && g->type == CHAR_GLYPH
21554 && g->u.ch == ' '))
21555 {
21556 if (g->charpos > 0)
21557 SET_PT (g->charpos);
21558 else if (!row->reversed_p
21559 && row->ends_at_zv_p
21560 && PT != ZV)
21561 SET_PT (ZV);
21562 else
21563 continue;
21564 w->cursor.vpos = -1;
21565 return make_number (PT);
21566 }
21567 }
21568 }
21569 else
21570 {
21571 if (!row->reversed_p && !row->continued_p)
21572 {
21573 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21574 w->cursor.vpos = -1;
21575 return make_number (PT);
21576 }
21577 e = row->glyphs[TEXT_AREA];
21578 g = e + row->used[TEXT_AREA] - 1;
21579 for ( ; g >= e; g--)
21580 {
21581 if (BUFFERP (g->object)
21582 || (ROW_GLYPH_NEWLINE_P (row, g)
21583 && g->charpos > 0)
21584 /* Empty R2L lines on GUI frames have the buffer
21585 position of the newline stored in the stretch
21586 glyph. */
21587 || g->type == STRETCH_GLYPH
21588 || (row->ends_at_zv_p
21589 && row->reversed_p
21590 && NILP (g->object)
21591 && g->type == CHAR_GLYPH
21592 && g->u.ch == ' '))
21593 {
21594 if (g->charpos > 0)
21595 SET_PT (g->charpos);
21596 else if (row->reversed_p
21597 && row->ends_at_zv_p
21598 && PT != ZV)
21599 SET_PT (ZV);
21600 else
21601 continue;
21602 w->cursor.vpos = -1;
21603 return make_number (PT);
21604 }
21605 }
21606 }
21607 }
21608 }
21609
21610 simulate_display:
21611
21612 /* If we wind up here, we failed to move by using the glyphs, so we
21613 need to simulate display instead. */
21614
21615 if (b)
21616 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21617 else
21618 paragraph_dir = Qleft_to_right;
21619 if (EQ (paragraph_dir, Qright_to_left))
21620 dir = -dir;
21621 if (PT <= BEGV && dir < 0)
21622 xsignal0 (Qbeginning_of_buffer);
21623 else if (PT >= ZV && dir > 0)
21624 xsignal0 (Qend_of_buffer);
21625 else
21626 {
21627 struct text_pos pt;
21628 struct it it;
21629 int pt_x, target_x, pixel_width, pt_vpos;
21630 bool at_eol_p;
21631 bool overshoot_expected = false;
21632 bool target_is_eol_p = false;
21633
21634 /* Setup the arena. */
21635 SET_TEXT_POS (pt, PT, PT_BYTE);
21636 start_display (&it, w, pt);
21637 /* When lines are truncated, we could be called with point
21638 outside of the windows edges, in which case move_it_*
21639 functions either prematurely stop at window's edge or jump to
21640 the next screen line, whereas we rely below on our ability to
21641 reach point, in order to start from its X coordinate. So we
21642 need to disregard the window's horizontal extent in that case. */
21643 if (it.line_wrap == TRUNCATE)
21644 it.last_visible_x = INFINITY;
21645
21646 if (it.cmp_it.id < 0
21647 && it.method == GET_FROM_STRING
21648 && it.area == TEXT_AREA
21649 && it.string_from_display_prop_p
21650 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21651 overshoot_expected = true;
21652
21653 /* Find the X coordinate of point. We start from the beginning
21654 of this or previous line to make sure we are before point in
21655 the logical order (since the move_it_* functions can only
21656 move forward). */
21657 reseat:
21658 reseat_at_previous_visible_line_start (&it);
21659 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21660 if (IT_CHARPOS (it) != PT)
21661 {
21662 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21663 -1, -1, -1, MOVE_TO_POS);
21664 /* If we missed point because the character there is
21665 displayed out of a display vector that has more than one
21666 glyph, retry expecting overshoot. */
21667 if (it.method == GET_FROM_DISPLAY_VECTOR
21668 && it.current.dpvec_index > 0
21669 && !overshoot_expected)
21670 {
21671 overshoot_expected = true;
21672 goto reseat;
21673 }
21674 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21675 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21676 }
21677 pt_x = it.current_x;
21678 pt_vpos = it.vpos;
21679 if (dir > 0 || overshoot_expected)
21680 {
21681 struct glyph_row *row = it.glyph_row;
21682
21683 /* When point is at beginning of line, we don't have
21684 information about the glyph there loaded into struct
21685 it. Calling get_next_display_element fixes that. */
21686 if (pt_x == 0)
21687 get_next_display_element (&it);
21688 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21689 it.glyph_row = NULL;
21690 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21691 it.glyph_row = row;
21692 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21693 it, lest it will become out of sync with it's buffer
21694 position. */
21695 it.current_x = pt_x;
21696 }
21697 else
21698 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21699 pixel_width = it.pixel_width;
21700 if (overshoot_expected && at_eol_p)
21701 pixel_width = 0;
21702 else if (pixel_width <= 0)
21703 pixel_width = 1;
21704
21705 /* If there's a display string (or something similar) at point,
21706 we are actually at the glyph to the left of point, so we need
21707 to correct the X coordinate. */
21708 if (overshoot_expected)
21709 {
21710 if (it.bidi_p)
21711 pt_x += pixel_width * it.bidi_it.scan_dir;
21712 else
21713 pt_x += pixel_width;
21714 }
21715
21716 /* Compute target X coordinate, either to the left or to the
21717 right of point. On TTY frames, all characters have the same
21718 pixel width of 1, so we can use that. On GUI frames we don't
21719 have an easy way of getting at the pixel width of the
21720 character to the left of point, so we use a different method
21721 of getting to that place. */
21722 if (dir > 0)
21723 target_x = pt_x + pixel_width;
21724 else
21725 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21726
21727 /* Target X coordinate could be one line above or below the line
21728 of point, in which case we need to adjust the target X
21729 coordinate. Also, if moving to the left, we need to begin at
21730 the left edge of the point's screen line. */
21731 if (dir < 0)
21732 {
21733 if (pt_x > 0)
21734 {
21735 start_display (&it, w, pt);
21736 if (it.line_wrap == TRUNCATE)
21737 it.last_visible_x = INFINITY;
21738 reseat_at_previous_visible_line_start (&it);
21739 it.current_x = it.current_y = it.hpos = 0;
21740 if (pt_vpos != 0)
21741 move_it_by_lines (&it, pt_vpos);
21742 }
21743 else
21744 {
21745 move_it_by_lines (&it, -1);
21746 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21747 target_is_eol_p = true;
21748 /* Under word-wrap, we don't know the x coordinate of
21749 the last character displayed on the previous line,
21750 which immediately precedes the wrap point. To find
21751 out its x coordinate, we try moving to the right
21752 margin of the window, which will stop at the wrap
21753 point, and then reset target_x to point at the
21754 character that precedes the wrap point. This is not
21755 needed on GUI frames, because (see below) there we
21756 move from the left margin one grapheme cluster at a
21757 time, and stop when we hit the wrap point. */
21758 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21759 {
21760 void *it_data = NULL;
21761 struct it it2;
21762
21763 SAVE_IT (it2, it, it_data);
21764 move_it_in_display_line_to (&it, ZV, target_x,
21765 MOVE_TO_POS | MOVE_TO_X);
21766 /* If we arrived at target_x, that _is_ the last
21767 character on the previous line. */
21768 if (it.current_x != target_x)
21769 target_x = it.current_x - 1;
21770 RESTORE_IT (&it, &it2, it_data);
21771 }
21772 }
21773 }
21774 else
21775 {
21776 if (at_eol_p
21777 || (target_x >= it.last_visible_x
21778 && it.line_wrap != TRUNCATE))
21779 {
21780 if (pt_x > 0)
21781 move_it_by_lines (&it, 0);
21782 move_it_by_lines (&it, 1);
21783 target_x = 0;
21784 }
21785 }
21786
21787 /* Move to the target X coordinate. */
21788 #ifdef HAVE_WINDOW_SYSTEM
21789 /* On GUI frames, as we don't know the X coordinate of the
21790 character to the left of point, moving point to the left
21791 requires walking, one grapheme cluster at a time, until we
21792 find ourself at a place immediately to the left of the
21793 character at point. */
21794 if (FRAME_WINDOW_P (it.f) && dir < 0)
21795 {
21796 struct text_pos new_pos;
21797 enum move_it_result rc = MOVE_X_REACHED;
21798
21799 if (it.current_x == 0)
21800 get_next_display_element (&it);
21801 if (it.what == IT_COMPOSITION)
21802 {
21803 new_pos.charpos = it.cmp_it.charpos;
21804 new_pos.bytepos = -1;
21805 }
21806 else
21807 new_pos = it.current.pos;
21808
21809 while (it.current_x + it.pixel_width <= target_x
21810 && (rc == MOVE_X_REACHED
21811 /* Under word-wrap, move_it_in_display_line_to
21812 stops at correct coordinates, but sometimes
21813 returns MOVE_POS_MATCH_OR_ZV. */
21814 || (it.line_wrap == WORD_WRAP
21815 && rc == MOVE_POS_MATCH_OR_ZV)))
21816 {
21817 int new_x = it.current_x + it.pixel_width;
21818
21819 /* For composed characters, we want the position of the
21820 first character in the grapheme cluster (usually, the
21821 composition's base character), whereas it.current
21822 might give us the position of the _last_ one, e.g. if
21823 the composition is rendered in reverse due to bidi
21824 reordering. */
21825 if (it.what == IT_COMPOSITION)
21826 {
21827 new_pos.charpos = it.cmp_it.charpos;
21828 new_pos.bytepos = -1;
21829 }
21830 else
21831 new_pos = it.current.pos;
21832 if (new_x == it.current_x)
21833 new_x++;
21834 rc = move_it_in_display_line_to (&it, ZV, new_x,
21835 MOVE_TO_POS | MOVE_TO_X);
21836 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21837 break;
21838 }
21839 /* The previous position we saw in the loop is the one we
21840 want. */
21841 if (new_pos.bytepos == -1)
21842 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21843 it.current.pos = new_pos;
21844 }
21845 else
21846 #endif
21847 if (it.current_x != target_x)
21848 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21849
21850 /* If we ended up in a display string that covers point, move to
21851 buffer position to the right in the visual order. */
21852 if (dir > 0)
21853 {
21854 while (IT_CHARPOS (it) == PT)
21855 {
21856 set_iterator_to_next (&it, false);
21857 if (!get_next_display_element (&it))
21858 break;
21859 }
21860 }
21861
21862 /* Move point to that position. */
21863 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21864 }
21865
21866 return make_number (PT);
21867
21868 #undef ROW_GLYPH_NEWLINE_P
21869 }
21870
21871 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21872 Sbidi_resolved_levels, 0, 1, 0,
21873 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21874
21875 The resolved levels are produced by the Emacs bidi reordering engine
21876 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21877 read the Unicode Standard Annex 9 (UAX#9) for background information
21878 about these levels.
21879
21880 VPOS is the zero-based number of the current window's screen line
21881 for which to produce the resolved levels. If VPOS is nil or omitted,
21882 it defaults to the screen line of point. If the window displays a
21883 header line, VPOS of zero will report on the header line, and first
21884 line of text in the window will have VPOS of 1.
21885
21886 Value is an array of resolved levels, indexed by glyph number.
21887 Glyphs are numbered from zero starting from the beginning of the
21888 screen line, i.e. the left edge of the window for left-to-right lines
21889 and from the right edge for right-to-left lines. The resolved levels
21890 are produced only for the window's text area; text in display margins
21891 is not included.
21892
21893 If the selected window's display is not up-to-date, or if the specified
21894 screen line does not display text, this function returns nil. It is
21895 highly recommended to bind this function to some simple key, like F8,
21896 in order to avoid these problems.
21897
21898 This function exists mainly for testing the correctness of the
21899 Emacs UBA implementation, in particular with the test suite. */)
21900 (Lisp_Object vpos)
21901 {
21902 struct window *w = XWINDOW (selected_window);
21903 struct buffer *b = XBUFFER (w->contents);
21904 int nrow;
21905 struct glyph_row *row;
21906
21907 if (NILP (vpos))
21908 {
21909 int d1, d2, d3, d4, d5;
21910
21911 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21912 }
21913 else
21914 {
21915 CHECK_NUMBER_COERCE_MARKER (vpos);
21916 nrow = XINT (vpos);
21917 }
21918
21919 /* We require up-to-date glyph matrix for this window. */
21920 if (w->window_end_valid
21921 && !windows_or_buffers_changed
21922 && b
21923 && !b->clip_changed
21924 && !b->prevent_redisplay_optimizations_p
21925 && !window_outdated (w)
21926 && nrow >= 0
21927 && nrow < w->current_matrix->nrows
21928 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21929 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21930 {
21931 struct glyph *g, *e, *g1;
21932 int nglyphs, i;
21933 Lisp_Object levels;
21934
21935 if (!row->reversed_p) /* Left-to-right glyph row. */
21936 {
21937 g = g1 = row->glyphs[TEXT_AREA];
21938 e = g + row->used[TEXT_AREA];
21939
21940 /* Skip over glyphs at the start of the row that was
21941 generated by redisplay for its own needs. */
21942 while (g < e
21943 && NILP (g->object)
21944 && g->charpos < 0)
21945 g++;
21946 g1 = g;
21947
21948 /* Count the "interesting" glyphs in this row. */
21949 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21950 nglyphs++;
21951
21952 /* Create and fill the array. */
21953 levels = make_uninit_vector (nglyphs);
21954 for (i = 0; g1 < g; i++, g1++)
21955 ASET (levels, i, make_number (g1->resolved_level));
21956 }
21957 else /* Right-to-left glyph row. */
21958 {
21959 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21960 e = row->glyphs[TEXT_AREA] - 1;
21961 while (g > e
21962 && NILP (g->object)
21963 && g->charpos < 0)
21964 g--;
21965 g1 = g;
21966 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21967 nglyphs++;
21968 levels = make_uninit_vector (nglyphs);
21969 for (i = 0; g1 > g; i++, g1--)
21970 ASET (levels, i, make_number (g1->resolved_level));
21971 }
21972 return levels;
21973 }
21974 else
21975 return Qnil;
21976 }
21977
21978
21979 \f
21980 /***********************************************************************
21981 Menu Bar
21982 ***********************************************************************/
21983
21984 /* Redisplay the menu bar in the frame for window W.
21985
21986 The menu bar of X frames that don't have X toolkit support is
21987 displayed in a special window W->frame->menu_bar_window.
21988
21989 The menu bar of terminal frames is treated specially as far as
21990 glyph matrices are concerned. Menu bar lines are not part of
21991 windows, so the update is done directly on the frame matrix rows
21992 for the menu bar. */
21993
21994 static void
21995 display_menu_bar (struct window *w)
21996 {
21997 struct frame *f = XFRAME (WINDOW_FRAME (w));
21998 struct it it;
21999 Lisp_Object items;
22000 int i;
22001
22002 /* Don't do all this for graphical frames. */
22003 #ifdef HAVE_NTGUI
22004 if (FRAME_W32_P (f))
22005 return;
22006 #endif
22007 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22008 if (FRAME_X_P (f))
22009 return;
22010 #endif
22011
22012 #ifdef HAVE_NS
22013 if (FRAME_NS_P (f))
22014 return;
22015 #endif /* HAVE_NS */
22016
22017 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22018 eassert (!FRAME_WINDOW_P (f));
22019 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22020 it.first_visible_x = 0;
22021 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22022 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22023 if (FRAME_WINDOW_P (f))
22024 {
22025 /* Menu bar lines are displayed in the desired matrix of the
22026 dummy window menu_bar_window. */
22027 struct window *menu_w;
22028 menu_w = XWINDOW (f->menu_bar_window);
22029 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22030 MENU_FACE_ID);
22031 it.first_visible_x = 0;
22032 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22033 }
22034 else
22035 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22036 {
22037 /* This is a TTY frame, i.e. character hpos/vpos are used as
22038 pixel x/y. */
22039 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22040 MENU_FACE_ID);
22041 it.first_visible_x = 0;
22042 it.last_visible_x = FRAME_COLS (f);
22043 }
22044
22045 /* FIXME: This should be controlled by a user option. See the
22046 comments in redisplay_tool_bar and display_mode_line about
22047 this. */
22048 it.paragraph_embedding = L2R;
22049
22050 /* Clear all rows of the menu bar. */
22051 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22052 {
22053 struct glyph_row *row = it.glyph_row + i;
22054 clear_glyph_row (row);
22055 row->enabled_p = true;
22056 row->full_width_p = true;
22057 row->reversed_p = false;
22058 }
22059
22060 /* Display all items of the menu bar. */
22061 items = FRAME_MENU_BAR_ITEMS (it.f);
22062 for (i = 0; i < ASIZE (items); i += 4)
22063 {
22064 Lisp_Object string;
22065
22066 /* Stop at nil string. */
22067 string = AREF (items, i + 1);
22068 if (NILP (string))
22069 break;
22070
22071 /* Remember where item was displayed. */
22072 ASET (items, i + 3, make_number (it.hpos));
22073
22074 /* Display the item, pad with one space. */
22075 if (it.current_x < it.last_visible_x)
22076 display_string (NULL, string, Qnil, 0, 0, &it,
22077 SCHARS (string) + 1, 0, 0, -1);
22078 }
22079
22080 /* Fill out the line with spaces. */
22081 if (it.current_x < it.last_visible_x)
22082 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22083
22084 /* Compute the total height of the lines. */
22085 compute_line_metrics (&it);
22086 }
22087
22088 /* Deep copy of a glyph row, including the glyphs. */
22089 static void
22090 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22091 {
22092 struct glyph *pointers[1 + LAST_AREA];
22093 int to_used = to->used[TEXT_AREA];
22094
22095 /* Save glyph pointers of TO. */
22096 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22097
22098 /* Do a structure assignment. */
22099 *to = *from;
22100
22101 /* Restore original glyph pointers of TO. */
22102 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22103
22104 /* Copy the glyphs. */
22105 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22106 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22107
22108 /* If we filled only part of the TO row, fill the rest with
22109 space_glyph (which will display as empty space). */
22110 if (to_used > from->used[TEXT_AREA])
22111 fill_up_frame_row_with_spaces (to, to_used);
22112 }
22113
22114 /* Display one menu item on a TTY, by overwriting the glyphs in the
22115 frame F's desired glyph matrix with glyphs produced from the menu
22116 item text. Called from term.c to display TTY drop-down menus one
22117 item at a time.
22118
22119 ITEM_TEXT is the menu item text as a C string.
22120
22121 FACE_ID is the face ID to be used for this menu item. FACE_ID
22122 could specify one of 3 faces: a face for an enabled item, a face
22123 for a disabled item, or a face for a selected item.
22124
22125 X and Y are coordinates of the first glyph in the frame's desired
22126 matrix to be overwritten by the menu item. Since this is a TTY, Y
22127 is the zero-based number of the glyph row and X is the zero-based
22128 glyph number in the row, starting from left, where to start
22129 displaying the item.
22130
22131 SUBMENU means this menu item drops down a submenu, which
22132 should be indicated by displaying a proper visual cue after the
22133 item text. */
22134
22135 void
22136 display_tty_menu_item (const char *item_text, int width, int face_id,
22137 int x, int y, bool submenu)
22138 {
22139 struct it it;
22140 struct frame *f = SELECTED_FRAME ();
22141 struct window *w = XWINDOW (f->selected_window);
22142 struct glyph_row *row;
22143 size_t item_len = strlen (item_text);
22144
22145 eassert (FRAME_TERMCAP_P (f));
22146
22147 /* Don't write beyond the matrix's last row. This can happen for
22148 TTY screens that are not high enough to show the entire menu.
22149 (This is actually a bit of defensive programming, as
22150 tty_menu_display already limits the number of menu items to one
22151 less than the number of screen lines.) */
22152 if (y >= f->desired_matrix->nrows)
22153 return;
22154
22155 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
22156 it.first_visible_x = 0;
22157 it.last_visible_x = FRAME_COLS (f) - 1;
22158 row = it.glyph_row;
22159 /* Start with the row contents from the current matrix. */
22160 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22161 bool saved_width = row->full_width_p;
22162 row->full_width_p = true;
22163 bool saved_reversed = row->reversed_p;
22164 row->reversed_p = false;
22165 row->enabled_p = true;
22166
22167 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22168 desired face. */
22169 eassert (x < f->desired_matrix->matrix_w);
22170 it.current_x = it.hpos = x;
22171 it.current_y = it.vpos = y;
22172 int saved_used = row->used[TEXT_AREA];
22173 bool saved_truncated = row->truncated_on_right_p;
22174 row->used[TEXT_AREA] = x;
22175 it.face_id = face_id;
22176 it.line_wrap = TRUNCATE;
22177
22178 /* FIXME: This should be controlled by a user option. See the
22179 comments in redisplay_tool_bar and display_mode_line about this.
22180 Also, if paragraph_embedding could ever be R2L, changes will be
22181 needed to avoid shifting to the right the row characters in
22182 term.c:append_glyph. */
22183 it.paragraph_embedding = L2R;
22184
22185 /* Pad with a space on the left. */
22186 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22187 width--;
22188 /* Display the menu item, pad with spaces to WIDTH. */
22189 if (submenu)
22190 {
22191 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22192 item_len, 0, FRAME_COLS (f) - 1, -1);
22193 width -= item_len;
22194 /* Indicate with " >" that there's a submenu. */
22195 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22196 FRAME_COLS (f) - 1, -1);
22197 }
22198 else
22199 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22200 width, 0, FRAME_COLS (f) - 1, -1);
22201
22202 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22203 row->truncated_on_right_p = saved_truncated;
22204 row->hash = row_hash (row);
22205 row->full_width_p = saved_width;
22206 row->reversed_p = saved_reversed;
22207 }
22208 \f
22209 /***********************************************************************
22210 Mode Line
22211 ***********************************************************************/
22212
22213 /* Redisplay mode lines in the window tree whose root is WINDOW.
22214 If FORCE, redisplay mode lines unconditionally.
22215 Otherwise, redisplay only mode lines that are garbaged. Value is
22216 the number of windows whose mode lines were redisplayed. */
22217
22218 static int
22219 redisplay_mode_lines (Lisp_Object window, bool force)
22220 {
22221 int nwindows = 0;
22222
22223 while (!NILP (window))
22224 {
22225 struct window *w = XWINDOW (window);
22226
22227 if (WINDOWP (w->contents))
22228 nwindows += redisplay_mode_lines (w->contents, force);
22229 else if (force
22230 || FRAME_GARBAGED_P (XFRAME (w->frame))
22231 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22232 {
22233 struct text_pos lpoint;
22234 struct buffer *old = current_buffer;
22235
22236 /* Set the window's buffer for the mode line display. */
22237 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22238 set_buffer_internal_1 (XBUFFER (w->contents));
22239
22240 /* Point refers normally to the selected window. For any
22241 other window, set up appropriate value. */
22242 if (!EQ (window, selected_window))
22243 {
22244 struct text_pos pt;
22245
22246 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22247 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22248 }
22249
22250 /* Display mode lines. */
22251 clear_glyph_matrix (w->desired_matrix);
22252 if (display_mode_lines (w))
22253 ++nwindows;
22254
22255 /* Restore old settings. */
22256 set_buffer_internal_1 (old);
22257 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22258 }
22259
22260 window = w->next;
22261 }
22262
22263 return nwindows;
22264 }
22265
22266
22267 /* Display the mode and/or header line of window W. Value is the
22268 sum number of mode lines and header lines displayed. */
22269
22270 static int
22271 display_mode_lines (struct window *w)
22272 {
22273 Lisp_Object old_selected_window = selected_window;
22274 Lisp_Object old_selected_frame = selected_frame;
22275 Lisp_Object new_frame = w->frame;
22276 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22277 int n = 0;
22278
22279 selected_frame = new_frame;
22280 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22281 or window's point, then we'd need select_window_1 here as well. */
22282 XSETWINDOW (selected_window, w);
22283 XFRAME (new_frame)->selected_window = selected_window;
22284
22285 /* These will be set while the mode line specs are processed. */
22286 line_number_displayed = false;
22287 w->column_number_displayed = -1;
22288
22289 if (WINDOW_WANTS_MODELINE_P (w))
22290 {
22291 struct window *sel_w = XWINDOW (old_selected_window);
22292
22293 /* Select mode line face based on the real selected window. */
22294 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22295 BVAR (current_buffer, mode_line_format));
22296 ++n;
22297 }
22298
22299 if (WINDOW_WANTS_HEADER_LINE_P (w))
22300 {
22301 display_mode_line (w, HEADER_LINE_FACE_ID,
22302 BVAR (current_buffer, header_line_format));
22303 ++n;
22304 }
22305
22306 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22307 selected_frame = old_selected_frame;
22308 selected_window = old_selected_window;
22309 if (n > 0)
22310 w->must_be_updated_p = true;
22311 return n;
22312 }
22313
22314
22315 /* Display mode or header line of window W. FACE_ID specifies which
22316 line to display; it is either MODE_LINE_FACE_ID or
22317 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22318 display. Value is the pixel height of the mode/header line
22319 displayed. */
22320
22321 static int
22322 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22323 {
22324 struct it it;
22325 struct face *face;
22326 ptrdiff_t count = SPECPDL_INDEX ();
22327
22328 init_iterator (&it, w, -1, -1, NULL, face_id);
22329 /* Don't extend on a previously drawn mode-line.
22330 This may happen if called from pos_visible_p. */
22331 it.glyph_row->enabled_p = false;
22332 prepare_desired_row (w, it.glyph_row, true);
22333
22334 it.glyph_row->mode_line_p = true;
22335
22336 /* FIXME: This should be controlled by a user option. But
22337 supporting such an option is not trivial, since the mode line is
22338 made up of many separate strings. */
22339 it.paragraph_embedding = L2R;
22340
22341 record_unwind_protect (unwind_format_mode_line,
22342 format_mode_line_unwind_data (NULL, NULL,
22343 Qnil, false));
22344
22345 mode_line_target = MODE_LINE_DISPLAY;
22346
22347 /* Temporarily make frame's keyboard the current kboard so that
22348 kboard-local variables in the mode_line_format will get the right
22349 values. */
22350 push_kboard (FRAME_KBOARD (it.f));
22351 record_unwind_save_match_data ();
22352 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22353 pop_kboard ();
22354
22355 unbind_to (count, Qnil);
22356
22357 /* Fill up with spaces. */
22358 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22359
22360 compute_line_metrics (&it);
22361 it.glyph_row->full_width_p = true;
22362 it.glyph_row->continued_p = false;
22363 it.glyph_row->truncated_on_left_p = false;
22364 it.glyph_row->truncated_on_right_p = false;
22365
22366 /* Make a 3D mode-line have a shadow at its right end. */
22367 face = FACE_FROM_ID (it.f, face_id);
22368 extend_face_to_end_of_line (&it);
22369 if (face->box != FACE_NO_BOX)
22370 {
22371 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22372 + it.glyph_row->used[TEXT_AREA] - 1);
22373 last->right_box_line_p = true;
22374 }
22375
22376 return it.glyph_row->height;
22377 }
22378
22379 /* Move element ELT in LIST to the front of LIST.
22380 Return the updated list. */
22381
22382 static Lisp_Object
22383 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22384 {
22385 register Lisp_Object tail, prev;
22386 register Lisp_Object tem;
22387
22388 tail = list;
22389 prev = Qnil;
22390 while (CONSP (tail))
22391 {
22392 tem = XCAR (tail);
22393
22394 if (EQ (elt, tem))
22395 {
22396 /* Splice out the link TAIL. */
22397 if (NILP (prev))
22398 list = XCDR (tail);
22399 else
22400 Fsetcdr (prev, XCDR (tail));
22401
22402 /* Now make it the first. */
22403 Fsetcdr (tail, list);
22404 return tail;
22405 }
22406 else
22407 prev = tail;
22408 tail = XCDR (tail);
22409 QUIT;
22410 }
22411
22412 /* Not found--return unchanged LIST. */
22413 return list;
22414 }
22415
22416 /* Contribute ELT to the mode line for window IT->w. How it
22417 translates into text depends on its data type.
22418
22419 IT describes the display environment in which we display, as usual.
22420
22421 DEPTH is the depth in recursion. It is used to prevent
22422 infinite recursion here.
22423
22424 FIELD_WIDTH is the number of characters the display of ELT should
22425 occupy in the mode line, and PRECISION is the maximum number of
22426 characters to display from ELT's representation. See
22427 display_string for details.
22428
22429 Returns the hpos of the end of the text generated by ELT.
22430
22431 PROPS is a property list to add to any string we encounter.
22432
22433 If RISKY, remove (disregard) any properties in any string
22434 we encounter, and ignore :eval and :propertize.
22435
22436 The global variable `mode_line_target' determines whether the
22437 output is passed to `store_mode_line_noprop',
22438 `store_mode_line_string', or `display_string'. */
22439
22440 static int
22441 display_mode_element (struct it *it, int depth, int field_width, int precision,
22442 Lisp_Object elt, Lisp_Object props, bool risky)
22443 {
22444 int n = 0, field, prec;
22445 bool literal = false;
22446
22447 tail_recurse:
22448 if (depth > 100)
22449 elt = build_string ("*too-deep*");
22450
22451 depth++;
22452
22453 switch (XTYPE (elt))
22454 {
22455 case Lisp_String:
22456 {
22457 /* A string: output it and check for %-constructs within it. */
22458 unsigned char c;
22459 ptrdiff_t offset = 0;
22460
22461 if (SCHARS (elt) > 0
22462 && (!NILP (props) || risky))
22463 {
22464 Lisp_Object oprops, aelt;
22465 oprops = Ftext_properties_at (make_number (0), elt);
22466
22467 /* If the starting string's properties are not what
22468 we want, translate the string. Also, if the string
22469 is risky, do that anyway. */
22470
22471 if (NILP (Fequal (props, oprops)) || risky)
22472 {
22473 /* If the starting string has properties,
22474 merge the specified ones onto the existing ones. */
22475 if (! NILP (oprops) && !risky)
22476 {
22477 Lisp_Object tem;
22478
22479 oprops = Fcopy_sequence (oprops);
22480 tem = props;
22481 while (CONSP (tem))
22482 {
22483 oprops = Fplist_put (oprops, XCAR (tem),
22484 XCAR (XCDR (tem)));
22485 tem = XCDR (XCDR (tem));
22486 }
22487 props = oprops;
22488 }
22489
22490 aelt = Fassoc (elt, mode_line_proptrans_alist);
22491 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22492 {
22493 /* AELT is what we want. Move it to the front
22494 without consing. */
22495 elt = XCAR (aelt);
22496 mode_line_proptrans_alist
22497 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22498 }
22499 else
22500 {
22501 Lisp_Object tem;
22502
22503 /* If AELT has the wrong props, it is useless.
22504 so get rid of it. */
22505 if (! NILP (aelt))
22506 mode_line_proptrans_alist
22507 = Fdelq (aelt, mode_line_proptrans_alist);
22508
22509 elt = Fcopy_sequence (elt);
22510 Fset_text_properties (make_number (0), Flength (elt),
22511 props, elt);
22512 /* Add this item to mode_line_proptrans_alist. */
22513 mode_line_proptrans_alist
22514 = Fcons (Fcons (elt, props),
22515 mode_line_proptrans_alist);
22516 /* Truncate mode_line_proptrans_alist
22517 to at most 50 elements. */
22518 tem = Fnthcdr (make_number (50),
22519 mode_line_proptrans_alist);
22520 if (! NILP (tem))
22521 XSETCDR (tem, Qnil);
22522 }
22523 }
22524 }
22525
22526 offset = 0;
22527
22528 if (literal)
22529 {
22530 prec = precision - n;
22531 switch (mode_line_target)
22532 {
22533 case MODE_LINE_NOPROP:
22534 case MODE_LINE_TITLE:
22535 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22536 break;
22537 case MODE_LINE_STRING:
22538 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22539 break;
22540 case MODE_LINE_DISPLAY:
22541 n += display_string (NULL, elt, Qnil, 0, 0, it,
22542 0, prec, 0, STRING_MULTIBYTE (elt));
22543 break;
22544 }
22545
22546 break;
22547 }
22548
22549 /* Handle the non-literal case. */
22550
22551 while ((precision <= 0 || n < precision)
22552 && SREF (elt, offset) != 0
22553 && (mode_line_target != MODE_LINE_DISPLAY
22554 || it->current_x < it->last_visible_x))
22555 {
22556 ptrdiff_t last_offset = offset;
22557
22558 /* Advance to end of string or next format specifier. */
22559 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22560 ;
22561
22562 if (offset - 1 != last_offset)
22563 {
22564 ptrdiff_t nchars, nbytes;
22565
22566 /* Output to end of string or up to '%'. Field width
22567 is length of string. Don't output more than
22568 PRECISION allows us. */
22569 offset--;
22570
22571 prec = c_string_width (SDATA (elt) + last_offset,
22572 offset - last_offset, precision - n,
22573 &nchars, &nbytes);
22574
22575 switch (mode_line_target)
22576 {
22577 case MODE_LINE_NOPROP:
22578 case MODE_LINE_TITLE:
22579 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22580 break;
22581 case MODE_LINE_STRING:
22582 {
22583 ptrdiff_t bytepos = last_offset;
22584 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22585 ptrdiff_t endpos = (precision <= 0
22586 ? string_byte_to_char (elt, offset)
22587 : charpos + nchars);
22588 Lisp_Object mode_string
22589 = Fsubstring (elt, make_number (charpos),
22590 make_number (endpos));
22591 n += store_mode_line_string (NULL, mode_string, false,
22592 0, 0, Qnil);
22593 }
22594 break;
22595 case MODE_LINE_DISPLAY:
22596 {
22597 ptrdiff_t bytepos = last_offset;
22598 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22599
22600 if (precision <= 0)
22601 nchars = string_byte_to_char (elt, offset) - charpos;
22602 n += display_string (NULL, elt, Qnil, 0, charpos,
22603 it, 0, nchars, 0,
22604 STRING_MULTIBYTE (elt));
22605 }
22606 break;
22607 }
22608 }
22609 else /* c == '%' */
22610 {
22611 ptrdiff_t percent_position = offset;
22612
22613 /* Get the specified minimum width. Zero means
22614 don't pad. */
22615 field = 0;
22616 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22617 field = field * 10 + c - '0';
22618
22619 /* Don't pad beyond the total padding allowed. */
22620 if (field_width - n > 0 && field > field_width - n)
22621 field = field_width - n;
22622
22623 /* Note that either PRECISION <= 0 or N < PRECISION. */
22624 prec = precision - n;
22625
22626 if (c == 'M')
22627 n += display_mode_element (it, depth, field, prec,
22628 Vglobal_mode_string, props,
22629 risky);
22630 else if (c != 0)
22631 {
22632 bool multibyte;
22633 ptrdiff_t bytepos, charpos;
22634 const char *spec;
22635 Lisp_Object string;
22636
22637 bytepos = percent_position;
22638 charpos = (STRING_MULTIBYTE (elt)
22639 ? string_byte_to_char (elt, bytepos)
22640 : bytepos);
22641 spec = decode_mode_spec (it->w, c, field, &string);
22642 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22643
22644 switch (mode_line_target)
22645 {
22646 case MODE_LINE_NOPROP:
22647 case MODE_LINE_TITLE:
22648 n += store_mode_line_noprop (spec, field, prec);
22649 break;
22650 case MODE_LINE_STRING:
22651 {
22652 Lisp_Object tem = build_string (spec);
22653 props = Ftext_properties_at (make_number (charpos), elt);
22654 /* Should only keep face property in props */
22655 n += store_mode_line_string (NULL, tem, false,
22656 field, prec, props);
22657 }
22658 break;
22659 case MODE_LINE_DISPLAY:
22660 {
22661 int nglyphs_before, nwritten;
22662
22663 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22664 nwritten = display_string (spec, string, elt,
22665 charpos, 0, it,
22666 field, prec, 0,
22667 multibyte);
22668
22669 /* Assign to the glyphs written above the
22670 string where the `%x' came from, position
22671 of the `%'. */
22672 if (nwritten > 0)
22673 {
22674 struct glyph *glyph
22675 = (it->glyph_row->glyphs[TEXT_AREA]
22676 + nglyphs_before);
22677 int i;
22678
22679 for (i = 0; i < nwritten; ++i)
22680 {
22681 glyph[i].object = elt;
22682 glyph[i].charpos = charpos;
22683 }
22684
22685 n += nwritten;
22686 }
22687 }
22688 break;
22689 }
22690 }
22691 else /* c == 0 */
22692 break;
22693 }
22694 }
22695 }
22696 break;
22697
22698 case Lisp_Symbol:
22699 /* A symbol: process the value of the symbol recursively
22700 as if it appeared here directly. Avoid error if symbol void.
22701 Special case: if value of symbol is a string, output the string
22702 literally. */
22703 {
22704 register Lisp_Object tem;
22705
22706 /* If the variable is not marked as risky to set
22707 then its contents are risky to use. */
22708 if (NILP (Fget (elt, Qrisky_local_variable)))
22709 risky = true;
22710
22711 tem = Fboundp (elt);
22712 if (!NILP (tem))
22713 {
22714 tem = Fsymbol_value (elt);
22715 /* If value is a string, output that string literally:
22716 don't check for % within it. */
22717 if (STRINGP (tem))
22718 literal = true;
22719
22720 if (!EQ (tem, elt))
22721 {
22722 /* Give up right away for nil or t. */
22723 elt = tem;
22724 goto tail_recurse;
22725 }
22726 }
22727 }
22728 break;
22729
22730 case Lisp_Cons:
22731 {
22732 register Lisp_Object car, tem;
22733
22734 /* A cons cell: five distinct cases.
22735 If first element is :eval or :propertize, do something special.
22736 If first element is a string or a cons, process all the elements
22737 and effectively concatenate them.
22738 If first element is a negative number, truncate displaying cdr to
22739 at most that many characters. If positive, pad (with spaces)
22740 to at least that many characters.
22741 If first element is a symbol, process the cadr or caddr recursively
22742 according to whether the symbol's value is non-nil or nil. */
22743 car = XCAR (elt);
22744 if (EQ (car, QCeval))
22745 {
22746 /* An element of the form (:eval FORM) means evaluate FORM
22747 and use the result as mode line elements. */
22748
22749 if (risky)
22750 break;
22751
22752 if (CONSP (XCDR (elt)))
22753 {
22754 Lisp_Object spec;
22755 spec = safe__eval (true, XCAR (XCDR (elt)));
22756 n += display_mode_element (it, depth, field_width - n,
22757 precision - n, spec, props,
22758 risky);
22759 }
22760 }
22761 else if (EQ (car, QCpropertize))
22762 {
22763 /* An element of the form (:propertize ELT PROPS...)
22764 means display ELT but applying properties PROPS. */
22765
22766 if (risky)
22767 break;
22768
22769 if (CONSP (XCDR (elt)))
22770 n += display_mode_element (it, depth, field_width - n,
22771 precision - n, XCAR (XCDR (elt)),
22772 XCDR (XCDR (elt)), risky);
22773 }
22774 else if (SYMBOLP (car))
22775 {
22776 tem = Fboundp (car);
22777 elt = XCDR (elt);
22778 if (!CONSP (elt))
22779 goto invalid;
22780 /* elt is now the cdr, and we know it is a cons cell.
22781 Use its car if CAR has a non-nil value. */
22782 if (!NILP (tem))
22783 {
22784 tem = Fsymbol_value (car);
22785 if (!NILP (tem))
22786 {
22787 elt = XCAR (elt);
22788 goto tail_recurse;
22789 }
22790 }
22791 /* Symbol's value is nil (or symbol is unbound)
22792 Get the cddr of the original list
22793 and if possible find the caddr and use that. */
22794 elt = XCDR (elt);
22795 if (NILP (elt))
22796 break;
22797 else if (!CONSP (elt))
22798 goto invalid;
22799 elt = XCAR (elt);
22800 goto tail_recurse;
22801 }
22802 else if (INTEGERP (car))
22803 {
22804 register int lim = XINT (car);
22805 elt = XCDR (elt);
22806 if (lim < 0)
22807 {
22808 /* Negative int means reduce maximum width. */
22809 if (precision <= 0)
22810 precision = -lim;
22811 else
22812 precision = min (precision, -lim);
22813 }
22814 else if (lim > 0)
22815 {
22816 /* Padding specified. Don't let it be more than
22817 current maximum. */
22818 if (precision > 0)
22819 lim = min (precision, lim);
22820
22821 /* If that's more padding than already wanted, queue it.
22822 But don't reduce padding already specified even if
22823 that is beyond the current truncation point. */
22824 field_width = max (lim, field_width);
22825 }
22826 goto tail_recurse;
22827 }
22828 else if (STRINGP (car) || CONSP (car))
22829 {
22830 Lisp_Object halftail = elt;
22831 int len = 0;
22832
22833 while (CONSP (elt)
22834 && (precision <= 0 || n < precision))
22835 {
22836 n += display_mode_element (it, depth,
22837 /* Do padding only after the last
22838 element in the list. */
22839 (! CONSP (XCDR (elt))
22840 ? field_width - n
22841 : 0),
22842 precision - n, XCAR (elt),
22843 props, risky);
22844 elt = XCDR (elt);
22845 len++;
22846 if ((len & 1) == 0)
22847 halftail = XCDR (halftail);
22848 /* Check for cycle. */
22849 if (EQ (halftail, elt))
22850 break;
22851 }
22852 }
22853 }
22854 break;
22855
22856 default:
22857 invalid:
22858 elt = build_string ("*invalid*");
22859 goto tail_recurse;
22860 }
22861
22862 /* Pad to FIELD_WIDTH. */
22863 if (field_width > 0 && n < field_width)
22864 {
22865 switch (mode_line_target)
22866 {
22867 case MODE_LINE_NOPROP:
22868 case MODE_LINE_TITLE:
22869 n += store_mode_line_noprop ("", field_width - n, 0);
22870 break;
22871 case MODE_LINE_STRING:
22872 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
22873 Qnil);
22874 break;
22875 case MODE_LINE_DISPLAY:
22876 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22877 0, 0, 0);
22878 break;
22879 }
22880 }
22881
22882 return n;
22883 }
22884
22885 /* Store a mode-line string element in mode_line_string_list.
22886
22887 If STRING is non-null, display that C string. Otherwise, the Lisp
22888 string LISP_STRING is displayed.
22889
22890 FIELD_WIDTH is the minimum number of output glyphs to produce.
22891 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22892 with spaces. FIELD_WIDTH <= 0 means don't pad.
22893
22894 PRECISION is the maximum number of characters to output from
22895 STRING. PRECISION <= 0 means don't truncate the string.
22896
22897 If COPY_STRING, make a copy of LISP_STRING before adding
22898 properties to the string.
22899
22900 PROPS are the properties to add to the string.
22901 The mode_line_string_face face property is always added to the string.
22902 */
22903
22904 static int
22905 store_mode_line_string (const char *string, Lisp_Object lisp_string,
22906 bool copy_string,
22907 int field_width, int precision, Lisp_Object props)
22908 {
22909 ptrdiff_t len;
22910 int n = 0;
22911
22912 if (string != NULL)
22913 {
22914 len = strlen (string);
22915 if (precision > 0 && len > precision)
22916 len = precision;
22917 lisp_string = make_string (string, len);
22918 if (NILP (props))
22919 props = mode_line_string_face_prop;
22920 else if (!NILP (mode_line_string_face))
22921 {
22922 Lisp_Object face = Fplist_get (props, Qface);
22923 props = Fcopy_sequence (props);
22924 if (NILP (face))
22925 face = mode_line_string_face;
22926 else
22927 face = list2 (face, mode_line_string_face);
22928 props = Fplist_put (props, Qface, face);
22929 }
22930 Fadd_text_properties (make_number (0), make_number (len),
22931 props, lisp_string);
22932 }
22933 else
22934 {
22935 len = XFASTINT (Flength (lisp_string));
22936 if (precision > 0 && len > precision)
22937 {
22938 len = precision;
22939 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22940 precision = -1;
22941 }
22942 if (!NILP (mode_line_string_face))
22943 {
22944 Lisp_Object face;
22945 if (NILP (props))
22946 props = Ftext_properties_at (make_number (0), lisp_string);
22947 face = Fplist_get (props, Qface);
22948 if (NILP (face))
22949 face = mode_line_string_face;
22950 else
22951 face = list2 (face, mode_line_string_face);
22952 props = list2 (Qface, face);
22953 if (copy_string)
22954 lisp_string = Fcopy_sequence (lisp_string);
22955 }
22956 if (!NILP (props))
22957 Fadd_text_properties (make_number (0), make_number (len),
22958 props, lisp_string);
22959 }
22960
22961 if (len > 0)
22962 {
22963 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22964 n += len;
22965 }
22966
22967 if (field_width > len)
22968 {
22969 field_width -= len;
22970 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22971 if (!NILP (props))
22972 Fadd_text_properties (make_number (0), make_number (field_width),
22973 props, lisp_string);
22974 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22975 n += field_width;
22976 }
22977
22978 return n;
22979 }
22980
22981
22982 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22983 1, 4, 0,
22984 doc: /* Format a string out of a mode line format specification.
22985 First arg FORMAT specifies the mode line format (see `mode-line-format'
22986 for details) to use.
22987
22988 By default, the format is evaluated for the currently selected window.
22989
22990 Optional second arg FACE specifies the face property to put on all
22991 characters for which no face is specified. The value nil means the
22992 default face. The value t means whatever face the window's mode line
22993 currently uses (either `mode-line' or `mode-line-inactive',
22994 depending on whether the window is the selected window or not).
22995 An integer value means the value string has no text
22996 properties.
22997
22998 Optional third and fourth args WINDOW and BUFFER specify the window
22999 and buffer to use as the context for the formatting (defaults
23000 are the selected window and the WINDOW's buffer). */)
23001 (Lisp_Object format, Lisp_Object face,
23002 Lisp_Object window, Lisp_Object buffer)
23003 {
23004 struct it it;
23005 int len;
23006 struct window *w;
23007 struct buffer *old_buffer = NULL;
23008 int face_id;
23009 bool no_props = INTEGERP (face);
23010 ptrdiff_t count = SPECPDL_INDEX ();
23011 Lisp_Object str;
23012 int string_start = 0;
23013
23014 w = decode_any_window (window);
23015 XSETWINDOW (window, w);
23016
23017 if (NILP (buffer))
23018 buffer = w->contents;
23019 CHECK_BUFFER (buffer);
23020
23021 /* Make formatting the modeline a non-op when noninteractive, otherwise
23022 there will be problems later caused by a partially initialized frame. */
23023 if (NILP (format) || noninteractive)
23024 return empty_unibyte_string;
23025
23026 if (no_props)
23027 face = Qnil;
23028
23029 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23030 : EQ (face, Qt) ? (EQ (window, selected_window)
23031 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23032 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23033 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23034 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23035 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23036 : DEFAULT_FACE_ID;
23037
23038 old_buffer = current_buffer;
23039
23040 /* Save things including mode_line_proptrans_alist,
23041 and set that to nil so that we don't alter the outer value. */
23042 record_unwind_protect (unwind_format_mode_line,
23043 format_mode_line_unwind_data
23044 (XFRAME (WINDOW_FRAME (w)),
23045 old_buffer, selected_window, true));
23046 mode_line_proptrans_alist = Qnil;
23047
23048 Fselect_window (window, Qt);
23049 set_buffer_internal_1 (XBUFFER (buffer));
23050
23051 init_iterator (&it, w, -1, -1, NULL, face_id);
23052
23053 if (no_props)
23054 {
23055 mode_line_target = MODE_LINE_NOPROP;
23056 mode_line_string_face_prop = Qnil;
23057 mode_line_string_list = Qnil;
23058 string_start = MODE_LINE_NOPROP_LEN (0);
23059 }
23060 else
23061 {
23062 mode_line_target = MODE_LINE_STRING;
23063 mode_line_string_list = Qnil;
23064 mode_line_string_face = face;
23065 mode_line_string_face_prop
23066 = NILP (face) ? Qnil : list2 (Qface, face);
23067 }
23068
23069 push_kboard (FRAME_KBOARD (it.f));
23070 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23071 pop_kboard ();
23072
23073 if (no_props)
23074 {
23075 len = MODE_LINE_NOPROP_LEN (string_start);
23076 str = make_string (mode_line_noprop_buf + string_start, len);
23077 }
23078 else
23079 {
23080 mode_line_string_list = Fnreverse (mode_line_string_list);
23081 str = Fmapconcat (Qidentity, mode_line_string_list,
23082 empty_unibyte_string);
23083 }
23084
23085 unbind_to (count, Qnil);
23086 return str;
23087 }
23088
23089 /* Write a null-terminated, right justified decimal representation of
23090 the positive integer D to BUF using a minimal field width WIDTH. */
23091
23092 static void
23093 pint2str (register char *buf, register int width, register ptrdiff_t d)
23094 {
23095 register char *p = buf;
23096
23097 if (d <= 0)
23098 *p++ = '0';
23099 else
23100 {
23101 while (d > 0)
23102 {
23103 *p++ = d % 10 + '0';
23104 d /= 10;
23105 }
23106 }
23107
23108 for (width -= (int) (p - buf); width > 0; --width)
23109 *p++ = ' ';
23110 *p-- = '\0';
23111 while (p > buf)
23112 {
23113 d = *buf;
23114 *buf++ = *p;
23115 *p-- = d;
23116 }
23117 }
23118
23119 /* Write a null-terminated, right justified decimal and "human
23120 readable" representation of the nonnegative integer D to BUF using
23121 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23122
23123 static const char power_letter[] =
23124 {
23125 0, /* no letter */
23126 'k', /* kilo */
23127 'M', /* mega */
23128 'G', /* giga */
23129 'T', /* tera */
23130 'P', /* peta */
23131 'E', /* exa */
23132 'Z', /* zetta */
23133 'Y' /* yotta */
23134 };
23135
23136 static void
23137 pint2hrstr (char *buf, int width, ptrdiff_t d)
23138 {
23139 /* We aim to represent the nonnegative integer D as
23140 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23141 ptrdiff_t quotient = d;
23142 int remainder = 0;
23143 /* -1 means: do not use TENTHS. */
23144 int tenths = -1;
23145 int exponent = 0;
23146
23147 /* Length of QUOTIENT.TENTHS as a string. */
23148 int length;
23149
23150 char * psuffix;
23151 char * p;
23152
23153 if (quotient >= 1000)
23154 {
23155 /* Scale to the appropriate EXPONENT. */
23156 do
23157 {
23158 remainder = quotient % 1000;
23159 quotient /= 1000;
23160 exponent++;
23161 }
23162 while (quotient >= 1000);
23163
23164 /* Round to nearest and decide whether to use TENTHS or not. */
23165 if (quotient <= 9)
23166 {
23167 tenths = remainder / 100;
23168 if (remainder % 100 >= 50)
23169 {
23170 if (tenths < 9)
23171 tenths++;
23172 else
23173 {
23174 quotient++;
23175 if (quotient == 10)
23176 tenths = -1;
23177 else
23178 tenths = 0;
23179 }
23180 }
23181 }
23182 else
23183 if (remainder >= 500)
23184 {
23185 if (quotient < 999)
23186 quotient++;
23187 else
23188 {
23189 quotient = 1;
23190 exponent++;
23191 tenths = 0;
23192 }
23193 }
23194 }
23195
23196 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23197 if (tenths == -1 && quotient <= 99)
23198 if (quotient <= 9)
23199 length = 1;
23200 else
23201 length = 2;
23202 else
23203 length = 3;
23204 p = psuffix = buf + max (width, length);
23205
23206 /* Print EXPONENT. */
23207 *psuffix++ = power_letter[exponent];
23208 *psuffix = '\0';
23209
23210 /* Print TENTHS. */
23211 if (tenths >= 0)
23212 {
23213 *--p = '0' + tenths;
23214 *--p = '.';
23215 }
23216
23217 /* Print QUOTIENT. */
23218 do
23219 {
23220 int digit = quotient % 10;
23221 *--p = '0' + digit;
23222 }
23223 while ((quotient /= 10) != 0);
23224
23225 /* Print leading spaces. */
23226 while (buf < p)
23227 *--p = ' ';
23228 }
23229
23230 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23231 If EOL_FLAG, set also a mnemonic character for end-of-line
23232 type of CODING_SYSTEM. Return updated pointer into BUF. */
23233
23234 static unsigned char invalid_eol_type[] = "(*invalid*)";
23235
23236 static char *
23237 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23238 {
23239 Lisp_Object val;
23240 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23241 const unsigned char *eol_str;
23242 int eol_str_len;
23243 /* The EOL conversion we are using. */
23244 Lisp_Object eoltype;
23245
23246 val = CODING_SYSTEM_SPEC (coding_system);
23247 eoltype = Qnil;
23248
23249 if (!VECTORP (val)) /* Not yet decided. */
23250 {
23251 *buf++ = multibyte ? '-' : ' ';
23252 if (eol_flag)
23253 eoltype = eol_mnemonic_undecided;
23254 /* Don't mention EOL conversion if it isn't decided. */
23255 }
23256 else
23257 {
23258 Lisp_Object attrs;
23259 Lisp_Object eolvalue;
23260
23261 attrs = AREF (val, 0);
23262 eolvalue = AREF (val, 2);
23263
23264 *buf++ = multibyte
23265 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23266 : ' ';
23267
23268 if (eol_flag)
23269 {
23270 /* The EOL conversion that is normal on this system. */
23271
23272 if (NILP (eolvalue)) /* Not yet decided. */
23273 eoltype = eol_mnemonic_undecided;
23274 else if (VECTORP (eolvalue)) /* Not yet decided. */
23275 eoltype = eol_mnemonic_undecided;
23276 else /* eolvalue is Qunix, Qdos, or Qmac. */
23277 eoltype = (EQ (eolvalue, Qunix)
23278 ? eol_mnemonic_unix
23279 : EQ (eolvalue, Qdos)
23280 ? eol_mnemonic_dos : eol_mnemonic_mac);
23281 }
23282 }
23283
23284 if (eol_flag)
23285 {
23286 /* Mention the EOL conversion if it is not the usual one. */
23287 if (STRINGP (eoltype))
23288 {
23289 eol_str = SDATA (eoltype);
23290 eol_str_len = SBYTES (eoltype);
23291 }
23292 else if (CHARACTERP (eoltype))
23293 {
23294 int c = XFASTINT (eoltype);
23295 return buf + CHAR_STRING (c, (unsigned char *) buf);
23296 }
23297 else
23298 {
23299 eol_str = invalid_eol_type;
23300 eol_str_len = sizeof (invalid_eol_type) - 1;
23301 }
23302 memcpy (buf, eol_str, eol_str_len);
23303 buf += eol_str_len;
23304 }
23305
23306 return buf;
23307 }
23308
23309 /* Return a string for the output of a mode line %-spec for window W,
23310 generated by character C. FIELD_WIDTH > 0 means pad the string
23311 returned with spaces to that value. Return a Lisp string in
23312 *STRING if the resulting string is taken from that Lisp string.
23313
23314 Note we operate on the current buffer for most purposes. */
23315
23316 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23317
23318 static const char *
23319 decode_mode_spec (struct window *w, register int c, int field_width,
23320 Lisp_Object *string)
23321 {
23322 Lisp_Object obj;
23323 struct frame *f = XFRAME (WINDOW_FRAME (w));
23324 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23325 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23326 produce strings from numerical values, so limit preposterously
23327 large values of FIELD_WIDTH to avoid overrunning the buffer's
23328 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23329 bytes plus the terminating null. */
23330 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23331 struct buffer *b = current_buffer;
23332
23333 obj = Qnil;
23334 *string = Qnil;
23335
23336 switch (c)
23337 {
23338 case '*':
23339 if (!NILP (BVAR (b, read_only)))
23340 return "%";
23341 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23342 return "*";
23343 return "-";
23344
23345 case '+':
23346 /* This differs from %* only for a modified read-only buffer. */
23347 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23348 return "*";
23349 if (!NILP (BVAR (b, read_only)))
23350 return "%";
23351 return "-";
23352
23353 case '&':
23354 /* This differs from %* in ignoring read-only-ness. */
23355 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23356 return "*";
23357 return "-";
23358
23359 case '%':
23360 return "%";
23361
23362 case '[':
23363 {
23364 int i;
23365 char *p;
23366
23367 if (command_loop_level > 5)
23368 return "[[[... ";
23369 p = decode_mode_spec_buf;
23370 for (i = 0; i < command_loop_level; i++)
23371 *p++ = '[';
23372 *p = 0;
23373 return decode_mode_spec_buf;
23374 }
23375
23376 case ']':
23377 {
23378 int i;
23379 char *p;
23380
23381 if (command_loop_level > 5)
23382 return " ...]]]";
23383 p = decode_mode_spec_buf;
23384 for (i = 0; i < command_loop_level; i++)
23385 *p++ = ']';
23386 *p = 0;
23387 return decode_mode_spec_buf;
23388 }
23389
23390 case '-':
23391 {
23392 register int i;
23393
23394 /* Let lots_of_dashes be a string of infinite length. */
23395 if (mode_line_target == MODE_LINE_NOPROP
23396 || mode_line_target == MODE_LINE_STRING)
23397 return "--";
23398 if (field_width <= 0
23399 || field_width > sizeof (lots_of_dashes))
23400 {
23401 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23402 decode_mode_spec_buf[i] = '-';
23403 decode_mode_spec_buf[i] = '\0';
23404 return decode_mode_spec_buf;
23405 }
23406 else
23407 return lots_of_dashes;
23408 }
23409
23410 case 'b':
23411 obj = BVAR (b, name);
23412 break;
23413
23414 case 'c':
23415 /* %c and %l are ignored in `frame-title-format'.
23416 (In redisplay_internal, the frame title is drawn _before_ the
23417 windows are updated, so the stuff which depends on actual
23418 window contents (such as %l) may fail to render properly, or
23419 even crash emacs.) */
23420 if (mode_line_target == MODE_LINE_TITLE)
23421 return "";
23422 else
23423 {
23424 ptrdiff_t col = current_column ();
23425 w->column_number_displayed = col;
23426 pint2str (decode_mode_spec_buf, width, col);
23427 return decode_mode_spec_buf;
23428 }
23429
23430 case 'e':
23431 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23432 {
23433 if (NILP (Vmemory_full))
23434 return "";
23435 else
23436 return "!MEM FULL! ";
23437 }
23438 #else
23439 return "";
23440 #endif
23441
23442 case 'F':
23443 /* %F displays the frame name. */
23444 if (!NILP (f->title))
23445 return SSDATA (f->title);
23446 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23447 return SSDATA (f->name);
23448 return "Emacs";
23449
23450 case 'f':
23451 obj = BVAR (b, filename);
23452 break;
23453
23454 case 'i':
23455 {
23456 ptrdiff_t size = ZV - BEGV;
23457 pint2str (decode_mode_spec_buf, width, size);
23458 return decode_mode_spec_buf;
23459 }
23460
23461 case 'I':
23462 {
23463 ptrdiff_t size = ZV - BEGV;
23464 pint2hrstr (decode_mode_spec_buf, width, size);
23465 return decode_mode_spec_buf;
23466 }
23467
23468 case 'l':
23469 {
23470 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23471 ptrdiff_t topline, nlines, height;
23472 ptrdiff_t junk;
23473
23474 /* %c and %l are ignored in `frame-title-format'. */
23475 if (mode_line_target == MODE_LINE_TITLE)
23476 return "";
23477
23478 startpos = marker_position (w->start);
23479 startpos_byte = marker_byte_position (w->start);
23480 height = WINDOW_TOTAL_LINES (w);
23481
23482 /* If we decided that this buffer isn't suitable for line numbers,
23483 don't forget that too fast. */
23484 if (w->base_line_pos == -1)
23485 goto no_value;
23486
23487 /* If the buffer is very big, don't waste time. */
23488 if (INTEGERP (Vline_number_display_limit)
23489 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23490 {
23491 w->base_line_pos = 0;
23492 w->base_line_number = 0;
23493 goto no_value;
23494 }
23495
23496 if (w->base_line_number > 0
23497 && w->base_line_pos > 0
23498 && w->base_line_pos <= startpos)
23499 {
23500 line = w->base_line_number;
23501 linepos = w->base_line_pos;
23502 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23503 }
23504 else
23505 {
23506 line = 1;
23507 linepos = BUF_BEGV (b);
23508 linepos_byte = BUF_BEGV_BYTE (b);
23509 }
23510
23511 /* Count lines from base line to window start position. */
23512 nlines = display_count_lines (linepos_byte,
23513 startpos_byte,
23514 startpos, &junk);
23515
23516 topline = nlines + line;
23517
23518 /* Determine a new base line, if the old one is too close
23519 or too far away, or if we did not have one.
23520 "Too close" means it's plausible a scroll-down would
23521 go back past it. */
23522 if (startpos == BUF_BEGV (b))
23523 {
23524 w->base_line_number = topline;
23525 w->base_line_pos = BUF_BEGV (b);
23526 }
23527 else if (nlines < height + 25 || nlines > height * 3 + 50
23528 || linepos == BUF_BEGV (b))
23529 {
23530 ptrdiff_t limit = BUF_BEGV (b);
23531 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23532 ptrdiff_t position;
23533 ptrdiff_t distance =
23534 (height * 2 + 30) * line_number_display_limit_width;
23535
23536 if (startpos - distance > limit)
23537 {
23538 limit = startpos - distance;
23539 limit_byte = CHAR_TO_BYTE (limit);
23540 }
23541
23542 nlines = display_count_lines (startpos_byte,
23543 limit_byte,
23544 - (height * 2 + 30),
23545 &position);
23546 /* If we couldn't find the lines we wanted within
23547 line_number_display_limit_width chars per line,
23548 give up on line numbers for this window. */
23549 if (position == limit_byte && limit == startpos - distance)
23550 {
23551 w->base_line_pos = -1;
23552 w->base_line_number = 0;
23553 goto no_value;
23554 }
23555
23556 w->base_line_number = topline - nlines;
23557 w->base_line_pos = BYTE_TO_CHAR (position);
23558 }
23559
23560 /* Now count lines from the start pos to point. */
23561 nlines = display_count_lines (startpos_byte,
23562 PT_BYTE, PT, &junk);
23563
23564 /* Record that we did display the line number. */
23565 line_number_displayed = true;
23566
23567 /* Make the string to show. */
23568 pint2str (decode_mode_spec_buf, width, topline + nlines);
23569 return decode_mode_spec_buf;
23570 no_value:
23571 {
23572 char *p = decode_mode_spec_buf;
23573 int pad = width - 2;
23574 while (pad-- > 0)
23575 *p++ = ' ';
23576 *p++ = '?';
23577 *p++ = '?';
23578 *p = '\0';
23579 return decode_mode_spec_buf;
23580 }
23581 }
23582 break;
23583
23584 case 'm':
23585 obj = BVAR (b, mode_name);
23586 break;
23587
23588 case 'n':
23589 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23590 return " Narrow";
23591 break;
23592
23593 case 'p':
23594 {
23595 ptrdiff_t pos = marker_position (w->start);
23596 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23597
23598 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23599 {
23600 if (pos <= BUF_BEGV (b))
23601 return "All";
23602 else
23603 return "Bottom";
23604 }
23605 else if (pos <= BUF_BEGV (b))
23606 return "Top";
23607 else
23608 {
23609 if (total > 1000000)
23610 /* Do it differently for a large value, to avoid overflow. */
23611 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23612 else
23613 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23614 /* We can't normally display a 3-digit number,
23615 so get us a 2-digit number that is close. */
23616 if (total == 100)
23617 total = 99;
23618 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23619 return decode_mode_spec_buf;
23620 }
23621 }
23622
23623 /* Display percentage of size above the bottom of the screen. */
23624 case 'P':
23625 {
23626 ptrdiff_t toppos = marker_position (w->start);
23627 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23628 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23629
23630 if (botpos >= BUF_ZV (b))
23631 {
23632 if (toppos <= BUF_BEGV (b))
23633 return "All";
23634 else
23635 return "Bottom";
23636 }
23637 else
23638 {
23639 if (total > 1000000)
23640 /* Do it differently for a large value, to avoid overflow. */
23641 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23642 else
23643 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23644 /* We can't normally display a 3-digit number,
23645 so get us a 2-digit number that is close. */
23646 if (total == 100)
23647 total = 99;
23648 if (toppos <= BUF_BEGV (b))
23649 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23650 else
23651 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23652 return decode_mode_spec_buf;
23653 }
23654 }
23655
23656 case 's':
23657 /* status of process */
23658 obj = Fget_buffer_process (Fcurrent_buffer ());
23659 if (NILP (obj))
23660 return "no process";
23661 #ifndef MSDOS
23662 obj = Fsymbol_name (Fprocess_status (obj));
23663 #endif
23664 break;
23665
23666 case '@':
23667 {
23668 ptrdiff_t count = inhibit_garbage_collection ();
23669 Lisp_Object curdir = BVAR (current_buffer, directory);
23670 Lisp_Object val = Qnil;
23671
23672 if (STRINGP (curdir))
23673 val = call1 (intern ("file-remote-p"), curdir);
23674
23675 unbind_to (count, Qnil);
23676
23677 if (NILP (val))
23678 return "-";
23679 else
23680 return "@";
23681 }
23682
23683 case 'z':
23684 /* coding-system (not including end-of-line format) */
23685 case 'Z':
23686 /* coding-system (including end-of-line type) */
23687 {
23688 bool eol_flag = (c == 'Z');
23689 char *p = decode_mode_spec_buf;
23690
23691 if (! FRAME_WINDOW_P (f))
23692 {
23693 /* No need to mention EOL here--the terminal never needs
23694 to do EOL conversion. */
23695 p = decode_mode_spec_coding (CODING_ID_NAME
23696 (FRAME_KEYBOARD_CODING (f)->id),
23697 p, false);
23698 p = decode_mode_spec_coding (CODING_ID_NAME
23699 (FRAME_TERMINAL_CODING (f)->id),
23700 p, false);
23701 }
23702 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23703 p, eol_flag);
23704
23705 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23706 #ifdef subprocesses
23707 obj = Fget_buffer_process (Fcurrent_buffer ());
23708 if (PROCESSP (obj))
23709 {
23710 p = decode_mode_spec_coding
23711 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23712 p = decode_mode_spec_coding
23713 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23714 }
23715 #endif /* subprocesses */
23716 #endif /* false */
23717 *p = 0;
23718 return decode_mode_spec_buf;
23719 }
23720 }
23721
23722 if (STRINGP (obj))
23723 {
23724 *string = obj;
23725 return SSDATA (obj);
23726 }
23727 else
23728 return "";
23729 }
23730
23731
23732 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23733 means count lines back from START_BYTE. But don't go beyond
23734 LIMIT_BYTE. Return the number of lines thus found (always
23735 nonnegative).
23736
23737 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23738 either the position COUNT lines after/before START_BYTE, if we
23739 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23740 COUNT lines. */
23741
23742 static ptrdiff_t
23743 display_count_lines (ptrdiff_t start_byte,
23744 ptrdiff_t limit_byte, ptrdiff_t count,
23745 ptrdiff_t *byte_pos_ptr)
23746 {
23747 register unsigned char *cursor;
23748 unsigned char *base;
23749
23750 register ptrdiff_t ceiling;
23751 register unsigned char *ceiling_addr;
23752 ptrdiff_t orig_count = count;
23753
23754 /* If we are not in selective display mode,
23755 check only for newlines. */
23756 bool selective_display
23757 = (!NILP (BVAR (current_buffer, selective_display))
23758 && !INTEGERP (BVAR (current_buffer, selective_display)));
23759
23760 if (count > 0)
23761 {
23762 while (start_byte < limit_byte)
23763 {
23764 ceiling = BUFFER_CEILING_OF (start_byte);
23765 ceiling = min (limit_byte - 1, ceiling);
23766 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23767 base = (cursor = BYTE_POS_ADDR (start_byte));
23768
23769 do
23770 {
23771 if (selective_display)
23772 {
23773 while (*cursor != '\n' && *cursor != 015
23774 && ++cursor != ceiling_addr)
23775 continue;
23776 if (cursor == ceiling_addr)
23777 break;
23778 }
23779 else
23780 {
23781 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23782 if (! cursor)
23783 break;
23784 }
23785
23786 cursor++;
23787
23788 if (--count == 0)
23789 {
23790 start_byte += cursor - base;
23791 *byte_pos_ptr = start_byte;
23792 return orig_count;
23793 }
23794 }
23795 while (cursor < ceiling_addr);
23796
23797 start_byte += ceiling_addr - base;
23798 }
23799 }
23800 else
23801 {
23802 while (start_byte > limit_byte)
23803 {
23804 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23805 ceiling = max (limit_byte, ceiling);
23806 ceiling_addr = BYTE_POS_ADDR (ceiling);
23807 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23808 while (true)
23809 {
23810 if (selective_display)
23811 {
23812 while (--cursor >= ceiling_addr
23813 && *cursor != '\n' && *cursor != 015)
23814 continue;
23815 if (cursor < ceiling_addr)
23816 break;
23817 }
23818 else
23819 {
23820 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23821 if (! cursor)
23822 break;
23823 }
23824
23825 if (++count == 0)
23826 {
23827 start_byte += cursor - base + 1;
23828 *byte_pos_ptr = start_byte;
23829 /* When scanning backwards, we should
23830 not count the newline posterior to which we stop. */
23831 return - orig_count - 1;
23832 }
23833 }
23834 start_byte += ceiling_addr - base;
23835 }
23836 }
23837
23838 *byte_pos_ptr = limit_byte;
23839
23840 if (count < 0)
23841 return - orig_count + count;
23842 return orig_count - count;
23843
23844 }
23845
23846
23847 \f
23848 /***********************************************************************
23849 Displaying strings
23850 ***********************************************************************/
23851
23852 /* Display a NUL-terminated string, starting with index START.
23853
23854 If STRING is non-null, display that C string. Otherwise, the Lisp
23855 string LISP_STRING is displayed. There's a case that STRING is
23856 non-null and LISP_STRING is not nil. It means STRING is a string
23857 data of LISP_STRING. In that case, we display LISP_STRING while
23858 ignoring its text properties.
23859
23860 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23861 FACE_STRING. Display STRING or LISP_STRING with the face at
23862 FACE_STRING_POS in FACE_STRING:
23863
23864 Display the string in the environment given by IT, but use the
23865 standard display table, temporarily.
23866
23867 FIELD_WIDTH is the minimum number of output glyphs to produce.
23868 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23869 with spaces. If STRING has more characters, more than FIELD_WIDTH
23870 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23871
23872 PRECISION is the maximum number of characters to output from
23873 STRING. PRECISION < 0 means don't truncate the string.
23874
23875 This is roughly equivalent to printf format specifiers:
23876
23877 FIELD_WIDTH PRECISION PRINTF
23878 ----------------------------------------
23879 -1 -1 %s
23880 -1 10 %.10s
23881 10 -1 %10s
23882 20 10 %20.10s
23883
23884 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23885 display them, and < 0 means obey the current buffer's value of
23886 enable_multibyte_characters.
23887
23888 Value is the number of columns displayed. */
23889
23890 static int
23891 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23892 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23893 int field_width, int precision, int max_x, int multibyte)
23894 {
23895 int hpos_at_start = it->hpos;
23896 int saved_face_id = it->face_id;
23897 struct glyph_row *row = it->glyph_row;
23898 ptrdiff_t it_charpos;
23899
23900 /* Initialize the iterator IT for iteration over STRING beginning
23901 with index START. */
23902 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23903 precision, field_width, multibyte);
23904 if (string && STRINGP (lisp_string))
23905 /* LISP_STRING is the one returned by decode_mode_spec. We should
23906 ignore its text properties. */
23907 it->stop_charpos = it->end_charpos;
23908
23909 /* If displaying STRING, set up the face of the iterator from
23910 FACE_STRING, if that's given. */
23911 if (STRINGP (face_string))
23912 {
23913 ptrdiff_t endptr;
23914 struct face *face;
23915
23916 it->face_id
23917 = face_at_string_position (it->w, face_string, face_string_pos,
23918 0, &endptr, it->base_face_id, false);
23919 face = FACE_FROM_ID (it->f, it->face_id);
23920 it->face_box_p = face->box != FACE_NO_BOX;
23921 }
23922
23923 /* Set max_x to the maximum allowed X position. Don't let it go
23924 beyond the right edge of the window. */
23925 if (max_x <= 0)
23926 max_x = it->last_visible_x;
23927 else
23928 max_x = min (max_x, it->last_visible_x);
23929
23930 /* Skip over display elements that are not visible. because IT->w is
23931 hscrolled. */
23932 if (it->current_x < it->first_visible_x)
23933 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23934 MOVE_TO_POS | MOVE_TO_X);
23935
23936 row->ascent = it->max_ascent;
23937 row->height = it->max_ascent + it->max_descent;
23938 row->phys_ascent = it->max_phys_ascent;
23939 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23940 row->extra_line_spacing = it->max_extra_line_spacing;
23941
23942 if (STRINGP (it->string))
23943 it_charpos = IT_STRING_CHARPOS (*it);
23944 else
23945 it_charpos = IT_CHARPOS (*it);
23946
23947 /* This condition is for the case that we are called with current_x
23948 past last_visible_x. */
23949 while (it->current_x < max_x)
23950 {
23951 int x_before, x, n_glyphs_before, i, nglyphs;
23952
23953 /* Get the next display element. */
23954 if (!get_next_display_element (it))
23955 break;
23956
23957 /* Produce glyphs. */
23958 x_before = it->current_x;
23959 n_glyphs_before = row->used[TEXT_AREA];
23960 PRODUCE_GLYPHS (it);
23961
23962 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23963 i = 0;
23964 x = x_before;
23965 while (i < nglyphs)
23966 {
23967 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23968
23969 if (it->line_wrap != TRUNCATE
23970 && x + glyph->pixel_width > max_x)
23971 {
23972 /* End of continued line or max_x reached. */
23973 if (CHAR_GLYPH_PADDING_P (*glyph))
23974 {
23975 /* A wide character is unbreakable. */
23976 if (row->reversed_p)
23977 unproduce_glyphs (it, row->used[TEXT_AREA]
23978 - n_glyphs_before);
23979 row->used[TEXT_AREA] = n_glyphs_before;
23980 it->current_x = x_before;
23981 }
23982 else
23983 {
23984 if (row->reversed_p)
23985 unproduce_glyphs (it, row->used[TEXT_AREA]
23986 - (n_glyphs_before + i));
23987 row->used[TEXT_AREA] = n_glyphs_before + i;
23988 it->current_x = x;
23989 }
23990 break;
23991 }
23992 else if (x + glyph->pixel_width >= it->first_visible_x)
23993 {
23994 /* Glyph is at least partially visible. */
23995 ++it->hpos;
23996 if (x < it->first_visible_x)
23997 row->x = x - it->first_visible_x;
23998 }
23999 else
24000 {
24001 /* Glyph is off the left margin of the display area.
24002 Should not happen. */
24003 emacs_abort ();
24004 }
24005
24006 row->ascent = max (row->ascent, it->max_ascent);
24007 row->height = max (row->height, it->max_ascent + it->max_descent);
24008 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24009 row->phys_height = max (row->phys_height,
24010 it->max_phys_ascent + it->max_phys_descent);
24011 row->extra_line_spacing = max (row->extra_line_spacing,
24012 it->max_extra_line_spacing);
24013 x += glyph->pixel_width;
24014 ++i;
24015 }
24016
24017 /* Stop if max_x reached. */
24018 if (i < nglyphs)
24019 break;
24020
24021 /* Stop at line ends. */
24022 if (ITERATOR_AT_END_OF_LINE_P (it))
24023 {
24024 it->continuation_lines_width = 0;
24025 break;
24026 }
24027
24028 set_iterator_to_next (it, true);
24029 if (STRINGP (it->string))
24030 it_charpos = IT_STRING_CHARPOS (*it);
24031 else
24032 it_charpos = IT_CHARPOS (*it);
24033
24034 /* Stop if truncating at the right edge. */
24035 if (it->line_wrap == TRUNCATE
24036 && it->current_x >= it->last_visible_x)
24037 {
24038 /* Add truncation mark, but don't do it if the line is
24039 truncated at a padding space. */
24040 if (it_charpos < it->string_nchars)
24041 {
24042 if (!FRAME_WINDOW_P (it->f))
24043 {
24044 int ii, n;
24045
24046 if (it->current_x > it->last_visible_x)
24047 {
24048 if (!row->reversed_p)
24049 {
24050 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24051 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24052 break;
24053 }
24054 else
24055 {
24056 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24057 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24058 break;
24059 unproduce_glyphs (it, ii + 1);
24060 ii = row->used[TEXT_AREA] - (ii + 1);
24061 }
24062 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24063 {
24064 row->used[TEXT_AREA] = ii;
24065 produce_special_glyphs (it, IT_TRUNCATION);
24066 }
24067 }
24068 produce_special_glyphs (it, IT_TRUNCATION);
24069 }
24070 row->truncated_on_right_p = true;
24071 }
24072 break;
24073 }
24074 }
24075
24076 /* Maybe insert a truncation at the left. */
24077 if (it->first_visible_x
24078 && it_charpos > 0)
24079 {
24080 if (!FRAME_WINDOW_P (it->f)
24081 || (row->reversed_p
24082 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24083 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24084 insert_left_trunc_glyphs (it);
24085 row->truncated_on_left_p = true;
24086 }
24087
24088 it->face_id = saved_face_id;
24089
24090 /* Value is number of columns displayed. */
24091 return it->hpos - hpos_at_start;
24092 }
24093
24094
24095 \f
24096 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24097 appears as an element of LIST or as the car of an element of LIST.
24098 If PROPVAL is a list, compare each element against LIST in that
24099 way, and return 1/2 if any element of PROPVAL is found in LIST.
24100 Otherwise return 0. This function cannot quit.
24101 The return value is 2 if the text is invisible but with an ellipsis
24102 and 1 if it's invisible and without an ellipsis. */
24103
24104 int
24105 invisible_prop (Lisp_Object propval, Lisp_Object list)
24106 {
24107 Lisp_Object tail, proptail;
24108
24109 for (tail = list; CONSP (tail); tail = XCDR (tail))
24110 {
24111 register Lisp_Object tem;
24112 tem = XCAR (tail);
24113 if (EQ (propval, tem))
24114 return 1;
24115 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24116 return NILP (XCDR (tem)) ? 1 : 2;
24117 }
24118
24119 if (CONSP (propval))
24120 {
24121 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
24122 {
24123 Lisp_Object propelt;
24124 propelt = XCAR (proptail);
24125 for (tail = list; CONSP (tail); tail = XCDR (tail))
24126 {
24127 register Lisp_Object tem;
24128 tem = XCAR (tail);
24129 if (EQ (propelt, tem))
24130 return 1;
24131 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
24132 return NILP (XCDR (tem)) ? 1 : 2;
24133 }
24134 }
24135 }
24136
24137 return 0;
24138 }
24139
24140 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
24141 doc: /* Non-nil if the property makes the text invisible.
24142 POS-OR-PROP can be a marker or number, in which case it is taken to be
24143 a position in the current buffer and the value of the `invisible' property
24144 is checked; or it can be some other value, which is then presumed to be the
24145 value of the `invisible' property of the text of interest.
24146 The non-nil value returned can be t for truly invisible text or something
24147 else if the text is replaced by an ellipsis. */)
24148 (Lisp_Object pos_or_prop)
24149 {
24150 Lisp_Object prop
24151 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
24152 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
24153 : pos_or_prop);
24154 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
24155 return (invis == 0 ? Qnil
24156 : invis == 1 ? Qt
24157 : make_number (invis));
24158 }
24159
24160 /* Calculate a width or height in pixels from a specification using
24161 the following elements:
24162
24163 SPEC ::=
24164 NUM - a (fractional) multiple of the default font width/height
24165 (NUM) - specifies exactly NUM pixels
24166 UNIT - a fixed number of pixels, see below.
24167 ELEMENT - size of a display element in pixels, see below.
24168 (NUM . SPEC) - equals NUM * SPEC
24169 (+ SPEC SPEC ...) - add pixel values
24170 (- SPEC SPEC ...) - subtract pixel values
24171 (- SPEC) - negate pixel value
24172
24173 NUM ::=
24174 INT or FLOAT - a number constant
24175 SYMBOL - use symbol's (buffer local) variable binding.
24176
24177 UNIT ::=
24178 in - pixels per inch *)
24179 mm - pixels per 1/1000 meter *)
24180 cm - pixels per 1/100 meter *)
24181 width - width of current font in pixels.
24182 height - height of current font in pixels.
24183
24184 *) using the ratio(s) defined in display-pixels-per-inch.
24185
24186 ELEMENT ::=
24187
24188 left-fringe - left fringe width in pixels
24189 right-fringe - right fringe width in pixels
24190
24191 left-margin - left margin width in pixels
24192 right-margin - right margin width in pixels
24193
24194 scroll-bar - scroll-bar area width in pixels
24195
24196 Examples:
24197
24198 Pixels corresponding to 5 inches:
24199 (5 . in)
24200
24201 Total width of non-text areas on left side of window (if scroll-bar is on left):
24202 '(space :width (+ left-fringe left-margin scroll-bar))
24203
24204 Align to first text column (in header line):
24205 '(space :align-to 0)
24206
24207 Align to middle of text area minus half the width of variable `my-image'
24208 containing a loaded image:
24209 '(space :align-to (0.5 . (- text my-image)))
24210
24211 Width of left margin minus width of 1 character in the default font:
24212 '(space :width (- left-margin 1))
24213
24214 Width of left margin minus width of 2 characters in the current font:
24215 '(space :width (- left-margin (2 . width)))
24216
24217 Center 1 character over left-margin (in header line):
24218 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24219
24220 Different ways to express width of left fringe plus left margin minus one pixel:
24221 '(space :width (- (+ left-fringe left-margin) (1)))
24222 '(space :width (+ left-fringe left-margin (- (1))))
24223 '(space :width (+ left-fringe left-margin (-1)))
24224
24225 */
24226
24227 static bool
24228 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24229 struct font *font, bool width_p, int *align_to)
24230 {
24231 double pixels;
24232
24233 # define OK_PIXELS(val) (*res = (val), true)
24234 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24235
24236 if (NILP (prop))
24237 return OK_PIXELS (0);
24238
24239 eassert (FRAME_LIVE_P (it->f));
24240
24241 if (SYMBOLP (prop))
24242 {
24243 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24244 {
24245 char *unit = SSDATA (SYMBOL_NAME (prop));
24246
24247 if (unit[0] == 'i' && unit[1] == 'n')
24248 pixels = 1.0;
24249 else if (unit[0] == 'm' && unit[1] == 'm')
24250 pixels = 25.4;
24251 else if (unit[0] == 'c' && unit[1] == 'm')
24252 pixels = 2.54;
24253 else
24254 pixels = 0;
24255 if (pixels > 0)
24256 {
24257 double ppi = (width_p ? FRAME_RES_X (it->f)
24258 : FRAME_RES_Y (it->f));
24259
24260 if (ppi > 0)
24261 return OK_PIXELS (ppi / pixels);
24262 return false;
24263 }
24264 }
24265
24266 #ifdef HAVE_WINDOW_SYSTEM
24267 if (EQ (prop, Qheight))
24268 return OK_PIXELS (font
24269 ? normal_char_height (font, -1)
24270 : FRAME_LINE_HEIGHT (it->f));
24271 if (EQ (prop, Qwidth))
24272 return OK_PIXELS (font
24273 ? FONT_WIDTH (font)
24274 : FRAME_COLUMN_WIDTH (it->f));
24275 #else
24276 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24277 return OK_PIXELS (1);
24278 #endif
24279
24280 if (EQ (prop, Qtext))
24281 return OK_PIXELS (width_p
24282 ? window_box_width (it->w, TEXT_AREA)
24283 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24284
24285 if (align_to && *align_to < 0)
24286 {
24287 *res = 0;
24288 if (EQ (prop, Qleft))
24289 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24290 if (EQ (prop, Qright))
24291 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24292 if (EQ (prop, Qcenter))
24293 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24294 + window_box_width (it->w, TEXT_AREA) / 2);
24295 if (EQ (prop, Qleft_fringe))
24296 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24297 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24298 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24299 if (EQ (prop, Qright_fringe))
24300 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24301 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24302 : window_box_right_offset (it->w, TEXT_AREA));
24303 if (EQ (prop, Qleft_margin))
24304 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24305 if (EQ (prop, Qright_margin))
24306 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24307 if (EQ (prop, Qscroll_bar))
24308 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24309 ? 0
24310 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24311 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24312 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24313 : 0)));
24314 }
24315 else
24316 {
24317 if (EQ (prop, Qleft_fringe))
24318 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24319 if (EQ (prop, Qright_fringe))
24320 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24321 if (EQ (prop, Qleft_margin))
24322 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24323 if (EQ (prop, Qright_margin))
24324 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24325 if (EQ (prop, Qscroll_bar))
24326 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24327 }
24328
24329 prop = buffer_local_value (prop, it->w->contents);
24330 if (EQ (prop, Qunbound))
24331 prop = Qnil;
24332 }
24333
24334 if (NUMBERP (prop))
24335 {
24336 int base_unit = (width_p
24337 ? FRAME_COLUMN_WIDTH (it->f)
24338 : FRAME_LINE_HEIGHT (it->f));
24339 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24340 }
24341
24342 if (CONSP (prop))
24343 {
24344 Lisp_Object car = XCAR (prop);
24345 Lisp_Object cdr = XCDR (prop);
24346
24347 if (SYMBOLP (car))
24348 {
24349 #ifdef HAVE_WINDOW_SYSTEM
24350 if (FRAME_WINDOW_P (it->f)
24351 && valid_image_p (prop))
24352 {
24353 ptrdiff_t id = lookup_image (it->f, prop);
24354 struct image *img = IMAGE_FROM_ID (it->f, id);
24355
24356 return OK_PIXELS (width_p ? img->width : img->height);
24357 }
24358 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
24359 {
24360 // TODO: Don't return dummy size.
24361 return OK_PIXELS (100);
24362 }
24363 #endif
24364 if (EQ (car, Qplus) || EQ (car, Qminus))
24365 {
24366 bool first = true;
24367 double px;
24368
24369 pixels = 0;
24370 while (CONSP (cdr))
24371 {
24372 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24373 font, width_p, align_to))
24374 return false;
24375 if (first)
24376 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24377 else
24378 pixels += px;
24379 cdr = XCDR (cdr);
24380 }
24381 if (EQ (car, Qminus))
24382 pixels = -pixels;
24383 return OK_PIXELS (pixels);
24384 }
24385
24386 car = buffer_local_value (car, it->w->contents);
24387 if (EQ (car, Qunbound))
24388 car = Qnil;
24389 }
24390
24391 if (NUMBERP (car))
24392 {
24393 double fact;
24394 pixels = XFLOATINT (car);
24395 if (NILP (cdr))
24396 return OK_PIXELS (pixels);
24397 if (calc_pixel_width_or_height (&fact, it, cdr,
24398 font, width_p, align_to))
24399 return OK_PIXELS (pixels * fact);
24400 return false;
24401 }
24402
24403 return false;
24404 }
24405
24406 return false;
24407 }
24408
24409 void
24410 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24411 {
24412 #ifdef HAVE_WINDOW_SYSTEM
24413 normal_char_ascent_descent (font, -1, ascent, descent);
24414 #else
24415 *ascent = 1;
24416 *descent = 0;
24417 #endif
24418 }
24419
24420 \f
24421 /***********************************************************************
24422 Glyph Display
24423 ***********************************************************************/
24424
24425 #ifdef HAVE_WINDOW_SYSTEM
24426
24427 #ifdef GLYPH_DEBUG
24428
24429 void
24430 dump_glyph_string (struct glyph_string *s)
24431 {
24432 fprintf (stderr, "glyph string\n");
24433 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24434 s->x, s->y, s->width, s->height);
24435 fprintf (stderr, " ybase = %d\n", s->ybase);
24436 fprintf (stderr, " hl = %d\n", s->hl);
24437 fprintf (stderr, " left overhang = %d, right = %d\n",
24438 s->left_overhang, s->right_overhang);
24439 fprintf (stderr, " nchars = %d\n", s->nchars);
24440 fprintf (stderr, " extends to end of line = %d\n",
24441 s->extends_to_end_of_line_p);
24442 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24443 fprintf (stderr, " bg width = %d\n", s->background_width);
24444 }
24445
24446 #endif /* GLYPH_DEBUG */
24447
24448 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24449 of XChar2b structures for S; it can't be allocated in
24450 init_glyph_string because it must be allocated via `alloca'. W
24451 is the window on which S is drawn. ROW and AREA are the glyph row
24452 and area within the row from which S is constructed. START is the
24453 index of the first glyph structure covered by S. HL is a
24454 face-override for drawing S. */
24455
24456 #ifdef HAVE_NTGUI
24457 #define OPTIONAL_HDC(hdc) HDC hdc,
24458 #define DECLARE_HDC(hdc) HDC hdc;
24459 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24460 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24461 #endif
24462
24463 #ifndef OPTIONAL_HDC
24464 #define OPTIONAL_HDC(hdc)
24465 #define DECLARE_HDC(hdc)
24466 #define ALLOCATE_HDC(hdc, f)
24467 #define RELEASE_HDC(hdc, f)
24468 #endif
24469
24470 static void
24471 init_glyph_string (struct glyph_string *s,
24472 OPTIONAL_HDC (hdc)
24473 XChar2b *char2b, struct window *w, struct glyph_row *row,
24474 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24475 {
24476 memset (s, 0, sizeof *s);
24477 s->w = w;
24478 s->f = XFRAME (w->frame);
24479 #ifdef HAVE_NTGUI
24480 s->hdc = hdc;
24481 #endif
24482 s->display = FRAME_X_DISPLAY (s->f);
24483 s->window = FRAME_X_WINDOW (s->f);
24484 s->char2b = char2b;
24485 s->hl = hl;
24486 s->row = row;
24487 s->area = area;
24488 s->first_glyph = row->glyphs[area] + start;
24489 s->height = row->height;
24490 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24491 s->ybase = s->y + row->ascent;
24492 }
24493
24494
24495 /* Append the list of glyph strings with head H and tail T to the list
24496 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24497
24498 static void
24499 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24500 struct glyph_string *h, struct glyph_string *t)
24501 {
24502 if (h)
24503 {
24504 if (*head)
24505 (*tail)->next = h;
24506 else
24507 *head = h;
24508 h->prev = *tail;
24509 *tail = t;
24510 }
24511 }
24512
24513
24514 /* Prepend the list of glyph strings with head H and tail T to the
24515 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24516 result. */
24517
24518 static void
24519 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24520 struct glyph_string *h, struct glyph_string *t)
24521 {
24522 if (h)
24523 {
24524 if (*head)
24525 (*head)->prev = t;
24526 else
24527 *tail = t;
24528 t->next = *head;
24529 *head = h;
24530 }
24531 }
24532
24533
24534 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24535 Set *HEAD and *TAIL to the resulting list. */
24536
24537 static void
24538 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24539 struct glyph_string *s)
24540 {
24541 s->next = s->prev = NULL;
24542 append_glyph_string_lists (head, tail, s, s);
24543 }
24544
24545
24546 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24547 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24548 make sure that X resources for the face returned are allocated.
24549 Value is a pointer to a realized face that is ready for display if
24550 DISPLAY_P. */
24551
24552 static struct face *
24553 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24554 XChar2b *char2b, bool display_p)
24555 {
24556 struct face *face = FACE_FROM_ID (f, face_id);
24557 unsigned code = 0;
24558
24559 if (face->font)
24560 {
24561 code = face->font->driver->encode_char (face->font, c);
24562
24563 if (code == FONT_INVALID_CODE)
24564 code = 0;
24565 }
24566 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24567
24568 /* Make sure X resources of the face are allocated. */
24569 #ifdef HAVE_X_WINDOWS
24570 if (display_p)
24571 #endif
24572 {
24573 eassert (face != NULL);
24574 prepare_face_for_display (f, face);
24575 }
24576
24577 return face;
24578 }
24579
24580
24581 /* Get face and two-byte form of character glyph GLYPH on frame F.
24582 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24583 a pointer to a realized face that is ready for display. */
24584
24585 static struct face *
24586 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24587 XChar2b *char2b)
24588 {
24589 struct face *face;
24590 unsigned code = 0;
24591
24592 eassert (glyph->type == CHAR_GLYPH);
24593 face = FACE_FROM_ID (f, glyph->face_id);
24594
24595 /* Make sure X resources of the face are allocated. */
24596 eassert (face != NULL);
24597 prepare_face_for_display (f, face);
24598
24599 if (face->font)
24600 {
24601 if (CHAR_BYTE8_P (glyph->u.ch))
24602 code = CHAR_TO_BYTE8 (glyph->u.ch);
24603 else
24604 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24605
24606 if (code == FONT_INVALID_CODE)
24607 code = 0;
24608 }
24609
24610 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24611 return face;
24612 }
24613
24614
24615 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24616 Return true iff FONT has a glyph for C. */
24617
24618 static bool
24619 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24620 {
24621 unsigned code;
24622
24623 if (CHAR_BYTE8_P (c))
24624 code = CHAR_TO_BYTE8 (c);
24625 else
24626 code = font->driver->encode_char (font, c);
24627
24628 if (code == FONT_INVALID_CODE)
24629 return false;
24630 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24631 return true;
24632 }
24633
24634
24635 /* Fill glyph string S with composition components specified by S->cmp.
24636
24637 BASE_FACE is the base face of the composition.
24638 S->cmp_from is the index of the first component for S.
24639
24640 OVERLAPS non-zero means S should draw the foreground only, and use
24641 its physical height for clipping. See also draw_glyphs.
24642
24643 Value is the index of a component not in S. */
24644
24645 static int
24646 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24647 int overlaps)
24648 {
24649 int i;
24650 /* For all glyphs of this composition, starting at the offset
24651 S->cmp_from, until we reach the end of the definition or encounter a
24652 glyph that requires the different face, add it to S. */
24653 struct face *face;
24654
24655 eassert (s);
24656
24657 s->for_overlaps = overlaps;
24658 s->face = NULL;
24659 s->font = NULL;
24660 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24661 {
24662 int c = COMPOSITION_GLYPH (s->cmp, i);
24663
24664 /* TAB in a composition means display glyphs with padding space
24665 on the left or right. */
24666 if (c != '\t')
24667 {
24668 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24669 -1, Qnil);
24670
24671 face = get_char_face_and_encoding (s->f, c, face_id,
24672 s->char2b + i, true);
24673 if (face)
24674 {
24675 if (! s->face)
24676 {
24677 s->face = face;
24678 s->font = s->face->font;
24679 }
24680 else if (s->face != face)
24681 break;
24682 }
24683 }
24684 ++s->nchars;
24685 }
24686 s->cmp_to = i;
24687
24688 if (s->face == NULL)
24689 {
24690 s->face = base_face->ascii_face;
24691 s->font = s->face->font;
24692 }
24693
24694 /* All glyph strings for the same composition has the same width,
24695 i.e. the width set for the first component of the composition. */
24696 s->width = s->first_glyph->pixel_width;
24697
24698 /* If the specified font could not be loaded, use the frame's
24699 default font, but record the fact that we couldn't load it in
24700 the glyph string so that we can draw rectangles for the
24701 characters of the glyph string. */
24702 if (s->font == NULL)
24703 {
24704 s->font_not_found_p = true;
24705 s->font = FRAME_FONT (s->f);
24706 }
24707
24708 /* Adjust base line for subscript/superscript text. */
24709 s->ybase += s->first_glyph->voffset;
24710
24711 return s->cmp_to;
24712 }
24713
24714 static int
24715 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24716 int start, int end, int overlaps)
24717 {
24718 struct glyph *glyph, *last;
24719 Lisp_Object lgstring;
24720 int i;
24721
24722 s->for_overlaps = overlaps;
24723 glyph = s->row->glyphs[s->area] + start;
24724 last = s->row->glyphs[s->area] + end;
24725 s->cmp_id = glyph->u.cmp.id;
24726 s->cmp_from = glyph->slice.cmp.from;
24727 s->cmp_to = glyph->slice.cmp.to + 1;
24728 s->face = FACE_FROM_ID (s->f, face_id);
24729 lgstring = composition_gstring_from_id (s->cmp_id);
24730 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24731 glyph++;
24732 while (glyph < last
24733 && glyph->u.cmp.automatic
24734 && glyph->u.cmp.id == s->cmp_id
24735 && s->cmp_to == glyph->slice.cmp.from)
24736 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24737
24738 for (i = s->cmp_from; i < s->cmp_to; i++)
24739 {
24740 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24741 unsigned code = LGLYPH_CODE (lglyph);
24742
24743 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24744 }
24745 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24746 return glyph - s->row->glyphs[s->area];
24747 }
24748
24749
24750 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24751 See the comment of fill_glyph_string for arguments.
24752 Value is the index of the first glyph not in S. */
24753
24754
24755 static int
24756 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24757 int start, int end, int overlaps)
24758 {
24759 struct glyph *glyph, *last;
24760 int voffset;
24761
24762 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24763 s->for_overlaps = overlaps;
24764 glyph = s->row->glyphs[s->area] + start;
24765 last = s->row->glyphs[s->area] + end;
24766 voffset = glyph->voffset;
24767 s->face = FACE_FROM_ID (s->f, face_id);
24768 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24769 s->nchars = 1;
24770 s->width = glyph->pixel_width;
24771 glyph++;
24772 while (glyph < last
24773 && glyph->type == GLYPHLESS_GLYPH
24774 && glyph->voffset == voffset
24775 && glyph->face_id == face_id)
24776 {
24777 s->nchars++;
24778 s->width += glyph->pixel_width;
24779 glyph++;
24780 }
24781 s->ybase += voffset;
24782 return glyph - s->row->glyphs[s->area];
24783 }
24784
24785
24786 /* Fill glyph string S from a sequence of character glyphs.
24787
24788 FACE_ID is the face id of the string. START is the index of the
24789 first glyph to consider, END is the index of the last + 1.
24790 OVERLAPS non-zero means S should draw the foreground only, and use
24791 its physical height for clipping. See also draw_glyphs.
24792
24793 Value is the index of the first glyph not in S. */
24794
24795 static int
24796 fill_glyph_string (struct glyph_string *s, int face_id,
24797 int start, int end, int overlaps)
24798 {
24799 struct glyph *glyph, *last;
24800 int voffset;
24801 bool glyph_not_available_p;
24802
24803 eassert (s->f == XFRAME (s->w->frame));
24804 eassert (s->nchars == 0);
24805 eassert (start >= 0 && end > start);
24806
24807 s->for_overlaps = overlaps;
24808 glyph = s->row->glyphs[s->area] + start;
24809 last = s->row->glyphs[s->area] + end;
24810 voffset = glyph->voffset;
24811 s->padding_p = glyph->padding_p;
24812 glyph_not_available_p = glyph->glyph_not_available_p;
24813
24814 while (glyph < last
24815 && glyph->type == CHAR_GLYPH
24816 && glyph->voffset == voffset
24817 /* Same face id implies same font, nowadays. */
24818 && glyph->face_id == face_id
24819 && glyph->glyph_not_available_p == glyph_not_available_p)
24820 {
24821 s->face = get_glyph_face_and_encoding (s->f, glyph,
24822 s->char2b + s->nchars);
24823 ++s->nchars;
24824 eassert (s->nchars <= end - start);
24825 s->width += glyph->pixel_width;
24826 if (glyph++->padding_p != s->padding_p)
24827 break;
24828 }
24829
24830 s->font = s->face->font;
24831
24832 /* If the specified font could not be loaded, use the frame's font,
24833 but record the fact that we couldn't load it in
24834 S->font_not_found_p so that we can draw rectangles for the
24835 characters of the glyph string. */
24836 if (s->font == NULL || glyph_not_available_p)
24837 {
24838 s->font_not_found_p = true;
24839 s->font = FRAME_FONT (s->f);
24840 }
24841
24842 /* Adjust base line for subscript/superscript text. */
24843 s->ybase += voffset;
24844
24845 eassert (s->face && s->face->gc);
24846 return glyph - s->row->glyphs[s->area];
24847 }
24848
24849
24850 /* Fill glyph string S from image glyph S->first_glyph. */
24851
24852 static void
24853 fill_image_glyph_string (struct glyph_string *s)
24854 {
24855 eassert (s->first_glyph->type == IMAGE_GLYPH);
24856 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24857 eassert (s->img);
24858 s->slice = s->first_glyph->slice.img;
24859 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24860 s->font = s->face->font;
24861 s->width = s->first_glyph->pixel_width;
24862
24863 /* Adjust base line for subscript/superscript text. */
24864 s->ybase += s->first_glyph->voffset;
24865 }
24866
24867
24868 #ifdef HAVE_XWIDGETS
24869 static void
24870 fill_xwidget_glyph_string (struct glyph_string *s)
24871 {
24872 eassert (s->first_glyph->type == XWIDGET_GLYPH);
24873 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24874 s->font = s->face->font;
24875 s->width = s->first_glyph->pixel_width;
24876 s->ybase += s->first_glyph->voffset;
24877 s->xwidget = s->first_glyph->u.xwidget;
24878 }
24879 #endif
24880 /* Fill glyph string S from a sequence of stretch glyphs.
24881
24882 START is the index of the first glyph to consider,
24883 END is the index of the last + 1.
24884
24885 Value is the index of the first glyph not in S. */
24886
24887 static int
24888 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24889 {
24890 struct glyph *glyph, *last;
24891 int voffset, face_id;
24892
24893 eassert (s->first_glyph->type == STRETCH_GLYPH);
24894
24895 glyph = s->row->glyphs[s->area] + start;
24896 last = s->row->glyphs[s->area] + end;
24897 face_id = glyph->face_id;
24898 s->face = FACE_FROM_ID (s->f, face_id);
24899 s->font = s->face->font;
24900 s->width = glyph->pixel_width;
24901 s->nchars = 1;
24902 voffset = glyph->voffset;
24903
24904 for (++glyph;
24905 (glyph < last
24906 && glyph->type == STRETCH_GLYPH
24907 && glyph->voffset == voffset
24908 && glyph->face_id == face_id);
24909 ++glyph)
24910 s->width += glyph->pixel_width;
24911
24912 /* Adjust base line for subscript/superscript text. */
24913 s->ybase += voffset;
24914
24915 /* The case that face->gc == 0 is handled when drawing the glyph
24916 string by calling prepare_face_for_display. */
24917 eassert (s->face);
24918 return glyph - s->row->glyphs[s->area];
24919 }
24920
24921 static struct font_metrics *
24922 get_per_char_metric (struct font *font, XChar2b *char2b)
24923 {
24924 static struct font_metrics metrics;
24925 unsigned code;
24926
24927 if (! font)
24928 return NULL;
24929 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24930 if (code == FONT_INVALID_CODE)
24931 return NULL;
24932 font->driver->text_extents (font, &code, 1, &metrics);
24933 return &metrics;
24934 }
24935
24936 /* A subroutine that computes "normal" values of ASCENT and DESCENT
24937 for FONT. Values are taken from font-global ones, except for fonts
24938 that claim preposterously large values, but whose glyphs actually
24939 have reasonable dimensions. C is the character to use for metrics
24940 if the font-global values are too large; if C is negative, the
24941 function selects a default character. */
24942 static void
24943 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
24944 {
24945 *ascent = FONT_BASE (font);
24946 *descent = FONT_DESCENT (font);
24947
24948 if (FONT_TOO_HIGH (font))
24949 {
24950 XChar2b char2b;
24951
24952 /* Get metrics of C, defaulting to a reasonably sized ASCII
24953 character. */
24954 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
24955 {
24956 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
24957
24958 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
24959 {
24960 /* We add 1 pixel to character dimensions as heuristics
24961 that produces nicer display, e.g. when the face has
24962 the box attribute. */
24963 *ascent = pcm->ascent + 1;
24964 *descent = pcm->descent + 1;
24965 }
24966 }
24967 }
24968 }
24969
24970 /* A subroutine that computes a reasonable "normal character height"
24971 for fonts that claim preposterously large vertical dimensions, but
24972 whose glyphs are actually reasonably sized. C is the character
24973 whose metrics to use for those fonts, or -1 for default
24974 character. */
24975 static int
24976 normal_char_height (struct font *font, int c)
24977 {
24978 int ascent, descent;
24979
24980 normal_char_ascent_descent (font, c, &ascent, &descent);
24981
24982 return ascent + descent;
24983 }
24984
24985 /* EXPORT for RIF:
24986 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24987 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24988 assumed to be zero. */
24989
24990 void
24991 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24992 {
24993 *left = *right = 0;
24994
24995 if (glyph->type == CHAR_GLYPH)
24996 {
24997 XChar2b char2b;
24998 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
24999 if (face->font)
25000 {
25001 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25002 if (pcm)
25003 {
25004 if (pcm->rbearing > pcm->width)
25005 *right = pcm->rbearing - pcm->width;
25006 if (pcm->lbearing < 0)
25007 *left = -pcm->lbearing;
25008 }
25009 }
25010 }
25011 else if (glyph->type == COMPOSITE_GLYPH)
25012 {
25013 if (! glyph->u.cmp.automatic)
25014 {
25015 struct composition *cmp = composition_table[glyph->u.cmp.id];
25016
25017 if (cmp->rbearing > cmp->pixel_width)
25018 *right = cmp->rbearing - cmp->pixel_width;
25019 if (cmp->lbearing < 0)
25020 *left = - cmp->lbearing;
25021 }
25022 else
25023 {
25024 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25025 struct font_metrics metrics;
25026
25027 composition_gstring_width (gstring, glyph->slice.cmp.from,
25028 glyph->slice.cmp.to + 1, &metrics);
25029 if (metrics.rbearing > metrics.width)
25030 *right = metrics.rbearing - metrics.width;
25031 if (metrics.lbearing < 0)
25032 *left = - metrics.lbearing;
25033 }
25034 }
25035 }
25036
25037
25038 /* Return the index of the first glyph preceding glyph string S that
25039 is overwritten by S because of S's left overhang. Value is -1
25040 if no glyphs are overwritten. */
25041
25042 static int
25043 left_overwritten (struct glyph_string *s)
25044 {
25045 int k;
25046
25047 if (s->left_overhang)
25048 {
25049 int x = 0, i;
25050 struct glyph *glyphs = s->row->glyphs[s->area];
25051 int first = s->first_glyph - glyphs;
25052
25053 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25054 x -= glyphs[i].pixel_width;
25055
25056 k = i + 1;
25057 }
25058 else
25059 k = -1;
25060
25061 return k;
25062 }
25063
25064
25065 /* Return the index of the first glyph preceding glyph string S that
25066 is overwriting S because of its right overhang. Value is -1 if no
25067 glyph in front of S overwrites S. */
25068
25069 static int
25070 left_overwriting (struct glyph_string *s)
25071 {
25072 int i, k, x;
25073 struct glyph *glyphs = s->row->glyphs[s->area];
25074 int first = s->first_glyph - glyphs;
25075
25076 k = -1;
25077 x = 0;
25078 for (i = first - 1; i >= 0; --i)
25079 {
25080 int left, right;
25081 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25082 if (x + right > 0)
25083 k = i;
25084 x -= glyphs[i].pixel_width;
25085 }
25086
25087 return k;
25088 }
25089
25090
25091 /* Return the index of the last glyph following glyph string S that is
25092 overwritten by S because of S's right overhang. Value is -1 if
25093 no such glyph is found. */
25094
25095 static int
25096 right_overwritten (struct glyph_string *s)
25097 {
25098 int k = -1;
25099
25100 if (s->right_overhang)
25101 {
25102 int x = 0, i;
25103 struct glyph *glyphs = s->row->glyphs[s->area];
25104 int first = (s->first_glyph - glyphs
25105 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25106 int end = s->row->used[s->area];
25107
25108 for (i = first; i < end && s->right_overhang > x; ++i)
25109 x += glyphs[i].pixel_width;
25110
25111 k = i;
25112 }
25113
25114 return k;
25115 }
25116
25117
25118 /* Return the index of the last glyph following glyph string S that
25119 overwrites S because of its left overhang. Value is negative
25120 if no such glyph is found. */
25121
25122 static int
25123 right_overwriting (struct glyph_string *s)
25124 {
25125 int i, k, x;
25126 int end = s->row->used[s->area];
25127 struct glyph *glyphs = s->row->glyphs[s->area];
25128 int first = (s->first_glyph - glyphs
25129 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25130
25131 k = -1;
25132 x = 0;
25133 for (i = first; i < end; ++i)
25134 {
25135 int left, right;
25136 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25137 if (x - left < 0)
25138 k = i;
25139 x += glyphs[i].pixel_width;
25140 }
25141
25142 return k;
25143 }
25144
25145
25146 /* Set background width of glyph string S. START is the index of the
25147 first glyph following S. LAST_X is the right-most x-position + 1
25148 in the drawing area. */
25149
25150 static void
25151 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
25152 {
25153 /* If the face of this glyph string has to be drawn to the end of
25154 the drawing area, set S->extends_to_end_of_line_p. */
25155
25156 if (start == s->row->used[s->area]
25157 && ((s->row->fill_line_p
25158 && (s->hl == DRAW_NORMAL_TEXT
25159 || s->hl == DRAW_IMAGE_RAISED
25160 || s->hl == DRAW_IMAGE_SUNKEN))
25161 || s->hl == DRAW_MOUSE_FACE))
25162 s->extends_to_end_of_line_p = true;
25163
25164 /* If S extends its face to the end of the line, set its
25165 background_width to the distance to the right edge of the drawing
25166 area. */
25167 if (s->extends_to_end_of_line_p)
25168 s->background_width = last_x - s->x + 1;
25169 else
25170 s->background_width = s->width;
25171 }
25172
25173
25174 /* Compute overhangs and x-positions for glyph string S and its
25175 predecessors, or successors. X is the starting x-position for S.
25176 BACKWARD_P means process predecessors. */
25177
25178 static void
25179 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
25180 {
25181 if (backward_p)
25182 {
25183 while (s)
25184 {
25185 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25186 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25187 x -= s->width;
25188 s->x = x;
25189 s = s->prev;
25190 }
25191 }
25192 else
25193 {
25194 while (s)
25195 {
25196 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25197 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25198 s->x = x;
25199 x += s->width;
25200 s = s->next;
25201 }
25202 }
25203 }
25204
25205
25206
25207 /* The following macros are only called from draw_glyphs below.
25208 They reference the following parameters of that function directly:
25209 `w', `row', `area', and `overlap_p'
25210 as well as the following local variables:
25211 `s', `f', and `hdc' (in W32) */
25212
25213 #ifdef HAVE_NTGUI
25214 /* On W32, silently add local `hdc' variable to argument list of
25215 init_glyph_string. */
25216 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25217 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25218 #else
25219 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25220 init_glyph_string (s, char2b, w, row, area, start, hl)
25221 #endif
25222
25223 /* Add a glyph string for a stretch glyph to the list of strings
25224 between HEAD and TAIL. START is the index of the stretch glyph in
25225 row area AREA of glyph row ROW. END is the index of the last glyph
25226 in that glyph row area. X is the current output position assigned
25227 to the new glyph string constructed. HL overrides that face of the
25228 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25229 is the right-most x-position of the drawing area. */
25230
25231 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25232 and below -- keep them on one line. */
25233 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25234 do \
25235 { \
25236 s = alloca (sizeof *s); \
25237 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25238 START = fill_stretch_glyph_string (s, START, END); \
25239 append_glyph_string (&HEAD, &TAIL, s); \
25240 s->x = (X); \
25241 } \
25242 while (false)
25243
25244
25245 /* Add a glyph string for an image glyph to the list of strings
25246 between HEAD and TAIL. START is the index of the image glyph in
25247 row area AREA of glyph row ROW. END is the index of the last glyph
25248 in that glyph row area. X is the current output position assigned
25249 to the new glyph string constructed. HL overrides that face of the
25250 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25251 is the right-most x-position of the drawing area. */
25252
25253 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25254 do \
25255 { \
25256 s = alloca (sizeof *s); \
25257 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25258 fill_image_glyph_string (s); \
25259 append_glyph_string (&HEAD, &TAIL, s); \
25260 ++START; \
25261 s->x = (X); \
25262 } \
25263 while (false)
25264
25265 #ifndef HAVE_XWIDGETS
25266 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25267 eassume (false)
25268 #else
25269 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25270 do \
25271 { \
25272 s = alloca (sizeof *s); \
25273 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25274 fill_xwidget_glyph_string (s); \
25275 append_glyph_string (&(HEAD), &(TAIL), s); \
25276 ++(START); \
25277 s->x = (X); \
25278 } \
25279 while (false)
25280 #endif
25281
25282 /* Add a glyph string for a sequence of character glyphs to the list
25283 of strings between HEAD and TAIL. START is the index of the first
25284 glyph in row area AREA of glyph row ROW that is part of the new
25285 glyph string. END is the index of the last glyph in that glyph row
25286 area. X is the current output position assigned to the new glyph
25287 string constructed. HL overrides that face of the glyph; e.g. it
25288 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25289 right-most x-position of the drawing area. */
25290
25291 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25292 do \
25293 { \
25294 int face_id; \
25295 XChar2b *char2b; \
25296 \
25297 face_id = (row)->glyphs[area][START].face_id; \
25298 \
25299 s = alloca (sizeof *s); \
25300 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25301 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25302 append_glyph_string (&HEAD, &TAIL, s); \
25303 s->x = (X); \
25304 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25305 } \
25306 while (false)
25307
25308
25309 /* Add a glyph string for a composite sequence to the list of strings
25310 between HEAD and TAIL. START is the index of the first glyph in
25311 row area AREA of glyph row ROW that is part of the new glyph
25312 string. END is the index of the last glyph in that glyph row area.
25313 X is the current output position assigned to the new glyph string
25314 constructed. HL overrides that face of the glyph; e.g. it is
25315 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25316 x-position of the drawing area. */
25317
25318 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25319 do { \
25320 int face_id = (row)->glyphs[area][START].face_id; \
25321 struct face *base_face = FACE_FROM_ID (f, face_id); \
25322 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25323 struct composition *cmp = composition_table[cmp_id]; \
25324 XChar2b *char2b; \
25325 struct glyph_string *first_s = NULL; \
25326 int n; \
25327 \
25328 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25329 \
25330 /* Make glyph_strings for each glyph sequence that is drawable by \
25331 the same face, and append them to HEAD/TAIL. */ \
25332 for (n = 0; n < cmp->glyph_len;) \
25333 { \
25334 s = alloca (sizeof *s); \
25335 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25336 append_glyph_string (&(HEAD), &(TAIL), s); \
25337 s->cmp = cmp; \
25338 s->cmp_from = n; \
25339 s->x = (X); \
25340 if (n == 0) \
25341 first_s = s; \
25342 n = fill_composite_glyph_string (s, base_face, overlaps); \
25343 } \
25344 \
25345 ++START; \
25346 s = first_s; \
25347 } while (false)
25348
25349
25350 /* Add a glyph string for a glyph-string sequence to the list of strings
25351 between HEAD and TAIL. */
25352
25353 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25354 do { \
25355 int face_id; \
25356 XChar2b *char2b; \
25357 Lisp_Object gstring; \
25358 \
25359 face_id = (row)->glyphs[area][START].face_id; \
25360 gstring = (composition_gstring_from_id \
25361 ((row)->glyphs[area][START].u.cmp.id)); \
25362 s = alloca (sizeof *s); \
25363 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25364 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25365 append_glyph_string (&(HEAD), &(TAIL), s); \
25366 s->x = (X); \
25367 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25368 } while (false)
25369
25370
25371 /* Add a glyph string for a sequence of glyphless character's glyphs
25372 to the list of strings between HEAD and TAIL. The meanings of
25373 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25374
25375 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25376 do \
25377 { \
25378 int face_id; \
25379 \
25380 face_id = (row)->glyphs[area][START].face_id; \
25381 \
25382 s = alloca (sizeof *s); \
25383 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25384 append_glyph_string (&HEAD, &TAIL, s); \
25385 s->x = (X); \
25386 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25387 overlaps); \
25388 } \
25389 while (false)
25390
25391
25392 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25393 of AREA of glyph row ROW on window W between indices START and END.
25394 HL overrides the face for drawing glyph strings, e.g. it is
25395 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25396 x-positions of the drawing area.
25397
25398 This is an ugly monster macro construct because we must use alloca
25399 to allocate glyph strings (because draw_glyphs can be called
25400 asynchronously). */
25401
25402 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25403 do \
25404 { \
25405 HEAD = TAIL = NULL; \
25406 while (START < END) \
25407 { \
25408 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25409 switch (first_glyph->type) \
25410 { \
25411 case CHAR_GLYPH: \
25412 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25413 HL, X, LAST_X); \
25414 break; \
25415 \
25416 case COMPOSITE_GLYPH: \
25417 if (first_glyph->u.cmp.automatic) \
25418 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25419 HL, X, LAST_X); \
25420 else \
25421 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25422 HL, X, LAST_X); \
25423 break; \
25424 \
25425 case STRETCH_GLYPH: \
25426 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25427 HL, X, LAST_X); \
25428 break; \
25429 \
25430 case IMAGE_GLYPH: \
25431 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25432 HL, X, LAST_X); \
25433 break;
25434
25435 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25436 case XWIDGET_GLYPH: \
25437 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
25438 HL, X, LAST_X); \
25439 break;
25440
25441 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
25442 case GLYPHLESS_GLYPH: \
25443 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25444 HL, X, LAST_X); \
25445 break; \
25446 \
25447 default: \
25448 emacs_abort (); \
25449 } \
25450 \
25451 if (s) \
25452 { \
25453 set_glyph_string_background_width (s, START, LAST_X); \
25454 (X) += s->width; \
25455 } \
25456 } \
25457 } while (false)
25458
25459
25460 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25461 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25462 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25463 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
25464
25465
25466 /* Draw glyphs between START and END in AREA of ROW on window W,
25467 starting at x-position X. X is relative to AREA in W. HL is a
25468 face-override with the following meaning:
25469
25470 DRAW_NORMAL_TEXT draw normally
25471 DRAW_CURSOR draw in cursor face
25472 DRAW_MOUSE_FACE draw in mouse face.
25473 DRAW_INVERSE_VIDEO draw in mode line face
25474 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25475 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25476
25477 If OVERLAPS is non-zero, draw only the foreground of characters and
25478 clip to the physical height of ROW. Non-zero value also defines
25479 the overlapping part to be drawn:
25480
25481 OVERLAPS_PRED overlap with preceding rows
25482 OVERLAPS_SUCC overlap with succeeding rows
25483 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25484 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25485
25486 Value is the x-position reached, relative to AREA of W. */
25487
25488 static int
25489 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25490 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25491 enum draw_glyphs_face hl, int overlaps)
25492 {
25493 struct glyph_string *head, *tail;
25494 struct glyph_string *s;
25495 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25496 int i, j, x_reached, last_x, area_left = 0;
25497 struct frame *f = XFRAME (WINDOW_FRAME (w));
25498 DECLARE_HDC (hdc);
25499
25500 ALLOCATE_HDC (hdc, f);
25501
25502 /* Let's rather be paranoid than getting a SEGV. */
25503 end = min (end, row->used[area]);
25504 start = clip_to_bounds (0, start, end);
25505
25506 /* Translate X to frame coordinates. Set last_x to the right
25507 end of the drawing area. */
25508 if (row->full_width_p)
25509 {
25510 /* X is relative to the left edge of W, without scroll bars
25511 or fringes. */
25512 area_left = WINDOW_LEFT_EDGE_X (w);
25513 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25514 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25515 }
25516 else
25517 {
25518 area_left = window_box_left (w, area);
25519 last_x = area_left + window_box_width (w, area);
25520 }
25521 x += area_left;
25522
25523 /* Build a doubly-linked list of glyph_string structures between
25524 head and tail from what we have to draw. Note that the macro
25525 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25526 the reason we use a separate variable `i'. */
25527 i = start;
25528 USE_SAFE_ALLOCA;
25529 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25530 if (tail)
25531 x_reached = tail->x + tail->background_width;
25532 else
25533 x_reached = x;
25534
25535 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25536 the row, redraw some glyphs in front or following the glyph
25537 strings built above. */
25538 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25539 {
25540 struct glyph_string *h, *t;
25541 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25542 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25543 bool check_mouse_face = false;
25544 int dummy_x = 0;
25545
25546 /* If mouse highlighting is on, we may need to draw adjacent
25547 glyphs using mouse-face highlighting. */
25548 if (area == TEXT_AREA && row->mouse_face_p
25549 && hlinfo->mouse_face_beg_row >= 0
25550 && hlinfo->mouse_face_end_row >= 0)
25551 {
25552 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25553
25554 if (row_vpos >= hlinfo->mouse_face_beg_row
25555 && row_vpos <= hlinfo->mouse_face_end_row)
25556 {
25557 check_mouse_face = true;
25558 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25559 ? hlinfo->mouse_face_beg_col : 0;
25560 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25561 ? hlinfo->mouse_face_end_col
25562 : row->used[TEXT_AREA];
25563 }
25564 }
25565
25566 /* Compute overhangs for all glyph strings. */
25567 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25568 for (s = head; s; s = s->next)
25569 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25570
25571 /* Prepend glyph strings for glyphs in front of the first glyph
25572 string that are overwritten because of the first glyph
25573 string's left overhang. The background of all strings
25574 prepended must be drawn because the first glyph string
25575 draws over it. */
25576 i = left_overwritten (head);
25577 if (i >= 0)
25578 {
25579 enum draw_glyphs_face overlap_hl;
25580
25581 /* If this row contains mouse highlighting, attempt to draw
25582 the overlapped glyphs with the correct highlight. This
25583 code fails if the overlap encompasses more than one glyph
25584 and mouse-highlight spans only some of these glyphs.
25585 However, making it work perfectly involves a lot more
25586 code, and I don't know if the pathological case occurs in
25587 practice, so we'll stick to this for now. --- cyd */
25588 if (check_mouse_face
25589 && mouse_beg_col < start && mouse_end_col > i)
25590 overlap_hl = DRAW_MOUSE_FACE;
25591 else
25592 overlap_hl = DRAW_NORMAL_TEXT;
25593
25594 if (hl != overlap_hl)
25595 clip_head = head;
25596 j = i;
25597 BUILD_GLYPH_STRINGS (j, start, h, t,
25598 overlap_hl, dummy_x, last_x);
25599 start = i;
25600 compute_overhangs_and_x (t, head->x, true);
25601 prepend_glyph_string_lists (&head, &tail, h, t);
25602 if (clip_head == NULL)
25603 clip_head = head;
25604 }
25605
25606 /* Prepend glyph strings for glyphs in front of the first glyph
25607 string that overwrite that glyph string because of their
25608 right overhang. For these strings, only the foreground must
25609 be drawn, because it draws over the glyph string at `head'.
25610 The background must not be drawn because this would overwrite
25611 right overhangs of preceding glyphs for which no glyph
25612 strings exist. */
25613 i = left_overwriting (head);
25614 if (i >= 0)
25615 {
25616 enum draw_glyphs_face overlap_hl;
25617
25618 if (check_mouse_face
25619 && mouse_beg_col < start && mouse_end_col > i)
25620 overlap_hl = DRAW_MOUSE_FACE;
25621 else
25622 overlap_hl = DRAW_NORMAL_TEXT;
25623
25624 if (hl == overlap_hl || clip_head == NULL)
25625 clip_head = head;
25626 BUILD_GLYPH_STRINGS (i, start, h, t,
25627 overlap_hl, dummy_x, last_x);
25628 for (s = h; s; s = s->next)
25629 s->background_filled_p = true;
25630 compute_overhangs_and_x (t, head->x, true);
25631 prepend_glyph_string_lists (&head, &tail, h, t);
25632 }
25633
25634 /* Append glyphs strings for glyphs following the last glyph
25635 string tail that are overwritten by tail. The background of
25636 these strings has to be drawn because tail's foreground draws
25637 over it. */
25638 i = right_overwritten (tail);
25639 if (i >= 0)
25640 {
25641 enum draw_glyphs_face overlap_hl;
25642
25643 if (check_mouse_face
25644 && mouse_beg_col < i && mouse_end_col > end)
25645 overlap_hl = DRAW_MOUSE_FACE;
25646 else
25647 overlap_hl = DRAW_NORMAL_TEXT;
25648
25649 if (hl != overlap_hl)
25650 clip_tail = tail;
25651 BUILD_GLYPH_STRINGS (end, i, h, t,
25652 overlap_hl, x, last_x);
25653 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25654 we don't have `end = i;' here. */
25655 compute_overhangs_and_x (h, tail->x + tail->width, false);
25656 append_glyph_string_lists (&head, &tail, h, t);
25657 if (clip_tail == NULL)
25658 clip_tail = tail;
25659 }
25660
25661 /* Append glyph strings for glyphs following the last glyph
25662 string tail that overwrite tail. The foreground of such
25663 glyphs has to be drawn because it writes into the background
25664 of tail. The background must not be drawn because it could
25665 paint over the foreground of following glyphs. */
25666 i = right_overwriting (tail);
25667 if (i >= 0)
25668 {
25669 enum draw_glyphs_face overlap_hl;
25670 if (check_mouse_face
25671 && mouse_beg_col < i && mouse_end_col > end)
25672 overlap_hl = DRAW_MOUSE_FACE;
25673 else
25674 overlap_hl = DRAW_NORMAL_TEXT;
25675
25676 if (hl == overlap_hl || clip_tail == NULL)
25677 clip_tail = tail;
25678 i++; /* We must include the Ith glyph. */
25679 BUILD_GLYPH_STRINGS (end, i, h, t,
25680 overlap_hl, x, last_x);
25681 for (s = h; s; s = s->next)
25682 s->background_filled_p = true;
25683 compute_overhangs_and_x (h, tail->x + tail->width, false);
25684 append_glyph_string_lists (&head, &tail, h, t);
25685 }
25686 if (clip_head || clip_tail)
25687 for (s = head; s; s = s->next)
25688 {
25689 s->clip_head = clip_head;
25690 s->clip_tail = clip_tail;
25691 }
25692 }
25693
25694 /* Draw all strings. */
25695 for (s = head; s; s = s->next)
25696 FRAME_RIF (f)->draw_glyph_string (s);
25697
25698 #ifndef HAVE_NS
25699 /* When focus a sole frame and move horizontally, this clears on_p
25700 causing a failure to erase prev cursor position. */
25701 if (area == TEXT_AREA
25702 && !row->full_width_p
25703 /* When drawing overlapping rows, only the glyph strings'
25704 foreground is drawn, which doesn't erase a cursor
25705 completely. */
25706 && !overlaps)
25707 {
25708 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25709 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25710 : (tail ? tail->x + tail->background_width : x));
25711 x0 -= area_left;
25712 x1 -= area_left;
25713
25714 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25715 row->y, MATRIX_ROW_BOTTOM_Y (row));
25716 }
25717 #endif
25718
25719 /* Value is the x-position up to which drawn, relative to AREA of W.
25720 This doesn't include parts drawn because of overhangs. */
25721 if (row->full_width_p)
25722 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25723 else
25724 x_reached -= area_left;
25725
25726 RELEASE_HDC (hdc, f);
25727
25728 SAFE_FREE ();
25729 return x_reached;
25730 }
25731
25732 /* Expand row matrix if too narrow. Don't expand if area
25733 is not present. */
25734
25735 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25736 { \
25737 if (!it->f->fonts_changed \
25738 && (it->glyph_row->glyphs[area] \
25739 < it->glyph_row->glyphs[area + 1])) \
25740 { \
25741 it->w->ncols_scale_factor++; \
25742 it->f->fonts_changed = true; \
25743 } \
25744 }
25745
25746 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25747 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25748
25749 static void
25750 append_glyph (struct it *it)
25751 {
25752 struct glyph *glyph;
25753 enum glyph_row_area area = it->area;
25754
25755 eassert (it->glyph_row);
25756 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25757
25758 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25759 if (glyph < it->glyph_row->glyphs[area + 1])
25760 {
25761 /* If the glyph row is reversed, we need to prepend the glyph
25762 rather than append it. */
25763 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25764 {
25765 struct glyph *g;
25766
25767 /* Make room for the additional glyph. */
25768 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25769 g[1] = *g;
25770 glyph = it->glyph_row->glyphs[area];
25771 }
25772 glyph->charpos = CHARPOS (it->position);
25773 glyph->object = it->object;
25774 if (it->pixel_width > 0)
25775 {
25776 glyph->pixel_width = it->pixel_width;
25777 glyph->padding_p = false;
25778 }
25779 else
25780 {
25781 /* Assure at least 1-pixel width. Otherwise, cursor can't
25782 be displayed correctly. */
25783 glyph->pixel_width = 1;
25784 glyph->padding_p = true;
25785 }
25786 glyph->ascent = it->ascent;
25787 glyph->descent = it->descent;
25788 glyph->voffset = it->voffset;
25789 glyph->type = CHAR_GLYPH;
25790 glyph->avoid_cursor_p = it->avoid_cursor_p;
25791 glyph->multibyte_p = it->multibyte_p;
25792 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25793 {
25794 /* In R2L rows, the left and the right box edges need to be
25795 drawn in reverse direction. */
25796 glyph->right_box_line_p = it->start_of_box_run_p;
25797 glyph->left_box_line_p = it->end_of_box_run_p;
25798 }
25799 else
25800 {
25801 glyph->left_box_line_p = it->start_of_box_run_p;
25802 glyph->right_box_line_p = it->end_of_box_run_p;
25803 }
25804 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25805 || it->phys_descent > it->descent);
25806 glyph->glyph_not_available_p = it->glyph_not_available_p;
25807 glyph->face_id = it->face_id;
25808 glyph->u.ch = it->char_to_display;
25809 glyph->slice.img = null_glyph_slice;
25810 glyph->font_type = FONT_TYPE_UNKNOWN;
25811 if (it->bidi_p)
25812 {
25813 glyph->resolved_level = it->bidi_it.resolved_level;
25814 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25815 glyph->bidi_type = it->bidi_it.type;
25816 }
25817 else
25818 {
25819 glyph->resolved_level = 0;
25820 glyph->bidi_type = UNKNOWN_BT;
25821 }
25822 ++it->glyph_row->used[area];
25823 }
25824 else
25825 IT_EXPAND_MATRIX_WIDTH (it, area);
25826 }
25827
25828 /* Store one glyph for the composition IT->cmp_it.id in
25829 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25830 non-null. */
25831
25832 static void
25833 append_composite_glyph (struct it *it)
25834 {
25835 struct glyph *glyph;
25836 enum glyph_row_area area = it->area;
25837
25838 eassert (it->glyph_row);
25839
25840 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25841 if (glyph < it->glyph_row->glyphs[area + 1])
25842 {
25843 /* If the glyph row is reversed, we need to prepend the glyph
25844 rather than append it. */
25845 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25846 {
25847 struct glyph *g;
25848
25849 /* Make room for the new glyph. */
25850 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25851 g[1] = *g;
25852 glyph = it->glyph_row->glyphs[it->area];
25853 }
25854 glyph->charpos = it->cmp_it.charpos;
25855 glyph->object = it->object;
25856 glyph->pixel_width = it->pixel_width;
25857 glyph->ascent = it->ascent;
25858 glyph->descent = it->descent;
25859 glyph->voffset = it->voffset;
25860 glyph->type = COMPOSITE_GLYPH;
25861 if (it->cmp_it.ch < 0)
25862 {
25863 glyph->u.cmp.automatic = false;
25864 glyph->u.cmp.id = it->cmp_it.id;
25865 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25866 }
25867 else
25868 {
25869 glyph->u.cmp.automatic = true;
25870 glyph->u.cmp.id = it->cmp_it.id;
25871 glyph->slice.cmp.from = it->cmp_it.from;
25872 glyph->slice.cmp.to = it->cmp_it.to - 1;
25873 }
25874 glyph->avoid_cursor_p = it->avoid_cursor_p;
25875 glyph->multibyte_p = it->multibyte_p;
25876 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25877 {
25878 /* In R2L rows, the left and the right box edges need to be
25879 drawn in reverse direction. */
25880 glyph->right_box_line_p = it->start_of_box_run_p;
25881 glyph->left_box_line_p = it->end_of_box_run_p;
25882 }
25883 else
25884 {
25885 glyph->left_box_line_p = it->start_of_box_run_p;
25886 glyph->right_box_line_p = it->end_of_box_run_p;
25887 }
25888 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25889 || it->phys_descent > it->descent);
25890 glyph->padding_p = false;
25891 glyph->glyph_not_available_p = false;
25892 glyph->face_id = it->face_id;
25893 glyph->font_type = FONT_TYPE_UNKNOWN;
25894 if (it->bidi_p)
25895 {
25896 glyph->resolved_level = it->bidi_it.resolved_level;
25897 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25898 glyph->bidi_type = it->bidi_it.type;
25899 }
25900 ++it->glyph_row->used[area];
25901 }
25902 else
25903 IT_EXPAND_MATRIX_WIDTH (it, area);
25904 }
25905
25906
25907 /* Change IT->ascent and IT->height according to the setting of
25908 IT->voffset. */
25909
25910 static void
25911 take_vertical_position_into_account (struct it *it)
25912 {
25913 if (it->voffset)
25914 {
25915 if (it->voffset < 0)
25916 /* Increase the ascent so that we can display the text higher
25917 in the line. */
25918 it->ascent -= it->voffset;
25919 else
25920 /* Increase the descent so that we can display the text lower
25921 in the line. */
25922 it->descent += it->voffset;
25923 }
25924 }
25925
25926
25927 /* Produce glyphs/get display metrics for the image IT is loaded with.
25928 See the description of struct display_iterator in dispextern.h for
25929 an overview of struct display_iterator. */
25930
25931 static void
25932 produce_image_glyph (struct it *it)
25933 {
25934 struct image *img;
25935 struct face *face;
25936 int glyph_ascent, crop;
25937 struct glyph_slice slice;
25938
25939 eassert (it->what == IT_IMAGE);
25940
25941 face = FACE_FROM_ID (it->f, it->face_id);
25942 eassert (face);
25943 /* Make sure X resources of the face is loaded. */
25944 prepare_face_for_display (it->f, face);
25945
25946 if (it->image_id < 0)
25947 {
25948 /* Fringe bitmap. */
25949 it->ascent = it->phys_ascent = 0;
25950 it->descent = it->phys_descent = 0;
25951 it->pixel_width = 0;
25952 it->nglyphs = 0;
25953 return;
25954 }
25955
25956 img = IMAGE_FROM_ID (it->f, it->image_id);
25957 eassert (img);
25958 /* Make sure X resources of the image is loaded. */
25959 prepare_image_for_display (it->f, img);
25960
25961 slice.x = slice.y = 0;
25962 slice.width = img->width;
25963 slice.height = img->height;
25964
25965 if (INTEGERP (it->slice.x))
25966 slice.x = XINT (it->slice.x);
25967 else if (FLOATP (it->slice.x))
25968 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25969
25970 if (INTEGERP (it->slice.y))
25971 slice.y = XINT (it->slice.y);
25972 else if (FLOATP (it->slice.y))
25973 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25974
25975 if (INTEGERP (it->slice.width))
25976 slice.width = XINT (it->slice.width);
25977 else if (FLOATP (it->slice.width))
25978 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25979
25980 if (INTEGERP (it->slice.height))
25981 slice.height = XINT (it->slice.height);
25982 else if (FLOATP (it->slice.height))
25983 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25984
25985 if (slice.x >= img->width)
25986 slice.x = img->width;
25987 if (slice.y >= img->height)
25988 slice.y = img->height;
25989 if (slice.x + slice.width >= img->width)
25990 slice.width = img->width - slice.x;
25991 if (slice.y + slice.height > img->height)
25992 slice.height = img->height - slice.y;
25993
25994 if (slice.width == 0 || slice.height == 0)
25995 return;
25996
25997 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25998
25999 it->descent = slice.height - glyph_ascent;
26000 if (slice.y == 0)
26001 it->descent += img->vmargin;
26002 if (slice.y + slice.height == img->height)
26003 it->descent += img->vmargin;
26004 it->phys_descent = it->descent;
26005
26006 it->pixel_width = slice.width;
26007 if (slice.x == 0)
26008 it->pixel_width += img->hmargin;
26009 if (slice.x + slice.width == img->width)
26010 it->pixel_width += img->hmargin;
26011
26012 /* It's quite possible for images to have an ascent greater than
26013 their height, so don't get confused in that case. */
26014 if (it->descent < 0)
26015 it->descent = 0;
26016
26017 it->nglyphs = 1;
26018
26019 if (face->box != FACE_NO_BOX)
26020 {
26021 if (face->box_line_width > 0)
26022 {
26023 if (slice.y == 0)
26024 it->ascent += face->box_line_width;
26025 if (slice.y + slice.height == img->height)
26026 it->descent += face->box_line_width;
26027 }
26028
26029 if (it->start_of_box_run_p && slice.x == 0)
26030 it->pixel_width += eabs (face->box_line_width);
26031 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26032 it->pixel_width += eabs (face->box_line_width);
26033 }
26034
26035 take_vertical_position_into_account (it);
26036
26037 /* Automatically crop wide image glyphs at right edge so we can
26038 draw the cursor on same display row. */
26039 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26040 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26041 {
26042 it->pixel_width -= crop;
26043 slice.width -= crop;
26044 }
26045
26046 if (it->glyph_row)
26047 {
26048 struct glyph *glyph;
26049 enum glyph_row_area area = it->area;
26050
26051 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26052 if (it->glyph_row->reversed_p)
26053 {
26054 struct glyph *g;
26055
26056 /* Make room for the new glyph. */
26057 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26058 g[1] = *g;
26059 glyph = it->glyph_row->glyphs[it->area];
26060 }
26061 if (glyph < it->glyph_row->glyphs[area + 1])
26062 {
26063 glyph->charpos = CHARPOS (it->position);
26064 glyph->object = it->object;
26065 glyph->pixel_width = it->pixel_width;
26066 glyph->ascent = glyph_ascent;
26067 glyph->descent = it->descent;
26068 glyph->voffset = it->voffset;
26069 glyph->type = IMAGE_GLYPH;
26070 glyph->avoid_cursor_p = it->avoid_cursor_p;
26071 glyph->multibyte_p = it->multibyte_p;
26072 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26073 {
26074 /* In R2L rows, the left and the right box edges need to be
26075 drawn in reverse direction. */
26076 glyph->right_box_line_p = it->start_of_box_run_p;
26077 glyph->left_box_line_p = it->end_of_box_run_p;
26078 }
26079 else
26080 {
26081 glyph->left_box_line_p = it->start_of_box_run_p;
26082 glyph->right_box_line_p = it->end_of_box_run_p;
26083 }
26084 glyph->overlaps_vertically_p = false;
26085 glyph->padding_p = false;
26086 glyph->glyph_not_available_p = false;
26087 glyph->face_id = it->face_id;
26088 glyph->u.img_id = img->id;
26089 glyph->slice.img = slice;
26090 glyph->font_type = FONT_TYPE_UNKNOWN;
26091 if (it->bidi_p)
26092 {
26093 glyph->resolved_level = it->bidi_it.resolved_level;
26094 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26095 glyph->bidi_type = it->bidi_it.type;
26096 }
26097 ++it->glyph_row->used[area];
26098 }
26099 else
26100 IT_EXPAND_MATRIX_WIDTH (it, area);
26101 }
26102 }
26103
26104 static void
26105 produce_xwidget_glyph (struct it *it)
26106 {
26107 #ifdef HAVE_XWIDGETS
26108 struct xwidget *xw;
26109 int glyph_ascent, crop;
26110 eassert (it->what == IT_XWIDGET);
26111
26112 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26113 eassert (face);
26114 /* Make sure X resources of the face is loaded. */
26115 prepare_face_for_display (it->f, face);
26116
26117 xw = it->xwidget;
26118 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
26119 it->descent = xw->height/2;
26120 it->phys_descent = it->descent;
26121 it->pixel_width = xw->width;
26122 /* It's quite possible for images to have an ascent greater than
26123 their height, so don't get confused in that case. */
26124 if (it->descent < 0)
26125 it->descent = 0;
26126
26127 it->nglyphs = 1;
26128
26129 if (face->box != FACE_NO_BOX)
26130 {
26131 if (face->box_line_width > 0)
26132 {
26133 it->ascent += face->box_line_width;
26134 it->descent += face->box_line_width;
26135 }
26136
26137 if (it->start_of_box_run_p)
26138 it->pixel_width += eabs (face->box_line_width);
26139 it->pixel_width += eabs (face->box_line_width);
26140 }
26141
26142 take_vertical_position_into_account (it);
26143
26144 /* Automatically crop wide image glyphs at right edge so we can
26145 draw the cursor on same display row. */
26146 crop = it->pixel_width - (it->last_visible_x - it->current_x);
26147 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26148 it->pixel_width -= crop;
26149
26150 if (it->glyph_row)
26151 {
26152 enum glyph_row_area area = it->area;
26153 struct glyph *glyph
26154 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26155
26156 if (it->glyph_row->reversed_p)
26157 {
26158 struct glyph *g;
26159
26160 /* Make room for the new glyph. */
26161 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26162 g[1] = *g;
26163 glyph = it->glyph_row->glyphs[it->area];
26164 }
26165 if (glyph < it->glyph_row->glyphs[area + 1])
26166 {
26167 glyph->charpos = CHARPOS (it->position);
26168 glyph->object = it->object;
26169 glyph->pixel_width = it->pixel_width;
26170 glyph->ascent = glyph_ascent;
26171 glyph->descent = it->descent;
26172 glyph->voffset = it->voffset;
26173 glyph->type = XWIDGET_GLYPH;
26174 glyph->avoid_cursor_p = it->avoid_cursor_p;
26175 glyph->multibyte_p = it->multibyte_p;
26176 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26177 {
26178 /* In R2L rows, the left and the right box edges need to be
26179 drawn in reverse direction. */
26180 glyph->right_box_line_p = it->start_of_box_run_p;
26181 glyph->left_box_line_p = it->end_of_box_run_p;
26182 }
26183 else
26184 {
26185 glyph->left_box_line_p = it->start_of_box_run_p;
26186 glyph->right_box_line_p = it->end_of_box_run_p;
26187 }
26188 glyph->overlaps_vertically_p = 0;
26189 glyph->padding_p = 0;
26190 glyph->glyph_not_available_p = 0;
26191 glyph->face_id = it->face_id;
26192 glyph->u.xwidget = it->xwidget;
26193 glyph->font_type = FONT_TYPE_UNKNOWN;
26194 if (it->bidi_p)
26195 {
26196 glyph->resolved_level = it->bidi_it.resolved_level;
26197 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26198 glyph->bidi_type = it->bidi_it.type;
26199 }
26200 ++it->glyph_row->used[area];
26201 }
26202 else
26203 IT_EXPAND_MATRIX_WIDTH (it, area);
26204 }
26205 #endif
26206 }
26207
26208 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
26209 of the glyph, WIDTH and HEIGHT are the width and height of the
26210 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
26211
26212 static void
26213 append_stretch_glyph (struct it *it, Lisp_Object object,
26214 int width, int height, int ascent)
26215 {
26216 struct glyph *glyph;
26217 enum glyph_row_area area = it->area;
26218
26219 eassert (ascent >= 0 && ascent <= height);
26220
26221 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26222 if (glyph < it->glyph_row->glyphs[area + 1])
26223 {
26224 /* If the glyph row is reversed, we need to prepend the glyph
26225 rather than append it. */
26226 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26227 {
26228 struct glyph *g;
26229
26230 /* Make room for the additional glyph. */
26231 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26232 g[1] = *g;
26233 glyph = it->glyph_row->glyphs[area];
26234
26235 /* Decrease the width of the first glyph of the row that
26236 begins before first_visible_x (e.g., due to hscroll).
26237 This is so the overall width of the row becomes smaller
26238 by the scroll amount, and the stretch glyph appended by
26239 extend_face_to_end_of_line will be wider, to shift the
26240 row glyphs to the right. (In L2R rows, the corresponding
26241 left-shift effect is accomplished by setting row->x to a
26242 negative value, which won't work with R2L rows.)
26243
26244 This must leave us with a positive value of WIDTH, since
26245 otherwise the call to move_it_in_display_line_to at the
26246 beginning of display_line would have got past the entire
26247 first glyph, and then it->current_x would have been
26248 greater or equal to it->first_visible_x. */
26249 if (it->current_x < it->first_visible_x)
26250 width -= it->first_visible_x - it->current_x;
26251 eassert (width > 0);
26252 }
26253 glyph->charpos = CHARPOS (it->position);
26254 glyph->object = object;
26255 glyph->pixel_width = width;
26256 glyph->ascent = ascent;
26257 glyph->descent = height - ascent;
26258 glyph->voffset = it->voffset;
26259 glyph->type = STRETCH_GLYPH;
26260 glyph->avoid_cursor_p = it->avoid_cursor_p;
26261 glyph->multibyte_p = it->multibyte_p;
26262 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26263 {
26264 /* In R2L rows, the left and the right box edges need to be
26265 drawn in reverse direction. */
26266 glyph->right_box_line_p = it->start_of_box_run_p;
26267 glyph->left_box_line_p = it->end_of_box_run_p;
26268 }
26269 else
26270 {
26271 glyph->left_box_line_p = it->start_of_box_run_p;
26272 glyph->right_box_line_p = it->end_of_box_run_p;
26273 }
26274 glyph->overlaps_vertically_p = false;
26275 glyph->padding_p = false;
26276 glyph->glyph_not_available_p = false;
26277 glyph->face_id = it->face_id;
26278 glyph->u.stretch.ascent = ascent;
26279 glyph->u.stretch.height = height;
26280 glyph->slice.img = null_glyph_slice;
26281 glyph->font_type = FONT_TYPE_UNKNOWN;
26282 if (it->bidi_p)
26283 {
26284 glyph->resolved_level = it->bidi_it.resolved_level;
26285 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26286 glyph->bidi_type = it->bidi_it.type;
26287 }
26288 else
26289 {
26290 glyph->resolved_level = 0;
26291 glyph->bidi_type = UNKNOWN_BT;
26292 }
26293 ++it->glyph_row->used[area];
26294 }
26295 else
26296 IT_EXPAND_MATRIX_WIDTH (it, area);
26297 }
26298
26299 #endif /* HAVE_WINDOW_SYSTEM */
26300
26301 /* Produce a stretch glyph for iterator IT. IT->object is the value
26302 of the glyph property displayed. The value must be a list
26303 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
26304 being recognized:
26305
26306 1. `:width WIDTH' specifies that the space should be WIDTH *
26307 canonical char width wide. WIDTH may be an integer or floating
26308 point number.
26309
26310 2. `:relative-width FACTOR' specifies that the width of the stretch
26311 should be computed from the width of the first character having the
26312 `glyph' property, and should be FACTOR times that width.
26313
26314 3. `:align-to HPOS' specifies that the space should be wide enough
26315 to reach HPOS, a value in canonical character units.
26316
26317 Exactly one of the above pairs must be present.
26318
26319 4. `:height HEIGHT' specifies that the height of the stretch produced
26320 should be HEIGHT, measured in canonical character units.
26321
26322 5. `:relative-height FACTOR' specifies that the height of the
26323 stretch should be FACTOR times the height of the characters having
26324 the glyph property.
26325
26326 Either none or exactly one of 4 or 5 must be present.
26327
26328 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26329 of the stretch should be used for the ascent of the stretch.
26330 ASCENT must be in the range 0 <= ASCENT <= 100. */
26331
26332 void
26333 produce_stretch_glyph (struct it *it)
26334 {
26335 /* (space :width WIDTH :height HEIGHT ...) */
26336 Lisp_Object prop, plist;
26337 int width = 0, height = 0, align_to = -1;
26338 bool zero_width_ok_p = false;
26339 double tem;
26340 struct font *font = NULL;
26341
26342 #ifdef HAVE_WINDOW_SYSTEM
26343 int ascent = 0;
26344 bool zero_height_ok_p = false;
26345
26346 if (FRAME_WINDOW_P (it->f))
26347 {
26348 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26349 font = face->font ? face->font : FRAME_FONT (it->f);
26350 prepare_face_for_display (it->f, face);
26351 }
26352 #endif
26353
26354 /* List should start with `space'. */
26355 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26356 plist = XCDR (it->object);
26357
26358 /* Compute the width of the stretch. */
26359 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26360 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26361 {
26362 /* Absolute width `:width WIDTH' specified and valid. */
26363 zero_width_ok_p = true;
26364 width = (int)tem;
26365 }
26366 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26367 {
26368 /* Relative width `:relative-width FACTOR' specified and valid.
26369 Compute the width of the characters having the `glyph'
26370 property. */
26371 struct it it2;
26372 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26373
26374 it2 = *it;
26375 if (it->multibyte_p)
26376 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26377 else
26378 {
26379 it2.c = it2.char_to_display = *p, it2.len = 1;
26380 if (! ASCII_CHAR_P (it2.c))
26381 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26382 }
26383
26384 it2.glyph_row = NULL;
26385 it2.what = IT_CHARACTER;
26386 PRODUCE_GLYPHS (&it2);
26387 width = NUMVAL (prop) * it2.pixel_width;
26388 }
26389 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26390 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26391 &align_to))
26392 {
26393 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26394 align_to = (align_to < 0
26395 ? 0
26396 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26397 else if (align_to < 0)
26398 align_to = window_box_left_offset (it->w, TEXT_AREA);
26399 width = max (0, (int)tem + align_to - it->current_x);
26400 zero_width_ok_p = true;
26401 }
26402 else
26403 /* Nothing specified -> width defaults to canonical char width. */
26404 width = FRAME_COLUMN_WIDTH (it->f);
26405
26406 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26407 width = 1;
26408
26409 #ifdef HAVE_WINDOW_SYSTEM
26410 /* Compute height. */
26411 if (FRAME_WINDOW_P (it->f))
26412 {
26413 int default_height = normal_char_height (font, ' ');
26414
26415 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26416 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26417 {
26418 height = (int)tem;
26419 zero_height_ok_p = true;
26420 }
26421 else if (prop = Fplist_get (plist, QCrelative_height),
26422 NUMVAL (prop) > 0)
26423 height = default_height * NUMVAL (prop);
26424 else
26425 height = default_height;
26426
26427 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26428 height = 1;
26429
26430 /* Compute percentage of height used for ascent. If
26431 `:ascent ASCENT' is present and valid, use that. Otherwise,
26432 derive the ascent from the font in use. */
26433 if (prop = Fplist_get (plist, QCascent),
26434 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26435 ascent = height * NUMVAL (prop) / 100.0;
26436 else if (!NILP (prop)
26437 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26438 ascent = min (max (0, (int)tem), height);
26439 else
26440 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26441 }
26442 else
26443 #endif /* HAVE_WINDOW_SYSTEM */
26444 height = 1;
26445
26446 if (width > 0 && it->line_wrap != TRUNCATE
26447 && it->current_x + width > it->last_visible_x)
26448 {
26449 width = it->last_visible_x - it->current_x;
26450 #ifdef HAVE_WINDOW_SYSTEM
26451 /* Subtract one more pixel from the stretch width, but only on
26452 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26453 width -= FRAME_WINDOW_P (it->f);
26454 #endif
26455 }
26456
26457 if (width > 0 && height > 0 && it->glyph_row)
26458 {
26459 Lisp_Object o_object = it->object;
26460 Lisp_Object object = it->stack[it->sp - 1].string;
26461 int n = width;
26462
26463 if (!STRINGP (object))
26464 object = it->w->contents;
26465 #ifdef HAVE_WINDOW_SYSTEM
26466 if (FRAME_WINDOW_P (it->f))
26467 append_stretch_glyph (it, object, width, height, ascent);
26468 else
26469 #endif
26470 {
26471 it->object = object;
26472 it->char_to_display = ' ';
26473 it->pixel_width = it->len = 1;
26474 while (n--)
26475 tty_append_glyph (it);
26476 it->object = o_object;
26477 }
26478 }
26479
26480 it->pixel_width = width;
26481 #ifdef HAVE_WINDOW_SYSTEM
26482 if (FRAME_WINDOW_P (it->f))
26483 {
26484 it->ascent = it->phys_ascent = ascent;
26485 it->descent = it->phys_descent = height - it->ascent;
26486 it->nglyphs = width > 0 && height > 0;
26487 take_vertical_position_into_account (it);
26488 }
26489 else
26490 #endif
26491 it->nglyphs = width;
26492 }
26493
26494 /* Get information about special display element WHAT in an
26495 environment described by IT. WHAT is one of IT_TRUNCATION or
26496 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26497 non-null glyph_row member. This function ensures that fields like
26498 face_id, c, len of IT are left untouched. */
26499
26500 static void
26501 produce_special_glyphs (struct it *it, enum display_element_type what)
26502 {
26503 struct it temp_it;
26504 Lisp_Object gc;
26505 GLYPH glyph;
26506
26507 temp_it = *it;
26508 temp_it.object = Qnil;
26509 memset (&temp_it.current, 0, sizeof temp_it.current);
26510
26511 if (what == IT_CONTINUATION)
26512 {
26513 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26514 if (it->bidi_it.paragraph_dir == R2L)
26515 SET_GLYPH_FROM_CHAR (glyph, '/');
26516 else
26517 SET_GLYPH_FROM_CHAR (glyph, '\\');
26518 if (it->dp
26519 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26520 {
26521 /* FIXME: Should we mirror GC for R2L lines? */
26522 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26523 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26524 }
26525 }
26526 else if (what == IT_TRUNCATION)
26527 {
26528 /* Truncation glyph. */
26529 SET_GLYPH_FROM_CHAR (glyph, '$');
26530 if (it->dp
26531 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26532 {
26533 /* FIXME: Should we mirror GC for R2L lines? */
26534 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26535 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26536 }
26537 }
26538 else
26539 emacs_abort ();
26540
26541 #ifdef HAVE_WINDOW_SYSTEM
26542 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26543 is turned off, we precede the truncation/continuation glyphs by a
26544 stretch glyph whose width is computed such that these special
26545 glyphs are aligned at the window margin, even when very different
26546 fonts are used in different glyph rows. */
26547 if (FRAME_WINDOW_P (temp_it.f)
26548 /* init_iterator calls this with it->glyph_row == NULL, and it
26549 wants only the pixel width of the truncation/continuation
26550 glyphs. */
26551 && temp_it.glyph_row
26552 /* insert_left_trunc_glyphs calls us at the beginning of the
26553 row, and it has its own calculation of the stretch glyph
26554 width. */
26555 && temp_it.glyph_row->used[TEXT_AREA] > 0
26556 && (temp_it.glyph_row->reversed_p
26557 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26558 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26559 {
26560 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26561
26562 if (stretch_width > 0)
26563 {
26564 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26565 struct font *font =
26566 face->font ? face->font : FRAME_FONT (temp_it.f);
26567 int stretch_ascent =
26568 (((temp_it.ascent + temp_it.descent)
26569 * FONT_BASE (font)) / FONT_HEIGHT (font));
26570
26571 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26572 temp_it.ascent + temp_it.descent,
26573 stretch_ascent);
26574 }
26575 }
26576 #endif
26577
26578 temp_it.dp = NULL;
26579 temp_it.what = IT_CHARACTER;
26580 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26581 temp_it.face_id = GLYPH_FACE (glyph);
26582 temp_it.len = CHAR_BYTES (temp_it.c);
26583
26584 PRODUCE_GLYPHS (&temp_it);
26585 it->pixel_width = temp_it.pixel_width;
26586 it->nglyphs = temp_it.nglyphs;
26587 }
26588
26589 #ifdef HAVE_WINDOW_SYSTEM
26590
26591 /* Calculate line-height and line-spacing properties.
26592 An integer value specifies explicit pixel value.
26593 A float value specifies relative value to current face height.
26594 A cons (float . face-name) specifies relative value to
26595 height of specified face font.
26596
26597 Returns height in pixels, or nil. */
26598
26599 static Lisp_Object
26600 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26601 int boff, bool override)
26602 {
26603 Lisp_Object face_name = Qnil;
26604 int ascent, descent, height;
26605
26606 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26607 return val;
26608
26609 if (CONSP (val))
26610 {
26611 face_name = XCAR (val);
26612 val = XCDR (val);
26613 if (!NUMBERP (val))
26614 val = make_number (1);
26615 if (NILP (face_name))
26616 {
26617 height = it->ascent + it->descent;
26618 goto scale;
26619 }
26620 }
26621
26622 if (NILP (face_name))
26623 {
26624 font = FRAME_FONT (it->f);
26625 boff = FRAME_BASELINE_OFFSET (it->f);
26626 }
26627 else if (EQ (face_name, Qt))
26628 {
26629 override = false;
26630 }
26631 else
26632 {
26633 int face_id;
26634 struct face *face;
26635
26636 face_id = lookup_named_face (it->f, face_name, false);
26637 if (face_id < 0)
26638 return make_number (-1);
26639
26640 face = FACE_FROM_ID (it->f, face_id);
26641 font = face->font;
26642 if (font == NULL)
26643 return make_number (-1);
26644 boff = font->baseline_offset;
26645 if (font->vertical_centering)
26646 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26647 }
26648
26649 normal_char_ascent_descent (font, -1, &ascent, &descent);
26650
26651 if (override)
26652 {
26653 it->override_ascent = ascent;
26654 it->override_descent = descent;
26655 it->override_boff = boff;
26656 }
26657
26658 height = ascent + descent;
26659
26660 scale:
26661 if (FLOATP (val))
26662 height = (int)(XFLOAT_DATA (val) * height);
26663 else if (INTEGERP (val))
26664 height *= XINT (val);
26665
26666 return make_number (height);
26667 }
26668
26669
26670 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26671 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26672 and only if this is for a character for which no font was found.
26673
26674 If the display method (it->glyphless_method) is
26675 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26676 length of the acronym or the hexadecimal string, UPPER_XOFF and
26677 UPPER_YOFF are pixel offsets for the upper part of the string,
26678 LOWER_XOFF and LOWER_YOFF are for the lower part.
26679
26680 For the other display methods, LEN through LOWER_YOFF are zero. */
26681
26682 static void
26683 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26684 short upper_xoff, short upper_yoff,
26685 short lower_xoff, short lower_yoff)
26686 {
26687 struct glyph *glyph;
26688 enum glyph_row_area area = it->area;
26689
26690 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26691 if (glyph < it->glyph_row->glyphs[area + 1])
26692 {
26693 /* If the glyph row is reversed, we need to prepend the glyph
26694 rather than append it. */
26695 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26696 {
26697 struct glyph *g;
26698
26699 /* Make room for the additional glyph. */
26700 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26701 g[1] = *g;
26702 glyph = it->glyph_row->glyphs[area];
26703 }
26704 glyph->charpos = CHARPOS (it->position);
26705 glyph->object = it->object;
26706 glyph->pixel_width = it->pixel_width;
26707 glyph->ascent = it->ascent;
26708 glyph->descent = it->descent;
26709 glyph->voffset = it->voffset;
26710 glyph->type = GLYPHLESS_GLYPH;
26711 glyph->u.glyphless.method = it->glyphless_method;
26712 glyph->u.glyphless.for_no_font = for_no_font;
26713 glyph->u.glyphless.len = len;
26714 glyph->u.glyphless.ch = it->c;
26715 glyph->slice.glyphless.upper_xoff = upper_xoff;
26716 glyph->slice.glyphless.upper_yoff = upper_yoff;
26717 glyph->slice.glyphless.lower_xoff = lower_xoff;
26718 glyph->slice.glyphless.lower_yoff = lower_yoff;
26719 glyph->avoid_cursor_p = it->avoid_cursor_p;
26720 glyph->multibyte_p = it->multibyte_p;
26721 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26722 {
26723 /* In R2L rows, the left and the right box edges need to be
26724 drawn in reverse direction. */
26725 glyph->right_box_line_p = it->start_of_box_run_p;
26726 glyph->left_box_line_p = it->end_of_box_run_p;
26727 }
26728 else
26729 {
26730 glyph->left_box_line_p = it->start_of_box_run_p;
26731 glyph->right_box_line_p = it->end_of_box_run_p;
26732 }
26733 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26734 || it->phys_descent > it->descent);
26735 glyph->padding_p = false;
26736 glyph->glyph_not_available_p = false;
26737 glyph->face_id = face_id;
26738 glyph->font_type = FONT_TYPE_UNKNOWN;
26739 if (it->bidi_p)
26740 {
26741 glyph->resolved_level = it->bidi_it.resolved_level;
26742 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26743 glyph->bidi_type = it->bidi_it.type;
26744 }
26745 ++it->glyph_row->used[area];
26746 }
26747 else
26748 IT_EXPAND_MATRIX_WIDTH (it, area);
26749 }
26750
26751
26752 /* Produce a glyph for a glyphless character for iterator IT.
26753 IT->glyphless_method specifies which method to use for displaying
26754 the character. See the description of enum
26755 glyphless_display_method in dispextern.h for the detail.
26756
26757 FOR_NO_FONT is true if and only if this is for a character for
26758 which no font was found. ACRONYM, if non-nil, is an acronym string
26759 for the character. */
26760
26761 static void
26762 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26763 {
26764 int face_id;
26765 struct face *face;
26766 struct font *font;
26767 int base_width, base_height, width, height;
26768 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26769 int len;
26770
26771 /* Get the metrics of the base font. We always refer to the current
26772 ASCII face. */
26773 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26774 font = face->font ? face->font : FRAME_FONT (it->f);
26775 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
26776 it->ascent += font->baseline_offset;
26777 it->descent -= font->baseline_offset;
26778 base_height = it->ascent + it->descent;
26779 base_width = font->average_width;
26780
26781 face_id = merge_glyphless_glyph_face (it);
26782
26783 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26784 {
26785 it->pixel_width = THIN_SPACE_WIDTH;
26786 len = 0;
26787 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26788 }
26789 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26790 {
26791 width = CHAR_WIDTH (it->c);
26792 if (width == 0)
26793 width = 1;
26794 else if (width > 4)
26795 width = 4;
26796 it->pixel_width = base_width * width;
26797 len = 0;
26798 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26799 }
26800 else
26801 {
26802 char buf[7];
26803 const char *str;
26804 unsigned int code[6];
26805 int upper_len;
26806 int ascent, descent;
26807 struct font_metrics metrics_upper, metrics_lower;
26808
26809 face = FACE_FROM_ID (it->f, face_id);
26810 font = face->font ? face->font : FRAME_FONT (it->f);
26811 prepare_face_for_display (it->f, face);
26812
26813 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26814 {
26815 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26816 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26817 if (CONSP (acronym))
26818 acronym = XCAR (acronym);
26819 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26820 }
26821 else
26822 {
26823 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26824 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
26825 str = buf;
26826 }
26827 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26828 code[len] = font->driver->encode_char (font, str[len]);
26829 upper_len = (len + 1) / 2;
26830 font->driver->text_extents (font, code, upper_len,
26831 &metrics_upper);
26832 font->driver->text_extents (font, code + upper_len, len - upper_len,
26833 &metrics_lower);
26834
26835
26836
26837 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26838 width = max (metrics_upper.width, metrics_lower.width) + 4;
26839 upper_xoff = upper_yoff = 2; /* the typical case */
26840 if (base_width >= width)
26841 {
26842 /* Align the upper to the left, the lower to the right. */
26843 it->pixel_width = base_width;
26844 lower_xoff = base_width - 2 - metrics_lower.width;
26845 }
26846 else
26847 {
26848 /* Center the shorter one. */
26849 it->pixel_width = width;
26850 if (metrics_upper.width >= metrics_lower.width)
26851 lower_xoff = (width - metrics_lower.width) / 2;
26852 else
26853 {
26854 /* FIXME: This code doesn't look right. It formerly was
26855 missing the "lower_xoff = 0;", which couldn't have
26856 been right since it left lower_xoff uninitialized. */
26857 lower_xoff = 0;
26858 upper_xoff = (width - metrics_upper.width) / 2;
26859 }
26860 }
26861
26862 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26863 top, bottom, and between upper and lower strings. */
26864 height = (metrics_upper.ascent + metrics_upper.descent
26865 + metrics_lower.ascent + metrics_lower.descent) + 5;
26866 /* Center vertically.
26867 H:base_height, D:base_descent
26868 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26869
26870 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26871 descent = D - H/2 + h/2;
26872 lower_yoff = descent - 2 - ld;
26873 upper_yoff = lower_yoff - la - 1 - ud; */
26874 ascent = - (it->descent - (base_height + height + 1) / 2);
26875 descent = it->descent - (base_height - height) / 2;
26876 lower_yoff = descent - 2 - metrics_lower.descent;
26877 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26878 - metrics_upper.descent);
26879 /* Don't make the height shorter than the base height. */
26880 if (height > base_height)
26881 {
26882 it->ascent = ascent;
26883 it->descent = descent;
26884 }
26885 }
26886
26887 it->phys_ascent = it->ascent;
26888 it->phys_descent = it->descent;
26889 if (it->glyph_row)
26890 append_glyphless_glyph (it, face_id, for_no_font, len,
26891 upper_xoff, upper_yoff,
26892 lower_xoff, lower_yoff);
26893 it->nglyphs = 1;
26894 take_vertical_position_into_account (it);
26895 }
26896
26897
26898 /* RIF:
26899 Produce glyphs/get display metrics for the display element IT is
26900 loaded with. See the description of struct it in dispextern.h
26901 for an overview of struct it. */
26902
26903 void
26904 x_produce_glyphs (struct it *it)
26905 {
26906 int extra_line_spacing = it->extra_line_spacing;
26907
26908 it->glyph_not_available_p = false;
26909
26910 if (it->what == IT_CHARACTER)
26911 {
26912 XChar2b char2b;
26913 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26914 struct font *font = face->font;
26915 struct font_metrics *pcm = NULL;
26916 int boff; /* Baseline offset. */
26917
26918 if (font == NULL)
26919 {
26920 /* When no suitable font is found, display this character by
26921 the method specified in the first extra slot of
26922 Vglyphless_char_display. */
26923 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26924
26925 eassert (it->what == IT_GLYPHLESS);
26926 produce_glyphless_glyph (it, true,
26927 STRINGP (acronym) ? acronym : Qnil);
26928 goto done;
26929 }
26930
26931 boff = font->baseline_offset;
26932 if (font->vertical_centering)
26933 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26934
26935 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26936 {
26937 it->nglyphs = 1;
26938
26939 if (it->override_ascent >= 0)
26940 {
26941 it->ascent = it->override_ascent;
26942 it->descent = it->override_descent;
26943 boff = it->override_boff;
26944 }
26945 else
26946 {
26947 it->ascent = FONT_BASE (font) + boff;
26948 it->descent = FONT_DESCENT (font) - boff;
26949 }
26950
26951 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26952 {
26953 pcm = get_per_char_metric (font, &char2b);
26954 if (pcm->width == 0
26955 && pcm->rbearing == 0 && pcm->lbearing == 0)
26956 pcm = NULL;
26957 }
26958
26959 if (pcm)
26960 {
26961 it->phys_ascent = pcm->ascent + boff;
26962 it->phys_descent = pcm->descent - boff;
26963 it->pixel_width = pcm->width;
26964 /* Don't use font-global values for ascent and descent
26965 if they result in an exceedingly large line height. */
26966 if (it->override_ascent < 0)
26967 {
26968 if (FONT_TOO_HIGH (font))
26969 {
26970 it->ascent = it->phys_ascent;
26971 it->descent = it->phys_descent;
26972 /* These limitations are enforced by an
26973 assertion near the end of this function. */
26974 if (it->ascent < 0)
26975 it->ascent = 0;
26976 if (it->descent < 0)
26977 it->descent = 0;
26978 }
26979 }
26980 }
26981 else
26982 {
26983 it->glyph_not_available_p = true;
26984 it->phys_ascent = it->ascent;
26985 it->phys_descent = it->descent;
26986 it->pixel_width = font->space_width;
26987 }
26988
26989 if (it->constrain_row_ascent_descent_p)
26990 {
26991 if (it->descent > it->max_descent)
26992 {
26993 it->ascent += it->descent - it->max_descent;
26994 it->descent = it->max_descent;
26995 }
26996 if (it->ascent > it->max_ascent)
26997 {
26998 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26999 it->ascent = it->max_ascent;
27000 }
27001 it->phys_ascent = min (it->phys_ascent, it->ascent);
27002 it->phys_descent = min (it->phys_descent, it->descent);
27003 extra_line_spacing = 0;
27004 }
27005
27006 /* If this is a space inside a region of text with
27007 `space-width' property, change its width. */
27008 bool stretched_p
27009 = it->char_to_display == ' ' && !NILP (it->space_width);
27010 if (stretched_p)
27011 it->pixel_width *= XFLOATINT (it->space_width);
27012
27013 /* If face has a box, add the box thickness to the character
27014 height. If character has a box line to the left and/or
27015 right, add the box line width to the character's width. */
27016 if (face->box != FACE_NO_BOX)
27017 {
27018 int thick = face->box_line_width;
27019
27020 if (thick > 0)
27021 {
27022 it->ascent += thick;
27023 it->descent += thick;
27024 }
27025 else
27026 thick = -thick;
27027
27028 if (it->start_of_box_run_p)
27029 it->pixel_width += thick;
27030 if (it->end_of_box_run_p)
27031 it->pixel_width += thick;
27032 }
27033
27034 /* If face has an overline, add the height of the overline
27035 (1 pixel) and a 1 pixel margin to the character height. */
27036 if (face->overline_p)
27037 it->ascent += overline_margin;
27038
27039 if (it->constrain_row_ascent_descent_p)
27040 {
27041 if (it->ascent > it->max_ascent)
27042 it->ascent = it->max_ascent;
27043 if (it->descent > it->max_descent)
27044 it->descent = it->max_descent;
27045 }
27046
27047 take_vertical_position_into_account (it);
27048
27049 /* If we have to actually produce glyphs, do it. */
27050 if (it->glyph_row)
27051 {
27052 if (stretched_p)
27053 {
27054 /* Translate a space with a `space-width' property
27055 into a stretch glyph. */
27056 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27057 / FONT_HEIGHT (font));
27058 append_stretch_glyph (it, it->object, it->pixel_width,
27059 it->ascent + it->descent, ascent);
27060 }
27061 else
27062 append_glyph (it);
27063
27064 /* If characters with lbearing or rbearing are displayed
27065 in this line, record that fact in a flag of the
27066 glyph row. This is used to optimize X output code. */
27067 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27068 it->glyph_row->contains_overlapping_glyphs_p = true;
27069 }
27070 if (! stretched_p && it->pixel_width == 0)
27071 /* We assure that all visible glyphs have at least 1-pixel
27072 width. */
27073 it->pixel_width = 1;
27074 }
27075 else if (it->char_to_display == '\n')
27076 {
27077 /* A newline has no width, but we need the height of the
27078 line. But if previous part of the line sets a height,
27079 don't increase that height. */
27080
27081 Lisp_Object height;
27082 Lisp_Object total_height = Qnil;
27083
27084 it->override_ascent = -1;
27085 it->pixel_width = 0;
27086 it->nglyphs = 0;
27087
27088 height = get_it_property (it, Qline_height);
27089 /* Split (line-height total-height) list. */
27090 if (CONSP (height)
27091 && CONSP (XCDR (height))
27092 && NILP (XCDR (XCDR (height))))
27093 {
27094 total_height = XCAR (XCDR (height));
27095 height = XCAR (height);
27096 }
27097 height = calc_line_height_property (it, height, font, boff, true);
27098
27099 if (it->override_ascent >= 0)
27100 {
27101 it->ascent = it->override_ascent;
27102 it->descent = it->override_descent;
27103 boff = it->override_boff;
27104 }
27105 else
27106 {
27107 if (FONT_TOO_HIGH (font))
27108 {
27109 it->ascent = font->pixel_size + boff - 1;
27110 it->descent = -boff + 1;
27111 if (it->descent < 0)
27112 it->descent = 0;
27113 }
27114 else
27115 {
27116 it->ascent = FONT_BASE (font) + boff;
27117 it->descent = FONT_DESCENT (font) - boff;
27118 }
27119 }
27120
27121 if (EQ (height, Qt))
27122 {
27123 if (it->descent > it->max_descent)
27124 {
27125 it->ascent += it->descent - it->max_descent;
27126 it->descent = it->max_descent;
27127 }
27128 if (it->ascent > it->max_ascent)
27129 {
27130 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27131 it->ascent = it->max_ascent;
27132 }
27133 it->phys_ascent = min (it->phys_ascent, it->ascent);
27134 it->phys_descent = min (it->phys_descent, it->descent);
27135 it->constrain_row_ascent_descent_p = true;
27136 extra_line_spacing = 0;
27137 }
27138 else
27139 {
27140 Lisp_Object spacing;
27141
27142 it->phys_ascent = it->ascent;
27143 it->phys_descent = it->descent;
27144
27145 if ((it->max_ascent > 0 || it->max_descent > 0)
27146 && face->box != FACE_NO_BOX
27147 && face->box_line_width > 0)
27148 {
27149 it->ascent += face->box_line_width;
27150 it->descent += face->box_line_width;
27151 }
27152 if (!NILP (height)
27153 && XINT (height) > it->ascent + it->descent)
27154 it->ascent = XINT (height) - it->descent;
27155
27156 if (!NILP (total_height))
27157 spacing = calc_line_height_property (it, total_height, font,
27158 boff, false);
27159 else
27160 {
27161 spacing = get_it_property (it, Qline_spacing);
27162 spacing = calc_line_height_property (it, spacing, font,
27163 boff, false);
27164 }
27165 if (INTEGERP (spacing))
27166 {
27167 extra_line_spacing = XINT (spacing);
27168 if (!NILP (total_height))
27169 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
27170 }
27171 }
27172 }
27173 else /* i.e. (it->char_to_display == '\t') */
27174 {
27175 if (font->space_width > 0)
27176 {
27177 int tab_width = it->tab_width * font->space_width;
27178 int x = it->current_x + it->continuation_lines_width;
27179 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
27180
27181 /* If the distance from the current position to the next tab
27182 stop is less than a space character width, use the
27183 tab stop after that. */
27184 if (next_tab_x - x < font->space_width)
27185 next_tab_x += tab_width;
27186
27187 it->pixel_width = next_tab_x - x;
27188 it->nglyphs = 1;
27189 if (FONT_TOO_HIGH (font))
27190 {
27191 if (get_char_glyph_code (' ', font, &char2b))
27192 {
27193 pcm = get_per_char_metric (font, &char2b);
27194 if (pcm->width == 0
27195 && pcm->rbearing == 0 && pcm->lbearing == 0)
27196 pcm = NULL;
27197 }
27198
27199 if (pcm)
27200 {
27201 it->ascent = pcm->ascent + boff;
27202 it->descent = pcm->descent - boff;
27203 }
27204 else
27205 {
27206 it->ascent = font->pixel_size + boff - 1;
27207 it->descent = -boff + 1;
27208 }
27209 if (it->ascent < 0)
27210 it->ascent = 0;
27211 if (it->descent < 0)
27212 it->descent = 0;
27213 }
27214 else
27215 {
27216 it->ascent = FONT_BASE (font) + boff;
27217 it->descent = FONT_DESCENT (font) - boff;
27218 }
27219 it->phys_ascent = it->ascent;
27220 it->phys_descent = it->descent;
27221
27222 if (it->glyph_row)
27223 {
27224 append_stretch_glyph (it, it->object, it->pixel_width,
27225 it->ascent + it->descent, it->ascent);
27226 }
27227 }
27228 else
27229 {
27230 it->pixel_width = 0;
27231 it->nglyphs = 1;
27232 }
27233 }
27234
27235 if (FONT_TOO_HIGH (font))
27236 {
27237 int font_ascent, font_descent;
27238
27239 /* For very large fonts, where we ignore the declared font
27240 dimensions, and go by per-character metrics instead,
27241 don't let the row ascent and descent values (and the row
27242 height computed from them) be smaller than the "normal"
27243 character metrics. This avoids unpleasant effects
27244 whereby lines on display would change their height
27245 depending on which characters are shown. */
27246 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27247 it->max_ascent = max (it->max_ascent, font_ascent);
27248 it->max_descent = max (it->max_descent, font_descent);
27249 }
27250 }
27251 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
27252 {
27253 /* A static composition.
27254
27255 Note: A composition is represented as one glyph in the
27256 glyph matrix. There are no padding glyphs.
27257
27258 Important note: pixel_width, ascent, and descent are the
27259 values of what is drawn by draw_glyphs (i.e. the values of
27260 the overall glyphs composed). */
27261 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27262 int boff; /* baseline offset */
27263 struct composition *cmp = composition_table[it->cmp_it.id];
27264 int glyph_len = cmp->glyph_len;
27265 struct font *font = face->font;
27266
27267 it->nglyphs = 1;
27268
27269 /* If we have not yet calculated pixel size data of glyphs of
27270 the composition for the current face font, calculate them
27271 now. Theoretically, we have to check all fonts for the
27272 glyphs, but that requires much time and memory space. So,
27273 here we check only the font of the first glyph. This may
27274 lead to incorrect display, but it's very rare, and C-l
27275 (recenter-top-bottom) can correct the display anyway. */
27276 if (! cmp->font || cmp->font != font)
27277 {
27278 /* Ascent and descent of the font of the first character
27279 of this composition (adjusted by baseline offset).
27280 Ascent and descent of overall glyphs should not be less
27281 than these, respectively. */
27282 int font_ascent, font_descent, font_height;
27283 /* Bounding box of the overall glyphs. */
27284 int leftmost, rightmost, lowest, highest;
27285 int lbearing, rbearing;
27286 int i, width, ascent, descent;
27287 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
27288 XChar2b char2b;
27289 struct font_metrics *pcm;
27290 ptrdiff_t pos;
27291
27292 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
27293 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
27294 break;
27295 bool right_padded = glyph_len < cmp->glyph_len;
27296 for (i = 0; i < glyph_len; i++)
27297 {
27298 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
27299 break;
27300 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27301 }
27302 bool left_padded = i > 0;
27303
27304 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
27305 : IT_CHARPOS (*it));
27306 /* If no suitable font is found, use the default font. */
27307 bool font_not_found_p = font == NULL;
27308 if (font_not_found_p)
27309 {
27310 face = face->ascii_face;
27311 font = face->font;
27312 }
27313 boff = font->baseline_offset;
27314 if (font->vertical_centering)
27315 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27316 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27317 font_ascent += boff;
27318 font_descent -= boff;
27319 font_height = font_ascent + font_descent;
27320
27321 cmp->font = font;
27322
27323 pcm = NULL;
27324 if (! font_not_found_p)
27325 {
27326 get_char_face_and_encoding (it->f, c, it->face_id,
27327 &char2b, false);
27328 pcm = get_per_char_metric (font, &char2b);
27329 }
27330
27331 /* Initialize the bounding box. */
27332 if (pcm)
27333 {
27334 width = cmp->glyph_len > 0 ? pcm->width : 0;
27335 ascent = pcm->ascent;
27336 descent = pcm->descent;
27337 lbearing = pcm->lbearing;
27338 rbearing = pcm->rbearing;
27339 }
27340 else
27341 {
27342 width = cmp->glyph_len > 0 ? font->space_width : 0;
27343 ascent = FONT_BASE (font);
27344 descent = FONT_DESCENT (font);
27345 lbearing = 0;
27346 rbearing = width;
27347 }
27348
27349 rightmost = width;
27350 leftmost = 0;
27351 lowest = - descent + boff;
27352 highest = ascent + boff;
27353
27354 if (! font_not_found_p
27355 && font->default_ascent
27356 && CHAR_TABLE_P (Vuse_default_ascent)
27357 && !NILP (Faref (Vuse_default_ascent,
27358 make_number (it->char_to_display))))
27359 highest = font->default_ascent + boff;
27360
27361 /* Draw the first glyph at the normal position. It may be
27362 shifted to right later if some other glyphs are drawn
27363 at the left. */
27364 cmp->offsets[i * 2] = 0;
27365 cmp->offsets[i * 2 + 1] = boff;
27366 cmp->lbearing = lbearing;
27367 cmp->rbearing = rbearing;
27368
27369 /* Set cmp->offsets for the remaining glyphs. */
27370 for (i++; i < glyph_len; i++)
27371 {
27372 int left, right, btm, top;
27373 int ch = COMPOSITION_GLYPH (cmp, i);
27374 int face_id;
27375 struct face *this_face;
27376
27377 if (ch == '\t')
27378 ch = ' ';
27379 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27380 this_face = FACE_FROM_ID (it->f, face_id);
27381 font = this_face->font;
27382
27383 if (font == NULL)
27384 pcm = NULL;
27385 else
27386 {
27387 get_char_face_and_encoding (it->f, ch, face_id,
27388 &char2b, false);
27389 pcm = get_per_char_metric (font, &char2b);
27390 }
27391 if (! pcm)
27392 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27393 else
27394 {
27395 width = pcm->width;
27396 ascent = pcm->ascent;
27397 descent = pcm->descent;
27398 lbearing = pcm->lbearing;
27399 rbearing = pcm->rbearing;
27400 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27401 {
27402 /* Relative composition with or without
27403 alternate chars. */
27404 left = (leftmost + rightmost - width) / 2;
27405 btm = - descent + boff;
27406 if (font->relative_compose
27407 && (! CHAR_TABLE_P (Vignore_relative_composition)
27408 || NILP (Faref (Vignore_relative_composition,
27409 make_number (ch)))))
27410 {
27411
27412 if (- descent >= font->relative_compose)
27413 /* One extra pixel between two glyphs. */
27414 btm = highest + 1;
27415 else if (ascent <= 0)
27416 /* One extra pixel between two glyphs. */
27417 btm = lowest - 1 - ascent - descent;
27418 }
27419 }
27420 else
27421 {
27422 /* A composition rule is specified by an integer
27423 value that encodes global and new reference
27424 points (GREF and NREF). GREF and NREF are
27425 specified by numbers as below:
27426
27427 0---1---2 -- ascent
27428 | |
27429 | |
27430 | |
27431 9--10--11 -- center
27432 | |
27433 ---3---4---5--- baseline
27434 | |
27435 6---7---8 -- descent
27436 */
27437 int rule = COMPOSITION_RULE (cmp, i);
27438 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27439
27440 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27441 grefx = gref % 3, nrefx = nref % 3;
27442 grefy = gref / 3, nrefy = nref / 3;
27443 if (xoff)
27444 xoff = font_height * (xoff - 128) / 256;
27445 if (yoff)
27446 yoff = font_height * (yoff - 128) / 256;
27447
27448 left = (leftmost
27449 + grefx * (rightmost - leftmost) / 2
27450 - nrefx * width / 2
27451 + xoff);
27452
27453 btm = ((grefy == 0 ? highest
27454 : grefy == 1 ? 0
27455 : grefy == 2 ? lowest
27456 : (highest + lowest) / 2)
27457 - (nrefy == 0 ? ascent + descent
27458 : nrefy == 1 ? descent - boff
27459 : nrefy == 2 ? 0
27460 : (ascent + descent) / 2)
27461 + yoff);
27462 }
27463
27464 cmp->offsets[i * 2] = left;
27465 cmp->offsets[i * 2 + 1] = btm + descent;
27466
27467 /* Update the bounding box of the overall glyphs. */
27468 if (width > 0)
27469 {
27470 right = left + width;
27471 if (left < leftmost)
27472 leftmost = left;
27473 if (right > rightmost)
27474 rightmost = right;
27475 }
27476 top = btm + descent + ascent;
27477 if (top > highest)
27478 highest = top;
27479 if (btm < lowest)
27480 lowest = btm;
27481
27482 if (cmp->lbearing > left + lbearing)
27483 cmp->lbearing = left + lbearing;
27484 if (cmp->rbearing < left + rbearing)
27485 cmp->rbearing = left + rbearing;
27486 }
27487 }
27488
27489 /* If there are glyphs whose x-offsets are negative,
27490 shift all glyphs to the right and make all x-offsets
27491 non-negative. */
27492 if (leftmost < 0)
27493 {
27494 for (i = 0; i < cmp->glyph_len; i++)
27495 cmp->offsets[i * 2] -= leftmost;
27496 rightmost -= leftmost;
27497 cmp->lbearing -= leftmost;
27498 cmp->rbearing -= leftmost;
27499 }
27500
27501 if (left_padded && cmp->lbearing < 0)
27502 {
27503 for (i = 0; i < cmp->glyph_len; i++)
27504 cmp->offsets[i * 2] -= cmp->lbearing;
27505 rightmost -= cmp->lbearing;
27506 cmp->rbearing -= cmp->lbearing;
27507 cmp->lbearing = 0;
27508 }
27509 if (right_padded && rightmost < cmp->rbearing)
27510 {
27511 rightmost = cmp->rbearing;
27512 }
27513
27514 cmp->pixel_width = rightmost;
27515 cmp->ascent = highest;
27516 cmp->descent = - lowest;
27517 if (cmp->ascent < font_ascent)
27518 cmp->ascent = font_ascent;
27519 if (cmp->descent < font_descent)
27520 cmp->descent = font_descent;
27521 }
27522
27523 if (it->glyph_row
27524 && (cmp->lbearing < 0
27525 || cmp->rbearing > cmp->pixel_width))
27526 it->glyph_row->contains_overlapping_glyphs_p = true;
27527
27528 it->pixel_width = cmp->pixel_width;
27529 it->ascent = it->phys_ascent = cmp->ascent;
27530 it->descent = it->phys_descent = cmp->descent;
27531 if (face->box != FACE_NO_BOX)
27532 {
27533 int thick = face->box_line_width;
27534
27535 if (thick > 0)
27536 {
27537 it->ascent += thick;
27538 it->descent += thick;
27539 }
27540 else
27541 thick = - thick;
27542
27543 if (it->start_of_box_run_p)
27544 it->pixel_width += thick;
27545 if (it->end_of_box_run_p)
27546 it->pixel_width += thick;
27547 }
27548
27549 /* If face has an overline, add the height of the overline
27550 (1 pixel) and a 1 pixel margin to the character height. */
27551 if (face->overline_p)
27552 it->ascent += overline_margin;
27553
27554 take_vertical_position_into_account (it);
27555 if (it->ascent < 0)
27556 it->ascent = 0;
27557 if (it->descent < 0)
27558 it->descent = 0;
27559
27560 if (it->glyph_row && cmp->glyph_len > 0)
27561 append_composite_glyph (it);
27562 }
27563 else if (it->what == IT_COMPOSITION)
27564 {
27565 /* A dynamic (automatic) composition. */
27566 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27567 Lisp_Object gstring;
27568 struct font_metrics metrics;
27569
27570 it->nglyphs = 1;
27571
27572 gstring = composition_gstring_from_id (it->cmp_it.id);
27573 it->pixel_width
27574 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27575 &metrics);
27576 if (it->glyph_row
27577 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27578 it->glyph_row->contains_overlapping_glyphs_p = true;
27579 it->ascent = it->phys_ascent = metrics.ascent;
27580 it->descent = it->phys_descent = metrics.descent;
27581 if (face->box != FACE_NO_BOX)
27582 {
27583 int thick = face->box_line_width;
27584
27585 if (thick > 0)
27586 {
27587 it->ascent += thick;
27588 it->descent += thick;
27589 }
27590 else
27591 thick = - thick;
27592
27593 if (it->start_of_box_run_p)
27594 it->pixel_width += thick;
27595 if (it->end_of_box_run_p)
27596 it->pixel_width += thick;
27597 }
27598 /* If face has an overline, add the height of the overline
27599 (1 pixel) and a 1 pixel margin to the character height. */
27600 if (face->overline_p)
27601 it->ascent += overline_margin;
27602 take_vertical_position_into_account (it);
27603 if (it->ascent < 0)
27604 it->ascent = 0;
27605 if (it->descent < 0)
27606 it->descent = 0;
27607
27608 if (it->glyph_row)
27609 append_composite_glyph (it);
27610 }
27611 else if (it->what == IT_GLYPHLESS)
27612 produce_glyphless_glyph (it, false, Qnil);
27613 else if (it->what == IT_IMAGE)
27614 produce_image_glyph (it);
27615 else if (it->what == IT_STRETCH)
27616 produce_stretch_glyph (it);
27617 else if (it->what == IT_XWIDGET)
27618 produce_xwidget_glyph (it);
27619
27620 done:
27621 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27622 because this isn't true for images with `:ascent 100'. */
27623 eassert (it->ascent >= 0 && it->descent >= 0);
27624 if (it->area == TEXT_AREA)
27625 it->current_x += it->pixel_width;
27626
27627 if (extra_line_spacing > 0)
27628 {
27629 it->descent += extra_line_spacing;
27630 if (extra_line_spacing > it->max_extra_line_spacing)
27631 it->max_extra_line_spacing = extra_line_spacing;
27632 }
27633
27634 it->max_ascent = max (it->max_ascent, it->ascent);
27635 it->max_descent = max (it->max_descent, it->descent);
27636 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27637 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27638 }
27639
27640 /* EXPORT for RIF:
27641 Output LEN glyphs starting at START at the nominal cursor position.
27642 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27643 being updated, and UPDATED_AREA is the area of that row being updated. */
27644
27645 void
27646 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27647 struct glyph *start, enum glyph_row_area updated_area, int len)
27648 {
27649 int x, hpos, chpos = w->phys_cursor.hpos;
27650
27651 eassert (updated_row);
27652 /* When the window is hscrolled, cursor hpos can legitimately be out
27653 of bounds, but we draw the cursor at the corresponding window
27654 margin in that case. */
27655 if (!updated_row->reversed_p && chpos < 0)
27656 chpos = 0;
27657 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27658 chpos = updated_row->used[TEXT_AREA] - 1;
27659
27660 block_input ();
27661
27662 /* Write glyphs. */
27663
27664 hpos = start - updated_row->glyphs[updated_area];
27665 x = draw_glyphs (w, w->output_cursor.x,
27666 updated_row, updated_area,
27667 hpos, hpos + len,
27668 DRAW_NORMAL_TEXT, 0);
27669
27670 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27671 if (updated_area == TEXT_AREA
27672 && w->phys_cursor_on_p
27673 && w->phys_cursor.vpos == w->output_cursor.vpos
27674 && chpos >= hpos
27675 && chpos < hpos + len)
27676 w->phys_cursor_on_p = false;
27677
27678 unblock_input ();
27679
27680 /* Advance the output cursor. */
27681 w->output_cursor.hpos += len;
27682 w->output_cursor.x = x;
27683 }
27684
27685
27686 /* EXPORT for RIF:
27687 Insert LEN glyphs from START at the nominal cursor position. */
27688
27689 void
27690 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27691 struct glyph *start, enum glyph_row_area updated_area, int len)
27692 {
27693 struct frame *f;
27694 int line_height, shift_by_width, shifted_region_width;
27695 struct glyph_row *row;
27696 struct glyph *glyph;
27697 int frame_x, frame_y;
27698 ptrdiff_t hpos;
27699
27700 eassert (updated_row);
27701 block_input ();
27702 f = XFRAME (WINDOW_FRAME (w));
27703
27704 /* Get the height of the line we are in. */
27705 row = updated_row;
27706 line_height = row->height;
27707
27708 /* Get the width of the glyphs to insert. */
27709 shift_by_width = 0;
27710 for (glyph = start; glyph < start + len; ++glyph)
27711 shift_by_width += glyph->pixel_width;
27712
27713 /* Get the width of the region to shift right. */
27714 shifted_region_width = (window_box_width (w, updated_area)
27715 - w->output_cursor.x
27716 - shift_by_width);
27717
27718 /* Shift right. */
27719 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27720 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27721
27722 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27723 line_height, shift_by_width);
27724
27725 /* Write the glyphs. */
27726 hpos = start - row->glyphs[updated_area];
27727 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27728 hpos, hpos + len,
27729 DRAW_NORMAL_TEXT, 0);
27730
27731 /* Advance the output cursor. */
27732 w->output_cursor.hpos += len;
27733 w->output_cursor.x += shift_by_width;
27734 unblock_input ();
27735 }
27736
27737
27738 /* EXPORT for RIF:
27739 Erase the current text line from the nominal cursor position
27740 (inclusive) to pixel column TO_X (exclusive). The idea is that
27741 everything from TO_X onward is already erased.
27742
27743 TO_X is a pixel position relative to UPDATED_AREA of currently
27744 updated window W. TO_X == -1 means clear to the end of this area. */
27745
27746 void
27747 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27748 enum glyph_row_area updated_area, int to_x)
27749 {
27750 struct frame *f;
27751 int max_x, min_y, max_y;
27752 int from_x, from_y, to_y;
27753
27754 eassert (updated_row);
27755 f = XFRAME (w->frame);
27756
27757 if (updated_row->full_width_p)
27758 max_x = (WINDOW_PIXEL_WIDTH (w)
27759 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27760 else
27761 max_x = window_box_width (w, updated_area);
27762 max_y = window_text_bottom_y (w);
27763
27764 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27765 of window. For TO_X > 0, truncate to end of drawing area. */
27766 if (to_x == 0)
27767 return;
27768 else if (to_x < 0)
27769 to_x = max_x;
27770 else
27771 to_x = min (to_x, max_x);
27772
27773 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27774
27775 /* Notice if the cursor will be cleared by this operation. */
27776 if (!updated_row->full_width_p)
27777 notice_overwritten_cursor (w, updated_area,
27778 w->output_cursor.x, -1,
27779 updated_row->y,
27780 MATRIX_ROW_BOTTOM_Y (updated_row));
27781
27782 from_x = w->output_cursor.x;
27783
27784 /* Translate to frame coordinates. */
27785 if (updated_row->full_width_p)
27786 {
27787 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27788 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27789 }
27790 else
27791 {
27792 int area_left = window_box_left (w, updated_area);
27793 from_x += area_left;
27794 to_x += area_left;
27795 }
27796
27797 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27798 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27799 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27800
27801 /* Prevent inadvertently clearing to end of the X window. */
27802 if (to_x > from_x && to_y > from_y)
27803 {
27804 block_input ();
27805 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27806 to_x - from_x, to_y - from_y);
27807 unblock_input ();
27808 }
27809 }
27810
27811 #endif /* HAVE_WINDOW_SYSTEM */
27812
27813
27814 \f
27815 /***********************************************************************
27816 Cursor types
27817 ***********************************************************************/
27818
27819 /* Value is the internal representation of the specified cursor type
27820 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27821 of the bar cursor. */
27822
27823 static enum text_cursor_kinds
27824 get_specified_cursor_type (Lisp_Object arg, int *width)
27825 {
27826 enum text_cursor_kinds type;
27827
27828 if (NILP (arg))
27829 return NO_CURSOR;
27830
27831 if (EQ (arg, Qbox))
27832 return FILLED_BOX_CURSOR;
27833
27834 if (EQ (arg, Qhollow))
27835 return HOLLOW_BOX_CURSOR;
27836
27837 if (EQ (arg, Qbar))
27838 {
27839 *width = 2;
27840 return BAR_CURSOR;
27841 }
27842
27843 if (CONSP (arg)
27844 && EQ (XCAR (arg), Qbar)
27845 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27846 {
27847 *width = XINT (XCDR (arg));
27848 return BAR_CURSOR;
27849 }
27850
27851 if (EQ (arg, Qhbar))
27852 {
27853 *width = 2;
27854 return HBAR_CURSOR;
27855 }
27856
27857 if (CONSP (arg)
27858 && EQ (XCAR (arg), Qhbar)
27859 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27860 {
27861 *width = XINT (XCDR (arg));
27862 return HBAR_CURSOR;
27863 }
27864
27865 /* Treat anything unknown as "hollow box cursor".
27866 It was bad to signal an error; people have trouble fixing
27867 .Xdefaults with Emacs, when it has something bad in it. */
27868 type = HOLLOW_BOX_CURSOR;
27869
27870 return type;
27871 }
27872
27873 /* Set the default cursor types for specified frame. */
27874 void
27875 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27876 {
27877 int width = 1;
27878 Lisp_Object tem;
27879
27880 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27881 FRAME_CURSOR_WIDTH (f) = width;
27882
27883 /* By default, set up the blink-off state depending on the on-state. */
27884
27885 tem = Fassoc (arg, Vblink_cursor_alist);
27886 if (!NILP (tem))
27887 {
27888 FRAME_BLINK_OFF_CURSOR (f)
27889 = get_specified_cursor_type (XCDR (tem), &width);
27890 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27891 }
27892 else
27893 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27894
27895 /* Make sure the cursor gets redrawn. */
27896 f->cursor_type_changed = true;
27897 }
27898
27899
27900 #ifdef HAVE_WINDOW_SYSTEM
27901
27902 /* Return the cursor we want to be displayed in window W. Return
27903 width of bar/hbar cursor through WIDTH arg. Return with
27904 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
27905 (i.e. if the `system caret' should track this cursor).
27906
27907 In a mini-buffer window, we want the cursor only to appear if we
27908 are reading input from this window. For the selected window, we
27909 want the cursor type given by the frame parameter or buffer local
27910 setting of cursor-type. If explicitly marked off, draw no cursor.
27911 In all other cases, we want a hollow box cursor. */
27912
27913 static enum text_cursor_kinds
27914 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27915 bool *active_cursor)
27916 {
27917 struct frame *f = XFRAME (w->frame);
27918 struct buffer *b = XBUFFER (w->contents);
27919 int cursor_type = DEFAULT_CURSOR;
27920 Lisp_Object alt_cursor;
27921 bool non_selected = false;
27922
27923 *active_cursor = true;
27924
27925 /* Echo area */
27926 if (cursor_in_echo_area
27927 && FRAME_HAS_MINIBUF_P (f)
27928 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27929 {
27930 if (w == XWINDOW (echo_area_window))
27931 {
27932 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27933 {
27934 *width = FRAME_CURSOR_WIDTH (f);
27935 return FRAME_DESIRED_CURSOR (f);
27936 }
27937 else
27938 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27939 }
27940
27941 *active_cursor = false;
27942 non_selected = true;
27943 }
27944
27945 /* Detect a nonselected window or nonselected frame. */
27946 else if (w != XWINDOW (f->selected_window)
27947 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27948 {
27949 *active_cursor = false;
27950
27951 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27952 return NO_CURSOR;
27953
27954 non_selected = true;
27955 }
27956
27957 /* Never display a cursor in a window in which cursor-type is nil. */
27958 if (NILP (BVAR (b, cursor_type)))
27959 return NO_CURSOR;
27960
27961 /* Get the normal cursor type for this window. */
27962 if (EQ (BVAR (b, cursor_type), Qt))
27963 {
27964 cursor_type = FRAME_DESIRED_CURSOR (f);
27965 *width = FRAME_CURSOR_WIDTH (f);
27966 }
27967 else
27968 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27969
27970 /* Use cursor-in-non-selected-windows instead
27971 for non-selected window or frame. */
27972 if (non_selected)
27973 {
27974 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27975 if (!EQ (Qt, alt_cursor))
27976 return get_specified_cursor_type (alt_cursor, width);
27977 /* t means modify the normal cursor type. */
27978 if (cursor_type == FILLED_BOX_CURSOR)
27979 cursor_type = HOLLOW_BOX_CURSOR;
27980 else if (cursor_type == BAR_CURSOR && *width > 1)
27981 --*width;
27982 return cursor_type;
27983 }
27984
27985 /* Use normal cursor if not blinked off. */
27986 if (!w->cursor_off_p)
27987 {
27988 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
27989 return NO_CURSOR;
27990 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27991 {
27992 if (cursor_type == FILLED_BOX_CURSOR)
27993 {
27994 /* Using a block cursor on large images can be very annoying.
27995 So use a hollow cursor for "large" images.
27996 If image is not transparent (no mask), also use hollow cursor. */
27997 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27998 if (img != NULL && IMAGEP (img->spec))
27999 {
28000 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
28001 where N = size of default frame font size.
28002 This should cover most of the "tiny" icons people may use. */
28003 if (!img->mask
28004 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
28005 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28006 cursor_type = HOLLOW_BOX_CURSOR;
28007 }
28008 }
28009 else if (cursor_type != NO_CURSOR)
28010 {
28011 /* Display current only supports BOX and HOLLOW cursors for images.
28012 So for now, unconditionally use a HOLLOW cursor when cursor is
28013 not a solid box cursor. */
28014 cursor_type = HOLLOW_BOX_CURSOR;
28015 }
28016 }
28017 return cursor_type;
28018 }
28019
28020 /* Cursor is blinked off, so determine how to "toggle" it. */
28021
28022 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28023 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
28024 return get_specified_cursor_type (XCDR (alt_cursor), width);
28025
28026 /* Then see if frame has specified a specific blink off cursor type. */
28027 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28028 {
28029 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28030 return FRAME_BLINK_OFF_CURSOR (f);
28031 }
28032
28033 #if false
28034 /* Some people liked having a permanently visible blinking cursor,
28035 while others had very strong opinions against it. So it was
28036 decided to remove it. KFS 2003-09-03 */
28037
28038 /* Finally perform built-in cursor blinking:
28039 filled box <-> hollow box
28040 wide [h]bar <-> narrow [h]bar
28041 narrow [h]bar <-> no cursor
28042 other type <-> no cursor */
28043
28044 if (cursor_type == FILLED_BOX_CURSOR)
28045 return HOLLOW_BOX_CURSOR;
28046
28047 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28048 {
28049 *width = 1;
28050 return cursor_type;
28051 }
28052 #endif
28053
28054 return NO_CURSOR;
28055 }
28056
28057
28058 /* Notice when the text cursor of window W has been completely
28059 overwritten by a drawing operation that outputs glyphs in AREA
28060 starting at X0 and ending at X1 in the line starting at Y0 and
28061 ending at Y1. X coordinates are area-relative. X1 < 0 means all
28062 the rest of the line after X0 has been written. Y coordinates
28063 are window-relative. */
28064
28065 static void
28066 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
28067 int x0, int x1, int y0, int y1)
28068 {
28069 int cx0, cx1, cy0, cy1;
28070 struct glyph_row *row;
28071
28072 if (!w->phys_cursor_on_p)
28073 return;
28074 if (area != TEXT_AREA)
28075 return;
28076
28077 if (w->phys_cursor.vpos < 0
28078 || w->phys_cursor.vpos >= w->current_matrix->nrows
28079 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
28080 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
28081 return;
28082
28083 if (row->cursor_in_fringe_p)
28084 {
28085 row->cursor_in_fringe_p = false;
28086 draw_fringe_bitmap (w, row, row->reversed_p);
28087 w->phys_cursor_on_p = false;
28088 return;
28089 }
28090
28091 cx0 = w->phys_cursor.x;
28092 cx1 = cx0 + w->phys_cursor_width;
28093 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
28094 return;
28095
28096 /* The cursor image will be completely removed from the
28097 screen if the output area intersects the cursor area in
28098 y-direction. When we draw in [y0 y1[, and some part of
28099 the cursor is at y < y0, that part must have been drawn
28100 before. When scrolling, the cursor is erased before
28101 actually scrolling, so we don't come here. When not
28102 scrolling, the rows above the old cursor row must have
28103 changed, and in this case these rows must have written
28104 over the cursor image.
28105
28106 Likewise if part of the cursor is below y1, with the
28107 exception of the cursor being in the first blank row at
28108 the buffer and window end because update_text_area
28109 doesn't draw that row. (Except when it does, but
28110 that's handled in update_text_area.) */
28111
28112 cy0 = w->phys_cursor.y;
28113 cy1 = cy0 + w->phys_cursor_height;
28114 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
28115 return;
28116
28117 w->phys_cursor_on_p = false;
28118 }
28119
28120 #endif /* HAVE_WINDOW_SYSTEM */
28121
28122 \f
28123 /************************************************************************
28124 Mouse Face
28125 ************************************************************************/
28126
28127 #ifdef HAVE_WINDOW_SYSTEM
28128
28129 /* EXPORT for RIF:
28130 Fix the display of area AREA of overlapping row ROW in window W
28131 with respect to the overlapping part OVERLAPS. */
28132
28133 void
28134 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
28135 enum glyph_row_area area, int overlaps)
28136 {
28137 int i, x;
28138
28139 block_input ();
28140
28141 x = 0;
28142 for (i = 0; i < row->used[area];)
28143 {
28144 if (row->glyphs[area][i].overlaps_vertically_p)
28145 {
28146 int start = i, start_x = x;
28147
28148 do
28149 {
28150 x += row->glyphs[area][i].pixel_width;
28151 ++i;
28152 }
28153 while (i < row->used[area]
28154 && row->glyphs[area][i].overlaps_vertically_p);
28155
28156 draw_glyphs (w, start_x, row, area,
28157 start, i,
28158 DRAW_NORMAL_TEXT, overlaps);
28159 }
28160 else
28161 {
28162 x += row->glyphs[area][i].pixel_width;
28163 ++i;
28164 }
28165 }
28166
28167 unblock_input ();
28168 }
28169
28170
28171 /* EXPORT:
28172 Draw the cursor glyph of window W in glyph row ROW. See the
28173 comment of draw_glyphs for the meaning of HL. */
28174
28175 void
28176 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
28177 enum draw_glyphs_face hl)
28178 {
28179 /* If cursor hpos is out of bounds, don't draw garbage. This can
28180 happen in mini-buffer windows when switching between echo area
28181 glyphs and mini-buffer. */
28182 if ((row->reversed_p
28183 ? (w->phys_cursor.hpos >= 0)
28184 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
28185 {
28186 bool on_p = w->phys_cursor_on_p;
28187 int x1;
28188 int hpos = w->phys_cursor.hpos;
28189
28190 /* When the window is hscrolled, cursor hpos can legitimately be
28191 out of bounds, but we draw the cursor at the corresponding
28192 window margin in that case. */
28193 if (!row->reversed_p && hpos < 0)
28194 hpos = 0;
28195 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28196 hpos = row->used[TEXT_AREA] - 1;
28197
28198 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
28199 hl, 0);
28200 w->phys_cursor_on_p = on_p;
28201
28202 if (hl == DRAW_CURSOR)
28203 w->phys_cursor_width = x1 - w->phys_cursor.x;
28204 /* When we erase the cursor, and ROW is overlapped by other
28205 rows, make sure that these overlapping parts of other rows
28206 are redrawn. */
28207 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
28208 {
28209 w->phys_cursor_width = x1 - w->phys_cursor.x;
28210
28211 if (row > w->current_matrix->rows
28212 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
28213 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
28214 OVERLAPS_ERASED_CURSOR);
28215
28216 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
28217 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
28218 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
28219 OVERLAPS_ERASED_CURSOR);
28220 }
28221 }
28222 }
28223
28224
28225 /* Erase the image of a cursor of window W from the screen. */
28226
28227 void
28228 erase_phys_cursor (struct window *w)
28229 {
28230 struct frame *f = XFRAME (w->frame);
28231 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28232 int hpos = w->phys_cursor.hpos;
28233 int vpos = w->phys_cursor.vpos;
28234 bool mouse_face_here_p = false;
28235 struct glyph_matrix *active_glyphs = w->current_matrix;
28236 struct glyph_row *cursor_row;
28237 struct glyph *cursor_glyph;
28238 enum draw_glyphs_face hl;
28239
28240 /* No cursor displayed or row invalidated => nothing to do on the
28241 screen. */
28242 if (w->phys_cursor_type == NO_CURSOR)
28243 goto mark_cursor_off;
28244
28245 /* VPOS >= active_glyphs->nrows means that window has been resized.
28246 Don't bother to erase the cursor. */
28247 if (vpos >= active_glyphs->nrows)
28248 goto mark_cursor_off;
28249
28250 /* If row containing cursor is marked invalid, there is nothing we
28251 can do. */
28252 cursor_row = MATRIX_ROW (active_glyphs, vpos);
28253 if (!cursor_row->enabled_p)
28254 goto mark_cursor_off;
28255
28256 /* If line spacing is > 0, old cursor may only be partially visible in
28257 window after split-window. So adjust visible height. */
28258 cursor_row->visible_height = min (cursor_row->visible_height,
28259 window_text_bottom_y (w) - cursor_row->y);
28260
28261 /* If row is completely invisible, don't attempt to delete a cursor which
28262 isn't there. This can happen if cursor is at top of a window, and
28263 we switch to a buffer with a header line in that window. */
28264 if (cursor_row->visible_height <= 0)
28265 goto mark_cursor_off;
28266
28267 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
28268 if (cursor_row->cursor_in_fringe_p)
28269 {
28270 cursor_row->cursor_in_fringe_p = false;
28271 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
28272 goto mark_cursor_off;
28273 }
28274
28275 /* This can happen when the new row is shorter than the old one.
28276 In this case, either draw_glyphs or clear_end_of_line
28277 should have cleared the cursor. Note that we wouldn't be
28278 able to erase the cursor in this case because we don't have a
28279 cursor glyph at hand. */
28280 if ((cursor_row->reversed_p
28281 ? (w->phys_cursor.hpos < 0)
28282 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
28283 goto mark_cursor_off;
28284
28285 /* When the window is hscrolled, cursor hpos can legitimately be out
28286 of bounds, but we draw the cursor at the corresponding window
28287 margin in that case. */
28288 if (!cursor_row->reversed_p && hpos < 0)
28289 hpos = 0;
28290 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
28291 hpos = cursor_row->used[TEXT_AREA] - 1;
28292
28293 /* If the cursor is in the mouse face area, redisplay that when
28294 we clear the cursor. */
28295 if (! NILP (hlinfo->mouse_face_window)
28296 && coords_in_mouse_face_p (w, hpos, vpos)
28297 /* Don't redraw the cursor's spot in mouse face if it is at the
28298 end of a line (on a newline). The cursor appears there, but
28299 mouse highlighting does not. */
28300 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
28301 mouse_face_here_p = true;
28302
28303 /* Maybe clear the display under the cursor. */
28304 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
28305 {
28306 int x, y;
28307 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
28308 int width;
28309
28310 cursor_glyph = get_phys_cursor_glyph (w);
28311 if (cursor_glyph == NULL)
28312 goto mark_cursor_off;
28313
28314 width = cursor_glyph->pixel_width;
28315 x = w->phys_cursor.x;
28316 if (x < 0)
28317 {
28318 width += x;
28319 x = 0;
28320 }
28321 width = min (width, window_box_width (w, TEXT_AREA) - x);
28322 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28323 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28324
28325 if (width > 0)
28326 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28327 }
28328
28329 /* Erase the cursor by redrawing the character underneath it. */
28330 if (mouse_face_here_p)
28331 hl = DRAW_MOUSE_FACE;
28332 else
28333 hl = DRAW_NORMAL_TEXT;
28334 draw_phys_cursor_glyph (w, cursor_row, hl);
28335
28336 mark_cursor_off:
28337 w->phys_cursor_on_p = false;
28338 w->phys_cursor_type = NO_CURSOR;
28339 }
28340
28341
28342 /* Display or clear cursor of window W. If !ON, clear the cursor.
28343 If ON, display the cursor; where to put the cursor is specified by
28344 HPOS, VPOS, X and Y. */
28345
28346 void
28347 display_and_set_cursor (struct window *w, bool on,
28348 int hpos, int vpos, int x, int y)
28349 {
28350 struct frame *f = XFRAME (w->frame);
28351 int new_cursor_type;
28352 int new_cursor_width;
28353 bool active_cursor;
28354 struct glyph_row *glyph_row;
28355 struct glyph *glyph;
28356
28357 /* This is pointless on invisible frames, and dangerous on garbaged
28358 windows and frames; in the latter case, the frame or window may
28359 be in the midst of changing its size, and x and y may be off the
28360 window. */
28361 if (! FRAME_VISIBLE_P (f)
28362 || FRAME_GARBAGED_P (f)
28363 || vpos >= w->current_matrix->nrows
28364 || hpos >= w->current_matrix->matrix_w)
28365 return;
28366
28367 /* If cursor is off and we want it off, return quickly. */
28368 if (!on && !w->phys_cursor_on_p)
28369 return;
28370
28371 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28372 /* If cursor row is not enabled, we don't really know where to
28373 display the cursor. */
28374 if (!glyph_row->enabled_p)
28375 {
28376 w->phys_cursor_on_p = false;
28377 return;
28378 }
28379
28380 glyph = NULL;
28381 if (!glyph_row->exact_window_width_line_p
28382 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
28383 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28384
28385 eassert (input_blocked_p ());
28386
28387 /* Set new_cursor_type to the cursor we want to be displayed. */
28388 new_cursor_type = get_window_cursor_type (w, glyph,
28389 &new_cursor_width, &active_cursor);
28390
28391 /* If cursor is currently being shown and we don't want it to be or
28392 it is in the wrong place, or the cursor type is not what we want,
28393 erase it. */
28394 if (w->phys_cursor_on_p
28395 && (!on
28396 || w->phys_cursor.x != x
28397 || w->phys_cursor.y != y
28398 /* HPOS can be negative in R2L rows whose
28399 exact_window_width_line_p flag is set (i.e. their newline
28400 would "overflow into the fringe"). */
28401 || hpos < 0
28402 || new_cursor_type != w->phys_cursor_type
28403 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28404 && new_cursor_width != w->phys_cursor_width)))
28405 erase_phys_cursor (w);
28406
28407 /* Don't check phys_cursor_on_p here because that flag is only set
28408 to false in some cases where we know that the cursor has been
28409 completely erased, to avoid the extra work of erasing the cursor
28410 twice. In other words, phys_cursor_on_p can be true and the cursor
28411 still not be visible, or it has only been partly erased. */
28412 if (on)
28413 {
28414 w->phys_cursor_ascent = glyph_row->ascent;
28415 w->phys_cursor_height = glyph_row->height;
28416
28417 /* Set phys_cursor_.* before x_draw_.* is called because some
28418 of them may need the information. */
28419 w->phys_cursor.x = x;
28420 w->phys_cursor.y = glyph_row->y;
28421 w->phys_cursor.hpos = hpos;
28422 w->phys_cursor.vpos = vpos;
28423 }
28424
28425 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28426 new_cursor_type, new_cursor_width,
28427 on, active_cursor);
28428 }
28429
28430
28431 /* Switch the display of W's cursor on or off, according to the value
28432 of ON. */
28433
28434 static void
28435 update_window_cursor (struct window *w, bool on)
28436 {
28437 /* Don't update cursor in windows whose frame is in the process
28438 of being deleted. */
28439 if (w->current_matrix)
28440 {
28441 int hpos = w->phys_cursor.hpos;
28442 int vpos = w->phys_cursor.vpos;
28443 struct glyph_row *row;
28444
28445 if (vpos >= w->current_matrix->nrows
28446 || hpos >= w->current_matrix->matrix_w)
28447 return;
28448
28449 row = MATRIX_ROW (w->current_matrix, vpos);
28450
28451 /* When the window is hscrolled, cursor hpos can legitimately be
28452 out of bounds, but we draw the cursor at the corresponding
28453 window margin in that case. */
28454 if (!row->reversed_p && hpos < 0)
28455 hpos = 0;
28456 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28457 hpos = row->used[TEXT_AREA] - 1;
28458
28459 block_input ();
28460 display_and_set_cursor (w, on, hpos, vpos,
28461 w->phys_cursor.x, w->phys_cursor.y);
28462 unblock_input ();
28463 }
28464 }
28465
28466
28467 /* Call update_window_cursor with parameter ON_P on all leaf windows
28468 in the window tree rooted at W. */
28469
28470 static void
28471 update_cursor_in_window_tree (struct window *w, bool on_p)
28472 {
28473 while (w)
28474 {
28475 if (WINDOWP (w->contents))
28476 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28477 else
28478 update_window_cursor (w, on_p);
28479
28480 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28481 }
28482 }
28483
28484
28485 /* EXPORT:
28486 Display the cursor on window W, or clear it, according to ON_P.
28487 Don't change the cursor's position. */
28488
28489 void
28490 x_update_cursor (struct frame *f, bool on_p)
28491 {
28492 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28493 }
28494
28495
28496 /* EXPORT:
28497 Clear the cursor of window W to background color, and mark the
28498 cursor as not shown. This is used when the text where the cursor
28499 is about to be rewritten. */
28500
28501 void
28502 x_clear_cursor (struct window *w)
28503 {
28504 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28505 update_window_cursor (w, false);
28506 }
28507
28508 #endif /* HAVE_WINDOW_SYSTEM */
28509
28510 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28511 and MSDOS. */
28512 static void
28513 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28514 int start_hpos, int end_hpos,
28515 enum draw_glyphs_face draw)
28516 {
28517 #ifdef HAVE_WINDOW_SYSTEM
28518 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28519 {
28520 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28521 return;
28522 }
28523 #endif
28524 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28525 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28526 #endif
28527 }
28528
28529 /* Display the active region described by mouse_face_* according to DRAW. */
28530
28531 static void
28532 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28533 {
28534 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28535 struct frame *f = XFRAME (WINDOW_FRAME (w));
28536
28537 if (/* If window is in the process of being destroyed, don't bother
28538 to do anything. */
28539 w->current_matrix != NULL
28540 /* Don't update mouse highlight if hidden. */
28541 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28542 /* Recognize when we are called to operate on rows that don't exist
28543 anymore. This can happen when a window is split. */
28544 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28545 {
28546 bool phys_cursor_on_p = w->phys_cursor_on_p;
28547 struct glyph_row *row, *first, *last;
28548
28549 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28550 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28551
28552 for (row = first; row <= last && row->enabled_p; ++row)
28553 {
28554 int start_hpos, end_hpos, start_x;
28555
28556 /* For all but the first row, the highlight starts at column 0. */
28557 if (row == first)
28558 {
28559 /* R2L rows have BEG and END in reversed order, but the
28560 screen drawing geometry is always left to right. So
28561 we need to mirror the beginning and end of the
28562 highlighted area in R2L rows. */
28563 if (!row->reversed_p)
28564 {
28565 start_hpos = hlinfo->mouse_face_beg_col;
28566 start_x = hlinfo->mouse_face_beg_x;
28567 }
28568 else if (row == last)
28569 {
28570 start_hpos = hlinfo->mouse_face_end_col;
28571 start_x = hlinfo->mouse_face_end_x;
28572 }
28573 else
28574 {
28575 start_hpos = 0;
28576 start_x = 0;
28577 }
28578 }
28579 else if (row->reversed_p && row == last)
28580 {
28581 start_hpos = hlinfo->mouse_face_end_col;
28582 start_x = hlinfo->mouse_face_end_x;
28583 }
28584 else
28585 {
28586 start_hpos = 0;
28587 start_x = 0;
28588 }
28589
28590 if (row == last)
28591 {
28592 if (!row->reversed_p)
28593 end_hpos = hlinfo->mouse_face_end_col;
28594 else if (row == first)
28595 end_hpos = hlinfo->mouse_face_beg_col;
28596 else
28597 {
28598 end_hpos = row->used[TEXT_AREA];
28599 if (draw == DRAW_NORMAL_TEXT)
28600 row->fill_line_p = true; /* Clear to end of line. */
28601 }
28602 }
28603 else if (row->reversed_p && row == first)
28604 end_hpos = hlinfo->mouse_face_beg_col;
28605 else
28606 {
28607 end_hpos = row->used[TEXT_AREA];
28608 if (draw == DRAW_NORMAL_TEXT)
28609 row->fill_line_p = true; /* Clear to end of line. */
28610 }
28611
28612 if (end_hpos > start_hpos)
28613 {
28614 draw_row_with_mouse_face (w, start_x, row,
28615 start_hpos, end_hpos, draw);
28616
28617 row->mouse_face_p
28618 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28619 }
28620 }
28621
28622 #ifdef HAVE_WINDOW_SYSTEM
28623 /* When we've written over the cursor, arrange for it to
28624 be displayed again. */
28625 if (FRAME_WINDOW_P (f)
28626 && phys_cursor_on_p && !w->phys_cursor_on_p)
28627 {
28628 int hpos = w->phys_cursor.hpos;
28629
28630 /* When the window is hscrolled, cursor hpos can legitimately be
28631 out of bounds, but we draw the cursor at the corresponding
28632 window margin in that case. */
28633 if (!row->reversed_p && hpos < 0)
28634 hpos = 0;
28635 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28636 hpos = row->used[TEXT_AREA] - 1;
28637
28638 block_input ();
28639 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28640 w->phys_cursor.x, w->phys_cursor.y);
28641 unblock_input ();
28642 }
28643 #endif /* HAVE_WINDOW_SYSTEM */
28644 }
28645
28646 #ifdef HAVE_WINDOW_SYSTEM
28647 /* Change the mouse cursor. */
28648 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28649 {
28650 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28651 if (draw == DRAW_NORMAL_TEXT
28652 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28653 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28654 else
28655 #endif
28656 if (draw == DRAW_MOUSE_FACE)
28657 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28658 else
28659 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28660 }
28661 #endif /* HAVE_WINDOW_SYSTEM */
28662 }
28663
28664 /* EXPORT:
28665 Clear out the mouse-highlighted active region.
28666 Redraw it un-highlighted first. Value is true if mouse
28667 face was actually drawn unhighlighted. */
28668
28669 bool
28670 clear_mouse_face (Mouse_HLInfo *hlinfo)
28671 {
28672 bool cleared
28673 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28674 if (cleared)
28675 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28676 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28677 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28678 hlinfo->mouse_face_window = Qnil;
28679 hlinfo->mouse_face_overlay = Qnil;
28680 return cleared;
28681 }
28682
28683 /* Return true if the coordinates HPOS and VPOS on windows W are
28684 within the mouse face on that window. */
28685 static bool
28686 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28687 {
28688 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28689
28690 /* Quickly resolve the easy cases. */
28691 if (!(WINDOWP (hlinfo->mouse_face_window)
28692 && XWINDOW (hlinfo->mouse_face_window) == w))
28693 return false;
28694 if (vpos < hlinfo->mouse_face_beg_row
28695 || vpos > hlinfo->mouse_face_end_row)
28696 return false;
28697 if (vpos > hlinfo->mouse_face_beg_row
28698 && vpos < hlinfo->mouse_face_end_row)
28699 return true;
28700
28701 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28702 {
28703 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28704 {
28705 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28706 return true;
28707 }
28708 else if ((vpos == hlinfo->mouse_face_beg_row
28709 && hpos >= hlinfo->mouse_face_beg_col)
28710 || (vpos == hlinfo->mouse_face_end_row
28711 && hpos < hlinfo->mouse_face_end_col))
28712 return true;
28713 }
28714 else
28715 {
28716 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28717 {
28718 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28719 return true;
28720 }
28721 else if ((vpos == hlinfo->mouse_face_beg_row
28722 && hpos <= hlinfo->mouse_face_beg_col)
28723 || (vpos == hlinfo->mouse_face_end_row
28724 && hpos > hlinfo->mouse_face_end_col))
28725 return true;
28726 }
28727 return false;
28728 }
28729
28730
28731 /* EXPORT:
28732 True if physical cursor of window W is within mouse face. */
28733
28734 bool
28735 cursor_in_mouse_face_p (struct window *w)
28736 {
28737 int hpos = w->phys_cursor.hpos;
28738 int vpos = w->phys_cursor.vpos;
28739 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28740
28741 /* When the window is hscrolled, cursor hpos can legitimately be out
28742 of bounds, but we draw the cursor at the corresponding window
28743 margin in that case. */
28744 if (!row->reversed_p && hpos < 0)
28745 hpos = 0;
28746 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28747 hpos = row->used[TEXT_AREA] - 1;
28748
28749 return coords_in_mouse_face_p (w, hpos, vpos);
28750 }
28751
28752
28753 \f
28754 /* Find the glyph rows START_ROW and END_ROW of window W that display
28755 characters between buffer positions START_CHARPOS and END_CHARPOS
28756 (excluding END_CHARPOS). DISP_STRING is a display string that
28757 covers these buffer positions. This is similar to
28758 row_containing_pos, but is more accurate when bidi reordering makes
28759 buffer positions change non-linearly with glyph rows. */
28760 static void
28761 rows_from_pos_range (struct window *w,
28762 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28763 Lisp_Object disp_string,
28764 struct glyph_row **start, struct glyph_row **end)
28765 {
28766 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28767 int last_y = window_text_bottom_y (w);
28768 struct glyph_row *row;
28769
28770 *start = NULL;
28771 *end = NULL;
28772
28773 while (!first->enabled_p
28774 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28775 first++;
28776
28777 /* Find the START row. */
28778 for (row = first;
28779 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28780 row++)
28781 {
28782 /* A row can potentially be the START row if the range of the
28783 characters it displays intersects the range
28784 [START_CHARPOS..END_CHARPOS). */
28785 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28786 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28787 /* See the commentary in row_containing_pos, for the
28788 explanation of the complicated way to check whether
28789 some position is beyond the end of the characters
28790 displayed by a row. */
28791 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28792 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28793 && !row->ends_at_zv_p
28794 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28795 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28796 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28797 && !row->ends_at_zv_p
28798 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28799 {
28800 /* Found a candidate row. Now make sure at least one of the
28801 glyphs it displays has a charpos from the range
28802 [START_CHARPOS..END_CHARPOS).
28803
28804 This is not obvious because bidi reordering could make
28805 buffer positions of a row be 1,2,3,102,101,100, and if we
28806 want to highlight characters in [50..60), we don't want
28807 this row, even though [50..60) does intersect [1..103),
28808 the range of character positions given by the row's start
28809 and end positions. */
28810 struct glyph *g = row->glyphs[TEXT_AREA];
28811 struct glyph *e = g + row->used[TEXT_AREA];
28812
28813 while (g < e)
28814 {
28815 if (((BUFFERP (g->object) || NILP (g->object))
28816 && start_charpos <= g->charpos && g->charpos < end_charpos)
28817 /* A glyph that comes from DISP_STRING is by
28818 definition to be highlighted. */
28819 || EQ (g->object, disp_string))
28820 *start = row;
28821 g++;
28822 }
28823 if (*start)
28824 break;
28825 }
28826 }
28827
28828 /* Find the END row. */
28829 if (!*start
28830 /* If the last row is partially visible, start looking for END
28831 from that row, instead of starting from FIRST. */
28832 && !(row->enabled_p
28833 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28834 row = first;
28835 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28836 {
28837 struct glyph_row *next = row + 1;
28838 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28839
28840 if (!next->enabled_p
28841 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28842 /* The first row >= START whose range of displayed characters
28843 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28844 is the row END + 1. */
28845 || (start_charpos < next_start
28846 && end_charpos < next_start)
28847 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28848 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28849 && !next->ends_at_zv_p
28850 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28851 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28852 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28853 && !next->ends_at_zv_p
28854 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28855 {
28856 *end = row;
28857 break;
28858 }
28859 else
28860 {
28861 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28862 but none of the characters it displays are in the range, it is
28863 also END + 1. */
28864 struct glyph *g = next->glyphs[TEXT_AREA];
28865 struct glyph *s = g;
28866 struct glyph *e = g + next->used[TEXT_AREA];
28867
28868 while (g < e)
28869 {
28870 if (((BUFFERP (g->object) || NILP (g->object))
28871 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28872 /* If the buffer position of the first glyph in
28873 the row is equal to END_CHARPOS, it means
28874 the last character to be highlighted is the
28875 newline of ROW, and we must consider NEXT as
28876 END, not END+1. */
28877 || (((!next->reversed_p && g == s)
28878 || (next->reversed_p && g == e - 1))
28879 && (g->charpos == end_charpos
28880 /* Special case for when NEXT is an
28881 empty line at ZV. */
28882 || (g->charpos == -1
28883 && !row->ends_at_zv_p
28884 && next_start == end_charpos)))))
28885 /* A glyph that comes from DISP_STRING is by
28886 definition to be highlighted. */
28887 || EQ (g->object, disp_string))
28888 break;
28889 g++;
28890 }
28891 if (g == e)
28892 {
28893 *end = row;
28894 break;
28895 }
28896 /* The first row that ends at ZV must be the last to be
28897 highlighted. */
28898 else if (next->ends_at_zv_p)
28899 {
28900 *end = next;
28901 break;
28902 }
28903 }
28904 }
28905 }
28906
28907 /* This function sets the mouse_face_* elements of HLINFO, assuming
28908 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28909 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28910 for the overlay or run of text properties specifying the mouse
28911 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28912 before-string and after-string that must also be highlighted.
28913 DISP_STRING, if non-nil, is a display string that may cover some
28914 or all of the highlighted text. */
28915
28916 static void
28917 mouse_face_from_buffer_pos (Lisp_Object window,
28918 Mouse_HLInfo *hlinfo,
28919 ptrdiff_t mouse_charpos,
28920 ptrdiff_t start_charpos,
28921 ptrdiff_t end_charpos,
28922 Lisp_Object before_string,
28923 Lisp_Object after_string,
28924 Lisp_Object disp_string)
28925 {
28926 struct window *w = XWINDOW (window);
28927 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28928 struct glyph_row *r1, *r2;
28929 struct glyph *glyph, *end;
28930 ptrdiff_t ignore, pos;
28931 int x;
28932
28933 eassert (NILP (disp_string) || STRINGP (disp_string));
28934 eassert (NILP (before_string) || STRINGP (before_string));
28935 eassert (NILP (after_string) || STRINGP (after_string));
28936
28937 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28938 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28939 if (r1 == NULL)
28940 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28941 /* If the before-string or display-string contains newlines,
28942 rows_from_pos_range skips to its last row. Move back. */
28943 if (!NILP (before_string) || !NILP (disp_string))
28944 {
28945 struct glyph_row *prev;
28946 while ((prev = r1 - 1, prev >= first)
28947 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28948 && prev->used[TEXT_AREA] > 0)
28949 {
28950 struct glyph *beg = prev->glyphs[TEXT_AREA];
28951 glyph = beg + prev->used[TEXT_AREA];
28952 while (--glyph >= beg && NILP (glyph->object));
28953 if (glyph < beg
28954 || !(EQ (glyph->object, before_string)
28955 || EQ (glyph->object, disp_string)))
28956 break;
28957 r1 = prev;
28958 }
28959 }
28960 if (r2 == NULL)
28961 {
28962 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28963 hlinfo->mouse_face_past_end = true;
28964 }
28965 else if (!NILP (after_string))
28966 {
28967 /* If the after-string has newlines, advance to its last row. */
28968 struct glyph_row *next;
28969 struct glyph_row *last
28970 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28971
28972 for (next = r2 + 1;
28973 next <= last
28974 && next->used[TEXT_AREA] > 0
28975 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28976 ++next)
28977 r2 = next;
28978 }
28979 /* The rest of the display engine assumes that mouse_face_beg_row is
28980 either above mouse_face_end_row or identical to it. But with
28981 bidi-reordered continued lines, the row for START_CHARPOS could
28982 be below the row for END_CHARPOS. If so, swap the rows and store
28983 them in correct order. */
28984 if (r1->y > r2->y)
28985 {
28986 struct glyph_row *tem = r2;
28987
28988 r2 = r1;
28989 r1 = tem;
28990 }
28991
28992 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28993 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28994
28995 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28996 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28997 could be anywhere in the row and in any order. The strategy
28998 below is to find the leftmost and the rightmost glyph that
28999 belongs to either of these 3 strings, or whose position is
29000 between START_CHARPOS and END_CHARPOS, and highlight all the
29001 glyphs between those two. This may cover more than just the text
29002 between START_CHARPOS and END_CHARPOS if the range of characters
29003 strides the bidi level boundary, e.g. if the beginning is in R2L
29004 text while the end is in L2R text or vice versa. */
29005 if (!r1->reversed_p)
29006 {
29007 /* This row is in a left to right paragraph. Scan it left to
29008 right. */
29009 glyph = r1->glyphs[TEXT_AREA];
29010 end = glyph + r1->used[TEXT_AREA];
29011 x = r1->x;
29012
29013 /* Skip truncation glyphs at the start of the glyph row. */
29014 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29015 for (; glyph < end
29016 && NILP (glyph->object)
29017 && glyph->charpos < 0;
29018 ++glyph)
29019 x += glyph->pixel_width;
29020
29021 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29022 or DISP_STRING, and the first glyph from buffer whose
29023 position is between START_CHARPOS and END_CHARPOS. */
29024 for (; glyph < end
29025 && !NILP (glyph->object)
29026 && !EQ (glyph->object, disp_string)
29027 && !(BUFFERP (glyph->object)
29028 && (glyph->charpos >= start_charpos
29029 && glyph->charpos < end_charpos));
29030 ++glyph)
29031 {
29032 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29033 are present at buffer positions between START_CHARPOS and
29034 END_CHARPOS, or if they come from an overlay. */
29035 if (EQ (glyph->object, before_string))
29036 {
29037 pos = string_buffer_position (before_string,
29038 start_charpos);
29039 /* If pos == 0, it means before_string came from an
29040 overlay, not from a buffer position. */
29041 if (!pos || (pos >= start_charpos && pos < end_charpos))
29042 break;
29043 }
29044 else if (EQ (glyph->object, after_string))
29045 {
29046 pos = string_buffer_position (after_string, end_charpos);
29047 if (!pos || (pos >= start_charpos && pos < end_charpos))
29048 break;
29049 }
29050 x += glyph->pixel_width;
29051 }
29052 hlinfo->mouse_face_beg_x = x;
29053 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29054 }
29055 else
29056 {
29057 /* This row is in a right to left paragraph. Scan it right to
29058 left. */
29059 struct glyph *g;
29060
29061 end = r1->glyphs[TEXT_AREA] - 1;
29062 glyph = end + r1->used[TEXT_AREA];
29063
29064 /* Skip truncation glyphs at the start of the glyph row. */
29065 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29066 for (; glyph > end
29067 && NILP (glyph->object)
29068 && glyph->charpos < 0;
29069 --glyph)
29070 ;
29071
29072 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29073 or DISP_STRING, and the first glyph from buffer whose
29074 position is between START_CHARPOS and END_CHARPOS. */
29075 for (; glyph > end
29076 && !NILP (glyph->object)
29077 && !EQ (glyph->object, disp_string)
29078 && !(BUFFERP (glyph->object)
29079 && (glyph->charpos >= start_charpos
29080 && glyph->charpos < end_charpos));
29081 --glyph)
29082 {
29083 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29084 are present at buffer positions between START_CHARPOS and
29085 END_CHARPOS, or if they come from an overlay. */
29086 if (EQ (glyph->object, before_string))
29087 {
29088 pos = string_buffer_position (before_string, start_charpos);
29089 /* If pos == 0, it means before_string came from an
29090 overlay, not from a buffer position. */
29091 if (!pos || (pos >= start_charpos && pos < end_charpos))
29092 break;
29093 }
29094 else if (EQ (glyph->object, after_string))
29095 {
29096 pos = string_buffer_position (after_string, end_charpos);
29097 if (!pos || (pos >= start_charpos && pos < end_charpos))
29098 break;
29099 }
29100 }
29101
29102 glyph++; /* first glyph to the right of the highlighted area */
29103 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
29104 x += g->pixel_width;
29105 hlinfo->mouse_face_beg_x = x;
29106 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29107 }
29108
29109 /* If the highlight ends in a different row, compute GLYPH and END
29110 for the end row. Otherwise, reuse the values computed above for
29111 the row where the highlight begins. */
29112 if (r2 != r1)
29113 {
29114 if (!r2->reversed_p)
29115 {
29116 glyph = r2->glyphs[TEXT_AREA];
29117 end = glyph + r2->used[TEXT_AREA];
29118 x = r2->x;
29119 }
29120 else
29121 {
29122 end = r2->glyphs[TEXT_AREA] - 1;
29123 glyph = end + r2->used[TEXT_AREA];
29124 }
29125 }
29126
29127 if (!r2->reversed_p)
29128 {
29129 /* Skip truncation and continuation glyphs near the end of the
29130 row, and also blanks and stretch glyphs inserted by
29131 extend_face_to_end_of_line. */
29132 while (end > glyph
29133 && NILP ((end - 1)->object))
29134 --end;
29135 /* Scan the rest of the glyph row from the end, looking for the
29136 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29137 DISP_STRING, or whose position is between START_CHARPOS
29138 and END_CHARPOS */
29139 for (--end;
29140 end > glyph
29141 && !NILP (end->object)
29142 && !EQ (end->object, disp_string)
29143 && !(BUFFERP (end->object)
29144 && (end->charpos >= start_charpos
29145 && end->charpos < end_charpos));
29146 --end)
29147 {
29148 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29149 are present at buffer positions between START_CHARPOS and
29150 END_CHARPOS, or if they come from an overlay. */
29151 if (EQ (end->object, before_string))
29152 {
29153 pos = string_buffer_position (before_string, start_charpos);
29154 if (!pos || (pos >= start_charpos && pos < end_charpos))
29155 break;
29156 }
29157 else if (EQ (end->object, after_string))
29158 {
29159 pos = string_buffer_position (after_string, end_charpos);
29160 if (!pos || (pos >= start_charpos && pos < end_charpos))
29161 break;
29162 }
29163 }
29164 /* Find the X coordinate of the last glyph to be highlighted. */
29165 for (; glyph <= end; ++glyph)
29166 x += glyph->pixel_width;
29167
29168 hlinfo->mouse_face_end_x = x;
29169 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
29170 }
29171 else
29172 {
29173 /* Skip truncation and continuation glyphs near the end of the
29174 row, and also blanks and stretch glyphs inserted by
29175 extend_face_to_end_of_line. */
29176 x = r2->x;
29177 end++;
29178 while (end < glyph
29179 && NILP (end->object))
29180 {
29181 x += end->pixel_width;
29182 ++end;
29183 }
29184 /* Scan the rest of the glyph row from the end, looking for the
29185 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29186 DISP_STRING, or whose position is between START_CHARPOS
29187 and END_CHARPOS */
29188 for ( ;
29189 end < glyph
29190 && !NILP (end->object)
29191 && !EQ (end->object, disp_string)
29192 && !(BUFFERP (end->object)
29193 && (end->charpos >= start_charpos
29194 && end->charpos < end_charpos));
29195 ++end)
29196 {
29197 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29198 are present at buffer positions between START_CHARPOS and
29199 END_CHARPOS, or if they come from an overlay. */
29200 if (EQ (end->object, before_string))
29201 {
29202 pos = string_buffer_position (before_string, start_charpos);
29203 if (!pos || (pos >= start_charpos && pos < end_charpos))
29204 break;
29205 }
29206 else if (EQ (end->object, after_string))
29207 {
29208 pos = string_buffer_position (after_string, end_charpos);
29209 if (!pos || (pos >= start_charpos && pos < end_charpos))
29210 break;
29211 }
29212 x += end->pixel_width;
29213 }
29214 /* If we exited the above loop because we arrived at the last
29215 glyph of the row, and its buffer position is still not in
29216 range, it means the last character in range is the preceding
29217 newline. Bump the end column and x values to get past the
29218 last glyph. */
29219 if (end == glyph
29220 && BUFFERP (end->object)
29221 && (end->charpos < start_charpos
29222 || end->charpos >= end_charpos))
29223 {
29224 x += end->pixel_width;
29225 ++end;
29226 }
29227 hlinfo->mouse_face_end_x = x;
29228 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
29229 }
29230
29231 hlinfo->mouse_face_window = window;
29232 hlinfo->mouse_face_face_id
29233 = face_at_buffer_position (w, mouse_charpos, &ignore,
29234 mouse_charpos + 1,
29235 !hlinfo->mouse_face_hidden, -1);
29236 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29237 }
29238
29239 /* The following function is not used anymore (replaced with
29240 mouse_face_from_string_pos), but I leave it here for the time
29241 being, in case someone would. */
29242
29243 #if false /* not used */
29244
29245 /* Find the position of the glyph for position POS in OBJECT in
29246 window W's current matrix, and return in *X, *Y the pixel
29247 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
29248
29249 RIGHT_P means return the position of the right edge of the glyph.
29250 !RIGHT_P means return the left edge position.
29251
29252 If no glyph for POS exists in the matrix, return the position of
29253 the glyph with the next smaller position that is in the matrix, if
29254 RIGHT_P is false. If RIGHT_P, and no glyph for POS
29255 exists in the matrix, return the position of the glyph with the
29256 next larger position in OBJECT.
29257
29258 Value is true if a glyph was found. */
29259
29260 static bool
29261 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
29262 int *hpos, int *vpos, int *x, int *y, bool right_p)
29263 {
29264 int yb = window_text_bottom_y (w);
29265 struct glyph_row *r;
29266 struct glyph *best_glyph = NULL;
29267 struct glyph_row *best_row = NULL;
29268 int best_x = 0;
29269
29270 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29271 r->enabled_p && r->y < yb;
29272 ++r)
29273 {
29274 struct glyph *g = r->glyphs[TEXT_AREA];
29275 struct glyph *e = g + r->used[TEXT_AREA];
29276 int gx;
29277
29278 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29279 if (EQ (g->object, object))
29280 {
29281 if (g->charpos == pos)
29282 {
29283 best_glyph = g;
29284 best_x = gx;
29285 best_row = r;
29286 goto found;
29287 }
29288 else if (best_glyph == NULL
29289 || ((eabs (g->charpos - pos)
29290 < eabs (best_glyph->charpos - pos))
29291 && (right_p
29292 ? g->charpos < pos
29293 : g->charpos > pos)))
29294 {
29295 best_glyph = g;
29296 best_x = gx;
29297 best_row = r;
29298 }
29299 }
29300 }
29301
29302 found:
29303
29304 if (best_glyph)
29305 {
29306 *x = best_x;
29307 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
29308
29309 if (right_p)
29310 {
29311 *x += best_glyph->pixel_width;
29312 ++*hpos;
29313 }
29314
29315 *y = best_row->y;
29316 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29317 }
29318
29319 return best_glyph != NULL;
29320 }
29321 #endif /* not used */
29322
29323 /* Find the positions of the first and the last glyphs in window W's
29324 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29325 (assumed to be a string), and return in HLINFO's mouse_face_*
29326 members the pixel and column/row coordinates of those glyphs. */
29327
29328 static void
29329 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29330 Lisp_Object object,
29331 ptrdiff_t startpos, ptrdiff_t endpos)
29332 {
29333 int yb = window_text_bottom_y (w);
29334 struct glyph_row *r;
29335 struct glyph *g, *e;
29336 int gx;
29337 bool found = false;
29338
29339 /* Find the glyph row with at least one position in the range
29340 [STARTPOS..ENDPOS), and the first glyph in that row whose
29341 position belongs to that range. */
29342 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29343 r->enabled_p && r->y < yb;
29344 ++r)
29345 {
29346 if (!r->reversed_p)
29347 {
29348 g = r->glyphs[TEXT_AREA];
29349 e = g + r->used[TEXT_AREA];
29350 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29351 if (EQ (g->object, object)
29352 && startpos <= g->charpos && g->charpos < endpos)
29353 {
29354 hlinfo->mouse_face_beg_row
29355 = MATRIX_ROW_VPOS (r, w->current_matrix);
29356 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29357 hlinfo->mouse_face_beg_x = gx;
29358 found = true;
29359 break;
29360 }
29361 }
29362 else
29363 {
29364 struct glyph *g1;
29365
29366 e = r->glyphs[TEXT_AREA];
29367 g = e + r->used[TEXT_AREA];
29368 for ( ; g > e; --g)
29369 if (EQ ((g-1)->object, object)
29370 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29371 {
29372 hlinfo->mouse_face_beg_row
29373 = MATRIX_ROW_VPOS (r, w->current_matrix);
29374 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29375 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29376 gx += g1->pixel_width;
29377 hlinfo->mouse_face_beg_x = gx;
29378 found = true;
29379 break;
29380 }
29381 }
29382 if (found)
29383 break;
29384 }
29385
29386 if (!found)
29387 return;
29388
29389 /* Starting with the next row, look for the first row which does NOT
29390 include any glyphs whose positions are in the range. */
29391 for (++r; r->enabled_p && r->y < yb; ++r)
29392 {
29393 g = r->glyphs[TEXT_AREA];
29394 e = g + r->used[TEXT_AREA];
29395 found = false;
29396 for ( ; g < e; ++g)
29397 if (EQ (g->object, object)
29398 && startpos <= g->charpos && g->charpos < endpos)
29399 {
29400 found = true;
29401 break;
29402 }
29403 if (!found)
29404 break;
29405 }
29406
29407 /* The highlighted region ends on the previous row. */
29408 r--;
29409
29410 /* Set the end row. */
29411 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29412
29413 /* Compute and set the end column and the end column's horizontal
29414 pixel coordinate. */
29415 if (!r->reversed_p)
29416 {
29417 g = r->glyphs[TEXT_AREA];
29418 e = g + r->used[TEXT_AREA];
29419 for ( ; e > g; --e)
29420 if (EQ ((e-1)->object, object)
29421 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29422 break;
29423 hlinfo->mouse_face_end_col = e - g;
29424
29425 for (gx = r->x; g < e; ++g)
29426 gx += g->pixel_width;
29427 hlinfo->mouse_face_end_x = gx;
29428 }
29429 else
29430 {
29431 e = r->glyphs[TEXT_AREA];
29432 g = e + r->used[TEXT_AREA];
29433 for (gx = r->x ; e < g; ++e)
29434 {
29435 if (EQ (e->object, object)
29436 && startpos <= e->charpos && e->charpos < endpos)
29437 break;
29438 gx += e->pixel_width;
29439 }
29440 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29441 hlinfo->mouse_face_end_x = gx;
29442 }
29443 }
29444
29445 #ifdef HAVE_WINDOW_SYSTEM
29446
29447 /* See if position X, Y is within a hot-spot of an image. */
29448
29449 static bool
29450 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29451 {
29452 if (!CONSP (hot_spot))
29453 return false;
29454
29455 if (EQ (XCAR (hot_spot), Qrect))
29456 {
29457 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29458 Lisp_Object rect = XCDR (hot_spot);
29459 Lisp_Object tem;
29460 if (!CONSP (rect))
29461 return false;
29462 if (!CONSP (XCAR (rect)))
29463 return false;
29464 if (!CONSP (XCDR (rect)))
29465 return false;
29466 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29467 return false;
29468 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29469 return false;
29470 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29471 return false;
29472 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29473 return false;
29474 return true;
29475 }
29476 else if (EQ (XCAR (hot_spot), Qcircle))
29477 {
29478 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29479 Lisp_Object circ = XCDR (hot_spot);
29480 Lisp_Object lr, lx0, ly0;
29481 if (CONSP (circ)
29482 && CONSP (XCAR (circ))
29483 && (lr = XCDR (circ), NUMBERP (lr))
29484 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29485 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29486 {
29487 double r = XFLOATINT (lr);
29488 double dx = XINT (lx0) - x;
29489 double dy = XINT (ly0) - y;
29490 return (dx * dx + dy * dy <= r * r);
29491 }
29492 }
29493 else if (EQ (XCAR (hot_spot), Qpoly))
29494 {
29495 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29496 if (VECTORP (XCDR (hot_spot)))
29497 {
29498 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29499 Lisp_Object *poly = v->contents;
29500 ptrdiff_t n = v->header.size;
29501 ptrdiff_t i;
29502 bool inside = false;
29503 Lisp_Object lx, ly;
29504 int x0, y0;
29505
29506 /* Need an even number of coordinates, and at least 3 edges. */
29507 if (n < 6 || n & 1)
29508 return false;
29509
29510 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29511 If count is odd, we are inside polygon. Pixels on edges
29512 may or may not be included depending on actual geometry of the
29513 polygon. */
29514 if ((lx = poly[n-2], !INTEGERP (lx))
29515 || (ly = poly[n-1], !INTEGERP (lx)))
29516 return false;
29517 x0 = XINT (lx), y0 = XINT (ly);
29518 for (i = 0; i < n; i += 2)
29519 {
29520 int x1 = x0, y1 = y0;
29521 if ((lx = poly[i], !INTEGERP (lx))
29522 || (ly = poly[i+1], !INTEGERP (ly)))
29523 return false;
29524 x0 = XINT (lx), y0 = XINT (ly);
29525
29526 /* Does this segment cross the X line? */
29527 if (x0 >= x)
29528 {
29529 if (x1 >= x)
29530 continue;
29531 }
29532 else if (x1 < x)
29533 continue;
29534 if (y > y0 && y > y1)
29535 continue;
29536 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29537 inside = !inside;
29538 }
29539 return inside;
29540 }
29541 }
29542 return false;
29543 }
29544
29545 Lisp_Object
29546 find_hot_spot (Lisp_Object map, int x, int y)
29547 {
29548 while (CONSP (map))
29549 {
29550 if (CONSP (XCAR (map))
29551 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29552 return XCAR (map);
29553 map = XCDR (map);
29554 }
29555
29556 return Qnil;
29557 }
29558
29559 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29560 3, 3, 0,
29561 doc: /* Lookup in image map MAP coordinates X and Y.
29562 An image map is an alist where each element has the format (AREA ID PLIST).
29563 An AREA is specified as either a rectangle, a circle, or a polygon:
29564 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29565 pixel coordinates of the upper left and bottom right corners.
29566 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29567 and the radius of the circle; r may be a float or integer.
29568 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29569 vector describes one corner in the polygon.
29570 Returns the alist element for the first matching AREA in MAP. */)
29571 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29572 {
29573 if (NILP (map))
29574 return Qnil;
29575
29576 CHECK_NUMBER (x);
29577 CHECK_NUMBER (y);
29578
29579 return find_hot_spot (map,
29580 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29581 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29582 }
29583
29584
29585 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29586 static void
29587 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29588 {
29589 /* Do not change cursor shape while dragging mouse. */
29590 if (EQ (do_mouse_tracking, Qdragging))
29591 return;
29592
29593 if (!NILP (pointer))
29594 {
29595 if (EQ (pointer, Qarrow))
29596 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29597 else if (EQ (pointer, Qhand))
29598 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29599 else if (EQ (pointer, Qtext))
29600 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29601 else if (EQ (pointer, intern ("hdrag")))
29602 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29603 else if (EQ (pointer, intern ("nhdrag")))
29604 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29605 #ifdef HAVE_X_WINDOWS
29606 else if (EQ (pointer, intern ("vdrag")))
29607 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29608 #endif
29609 else if (EQ (pointer, intern ("hourglass")))
29610 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29611 else if (EQ (pointer, Qmodeline))
29612 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29613 else
29614 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29615 }
29616
29617 if (cursor != No_Cursor)
29618 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29619 }
29620
29621 #endif /* HAVE_WINDOW_SYSTEM */
29622
29623 /* Take proper action when mouse has moved to the mode or header line
29624 or marginal area AREA of window W, x-position X and y-position Y.
29625 X is relative to the start of the text display area of W, so the
29626 width of bitmap areas and scroll bars must be subtracted to get a
29627 position relative to the start of the mode line. */
29628
29629 static void
29630 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29631 enum window_part area)
29632 {
29633 struct window *w = XWINDOW (window);
29634 struct frame *f = XFRAME (w->frame);
29635 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29636 #ifdef HAVE_WINDOW_SYSTEM
29637 Display_Info *dpyinfo;
29638 #endif
29639 Cursor cursor = No_Cursor;
29640 Lisp_Object pointer = Qnil;
29641 int dx, dy, width, height;
29642 ptrdiff_t charpos;
29643 Lisp_Object string, object = Qnil;
29644 Lisp_Object pos IF_LINT (= Qnil), help;
29645
29646 Lisp_Object mouse_face;
29647 int original_x_pixel = x;
29648 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29649 struct glyph_row *row IF_LINT (= 0);
29650
29651 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29652 {
29653 int x0;
29654 struct glyph *end;
29655
29656 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29657 returns them in row/column units! */
29658 string = mode_line_string (w, area, &x, &y, &charpos,
29659 &object, &dx, &dy, &width, &height);
29660
29661 row = (area == ON_MODE_LINE
29662 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29663 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29664
29665 /* Find the glyph under the mouse pointer. */
29666 if (row->mode_line_p && row->enabled_p)
29667 {
29668 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29669 end = glyph + row->used[TEXT_AREA];
29670
29671 for (x0 = original_x_pixel;
29672 glyph < end && x0 >= glyph->pixel_width;
29673 ++glyph)
29674 x0 -= glyph->pixel_width;
29675
29676 if (glyph >= end)
29677 glyph = NULL;
29678 }
29679 }
29680 else
29681 {
29682 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29683 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29684 returns them in row/column units! */
29685 string = marginal_area_string (w, area, &x, &y, &charpos,
29686 &object, &dx, &dy, &width, &height);
29687 }
29688
29689 help = Qnil;
29690
29691 #ifdef HAVE_WINDOW_SYSTEM
29692 if (IMAGEP (object))
29693 {
29694 Lisp_Object image_map, hotspot;
29695 if ((image_map = Fplist_get (XCDR (object), QCmap),
29696 !NILP (image_map))
29697 && (hotspot = find_hot_spot (image_map, dx, dy),
29698 CONSP (hotspot))
29699 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29700 {
29701 Lisp_Object plist;
29702
29703 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29704 If so, we could look for mouse-enter, mouse-leave
29705 properties in PLIST (and do something...). */
29706 hotspot = XCDR (hotspot);
29707 if (CONSP (hotspot)
29708 && (plist = XCAR (hotspot), CONSP (plist)))
29709 {
29710 pointer = Fplist_get (plist, Qpointer);
29711 if (NILP (pointer))
29712 pointer = Qhand;
29713 help = Fplist_get (plist, Qhelp_echo);
29714 if (!NILP (help))
29715 {
29716 help_echo_string = help;
29717 XSETWINDOW (help_echo_window, w);
29718 help_echo_object = w->contents;
29719 help_echo_pos = charpos;
29720 }
29721 }
29722 }
29723 if (NILP (pointer))
29724 pointer = Fplist_get (XCDR (object), QCpointer);
29725 }
29726 #endif /* HAVE_WINDOW_SYSTEM */
29727
29728 if (STRINGP (string))
29729 pos = make_number (charpos);
29730
29731 /* Set the help text and mouse pointer. If the mouse is on a part
29732 of the mode line without any text (e.g. past the right edge of
29733 the mode line text), use the default help text and pointer. */
29734 if (STRINGP (string) || area == ON_MODE_LINE)
29735 {
29736 /* Arrange to display the help by setting the global variables
29737 help_echo_string, help_echo_object, and help_echo_pos. */
29738 if (NILP (help))
29739 {
29740 if (STRINGP (string))
29741 help = Fget_text_property (pos, Qhelp_echo, string);
29742
29743 if (!NILP (help))
29744 {
29745 help_echo_string = help;
29746 XSETWINDOW (help_echo_window, w);
29747 help_echo_object = string;
29748 help_echo_pos = charpos;
29749 }
29750 else if (area == ON_MODE_LINE)
29751 {
29752 Lisp_Object default_help
29753 = buffer_local_value (Qmode_line_default_help_echo,
29754 w->contents);
29755
29756 if (STRINGP (default_help))
29757 {
29758 help_echo_string = default_help;
29759 XSETWINDOW (help_echo_window, w);
29760 help_echo_object = Qnil;
29761 help_echo_pos = -1;
29762 }
29763 }
29764 }
29765
29766 #ifdef HAVE_WINDOW_SYSTEM
29767 /* Change the mouse pointer according to what is under it. */
29768 if (FRAME_WINDOW_P (f))
29769 {
29770 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29771 || minibuf_level
29772 || NILP (Vresize_mini_windows));
29773
29774 dpyinfo = FRAME_DISPLAY_INFO (f);
29775 if (STRINGP (string))
29776 {
29777 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29778
29779 if (NILP (pointer))
29780 pointer = Fget_text_property (pos, Qpointer, string);
29781
29782 /* Change the mouse pointer according to what is under X/Y. */
29783 if (NILP (pointer)
29784 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29785 {
29786 Lisp_Object map;
29787 map = Fget_text_property (pos, Qlocal_map, string);
29788 if (!KEYMAPP (map))
29789 map = Fget_text_property (pos, Qkeymap, string);
29790 if (!KEYMAPP (map) && draggable)
29791 cursor = dpyinfo->vertical_scroll_bar_cursor;
29792 }
29793 }
29794 else if (draggable)
29795 /* Default mode-line pointer. */
29796 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29797 }
29798 #endif
29799 }
29800
29801 /* Change the mouse face according to what is under X/Y. */
29802 bool mouse_face_shown = false;
29803 if (STRINGP (string))
29804 {
29805 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29806 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29807 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29808 && glyph)
29809 {
29810 Lisp_Object b, e;
29811
29812 struct glyph * tmp_glyph;
29813
29814 int gpos;
29815 int gseq_length;
29816 int total_pixel_width;
29817 ptrdiff_t begpos, endpos, ignore;
29818
29819 int vpos, hpos;
29820
29821 b = Fprevious_single_property_change (make_number (charpos + 1),
29822 Qmouse_face, string, Qnil);
29823 if (NILP (b))
29824 begpos = 0;
29825 else
29826 begpos = XINT (b);
29827
29828 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29829 if (NILP (e))
29830 endpos = SCHARS (string);
29831 else
29832 endpos = XINT (e);
29833
29834 /* Calculate the glyph position GPOS of GLYPH in the
29835 displayed string, relative to the beginning of the
29836 highlighted part of the string.
29837
29838 Note: GPOS is different from CHARPOS. CHARPOS is the
29839 position of GLYPH in the internal string object. A mode
29840 line string format has structures which are converted to
29841 a flattened string by the Emacs Lisp interpreter. The
29842 internal string is an element of those structures. The
29843 displayed string is the flattened string. */
29844 tmp_glyph = row_start_glyph;
29845 while (tmp_glyph < glyph
29846 && (!(EQ (tmp_glyph->object, glyph->object)
29847 && begpos <= tmp_glyph->charpos
29848 && tmp_glyph->charpos < endpos)))
29849 tmp_glyph++;
29850 gpos = glyph - tmp_glyph;
29851
29852 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29853 the highlighted part of the displayed string to which
29854 GLYPH belongs. Note: GSEQ_LENGTH is different from
29855 SCHARS (STRING), because the latter returns the length of
29856 the internal string. */
29857 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29858 tmp_glyph > glyph
29859 && (!(EQ (tmp_glyph->object, glyph->object)
29860 && begpos <= tmp_glyph->charpos
29861 && tmp_glyph->charpos < endpos));
29862 tmp_glyph--)
29863 ;
29864 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29865
29866 /* Calculate the total pixel width of all the glyphs between
29867 the beginning of the highlighted area and GLYPH. */
29868 total_pixel_width = 0;
29869 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29870 total_pixel_width += tmp_glyph->pixel_width;
29871
29872 /* Pre calculation of re-rendering position. Note: X is in
29873 column units here, after the call to mode_line_string or
29874 marginal_area_string. */
29875 hpos = x - gpos;
29876 vpos = (area == ON_MODE_LINE
29877 ? (w->current_matrix)->nrows - 1
29878 : 0);
29879
29880 /* If GLYPH's position is included in the region that is
29881 already drawn in mouse face, we have nothing to do. */
29882 if ( EQ (window, hlinfo->mouse_face_window)
29883 && (!row->reversed_p
29884 ? (hlinfo->mouse_face_beg_col <= hpos
29885 && hpos < hlinfo->mouse_face_end_col)
29886 /* In R2L rows we swap BEG and END, see below. */
29887 : (hlinfo->mouse_face_end_col <= hpos
29888 && hpos < hlinfo->mouse_face_beg_col))
29889 && hlinfo->mouse_face_beg_row == vpos )
29890 return;
29891
29892 if (clear_mouse_face (hlinfo))
29893 cursor = No_Cursor;
29894
29895 if (!row->reversed_p)
29896 {
29897 hlinfo->mouse_face_beg_col = hpos;
29898 hlinfo->mouse_face_beg_x = original_x_pixel
29899 - (total_pixel_width + dx);
29900 hlinfo->mouse_face_end_col = hpos + gseq_length;
29901 hlinfo->mouse_face_end_x = 0;
29902 }
29903 else
29904 {
29905 /* In R2L rows, show_mouse_face expects BEG and END
29906 coordinates to be swapped. */
29907 hlinfo->mouse_face_end_col = hpos;
29908 hlinfo->mouse_face_end_x = original_x_pixel
29909 - (total_pixel_width + dx);
29910 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29911 hlinfo->mouse_face_beg_x = 0;
29912 }
29913
29914 hlinfo->mouse_face_beg_row = vpos;
29915 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29916 hlinfo->mouse_face_past_end = false;
29917 hlinfo->mouse_face_window = window;
29918
29919 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29920 charpos,
29921 0, &ignore,
29922 glyph->face_id,
29923 true);
29924 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29925 mouse_face_shown = true;
29926
29927 if (NILP (pointer))
29928 pointer = Qhand;
29929 }
29930 }
29931
29932 /* If mouse-face doesn't need to be shown, clear any existing
29933 mouse-face. */
29934 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
29935 clear_mouse_face (hlinfo);
29936
29937 #ifdef HAVE_WINDOW_SYSTEM
29938 if (FRAME_WINDOW_P (f))
29939 define_frame_cursor1 (f, cursor, pointer);
29940 #endif
29941 }
29942
29943
29944 /* EXPORT:
29945 Take proper action when the mouse has moved to position X, Y on
29946 frame F with regards to highlighting portions of display that have
29947 mouse-face properties. Also de-highlight portions of display where
29948 the mouse was before, set the mouse pointer shape as appropriate
29949 for the mouse coordinates, and activate help echo (tooltips).
29950 X and Y can be negative or out of range. */
29951
29952 void
29953 note_mouse_highlight (struct frame *f, int x, int y)
29954 {
29955 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29956 enum window_part part = ON_NOTHING;
29957 Lisp_Object window;
29958 struct window *w;
29959 Cursor cursor = No_Cursor;
29960 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29961 struct buffer *b;
29962
29963 /* When a menu is active, don't highlight because this looks odd. */
29964 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29965 if (popup_activated ())
29966 return;
29967 #endif
29968
29969 if (!f->glyphs_initialized_p
29970 || f->pointer_invisible)
29971 return;
29972
29973 hlinfo->mouse_face_mouse_x = x;
29974 hlinfo->mouse_face_mouse_y = y;
29975 hlinfo->mouse_face_mouse_frame = f;
29976
29977 if (hlinfo->mouse_face_defer)
29978 return;
29979
29980 /* Which window is that in? */
29981 window = window_from_coordinates (f, x, y, &part, true);
29982
29983 /* If displaying active text in another window, clear that. */
29984 if (! EQ (window, hlinfo->mouse_face_window)
29985 /* Also clear if we move out of text area in same window. */
29986 || (!NILP (hlinfo->mouse_face_window)
29987 && !NILP (window)
29988 && part != ON_TEXT
29989 && part != ON_MODE_LINE
29990 && part != ON_HEADER_LINE))
29991 clear_mouse_face (hlinfo);
29992
29993 /* Not on a window -> return. */
29994 if (!WINDOWP (window))
29995 return;
29996
29997 /* Reset help_echo_string. It will get recomputed below. */
29998 help_echo_string = Qnil;
29999
30000 /* Convert to window-relative pixel coordinates. */
30001 w = XWINDOW (window);
30002 frame_to_window_pixel_xy (w, &x, &y);
30003
30004 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
30005 /* Handle tool-bar window differently since it doesn't display a
30006 buffer. */
30007 if (EQ (window, f->tool_bar_window))
30008 {
30009 note_tool_bar_highlight (f, x, y);
30010 return;
30011 }
30012 #endif
30013
30014 /* Mouse is on the mode, header line or margin? */
30015 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
30016 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30017 {
30018 note_mode_line_or_margin_highlight (window, x, y, part);
30019
30020 #ifdef HAVE_WINDOW_SYSTEM
30021 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30022 {
30023 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30024 /* Show non-text cursor (Bug#16647). */
30025 goto set_cursor;
30026 }
30027 else
30028 #endif
30029 return;
30030 }
30031
30032 #ifdef HAVE_WINDOW_SYSTEM
30033 if (part == ON_VERTICAL_BORDER)
30034 {
30035 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30036 help_echo_string = build_string ("drag-mouse-1: resize");
30037 }
30038 else if (part == ON_RIGHT_DIVIDER)
30039 {
30040 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30041 help_echo_string = build_string ("drag-mouse-1: resize");
30042 }
30043 else if (part == ON_BOTTOM_DIVIDER)
30044 if (! WINDOW_BOTTOMMOST_P (w)
30045 || minibuf_level
30046 || NILP (Vresize_mini_windows))
30047 {
30048 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30049 help_echo_string = build_string ("drag-mouse-1: resize");
30050 }
30051 else
30052 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30053 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
30054 || part == ON_VERTICAL_SCROLL_BAR
30055 || part == ON_HORIZONTAL_SCROLL_BAR)
30056 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30057 else
30058 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30059 #endif
30060
30061 /* Are we in a window whose display is up to date?
30062 And verify the buffer's text has not changed. */
30063 b = XBUFFER (w->contents);
30064 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
30065 {
30066 int hpos, vpos, dx, dy, area = LAST_AREA;
30067 ptrdiff_t pos;
30068 struct glyph *glyph;
30069 Lisp_Object object;
30070 Lisp_Object mouse_face = Qnil, position;
30071 Lisp_Object *overlay_vec = NULL;
30072 ptrdiff_t i, noverlays;
30073 struct buffer *obuf;
30074 ptrdiff_t obegv, ozv;
30075 bool same_region;
30076
30077 /* Find the glyph under X/Y. */
30078 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
30079
30080 #ifdef HAVE_WINDOW_SYSTEM
30081 /* Look for :pointer property on image. */
30082 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
30083 {
30084 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
30085 if (img != NULL && IMAGEP (img->spec))
30086 {
30087 Lisp_Object image_map, hotspot;
30088 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
30089 !NILP (image_map))
30090 && (hotspot = find_hot_spot (image_map,
30091 glyph->slice.img.x + dx,
30092 glyph->slice.img.y + dy),
30093 CONSP (hotspot))
30094 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30095 {
30096 Lisp_Object plist;
30097
30098 /* Could check XCAR (hotspot) to see if we enter/leave
30099 this hot-spot.
30100 If so, we could look for mouse-enter, mouse-leave
30101 properties in PLIST (and do something...). */
30102 hotspot = XCDR (hotspot);
30103 if (CONSP (hotspot)
30104 && (plist = XCAR (hotspot), CONSP (plist)))
30105 {
30106 pointer = Fplist_get (plist, Qpointer);
30107 if (NILP (pointer))
30108 pointer = Qhand;
30109 help_echo_string = Fplist_get (plist, Qhelp_echo);
30110 if (!NILP (help_echo_string))
30111 {
30112 help_echo_window = window;
30113 help_echo_object = glyph->object;
30114 help_echo_pos = glyph->charpos;
30115 }
30116 }
30117 }
30118 if (NILP (pointer))
30119 pointer = Fplist_get (XCDR (img->spec), QCpointer);
30120 }
30121 }
30122 #endif /* HAVE_WINDOW_SYSTEM */
30123
30124 /* Clear mouse face if X/Y not over text. */
30125 if (glyph == NULL
30126 || area != TEXT_AREA
30127 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
30128 /* Glyph's OBJECT is nil for glyphs inserted by the
30129 display engine for its internal purposes, like truncation
30130 and continuation glyphs and blanks beyond the end of
30131 line's text on text terminals. If we are over such a
30132 glyph, we are not over any text. */
30133 || NILP (glyph->object)
30134 /* R2L rows have a stretch glyph at their front, which
30135 stands for no text, whereas L2R rows have no glyphs at
30136 all beyond the end of text. Treat such stretch glyphs
30137 like we do with NULL glyphs in L2R rows. */
30138 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
30139 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
30140 && glyph->type == STRETCH_GLYPH
30141 && glyph->avoid_cursor_p))
30142 {
30143 if (clear_mouse_face (hlinfo))
30144 cursor = No_Cursor;
30145 #ifdef HAVE_WINDOW_SYSTEM
30146 if (FRAME_WINDOW_P (f) && NILP (pointer))
30147 {
30148 if (area != TEXT_AREA)
30149 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30150 else
30151 pointer = Vvoid_text_area_pointer;
30152 }
30153 #endif
30154 goto set_cursor;
30155 }
30156
30157 pos = glyph->charpos;
30158 object = glyph->object;
30159 if (!STRINGP (object) && !BUFFERP (object))
30160 goto set_cursor;
30161
30162 /* If we get an out-of-range value, return now; avoid an error. */
30163 if (BUFFERP (object) && pos > BUF_Z (b))
30164 goto set_cursor;
30165
30166 /* Make the window's buffer temporarily current for
30167 overlays_at and compute_char_face. */
30168 obuf = current_buffer;
30169 current_buffer = b;
30170 obegv = BEGV;
30171 ozv = ZV;
30172 BEGV = BEG;
30173 ZV = Z;
30174
30175 /* Is this char mouse-active or does it have help-echo? */
30176 position = make_number (pos);
30177
30178 USE_SAFE_ALLOCA;
30179
30180 if (BUFFERP (object))
30181 {
30182 /* Put all the overlays we want in a vector in overlay_vec. */
30183 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
30184 /* Sort overlays into increasing priority order. */
30185 noverlays = sort_overlays (overlay_vec, noverlays, w);
30186 }
30187 else
30188 noverlays = 0;
30189
30190 if (NILP (Vmouse_highlight))
30191 {
30192 clear_mouse_face (hlinfo);
30193 goto check_help_echo;
30194 }
30195
30196 same_region = coords_in_mouse_face_p (w, hpos, vpos);
30197
30198 if (same_region)
30199 cursor = No_Cursor;
30200
30201 /* Check mouse-face highlighting. */
30202 if (! same_region
30203 /* If there exists an overlay with mouse-face overlapping
30204 the one we are currently highlighting, we have to
30205 check if we enter the overlapping overlay, and then
30206 highlight only that. */
30207 || (OVERLAYP (hlinfo->mouse_face_overlay)
30208 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
30209 {
30210 /* Find the highest priority overlay with a mouse-face. */
30211 Lisp_Object overlay = Qnil;
30212 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
30213 {
30214 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
30215 if (!NILP (mouse_face))
30216 overlay = overlay_vec[i];
30217 }
30218
30219 /* If we're highlighting the same overlay as before, there's
30220 no need to do that again. */
30221 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
30222 goto check_help_echo;
30223 hlinfo->mouse_face_overlay = overlay;
30224
30225 /* Clear the display of the old active region, if any. */
30226 if (clear_mouse_face (hlinfo))
30227 cursor = No_Cursor;
30228
30229 /* If no overlay applies, get a text property. */
30230 if (NILP (overlay))
30231 mouse_face = Fget_text_property (position, Qmouse_face, object);
30232
30233 /* Next, compute the bounds of the mouse highlighting and
30234 display it. */
30235 if (!NILP (mouse_face) && STRINGP (object))
30236 {
30237 /* The mouse-highlighting comes from a display string
30238 with a mouse-face. */
30239 Lisp_Object s, e;
30240 ptrdiff_t ignore;
30241
30242 s = Fprevious_single_property_change
30243 (make_number (pos + 1), Qmouse_face, object, Qnil);
30244 e = Fnext_single_property_change
30245 (position, Qmouse_face, object, Qnil);
30246 if (NILP (s))
30247 s = make_number (0);
30248 if (NILP (e))
30249 e = make_number (SCHARS (object));
30250 mouse_face_from_string_pos (w, hlinfo, object,
30251 XINT (s), XINT (e));
30252 hlinfo->mouse_face_past_end = false;
30253 hlinfo->mouse_face_window = window;
30254 hlinfo->mouse_face_face_id
30255 = face_at_string_position (w, object, pos, 0, &ignore,
30256 glyph->face_id, true);
30257 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30258 cursor = No_Cursor;
30259 }
30260 else
30261 {
30262 /* The mouse-highlighting, if any, comes from an overlay
30263 or text property in the buffer. */
30264 Lisp_Object buffer IF_LINT (= Qnil);
30265 Lisp_Object disp_string IF_LINT (= Qnil);
30266
30267 if (STRINGP (object))
30268 {
30269 /* If we are on a display string with no mouse-face,
30270 check if the text under it has one. */
30271 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
30272 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30273 pos = string_buffer_position (object, start);
30274 if (pos > 0)
30275 {
30276 mouse_face = get_char_property_and_overlay
30277 (make_number (pos), Qmouse_face, w->contents, &overlay);
30278 buffer = w->contents;
30279 disp_string = object;
30280 }
30281 }
30282 else
30283 {
30284 buffer = object;
30285 disp_string = Qnil;
30286 }
30287
30288 if (!NILP (mouse_face))
30289 {
30290 Lisp_Object before, after;
30291 Lisp_Object before_string, after_string;
30292 /* To correctly find the limits of mouse highlight
30293 in a bidi-reordered buffer, we must not use the
30294 optimization of limiting the search in
30295 previous-single-property-change and
30296 next-single-property-change, because
30297 rows_from_pos_range needs the real start and end
30298 positions to DTRT in this case. That's because
30299 the first row visible in a window does not
30300 necessarily display the character whose position
30301 is the smallest. */
30302 Lisp_Object lim1
30303 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30304 ? Fmarker_position (w->start)
30305 : Qnil;
30306 Lisp_Object lim2
30307 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30308 ? make_number (BUF_Z (XBUFFER (buffer))
30309 - w->window_end_pos)
30310 : Qnil;
30311
30312 if (NILP (overlay))
30313 {
30314 /* Handle the text property case. */
30315 before = Fprevious_single_property_change
30316 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30317 after = Fnext_single_property_change
30318 (make_number (pos), Qmouse_face, buffer, lim2);
30319 before_string = after_string = Qnil;
30320 }
30321 else
30322 {
30323 /* Handle the overlay case. */
30324 before = Foverlay_start (overlay);
30325 after = Foverlay_end (overlay);
30326 before_string = Foverlay_get (overlay, Qbefore_string);
30327 after_string = Foverlay_get (overlay, Qafter_string);
30328
30329 if (!STRINGP (before_string)) before_string = Qnil;
30330 if (!STRINGP (after_string)) after_string = Qnil;
30331 }
30332
30333 mouse_face_from_buffer_pos (window, hlinfo, pos,
30334 NILP (before)
30335 ? 1
30336 : XFASTINT (before),
30337 NILP (after)
30338 ? BUF_Z (XBUFFER (buffer))
30339 : XFASTINT (after),
30340 before_string, after_string,
30341 disp_string);
30342 cursor = No_Cursor;
30343 }
30344 }
30345 }
30346
30347 check_help_echo:
30348
30349 /* Look for a `help-echo' property. */
30350 if (NILP (help_echo_string)) {
30351 Lisp_Object help, overlay;
30352
30353 /* Check overlays first. */
30354 help = overlay = Qnil;
30355 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30356 {
30357 overlay = overlay_vec[i];
30358 help = Foverlay_get (overlay, Qhelp_echo);
30359 }
30360
30361 if (!NILP (help))
30362 {
30363 help_echo_string = help;
30364 help_echo_window = window;
30365 help_echo_object = overlay;
30366 help_echo_pos = pos;
30367 }
30368 else
30369 {
30370 Lisp_Object obj = glyph->object;
30371 ptrdiff_t charpos = glyph->charpos;
30372
30373 /* Try text properties. */
30374 if (STRINGP (obj)
30375 && charpos >= 0
30376 && charpos < SCHARS (obj))
30377 {
30378 help = Fget_text_property (make_number (charpos),
30379 Qhelp_echo, obj);
30380 if (NILP (help))
30381 {
30382 /* If the string itself doesn't specify a help-echo,
30383 see if the buffer text ``under'' it does. */
30384 struct glyph_row *r
30385 = MATRIX_ROW (w->current_matrix, vpos);
30386 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30387 ptrdiff_t p = string_buffer_position (obj, start);
30388 if (p > 0)
30389 {
30390 help = Fget_char_property (make_number (p),
30391 Qhelp_echo, w->contents);
30392 if (!NILP (help))
30393 {
30394 charpos = p;
30395 obj = w->contents;
30396 }
30397 }
30398 }
30399 }
30400 else if (BUFFERP (obj)
30401 && charpos >= BEGV
30402 && charpos < ZV)
30403 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30404 obj);
30405
30406 if (!NILP (help))
30407 {
30408 help_echo_string = help;
30409 help_echo_window = window;
30410 help_echo_object = obj;
30411 help_echo_pos = charpos;
30412 }
30413 }
30414 }
30415
30416 #ifdef HAVE_WINDOW_SYSTEM
30417 /* Look for a `pointer' property. */
30418 if (FRAME_WINDOW_P (f) && NILP (pointer))
30419 {
30420 /* Check overlays first. */
30421 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30422 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30423
30424 if (NILP (pointer))
30425 {
30426 Lisp_Object obj = glyph->object;
30427 ptrdiff_t charpos = glyph->charpos;
30428
30429 /* Try text properties. */
30430 if (STRINGP (obj)
30431 && charpos >= 0
30432 && charpos < SCHARS (obj))
30433 {
30434 pointer = Fget_text_property (make_number (charpos),
30435 Qpointer, obj);
30436 if (NILP (pointer))
30437 {
30438 /* If the string itself doesn't specify a pointer,
30439 see if the buffer text ``under'' it does. */
30440 struct glyph_row *r
30441 = MATRIX_ROW (w->current_matrix, vpos);
30442 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30443 ptrdiff_t p = string_buffer_position (obj, start);
30444 if (p > 0)
30445 pointer = Fget_char_property (make_number (p),
30446 Qpointer, w->contents);
30447 }
30448 }
30449 else if (BUFFERP (obj)
30450 && charpos >= BEGV
30451 && charpos < ZV)
30452 pointer = Fget_text_property (make_number (charpos),
30453 Qpointer, obj);
30454 }
30455 }
30456 #endif /* HAVE_WINDOW_SYSTEM */
30457
30458 BEGV = obegv;
30459 ZV = ozv;
30460 current_buffer = obuf;
30461 SAFE_FREE ();
30462 }
30463
30464 set_cursor:
30465
30466 #ifdef HAVE_WINDOW_SYSTEM
30467 if (FRAME_WINDOW_P (f))
30468 define_frame_cursor1 (f, cursor, pointer);
30469 #else
30470 /* This is here to prevent a compiler error, about "label at end of
30471 compound statement". */
30472 return;
30473 #endif
30474 }
30475
30476
30477 /* EXPORT for RIF:
30478 Clear any mouse-face on window W. This function is part of the
30479 redisplay interface, and is called from try_window_id and similar
30480 functions to ensure the mouse-highlight is off. */
30481
30482 void
30483 x_clear_window_mouse_face (struct window *w)
30484 {
30485 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30486 Lisp_Object window;
30487
30488 block_input ();
30489 XSETWINDOW (window, w);
30490 if (EQ (window, hlinfo->mouse_face_window))
30491 clear_mouse_face (hlinfo);
30492 unblock_input ();
30493 }
30494
30495
30496 /* EXPORT:
30497 Just discard the mouse face information for frame F, if any.
30498 This is used when the size of F is changed. */
30499
30500 void
30501 cancel_mouse_face (struct frame *f)
30502 {
30503 Lisp_Object window;
30504 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30505
30506 window = hlinfo->mouse_face_window;
30507 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30508 reset_mouse_highlight (hlinfo);
30509 }
30510
30511
30512 \f
30513 /***********************************************************************
30514 Exposure Events
30515 ***********************************************************************/
30516
30517 #ifdef HAVE_WINDOW_SYSTEM
30518
30519 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30520 which intersects rectangle R. R is in window-relative coordinates. */
30521
30522 static void
30523 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30524 enum glyph_row_area area)
30525 {
30526 struct glyph *first = row->glyphs[area];
30527 struct glyph *end = row->glyphs[area] + row->used[area];
30528 struct glyph *last;
30529 int first_x, start_x, x;
30530
30531 if (area == TEXT_AREA && row->fill_line_p)
30532 /* If row extends face to end of line write the whole line. */
30533 draw_glyphs (w, 0, row, area,
30534 0, row->used[area],
30535 DRAW_NORMAL_TEXT, 0);
30536 else
30537 {
30538 /* Set START_X to the window-relative start position for drawing glyphs of
30539 AREA. The first glyph of the text area can be partially visible.
30540 The first glyphs of other areas cannot. */
30541 start_x = window_box_left_offset (w, area);
30542 x = start_x;
30543 if (area == TEXT_AREA)
30544 x += row->x;
30545
30546 /* Find the first glyph that must be redrawn. */
30547 while (first < end
30548 && x + first->pixel_width < r->x)
30549 {
30550 x += first->pixel_width;
30551 ++first;
30552 }
30553
30554 /* Find the last one. */
30555 last = first;
30556 first_x = x;
30557 /* Use a signed int intermediate value to avoid catastrophic
30558 failures due to comparison between signed and unsigned, when
30559 x is negative (can happen for wide images that are hscrolled). */
30560 int r_end = r->x + r->width;
30561 while (last < end && x < r_end)
30562 {
30563 x += last->pixel_width;
30564 ++last;
30565 }
30566
30567 /* Repaint. */
30568 if (last > first)
30569 draw_glyphs (w, first_x - start_x, row, area,
30570 first - row->glyphs[area], last - row->glyphs[area],
30571 DRAW_NORMAL_TEXT, 0);
30572 }
30573 }
30574
30575
30576 /* Redraw the parts of the glyph row ROW on window W intersecting
30577 rectangle R. R is in window-relative coordinates. Value is
30578 true if mouse-face was overwritten. */
30579
30580 static bool
30581 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30582 {
30583 eassert (row->enabled_p);
30584
30585 if (row->mode_line_p || w->pseudo_window_p)
30586 draw_glyphs (w, 0, row, TEXT_AREA,
30587 0, row->used[TEXT_AREA],
30588 DRAW_NORMAL_TEXT, 0);
30589 else
30590 {
30591 if (row->used[LEFT_MARGIN_AREA])
30592 expose_area (w, row, r, LEFT_MARGIN_AREA);
30593 if (row->used[TEXT_AREA])
30594 expose_area (w, row, r, TEXT_AREA);
30595 if (row->used[RIGHT_MARGIN_AREA])
30596 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30597 draw_row_fringe_bitmaps (w, row);
30598 }
30599
30600 return row->mouse_face_p;
30601 }
30602
30603
30604 /* Redraw those parts of glyphs rows during expose event handling that
30605 overlap other rows. Redrawing of an exposed line writes over parts
30606 of lines overlapping that exposed line; this function fixes that.
30607
30608 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30609 row in W's current matrix that is exposed and overlaps other rows.
30610 LAST_OVERLAPPING_ROW is the last such row. */
30611
30612 static void
30613 expose_overlaps (struct window *w,
30614 struct glyph_row *first_overlapping_row,
30615 struct glyph_row *last_overlapping_row,
30616 XRectangle *r)
30617 {
30618 struct glyph_row *row;
30619
30620 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30621 if (row->overlapping_p)
30622 {
30623 eassert (row->enabled_p && !row->mode_line_p);
30624
30625 row->clip = r;
30626 if (row->used[LEFT_MARGIN_AREA])
30627 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30628
30629 if (row->used[TEXT_AREA])
30630 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30631
30632 if (row->used[RIGHT_MARGIN_AREA])
30633 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30634 row->clip = NULL;
30635 }
30636 }
30637
30638
30639 /* Return true if W's cursor intersects rectangle R. */
30640
30641 static bool
30642 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30643 {
30644 XRectangle cr, result;
30645 struct glyph *cursor_glyph;
30646 struct glyph_row *row;
30647
30648 if (w->phys_cursor.vpos >= 0
30649 && w->phys_cursor.vpos < w->current_matrix->nrows
30650 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30651 row->enabled_p)
30652 && row->cursor_in_fringe_p)
30653 {
30654 /* Cursor is in the fringe. */
30655 cr.x = window_box_right_offset (w,
30656 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30657 ? RIGHT_MARGIN_AREA
30658 : TEXT_AREA));
30659 cr.y = row->y;
30660 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30661 cr.height = row->height;
30662 return x_intersect_rectangles (&cr, r, &result);
30663 }
30664
30665 cursor_glyph = get_phys_cursor_glyph (w);
30666 if (cursor_glyph)
30667 {
30668 /* r is relative to W's box, but w->phys_cursor.x is relative
30669 to left edge of W's TEXT area. Adjust it. */
30670 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30671 cr.y = w->phys_cursor.y;
30672 cr.width = cursor_glyph->pixel_width;
30673 cr.height = w->phys_cursor_height;
30674 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30675 I assume the effect is the same -- and this is portable. */
30676 return x_intersect_rectangles (&cr, r, &result);
30677 }
30678 /* If we don't understand the format, pretend we're not in the hot-spot. */
30679 return false;
30680 }
30681
30682
30683 /* EXPORT:
30684 Draw a vertical window border to the right of window W if W doesn't
30685 have vertical scroll bars. */
30686
30687 void
30688 x_draw_vertical_border (struct window *w)
30689 {
30690 struct frame *f = XFRAME (WINDOW_FRAME (w));
30691
30692 /* We could do better, if we knew what type of scroll-bar the adjacent
30693 windows (on either side) have... But we don't :-(
30694 However, I think this works ok. ++KFS 2003-04-25 */
30695
30696 /* Redraw borders between horizontally adjacent windows. Don't
30697 do it for frames with vertical scroll bars because either the
30698 right scroll bar of a window, or the left scroll bar of its
30699 neighbor will suffice as a border. */
30700 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30701 return;
30702
30703 /* Note: It is necessary to redraw both the left and the right
30704 borders, for when only this single window W is being
30705 redisplayed. */
30706 if (!WINDOW_RIGHTMOST_P (w)
30707 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30708 {
30709 int x0, x1, y0, y1;
30710
30711 window_box_edges (w, &x0, &y0, &x1, &y1);
30712 y1 -= 1;
30713
30714 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30715 x1 -= 1;
30716
30717 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30718 }
30719
30720 if (!WINDOW_LEFTMOST_P (w)
30721 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30722 {
30723 int x0, x1, y0, y1;
30724
30725 window_box_edges (w, &x0, &y0, &x1, &y1);
30726 y1 -= 1;
30727
30728 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30729 x0 -= 1;
30730
30731 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30732 }
30733 }
30734
30735
30736 /* Draw window dividers for window W. */
30737
30738 void
30739 x_draw_right_divider (struct window *w)
30740 {
30741 struct frame *f = WINDOW_XFRAME (w);
30742
30743 if (w->mini || w->pseudo_window_p)
30744 return;
30745 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30746 {
30747 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30748 int x1 = WINDOW_RIGHT_EDGE_X (w);
30749 int y0 = WINDOW_TOP_EDGE_Y (w);
30750 /* The bottom divider prevails. */
30751 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30752
30753 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30754 }
30755 }
30756
30757 static void
30758 x_draw_bottom_divider (struct window *w)
30759 {
30760 struct frame *f = XFRAME (WINDOW_FRAME (w));
30761
30762 if (w->mini || w->pseudo_window_p)
30763 return;
30764 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30765 {
30766 int x0 = WINDOW_LEFT_EDGE_X (w);
30767 int x1 = WINDOW_RIGHT_EDGE_X (w);
30768 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30769 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30770
30771 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30772 }
30773 }
30774
30775 /* Redraw the part of window W intersection rectangle FR. Pixel
30776 coordinates in FR are frame-relative. Call this function with
30777 input blocked. Value is true if the exposure overwrites
30778 mouse-face. */
30779
30780 static bool
30781 expose_window (struct window *w, XRectangle *fr)
30782 {
30783 struct frame *f = XFRAME (w->frame);
30784 XRectangle wr, r;
30785 bool mouse_face_overwritten_p = false;
30786
30787 /* If window is not yet fully initialized, do nothing. This can
30788 happen when toolkit scroll bars are used and a window is split.
30789 Reconfiguring the scroll bar will generate an expose for a newly
30790 created window. */
30791 if (w->current_matrix == NULL)
30792 return false;
30793
30794 /* When we're currently updating the window, display and current
30795 matrix usually don't agree. Arrange for a thorough display
30796 later. */
30797 if (w->must_be_updated_p)
30798 {
30799 SET_FRAME_GARBAGED (f);
30800 return false;
30801 }
30802
30803 /* Frame-relative pixel rectangle of W. */
30804 wr.x = WINDOW_LEFT_EDGE_X (w);
30805 wr.y = WINDOW_TOP_EDGE_Y (w);
30806 wr.width = WINDOW_PIXEL_WIDTH (w);
30807 wr.height = WINDOW_PIXEL_HEIGHT (w);
30808
30809 if (x_intersect_rectangles (fr, &wr, &r))
30810 {
30811 int yb = window_text_bottom_y (w);
30812 struct glyph_row *row;
30813 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30814
30815 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30816 r.x, r.y, r.width, r.height));
30817
30818 /* Convert to window coordinates. */
30819 r.x -= WINDOW_LEFT_EDGE_X (w);
30820 r.y -= WINDOW_TOP_EDGE_Y (w);
30821
30822 /* Turn off the cursor. */
30823 bool cursor_cleared_p = (!w->pseudo_window_p
30824 && phys_cursor_in_rect_p (w, &r));
30825 if (cursor_cleared_p)
30826 x_clear_cursor (w);
30827
30828 /* If the row containing the cursor extends face to end of line,
30829 then expose_area might overwrite the cursor outside the
30830 rectangle and thus notice_overwritten_cursor might clear
30831 w->phys_cursor_on_p. We remember the original value and
30832 check later if it is changed. */
30833 bool phys_cursor_on_p = w->phys_cursor_on_p;
30834
30835 /* Use a signed int intermediate value to avoid catastrophic
30836 failures due to comparison between signed and unsigned, when
30837 y0 or y1 is negative (can happen for tall images). */
30838 int r_bottom = r.y + r.height;
30839
30840 /* Update lines intersecting rectangle R. */
30841 first_overlapping_row = last_overlapping_row = NULL;
30842 for (row = w->current_matrix->rows;
30843 row->enabled_p;
30844 ++row)
30845 {
30846 int y0 = row->y;
30847 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30848
30849 if ((y0 >= r.y && y0 < r_bottom)
30850 || (y1 > r.y && y1 < r_bottom)
30851 || (r.y >= y0 && r.y < y1)
30852 || (r_bottom > y0 && r_bottom < y1))
30853 {
30854 /* A header line may be overlapping, but there is no need
30855 to fix overlapping areas for them. KFS 2005-02-12 */
30856 if (row->overlapping_p && !row->mode_line_p)
30857 {
30858 if (first_overlapping_row == NULL)
30859 first_overlapping_row = row;
30860 last_overlapping_row = row;
30861 }
30862
30863 row->clip = fr;
30864 if (expose_line (w, row, &r))
30865 mouse_face_overwritten_p = true;
30866 row->clip = NULL;
30867 }
30868 else if (row->overlapping_p)
30869 {
30870 /* We must redraw a row overlapping the exposed area. */
30871 if (y0 < r.y
30872 ? y0 + row->phys_height > r.y
30873 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30874 {
30875 if (first_overlapping_row == NULL)
30876 first_overlapping_row = row;
30877 last_overlapping_row = row;
30878 }
30879 }
30880
30881 if (y1 >= yb)
30882 break;
30883 }
30884
30885 /* Display the mode line if there is one. */
30886 if (WINDOW_WANTS_MODELINE_P (w)
30887 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30888 row->enabled_p)
30889 && row->y < r_bottom)
30890 {
30891 if (expose_line (w, row, &r))
30892 mouse_face_overwritten_p = true;
30893 }
30894
30895 if (!w->pseudo_window_p)
30896 {
30897 /* Fix the display of overlapping rows. */
30898 if (first_overlapping_row)
30899 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30900 fr);
30901
30902 /* Draw border between windows. */
30903 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30904 x_draw_right_divider (w);
30905 else
30906 x_draw_vertical_border (w);
30907
30908 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30909 x_draw_bottom_divider (w);
30910
30911 /* Turn the cursor on again. */
30912 if (cursor_cleared_p
30913 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30914 update_window_cursor (w, true);
30915 }
30916 }
30917
30918 return mouse_face_overwritten_p;
30919 }
30920
30921
30922
30923 /* Redraw (parts) of all windows in the window tree rooted at W that
30924 intersect R. R contains frame pixel coordinates. Value is
30925 true if the exposure overwrites mouse-face. */
30926
30927 static bool
30928 expose_window_tree (struct window *w, XRectangle *r)
30929 {
30930 struct frame *f = XFRAME (w->frame);
30931 bool mouse_face_overwritten_p = false;
30932
30933 while (w && !FRAME_GARBAGED_P (f))
30934 {
30935 mouse_face_overwritten_p
30936 |= (WINDOWP (w->contents)
30937 ? expose_window_tree (XWINDOW (w->contents), r)
30938 : expose_window (w, r));
30939
30940 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30941 }
30942
30943 return mouse_face_overwritten_p;
30944 }
30945
30946
30947 /* EXPORT:
30948 Redisplay an exposed area of frame F. X and Y are the upper-left
30949 corner of the exposed rectangle. W and H are width and height of
30950 the exposed area. All are pixel values. W or H zero means redraw
30951 the entire frame. */
30952
30953 void
30954 expose_frame (struct frame *f, int x, int y, int w, int h)
30955 {
30956 XRectangle r;
30957 bool mouse_face_overwritten_p = false;
30958
30959 TRACE ((stderr, "expose_frame "));
30960
30961 /* No need to redraw if frame will be redrawn soon. */
30962 if (FRAME_GARBAGED_P (f))
30963 {
30964 TRACE ((stderr, " garbaged\n"));
30965 return;
30966 }
30967
30968 /* If basic faces haven't been realized yet, there is no point in
30969 trying to redraw anything. This can happen when we get an expose
30970 event while Emacs is starting, e.g. by moving another window. */
30971 if (FRAME_FACE_CACHE (f) == NULL
30972 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30973 {
30974 TRACE ((stderr, " no faces\n"));
30975 return;
30976 }
30977
30978 if (w == 0 || h == 0)
30979 {
30980 r.x = r.y = 0;
30981 r.width = FRAME_TEXT_WIDTH (f);
30982 r.height = FRAME_TEXT_HEIGHT (f);
30983 }
30984 else
30985 {
30986 r.x = x;
30987 r.y = y;
30988 r.width = w;
30989 r.height = h;
30990 }
30991
30992 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30993 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30994
30995 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30996 if (WINDOWP (f->tool_bar_window))
30997 mouse_face_overwritten_p
30998 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30999 #endif
31000
31001 #ifdef HAVE_X_WINDOWS
31002 #ifndef MSDOS
31003 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
31004 if (WINDOWP (f->menu_bar_window))
31005 mouse_face_overwritten_p
31006 |= expose_window (XWINDOW (f->menu_bar_window), &r);
31007 #endif /* not USE_X_TOOLKIT and not USE_GTK */
31008 #endif
31009 #endif
31010
31011 /* Some window managers support a focus-follows-mouse style with
31012 delayed raising of frames. Imagine a partially obscured frame,
31013 and moving the mouse into partially obscured mouse-face on that
31014 frame. The visible part of the mouse-face will be highlighted,
31015 then the WM raises the obscured frame. With at least one WM, KDE
31016 2.1, Emacs is not getting any event for the raising of the frame
31017 (even tried with SubstructureRedirectMask), only Expose events.
31018 These expose events will draw text normally, i.e. not
31019 highlighted. Which means we must redo the highlight here.
31020 Subsume it under ``we love X''. --gerd 2001-08-15 */
31021 /* Included in Windows version because Windows most likely does not
31022 do the right thing if any third party tool offers
31023 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
31024 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
31025 {
31026 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31027 if (f == hlinfo->mouse_face_mouse_frame)
31028 {
31029 int mouse_x = hlinfo->mouse_face_mouse_x;
31030 int mouse_y = hlinfo->mouse_face_mouse_y;
31031 clear_mouse_face (hlinfo);
31032 note_mouse_highlight (f, mouse_x, mouse_y);
31033 }
31034 }
31035 }
31036
31037
31038 /* EXPORT:
31039 Determine the intersection of two rectangles R1 and R2. Return
31040 the intersection in *RESULT. Value is true if RESULT is not
31041 empty. */
31042
31043 bool
31044 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
31045 {
31046 XRectangle *left, *right;
31047 XRectangle *upper, *lower;
31048 bool intersection_p = false;
31049
31050 /* Rearrange so that R1 is the left-most rectangle. */
31051 if (r1->x < r2->x)
31052 left = r1, right = r2;
31053 else
31054 left = r2, right = r1;
31055
31056 /* X0 of the intersection is right.x0, if this is inside R1,
31057 otherwise there is no intersection. */
31058 if (right->x <= left->x + left->width)
31059 {
31060 result->x = right->x;
31061
31062 /* The right end of the intersection is the minimum of
31063 the right ends of left and right. */
31064 result->width = (min (left->x + left->width, right->x + right->width)
31065 - result->x);
31066
31067 /* Same game for Y. */
31068 if (r1->y < r2->y)
31069 upper = r1, lower = r2;
31070 else
31071 upper = r2, lower = r1;
31072
31073 /* The upper end of the intersection is lower.y0, if this is inside
31074 of upper. Otherwise, there is no intersection. */
31075 if (lower->y <= upper->y + upper->height)
31076 {
31077 result->y = lower->y;
31078
31079 /* The lower end of the intersection is the minimum of the lower
31080 ends of upper and lower. */
31081 result->height = (min (lower->y + lower->height,
31082 upper->y + upper->height)
31083 - result->y);
31084 intersection_p = true;
31085 }
31086 }
31087
31088 return intersection_p;
31089 }
31090
31091 #endif /* HAVE_WINDOW_SYSTEM */
31092
31093 \f
31094 /***********************************************************************
31095 Initialization
31096 ***********************************************************************/
31097
31098 void
31099 syms_of_xdisp (void)
31100 {
31101 Vwith_echo_area_save_vector = Qnil;
31102 staticpro (&Vwith_echo_area_save_vector);
31103
31104 Vmessage_stack = Qnil;
31105 staticpro (&Vmessage_stack);
31106
31107 /* Non-nil means don't actually do any redisplay. */
31108 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
31109
31110 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
31111
31112 DEFVAR_BOOL("inhibit-message", inhibit_message,
31113 doc: /* Non-nil means calls to `message' are not displayed.
31114 They are still logged to the *Messages* buffer. */);
31115 inhibit_message = 0;
31116
31117 message_dolog_marker1 = Fmake_marker ();
31118 staticpro (&message_dolog_marker1);
31119 message_dolog_marker2 = Fmake_marker ();
31120 staticpro (&message_dolog_marker2);
31121 message_dolog_marker3 = Fmake_marker ();
31122 staticpro (&message_dolog_marker3);
31123
31124 #ifdef GLYPH_DEBUG
31125 defsubr (&Sdump_frame_glyph_matrix);
31126 defsubr (&Sdump_glyph_matrix);
31127 defsubr (&Sdump_glyph_row);
31128 defsubr (&Sdump_tool_bar_row);
31129 defsubr (&Strace_redisplay);
31130 defsubr (&Strace_to_stderr);
31131 #endif
31132 #ifdef HAVE_WINDOW_SYSTEM
31133 defsubr (&Stool_bar_height);
31134 defsubr (&Slookup_image_map);
31135 #endif
31136 defsubr (&Sline_pixel_height);
31137 defsubr (&Sformat_mode_line);
31138 defsubr (&Sinvisible_p);
31139 defsubr (&Scurrent_bidi_paragraph_direction);
31140 defsubr (&Swindow_text_pixel_size);
31141 defsubr (&Smove_point_visually);
31142 defsubr (&Sbidi_find_overridden_directionality);
31143
31144 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
31145 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
31146 DEFSYM (Qoverriding_local_map, "overriding-local-map");
31147 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
31148 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
31149 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
31150 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
31151 DEFSYM (Qeval, "eval");
31152 DEFSYM (QCdata, ":data");
31153
31154 /* Names of text properties relevant for redisplay. */
31155 DEFSYM (Qdisplay, "display");
31156 DEFSYM (Qspace_width, "space-width");
31157 DEFSYM (Qraise, "raise");
31158 DEFSYM (Qslice, "slice");
31159 DEFSYM (Qspace, "space");
31160 DEFSYM (Qmargin, "margin");
31161 DEFSYM (Qpointer, "pointer");
31162 DEFSYM (Qleft_margin, "left-margin");
31163 DEFSYM (Qright_margin, "right-margin");
31164 DEFSYM (Qcenter, "center");
31165 DEFSYM (Qline_height, "line-height");
31166 DEFSYM (QCalign_to, ":align-to");
31167 DEFSYM (QCrelative_width, ":relative-width");
31168 DEFSYM (QCrelative_height, ":relative-height");
31169 DEFSYM (QCeval, ":eval");
31170 DEFSYM (QCpropertize, ":propertize");
31171 DEFSYM (QCfile, ":file");
31172 DEFSYM (Qfontified, "fontified");
31173 DEFSYM (Qfontification_functions, "fontification-functions");
31174
31175 /* Name of the face used to highlight trailing whitespace. */
31176 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
31177
31178 /* Name and number of the face used to highlight escape glyphs. */
31179 DEFSYM (Qescape_glyph, "escape-glyph");
31180
31181 /* Name and number of the face used to highlight non-breaking spaces. */
31182 DEFSYM (Qnobreak_space, "nobreak-space");
31183
31184 /* The symbol 'image' which is the car of the lists used to represent
31185 images in Lisp. Also a tool bar style. */
31186 DEFSYM (Qimage, "image");
31187
31188 /* Tool bar styles. */
31189 DEFSYM (Qtext, "text");
31190 DEFSYM (Qboth, "both");
31191 DEFSYM (Qboth_horiz, "both-horiz");
31192 DEFSYM (Qtext_image_horiz, "text-image-horiz");
31193
31194 /* The image map types. */
31195 DEFSYM (QCmap, ":map");
31196 DEFSYM (QCpointer, ":pointer");
31197 DEFSYM (Qrect, "rect");
31198 DEFSYM (Qcircle, "circle");
31199 DEFSYM (Qpoly, "poly");
31200
31201 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
31202
31203 DEFSYM (Qgrow_only, "grow-only");
31204 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
31205 DEFSYM (Qposition, "position");
31206 DEFSYM (Qbuffer_position, "buffer-position");
31207 DEFSYM (Qobject, "object");
31208
31209 /* Cursor shapes. */
31210 DEFSYM (Qbar, "bar");
31211 DEFSYM (Qhbar, "hbar");
31212 DEFSYM (Qbox, "box");
31213 DEFSYM (Qhollow, "hollow");
31214
31215 /* Pointer shapes. */
31216 DEFSYM (Qhand, "hand");
31217 DEFSYM (Qarrow, "arrow");
31218 /* also Qtext */
31219
31220 DEFSYM (Qdragging, "dragging");
31221
31222 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
31223
31224 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
31225 staticpro (&list_of_error);
31226
31227 /* Values of those variables at last redisplay are stored as
31228 properties on 'overlay-arrow-position' symbol. However, if
31229 Voverlay_arrow_position is a marker, last-arrow-position is its
31230 numerical position. */
31231 DEFSYM (Qlast_arrow_position, "last-arrow-position");
31232 DEFSYM (Qlast_arrow_string, "last-arrow-string");
31233
31234 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
31235 properties on a symbol in overlay-arrow-variable-list. */
31236 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
31237 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
31238
31239 echo_buffer[0] = echo_buffer[1] = Qnil;
31240 staticpro (&echo_buffer[0]);
31241 staticpro (&echo_buffer[1]);
31242
31243 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
31244 staticpro (&echo_area_buffer[0]);
31245 staticpro (&echo_area_buffer[1]);
31246
31247 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
31248 staticpro (&Vmessages_buffer_name);
31249
31250 mode_line_proptrans_alist = Qnil;
31251 staticpro (&mode_line_proptrans_alist);
31252 mode_line_string_list = Qnil;
31253 staticpro (&mode_line_string_list);
31254 mode_line_string_face = Qnil;
31255 staticpro (&mode_line_string_face);
31256 mode_line_string_face_prop = Qnil;
31257 staticpro (&mode_line_string_face_prop);
31258 Vmode_line_unwind_vector = Qnil;
31259 staticpro (&Vmode_line_unwind_vector);
31260
31261 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
31262
31263 help_echo_string = Qnil;
31264 staticpro (&help_echo_string);
31265 help_echo_object = Qnil;
31266 staticpro (&help_echo_object);
31267 help_echo_window = Qnil;
31268 staticpro (&help_echo_window);
31269 previous_help_echo_string = Qnil;
31270 staticpro (&previous_help_echo_string);
31271 help_echo_pos = -1;
31272
31273 DEFSYM (Qright_to_left, "right-to-left");
31274 DEFSYM (Qleft_to_right, "left-to-right");
31275 defsubr (&Sbidi_resolved_levels);
31276
31277 #ifdef HAVE_WINDOW_SYSTEM
31278 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
31279 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
31280 For example, if a block cursor is over a tab, it will be drawn as
31281 wide as that tab on the display. */);
31282 x_stretch_cursor_p = 0;
31283 #endif
31284
31285 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
31286 doc: /* Non-nil means highlight trailing whitespace.
31287 The face used for trailing whitespace is `trailing-whitespace'. */);
31288 Vshow_trailing_whitespace = Qnil;
31289
31290 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
31291 doc: /* Control highlighting of non-ASCII space and hyphen chars.
31292 If the value is t, Emacs highlights non-ASCII chars which have the
31293 same appearance as an ASCII space or hyphen, using the `nobreak-space'
31294 or `escape-glyph' face respectively.
31295
31296 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
31297 U+2011 (non-breaking hyphen) are affected.
31298
31299 Any other non-nil value means to display these characters as a escape
31300 glyph followed by an ordinary space or hyphen.
31301
31302 A value of nil means no special handling of these characters. */);
31303 Vnobreak_char_display = Qt;
31304
31305 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
31306 doc: /* The pointer shape to show in void text areas.
31307 A value of nil means to show the text pointer. Other options are
31308 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
31309 `hourglass'. */);
31310 Vvoid_text_area_pointer = Qarrow;
31311
31312 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
31313 doc: /* Non-nil means don't actually do any redisplay.
31314 This is used for internal purposes. */);
31315 Vinhibit_redisplay = Qnil;
31316
31317 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
31318 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
31319 Vglobal_mode_string = Qnil;
31320
31321 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31322 doc: /* Marker for where to display an arrow on top of the buffer text.
31323 This must be the beginning of a line in order to work.
31324 See also `overlay-arrow-string'. */);
31325 Voverlay_arrow_position = Qnil;
31326
31327 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31328 doc: /* String to display as an arrow in non-window frames.
31329 See also `overlay-arrow-position'. */);
31330 Voverlay_arrow_string = build_pure_c_string ("=>");
31331
31332 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31333 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31334 The symbols on this list are examined during redisplay to determine
31335 where to display overlay arrows. */);
31336 Voverlay_arrow_variable_list
31337 = list1 (intern_c_string ("overlay-arrow-position"));
31338
31339 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31340 doc: /* The number of lines to try scrolling a window by when point moves out.
31341 If that fails to bring point back on frame, point is centered instead.
31342 If this is zero, point is always centered after it moves off frame.
31343 If you want scrolling to always be a line at a time, you should set
31344 `scroll-conservatively' to a large value rather than set this to 1. */);
31345
31346 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31347 doc: /* Scroll up to this many lines, to bring point back on screen.
31348 If point moves off-screen, redisplay will scroll by up to
31349 `scroll-conservatively' lines in order to bring point just barely
31350 onto the screen again. If that cannot be done, then redisplay
31351 recenters point as usual.
31352
31353 If the value is greater than 100, redisplay will never recenter point,
31354 but will always scroll just enough text to bring point into view, even
31355 if you move far away.
31356
31357 A value of zero means always recenter point if it moves off screen. */);
31358 scroll_conservatively = 0;
31359
31360 DEFVAR_INT ("scroll-margin", scroll_margin,
31361 doc: /* Number of lines of margin at the top and bottom of a window.
31362 Recenter the window whenever point gets within this many lines
31363 of the top or bottom of the window. */);
31364 scroll_margin = 0;
31365
31366 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31367 doc: /* Pixels per inch value for non-window system displays.
31368 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31369 Vdisplay_pixels_per_inch = make_float (72.0);
31370
31371 #ifdef GLYPH_DEBUG
31372 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31373 #endif
31374
31375 DEFVAR_LISP ("truncate-partial-width-windows",
31376 Vtruncate_partial_width_windows,
31377 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31378 For an integer value, truncate lines in each window narrower than the
31379 full frame width, provided the window width is less than that integer;
31380 otherwise, respect the value of `truncate-lines'.
31381
31382 For any other non-nil value, truncate lines in all windows that do
31383 not span the full frame width.
31384
31385 A value of nil means to respect the value of `truncate-lines'.
31386
31387 If `word-wrap' is enabled, you might want to reduce this. */);
31388 Vtruncate_partial_width_windows = make_number (50);
31389
31390 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31391 doc: /* Maximum buffer size for which line number should be displayed.
31392 If the buffer is bigger than this, the line number does not appear
31393 in the mode line. A value of nil means no limit. */);
31394 Vline_number_display_limit = Qnil;
31395
31396 DEFVAR_INT ("line-number-display-limit-width",
31397 line_number_display_limit_width,
31398 doc: /* Maximum line width (in characters) for line number display.
31399 If the average length of the lines near point is bigger than this, then the
31400 line number may be omitted from the mode line. */);
31401 line_number_display_limit_width = 200;
31402
31403 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31404 doc: /* Non-nil means highlight region even in nonselected windows. */);
31405 highlight_nonselected_windows = false;
31406
31407 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31408 doc: /* Non-nil if more than one frame is visible on this display.
31409 Minibuffer-only frames don't count, but iconified frames do.
31410 This variable is not guaranteed to be accurate except while processing
31411 `frame-title-format' and `icon-title-format'. */);
31412
31413 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31414 doc: /* Template for displaying the title bar of visible frames.
31415 (Assuming the window manager supports this feature.)
31416
31417 This variable has the same structure as `mode-line-format', except that
31418 the %c and %l constructs are ignored. It is used only on frames for
31419 which no explicit name has been set (see `modify-frame-parameters'). */);
31420
31421 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31422 doc: /* Template for displaying the title bar of an iconified frame.
31423 (Assuming the window manager supports this feature.)
31424 This variable has the same structure as `mode-line-format' (which see),
31425 and is used only on frames for which no explicit name has been set
31426 (see `modify-frame-parameters'). */);
31427 Vicon_title_format
31428 = Vframe_title_format
31429 = listn (CONSTYPE_PURE, 3,
31430 intern_c_string ("multiple-frames"),
31431 build_pure_c_string ("%b"),
31432 listn (CONSTYPE_PURE, 4,
31433 empty_unibyte_string,
31434 intern_c_string ("invocation-name"),
31435 build_pure_c_string ("@"),
31436 intern_c_string ("system-name")));
31437
31438 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31439 doc: /* Maximum number of lines to keep in the message log buffer.
31440 If nil, disable message logging. If t, log messages but don't truncate
31441 the buffer when it becomes large. */);
31442 Vmessage_log_max = make_number (1000);
31443
31444 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
31445 doc: /* Functions called during redisplay, if window sizes have changed.
31446 The value should be a list of functions that take one argument.
31447 During the first part of redisplay, for each frame, if any of its windows
31448 have changed size since the last redisplay, or have been split or deleted,
31449 all the functions in the list are called, with the frame as argument.
31450 If redisplay decides to resize the minibuffer window, it calls these
31451 functions on behalf of that as well. */);
31452 Vwindow_size_change_functions = Qnil;
31453
31454 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31455 doc: /* List of functions to call before redisplaying a window with scrolling.
31456 Each function is called with two arguments, the window and its new
31457 display-start position.
31458 These functions are called whenever the `window-start' marker is modified,
31459 either to point into another buffer (e.g. via `set-window-buffer') or another
31460 place in the same buffer.
31461 Note that the value of `window-end' is not valid when these functions are
31462 called.
31463
31464 Warning: Do not use this feature to alter the way the window
31465 is scrolled. It is not designed for that, and such use probably won't
31466 work. */);
31467 Vwindow_scroll_functions = Qnil;
31468
31469 DEFVAR_LISP ("window-text-change-functions",
31470 Vwindow_text_change_functions,
31471 doc: /* Functions to call in redisplay when text in the window might change. */);
31472 Vwindow_text_change_functions = Qnil;
31473
31474 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31475 doc: /* Functions called when redisplay of a window reaches the end trigger.
31476 Each function is called with two arguments, the window and the end trigger value.
31477 See `set-window-redisplay-end-trigger'. */);
31478 Vredisplay_end_trigger_functions = Qnil;
31479
31480 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31481 doc: /* Non-nil means autoselect window with mouse pointer.
31482 If nil, do not autoselect windows.
31483 A positive number means delay autoselection by that many seconds: a
31484 window is autoselected only after the mouse has remained in that
31485 window for the duration of the delay.
31486 A negative number has a similar effect, but causes windows to be
31487 autoselected only after the mouse has stopped moving. (Because of
31488 the way Emacs compares mouse events, you will occasionally wait twice
31489 that time before the window gets selected.)
31490 Any other value means to autoselect window instantaneously when the
31491 mouse pointer enters it.
31492
31493 Autoselection selects the minibuffer only if it is active, and never
31494 unselects the minibuffer if it is active.
31495
31496 When customizing this variable make sure that the actual value of
31497 `focus-follows-mouse' matches the behavior of your window manager. */);
31498 Vmouse_autoselect_window = Qnil;
31499
31500 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31501 doc: /* Non-nil means automatically resize tool-bars.
31502 This dynamically changes the tool-bar's height to the minimum height
31503 that is needed to make all tool-bar items visible.
31504 If value is `grow-only', the tool-bar's height is only increased
31505 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31506 Vauto_resize_tool_bars = Qt;
31507
31508 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31509 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31510 auto_raise_tool_bar_buttons_p = true;
31511
31512 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31513 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31514 make_cursor_line_fully_visible_p = true;
31515
31516 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31517 doc: /* Border below tool-bar in pixels.
31518 If an integer, use it as the height of the border.
31519 If it is one of `internal-border-width' or `border-width', use the
31520 value of the corresponding frame parameter.
31521 Otherwise, no border is added below the tool-bar. */);
31522 Vtool_bar_border = Qinternal_border_width;
31523
31524 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31525 doc: /* Margin around tool-bar buttons in pixels.
31526 If an integer, use that for both horizontal and vertical margins.
31527 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31528 HORZ specifying the horizontal margin, and VERT specifying the
31529 vertical margin. */);
31530 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31531
31532 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31533 doc: /* Relief thickness of tool-bar buttons. */);
31534 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31535
31536 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31537 doc: /* Tool bar style to use.
31538 It can be one of
31539 image - show images only
31540 text - show text only
31541 both - show both, text below image
31542 both-horiz - show text to the right of the image
31543 text-image-horiz - show text to the left of the image
31544 any other - use system default or image if no system default.
31545
31546 This variable only affects the GTK+ toolkit version of Emacs. */);
31547 Vtool_bar_style = Qnil;
31548
31549 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31550 doc: /* Maximum number of characters a label can have to be shown.
31551 The tool bar style must also show labels for this to have any effect, see
31552 `tool-bar-style'. */);
31553 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31554
31555 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31556 doc: /* List of functions to call to fontify regions of text.
31557 Each function is called with one argument POS. Functions must
31558 fontify a region starting at POS in the current buffer, and give
31559 fontified regions the property `fontified'. */);
31560 Vfontification_functions = Qnil;
31561 Fmake_variable_buffer_local (Qfontification_functions);
31562
31563 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31564 unibyte_display_via_language_environment,
31565 doc: /* Non-nil means display unibyte text according to language environment.
31566 Specifically, this means that raw bytes in the range 160-255 decimal
31567 are displayed by converting them to the equivalent multibyte characters
31568 according to the current language environment. As a result, they are
31569 displayed according to the current fontset.
31570
31571 Note that this variable affects only how these bytes are displayed,
31572 but does not change the fact they are interpreted as raw bytes. */);
31573 unibyte_display_via_language_environment = false;
31574
31575 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31576 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31577 If a float, it specifies a fraction of the mini-window frame's height.
31578 If an integer, it specifies a number of lines. */);
31579 Vmax_mini_window_height = make_float (0.25);
31580
31581 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31582 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31583 A value of nil means don't automatically resize mini-windows.
31584 A value of t means resize them to fit the text displayed in them.
31585 A value of `grow-only', the default, means let mini-windows grow only;
31586 they return to their normal size when the minibuffer is closed, or the
31587 echo area becomes empty. */);
31588 Vresize_mini_windows = Qgrow_only;
31589
31590 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31591 doc: /* Alist specifying how to blink the cursor off.
31592 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31593 `cursor-type' frame-parameter or variable equals ON-STATE,
31594 comparing using `equal', Emacs uses OFF-STATE to specify
31595 how to blink it off. ON-STATE and OFF-STATE are values for
31596 the `cursor-type' frame parameter.
31597
31598 If a frame's ON-STATE has no entry in this list,
31599 the frame's other specifications determine how to blink the cursor off. */);
31600 Vblink_cursor_alist = Qnil;
31601
31602 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31603 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31604 If non-nil, windows are automatically scrolled horizontally to make
31605 point visible. */);
31606 automatic_hscrolling_p = true;
31607 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31608
31609 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31610 doc: /* How many columns away from the window edge point is allowed to get
31611 before automatic hscrolling will horizontally scroll the window. */);
31612 hscroll_margin = 5;
31613
31614 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31615 doc: /* How many columns to scroll the window when point gets too close to the edge.
31616 When point is less than `hscroll-margin' columns from the window
31617 edge, automatic hscrolling will scroll the window by the amount of columns
31618 determined by this variable. If its value is a positive integer, scroll that
31619 many columns. If it's a positive floating-point number, it specifies the
31620 fraction of the window's width to scroll. If it's nil or zero, point will be
31621 centered horizontally after the scroll. Any other value, including negative
31622 numbers, are treated as if the value were zero.
31623
31624 Automatic hscrolling always moves point outside the scroll margin, so if
31625 point was more than scroll step columns inside the margin, the window will
31626 scroll more than the value given by the scroll step.
31627
31628 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31629 and `scroll-right' overrides this variable's effect. */);
31630 Vhscroll_step = make_number (0);
31631
31632 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31633 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31634 Bind this around calls to `message' to let it take effect. */);
31635 message_truncate_lines = false;
31636
31637 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31638 doc: /* Normal hook run to update the menu bar definitions.
31639 Redisplay runs this hook before it redisplays the menu bar.
31640 This is used to update menus such as Buffers, whose contents depend on
31641 various data. */);
31642 Vmenu_bar_update_hook = Qnil;
31643
31644 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31645 doc: /* Frame for which we are updating a menu.
31646 The enable predicate for a menu binding should check this variable. */);
31647 Vmenu_updating_frame = Qnil;
31648
31649 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31650 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31651 inhibit_menubar_update = false;
31652
31653 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31654 doc: /* Prefix prepended to all continuation lines at display time.
31655 The value may be a string, an image, or a stretch-glyph; it is
31656 interpreted in the same way as the value of a `display' text property.
31657
31658 This variable is overridden by any `wrap-prefix' text or overlay
31659 property.
31660
31661 To add a prefix to non-continuation lines, use `line-prefix'. */);
31662 Vwrap_prefix = Qnil;
31663 DEFSYM (Qwrap_prefix, "wrap-prefix");
31664 Fmake_variable_buffer_local (Qwrap_prefix);
31665
31666 DEFVAR_LISP ("line-prefix", Vline_prefix,
31667 doc: /* Prefix prepended to all non-continuation lines at display time.
31668 The value may be a string, an image, or a stretch-glyph; it is
31669 interpreted in the same way as the value of a `display' text property.
31670
31671 This variable is overridden by any `line-prefix' text or overlay
31672 property.
31673
31674 To add a prefix to continuation lines, use `wrap-prefix'. */);
31675 Vline_prefix = Qnil;
31676 DEFSYM (Qline_prefix, "line-prefix");
31677 Fmake_variable_buffer_local (Qline_prefix);
31678
31679 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31680 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31681 inhibit_eval_during_redisplay = false;
31682
31683 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31684 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31685 inhibit_free_realized_faces = false;
31686
31687 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31688 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31689 Intended for use during debugging and for testing bidi display;
31690 see biditest.el in the test suite. */);
31691 inhibit_bidi_mirroring = false;
31692
31693 #ifdef GLYPH_DEBUG
31694 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31695 doc: /* Inhibit try_window_id display optimization. */);
31696 inhibit_try_window_id = false;
31697
31698 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31699 doc: /* Inhibit try_window_reusing display optimization. */);
31700 inhibit_try_window_reusing = false;
31701
31702 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31703 doc: /* Inhibit try_cursor_movement display optimization. */);
31704 inhibit_try_cursor_movement = false;
31705 #endif /* GLYPH_DEBUG */
31706
31707 DEFVAR_INT ("overline-margin", overline_margin,
31708 doc: /* Space between overline and text, in pixels.
31709 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31710 margin to the character height. */);
31711 overline_margin = 2;
31712
31713 DEFVAR_INT ("underline-minimum-offset",
31714 underline_minimum_offset,
31715 doc: /* Minimum distance between baseline and underline.
31716 This can improve legibility of underlined text at small font sizes,
31717 particularly when using variable `x-use-underline-position-properties'
31718 with fonts that specify an UNDERLINE_POSITION relatively close to the
31719 baseline. The default value is 1. */);
31720 underline_minimum_offset = 1;
31721
31722 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31723 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31724 This feature only works when on a window system that can change
31725 cursor shapes. */);
31726 display_hourglass_p = true;
31727
31728 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31729 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31730 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31731
31732 #ifdef HAVE_WINDOW_SYSTEM
31733 hourglass_atimer = NULL;
31734 hourglass_shown_p = false;
31735 #endif /* HAVE_WINDOW_SYSTEM */
31736
31737 /* Name of the face used to display glyphless characters. */
31738 DEFSYM (Qglyphless_char, "glyphless-char");
31739
31740 /* Method symbols for Vglyphless_char_display. */
31741 DEFSYM (Qhex_code, "hex-code");
31742 DEFSYM (Qempty_box, "empty-box");
31743 DEFSYM (Qthin_space, "thin-space");
31744 DEFSYM (Qzero_width, "zero-width");
31745
31746 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31747 doc: /* Function run just before redisplay.
31748 It is called with one argument, which is the set of windows that are to
31749 be redisplayed. This set can be nil (meaning, only the selected window),
31750 or t (meaning all windows). */);
31751 Vpre_redisplay_function = intern ("ignore");
31752
31753 /* Symbol for the purpose of Vglyphless_char_display. */
31754 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31755 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31756
31757 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31758 doc: /* Char-table defining glyphless characters.
31759 Each element, if non-nil, should be one of the following:
31760 an ASCII acronym string: display this string in a box
31761 `hex-code': display the hexadecimal code of a character in a box
31762 `empty-box': display as an empty box
31763 `thin-space': display as 1-pixel width space
31764 `zero-width': don't display
31765 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31766 display method for graphical terminals and text terminals respectively.
31767 GRAPHICAL and TEXT should each have one of the values listed above.
31768
31769 The char-table has one extra slot to control the display of a character for
31770 which no font is found. This slot only takes effect on graphical terminals.
31771 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31772 `thin-space'. The default is `empty-box'.
31773
31774 If a character has a non-nil entry in an active display table, the
31775 display table takes effect; in this case, Emacs does not consult
31776 `glyphless-char-display' at all. */);
31777 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31778 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31779 Qempty_box);
31780
31781 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31782 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31783 Vdebug_on_message = Qnil;
31784
31785 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31786 doc: /* */);
31787 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
31788
31789 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31790 doc: /* */);
31791 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
31792
31793 DEFVAR_LISP ("redisplay--variables", Vredisplay__variables,
31794 doc: /* A hash-table of variables changing which triggers a thorough redisplay. */);
31795 Vredisplay__variables = Qnil;
31796 }
31797
31798
31799 /* Initialize this module when Emacs starts. */
31800
31801 void
31802 init_xdisp (void)
31803 {
31804 CHARPOS (this_line_start_pos) = 0;
31805
31806 if (!noninteractive)
31807 {
31808 struct window *m = XWINDOW (minibuf_window);
31809 Lisp_Object frame = m->frame;
31810 struct frame *f = XFRAME (frame);
31811 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31812 struct window *r = XWINDOW (root);
31813 int i;
31814
31815 echo_area_window = minibuf_window;
31816
31817 r->top_line = FRAME_TOP_MARGIN (f);
31818 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31819 r->total_cols = FRAME_COLS (f);
31820 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31821 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31822 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31823
31824 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31825 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31826 m->total_cols = FRAME_COLS (f);
31827 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31828 m->total_lines = 1;
31829 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31830
31831 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31832 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31833 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31834
31835 /* The default ellipsis glyphs `...'. */
31836 for (i = 0; i < 3; ++i)
31837 default_invis_vector[i] = make_number ('.');
31838 }
31839
31840 {
31841 /* Allocate the buffer for frame titles.
31842 Also used for `format-mode-line'. */
31843 int size = 100;
31844 mode_line_noprop_buf = xmalloc (size);
31845 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31846 mode_line_noprop_ptr = mode_line_noprop_buf;
31847 mode_line_target = MODE_LINE_DISPLAY;
31848 }
31849
31850 help_echo_showing_p = false;
31851 }
31852
31853 #ifdef HAVE_WINDOW_SYSTEM
31854
31855 /* Platform-independent portion of hourglass implementation. */
31856
31857 /* Timer function of hourglass_atimer. */
31858
31859 static void
31860 show_hourglass (struct atimer *timer)
31861 {
31862 /* The timer implementation will cancel this timer automatically
31863 after this function has run. Set hourglass_atimer to null
31864 so that we know the timer doesn't have to be canceled. */
31865 hourglass_atimer = NULL;
31866
31867 if (!hourglass_shown_p)
31868 {
31869 Lisp_Object tail, frame;
31870
31871 block_input ();
31872
31873 FOR_EACH_FRAME (tail, frame)
31874 {
31875 struct frame *f = XFRAME (frame);
31876
31877 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31878 && FRAME_RIF (f)->show_hourglass)
31879 FRAME_RIF (f)->show_hourglass (f);
31880 }
31881
31882 hourglass_shown_p = true;
31883 unblock_input ();
31884 }
31885 }
31886
31887 /* Cancel a currently active hourglass timer, and start a new one. */
31888
31889 void
31890 start_hourglass (void)
31891 {
31892 struct timespec delay;
31893
31894 cancel_hourglass ();
31895
31896 if (INTEGERP (Vhourglass_delay)
31897 && XINT (Vhourglass_delay) > 0)
31898 delay = make_timespec (min (XINT (Vhourglass_delay),
31899 TYPE_MAXIMUM (time_t)),
31900 0);
31901 else if (FLOATP (Vhourglass_delay)
31902 && XFLOAT_DATA (Vhourglass_delay) > 0)
31903 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31904 else
31905 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31906
31907 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31908 show_hourglass, NULL);
31909 }
31910
31911 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31912 shown. */
31913
31914 void
31915 cancel_hourglass (void)
31916 {
31917 if (hourglass_atimer)
31918 {
31919 cancel_atimer (hourglass_atimer);
31920 hourglass_atimer = NULL;
31921 }
31922
31923 if (hourglass_shown_p)
31924 {
31925 Lisp_Object tail, frame;
31926
31927 block_input ();
31928
31929 FOR_EACH_FRAME (tail, frame)
31930 {
31931 struct frame *f = XFRAME (frame);
31932
31933 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31934 && FRAME_RIF (f)->hide_hourglass)
31935 FRAME_RIF (f)->hide_hourglass (f);
31936 #ifdef HAVE_NTGUI
31937 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31938 else if (!FRAME_W32_P (f))
31939 w32_arrow_cursor ();
31940 #endif
31941 }
31942
31943 hourglass_shown_p = false;
31944 unblock_input ();
31945 }
31946 }
31947
31948 #endif /* HAVE_WINDOW_SYSTEM */