]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Fix the build with --enable-checking=glyphs
[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 is 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 pos = (it->stack + it->sp - 1)->position;
7245 else
7246 INC_TEXT_POS (pos, it->multibyte_p);
7247
7248 if (CHARPOS (pos) >= ZV)
7249 it->end_of_box_run_p = true;
7250 else
7251 {
7252 next_face_id = face_at_buffer_position
7253 (it->w, CHARPOS (pos), &ignore,
7254 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, false, -1);
7255 it->end_of_box_run_p
7256 = (FACE_FROM_ID (it->f, next_face_id)->box
7257 == FACE_NO_BOX);
7258 }
7259 }
7260 }
7261 }
7262 /* next_element_from_display_vector sets this flag according to
7263 faces of the display vector glyphs, see there. */
7264 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7265 {
7266 int face_id = face_after_it_pos (it);
7267 it->end_of_box_run_p
7268 = (face_id != it->face_id
7269 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7270 }
7271 }
7272 /* If we reached the end of the object we've been iterating (e.g., a
7273 display string or an overlay string), and there's something on
7274 IT->stack, proceed with what's on the stack. It doesn't make
7275 sense to return false if there's unprocessed stuff on the stack,
7276 because otherwise that stuff will never be displayed. */
7277 if (!success_p && it->sp > 0)
7278 {
7279 set_iterator_to_next (it, false);
7280 success_p = get_next_display_element (it);
7281 }
7282
7283 /* Value is false if end of buffer or string reached. */
7284 return success_p;
7285 }
7286
7287
7288 /* Move IT to the next display element.
7289
7290 RESEAT_P means if called on a newline in buffer text,
7291 skip to the next visible line start.
7292
7293 Functions get_next_display_element and set_iterator_to_next are
7294 separate because I find this arrangement easier to handle than a
7295 get_next_display_element function that also increments IT's
7296 position. The way it is we can first look at an iterator's current
7297 display element, decide whether it fits on a line, and if it does,
7298 increment the iterator position. The other way around we probably
7299 would either need a flag indicating whether the iterator has to be
7300 incremented the next time, or we would have to implement a
7301 decrement position function which would not be easy to write. */
7302
7303 void
7304 set_iterator_to_next (struct it *it, bool reseat_p)
7305 {
7306 /* Reset flags indicating start and end of a sequence of characters
7307 with box. Reset them at the start of this function because
7308 moving the iterator to a new position might set them. */
7309 it->start_of_box_run_p = it->end_of_box_run_p = false;
7310
7311 switch (it->method)
7312 {
7313 case GET_FROM_BUFFER:
7314 /* The current display element of IT is a character from
7315 current_buffer. Advance in the buffer, and maybe skip over
7316 invisible lines that are so because of selective display. */
7317 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7318 reseat_at_next_visible_line_start (it, false);
7319 else if (it->cmp_it.id >= 0)
7320 {
7321 /* We are currently getting glyphs from a composition. */
7322 if (! it->bidi_p)
7323 {
7324 IT_CHARPOS (*it) += it->cmp_it.nchars;
7325 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7326 }
7327 else
7328 {
7329 int i;
7330
7331 /* Update IT's char/byte positions to point to the first
7332 character of the next grapheme cluster, or to the
7333 character visually after the current composition. */
7334 for (i = 0; i < it->cmp_it.nchars; i++)
7335 bidi_move_to_visually_next (&it->bidi_it);
7336 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7337 IT_CHARPOS (*it) = it->bidi_it.charpos;
7338 }
7339
7340 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7341 && it->cmp_it.to < it->cmp_it.nglyphs)
7342 {
7343 /* Composition created while scanning forward. Proceed
7344 to the next grapheme cluster. */
7345 it->cmp_it.from = it->cmp_it.to;
7346 }
7347 else if ((it->bidi_p && it->cmp_it.reversed_p)
7348 && it->cmp_it.from > 0)
7349 {
7350 /* Composition created while scanning backward. Proceed
7351 to the previous grapheme cluster. */
7352 it->cmp_it.to = it->cmp_it.from;
7353 }
7354 else
7355 {
7356 /* No more grapheme clusters in this composition.
7357 Find the next stop position. */
7358 ptrdiff_t stop = it->end_charpos;
7359
7360 if (it->bidi_it.scan_dir < 0)
7361 /* Now we are scanning backward and don't know
7362 where to stop. */
7363 stop = -1;
7364 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7365 IT_BYTEPOS (*it), stop, Qnil);
7366 }
7367 }
7368 else
7369 {
7370 eassert (it->len != 0);
7371
7372 if (!it->bidi_p)
7373 {
7374 IT_BYTEPOS (*it) += it->len;
7375 IT_CHARPOS (*it) += 1;
7376 }
7377 else
7378 {
7379 int prev_scan_dir = it->bidi_it.scan_dir;
7380 /* If this is a new paragraph, determine its base
7381 direction (a.k.a. its base embedding level). */
7382 if (it->bidi_it.new_paragraph)
7383 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7384 false);
7385 bidi_move_to_visually_next (&it->bidi_it);
7386 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7387 IT_CHARPOS (*it) = it->bidi_it.charpos;
7388 if (prev_scan_dir != it->bidi_it.scan_dir)
7389 {
7390 /* As the scan direction was changed, we must
7391 re-compute the stop position for composition. */
7392 ptrdiff_t stop = it->end_charpos;
7393 if (it->bidi_it.scan_dir < 0)
7394 stop = -1;
7395 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7396 IT_BYTEPOS (*it), stop, Qnil);
7397 }
7398 }
7399 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7400 }
7401 break;
7402
7403 case GET_FROM_C_STRING:
7404 /* Current display element of IT is from a C string. */
7405 if (!it->bidi_p
7406 /* If the string position is beyond string's end, it means
7407 next_element_from_c_string is padding the string with
7408 blanks, in which case we bypass the bidi iterator,
7409 because it cannot deal with such virtual characters. */
7410 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7411 {
7412 IT_BYTEPOS (*it) += it->len;
7413 IT_CHARPOS (*it) += 1;
7414 }
7415 else
7416 {
7417 bidi_move_to_visually_next (&it->bidi_it);
7418 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7419 IT_CHARPOS (*it) = it->bidi_it.charpos;
7420 }
7421 break;
7422
7423 case GET_FROM_DISPLAY_VECTOR:
7424 /* Current display element of IT is from a display table entry.
7425 Advance in the display table definition. Reset it to null if
7426 end reached, and continue with characters from buffers/
7427 strings. */
7428 ++it->current.dpvec_index;
7429
7430 /* Restore face of the iterator to what they were before the
7431 display vector entry (these entries may contain faces). */
7432 it->face_id = it->saved_face_id;
7433
7434 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7435 {
7436 bool recheck_faces = it->ellipsis_p;
7437
7438 if (it->s)
7439 it->method = GET_FROM_C_STRING;
7440 else if (STRINGP (it->string))
7441 it->method = GET_FROM_STRING;
7442 else
7443 {
7444 it->method = GET_FROM_BUFFER;
7445 it->object = it->w->contents;
7446 }
7447
7448 it->dpvec = NULL;
7449 it->current.dpvec_index = -1;
7450
7451 /* Skip over characters which were displayed via IT->dpvec. */
7452 if (it->dpvec_char_len < 0)
7453 reseat_at_next_visible_line_start (it, true);
7454 else if (it->dpvec_char_len > 0)
7455 {
7456 it->len = it->dpvec_char_len;
7457 set_iterator_to_next (it, reseat_p);
7458 }
7459
7460 /* Maybe recheck faces after display vector. */
7461 if (recheck_faces)
7462 {
7463 if (it->method == GET_FROM_STRING)
7464 it->stop_charpos = IT_STRING_CHARPOS (*it);
7465 else
7466 it->stop_charpos = IT_CHARPOS (*it);
7467 }
7468 }
7469 break;
7470
7471 case GET_FROM_STRING:
7472 /* Current display element is a character from a Lisp string. */
7473 eassert (it->s == NULL && STRINGP (it->string));
7474 /* Don't advance past string end. These conditions are true
7475 when set_iterator_to_next is called at the end of
7476 get_next_display_element, in which case the Lisp string is
7477 already exhausted, and all we want is pop the iterator
7478 stack. */
7479 if (it->current.overlay_string_index >= 0)
7480 {
7481 /* This is an overlay string, so there's no padding with
7482 spaces, and the number of characters in the string is
7483 where the string ends. */
7484 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7485 goto consider_string_end;
7486 }
7487 else
7488 {
7489 /* Not an overlay string. There could be padding, so test
7490 against it->end_charpos. */
7491 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7492 goto consider_string_end;
7493 }
7494 if (it->cmp_it.id >= 0)
7495 {
7496 /* We are delivering display elements from a composition.
7497 Update the string position past the grapheme cluster
7498 we've just processed. */
7499 if (! it->bidi_p)
7500 {
7501 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7502 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7503 }
7504 else
7505 {
7506 int i;
7507
7508 for (i = 0; i < it->cmp_it.nchars; i++)
7509 bidi_move_to_visually_next (&it->bidi_it);
7510 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7511 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7512 }
7513
7514 /* Did we exhaust all the grapheme clusters of this
7515 composition? */
7516 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7517 && (it->cmp_it.to < it->cmp_it.nglyphs))
7518 {
7519 /* Not all the grapheme clusters were processed yet;
7520 advance to the next cluster. */
7521 it->cmp_it.from = it->cmp_it.to;
7522 }
7523 else if ((it->bidi_p && it->cmp_it.reversed_p)
7524 && it->cmp_it.from > 0)
7525 {
7526 /* Likewise: advance to the next cluster, but going in
7527 the reverse direction. */
7528 it->cmp_it.to = it->cmp_it.from;
7529 }
7530 else
7531 {
7532 /* This composition was fully processed; find the next
7533 candidate place for checking for composed
7534 characters. */
7535 /* Always limit string searches to the string length;
7536 any padding spaces are not part of the string, and
7537 there cannot be any compositions in that padding. */
7538 ptrdiff_t stop = SCHARS (it->string);
7539
7540 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7541 stop = -1;
7542 else if (it->end_charpos < stop)
7543 {
7544 /* Cf. PRECISION in reseat_to_string: we might be
7545 limited in how many of the string characters we
7546 need to deliver. */
7547 stop = it->end_charpos;
7548 }
7549 composition_compute_stop_pos (&it->cmp_it,
7550 IT_STRING_CHARPOS (*it),
7551 IT_STRING_BYTEPOS (*it), stop,
7552 it->string);
7553 }
7554 }
7555 else
7556 {
7557 if (!it->bidi_p
7558 /* If the string position is beyond string's end, it
7559 means next_element_from_string is padding the string
7560 with blanks, in which case we bypass the bidi
7561 iterator, because it cannot deal with such virtual
7562 characters. */
7563 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7564 {
7565 IT_STRING_BYTEPOS (*it) += it->len;
7566 IT_STRING_CHARPOS (*it) += 1;
7567 }
7568 else
7569 {
7570 int prev_scan_dir = it->bidi_it.scan_dir;
7571
7572 bidi_move_to_visually_next (&it->bidi_it);
7573 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7574 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7575 /* If the scan direction changes, we may need to update
7576 the place where to check for composed characters. */
7577 if (prev_scan_dir != it->bidi_it.scan_dir)
7578 {
7579 ptrdiff_t stop = SCHARS (it->string);
7580
7581 if (it->bidi_it.scan_dir < 0)
7582 stop = -1;
7583 else if (it->end_charpos < stop)
7584 stop = it->end_charpos;
7585
7586 composition_compute_stop_pos (&it->cmp_it,
7587 IT_STRING_CHARPOS (*it),
7588 IT_STRING_BYTEPOS (*it), stop,
7589 it->string);
7590 }
7591 }
7592 }
7593
7594 consider_string_end:
7595
7596 if (it->current.overlay_string_index >= 0)
7597 {
7598 /* IT->string is an overlay string. Advance to the
7599 next, if there is one. */
7600 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7601 {
7602 it->ellipsis_p = false;
7603 next_overlay_string (it);
7604 if (it->ellipsis_p)
7605 setup_for_ellipsis (it, 0);
7606 }
7607 }
7608 else
7609 {
7610 /* IT->string is not an overlay string. If we reached
7611 its end, and there is something on IT->stack, proceed
7612 with what is on the stack. This can be either another
7613 string, this time an overlay string, or a buffer. */
7614 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7615 && it->sp > 0)
7616 {
7617 pop_it (it);
7618 if (it->method == GET_FROM_STRING)
7619 goto consider_string_end;
7620 }
7621 }
7622 break;
7623
7624 case GET_FROM_IMAGE:
7625 case GET_FROM_STRETCH:
7626 case GET_FROM_XWIDGET:
7627
7628 /* The position etc with which we have to proceed are on
7629 the stack. The position may be at the end of a string,
7630 if the `display' property takes up the whole string. */
7631 eassert (it->sp > 0);
7632 pop_it (it);
7633 if (it->method == GET_FROM_STRING)
7634 goto consider_string_end;
7635 break;
7636
7637 default:
7638 /* There are no other methods defined, so this should be a bug. */
7639 emacs_abort ();
7640 }
7641
7642 eassert (it->method != GET_FROM_STRING
7643 || (STRINGP (it->string)
7644 && IT_STRING_CHARPOS (*it) >= 0));
7645 }
7646
7647 /* Load IT's display element fields with information about the next
7648 display element which comes from a display table entry or from the
7649 result of translating a control character to one of the forms `^C'
7650 or `\003'.
7651
7652 IT->dpvec holds the glyphs to return as characters.
7653 IT->saved_face_id holds the face id before the display vector--it
7654 is restored into IT->face_id in set_iterator_to_next. */
7655
7656 static bool
7657 next_element_from_display_vector (struct it *it)
7658 {
7659 Lisp_Object gc;
7660 int prev_face_id = it->face_id;
7661 int next_face_id;
7662
7663 /* Precondition. */
7664 eassert (it->dpvec && it->current.dpvec_index >= 0);
7665
7666 it->face_id = it->saved_face_id;
7667
7668 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7669 That seemed totally bogus - so I changed it... */
7670 gc = it->dpvec[it->current.dpvec_index];
7671
7672 if (GLYPH_CODE_P (gc))
7673 {
7674 struct face *this_face, *prev_face, *next_face;
7675
7676 it->c = GLYPH_CODE_CHAR (gc);
7677 it->len = CHAR_BYTES (it->c);
7678
7679 /* The entry may contain a face id to use. Such a face id is
7680 the id of a Lisp face, not a realized face. A face id of
7681 zero means no face is specified. */
7682 if (it->dpvec_face_id >= 0)
7683 it->face_id = it->dpvec_face_id;
7684 else
7685 {
7686 int lface_id = GLYPH_CODE_FACE (gc);
7687 if (lface_id > 0)
7688 it->face_id = merge_faces (it->f, Qt, lface_id,
7689 it->saved_face_id);
7690 }
7691
7692 /* Glyphs in the display vector could have the box face, so we
7693 need to set the related flags in the iterator, as
7694 appropriate. */
7695 this_face = FACE_FROM_ID (it->f, it->face_id);
7696 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7697
7698 /* Is this character the first character of a box-face run? */
7699 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7700 && (!prev_face
7701 || prev_face->box == FACE_NO_BOX));
7702
7703 /* For the last character of the box-face run, we need to look
7704 either at the next glyph from the display vector, or at the
7705 face we saw before the display vector. */
7706 next_face_id = it->saved_face_id;
7707 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7708 {
7709 if (it->dpvec_face_id >= 0)
7710 next_face_id = it->dpvec_face_id;
7711 else
7712 {
7713 int lface_id =
7714 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7715
7716 if (lface_id > 0)
7717 next_face_id = merge_faces (it->f, Qt, lface_id,
7718 it->saved_face_id);
7719 }
7720 }
7721 next_face = FACE_FROM_ID (it->f, next_face_id);
7722 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7723 && (!next_face
7724 || next_face->box == FACE_NO_BOX));
7725 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7726 }
7727 else
7728 /* Display table entry is invalid. Return a space. */
7729 it->c = ' ', it->len = 1;
7730
7731 /* Don't change position and object of the iterator here. They are
7732 still the values of the character that had this display table
7733 entry or was translated, and that's what we want. */
7734 it->what = IT_CHARACTER;
7735 return true;
7736 }
7737
7738 /* Get the first element of string/buffer in the visual order, after
7739 being reseated to a new position in a string or a buffer. */
7740 static void
7741 get_visually_first_element (struct it *it)
7742 {
7743 bool string_p = STRINGP (it->string) || it->s;
7744 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7745 ptrdiff_t bob = (string_p ? 0 : BEGV);
7746
7747 if (STRINGP (it->string))
7748 {
7749 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7750 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7751 }
7752 else
7753 {
7754 it->bidi_it.charpos = IT_CHARPOS (*it);
7755 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7756 }
7757
7758 if (it->bidi_it.charpos == eob)
7759 {
7760 /* Nothing to do, but reset the FIRST_ELT flag, like
7761 bidi_paragraph_init does, because we are not going to
7762 call it. */
7763 it->bidi_it.first_elt = false;
7764 }
7765 else if (it->bidi_it.charpos == bob
7766 || (!string_p
7767 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7768 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7769 {
7770 /* If we are at the beginning of a line/string, we can produce
7771 the next element right away. */
7772 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7773 bidi_move_to_visually_next (&it->bidi_it);
7774 }
7775 else
7776 {
7777 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7778
7779 /* We need to prime the bidi iterator starting at the line's or
7780 string's beginning, before we will be able to produce the
7781 next element. */
7782 if (string_p)
7783 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7784 else
7785 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7786 IT_BYTEPOS (*it), -1,
7787 &it->bidi_it.bytepos);
7788 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7789 do
7790 {
7791 /* Now return to buffer/string position where we were asked
7792 to get the next display element, and produce that. */
7793 bidi_move_to_visually_next (&it->bidi_it);
7794 }
7795 while (it->bidi_it.bytepos != orig_bytepos
7796 && it->bidi_it.charpos < eob);
7797 }
7798
7799 /* Adjust IT's position information to where we ended up. */
7800 if (STRINGP (it->string))
7801 {
7802 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7803 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7804 }
7805 else
7806 {
7807 IT_CHARPOS (*it) = it->bidi_it.charpos;
7808 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7809 }
7810
7811 if (STRINGP (it->string) || !it->s)
7812 {
7813 ptrdiff_t stop, charpos, bytepos;
7814
7815 if (STRINGP (it->string))
7816 {
7817 eassert (!it->s);
7818 stop = SCHARS (it->string);
7819 if (stop > it->end_charpos)
7820 stop = it->end_charpos;
7821 charpos = IT_STRING_CHARPOS (*it);
7822 bytepos = IT_STRING_BYTEPOS (*it);
7823 }
7824 else
7825 {
7826 stop = it->end_charpos;
7827 charpos = IT_CHARPOS (*it);
7828 bytepos = IT_BYTEPOS (*it);
7829 }
7830 if (it->bidi_it.scan_dir < 0)
7831 stop = -1;
7832 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7833 it->string);
7834 }
7835 }
7836
7837 /* Load IT with the next display element from Lisp string IT->string.
7838 IT->current.string_pos is the current position within the string.
7839 If IT->current.overlay_string_index >= 0, the Lisp string is an
7840 overlay string. */
7841
7842 static bool
7843 next_element_from_string (struct it *it)
7844 {
7845 struct text_pos position;
7846
7847 eassert (STRINGP (it->string));
7848 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7849 eassert (IT_STRING_CHARPOS (*it) >= 0);
7850 position = it->current.string_pos;
7851
7852 /* With bidi reordering, the character to display might not be the
7853 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7854 that we were reseat()ed to a new string, whose paragraph
7855 direction is not known. */
7856 if (it->bidi_p && it->bidi_it.first_elt)
7857 {
7858 get_visually_first_element (it);
7859 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7860 }
7861
7862 /* Time to check for invisible text? */
7863 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7864 {
7865 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7866 {
7867 if (!(!it->bidi_p
7868 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7869 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7870 {
7871 /* With bidi non-linear iteration, we could find
7872 ourselves far beyond the last computed stop_charpos,
7873 with several other stop positions in between that we
7874 missed. Scan them all now, in buffer's logical
7875 order, until we find and handle the last stop_charpos
7876 that precedes our current position. */
7877 handle_stop_backwards (it, it->stop_charpos);
7878 return GET_NEXT_DISPLAY_ELEMENT (it);
7879 }
7880 else
7881 {
7882 if (it->bidi_p)
7883 {
7884 /* Take note of the stop position we just moved
7885 across, for when we will move back across it. */
7886 it->prev_stop = it->stop_charpos;
7887 /* If we are at base paragraph embedding level, take
7888 note of the last stop position seen at this
7889 level. */
7890 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7891 it->base_level_stop = it->stop_charpos;
7892 }
7893 handle_stop (it);
7894
7895 /* Since a handler may have changed IT->method, we must
7896 recurse here. */
7897 return GET_NEXT_DISPLAY_ELEMENT (it);
7898 }
7899 }
7900 else if (it->bidi_p
7901 /* If we are before prev_stop, we may have overstepped
7902 on our way backwards a stop_pos, and if so, we need
7903 to handle that stop_pos. */
7904 && IT_STRING_CHARPOS (*it) < it->prev_stop
7905 /* We can sometimes back up for reasons that have nothing
7906 to do with bidi reordering. E.g., compositions. The
7907 code below is only needed when we are above the base
7908 embedding level, so test for that explicitly. */
7909 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7910 {
7911 /* If we lost track of base_level_stop, we have no better
7912 place for handle_stop_backwards to start from than string
7913 beginning. This happens, e.g., when we were reseated to
7914 the previous screenful of text by vertical-motion. */
7915 if (it->base_level_stop <= 0
7916 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7917 it->base_level_stop = 0;
7918 handle_stop_backwards (it, it->base_level_stop);
7919 return GET_NEXT_DISPLAY_ELEMENT (it);
7920 }
7921 }
7922
7923 if (it->current.overlay_string_index >= 0)
7924 {
7925 /* Get the next character from an overlay string. In overlay
7926 strings, there is no field width or padding with spaces to
7927 do. */
7928 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7929 {
7930 it->what = IT_EOB;
7931 return false;
7932 }
7933 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7934 IT_STRING_BYTEPOS (*it),
7935 it->bidi_it.scan_dir < 0
7936 ? -1
7937 : SCHARS (it->string))
7938 && next_element_from_composition (it))
7939 {
7940 return true;
7941 }
7942 else if (STRING_MULTIBYTE (it->string))
7943 {
7944 const unsigned char *s = (SDATA (it->string)
7945 + IT_STRING_BYTEPOS (*it));
7946 it->c = string_char_and_length (s, &it->len);
7947 }
7948 else
7949 {
7950 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7951 it->len = 1;
7952 }
7953 }
7954 else
7955 {
7956 /* Get the next character from a Lisp string that is not an
7957 overlay string. Such strings come from the mode line, for
7958 example. We may have to pad with spaces, or truncate the
7959 string. See also next_element_from_c_string. */
7960 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7961 {
7962 it->what = IT_EOB;
7963 return false;
7964 }
7965 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7966 {
7967 /* Pad with spaces. */
7968 it->c = ' ', it->len = 1;
7969 CHARPOS (position) = BYTEPOS (position) = -1;
7970 }
7971 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7972 IT_STRING_BYTEPOS (*it),
7973 it->bidi_it.scan_dir < 0
7974 ? -1
7975 : it->string_nchars)
7976 && next_element_from_composition (it))
7977 {
7978 return true;
7979 }
7980 else if (STRING_MULTIBYTE (it->string))
7981 {
7982 const unsigned char *s = (SDATA (it->string)
7983 + IT_STRING_BYTEPOS (*it));
7984 it->c = string_char_and_length (s, &it->len);
7985 }
7986 else
7987 {
7988 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7989 it->len = 1;
7990 }
7991 }
7992
7993 /* Record what we have and where it came from. */
7994 it->what = IT_CHARACTER;
7995 it->object = it->string;
7996 it->position = position;
7997 return true;
7998 }
7999
8000
8001 /* Load IT with next display element from C string IT->s.
8002 IT->string_nchars is the maximum number of characters to return
8003 from the string. IT->end_charpos may be greater than
8004 IT->string_nchars when this function is called, in which case we
8005 may have to return padding spaces. Value is false if end of string
8006 reached, including padding spaces. */
8007
8008 static bool
8009 next_element_from_c_string (struct it *it)
8010 {
8011 bool success_p = true;
8012
8013 eassert (it->s);
8014 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8015 it->what = IT_CHARACTER;
8016 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8017 it->object = make_number (0);
8018
8019 /* With bidi reordering, the character to display might not be the
8020 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8021 we were reseated to a new string, whose paragraph direction is
8022 not known. */
8023 if (it->bidi_p && it->bidi_it.first_elt)
8024 get_visually_first_element (it);
8025
8026 /* IT's position can be greater than IT->string_nchars in case a
8027 field width or precision has been specified when the iterator was
8028 initialized. */
8029 if (IT_CHARPOS (*it) >= it->end_charpos)
8030 {
8031 /* End of the game. */
8032 it->what = IT_EOB;
8033 success_p = false;
8034 }
8035 else if (IT_CHARPOS (*it) >= it->string_nchars)
8036 {
8037 /* Pad with spaces. */
8038 it->c = ' ', it->len = 1;
8039 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8040 }
8041 else if (it->multibyte_p)
8042 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8043 else
8044 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8045
8046 return success_p;
8047 }
8048
8049
8050 /* Set up IT to return characters from an ellipsis, if appropriate.
8051 The definition of the ellipsis glyphs may come from a display table
8052 entry. This function fills IT with the first glyph from the
8053 ellipsis if an ellipsis is to be displayed. */
8054
8055 static bool
8056 next_element_from_ellipsis (struct it *it)
8057 {
8058 if (it->selective_display_ellipsis_p)
8059 setup_for_ellipsis (it, it->len);
8060 else
8061 {
8062 /* The face at the current position may be different from the
8063 face we find after the invisible text. Remember what it
8064 was in IT->saved_face_id, and signal that it's there by
8065 setting face_before_selective_p. */
8066 it->saved_face_id = it->face_id;
8067 it->method = GET_FROM_BUFFER;
8068 it->object = it->w->contents;
8069 reseat_at_next_visible_line_start (it, true);
8070 it->face_before_selective_p = true;
8071 }
8072
8073 return GET_NEXT_DISPLAY_ELEMENT (it);
8074 }
8075
8076
8077 /* Deliver an image display element. The iterator IT is already
8078 filled with image information (done in handle_display_prop). Value
8079 is always true. */
8080
8081
8082 static bool
8083 next_element_from_image (struct it *it)
8084 {
8085 it->what = IT_IMAGE;
8086 return true;
8087 }
8088
8089 static bool
8090 next_element_from_xwidget (struct it *it)
8091 {
8092 it->what = IT_XWIDGET;
8093 return true;
8094 }
8095
8096
8097 /* Fill iterator IT with next display element from a stretch glyph
8098 property. IT->object is the value of the text property. Value is
8099 always true. */
8100
8101 static bool
8102 next_element_from_stretch (struct it *it)
8103 {
8104 it->what = IT_STRETCH;
8105 return true;
8106 }
8107
8108 /* Scan backwards from IT's current position until we find a stop
8109 position, or until BEGV. This is called when we find ourself
8110 before both the last known prev_stop and base_level_stop while
8111 reordering bidirectional text. */
8112
8113 static void
8114 compute_stop_pos_backwards (struct it *it)
8115 {
8116 const int SCAN_BACK_LIMIT = 1000;
8117 struct text_pos pos;
8118 struct display_pos save_current = it->current;
8119 struct text_pos save_position = it->position;
8120 ptrdiff_t charpos = IT_CHARPOS (*it);
8121 ptrdiff_t where_we_are = charpos;
8122 ptrdiff_t save_stop_pos = it->stop_charpos;
8123 ptrdiff_t save_end_pos = it->end_charpos;
8124
8125 eassert (NILP (it->string) && !it->s);
8126 eassert (it->bidi_p);
8127 it->bidi_p = false;
8128 do
8129 {
8130 it->end_charpos = min (charpos + 1, ZV);
8131 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8132 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8133 reseat_1 (it, pos, false);
8134 compute_stop_pos (it);
8135 /* We must advance forward, right? */
8136 if (it->stop_charpos <= charpos)
8137 emacs_abort ();
8138 }
8139 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8140
8141 if (it->stop_charpos <= where_we_are)
8142 it->prev_stop = it->stop_charpos;
8143 else
8144 it->prev_stop = BEGV;
8145 it->bidi_p = true;
8146 it->current = save_current;
8147 it->position = save_position;
8148 it->stop_charpos = save_stop_pos;
8149 it->end_charpos = save_end_pos;
8150 }
8151
8152 /* Scan forward from CHARPOS in the current buffer/string, until we
8153 find a stop position > current IT's position. Then handle the stop
8154 position before that. This is called when we bump into a stop
8155 position while reordering bidirectional text. CHARPOS should be
8156 the last previously processed stop_pos (or BEGV/0, if none were
8157 processed yet) whose position is less that IT's current
8158 position. */
8159
8160 static void
8161 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8162 {
8163 bool bufp = !STRINGP (it->string);
8164 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8165 struct display_pos save_current = it->current;
8166 struct text_pos save_position = it->position;
8167 struct text_pos pos1;
8168 ptrdiff_t next_stop;
8169
8170 /* Scan in strict logical order. */
8171 eassert (it->bidi_p);
8172 it->bidi_p = false;
8173 do
8174 {
8175 it->prev_stop = charpos;
8176 if (bufp)
8177 {
8178 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8179 reseat_1 (it, pos1, false);
8180 }
8181 else
8182 it->current.string_pos = string_pos (charpos, it->string);
8183 compute_stop_pos (it);
8184 /* We must advance forward, right? */
8185 if (it->stop_charpos <= it->prev_stop)
8186 emacs_abort ();
8187 charpos = it->stop_charpos;
8188 }
8189 while (charpos <= where_we_are);
8190
8191 it->bidi_p = true;
8192 it->current = save_current;
8193 it->position = save_position;
8194 next_stop = it->stop_charpos;
8195 it->stop_charpos = it->prev_stop;
8196 handle_stop (it);
8197 it->stop_charpos = next_stop;
8198 }
8199
8200 /* Load IT with the next display element from current_buffer. Value
8201 is false if end of buffer reached. IT->stop_charpos is the next
8202 position at which to stop and check for text properties or buffer
8203 end. */
8204
8205 static bool
8206 next_element_from_buffer (struct it *it)
8207 {
8208 bool success_p = true;
8209
8210 eassert (IT_CHARPOS (*it) >= BEGV);
8211 eassert (NILP (it->string) && !it->s);
8212 eassert (!it->bidi_p
8213 || (EQ (it->bidi_it.string.lstring, Qnil)
8214 && it->bidi_it.string.s == NULL));
8215
8216 /* With bidi reordering, the character to display might not be the
8217 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8218 we were reseat()ed to a new buffer position, which is potentially
8219 a different paragraph. */
8220 if (it->bidi_p && it->bidi_it.first_elt)
8221 {
8222 get_visually_first_element (it);
8223 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8224 }
8225
8226 if (IT_CHARPOS (*it) >= it->stop_charpos)
8227 {
8228 if (IT_CHARPOS (*it) >= it->end_charpos)
8229 {
8230 bool overlay_strings_follow_p;
8231
8232 /* End of the game, except when overlay strings follow that
8233 haven't been returned yet. */
8234 if (it->overlay_strings_at_end_processed_p)
8235 overlay_strings_follow_p = false;
8236 else
8237 {
8238 it->overlay_strings_at_end_processed_p = true;
8239 overlay_strings_follow_p = get_overlay_strings (it, 0);
8240 }
8241
8242 if (overlay_strings_follow_p)
8243 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8244 else
8245 {
8246 it->what = IT_EOB;
8247 it->position = it->current.pos;
8248 success_p = false;
8249 }
8250 }
8251 else if (!(!it->bidi_p
8252 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8253 || IT_CHARPOS (*it) == it->stop_charpos))
8254 {
8255 /* With bidi non-linear iteration, we could find ourselves
8256 far beyond the last computed stop_charpos, with several
8257 other stop positions in between that we missed. Scan
8258 them all now, in buffer's logical order, until we find
8259 and handle the last stop_charpos that precedes our
8260 current position. */
8261 handle_stop_backwards (it, it->stop_charpos);
8262 it->ignore_overlay_strings_at_pos_p = false;
8263 return GET_NEXT_DISPLAY_ELEMENT (it);
8264 }
8265 else
8266 {
8267 if (it->bidi_p)
8268 {
8269 /* Take note of the stop position we just moved across,
8270 for when we will move back across it. */
8271 it->prev_stop = it->stop_charpos;
8272 /* If we are at base paragraph embedding level, take
8273 note of the last stop position seen at this
8274 level. */
8275 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8276 it->base_level_stop = it->stop_charpos;
8277 }
8278 handle_stop (it);
8279 it->ignore_overlay_strings_at_pos_p = false;
8280 return GET_NEXT_DISPLAY_ELEMENT (it);
8281 }
8282 }
8283 else if (it->bidi_p
8284 /* If we are before prev_stop, we may have overstepped on
8285 our way backwards a stop_pos, and if so, we need to
8286 handle that stop_pos. */
8287 && IT_CHARPOS (*it) < it->prev_stop
8288 /* We can sometimes back up for reasons that have nothing
8289 to do with bidi reordering. E.g., compositions. The
8290 code below is only needed when we are above the base
8291 embedding level, so test for that explicitly. */
8292 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8293 {
8294 if (it->base_level_stop <= 0
8295 || IT_CHARPOS (*it) < it->base_level_stop)
8296 {
8297 /* If we lost track of base_level_stop, we need to find
8298 prev_stop by looking backwards. This happens, e.g., when
8299 we were reseated to the previous screenful of text by
8300 vertical-motion. */
8301 it->base_level_stop = BEGV;
8302 compute_stop_pos_backwards (it);
8303 handle_stop_backwards (it, it->prev_stop);
8304 }
8305 else
8306 handle_stop_backwards (it, it->base_level_stop);
8307 it->ignore_overlay_strings_at_pos_p = false;
8308 return GET_NEXT_DISPLAY_ELEMENT (it);
8309 }
8310 else
8311 {
8312 /* No face changes, overlays etc. in sight, so just return a
8313 character from current_buffer. */
8314 unsigned char *p;
8315 ptrdiff_t stop;
8316
8317 /* We moved to the next buffer position, so any info about
8318 previously seen overlays is no longer valid. */
8319 it->ignore_overlay_strings_at_pos_p = false;
8320
8321 /* Maybe run the redisplay end trigger hook. Performance note:
8322 This doesn't seem to cost measurable time. */
8323 if (it->redisplay_end_trigger_charpos
8324 && it->glyph_row
8325 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8326 run_redisplay_end_trigger_hook (it);
8327
8328 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8329 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8330 stop)
8331 && next_element_from_composition (it))
8332 {
8333 return true;
8334 }
8335
8336 /* Get the next character, maybe multibyte. */
8337 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8338 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8339 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8340 else
8341 it->c = *p, it->len = 1;
8342
8343 /* Record what we have and where it came from. */
8344 it->what = IT_CHARACTER;
8345 it->object = it->w->contents;
8346 it->position = it->current.pos;
8347
8348 /* Normally we return the character found above, except when we
8349 really want to return an ellipsis for selective display. */
8350 if (it->selective)
8351 {
8352 if (it->c == '\n')
8353 {
8354 /* A value of selective > 0 means hide lines indented more
8355 than that number of columns. */
8356 if (it->selective > 0
8357 && IT_CHARPOS (*it) + 1 < ZV
8358 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8359 IT_BYTEPOS (*it) + 1,
8360 it->selective))
8361 {
8362 success_p = next_element_from_ellipsis (it);
8363 it->dpvec_char_len = -1;
8364 }
8365 }
8366 else if (it->c == '\r' && it->selective == -1)
8367 {
8368 /* A value of selective == -1 means that everything from the
8369 CR to the end of the line is invisible, with maybe an
8370 ellipsis displayed for it. */
8371 success_p = next_element_from_ellipsis (it);
8372 it->dpvec_char_len = -1;
8373 }
8374 }
8375 }
8376
8377 /* Value is false if end of buffer reached. */
8378 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8379 return success_p;
8380 }
8381
8382
8383 /* Run the redisplay end trigger hook for IT. */
8384
8385 static void
8386 run_redisplay_end_trigger_hook (struct it *it)
8387 {
8388 /* IT->glyph_row should be non-null, i.e. we should be actually
8389 displaying something, or otherwise we should not run the hook. */
8390 eassert (it->glyph_row);
8391
8392 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8393 it->redisplay_end_trigger_charpos = 0;
8394
8395 /* Since we are *trying* to run these functions, don't try to run
8396 them again, even if they get an error. */
8397 wset_redisplay_end_trigger (it->w, Qnil);
8398 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8399 make_number (charpos));
8400
8401 /* Notice if it changed the face of the character we are on. */
8402 handle_face_prop (it);
8403 }
8404
8405
8406 /* Deliver a composition display element. Unlike the other
8407 next_element_from_XXX, this function is not registered in the array
8408 get_next_element[]. It is called from next_element_from_buffer and
8409 next_element_from_string when necessary. */
8410
8411 static bool
8412 next_element_from_composition (struct it *it)
8413 {
8414 it->what = IT_COMPOSITION;
8415 it->len = it->cmp_it.nbytes;
8416 if (STRINGP (it->string))
8417 {
8418 if (it->c < 0)
8419 {
8420 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8421 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8422 return false;
8423 }
8424 it->position = it->current.string_pos;
8425 it->object = it->string;
8426 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8427 IT_STRING_BYTEPOS (*it), it->string);
8428 }
8429 else
8430 {
8431 if (it->c < 0)
8432 {
8433 IT_CHARPOS (*it) += it->cmp_it.nchars;
8434 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8435 if (it->bidi_p)
8436 {
8437 if (it->bidi_it.new_paragraph)
8438 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8439 false);
8440 /* Resync the bidi iterator with IT's new position.
8441 FIXME: this doesn't support bidirectional text. */
8442 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8443 bidi_move_to_visually_next (&it->bidi_it);
8444 }
8445 return false;
8446 }
8447 it->position = it->current.pos;
8448 it->object = it->w->contents;
8449 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8450 IT_BYTEPOS (*it), Qnil);
8451 }
8452 return true;
8453 }
8454
8455
8456 \f
8457 /***********************************************************************
8458 Moving an iterator without producing glyphs
8459 ***********************************************************************/
8460
8461 /* Check if iterator is at a position corresponding to a valid buffer
8462 position after some move_it_ call. */
8463
8464 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8465 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8466
8467
8468 /* Move iterator IT to a specified buffer or X position within one
8469 line on the display without producing glyphs.
8470
8471 OP should be a bit mask including some or all of these bits:
8472 MOVE_TO_X: Stop upon reaching x-position TO_X.
8473 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8474 Regardless of OP's value, stop upon reaching the end of the display line.
8475
8476 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8477 This means, in particular, that TO_X includes window's horizontal
8478 scroll amount.
8479
8480 The return value has several possible values that
8481 say what condition caused the scan to stop:
8482
8483 MOVE_POS_MATCH_OR_ZV
8484 - when TO_POS or ZV was reached.
8485
8486 MOVE_X_REACHED
8487 -when TO_X was reached before TO_POS or ZV were reached.
8488
8489 MOVE_LINE_CONTINUED
8490 - when we reached the end of the display area and the line must
8491 be continued.
8492
8493 MOVE_LINE_TRUNCATED
8494 - when we reached the end of the display area and the line is
8495 truncated.
8496
8497 MOVE_NEWLINE_OR_CR
8498 - when we stopped at a line end, i.e. a newline or a CR and selective
8499 display is on. */
8500
8501 static enum move_it_result
8502 move_it_in_display_line_to (struct it *it,
8503 ptrdiff_t to_charpos, int to_x,
8504 enum move_operation_enum op)
8505 {
8506 enum move_it_result result = MOVE_UNDEFINED;
8507 struct glyph_row *saved_glyph_row;
8508 struct it wrap_it, atpos_it, atx_it, ppos_it;
8509 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8510 void *ppos_data = NULL;
8511 bool may_wrap = false;
8512 enum it_method prev_method = it->method;
8513 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8514 bool saw_smaller_pos = prev_pos < to_charpos;
8515
8516 /* Don't produce glyphs in produce_glyphs. */
8517 saved_glyph_row = it->glyph_row;
8518 it->glyph_row = NULL;
8519
8520 /* Use wrap_it to save a copy of IT wherever a word wrap could
8521 occur. Use atpos_it to save a copy of IT at the desired buffer
8522 position, if found, so that we can scan ahead and check if the
8523 word later overshoots the window edge. Use atx_it similarly, for
8524 pixel positions. */
8525 wrap_it.sp = -1;
8526 atpos_it.sp = -1;
8527 atx_it.sp = -1;
8528
8529 /* Use ppos_it under bidi reordering to save a copy of IT for the
8530 initial position. We restore that position in IT when we have
8531 scanned the entire display line without finding a match for
8532 TO_CHARPOS and all the character positions are greater than
8533 TO_CHARPOS. We then restart the scan from the initial position,
8534 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8535 the closest to TO_CHARPOS. */
8536 if (it->bidi_p)
8537 {
8538 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8539 {
8540 SAVE_IT (ppos_it, *it, ppos_data);
8541 closest_pos = IT_CHARPOS (*it);
8542 }
8543 else
8544 closest_pos = ZV;
8545 }
8546
8547 #define BUFFER_POS_REACHED_P() \
8548 ((op & MOVE_TO_POS) != 0 \
8549 && BUFFERP (it->object) \
8550 && (IT_CHARPOS (*it) == to_charpos \
8551 || ((!it->bidi_p \
8552 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8553 && IT_CHARPOS (*it) > to_charpos) \
8554 || (it->what == IT_COMPOSITION \
8555 && ((IT_CHARPOS (*it) > to_charpos \
8556 && to_charpos >= it->cmp_it.charpos) \
8557 || (IT_CHARPOS (*it) < to_charpos \
8558 && to_charpos <= it->cmp_it.charpos)))) \
8559 && (it->method == GET_FROM_BUFFER \
8560 || (it->method == GET_FROM_DISPLAY_VECTOR \
8561 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8562
8563 /* If there's a line-/wrap-prefix, handle it. */
8564 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8565 && it->current_y < it->last_visible_y)
8566 handle_line_prefix (it);
8567
8568 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8569 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8570
8571 while (true)
8572 {
8573 int x, i, ascent = 0, descent = 0;
8574
8575 /* Utility macro to reset an iterator with x, ascent, and descent. */
8576 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8577 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8578 (IT)->max_descent = descent)
8579
8580 /* Stop if we move beyond TO_CHARPOS (after an image or a
8581 display string or stretch glyph). */
8582 if ((op & MOVE_TO_POS) != 0
8583 && BUFFERP (it->object)
8584 && it->method == GET_FROM_BUFFER
8585 && (((!it->bidi_p
8586 /* When the iterator is at base embedding level, we
8587 are guaranteed that characters are delivered for
8588 display in strictly increasing order of their
8589 buffer positions. */
8590 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8591 && IT_CHARPOS (*it) > to_charpos)
8592 || (it->bidi_p
8593 && (prev_method == GET_FROM_IMAGE
8594 || prev_method == GET_FROM_STRETCH
8595 || prev_method == GET_FROM_STRING)
8596 /* Passed TO_CHARPOS from left to right. */
8597 && ((prev_pos < to_charpos
8598 && IT_CHARPOS (*it) > to_charpos)
8599 /* Passed TO_CHARPOS from right to left. */
8600 || (prev_pos > to_charpos
8601 && IT_CHARPOS (*it) < to_charpos)))))
8602 {
8603 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8604 {
8605 result = MOVE_POS_MATCH_OR_ZV;
8606 break;
8607 }
8608 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8609 /* If wrap_it is valid, the current position might be in a
8610 word that is wrapped. So, save the iterator in
8611 atpos_it and continue to see if wrapping happens. */
8612 SAVE_IT (atpos_it, *it, atpos_data);
8613 }
8614
8615 /* Stop when ZV reached.
8616 We used to stop here when TO_CHARPOS reached as well, but that is
8617 too soon if this glyph does not fit on this line. So we handle it
8618 explicitly below. */
8619 if (!get_next_display_element (it))
8620 {
8621 result = MOVE_POS_MATCH_OR_ZV;
8622 break;
8623 }
8624
8625 if (it->line_wrap == TRUNCATE)
8626 {
8627 if (BUFFER_POS_REACHED_P ())
8628 {
8629 result = MOVE_POS_MATCH_OR_ZV;
8630 break;
8631 }
8632 }
8633 else
8634 {
8635 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8636 {
8637 if (IT_DISPLAYING_WHITESPACE (it))
8638 may_wrap = true;
8639 else if (may_wrap)
8640 {
8641 /* We have reached a glyph that follows one or more
8642 whitespace characters. If the position is
8643 already found, we are done. */
8644 if (atpos_it.sp >= 0)
8645 {
8646 RESTORE_IT (it, &atpos_it, atpos_data);
8647 result = MOVE_POS_MATCH_OR_ZV;
8648 goto done;
8649 }
8650 if (atx_it.sp >= 0)
8651 {
8652 RESTORE_IT (it, &atx_it, atx_data);
8653 result = MOVE_X_REACHED;
8654 goto done;
8655 }
8656 /* Otherwise, we can wrap here. */
8657 SAVE_IT (wrap_it, *it, wrap_data);
8658 may_wrap = false;
8659 }
8660 }
8661 }
8662
8663 /* Remember the line height for the current line, in case
8664 the next element doesn't fit on the line. */
8665 ascent = it->max_ascent;
8666 descent = it->max_descent;
8667
8668 /* The call to produce_glyphs will get the metrics of the
8669 display element IT is loaded with. Record the x-position
8670 before this display element, in case it doesn't fit on the
8671 line. */
8672 x = it->current_x;
8673
8674 PRODUCE_GLYPHS (it);
8675
8676 if (it->area != TEXT_AREA)
8677 {
8678 prev_method = it->method;
8679 if (it->method == GET_FROM_BUFFER)
8680 prev_pos = IT_CHARPOS (*it);
8681 set_iterator_to_next (it, true);
8682 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8683 SET_TEXT_POS (this_line_min_pos,
8684 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8685 if (it->bidi_p
8686 && (op & MOVE_TO_POS)
8687 && IT_CHARPOS (*it) > to_charpos
8688 && IT_CHARPOS (*it) < closest_pos)
8689 closest_pos = IT_CHARPOS (*it);
8690 continue;
8691 }
8692
8693 /* The number of glyphs we get back in IT->nglyphs will normally
8694 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8695 character on a terminal frame, or (iii) a line end. For the
8696 second case, IT->nglyphs - 1 padding glyphs will be present.
8697 (On X frames, there is only one glyph produced for a
8698 composite character.)
8699
8700 The behavior implemented below means, for continuation lines,
8701 that as many spaces of a TAB as fit on the current line are
8702 displayed there. For terminal frames, as many glyphs of a
8703 multi-glyph character are displayed in the current line, too.
8704 This is what the old redisplay code did, and we keep it that
8705 way. Under X, the whole shape of a complex character must
8706 fit on the line or it will be completely displayed in the
8707 next line.
8708
8709 Note that both for tabs and padding glyphs, all glyphs have
8710 the same width. */
8711 if (it->nglyphs)
8712 {
8713 /* More than one glyph or glyph doesn't fit on line. All
8714 glyphs have the same width. */
8715 int single_glyph_width = it->pixel_width / it->nglyphs;
8716 int new_x;
8717 int x_before_this_char = x;
8718 int hpos_before_this_char = it->hpos;
8719
8720 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8721 {
8722 new_x = x + single_glyph_width;
8723
8724 /* We want to leave anything reaching TO_X to the caller. */
8725 if ((op & MOVE_TO_X) && new_x > to_x)
8726 {
8727 if (BUFFER_POS_REACHED_P ())
8728 {
8729 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8730 goto buffer_pos_reached;
8731 if (atpos_it.sp < 0)
8732 {
8733 SAVE_IT (atpos_it, *it, atpos_data);
8734 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8735 }
8736 }
8737 else
8738 {
8739 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8740 {
8741 it->current_x = x;
8742 result = MOVE_X_REACHED;
8743 break;
8744 }
8745 if (atx_it.sp < 0)
8746 {
8747 SAVE_IT (atx_it, *it, atx_data);
8748 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8749 }
8750 }
8751 }
8752
8753 if (/* Lines are continued. */
8754 it->line_wrap != TRUNCATE
8755 && (/* And glyph doesn't fit on the line. */
8756 new_x > it->last_visible_x
8757 /* Or it fits exactly and we're on a window
8758 system frame. */
8759 || (new_x == it->last_visible_x
8760 && FRAME_WINDOW_P (it->f)
8761 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8762 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8763 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8764 {
8765 if (/* IT->hpos == 0 means the very first glyph
8766 doesn't fit on the line, e.g. a wide image. */
8767 it->hpos == 0
8768 || (new_x == it->last_visible_x
8769 && FRAME_WINDOW_P (it->f)))
8770 {
8771 ++it->hpos;
8772 it->current_x = new_x;
8773
8774 /* The character's last glyph just barely fits
8775 in this row. */
8776 if (i == it->nglyphs - 1)
8777 {
8778 /* If this is the destination position,
8779 return a position *before* it in this row,
8780 now that we know it fits in this row. */
8781 if (BUFFER_POS_REACHED_P ())
8782 {
8783 if (it->line_wrap != WORD_WRAP
8784 || wrap_it.sp < 0
8785 /* If we've just found whitespace to
8786 wrap, effectively ignore the
8787 previous wrap point -- it is no
8788 longer relevant, but we won't
8789 have an opportunity to update it,
8790 since we've reached the edge of
8791 this screen line. */
8792 || (may_wrap
8793 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8794 {
8795 it->hpos = hpos_before_this_char;
8796 it->current_x = x_before_this_char;
8797 result = MOVE_POS_MATCH_OR_ZV;
8798 break;
8799 }
8800 if (it->line_wrap == WORD_WRAP
8801 && atpos_it.sp < 0)
8802 {
8803 SAVE_IT (atpos_it, *it, atpos_data);
8804 atpos_it.current_x = x_before_this_char;
8805 atpos_it.hpos = hpos_before_this_char;
8806 }
8807 }
8808
8809 prev_method = it->method;
8810 if (it->method == GET_FROM_BUFFER)
8811 prev_pos = IT_CHARPOS (*it);
8812 set_iterator_to_next (it, true);
8813 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8814 SET_TEXT_POS (this_line_min_pos,
8815 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8816 /* On graphical terminals, newlines may
8817 "overflow" into the fringe if
8818 overflow-newline-into-fringe is non-nil.
8819 On text terminals, and on graphical
8820 terminals with no right margin, newlines
8821 may overflow into the last glyph on the
8822 display line.*/
8823 if (!FRAME_WINDOW_P (it->f)
8824 || ((it->bidi_p
8825 && it->bidi_it.paragraph_dir == R2L)
8826 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8827 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8828 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8829 {
8830 if (!get_next_display_element (it))
8831 {
8832 result = MOVE_POS_MATCH_OR_ZV;
8833 break;
8834 }
8835 if (BUFFER_POS_REACHED_P ())
8836 {
8837 if (ITERATOR_AT_END_OF_LINE_P (it))
8838 result = MOVE_POS_MATCH_OR_ZV;
8839 else
8840 result = MOVE_LINE_CONTINUED;
8841 break;
8842 }
8843 if (ITERATOR_AT_END_OF_LINE_P (it)
8844 && (it->line_wrap != WORD_WRAP
8845 || wrap_it.sp < 0
8846 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8847 {
8848 result = MOVE_NEWLINE_OR_CR;
8849 break;
8850 }
8851 }
8852 }
8853 }
8854 else
8855 IT_RESET_X_ASCENT_DESCENT (it);
8856
8857 /* If the screen line ends with whitespace, and we
8858 are under word-wrap, don't use wrap_it: it is no
8859 longer relevant, but we won't have an opportunity
8860 to update it, since we are done with this screen
8861 line. */
8862 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8863 {
8864 /* If we've found TO_X, go back there, as we now
8865 know the last word fits on this screen line. */
8866 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8867 && atx_it.sp >= 0)
8868 {
8869 RESTORE_IT (it, &atx_it, atx_data);
8870 atpos_it.sp = -1;
8871 atx_it.sp = -1;
8872 result = MOVE_X_REACHED;
8873 break;
8874 }
8875 }
8876 else if (wrap_it.sp >= 0)
8877 {
8878 RESTORE_IT (it, &wrap_it, wrap_data);
8879 atpos_it.sp = -1;
8880 atx_it.sp = -1;
8881 }
8882
8883 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8884 IT_CHARPOS (*it)));
8885 result = MOVE_LINE_CONTINUED;
8886 break;
8887 }
8888
8889 if (BUFFER_POS_REACHED_P ())
8890 {
8891 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8892 goto buffer_pos_reached;
8893 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8894 {
8895 SAVE_IT (atpos_it, *it, atpos_data);
8896 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8897 }
8898 }
8899
8900 if (new_x > it->first_visible_x)
8901 {
8902 /* Glyph is visible. Increment number of glyphs that
8903 would be displayed. */
8904 ++it->hpos;
8905 }
8906 }
8907
8908 if (result != MOVE_UNDEFINED)
8909 break;
8910 }
8911 else if (BUFFER_POS_REACHED_P ())
8912 {
8913 buffer_pos_reached:
8914 IT_RESET_X_ASCENT_DESCENT (it);
8915 result = MOVE_POS_MATCH_OR_ZV;
8916 break;
8917 }
8918 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8919 {
8920 /* Stop when TO_X specified and reached. This check is
8921 necessary here because of lines consisting of a line end,
8922 only. The line end will not produce any glyphs and we
8923 would never get MOVE_X_REACHED. */
8924 eassert (it->nglyphs == 0);
8925 result = MOVE_X_REACHED;
8926 break;
8927 }
8928
8929 /* Is this a line end? If yes, we're done. */
8930 if (ITERATOR_AT_END_OF_LINE_P (it))
8931 {
8932 /* If we are past TO_CHARPOS, but never saw any character
8933 positions smaller than TO_CHARPOS, return
8934 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8935 did. */
8936 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8937 {
8938 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8939 {
8940 if (closest_pos < ZV)
8941 {
8942 RESTORE_IT (it, &ppos_it, ppos_data);
8943 /* Don't recurse if closest_pos is equal to
8944 to_charpos, since we have just tried that. */
8945 if (closest_pos != to_charpos)
8946 move_it_in_display_line_to (it, closest_pos, -1,
8947 MOVE_TO_POS);
8948 result = MOVE_POS_MATCH_OR_ZV;
8949 }
8950 else
8951 goto buffer_pos_reached;
8952 }
8953 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8954 && IT_CHARPOS (*it) > to_charpos)
8955 goto buffer_pos_reached;
8956 else
8957 result = MOVE_NEWLINE_OR_CR;
8958 }
8959 else
8960 result = MOVE_NEWLINE_OR_CR;
8961 break;
8962 }
8963
8964 prev_method = it->method;
8965 if (it->method == GET_FROM_BUFFER)
8966 prev_pos = IT_CHARPOS (*it);
8967 /* The current display element has been consumed. Advance
8968 to the next. */
8969 set_iterator_to_next (it, true);
8970 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8971 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8972 if (IT_CHARPOS (*it) < to_charpos)
8973 saw_smaller_pos = true;
8974 if (it->bidi_p
8975 && (op & MOVE_TO_POS)
8976 && IT_CHARPOS (*it) >= to_charpos
8977 && IT_CHARPOS (*it) < closest_pos)
8978 closest_pos = IT_CHARPOS (*it);
8979
8980 /* Stop if lines are truncated and IT's current x-position is
8981 past the right edge of the window now. */
8982 if (it->line_wrap == TRUNCATE
8983 && it->current_x >= it->last_visible_x)
8984 {
8985 if (!FRAME_WINDOW_P (it->f)
8986 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8987 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8988 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8989 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8990 {
8991 bool at_eob_p = false;
8992
8993 if ((at_eob_p = !get_next_display_element (it))
8994 || BUFFER_POS_REACHED_P ()
8995 /* If we are past TO_CHARPOS, but never saw any
8996 character positions smaller than TO_CHARPOS,
8997 return MOVE_POS_MATCH_OR_ZV, like the
8998 unidirectional display did. */
8999 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9000 && !saw_smaller_pos
9001 && IT_CHARPOS (*it) > to_charpos))
9002 {
9003 if (it->bidi_p
9004 && !BUFFER_POS_REACHED_P ()
9005 && !at_eob_p && closest_pos < ZV)
9006 {
9007 RESTORE_IT (it, &ppos_it, ppos_data);
9008 if (closest_pos != to_charpos)
9009 move_it_in_display_line_to (it, closest_pos, -1,
9010 MOVE_TO_POS);
9011 }
9012 result = MOVE_POS_MATCH_OR_ZV;
9013 break;
9014 }
9015 if (ITERATOR_AT_END_OF_LINE_P (it))
9016 {
9017 result = MOVE_NEWLINE_OR_CR;
9018 break;
9019 }
9020 }
9021 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9022 && !saw_smaller_pos
9023 && IT_CHARPOS (*it) > to_charpos)
9024 {
9025 if (closest_pos < ZV)
9026 {
9027 RESTORE_IT (it, &ppos_it, ppos_data);
9028 if (closest_pos != to_charpos)
9029 move_it_in_display_line_to (it, closest_pos, -1,
9030 MOVE_TO_POS);
9031 }
9032 result = MOVE_POS_MATCH_OR_ZV;
9033 break;
9034 }
9035 result = MOVE_LINE_TRUNCATED;
9036 break;
9037 }
9038 #undef IT_RESET_X_ASCENT_DESCENT
9039 }
9040
9041 #undef BUFFER_POS_REACHED_P
9042
9043 /* If we scanned beyond to_pos and didn't find a point to wrap at,
9044 restore the saved iterator. */
9045 if (atpos_it.sp >= 0)
9046 RESTORE_IT (it, &atpos_it, atpos_data);
9047 else if (atx_it.sp >= 0)
9048 RESTORE_IT (it, &atx_it, atx_data);
9049
9050 done:
9051
9052 if (atpos_data)
9053 bidi_unshelve_cache (atpos_data, true);
9054 if (atx_data)
9055 bidi_unshelve_cache (atx_data, true);
9056 if (wrap_data)
9057 bidi_unshelve_cache (wrap_data, true);
9058 if (ppos_data)
9059 bidi_unshelve_cache (ppos_data, true);
9060
9061 /* Restore the iterator settings altered at the beginning of this
9062 function. */
9063 it->glyph_row = saved_glyph_row;
9064 return result;
9065 }
9066
9067 /* For external use. */
9068 void
9069 move_it_in_display_line (struct it *it,
9070 ptrdiff_t to_charpos, int to_x,
9071 enum move_operation_enum op)
9072 {
9073 if (it->line_wrap == WORD_WRAP
9074 && (op & MOVE_TO_X))
9075 {
9076 struct it save_it;
9077 void *save_data = NULL;
9078 int skip;
9079
9080 SAVE_IT (save_it, *it, save_data);
9081 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9082 /* When word-wrap is on, TO_X may lie past the end
9083 of a wrapped line. Then it->current is the
9084 character on the next line, so backtrack to the
9085 space before the wrap point. */
9086 if (skip == MOVE_LINE_CONTINUED)
9087 {
9088 int prev_x = max (it->current_x - 1, 0);
9089 RESTORE_IT (it, &save_it, save_data);
9090 move_it_in_display_line_to
9091 (it, -1, prev_x, MOVE_TO_X);
9092 }
9093 else
9094 bidi_unshelve_cache (save_data, true);
9095 }
9096 else
9097 move_it_in_display_line_to (it, to_charpos, to_x, op);
9098 }
9099
9100
9101 /* Move IT forward until it satisfies one or more of the criteria in
9102 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9103
9104 OP is a bit-mask that specifies where to stop, and in particular,
9105 which of those four position arguments makes a difference. See the
9106 description of enum move_operation_enum.
9107
9108 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9109 screen line, this function will set IT to the next position that is
9110 displayed to the right of TO_CHARPOS on the screen.
9111
9112 Return the maximum pixel length of any line scanned but never more
9113 than it.last_visible_x. */
9114
9115 int
9116 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9117 {
9118 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9119 int line_height, line_start_x = 0, reached = 0;
9120 int max_current_x = 0;
9121 void *backup_data = NULL;
9122
9123 for (;;)
9124 {
9125 if (op & MOVE_TO_VPOS)
9126 {
9127 /* If no TO_CHARPOS and no TO_X specified, stop at the
9128 start of the line TO_VPOS. */
9129 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9130 {
9131 if (it->vpos == to_vpos)
9132 {
9133 reached = 1;
9134 break;
9135 }
9136 else
9137 skip = move_it_in_display_line_to (it, -1, -1, 0);
9138 }
9139 else
9140 {
9141 /* TO_VPOS >= 0 means stop at TO_X in the line at
9142 TO_VPOS, or at TO_POS, whichever comes first. */
9143 if (it->vpos == to_vpos)
9144 {
9145 reached = 2;
9146 break;
9147 }
9148
9149 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9150
9151 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9152 {
9153 reached = 3;
9154 break;
9155 }
9156 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9157 {
9158 /* We have reached TO_X but not in the line we want. */
9159 skip = move_it_in_display_line_to (it, to_charpos,
9160 -1, MOVE_TO_POS);
9161 if (skip == MOVE_POS_MATCH_OR_ZV)
9162 {
9163 reached = 4;
9164 break;
9165 }
9166 }
9167 }
9168 }
9169 else if (op & MOVE_TO_Y)
9170 {
9171 struct it it_backup;
9172
9173 if (it->line_wrap == WORD_WRAP)
9174 SAVE_IT (it_backup, *it, backup_data);
9175
9176 /* TO_Y specified means stop at TO_X in the line containing
9177 TO_Y---or at TO_CHARPOS if this is reached first. The
9178 problem is that we can't really tell whether the line
9179 contains TO_Y before we have completely scanned it, and
9180 this may skip past TO_X. What we do is to first scan to
9181 TO_X.
9182
9183 If TO_X is not specified, use a TO_X of zero. The reason
9184 is to make the outcome of this function more predictable.
9185 If we didn't use TO_X == 0, we would stop at the end of
9186 the line which is probably not what a caller would expect
9187 to happen. */
9188 skip = move_it_in_display_line_to
9189 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9190 (MOVE_TO_X | (op & MOVE_TO_POS)));
9191
9192 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9193 if (skip == MOVE_POS_MATCH_OR_ZV)
9194 reached = 5;
9195 else if (skip == MOVE_X_REACHED)
9196 {
9197 /* If TO_X was reached, we want to know whether TO_Y is
9198 in the line. We know this is the case if the already
9199 scanned glyphs make the line tall enough. Otherwise,
9200 we must check by scanning the rest of the line. */
9201 line_height = it->max_ascent + it->max_descent;
9202 if (to_y >= it->current_y
9203 && to_y < it->current_y + line_height)
9204 {
9205 reached = 6;
9206 break;
9207 }
9208 SAVE_IT (it_backup, *it, backup_data);
9209 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9210 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9211 op & MOVE_TO_POS);
9212 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9213 line_height = it->max_ascent + it->max_descent;
9214 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9215
9216 if (to_y >= it->current_y
9217 && to_y < it->current_y + line_height)
9218 {
9219 /* If TO_Y is in this line and TO_X was reached
9220 above, we scanned too far. We have to restore
9221 IT's settings to the ones before skipping. But
9222 keep the more accurate values of max_ascent and
9223 max_descent we've found while skipping the rest
9224 of the line, for the sake of callers, such as
9225 pos_visible_p, that need to know the line
9226 height. */
9227 int max_ascent = it->max_ascent;
9228 int max_descent = it->max_descent;
9229
9230 RESTORE_IT (it, &it_backup, backup_data);
9231 it->max_ascent = max_ascent;
9232 it->max_descent = max_descent;
9233 reached = 6;
9234 }
9235 else
9236 {
9237 skip = skip2;
9238 if (skip == MOVE_POS_MATCH_OR_ZV)
9239 reached = 7;
9240 }
9241 }
9242 else
9243 {
9244 /* Check whether TO_Y is in this line. */
9245 line_height = it->max_ascent + it->max_descent;
9246 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9247
9248 if (to_y >= it->current_y
9249 && to_y < it->current_y + line_height)
9250 {
9251 if (to_y > it->current_y)
9252 max_current_x = max (it->current_x, max_current_x);
9253
9254 /* When word-wrap is on, TO_X may lie past the end
9255 of a wrapped line. Then it->current is the
9256 character on the next line, so backtrack to the
9257 space before the wrap point. */
9258 if (skip == MOVE_LINE_CONTINUED
9259 && it->line_wrap == WORD_WRAP)
9260 {
9261 int prev_x = max (it->current_x - 1, 0);
9262 RESTORE_IT (it, &it_backup, backup_data);
9263 skip = move_it_in_display_line_to
9264 (it, -1, prev_x, MOVE_TO_X);
9265 }
9266
9267 reached = 6;
9268 }
9269 }
9270
9271 if (reached)
9272 {
9273 max_current_x = max (it->current_x, max_current_x);
9274 break;
9275 }
9276 }
9277 else if (BUFFERP (it->object)
9278 && (it->method == GET_FROM_BUFFER
9279 || it->method == GET_FROM_STRETCH)
9280 && IT_CHARPOS (*it) >= to_charpos
9281 /* Under bidi iteration, a call to set_iterator_to_next
9282 can scan far beyond to_charpos if the initial
9283 portion of the next line needs to be reordered. In
9284 that case, give move_it_in_display_line_to another
9285 chance below. */
9286 && !(it->bidi_p
9287 && it->bidi_it.scan_dir == -1))
9288 skip = MOVE_POS_MATCH_OR_ZV;
9289 else
9290 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9291
9292 switch (skip)
9293 {
9294 case MOVE_POS_MATCH_OR_ZV:
9295 max_current_x = max (it->current_x, max_current_x);
9296 reached = 8;
9297 goto out;
9298
9299 case MOVE_NEWLINE_OR_CR:
9300 max_current_x = max (it->current_x, max_current_x);
9301 set_iterator_to_next (it, true);
9302 it->continuation_lines_width = 0;
9303 break;
9304
9305 case MOVE_LINE_TRUNCATED:
9306 max_current_x = it->last_visible_x;
9307 it->continuation_lines_width = 0;
9308 reseat_at_next_visible_line_start (it, false);
9309 if ((op & MOVE_TO_POS) != 0
9310 && IT_CHARPOS (*it) > to_charpos)
9311 {
9312 reached = 9;
9313 goto out;
9314 }
9315 break;
9316
9317 case MOVE_LINE_CONTINUED:
9318 max_current_x = it->last_visible_x;
9319 /* For continued lines ending in a tab, some of the glyphs
9320 associated with the tab are displayed on the current
9321 line. Since it->current_x does not include these glyphs,
9322 we use it->last_visible_x instead. */
9323 if (it->c == '\t')
9324 {
9325 it->continuation_lines_width += it->last_visible_x;
9326 /* When moving by vpos, ensure that the iterator really
9327 advances to the next line (bug#847, bug#969). Fixme:
9328 do we need to do this in other circumstances? */
9329 if (it->current_x != it->last_visible_x
9330 && (op & MOVE_TO_VPOS)
9331 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9332 {
9333 line_start_x = it->current_x + it->pixel_width
9334 - it->last_visible_x;
9335 if (FRAME_WINDOW_P (it->f))
9336 {
9337 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9338 struct font *face_font = face->font;
9339
9340 /* When display_line produces a continued line
9341 that ends in a TAB, it skips a tab stop that
9342 is closer than the font's space character
9343 width (see x_produce_glyphs where it produces
9344 the stretch glyph which represents a TAB).
9345 We need to reproduce the same logic here. */
9346 eassert (face_font);
9347 if (face_font)
9348 {
9349 if (line_start_x < face_font->space_width)
9350 line_start_x
9351 += it->tab_width * face_font->space_width;
9352 }
9353 }
9354 set_iterator_to_next (it, false);
9355 }
9356 }
9357 else
9358 it->continuation_lines_width += it->current_x;
9359 break;
9360
9361 default:
9362 emacs_abort ();
9363 }
9364
9365 /* Reset/increment for the next run. */
9366 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9367 it->current_x = line_start_x;
9368 line_start_x = 0;
9369 it->hpos = 0;
9370 it->current_y += it->max_ascent + it->max_descent;
9371 ++it->vpos;
9372 last_height = it->max_ascent + it->max_descent;
9373 it->max_ascent = it->max_descent = 0;
9374 }
9375
9376 out:
9377
9378 /* On text terminals, we may stop at the end of a line in the middle
9379 of a multi-character glyph. If the glyph itself is continued,
9380 i.e. it is actually displayed on the next line, don't treat this
9381 stopping point as valid; move to the next line instead (unless
9382 that brings us offscreen). */
9383 if (!FRAME_WINDOW_P (it->f)
9384 && op & MOVE_TO_POS
9385 && IT_CHARPOS (*it) == to_charpos
9386 && it->what == IT_CHARACTER
9387 && it->nglyphs > 1
9388 && it->line_wrap == WINDOW_WRAP
9389 && it->current_x == it->last_visible_x - 1
9390 && it->c != '\n'
9391 && it->c != '\t'
9392 && it->w->window_end_valid
9393 && it->vpos < it->w->window_end_vpos)
9394 {
9395 it->continuation_lines_width += it->current_x;
9396 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9397 it->current_y += it->max_ascent + it->max_descent;
9398 ++it->vpos;
9399 last_height = it->max_ascent + it->max_descent;
9400 }
9401
9402 if (backup_data)
9403 bidi_unshelve_cache (backup_data, true);
9404
9405 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9406
9407 return max_current_x;
9408 }
9409
9410
9411 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9412
9413 If DY > 0, move IT backward at least that many pixels. DY = 0
9414 means move IT backward to the preceding line start or BEGV. This
9415 function may move over more than DY pixels if IT->current_y - DY
9416 ends up in the middle of a line; in this case IT->current_y will be
9417 set to the top of the line moved to. */
9418
9419 void
9420 move_it_vertically_backward (struct it *it, int dy)
9421 {
9422 int nlines, h;
9423 struct it it2, it3;
9424 void *it2data = NULL, *it3data = NULL;
9425 ptrdiff_t start_pos;
9426 int nchars_per_row
9427 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9428 ptrdiff_t pos_limit;
9429
9430 move_further_back:
9431 eassert (dy >= 0);
9432
9433 start_pos = IT_CHARPOS (*it);
9434
9435 /* Estimate how many newlines we must move back. */
9436 nlines = max (1, dy / default_line_pixel_height (it->w));
9437 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9438 pos_limit = BEGV;
9439 else
9440 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9441
9442 /* Set the iterator's position that many lines back. But don't go
9443 back more than NLINES full screen lines -- this wins a day with
9444 buffers which have very long lines. */
9445 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9446 back_to_previous_visible_line_start (it);
9447
9448 /* Reseat the iterator here. When moving backward, we don't want
9449 reseat to skip forward over invisible text, set up the iterator
9450 to deliver from overlay strings at the new position etc. So,
9451 use reseat_1 here. */
9452 reseat_1 (it, it->current.pos, true);
9453
9454 /* We are now surely at a line start. */
9455 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9456 reordering is in effect. */
9457 it->continuation_lines_width = 0;
9458
9459 /* Move forward and see what y-distance we moved. First move to the
9460 start of the next line so that we get its height. We need this
9461 height to be able to tell whether we reached the specified
9462 y-distance. */
9463 SAVE_IT (it2, *it, it2data);
9464 it2.max_ascent = it2.max_descent = 0;
9465 do
9466 {
9467 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9468 MOVE_TO_POS | MOVE_TO_VPOS);
9469 }
9470 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9471 /* If we are in a display string which starts at START_POS,
9472 and that display string includes a newline, and we are
9473 right after that newline (i.e. at the beginning of a
9474 display line), exit the loop, because otherwise we will
9475 infloop, since move_it_to will see that it is already at
9476 START_POS and will not move. */
9477 || (it2.method == GET_FROM_STRING
9478 && IT_CHARPOS (it2) == start_pos
9479 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9480 eassert (IT_CHARPOS (*it) >= BEGV);
9481 SAVE_IT (it3, it2, it3data);
9482
9483 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9484 eassert (IT_CHARPOS (*it) >= BEGV);
9485 /* H is the actual vertical distance from the position in *IT
9486 and the starting position. */
9487 h = it2.current_y - it->current_y;
9488 /* NLINES is the distance in number of lines. */
9489 nlines = it2.vpos - it->vpos;
9490
9491 /* Correct IT's y and vpos position
9492 so that they are relative to the starting point. */
9493 it->vpos -= nlines;
9494 it->current_y -= h;
9495
9496 if (dy == 0)
9497 {
9498 /* DY == 0 means move to the start of the screen line. The
9499 value of nlines is > 0 if continuation lines were involved,
9500 or if the original IT position was at start of a line. */
9501 RESTORE_IT (it, it, it2data);
9502 if (nlines > 0)
9503 move_it_by_lines (it, nlines);
9504 /* The above code moves us to some position NLINES down,
9505 usually to its first glyph (leftmost in an L2R line), but
9506 that's not necessarily the start of the line, under bidi
9507 reordering. We want to get to the character position
9508 that is immediately after the newline of the previous
9509 line. */
9510 if (it->bidi_p
9511 && !it->continuation_lines_width
9512 && !STRINGP (it->string)
9513 && IT_CHARPOS (*it) > BEGV
9514 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9515 {
9516 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9517
9518 DEC_BOTH (cp, bp);
9519 cp = find_newline_no_quit (cp, bp, -1, NULL);
9520 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9521 }
9522 bidi_unshelve_cache (it3data, true);
9523 }
9524 else
9525 {
9526 /* The y-position we try to reach, relative to *IT.
9527 Note that H has been subtracted in front of the if-statement. */
9528 int target_y = it->current_y + h - dy;
9529 int y0 = it3.current_y;
9530 int y1;
9531 int line_height;
9532
9533 RESTORE_IT (&it3, &it3, it3data);
9534 y1 = line_bottom_y (&it3);
9535 line_height = y1 - y0;
9536 RESTORE_IT (it, it, it2data);
9537 /* If we did not reach target_y, try to move further backward if
9538 we can. If we moved too far backward, try to move forward. */
9539 if (target_y < it->current_y
9540 /* This is heuristic. In a window that's 3 lines high, with
9541 a line height of 13 pixels each, recentering with point
9542 on the bottom line will try to move -39/2 = 19 pixels
9543 backward. Try to avoid moving into the first line. */
9544 && (it->current_y - target_y
9545 > min (window_box_height (it->w), line_height * 2 / 3))
9546 && IT_CHARPOS (*it) > BEGV)
9547 {
9548 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9549 target_y - it->current_y));
9550 dy = it->current_y - target_y;
9551 goto move_further_back;
9552 }
9553 else if (target_y >= it->current_y + line_height
9554 && IT_CHARPOS (*it) < ZV)
9555 {
9556 /* Should move forward by at least one line, maybe more.
9557
9558 Note: Calling move_it_by_lines can be expensive on
9559 terminal frames, where compute_motion is used (via
9560 vmotion) to do the job, when there are very long lines
9561 and truncate-lines is nil. That's the reason for
9562 treating terminal frames specially here. */
9563
9564 if (!FRAME_WINDOW_P (it->f))
9565 move_it_vertically (it, target_y - it->current_y);
9566 else
9567 {
9568 do
9569 {
9570 move_it_by_lines (it, 1);
9571 }
9572 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9573 }
9574 }
9575 }
9576 }
9577
9578
9579 /* Move IT by a specified amount of pixel lines DY. DY negative means
9580 move backwards. DY = 0 means move to start of screen line. At the
9581 end, IT will be on the start of a screen line. */
9582
9583 void
9584 move_it_vertically (struct it *it, int dy)
9585 {
9586 if (dy <= 0)
9587 move_it_vertically_backward (it, -dy);
9588 else
9589 {
9590 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9591 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9592 MOVE_TO_POS | MOVE_TO_Y);
9593 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9594
9595 /* If buffer ends in ZV without a newline, move to the start of
9596 the line to satisfy the post-condition. */
9597 if (IT_CHARPOS (*it) == ZV
9598 && ZV > BEGV
9599 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9600 move_it_by_lines (it, 0);
9601 }
9602 }
9603
9604
9605 /* Move iterator IT past the end of the text line it is in. */
9606
9607 void
9608 move_it_past_eol (struct it *it)
9609 {
9610 enum move_it_result rc;
9611
9612 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9613 if (rc == MOVE_NEWLINE_OR_CR)
9614 set_iterator_to_next (it, false);
9615 }
9616
9617
9618 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9619 negative means move up. DVPOS == 0 means move to the start of the
9620 screen line.
9621
9622 Optimization idea: If we would know that IT->f doesn't use
9623 a face with proportional font, we could be faster for
9624 truncate-lines nil. */
9625
9626 void
9627 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9628 {
9629
9630 /* The commented-out optimization uses vmotion on terminals. This
9631 gives bad results, because elements like it->what, on which
9632 callers such as pos_visible_p rely, aren't updated. */
9633 /* struct position pos;
9634 if (!FRAME_WINDOW_P (it->f))
9635 {
9636 struct text_pos textpos;
9637
9638 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9639 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9640 reseat (it, textpos, true);
9641 it->vpos += pos.vpos;
9642 it->current_y += pos.vpos;
9643 }
9644 else */
9645
9646 if (dvpos == 0)
9647 {
9648 /* DVPOS == 0 means move to the start of the screen line. */
9649 move_it_vertically_backward (it, 0);
9650 /* Let next call to line_bottom_y calculate real line height. */
9651 last_height = 0;
9652 }
9653 else if (dvpos > 0)
9654 {
9655 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9656 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9657 {
9658 /* Only move to the next buffer position if we ended up in a
9659 string from display property, not in an overlay string
9660 (before-string or after-string). That is because the
9661 latter don't conceal the underlying buffer position, so
9662 we can ask to move the iterator to the exact position we
9663 are interested in. Note that, even if we are already at
9664 IT_CHARPOS (*it), the call below is not a no-op, as it
9665 will detect that we are at the end of the string, pop the
9666 iterator, and compute it->current_x and it->hpos
9667 correctly. */
9668 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9669 -1, -1, -1, MOVE_TO_POS);
9670 }
9671 }
9672 else
9673 {
9674 struct it it2;
9675 void *it2data = NULL;
9676 ptrdiff_t start_charpos, i;
9677 int nchars_per_row
9678 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9679 bool hit_pos_limit = false;
9680 ptrdiff_t pos_limit;
9681
9682 /* Start at the beginning of the screen line containing IT's
9683 position. This may actually move vertically backwards,
9684 in case of overlays, so adjust dvpos accordingly. */
9685 dvpos += it->vpos;
9686 move_it_vertically_backward (it, 0);
9687 dvpos -= it->vpos;
9688
9689 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9690 screen lines, and reseat the iterator there. */
9691 start_charpos = IT_CHARPOS (*it);
9692 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9693 pos_limit = BEGV;
9694 else
9695 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9696
9697 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9698 back_to_previous_visible_line_start (it);
9699 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9700 hit_pos_limit = true;
9701 reseat (it, it->current.pos, true);
9702
9703 /* Move further back if we end up in a string or an image. */
9704 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9705 {
9706 /* First try to move to start of display line. */
9707 dvpos += it->vpos;
9708 move_it_vertically_backward (it, 0);
9709 dvpos -= it->vpos;
9710 if (IT_POS_VALID_AFTER_MOVE_P (it))
9711 break;
9712 /* If start of line is still in string or image,
9713 move further back. */
9714 back_to_previous_visible_line_start (it);
9715 reseat (it, it->current.pos, true);
9716 dvpos--;
9717 }
9718
9719 it->current_x = it->hpos = 0;
9720
9721 /* Above call may have moved too far if continuation lines
9722 are involved. Scan forward and see if it did. */
9723 SAVE_IT (it2, *it, it2data);
9724 it2.vpos = it2.current_y = 0;
9725 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9726 it->vpos -= it2.vpos;
9727 it->current_y -= it2.current_y;
9728 it->current_x = it->hpos = 0;
9729
9730 /* If we moved too far back, move IT some lines forward. */
9731 if (it2.vpos > -dvpos)
9732 {
9733 int delta = it2.vpos + dvpos;
9734
9735 RESTORE_IT (&it2, &it2, it2data);
9736 SAVE_IT (it2, *it, it2data);
9737 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9738 /* Move back again if we got too far ahead. */
9739 if (IT_CHARPOS (*it) >= start_charpos)
9740 RESTORE_IT (it, &it2, it2data);
9741 else
9742 bidi_unshelve_cache (it2data, true);
9743 }
9744 else if (hit_pos_limit && pos_limit > BEGV
9745 && dvpos < 0 && it2.vpos < -dvpos)
9746 {
9747 /* If we hit the limit, but still didn't make it far enough
9748 back, that means there's a display string with a newline
9749 covering a large chunk of text, and that caused
9750 back_to_previous_visible_line_start try to go too far.
9751 Punish those who commit such atrocities by going back
9752 until we've reached DVPOS, after lifting the limit, which
9753 could make it slow for very long lines. "If it hurts,
9754 don't do that!" */
9755 dvpos += it2.vpos;
9756 RESTORE_IT (it, it, it2data);
9757 for (i = -dvpos; i > 0; --i)
9758 {
9759 back_to_previous_visible_line_start (it);
9760 it->vpos--;
9761 }
9762 reseat_1 (it, it->current.pos, true);
9763 }
9764 else
9765 RESTORE_IT (it, it, it2data);
9766 }
9767 }
9768
9769 /* Return true if IT points into the middle of a display vector. */
9770
9771 bool
9772 in_display_vector_p (struct it *it)
9773 {
9774 return (it->method == GET_FROM_DISPLAY_VECTOR
9775 && it->current.dpvec_index > 0
9776 && it->dpvec + it->current.dpvec_index != it->dpend);
9777 }
9778
9779 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9780 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9781 WINDOW must be a live window and defaults to the selected one. The
9782 return value is a cons of the maximum pixel-width of any text line and
9783 the maximum pixel-height of all text lines.
9784
9785 The optional argument FROM, if non-nil, specifies the first text
9786 position and defaults to the minimum accessible position of the buffer.
9787 If FROM is t, use the minimum accessible position that is not a newline
9788 character. TO, if non-nil, specifies the last text position and
9789 defaults to the maximum accessible position of the buffer. If TO is t,
9790 use the maximum accessible position that is not a newline character.
9791
9792 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9793 width that can be returned. X-LIMIT nil or omitted, means to use the
9794 pixel-width of WINDOW's body; use this if you do not intend to change
9795 the width of WINDOW. Use the maximum width WINDOW may assume if you
9796 intend to change WINDOW's width. In any case, text whose x-coordinate
9797 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9798 can take some time, it's always a good idea to make this argument as
9799 small as possible; in particular, if the buffer contains long lines that
9800 shall be truncated anyway.
9801
9802 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9803 height that can be returned. Text lines whose y-coordinate is beyond
9804 Y-LIMIT are ignored. Since calculating the text height of a large
9805 buffer can take some time, it makes sense to specify this argument if
9806 the size of the buffer is unknown.
9807
9808 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9809 include the height of the mode- or header-line of WINDOW in the return
9810 value. If it is either the symbol `mode-line' or `header-line', include
9811 only the height of that line, if present, in the return value. If t,
9812 include the height of both, if present, in the return value. */)
9813 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9814 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9815 {
9816 struct window *w = decode_live_window (window);
9817 Lisp_Object buffer = w->contents;
9818 struct buffer *b;
9819 struct it it;
9820 struct buffer *old_b = NULL;
9821 ptrdiff_t start, end, pos;
9822 struct text_pos startp;
9823 void *itdata = NULL;
9824 int c, max_y = -1, x = 0, y = 0;
9825
9826 CHECK_BUFFER (buffer);
9827 b = XBUFFER (buffer);
9828
9829 if (b != current_buffer)
9830 {
9831 old_b = current_buffer;
9832 set_buffer_internal (b);
9833 }
9834
9835 if (NILP (from))
9836 start = BEGV;
9837 else if (EQ (from, Qt))
9838 {
9839 start = pos = BEGV;
9840 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9841 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9842 start = pos;
9843 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9844 start = pos;
9845 }
9846 else
9847 {
9848 CHECK_NUMBER_COERCE_MARKER (from);
9849 start = min (max (XINT (from), BEGV), ZV);
9850 }
9851
9852 if (NILP (to))
9853 end = ZV;
9854 else if (EQ (to, Qt))
9855 {
9856 end = pos = ZV;
9857 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9858 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9859 end = pos;
9860 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9861 end = pos;
9862 }
9863 else
9864 {
9865 CHECK_NUMBER_COERCE_MARKER (to);
9866 end = max (start, min (XINT (to), ZV));
9867 }
9868
9869 if (!NILP (y_limit))
9870 {
9871 CHECK_NUMBER (y_limit);
9872 max_y = min (XINT (y_limit), INT_MAX);
9873 }
9874
9875 itdata = bidi_shelve_cache ();
9876 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9877 start_display (&it, w, startp);
9878
9879 if (NILP (x_limit))
9880 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9881 else
9882 {
9883 CHECK_NUMBER (x_limit);
9884 it.last_visible_x = min (XINT (x_limit), INFINITY);
9885 /* Actually, we never want move_it_to stop at to_x. But to make
9886 sure that move_it_in_display_line_to always moves far enough,
9887 we set it to INT_MAX and specify MOVE_TO_X. */
9888 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9889 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9890 }
9891
9892 y = it.current_y + it.max_ascent + it.max_descent;
9893
9894 if (!EQ (mode_and_header_line, Qheader_line)
9895 && !EQ (mode_and_header_line, Qt))
9896 /* Do not count the header-line which was counted automatically by
9897 start_display. */
9898 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9899
9900 if (EQ (mode_and_header_line, Qmode_line)
9901 || EQ (mode_and_header_line, Qt))
9902 /* Do count the mode-line which is not included automatically by
9903 start_display. */
9904 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9905
9906 bidi_unshelve_cache (itdata, false);
9907
9908 if (old_b)
9909 set_buffer_internal (old_b);
9910
9911 return Fcons (make_number (x), make_number (y));
9912 }
9913 \f
9914 /***********************************************************************
9915 Messages
9916 ***********************************************************************/
9917
9918 /* Return the number of arguments the format string FORMAT needs. */
9919
9920 static ptrdiff_t
9921 format_nargs (char const *format)
9922 {
9923 ptrdiff_t nargs = 0;
9924 for (char const *p = format; (p = strchr (p, '%')); p++)
9925 if (p[1] == '%')
9926 p++;
9927 else
9928 nargs++;
9929 return nargs;
9930 }
9931
9932 /* Add a message with format string FORMAT and formatted arguments
9933 to *Messages*. */
9934
9935 void
9936 add_to_log (const char *format, ...)
9937 {
9938 va_list ap;
9939 va_start (ap, format);
9940 vadd_to_log (format, ap);
9941 va_end (ap);
9942 }
9943
9944 void
9945 vadd_to_log (char const *format, va_list ap)
9946 {
9947 ptrdiff_t form_nargs = format_nargs (format);
9948 ptrdiff_t nargs = 1 + form_nargs;
9949 Lisp_Object args[10];
9950 eassert (nargs <= ARRAYELTS (args));
9951 AUTO_STRING (args0, format);
9952 args[0] = args0;
9953 for (ptrdiff_t i = 1; i <= nargs; i++)
9954 args[i] = va_arg (ap, Lisp_Object);
9955 Lisp_Object msg = Qnil;
9956 msg = Fformat_message (nargs, args);
9957
9958 ptrdiff_t len = SBYTES (msg) + 1;
9959 USE_SAFE_ALLOCA;
9960 char *buffer = SAFE_ALLOCA (len);
9961 memcpy (buffer, SDATA (msg), len);
9962
9963 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
9964 SAFE_FREE ();
9965 }
9966
9967
9968 /* Output a newline in the *Messages* buffer if "needs" one. */
9969
9970 void
9971 message_log_maybe_newline (void)
9972 {
9973 if (message_log_need_newline)
9974 message_dolog ("", 0, true, false);
9975 }
9976
9977
9978 /* Add a string M of length NBYTES to the message log, optionally
9979 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9980 true, means interpret the contents of M as multibyte. This
9981 function calls low-level routines in order to bypass text property
9982 hooks, etc. which might not be safe to run.
9983
9984 This may GC (insert may run before/after change hooks),
9985 so the buffer M must NOT point to a Lisp string. */
9986
9987 void
9988 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9989 {
9990 const unsigned char *msg = (const unsigned char *) m;
9991
9992 if (!NILP (Vmemory_full))
9993 return;
9994
9995 if (!NILP (Vmessage_log_max))
9996 {
9997 struct buffer *oldbuf;
9998 Lisp_Object oldpoint, oldbegv, oldzv;
9999 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10000 ptrdiff_t point_at_end = 0;
10001 ptrdiff_t zv_at_end = 0;
10002 Lisp_Object old_deactivate_mark;
10003
10004 old_deactivate_mark = Vdeactivate_mark;
10005 oldbuf = current_buffer;
10006
10007 /* Ensure the Messages buffer exists, and switch to it.
10008 If we created it, set the major-mode. */
10009 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10010 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10011 if (newbuffer
10012 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10013 call0 (intern ("messages-buffer-mode"));
10014
10015 bset_undo_list (current_buffer, Qt);
10016 bset_cache_long_scans (current_buffer, Qnil);
10017
10018 oldpoint = message_dolog_marker1;
10019 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10020 oldbegv = message_dolog_marker2;
10021 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10022 oldzv = message_dolog_marker3;
10023 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10024
10025 if (PT == Z)
10026 point_at_end = 1;
10027 if (ZV == Z)
10028 zv_at_end = 1;
10029
10030 BEGV = BEG;
10031 BEGV_BYTE = BEG_BYTE;
10032 ZV = Z;
10033 ZV_BYTE = Z_BYTE;
10034 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10035
10036 /* Insert the string--maybe converting multibyte to single byte
10037 or vice versa, so that all the text fits the buffer. */
10038 if (multibyte
10039 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10040 {
10041 ptrdiff_t i;
10042 int c, char_bytes;
10043 char work[1];
10044
10045 /* Convert a multibyte string to single-byte
10046 for the *Message* buffer. */
10047 for (i = 0; i < nbytes; i += char_bytes)
10048 {
10049 c = string_char_and_length (msg + i, &char_bytes);
10050 work[0] = CHAR_TO_BYTE8 (c);
10051 insert_1_both (work, 1, 1, true, false, false);
10052 }
10053 }
10054 else if (! multibyte
10055 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10056 {
10057 ptrdiff_t i;
10058 int c, char_bytes;
10059 unsigned char str[MAX_MULTIBYTE_LENGTH];
10060 /* Convert a single-byte string to multibyte
10061 for the *Message* buffer. */
10062 for (i = 0; i < nbytes; i++)
10063 {
10064 c = msg[i];
10065 MAKE_CHAR_MULTIBYTE (c);
10066 char_bytes = CHAR_STRING (c, str);
10067 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10068 }
10069 }
10070 else if (nbytes)
10071 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10072 true, false, false);
10073
10074 if (nlflag)
10075 {
10076 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10077 printmax_t dups;
10078
10079 insert_1_both ("\n", 1, 1, true, false, false);
10080
10081 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10082 this_bol = PT;
10083 this_bol_byte = PT_BYTE;
10084
10085 /* See if this line duplicates the previous one.
10086 If so, combine duplicates. */
10087 if (this_bol > BEG)
10088 {
10089 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10090 prev_bol = PT;
10091 prev_bol_byte = PT_BYTE;
10092
10093 dups = message_log_check_duplicate (prev_bol_byte,
10094 this_bol_byte);
10095 if (dups)
10096 {
10097 del_range_both (prev_bol, prev_bol_byte,
10098 this_bol, this_bol_byte, false);
10099 if (dups > 1)
10100 {
10101 char dupstr[sizeof " [ times]"
10102 + INT_STRLEN_BOUND (printmax_t)];
10103
10104 /* If you change this format, don't forget to also
10105 change message_log_check_duplicate. */
10106 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10107 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10108 insert_1_both (dupstr, duplen, duplen,
10109 true, false, true);
10110 }
10111 }
10112 }
10113
10114 /* If we have more than the desired maximum number of lines
10115 in the *Messages* buffer now, delete the oldest ones.
10116 This is safe because we don't have undo in this buffer. */
10117
10118 if (NATNUMP (Vmessage_log_max))
10119 {
10120 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10121 -XFASTINT (Vmessage_log_max) - 1, false);
10122 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10123 }
10124 }
10125 BEGV = marker_position (oldbegv);
10126 BEGV_BYTE = marker_byte_position (oldbegv);
10127
10128 if (zv_at_end)
10129 {
10130 ZV = Z;
10131 ZV_BYTE = Z_BYTE;
10132 }
10133 else
10134 {
10135 ZV = marker_position (oldzv);
10136 ZV_BYTE = marker_byte_position (oldzv);
10137 }
10138
10139 if (point_at_end)
10140 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10141 else
10142 /* We can't do Fgoto_char (oldpoint) because it will run some
10143 Lisp code. */
10144 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10145 marker_byte_position (oldpoint));
10146
10147 unchain_marker (XMARKER (oldpoint));
10148 unchain_marker (XMARKER (oldbegv));
10149 unchain_marker (XMARKER (oldzv));
10150
10151 /* We called insert_1_both above with its 5th argument (PREPARE)
10152 false, which prevents insert_1_both from calling
10153 prepare_to_modify_buffer, which in turns prevents us from
10154 incrementing windows_or_buffers_changed even if *Messages* is
10155 shown in some window. So we must manually set
10156 windows_or_buffers_changed here to make up for that. */
10157 windows_or_buffers_changed = old_windows_or_buffers_changed;
10158 bset_redisplay (current_buffer);
10159
10160 set_buffer_internal (oldbuf);
10161
10162 message_log_need_newline = !nlflag;
10163 Vdeactivate_mark = old_deactivate_mark;
10164 }
10165 }
10166
10167
10168 /* We are at the end of the buffer after just having inserted a newline.
10169 (Note: We depend on the fact we won't be crossing the gap.)
10170 Check to see if the most recent message looks a lot like the previous one.
10171 Return 0 if different, 1 if the new one should just replace it, or a
10172 value N > 1 if we should also append " [N times]". */
10173
10174 static intmax_t
10175 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10176 {
10177 ptrdiff_t i;
10178 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10179 bool seen_dots = false;
10180 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10181 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10182
10183 for (i = 0; i < len; i++)
10184 {
10185 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10186 seen_dots = true;
10187 if (p1[i] != p2[i])
10188 return seen_dots;
10189 }
10190 p1 += len;
10191 if (*p1 == '\n')
10192 return 2;
10193 if (*p1++ == ' ' && *p1++ == '[')
10194 {
10195 char *pend;
10196 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10197 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10198 return n + 1;
10199 }
10200 return 0;
10201 }
10202 \f
10203
10204 /* Display an echo area message M with a specified length of NBYTES
10205 bytes. The string may include null characters. If M is not a
10206 string, clear out any existing message, and let the mini-buffer
10207 text show through.
10208
10209 This function cancels echoing. */
10210
10211 void
10212 message3 (Lisp_Object m)
10213 {
10214 clear_message (true, true);
10215 cancel_echoing ();
10216
10217 /* First flush out any partial line written with print. */
10218 message_log_maybe_newline ();
10219 if (STRINGP (m))
10220 {
10221 ptrdiff_t nbytes = SBYTES (m);
10222 bool multibyte = STRING_MULTIBYTE (m);
10223 char *buffer;
10224 USE_SAFE_ALLOCA;
10225 SAFE_ALLOCA_STRING (buffer, m);
10226 message_dolog (buffer, nbytes, true, multibyte);
10227 SAFE_FREE ();
10228 }
10229 if (! inhibit_message)
10230 message3_nolog (m);
10231 }
10232
10233 /* Log the message M to stderr. Log an empty line if M is not a string. */
10234
10235 static void
10236 message_to_stderr (Lisp_Object m)
10237 {
10238 if (noninteractive_need_newline)
10239 {
10240 noninteractive_need_newline = false;
10241 fputc ('\n', stderr);
10242 }
10243 if (STRINGP (m))
10244 {
10245 Lisp_Object coding_system = Vlocale_coding_system;
10246 Lisp_Object s;
10247
10248 if (!NILP (Vcoding_system_for_write))
10249 coding_system = Vcoding_system_for_write;
10250 if (!NILP (coding_system))
10251 s = code_convert_string_norecord (m, coding_system, true);
10252 else
10253 s = m;
10254
10255 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10256 }
10257 if (!cursor_in_echo_area)
10258 fputc ('\n', stderr);
10259 fflush (stderr);
10260 }
10261
10262 /* The non-logging version of message3.
10263 This does not cancel echoing, because it is used for echoing.
10264 Perhaps we need to make a separate function for echoing
10265 and make this cancel echoing. */
10266
10267 void
10268 message3_nolog (Lisp_Object m)
10269 {
10270 struct frame *sf = SELECTED_FRAME ();
10271
10272 if (FRAME_INITIAL_P (sf))
10273 message_to_stderr (m);
10274 /* Error messages get reported properly by cmd_error, so this must be just an
10275 informative message; if the frame hasn't really been initialized yet, just
10276 toss it. */
10277 else if (INTERACTIVE && sf->glyphs_initialized_p)
10278 {
10279 /* Get the frame containing the mini-buffer
10280 that the selected frame is using. */
10281 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10282 Lisp_Object frame = XWINDOW (mini_window)->frame;
10283 struct frame *f = XFRAME (frame);
10284
10285 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10286 Fmake_frame_visible (frame);
10287
10288 if (STRINGP (m) && SCHARS (m) > 0)
10289 {
10290 set_message (m);
10291 if (minibuffer_auto_raise)
10292 Fraise_frame (frame);
10293 /* Assume we are not echoing.
10294 (If we are, echo_now will override this.) */
10295 echo_message_buffer = Qnil;
10296 }
10297 else
10298 clear_message (true, true);
10299
10300 do_pending_window_change (false);
10301 echo_area_display (true);
10302 do_pending_window_change (false);
10303 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10304 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10305 }
10306 }
10307
10308
10309 /* Display a null-terminated echo area message M. If M is 0, clear
10310 out any existing message, and let the mini-buffer text show through.
10311
10312 The buffer M must continue to exist until after the echo area gets
10313 cleared or some other message gets displayed there. Do not pass
10314 text that is stored in a Lisp string. Do not pass text in a buffer
10315 that was alloca'd. */
10316
10317 void
10318 message1 (const char *m)
10319 {
10320 message3 (m ? build_unibyte_string (m) : Qnil);
10321 }
10322
10323
10324 /* The non-logging counterpart of message1. */
10325
10326 void
10327 message1_nolog (const char *m)
10328 {
10329 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10330 }
10331
10332 /* Display a message M which contains a single %s
10333 which gets replaced with STRING. */
10334
10335 void
10336 message_with_string (const char *m, Lisp_Object string, bool log)
10337 {
10338 CHECK_STRING (string);
10339
10340 bool need_message;
10341 if (noninteractive)
10342 need_message = !!m;
10343 else if (!INTERACTIVE)
10344 need_message = false;
10345 else
10346 {
10347 /* The frame whose minibuffer we're going to display the message on.
10348 It may be larger than the selected frame, so we need
10349 to use its buffer, not the selected frame's buffer. */
10350 Lisp_Object mini_window;
10351 struct frame *f, *sf = SELECTED_FRAME ();
10352
10353 /* Get the frame containing the minibuffer
10354 that the selected frame is using. */
10355 mini_window = FRAME_MINIBUF_WINDOW (sf);
10356 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10357
10358 /* Error messages get reported properly by cmd_error, so this must be
10359 just an informative message; if the frame hasn't really been
10360 initialized yet, just toss it. */
10361 need_message = f->glyphs_initialized_p;
10362 }
10363
10364 if (need_message)
10365 {
10366 AUTO_STRING (fmt, m);
10367 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10368
10369 if (noninteractive)
10370 message_to_stderr (msg);
10371 else
10372 {
10373 if (log)
10374 message3 (msg);
10375 else
10376 message3_nolog (msg);
10377
10378 /* Print should start at the beginning of the message
10379 buffer next time. */
10380 message_buf_print = false;
10381 }
10382 }
10383 }
10384
10385
10386 /* Dump an informative message to the minibuf. If M is 0, clear out
10387 any existing message, and let the mini-buffer text show through.
10388
10389 The message must be safe ASCII and the format must not contain ` or
10390 '. If your message and format do not fit into this category,
10391 convert your arguments to Lisp objects and use Fmessage instead. */
10392
10393 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10394 vmessage (const char *m, va_list ap)
10395 {
10396 if (noninteractive)
10397 {
10398 if (m)
10399 {
10400 if (noninteractive_need_newline)
10401 putc ('\n', stderr);
10402 noninteractive_need_newline = false;
10403 vfprintf (stderr, m, ap);
10404 if (!cursor_in_echo_area)
10405 fprintf (stderr, "\n");
10406 fflush (stderr);
10407 }
10408 }
10409 else if (INTERACTIVE)
10410 {
10411 /* The frame whose mini-buffer we're going to display the message
10412 on. It may be larger than the selected frame, so we need to
10413 use its buffer, not the selected frame's buffer. */
10414 Lisp_Object mini_window;
10415 struct frame *f, *sf = SELECTED_FRAME ();
10416
10417 /* Get the frame containing the mini-buffer
10418 that the selected frame is using. */
10419 mini_window = FRAME_MINIBUF_WINDOW (sf);
10420 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10421
10422 /* Error messages get reported properly by cmd_error, so this must be
10423 just an informative message; if the frame hasn't really been
10424 initialized yet, just toss it. */
10425 if (f->glyphs_initialized_p)
10426 {
10427 if (m)
10428 {
10429 ptrdiff_t len;
10430 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10431 USE_SAFE_ALLOCA;
10432 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10433
10434 len = doprnt (message_buf, maxsize, m, 0, ap);
10435
10436 message3 (make_string (message_buf, len));
10437 SAFE_FREE ();
10438 }
10439 else
10440 message1 (0);
10441
10442 /* Print should start at the beginning of the message
10443 buffer next time. */
10444 message_buf_print = false;
10445 }
10446 }
10447 }
10448
10449 void
10450 message (const char *m, ...)
10451 {
10452 va_list ap;
10453 va_start (ap, m);
10454 vmessage (m, ap);
10455 va_end (ap);
10456 }
10457
10458
10459 /* Display the current message in the current mini-buffer. This is
10460 only called from error handlers in process.c, and is not time
10461 critical. */
10462
10463 void
10464 update_echo_area (void)
10465 {
10466 if (!NILP (echo_area_buffer[0]))
10467 {
10468 Lisp_Object string;
10469 string = Fcurrent_message ();
10470 message3 (string);
10471 }
10472 }
10473
10474
10475 /* Make sure echo area buffers in `echo_buffers' are live.
10476 If they aren't, make new ones. */
10477
10478 static void
10479 ensure_echo_area_buffers (void)
10480 {
10481 int i;
10482
10483 for (i = 0; i < 2; ++i)
10484 if (!BUFFERP (echo_buffer[i])
10485 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10486 {
10487 char name[30];
10488 Lisp_Object old_buffer;
10489 int j;
10490
10491 old_buffer = echo_buffer[i];
10492 echo_buffer[i] = Fget_buffer_create
10493 (make_formatted_string (name, " *Echo Area %d*", i));
10494 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10495 /* to force word wrap in echo area -
10496 it was decided to postpone this*/
10497 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10498
10499 for (j = 0; j < 2; ++j)
10500 if (EQ (old_buffer, echo_area_buffer[j]))
10501 echo_area_buffer[j] = echo_buffer[i];
10502 }
10503 }
10504
10505
10506 /* Call FN with args A1..A2 with either the current or last displayed
10507 echo_area_buffer as current buffer.
10508
10509 WHICH zero means use the current message buffer
10510 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10511 from echo_buffer[] and clear it.
10512
10513 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10514 suitable buffer from echo_buffer[] and clear it.
10515
10516 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10517 that the current message becomes the last displayed one, make
10518 choose a suitable buffer for echo_area_buffer[0], and clear it.
10519
10520 Value is what FN returns. */
10521
10522 static bool
10523 with_echo_area_buffer (struct window *w, int which,
10524 bool (*fn) (ptrdiff_t, Lisp_Object),
10525 ptrdiff_t a1, Lisp_Object a2)
10526 {
10527 Lisp_Object buffer;
10528 bool this_one, the_other, clear_buffer_p, rc;
10529 ptrdiff_t count = SPECPDL_INDEX ();
10530
10531 /* If buffers aren't live, make new ones. */
10532 ensure_echo_area_buffers ();
10533
10534 clear_buffer_p = false;
10535
10536 if (which == 0)
10537 this_one = false, the_other = true;
10538 else if (which > 0)
10539 this_one = true, the_other = false;
10540 else
10541 {
10542 this_one = false, the_other = true;
10543 clear_buffer_p = true;
10544
10545 /* We need a fresh one in case the current echo buffer equals
10546 the one containing the last displayed echo area message. */
10547 if (!NILP (echo_area_buffer[this_one])
10548 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10549 echo_area_buffer[this_one] = Qnil;
10550 }
10551
10552 /* Choose a suitable buffer from echo_buffer[] is we don't
10553 have one. */
10554 if (NILP (echo_area_buffer[this_one]))
10555 {
10556 echo_area_buffer[this_one]
10557 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10558 ? echo_buffer[the_other]
10559 : echo_buffer[this_one]);
10560 clear_buffer_p = true;
10561 }
10562
10563 buffer = echo_area_buffer[this_one];
10564
10565 /* Don't get confused by reusing the buffer used for echoing
10566 for a different purpose. */
10567 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10568 cancel_echoing ();
10569
10570 record_unwind_protect (unwind_with_echo_area_buffer,
10571 with_echo_area_buffer_unwind_data (w));
10572
10573 /* Make the echo area buffer current. Note that for display
10574 purposes, it is not necessary that the displayed window's buffer
10575 == current_buffer, except for text property lookup. So, let's
10576 only set that buffer temporarily here without doing a full
10577 Fset_window_buffer. We must also change w->pointm, though,
10578 because otherwise an assertions in unshow_buffer fails, and Emacs
10579 aborts. */
10580 set_buffer_internal_1 (XBUFFER (buffer));
10581 if (w)
10582 {
10583 wset_buffer (w, buffer);
10584 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10585 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10586 }
10587
10588 bset_undo_list (current_buffer, Qt);
10589 bset_read_only (current_buffer, Qnil);
10590 specbind (Qinhibit_read_only, Qt);
10591 specbind (Qinhibit_modification_hooks, Qt);
10592
10593 if (clear_buffer_p && Z > BEG)
10594 del_range (BEG, Z);
10595
10596 eassert (BEGV >= BEG);
10597 eassert (ZV <= Z && ZV >= BEGV);
10598
10599 rc = fn (a1, a2);
10600
10601 eassert (BEGV >= BEG);
10602 eassert (ZV <= Z && ZV >= BEGV);
10603
10604 unbind_to (count, Qnil);
10605 return rc;
10606 }
10607
10608
10609 /* Save state that should be preserved around the call to the function
10610 FN called in with_echo_area_buffer. */
10611
10612 static Lisp_Object
10613 with_echo_area_buffer_unwind_data (struct window *w)
10614 {
10615 int i = 0;
10616 Lisp_Object vector, tmp;
10617
10618 /* Reduce consing by keeping one vector in
10619 Vwith_echo_area_save_vector. */
10620 vector = Vwith_echo_area_save_vector;
10621 Vwith_echo_area_save_vector = Qnil;
10622
10623 if (NILP (vector))
10624 vector = Fmake_vector (make_number (11), Qnil);
10625
10626 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10627 ASET (vector, i, Vdeactivate_mark); ++i;
10628 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10629
10630 if (w)
10631 {
10632 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10633 ASET (vector, i, w->contents); ++i;
10634 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10635 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10636 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10637 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10638 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10639 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10640 }
10641 else
10642 {
10643 int end = i + 8;
10644 for (; i < end; ++i)
10645 ASET (vector, i, Qnil);
10646 }
10647
10648 eassert (i == ASIZE (vector));
10649 return vector;
10650 }
10651
10652
10653 /* Restore global state from VECTOR which was created by
10654 with_echo_area_buffer_unwind_data. */
10655
10656 static void
10657 unwind_with_echo_area_buffer (Lisp_Object vector)
10658 {
10659 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10660 Vdeactivate_mark = AREF (vector, 1);
10661 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10662
10663 if (WINDOWP (AREF (vector, 3)))
10664 {
10665 struct window *w;
10666 Lisp_Object buffer;
10667
10668 w = XWINDOW (AREF (vector, 3));
10669 buffer = AREF (vector, 4);
10670
10671 wset_buffer (w, buffer);
10672 set_marker_both (w->pointm, buffer,
10673 XFASTINT (AREF (vector, 5)),
10674 XFASTINT (AREF (vector, 6)));
10675 set_marker_both (w->old_pointm, buffer,
10676 XFASTINT (AREF (vector, 7)),
10677 XFASTINT (AREF (vector, 8)));
10678 set_marker_both (w->start, buffer,
10679 XFASTINT (AREF (vector, 9)),
10680 XFASTINT (AREF (vector, 10)));
10681 }
10682
10683 Vwith_echo_area_save_vector = vector;
10684 }
10685
10686
10687 /* Set up the echo area for use by print functions. MULTIBYTE_P
10688 means we will print multibyte. */
10689
10690 void
10691 setup_echo_area_for_printing (bool multibyte_p)
10692 {
10693 /* If we can't find an echo area any more, exit. */
10694 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10695 Fkill_emacs (Qnil);
10696
10697 ensure_echo_area_buffers ();
10698
10699 if (!message_buf_print)
10700 {
10701 /* A message has been output since the last time we printed.
10702 Choose a fresh echo area buffer. */
10703 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10704 echo_area_buffer[0] = echo_buffer[1];
10705 else
10706 echo_area_buffer[0] = echo_buffer[0];
10707
10708 /* Switch to that buffer and clear it. */
10709 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10710 bset_truncate_lines (current_buffer, Qnil);
10711
10712 if (Z > BEG)
10713 {
10714 ptrdiff_t count = SPECPDL_INDEX ();
10715 specbind (Qinhibit_read_only, Qt);
10716 /* Note that undo recording is always disabled. */
10717 del_range (BEG, Z);
10718 unbind_to (count, Qnil);
10719 }
10720 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10721
10722 /* Set up the buffer for the multibyteness we need. */
10723 if (multibyte_p
10724 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10725 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10726
10727 /* Raise the frame containing the echo area. */
10728 if (minibuffer_auto_raise)
10729 {
10730 struct frame *sf = SELECTED_FRAME ();
10731 Lisp_Object mini_window;
10732 mini_window = FRAME_MINIBUF_WINDOW (sf);
10733 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10734 }
10735
10736 message_log_maybe_newline ();
10737 message_buf_print = true;
10738 }
10739 else
10740 {
10741 if (NILP (echo_area_buffer[0]))
10742 {
10743 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10744 echo_area_buffer[0] = echo_buffer[1];
10745 else
10746 echo_area_buffer[0] = echo_buffer[0];
10747 }
10748
10749 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10750 {
10751 /* Someone switched buffers between print requests. */
10752 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10753 bset_truncate_lines (current_buffer, Qnil);
10754 }
10755 }
10756 }
10757
10758
10759 /* Display an echo area message in window W. Value is true if W's
10760 height is changed. If display_last_displayed_message_p,
10761 display the message that was last displayed, otherwise
10762 display the current message. */
10763
10764 static bool
10765 display_echo_area (struct window *w)
10766 {
10767 bool no_message_p, window_height_changed_p;
10768
10769 /* Temporarily disable garbage collections while displaying the echo
10770 area. This is done because a GC can print a message itself.
10771 That message would modify the echo area buffer's contents while a
10772 redisplay of the buffer is going on, and seriously confuse
10773 redisplay. */
10774 ptrdiff_t count = inhibit_garbage_collection ();
10775
10776 /* If there is no message, we must call display_echo_area_1
10777 nevertheless because it resizes the window. But we will have to
10778 reset the echo_area_buffer in question to nil at the end because
10779 with_echo_area_buffer will sets it to an empty buffer. */
10780 bool i = display_last_displayed_message_p;
10781 /* According to the C99, C11 and C++11 standards, the integral value
10782 of a "bool" is always 0 or 1, so this array access is safe here,
10783 if oddly typed. */
10784 no_message_p = NILP (echo_area_buffer[i]);
10785
10786 window_height_changed_p
10787 = with_echo_area_buffer (w, display_last_displayed_message_p,
10788 display_echo_area_1,
10789 (intptr_t) w, Qnil);
10790
10791 if (no_message_p)
10792 echo_area_buffer[i] = Qnil;
10793
10794 unbind_to (count, Qnil);
10795 return window_height_changed_p;
10796 }
10797
10798
10799 /* Helper for display_echo_area. Display the current buffer which
10800 contains the current echo area message in window W, a mini-window,
10801 a pointer to which is passed in A1. A2..A4 are currently not used.
10802 Change the height of W so that all of the message is displayed.
10803 Value is true if height of W was changed. */
10804
10805 static bool
10806 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10807 {
10808 intptr_t i1 = a1;
10809 struct window *w = (struct window *) i1;
10810 Lisp_Object window;
10811 struct text_pos start;
10812
10813 /* We are about to enter redisplay without going through
10814 redisplay_internal, so we need to forget these faces by hand
10815 here. */
10816 forget_escape_and_glyphless_faces ();
10817
10818 /* Do this before displaying, so that we have a large enough glyph
10819 matrix for the display. If we can't get enough space for the
10820 whole text, display the last N lines. That works by setting w->start. */
10821 bool window_height_changed_p = resize_mini_window (w, false);
10822
10823 /* Use the starting position chosen by resize_mini_window. */
10824 SET_TEXT_POS_FROM_MARKER (start, w->start);
10825
10826 /* Display. */
10827 clear_glyph_matrix (w->desired_matrix);
10828 XSETWINDOW (window, w);
10829 try_window (window, start, 0);
10830
10831 return window_height_changed_p;
10832 }
10833
10834
10835 /* Resize the echo area window to exactly the size needed for the
10836 currently displayed message, if there is one. If a mini-buffer
10837 is active, don't shrink it. */
10838
10839 void
10840 resize_echo_area_exactly (void)
10841 {
10842 if (BUFFERP (echo_area_buffer[0])
10843 && WINDOWP (echo_area_window))
10844 {
10845 struct window *w = XWINDOW (echo_area_window);
10846 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10847 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10848 (intptr_t) w, resize_exactly);
10849 if (resized_p)
10850 {
10851 windows_or_buffers_changed = 42;
10852 update_mode_lines = 30;
10853 redisplay_internal ();
10854 }
10855 }
10856 }
10857
10858
10859 /* Callback function for with_echo_area_buffer, when used from
10860 resize_echo_area_exactly. A1 contains a pointer to the window to
10861 resize, EXACTLY non-nil means resize the mini-window exactly to the
10862 size of the text displayed. A3 and A4 are not used. Value is what
10863 resize_mini_window returns. */
10864
10865 static bool
10866 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10867 {
10868 intptr_t i1 = a1;
10869 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10870 }
10871
10872
10873 /* Resize mini-window W to fit the size of its contents. EXACT_P
10874 means size the window exactly to the size needed. Otherwise, it's
10875 only enlarged until W's buffer is empty.
10876
10877 Set W->start to the right place to begin display. If the whole
10878 contents fit, start at the beginning. Otherwise, start so as
10879 to make the end of the contents appear. This is particularly
10880 important for y-or-n-p, but seems desirable generally.
10881
10882 Value is true if the window height has been changed. */
10883
10884 bool
10885 resize_mini_window (struct window *w, bool exact_p)
10886 {
10887 struct frame *f = XFRAME (w->frame);
10888 bool window_height_changed_p = false;
10889
10890 eassert (MINI_WINDOW_P (w));
10891
10892 /* By default, start display at the beginning. */
10893 set_marker_both (w->start, w->contents,
10894 BUF_BEGV (XBUFFER (w->contents)),
10895 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10896
10897 /* Don't resize windows while redisplaying a window; it would
10898 confuse redisplay functions when the size of the window they are
10899 displaying changes from under them. Such a resizing can happen,
10900 for instance, when which-func prints a long message while
10901 we are running fontification-functions. We're running these
10902 functions with safe_call which binds inhibit-redisplay to t. */
10903 if (!NILP (Vinhibit_redisplay))
10904 return false;
10905
10906 /* Nil means don't try to resize. */
10907 if (NILP (Vresize_mini_windows)
10908 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10909 return false;
10910
10911 if (!FRAME_MINIBUF_ONLY_P (f))
10912 {
10913 struct it it;
10914 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10915 + WINDOW_PIXEL_HEIGHT (w));
10916 int unit = FRAME_LINE_HEIGHT (f);
10917 int height, max_height;
10918 struct text_pos start;
10919 struct buffer *old_current_buffer = NULL;
10920
10921 if (current_buffer != XBUFFER (w->contents))
10922 {
10923 old_current_buffer = current_buffer;
10924 set_buffer_internal (XBUFFER (w->contents));
10925 }
10926
10927 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10928
10929 /* Compute the max. number of lines specified by the user. */
10930 if (FLOATP (Vmax_mini_window_height))
10931 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10932 else if (INTEGERP (Vmax_mini_window_height))
10933 max_height = XINT (Vmax_mini_window_height) * unit;
10934 else
10935 max_height = total_height / 4;
10936
10937 /* Correct that max. height if it's bogus. */
10938 max_height = clip_to_bounds (unit, max_height, total_height);
10939
10940 /* Find out the height of the text in the window. */
10941 if (it.line_wrap == TRUNCATE)
10942 height = unit;
10943 else
10944 {
10945 last_height = 0;
10946 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10947 if (it.max_ascent == 0 && it.max_descent == 0)
10948 height = it.current_y + last_height;
10949 else
10950 height = it.current_y + it.max_ascent + it.max_descent;
10951 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10952 }
10953
10954 /* Compute a suitable window start. */
10955 if (height > max_height)
10956 {
10957 height = (max_height / unit) * unit;
10958 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10959 move_it_vertically_backward (&it, height - unit);
10960 start = it.current.pos;
10961 }
10962 else
10963 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10964 SET_MARKER_FROM_TEXT_POS (w->start, start);
10965
10966 if (EQ (Vresize_mini_windows, Qgrow_only))
10967 {
10968 /* Let it grow only, until we display an empty message, in which
10969 case the window shrinks again. */
10970 if (height > WINDOW_PIXEL_HEIGHT (w))
10971 {
10972 int old_height = WINDOW_PIXEL_HEIGHT (w);
10973
10974 FRAME_WINDOWS_FROZEN (f) = true;
10975 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10976 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10977 }
10978 else if (height < WINDOW_PIXEL_HEIGHT (w)
10979 && (exact_p || BEGV == ZV))
10980 {
10981 int old_height = WINDOW_PIXEL_HEIGHT (w);
10982
10983 FRAME_WINDOWS_FROZEN (f) = false;
10984 shrink_mini_window (w, true);
10985 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10986 }
10987 }
10988 else
10989 {
10990 /* Always resize to exact size needed. */
10991 if (height > WINDOW_PIXEL_HEIGHT (w))
10992 {
10993 int old_height = WINDOW_PIXEL_HEIGHT (w);
10994
10995 FRAME_WINDOWS_FROZEN (f) = true;
10996 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10997 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10998 }
10999 else if (height < WINDOW_PIXEL_HEIGHT (w))
11000 {
11001 int old_height = WINDOW_PIXEL_HEIGHT (w);
11002
11003 FRAME_WINDOWS_FROZEN (f) = false;
11004 shrink_mini_window (w, true);
11005
11006 if (height)
11007 {
11008 FRAME_WINDOWS_FROZEN (f) = true;
11009 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11010 }
11011
11012 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11013 }
11014 }
11015
11016 if (old_current_buffer)
11017 set_buffer_internal (old_current_buffer);
11018 }
11019
11020 return window_height_changed_p;
11021 }
11022
11023
11024 /* Value is the current message, a string, or nil if there is no
11025 current message. */
11026
11027 Lisp_Object
11028 current_message (void)
11029 {
11030 Lisp_Object msg;
11031
11032 if (!BUFFERP (echo_area_buffer[0]))
11033 msg = Qnil;
11034 else
11035 {
11036 with_echo_area_buffer (0, 0, current_message_1,
11037 (intptr_t) &msg, Qnil);
11038 if (NILP (msg))
11039 echo_area_buffer[0] = Qnil;
11040 }
11041
11042 return msg;
11043 }
11044
11045
11046 static bool
11047 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11048 {
11049 intptr_t i1 = a1;
11050 Lisp_Object *msg = (Lisp_Object *) i1;
11051
11052 if (Z > BEG)
11053 *msg = make_buffer_string (BEG, Z, true);
11054 else
11055 *msg = Qnil;
11056 return false;
11057 }
11058
11059
11060 /* Push the current message on Vmessage_stack for later restoration
11061 by restore_message. Value is true if the current message isn't
11062 empty. This is a relatively infrequent operation, so it's not
11063 worth optimizing. */
11064
11065 bool
11066 push_message (void)
11067 {
11068 Lisp_Object msg = current_message ();
11069 Vmessage_stack = Fcons (msg, Vmessage_stack);
11070 return STRINGP (msg);
11071 }
11072
11073
11074 /* Restore message display from the top of Vmessage_stack. */
11075
11076 void
11077 restore_message (void)
11078 {
11079 eassert (CONSP (Vmessage_stack));
11080 message3_nolog (XCAR (Vmessage_stack));
11081 }
11082
11083
11084 /* Handler for unwind-protect calling pop_message. */
11085
11086 void
11087 pop_message_unwind (void)
11088 {
11089 /* Pop the top-most entry off Vmessage_stack. */
11090 eassert (CONSP (Vmessage_stack));
11091 Vmessage_stack = XCDR (Vmessage_stack);
11092 }
11093
11094
11095 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11096 exits. If the stack is not empty, we have a missing pop_message
11097 somewhere. */
11098
11099 void
11100 check_message_stack (void)
11101 {
11102 if (!NILP (Vmessage_stack))
11103 emacs_abort ();
11104 }
11105
11106
11107 /* Truncate to NCHARS what will be displayed in the echo area the next
11108 time we display it---but don't redisplay it now. */
11109
11110 void
11111 truncate_echo_area (ptrdiff_t nchars)
11112 {
11113 if (nchars == 0)
11114 echo_area_buffer[0] = Qnil;
11115 else if (!noninteractive
11116 && INTERACTIVE
11117 && !NILP (echo_area_buffer[0]))
11118 {
11119 struct frame *sf = SELECTED_FRAME ();
11120 /* Error messages get reported properly by cmd_error, so this must be
11121 just an informative message; if the frame hasn't really been
11122 initialized yet, just toss it. */
11123 if (sf->glyphs_initialized_p)
11124 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11125 }
11126 }
11127
11128
11129 /* Helper function for truncate_echo_area. Truncate the current
11130 message to at most NCHARS characters. */
11131
11132 static bool
11133 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11134 {
11135 if (BEG + nchars < Z)
11136 del_range (BEG + nchars, Z);
11137 if (Z == BEG)
11138 echo_area_buffer[0] = Qnil;
11139 return false;
11140 }
11141
11142 /* Set the current message to STRING. */
11143
11144 static void
11145 set_message (Lisp_Object string)
11146 {
11147 eassert (STRINGP (string));
11148
11149 message_enable_multibyte = STRING_MULTIBYTE (string);
11150
11151 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11152 message_buf_print = false;
11153 help_echo_showing_p = false;
11154
11155 if (STRINGP (Vdebug_on_message)
11156 && STRINGP (string)
11157 && fast_string_match (Vdebug_on_message, string) >= 0)
11158 call_debugger (list2 (Qerror, string));
11159 }
11160
11161
11162 /* Helper function for set_message. First argument is ignored and second
11163 argument has the same meaning as for set_message.
11164 This function is called with the echo area buffer being current. */
11165
11166 static bool
11167 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11168 {
11169 eassert (STRINGP (string));
11170
11171 /* Change multibyteness of the echo buffer appropriately. */
11172 if (message_enable_multibyte
11173 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11174 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11175
11176 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11177 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11178 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11179
11180 /* Insert new message at BEG. */
11181 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11182
11183 /* This function takes care of single/multibyte conversion.
11184 We just have to ensure that the echo area buffer has the right
11185 setting of enable_multibyte_characters. */
11186 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11187
11188 return false;
11189 }
11190
11191
11192 /* Clear messages. CURRENT_P means clear the current message.
11193 LAST_DISPLAYED_P means clear the message last displayed. */
11194
11195 void
11196 clear_message (bool current_p, bool last_displayed_p)
11197 {
11198 if (current_p)
11199 {
11200 echo_area_buffer[0] = Qnil;
11201 message_cleared_p = true;
11202 }
11203
11204 if (last_displayed_p)
11205 echo_area_buffer[1] = Qnil;
11206
11207 message_buf_print = false;
11208 }
11209
11210 /* Clear garbaged frames.
11211
11212 This function is used where the old redisplay called
11213 redraw_garbaged_frames which in turn called redraw_frame which in
11214 turn called clear_frame. The call to clear_frame was a source of
11215 flickering. I believe a clear_frame is not necessary. It should
11216 suffice in the new redisplay to invalidate all current matrices,
11217 and ensure a complete redisplay of all windows. */
11218
11219 static void
11220 clear_garbaged_frames (void)
11221 {
11222 if (frame_garbaged)
11223 {
11224 Lisp_Object tail, frame;
11225
11226 FOR_EACH_FRAME (tail, frame)
11227 {
11228 struct frame *f = XFRAME (frame);
11229
11230 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11231 {
11232 if (f->resized_p)
11233 redraw_frame (f);
11234 else
11235 clear_current_matrices (f);
11236 fset_redisplay (f);
11237 f->garbaged = false;
11238 f->resized_p = false;
11239 }
11240 }
11241
11242 frame_garbaged = false;
11243 }
11244 }
11245
11246
11247 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11248 selected_frame. */
11249
11250 static void
11251 echo_area_display (bool update_frame_p)
11252 {
11253 Lisp_Object mini_window;
11254 struct window *w;
11255 struct frame *f;
11256 bool window_height_changed_p = false;
11257 struct frame *sf = SELECTED_FRAME ();
11258
11259 mini_window = FRAME_MINIBUF_WINDOW (sf);
11260 w = XWINDOW (mini_window);
11261 f = XFRAME (WINDOW_FRAME (w));
11262
11263 /* Don't display if frame is invisible or not yet initialized. */
11264 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11265 return;
11266
11267 #ifdef HAVE_WINDOW_SYSTEM
11268 /* When Emacs starts, selected_frame may be the initial terminal
11269 frame. If we let this through, a message would be displayed on
11270 the terminal. */
11271 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11272 return;
11273 #endif /* HAVE_WINDOW_SYSTEM */
11274
11275 /* Redraw garbaged frames. */
11276 clear_garbaged_frames ();
11277
11278 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11279 {
11280 echo_area_window = mini_window;
11281 window_height_changed_p = display_echo_area (w);
11282 w->must_be_updated_p = true;
11283
11284 /* Update the display, unless called from redisplay_internal.
11285 Also don't update the screen during redisplay itself. The
11286 update will happen at the end of redisplay, and an update
11287 here could cause confusion. */
11288 if (update_frame_p && !redisplaying_p)
11289 {
11290 int n = 0;
11291
11292 /* If the display update has been interrupted by pending
11293 input, update mode lines in the frame. Due to the
11294 pending input, it might have been that redisplay hasn't
11295 been called, so that mode lines above the echo area are
11296 garbaged. This looks odd, so we prevent it here. */
11297 if (!display_completed)
11298 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11299
11300 if (window_height_changed_p
11301 /* Don't do this if Emacs is shutting down. Redisplay
11302 needs to run hooks. */
11303 && !NILP (Vrun_hooks))
11304 {
11305 /* Must update other windows. Likewise as in other
11306 cases, don't let this update be interrupted by
11307 pending input. */
11308 ptrdiff_t count = SPECPDL_INDEX ();
11309 specbind (Qredisplay_dont_pause, Qt);
11310 fset_redisplay (f);
11311 redisplay_internal ();
11312 unbind_to (count, Qnil);
11313 }
11314 else if (FRAME_WINDOW_P (f) && n == 0)
11315 {
11316 /* Window configuration is the same as before.
11317 Can do with a display update of the echo area,
11318 unless we displayed some mode lines. */
11319 update_single_window (w);
11320 flush_frame (f);
11321 }
11322 else
11323 update_frame (f, true, true);
11324
11325 /* If cursor is in the echo area, make sure that the next
11326 redisplay displays the minibuffer, so that the cursor will
11327 be replaced with what the minibuffer wants. */
11328 if (cursor_in_echo_area)
11329 wset_redisplay (XWINDOW (mini_window));
11330 }
11331 }
11332 else if (!EQ (mini_window, selected_window))
11333 wset_redisplay (XWINDOW (mini_window));
11334
11335 /* Last displayed message is now the current message. */
11336 echo_area_buffer[1] = echo_area_buffer[0];
11337 /* Inform read_char that we're not echoing. */
11338 echo_message_buffer = Qnil;
11339
11340 /* Prevent redisplay optimization in redisplay_internal by resetting
11341 this_line_start_pos. This is done because the mini-buffer now
11342 displays the message instead of its buffer text. */
11343 if (EQ (mini_window, selected_window))
11344 CHARPOS (this_line_start_pos) = 0;
11345
11346 if (window_height_changed_p)
11347 {
11348 fset_redisplay (f);
11349
11350 /* If window configuration was changed, frames may have been
11351 marked garbaged. Clear them or we will experience
11352 surprises wrt scrolling.
11353 FIXME: How/why/when? */
11354 clear_garbaged_frames ();
11355 }
11356 }
11357
11358 /* True if W's buffer was changed but not saved. */
11359
11360 static bool
11361 window_buffer_changed (struct window *w)
11362 {
11363 struct buffer *b = XBUFFER (w->contents);
11364
11365 eassert (BUFFER_LIVE_P (b));
11366
11367 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11368 }
11369
11370 /* True if W has %c in its mode line and mode line should be updated. */
11371
11372 static bool
11373 mode_line_update_needed (struct window *w)
11374 {
11375 return (w->column_number_displayed != -1
11376 && !(PT == w->last_point && !window_outdated (w))
11377 && (w->column_number_displayed != current_column ()));
11378 }
11379
11380 /* True if window start of W is frozen and may not be changed during
11381 redisplay. */
11382
11383 static bool
11384 window_frozen_p (struct window *w)
11385 {
11386 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11387 {
11388 Lisp_Object window;
11389
11390 XSETWINDOW (window, w);
11391 if (MINI_WINDOW_P (w))
11392 return false;
11393 else if (EQ (window, selected_window))
11394 return false;
11395 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11396 && EQ (window, Vminibuf_scroll_window))
11397 /* This special window can't be frozen too. */
11398 return false;
11399 else
11400 return true;
11401 }
11402 return false;
11403 }
11404
11405 /***********************************************************************
11406 Mode Lines and Frame Titles
11407 ***********************************************************************/
11408
11409 /* A buffer for constructing non-propertized mode-line strings and
11410 frame titles in it; allocated from the heap in init_xdisp and
11411 resized as needed in store_mode_line_noprop_char. */
11412
11413 static char *mode_line_noprop_buf;
11414
11415 /* The buffer's end, and a current output position in it. */
11416
11417 static char *mode_line_noprop_buf_end;
11418 static char *mode_line_noprop_ptr;
11419
11420 #define MODE_LINE_NOPROP_LEN(start) \
11421 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11422
11423 static enum {
11424 MODE_LINE_DISPLAY = 0,
11425 MODE_LINE_TITLE,
11426 MODE_LINE_NOPROP,
11427 MODE_LINE_STRING
11428 } mode_line_target;
11429
11430 /* Alist that caches the results of :propertize.
11431 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11432 static Lisp_Object mode_line_proptrans_alist;
11433
11434 /* List of strings making up the mode-line. */
11435 static Lisp_Object mode_line_string_list;
11436
11437 /* Base face property when building propertized mode line string. */
11438 static Lisp_Object mode_line_string_face;
11439 static Lisp_Object mode_line_string_face_prop;
11440
11441
11442 /* Unwind data for mode line strings */
11443
11444 static Lisp_Object Vmode_line_unwind_vector;
11445
11446 static Lisp_Object
11447 format_mode_line_unwind_data (struct frame *target_frame,
11448 struct buffer *obuf,
11449 Lisp_Object owin,
11450 bool save_proptrans)
11451 {
11452 Lisp_Object vector, tmp;
11453
11454 /* Reduce consing by keeping one vector in
11455 Vwith_echo_area_save_vector. */
11456 vector = Vmode_line_unwind_vector;
11457 Vmode_line_unwind_vector = Qnil;
11458
11459 if (NILP (vector))
11460 vector = Fmake_vector (make_number (10), Qnil);
11461
11462 ASET (vector, 0, make_number (mode_line_target));
11463 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11464 ASET (vector, 2, mode_line_string_list);
11465 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11466 ASET (vector, 4, mode_line_string_face);
11467 ASET (vector, 5, mode_line_string_face_prop);
11468
11469 if (obuf)
11470 XSETBUFFER (tmp, obuf);
11471 else
11472 tmp = Qnil;
11473 ASET (vector, 6, tmp);
11474 ASET (vector, 7, owin);
11475 if (target_frame)
11476 {
11477 /* Similarly to `with-selected-window', if the operation selects
11478 a window on another frame, we must restore that frame's
11479 selected window, and (for a tty) the top-frame. */
11480 ASET (vector, 8, target_frame->selected_window);
11481 if (FRAME_TERMCAP_P (target_frame))
11482 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11483 }
11484
11485 return vector;
11486 }
11487
11488 static void
11489 unwind_format_mode_line (Lisp_Object vector)
11490 {
11491 Lisp_Object old_window = AREF (vector, 7);
11492 Lisp_Object target_frame_window = AREF (vector, 8);
11493 Lisp_Object old_top_frame = AREF (vector, 9);
11494
11495 mode_line_target = XINT (AREF (vector, 0));
11496 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11497 mode_line_string_list = AREF (vector, 2);
11498 if (! EQ (AREF (vector, 3), Qt))
11499 mode_line_proptrans_alist = AREF (vector, 3);
11500 mode_line_string_face = AREF (vector, 4);
11501 mode_line_string_face_prop = AREF (vector, 5);
11502
11503 /* Select window before buffer, since it may change the buffer. */
11504 if (!NILP (old_window))
11505 {
11506 /* If the operation that we are unwinding had selected a window
11507 on a different frame, reset its frame-selected-window. For a
11508 text terminal, reset its top-frame if necessary. */
11509 if (!NILP (target_frame_window))
11510 {
11511 Lisp_Object frame
11512 = WINDOW_FRAME (XWINDOW (target_frame_window));
11513
11514 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11515 Fselect_window (target_frame_window, Qt);
11516
11517 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11518 Fselect_frame (old_top_frame, Qt);
11519 }
11520
11521 Fselect_window (old_window, Qt);
11522 }
11523
11524 if (!NILP (AREF (vector, 6)))
11525 {
11526 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11527 ASET (vector, 6, Qnil);
11528 }
11529
11530 Vmode_line_unwind_vector = vector;
11531 }
11532
11533
11534 /* Store a single character C for the frame title in mode_line_noprop_buf.
11535 Re-allocate mode_line_noprop_buf if necessary. */
11536
11537 static void
11538 store_mode_line_noprop_char (char c)
11539 {
11540 /* If output position has reached the end of the allocated buffer,
11541 increase the buffer's size. */
11542 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11543 {
11544 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11545 ptrdiff_t size = len;
11546 mode_line_noprop_buf =
11547 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11548 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11549 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11550 }
11551
11552 *mode_line_noprop_ptr++ = c;
11553 }
11554
11555
11556 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11557 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11558 characters that yield more columns than PRECISION; PRECISION <= 0
11559 means copy the whole string. Pad with spaces until FIELD_WIDTH
11560 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11561 pad. Called from display_mode_element when it is used to build a
11562 frame title. */
11563
11564 static int
11565 store_mode_line_noprop (const char *string, int field_width, int precision)
11566 {
11567 const unsigned char *str = (const unsigned char *) string;
11568 int n = 0;
11569 ptrdiff_t dummy, nbytes;
11570
11571 /* Copy at most PRECISION chars from STR. */
11572 nbytes = strlen (string);
11573 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11574 while (nbytes--)
11575 store_mode_line_noprop_char (*str++);
11576
11577 /* Fill up with spaces until FIELD_WIDTH reached. */
11578 while (field_width > 0
11579 && n < field_width)
11580 {
11581 store_mode_line_noprop_char (' ');
11582 ++n;
11583 }
11584
11585 return n;
11586 }
11587
11588 /***********************************************************************
11589 Frame Titles
11590 ***********************************************************************/
11591
11592 #ifdef HAVE_WINDOW_SYSTEM
11593
11594 /* Set the title of FRAME, if it has changed. The title format is
11595 Vicon_title_format if FRAME is iconified, otherwise it is
11596 frame_title_format. */
11597
11598 static void
11599 x_consider_frame_title (Lisp_Object frame)
11600 {
11601 struct frame *f = XFRAME (frame);
11602
11603 if ((FRAME_WINDOW_P (f)
11604 || FRAME_MINIBUF_ONLY_P (f)
11605 || f->explicit_name)
11606 && NILP (Fframe_parameter (frame, Qtooltip)))
11607 {
11608 /* Do we have more than one visible frame on this X display? */
11609 Lisp_Object tail, other_frame, fmt;
11610 ptrdiff_t title_start;
11611 char *title;
11612 ptrdiff_t len;
11613 struct it it;
11614 ptrdiff_t count = SPECPDL_INDEX ();
11615
11616 FOR_EACH_FRAME (tail, other_frame)
11617 {
11618 struct frame *tf = XFRAME (other_frame);
11619
11620 if (tf != f
11621 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11622 && !FRAME_MINIBUF_ONLY_P (tf)
11623 && !EQ (other_frame, tip_frame)
11624 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11625 break;
11626 }
11627
11628 /* Set global variable indicating that multiple frames exist. */
11629 multiple_frames = CONSP (tail);
11630
11631 /* Switch to the buffer of selected window of the frame. Set up
11632 mode_line_target so that display_mode_element will output into
11633 mode_line_noprop_buf; then display the title. */
11634 record_unwind_protect (unwind_format_mode_line,
11635 format_mode_line_unwind_data
11636 (f, current_buffer, selected_window, false));
11637
11638 Fselect_window (f->selected_window, Qt);
11639 set_buffer_internal_1
11640 (XBUFFER (XWINDOW (f->selected_window)->contents));
11641 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11642
11643 mode_line_target = MODE_LINE_TITLE;
11644 title_start = MODE_LINE_NOPROP_LEN (0);
11645 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11646 NULL, DEFAULT_FACE_ID);
11647 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11648 len = MODE_LINE_NOPROP_LEN (title_start);
11649 title = mode_line_noprop_buf + title_start;
11650 unbind_to (count, Qnil);
11651
11652 /* Set the title only if it's changed. This avoids consing in
11653 the common case where it hasn't. (If it turns out that we've
11654 already wasted too much time by walking through the list with
11655 display_mode_element, then we might need to optimize at a
11656 higher level than this.) */
11657 if (! STRINGP (f->name)
11658 || SBYTES (f->name) != len
11659 || memcmp (title, SDATA (f->name), len) != 0)
11660 x_implicitly_set_name (f, make_string (title, len), Qnil);
11661 }
11662 }
11663
11664 #endif /* not HAVE_WINDOW_SYSTEM */
11665
11666 \f
11667 /***********************************************************************
11668 Menu Bars
11669 ***********************************************************************/
11670
11671 /* True if we will not redisplay all visible windows. */
11672 #define REDISPLAY_SOME_P() \
11673 ((windows_or_buffers_changed == 0 \
11674 || windows_or_buffers_changed == REDISPLAY_SOME) \
11675 && (update_mode_lines == 0 \
11676 || update_mode_lines == REDISPLAY_SOME))
11677
11678 /* Prepare for redisplay by updating menu-bar item lists when
11679 appropriate. This can call eval. */
11680
11681 static void
11682 prepare_menu_bars (void)
11683 {
11684 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11685 bool some_windows = REDISPLAY_SOME_P ();
11686 Lisp_Object tooltip_frame;
11687
11688 #ifdef HAVE_WINDOW_SYSTEM
11689 tooltip_frame = tip_frame;
11690 #else
11691 tooltip_frame = Qnil;
11692 #endif
11693
11694 if (FUNCTIONP (Vpre_redisplay_function))
11695 {
11696 Lisp_Object windows = all_windows ? Qt : Qnil;
11697 if (all_windows && some_windows)
11698 {
11699 Lisp_Object ws = window_list ();
11700 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11701 {
11702 Lisp_Object this = XCAR (ws);
11703 struct window *w = XWINDOW (this);
11704 if (w->redisplay
11705 || XFRAME (w->frame)->redisplay
11706 || XBUFFER (w->contents)->text->redisplay)
11707 {
11708 windows = Fcons (this, windows);
11709 }
11710 }
11711 }
11712 safe__call1 (true, Vpre_redisplay_function, windows);
11713 }
11714
11715 /* Update all frame titles based on their buffer names, etc. We do
11716 this before the menu bars so that the buffer-menu will show the
11717 up-to-date frame titles. */
11718 #ifdef HAVE_WINDOW_SYSTEM
11719 if (all_windows)
11720 {
11721 Lisp_Object tail, frame;
11722
11723 FOR_EACH_FRAME (tail, frame)
11724 {
11725 struct frame *f = XFRAME (frame);
11726 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11727 if (some_windows
11728 && !f->redisplay
11729 && !w->redisplay
11730 && !XBUFFER (w->contents)->text->redisplay)
11731 continue;
11732
11733 if (!EQ (frame, tooltip_frame)
11734 && (FRAME_ICONIFIED_P (f)
11735 || FRAME_VISIBLE_P (f) == 1
11736 /* Exclude TTY frames that are obscured because they
11737 are not the top frame on their console. This is
11738 because x_consider_frame_title actually switches
11739 to the frame, which for TTY frames means it is
11740 marked as garbaged, and will be completely
11741 redrawn on the next redisplay cycle. This causes
11742 TTY frames to be completely redrawn, when there
11743 are more than one of them, even though nothing
11744 should be changed on display. */
11745 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11746 x_consider_frame_title (frame);
11747 }
11748 }
11749 #endif /* HAVE_WINDOW_SYSTEM */
11750
11751 /* Update the menu bar item lists, if appropriate. This has to be
11752 done before any actual redisplay or generation of display lines. */
11753
11754 if (all_windows)
11755 {
11756 Lisp_Object tail, frame;
11757 ptrdiff_t count = SPECPDL_INDEX ();
11758 /* True means that update_menu_bar has run its hooks
11759 so any further calls to update_menu_bar shouldn't do so again. */
11760 bool menu_bar_hooks_run = false;
11761
11762 record_unwind_save_match_data ();
11763
11764 FOR_EACH_FRAME (tail, frame)
11765 {
11766 struct frame *f = XFRAME (frame);
11767 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11768
11769 /* Ignore tooltip frame. */
11770 if (EQ (frame, tooltip_frame))
11771 continue;
11772
11773 if (some_windows
11774 && !f->redisplay
11775 && !w->redisplay
11776 && !XBUFFER (w->contents)->text->redisplay)
11777 continue;
11778
11779 /* If a window on this frame changed size, report that to
11780 the user and clear the size-change flag. */
11781 if (FRAME_WINDOW_SIZES_CHANGED (f))
11782 {
11783 Lisp_Object functions;
11784
11785 /* Clear flag first in case we get an error below. */
11786 FRAME_WINDOW_SIZES_CHANGED (f) = false;
11787 functions = Vwindow_size_change_functions;
11788
11789 while (CONSP (functions))
11790 {
11791 if (!EQ (XCAR (functions), Qt))
11792 call1 (XCAR (functions), frame);
11793 functions = XCDR (functions);
11794 }
11795 }
11796
11797 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11798 #ifdef HAVE_WINDOW_SYSTEM
11799 update_tool_bar (f, false);
11800 #endif
11801 }
11802
11803 unbind_to (count, Qnil);
11804 }
11805 else
11806 {
11807 struct frame *sf = SELECTED_FRAME ();
11808 update_menu_bar (sf, true, false);
11809 #ifdef HAVE_WINDOW_SYSTEM
11810 update_tool_bar (sf, true);
11811 #endif
11812 }
11813 }
11814
11815
11816 /* Update the menu bar item list for frame F. This has to be done
11817 before we start to fill in any display lines, because it can call
11818 eval.
11819
11820 If SAVE_MATCH_DATA, we must save and restore it here.
11821
11822 If HOOKS_RUN, a previous call to update_menu_bar
11823 already ran the menu bar hooks for this redisplay, so there
11824 is no need to run them again. The return value is the
11825 updated value of this flag, to pass to the next call. */
11826
11827 static bool
11828 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11829 {
11830 Lisp_Object window;
11831 struct window *w;
11832
11833 /* If called recursively during a menu update, do nothing. This can
11834 happen when, for instance, an activate-menubar-hook causes a
11835 redisplay. */
11836 if (inhibit_menubar_update)
11837 return hooks_run;
11838
11839 window = FRAME_SELECTED_WINDOW (f);
11840 w = XWINDOW (window);
11841
11842 if (FRAME_WINDOW_P (f)
11843 ?
11844 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11845 || defined (HAVE_NS) || defined (USE_GTK)
11846 FRAME_EXTERNAL_MENU_BAR (f)
11847 #else
11848 FRAME_MENU_BAR_LINES (f) > 0
11849 #endif
11850 : FRAME_MENU_BAR_LINES (f) > 0)
11851 {
11852 /* If the user has switched buffers or windows, we need to
11853 recompute to reflect the new bindings. But we'll
11854 recompute when update_mode_lines is set too; that means
11855 that people can use force-mode-line-update to request
11856 that the menu bar be recomputed. The adverse effect on
11857 the rest of the redisplay algorithm is about the same as
11858 windows_or_buffers_changed anyway. */
11859 if (windows_or_buffers_changed
11860 /* This used to test w->update_mode_line, but we believe
11861 there is no need to recompute the menu in that case. */
11862 || update_mode_lines
11863 || window_buffer_changed (w))
11864 {
11865 struct buffer *prev = current_buffer;
11866 ptrdiff_t count = SPECPDL_INDEX ();
11867
11868 specbind (Qinhibit_menubar_update, Qt);
11869
11870 set_buffer_internal_1 (XBUFFER (w->contents));
11871 if (save_match_data)
11872 record_unwind_save_match_data ();
11873 if (NILP (Voverriding_local_map_menu_flag))
11874 {
11875 specbind (Qoverriding_terminal_local_map, Qnil);
11876 specbind (Qoverriding_local_map, Qnil);
11877 }
11878
11879 if (!hooks_run)
11880 {
11881 /* Run the Lucid hook. */
11882 safe_run_hooks (Qactivate_menubar_hook);
11883
11884 /* If it has changed current-menubar from previous value,
11885 really recompute the menu-bar from the value. */
11886 if (! NILP (Vlucid_menu_bar_dirty_flag))
11887 call0 (Qrecompute_lucid_menubar);
11888
11889 safe_run_hooks (Qmenu_bar_update_hook);
11890
11891 hooks_run = true;
11892 }
11893
11894 XSETFRAME (Vmenu_updating_frame, f);
11895 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11896
11897 /* Redisplay the menu bar in case we changed it. */
11898 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11899 || defined (HAVE_NS) || defined (USE_GTK)
11900 if (FRAME_WINDOW_P (f))
11901 {
11902 #if defined (HAVE_NS)
11903 /* All frames on Mac OS share the same menubar. So only
11904 the selected frame should be allowed to set it. */
11905 if (f == SELECTED_FRAME ())
11906 #endif
11907 set_frame_menubar (f, false, false);
11908 }
11909 else
11910 /* On a terminal screen, the menu bar is an ordinary screen
11911 line, and this makes it get updated. */
11912 w->update_mode_line = true;
11913 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11914 /* In the non-toolkit version, the menu bar is an ordinary screen
11915 line, and this makes it get updated. */
11916 w->update_mode_line = true;
11917 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11918
11919 unbind_to (count, Qnil);
11920 set_buffer_internal_1 (prev);
11921 }
11922 }
11923
11924 return hooks_run;
11925 }
11926
11927 /***********************************************************************
11928 Tool-bars
11929 ***********************************************************************/
11930
11931 #ifdef HAVE_WINDOW_SYSTEM
11932
11933 /* Select `frame' temporarily without running all the code in
11934 do_switch_frame.
11935 FIXME: Maybe do_switch_frame should be trimmed down similarly
11936 when `norecord' is set. */
11937 static void
11938 fast_set_selected_frame (Lisp_Object frame)
11939 {
11940 if (!EQ (selected_frame, frame))
11941 {
11942 selected_frame = frame;
11943 selected_window = XFRAME (frame)->selected_window;
11944 }
11945 }
11946
11947 /* Update the tool-bar item list for frame F. This has to be done
11948 before we start to fill in any display lines. Called from
11949 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
11950 and restore it here. */
11951
11952 static void
11953 update_tool_bar (struct frame *f, bool save_match_data)
11954 {
11955 #if defined (USE_GTK) || defined (HAVE_NS)
11956 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11957 #else
11958 bool do_update = (WINDOWP (f->tool_bar_window)
11959 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11960 #endif
11961
11962 if (do_update)
11963 {
11964 Lisp_Object window;
11965 struct window *w;
11966
11967 window = FRAME_SELECTED_WINDOW (f);
11968 w = XWINDOW (window);
11969
11970 /* If the user has switched buffers or windows, we need to
11971 recompute to reflect the new bindings. But we'll
11972 recompute when update_mode_lines is set too; that means
11973 that people can use force-mode-line-update to request
11974 that the menu bar be recomputed. The adverse effect on
11975 the rest of the redisplay algorithm is about the same as
11976 windows_or_buffers_changed anyway. */
11977 if (windows_or_buffers_changed
11978 || w->update_mode_line
11979 || update_mode_lines
11980 || window_buffer_changed (w))
11981 {
11982 struct buffer *prev = current_buffer;
11983 ptrdiff_t count = SPECPDL_INDEX ();
11984 Lisp_Object frame, new_tool_bar;
11985 int new_n_tool_bar;
11986
11987 /* Set current_buffer to the buffer of the selected
11988 window of the frame, so that we get the right local
11989 keymaps. */
11990 set_buffer_internal_1 (XBUFFER (w->contents));
11991
11992 /* Save match data, if we must. */
11993 if (save_match_data)
11994 record_unwind_save_match_data ();
11995
11996 /* Make sure that we don't accidentally use bogus keymaps. */
11997 if (NILP (Voverriding_local_map_menu_flag))
11998 {
11999 specbind (Qoverriding_terminal_local_map, Qnil);
12000 specbind (Qoverriding_local_map, Qnil);
12001 }
12002
12003 /* We must temporarily set the selected frame to this frame
12004 before calling tool_bar_items, because the calculation of
12005 the tool-bar keymap uses the selected frame (see
12006 `tool-bar-make-keymap' in tool-bar.el). */
12007 eassert (EQ (selected_window,
12008 /* Since we only explicitly preserve selected_frame,
12009 check that selected_window would be redundant. */
12010 XFRAME (selected_frame)->selected_window));
12011 record_unwind_protect (fast_set_selected_frame, selected_frame);
12012 XSETFRAME (frame, f);
12013 fast_set_selected_frame (frame);
12014
12015 /* Build desired tool-bar items from keymaps. */
12016 new_tool_bar
12017 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12018 &new_n_tool_bar);
12019
12020 /* Redisplay the tool-bar if we changed it. */
12021 if (new_n_tool_bar != f->n_tool_bar_items
12022 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12023 {
12024 /* Redisplay that happens asynchronously due to an expose event
12025 may access f->tool_bar_items. Make sure we update both
12026 variables within BLOCK_INPUT so no such event interrupts. */
12027 block_input ();
12028 fset_tool_bar_items (f, new_tool_bar);
12029 f->n_tool_bar_items = new_n_tool_bar;
12030 w->update_mode_line = true;
12031 unblock_input ();
12032 }
12033
12034 unbind_to (count, Qnil);
12035 set_buffer_internal_1 (prev);
12036 }
12037 }
12038 }
12039
12040 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12041
12042 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12043 F's desired tool-bar contents. F->tool_bar_items must have
12044 been set up previously by calling prepare_menu_bars. */
12045
12046 static void
12047 build_desired_tool_bar_string (struct frame *f)
12048 {
12049 int i, size, size_needed;
12050 Lisp_Object image, plist;
12051
12052 image = plist = Qnil;
12053
12054 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12055 Otherwise, make a new string. */
12056
12057 /* The size of the string we might be able to reuse. */
12058 size = (STRINGP (f->desired_tool_bar_string)
12059 ? SCHARS (f->desired_tool_bar_string)
12060 : 0);
12061
12062 /* We need one space in the string for each image. */
12063 size_needed = f->n_tool_bar_items;
12064
12065 /* Reuse f->desired_tool_bar_string, if possible. */
12066 if (size < size_needed || NILP (f->desired_tool_bar_string))
12067 fset_desired_tool_bar_string
12068 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12069 else
12070 {
12071 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12072 Fremove_text_properties (make_number (0), make_number (size),
12073 props, f->desired_tool_bar_string);
12074 }
12075
12076 /* Put a `display' property on the string for the images to display,
12077 put a `menu_item' property on tool-bar items with a value that
12078 is the index of the item in F's tool-bar item vector. */
12079 for (i = 0; i < f->n_tool_bar_items; ++i)
12080 {
12081 #define PROP(IDX) \
12082 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12083
12084 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12085 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12086 int hmargin, vmargin, relief, idx, end;
12087
12088 /* If image is a vector, choose the image according to the
12089 button state. */
12090 image = PROP (TOOL_BAR_ITEM_IMAGES);
12091 if (VECTORP (image))
12092 {
12093 if (enabled_p)
12094 idx = (selected_p
12095 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12096 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12097 else
12098 idx = (selected_p
12099 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12100 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12101
12102 eassert (ASIZE (image) >= idx);
12103 image = AREF (image, idx);
12104 }
12105 else
12106 idx = -1;
12107
12108 /* Ignore invalid image specifications. */
12109 if (!valid_image_p (image))
12110 continue;
12111
12112 /* Display the tool-bar button pressed, or depressed. */
12113 plist = Fcopy_sequence (XCDR (image));
12114
12115 /* Compute margin and relief to draw. */
12116 relief = (tool_bar_button_relief >= 0
12117 ? tool_bar_button_relief
12118 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12119 hmargin = vmargin = relief;
12120
12121 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12122 INT_MAX - max (hmargin, vmargin)))
12123 {
12124 hmargin += XFASTINT (Vtool_bar_button_margin);
12125 vmargin += XFASTINT (Vtool_bar_button_margin);
12126 }
12127 else if (CONSP (Vtool_bar_button_margin))
12128 {
12129 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12130 INT_MAX - hmargin))
12131 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12132
12133 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12134 INT_MAX - vmargin))
12135 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12136 }
12137
12138 if (auto_raise_tool_bar_buttons_p)
12139 {
12140 /* Add a `:relief' property to the image spec if the item is
12141 selected. */
12142 if (selected_p)
12143 {
12144 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12145 hmargin -= relief;
12146 vmargin -= relief;
12147 }
12148 }
12149 else
12150 {
12151 /* If image is selected, display it pressed, i.e. with a
12152 negative relief. If it's not selected, display it with a
12153 raised relief. */
12154 plist = Fplist_put (plist, QCrelief,
12155 (selected_p
12156 ? make_number (-relief)
12157 : make_number (relief)));
12158 hmargin -= relief;
12159 vmargin -= relief;
12160 }
12161
12162 /* Put a margin around the image. */
12163 if (hmargin || vmargin)
12164 {
12165 if (hmargin == vmargin)
12166 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12167 else
12168 plist = Fplist_put (plist, QCmargin,
12169 Fcons (make_number (hmargin),
12170 make_number (vmargin)));
12171 }
12172
12173 /* If button is not enabled, and we don't have special images
12174 for the disabled state, make the image appear disabled by
12175 applying an appropriate algorithm to it. */
12176 if (!enabled_p && idx < 0)
12177 plist = Fplist_put (plist, QCconversion, Qdisabled);
12178
12179 /* Put a `display' text property on the string for the image to
12180 display. Put a `menu-item' property on the string that gives
12181 the start of this item's properties in the tool-bar items
12182 vector. */
12183 image = Fcons (Qimage, plist);
12184 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12185 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12186
12187 /* Let the last image hide all remaining spaces in the tool bar
12188 string. The string can be longer than needed when we reuse a
12189 previous string. */
12190 if (i + 1 == f->n_tool_bar_items)
12191 end = SCHARS (f->desired_tool_bar_string);
12192 else
12193 end = i + 1;
12194 Fadd_text_properties (make_number (i), make_number (end),
12195 props, f->desired_tool_bar_string);
12196 #undef PROP
12197 }
12198 }
12199
12200
12201 /* Display one line of the tool-bar of frame IT->f.
12202
12203 HEIGHT specifies the desired height of the tool-bar line.
12204 If the actual height of the glyph row is less than HEIGHT, the
12205 row's height is increased to HEIGHT, and the icons are centered
12206 vertically in the new height.
12207
12208 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12209 count a final empty row in case the tool-bar width exactly matches
12210 the window width.
12211 */
12212
12213 static void
12214 display_tool_bar_line (struct it *it, int height)
12215 {
12216 struct glyph_row *row = it->glyph_row;
12217 int max_x = it->last_visible_x;
12218 struct glyph *last;
12219
12220 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12221 clear_glyph_row (row);
12222 row->enabled_p = true;
12223 row->y = it->current_y;
12224
12225 /* Note that this isn't made use of if the face hasn't a box,
12226 so there's no need to check the face here. */
12227 it->start_of_box_run_p = true;
12228
12229 while (it->current_x < max_x)
12230 {
12231 int x, n_glyphs_before, i, nglyphs;
12232 struct it it_before;
12233
12234 /* Get the next display element. */
12235 if (!get_next_display_element (it))
12236 {
12237 /* Don't count empty row if we are counting needed tool-bar lines. */
12238 if (height < 0 && !it->hpos)
12239 return;
12240 break;
12241 }
12242
12243 /* Produce glyphs. */
12244 n_glyphs_before = row->used[TEXT_AREA];
12245 it_before = *it;
12246
12247 PRODUCE_GLYPHS (it);
12248
12249 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12250 i = 0;
12251 x = it_before.current_x;
12252 while (i < nglyphs)
12253 {
12254 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12255
12256 if (x + glyph->pixel_width > max_x)
12257 {
12258 /* Glyph doesn't fit on line. Backtrack. */
12259 row->used[TEXT_AREA] = n_glyphs_before;
12260 *it = it_before;
12261 /* If this is the only glyph on this line, it will never fit on the
12262 tool-bar, so skip it. But ensure there is at least one glyph,
12263 so we don't accidentally disable the tool-bar. */
12264 if (n_glyphs_before == 0
12265 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12266 break;
12267 goto out;
12268 }
12269
12270 ++it->hpos;
12271 x += glyph->pixel_width;
12272 ++i;
12273 }
12274
12275 /* Stop at line end. */
12276 if (ITERATOR_AT_END_OF_LINE_P (it))
12277 break;
12278
12279 set_iterator_to_next (it, true);
12280 }
12281
12282 out:;
12283
12284 row->displays_text_p = row->used[TEXT_AREA] != 0;
12285
12286 /* Use default face for the border below the tool bar.
12287
12288 FIXME: When auto-resize-tool-bars is grow-only, there is
12289 no additional border below the possibly empty tool-bar lines.
12290 So to make the extra empty lines look "normal", we have to
12291 use the tool-bar face for the border too. */
12292 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12293 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12294 it->face_id = DEFAULT_FACE_ID;
12295
12296 extend_face_to_end_of_line (it);
12297 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12298 last->right_box_line_p = true;
12299 if (last == row->glyphs[TEXT_AREA])
12300 last->left_box_line_p = true;
12301
12302 /* Make line the desired height and center it vertically. */
12303 if ((height -= it->max_ascent + it->max_descent) > 0)
12304 {
12305 /* Don't add more than one line height. */
12306 height %= FRAME_LINE_HEIGHT (it->f);
12307 it->max_ascent += height / 2;
12308 it->max_descent += (height + 1) / 2;
12309 }
12310
12311 compute_line_metrics (it);
12312
12313 /* If line is empty, make it occupy the rest of the tool-bar. */
12314 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12315 {
12316 row->height = row->phys_height = it->last_visible_y - row->y;
12317 row->visible_height = row->height;
12318 row->ascent = row->phys_ascent = 0;
12319 row->extra_line_spacing = 0;
12320 }
12321
12322 row->full_width_p = true;
12323 row->continued_p = false;
12324 row->truncated_on_left_p = false;
12325 row->truncated_on_right_p = false;
12326
12327 it->current_x = it->hpos = 0;
12328 it->current_y += row->height;
12329 ++it->vpos;
12330 ++it->glyph_row;
12331 }
12332
12333
12334 /* Value is the number of pixels needed to make all tool-bar items of
12335 frame F visible. The actual number of glyph rows needed is
12336 returned in *N_ROWS if non-NULL. */
12337 static int
12338 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12339 {
12340 struct window *w = XWINDOW (f->tool_bar_window);
12341 struct it it;
12342 /* tool_bar_height is called from redisplay_tool_bar after building
12343 the desired matrix, so use (unused) mode-line row as temporary row to
12344 avoid destroying the first tool-bar row. */
12345 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12346
12347 /* Initialize an iterator for iteration over
12348 F->desired_tool_bar_string in the tool-bar window of frame F. */
12349 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12350 temp_row->reversed_p = false;
12351 it.first_visible_x = 0;
12352 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12353 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12354 it.paragraph_embedding = L2R;
12355
12356 while (!ITERATOR_AT_END_P (&it))
12357 {
12358 clear_glyph_row (temp_row);
12359 it.glyph_row = temp_row;
12360 display_tool_bar_line (&it, -1);
12361 }
12362 clear_glyph_row (temp_row);
12363
12364 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12365 if (n_rows)
12366 *n_rows = it.vpos > 0 ? it.vpos : -1;
12367
12368 if (pixelwise)
12369 return it.current_y;
12370 else
12371 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12372 }
12373
12374 #endif /* !USE_GTK && !HAVE_NS */
12375
12376 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12377 0, 2, 0,
12378 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12379 If FRAME is nil or omitted, use the selected frame. Optional argument
12380 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12381 (Lisp_Object frame, Lisp_Object pixelwise)
12382 {
12383 int height = 0;
12384
12385 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12386 struct frame *f = decode_any_frame (frame);
12387
12388 if (WINDOWP (f->tool_bar_window)
12389 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12390 {
12391 update_tool_bar (f, true);
12392 if (f->n_tool_bar_items)
12393 {
12394 build_desired_tool_bar_string (f);
12395 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12396 }
12397 }
12398 #endif
12399
12400 return make_number (height);
12401 }
12402
12403
12404 /* Display the tool-bar of frame F. Value is true if tool-bar's
12405 height should be changed. */
12406 static bool
12407 redisplay_tool_bar (struct frame *f)
12408 {
12409 f->tool_bar_redisplayed = true;
12410 #if defined (USE_GTK) || defined (HAVE_NS)
12411
12412 if (FRAME_EXTERNAL_TOOL_BAR (f))
12413 update_frame_tool_bar (f);
12414 return false;
12415
12416 #else /* !USE_GTK && !HAVE_NS */
12417
12418 struct window *w;
12419 struct it it;
12420 struct glyph_row *row;
12421
12422 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12423 do anything. This means you must start with tool-bar-lines
12424 non-zero to get the auto-sizing effect. Or in other words, you
12425 can turn off tool-bars by specifying tool-bar-lines zero. */
12426 if (!WINDOWP (f->tool_bar_window)
12427 || (w = XWINDOW (f->tool_bar_window),
12428 WINDOW_TOTAL_LINES (w) == 0))
12429 return false;
12430
12431 /* Set up an iterator for the tool-bar window. */
12432 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12433 it.first_visible_x = 0;
12434 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12435 row = it.glyph_row;
12436 row->reversed_p = false;
12437
12438 /* Build a string that represents the contents of the tool-bar. */
12439 build_desired_tool_bar_string (f);
12440 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12441 /* FIXME: This should be controlled by a user option. But it
12442 doesn't make sense to have an R2L tool bar if the menu bar cannot
12443 be drawn also R2L, and making the menu bar R2L is tricky due
12444 toolkit-specific code that implements it. If an R2L tool bar is
12445 ever supported, display_tool_bar_line should also be augmented to
12446 call unproduce_glyphs like display_line and display_string
12447 do. */
12448 it.paragraph_embedding = L2R;
12449
12450 if (f->n_tool_bar_rows == 0)
12451 {
12452 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12453
12454 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12455 {
12456 x_change_tool_bar_height (f, new_height);
12457 frame_default_tool_bar_height = new_height;
12458 /* Always do that now. */
12459 clear_glyph_matrix (w->desired_matrix);
12460 f->fonts_changed = true;
12461 return true;
12462 }
12463 }
12464
12465 /* Display as many lines as needed to display all tool-bar items. */
12466
12467 if (f->n_tool_bar_rows > 0)
12468 {
12469 int border, rows, height, extra;
12470
12471 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12472 border = XINT (Vtool_bar_border);
12473 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12474 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12475 else if (EQ (Vtool_bar_border, Qborder_width))
12476 border = f->border_width;
12477 else
12478 border = 0;
12479 if (border < 0)
12480 border = 0;
12481
12482 rows = f->n_tool_bar_rows;
12483 height = max (1, (it.last_visible_y - border) / rows);
12484 extra = it.last_visible_y - border - height * rows;
12485
12486 while (it.current_y < it.last_visible_y)
12487 {
12488 int h = 0;
12489 if (extra > 0 && rows-- > 0)
12490 {
12491 h = (extra + rows - 1) / rows;
12492 extra -= h;
12493 }
12494 display_tool_bar_line (&it, height + h);
12495 }
12496 }
12497 else
12498 {
12499 while (it.current_y < it.last_visible_y)
12500 display_tool_bar_line (&it, 0);
12501 }
12502
12503 /* It doesn't make much sense to try scrolling in the tool-bar
12504 window, so don't do it. */
12505 w->desired_matrix->no_scrolling_p = true;
12506 w->must_be_updated_p = true;
12507
12508 if (!NILP (Vauto_resize_tool_bars))
12509 {
12510 bool change_height_p = true;
12511
12512 /* If we couldn't display everything, change the tool-bar's
12513 height if there is room for more. */
12514 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12515 change_height_p = true;
12516
12517 /* We subtract 1 because display_tool_bar_line advances the
12518 glyph_row pointer before returning to its caller. We want to
12519 examine the last glyph row produced by
12520 display_tool_bar_line. */
12521 row = it.glyph_row - 1;
12522
12523 /* If there are blank lines at the end, except for a partially
12524 visible blank line at the end that is smaller than
12525 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12526 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12527 && row->height >= FRAME_LINE_HEIGHT (f))
12528 change_height_p = true;
12529
12530 /* If row displays tool-bar items, but is partially visible,
12531 change the tool-bar's height. */
12532 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12533 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12534 change_height_p = true;
12535
12536 /* Resize windows as needed by changing the `tool-bar-lines'
12537 frame parameter. */
12538 if (change_height_p)
12539 {
12540 int nrows;
12541 int new_height = tool_bar_height (f, &nrows, true);
12542
12543 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12544 && !f->minimize_tool_bar_window_p)
12545 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12546 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12547 f->minimize_tool_bar_window_p = false;
12548
12549 if (change_height_p)
12550 {
12551 x_change_tool_bar_height (f, new_height);
12552 frame_default_tool_bar_height = new_height;
12553 clear_glyph_matrix (w->desired_matrix);
12554 f->n_tool_bar_rows = nrows;
12555 f->fonts_changed = true;
12556
12557 return true;
12558 }
12559 }
12560 }
12561
12562 f->minimize_tool_bar_window_p = false;
12563 return false;
12564
12565 #endif /* USE_GTK || HAVE_NS */
12566 }
12567
12568 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12569
12570 /* Get information about the tool-bar item which is displayed in GLYPH
12571 on frame F. Return in *PROP_IDX the index where tool-bar item
12572 properties start in F->tool_bar_items. Value is false if
12573 GLYPH doesn't display a tool-bar item. */
12574
12575 static bool
12576 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12577 {
12578 Lisp_Object prop;
12579 int charpos;
12580
12581 /* This function can be called asynchronously, which means we must
12582 exclude any possibility that Fget_text_property signals an
12583 error. */
12584 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12585 charpos = max (0, charpos);
12586
12587 /* Get the text property `menu-item' at pos. The value of that
12588 property is the start index of this item's properties in
12589 F->tool_bar_items. */
12590 prop = Fget_text_property (make_number (charpos),
12591 Qmenu_item, f->current_tool_bar_string);
12592 if (! INTEGERP (prop))
12593 return false;
12594 *prop_idx = XINT (prop);
12595 return true;
12596 }
12597
12598 \f
12599 /* Get information about the tool-bar item at position X/Y on frame F.
12600 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12601 the current matrix of the tool-bar window of F, or NULL if not
12602 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12603 item in F->tool_bar_items. Value is
12604
12605 -1 if X/Y is not on a tool-bar item
12606 0 if X/Y is on the same item that was highlighted before.
12607 1 otherwise. */
12608
12609 static int
12610 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12611 int *hpos, int *vpos, int *prop_idx)
12612 {
12613 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12614 struct window *w = XWINDOW (f->tool_bar_window);
12615 int area;
12616
12617 /* Find the glyph under X/Y. */
12618 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12619 if (*glyph == NULL)
12620 return -1;
12621
12622 /* Get the start of this tool-bar item's properties in
12623 f->tool_bar_items. */
12624 if (!tool_bar_item_info (f, *glyph, prop_idx))
12625 return -1;
12626
12627 /* Is mouse on the highlighted item? */
12628 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12629 && *vpos >= hlinfo->mouse_face_beg_row
12630 && *vpos <= hlinfo->mouse_face_end_row
12631 && (*vpos > hlinfo->mouse_face_beg_row
12632 || *hpos >= hlinfo->mouse_face_beg_col)
12633 && (*vpos < hlinfo->mouse_face_end_row
12634 || *hpos < hlinfo->mouse_face_end_col
12635 || hlinfo->mouse_face_past_end))
12636 return 0;
12637
12638 return 1;
12639 }
12640
12641
12642 /* EXPORT:
12643 Handle mouse button event on the tool-bar of frame F, at
12644 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12645 false for button release. MODIFIERS is event modifiers for button
12646 release. */
12647
12648 void
12649 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12650 int modifiers)
12651 {
12652 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12653 struct window *w = XWINDOW (f->tool_bar_window);
12654 int hpos, vpos, prop_idx;
12655 struct glyph *glyph;
12656 Lisp_Object enabled_p;
12657 int ts;
12658
12659 /* If not on the highlighted tool-bar item, and mouse-highlight is
12660 non-nil, return. This is so we generate the tool-bar button
12661 click only when the mouse button is released on the same item as
12662 where it was pressed. However, when mouse-highlight is disabled,
12663 generate the click when the button is released regardless of the
12664 highlight, since tool-bar items are not highlighted in that
12665 case. */
12666 frame_to_window_pixel_xy (w, &x, &y);
12667 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12668 if (ts == -1
12669 || (ts != 0 && !NILP (Vmouse_highlight)))
12670 return;
12671
12672 /* When mouse-highlight is off, generate the click for the item
12673 where the button was pressed, disregarding where it was
12674 released. */
12675 if (NILP (Vmouse_highlight) && !down_p)
12676 prop_idx = f->last_tool_bar_item;
12677
12678 /* If item is disabled, do nothing. */
12679 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12680 if (NILP (enabled_p))
12681 return;
12682
12683 if (down_p)
12684 {
12685 /* Show item in pressed state. */
12686 if (!NILP (Vmouse_highlight))
12687 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12688 f->last_tool_bar_item = prop_idx;
12689 }
12690 else
12691 {
12692 Lisp_Object key, frame;
12693 struct input_event event;
12694 EVENT_INIT (event);
12695
12696 /* Show item in released state. */
12697 if (!NILP (Vmouse_highlight))
12698 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12699
12700 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12701
12702 XSETFRAME (frame, f);
12703 event.kind = TOOL_BAR_EVENT;
12704 event.frame_or_window = frame;
12705 event.arg = frame;
12706 kbd_buffer_store_event (&event);
12707
12708 event.kind = TOOL_BAR_EVENT;
12709 event.frame_or_window = frame;
12710 event.arg = key;
12711 event.modifiers = modifiers;
12712 kbd_buffer_store_event (&event);
12713 f->last_tool_bar_item = -1;
12714 }
12715 }
12716
12717
12718 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12719 tool-bar window-relative coordinates X/Y. Called from
12720 note_mouse_highlight. */
12721
12722 static void
12723 note_tool_bar_highlight (struct frame *f, int x, int y)
12724 {
12725 Lisp_Object window = f->tool_bar_window;
12726 struct window *w = XWINDOW (window);
12727 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12728 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12729 int hpos, vpos;
12730 struct glyph *glyph;
12731 struct glyph_row *row;
12732 int i;
12733 Lisp_Object enabled_p;
12734 int prop_idx;
12735 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12736 bool mouse_down_p;
12737 int rc;
12738
12739 /* Function note_mouse_highlight is called with negative X/Y
12740 values when mouse moves outside of the frame. */
12741 if (x <= 0 || y <= 0)
12742 {
12743 clear_mouse_face (hlinfo);
12744 return;
12745 }
12746
12747 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12748 if (rc < 0)
12749 {
12750 /* Not on tool-bar item. */
12751 clear_mouse_face (hlinfo);
12752 return;
12753 }
12754 else if (rc == 0)
12755 /* On same tool-bar item as before. */
12756 goto set_help_echo;
12757
12758 clear_mouse_face (hlinfo);
12759
12760 /* Mouse is down, but on different tool-bar item? */
12761 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12762 && f == dpyinfo->last_mouse_frame);
12763
12764 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12765 return;
12766
12767 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12768
12769 /* If tool-bar item is not enabled, don't highlight it. */
12770 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12771 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12772 {
12773 /* Compute the x-position of the glyph. In front and past the
12774 image is a space. We include this in the highlighted area. */
12775 row = MATRIX_ROW (w->current_matrix, vpos);
12776 for (i = x = 0; i < hpos; ++i)
12777 x += row->glyphs[TEXT_AREA][i].pixel_width;
12778
12779 /* Record this as the current active region. */
12780 hlinfo->mouse_face_beg_col = hpos;
12781 hlinfo->mouse_face_beg_row = vpos;
12782 hlinfo->mouse_face_beg_x = x;
12783 hlinfo->mouse_face_past_end = false;
12784
12785 hlinfo->mouse_face_end_col = hpos + 1;
12786 hlinfo->mouse_face_end_row = vpos;
12787 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12788 hlinfo->mouse_face_window = window;
12789 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12790
12791 /* Display it as active. */
12792 show_mouse_face (hlinfo, draw);
12793 }
12794
12795 set_help_echo:
12796
12797 /* Set help_echo_string to a help string to display for this tool-bar item.
12798 XTread_socket does the rest. */
12799 help_echo_object = help_echo_window = Qnil;
12800 help_echo_pos = -1;
12801 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12802 if (NILP (help_echo_string))
12803 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12804 }
12805
12806 #endif /* !USE_GTK && !HAVE_NS */
12807
12808 #endif /* HAVE_WINDOW_SYSTEM */
12809
12810
12811 \f
12812 /************************************************************************
12813 Horizontal scrolling
12814 ************************************************************************/
12815
12816 /* For all leaf windows in the window tree rooted at WINDOW, set their
12817 hscroll value so that PT is (i) visible in the window, and (ii) so
12818 that it is not within a certain margin at the window's left and
12819 right border. Value is true if any window's hscroll has been
12820 changed. */
12821
12822 static bool
12823 hscroll_window_tree (Lisp_Object window)
12824 {
12825 bool hscrolled_p = false;
12826 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12827 int hscroll_step_abs = 0;
12828 double hscroll_step_rel = 0;
12829
12830 if (hscroll_relative_p)
12831 {
12832 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12833 if (hscroll_step_rel < 0)
12834 {
12835 hscroll_relative_p = false;
12836 hscroll_step_abs = 0;
12837 }
12838 }
12839 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12840 {
12841 hscroll_step_abs = XINT (Vhscroll_step);
12842 if (hscroll_step_abs < 0)
12843 hscroll_step_abs = 0;
12844 }
12845 else
12846 hscroll_step_abs = 0;
12847
12848 while (WINDOWP (window))
12849 {
12850 struct window *w = XWINDOW (window);
12851
12852 if (WINDOWP (w->contents))
12853 hscrolled_p |= hscroll_window_tree (w->contents);
12854 else if (w->cursor.vpos >= 0)
12855 {
12856 int h_margin;
12857 int text_area_width;
12858 struct glyph_row *cursor_row;
12859 struct glyph_row *bottom_row;
12860
12861 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12862 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12863 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12864 else
12865 cursor_row = bottom_row - 1;
12866
12867 if (!cursor_row->enabled_p)
12868 {
12869 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12870 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12871 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12872 else
12873 cursor_row = bottom_row - 1;
12874 }
12875 bool row_r2l_p = cursor_row->reversed_p;
12876
12877 text_area_width = window_box_width (w, TEXT_AREA);
12878
12879 /* Scroll when cursor is inside this scroll margin. */
12880 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12881
12882 /* If the position of this window's point has explicitly
12883 changed, no more suspend auto hscrolling. */
12884 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12885 w->suspend_auto_hscroll = false;
12886
12887 /* Remember window point. */
12888 Fset_marker (w->old_pointm,
12889 ((w == XWINDOW (selected_window))
12890 ? make_number (BUF_PT (XBUFFER (w->contents)))
12891 : Fmarker_position (w->pointm)),
12892 w->contents);
12893
12894 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12895 && !w->suspend_auto_hscroll
12896 /* In some pathological cases, like restoring a window
12897 configuration into a frame that is much smaller than
12898 the one from which the configuration was saved, we
12899 get glyph rows whose start and end have zero buffer
12900 positions, which we cannot handle below. Just skip
12901 such windows. */
12902 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12903 /* For left-to-right rows, hscroll when cursor is either
12904 (i) inside the right hscroll margin, or (ii) if it is
12905 inside the left margin and the window is already
12906 hscrolled. */
12907 && ((!row_r2l_p
12908 && ((w->hscroll && w->cursor.x <= h_margin)
12909 || (cursor_row->enabled_p
12910 && cursor_row->truncated_on_right_p
12911 && (w->cursor.x >= text_area_width - h_margin))))
12912 /* For right-to-left rows, the logic is similar,
12913 except that rules for scrolling to left and right
12914 are reversed. E.g., if cursor.x <= h_margin, we
12915 need to hscroll "to the right" unconditionally,
12916 and that will scroll the screen to the left so as
12917 to reveal the next portion of the row. */
12918 || (row_r2l_p
12919 && ((cursor_row->enabled_p
12920 /* FIXME: It is confusing to set the
12921 truncated_on_right_p flag when R2L rows
12922 are actually truncated on the left. */
12923 && cursor_row->truncated_on_right_p
12924 && w->cursor.x <= h_margin)
12925 || (w->hscroll
12926 && (w->cursor.x >= text_area_width - h_margin))))))
12927 {
12928 struct it it;
12929 ptrdiff_t hscroll;
12930 struct buffer *saved_current_buffer;
12931 ptrdiff_t pt;
12932 int wanted_x;
12933
12934 /* Find point in a display of infinite width. */
12935 saved_current_buffer = current_buffer;
12936 current_buffer = XBUFFER (w->contents);
12937
12938 if (w == XWINDOW (selected_window))
12939 pt = PT;
12940 else
12941 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12942
12943 /* Move iterator to pt starting at cursor_row->start in
12944 a line with infinite width. */
12945 init_to_row_start (&it, w, cursor_row);
12946 it.last_visible_x = INFINITY;
12947 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12948 current_buffer = saved_current_buffer;
12949
12950 /* Position cursor in window. */
12951 if (!hscroll_relative_p && hscroll_step_abs == 0)
12952 hscroll = max (0, (it.current_x
12953 - (ITERATOR_AT_END_OF_LINE_P (&it)
12954 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12955 : (text_area_width / 2))))
12956 / FRAME_COLUMN_WIDTH (it.f);
12957 else if ((!row_r2l_p
12958 && w->cursor.x >= text_area_width - h_margin)
12959 || (row_r2l_p && w->cursor.x <= h_margin))
12960 {
12961 if (hscroll_relative_p)
12962 wanted_x = text_area_width * (1 - hscroll_step_rel)
12963 - h_margin;
12964 else
12965 wanted_x = text_area_width
12966 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12967 - h_margin;
12968 hscroll
12969 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12970 }
12971 else
12972 {
12973 if (hscroll_relative_p)
12974 wanted_x = text_area_width * hscroll_step_rel
12975 + h_margin;
12976 else
12977 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12978 + h_margin;
12979 hscroll
12980 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12981 }
12982 hscroll = max (hscroll, w->min_hscroll);
12983
12984 /* Don't prevent redisplay optimizations if hscroll
12985 hasn't changed, as it will unnecessarily slow down
12986 redisplay. */
12987 if (w->hscroll != hscroll)
12988 {
12989 struct buffer *b = XBUFFER (w->contents);
12990 b->prevent_redisplay_optimizations_p = true;
12991 w->hscroll = hscroll;
12992 hscrolled_p = true;
12993 }
12994 }
12995 }
12996
12997 window = w->next;
12998 }
12999
13000 /* Value is true if hscroll of any leaf window has been changed. */
13001 return hscrolled_p;
13002 }
13003
13004
13005 /* Set hscroll so that cursor is visible and not inside horizontal
13006 scroll margins for all windows in the tree rooted at WINDOW. See
13007 also hscroll_window_tree above. Value is true if any window's
13008 hscroll has been changed. If it has, desired matrices on the frame
13009 of WINDOW are cleared. */
13010
13011 static bool
13012 hscroll_windows (Lisp_Object window)
13013 {
13014 bool hscrolled_p = hscroll_window_tree (window);
13015 if (hscrolled_p)
13016 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13017 return hscrolled_p;
13018 }
13019
13020
13021 \f
13022 /************************************************************************
13023 Redisplay
13024 ************************************************************************/
13025
13026 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13027 This is sometimes handy to have in a debugger session. */
13028
13029 #ifdef GLYPH_DEBUG
13030
13031 /* First and last unchanged row for try_window_id. */
13032
13033 static int debug_first_unchanged_at_end_vpos;
13034 static int debug_last_unchanged_at_beg_vpos;
13035
13036 /* Delta vpos and y. */
13037
13038 static int debug_dvpos, debug_dy;
13039
13040 /* Delta in characters and bytes for try_window_id. */
13041
13042 static ptrdiff_t debug_delta, debug_delta_bytes;
13043
13044 /* Values of window_end_pos and window_end_vpos at the end of
13045 try_window_id. */
13046
13047 static ptrdiff_t debug_end_vpos;
13048
13049 /* Append a string to W->desired_matrix->method. FMT is a printf
13050 format string. If trace_redisplay_p is true also printf the
13051 resulting string to stderr. */
13052
13053 static void debug_method_add (struct window *, char const *, ...)
13054 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13055
13056 static void
13057 debug_method_add (struct window *w, char const *fmt, ...)
13058 {
13059 void *ptr = w;
13060 char *method = w->desired_matrix->method;
13061 int len = strlen (method);
13062 int size = sizeof w->desired_matrix->method;
13063 int remaining = size - len - 1;
13064 va_list ap;
13065
13066 if (len && remaining)
13067 {
13068 method[len] = '|';
13069 --remaining, ++len;
13070 }
13071
13072 va_start (ap, fmt);
13073 vsnprintf (method + len, remaining + 1, fmt, ap);
13074 va_end (ap);
13075
13076 if (trace_redisplay_p)
13077 fprintf (stderr, "%p (%s): %s\n",
13078 ptr,
13079 ((BUFFERP (w->contents)
13080 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13081 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13082 : "no buffer"),
13083 method + len);
13084 }
13085
13086 #endif /* GLYPH_DEBUG */
13087
13088
13089 /* Value is true if all changes in window W, which displays
13090 current_buffer, are in the text between START and END. START is a
13091 buffer position, END is given as a distance from Z. Used in
13092 redisplay_internal for display optimization. */
13093
13094 static bool
13095 text_outside_line_unchanged_p (struct window *w,
13096 ptrdiff_t start, ptrdiff_t end)
13097 {
13098 bool unchanged_p = true;
13099
13100 /* If text or overlays have changed, see where. */
13101 if (window_outdated (w))
13102 {
13103 /* Gap in the line? */
13104 if (GPT < start || Z - GPT < end)
13105 unchanged_p = false;
13106
13107 /* Changes start in front of the line, or end after it? */
13108 if (unchanged_p
13109 && (BEG_UNCHANGED < start - 1
13110 || END_UNCHANGED < end))
13111 unchanged_p = false;
13112
13113 /* If selective display, can't optimize if changes start at the
13114 beginning of the line. */
13115 if (unchanged_p
13116 && INTEGERP (BVAR (current_buffer, selective_display))
13117 && XINT (BVAR (current_buffer, selective_display)) > 0
13118 && (BEG_UNCHANGED < start || GPT <= start))
13119 unchanged_p = false;
13120
13121 /* If there are overlays at the start or end of the line, these
13122 may have overlay strings with newlines in them. A change at
13123 START, for instance, may actually concern the display of such
13124 overlay strings as well, and they are displayed on different
13125 lines. So, quickly rule out this case. (For the future, it
13126 might be desirable to implement something more telling than
13127 just BEG/END_UNCHANGED.) */
13128 if (unchanged_p)
13129 {
13130 if (BEG + BEG_UNCHANGED == start
13131 && overlay_touches_p (start))
13132 unchanged_p = false;
13133 if (END_UNCHANGED == end
13134 && overlay_touches_p (Z - end))
13135 unchanged_p = false;
13136 }
13137
13138 /* Under bidi reordering, adding or deleting a character in the
13139 beginning of a paragraph, before the first strong directional
13140 character, can change the base direction of the paragraph (unless
13141 the buffer specifies a fixed paragraph direction), which will
13142 require to redisplay the whole paragraph. It might be worthwhile
13143 to find the paragraph limits and widen the range of redisplayed
13144 lines to that, but for now just give up this optimization. */
13145 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13146 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13147 unchanged_p = false;
13148 }
13149
13150 return unchanged_p;
13151 }
13152
13153
13154 /* Do a frame update, taking possible shortcuts into account. This is
13155 the main external entry point for redisplay.
13156
13157 If the last redisplay displayed an echo area message and that message
13158 is no longer requested, we clear the echo area or bring back the
13159 mini-buffer if that is in use. */
13160
13161 void
13162 redisplay (void)
13163 {
13164 redisplay_internal ();
13165 }
13166
13167
13168 static Lisp_Object
13169 overlay_arrow_string_or_property (Lisp_Object var)
13170 {
13171 Lisp_Object val;
13172
13173 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13174 return val;
13175
13176 return Voverlay_arrow_string;
13177 }
13178
13179 /* Return true if there are any overlay-arrows in current_buffer. */
13180 static bool
13181 overlay_arrow_in_current_buffer_p (void)
13182 {
13183 Lisp_Object vlist;
13184
13185 for (vlist = Voverlay_arrow_variable_list;
13186 CONSP (vlist);
13187 vlist = XCDR (vlist))
13188 {
13189 Lisp_Object var = XCAR (vlist);
13190 Lisp_Object val;
13191
13192 if (!SYMBOLP (var))
13193 continue;
13194 val = find_symbol_value (var);
13195 if (MARKERP (val)
13196 && current_buffer == XMARKER (val)->buffer)
13197 return true;
13198 }
13199 return false;
13200 }
13201
13202
13203 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13204 has changed. */
13205
13206 static bool
13207 overlay_arrows_changed_p (void)
13208 {
13209 Lisp_Object vlist;
13210
13211 for (vlist = Voverlay_arrow_variable_list;
13212 CONSP (vlist);
13213 vlist = XCDR (vlist))
13214 {
13215 Lisp_Object var = XCAR (vlist);
13216 Lisp_Object val, pstr;
13217
13218 if (!SYMBOLP (var))
13219 continue;
13220 val = find_symbol_value (var);
13221 if (!MARKERP (val))
13222 continue;
13223 if (! EQ (COERCE_MARKER (val),
13224 Fget (var, Qlast_arrow_position))
13225 || ! (pstr = overlay_arrow_string_or_property (var),
13226 EQ (pstr, Fget (var, Qlast_arrow_string))))
13227 return true;
13228 }
13229 return false;
13230 }
13231
13232 /* Mark overlay arrows to be updated on next redisplay. */
13233
13234 static void
13235 update_overlay_arrows (int up_to_date)
13236 {
13237 Lisp_Object vlist;
13238
13239 for (vlist = Voverlay_arrow_variable_list;
13240 CONSP (vlist);
13241 vlist = XCDR (vlist))
13242 {
13243 Lisp_Object var = XCAR (vlist);
13244
13245 if (!SYMBOLP (var))
13246 continue;
13247
13248 if (up_to_date > 0)
13249 {
13250 Lisp_Object val = find_symbol_value (var);
13251 Fput (var, Qlast_arrow_position,
13252 COERCE_MARKER (val));
13253 Fput (var, Qlast_arrow_string,
13254 overlay_arrow_string_or_property (var));
13255 }
13256 else if (up_to_date < 0
13257 || !NILP (Fget (var, Qlast_arrow_position)))
13258 {
13259 Fput (var, Qlast_arrow_position, Qt);
13260 Fput (var, Qlast_arrow_string, Qt);
13261 }
13262 }
13263 }
13264
13265
13266 /* Return overlay arrow string to display at row.
13267 Return integer (bitmap number) for arrow bitmap in left fringe.
13268 Return nil if no overlay arrow. */
13269
13270 static Lisp_Object
13271 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13272 {
13273 Lisp_Object vlist;
13274
13275 for (vlist = Voverlay_arrow_variable_list;
13276 CONSP (vlist);
13277 vlist = XCDR (vlist))
13278 {
13279 Lisp_Object var = XCAR (vlist);
13280 Lisp_Object val;
13281
13282 if (!SYMBOLP (var))
13283 continue;
13284
13285 val = find_symbol_value (var);
13286
13287 if (MARKERP (val)
13288 && current_buffer == XMARKER (val)->buffer
13289 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13290 {
13291 if (FRAME_WINDOW_P (it->f)
13292 /* FIXME: if ROW->reversed_p is set, this should test
13293 the right fringe, not the left one. */
13294 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13295 {
13296 #ifdef HAVE_WINDOW_SYSTEM
13297 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13298 {
13299 int fringe_bitmap = lookup_fringe_bitmap (val);
13300 if (fringe_bitmap != 0)
13301 return make_number (fringe_bitmap);
13302 }
13303 #endif
13304 return make_number (-1); /* Use default arrow bitmap. */
13305 }
13306 return overlay_arrow_string_or_property (var);
13307 }
13308 }
13309
13310 return Qnil;
13311 }
13312
13313 /* Return true if point moved out of or into a composition. Otherwise
13314 return false. PREV_BUF and PREV_PT are the last point buffer and
13315 position. BUF and PT are the current point buffer and position. */
13316
13317 static bool
13318 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13319 struct buffer *buf, ptrdiff_t pt)
13320 {
13321 ptrdiff_t start, end;
13322 Lisp_Object prop;
13323 Lisp_Object buffer;
13324
13325 XSETBUFFER (buffer, buf);
13326 /* Check a composition at the last point if point moved within the
13327 same buffer. */
13328 if (prev_buf == buf)
13329 {
13330 if (prev_pt == pt)
13331 /* Point didn't move. */
13332 return false;
13333
13334 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13335 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13336 && composition_valid_p (start, end, prop)
13337 && start < prev_pt && end > prev_pt)
13338 /* The last point was within the composition. Return true iff
13339 point moved out of the composition. */
13340 return (pt <= start || pt >= end);
13341 }
13342
13343 /* Check a composition at the current point. */
13344 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13345 && find_composition (pt, -1, &start, &end, &prop, buffer)
13346 && composition_valid_p (start, end, prop)
13347 && start < pt && end > pt);
13348 }
13349
13350 /* Reconsider the clip changes of buffer which is displayed in W. */
13351
13352 static void
13353 reconsider_clip_changes (struct window *w)
13354 {
13355 struct buffer *b = XBUFFER (w->contents);
13356
13357 if (b->clip_changed
13358 && w->window_end_valid
13359 && w->current_matrix->buffer == b
13360 && w->current_matrix->zv == BUF_ZV (b)
13361 && w->current_matrix->begv == BUF_BEGV (b))
13362 b->clip_changed = false;
13363
13364 /* If display wasn't paused, and W is not a tool bar window, see if
13365 point has been moved into or out of a composition. In that case,
13366 set b->clip_changed to force updating the screen. If
13367 b->clip_changed has already been set, skip this check. */
13368 if (!b->clip_changed && w->window_end_valid)
13369 {
13370 ptrdiff_t pt = (w == XWINDOW (selected_window)
13371 ? PT : marker_position (w->pointm));
13372
13373 if ((w->current_matrix->buffer != b || pt != w->last_point)
13374 && check_point_in_composition (w->current_matrix->buffer,
13375 w->last_point, b, pt))
13376 b->clip_changed = true;
13377 }
13378 }
13379
13380 static void
13381 propagate_buffer_redisplay (void)
13382 { /* Resetting b->text->redisplay is problematic!
13383 We can't just reset it in the case that some window that displays
13384 it has not been redisplayed; and such a window can stay
13385 unredisplayed for a long time if it's currently invisible.
13386 But we do want to reset it at the end of redisplay otherwise
13387 its displayed windows will keep being redisplayed over and over
13388 again.
13389 So we copy all b->text->redisplay flags up to their windows here,
13390 such that mark_window_display_accurate can safely reset
13391 b->text->redisplay. */
13392 Lisp_Object ws = window_list ();
13393 for (; CONSP (ws); ws = XCDR (ws))
13394 {
13395 struct window *thisw = XWINDOW (XCAR (ws));
13396 struct buffer *thisb = XBUFFER (thisw->contents);
13397 if (thisb->text->redisplay)
13398 thisw->redisplay = true;
13399 }
13400 }
13401
13402 #define STOP_POLLING \
13403 do { if (! polling_stopped_here) stop_polling (); \
13404 polling_stopped_here = true; } while (false)
13405
13406 #define RESUME_POLLING \
13407 do { if (polling_stopped_here) start_polling (); \
13408 polling_stopped_here = false; } while (false)
13409
13410
13411 /* Perhaps in the future avoid recentering windows if it
13412 is not necessary; currently that causes some problems. */
13413
13414 static void
13415 redisplay_internal (void)
13416 {
13417 struct window *w = XWINDOW (selected_window);
13418 struct window *sw;
13419 struct frame *fr;
13420 bool pending;
13421 bool must_finish = false, match_p;
13422 struct text_pos tlbufpos, tlendpos;
13423 int number_of_visible_frames;
13424 ptrdiff_t count;
13425 struct frame *sf;
13426 bool polling_stopped_here = false;
13427 Lisp_Object tail, frame;
13428
13429 /* True means redisplay has to consider all windows on all
13430 frames. False, only selected_window is considered. */
13431 bool consider_all_windows_p;
13432
13433 /* True means redisplay has to redisplay the miniwindow. */
13434 bool update_miniwindow_p = false;
13435
13436 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13437
13438 /* No redisplay if running in batch mode or frame is not yet fully
13439 initialized, or redisplay is explicitly turned off by setting
13440 Vinhibit_redisplay. */
13441 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13442 || !NILP (Vinhibit_redisplay))
13443 return;
13444
13445 /* Don't examine these until after testing Vinhibit_redisplay.
13446 When Emacs is shutting down, perhaps because its connection to
13447 X has dropped, we should not look at them at all. */
13448 fr = XFRAME (w->frame);
13449 sf = SELECTED_FRAME ();
13450
13451 if (!fr->glyphs_initialized_p)
13452 return;
13453
13454 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13455 if (popup_activated ())
13456 return;
13457 #endif
13458
13459 /* I don't think this happens but let's be paranoid. */
13460 if (redisplaying_p)
13461 return;
13462
13463 /* Record a function that clears redisplaying_p
13464 when we leave this function. */
13465 count = SPECPDL_INDEX ();
13466 record_unwind_protect_void (unwind_redisplay);
13467 redisplaying_p = true;
13468 specbind (Qinhibit_free_realized_faces, Qnil);
13469
13470 /* Record this function, so it appears on the profiler's backtraces. */
13471 record_in_backtrace (Qredisplay_internal, 0, 0);
13472
13473 FOR_EACH_FRAME (tail, frame)
13474 XFRAME (frame)->already_hscrolled_p = false;
13475
13476 retry:
13477 /* Remember the currently selected window. */
13478 sw = w;
13479
13480 pending = false;
13481 forget_escape_and_glyphless_faces ();
13482
13483 inhibit_free_realized_faces = false;
13484
13485 /* If face_change, init_iterator will free all realized faces, which
13486 includes the faces referenced from current matrices. So, we
13487 can't reuse current matrices in this case. */
13488 if (face_change)
13489 windows_or_buffers_changed = 47;
13490
13491 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13492 && FRAME_TTY (sf)->previous_frame != sf)
13493 {
13494 /* Since frames on a single ASCII terminal share the same
13495 display area, displaying a different frame means redisplay
13496 the whole thing. */
13497 SET_FRAME_GARBAGED (sf);
13498 #ifndef DOS_NT
13499 set_tty_color_mode (FRAME_TTY (sf), sf);
13500 #endif
13501 FRAME_TTY (sf)->previous_frame = sf;
13502 }
13503
13504 /* Set the visible flags for all frames. Do this before checking for
13505 resized or garbaged frames; they want to know if their frames are
13506 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13507 number_of_visible_frames = 0;
13508
13509 FOR_EACH_FRAME (tail, frame)
13510 {
13511 struct frame *f = XFRAME (frame);
13512
13513 if (FRAME_VISIBLE_P (f))
13514 {
13515 ++number_of_visible_frames;
13516 /* Adjust matrices for visible frames only. */
13517 if (f->fonts_changed)
13518 {
13519 adjust_frame_glyphs (f);
13520 /* Disable all redisplay optimizations for this frame.
13521 This is because adjust_frame_glyphs resets the
13522 enabled_p flag for all glyph rows of all windows, so
13523 many optimizations will fail anyway, and some might
13524 fail to test that flag and do bogus things as
13525 result. */
13526 SET_FRAME_GARBAGED (f);
13527 f->fonts_changed = false;
13528 }
13529 /* If cursor type has been changed on the frame
13530 other than selected, consider all frames. */
13531 if (f != sf && f->cursor_type_changed)
13532 fset_redisplay (f);
13533 }
13534 clear_desired_matrices (f);
13535 }
13536
13537 /* Notice any pending interrupt request to change frame size. */
13538 do_pending_window_change (true);
13539
13540 /* do_pending_window_change could change the selected_window due to
13541 frame resizing which makes the selected window too small. */
13542 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13543 sw = w;
13544
13545 /* Clear frames marked as garbaged. */
13546 clear_garbaged_frames ();
13547
13548 /* Build menubar and tool-bar items. */
13549 if (NILP (Vmemory_full))
13550 prepare_menu_bars ();
13551
13552 reconsider_clip_changes (w);
13553
13554 /* In most cases selected window displays current buffer. */
13555 match_p = XBUFFER (w->contents) == current_buffer;
13556 if (match_p)
13557 {
13558 /* Detect case that we need to write or remove a star in the mode line. */
13559 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13560 w->update_mode_line = true;
13561
13562 if (mode_line_update_needed (w))
13563 w->update_mode_line = true;
13564
13565 /* If reconsider_clip_changes above decided that the narrowing
13566 in the current buffer changed, make sure all other windows
13567 showing that buffer will be redisplayed. */
13568 if (current_buffer->clip_changed)
13569 bset_update_mode_line (current_buffer);
13570 }
13571
13572 /* Normally the message* functions will have already displayed and
13573 updated the echo area, but the frame may have been trashed, or
13574 the update may have been preempted, so display the echo area
13575 again here. Checking message_cleared_p captures the case that
13576 the echo area should be cleared. */
13577 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13578 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13579 || (message_cleared_p
13580 && minibuf_level == 0
13581 /* If the mini-window is currently selected, this means the
13582 echo-area doesn't show through. */
13583 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13584 {
13585 echo_area_display (false);
13586
13587 /* If echo_area_display resizes the mini-window, the redisplay and
13588 window_sizes_changed flags of the selected frame are set, but
13589 it's too late for the hooks in window-size-change-functions,
13590 which have been examined already in prepare_menu_bars. So in
13591 that case we call the hooks here only for the selected frame. */
13592 if (sf->redisplay && FRAME_WINDOW_SIZES_CHANGED (sf))
13593 {
13594 Lisp_Object functions;
13595 ptrdiff_t count1 = SPECPDL_INDEX ();
13596
13597 record_unwind_save_match_data ();
13598
13599 /* Clear flag first in case we get an error below. */
13600 FRAME_WINDOW_SIZES_CHANGED (sf) = false;
13601 functions = Vwindow_size_change_functions;
13602
13603 while (CONSP (functions))
13604 {
13605 if (!EQ (XCAR (functions), Qt))
13606 call1 (XCAR (functions), selected_frame);
13607 functions = XCDR (functions);
13608 }
13609
13610 unbind_to (count1, Qnil);
13611 }
13612
13613 if (message_cleared_p)
13614 update_miniwindow_p = true;
13615
13616 must_finish = true;
13617
13618 /* If we don't display the current message, don't clear the
13619 message_cleared_p flag, because, if we did, we wouldn't clear
13620 the echo area in the next redisplay which doesn't preserve
13621 the echo area. */
13622 if (!display_last_displayed_message_p)
13623 message_cleared_p = false;
13624 }
13625 else if (EQ (selected_window, minibuf_window)
13626 && (current_buffer->clip_changed || window_outdated (w))
13627 && resize_mini_window (w, false))
13628 {
13629 if (sf->redisplay)
13630 {
13631 Lisp_Object functions;
13632 ptrdiff_t count1 = SPECPDL_INDEX ();
13633
13634 record_unwind_save_match_data ();
13635
13636 /* Clear flag first in case we get an error below. */
13637 FRAME_WINDOW_SIZES_CHANGED (sf) = false;
13638 functions = Vwindow_size_change_functions;
13639
13640 while (CONSP (functions))
13641 {
13642 if (!EQ (XCAR (functions), Qt))
13643 call1 (XCAR (functions), selected_frame);
13644 functions = XCDR (functions);
13645 }
13646
13647 unbind_to (count1, Qnil);
13648 }
13649
13650 /* Resized active mini-window to fit the size of what it is
13651 showing if its contents might have changed. */
13652 must_finish = true;
13653
13654 /* If window configuration was changed, frames may have been
13655 marked garbaged. Clear them or we will experience
13656 surprises wrt scrolling. */
13657 clear_garbaged_frames ();
13658 }
13659
13660 if (windows_or_buffers_changed && !update_mode_lines)
13661 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13662 only the windows's contents needs to be refreshed, or whether the
13663 mode-lines also need a refresh. */
13664 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13665 ? REDISPLAY_SOME : 32);
13666
13667 /* If specs for an arrow have changed, do thorough redisplay
13668 to ensure we remove any arrow that should no longer exist. */
13669 if (overlay_arrows_changed_p ())
13670 /* Apparently, this is the only case where we update other windows,
13671 without updating other mode-lines. */
13672 windows_or_buffers_changed = 49;
13673
13674 consider_all_windows_p = (update_mode_lines
13675 || windows_or_buffers_changed);
13676
13677 #define AINC(a,i) \
13678 { \
13679 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13680 if (INTEGERP (entry)) \
13681 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13682 }
13683
13684 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13685 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13686
13687 /* Optimize the case that only the line containing the cursor in the
13688 selected window has changed. Variables starting with this_ are
13689 set in display_line and record information about the line
13690 containing the cursor. */
13691 tlbufpos = this_line_start_pos;
13692 tlendpos = this_line_end_pos;
13693 if (!consider_all_windows_p
13694 && CHARPOS (tlbufpos) > 0
13695 && !w->update_mode_line
13696 && !current_buffer->clip_changed
13697 && !current_buffer->prevent_redisplay_optimizations_p
13698 && FRAME_VISIBLE_P (XFRAME (w->frame))
13699 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13700 && !XFRAME (w->frame)->cursor_type_changed
13701 && !XFRAME (w->frame)->face_change
13702 /* Make sure recorded data applies to current buffer, etc. */
13703 && this_line_buffer == current_buffer
13704 && match_p
13705 && !w->force_start
13706 && !w->optional_new_start
13707 /* Point must be on the line that we have info recorded about. */
13708 && PT >= CHARPOS (tlbufpos)
13709 && PT <= Z - CHARPOS (tlendpos)
13710 /* All text outside that line, including its final newline,
13711 must be unchanged. */
13712 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13713 CHARPOS (tlendpos)))
13714 {
13715 if (CHARPOS (tlbufpos) > BEGV
13716 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13717 && (CHARPOS (tlbufpos) == ZV
13718 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13719 /* Former continuation line has disappeared by becoming empty. */
13720 goto cancel;
13721 else if (window_outdated (w) || MINI_WINDOW_P (w))
13722 {
13723 /* We have to handle the case of continuation around a
13724 wide-column character (see the comment in indent.c around
13725 line 1340).
13726
13727 For instance, in the following case:
13728
13729 -------- Insert --------
13730 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13731 J_I_ ==> J_I_ `^^' are cursors.
13732 ^^ ^^
13733 -------- --------
13734
13735 As we have to redraw the line above, we cannot use this
13736 optimization. */
13737
13738 struct it it;
13739 int line_height_before = this_line_pixel_height;
13740
13741 /* Note that start_display will handle the case that the
13742 line starting at tlbufpos is a continuation line. */
13743 start_display (&it, w, tlbufpos);
13744
13745 /* Implementation note: It this still necessary? */
13746 if (it.current_x != this_line_start_x)
13747 goto cancel;
13748
13749 TRACE ((stderr, "trying display optimization 1\n"));
13750 w->cursor.vpos = -1;
13751 overlay_arrow_seen = false;
13752 it.vpos = this_line_vpos;
13753 it.current_y = this_line_y;
13754 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13755 display_line (&it);
13756
13757 /* If line contains point, is not continued,
13758 and ends at same distance from eob as before, we win. */
13759 if (w->cursor.vpos >= 0
13760 /* Line is not continued, otherwise this_line_start_pos
13761 would have been set to 0 in display_line. */
13762 && CHARPOS (this_line_start_pos)
13763 /* Line ends as before. */
13764 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13765 /* Line has same height as before. Otherwise other lines
13766 would have to be shifted up or down. */
13767 && this_line_pixel_height == line_height_before)
13768 {
13769 /* If this is not the window's last line, we must adjust
13770 the charstarts of the lines below. */
13771 if (it.current_y < it.last_visible_y)
13772 {
13773 struct glyph_row *row
13774 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13775 ptrdiff_t delta, delta_bytes;
13776
13777 /* We used to distinguish between two cases here,
13778 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13779 when the line ends in a newline or the end of the
13780 buffer's accessible portion. But both cases did
13781 the same, so they were collapsed. */
13782 delta = (Z
13783 - CHARPOS (tlendpos)
13784 - MATRIX_ROW_START_CHARPOS (row));
13785 delta_bytes = (Z_BYTE
13786 - BYTEPOS (tlendpos)
13787 - MATRIX_ROW_START_BYTEPOS (row));
13788
13789 increment_matrix_positions (w->current_matrix,
13790 this_line_vpos + 1,
13791 w->current_matrix->nrows,
13792 delta, delta_bytes);
13793 }
13794
13795 /* If this row displays text now but previously didn't,
13796 or vice versa, w->window_end_vpos may have to be
13797 adjusted. */
13798 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13799 {
13800 if (w->window_end_vpos < this_line_vpos)
13801 w->window_end_vpos = this_line_vpos;
13802 }
13803 else if (w->window_end_vpos == this_line_vpos
13804 && this_line_vpos > 0)
13805 w->window_end_vpos = this_line_vpos - 1;
13806 w->window_end_valid = false;
13807
13808 /* Update hint: No need to try to scroll in update_window. */
13809 w->desired_matrix->no_scrolling_p = true;
13810
13811 #ifdef GLYPH_DEBUG
13812 *w->desired_matrix->method = 0;
13813 debug_method_add (w, "optimization 1");
13814 #endif
13815 #ifdef HAVE_WINDOW_SYSTEM
13816 update_window_fringes (w, false);
13817 #endif
13818 goto update;
13819 }
13820 else
13821 goto cancel;
13822 }
13823 else if (/* Cursor position hasn't changed. */
13824 PT == w->last_point
13825 /* Make sure the cursor was last displayed
13826 in this window. Otherwise we have to reposition it. */
13827
13828 /* PXW: Must be converted to pixels, probably. */
13829 && 0 <= w->cursor.vpos
13830 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13831 {
13832 if (!must_finish)
13833 {
13834 do_pending_window_change (true);
13835 /* If selected_window changed, redisplay again. */
13836 if (WINDOWP (selected_window)
13837 && (w = XWINDOW (selected_window)) != sw)
13838 goto retry;
13839
13840 /* We used to always goto end_of_redisplay here, but this
13841 isn't enough if we have a blinking cursor. */
13842 if (w->cursor_off_p == w->last_cursor_off_p)
13843 goto end_of_redisplay;
13844 }
13845 goto update;
13846 }
13847 /* If highlighting the region, or if the cursor is in the echo area,
13848 then we can't just move the cursor. */
13849 else if (NILP (Vshow_trailing_whitespace)
13850 && !cursor_in_echo_area)
13851 {
13852 struct it it;
13853 struct glyph_row *row;
13854
13855 /* Skip from tlbufpos to PT and see where it is. Note that
13856 PT may be in invisible text. If so, we will end at the
13857 next visible position. */
13858 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13859 NULL, DEFAULT_FACE_ID);
13860 it.current_x = this_line_start_x;
13861 it.current_y = this_line_y;
13862 it.vpos = this_line_vpos;
13863
13864 /* The call to move_it_to stops in front of PT, but
13865 moves over before-strings. */
13866 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13867
13868 if (it.vpos == this_line_vpos
13869 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13870 row->enabled_p))
13871 {
13872 eassert (this_line_vpos == it.vpos);
13873 eassert (this_line_y == it.current_y);
13874 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13875 #ifdef GLYPH_DEBUG
13876 *w->desired_matrix->method = 0;
13877 debug_method_add (w, "optimization 3");
13878 #endif
13879 goto update;
13880 }
13881 else
13882 goto cancel;
13883 }
13884
13885 cancel:
13886 /* Text changed drastically or point moved off of line. */
13887 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13888 }
13889
13890 CHARPOS (this_line_start_pos) = 0;
13891 ++clear_face_cache_count;
13892 #ifdef HAVE_WINDOW_SYSTEM
13893 ++clear_image_cache_count;
13894 #endif
13895
13896 /* Build desired matrices, and update the display. If
13897 consider_all_windows_p, do it for all windows on all frames that
13898 require redisplay, as specified by their 'redisplay' flag.
13899 Otherwise do it for selected_window, only. */
13900
13901 if (consider_all_windows_p)
13902 {
13903 FOR_EACH_FRAME (tail, frame)
13904 XFRAME (frame)->updated_p = false;
13905
13906 propagate_buffer_redisplay ();
13907
13908 FOR_EACH_FRAME (tail, frame)
13909 {
13910 struct frame *f = XFRAME (frame);
13911
13912 /* We don't have to do anything for unselected terminal
13913 frames. */
13914 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13915 && !EQ (FRAME_TTY (f)->top_frame, frame))
13916 continue;
13917
13918 retry_frame:
13919 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13920 {
13921 bool gcscrollbars
13922 /* Only GC scrollbars when we redisplay the whole frame. */
13923 = f->redisplay || !REDISPLAY_SOME_P ();
13924 bool f_redisplay_flag = f->redisplay;
13925 /* Mark all the scroll bars to be removed; we'll redeem
13926 the ones we want when we redisplay their windows. */
13927 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13928 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13929
13930 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13931 redisplay_windows (FRAME_ROOT_WINDOW (f));
13932 /* Remember that the invisible frames need to be redisplayed next
13933 time they're visible. */
13934 else if (!REDISPLAY_SOME_P ())
13935 f->redisplay = true;
13936
13937 /* The X error handler may have deleted that frame. */
13938 if (!FRAME_LIVE_P (f))
13939 continue;
13940
13941 /* Any scroll bars which redisplay_windows should have
13942 nuked should now go away. */
13943 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13944 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13945
13946 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13947 {
13948 /* If fonts changed on visible frame, display again. */
13949 if (f->fonts_changed)
13950 {
13951 adjust_frame_glyphs (f);
13952 /* Disable all redisplay optimizations for this
13953 frame. For the reasons, see the comment near
13954 the previous call to adjust_frame_glyphs above. */
13955 SET_FRAME_GARBAGED (f);
13956 f->fonts_changed = false;
13957 goto retry_frame;
13958 }
13959
13960 /* See if we have to hscroll. */
13961 if (!f->already_hscrolled_p)
13962 {
13963 f->already_hscrolled_p = true;
13964 if (hscroll_windows (f->root_window))
13965 goto retry_frame;
13966 }
13967
13968 /* If the frame's redisplay flag was not set before
13969 we went about redisplaying its windows, but it is
13970 set now, that means we employed some redisplay
13971 optimizations inside redisplay_windows, and
13972 bypassed producing some screen lines. But if
13973 f->redisplay is now set, it might mean the old
13974 faces are no longer valid (e.g., if redisplaying
13975 some window called some Lisp which defined a new
13976 face or redefined an existing face), so trying to
13977 use them in update_frame will segfault.
13978 Therefore, we must redisplay this frame. */
13979 if (!f_redisplay_flag && f->redisplay)
13980 goto retry_frame;
13981
13982 /* Prevent various kinds of signals during display
13983 update. stdio is not robust about handling
13984 signals, which can cause an apparent I/O error. */
13985 if (interrupt_input)
13986 unrequest_sigio ();
13987 STOP_POLLING;
13988
13989 pending |= update_frame (f, false, false);
13990 f->cursor_type_changed = false;
13991 f->updated_p = true;
13992 }
13993 }
13994 }
13995
13996 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13997
13998 if (!pending)
13999 {
14000 /* Do the mark_window_display_accurate after all windows have
14001 been redisplayed because this call resets flags in buffers
14002 which are needed for proper redisplay. */
14003 FOR_EACH_FRAME (tail, frame)
14004 {
14005 struct frame *f = XFRAME (frame);
14006 if (f->updated_p)
14007 {
14008 f->redisplay = false;
14009 mark_window_display_accurate (f->root_window, true);
14010 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14011 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14012 }
14013 }
14014 }
14015 }
14016 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14017 {
14018 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14019 struct frame *mini_frame;
14020
14021 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14022 /* Use list_of_error, not Qerror, so that
14023 we catch only errors and don't run the debugger. */
14024 internal_condition_case_1 (redisplay_window_1, selected_window,
14025 list_of_error,
14026 redisplay_window_error);
14027 if (update_miniwindow_p)
14028 internal_condition_case_1 (redisplay_window_1, mini_window,
14029 list_of_error,
14030 redisplay_window_error);
14031
14032 /* Compare desired and current matrices, perform output. */
14033
14034 update:
14035 /* If fonts changed, display again. Likewise if redisplay_window_1
14036 above caused some change (e.g., a change in faces) that requires
14037 considering the entire frame again. */
14038 if (sf->fonts_changed || sf->redisplay)
14039 {
14040 if (sf->redisplay)
14041 {
14042 /* Set this to force a more thorough redisplay.
14043 Otherwise, we might immediately loop back to the
14044 above "else-if" clause (since all the conditions that
14045 led here might still be true), and we will then
14046 infloop, because the selected-frame's redisplay flag
14047 is not (and cannot be) reset. */
14048 windows_or_buffers_changed = 50;
14049 }
14050 goto retry;
14051 }
14052
14053 /* Prevent freeing of realized faces, since desired matrices are
14054 pending that reference the faces we computed and cached. */
14055 inhibit_free_realized_faces = true;
14056
14057 /* Prevent various kinds of signals during display update.
14058 stdio is not robust about handling signals,
14059 which can cause an apparent I/O error. */
14060 if (interrupt_input)
14061 unrequest_sigio ();
14062 STOP_POLLING;
14063
14064 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14065 {
14066 if (hscroll_windows (selected_window))
14067 goto retry;
14068
14069 XWINDOW (selected_window)->must_be_updated_p = true;
14070 pending = update_frame (sf, false, false);
14071 sf->cursor_type_changed = false;
14072 }
14073
14074 /* We may have called echo_area_display at the top of this
14075 function. If the echo area is on another frame, that may
14076 have put text on a frame other than the selected one, so the
14077 above call to update_frame would not have caught it. Catch
14078 it here. */
14079 mini_window = FRAME_MINIBUF_WINDOW (sf);
14080 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14081
14082 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14083 {
14084 XWINDOW (mini_window)->must_be_updated_p = true;
14085 pending |= update_frame (mini_frame, false, false);
14086 mini_frame->cursor_type_changed = false;
14087 if (!pending && hscroll_windows (mini_window))
14088 goto retry;
14089 }
14090 }
14091
14092 /* If display was paused because of pending input, make sure we do a
14093 thorough update the next time. */
14094 if (pending)
14095 {
14096 /* Prevent the optimization at the beginning of
14097 redisplay_internal that tries a single-line update of the
14098 line containing the cursor in the selected window. */
14099 CHARPOS (this_line_start_pos) = 0;
14100
14101 /* Let the overlay arrow be updated the next time. */
14102 update_overlay_arrows (0);
14103
14104 /* If we pause after scrolling, some rows in the current
14105 matrices of some windows are not valid. */
14106 if (!WINDOW_FULL_WIDTH_P (w)
14107 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14108 update_mode_lines = 36;
14109 }
14110 else
14111 {
14112 if (!consider_all_windows_p)
14113 {
14114 /* This has already been done above if
14115 consider_all_windows_p is set. */
14116 if (XBUFFER (w->contents)->text->redisplay
14117 && buffer_window_count (XBUFFER (w->contents)) > 1)
14118 /* This can happen if b->text->redisplay was set during
14119 jit-lock. */
14120 propagate_buffer_redisplay ();
14121 mark_window_display_accurate_1 (w, true);
14122
14123 /* Say overlay arrows are up to date. */
14124 update_overlay_arrows (1);
14125
14126 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14127 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14128 }
14129
14130 update_mode_lines = 0;
14131 windows_or_buffers_changed = 0;
14132 }
14133
14134 /* Start SIGIO interrupts coming again. Having them off during the
14135 code above makes it less likely one will discard output, but not
14136 impossible, since there might be stuff in the system buffer here.
14137 But it is much hairier to try to do anything about that. */
14138 if (interrupt_input)
14139 request_sigio ();
14140 RESUME_POLLING;
14141
14142 /* If a frame has become visible which was not before, redisplay
14143 again, so that we display it. Expose events for such a frame
14144 (which it gets when becoming visible) don't call the parts of
14145 redisplay constructing glyphs, so simply exposing a frame won't
14146 display anything in this case. So, we have to display these
14147 frames here explicitly. */
14148 if (!pending)
14149 {
14150 int new_count = 0;
14151
14152 FOR_EACH_FRAME (tail, frame)
14153 {
14154 if (XFRAME (frame)->visible)
14155 new_count++;
14156 }
14157
14158 if (new_count != number_of_visible_frames)
14159 windows_or_buffers_changed = 52;
14160 }
14161
14162 /* Change frame size now if a change is pending. */
14163 do_pending_window_change (true);
14164
14165 /* If we just did a pending size change, or have additional
14166 visible frames, or selected_window changed, redisplay again. */
14167 if ((windows_or_buffers_changed && !pending)
14168 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14169 goto retry;
14170
14171 /* Clear the face and image caches.
14172
14173 We used to do this only if consider_all_windows_p. But the cache
14174 needs to be cleared if a timer creates images in the current
14175 buffer (e.g. the test case in Bug#6230). */
14176
14177 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14178 {
14179 clear_face_cache (false);
14180 clear_face_cache_count = 0;
14181 }
14182
14183 #ifdef HAVE_WINDOW_SYSTEM
14184 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14185 {
14186 clear_image_caches (Qnil);
14187 clear_image_cache_count = 0;
14188 }
14189 #endif /* HAVE_WINDOW_SYSTEM */
14190
14191 end_of_redisplay:
14192 #ifdef HAVE_NS
14193 ns_set_doc_edited ();
14194 #endif
14195 if (interrupt_input && interrupts_deferred)
14196 request_sigio ();
14197
14198 unbind_to (count, Qnil);
14199 RESUME_POLLING;
14200 }
14201
14202
14203 /* Redisplay, but leave alone any recent echo area message unless
14204 another message has been requested in its place.
14205
14206 This is useful in situations where you need to redisplay but no
14207 user action has occurred, making it inappropriate for the message
14208 area to be cleared. See tracking_off and
14209 wait_reading_process_output for examples of these situations.
14210
14211 FROM_WHERE is an integer saying from where this function was
14212 called. This is useful for debugging. */
14213
14214 void
14215 redisplay_preserve_echo_area (int from_where)
14216 {
14217 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14218
14219 if (!NILP (echo_area_buffer[1]))
14220 {
14221 /* We have a previously displayed message, but no current
14222 message. Redisplay the previous message. */
14223 display_last_displayed_message_p = true;
14224 redisplay_internal ();
14225 display_last_displayed_message_p = false;
14226 }
14227 else
14228 redisplay_internal ();
14229
14230 flush_frame (SELECTED_FRAME ());
14231 }
14232
14233
14234 /* Function registered with record_unwind_protect in redisplay_internal. */
14235
14236 static void
14237 unwind_redisplay (void)
14238 {
14239 redisplaying_p = false;
14240 }
14241
14242
14243 /* Mark the display of leaf window W as accurate or inaccurate.
14244 If ACCURATE_P, mark display of W as accurate.
14245 If !ACCURATE_P, arrange for W to be redisplayed the next
14246 time redisplay_internal is called. */
14247
14248 static void
14249 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14250 {
14251 struct buffer *b = XBUFFER (w->contents);
14252
14253 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14254 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14255 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14256
14257 if (accurate_p)
14258 {
14259 b->clip_changed = false;
14260 b->prevent_redisplay_optimizations_p = false;
14261 eassert (buffer_window_count (b) > 0);
14262 /* Resetting b->text->redisplay is problematic!
14263 In order to make it safer to do it here, redisplay_internal must
14264 have copied all b->text->redisplay to their respective windows. */
14265 b->text->redisplay = false;
14266
14267 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14268 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14269 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14270 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14271
14272 w->current_matrix->buffer = b;
14273 w->current_matrix->begv = BUF_BEGV (b);
14274 w->current_matrix->zv = BUF_ZV (b);
14275
14276 w->last_cursor_vpos = w->cursor.vpos;
14277 w->last_cursor_off_p = w->cursor_off_p;
14278
14279 if (w == XWINDOW (selected_window))
14280 w->last_point = BUF_PT (b);
14281 else
14282 w->last_point = marker_position (w->pointm);
14283
14284 w->window_end_valid = true;
14285 w->update_mode_line = false;
14286 }
14287
14288 w->redisplay = !accurate_p;
14289 }
14290
14291
14292 /* Mark the display of windows in the window tree rooted at WINDOW as
14293 accurate or inaccurate. If ACCURATE_P, mark display of
14294 windows as accurate. If !ACCURATE_P, arrange for windows to
14295 be redisplayed the next time redisplay_internal is called. */
14296
14297 void
14298 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14299 {
14300 struct window *w;
14301
14302 for (; !NILP (window); window = w->next)
14303 {
14304 w = XWINDOW (window);
14305 if (WINDOWP (w->contents))
14306 mark_window_display_accurate (w->contents, accurate_p);
14307 else
14308 mark_window_display_accurate_1 (w, accurate_p);
14309 }
14310
14311 if (accurate_p)
14312 update_overlay_arrows (1);
14313 else
14314 /* Force a thorough redisplay the next time by setting
14315 last_arrow_position and last_arrow_string to t, which is
14316 unequal to any useful value of Voverlay_arrow_... */
14317 update_overlay_arrows (-1);
14318 }
14319
14320
14321 /* Return value in display table DP (Lisp_Char_Table *) for character
14322 C. Since a display table doesn't have any parent, we don't have to
14323 follow parent. Do not call this function directly but use the
14324 macro DISP_CHAR_VECTOR. */
14325
14326 Lisp_Object
14327 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14328 {
14329 Lisp_Object val;
14330
14331 if (ASCII_CHAR_P (c))
14332 {
14333 val = dp->ascii;
14334 if (SUB_CHAR_TABLE_P (val))
14335 val = XSUB_CHAR_TABLE (val)->contents[c];
14336 }
14337 else
14338 {
14339 Lisp_Object table;
14340
14341 XSETCHAR_TABLE (table, dp);
14342 val = char_table_ref (table, c);
14343 }
14344 if (NILP (val))
14345 val = dp->defalt;
14346 return val;
14347 }
14348
14349
14350 \f
14351 /***********************************************************************
14352 Window Redisplay
14353 ***********************************************************************/
14354
14355 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14356
14357 static void
14358 redisplay_windows (Lisp_Object window)
14359 {
14360 while (!NILP (window))
14361 {
14362 struct window *w = XWINDOW (window);
14363
14364 if (WINDOWP (w->contents))
14365 redisplay_windows (w->contents);
14366 else if (BUFFERP (w->contents))
14367 {
14368 displayed_buffer = XBUFFER (w->contents);
14369 /* Use list_of_error, not Qerror, so that
14370 we catch only errors and don't run the debugger. */
14371 internal_condition_case_1 (redisplay_window_0, window,
14372 list_of_error,
14373 redisplay_window_error);
14374 }
14375
14376 window = w->next;
14377 }
14378 }
14379
14380 static Lisp_Object
14381 redisplay_window_error (Lisp_Object ignore)
14382 {
14383 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14384 return Qnil;
14385 }
14386
14387 static Lisp_Object
14388 redisplay_window_0 (Lisp_Object window)
14389 {
14390 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14391 redisplay_window (window, false);
14392 return Qnil;
14393 }
14394
14395 static Lisp_Object
14396 redisplay_window_1 (Lisp_Object window)
14397 {
14398 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14399 redisplay_window (window, true);
14400 return Qnil;
14401 }
14402 \f
14403
14404 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14405 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14406 which positions recorded in ROW differ from current buffer
14407 positions.
14408
14409 Return true iff cursor is on this row. */
14410
14411 static bool
14412 set_cursor_from_row (struct window *w, struct glyph_row *row,
14413 struct glyph_matrix *matrix,
14414 ptrdiff_t delta, ptrdiff_t delta_bytes,
14415 int dy, int dvpos)
14416 {
14417 struct glyph *glyph = row->glyphs[TEXT_AREA];
14418 struct glyph *end = glyph + row->used[TEXT_AREA];
14419 struct glyph *cursor = NULL;
14420 /* The last known character position in row. */
14421 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14422 int x = row->x;
14423 ptrdiff_t pt_old = PT - delta;
14424 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14425 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14426 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14427 /* A glyph beyond the edge of TEXT_AREA which we should never
14428 touch. */
14429 struct glyph *glyphs_end = end;
14430 /* True means we've found a match for cursor position, but that
14431 glyph has the avoid_cursor_p flag set. */
14432 bool match_with_avoid_cursor = false;
14433 /* True means we've seen at least one glyph that came from a
14434 display string. */
14435 bool string_seen = false;
14436 /* Largest and smallest buffer positions seen so far during scan of
14437 glyph row. */
14438 ptrdiff_t bpos_max = pos_before;
14439 ptrdiff_t bpos_min = pos_after;
14440 /* Last buffer position covered by an overlay string with an integer
14441 `cursor' property. */
14442 ptrdiff_t bpos_covered = 0;
14443 /* True means the display string on which to display the cursor
14444 comes from a text property, not from an overlay. */
14445 bool string_from_text_prop = false;
14446
14447 /* Don't even try doing anything if called for a mode-line or
14448 header-line row, since the rest of the code isn't prepared to
14449 deal with such calamities. */
14450 eassert (!row->mode_line_p);
14451 if (row->mode_line_p)
14452 return false;
14453
14454 /* Skip over glyphs not having an object at the start and the end of
14455 the row. These are special glyphs like truncation marks on
14456 terminal frames. */
14457 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14458 {
14459 if (!row->reversed_p)
14460 {
14461 while (glyph < end
14462 && NILP (glyph->object)
14463 && glyph->charpos < 0)
14464 {
14465 x += glyph->pixel_width;
14466 ++glyph;
14467 }
14468 while (end > glyph
14469 && NILP ((end - 1)->object)
14470 /* CHARPOS is zero for blanks and stretch glyphs
14471 inserted by extend_face_to_end_of_line. */
14472 && (end - 1)->charpos <= 0)
14473 --end;
14474 glyph_before = glyph - 1;
14475 glyph_after = end;
14476 }
14477 else
14478 {
14479 struct glyph *g;
14480
14481 /* If the glyph row is reversed, we need to process it from back
14482 to front, so swap the edge pointers. */
14483 glyphs_end = end = glyph - 1;
14484 glyph += row->used[TEXT_AREA] - 1;
14485
14486 while (glyph > end + 1
14487 && NILP (glyph->object)
14488 && glyph->charpos < 0)
14489 {
14490 --glyph;
14491 x -= glyph->pixel_width;
14492 }
14493 if (NILP (glyph->object) && glyph->charpos < 0)
14494 --glyph;
14495 /* By default, in reversed rows we put the cursor on the
14496 rightmost (first in the reading order) glyph. */
14497 for (g = end + 1; g < glyph; g++)
14498 x += g->pixel_width;
14499 while (end < glyph
14500 && NILP ((end + 1)->object)
14501 && (end + 1)->charpos <= 0)
14502 ++end;
14503 glyph_before = glyph + 1;
14504 glyph_after = end;
14505 }
14506 }
14507 else if (row->reversed_p)
14508 {
14509 /* In R2L rows that don't display text, put the cursor on the
14510 rightmost glyph. Case in point: an empty last line that is
14511 part of an R2L paragraph. */
14512 cursor = end - 1;
14513 /* Avoid placing the cursor on the last glyph of the row, where
14514 on terminal frames we hold the vertical border between
14515 adjacent windows. */
14516 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14517 && !WINDOW_RIGHTMOST_P (w)
14518 && cursor == row->glyphs[LAST_AREA] - 1)
14519 cursor--;
14520 x = -1; /* will be computed below, at label compute_x */
14521 }
14522
14523 /* Step 1: Try to find the glyph whose character position
14524 corresponds to point. If that's not possible, find 2 glyphs
14525 whose character positions are the closest to point, one before
14526 point, the other after it. */
14527 if (!row->reversed_p)
14528 while (/* not marched to end of glyph row */
14529 glyph < end
14530 /* glyph was not inserted by redisplay for internal purposes */
14531 && !NILP (glyph->object))
14532 {
14533 if (BUFFERP (glyph->object))
14534 {
14535 ptrdiff_t dpos = glyph->charpos - pt_old;
14536
14537 if (glyph->charpos > bpos_max)
14538 bpos_max = glyph->charpos;
14539 if (glyph->charpos < bpos_min)
14540 bpos_min = glyph->charpos;
14541 if (!glyph->avoid_cursor_p)
14542 {
14543 /* If we hit point, we've found the glyph on which to
14544 display the cursor. */
14545 if (dpos == 0)
14546 {
14547 match_with_avoid_cursor = false;
14548 break;
14549 }
14550 /* See if we've found a better approximation to
14551 POS_BEFORE or to POS_AFTER. */
14552 if (0 > dpos && dpos > pos_before - pt_old)
14553 {
14554 pos_before = glyph->charpos;
14555 glyph_before = glyph;
14556 }
14557 else if (0 < dpos && dpos < pos_after - pt_old)
14558 {
14559 pos_after = glyph->charpos;
14560 glyph_after = glyph;
14561 }
14562 }
14563 else if (dpos == 0)
14564 match_with_avoid_cursor = true;
14565 }
14566 else if (STRINGP (glyph->object))
14567 {
14568 Lisp_Object chprop;
14569 ptrdiff_t glyph_pos = glyph->charpos;
14570
14571 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14572 glyph->object);
14573 if (!NILP (chprop))
14574 {
14575 /* If the string came from a `display' text property,
14576 look up the buffer position of that property and
14577 use that position to update bpos_max, as if we
14578 actually saw such a position in one of the row's
14579 glyphs. This helps with supporting integer values
14580 of `cursor' property on the display string in
14581 situations where most or all of the row's buffer
14582 text is completely covered by display properties,
14583 so that no glyph with valid buffer positions is
14584 ever seen in the row. */
14585 ptrdiff_t prop_pos =
14586 string_buffer_position_lim (glyph->object, pos_before,
14587 pos_after, false);
14588
14589 if (prop_pos >= pos_before)
14590 bpos_max = prop_pos;
14591 }
14592 if (INTEGERP (chprop))
14593 {
14594 bpos_covered = bpos_max + XINT (chprop);
14595 /* If the `cursor' property covers buffer positions up
14596 to and including point, we should display cursor on
14597 this glyph. Note that, if a `cursor' property on one
14598 of the string's characters has an integer value, we
14599 will break out of the loop below _before_ we get to
14600 the position match above. IOW, integer values of
14601 the `cursor' property override the "exact match for
14602 point" strategy of positioning the cursor. */
14603 /* Implementation note: bpos_max == pt_old when, e.g.,
14604 we are in an empty line, where bpos_max is set to
14605 MATRIX_ROW_START_CHARPOS, see above. */
14606 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14607 {
14608 cursor = glyph;
14609 break;
14610 }
14611 }
14612
14613 string_seen = true;
14614 }
14615 x += glyph->pixel_width;
14616 ++glyph;
14617 }
14618 else if (glyph > end) /* row is reversed */
14619 while (!NILP (glyph->object))
14620 {
14621 if (BUFFERP (glyph->object))
14622 {
14623 ptrdiff_t dpos = glyph->charpos - pt_old;
14624
14625 if (glyph->charpos > bpos_max)
14626 bpos_max = glyph->charpos;
14627 if (glyph->charpos < bpos_min)
14628 bpos_min = glyph->charpos;
14629 if (!glyph->avoid_cursor_p)
14630 {
14631 if (dpos == 0)
14632 {
14633 match_with_avoid_cursor = false;
14634 break;
14635 }
14636 if (0 > dpos && dpos > pos_before - pt_old)
14637 {
14638 pos_before = glyph->charpos;
14639 glyph_before = glyph;
14640 }
14641 else if (0 < dpos && dpos < pos_after - pt_old)
14642 {
14643 pos_after = glyph->charpos;
14644 glyph_after = glyph;
14645 }
14646 }
14647 else if (dpos == 0)
14648 match_with_avoid_cursor = true;
14649 }
14650 else if (STRINGP (glyph->object))
14651 {
14652 Lisp_Object chprop;
14653 ptrdiff_t glyph_pos = glyph->charpos;
14654
14655 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14656 glyph->object);
14657 if (!NILP (chprop))
14658 {
14659 ptrdiff_t prop_pos =
14660 string_buffer_position_lim (glyph->object, pos_before,
14661 pos_after, false);
14662
14663 if (prop_pos >= pos_before)
14664 bpos_max = prop_pos;
14665 }
14666 if (INTEGERP (chprop))
14667 {
14668 bpos_covered = bpos_max + XINT (chprop);
14669 /* If the `cursor' property covers buffer positions up
14670 to and including point, we should display cursor on
14671 this glyph. */
14672 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14673 {
14674 cursor = glyph;
14675 break;
14676 }
14677 }
14678 string_seen = true;
14679 }
14680 --glyph;
14681 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14682 {
14683 x--; /* can't use any pixel_width */
14684 break;
14685 }
14686 x -= glyph->pixel_width;
14687 }
14688
14689 /* Step 2: If we didn't find an exact match for point, we need to
14690 look for a proper place to put the cursor among glyphs between
14691 GLYPH_BEFORE and GLYPH_AFTER. */
14692 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14693 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14694 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14695 {
14696 /* An empty line has a single glyph whose OBJECT is nil and
14697 whose CHARPOS is the position of a newline on that line.
14698 Note that on a TTY, there are more glyphs after that, which
14699 were produced by extend_face_to_end_of_line, but their
14700 CHARPOS is zero or negative. */
14701 bool empty_line_p =
14702 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14703 && NILP (glyph->object) && glyph->charpos > 0
14704 /* On a TTY, continued and truncated rows also have a glyph at
14705 their end whose OBJECT is nil and whose CHARPOS is
14706 positive (the continuation and truncation glyphs), but such
14707 rows are obviously not "empty". */
14708 && !(row->continued_p || row->truncated_on_right_p));
14709
14710 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14711 {
14712 ptrdiff_t ellipsis_pos;
14713
14714 /* Scan back over the ellipsis glyphs. */
14715 if (!row->reversed_p)
14716 {
14717 ellipsis_pos = (glyph - 1)->charpos;
14718 while (glyph > row->glyphs[TEXT_AREA]
14719 && (glyph - 1)->charpos == ellipsis_pos)
14720 glyph--, x -= glyph->pixel_width;
14721 /* That loop always goes one position too far, including
14722 the glyph before the ellipsis. So scan forward over
14723 that one. */
14724 x += glyph->pixel_width;
14725 glyph++;
14726 }
14727 else /* row is reversed */
14728 {
14729 ellipsis_pos = (glyph + 1)->charpos;
14730 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14731 && (glyph + 1)->charpos == ellipsis_pos)
14732 glyph++, x += glyph->pixel_width;
14733 x -= glyph->pixel_width;
14734 glyph--;
14735 }
14736 }
14737 else if (match_with_avoid_cursor)
14738 {
14739 cursor = glyph_after;
14740 x = -1;
14741 }
14742 else if (string_seen)
14743 {
14744 int incr = row->reversed_p ? -1 : +1;
14745
14746 /* Need to find the glyph that came out of a string which is
14747 present at point. That glyph is somewhere between
14748 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14749 positioned between POS_BEFORE and POS_AFTER in the
14750 buffer. */
14751 struct glyph *start, *stop;
14752 ptrdiff_t pos = pos_before;
14753
14754 x = -1;
14755
14756 /* If the row ends in a newline from a display string,
14757 reordering could have moved the glyphs belonging to the
14758 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14759 in this case we extend the search to the last glyph in
14760 the row that was not inserted by redisplay. */
14761 if (row->ends_in_newline_from_string_p)
14762 {
14763 glyph_after = end;
14764 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14765 }
14766
14767 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14768 correspond to POS_BEFORE and POS_AFTER, respectively. We
14769 need START and STOP in the order that corresponds to the
14770 row's direction as given by its reversed_p flag. If the
14771 directionality of characters between POS_BEFORE and
14772 POS_AFTER is the opposite of the row's base direction,
14773 these characters will have been reordered for display,
14774 and we need to reverse START and STOP. */
14775 if (!row->reversed_p)
14776 {
14777 start = min (glyph_before, glyph_after);
14778 stop = max (glyph_before, glyph_after);
14779 }
14780 else
14781 {
14782 start = max (glyph_before, glyph_after);
14783 stop = min (glyph_before, glyph_after);
14784 }
14785 for (glyph = start + incr;
14786 row->reversed_p ? glyph > stop : glyph < stop; )
14787 {
14788
14789 /* Any glyphs that come from the buffer are here because
14790 of bidi reordering. Skip them, and only pay
14791 attention to glyphs that came from some string. */
14792 if (STRINGP (glyph->object))
14793 {
14794 Lisp_Object str;
14795 ptrdiff_t tem;
14796 /* If the display property covers the newline, we
14797 need to search for it one position farther. */
14798 ptrdiff_t lim = pos_after
14799 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14800
14801 string_from_text_prop = false;
14802 str = glyph->object;
14803 tem = string_buffer_position_lim (str, pos, lim, false);
14804 if (tem == 0 /* from overlay */
14805 || pos <= tem)
14806 {
14807 /* If the string from which this glyph came is
14808 found in the buffer at point, or at position
14809 that is closer to point than pos_after, then
14810 we've found the glyph we've been looking for.
14811 If it comes from an overlay (tem == 0), and
14812 it has the `cursor' property on one of its
14813 glyphs, record that glyph as a candidate for
14814 displaying the cursor. (As in the
14815 unidirectional version, we will display the
14816 cursor on the last candidate we find.) */
14817 if (tem == 0
14818 || tem == pt_old
14819 || (tem - pt_old > 0 && tem < pos_after))
14820 {
14821 /* The glyphs from this string could have
14822 been reordered. Find the one with the
14823 smallest string position. Or there could
14824 be a character in the string with the
14825 `cursor' property, which means display
14826 cursor on that character's glyph. */
14827 ptrdiff_t strpos = glyph->charpos;
14828
14829 if (tem)
14830 {
14831 cursor = glyph;
14832 string_from_text_prop = true;
14833 }
14834 for ( ;
14835 (row->reversed_p ? glyph > stop : glyph < stop)
14836 && EQ (glyph->object, str);
14837 glyph += incr)
14838 {
14839 Lisp_Object cprop;
14840 ptrdiff_t gpos = glyph->charpos;
14841
14842 cprop = Fget_char_property (make_number (gpos),
14843 Qcursor,
14844 glyph->object);
14845 if (!NILP (cprop))
14846 {
14847 cursor = glyph;
14848 break;
14849 }
14850 if (tem && glyph->charpos < strpos)
14851 {
14852 strpos = glyph->charpos;
14853 cursor = glyph;
14854 }
14855 }
14856
14857 if (tem == pt_old
14858 || (tem - pt_old > 0 && tem < pos_after))
14859 goto compute_x;
14860 }
14861 if (tem)
14862 pos = tem + 1; /* don't find previous instances */
14863 }
14864 /* This string is not what we want; skip all of the
14865 glyphs that came from it. */
14866 while ((row->reversed_p ? glyph > stop : glyph < stop)
14867 && EQ (glyph->object, str))
14868 glyph += incr;
14869 }
14870 else
14871 glyph += incr;
14872 }
14873
14874 /* If we reached the end of the line, and END was from a string,
14875 the cursor is not on this line. */
14876 if (cursor == NULL
14877 && (row->reversed_p ? glyph <= end : glyph >= end)
14878 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14879 && STRINGP (end->object)
14880 && row->continued_p)
14881 return false;
14882 }
14883 /* A truncated row may not include PT among its character positions.
14884 Setting the cursor inside the scroll margin will trigger
14885 recalculation of hscroll in hscroll_window_tree. But if a
14886 display string covers point, defer to the string-handling
14887 code below to figure this out. */
14888 else if (row->truncated_on_left_p && pt_old < bpos_min)
14889 {
14890 cursor = glyph_before;
14891 x = -1;
14892 }
14893 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14894 /* Zero-width characters produce no glyphs. */
14895 || (!empty_line_p
14896 && (row->reversed_p
14897 ? glyph_after > glyphs_end
14898 : glyph_after < glyphs_end)))
14899 {
14900 cursor = glyph_after;
14901 x = -1;
14902 }
14903 }
14904
14905 compute_x:
14906 if (cursor != NULL)
14907 glyph = cursor;
14908 else if (glyph == glyphs_end
14909 && pos_before == pos_after
14910 && STRINGP ((row->reversed_p
14911 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14912 : row->glyphs[TEXT_AREA])->object))
14913 {
14914 /* If all the glyphs of this row came from strings, put the
14915 cursor on the first glyph of the row. This avoids having the
14916 cursor outside of the text area in this very rare and hard
14917 use case. */
14918 glyph =
14919 row->reversed_p
14920 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14921 : row->glyphs[TEXT_AREA];
14922 }
14923 if (x < 0)
14924 {
14925 struct glyph *g;
14926
14927 /* Need to compute x that corresponds to GLYPH. */
14928 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14929 {
14930 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14931 emacs_abort ();
14932 x += g->pixel_width;
14933 }
14934 }
14935
14936 /* ROW could be part of a continued line, which, under bidi
14937 reordering, might have other rows whose start and end charpos
14938 occlude point. Only set w->cursor if we found a better
14939 approximation to the cursor position than we have from previously
14940 examined candidate rows belonging to the same continued line. */
14941 if (/* We already have a candidate row. */
14942 w->cursor.vpos >= 0
14943 /* That candidate is not the row we are processing. */
14944 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14945 /* Make sure cursor.vpos specifies a row whose start and end
14946 charpos occlude point, and it is valid candidate for being a
14947 cursor-row. This is because some callers of this function
14948 leave cursor.vpos at the row where the cursor was displayed
14949 during the last redisplay cycle. */
14950 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14951 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14952 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14953 {
14954 struct glyph *g1
14955 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14956
14957 /* Don't consider glyphs that are outside TEXT_AREA. */
14958 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14959 return false;
14960 /* Keep the candidate whose buffer position is the closest to
14961 point or has the `cursor' property. */
14962 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14963 w->cursor.hpos >= 0
14964 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14965 && ((BUFFERP (g1->object)
14966 && (g1->charpos == pt_old /* An exact match always wins. */
14967 || (BUFFERP (glyph->object)
14968 && eabs (g1->charpos - pt_old)
14969 < eabs (glyph->charpos - pt_old))))
14970 /* Previous candidate is a glyph from a string that has
14971 a non-nil `cursor' property. */
14972 || (STRINGP (g1->object)
14973 && (!NILP (Fget_char_property (make_number (g1->charpos),
14974 Qcursor, g1->object))
14975 /* Previous candidate is from the same display
14976 string as this one, and the display string
14977 came from a text property. */
14978 || (EQ (g1->object, glyph->object)
14979 && string_from_text_prop)
14980 /* this candidate is from newline and its
14981 position is not an exact match */
14982 || (NILP (glyph->object)
14983 && glyph->charpos != pt_old)))))
14984 return false;
14985 /* If this candidate gives an exact match, use that. */
14986 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14987 /* If this candidate is a glyph created for the
14988 terminating newline of a line, and point is on that
14989 newline, it wins because it's an exact match. */
14990 || (!row->continued_p
14991 && NILP (glyph->object)
14992 && glyph->charpos == 0
14993 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14994 /* Otherwise, keep the candidate that comes from a row
14995 spanning less buffer positions. This may win when one or
14996 both candidate positions are on glyphs that came from
14997 display strings, for which we cannot compare buffer
14998 positions. */
14999 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15000 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15001 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15002 return false;
15003 }
15004 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15005 w->cursor.x = x;
15006 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15007 w->cursor.y = row->y + dy;
15008
15009 if (w == XWINDOW (selected_window))
15010 {
15011 if (!row->continued_p
15012 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15013 && row->x == 0)
15014 {
15015 this_line_buffer = XBUFFER (w->contents);
15016
15017 CHARPOS (this_line_start_pos)
15018 = MATRIX_ROW_START_CHARPOS (row) + delta;
15019 BYTEPOS (this_line_start_pos)
15020 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15021
15022 CHARPOS (this_line_end_pos)
15023 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15024 BYTEPOS (this_line_end_pos)
15025 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15026
15027 this_line_y = w->cursor.y;
15028 this_line_pixel_height = row->height;
15029 this_line_vpos = w->cursor.vpos;
15030 this_line_start_x = row->x;
15031 }
15032 else
15033 CHARPOS (this_line_start_pos) = 0;
15034 }
15035
15036 return true;
15037 }
15038
15039
15040 /* Run window scroll functions, if any, for WINDOW with new window
15041 start STARTP. Sets the window start of WINDOW to that position.
15042
15043 We assume that the window's buffer is really current. */
15044
15045 static struct text_pos
15046 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15047 {
15048 struct window *w = XWINDOW (window);
15049 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15050
15051 eassert (current_buffer == XBUFFER (w->contents));
15052
15053 if (!NILP (Vwindow_scroll_functions))
15054 {
15055 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15056 make_number (CHARPOS (startp)));
15057 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15058 /* In case the hook functions switch buffers. */
15059 set_buffer_internal (XBUFFER (w->contents));
15060 }
15061
15062 return startp;
15063 }
15064
15065
15066 /* Make sure the line containing the cursor is fully visible.
15067 A value of true means there is nothing to be done.
15068 (Either the line is fully visible, or it cannot be made so,
15069 or we cannot tell.)
15070
15071 If FORCE_P, return false even if partial visible cursor row
15072 is higher than window.
15073
15074 If CURRENT_MATRIX_P, use the information from the
15075 window's current glyph matrix; otherwise use the desired glyph
15076 matrix.
15077
15078 A value of false means the caller should do scrolling
15079 as if point had gone off the screen. */
15080
15081 static bool
15082 cursor_row_fully_visible_p (struct window *w, bool force_p,
15083 bool current_matrix_p)
15084 {
15085 struct glyph_matrix *matrix;
15086 struct glyph_row *row;
15087 int window_height;
15088
15089 if (!make_cursor_line_fully_visible_p)
15090 return true;
15091
15092 /* It's not always possible to find the cursor, e.g, when a window
15093 is full of overlay strings. Don't do anything in that case. */
15094 if (w->cursor.vpos < 0)
15095 return true;
15096
15097 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15098 row = MATRIX_ROW (matrix, w->cursor.vpos);
15099
15100 /* If the cursor row is not partially visible, there's nothing to do. */
15101 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15102 return true;
15103
15104 /* If the row the cursor is in is taller than the window's height,
15105 it's not clear what to do, so do nothing. */
15106 window_height = window_box_height (w);
15107 if (row->height >= window_height)
15108 {
15109 if (!force_p || MINI_WINDOW_P (w)
15110 || w->vscroll || w->cursor.vpos == 0)
15111 return true;
15112 }
15113 return false;
15114 }
15115
15116
15117 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15118 means only WINDOW is redisplayed in redisplay_internal.
15119 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15120 in redisplay_window to bring a partially visible line into view in
15121 the case that only the cursor has moved.
15122
15123 LAST_LINE_MISFIT should be true if we're scrolling because the
15124 last screen line's vertical height extends past the end of the screen.
15125
15126 Value is
15127
15128 1 if scrolling succeeded
15129
15130 0 if scrolling didn't find point.
15131
15132 -1 if new fonts have been loaded so that we must interrupt
15133 redisplay, adjust glyph matrices, and try again. */
15134
15135 enum
15136 {
15137 SCROLLING_SUCCESS,
15138 SCROLLING_FAILED,
15139 SCROLLING_NEED_LARGER_MATRICES
15140 };
15141
15142 /* If scroll-conservatively is more than this, never recenter.
15143
15144 If you change this, don't forget to update the doc string of
15145 `scroll-conservatively' and the Emacs manual. */
15146 #define SCROLL_LIMIT 100
15147
15148 static int
15149 try_scrolling (Lisp_Object window, bool just_this_one_p,
15150 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15151 bool temp_scroll_step, bool last_line_misfit)
15152 {
15153 struct window *w = XWINDOW (window);
15154 struct frame *f = XFRAME (w->frame);
15155 struct text_pos pos, startp;
15156 struct it it;
15157 int this_scroll_margin, scroll_max, rc, height;
15158 int dy = 0, amount_to_scroll = 0;
15159 bool scroll_down_p = false;
15160 int extra_scroll_margin_lines = last_line_misfit;
15161 Lisp_Object aggressive;
15162 /* We will never try scrolling more than this number of lines. */
15163 int scroll_limit = SCROLL_LIMIT;
15164 int frame_line_height = default_line_pixel_height (w);
15165 int window_total_lines
15166 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15167
15168 #ifdef GLYPH_DEBUG
15169 debug_method_add (w, "try_scrolling");
15170 #endif
15171
15172 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15173
15174 /* Compute scroll margin height in pixels. We scroll when point is
15175 within this distance from the top or bottom of the window. */
15176 if (scroll_margin > 0)
15177 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15178 * frame_line_height;
15179 else
15180 this_scroll_margin = 0;
15181
15182 /* Force arg_scroll_conservatively to have a reasonable value, to
15183 avoid scrolling too far away with slow move_it_* functions. Note
15184 that the user can supply scroll-conservatively equal to
15185 `most-positive-fixnum', which can be larger than INT_MAX. */
15186 if (arg_scroll_conservatively > scroll_limit)
15187 {
15188 arg_scroll_conservatively = scroll_limit + 1;
15189 scroll_max = scroll_limit * frame_line_height;
15190 }
15191 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15192 /* Compute how much we should try to scroll maximally to bring
15193 point into view. */
15194 scroll_max = (max (scroll_step,
15195 max (arg_scroll_conservatively, temp_scroll_step))
15196 * frame_line_height);
15197 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15198 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15199 /* We're trying to scroll because of aggressive scrolling but no
15200 scroll_step is set. Choose an arbitrary one. */
15201 scroll_max = 10 * frame_line_height;
15202 else
15203 scroll_max = 0;
15204
15205 too_near_end:
15206
15207 /* Decide whether to scroll down. */
15208 if (PT > CHARPOS (startp))
15209 {
15210 int scroll_margin_y;
15211
15212 /* Compute the pixel ypos of the scroll margin, then move IT to
15213 either that ypos or PT, whichever comes first. */
15214 start_display (&it, w, startp);
15215 scroll_margin_y = it.last_visible_y - this_scroll_margin
15216 - frame_line_height * extra_scroll_margin_lines;
15217 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15218 (MOVE_TO_POS | MOVE_TO_Y));
15219
15220 if (PT > CHARPOS (it.current.pos))
15221 {
15222 int y0 = line_bottom_y (&it);
15223 /* Compute how many pixels below window bottom to stop searching
15224 for PT. This avoids costly search for PT that is far away if
15225 the user limited scrolling by a small number of lines, but
15226 always finds PT if scroll_conservatively is set to a large
15227 number, such as most-positive-fixnum. */
15228 int slack = max (scroll_max, 10 * frame_line_height);
15229 int y_to_move = it.last_visible_y + slack;
15230
15231 /* Compute the distance from the scroll margin to PT or to
15232 the scroll limit, whichever comes first. This should
15233 include the height of the cursor line, to make that line
15234 fully visible. */
15235 move_it_to (&it, PT, -1, y_to_move,
15236 -1, MOVE_TO_POS | MOVE_TO_Y);
15237 dy = line_bottom_y (&it) - y0;
15238
15239 if (dy > scroll_max)
15240 return SCROLLING_FAILED;
15241
15242 if (dy > 0)
15243 scroll_down_p = true;
15244 }
15245 }
15246
15247 if (scroll_down_p)
15248 {
15249 /* Point is in or below the bottom scroll margin, so move the
15250 window start down. If scrolling conservatively, move it just
15251 enough down to make point visible. If scroll_step is set,
15252 move it down by scroll_step. */
15253 if (arg_scroll_conservatively)
15254 amount_to_scroll
15255 = min (max (dy, frame_line_height),
15256 frame_line_height * arg_scroll_conservatively);
15257 else if (scroll_step || temp_scroll_step)
15258 amount_to_scroll = scroll_max;
15259 else
15260 {
15261 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15262 height = WINDOW_BOX_TEXT_HEIGHT (w);
15263 if (NUMBERP (aggressive))
15264 {
15265 double float_amount = XFLOATINT (aggressive) * height;
15266 int aggressive_scroll = float_amount;
15267 if (aggressive_scroll == 0 && float_amount > 0)
15268 aggressive_scroll = 1;
15269 /* Don't let point enter the scroll margin near top of
15270 the window. This could happen if the value of
15271 scroll_up_aggressively is too large and there are
15272 non-zero margins, because scroll_up_aggressively
15273 means put point that fraction of window height
15274 _from_the_bottom_margin_. */
15275 if (aggressive_scroll + 2 * this_scroll_margin > height)
15276 aggressive_scroll = height - 2 * this_scroll_margin;
15277 amount_to_scroll = dy + aggressive_scroll;
15278 }
15279 }
15280
15281 if (amount_to_scroll <= 0)
15282 return SCROLLING_FAILED;
15283
15284 start_display (&it, w, startp);
15285 if (arg_scroll_conservatively <= scroll_limit)
15286 move_it_vertically (&it, amount_to_scroll);
15287 else
15288 {
15289 /* Extra precision for users who set scroll-conservatively
15290 to a large number: make sure the amount we scroll
15291 the window start is never less than amount_to_scroll,
15292 which was computed as distance from window bottom to
15293 point. This matters when lines at window top and lines
15294 below window bottom have different height. */
15295 struct it it1;
15296 void *it1data = NULL;
15297 /* We use a temporary it1 because line_bottom_y can modify
15298 its argument, if it moves one line down; see there. */
15299 int start_y;
15300
15301 SAVE_IT (it1, it, it1data);
15302 start_y = line_bottom_y (&it1);
15303 do {
15304 RESTORE_IT (&it, &it, it1data);
15305 move_it_by_lines (&it, 1);
15306 SAVE_IT (it1, it, it1data);
15307 } while (IT_CHARPOS (it) < ZV
15308 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15309 bidi_unshelve_cache (it1data, true);
15310 }
15311
15312 /* If STARTP is unchanged, move it down another screen line. */
15313 if (IT_CHARPOS (it) == CHARPOS (startp))
15314 move_it_by_lines (&it, 1);
15315 startp = it.current.pos;
15316 }
15317 else
15318 {
15319 struct text_pos scroll_margin_pos = startp;
15320 int y_offset = 0;
15321
15322 /* See if point is inside the scroll margin at the top of the
15323 window. */
15324 if (this_scroll_margin)
15325 {
15326 int y_start;
15327
15328 start_display (&it, w, startp);
15329 y_start = it.current_y;
15330 move_it_vertically (&it, this_scroll_margin);
15331 scroll_margin_pos = it.current.pos;
15332 /* If we didn't move enough before hitting ZV, request
15333 additional amount of scroll, to move point out of the
15334 scroll margin. */
15335 if (IT_CHARPOS (it) == ZV
15336 && it.current_y - y_start < this_scroll_margin)
15337 y_offset = this_scroll_margin - (it.current_y - y_start);
15338 }
15339
15340 if (PT < CHARPOS (scroll_margin_pos))
15341 {
15342 /* Point is in the scroll margin at the top of the window or
15343 above what is displayed in the window. */
15344 int y0, y_to_move;
15345
15346 /* Compute the vertical distance from PT to the scroll
15347 margin position. Move as far as scroll_max allows, or
15348 one screenful, or 10 screen lines, whichever is largest.
15349 Give up if distance is greater than scroll_max or if we
15350 didn't reach the scroll margin position. */
15351 SET_TEXT_POS (pos, PT, PT_BYTE);
15352 start_display (&it, w, pos);
15353 y0 = it.current_y;
15354 y_to_move = max (it.last_visible_y,
15355 max (scroll_max, 10 * frame_line_height));
15356 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15357 y_to_move, -1,
15358 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15359 dy = it.current_y - y0;
15360 if (dy > scroll_max
15361 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15362 return SCROLLING_FAILED;
15363
15364 /* Additional scroll for when ZV was too close to point. */
15365 dy += y_offset;
15366
15367 /* Compute new window start. */
15368 start_display (&it, w, startp);
15369
15370 if (arg_scroll_conservatively)
15371 amount_to_scroll = max (dy, frame_line_height
15372 * max (scroll_step, temp_scroll_step));
15373 else if (scroll_step || temp_scroll_step)
15374 amount_to_scroll = scroll_max;
15375 else
15376 {
15377 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15378 height = WINDOW_BOX_TEXT_HEIGHT (w);
15379 if (NUMBERP (aggressive))
15380 {
15381 double float_amount = XFLOATINT (aggressive) * height;
15382 int aggressive_scroll = float_amount;
15383 if (aggressive_scroll == 0 && float_amount > 0)
15384 aggressive_scroll = 1;
15385 /* Don't let point enter the scroll margin near
15386 bottom of the window, if the value of
15387 scroll_down_aggressively happens to be too
15388 large. */
15389 if (aggressive_scroll + 2 * this_scroll_margin > height)
15390 aggressive_scroll = height - 2 * this_scroll_margin;
15391 amount_to_scroll = dy + aggressive_scroll;
15392 }
15393 }
15394
15395 if (amount_to_scroll <= 0)
15396 return SCROLLING_FAILED;
15397
15398 move_it_vertically_backward (&it, amount_to_scroll);
15399 startp = it.current.pos;
15400 }
15401 }
15402
15403 /* Run window scroll functions. */
15404 startp = run_window_scroll_functions (window, startp);
15405
15406 /* Display the window. Give up if new fonts are loaded, or if point
15407 doesn't appear. */
15408 if (!try_window (window, startp, 0))
15409 rc = SCROLLING_NEED_LARGER_MATRICES;
15410 else if (w->cursor.vpos < 0)
15411 {
15412 clear_glyph_matrix (w->desired_matrix);
15413 rc = SCROLLING_FAILED;
15414 }
15415 else
15416 {
15417 /* Maybe forget recorded base line for line number display. */
15418 if (!just_this_one_p
15419 || current_buffer->clip_changed
15420 || BEG_UNCHANGED < CHARPOS (startp))
15421 w->base_line_number = 0;
15422
15423 /* If cursor ends up on a partially visible line,
15424 treat that as being off the bottom of the screen. */
15425 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15426 false)
15427 /* It's possible that the cursor is on the first line of the
15428 buffer, which is partially obscured due to a vscroll
15429 (Bug#7537). In that case, avoid looping forever. */
15430 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15431 {
15432 clear_glyph_matrix (w->desired_matrix);
15433 ++extra_scroll_margin_lines;
15434 goto too_near_end;
15435 }
15436 rc = SCROLLING_SUCCESS;
15437 }
15438
15439 return rc;
15440 }
15441
15442
15443 /* Compute a suitable window start for window W if display of W starts
15444 on a continuation line. Value is true if a new window start
15445 was computed.
15446
15447 The new window start will be computed, based on W's width, starting
15448 from the start of the continued line. It is the start of the
15449 screen line with the minimum distance from the old start W->start. */
15450
15451 static bool
15452 compute_window_start_on_continuation_line (struct window *w)
15453 {
15454 struct text_pos pos, start_pos;
15455 bool window_start_changed_p = false;
15456
15457 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15458
15459 /* If window start is on a continuation line... Window start may be
15460 < BEGV in case there's invisible text at the start of the
15461 buffer (M-x rmail, for example). */
15462 if (CHARPOS (start_pos) > BEGV
15463 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15464 {
15465 struct it it;
15466 struct glyph_row *row;
15467
15468 /* Handle the case that the window start is out of range. */
15469 if (CHARPOS (start_pos) < BEGV)
15470 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15471 else if (CHARPOS (start_pos) > ZV)
15472 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15473
15474 /* Find the start of the continued line. This should be fast
15475 because find_newline is fast (newline cache). */
15476 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15477 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15478 row, DEFAULT_FACE_ID);
15479 reseat_at_previous_visible_line_start (&it);
15480
15481 /* If the line start is "too far" away from the window start,
15482 say it takes too much time to compute a new window start. */
15483 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15484 /* PXW: Do we need upper bounds here? */
15485 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15486 {
15487 int min_distance, distance;
15488
15489 /* Move forward by display lines to find the new window
15490 start. If window width was enlarged, the new start can
15491 be expected to be > the old start. If window width was
15492 decreased, the new window start will be < the old start.
15493 So, we're looking for the display line start with the
15494 minimum distance from the old window start. */
15495 pos = it.current.pos;
15496 min_distance = INFINITY;
15497 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15498 distance < min_distance)
15499 {
15500 min_distance = distance;
15501 pos = it.current.pos;
15502 if (it.line_wrap == WORD_WRAP)
15503 {
15504 /* Under WORD_WRAP, move_it_by_lines is likely to
15505 overshoot and stop not at the first, but the
15506 second character from the left margin. So in
15507 that case, we need a more tight control on the X
15508 coordinate of the iterator than move_it_by_lines
15509 promises in its contract. The method is to first
15510 go to the last (rightmost) visible character of a
15511 line, then move to the leftmost character on the
15512 next line in a separate call. */
15513 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15514 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15515 move_it_to (&it, ZV, 0,
15516 it.current_y + it.max_ascent + it.max_descent, -1,
15517 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15518 }
15519 else
15520 move_it_by_lines (&it, 1);
15521 }
15522
15523 /* Set the window start there. */
15524 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15525 window_start_changed_p = true;
15526 }
15527 }
15528
15529 return window_start_changed_p;
15530 }
15531
15532
15533 /* Try cursor movement in case text has not changed in window WINDOW,
15534 with window start STARTP. Value is
15535
15536 CURSOR_MOVEMENT_SUCCESS if successful
15537
15538 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15539
15540 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15541 display. *SCROLL_STEP is set to true, under certain circumstances, if
15542 we want to scroll as if scroll-step were set to 1. See the code.
15543
15544 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15545 which case we have to abort this redisplay, and adjust matrices
15546 first. */
15547
15548 enum
15549 {
15550 CURSOR_MOVEMENT_SUCCESS,
15551 CURSOR_MOVEMENT_CANNOT_BE_USED,
15552 CURSOR_MOVEMENT_MUST_SCROLL,
15553 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15554 };
15555
15556 static int
15557 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15558 bool *scroll_step)
15559 {
15560 struct window *w = XWINDOW (window);
15561 struct frame *f = XFRAME (w->frame);
15562 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15563
15564 #ifdef GLYPH_DEBUG
15565 if (inhibit_try_cursor_movement)
15566 return rc;
15567 #endif
15568
15569 /* Previously, there was a check for Lisp integer in the
15570 if-statement below. Now, this field is converted to
15571 ptrdiff_t, thus zero means invalid position in a buffer. */
15572 eassert (w->last_point > 0);
15573 /* Likewise there was a check whether window_end_vpos is nil or larger
15574 than the window. Now window_end_vpos is int and so never nil, but
15575 let's leave eassert to check whether it fits in the window. */
15576 eassert (!w->window_end_valid
15577 || w->window_end_vpos < w->current_matrix->nrows);
15578
15579 /* Handle case where text has not changed, only point, and it has
15580 not moved off the frame. */
15581 if (/* Point may be in this window. */
15582 PT >= CHARPOS (startp)
15583 /* Selective display hasn't changed. */
15584 && !current_buffer->clip_changed
15585 /* Function force-mode-line-update is used to force a thorough
15586 redisplay. It sets either windows_or_buffers_changed or
15587 update_mode_lines. So don't take a shortcut here for these
15588 cases. */
15589 && !update_mode_lines
15590 && !windows_or_buffers_changed
15591 && !f->cursor_type_changed
15592 && NILP (Vshow_trailing_whitespace)
15593 /* This code is not used for mini-buffer for the sake of the case
15594 of redisplaying to replace an echo area message; since in
15595 that case the mini-buffer contents per se are usually
15596 unchanged. This code is of no real use in the mini-buffer
15597 since the handling of this_line_start_pos, etc., in redisplay
15598 handles the same cases. */
15599 && !EQ (window, minibuf_window)
15600 && (FRAME_WINDOW_P (f)
15601 || !overlay_arrow_in_current_buffer_p ()))
15602 {
15603 int this_scroll_margin, top_scroll_margin;
15604 struct glyph_row *row = NULL;
15605 int frame_line_height = default_line_pixel_height (w);
15606 int window_total_lines
15607 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15608
15609 #ifdef GLYPH_DEBUG
15610 debug_method_add (w, "cursor movement");
15611 #endif
15612
15613 /* Scroll if point within this distance from the top or bottom
15614 of the window. This is a pixel value. */
15615 if (scroll_margin > 0)
15616 {
15617 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15618 this_scroll_margin *= frame_line_height;
15619 }
15620 else
15621 this_scroll_margin = 0;
15622
15623 top_scroll_margin = this_scroll_margin;
15624 if (WINDOW_WANTS_HEADER_LINE_P (w))
15625 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15626
15627 /* Start with the row the cursor was displayed during the last
15628 not paused redisplay. Give up if that row is not valid. */
15629 if (w->last_cursor_vpos < 0
15630 || w->last_cursor_vpos >= w->current_matrix->nrows)
15631 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15632 else
15633 {
15634 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15635 if (row->mode_line_p)
15636 ++row;
15637 if (!row->enabled_p)
15638 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15639 }
15640
15641 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15642 {
15643 bool scroll_p = false, must_scroll = false;
15644 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15645
15646 if (PT > w->last_point)
15647 {
15648 /* Point has moved forward. */
15649 while (MATRIX_ROW_END_CHARPOS (row) < PT
15650 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15651 {
15652 eassert (row->enabled_p);
15653 ++row;
15654 }
15655
15656 /* If the end position of a row equals the start
15657 position of the next row, and PT is at that position,
15658 we would rather display cursor in the next line. */
15659 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15660 && MATRIX_ROW_END_CHARPOS (row) == PT
15661 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15662 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15663 && !cursor_row_p (row))
15664 ++row;
15665
15666 /* If within the scroll margin, scroll. Note that
15667 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15668 the next line would be drawn, and that
15669 this_scroll_margin can be zero. */
15670 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15671 || PT > MATRIX_ROW_END_CHARPOS (row)
15672 /* Line is completely visible last line in window
15673 and PT is to be set in the next line. */
15674 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15675 && PT == MATRIX_ROW_END_CHARPOS (row)
15676 && !row->ends_at_zv_p
15677 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15678 scroll_p = true;
15679 }
15680 else if (PT < w->last_point)
15681 {
15682 /* Cursor has to be moved backward. Note that PT >=
15683 CHARPOS (startp) because of the outer if-statement. */
15684 while (!row->mode_line_p
15685 && (MATRIX_ROW_START_CHARPOS (row) > PT
15686 || (MATRIX_ROW_START_CHARPOS (row) == PT
15687 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15688 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15689 row > w->current_matrix->rows
15690 && (row-1)->ends_in_newline_from_string_p))))
15691 && (row->y > top_scroll_margin
15692 || CHARPOS (startp) == BEGV))
15693 {
15694 eassert (row->enabled_p);
15695 --row;
15696 }
15697
15698 /* Consider the following case: Window starts at BEGV,
15699 there is invisible, intangible text at BEGV, so that
15700 display starts at some point START > BEGV. It can
15701 happen that we are called with PT somewhere between
15702 BEGV and START. Try to handle that case. */
15703 if (row < w->current_matrix->rows
15704 || row->mode_line_p)
15705 {
15706 row = w->current_matrix->rows;
15707 if (row->mode_line_p)
15708 ++row;
15709 }
15710
15711 /* Due to newlines in overlay strings, we may have to
15712 skip forward over overlay strings. */
15713 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15714 && MATRIX_ROW_END_CHARPOS (row) == PT
15715 && !cursor_row_p (row))
15716 ++row;
15717
15718 /* If within the scroll margin, scroll. */
15719 if (row->y < top_scroll_margin
15720 && CHARPOS (startp) != BEGV)
15721 scroll_p = true;
15722 }
15723 else
15724 {
15725 /* Cursor did not move. So don't scroll even if cursor line
15726 is partially visible, as it was so before. */
15727 rc = CURSOR_MOVEMENT_SUCCESS;
15728 }
15729
15730 if (PT < MATRIX_ROW_START_CHARPOS (row)
15731 || PT > MATRIX_ROW_END_CHARPOS (row))
15732 {
15733 /* if PT is not in the glyph row, give up. */
15734 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15735 must_scroll = true;
15736 }
15737 else if (rc != CURSOR_MOVEMENT_SUCCESS
15738 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15739 {
15740 struct glyph_row *row1;
15741
15742 /* If rows are bidi-reordered and point moved, back up
15743 until we find a row that does not belong to a
15744 continuation line. This is because we must consider
15745 all rows of a continued line as candidates for the
15746 new cursor positioning, since row start and end
15747 positions change non-linearly with vertical position
15748 in such rows. */
15749 /* FIXME: Revisit this when glyph ``spilling'' in
15750 continuation lines' rows is implemented for
15751 bidi-reordered rows. */
15752 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15753 MATRIX_ROW_CONTINUATION_LINE_P (row);
15754 --row)
15755 {
15756 /* If we hit the beginning of the displayed portion
15757 without finding the first row of a continued
15758 line, give up. */
15759 if (row <= row1)
15760 {
15761 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15762 break;
15763 }
15764 eassert (row->enabled_p);
15765 }
15766 }
15767 if (must_scroll)
15768 ;
15769 else if (rc != CURSOR_MOVEMENT_SUCCESS
15770 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15771 /* Make sure this isn't a header line by any chance, since
15772 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15773 && !row->mode_line_p
15774 && make_cursor_line_fully_visible_p)
15775 {
15776 if (PT == MATRIX_ROW_END_CHARPOS (row)
15777 && !row->ends_at_zv_p
15778 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15779 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15780 else if (row->height > window_box_height (w))
15781 {
15782 /* If we end up in a partially visible line, let's
15783 make it fully visible, except when it's taller
15784 than the window, in which case we can't do much
15785 about it. */
15786 *scroll_step = true;
15787 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15788 }
15789 else
15790 {
15791 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15792 if (!cursor_row_fully_visible_p (w, false, true))
15793 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15794 else
15795 rc = CURSOR_MOVEMENT_SUCCESS;
15796 }
15797 }
15798 else if (scroll_p)
15799 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15800 else if (rc != CURSOR_MOVEMENT_SUCCESS
15801 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15802 {
15803 /* With bidi-reordered rows, there could be more than
15804 one candidate row whose start and end positions
15805 occlude point. We need to let set_cursor_from_row
15806 find the best candidate. */
15807 /* FIXME: Revisit this when glyph ``spilling'' in
15808 continuation lines' rows is implemented for
15809 bidi-reordered rows. */
15810 bool rv = false;
15811
15812 do
15813 {
15814 bool at_zv_p = false, exact_match_p = false;
15815
15816 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15817 && PT <= MATRIX_ROW_END_CHARPOS (row)
15818 && cursor_row_p (row))
15819 rv |= set_cursor_from_row (w, row, w->current_matrix,
15820 0, 0, 0, 0);
15821 /* As soon as we've found the exact match for point,
15822 or the first suitable row whose ends_at_zv_p flag
15823 is set, we are done. */
15824 if (rv)
15825 {
15826 at_zv_p = MATRIX_ROW (w->current_matrix,
15827 w->cursor.vpos)->ends_at_zv_p;
15828 if (!at_zv_p
15829 && w->cursor.hpos >= 0
15830 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15831 w->cursor.vpos))
15832 {
15833 struct glyph_row *candidate =
15834 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15835 struct glyph *g =
15836 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15837 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15838
15839 exact_match_p =
15840 (BUFFERP (g->object) && g->charpos == PT)
15841 || (NILP (g->object)
15842 && (g->charpos == PT
15843 || (g->charpos == 0 && endpos - 1 == PT)));
15844 }
15845 if (at_zv_p || exact_match_p)
15846 {
15847 rc = CURSOR_MOVEMENT_SUCCESS;
15848 break;
15849 }
15850 }
15851 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15852 break;
15853 ++row;
15854 }
15855 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15856 || row->continued_p)
15857 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15858 || (MATRIX_ROW_START_CHARPOS (row) == PT
15859 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15860 /* If we didn't find any candidate rows, or exited the
15861 loop before all the candidates were examined, signal
15862 to the caller that this method failed. */
15863 if (rc != CURSOR_MOVEMENT_SUCCESS
15864 && !(rv
15865 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15866 && !row->continued_p))
15867 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15868 else if (rv)
15869 rc = CURSOR_MOVEMENT_SUCCESS;
15870 }
15871 else
15872 {
15873 do
15874 {
15875 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15876 {
15877 rc = CURSOR_MOVEMENT_SUCCESS;
15878 break;
15879 }
15880 ++row;
15881 }
15882 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15883 && MATRIX_ROW_START_CHARPOS (row) == PT
15884 && cursor_row_p (row));
15885 }
15886 }
15887 }
15888
15889 return rc;
15890 }
15891
15892
15893 void
15894 set_vertical_scroll_bar (struct window *w)
15895 {
15896 ptrdiff_t start, end, whole;
15897
15898 /* Calculate the start and end positions for the current window.
15899 At some point, it would be nice to choose between scrollbars
15900 which reflect the whole buffer size, with special markers
15901 indicating narrowing, and scrollbars which reflect only the
15902 visible region.
15903
15904 Note that mini-buffers sometimes aren't displaying any text. */
15905 if (!MINI_WINDOW_P (w)
15906 || (w == XWINDOW (minibuf_window)
15907 && NILP (echo_area_buffer[0])))
15908 {
15909 struct buffer *buf = XBUFFER (w->contents);
15910 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15911 start = marker_position (w->start) - BUF_BEGV (buf);
15912 /* I don't think this is guaranteed to be right. For the
15913 moment, we'll pretend it is. */
15914 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15915
15916 if (end < start)
15917 end = start;
15918 if (whole < (end - start))
15919 whole = end - start;
15920 }
15921 else
15922 start = end = whole = 0;
15923
15924 /* Indicate what this scroll bar ought to be displaying now. */
15925 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15926 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15927 (w, end - start, whole, start);
15928 }
15929
15930
15931 void
15932 set_horizontal_scroll_bar (struct window *w)
15933 {
15934 int start, end, whole, portion;
15935
15936 if (!MINI_WINDOW_P (w)
15937 || (w == XWINDOW (minibuf_window)
15938 && NILP (echo_area_buffer[0])))
15939 {
15940 struct buffer *b = XBUFFER (w->contents);
15941 struct buffer *old_buffer = NULL;
15942 struct it it;
15943 struct text_pos startp;
15944
15945 if (b != current_buffer)
15946 {
15947 old_buffer = current_buffer;
15948 set_buffer_internal (b);
15949 }
15950
15951 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15952 start_display (&it, w, startp);
15953 it.last_visible_x = INT_MAX;
15954 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15955 MOVE_TO_X | MOVE_TO_Y);
15956 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15957 window_box_height (w), -1,
15958 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15959
15960 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15961 end = start + window_box_width (w, TEXT_AREA);
15962 portion = end - start;
15963 /* After enlarging a horizontally scrolled window such that it
15964 gets at least as wide as the text it contains, make sure that
15965 the thumb doesn't fill the entire scroll bar so we can still
15966 drag it back to see the entire text. */
15967 whole = max (whole, end);
15968
15969 if (it.bidi_p)
15970 {
15971 Lisp_Object pdir;
15972
15973 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15974 if (EQ (pdir, Qright_to_left))
15975 {
15976 start = whole - end;
15977 end = start + portion;
15978 }
15979 }
15980
15981 if (old_buffer)
15982 set_buffer_internal (old_buffer);
15983 }
15984 else
15985 start = end = whole = portion = 0;
15986
15987 w->hscroll_whole = whole;
15988
15989 /* Indicate what this scroll bar ought to be displaying now. */
15990 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15991 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15992 (w, portion, whole, start);
15993 }
15994
15995
15996 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
15997 selected_window is redisplayed.
15998
15999 We can return without actually redisplaying the window if fonts has been
16000 changed on window's frame. In that case, redisplay_internal will retry.
16001
16002 As one of the important parts of redisplaying a window, we need to
16003 decide whether the previous window-start position (stored in the
16004 window's w->start marker position) is still valid, and if it isn't,
16005 recompute it. Some details about that:
16006
16007 . The previous window-start could be in a continuation line, in
16008 which case we need to recompute it when the window width
16009 changes. See compute_window_start_on_continuation_line and its
16010 call below.
16011
16012 . The text that changed since last redisplay could include the
16013 previous window-start position. In that case, we try to salvage
16014 what we can from the current glyph matrix by calling
16015 try_scrolling, which see.
16016
16017 . Some Emacs command could force us to use a specific window-start
16018 position by setting the window's force_start flag, or gently
16019 propose doing that by setting the window's optional_new_start
16020 flag. In these cases, we try using the specified start point if
16021 that succeeds (i.e. the window desired matrix is successfully
16022 recomputed, and point location is within the window). In case
16023 of optional_new_start, we first check if the specified start
16024 position is feasible, i.e. if it will allow point to be
16025 displayed in the window. If using the specified start point
16026 fails, e.g., if new fonts are needed to be loaded, we abort the
16027 redisplay cycle and leave it up to the next cycle to figure out
16028 things.
16029
16030 . Note that the window's force_start flag is sometimes set by
16031 redisplay itself, when it decides that the previous window start
16032 point is fine and should be kept. Search for "goto force_start"
16033 below to see the details. Like the values of window-start
16034 specified outside of redisplay, these internally-deduced values
16035 are tested for feasibility, and ignored if found to be
16036 unfeasible.
16037
16038 . Note that the function try_window, used to completely redisplay
16039 a window, accepts the window's start point as its argument.
16040 This is used several times in the redisplay code to control
16041 where the window start will be, according to user options such
16042 as scroll-conservatively, and also to ensure the screen line
16043 showing point will be fully (as opposed to partially) visible on
16044 display. */
16045
16046 static void
16047 redisplay_window (Lisp_Object window, bool just_this_one_p)
16048 {
16049 struct window *w = XWINDOW (window);
16050 struct frame *f = XFRAME (w->frame);
16051 struct buffer *buffer = XBUFFER (w->contents);
16052 struct buffer *old = current_buffer;
16053 struct text_pos lpoint, opoint, startp;
16054 bool update_mode_line;
16055 int tem;
16056 struct it it;
16057 /* Record it now because it's overwritten. */
16058 bool current_matrix_up_to_date_p = false;
16059 bool used_current_matrix_p = false;
16060 /* This is less strict than current_matrix_up_to_date_p.
16061 It indicates that the buffer contents and narrowing are unchanged. */
16062 bool buffer_unchanged_p = false;
16063 bool temp_scroll_step = false;
16064 ptrdiff_t count = SPECPDL_INDEX ();
16065 int rc;
16066 int centering_position = -1;
16067 bool last_line_misfit = false;
16068 ptrdiff_t beg_unchanged, end_unchanged;
16069 int frame_line_height;
16070
16071 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16072 opoint = lpoint;
16073
16074 #ifdef GLYPH_DEBUG
16075 *w->desired_matrix->method = 0;
16076 #endif
16077
16078 if (!just_this_one_p
16079 && REDISPLAY_SOME_P ()
16080 && !w->redisplay
16081 && !w->update_mode_line
16082 && !f->face_change
16083 && !f->redisplay
16084 && !buffer->text->redisplay
16085 && BUF_PT (buffer) == w->last_point)
16086 return;
16087
16088 /* Make sure that both W's markers are valid. */
16089 eassert (XMARKER (w->start)->buffer == buffer);
16090 eassert (XMARKER (w->pointm)->buffer == buffer);
16091
16092 /* We come here again if we need to run window-text-change-functions
16093 below. */
16094 restart:
16095 reconsider_clip_changes (w);
16096 frame_line_height = default_line_pixel_height (w);
16097
16098 /* Has the mode line to be updated? */
16099 update_mode_line = (w->update_mode_line
16100 || update_mode_lines
16101 || buffer->clip_changed
16102 || buffer->prevent_redisplay_optimizations_p);
16103
16104 if (!just_this_one_p)
16105 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16106 cleverly elsewhere. */
16107 w->must_be_updated_p = true;
16108
16109 if (MINI_WINDOW_P (w))
16110 {
16111 if (w == XWINDOW (echo_area_window)
16112 && !NILP (echo_area_buffer[0]))
16113 {
16114 if (update_mode_line)
16115 /* We may have to update a tty frame's menu bar or a
16116 tool-bar. Example `M-x C-h C-h C-g'. */
16117 goto finish_menu_bars;
16118 else
16119 /* We've already displayed the echo area glyphs in this window. */
16120 goto finish_scroll_bars;
16121 }
16122 else if ((w != XWINDOW (minibuf_window)
16123 || minibuf_level == 0)
16124 /* When buffer is nonempty, redisplay window normally. */
16125 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16126 /* Quail displays non-mini buffers in minibuffer window.
16127 In that case, redisplay the window normally. */
16128 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16129 {
16130 /* W is a mini-buffer window, but it's not active, so clear
16131 it. */
16132 int yb = window_text_bottom_y (w);
16133 struct glyph_row *row;
16134 int y;
16135
16136 for (y = 0, row = w->desired_matrix->rows;
16137 y < yb;
16138 y += row->height, ++row)
16139 blank_row (w, row, y);
16140 goto finish_scroll_bars;
16141 }
16142
16143 clear_glyph_matrix (w->desired_matrix);
16144 }
16145
16146 /* Otherwise set up data on this window; select its buffer and point
16147 value. */
16148 /* Really select the buffer, for the sake of buffer-local
16149 variables. */
16150 set_buffer_internal_1 (XBUFFER (w->contents));
16151
16152 current_matrix_up_to_date_p
16153 = (w->window_end_valid
16154 && !current_buffer->clip_changed
16155 && !current_buffer->prevent_redisplay_optimizations_p
16156 && !window_outdated (w));
16157
16158 /* Run the window-text-change-functions
16159 if it is possible that the text on the screen has changed
16160 (either due to modification of the text, or any other reason). */
16161 if (!current_matrix_up_to_date_p
16162 && !NILP (Vwindow_text_change_functions))
16163 {
16164 safe_run_hooks (Qwindow_text_change_functions);
16165 goto restart;
16166 }
16167
16168 beg_unchanged = BEG_UNCHANGED;
16169 end_unchanged = END_UNCHANGED;
16170
16171 SET_TEXT_POS (opoint, PT, PT_BYTE);
16172
16173 specbind (Qinhibit_point_motion_hooks, Qt);
16174
16175 buffer_unchanged_p
16176 = (w->window_end_valid
16177 && !current_buffer->clip_changed
16178 && !window_outdated (w));
16179
16180 /* When windows_or_buffers_changed is non-zero, we can't rely
16181 on the window end being valid, so set it to zero there. */
16182 if (windows_or_buffers_changed)
16183 {
16184 /* If window starts on a continuation line, maybe adjust the
16185 window start in case the window's width changed. */
16186 if (XMARKER (w->start)->buffer == current_buffer)
16187 compute_window_start_on_continuation_line (w);
16188
16189 w->window_end_valid = false;
16190 /* If so, we also can't rely on current matrix
16191 and should not fool try_cursor_movement below. */
16192 current_matrix_up_to_date_p = false;
16193 }
16194
16195 /* Some sanity checks. */
16196 CHECK_WINDOW_END (w);
16197 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16198 emacs_abort ();
16199 if (BYTEPOS (opoint) < CHARPOS (opoint))
16200 emacs_abort ();
16201
16202 if (mode_line_update_needed (w))
16203 update_mode_line = true;
16204
16205 /* Point refers normally to the selected window. For any other
16206 window, set up appropriate value. */
16207 if (!EQ (window, selected_window))
16208 {
16209 ptrdiff_t new_pt = marker_position (w->pointm);
16210 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16211
16212 if (new_pt < BEGV)
16213 {
16214 new_pt = BEGV;
16215 new_pt_byte = BEGV_BYTE;
16216 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16217 }
16218 else if (new_pt > (ZV - 1))
16219 {
16220 new_pt = ZV;
16221 new_pt_byte = ZV_BYTE;
16222 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16223 }
16224
16225 /* We don't use SET_PT so that the point-motion hooks don't run. */
16226 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16227 }
16228
16229 /* If any of the character widths specified in the display table
16230 have changed, invalidate the width run cache. It's true that
16231 this may be a bit late to catch such changes, but the rest of
16232 redisplay goes (non-fatally) haywire when the display table is
16233 changed, so why should we worry about doing any better? */
16234 if (current_buffer->width_run_cache
16235 || (current_buffer->base_buffer
16236 && current_buffer->base_buffer->width_run_cache))
16237 {
16238 struct Lisp_Char_Table *disptab = buffer_display_table ();
16239
16240 if (! disptab_matches_widthtab
16241 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16242 {
16243 struct buffer *buf = current_buffer;
16244
16245 if (buf->base_buffer)
16246 buf = buf->base_buffer;
16247 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16248 recompute_width_table (current_buffer, disptab);
16249 }
16250 }
16251
16252 /* If window-start is screwed up, choose a new one. */
16253 if (XMARKER (w->start)->buffer != current_buffer)
16254 goto recenter;
16255
16256 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16257
16258 /* If someone specified a new starting point but did not insist,
16259 check whether it can be used. */
16260 if ((w->optional_new_start || window_frozen_p (w))
16261 && CHARPOS (startp) >= BEGV
16262 && CHARPOS (startp) <= ZV)
16263 {
16264 ptrdiff_t it_charpos;
16265
16266 w->optional_new_start = false;
16267 start_display (&it, w, startp);
16268 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16269 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16270 /* Record IT's position now, since line_bottom_y might change
16271 that. */
16272 it_charpos = IT_CHARPOS (it);
16273 /* Make sure we set the force_start flag only if the cursor row
16274 will be fully visible. Otherwise, the code under force_start
16275 label below will try to move point back into view, which is
16276 not what the code which sets optional_new_start wants. */
16277 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16278 && !w->force_start)
16279 {
16280 if (it_charpos == PT)
16281 w->force_start = true;
16282 /* IT may overshoot PT if text at PT is invisible. */
16283 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16284 w->force_start = true;
16285 #ifdef GLYPH_DEBUG
16286 if (w->force_start)
16287 {
16288 if (window_frozen_p (w))
16289 debug_method_add (w, "set force_start from frozen window start");
16290 else
16291 debug_method_add (w, "set force_start from optional_new_start");
16292 }
16293 #endif
16294 }
16295 }
16296
16297 force_start:
16298
16299 /* Handle case where place to start displaying has been specified,
16300 unless the specified location is outside the accessible range. */
16301 if (w->force_start)
16302 {
16303 /* We set this later on if we have to adjust point. */
16304 int new_vpos = -1;
16305
16306 w->force_start = false;
16307 w->vscroll = 0;
16308 w->window_end_valid = false;
16309
16310 /* Forget any recorded base line for line number display. */
16311 if (!buffer_unchanged_p)
16312 w->base_line_number = 0;
16313
16314 /* Redisplay the mode line. Select the buffer properly for that.
16315 Also, run the hook window-scroll-functions
16316 because we have scrolled. */
16317 /* Note, we do this after clearing force_start because
16318 if there's an error, it is better to forget about force_start
16319 than to get into an infinite loop calling the hook functions
16320 and having them get more errors. */
16321 if (!update_mode_line
16322 || ! NILP (Vwindow_scroll_functions))
16323 {
16324 update_mode_line = true;
16325 w->update_mode_line = true;
16326 startp = run_window_scroll_functions (window, startp);
16327 }
16328
16329 if (CHARPOS (startp) < BEGV)
16330 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16331 else if (CHARPOS (startp) > ZV)
16332 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16333
16334 /* Redisplay, then check if cursor has been set during the
16335 redisplay. Give up if new fonts were loaded. */
16336 /* We used to issue a CHECK_MARGINS argument to try_window here,
16337 but this causes scrolling to fail when point begins inside
16338 the scroll margin (bug#148) -- cyd */
16339 if (!try_window (window, startp, 0))
16340 {
16341 w->force_start = true;
16342 clear_glyph_matrix (w->desired_matrix);
16343 goto need_larger_matrices;
16344 }
16345
16346 if (w->cursor.vpos < 0)
16347 {
16348 /* If point does not appear, try to move point so it does
16349 appear. The desired matrix has been built above, so we
16350 can use it here. First see if point is in invisible
16351 text, and if so, move it to the first visible buffer
16352 position past that. */
16353 struct glyph_row *r = NULL;
16354 Lisp_Object invprop =
16355 get_char_property_and_overlay (make_number (PT), Qinvisible,
16356 Qnil, NULL);
16357
16358 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16359 {
16360 ptrdiff_t alt_pt;
16361 Lisp_Object invprop_end =
16362 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16363 Qnil, Qnil);
16364
16365 if (NATNUMP (invprop_end))
16366 alt_pt = XFASTINT (invprop_end);
16367 else
16368 alt_pt = ZV;
16369 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16370 NULL, 0);
16371 }
16372 if (r)
16373 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16374 else /* Give up and just move to the middle of the window. */
16375 new_vpos = window_box_height (w) / 2;
16376 }
16377
16378 if (!cursor_row_fully_visible_p (w, false, false))
16379 {
16380 /* Point does appear, but on a line partly visible at end of window.
16381 Move it back to a fully-visible line. */
16382 new_vpos = window_box_height (w);
16383 /* But if window_box_height suggests a Y coordinate that is
16384 not less than we already have, that line will clearly not
16385 be fully visible, so give up and scroll the display.
16386 This can happen when the default face uses a font whose
16387 dimensions are different from the frame's default
16388 font. */
16389 if (new_vpos >= w->cursor.y)
16390 {
16391 w->cursor.vpos = -1;
16392 clear_glyph_matrix (w->desired_matrix);
16393 goto try_to_scroll;
16394 }
16395 }
16396 else if (w->cursor.vpos >= 0)
16397 {
16398 /* Some people insist on not letting point enter the scroll
16399 margin, even though this part handles windows that didn't
16400 scroll at all. */
16401 int window_total_lines
16402 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16403 int margin = min (scroll_margin, window_total_lines / 4);
16404 int pixel_margin = margin * frame_line_height;
16405 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16406
16407 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16408 below, which finds the row to move point to, advances by
16409 the Y coordinate of the _next_ row, see the definition of
16410 MATRIX_ROW_BOTTOM_Y. */
16411 if (w->cursor.vpos < margin + header_line)
16412 {
16413 w->cursor.vpos = -1;
16414 clear_glyph_matrix (w->desired_matrix);
16415 goto try_to_scroll;
16416 }
16417 else
16418 {
16419 int window_height = window_box_height (w);
16420
16421 if (header_line)
16422 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16423 if (w->cursor.y >= window_height - pixel_margin)
16424 {
16425 w->cursor.vpos = -1;
16426 clear_glyph_matrix (w->desired_matrix);
16427 goto try_to_scroll;
16428 }
16429 }
16430 }
16431
16432 /* If we need to move point for either of the above reasons,
16433 now actually do it. */
16434 if (new_vpos >= 0)
16435 {
16436 struct glyph_row *row;
16437
16438 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16439 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16440 ++row;
16441
16442 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16443 MATRIX_ROW_START_BYTEPOS (row));
16444
16445 if (w != XWINDOW (selected_window))
16446 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16447 else if (current_buffer == old)
16448 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16449
16450 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16451
16452 /* Re-run pre-redisplay-function so it can update the region
16453 according to the new position of point. */
16454 /* Other than the cursor, w's redisplay is done so we can set its
16455 redisplay to false. Also the buffer's redisplay can be set to
16456 false, since propagate_buffer_redisplay should have already
16457 propagated its info to `w' anyway. */
16458 w->redisplay = false;
16459 XBUFFER (w->contents)->text->redisplay = false;
16460 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16461
16462 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16463 {
16464 /* pre-redisplay-function made changes (e.g. move the region)
16465 that require another round of redisplay. */
16466 clear_glyph_matrix (w->desired_matrix);
16467 if (!try_window (window, startp, 0))
16468 goto need_larger_matrices;
16469 }
16470 }
16471 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16472 {
16473 clear_glyph_matrix (w->desired_matrix);
16474 goto try_to_scroll;
16475 }
16476
16477 #ifdef GLYPH_DEBUG
16478 debug_method_add (w, "forced window start");
16479 #endif
16480 goto done;
16481 }
16482
16483 /* Handle case where text has not changed, only point, and it has
16484 not moved off the frame, and we are not retrying after hscroll.
16485 (current_matrix_up_to_date_p is true when retrying.) */
16486 if (current_matrix_up_to_date_p
16487 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16488 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16489 {
16490 switch (rc)
16491 {
16492 case CURSOR_MOVEMENT_SUCCESS:
16493 used_current_matrix_p = true;
16494 goto done;
16495
16496 case CURSOR_MOVEMENT_MUST_SCROLL:
16497 goto try_to_scroll;
16498
16499 default:
16500 emacs_abort ();
16501 }
16502 }
16503 /* If current starting point was originally the beginning of a line
16504 but no longer is, find a new starting point. */
16505 else if (w->start_at_line_beg
16506 && !(CHARPOS (startp) <= BEGV
16507 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16508 {
16509 #ifdef GLYPH_DEBUG
16510 debug_method_add (w, "recenter 1");
16511 #endif
16512 goto recenter;
16513 }
16514
16515 /* Try scrolling with try_window_id. Value is > 0 if update has
16516 been done, it is -1 if we know that the same window start will
16517 not work. It is 0 if unsuccessful for some other reason. */
16518 else if ((tem = try_window_id (w)) != 0)
16519 {
16520 #ifdef GLYPH_DEBUG
16521 debug_method_add (w, "try_window_id %d", tem);
16522 #endif
16523
16524 if (f->fonts_changed)
16525 goto need_larger_matrices;
16526 if (tem > 0)
16527 goto done;
16528
16529 /* Otherwise try_window_id has returned -1 which means that we
16530 don't want the alternative below this comment to execute. */
16531 }
16532 else if (CHARPOS (startp) >= BEGV
16533 && CHARPOS (startp) <= ZV
16534 && PT >= CHARPOS (startp)
16535 && (CHARPOS (startp) < ZV
16536 /* Avoid starting at end of buffer. */
16537 || CHARPOS (startp) == BEGV
16538 || !window_outdated (w)))
16539 {
16540 int d1, d2, d5, d6;
16541 int rtop, rbot;
16542
16543 /* If first window line is a continuation line, and window start
16544 is inside the modified region, but the first change is before
16545 current window start, we must select a new window start.
16546
16547 However, if this is the result of a down-mouse event (e.g. by
16548 extending the mouse-drag-overlay), we don't want to select a
16549 new window start, since that would change the position under
16550 the mouse, resulting in an unwanted mouse-movement rather
16551 than a simple mouse-click. */
16552 if (!w->start_at_line_beg
16553 && NILP (do_mouse_tracking)
16554 && CHARPOS (startp) > BEGV
16555 && CHARPOS (startp) > BEG + beg_unchanged
16556 && CHARPOS (startp) <= Z - end_unchanged
16557 /* Even if w->start_at_line_beg is nil, a new window may
16558 start at a line_beg, since that's how set_buffer_window
16559 sets it. So, we need to check the return value of
16560 compute_window_start_on_continuation_line. (See also
16561 bug#197). */
16562 && XMARKER (w->start)->buffer == current_buffer
16563 && compute_window_start_on_continuation_line (w)
16564 /* It doesn't make sense to force the window start like we
16565 do at label force_start if it is already known that point
16566 will not be fully visible in the resulting window, because
16567 doing so will move point from its correct position
16568 instead of scrolling the window to bring point into view.
16569 See bug#9324. */
16570 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16571 /* A very tall row could need more than the window height,
16572 in which case we accept that it is partially visible. */
16573 && (rtop != 0) == (rbot != 0))
16574 {
16575 w->force_start = true;
16576 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16577 #ifdef GLYPH_DEBUG
16578 debug_method_add (w, "recomputed window start in continuation line");
16579 #endif
16580 goto force_start;
16581 }
16582
16583 #ifdef GLYPH_DEBUG
16584 debug_method_add (w, "same window start");
16585 #endif
16586
16587 /* Try to redisplay starting at same place as before.
16588 If point has not moved off frame, accept the results. */
16589 if (!current_matrix_up_to_date_p
16590 /* Don't use try_window_reusing_current_matrix in this case
16591 because a window scroll function can have changed the
16592 buffer. */
16593 || !NILP (Vwindow_scroll_functions)
16594 || MINI_WINDOW_P (w)
16595 || !(used_current_matrix_p
16596 = try_window_reusing_current_matrix (w)))
16597 {
16598 IF_DEBUG (debug_method_add (w, "1"));
16599 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16600 /* -1 means we need to scroll.
16601 0 means we need new matrices, but fonts_changed
16602 is set in that case, so we will detect it below. */
16603 goto try_to_scroll;
16604 }
16605
16606 if (f->fonts_changed)
16607 goto need_larger_matrices;
16608
16609 if (w->cursor.vpos >= 0)
16610 {
16611 if (!just_this_one_p
16612 || current_buffer->clip_changed
16613 || BEG_UNCHANGED < CHARPOS (startp))
16614 /* Forget any recorded base line for line number display. */
16615 w->base_line_number = 0;
16616
16617 if (!cursor_row_fully_visible_p (w, true, false))
16618 {
16619 clear_glyph_matrix (w->desired_matrix);
16620 last_line_misfit = true;
16621 }
16622 /* Drop through and scroll. */
16623 else
16624 goto done;
16625 }
16626 else
16627 clear_glyph_matrix (w->desired_matrix);
16628 }
16629
16630 try_to_scroll:
16631
16632 /* Redisplay the mode line. Select the buffer properly for that. */
16633 if (!update_mode_line)
16634 {
16635 update_mode_line = true;
16636 w->update_mode_line = true;
16637 }
16638
16639 /* Try to scroll by specified few lines. */
16640 if ((scroll_conservatively
16641 || emacs_scroll_step
16642 || temp_scroll_step
16643 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16644 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16645 && CHARPOS (startp) >= BEGV
16646 && CHARPOS (startp) <= ZV)
16647 {
16648 /* The function returns -1 if new fonts were loaded, 1 if
16649 successful, 0 if not successful. */
16650 int ss = try_scrolling (window, just_this_one_p,
16651 scroll_conservatively,
16652 emacs_scroll_step,
16653 temp_scroll_step, last_line_misfit);
16654 switch (ss)
16655 {
16656 case SCROLLING_SUCCESS:
16657 goto done;
16658
16659 case SCROLLING_NEED_LARGER_MATRICES:
16660 goto need_larger_matrices;
16661
16662 case SCROLLING_FAILED:
16663 break;
16664
16665 default:
16666 emacs_abort ();
16667 }
16668 }
16669
16670 /* Finally, just choose a place to start which positions point
16671 according to user preferences. */
16672
16673 recenter:
16674
16675 #ifdef GLYPH_DEBUG
16676 debug_method_add (w, "recenter");
16677 #endif
16678
16679 /* Forget any previously recorded base line for line number display. */
16680 if (!buffer_unchanged_p)
16681 w->base_line_number = 0;
16682
16683 /* Determine the window start relative to point. */
16684 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16685 it.current_y = it.last_visible_y;
16686 if (centering_position < 0)
16687 {
16688 int window_total_lines
16689 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16690 int margin
16691 = scroll_margin > 0
16692 ? min (scroll_margin, window_total_lines / 4)
16693 : 0;
16694 ptrdiff_t margin_pos = CHARPOS (startp);
16695 Lisp_Object aggressive;
16696 bool scrolling_up;
16697
16698 /* If there is a scroll margin at the top of the window, find
16699 its character position. */
16700 if (margin
16701 /* Cannot call start_display if startp is not in the
16702 accessible region of the buffer. This can happen when we
16703 have just switched to a different buffer and/or changed
16704 its restriction. In that case, startp is initialized to
16705 the character position 1 (BEGV) because we did not yet
16706 have chance to display the buffer even once. */
16707 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16708 {
16709 struct it it1;
16710 void *it1data = NULL;
16711
16712 SAVE_IT (it1, it, it1data);
16713 start_display (&it1, w, startp);
16714 move_it_vertically (&it1, margin * frame_line_height);
16715 margin_pos = IT_CHARPOS (it1);
16716 RESTORE_IT (&it, &it, it1data);
16717 }
16718 scrolling_up = PT > margin_pos;
16719 aggressive =
16720 scrolling_up
16721 ? BVAR (current_buffer, scroll_up_aggressively)
16722 : BVAR (current_buffer, scroll_down_aggressively);
16723
16724 if (!MINI_WINDOW_P (w)
16725 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16726 {
16727 int pt_offset = 0;
16728
16729 /* Setting scroll-conservatively overrides
16730 scroll-*-aggressively. */
16731 if (!scroll_conservatively && NUMBERP (aggressive))
16732 {
16733 double float_amount = XFLOATINT (aggressive);
16734
16735 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16736 if (pt_offset == 0 && float_amount > 0)
16737 pt_offset = 1;
16738 if (pt_offset && margin > 0)
16739 margin -= 1;
16740 }
16741 /* Compute how much to move the window start backward from
16742 point so that point will be displayed where the user
16743 wants it. */
16744 if (scrolling_up)
16745 {
16746 centering_position = it.last_visible_y;
16747 if (pt_offset)
16748 centering_position -= pt_offset;
16749 centering_position -=
16750 (frame_line_height * (1 + margin + last_line_misfit)
16751 + WINDOW_HEADER_LINE_HEIGHT (w));
16752 /* Don't let point enter the scroll margin near top of
16753 the window. */
16754 if (centering_position < margin * frame_line_height)
16755 centering_position = margin * frame_line_height;
16756 }
16757 else
16758 centering_position = margin * frame_line_height + pt_offset;
16759 }
16760 else
16761 /* Set the window start half the height of the window backward
16762 from point. */
16763 centering_position = window_box_height (w) / 2;
16764 }
16765 move_it_vertically_backward (&it, centering_position);
16766
16767 eassert (IT_CHARPOS (it) >= BEGV);
16768
16769 /* The function move_it_vertically_backward may move over more
16770 than the specified y-distance. If it->w is small, e.g. a
16771 mini-buffer window, we may end up in front of the window's
16772 display area. Start displaying at the start of the line
16773 containing PT in this case. */
16774 if (it.current_y <= 0)
16775 {
16776 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16777 move_it_vertically_backward (&it, 0);
16778 it.current_y = 0;
16779 }
16780
16781 it.current_x = it.hpos = 0;
16782
16783 /* Set the window start position here explicitly, to avoid an
16784 infinite loop in case the functions in window-scroll-functions
16785 get errors. */
16786 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16787
16788 /* Run scroll hooks. */
16789 startp = run_window_scroll_functions (window, it.current.pos);
16790
16791 /* Redisplay the window. */
16792 bool use_desired_matrix = false;
16793 if (!current_matrix_up_to_date_p
16794 || windows_or_buffers_changed
16795 || f->cursor_type_changed
16796 /* Don't use try_window_reusing_current_matrix in this case
16797 because it can have changed the buffer. */
16798 || !NILP (Vwindow_scroll_functions)
16799 || !just_this_one_p
16800 || MINI_WINDOW_P (w)
16801 || !(used_current_matrix_p
16802 = try_window_reusing_current_matrix (w)))
16803 use_desired_matrix = (try_window (window, startp, 0) == 1);
16804
16805 /* If new fonts have been loaded (due to fontsets), give up. We
16806 have to start a new redisplay since we need to re-adjust glyph
16807 matrices. */
16808 if (f->fonts_changed)
16809 goto need_larger_matrices;
16810
16811 /* If cursor did not appear assume that the middle of the window is
16812 in the first line of the window. Do it again with the next line.
16813 (Imagine a window of height 100, displaying two lines of height
16814 60. Moving back 50 from it->last_visible_y will end in the first
16815 line.) */
16816 if (w->cursor.vpos < 0)
16817 {
16818 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16819 {
16820 clear_glyph_matrix (w->desired_matrix);
16821 move_it_by_lines (&it, 1);
16822 try_window (window, it.current.pos, 0);
16823 }
16824 else if (PT < IT_CHARPOS (it))
16825 {
16826 clear_glyph_matrix (w->desired_matrix);
16827 move_it_by_lines (&it, -1);
16828 try_window (window, it.current.pos, 0);
16829 }
16830 else
16831 {
16832 /* Not much we can do about it. */
16833 }
16834 }
16835
16836 /* Consider the following case: Window starts at BEGV, there is
16837 invisible, intangible text at BEGV, so that display starts at
16838 some point START > BEGV. It can happen that we are called with
16839 PT somewhere between BEGV and START. Try to handle that case,
16840 and similar ones. */
16841 if (w->cursor.vpos < 0)
16842 {
16843 /* Prefer the desired matrix to the current matrix, if possible,
16844 in the fallback calculations below. This is because using
16845 the current matrix might completely goof, e.g. if its first
16846 row is after point. */
16847 struct glyph_matrix *matrix =
16848 use_desired_matrix ? w->desired_matrix : w->current_matrix;
16849 /* First, try locating the proper glyph row for PT. */
16850 struct glyph_row *row =
16851 row_containing_pos (w, PT, matrix->rows, NULL, 0);
16852
16853 /* Sometimes point is at the beginning of invisible text that is
16854 before the 1st character displayed in the row. In that case,
16855 row_containing_pos fails to find the row, because no glyphs
16856 with appropriate buffer positions are present in the row.
16857 Therefore, we next try to find the row which shows the 1st
16858 position after the invisible text. */
16859 if (!row)
16860 {
16861 Lisp_Object val =
16862 get_char_property_and_overlay (make_number (PT), Qinvisible,
16863 Qnil, NULL);
16864
16865 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
16866 {
16867 ptrdiff_t alt_pos;
16868 Lisp_Object invis_end =
16869 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16870 Qnil, Qnil);
16871
16872 if (NATNUMP (invis_end))
16873 alt_pos = XFASTINT (invis_end);
16874 else
16875 alt_pos = ZV;
16876 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
16877 }
16878 }
16879 /* Finally, fall back on the first row of the window after the
16880 header line (if any). This is slightly better than not
16881 displaying the cursor at all. */
16882 if (!row)
16883 {
16884 row = matrix->rows;
16885 if (row->mode_line_p)
16886 ++row;
16887 }
16888 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
16889 }
16890
16891 if (!cursor_row_fully_visible_p (w, false, false))
16892 {
16893 /* If vscroll is enabled, disable it and try again. */
16894 if (w->vscroll)
16895 {
16896 w->vscroll = 0;
16897 clear_glyph_matrix (w->desired_matrix);
16898 goto recenter;
16899 }
16900
16901 /* Users who set scroll-conservatively to a large number want
16902 point just above/below the scroll margin. If we ended up
16903 with point's row partially visible, move the window start to
16904 make that row fully visible and out of the margin. */
16905 if (scroll_conservatively > SCROLL_LIMIT)
16906 {
16907 int window_total_lines
16908 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16909 int margin =
16910 scroll_margin > 0
16911 ? min (scroll_margin, window_total_lines / 4)
16912 : 0;
16913 bool move_down = w->cursor.vpos >= window_total_lines / 2;
16914
16915 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16916 clear_glyph_matrix (w->desired_matrix);
16917 if (1 == try_window (window, it.current.pos,
16918 TRY_WINDOW_CHECK_MARGINS))
16919 goto done;
16920 }
16921
16922 /* If centering point failed to make the whole line visible,
16923 put point at the top instead. That has to make the whole line
16924 visible, if it can be done. */
16925 if (centering_position == 0)
16926 goto done;
16927
16928 clear_glyph_matrix (w->desired_matrix);
16929 centering_position = 0;
16930 goto recenter;
16931 }
16932
16933 done:
16934
16935 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16936 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16937 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16938
16939 /* Display the mode line, if we must. */
16940 if ((update_mode_line
16941 /* If window not full width, must redo its mode line
16942 if (a) the window to its side is being redone and
16943 (b) we do a frame-based redisplay. This is a consequence
16944 of how inverted lines are drawn in frame-based redisplay. */
16945 || (!just_this_one_p
16946 && !FRAME_WINDOW_P (f)
16947 && !WINDOW_FULL_WIDTH_P (w))
16948 /* Line number to display. */
16949 || w->base_line_pos > 0
16950 /* Column number is displayed and different from the one displayed. */
16951 || (w->column_number_displayed != -1
16952 && (w->column_number_displayed != current_column ())))
16953 /* This means that the window has a mode line. */
16954 && (WINDOW_WANTS_MODELINE_P (w)
16955 || WINDOW_WANTS_HEADER_LINE_P (w)))
16956 {
16957
16958 display_mode_lines (w);
16959
16960 /* If mode line height has changed, arrange for a thorough
16961 immediate redisplay using the correct mode line height. */
16962 if (WINDOW_WANTS_MODELINE_P (w)
16963 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16964 {
16965 f->fonts_changed = true;
16966 w->mode_line_height = -1;
16967 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16968 = DESIRED_MODE_LINE_HEIGHT (w);
16969 }
16970
16971 /* If header line height has changed, arrange for a thorough
16972 immediate redisplay using the correct header line height. */
16973 if (WINDOW_WANTS_HEADER_LINE_P (w)
16974 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16975 {
16976 f->fonts_changed = true;
16977 w->header_line_height = -1;
16978 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16979 = DESIRED_HEADER_LINE_HEIGHT (w);
16980 }
16981
16982 if (f->fonts_changed)
16983 goto need_larger_matrices;
16984 }
16985
16986 if (!line_number_displayed && w->base_line_pos != -1)
16987 {
16988 w->base_line_pos = 0;
16989 w->base_line_number = 0;
16990 }
16991
16992 finish_menu_bars:
16993
16994 /* When we reach a frame's selected window, redo the frame's menu
16995 bar and the frame's title. */
16996 if (update_mode_line
16997 && EQ (FRAME_SELECTED_WINDOW (f), window))
16998 {
16999 bool redisplay_menu_p;
17000
17001 if (FRAME_WINDOW_P (f))
17002 {
17003 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17004 || defined (HAVE_NS) || defined (USE_GTK)
17005 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17006 #else
17007 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17008 #endif
17009 }
17010 else
17011 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17012
17013 if (redisplay_menu_p)
17014 display_menu_bar (w);
17015
17016 #ifdef HAVE_WINDOW_SYSTEM
17017 if (FRAME_WINDOW_P (f))
17018 {
17019 #if defined (USE_GTK) || defined (HAVE_NS)
17020 if (FRAME_EXTERNAL_TOOL_BAR (f))
17021 redisplay_tool_bar (f);
17022 #else
17023 if (WINDOWP (f->tool_bar_window)
17024 && (FRAME_TOOL_BAR_LINES (f) > 0
17025 || !NILP (Vauto_resize_tool_bars))
17026 && redisplay_tool_bar (f))
17027 ignore_mouse_drag_p = true;
17028 #endif
17029 }
17030 x_consider_frame_title (w->frame);
17031 #endif
17032 }
17033
17034 #ifdef HAVE_WINDOW_SYSTEM
17035 if (FRAME_WINDOW_P (f)
17036 && update_window_fringes (w, (just_this_one_p
17037 || (!used_current_matrix_p && !overlay_arrow_seen)
17038 || w->pseudo_window_p)))
17039 {
17040 update_begin (f);
17041 block_input ();
17042 if (draw_window_fringes (w, true))
17043 {
17044 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17045 x_draw_right_divider (w);
17046 else
17047 x_draw_vertical_border (w);
17048 }
17049 unblock_input ();
17050 update_end (f);
17051 }
17052
17053 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17054 x_draw_bottom_divider (w);
17055 #endif /* HAVE_WINDOW_SYSTEM */
17056
17057 /* We go to this label, with fonts_changed set, if it is
17058 necessary to try again using larger glyph matrices.
17059 We have to redeem the scroll bar even in this case,
17060 because the loop in redisplay_internal expects that. */
17061 need_larger_matrices:
17062 ;
17063 finish_scroll_bars:
17064
17065 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17066 {
17067 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17068 /* Set the thumb's position and size. */
17069 set_vertical_scroll_bar (w);
17070
17071 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17072 /* Set the thumb's position and size. */
17073 set_horizontal_scroll_bar (w);
17074
17075 /* Note that we actually used the scroll bar attached to this
17076 window, so it shouldn't be deleted at the end of redisplay. */
17077 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17078 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17079 }
17080
17081 /* Restore current_buffer and value of point in it. The window
17082 update may have changed the buffer, so first make sure `opoint'
17083 is still valid (Bug#6177). */
17084 if (CHARPOS (opoint) < BEGV)
17085 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17086 else if (CHARPOS (opoint) > ZV)
17087 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17088 else
17089 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17090
17091 set_buffer_internal_1 (old);
17092 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17093 shorter. This can be caused by log truncation in *Messages*. */
17094 if (CHARPOS (lpoint) <= ZV)
17095 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17096
17097 unbind_to (count, Qnil);
17098 }
17099
17100
17101 /* Build the complete desired matrix of WINDOW with a window start
17102 buffer position POS.
17103
17104 Value is 1 if successful. It is zero if fonts were loaded during
17105 redisplay which makes re-adjusting glyph matrices necessary, and -1
17106 if point would appear in the scroll margins.
17107 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17108 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17109 set in FLAGS.) */
17110
17111 int
17112 try_window (Lisp_Object window, struct text_pos pos, int flags)
17113 {
17114 struct window *w = XWINDOW (window);
17115 struct it it;
17116 struct glyph_row *last_text_row = NULL;
17117 struct frame *f = XFRAME (w->frame);
17118 int frame_line_height = default_line_pixel_height (w);
17119
17120 /* Make POS the new window start. */
17121 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17122
17123 /* Mark cursor position as unknown. No overlay arrow seen. */
17124 w->cursor.vpos = -1;
17125 overlay_arrow_seen = false;
17126
17127 /* Initialize iterator and info to start at POS. */
17128 start_display (&it, w, pos);
17129 it.glyph_row->reversed_p = false;
17130
17131 /* Display all lines of W. */
17132 while (it.current_y < it.last_visible_y)
17133 {
17134 if (display_line (&it))
17135 last_text_row = it.glyph_row - 1;
17136 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17137 return 0;
17138 }
17139
17140 /* Don't let the cursor end in the scroll margins. */
17141 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17142 && !MINI_WINDOW_P (w))
17143 {
17144 int this_scroll_margin;
17145 int window_total_lines
17146 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17147
17148 if (scroll_margin > 0)
17149 {
17150 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17151 this_scroll_margin *= frame_line_height;
17152 }
17153 else
17154 this_scroll_margin = 0;
17155
17156 if ((w->cursor.y >= 0 /* not vscrolled */
17157 && w->cursor.y < this_scroll_margin
17158 && CHARPOS (pos) > BEGV
17159 && IT_CHARPOS (it) < ZV)
17160 /* rms: considering make_cursor_line_fully_visible_p here
17161 seems to give wrong results. We don't want to recenter
17162 when the last line is partly visible, we want to allow
17163 that case to be handled in the usual way. */
17164 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17165 {
17166 w->cursor.vpos = -1;
17167 clear_glyph_matrix (w->desired_matrix);
17168 return -1;
17169 }
17170 }
17171
17172 /* If bottom moved off end of frame, change mode line percentage. */
17173 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17174 w->update_mode_line = true;
17175
17176 /* Set window_end_pos to the offset of the last character displayed
17177 on the window from the end of current_buffer. Set
17178 window_end_vpos to its row number. */
17179 if (last_text_row)
17180 {
17181 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17182 adjust_window_ends (w, last_text_row, false);
17183 eassert
17184 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17185 w->window_end_vpos)));
17186 }
17187 else
17188 {
17189 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17190 w->window_end_pos = Z - ZV;
17191 w->window_end_vpos = 0;
17192 }
17193
17194 /* But that is not valid info until redisplay finishes. */
17195 w->window_end_valid = false;
17196 return 1;
17197 }
17198
17199
17200 \f
17201 /************************************************************************
17202 Window redisplay reusing current matrix when buffer has not changed
17203 ************************************************************************/
17204
17205 /* Try redisplay of window W showing an unchanged buffer with a
17206 different window start than the last time it was displayed by
17207 reusing its current matrix. Value is true if successful.
17208 W->start is the new window start. */
17209
17210 static bool
17211 try_window_reusing_current_matrix (struct window *w)
17212 {
17213 struct frame *f = XFRAME (w->frame);
17214 struct glyph_row *bottom_row;
17215 struct it it;
17216 struct run run;
17217 struct text_pos start, new_start;
17218 int nrows_scrolled, i;
17219 struct glyph_row *last_text_row;
17220 struct glyph_row *last_reused_text_row;
17221 struct glyph_row *start_row;
17222 int start_vpos, min_y, max_y;
17223
17224 #ifdef GLYPH_DEBUG
17225 if (inhibit_try_window_reusing)
17226 return false;
17227 #endif
17228
17229 if (/* This function doesn't handle terminal frames. */
17230 !FRAME_WINDOW_P (f)
17231 /* Don't try to reuse the display if windows have been split
17232 or such. */
17233 || windows_or_buffers_changed
17234 || f->cursor_type_changed)
17235 return false;
17236
17237 /* Can't do this if showing trailing whitespace. */
17238 if (!NILP (Vshow_trailing_whitespace))
17239 return false;
17240
17241 /* If top-line visibility has changed, give up. */
17242 if (WINDOW_WANTS_HEADER_LINE_P (w)
17243 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17244 return false;
17245
17246 /* Give up if old or new display is scrolled vertically. We could
17247 make this function handle this, but right now it doesn't. */
17248 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17249 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17250 return false;
17251
17252 /* The variable new_start now holds the new window start. The old
17253 start `start' can be determined from the current matrix. */
17254 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17255 start = start_row->minpos;
17256 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17257
17258 /* Clear the desired matrix for the display below. */
17259 clear_glyph_matrix (w->desired_matrix);
17260
17261 if (CHARPOS (new_start) <= CHARPOS (start))
17262 {
17263 /* Don't use this method if the display starts with an ellipsis
17264 displayed for invisible text. It's not easy to handle that case
17265 below, and it's certainly not worth the effort since this is
17266 not a frequent case. */
17267 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17268 return false;
17269
17270 IF_DEBUG (debug_method_add (w, "twu1"));
17271
17272 /* Display up to a row that can be reused. The variable
17273 last_text_row is set to the last row displayed that displays
17274 text. Note that it.vpos == 0 if or if not there is a
17275 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17276 start_display (&it, w, new_start);
17277 w->cursor.vpos = -1;
17278 last_text_row = last_reused_text_row = NULL;
17279
17280 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17281 {
17282 /* If we have reached into the characters in the START row,
17283 that means the line boundaries have changed. So we
17284 can't start copying with the row START. Maybe it will
17285 work to start copying with the following row. */
17286 while (IT_CHARPOS (it) > CHARPOS (start))
17287 {
17288 /* Advance to the next row as the "start". */
17289 start_row++;
17290 start = start_row->minpos;
17291 /* If there are no more rows to try, or just one, give up. */
17292 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17293 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17294 || CHARPOS (start) == ZV)
17295 {
17296 clear_glyph_matrix (w->desired_matrix);
17297 return false;
17298 }
17299
17300 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17301 }
17302 /* If we have reached alignment, we can copy the rest of the
17303 rows. */
17304 if (IT_CHARPOS (it) == CHARPOS (start)
17305 /* Don't accept "alignment" inside a display vector,
17306 since start_row could have started in the middle of
17307 that same display vector (thus their character
17308 positions match), and we have no way of telling if
17309 that is the case. */
17310 && it.current.dpvec_index < 0)
17311 break;
17312
17313 it.glyph_row->reversed_p = false;
17314 if (display_line (&it))
17315 last_text_row = it.glyph_row - 1;
17316
17317 }
17318
17319 /* A value of current_y < last_visible_y means that we stopped
17320 at the previous window start, which in turn means that we
17321 have at least one reusable row. */
17322 if (it.current_y < it.last_visible_y)
17323 {
17324 struct glyph_row *row;
17325
17326 /* IT.vpos always starts from 0; it counts text lines. */
17327 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17328
17329 /* Find PT if not already found in the lines displayed. */
17330 if (w->cursor.vpos < 0)
17331 {
17332 int dy = it.current_y - start_row->y;
17333
17334 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17335 row = row_containing_pos (w, PT, row, NULL, dy);
17336 if (row)
17337 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17338 dy, nrows_scrolled);
17339 else
17340 {
17341 clear_glyph_matrix (w->desired_matrix);
17342 return false;
17343 }
17344 }
17345
17346 /* Scroll the display. Do it before the current matrix is
17347 changed. The problem here is that update has not yet
17348 run, i.e. part of the current matrix is not up to date.
17349 scroll_run_hook will clear the cursor, and use the
17350 current matrix to get the height of the row the cursor is
17351 in. */
17352 run.current_y = start_row->y;
17353 run.desired_y = it.current_y;
17354 run.height = it.last_visible_y - it.current_y;
17355
17356 if (run.height > 0 && run.current_y != run.desired_y)
17357 {
17358 update_begin (f);
17359 FRAME_RIF (f)->update_window_begin_hook (w);
17360 FRAME_RIF (f)->clear_window_mouse_face (w);
17361 FRAME_RIF (f)->scroll_run_hook (w, &run);
17362 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17363 update_end (f);
17364 }
17365
17366 /* Shift current matrix down by nrows_scrolled lines. */
17367 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17368 rotate_matrix (w->current_matrix,
17369 start_vpos,
17370 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17371 nrows_scrolled);
17372
17373 /* Disable lines that must be updated. */
17374 for (i = 0; i < nrows_scrolled; ++i)
17375 (start_row + i)->enabled_p = false;
17376
17377 /* Re-compute Y positions. */
17378 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17379 max_y = it.last_visible_y;
17380 for (row = start_row + nrows_scrolled;
17381 row < bottom_row;
17382 ++row)
17383 {
17384 row->y = it.current_y;
17385 row->visible_height = row->height;
17386
17387 if (row->y < min_y)
17388 row->visible_height -= min_y - row->y;
17389 if (row->y + row->height > max_y)
17390 row->visible_height -= row->y + row->height - max_y;
17391 if (row->fringe_bitmap_periodic_p)
17392 row->redraw_fringe_bitmaps_p = true;
17393
17394 it.current_y += row->height;
17395
17396 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17397 last_reused_text_row = row;
17398 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17399 break;
17400 }
17401
17402 /* Disable lines in the current matrix which are now
17403 below the window. */
17404 for (++row; row < bottom_row; ++row)
17405 row->enabled_p = row->mode_line_p = false;
17406 }
17407
17408 /* Update window_end_pos etc.; last_reused_text_row is the last
17409 reused row from the current matrix containing text, if any.
17410 The value of last_text_row is the last displayed line
17411 containing text. */
17412 if (last_reused_text_row)
17413 adjust_window_ends (w, last_reused_text_row, true);
17414 else if (last_text_row)
17415 adjust_window_ends (w, last_text_row, false);
17416 else
17417 {
17418 /* This window must be completely empty. */
17419 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17420 w->window_end_pos = Z - ZV;
17421 w->window_end_vpos = 0;
17422 }
17423 w->window_end_valid = false;
17424
17425 /* Update hint: don't try scrolling again in update_window. */
17426 w->desired_matrix->no_scrolling_p = true;
17427
17428 #ifdef GLYPH_DEBUG
17429 debug_method_add (w, "try_window_reusing_current_matrix 1");
17430 #endif
17431 return true;
17432 }
17433 else if (CHARPOS (new_start) > CHARPOS (start))
17434 {
17435 struct glyph_row *pt_row, *row;
17436 struct glyph_row *first_reusable_row;
17437 struct glyph_row *first_row_to_display;
17438 int dy;
17439 int yb = window_text_bottom_y (w);
17440
17441 /* Find the row starting at new_start, if there is one. Don't
17442 reuse a partially visible line at the end. */
17443 first_reusable_row = start_row;
17444 while (first_reusable_row->enabled_p
17445 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17446 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17447 < CHARPOS (new_start)))
17448 ++first_reusable_row;
17449
17450 /* Give up if there is no row to reuse. */
17451 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17452 || !first_reusable_row->enabled_p
17453 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17454 != CHARPOS (new_start)))
17455 return false;
17456
17457 /* We can reuse fully visible rows beginning with
17458 first_reusable_row to the end of the window. Set
17459 first_row_to_display to the first row that cannot be reused.
17460 Set pt_row to the row containing point, if there is any. */
17461 pt_row = NULL;
17462 for (first_row_to_display = first_reusable_row;
17463 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17464 ++first_row_to_display)
17465 {
17466 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17467 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17468 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17469 && first_row_to_display->ends_at_zv_p
17470 && pt_row == NULL)))
17471 pt_row = first_row_to_display;
17472 }
17473
17474 /* Start displaying at the start of first_row_to_display. */
17475 eassert (first_row_to_display->y < yb);
17476 init_to_row_start (&it, w, first_row_to_display);
17477
17478 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17479 - start_vpos);
17480 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17481 - nrows_scrolled);
17482 it.current_y = (first_row_to_display->y - first_reusable_row->y
17483 + WINDOW_HEADER_LINE_HEIGHT (w));
17484
17485 /* Display lines beginning with first_row_to_display in the
17486 desired matrix. Set last_text_row to the last row displayed
17487 that displays text. */
17488 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17489 if (pt_row == NULL)
17490 w->cursor.vpos = -1;
17491 last_text_row = NULL;
17492 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17493 if (display_line (&it))
17494 last_text_row = it.glyph_row - 1;
17495
17496 /* If point is in a reused row, adjust y and vpos of the cursor
17497 position. */
17498 if (pt_row)
17499 {
17500 w->cursor.vpos -= nrows_scrolled;
17501 w->cursor.y -= first_reusable_row->y - start_row->y;
17502 }
17503
17504 /* Give up if point isn't in a row displayed or reused. (This
17505 also handles the case where w->cursor.vpos < nrows_scrolled
17506 after the calls to display_line, which can happen with scroll
17507 margins. See bug#1295.) */
17508 if (w->cursor.vpos < 0)
17509 {
17510 clear_glyph_matrix (w->desired_matrix);
17511 return false;
17512 }
17513
17514 /* Scroll the display. */
17515 run.current_y = first_reusable_row->y;
17516 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17517 run.height = it.last_visible_y - run.current_y;
17518 dy = run.current_y - run.desired_y;
17519
17520 if (run.height)
17521 {
17522 update_begin (f);
17523 FRAME_RIF (f)->update_window_begin_hook (w);
17524 FRAME_RIF (f)->clear_window_mouse_face (w);
17525 FRAME_RIF (f)->scroll_run_hook (w, &run);
17526 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17527 update_end (f);
17528 }
17529
17530 /* Adjust Y positions of reused rows. */
17531 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17532 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17533 max_y = it.last_visible_y;
17534 for (row = first_reusable_row; row < first_row_to_display; ++row)
17535 {
17536 row->y -= dy;
17537 row->visible_height = row->height;
17538 if (row->y < min_y)
17539 row->visible_height -= min_y - row->y;
17540 if (row->y + row->height > max_y)
17541 row->visible_height -= row->y + row->height - max_y;
17542 if (row->fringe_bitmap_periodic_p)
17543 row->redraw_fringe_bitmaps_p = true;
17544 }
17545
17546 /* Scroll the current matrix. */
17547 eassert (nrows_scrolled > 0);
17548 rotate_matrix (w->current_matrix,
17549 start_vpos,
17550 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17551 -nrows_scrolled);
17552
17553 /* Disable rows not reused. */
17554 for (row -= nrows_scrolled; row < bottom_row; ++row)
17555 row->enabled_p = false;
17556
17557 /* Point may have moved to a different line, so we cannot assume that
17558 the previous cursor position is valid; locate the correct row. */
17559 if (pt_row)
17560 {
17561 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17562 row < bottom_row
17563 && PT >= MATRIX_ROW_END_CHARPOS (row)
17564 && !row->ends_at_zv_p;
17565 row++)
17566 {
17567 w->cursor.vpos++;
17568 w->cursor.y = row->y;
17569 }
17570 if (row < bottom_row)
17571 {
17572 /* Can't simply scan the row for point with
17573 bidi-reordered glyph rows. Let set_cursor_from_row
17574 figure out where to put the cursor, and if it fails,
17575 give up. */
17576 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17577 {
17578 if (!set_cursor_from_row (w, row, w->current_matrix,
17579 0, 0, 0, 0))
17580 {
17581 clear_glyph_matrix (w->desired_matrix);
17582 return false;
17583 }
17584 }
17585 else
17586 {
17587 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17588 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17589
17590 for (; glyph < end
17591 && (!BUFFERP (glyph->object)
17592 || glyph->charpos < PT);
17593 glyph++)
17594 {
17595 w->cursor.hpos++;
17596 w->cursor.x += glyph->pixel_width;
17597 }
17598 }
17599 }
17600 }
17601
17602 /* Adjust window end. A null value of last_text_row means that
17603 the window end is in reused rows which in turn means that
17604 only its vpos can have changed. */
17605 if (last_text_row)
17606 adjust_window_ends (w, last_text_row, false);
17607 else
17608 w->window_end_vpos -= nrows_scrolled;
17609
17610 w->window_end_valid = false;
17611 w->desired_matrix->no_scrolling_p = true;
17612
17613 #ifdef GLYPH_DEBUG
17614 debug_method_add (w, "try_window_reusing_current_matrix 2");
17615 #endif
17616 return true;
17617 }
17618
17619 return false;
17620 }
17621
17622
17623 \f
17624 /************************************************************************
17625 Window redisplay reusing current matrix when buffer has changed
17626 ************************************************************************/
17627
17628 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17629 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17630 ptrdiff_t *, ptrdiff_t *);
17631 static struct glyph_row *
17632 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17633 struct glyph_row *);
17634
17635
17636 /* Return the last row in MATRIX displaying text. If row START is
17637 non-null, start searching with that row. IT gives the dimensions
17638 of the display. Value is null if matrix is empty; otherwise it is
17639 a pointer to the row found. */
17640
17641 static struct glyph_row *
17642 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17643 struct glyph_row *start)
17644 {
17645 struct glyph_row *row, *row_found;
17646
17647 /* Set row_found to the last row in IT->w's current matrix
17648 displaying text. The loop looks funny but think of partially
17649 visible lines. */
17650 row_found = NULL;
17651 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17652 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17653 {
17654 eassert (row->enabled_p);
17655 row_found = row;
17656 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17657 break;
17658 ++row;
17659 }
17660
17661 return row_found;
17662 }
17663
17664
17665 /* Return the last row in the current matrix of W that is not affected
17666 by changes at the start of current_buffer that occurred since W's
17667 current matrix was built. Value is null if no such row exists.
17668
17669 BEG_UNCHANGED us the number of characters unchanged at the start of
17670 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17671 first changed character in current_buffer. Characters at positions <
17672 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17673 when the current matrix was built. */
17674
17675 static struct glyph_row *
17676 find_last_unchanged_at_beg_row (struct window *w)
17677 {
17678 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17679 struct glyph_row *row;
17680 struct glyph_row *row_found = NULL;
17681 int yb = window_text_bottom_y (w);
17682
17683 /* Find the last row displaying unchanged text. */
17684 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17685 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17686 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17687 ++row)
17688 {
17689 if (/* If row ends before first_changed_pos, it is unchanged,
17690 except in some case. */
17691 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17692 /* When row ends in ZV and we write at ZV it is not
17693 unchanged. */
17694 && !row->ends_at_zv_p
17695 /* When first_changed_pos is the end of a continued line,
17696 row is not unchanged because it may be no longer
17697 continued. */
17698 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17699 && (row->continued_p
17700 || row->exact_window_width_line_p))
17701 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17702 needs to be recomputed, so don't consider this row as
17703 unchanged. This happens when the last line was
17704 bidi-reordered and was killed immediately before this
17705 redisplay cycle. In that case, ROW->end stores the
17706 buffer position of the first visual-order character of
17707 the killed text, which is now beyond ZV. */
17708 && CHARPOS (row->end.pos) <= ZV)
17709 row_found = row;
17710
17711 /* Stop if last visible row. */
17712 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17713 break;
17714 }
17715
17716 return row_found;
17717 }
17718
17719
17720 /* Find the first glyph row in the current matrix of W that is not
17721 affected by changes at the end of current_buffer since the
17722 time W's current matrix was built.
17723
17724 Return in *DELTA the number of chars by which buffer positions in
17725 unchanged text at the end of current_buffer must be adjusted.
17726
17727 Return in *DELTA_BYTES the corresponding number of bytes.
17728
17729 Value is null if no such row exists, i.e. all rows are affected by
17730 changes. */
17731
17732 static struct glyph_row *
17733 find_first_unchanged_at_end_row (struct window *w,
17734 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17735 {
17736 struct glyph_row *row;
17737 struct glyph_row *row_found = NULL;
17738
17739 *delta = *delta_bytes = 0;
17740
17741 /* Display must not have been paused, otherwise the current matrix
17742 is not up to date. */
17743 eassert (w->window_end_valid);
17744
17745 /* A value of window_end_pos >= END_UNCHANGED means that the window
17746 end is in the range of changed text. If so, there is no
17747 unchanged row at the end of W's current matrix. */
17748 if (w->window_end_pos >= END_UNCHANGED)
17749 return NULL;
17750
17751 /* Set row to the last row in W's current matrix displaying text. */
17752 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17753
17754 /* If matrix is entirely empty, no unchanged row exists. */
17755 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17756 {
17757 /* The value of row is the last glyph row in the matrix having a
17758 meaningful buffer position in it. The end position of row
17759 corresponds to window_end_pos. This allows us to translate
17760 buffer positions in the current matrix to current buffer
17761 positions for characters not in changed text. */
17762 ptrdiff_t Z_old =
17763 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17764 ptrdiff_t Z_BYTE_old =
17765 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17766 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17767 struct glyph_row *first_text_row
17768 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17769
17770 *delta = Z - Z_old;
17771 *delta_bytes = Z_BYTE - Z_BYTE_old;
17772
17773 /* Set last_unchanged_pos to the buffer position of the last
17774 character in the buffer that has not been changed. Z is the
17775 index + 1 of the last character in current_buffer, i.e. by
17776 subtracting END_UNCHANGED we get the index of the last
17777 unchanged character, and we have to add BEG to get its buffer
17778 position. */
17779 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17780 last_unchanged_pos_old = last_unchanged_pos - *delta;
17781
17782 /* Search backward from ROW for a row displaying a line that
17783 starts at a minimum position >= last_unchanged_pos_old. */
17784 for (; row > first_text_row; --row)
17785 {
17786 /* This used to abort, but it can happen.
17787 It is ok to just stop the search instead here. KFS. */
17788 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17789 break;
17790
17791 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17792 row_found = row;
17793 }
17794 }
17795
17796 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17797
17798 return row_found;
17799 }
17800
17801
17802 /* Make sure that glyph rows in the current matrix of window W
17803 reference the same glyph memory as corresponding rows in the
17804 frame's frame matrix. This function is called after scrolling W's
17805 current matrix on a terminal frame in try_window_id and
17806 try_window_reusing_current_matrix. */
17807
17808 static void
17809 sync_frame_with_window_matrix_rows (struct window *w)
17810 {
17811 struct frame *f = XFRAME (w->frame);
17812 struct glyph_row *window_row, *window_row_end, *frame_row;
17813
17814 /* Preconditions: W must be a leaf window and full-width. Its frame
17815 must have a frame matrix. */
17816 eassert (BUFFERP (w->contents));
17817 eassert (WINDOW_FULL_WIDTH_P (w));
17818 eassert (!FRAME_WINDOW_P (f));
17819
17820 /* If W is a full-width window, glyph pointers in W's current matrix
17821 have, by definition, to be the same as glyph pointers in the
17822 corresponding frame matrix. Note that frame matrices have no
17823 marginal areas (see build_frame_matrix). */
17824 window_row = w->current_matrix->rows;
17825 window_row_end = window_row + w->current_matrix->nrows;
17826 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17827 while (window_row < window_row_end)
17828 {
17829 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17830 struct glyph *end = window_row->glyphs[LAST_AREA];
17831
17832 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17833 frame_row->glyphs[TEXT_AREA] = start;
17834 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17835 frame_row->glyphs[LAST_AREA] = end;
17836
17837 /* Disable frame rows whose corresponding window rows have
17838 been disabled in try_window_id. */
17839 if (!window_row->enabled_p)
17840 frame_row->enabled_p = false;
17841
17842 ++window_row, ++frame_row;
17843 }
17844 }
17845
17846
17847 /* Find the glyph row in window W containing CHARPOS. Consider all
17848 rows between START and END (not inclusive). END null means search
17849 all rows to the end of the display area of W. Value is the row
17850 containing CHARPOS or null. */
17851
17852 struct glyph_row *
17853 row_containing_pos (struct window *w, ptrdiff_t charpos,
17854 struct glyph_row *start, struct glyph_row *end, int dy)
17855 {
17856 struct glyph_row *row = start;
17857 struct glyph_row *best_row = NULL;
17858 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17859 int last_y;
17860
17861 /* If we happen to start on a header-line, skip that. */
17862 if (row->mode_line_p)
17863 ++row;
17864
17865 if ((end && row >= end) || !row->enabled_p)
17866 return NULL;
17867
17868 last_y = window_text_bottom_y (w) - dy;
17869
17870 while (true)
17871 {
17872 /* Give up if we have gone too far. */
17873 if ((end && row >= end) || !row->enabled_p)
17874 return NULL;
17875 /* This formerly returned if they were equal.
17876 I think that both quantities are of a "last plus one" type;
17877 if so, when they are equal, the row is within the screen. -- rms. */
17878 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17879 return NULL;
17880
17881 /* If it is in this row, return this row. */
17882 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17883 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17884 /* The end position of a row equals the start
17885 position of the next row. If CHARPOS is there, we
17886 would rather consider it displayed in the next
17887 line, except when this line ends in ZV. */
17888 && !row_for_charpos_p (row, charpos)))
17889 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17890 {
17891 struct glyph *g;
17892
17893 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17894 || (!best_row && !row->continued_p))
17895 return row;
17896 /* In bidi-reordered rows, there could be several rows whose
17897 edges surround CHARPOS, all of these rows belonging to
17898 the same continued line. We need to find the row which
17899 fits CHARPOS the best. */
17900 for (g = row->glyphs[TEXT_AREA];
17901 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17902 g++)
17903 {
17904 if (!STRINGP (g->object))
17905 {
17906 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17907 {
17908 mindif = eabs (g->charpos - charpos);
17909 best_row = row;
17910 /* Exact match always wins. */
17911 if (mindif == 0)
17912 return best_row;
17913 }
17914 }
17915 }
17916 }
17917 else if (best_row && !row->continued_p)
17918 return best_row;
17919 ++row;
17920 }
17921 }
17922
17923
17924 /* Try to redisplay window W by reusing its existing display. W's
17925 current matrix must be up to date when this function is called,
17926 i.e., window_end_valid must be true.
17927
17928 Value is
17929
17930 >= 1 if successful, i.e. display has been updated
17931 specifically:
17932 1 means the changes were in front of a newline that precedes
17933 the window start, and the whole current matrix was reused
17934 2 means the changes were after the last position displayed
17935 in the window, and the whole current matrix was reused
17936 3 means portions of the current matrix were reused, while
17937 some of the screen lines were redrawn
17938 -1 if redisplay with same window start is known not to succeed
17939 0 if otherwise unsuccessful
17940
17941 The following steps are performed:
17942
17943 1. Find the last row in the current matrix of W that is not
17944 affected by changes at the start of current_buffer. If no such row
17945 is found, give up.
17946
17947 2. Find the first row in W's current matrix that is not affected by
17948 changes at the end of current_buffer. Maybe there is no such row.
17949
17950 3. Display lines beginning with the row + 1 found in step 1 to the
17951 row found in step 2 or, if step 2 didn't find a row, to the end of
17952 the window.
17953
17954 4. If cursor is not known to appear on the window, give up.
17955
17956 5. If display stopped at the row found in step 2, scroll the
17957 display and current matrix as needed.
17958
17959 6. Maybe display some lines at the end of W, if we must. This can
17960 happen under various circumstances, like a partially visible line
17961 becoming fully visible, or because newly displayed lines are displayed
17962 in smaller font sizes.
17963
17964 7. Update W's window end information. */
17965
17966 static int
17967 try_window_id (struct window *w)
17968 {
17969 struct frame *f = XFRAME (w->frame);
17970 struct glyph_matrix *current_matrix = w->current_matrix;
17971 struct glyph_matrix *desired_matrix = w->desired_matrix;
17972 struct glyph_row *last_unchanged_at_beg_row;
17973 struct glyph_row *first_unchanged_at_end_row;
17974 struct glyph_row *row;
17975 struct glyph_row *bottom_row;
17976 int bottom_vpos;
17977 struct it it;
17978 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17979 int dvpos, dy;
17980 struct text_pos start_pos;
17981 struct run run;
17982 int first_unchanged_at_end_vpos = 0;
17983 struct glyph_row *last_text_row, *last_text_row_at_end;
17984 struct text_pos start;
17985 ptrdiff_t first_changed_charpos, last_changed_charpos;
17986
17987 #ifdef GLYPH_DEBUG
17988 if (inhibit_try_window_id)
17989 return 0;
17990 #endif
17991
17992 /* This is handy for debugging. */
17993 #if false
17994 #define GIVE_UP(X) \
17995 do { \
17996 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
17997 return 0; \
17998 } while (false)
17999 #else
18000 #define GIVE_UP(X) return 0
18001 #endif
18002
18003 SET_TEXT_POS_FROM_MARKER (start, w->start);
18004
18005 /* Don't use this for mini-windows because these can show
18006 messages and mini-buffers, and we don't handle that here. */
18007 if (MINI_WINDOW_P (w))
18008 GIVE_UP (1);
18009
18010 /* This flag is used to prevent redisplay optimizations. */
18011 if (windows_or_buffers_changed || f->cursor_type_changed)
18012 GIVE_UP (2);
18013
18014 /* This function's optimizations cannot be used if overlays have
18015 changed in the buffer displayed by the window, so give up if they
18016 have. */
18017 if (w->last_overlay_modified != OVERLAY_MODIFF)
18018 GIVE_UP (200);
18019
18020 /* Verify that narrowing has not changed.
18021 Also verify that we were not told to prevent redisplay optimizations.
18022 It would be nice to further
18023 reduce the number of cases where this prevents try_window_id. */
18024 if (current_buffer->clip_changed
18025 || current_buffer->prevent_redisplay_optimizations_p)
18026 GIVE_UP (3);
18027
18028 /* Window must either use window-based redisplay or be full width. */
18029 if (!FRAME_WINDOW_P (f)
18030 && (!FRAME_LINE_INS_DEL_OK (f)
18031 || !WINDOW_FULL_WIDTH_P (w)))
18032 GIVE_UP (4);
18033
18034 /* Give up if point is known NOT to appear in W. */
18035 if (PT < CHARPOS (start))
18036 GIVE_UP (5);
18037
18038 /* Another way to prevent redisplay optimizations. */
18039 if (w->last_modified == 0)
18040 GIVE_UP (6);
18041
18042 /* Verify that window is not hscrolled. */
18043 if (w->hscroll != 0)
18044 GIVE_UP (7);
18045
18046 /* Verify that display wasn't paused. */
18047 if (!w->window_end_valid)
18048 GIVE_UP (8);
18049
18050 /* Likewise if highlighting trailing whitespace. */
18051 if (!NILP (Vshow_trailing_whitespace))
18052 GIVE_UP (11);
18053
18054 /* Can't use this if overlay arrow position and/or string have
18055 changed. */
18056 if (overlay_arrows_changed_p ())
18057 GIVE_UP (12);
18058
18059 /* When word-wrap is on, adding a space to the first word of a
18060 wrapped line can change the wrap position, altering the line
18061 above it. It might be worthwhile to handle this more
18062 intelligently, but for now just redisplay from scratch. */
18063 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18064 GIVE_UP (21);
18065
18066 /* Under bidi reordering, adding or deleting a character in the
18067 beginning of a paragraph, before the first strong directional
18068 character, can change the base direction of the paragraph (unless
18069 the buffer specifies a fixed paragraph direction), which will
18070 require to redisplay the whole paragraph. It might be worthwhile
18071 to find the paragraph limits and widen the range of redisplayed
18072 lines to that, but for now just give up this optimization and
18073 redisplay from scratch. */
18074 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18075 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18076 GIVE_UP (22);
18077
18078 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18079 to that variable require thorough redisplay. */
18080 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18081 GIVE_UP (23);
18082
18083 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18084 only if buffer has really changed. The reason is that the gap is
18085 initially at Z for freshly visited files. The code below would
18086 set end_unchanged to 0 in that case. */
18087 if (MODIFF > SAVE_MODIFF
18088 /* This seems to happen sometimes after saving a buffer. */
18089 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18090 {
18091 if (GPT - BEG < BEG_UNCHANGED)
18092 BEG_UNCHANGED = GPT - BEG;
18093 if (Z - GPT < END_UNCHANGED)
18094 END_UNCHANGED = Z - GPT;
18095 }
18096
18097 /* The position of the first and last character that has been changed. */
18098 first_changed_charpos = BEG + BEG_UNCHANGED;
18099 last_changed_charpos = Z - END_UNCHANGED;
18100
18101 /* If window starts after a line end, and the last change is in
18102 front of that newline, then changes don't affect the display.
18103 This case happens with stealth-fontification. Note that although
18104 the display is unchanged, glyph positions in the matrix have to
18105 be adjusted, of course. */
18106 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18107 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18108 && ((last_changed_charpos < CHARPOS (start)
18109 && CHARPOS (start) == BEGV)
18110 || (last_changed_charpos < CHARPOS (start) - 1
18111 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18112 {
18113 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18114 struct glyph_row *r0;
18115
18116 /* Compute how many chars/bytes have been added to or removed
18117 from the buffer. */
18118 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18119 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18120 Z_delta = Z - Z_old;
18121 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18122
18123 /* Give up if PT is not in the window. Note that it already has
18124 been checked at the start of try_window_id that PT is not in
18125 front of the window start. */
18126 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18127 GIVE_UP (13);
18128
18129 /* If window start is unchanged, we can reuse the whole matrix
18130 as is, after adjusting glyph positions. No need to compute
18131 the window end again, since its offset from Z hasn't changed. */
18132 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18133 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18134 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18135 /* PT must not be in a partially visible line. */
18136 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18137 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18138 {
18139 /* Adjust positions in the glyph matrix. */
18140 if (Z_delta || Z_delta_bytes)
18141 {
18142 struct glyph_row *r1
18143 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18144 increment_matrix_positions (w->current_matrix,
18145 MATRIX_ROW_VPOS (r0, current_matrix),
18146 MATRIX_ROW_VPOS (r1, current_matrix),
18147 Z_delta, Z_delta_bytes);
18148 }
18149
18150 /* Set the cursor. */
18151 row = row_containing_pos (w, PT, r0, NULL, 0);
18152 if (row)
18153 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18154 return 1;
18155 }
18156 }
18157
18158 /* Handle the case that changes are all below what is displayed in
18159 the window, and that PT is in the window. This shortcut cannot
18160 be taken if ZV is visible in the window, and text has been added
18161 there that is visible in the window. */
18162 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18163 /* ZV is not visible in the window, or there are no
18164 changes at ZV, actually. */
18165 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18166 || first_changed_charpos == last_changed_charpos))
18167 {
18168 struct glyph_row *r0;
18169
18170 /* Give up if PT is not in the window. Note that it already has
18171 been checked at the start of try_window_id that PT is not in
18172 front of the window start. */
18173 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18174 GIVE_UP (14);
18175
18176 /* If window start is unchanged, we can reuse the whole matrix
18177 as is, without changing glyph positions since no text has
18178 been added/removed in front of the window end. */
18179 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18180 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18181 /* PT must not be in a partially visible line. */
18182 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18183 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18184 {
18185 /* We have to compute the window end anew since text
18186 could have been added/removed after it. */
18187 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18188 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18189
18190 /* Set the cursor. */
18191 row = row_containing_pos (w, PT, r0, NULL, 0);
18192 if (row)
18193 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18194 return 2;
18195 }
18196 }
18197
18198 /* Give up if window start is in the changed area.
18199
18200 The condition used to read
18201
18202 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18203
18204 but why that was tested escapes me at the moment. */
18205 if (CHARPOS (start) >= first_changed_charpos
18206 && CHARPOS (start) <= last_changed_charpos)
18207 GIVE_UP (15);
18208
18209 /* Check that window start agrees with the start of the first glyph
18210 row in its current matrix. Check this after we know the window
18211 start is not in changed text, otherwise positions would not be
18212 comparable. */
18213 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18214 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18215 GIVE_UP (16);
18216
18217 /* Give up if the window ends in strings. Overlay strings
18218 at the end are difficult to handle, so don't try. */
18219 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18220 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18221 GIVE_UP (20);
18222
18223 /* Compute the position at which we have to start displaying new
18224 lines. Some of the lines at the top of the window might be
18225 reusable because they are not displaying changed text. Find the
18226 last row in W's current matrix not affected by changes at the
18227 start of current_buffer. Value is null if changes start in the
18228 first line of window. */
18229 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18230 if (last_unchanged_at_beg_row)
18231 {
18232 /* Avoid starting to display in the middle of a character, a TAB
18233 for instance. This is easier than to set up the iterator
18234 exactly, and it's not a frequent case, so the additional
18235 effort wouldn't really pay off. */
18236 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18237 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18238 && last_unchanged_at_beg_row > w->current_matrix->rows)
18239 --last_unchanged_at_beg_row;
18240
18241 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18242 GIVE_UP (17);
18243
18244 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18245 GIVE_UP (18);
18246 start_pos = it.current.pos;
18247
18248 /* Start displaying new lines in the desired matrix at the same
18249 vpos we would use in the current matrix, i.e. below
18250 last_unchanged_at_beg_row. */
18251 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18252 current_matrix);
18253 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18254 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18255
18256 eassert (it.hpos == 0 && it.current_x == 0);
18257 }
18258 else
18259 {
18260 /* There are no reusable lines at the start of the window.
18261 Start displaying in the first text line. */
18262 start_display (&it, w, start);
18263 it.vpos = it.first_vpos;
18264 start_pos = it.current.pos;
18265 }
18266
18267 /* Find the first row that is not affected by changes at the end of
18268 the buffer. Value will be null if there is no unchanged row, in
18269 which case we must redisplay to the end of the window. delta
18270 will be set to the value by which buffer positions beginning with
18271 first_unchanged_at_end_row have to be adjusted due to text
18272 changes. */
18273 first_unchanged_at_end_row
18274 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18275 IF_DEBUG (debug_delta = delta);
18276 IF_DEBUG (debug_delta_bytes = delta_bytes);
18277
18278 /* Set stop_pos to the buffer position up to which we will have to
18279 display new lines. If first_unchanged_at_end_row != NULL, this
18280 is the buffer position of the start of the line displayed in that
18281 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18282 that we don't stop at a buffer position. */
18283 stop_pos = 0;
18284 if (first_unchanged_at_end_row)
18285 {
18286 eassert (last_unchanged_at_beg_row == NULL
18287 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18288
18289 /* If this is a continuation line, move forward to the next one
18290 that isn't. Changes in lines above affect this line.
18291 Caution: this may move first_unchanged_at_end_row to a row
18292 not displaying text. */
18293 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18294 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18295 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18296 < it.last_visible_y))
18297 ++first_unchanged_at_end_row;
18298
18299 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18300 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18301 >= it.last_visible_y))
18302 first_unchanged_at_end_row = NULL;
18303 else
18304 {
18305 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18306 + delta);
18307 first_unchanged_at_end_vpos
18308 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18309 eassert (stop_pos >= Z - END_UNCHANGED);
18310 }
18311 }
18312 else if (last_unchanged_at_beg_row == NULL)
18313 GIVE_UP (19);
18314
18315
18316 #ifdef GLYPH_DEBUG
18317
18318 /* Either there is no unchanged row at the end, or the one we have
18319 now displays text. This is a necessary condition for the window
18320 end pos calculation at the end of this function. */
18321 eassert (first_unchanged_at_end_row == NULL
18322 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18323
18324 debug_last_unchanged_at_beg_vpos
18325 = (last_unchanged_at_beg_row
18326 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18327 : -1);
18328 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18329
18330 #endif /* GLYPH_DEBUG */
18331
18332
18333 /* Display new lines. Set last_text_row to the last new line
18334 displayed which has text on it, i.e. might end up as being the
18335 line where the window_end_vpos is. */
18336 w->cursor.vpos = -1;
18337 last_text_row = NULL;
18338 overlay_arrow_seen = false;
18339 if (it.current_y < it.last_visible_y
18340 && !f->fonts_changed
18341 && (first_unchanged_at_end_row == NULL
18342 || IT_CHARPOS (it) < stop_pos))
18343 it.glyph_row->reversed_p = false;
18344 while (it.current_y < it.last_visible_y
18345 && !f->fonts_changed
18346 && (first_unchanged_at_end_row == NULL
18347 || IT_CHARPOS (it) < stop_pos))
18348 {
18349 if (display_line (&it))
18350 last_text_row = it.glyph_row - 1;
18351 }
18352
18353 if (f->fonts_changed)
18354 return -1;
18355
18356 /* The redisplay iterations in display_line above could have
18357 triggered font-lock, which could have done something that
18358 invalidates IT->w window's end-point information, on which we
18359 rely below. E.g., one package, which will remain unnamed, used
18360 to install a font-lock-fontify-region-function that called
18361 bury-buffer, whose side effect is to switch the buffer displayed
18362 by IT->w, and that predictably resets IT->w's window_end_valid
18363 flag, which we already tested at the entry to this function.
18364 Amply punish such packages/modes by giving up on this
18365 optimization in those cases. */
18366 if (!w->window_end_valid)
18367 {
18368 clear_glyph_matrix (w->desired_matrix);
18369 return -1;
18370 }
18371
18372 /* Compute differences in buffer positions, y-positions etc. for
18373 lines reused at the bottom of the window. Compute what we can
18374 scroll. */
18375 if (first_unchanged_at_end_row
18376 /* No lines reused because we displayed everything up to the
18377 bottom of the window. */
18378 && it.current_y < it.last_visible_y)
18379 {
18380 dvpos = (it.vpos
18381 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18382 current_matrix));
18383 dy = it.current_y - first_unchanged_at_end_row->y;
18384 run.current_y = first_unchanged_at_end_row->y;
18385 run.desired_y = run.current_y + dy;
18386 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18387 }
18388 else
18389 {
18390 delta = delta_bytes = dvpos = dy
18391 = run.current_y = run.desired_y = run.height = 0;
18392 first_unchanged_at_end_row = NULL;
18393 }
18394 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18395
18396
18397 /* Find the cursor if not already found. We have to decide whether
18398 PT will appear on this window (it sometimes doesn't, but this is
18399 not a very frequent case.) This decision has to be made before
18400 the current matrix is altered. A value of cursor.vpos < 0 means
18401 that PT is either in one of the lines beginning at
18402 first_unchanged_at_end_row or below the window. Don't care for
18403 lines that might be displayed later at the window end; as
18404 mentioned, this is not a frequent case. */
18405 if (w->cursor.vpos < 0)
18406 {
18407 /* Cursor in unchanged rows at the top? */
18408 if (PT < CHARPOS (start_pos)
18409 && last_unchanged_at_beg_row)
18410 {
18411 row = row_containing_pos (w, PT,
18412 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18413 last_unchanged_at_beg_row + 1, 0);
18414 if (row)
18415 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18416 }
18417
18418 /* Start from first_unchanged_at_end_row looking for PT. */
18419 else if (first_unchanged_at_end_row)
18420 {
18421 row = row_containing_pos (w, PT - delta,
18422 first_unchanged_at_end_row, NULL, 0);
18423 if (row)
18424 set_cursor_from_row (w, row, w->current_matrix, delta,
18425 delta_bytes, dy, dvpos);
18426 }
18427
18428 /* Give up if cursor was not found. */
18429 if (w->cursor.vpos < 0)
18430 {
18431 clear_glyph_matrix (w->desired_matrix);
18432 return -1;
18433 }
18434 }
18435
18436 /* Don't let the cursor end in the scroll margins. */
18437 {
18438 int this_scroll_margin, cursor_height;
18439 int frame_line_height = default_line_pixel_height (w);
18440 int window_total_lines
18441 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18442
18443 this_scroll_margin =
18444 max (0, min (scroll_margin, window_total_lines / 4));
18445 this_scroll_margin *= frame_line_height;
18446 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18447
18448 if ((w->cursor.y < this_scroll_margin
18449 && CHARPOS (start) > BEGV)
18450 /* Old redisplay didn't take scroll margin into account at the bottom,
18451 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18452 || (w->cursor.y + (make_cursor_line_fully_visible_p
18453 ? cursor_height + this_scroll_margin
18454 : 1)) > it.last_visible_y)
18455 {
18456 w->cursor.vpos = -1;
18457 clear_glyph_matrix (w->desired_matrix);
18458 return -1;
18459 }
18460 }
18461
18462 /* Scroll the display. Do it before changing the current matrix so
18463 that xterm.c doesn't get confused about where the cursor glyph is
18464 found. */
18465 if (dy && run.height)
18466 {
18467 update_begin (f);
18468
18469 if (FRAME_WINDOW_P (f))
18470 {
18471 FRAME_RIF (f)->update_window_begin_hook (w);
18472 FRAME_RIF (f)->clear_window_mouse_face (w);
18473 FRAME_RIF (f)->scroll_run_hook (w, &run);
18474 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18475 }
18476 else
18477 {
18478 /* Terminal frame. In this case, dvpos gives the number of
18479 lines to scroll by; dvpos < 0 means scroll up. */
18480 int from_vpos
18481 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18482 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18483 int end = (WINDOW_TOP_EDGE_LINE (w)
18484 + WINDOW_WANTS_HEADER_LINE_P (w)
18485 + window_internal_height (w));
18486
18487 #if defined (HAVE_GPM) || defined (MSDOS)
18488 x_clear_window_mouse_face (w);
18489 #endif
18490 /* Perform the operation on the screen. */
18491 if (dvpos > 0)
18492 {
18493 /* Scroll last_unchanged_at_beg_row to the end of the
18494 window down dvpos lines. */
18495 set_terminal_window (f, end);
18496
18497 /* On dumb terminals delete dvpos lines at the end
18498 before inserting dvpos empty lines. */
18499 if (!FRAME_SCROLL_REGION_OK (f))
18500 ins_del_lines (f, end - dvpos, -dvpos);
18501
18502 /* Insert dvpos empty lines in front of
18503 last_unchanged_at_beg_row. */
18504 ins_del_lines (f, from, dvpos);
18505 }
18506 else if (dvpos < 0)
18507 {
18508 /* Scroll up last_unchanged_at_beg_vpos to the end of
18509 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18510 set_terminal_window (f, end);
18511
18512 /* Delete dvpos lines in front of
18513 last_unchanged_at_beg_vpos. ins_del_lines will set
18514 the cursor to the given vpos and emit |dvpos| delete
18515 line sequences. */
18516 ins_del_lines (f, from + dvpos, dvpos);
18517
18518 /* On a dumb terminal insert dvpos empty lines at the
18519 end. */
18520 if (!FRAME_SCROLL_REGION_OK (f))
18521 ins_del_lines (f, end + dvpos, -dvpos);
18522 }
18523
18524 set_terminal_window (f, 0);
18525 }
18526
18527 update_end (f);
18528 }
18529
18530 /* Shift reused rows of the current matrix to the right position.
18531 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18532 text. */
18533 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18534 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18535 if (dvpos < 0)
18536 {
18537 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18538 bottom_vpos, dvpos);
18539 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18540 bottom_vpos);
18541 }
18542 else if (dvpos > 0)
18543 {
18544 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18545 bottom_vpos, dvpos);
18546 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18547 first_unchanged_at_end_vpos + dvpos);
18548 }
18549
18550 /* For frame-based redisplay, make sure that current frame and window
18551 matrix are in sync with respect to glyph memory. */
18552 if (!FRAME_WINDOW_P (f))
18553 sync_frame_with_window_matrix_rows (w);
18554
18555 /* Adjust buffer positions in reused rows. */
18556 if (delta || delta_bytes)
18557 increment_matrix_positions (current_matrix,
18558 first_unchanged_at_end_vpos + dvpos,
18559 bottom_vpos, delta, delta_bytes);
18560
18561 /* Adjust Y positions. */
18562 if (dy)
18563 shift_glyph_matrix (w, current_matrix,
18564 first_unchanged_at_end_vpos + dvpos,
18565 bottom_vpos, dy);
18566
18567 if (first_unchanged_at_end_row)
18568 {
18569 first_unchanged_at_end_row += dvpos;
18570 if (first_unchanged_at_end_row->y >= it.last_visible_y
18571 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18572 first_unchanged_at_end_row = NULL;
18573 }
18574
18575 /* If scrolling up, there may be some lines to display at the end of
18576 the window. */
18577 last_text_row_at_end = NULL;
18578 if (dy < 0)
18579 {
18580 /* Scrolling up can leave for example a partially visible line
18581 at the end of the window to be redisplayed. */
18582 /* Set last_row to the glyph row in the current matrix where the
18583 window end line is found. It has been moved up or down in
18584 the matrix by dvpos. */
18585 int last_vpos = w->window_end_vpos + dvpos;
18586 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18587
18588 /* If last_row is the window end line, it should display text. */
18589 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18590
18591 /* If window end line was partially visible before, begin
18592 displaying at that line. Otherwise begin displaying with the
18593 line following it. */
18594 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18595 {
18596 init_to_row_start (&it, w, last_row);
18597 it.vpos = last_vpos;
18598 it.current_y = last_row->y;
18599 }
18600 else
18601 {
18602 init_to_row_end (&it, w, last_row);
18603 it.vpos = 1 + last_vpos;
18604 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18605 ++last_row;
18606 }
18607
18608 /* We may start in a continuation line. If so, we have to
18609 get the right continuation_lines_width and current_x. */
18610 it.continuation_lines_width = last_row->continuation_lines_width;
18611 it.hpos = it.current_x = 0;
18612
18613 /* Display the rest of the lines at the window end. */
18614 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18615 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18616 {
18617 /* Is it always sure that the display agrees with lines in
18618 the current matrix? I don't think so, so we mark rows
18619 displayed invalid in the current matrix by setting their
18620 enabled_p flag to false. */
18621 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18622 if (display_line (&it))
18623 last_text_row_at_end = it.glyph_row - 1;
18624 }
18625 }
18626
18627 /* Update window_end_pos and window_end_vpos. */
18628 if (first_unchanged_at_end_row && !last_text_row_at_end)
18629 {
18630 /* Window end line if one of the preserved rows from the current
18631 matrix. Set row to the last row displaying text in current
18632 matrix starting at first_unchanged_at_end_row, after
18633 scrolling. */
18634 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18635 row = find_last_row_displaying_text (w->current_matrix, &it,
18636 first_unchanged_at_end_row);
18637 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18638 adjust_window_ends (w, row, true);
18639 eassert (w->window_end_bytepos >= 0);
18640 IF_DEBUG (debug_method_add (w, "A"));
18641 }
18642 else if (last_text_row_at_end)
18643 {
18644 adjust_window_ends (w, last_text_row_at_end, false);
18645 eassert (w->window_end_bytepos >= 0);
18646 IF_DEBUG (debug_method_add (w, "B"));
18647 }
18648 else if (last_text_row)
18649 {
18650 /* We have displayed either to the end of the window or at the
18651 end of the window, i.e. the last row with text is to be found
18652 in the desired matrix. */
18653 adjust_window_ends (w, last_text_row, false);
18654 eassert (w->window_end_bytepos >= 0);
18655 }
18656 else if (first_unchanged_at_end_row == NULL
18657 && last_text_row == NULL
18658 && last_text_row_at_end == NULL)
18659 {
18660 /* Displayed to end of window, but no line containing text was
18661 displayed. Lines were deleted at the end of the window. */
18662 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18663 int vpos = w->window_end_vpos;
18664 struct glyph_row *current_row = current_matrix->rows + vpos;
18665 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18666
18667 for (row = NULL;
18668 row == NULL && vpos >= first_vpos;
18669 --vpos, --current_row, --desired_row)
18670 {
18671 if (desired_row->enabled_p)
18672 {
18673 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18674 row = desired_row;
18675 }
18676 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18677 row = current_row;
18678 }
18679
18680 eassert (row != NULL);
18681 w->window_end_vpos = vpos + 1;
18682 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18683 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18684 eassert (w->window_end_bytepos >= 0);
18685 IF_DEBUG (debug_method_add (w, "C"));
18686 }
18687 else
18688 emacs_abort ();
18689
18690 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18691 debug_end_vpos = w->window_end_vpos));
18692
18693 /* Record that display has not been completed. */
18694 w->window_end_valid = false;
18695 w->desired_matrix->no_scrolling_p = true;
18696 return 3;
18697
18698 #undef GIVE_UP
18699 }
18700
18701
18702 \f
18703 /***********************************************************************
18704 More debugging support
18705 ***********************************************************************/
18706
18707 #ifdef GLYPH_DEBUG
18708
18709 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18710 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18711 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18712
18713
18714 /* Dump the contents of glyph matrix MATRIX on stderr.
18715
18716 GLYPHS 0 means don't show glyph contents.
18717 GLYPHS 1 means show glyphs in short form
18718 GLYPHS > 1 means show glyphs in long form. */
18719
18720 void
18721 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18722 {
18723 int i;
18724 for (i = 0; i < matrix->nrows; ++i)
18725 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18726 }
18727
18728
18729 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18730 the glyph row and area where the glyph comes from. */
18731
18732 void
18733 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18734 {
18735 if (glyph->type == CHAR_GLYPH
18736 || glyph->type == GLYPHLESS_GLYPH)
18737 {
18738 fprintf (stderr,
18739 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18740 glyph - row->glyphs[TEXT_AREA],
18741 (glyph->type == CHAR_GLYPH
18742 ? 'C'
18743 : 'G'),
18744 glyph->charpos,
18745 (BUFFERP (glyph->object)
18746 ? 'B'
18747 : (STRINGP (glyph->object)
18748 ? 'S'
18749 : (NILP (glyph->object)
18750 ? '0'
18751 : '-'))),
18752 glyph->pixel_width,
18753 glyph->u.ch,
18754 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18755 ? glyph->u.ch
18756 : '.'),
18757 glyph->face_id,
18758 glyph->left_box_line_p,
18759 glyph->right_box_line_p);
18760 }
18761 else if (glyph->type == STRETCH_GLYPH)
18762 {
18763 fprintf (stderr,
18764 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18765 glyph - row->glyphs[TEXT_AREA],
18766 'S',
18767 glyph->charpos,
18768 (BUFFERP (glyph->object)
18769 ? 'B'
18770 : (STRINGP (glyph->object)
18771 ? 'S'
18772 : (NILP (glyph->object)
18773 ? '0'
18774 : '-'))),
18775 glyph->pixel_width,
18776 0,
18777 ' ',
18778 glyph->face_id,
18779 glyph->left_box_line_p,
18780 glyph->right_box_line_p);
18781 }
18782 else if (glyph->type == IMAGE_GLYPH)
18783 {
18784 fprintf (stderr,
18785 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18786 glyph - row->glyphs[TEXT_AREA],
18787 'I',
18788 glyph->charpos,
18789 (BUFFERP (glyph->object)
18790 ? 'B'
18791 : (STRINGP (glyph->object)
18792 ? 'S'
18793 : (NILP (glyph->object)
18794 ? '0'
18795 : '-'))),
18796 glyph->pixel_width,
18797 glyph->u.img_id,
18798 '.',
18799 glyph->face_id,
18800 glyph->left_box_line_p,
18801 glyph->right_box_line_p);
18802 }
18803 else if (glyph->type == COMPOSITE_GLYPH)
18804 {
18805 fprintf (stderr,
18806 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18807 glyph - row->glyphs[TEXT_AREA],
18808 '+',
18809 glyph->charpos,
18810 (BUFFERP (glyph->object)
18811 ? 'B'
18812 : (STRINGP (glyph->object)
18813 ? 'S'
18814 : (NILP (glyph->object)
18815 ? '0'
18816 : '-'))),
18817 glyph->pixel_width,
18818 glyph->u.cmp.id);
18819 if (glyph->u.cmp.automatic)
18820 fprintf (stderr,
18821 "[%d-%d]",
18822 glyph->slice.cmp.from, glyph->slice.cmp.to);
18823 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18824 glyph->face_id,
18825 glyph->left_box_line_p,
18826 glyph->right_box_line_p);
18827 }
18828 else if (glyph->type == XWIDGET_GLYPH)
18829 {
18830 fprintf (stderr,
18831 #ifdef HAVE_XWIDGETS
18832 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18833 #else
18834 " %5d %4c %6d %c %3d %c %4d %1.1d%1.1d\n",
18835 #endif
18836 glyph - row->glyphs[TEXT_AREA],
18837 'X',
18838 glyph->charpos,
18839 (BUFFERP (glyph->object)
18840 ? 'B'
18841 : (STRINGP (glyph->object)
18842 ? 'S'
18843 : '-')),
18844 glyph->pixel_width,
18845 #ifdef HAVE_XWIDGETS
18846 glyph->u.xwidget,
18847 #endif
18848 '.',
18849 glyph->face_id,
18850 glyph->left_box_line_p,
18851 glyph->right_box_line_p);
18852
18853 }
18854 }
18855
18856
18857 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18858 GLYPHS 0 means don't show glyph contents.
18859 GLYPHS 1 means show glyphs in short form
18860 GLYPHS > 1 means show glyphs in long form. */
18861
18862 void
18863 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18864 {
18865 if (glyphs != 1)
18866 {
18867 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18868 fprintf (stderr, "==============================================================================\n");
18869
18870 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18871 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18872 vpos,
18873 MATRIX_ROW_START_CHARPOS (row),
18874 MATRIX_ROW_END_CHARPOS (row),
18875 row->used[TEXT_AREA],
18876 row->contains_overlapping_glyphs_p,
18877 row->enabled_p,
18878 row->truncated_on_left_p,
18879 row->truncated_on_right_p,
18880 row->continued_p,
18881 MATRIX_ROW_CONTINUATION_LINE_P (row),
18882 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18883 row->ends_at_zv_p,
18884 row->fill_line_p,
18885 row->ends_in_middle_of_char_p,
18886 row->starts_in_middle_of_char_p,
18887 row->mouse_face_p,
18888 row->x,
18889 row->y,
18890 row->pixel_width,
18891 row->height,
18892 row->visible_height,
18893 row->ascent,
18894 row->phys_ascent);
18895 /* The next 3 lines should align to "Start" in the header. */
18896 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18897 row->end.overlay_string_index,
18898 row->continuation_lines_width);
18899 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18900 CHARPOS (row->start.string_pos),
18901 CHARPOS (row->end.string_pos));
18902 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18903 row->end.dpvec_index);
18904 }
18905
18906 if (glyphs > 1)
18907 {
18908 int area;
18909
18910 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18911 {
18912 struct glyph *glyph = row->glyphs[area];
18913 struct glyph *glyph_end = glyph + row->used[area];
18914
18915 /* Glyph for a line end in text. */
18916 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18917 ++glyph_end;
18918
18919 if (glyph < glyph_end)
18920 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18921
18922 for (; glyph < glyph_end; ++glyph)
18923 dump_glyph (row, glyph, area);
18924 }
18925 }
18926 else if (glyphs == 1)
18927 {
18928 int area;
18929 char s[SHRT_MAX + 4];
18930
18931 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18932 {
18933 int i;
18934
18935 for (i = 0; i < row->used[area]; ++i)
18936 {
18937 struct glyph *glyph = row->glyphs[area] + i;
18938 if (i == row->used[area] - 1
18939 && area == TEXT_AREA
18940 && NILP (glyph->object)
18941 && glyph->type == CHAR_GLYPH
18942 && glyph->u.ch == ' ')
18943 {
18944 strcpy (&s[i], "[\\n]");
18945 i += 4;
18946 }
18947 else if (glyph->type == CHAR_GLYPH
18948 && glyph->u.ch < 0x80
18949 && glyph->u.ch >= ' ')
18950 s[i] = glyph->u.ch;
18951 else
18952 s[i] = '.';
18953 }
18954
18955 s[i] = '\0';
18956 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18957 }
18958 }
18959 }
18960
18961
18962 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18963 Sdump_glyph_matrix, 0, 1, "p",
18964 doc: /* Dump the current matrix of the selected window to stderr.
18965 Shows contents of glyph row structures. With non-nil
18966 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18967 glyphs in short form, otherwise show glyphs in long form.
18968
18969 Interactively, no argument means show glyphs in short form;
18970 with numeric argument, its value is passed as the GLYPHS flag. */)
18971 (Lisp_Object glyphs)
18972 {
18973 struct window *w = XWINDOW (selected_window);
18974 struct buffer *buffer = XBUFFER (w->contents);
18975
18976 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18977 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18978 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18979 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18980 fprintf (stderr, "=============================================\n");
18981 dump_glyph_matrix (w->current_matrix,
18982 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18983 return Qnil;
18984 }
18985
18986
18987 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18988 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18989 Only text-mode frames have frame glyph matrices. */)
18990 (void)
18991 {
18992 struct frame *f = XFRAME (selected_frame);
18993
18994 if (f->current_matrix)
18995 dump_glyph_matrix (f->current_matrix, 1);
18996 else
18997 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18998 return Qnil;
18999 }
19000
19001
19002 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19003 doc: /* Dump glyph row ROW to stderr.
19004 GLYPH 0 means don't dump glyphs.
19005 GLYPH 1 means dump glyphs in short form.
19006 GLYPH > 1 or omitted means dump glyphs in long form. */)
19007 (Lisp_Object row, Lisp_Object glyphs)
19008 {
19009 struct glyph_matrix *matrix;
19010 EMACS_INT vpos;
19011
19012 CHECK_NUMBER (row);
19013 matrix = XWINDOW (selected_window)->current_matrix;
19014 vpos = XINT (row);
19015 if (vpos >= 0 && vpos < matrix->nrows)
19016 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19017 vpos,
19018 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19019 return Qnil;
19020 }
19021
19022
19023 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19024 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19025 GLYPH 0 means don't dump glyphs.
19026 GLYPH 1 means dump glyphs in short form.
19027 GLYPH > 1 or omitted means dump glyphs in long form.
19028
19029 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19030 do nothing. */)
19031 (Lisp_Object row, Lisp_Object glyphs)
19032 {
19033 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19034 struct frame *sf = SELECTED_FRAME ();
19035 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19036 EMACS_INT vpos;
19037
19038 CHECK_NUMBER (row);
19039 vpos = XINT (row);
19040 if (vpos >= 0 && vpos < m->nrows)
19041 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19042 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19043 #endif
19044 return Qnil;
19045 }
19046
19047
19048 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19049 doc: /* Toggle tracing of redisplay.
19050 With ARG, turn tracing on if and only if ARG is positive. */)
19051 (Lisp_Object arg)
19052 {
19053 if (NILP (arg))
19054 trace_redisplay_p = !trace_redisplay_p;
19055 else
19056 {
19057 arg = Fprefix_numeric_value (arg);
19058 trace_redisplay_p = XINT (arg) > 0;
19059 }
19060
19061 return Qnil;
19062 }
19063
19064
19065 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19066 doc: /* Like `format', but print result to stderr.
19067 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19068 (ptrdiff_t nargs, Lisp_Object *args)
19069 {
19070 Lisp_Object s = Fformat (nargs, args);
19071 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19072 return Qnil;
19073 }
19074
19075 #endif /* GLYPH_DEBUG */
19076
19077
19078 \f
19079 /***********************************************************************
19080 Building Desired Matrix Rows
19081 ***********************************************************************/
19082
19083 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19084 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19085
19086 static struct glyph_row *
19087 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19088 {
19089 struct frame *f = XFRAME (WINDOW_FRAME (w));
19090 struct buffer *buffer = XBUFFER (w->contents);
19091 struct buffer *old = current_buffer;
19092 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19093 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19094 const unsigned char *arrow_end = arrow_string + arrow_len;
19095 const unsigned char *p;
19096 struct it it;
19097 bool multibyte_p;
19098 int n_glyphs_before;
19099
19100 set_buffer_temp (buffer);
19101 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19102 scratch_glyph_row.reversed_p = false;
19103 it.glyph_row->used[TEXT_AREA] = 0;
19104 SET_TEXT_POS (it.position, 0, 0);
19105
19106 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19107 p = arrow_string;
19108 while (p < arrow_end)
19109 {
19110 Lisp_Object face, ilisp;
19111
19112 /* Get the next character. */
19113 if (multibyte_p)
19114 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19115 else
19116 {
19117 it.c = it.char_to_display = *p, it.len = 1;
19118 if (! ASCII_CHAR_P (it.c))
19119 it.char_to_display = BYTE8_TO_CHAR (it.c);
19120 }
19121 p += it.len;
19122
19123 /* Get its face. */
19124 ilisp = make_number (p - arrow_string);
19125 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19126 it.face_id = compute_char_face (f, it.char_to_display, face);
19127
19128 /* Compute its width, get its glyphs. */
19129 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19130 SET_TEXT_POS (it.position, -1, -1);
19131 PRODUCE_GLYPHS (&it);
19132
19133 /* If this character doesn't fit any more in the line, we have
19134 to remove some glyphs. */
19135 if (it.current_x > it.last_visible_x)
19136 {
19137 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19138 break;
19139 }
19140 }
19141
19142 set_buffer_temp (old);
19143 return it.glyph_row;
19144 }
19145
19146
19147 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19148 glyphs to insert is determined by produce_special_glyphs. */
19149
19150 static void
19151 insert_left_trunc_glyphs (struct it *it)
19152 {
19153 struct it truncate_it;
19154 struct glyph *from, *end, *to, *toend;
19155
19156 eassert (!FRAME_WINDOW_P (it->f)
19157 || (!it->glyph_row->reversed_p
19158 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19159 || (it->glyph_row->reversed_p
19160 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19161
19162 /* Get the truncation glyphs. */
19163 truncate_it = *it;
19164 truncate_it.current_x = 0;
19165 truncate_it.face_id = DEFAULT_FACE_ID;
19166 truncate_it.glyph_row = &scratch_glyph_row;
19167 truncate_it.area = TEXT_AREA;
19168 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19169 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19170 truncate_it.object = Qnil;
19171 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19172
19173 /* Overwrite glyphs from IT with truncation glyphs. */
19174 if (!it->glyph_row->reversed_p)
19175 {
19176 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19177
19178 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19179 end = from + tused;
19180 to = it->glyph_row->glyphs[TEXT_AREA];
19181 toend = to + it->glyph_row->used[TEXT_AREA];
19182 if (FRAME_WINDOW_P (it->f))
19183 {
19184 /* On GUI frames, when variable-size fonts are displayed,
19185 the truncation glyphs may need more pixels than the row's
19186 glyphs they overwrite. We overwrite more glyphs to free
19187 enough screen real estate, and enlarge the stretch glyph
19188 on the right (see display_line), if there is one, to
19189 preserve the screen position of the truncation glyphs on
19190 the right. */
19191 int w = 0;
19192 struct glyph *g = to;
19193 short used;
19194
19195 /* The first glyph could be partially visible, in which case
19196 it->glyph_row->x will be negative. But we want the left
19197 truncation glyphs to be aligned at the left margin of the
19198 window, so we override the x coordinate at which the row
19199 will begin. */
19200 it->glyph_row->x = 0;
19201 while (g < toend && w < it->truncation_pixel_width)
19202 {
19203 w += g->pixel_width;
19204 ++g;
19205 }
19206 if (g - to - tused > 0)
19207 {
19208 memmove (to + tused, g, (toend - g) * sizeof(*g));
19209 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19210 }
19211 used = it->glyph_row->used[TEXT_AREA];
19212 if (it->glyph_row->truncated_on_right_p
19213 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19214 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19215 == STRETCH_GLYPH)
19216 {
19217 int extra = w - it->truncation_pixel_width;
19218
19219 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19220 }
19221 }
19222
19223 while (from < end)
19224 *to++ = *from++;
19225
19226 /* There may be padding glyphs left over. Overwrite them too. */
19227 if (!FRAME_WINDOW_P (it->f))
19228 {
19229 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19230 {
19231 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19232 while (from < end)
19233 *to++ = *from++;
19234 }
19235 }
19236
19237 if (to > toend)
19238 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19239 }
19240 else
19241 {
19242 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19243
19244 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19245 that back to front. */
19246 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19247 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19248 toend = it->glyph_row->glyphs[TEXT_AREA];
19249 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19250 if (FRAME_WINDOW_P (it->f))
19251 {
19252 int w = 0;
19253 struct glyph *g = to;
19254
19255 while (g >= toend && w < it->truncation_pixel_width)
19256 {
19257 w += g->pixel_width;
19258 --g;
19259 }
19260 if (to - g - tused > 0)
19261 to = g + tused;
19262 if (it->glyph_row->truncated_on_right_p
19263 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19264 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19265 {
19266 int extra = w - it->truncation_pixel_width;
19267
19268 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19269 }
19270 }
19271
19272 while (from >= end && to >= toend)
19273 *to-- = *from--;
19274 if (!FRAME_WINDOW_P (it->f))
19275 {
19276 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19277 {
19278 from =
19279 truncate_it.glyph_row->glyphs[TEXT_AREA]
19280 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19281 while (from >= end && to >= toend)
19282 *to-- = *from--;
19283 }
19284 }
19285 if (from >= end)
19286 {
19287 /* Need to free some room before prepending additional
19288 glyphs. */
19289 int move_by = from - end + 1;
19290 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19291 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19292
19293 for ( ; g >= g0; g--)
19294 g[move_by] = *g;
19295 while (from >= end)
19296 *to-- = *from--;
19297 it->glyph_row->used[TEXT_AREA] += move_by;
19298 }
19299 }
19300 }
19301
19302 /* Compute the hash code for ROW. */
19303 unsigned
19304 row_hash (struct glyph_row *row)
19305 {
19306 int area, k;
19307 unsigned hashval = 0;
19308
19309 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19310 for (k = 0; k < row->used[area]; ++k)
19311 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19312 + row->glyphs[area][k].u.val
19313 + row->glyphs[area][k].face_id
19314 + row->glyphs[area][k].padding_p
19315 + (row->glyphs[area][k].type << 2));
19316
19317 return hashval;
19318 }
19319
19320 /* Compute the pixel height and width of IT->glyph_row.
19321
19322 Most of the time, ascent and height of a display line will be equal
19323 to the max_ascent and max_height values of the display iterator
19324 structure. This is not the case if
19325
19326 1. We hit ZV without displaying anything. In this case, max_ascent
19327 and max_height will be zero.
19328
19329 2. We have some glyphs that don't contribute to the line height.
19330 (The glyph row flag contributes_to_line_height_p is for future
19331 pixmap extensions).
19332
19333 The first case is easily covered by using default values because in
19334 these cases, the line height does not really matter, except that it
19335 must not be zero. */
19336
19337 static void
19338 compute_line_metrics (struct it *it)
19339 {
19340 struct glyph_row *row = it->glyph_row;
19341
19342 if (FRAME_WINDOW_P (it->f))
19343 {
19344 int i, min_y, max_y;
19345
19346 /* The line may consist of one space only, that was added to
19347 place the cursor on it. If so, the row's height hasn't been
19348 computed yet. */
19349 if (row->height == 0)
19350 {
19351 if (it->max_ascent + it->max_descent == 0)
19352 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19353 row->ascent = it->max_ascent;
19354 row->height = it->max_ascent + it->max_descent;
19355 row->phys_ascent = it->max_phys_ascent;
19356 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19357 row->extra_line_spacing = it->max_extra_line_spacing;
19358 }
19359
19360 /* Compute the width of this line. */
19361 row->pixel_width = row->x;
19362 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19363 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19364
19365 eassert (row->pixel_width >= 0);
19366 eassert (row->ascent >= 0 && row->height > 0);
19367
19368 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19369 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19370
19371 /* If first line's physical ascent is larger than its logical
19372 ascent, use the physical ascent, and make the row taller.
19373 This makes accented characters fully visible. */
19374 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19375 && row->phys_ascent > row->ascent)
19376 {
19377 row->height += row->phys_ascent - row->ascent;
19378 row->ascent = row->phys_ascent;
19379 }
19380
19381 /* Compute how much of the line is visible. */
19382 row->visible_height = row->height;
19383
19384 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19385 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19386
19387 if (row->y < min_y)
19388 row->visible_height -= min_y - row->y;
19389 if (row->y + row->height > max_y)
19390 row->visible_height -= row->y + row->height - max_y;
19391 }
19392 else
19393 {
19394 row->pixel_width = row->used[TEXT_AREA];
19395 if (row->continued_p)
19396 row->pixel_width -= it->continuation_pixel_width;
19397 else if (row->truncated_on_right_p)
19398 row->pixel_width -= it->truncation_pixel_width;
19399 row->ascent = row->phys_ascent = 0;
19400 row->height = row->phys_height = row->visible_height = 1;
19401 row->extra_line_spacing = 0;
19402 }
19403
19404 /* Compute a hash code for this row. */
19405 row->hash = row_hash (row);
19406
19407 it->max_ascent = it->max_descent = 0;
19408 it->max_phys_ascent = it->max_phys_descent = 0;
19409 }
19410
19411
19412 /* Append one space to the glyph row of iterator IT if doing a
19413 window-based redisplay. The space has the same face as
19414 IT->face_id. Value is true if a space was added.
19415
19416 This function is called to make sure that there is always one glyph
19417 at the end of a glyph row that the cursor can be set on under
19418 window-systems. (If there weren't such a glyph we would not know
19419 how wide and tall a box cursor should be displayed).
19420
19421 At the same time this space let's a nicely handle clearing to the
19422 end of the line if the row ends in italic text. */
19423
19424 static bool
19425 append_space_for_newline (struct it *it, bool default_face_p)
19426 {
19427 if (FRAME_WINDOW_P (it->f))
19428 {
19429 int n = it->glyph_row->used[TEXT_AREA];
19430
19431 if (it->glyph_row->glyphs[TEXT_AREA] + n
19432 < it->glyph_row->glyphs[1 + TEXT_AREA])
19433 {
19434 /* Save some values that must not be changed.
19435 Must save IT->c and IT->len because otherwise
19436 ITERATOR_AT_END_P wouldn't work anymore after
19437 append_space_for_newline has been called. */
19438 enum display_element_type saved_what = it->what;
19439 int saved_c = it->c, saved_len = it->len;
19440 int saved_char_to_display = it->char_to_display;
19441 int saved_x = it->current_x;
19442 int saved_face_id = it->face_id;
19443 bool saved_box_end = it->end_of_box_run_p;
19444 struct text_pos saved_pos;
19445 Lisp_Object saved_object;
19446 struct face *face;
19447 struct glyph *g;
19448
19449 saved_object = it->object;
19450 saved_pos = it->position;
19451
19452 it->what = IT_CHARACTER;
19453 memset (&it->position, 0, sizeof it->position);
19454 it->object = Qnil;
19455 it->c = it->char_to_display = ' ';
19456 it->len = 1;
19457
19458 /* If the default face was remapped, be sure to use the
19459 remapped face for the appended newline. */
19460 if (default_face_p)
19461 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19462 else if (it->face_before_selective_p)
19463 it->face_id = it->saved_face_id;
19464 face = FACE_FROM_ID (it->f, it->face_id);
19465 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19466 /* In R2L rows, we will prepend a stretch glyph that will
19467 have the end_of_box_run_p flag set for it, so there's no
19468 need for the appended newline glyph to have that flag
19469 set. */
19470 if (it->glyph_row->reversed_p
19471 /* But if the appended newline glyph goes all the way to
19472 the end of the row, there will be no stretch glyph,
19473 so leave the box flag set. */
19474 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19475 it->end_of_box_run_p = false;
19476
19477 PRODUCE_GLYPHS (it);
19478
19479 #ifdef HAVE_WINDOW_SYSTEM
19480 /* Make sure this space glyph has the right ascent and
19481 descent values, or else cursor at end of line will look
19482 funny, and height of empty lines will be incorrect. */
19483 g = it->glyph_row->glyphs[TEXT_AREA] + n;
19484 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19485 if (n == 0)
19486 {
19487 Lisp_Object height, total_height;
19488 int extra_line_spacing = it->extra_line_spacing;
19489 int boff = font->baseline_offset;
19490
19491 if (font->vertical_centering)
19492 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19493
19494 it->object = saved_object; /* get_it_property needs this */
19495 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19496 /* Must do a subset of line height processing from
19497 x_produce_glyph for newline characters. */
19498 height = get_it_property (it, Qline_height);
19499 if (CONSP (height)
19500 && CONSP (XCDR (height))
19501 && NILP (XCDR (XCDR (height))))
19502 {
19503 total_height = XCAR (XCDR (height));
19504 height = XCAR (height);
19505 }
19506 else
19507 total_height = Qnil;
19508 height = calc_line_height_property (it, height, font, boff, true);
19509
19510 if (it->override_ascent >= 0)
19511 {
19512 it->ascent = it->override_ascent;
19513 it->descent = it->override_descent;
19514 boff = it->override_boff;
19515 }
19516 if (EQ (height, Qt))
19517 extra_line_spacing = 0;
19518 else
19519 {
19520 Lisp_Object spacing;
19521
19522 it->phys_ascent = it->ascent;
19523 it->phys_descent = it->descent;
19524 if (!NILP (height)
19525 && XINT (height) > it->ascent + it->descent)
19526 it->ascent = XINT (height) - it->descent;
19527
19528 if (!NILP (total_height))
19529 spacing = calc_line_height_property (it, total_height, font,
19530 boff, false);
19531 else
19532 {
19533 spacing = get_it_property (it, Qline_spacing);
19534 spacing = calc_line_height_property (it, spacing, font,
19535 boff, false);
19536 }
19537 if (INTEGERP (spacing))
19538 {
19539 extra_line_spacing = XINT (spacing);
19540 if (!NILP (total_height))
19541 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19542 }
19543 }
19544 if (extra_line_spacing > 0)
19545 {
19546 it->descent += extra_line_spacing;
19547 if (extra_line_spacing > it->max_extra_line_spacing)
19548 it->max_extra_line_spacing = extra_line_spacing;
19549 }
19550 it->max_ascent = it->ascent;
19551 it->max_descent = it->descent;
19552 /* Make sure compute_line_metrics recomputes the row height. */
19553 it->glyph_row->height = 0;
19554 }
19555
19556 g->ascent = it->max_ascent;
19557 g->descent = it->max_descent;
19558 #endif
19559
19560 it->override_ascent = -1;
19561 it->constrain_row_ascent_descent_p = false;
19562 it->current_x = saved_x;
19563 it->object = saved_object;
19564 it->position = saved_pos;
19565 it->what = saved_what;
19566 it->face_id = saved_face_id;
19567 it->len = saved_len;
19568 it->c = saved_c;
19569 it->char_to_display = saved_char_to_display;
19570 it->end_of_box_run_p = saved_box_end;
19571 return true;
19572 }
19573 }
19574
19575 return false;
19576 }
19577
19578
19579 /* Extend the face of the last glyph in the text area of IT->glyph_row
19580 to the end of the display line. Called from display_line. If the
19581 glyph row is empty, add a space glyph to it so that we know the
19582 face to draw. Set the glyph row flag fill_line_p. If the glyph
19583 row is R2L, prepend a stretch glyph to cover the empty space to the
19584 left of the leftmost glyph. */
19585
19586 static void
19587 extend_face_to_end_of_line (struct it *it)
19588 {
19589 struct face *face, *default_face;
19590 struct frame *f = it->f;
19591
19592 /* If line is already filled, do nothing. Non window-system frames
19593 get a grace of one more ``pixel'' because their characters are
19594 1-``pixel'' wide, so they hit the equality too early. This grace
19595 is needed only for R2L rows that are not continued, to produce
19596 one extra blank where we could display the cursor. */
19597 if ((it->current_x >= it->last_visible_x
19598 + (!FRAME_WINDOW_P (f)
19599 && it->glyph_row->reversed_p
19600 && !it->glyph_row->continued_p))
19601 /* If the window has display margins, we will need to extend
19602 their face even if the text area is filled. */
19603 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19604 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19605 return;
19606
19607 /* The default face, possibly remapped. */
19608 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19609
19610 /* Face extension extends the background and box of IT->face_id
19611 to the end of the line. If the background equals the background
19612 of the frame, we don't have to do anything. */
19613 if (it->face_before_selective_p)
19614 face = FACE_FROM_ID (f, it->saved_face_id);
19615 else
19616 face = FACE_FROM_ID (f, it->face_id);
19617
19618 if (FRAME_WINDOW_P (f)
19619 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19620 && face->box == FACE_NO_BOX
19621 && face->background == FRAME_BACKGROUND_PIXEL (f)
19622 #ifdef HAVE_WINDOW_SYSTEM
19623 && !face->stipple
19624 #endif
19625 && !it->glyph_row->reversed_p)
19626 return;
19627
19628 /* Set the glyph row flag indicating that the face of the last glyph
19629 in the text area has to be drawn to the end of the text area. */
19630 it->glyph_row->fill_line_p = true;
19631
19632 /* If current character of IT is not ASCII, make sure we have the
19633 ASCII face. This will be automatically undone the next time
19634 get_next_display_element returns a multibyte character. Note
19635 that the character will always be single byte in unibyte
19636 text. */
19637 if (!ASCII_CHAR_P (it->c))
19638 {
19639 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19640 }
19641
19642 if (FRAME_WINDOW_P (f))
19643 {
19644 /* If the row is empty, add a space with the current face of IT,
19645 so that we know which face to draw. */
19646 if (it->glyph_row->used[TEXT_AREA] == 0)
19647 {
19648 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19649 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19650 it->glyph_row->used[TEXT_AREA] = 1;
19651 }
19652 /* Mode line and the header line don't have margins, and
19653 likewise the frame's tool-bar window, if there is any. */
19654 if (!(it->glyph_row->mode_line_p
19655 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19656 || (WINDOWP (f->tool_bar_window)
19657 && it->w == XWINDOW (f->tool_bar_window))
19658 #endif
19659 ))
19660 {
19661 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19662 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19663 {
19664 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19665 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19666 default_face->id;
19667 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19668 }
19669 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19670 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19671 {
19672 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19673 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19674 default_face->id;
19675 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19676 }
19677 }
19678 #ifdef HAVE_WINDOW_SYSTEM
19679 if (it->glyph_row->reversed_p)
19680 {
19681 /* Prepend a stretch glyph to the row, such that the
19682 rightmost glyph will be drawn flushed all the way to the
19683 right margin of the window. The stretch glyph that will
19684 occupy the empty space, if any, to the left of the
19685 glyphs. */
19686 struct font *font = face->font ? face->font : FRAME_FONT (f);
19687 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19688 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19689 struct glyph *g;
19690 int row_width, stretch_ascent, stretch_width;
19691 struct text_pos saved_pos;
19692 int saved_face_id;
19693 bool saved_avoid_cursor, saved_box_start;
19694
19695 for (row_width = 0, g = row_start; g < row_end; g++)
19696 row_width += g->pixel_width;
19697
19698 /* FIXME: There are various minor display glitches in R2L
19699 rows when only one of the fringes is missing. The
19700 strange condition below produces the least bad effect. */
19701 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19702 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19703 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19704 stretch_width = window_box_width (it->w, TEXT_AREA);
19705 else
19706 stretch_width = it->last_visible_x - it->first_visible_x;
19707 stretch_width -= row_width;
19708
19709 if (stretch_width > 0)
19710 {
19711 stretch_ascent =
19712 (((it->ascent + it->descent)
19713 * FONT_BASE (font)) / FONT_HEIGHT (font));
19714 saved_pos = it->position;
19715 memset (&it->position, 0, sizeof it->position);
19716 saved_avoid_cursor = it->avoid_cursor_p;
19717 it->avoid_cursor_p = true;
19718 saved_face_id = it->face_id;
19719 saved_box_start = it->start_of_box_run_p;
19720 /* The last row's stretch glyph should get the default
19721 face, to avoid painting the rest of the window with
19722 the region face, if the region ends at ZV. */
19723 if (it->glyph_row->ends_at_zv_p)
19724 it->face_id = default_face->id;
19725 else
19726 it->face_id = face->id;
19727 it->start_of_box_run_p = false;
19728 append_stretch_glyph (it, Qnil, stretch_width,
19729 it->ascent + it->descent, stretch_ascent);
19730 it->position = saved_pos;
19731 it->avoid_cursor_p = saved_avoid_cursor;
19732 it->face_id = saved_face_id;
19733 it->start_of_box_run_p = saved_box_start;
19734 }
19735 /* If stretch_width comes out negative, it means that the
19736 last glyph is only partially visible. In R2L rows, we
19737 want the leftmost glyph to be partially visible, so we
19738 need to give the row the corresponding left offset. */
19739 if (stretch_width < 0)
19740 it->glyph_row->x = stretch_width;
19741 }
19742 #endif /* HAVE_WINDOW_SYSTEM */
19743 }
19744 else
19745 {
19746 /* Save some values that must not be changed. */
19747 int saved_x = it->current_x;
19748 struct text_pos saved_pos;
19749 Lisp_Object saved_object;
19750 enum display_element_type saved_what = it->what;
19751 int saved_face_id = it->face_id;
19752
19753 saved_object = it->object;
19754 saved_pos = it->position;
19755
19756 it->what = IT_CHARACTER;
19757 memset (&it->position, 0, sizeof it->position);
19758 it->object = Qnil;
19759 it->c = it->char_to_display = ' ';
19760 it->len = 1;
19761
19762 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19763 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19764 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19765 && !it->glyph_row->mode_line_p
19766 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19767 {
19768 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19769 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19770
19771 for (it->current_x = 0; g < e; g++)
19772 it->current_x += g->pixel_width;
19773
19774 it->area = LEFT_MARGIN_AREA;
19775 it->face_id = default_face->id;
19776 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19777 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19778 {
19779 PRODUCE_GLYPHS (it);
19780 /* term.c:produce_glyphs advances it->current_x only for
19781 TEXT_AREA. */
19782 it->current_x += it->pixel_width;
19783 }
19784
19785 it->current_x = saved_x;
19786 it->area = TEXT_AREA;
19787 }
19788
19789 /* The last row's blank glyphs should get the default face, to
19790 avoid painting the rest of the window with the region face,
19791 if the region ends at ZV. */
19792 if (it->glyph_row->ends_at_zv_p)
19793 it->face_id = default_face->id;
19794 else
19795 it->face_id = face->id;
19796 PRODUCE_GLYPHS (it);
19797
19798 while (it->current_x <= it->last_visible_x)
19799 PRODUCE_GLYPHS (it);
19800
19801 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19802 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19803 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19804 && !it->glyph_row->mode_line_p
19805 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19806 {
19807 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19808 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19809
19810 for ( ; g < e; g++)
19811 it->current_x += g->pixel_width;
19812
19813 it->area = RIGHT_MARGIN_AREA;
19814 it->face_id = default_face->id;
19815 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19816 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19817 {
19818 PRODUCE_GLYPHS (it);
19819 it->current_x += it->pixel_width;
19820 }
19821
19822 it->area = TEXT_AREA;
19823 }
19824
19825 /* Don't count these blanks really. It would let us insert a left
19826 truncation glyph below and make us set the cursor on them, maybe. */
19827 it->current_x = saved_x;
19828 it->object = saved_object;
19829 it->position = saved_pos;
19830 it->what = saved_what;
19831 it->face_id = saved_face_id;
19832 }
19833 }
19834
19835
19836 /* Value is true if text starting at CHARPOS in current_buffer is
19837 trailing whitespace. */
19838
19839 static bool
19840 trailing_whitespace_p (ptrdiff_t charpos)
19841 {
19842 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19843 int c = 0;
19844
19845 while (bytepos < ZV_BYTE
19846 && (c = FETCH_CHAR (bytepos),
19847 c == ' ' || c == '\t'))
19848 ++bytepos;
19849
19850 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19851 {
19852 if (bytepos != PT_BYTE)
19853 return true;
19854 }
19855 return false;
19856 }
19857
19858
19859 /* Highlight trailing whitespace, if any, in ROW. */
19860
19861 static void
19862 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19863 {
19864 int used = row->used[TEXT_AREA];
19865
19866 if (used)
19867 {
19868 struct glyph *start = row->glyphs[TEXT_AREA];
19869 struct glyph *glyph = start + used - 1;
19870
19871 if (row->reversed_p)
19872 {
19873 /* Right-to-left rows need to be processed in the opposite
19874 direction, so swap the edge pointers. */
19875 glyph = start;
19876 start = row->glyphs[TEXT_AREA] + used - 1;
19877 }
19878
19879 /* Skip over glyphs inserted to display the cursor at the
19880 end of a line, for extending the face of the last glyph
19881 to the end of the line on terminals, and for truncation
19882 and continuation glyphs. */
19883 if (!row->reversed_p)
19884 {
19885 while (glyph >= start
19886 && glyph->type == CHAR_GLYPH
19887 && NILP (glyph->object))
19888 --glyph;
19889 }
19890 else
19891 {
19892 while (glyph <= start
19893 && glyph->type == CHAR_GLYPH
19894 && NILP (glyph->object))
19895 ++glyph;
19896 }
19897
19898 /* If last glyph is a space or stretch, and it's trailing
19899 whitespace, set the face of all trailing whitespace glyphs in
19900 IT->glyph_row to `trailing-whitespace'. */
19901 if ((row->reversed_p ? glyph <= start : glyph >= start)
19902 && BUFFERP (glyph->object)
19903 && (glyph->type == STRETCH_GLYPH
19904 || (glyph->type == CHAR_GLYPH
19905 && glyph->u.ch == ' '))
19906 && trailing_whitespace_p (glyph->charpos))
19907 {
19908 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19909 if (face_id < 0)
19910 return;
19911
19912 if (!row->reversed_p)
19913 {
19914 while (glyph >= start
19915 && BUFFERP (glyph->object)
19916 && (glyph->type == STRETCH_GLYPH
19917 || (glyph->type == CHAR_GLYPH
19918 && glyph->u.ch == ' ')))
19919 (glyph--)->face_id = face_id;
19920 }
19921 else
19922 {
19923 while (glyph <= start
19924 && BUFFERP (glyph->object)
19925 && (glyph->type == STRETCH_GLYPH
19926 || (glyph->type == CHAR_GLYPH
19927 && glyph->u.ch == ' ')))
19928 (glyph++)->face_id = face_id;
19929 }
19930 }
19931 }
19932 }
19933
19934
19935 /* Value is true if glyph row ROW should be
19936 considered to hold the buffer position CHARPOS. */
19937
19938 static bool
19939 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19940 {
19941 bool result = true;
19942
19943 if (charpos == CHARPOS (row->end.pos)
19944 || charpos == MATRIX_ROW_END_CHARPOS (row))
19945 {
19946 /* Suppose the row ends on a string.
19947 Unless the row is continued, that means it ends on a newline
19948 in the string. If it's anything other than a display string
19949 (e.g., a before-string from an overlay), we don't want the
19950 cursor there. (This heuristic seems to give the optimal
19951 behavior for the various types of multi-line strings.)
19952 One exception: if the string has `cursor' property on one of
19953 its characters, we _do_ want the cursor there. */
19954 if (CHARPOS (row->end.string_pos) >= 0)
19955 {
19956 if (row->continued_p)
19957 result = true;
19958 else
19959 {
19960 /* Check for `display' property. */
19961 struct glyph *beg = row->glyphs[TEXT_AREA];
19962 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19963 struct glyph *glyph;
19964
19965 result = false;
19966 for (glyph = end; glyph >= beg; --glyph)
19967 if (STRINGP (glyph->object))
19968 {
19969 Lisp_Object prop
19970 = Fget_char_property (make_number (charpos),
19971 Qdisplay, Qnil);
19972 result =
19973 (!NILP (prop)
19974 && display_prop_string_p (prop, glyph->object));
19975 /* If there's a `cursor' property on one of the
19976 string's characters, this row is a cursor row,
19977 even though this is not a display string. */
19978 if (!result)
19979 {
19980 Lisp_Object s = glyph->object;
19981
19982 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19983 {
19984 ptrdiff_t gpos = glyph->charpos;
19985
19986 if (!NILP (Fget_char_property (make_number (gpos),
19987 Qcursor, s)))
19988 {
19989 result = true;
19990 break;
19991 }
19992 }
19993 }
19994 break;
19995 }
19996 }
19997 }
19998 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19999 {
20000 /* If the row ends in middle of a real character,
20001 and the line is continued, we want the cursor here.
20002 That's because CHARPOS (ROW->end.pos) would equal
20003 PT if PT is before the character. */
20004 if (!row->ends_in_ellipsis_p)
20005 result = row->continued_p;
20006 else
20007 /* If the row ends in an ellipsis, then
20008 CHARPOS (ROW->end.pos) will equal point after the
20009 invisible text. We want that position to be displayed
20010 after the ellipsis. */
20011 result = false;
20012 }
20013 /* If the row ends at ZV, display the cursor at the end of that
20014 row instead of at the start of the row below. */
20015 else
20016 result = row->ends_at_zv_p;
20017 }
20018
20019 return result;
20020 }
20021
20022 /* Value is true if glyph row ROW should be
20023 used to hold the cursor. */
20024
20025 static bool
20026 cursor_row_p (struct glyph_row *row)
20027 {
20028 return row_for_charpos_p (row, PT);
20029 }
20030
20031 \f
20032
20033 /* Push the property PROP so that it will be rendered at the current
20034 position in IT. Return true if PROP was successfully pushed, false
20035 otherwise. Called from handle_line_prefix to handle the
20036 `line-prefix' and `wrap-prefix' properties. */
20037
20038 static bool
20039 push_prefix_prop (struct it *it, Lisp_Object prop)
20040 {
20041 struct text_pos pos =
20042 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20043
20044 eassert (it->method == GET_FROM_BUFFER
20045 || it->method == GET_FROM_DISPLAY_VECTOR
20046 || it->method == GET_FROM_STRING
20047 || it->method == GET_FROM_IMAGE);
20048
20049 /* We need to save the current buffer/string position, so it will be
20050 restored by pop_it, because iterate_out_of_display_property
20051 depends on that being set correctly, but some situations leave
20052 it->position not yet set when this function is called. */
20053 push_it (it, &pos);
20054
20055 if (STRINGP (prop))
20056 {
20057 if (SCHARS (prop) == 0)
20058 {
20059 pop_it (it);
20060 return false;
20061 }
20062
20063 it->string = prop;
20064 it->string_from_prefix_prop_p = true;
20065 it->multibyte_p = STRING_MULTIBYTE (it->string);
20066 it->current.overlay_string_index = -1;
20067 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20068 it->end_charpos = it->string_nchars = SCHARS (it->string);
20069 it->method = GET_FROM_STRING;
20070 it->stop_charpos = 0;
20071 it->prev_stop = 0;
20072 it->base_level_stop = 0;
20073
20074 /* Force paragraph direction to be that of the parent
20075 buffer/string. */
20076 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20077 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20078 else
20079 it->paragraph_embedding = L2R;
20080
20081 /* Set up the bidi iterator for this display string. */
20082 if (it->bidi_p)
20083 {
20084 it->bidi_it.string.lstring = it->string;
20085 it->bidi_it.string.s = NULL;
20086 it->bidi_it.string.schars = it->end_charpos;
20087 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20088 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20089 it->bidi_it.string.unibyte = !it->multibyte_p;
20090 it->bidi_it.w = it->w;
20091 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20092 }
20093 }
20094 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20095 {
20096 it->method = GET_FROM_STRETCH;
20097 it->object = prop;
20098 }
20099 #ifdef HAVE_WINDOW_SYSTEM
20100 else if (IMAGEP (prop))
20101 {
20102 it->what = IT_IMAGE;
20103 it->image_id = lookup_image (it->f, prop);
20104 it->method = GET_FROM_IMAGE;
20105 }
20106 #endif /* HAVE_WINDOW_SYSTEM */
20107 else
20108 {
20109 pop_it (it); /* bogus display property, give up */
20110 return false;
20111 }
20112
20113 return true;
20114 }
20115
20116 /* Return the character-property PROP at the current position in IT. */
20117
20118 static Lisp_Object
20119 get_it_property (struct it *it, Lisp_Object prop)
20120 {
20121 Lisp_Object position, object = it->object;
20122
20123 if (STRINGP (object))
20124 position = make_number (IT_STRING_CHARPOS (*it));
20125 else if (BUFFERP (object))
20126 {
20127 position = make_number (IT_CHARPOS (*it));
20128 object = it->window;
20129 }
20130 else
20131 return Qnil;
20132
20133 return Fget_char_property (position, prop, object);
20134 }
20135
20136 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20137
20138 static void
20139 handle_line_prefix (struct it *it)
20140 {
20141 Lisp_Object prefix;
20142
20143 if (it->continuation_lines_width > 0)
20144 {
20145 prefix = get_it_property (it, Qwrap_prefix);
20146 if (NILP (prefix))
20147 prefix = Vwrap_prefix;
20148 }
20149 else
20150 {
20151 prefix = get_it_property (it, Qline_prefix);
20152 if (NILP (prefix))
20153 prefix = Vline_prefix;
20154 }
20155 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20156 {
20157 /* If the prefix is wider than the window, and we try to wrap
20158 it, it would acquire its own wrap prefix, and so on till the
20159 iterator stack overflows. So, don't wrap the prefix. */
20160 it->line_wrap = TRUNCATE;
20161 it->avoid_cursor_p = true;
20162 }
20163 }
20164
20165 \f
20166
20167 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20168 only for R2L lines from display_line and display_string, when they
20169 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20170 the line/string needs to be continued on the next glyph row. */
20171 static void
20172 unproduce_glyphs (struct it *it, int n)
20173 {
20174 struct glyph *glyph, *end;
20175
20176 eassert (it->glyph_row);
20177 eassert (it->glyph_row->reversed_p);
20178 eassert (it->area == TEXT_AREA);
20179 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20180
20181 if (n > it->glyph_row->used[TEXT_AREA])
20182 n = it->glyph_row->used[TEXT_AREA];
20183 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20184 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20185 for ( ; glyph < end; glyph++)
20186 glyph[-n] = *glyph;
20187 }
20188
20189 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20190 and ROW->maxpos. */
20191 static void
20192 find_row_edges (struct it *it, struct glyph_row *row,
20193 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20194 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20195 {
20196 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20197 lines' rows is implemented for bidi-reordered rows. */
20198
20199 /* ROW->minpos is the value of min_pos, the minimal buffer position
20200 we have in ROW, or ROW->start.pos if that is smaller. */
20201 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20202 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20203 else
20204 /* We didn't find buffer positions smaller than ROW->start, or
20205 didn't find _any_ valid buffer positions in any of the glyphs,
20206 so we must trust the iterator's computed positions. */
20207 row->minpos = row->start.pos;
20208 if (max_pos <= 0)
20209 {
20210 max_pos = CHARPOS (it->current.pos);
20211 max_bpos = BYTEPOS (it->current.pos);
20212 }
20213
20214 /* Here are the various use-cases for ending the row, and the
20215 corresponding values for ROW->maxpos:
20216
20217 Line ends in a newline from buffer eol_pos + 1
20218 Line is continued from buffer max_pos + 1
20219 Line is truncated on right it->current.pos
20220 Line ends in a newline from string max_pos + 1(*)
20221 (*) + 1 only when line ends in a forward scan
20222 Line is continued from string max_pos
20223 Line is continued from display vector max_pos
20224 Line is entirely from a string min_pos == max_pos
20225 Line is entirely from a display vector min_pos == max_pos
20226 Line that ends at ZV ZV
20227
20228 If you discover other use-cases, please add them here as
20229 appropriate. */
20230 if (row->ends_at_zv_p)
20231 row->maxpos = it->current.pos;
20232 else if (row->used[TEXT_AREA])
20233 {
20234 bool seen_this_string = false;
20235 struct glyph_row *r1 = row - 1;
20236
20237 /* Did we see the same display string on the previous row? */
20238 if (STRINGP (it->object)
20239 /* this is not the first row */
20240 && row > it->w->desired_matrix->rows
20241 /* previous row is not the header line */
20242 && !r1->mode_line_p
20243 /* previous row also ends in a newline from a string */
20244 && r1->ends_in_newline_from_string_p)
20245 {
20246 struct glyph *start, *end;
20247
20248 /* Search for the last glyph of the previous row that came
20249 from buffer or string. Depending on whether the row is
20250 L2R or R2L, we need to process it front to back or the
20251 other way round. */
20252 if (!r1->reversed_p)
20253 {
20254 start = r1->glyphs[TEXT_AREA];
20255 end = start + r1->used[TEXT_AREA];
20256 /* Glyphs inserted by redisplay have nil as their object. */
20257 while (end > start
20258 && NILP ((end - 1)->object)
20259 && (end - 1)->charpos <= 0)
20260 --end;
20261 if (end > start)
20262 {
20263 if (EQ ((end - 1)->object, it->object))
20264 seen_this_string = true;
20265 }
20266 else
20267 /* If all the glyphs of the previous row were inserted
20268 by redisplay, it means the previous row was
20269 produced from a single newline, which is only
20270 possible if that newline came from the same string
20271 as the one which produced this ROW. */
20272 seen_this_string = true;
20273 }
20274 else
20275 {
20276 end = r1->glyphs[TEXT_AREA] - 1;
20277 start = end + r1->used[TEXT_AREA];
20278 while (end < start
20279 && NILP ((end + 1)->object)
20280 && (end + 1)->charpos <= 0)
20281 ++end;
20282 if (end < start)
20283 {
20284 if (EQ ((end + 1)->object, it->object))
20285 seen_this_string = true;
20286 }
20287 else
20288 seen_this_string = true;
20289 }
20290 }
20291 /* Take note of each display string that covers a newline only
20292 once, the first time we see it. This is for when a display
20293 string includes more than one newline in it. */
20294 if (row->ends_in_newline_from_string_p && !seen_this_string)
20295 {
20296 /* If we were scanning the buffer forward when we displayed
20297 the string, we want to account for at least one buffer
20298 position that belongs to this row (position covered by
20299 the display string), so that cursor positioning will
20300 consider this row as a candidate when point is at the end
20301 of the visual line represented by this row. This is not
20302 required when scanning back, because max_pos will already
20303 have a much larger value. */
20304 if (CHARPOS (row->end.pos) > max_pos)
20305 INC_BOTH (max_pos, max_bpos);
20306 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20307 }
20308 else if (CHARPOS (it->eol_pos) > 0)
20309 SET_TEXT_POS (row->maxpos,
20310 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20311 else if (row->continued_p)
20312 {
20313 /* If max_pos is different from IT's current position, it
20314 means IT->method does not belong to the display element
20315 at max_pos. However, it also means that the display
20316 element at max_pos was displayed in its entirety on this
20317 line, which is equivalent to saying that the next line
20318 starts at the next buffer position. */
20319 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20320 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20321 else
20322 {
20323 INC_BOTH (max_pos, max_bpos);
20324 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20325 }
20326 }
20327 else if (row->truncated_on_right_p)
20328 /* display_line already called reseat_at_next_visible_line_start,
20329 which puts the iterator at the beginning of the next line, in
20330 the logical order. */
20331 row->maxpos = it->current.pos;
20332 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20333 /* A line that is entirely from a string/image/stretch... */
20334 row->maxpos = row->minpos;
20335 else
20336 emacs_abort ();
20337 }
20338 else
20339 row->maxpos = it->current.pos;
20340 }
20341
20342 /* Construct the glyph row IT->glyph_row in the desired matrix of
20343 IT->w from text at the current position of IT. See dispextern.h
20344 for an overview of struct it. Value is true if
20345 IT->glyph_row displays text, as opposed to a line displaying ZV
20346 only. */
20347
20348 static bool
20349 display_line (struct it *it)
20350 {
20351 struct glyph_row *row = it->glyph_row;
20352 Lisp_Object overlay_arrow_string;
20353 struct it wrap_it;
20354 void *wrap_data = NULL;
20355 bool may_wrap = false;
20356 int wrap_x IF_LINT (= 0);
20357 int wrap_row_used = -1;
20358 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20359 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20360 int wrap_row_extra_line_spacing IF_LINT (= 0);
20361 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20362 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20363 int cvpos;
20364 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20365 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20366 bool pending_handle_line_prefix = false;
20367
20368 /* We always start displaying at hpos zero even if hscrolled. */
20369 eassert (it->hpos == 0 && it->current_x == 0);
20370
20371 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20372 >= it->w->desired_matrix->nrows)
20373 {
20374 it->w->nrows_scale_factor++;
20375 it->f->fonts_changed = true;
20376 return false;
20377 }
20378
20379 /* Clear the result glyph row and enable it. */
20380 prepare_desired_row (it->w, row, false);
20381
20382 row->y = it->current_y;
20383 row->start = it->start;
20384 row->continuation_lines_width = it->continuation_lines_width;
20385 row->displays_text_p = true;
20386 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20387 it->starts_in_middle_of_char_p = false;
20388
20389 /* Arrange the overlays nicely for our purposes. Usually, we call
20390 display_line on only one line at a time, in which case this
20391 can't really hurt too much, or we call it on lines which appear
20392 one after another in the buffer, in which case all calls to
20393 recenter_overlay_lists but the first will be pretty cheap. */
20394 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20395
20396 /* Move over display elements that are not visible because we are
20397 hscrolled. This may stop at an x-position < IT->first_visible_x
20398 if the first glyph is partially visible or if we hit a line end. */
20399 if (it->current_x < it->first_visible_x)
20400 {
20401 enum move_it_result move_result;
20402
20403 this_line_min_pos = row->start.pos;
20404 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20405 MOVE_TO_POS | MOVE_TO_X);
20406 /* If we are under a large hscroll, move_it_in_display_line_to
20407 could hit the end of the line without reaching
20408 it->first_visible_x. Pretend that we did reach it. This is
20409 especially important on a TTY, where we will call
20410 extend_face_to_end_of_line, which needs to know how many
20411 blank glyphs to produce. */
20412 if (it->current_x < it->first_visible_x
20413 && (move_result == MOVE_NEWLINE_OR_CR
20414 || move_result == MOVE_POS_MATCH_OR_ZV))
20415 it->current_x = it->first_visible_x;
20416
20417 /* Record the smallest positions seen while we moved over
20418 display elements that are not visible. This is needed by
20419 redisplay_internal for optimizing the case where the cursor
20420 stays inside the same line. The rest of this function only
20421 considers positions that are actually displayed, so
20422 RECORD_MAX_MIN_POS will not otherwise record positions that
20423 are hscrolled to the left of the left edge of the window. */
20424 min_pos = CHARPOS (this_line_min_pos);
20425 min_bpos = BYTEPOS (this_line_min_pos);
20426 }
20427 else if (it->area == TEXT_AREA)
20428 {
20429 /* We only do this when not calling move_it_in_display_line_to
20430 above, because that function calls itself handle_line_prefix. */
20431 handle_line_prefix (it);
20432 }
20433 else
20434 {
20435 /* Line-prefix and wrap-prefix are always displayed in the text
20436 area. But if this is the first call to display_line after
20437 init_iterator, the iterator might have been set up to write
20438 into a marginal area, e.g. if the line begins with some
20439 display property that writes to the margins. So we need to
20440 wait with the call to handle_line_prefix until whatever
20441 writes to the margin has done its job. */
20442 pending_handle_line_prefix = true;
20443 }
20444
20445 /* Get the initial row height. This is either the height of the
20446 text hscrolled, if there is any, or zero. */
20447 row->ascent = it->max_ascent;
20448 row->height = it->max_ascent + it->max_descent;
20449 row->phys_ascent = it->max_phys_ascent;
20450 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20451 row->extra_line_spacing = it->max_extra_line_spacing;
20452
20453 /* Utility macro to record max and min buffer positions seen until now. */
20454 #define RECORD_MAX_MIN_POS(IT) \
20455 do \
20456 { \
20457 bool composition_p \
20458 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20459 ptrdiff_t current_pos = \
20460 composition_p ? (IT)->cmp_it.charpos \
20461 : IT_CHARPOS (*(IT)); \
20462 ptrdiff_t current_bpos = \
20463 composition_p ? CHAR_TO_BYTE (current_pos) \
20464 : IT_BYTEPOS (*(IT)); \
20465 if (current_pos < min_pos) \
20466 { \
20467 min_pos = current_pos; \
20468 min_bpos = current_bpos; \
20469 } \
20470 if (IT_CHARPOS (*it) > max_pos) \
20471 { \
20472 max_pos = IT_CHARPOS (*it); \
20473 max_bpos = IT_BYTEPOS (*it); \
20474 } \
20475 } \
20476 while (false)
20477
20478 /* Loop generating characters. The loop is left with IT on the next
20479 character to display. */
20480 while (true)
20481 {
20482 int n_glyphs_before, hpos_before, x_before;
20483 int x, nglyphs;
20484 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20485
20486 /* Retrieve the next thing to display. Value is false if end of
20487 buffer reached. */
20488 if (!get_next_display_element (it))
20489 {
20490 /* Maybe add a space at the end of this line that is used to
20491 display the cursor there under X. Set the charpos of the
20492 first glyph of blank lines not corresponding to any text
20493 to -1. */
20494 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20495 row->exact_window_width_line_p = true;
20496 else if ((append_space_for_newline (it, true)
20497 && row->used[TEXT_AREA] == 1)
20498 || row->used[TEXT_AREA] == 0)
20499 {
20500 row->glyphs[TEXT_AREA]->charpos = -1;
20501 row->displays_text_p = false;
20502
20503 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20504 && (!MINI_WINDOW_P (it->w)
20505 || (minibuf_level && EQ (it->window, minibuf_window))))
20506 row->indicate_empty_line_p = true;
20507 }
20508
20509 it->continuation_lines_width = 0;
20510 row->ends_at_zv_p = true;
20511 /* A row that displays right-to-left text must always have
20512 its last face extended all the way to the end of line,
20513 even if this row ends in ZV, because we still write to
20514 the screen left to right. We also need to extend the
20515 last face if the default face is remapped to some
20516 different face, otherwise the functions that clear
20517 portions of the screen will clear with the default face's
20518 background color. */
20519 if (row->reversed_p
20520 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20521 extend_face_to_end_of_line (it);
20522 break;
20523 }
20524
20525 /* Now, get the metrics of what we want to display. This also
20526 generates glyphs in `row' (which is IT->glyph_row). */
20527 n_glyphs_before = row->used[TEXT_AREA];
20528 x = it->current_x;
20529
20530 /* Remember the line height so far in case the next element doesn't
20531 fit on the line. */
20532 if (it->line_wrap != TRUNCATE)
20533 {
20534 ascent = it->max_ascent;
20535 descent = it->max_descent;
20536 phys_ascent = it->max_phys_ascent;
20537 phys_descent = it->max_phys_descent;
20538
20539 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20540 {
20541 if (IT_DISPLAYING_WHITESPACE (it))
20542 may_wrap = true;
20543 else if (may_wrap)
20544 {
20545 SAVE_IT (wrap_it, *it, wrap_data);
20546 wrap_x = x;
20547 wrap_row_used = row->used[TEXT_AREA];
20548 wrap_row_ascent = row->ascent;
20549 wrap_row_height = row->height;
20550 wrap_row_phys_ascent = row->phys_ascent;
20551 wrap_row_phys_height = row->phys_height;
20552 wrap_row_extra_line_spacing = row->extra_line_spacing;
20553 wrap_row_min_pos = min_pos;
20554 wrap_row_min_bpos = min_bpos;
20555 wrap_row_max_pos = max_pos;
20556 wrap_row_max_bpos = max_bpos;
20557 may_wrap = false;
20558 }
20559 }
20560 }
20561
20562 PRODUCE_GLYPHS (it);
20563
20564 /* If this display element was in marginal areas, continue with
20565 the next one. */
20566 if (it->area != TEXT_AREA)
20567 {
20568 row->ascent = max (row->ascent, it->max_ascent);
20569 row->height = max (row->height, it->max_ascent + it->max_descent);
20570 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20571 row->phys_height = max (row->phys_height,
20572 it->max_phys_ascent + it->max_phys_descent);
20573 row->extra_line_spacing = max (row->extra_line_spacing,
20574 it->max_extra_line_spacing);
20575 set_iterator_to_next (it, true);
20576 /* If we didn't handle the line/wrap prefix above, and the
20577 call to set_iterator_to_next just switched to TEXT_AREA,
20578 process the prefix now. */
20579 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20580 {
20581 pending_handle_line_prefix = false;
20582 handle_line_prefix (it);
20583 }
20584 continue;
20585 }
20586
20587 /* Does the display element fit on the line? If we truncate
20588 lines, we should draw past the right edge of the window. If
20589 we don't truncate, we want to stop so that we can display the
20590 continuation glyph before the right margin. If lines are
20591 continued, there are two possible strategies for characters
20592 resulting in more than 1 glyph (e.g. tabs): Display as many
20593 glyphs as possible in this line and leave the rest for the
20594 continuation line, or display the whole element in the next
20595 line. Original redisplay did the former, so we do it also. */
20596 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20597 hpos_before = it->hpos;
20598 x_before = x;
20599
20600 if (/* Not a newline. */
20601 nglyphs > 0
20602 /* Glyphs produced fit entirely in the line. */
20603 && it->current_x < it->last_visible_x)
20604 {
20605 it->hpos += nglyphs;
20606 row->ascent = max (row->ascent, it->max_ascent);
20607 row->height = max (row->height, it->max_ascent + it->max_descent);
20608 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20609 row->phys_height = max (row->phys_height,
20610 it->max_phys_ascent + it->max_phys_descent);
20611 row->extra_line_spacing = max (row->extra_line_spacing,
20612 it->max_extra_line_spacing);
20613 if (it->current_x - it->pixel_width < it->first_visible_x
20614 /* In R2L rows, we arrange in extend_face_to_end_of_line
20615 to add a right offset to the line, by a suitable
20616 change to the stretch glyph that is the leftmost
20617 glyph of the line. */
20618 && !row->reversed_p)
20619 row->x = x - it->first_visible_x;
20620 /* Record the maximum and minimum buffer positions seen so
20621 far in glyphs that will be displayed by this row. */
20622 if (it->bidi_p)
20623 RECORD_MAX_MIN_POS (it);
20624 }
20625 else
20626 {
20627 int i, new_x;
20628 struct glyph *glyph;
20629
20630 for (i = 0; i < nglyphs; ++i, x = new_x)
20631 {
20632 /* Identify the glyphs added by the last call to
20633 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20634 the previous glyphs. */
20635 if (!row->reversed_p)
20636 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20637 else
20638 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20639 new_x = x + glyph->pixel_width;
20640
20641 if (/* Lines are continued. */
20642 it->line_wrap != TRUNCATE
20643 && (/* Glyph doesn't fit on the line. */
20644 new_x > it->last_visible_x
20645 /* Or it fits exactly on a window system frame. */
20646 || (new_x == it->last_visible_x
20647 && FRAME_WINDOW_P (it->f)
20648 && (row->reversed_p
20649 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20650 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20651 {
20652 /* End of a continued line. */
20653
20654 if (it->hpos == 0
20655 || (new_x == it->last_visible_x
20656 && FRAME_WINDOW_P (it->f)
20657 && (row->reversed_p
20658 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20659 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20660 {
20661 /* Current glyph is the only one on the line or
20662 fits exactly on the line. We must continue
20663 the line because we can't draw the cursor
20664 after the glyph. */
20665 row->continued_p = true;
20666 it->current_x = new_x;
20667 it->continuation_lines_width += new_x;
20668 ++it->hpos;
20669 if (i == nglyphs - 1)
20670 {
20671 /* If line-wrap is on, check if a previous
20672 wrap point was found. */
20673 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20674 && wrap_row_used > 0
20675 /* Even if there is a previous wrap
20676 point, continue the line here as
20677 usual, if (i) the previous character
20678 was a space or tab AND (ii) the
20679 current character is not. */
20680 && (!may_wrap
20681 || IT_DISPLAYING_WHITESPACE (it)))
20682 goto back_to_wrap;
20683
20684 /* Record the maximum and minimum buffer
20685 positions seen so far in glyphs that will be
20686 displayed by this row. */
20687 if (it->bidi_p)
20688 RECORD_MAX_MIN_POS (it);
20689 set_iterator_to_next (it, true);
20690 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20691 {
20692 if (!get_next_display_element (it))
20693 {
20694 row->exact_window_width_line_p = true;
20695 it->continuation_lines_width = 0;
20696 row->continued_p = false;
20697 row->ends_at_zv_p = true;
20698 }
20699 else if (ITERATOR_AT_END_OF_LINE_P (it))
20700 {
20701 row->continued_p = false;
20702 row->exact_window_width_line_p = true;
20703 }
20704 /* If line-wrap is on, check if a
20705 previous wrap point was found. */
20706 else if (wrap_row_used > 0
20707 /* Even if there is a previous wrap
20708 point, continue the line here as
20709 usual, if (i) the previous character
20710 was a space or tab AND (ii) the
20711 current character is not. */
20712 && (!may_wrap
20713 || IT_DISPLAYING_WHITESPACE (it)))
20714 goto back_to_wrap;
20715
20716 }
20717 }
20718 else if (it->bidi_p)
20719 RECORD_MAX_MIN_POS (it);
20720 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20721 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20722 extend_face_to_end_of_line (it);
20723 }
20724 else if (CHAR_GLYPH_PADDING_P (*glyph)
20725 && !FRAME_WINDOW_P (it->f))
20726 {
20727 /* A padding glyph that doesn't fit on this line.
20728 This means the whole character doesn't fit
20729 on the line. */
20730 if (row->reversed_p)
20731 unproduce_glyphs (it, row->used[TEXT_AREA]
20732 - n_glyphs_before);
20733 row->used[TEXT_AREA] = n_glyphs_before;
20734
20735 /* Fill the rest of the row with continuation
20736 glyphs like in 20.x. */
20737 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20738 < row->glyphs[1 + TEXT_AREA])
20739 produce_special_glyphs (it, IT_CONTINUATION);
20740
20741 row->continued_p = true;
20742 it->current_x = x_before;
20743 it->continuation_lines_width += x_before;
20744
20745 /* Restore the height to what it was before the
20746 element not fitting on the line. */
20747 it->max_ascent = ascent;
20748 it->max_descent = descent;
20749 it->max_phys_ascent = phys_ascent;
20750 it->max_phys_descent = phys_descent;
20751 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20752 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20753 extend_face_to_end_of_line (it);
20754 }
20755 else if (wrap_row_used > 0)
20756 {
20757 back_to_wrap:
20758 if (row->reversed_p)
20759 unproduce_glyphs (it,
20760 row->used[TEXT_AREA] - wrap_row_used);
20761 RESTORE_IT (it, &wrap_it, wrap_data);
20762 it->continuation_lines_width += wrap_x;
20763 row->used[TEXT_AREA] = wrap_row_used;
20764 row->ascent = wrap_row_ascent;
20765 row->height = wrap_row_height;
20766 row->phys_ascent = wrap_row_phys_ascent;
20767 row->phys_height = wrap_row_phys_height;
20768 row->extra_line_spacing = wrap_row_extra_line_spacing;
20769 min_pos = wrap_row_min_pos;
20770 min_bpos = wrap_row_min_bpos;
20771 max_pos = wrap_row_max_pos;
20772 max_bpos = wrap_row_max_bpos;
20773 row->continued_p = true;
20774 row->ends_at_zv_p = false;
20775 row->exact_window_width_line_p = false;
20776 it->continuation_lines_width += x;
20777
20778 /* Make sure that a non-default face is extended
20779 up to the right margin of the window. */
20780 extend_face_to_end_of_line (it);
20781 }
20782 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20783 {
20784 /* A TAB that extends past the right edge of the
20785 window. This produces a single glyph on
20786 window system frames. We leave the glyph in
20787 this row and let it fill the row, but don't
20788 consume the TAB. */
20789 if ((row->reversed_p
20790 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20791 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20792 produce_special_glyphs (it, IT_CONTINUATION);
20793 it->continuation_lines_width += it->last_visible_x;
20794 row->ends_in_middle_of_char_p = true;
20795 row->continued_p = true;
20796 glyph->pixel_width = it->last_visible_x - x;
20797 it->starts_in_middle_of_char_p = true;
20798 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20799 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20800 extend_face_to_end_of_line (it);
20801 }
20802 else
20803 {
20804 /* Something other than a TAB that draws past
20805 the right edge of the window. Restore
20806 positions to values before the element. */
20807 if (row->reversed_p)
20808 unproduce_glyphs (it, row->used[TEXT_AREA]
20809 - (n_glyphs_before + i));
20810 row->used[TEXT_AREA] = n_glyphs_before + i;
20811
20812 /* Display continuation glyphs. */
20813 it->current_x = x_before;
20814 it->continuation_lines_width += x;
20815 if (!FRAME_WINDOW_P (it->f)
20816 || (row->reversed_p
20817 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20818 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20819 produce_special_glyphs (it, IT_CONTINUATION);
20820 row->continued_p = true;
20821
20822 extend_face_to_end_of_line (it);
20823
20824 if (nglyphs > 1 && i > 0)
20825 {
20826 row->ends_in_middle_of_char_p = true;
20827 it->starts_in_middle_of_char_p = true;
20828 }
20829
20830 /* Restore the height to what it was before the
20831 element not fitting on the line. */
20832 it->max_ascent = ascent;
20833 it->max_descent = descent;
20834 it->max_phys_ascent = phys_ascent;
20835 it->max_phys_descent = phys_descent;
20836 }
20837
20838 break;
20839 }
20840 else if (new_x > it->first_visible_x)
20841 {
20842 /* Increment number of glyphs actually displayed. */
20843 ++it->hpos;
20844
20845 /* Record the maximum and minimum buffer positions
20846 seen so far in glyphs that will be displayed by
20847 this row. */
20848 if (it->bidi_p)
20849 RECORD_MAX_MIN_POS (it);
20850
20851 if (x < it->first_visible_x && !row->reversed_p)
20852 /* Glyph is partially visible, i.e. row starts at
20853 negative X position. Don't do that in R2L
20854 rows, where we arrange to add a right offset to
20855 the line in extend_face_to_end_of_line, by a
20856 suitable change to the stretch glyph that is
20857 the leftmost glyph of the line. */
20858 row->x = x - it->first_visible_x;
20859 /* When the last glyph of an R2L row only fits
20860 partially on the line, we need to set row->x to a
20861 negative offset, so that the leftmost glyph is
20862 the one that is partially visible. But if we are
20863 going to produce the truncation glyph, this will
20864 be taken care of in produce_special_glyphs. */
20865 if (row->reversed_p
20866 && new_x > it->last_visible_x
20867 && !(it->line_wrap == TRUNCATE
20868 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20869 {
20870 eassert (FRAME_WINDOW_P (it->f));
20871 row->x = it->last_visible_x - new_x;
20872 }
20873 }
20874 else
20875 {
20876 /* Glyph is completely off the left margin of the
20877 window. This should not happen because of the
20878 move_it_in_display_line at the start of this
20879 function, unless the text display area of the
20880 window is empty. */
20881 eassert (it->first_visible_x <= it->last_visible_x);
20882 }
20883 }
20884 /* Even if this display element produced no glyphs at all,
20885 we want to record its position. */
20886 if (it->bidi_p && nglyphs == 0)
20887 RECORD_MAX_MIN_POS (it);
20888
20889 row->ascent = max (row->ascent, it->max_ascent);
20890 row->height = max (row->height, it->max_ascent + it->max_descent);
20891 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20892 row->phys_height = max (row->phys_height,
20893 it->max_phys_ascent + it->max_phys_descent);
20894 row->extra_line_spacing = max (row->extra_line_spacing,
20895 it->max_extra_line_spacing);
20896
20897 /* End of this display line if row is continued. */
20898 if (row->continued_p || row->ends_at_zv_p)
20899 break;
20900 }
20901
20902 at_end_of_line:
20903 /* Is this a line end? If yes, we're also done, after making
20904 sure that a non-default face is extended up to the right
20905 margin of the window. */
20906 if (ITERATOR_AT_END_OF_LINE_P (it))
20907 {
20908 int used_before = row->used[TEXT_AREA];
20909
20910 row->ends_in_newline_from_string_p = STRINGP (it->object);
20911
20912 /* Add a space at the end of the line that is used to
20913 display the cursor there. */
20914 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20915 append_space_for_newline (it, false);
20916
20917 /* Extend the face to the end of the line. */
20918 extend_face_to_end_of_line (it);
20919
20920 /* Make sure we have the position. */
20921 if (used_before == 0)
20922 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20923
20924 /* Record the position of the newline, for use in
20925 find_row_edges. */
20926 it->eol_pos = it->current.pos;
20927
20928 /* Consume the line end. This skips over invisible lines. */
20929 set_iterator_to_next (it, true);
20930 it->continuation_lines_width = 0;
20931 break;
20932 }
20933
20934 /* Proceed with next display element. Note that this skips
20935 over lines invisible because of selective display. */
20936 set_iterator_to_next (it, true);
20937
20938 /* If we truncate lines, we are done when the last displayed
20939 glyphs reach past the right margin of the window. */
20940 if (it->line_wrap == TRUNCATE
20941 && ((FRAME_WINDOW_P (it->f)
20942 /* Images are preprocessed in produce_image_glyph such
20943 that they are cropped at the right edge of the
20944 window, so an image glyph will always end exactly at
20945 last_visible_x, even if there's no right fringe. */
20946 && ((row->reversed_p
20947 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20948 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20949 || it->what == IT_IMAGE))
20950 ? (it->current_x >= it->last_visible_x)
20951 : (it->current_x > it->last_visible_x)))
20952 {
20953 /* Maybe add truncation glyphs. */
20954 if (!FRAME_WINDOW_P (it->f)
20955 || (row->reversed_p
20956 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20957 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20958 {
20959 int i, n;
20960
20961 if (!row->reversed_p)
20962 {
20963 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20964 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20965 break;
20966 }
20967 else
20968 {
20969 for (i = 0; i < row->used[TEXT_AREA]; i++)
20970 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20971 break;
20972 /* Remove any padding glyphs at the front of ROW, to
20973 make room for the truncation glyphs we will be
20974 adding below. The loop below always inserts at
20975 least one truncation glyph, so also remove the
20976 last glyph added to ROW. */
20977 unproduce_glyphs (it, i + 1);
20978 /* Adjust i for the loop below. */
20979 i = row->used[TEXT_AREA] - (i + 1);
20980 }
20981
20982 /* produce_special_glyphs overwrites the last glyph, so
20983 we don't want that if we want to keep that last
20984 glyph, which means it's an image. */
20985 if (it->current_x > it->last_visible_x)
20986 {
20987 it->current_x = x_before;
20988 if (!FRAME_WINDOW_P (it->f))
20989 {
20990 for (n = row->used[TEXT_AREA]; i < n; ++i)
20991 {
20992 row->used[TEXT_AREA] = i;
20993 produce_special_glyphs (it, IT_TRUNCATION);
20994 }
20995 }
20996 else
20997 {
20998 row->used[TEXT_AREA] = i;
20999 produce_special_glyphs (it, IT_TRUNCATION);
21000 }
21001 it->hpos = hpos_before;
21002 }
21003 }
21004 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21005 {
21006 /* Don't truncate if we can overflow newline into fringe. */
21007 if (!get_next_display_element (it))
21008 {
21009 it->continuation_lines_width = 0;
21010 row->ends_at_zv_p = true;
21011 row->exact_window_width_line_p = true;
21012 break;
21013 }
21014 if (ITERATOR_AT_END_OF_LINE_P (it))
21015 {
21016 row->exact_window_width_line_p = true;
21017 goto at_end_of_line;
21018 }
21019 it->current_x = x_before;
21020 it->hpos = hpos_before;
21021 }
21022
21023 row->truncated_on_right_p = true;
21024 it->continuation_lines_width = 0;
21025 reseat_at_next_visible_line_start (it, false);
21026 /* We insist below that IT's position be at ZV because in
21027 bidi-reordered lines the character at visible line start
21028 might not be the character that follows the newline in
21029 the logical order. */
21030 if (IT_BYTEPOS (*it) > BEG_BYTE)
21031 row->ends_at_zv_p =
21032 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21033 else
21034 row->ends_at_zv_p = false;
21035 break;
21036 }
21037 }
21038
21039 if (wrap_data)
21040 bidi_unshelve_cache (wrap_data, true);
21041
21042 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21043 at the left window margin. */
21044 if (it->first_visible_x
21045 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21046 {
21047 if (!FRAME_WINDOW_P (it->f)
21048 || (((row->reversed_p
21049 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21050 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21051 /* Don't let insert_left_trunc_glyphs overwrite the
21052 first glyph of the row if it is an image. */
21053 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21054 insert_left_trunc_glyphs (it);
21055 row->truncated_on_left_p = true;
21056 }
21057
21058 /* Remember the position at which this line ends.
21059
21060 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21061 cannot be before the call to find_row_edges below, since that is
21062 where these positions are determined. */
21063 row->end = it->current;
21064 if (!it->bidi_p)
21065 {
21066 row->minpos = row->start.pos;
21067 row->maxpos = row->end.pos;
21068 }
21069 else
21070 {
21071 /* ROW->minpos and ROW->maxpos must be the smallest and
21072 `1 + the largest' buffer positions in ROW. But if ROW was
21073 bidi-reordered, these two positions can be anywhere in the
21074 row, so we must determine them now. */
21075 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21076 }
21077
21078 /* If the start of this line is the overlay arrow-position, then
21079 mark this glyph row as the one containing the overlay arrow.
21080 This is clearly a mess with variable size fonts. It would be
21081 better to let it be displayed like cursors under X. */
21082 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21083 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21084 !NILP (overlay_arrow_string)))
21085 {
21086 /* Overlay arrow in window redisplay is a fringe bitmap. */
21087 if (STRINGP (overlay_arrow_string))
21088 {
21089 struct glyph_row *arrow_row
21090 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21091 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21092 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21093 struct glyph *p = row->glyphs[TEXT_AREA];
21094 struct glyph *p2, *end;
21095
21096 /* Copy the arrow glyphs. */
21097 while (glyph < arrow_end)
21098 *p++ = *glyph++;
21099
21100 /* Throw away padding glyphs. */
21101 p2 = p;
21102 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21103 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21104 ++p2;
21105 if (p2 > p)
21106 {
21107 while (p2 < end)
21108 *p++ = *p2++;
21109 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21110 }
21111 }
21112 else
21113 {
21114 eassert (INTEGERP (overlay_arrow_string));
21115 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21116 }
21117 overlay_arrow_seen = true;
21118 }
21119
21120 /* Highlight trailing whitespace. */
21121 if (!NILP (Vshow_trailing_whitespace))
21122 highlight_trailing_whitespace (it->f, it->glyph_row);
21123
21124 /* Compute pixel dimensions of this line. */
21125 compute_line_metrics (it);
21126
21127 /* Implementation note: No changes in the glyphs of ROW or in their
21128 faces can be done past this point, because compute_line_metrics
21129 computes ROW's hash value and stores it within the glyph_row
21130 structure. */
21131
21132 /* Record whether this row ends inside an ellipsis. */
21133 row->ends_in_ellipsis_p
21134 = (it->method == GET_FROM_DISPLAY_VECTOR
21135 && it->ellipsis_p);
21136
21137 /* Save fringe bitmaps in this row. */
21138 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21139 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21140 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21141 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21142
21143 it->left_user_fringe_bitmap = 0;
21144 it->left_user_fringe_face_id = 0;
21145 it->right_user_fringe_bitmap = 0;
21146 it->right_user_fringe_face_id = 0;
21147
21148 /* Maybe set the cursor. */
21149 cvpos = it->w->cursor.vpos;
21150 if ((cvpos < 0
21151 /* In bidi-reordered rows, keep checking for proper cursor
21152 position even if one has been found already, because buffer
21153 positions in such rows change non-linearly with ROW->VPOS,
21154 when a line is continued. One exception: when we are at ZV,
21155 display cursor on the first suitable glyph row, since all
21156 the empty rows after that also have their position set to ZV. */
21157 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21158 lines' rows is implemented for bidi-reordered rows. */
21159 || (it->bidi_p
21160 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21161 && PT >= MATRIX_ROW_START_CHARPOS (row)
21162 && PT <= MATRIX_ROW_END_CHARPOS (row)
21163 && cursor_row_p (row))
21164 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21165
21166 /* Prepare for the next line. This line starts horizontally at (X
21167 HPOS) = (0 0). Vertical positions are incremented. As a
21168 convenience for the caller, IT->glyph_row is set to the next
21169 row to be used. */
21170 it->current_x = it->hpos = 0;
21171 it->current_y += row->height;
21172 SET_TEXT_POS (it->eol_pos, 0, 0);
21173 ++it->vpos;
21174 ++it->glyph_row;
21175 /* The next row should by default use the same value of the
21176 reversed_p flag as this one. set_iterator_to_next decides when
21177 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21178 the flag accordingly. */
21179 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21180 it->glyph_row->reversed_p = row->reversed_p;
21181 it->start = row->end;
21182 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21183
21184 #undef RECORD_MAX_MIN_POS
21185 }
21186
21187 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21188 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21189 doc: /* Return paragraph direction at point in BUFFER.
21190 Value is either `left-to-right' or `right-to-left'.
21191 If BUFFER is omitted or nil, it defaults to the current buffer.
21192
21193 Paragraph direction determines how the text in the paragraph is displayed.
21194 In left-to-right paragraphs, text begins at the left margin of the window
21195 and the reading direction is generally left to right. In right-to-left
21196 paragraphs, text begins at the right margin and is read from right to left.
21197
21198 See also `bidi-paragraph-direction'. */)
21199 (Lisp_Object buffer)
21200 {
21201 struct buffer *buf = current_buffer;
21202 struct buffer *old = buf;
21203
21204 if (! NILP (buffer))
21205 {
21206 CHECK_BUFFER (buffer);
21207 buf = XBUFFER (buffer);
21208 }
21209
21210 if (NILP (BVAR (buf, bidi_display_reordering))
21211 || NILP (BVAR (buf, enable_multibyte_characters))
21212 /* When we are loading loadup.el, the character property tables
21213 needed for bidi iteration are not yet available. */
21214 || !NILP (Vpurify_flag))
21215 return Qleft_to_right;
21216 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21217 return BVAR (buf, bidi_paragraph_direction);
21218 else
21219 {
21220 /* Determine the direction from buffer text. We could try to
21221 use current_matrix if it is up to date, but this seems fast
21222 enough as it is. */
21223 struct bidi_it itb;
21224 ptrdiff_t pos = BUF_PT (buf);
21225 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21226 int c;
21227 void *itb_data = bidi_shelve_cache ();
21228
21229 set_buffer_temp (buf);
21230 /* bidi_paragraph_init finds the base direction of the paragraph
21231 by searching forward from paragraph start. We need the base
21232 direction of the current or _previous_ paragraph, so we need
21233 to make sure we are within that paragraph. To that end, find
21234 the previous non-empty line. */
21235 if (pos >= ZV && pos > BEGV)
21236 DEC_BOTH (pos, bytepos);
21237 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21238 if (fast_looking_at (trailing_white_space,
21239 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21240 {
21241 while ((c = FETCH_BYTE (bytepos)) == '\n'
21242 || c == ' ' || c == '\t' || c == '\f')
21243 {
21244 if (bytepos <= BEGV_BYTE)
21245 break;
21246 bytepos--;
21247 pos--;
21248 }
21249 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21250 bytepos--;
21251 }
21252 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21253 itb.paragraph_dir = NEUTRAL_DIR;
21254 itb.string.s = NULL;
21255 itb.string.lstring = Qnil;
21256 itb.string.bufpos = 0;
21257 itb.string.from_disp_str = false;
21258 itb.string.unibyte = false;
21259 /* We have no window to use here for ignoring window-specific
21260 overlays. Using NULL for window pointer will cause
21261 compute_display_string_pos to use the current buffer. */
21262 itb.w = NULL;
21263 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21264 bidi_unshelve_cache (itb_data, false);
21265 set_buffer_temp (old);
21266 switch (itb.paragraph_dir)
21267 {
21268 case L2R:
21269 return Qleft_to_right;
21270 break;
21271 case R2L:
21272 return Qright_to_left;
21273 break;
21274 default:
21275 emacs_abort ();
21276 }
21277 }
21278 }
21279
21280 DEFUN ("bidi-find-overridden-directionality",
21281 Fbidi_find_overridden_directionality,
21282 Sbidi_find_overridden_directionality, 2, 3, 0,
21283 doc: /* Return position between FROM and TO where directionality was overridden.
21284
21285 This function returns the first character position in the specified
21286 region of OBJECT where there is a character whose `bidi-class' property
21287 is `L', but which was forced to display as `R' by a directional
21288 override, and likewise with characters whose `bidi-class' is `R'
21289 or `AL' that were forced to display as `L'.
21290
21291 If no such character is found, the function returns nil.
21292
21293 OBJECT is a Lisp string or buffer to search for overridden
21294 directionality, and defaults to the current buffer if nil or omitted.
21295 OBJECT can also be a window, in which case the function will search
21296 the buffer displayed in that window. Passing the window instead of
21297 a buffer is preferable when the buffer is displayed in some window,
21298 because this function will then be able to correctly account for
21299 window-specific overlays, which can affect the results.
21300
21301 Strong directional characters `L', `R', and `AL' can have their
21302 intrinsic directionality overridden by directional override
21303 control characters RLO (u+202e) and LRO (u+202d). See the
21304 function `get-char-code-property' for a way to inquire about
21305 the `bidi-class' property of a character. */)
21306 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21307 {
21308 struct buffer *buf = current_buffer;
21309 struct buffer *old = buf;
21310 struct window *w = NULL;
21311 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21312 struct bidi_it itb;
21313 ptrdiff_t from_pos, to_pos, from_bpos;
21314 void *itb_data;
21315
21316 if (!NILP (object))
21317 {
21318 if (BUFFERP (object))
21319 buf = XBUFFER (object);
21320 else if (WINDOWP (object))
21321 {
21322 w = decode_live_window (object);
21323 buf = XBUFFER (w->contents);
21324 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21325 }
21326 else
21327 CHECK_STRING (object);
21328 }
21329
21330 if (STRINGP (object))
21331 {
21332 /* Characters in unibyte strings are always treated by bidi.c as
21333 strong LTR. */
21334 if (!STRING_MULTIBYTE (object)
21335 /* When we are loading loadup.el, the character property
21336 tables needed for bidi iteration are not yet
21337 available. */
21338 || !NILP (Vpurify_flag))
21339 return Qnil;
21340
21341 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21342 if (from_pos >= SCHARS (object))
21343 return Qnil;
21344
21345 /* Set up the bidi iterator. */
21346 itb_data = bidi_shelve_cache ();
21347 itb.paragraph_dir = NEUTRAL_DIR;
21348 itb.string.lstring = object;
21349 itb.string.s = NULL;
21350 itb.string.schars = SCHARS (object);
21351 itb.string.bufpos = 0;
21352 itb.string.from_disp_str = false;
21353 itb.string.unibyte = false;
21354 itb.w = w;
21355 bidi_init_it (0, 0, frame_window_p, &itb);
21356 }
21357 else
21358 {
21359 /* Nothing this fancy can happen in unibyte buffers, or in a
21360 buffer that disabled reordering, or if FROM is at EOB. */
21361 if (NILP (BVAR (buf, bidi_display_reordering))
21362 || NILP (BVAR (buf, enable_multibyte_characters))
21363 /* When we are loading loadup.el, the character property
21364 tables needed for bidi iteration are not yet
21365 available. */
21366 || !NILP (Vpurify_flag))
21367 return Qnil;
21368
21369 set_buffer_temp (buf);
21370 validate_region (&from, &to);
21371 from_pos = XINT (from);
21372 to_pos = XINT (to);
21373 if (from_pos >= ZV)
21374 return Qnil;
21375
21376 /* Set up the bidi iterator. */
21377 itb_data = bidi_shelve_cache ();
21378 from_bpos = CHAR_TO_BYTE (from_pos);
21379 if (from_pos == BEGV)
21380 {
21381 itb.charpos = BEGV;
21382 itb.bytepos = BEGV_BYTE;
21383 }
21384 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21385 {
21386 itb.charpos = from_pos;
21387 itb.bytepos = from_bpos;
21388 }
21389 else
21390 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21391 -1, &itb.bytepos);
21392 itb.paragraph_dir = NEUTRAL_DIR;
21393 itb.string.s = NULL;
21394 itb.string.lstring = Qnil;
21395 itb.string.bufpos = 0;
21396 itb.string.from_disp_str = false;
21397 itb.string.unibyte = false;
21398 itb.w = w;
21399 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21400 }
21401
21402 ptrdiff_t found;
21403 do {
21404 /* For the purposes of this function, the actual base direction of
21405 the paragraph doesn't matter, so just set it to L2R. */
21406 bidi_paragraph_init (L2R, &itb, false);
21407 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21408 ;
21409 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21410
21411 bidi_unshelve_cache (itb_data, false);
21412 set_buffer_temp (old);
21413
21414 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21415 }
21416
21417 DEFUN ("move-point-visually", Fmove_point_visually,
21418 Smove_point_visually, 1, 1, 0,
21419 doc: /* Move point in the visual order in the specified DIRECTION.
21420 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21421 left.
21422
21423 Value is the new character position of point. */)
21424 (Lisp_Object direction)
21425 {
21426 struct window *w = XWINDOW (selected_window);
21427 struct buffer *b = XBUFFER (w->contents);
21428 struct glyph_row *row;
21429 int dir;
21430 Lisp_Object paragraph_dir;
21431
21432 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21433 (!(ROW)->continued_p \
21434 && NILP ((GLYPH)->object) \
21435 && (GLYPH)->type == CHAR_GLYPH \
21436 && (GLYPH)->u.ch == ' ' \
21437 && (GLYPH)->charpos >= 0 \
21438 && !(GLYPH)->avoid_cursor_p)
21439
21440 CHECK_NUMBER (direction);
21441 dir = XINT (direction);
21442 if (dir > 0)
21443 dir = 1;
21444 else
21445 dir = -1;
21446
21447 /* If current matrix is up-to-date, we can use the information
21448 recorded in the glyphs, at least as long as the goal is on the
21449 screen. */
21450 if (w->window_end_valid
21451 && !windows_or_buffers_changed
21452 && b
21453 && !b->clip_changed
21454 && !b->prevent_redisplay_optimizations_p
21455 && !window_outdated (w)
21456 /* We rely below on the cursor coordinates to be up to date, but
21457 we cannot trust them if some command moved point since the
21458 last complete redisplay. */
21459 && w->last_point == BUF_PT (b)
21460 && w->cursor.vpos >= 0
21461 && w->cursor.vpos < w->current_matrix->nrows
21462 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21463 {
21464 struct glyph *g = row->glyphs[TEXT_AREA];
21465 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21466 struct glyph *gpt = g + w->cursor.hpos;
21467
21468 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21469 {
21470 if (BUFFERP (g->object) && g->charpos != PT)
21471 {
21472 SET_PT (g->charpos);
21473 w->cursor.vpos = -1;
21474 return make_number (PT);
21475 }
21476 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21477 {
21478 ptrdiff_t new_pos;
21479
21480 if (BUFFERP (gpt->object))
21481 {
21482 new_pos = PT;
21483 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21484 new_pos += (row->reversed_p ? -dir : dir);
21485 else
21486 new_pos -= (row->reversed_p ? -dir : dir);
21487 }
21488 else if (BUFFERP (g->object))
21489 new_pos = g->charpos;
21490 else
21491 break;
21492 SET_PT (new_pos);
21493 w->cursor.vpos = -1;
21494 return make_number (PT);
21495 }
21496 else if (ROW_GLYPH_NEWLINE_P (row, g))
21497 {
21498 /* Glyphs inserted at the end of a non-empty line for
21499 positioning the cursor have zero charpos, so we must
21500 deduce the value of point by other means. */
21501 if (g->charpos > 0)
21502 SET_PT (g->charpos);
21503 else if (row->ends_at_zv_p && PT != ZV)
21504 SET_PT (ZV);
21505 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21506 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21507 else
21508 break;
21509 w->cursor.vpos = -1;
21510 return make_number (PT);
21511 }
21512 }
21513 if (g == e || NILP (g->object))
21514 {
21515 if (row->truncated_on_left_p || row->truncated_on_right_p)
21516 goto simulate_display;
21517 if (!row->reversed_p)
21518 row += dir;
21519 else
21520 row -= dir;
21521 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21522 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21523 goto simulate_display;
21524
21525 if (dir > 0)
21526 {
21527 if (row->reversed_p && !row->continued_p)
21528 {
21529 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21530 w->cursor.vpos = -1;
21531 return make_number (PT);
21532 }
21533 g = row->glyphs[TEXT_AREA];
21534 e = g + row->used[TEXT_AREA];
21535 for ( ; g < e; g++)
21536 {
21537 if (BUFFERP (g->object)
21538 /* Empty lines have only one glyph, which stands
21539 for the newline, and whose charpos is the
21540 buffer position of the newline. */
21541 || ROW_GLYPH_NEWLINE_P (row, g)
21542 /* When the buffer ends in a newline, the line at
21543 EOB also has one glyph, but its charpos is -1. */
21544 || (row->ends_at_zv_p
21545 && !row->reversed_p
21546 && NILP (g->object)
21547 && g->type == CHAR_GLYPH
21548 && g->u.ch == ' '))
21549 {
21550 if (g->charpos > 0)
21551 SET_PT (g->charpos);
21552 else if (!row->reversed_p
21553 && row->ends_at_zv_p
21554 && PT != ZV)
21555 SET_PT (ZV);
21556 else
21557 continue;
21558 w->cursor.vpos = -1;
21559 return make_number (PT);
21560 }
21561 }
21562 }
21563 else
21564 {
21565 if (!row->reversed_p && !row->continued_p)
21566 {
21567 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21568 w->cursor.vpos = -1;
21569 return make_number (PT);
21570 }
21571 e = row->glyphs[TEXT_AREA];
21572 g = e + row->used[TEXT_AREA] - 1;
21573 for ( ; g >= e; g--)
21574 {
21575 if (BUFFERP (g->object)
21576 || (ROW_GLYPH_NEWLINE_P (row, g)
21577 && g->charpos > 0)
21578 /* Empty R2L lines on GUI frames have the buffer
21579 position of the newline stored in the stretch
21580 glyph. */
21581 || g->type == STRETCH_GLYPH
21582 || (row->ends_at_zv_p
21583 && row->reversed_p
21584 && NILP (g->object)
21585 && g->type == CHAR_GLYPH
21586 && g->u.ch == ' '))
21587 {
21588 if (g->charpos > 0)
21589 SET_PT (g->charpos);
21590 else if (row->reversed_p
21591 && row->ends_at_zv_p
21592 && PT != ZV)
21593 SET_PT (ZV);
21594 else
21595 continue;
21596 w->cursor.vpos = -1;
21597 return make_number (PT);
21598 }
21599 }
21600 }
21601 }
21602 }
21603
21604 simulate_display:
21605
21606 /* If we wind up here, we failed to move by using the glyphs, so we
21607 need to simulate display instead. */
21608
21609 if (b)
21610 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21611 else
21612 paragraph_dir = Qleft_to_right;
21613 if (EQ (paragraph_dir, Qright_to_left))
21614 dir = -dir;
21615 if (PT <= BEGV && dir < 0)
21616 xsignal0 (Qbeginning_of_buffer);
21617 else if (PT >= ZV && dir > 0)
21618 xsignal0 (Qend_of_buffer);
21619 else
21620 {
21621 struct text_pos pt;
21622 struct it it;
21623 int pt_x, target_x, pixel_width, pt_vpos;
21624 bool at_eol_p;
21625 bool overshoot_expected = false;
21626 bool target_is_eol_p = false;
21627
21628 /* Setup the arena. */
21629 SET_TEXT_POS (pt, PT, PT_BYTE);
21630 start_display (&it, w, pt);
21631 /* When lines are truncated, we could be called with point
21632 outside of the windows edges, in which case move_it_*
21633 functions either prematurely stop at window's edge or jump to
21634 the next screen line, whereas we rely below on our ability to
21635 reach point, in order to start from its X coordinate. So we
21636 need to disregard the window's horizontal extent in that case. */
21637 if (it.line_wrap == TRUNCATE)
21638 it.last_visible_x = INFINITY;
21639
21640 if (it.cmp_it.id < 0
21641 && it.method == GET_FROM_STRING
21642 && it.area == TEXT_AREA
21643 && it.string_from_display_prop_p
21644 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21645 overshoot_expected = true;
21646
21647 /* Find the X coordinate of point. We start from the beginning
21648 of this or previous line to make sure we are before point in
21649 the logical order (since the move_it_* functions can only
21650 move forward). */
21651 reseat:
21652 reseat_at_previous_visible_line_start (&it);
21653 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21654 if (IT_CHARPOS (it) != PT)
21655 {
21656 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21657 -1, -1, -1, MOVE_TO_POS);
21658 /* If we missed point because the character there is
21659 displayed out of a display vector that has more than one
21660 glyph, retry expecting overshoot. */
21661 if (it.method == GET_FROM_DISPLAY_VECTOR
21662 && it.current.dpvec_index > 0
21663 && !overshoot_expected)
21664 {
21665 overshoot_expected = true;
21666 goto reseat;
21667 }
21668 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21669 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21670 }
21671 pt_x = it.current_x;
21672 pt_vpos = it.vpos;
21673 if (dir > 0 || overshoot_expected)
21674 {
21675 struct glyph_row *row = it.glyph_row;
21676
21677 /* When point is at beginning of line, we don't have
21678 information about the glyph there loaded into struct
21679 it. Calling get_next_display_element fixes that. */
21680 if (pt_x == 0)
21681 get_next_display_element (&it);
21682 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21683 it.glyph_row = NULL;
21684 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21685 it.glyph_row = row;
21686 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21687 it, lest it will become out of sync with it's buffer
21688 position. */
21689 it.current_x = pt_x;
21690 }
21691 else
21692 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21693 pixel_width = it.pixel_width;
21694 if (overshoot_expected && at_eol_p)
21695 pixel_width = 0;
21696 else if (pixel_width <= 0)
21697 pixel_width = 1;
21698
21699 /* If there's a display string (or something similar) at point,
21700 we are actually at the glyph to the left of point, so we need
21701 to correct the X coordinate. */
21702 if (overshoot_expected)
21703 {
21704 if (it.bidi_p)
21705 pt_x += pixel_width * it.bidi_it.scan_dir;
21706 else
21707 pt_x += pixel_width;
21708 }
21709
21710 /* Compute target X coordinate, either to the left or to the
21711 right of point. On TTY frames, all characters have the same
21712 pixel width of 1, so we can use that. On GUI frames we don't
21713 have an easy way of getting at the pixel width of the
21714 character to the left of point, so we use a different method
21715 of getting to that place. */
21716 if (dir > 0)
21717 target_x = pt_x + pixel_width;
21718 else
21719 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21720
21721 /* Target X coordinate could be one line above or below the line
21722 of point, in which case we need to adjust the target X
21723 coordinate. Also, if moving to the left, we need to begin at
21724 the left edge of the point's screen line. */
21725 if (dir < 0)
21726 {
21727 if (pt_x > 0)
21728 {
21729 start_display (&it, w, pt);
21730 if (it.line_wrap == TRUNCATE)
21731 it.last_visible_x = INFINITY;
21732 reseat_at_previous_visible_line_start (&it);
21733 it.current_x = it.current_y = it.hpos = 0;
21734 if (pt_vpos != 0)
21735 move_it_by_lines (&it, pt_vpos);
21736 }
21737 else
21738 {
21739 move_it_by_lines (&it, -1);
21740 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21741 target_is_eol_p = true;
21742 /* Under word-wrap, we don't know the x coordinate of
21743 the last character displayed on the previous line,
21744 which immediately precedes the wrap point. To find
21745 out its x coordinate, we try moving to the right
21746 margin of the window, which will stop at the wrap
21747 point, and then reset target_x to point at the
21748 character that precedes the wrap point. This is not
21749 needed on GUI frames, because (see below) there we
21750 move from the left margin one grapheme cluster at a
21751 time, and stop when we hit the wrap point. */
21752 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21753 {
21754 void *it_data = NULL;
21755 struct it it2;
21756
21757 SAVE_IT (it2, it, it_data);
21758 move_it_in_display_line_to (&it, ZV, target_x,
21759 MOVE_TO_POS | MOVE_TO_X);
21760 /* If we arrived at target_x, that _is_ the last
21761 character on the previous line. */
21762 if (it.current_x != target_x)
21763 target_x = it.current_x - 1;
21764 RESTORE_IT (&it, &it2, it_data);
21765 }
21766 }
21767 }
21768 else
21769 {
21770 if (at_eol_p
21771 || (target_x >= it.last_visible_x
21772 && it.line_wrap != TRUNCATE))
21773 {
21774 if (pt_x > 0)
21775 move_it_by_lines (&it, 0);
21776 move_it_by_lines (&it, 1);
21777 target_x = 0;
21778 }
21779 }
21780
21781 /* Move to the target X coordinate. */
21782 #ifdef HAVE_WINDOW_SYSTEM
21783 /* On GUI frames, as we don't know the X coordinate of the
21784 character to the left of point, moving point to the left
21785 requires walking, one grapheme cluster at a time, until we
21786 find ourself at a place immediately to the left of the
21787 character at point. */
21788 if (FRAME_WINDOW_P (it.f) && dir < 0)
21789 {
21790 struct text_pos new_pos;
21791 enum move_it_result rc = MOVE_X_REACHED;
21792
21793 if (it.current_x == 0)
21794 get_next_display_element (&it);
21795 if (it.what == IT_COMPOSITION)
21796 {
21797 new_pos.charpos = it.cmp_it.charpos;
21798 new_pos.bytepos = -1;
21799 }
21800 else
21801 new_pos = it.current.pos;
21802
21803 while (it.current_x + it.pixel_width <= target_x
21804 && (rc == MOVE_X_REACHED
21805 /* Under word-wrap, move_it_in_display_line_to
21806 stops at correct coordinates, but sometimes
21807 returns MOVE_POS_MATCH_OR_ZV. */
21808 || (it.line_wrap == WORD_WRAP
21809 && rc == MOVE_POS_MATCH_OR_ZV)))
21810 {
21811 int new_x = it.current_x + it.pixel_width;
21812
21813 /* For composed characters, we want the position of the
21814 first character in the grapheme cluster (usually, the
21815 composition's base character), whereas it.current
21816 might give us the position of the _last_ one, e.g. if
21817 the composition is rendered in reverse due to bidi
21818 reordering. */
21819 if (it.what == IT_COMPOSITION)
21820 {
21821 new_pos.charpos = it.cmp_it.charpos;
21822 new_pos.bytepos = -1;
21823 }
21824 else
21825 new_pos = it.current.pos;
21826 if (new_x == it.current_x)
21827 new_x++;
21828 rc = move_it_in_display_line_to (&it, ZV, new_x,
21829 MOVE_TO_POS | MOVE_TO_X);
21830 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21831 break;
21832 }
21833 /* The previous position we saw in the loop is the one we
21834 want. */
21835 if (new_pos.bytepos == -1)
21836 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21837 it.current.pos = new_pos;
21838 }
21839 else
21840 #endif
21841 if (it.current_x != target_x)
21842 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21843
21844 /* If we ended up in a display string that covers point, move to
21845 buffer position to the right in the visual order. */
21846 if (dir > 0)
21847 {
21848 while (IT_CHARPOS (it) == PT)
21849 {
21850 set_iterator_to_next (&it, false);
21851 if (!get_next_display_element (&it))
21852 break;
21853 }
21854 }
21855
21856 /* Move point to that position. */
21857 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21858 }
21859
21860 return make_number (PT);
21861
21862 #undef ROW_GLYPH_NEWLINE_P
21863 }
21864
21865 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21866 Sbidi_resolved_levels, 0, 1, 0,
21867 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21868
21869 The resolved levels are produced by the Emacs bidi reordering engine
21870 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21871 read the Unicode Standard Annex 9 (UAX#9) for background information
21872 about these levels.
21873
21874 VPOS is the zero-based number of the current window's screen line
21875 for which to produce the resolved levels. If VPOS is nil or omitted,
21876 it defaults to the screen line of point. If the window displays a
21877 header line, VPOS of zero will report on the header line, and first
21878 line of text in the window will have VPOS of 1.
21879
21880 Value is an array of resolved levels, indexed by glyph number.
21881 Glyphs are numbered from zero starting from the beginning of the
21882 screen line, i.e. the left edge of the window for left-to-right lines
21883 and from the right edge for right-to-left lines. The resolved levels
21884 are produced only for the window's text area; text in display margins
21885 is not included.
21886
21887 If the selected window's display is not up-to-date, or if the specified
21888 screen line does not display text, this function returns nil. It is
21889 highly recommended to bind this function to some simple key, like F8,
21890 in order to avoid these problems.
21891
21892 This function exists mainly for testing the correctness of the
21893 Emacs UBA implementation, in particular with the test suite. */)
21894 (Lisp_Object vpos)
21895 {
21896 struct window *w = XWINDOW (selected_window);
21897 struct buffer *b = XBUFFER (w->contents);
21898 int nrow;
21899 struct glyph_row *row;
21900
21901 if (NILP (vpos))
21902 {
21903 int d1, d2, d3, d4, d5;
21904
21905 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21906 }
21907 else
21908 {
21909 CHECK_NUMBER_COERCE_MARKER (vpos);
21910 nrow = XINT (vpos);
21911 }
21912
21913 /* We require up-to-date glyph matrix for this window. */
21914 if (w->window_end_valid
21915 && !windows_or_buffers_changed
21916 && b
21917 && !b->clip_changed
21918 && !b->prevent_redisplay_optimizations_p
21919 && !window_outdated (w)
21920 && nrow >= 0
21921 && nrow < w->current_matrix->nrows
21922 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21923 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21924 {
21925 struct glyph *g, *e, *g1;
21926 int nglyphs, i;
21927 Lisp_Object levels;
21928
21929 if (!row->reversed_p) /* Left-to-right glyph row. */
21930 {
21931 g = g1 = row->glyphs[TEXT_AREA];
21932 e = g + row->used[TEXT_AREA];
21933
21934 /* Skip over glyphs at the start of the row that was
21935 generated by redisplay for its own needs. */
21936 while (g < e
21937 && NILP (g->object)
21938 && g->charpos < 0)
21939 g++;
21940 g1 = g;
21941
21942 /* Count the "interesting" glyphs in this row. */
21943 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21944 nglyphs++;
21945
21946 /* Create and fill the array. */
21947 levels = make_uninit_vector (nglyphs);
21948 for (i = 0; g1 < g; i++, g1++)
21949 ASET (levels, i, make_number (g1->resolved_level));
21950 }
21951 else /* Right-to-left glyph row. */
21952 {
21953 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21954 e = row->glyphs[TEXT_AREA] - 1;
21955 while (g > e
21956 && NILP (g->object)
21957 && g->charpos < 0)
21958 g--;
21959 g1 = g;
21960 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21961 nglyphs++;
21962 levels = make_uninit_vector (nglyphs);
21963 for (i = 0; g1 > g; i++, g1--)
21964 ASET (levels, i, make_number (g1->resolved_level));
21965 }
21966 return levels;
21967 }
21968 else
21969 return Qnil;
21970 }
21971
21972
21973 \f
21974 /***********************************************************************
21975 Menu Bar
21976 ***********************************************************************/
21977
21978 /* Redisplay the menu bar in the frame for window W.
21979
21980 The menu bar of X frames that don't have X toolkit support is
21981 displayed in a special window W->frame->menu_bar_window.
21982
21983 The menu bar of terminal frames is treated specially as far as
21984 glyph matrices are concerned. Menu bar lines are not part of
21985 windows, so the update is done directly on the frame matrix rows
21986 for the menu bar. */
21987
21988 static void
21989 display_menu_bar (struct window *w)
21990 {
21991 struct frame *f = XFRAME (WINDOW_FRAME (w));
21992 struct it it;
21993 Lisp_Object items;
21994 int i;
21995
21996 /* Don't do all this for graphical frames. */
21997 #ifdef HAVE_NTGUI
21998 if (FRAME_W32_P (f))
21999 return;
22000 #endif
22001 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22002 if (FRAME_X_P (f))
22003 return;
22004 #endif
22005
22006 #ifdef HAVE_NS
22007 if (FRAME_NS_P (f))
22008 return;
22009 #endif /* HAVE_NS */
22010
22011 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22012 eassert (!FRAME_WINDOW_P (f));
22013 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22014 it.first_visible_x = 0;
22015 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22016 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22017 if (FRAME_WINDOW_P (f))
22018 {
22019 /* Menu bar lines are displayed in the desired matrix of the
22020 dummy window menu_bar_window. */
22021 struct window *menu_w;
22022 menu_w = XWINDOW (f->menu_bar_window);
22023 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22024 MENU_FACE_ID);
22025 it.first_visible_x = 0;
22026 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22027 }
22028 else
22029 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22030 {
22031 /* This is a TTY frame, i.e. character hpos/vpos are used as
22032 pixel x/y. */
22033 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22034 MENU_FACE_ID);
22035 it.first_visible_x = 0;
22036 it.last_visible_x = FRAME_COLS (f);
22037 }
22038
22039 /* FIXME: This should be controlled by a user option. See the
22040 comments in redisplay_tool_bar and display_mode_line about
22041 this. */
22042 it.paragraph_embedding = L2R;
22043
22044 /* Clear all rows of the menu bar. */
22045 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22046 {
22047 struct glyph_row *row = it.glyph_row + i;
22048 clear_glyph_row (row);
22049 row->enabled_p = true;
22050 row->full_width_p = true;
22051 row->reversed_p = false;
22052 }
22053
22054 /* Display all items of the menu bar. */
22055 items = FRAME_MENU_BAR_ITEMS (it.f);
22056 for (i = 0; i < ASIZE (items); i += 4)
22057 {
22058 Lisp_Object string;
22059
22060 /* Stop at nil string. */
22061 string = AREF (items, i + 1);
22062 if (NILP (string))
22063 break;
22064
22065 /* Remember where item was displayed. */
22066 ASET (items, i + 3, make_number (it.hpos));
22067
22068 /* Display the item, pad with one space. */
22069 if (it.current_x < it.last_visible_x)
22070 display_string (NULL, string, Qnil, 0, 0, &it,
22071 SCHARS (string) + 1, 0, 0, -1);
22072 }
22073
22074 /* Fill out the line with spaces. */
22075 if (it.current_x < it.last_visible_x)
22076 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22077
22078 /* Compute the total height of the lines. */
22079 compute_line_metrics (&it);
22080 }
22081
22082 /* Deep copy of a glyph row, including the glyphs. */
22083 static void
22084 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22085 {
22086 struct glyph *pointers[1 + LAST_AREA];
22087 int to_used = to->used[TEXT_AREA];
22088
22089 /* Save glyph pointers of TO. */
22090 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22091
22092 /* Do a structure assignment. */
22093 *to = *from;
22094
22095 /* Restore original glyph pointers of TO. */
22096 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22097
22098 /* Copy the glyphs. */
22099 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22100 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22101
22102 /* If we filled only part of the TO row, fill the rest with
22103 space_glyph (which will display as empty space). */
22104 if (to_used > from->used[TEXT_AREA])
22105 fill_up_frame_row_with_spaces (to, to_used);
22106 }
22107
22108 /* Display one menu item on a TTY, by overwriting the glyphs in the
22109 frame F's desired glyph matrix with glyphs produced from the menu
22110 item text. Called from term.c to display TTY drop-down menus one
22111 item at a time.
22112
22113 ITEM_TEXT is the menu item text as a C string.
22114
22115 FACE_ID is the face ID to be used for this menu item. FACE_ID
22116 could specify one of 3 faces: a face for an enabled item, a face
22117 for a disabled item, or a face for a selected item.
22118
22119 X and Y are coordinates of the first glyph in the frame's desired
22120 matrix to be overwritten by the menu item. Since this is a TTY, Y
22121 is the zero-based number of the glyph row and X is the zero-based
22122 glyph number in the row, starting from left, where to start
22123 displaying the item.
22124
22125 SUBMENU means this menu item drops down a submenu, which
22126 should be indicated by displaying a proper visual cue after the
22127 item text. */
22128
22129 void
22130 display_tty_menu_item (const char *item_text, int width, int face_id,
22131 int x, int y, bool submenu)
22132 {
22133 struct it it;
22134 struct frame *f = SELECTED_FRAME ();
22135 struct window *w = XWINDOW (f->selected_window);
22136 struct glyph_row *row;
22137 size_t item_len = strlen (item_text);
22138
22139 eassert (FRAME_TERMCAP_P (f));
22140
22141 /* Don't write beyond the matrix's last row. This can happen for
22142 TTY screens that are not high enough to show the entire menu.
22143 (This is actually a bit of defensive programming, as
22144 tty_menu_display already limits the number of menu items to one
22145 less than the number of screen lines.) */
22146 if (y >= f->desired_matrix->nrows)
22147 return;
22148
22149 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
22150 it.first_visible_x = 0;
22151 it.last_visible_x = FRAME_COLS (f) - 1;
22152 row = it.glyph_row;
22153 /* Start with the row contents from the current matrix. */
22154 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22155 bool saved_width = row->full_width_p;
22156 row->full_width_p = true;
22157 bool saved_reversed = row->reversed_p;
22158 row->reversed_p = false;
22159 row->enabled_p = true;
22160
22161 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22162 desired face. */
22163 eassert (x < f->desired_matrix->matrix_w);
22164 it.current_x = it.hpos = x;
22165 it.current_y = it.vpos = y;
22166 int saved_used = row->used[TEXT_AREA];
22167 bool saved_truncated = row->truncated_on_right_p;
22168 row->used[TEXT_AREA] = x;
22169 it.face_id = face_id;
22170 it.line_wrap = TRUNCATE;
22171
22172 /* FIXME: This should be controlled by a user option. See the
22173 comments in redisplay_tool_bar and display_mode_line about this.
22174 Also, if paragraph_embedding could ever be R2L, changes will be
22175 needed to avoid shifting to the right the row characters in
22176 term.c:append_glyph. */
22177 it.paragraph_embedding = L2R;
22178
22179 /* Pad with a space on the left. */
22180 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22181 width--;
22182 /* Display the menu item, pad with spaces to WIDTH. */
22183 if (submenu)
22184 {
22185 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22186 item_len, 0, FRAME_COLS (f) - 1, -1);
22187 width -= item_len;
22188 /* Indicate with " >" that there's a submenu. */
22189 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22190 FRAME_COLS (f) - 1, -1);
22191 }
22192 else
22193 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22194 width, 0, FRAME_COLS (f) - 1, -1);
22195
22196 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22197 row->truncated_on_right_p = saved_truncated;
22198 row->hash = row_hash (row);
22199 row->full_width_p = saved_width;
22200 row->reversed_p = saved_reversed;
22201 }
22202 \f
22203 /***********************************************************************
22204 Mode Line
22205 ***********************************************************************/
22206
22207 /* Redisplay mode lines in the window tree whose root is WINDOW.
22208 If FORCE, redisplay mode lines unconditionally.
22209 Otherwise, redisplay only mode lines that are garbaged. Value is
22210 the number of windows whose mode lines were redisplayed. */
22211
22212 static int
22213 redisplay_mode_lines (Lisp_Object window, bool force)
22214 {
22215 int nwindows = 0;
22216
22217 while (!NILP (window))
22218 {
22219 struct window *w = XWINDOW (window);
22220
22221 if (WINDOWP (w->contents))
22222 nwindows += redisplay_mode_lines (w->contents, force);
22223 else if (force
22224 || FRAME_GARBAGED_P (XFRAME (w->frame))
22225 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22226 {
22227 struct text_pos lpoint;
22228 struct buffer *old = current_buffer;
22229
22230 /* Set the window's buffer for the mode line display. */
22231 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22232 set_buffer_internal_1 (XBUFFER (w->contents));
22233
22234 /* Point refers normally to the selected window. For any
22235 other window, set up appropriate value. */
22236 if (!EQ (window, selected_window))
22237 {
22238 struct text_pos pt;
22239
22240 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22241 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22242 }
22243
22244 /* Display mode lines. */
22245 clear_glyph_matrix (w->desired_matrix);
22246 if (display_mode_lines (w))
22247 ++nwindows;
22248
22249 /* Restore old settings. */
22250 set_buffer_internal_1 (old);
22251 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22252 }
22253
22254 window = w->next;
22255 }
22256
22257 return nwindows;
22258 }
22259
22260
22261 /* Display the mode and/or header line of window W. Value is the
22262 sum number of mode lines and header lines displayed. */
22263
22264 static int
22265 display_mode_lines (struct window *w)
22266 {
22267 Lisp_Object old_selected_window = selected_window;
22268 Lisp_Object old_selected_frame = selected_frame;
22269 Lisp_Object new_frame = w->frame;
22270 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22271 int n = 0;
22272
22273 selected_frame = new_frame;
22274 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22275 or window's point, then we'd need select_window_1 here as well. */
22276 XSETWINDOW (selected_window, w);
22277 XFRAME (new_frame)->selected_window = selected_window;
22278
22279 /* These will be set while the mode line specs are processed. */
22280 line_number_displayed = false;
22281 w->column_number_displayed = -1;
22282
22283 if (WINDOW_WANTS_MODELINE_P (w))
22284 {
22285 struct window *sel_w = XWINDOW (old_selected_window);
22286
22287 /* Select mode line face based on the real selected window. */
22288 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22289 BVAR (current_buffer, mode_line_format));
22290 ++n;
22291 }
22292
22293 if (WINDOW_WANTS_HEADER_LINE_P (w))
22294 {
22295 display_mode_line (w, HEADER_LINE_FACE_ID,
22296 BVAR (current_buffer, header_line_format));
22297 ++n;
22298 }
22299
22300 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22301 selected_frame = old_selected_frame;
22302 selected_window = old_selected_window;
22303 if (n > 0)
22304 w->must_be_updated_p = true;
22305 return n;
22306 }
22307
22308
22309 /* Display mode or header line of window W. FACE_ID specifies which
22310 line to display; it is either MODE_LINE_FACE_ID or
22311 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22312 display. Value is the pixel height of the mode/header line
22313 displayed. */
22314
22315 static int
22316 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22317 {
22318 struct it it;
22319 struct face *face;
22320 ptrdiff_t count = SPECPDL_INDEX ();
22321
22322 init_iterator (&it, w, -1, -1, NULL, face_id);
22323 /* Don't extend on a previously drawn mode-line.
22324 This may happen if called from pos_visible_p. */
22325 it.glyph_row->enabled_p = false;
22326 prepare_desired_row (w, it.glyph_row, true);
22327
22328 it.glyph_row->mode_line_p = true;
22329
22330 /* FIXME: This should be controlled by a user option. But
22331 supporting such an option is not trivial, since the mode line is
22332 made up of many separate strings. */
22333 it.paragraph_embedding = L2R;
22334
22335 record_unwind_protect (unwind_format_mode_line,
22336 format_mode_line_unwind_data (NULL, NULL,
22337 Qnil, false));
22338
22339 mode_line_target = MODE_LINE_DISPLAY;
22340
22341 /* Temporarily make frame's keyboard the current kboard so that
22342 kboard-local variables in the mode_line_format will get the right
22343 values. */
22344 push_kboard (FRAME_KBOARD (it.f));
22345 record_unwind_save_match_data ();
22346 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22347 pop_kboard ();
22348
22349 unbind_to (count, Qnil);
22350
22351 /* Fill up with spaces. */
22352 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22353
22354 compute_line_metrics (&it);
22355 it.glyph_row->full_width_p = true;
22356 it.glyph_row->continued_p = false;
22357 it.glyph_row->truncated_on_left_p = false;
22358 it.glyph_row->truncated_on_right_p = false;
22359
22360 /* Make a 3D mode-line have a shadow at its right end. */
22361 face = FACE_FROM_ID (it.f, face_id);
22362 extend_face_to_end_of_line (&it);
22363 if (face->box != FACE_NO_BOX)
22364 {
22365 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22366 + it.glyph_row->used[TEXT_AREA] - 1);
22367 last->right_box_line_p = true;
22368 }
22369
22370 return it.glyph_row->height;
22371 }
22372
22373 /* Move element ELT in LIST to the front of LIST.
22374 Return the updated list. */
22375
22376 static Lisp_Object
22377 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22378 {
22379 register Lisp_Object tail, prev;
22380 register Lisp_Object tem;
22381
22382 tail = list;
22383 prev = Qnil;
22384 while (CONSP (tail))
22385 {
22386 tem = XCAR (tail);
22387
22388 if (EQ (elt, tem))
22389 {
22390 /* Splice out the link TAIL. */
22391 if (NILP (prev))
22392 list = XCDR (tail);
22393 else
22394 Fsetcdr (prev, XCDR (tail));
22395
22396 /* Now make it the first. */
22397 Fsetcdr (tail, list);
22398 return tail;
22399 }
22400 else
22401 prev = tail;
22402 tail = XCDR (tail);
22403 QUIT;
22404 }
22405
22406 /* Not found--return unchanged LIST. */
22407 return list;
22408 }
22409
22410 /* Contribute ELT to the mode line for window IT->w. How it
22411 translates into text depends on its data type.
22412
22413 IT describes the display environment in which we display, as usual.
22414
22415 DEPTH is the depth in recursion. It is used to prevent
22416 infinite recursion here.
22417
22418 FIELD_WIDTH is the number of characters the display of ELT should
22419 occupy in the mode line, and PRECISION is the maximum number of
22420 characters to display from ELT's representation. See
22421 display_string for details.
22422
22423 Returns the hpos of the end of the text generated by ELT.
22424
22425 PROPS is a property list to add to any string we encounter.
22426
22427 If RISKY, remove (disregard) any properties in any string
22428 we encounter, and ignore :eval and :propertize.
22429
22430 The global variable `mode_line_target' determines whether the
22431 output is passed to `store_mode_line_noprop',
22432 `store_mode_line_string', or `display_string'. */
22433
22434 static int
22435 display_mode_element (struct it *it, int depth, int field_width, int precision,
22436 Lisp_Object elt, Lisp_Object props, bool risky)
22437 {
22438 int n = 0, field, prec;
22439 bool literal = false;
22440
22441 tail_recurse:
22442 if (depth > 100)
22443 elt = build_string ("*too-deep*");
22444
22445 depth++;
22446
22447 switch (XTYPE (elt))
22448 {
22449 case Lisp_String:
22450 {
22451 /* A string: output it and check for %-constructs within it. */
22452 unsigned char c;
22453 ptrdiff_t offset = 0;
22454
22455 if (SCHARS (elt) > 0
22456 && (!NILP (props) || risky))
22457 {
22458 Lisp_Object oprops, aelt;
22459 oprops = Ftext_properties_at (make_number (0), elt);
22460
22461 /* If the starting string's properties are not what
22462 we want, translate the string. Also, if the string
22463 is risky, do that anyway. */
22464
22465 if (NILP (Fequal (props, oprops)) || risky)
22466 {
22467 /* If the starting string has properties,
22468 merge the specified ones onto the existing ones. */
22469 if (! NILP (oprops) && !risky)
22470 {
22471 Lisp_Object tem;
22472
22473 oprops = Fcopy_sequence (oprops);
22474 tem = props;
22475 while (CONSP (tem))
22476 {
22477 oprops = Fplist_put (oprops, XCAR (tem),
22478 XCAR (XCDR (tem)));
22479 tem = XCDR (XCDR (tem));
22480 }
22481 props = oprops;
22482 }
22483
22484 aelt = Fassoc (elt, mode_line_proptrans_alist);
22485 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22486 {
22487 /* AELT is what we want. Move it to the front
22488 without consing. */
22489 elt = XCAR (aelt);
22490 mode_line_proptrans_alist
22491 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22492 }
22493 else
22494 {
22495 Lisp_Object tem;
22496
22497 /* If AELT has the wrong props, it is useless.
22498 so get rid of it. */
22499 if (! NILP (aelt))
22500 mode_line_proptrans_alist
22501 = Fdelq (aelt, mode_line_proptrans_alist);
22502
22503 elt = Fcopy_sequence (elt);
22504 Fset_text_properties (make_number (0), Flength (elt),
22505 props, elt);
22506 /* Add this item to mode_line_proptrans_alist. */
22507 mode_line_proptrans_alist
22508 = Fcons (Fcons (elt, props),
22509 mode_line_proptrans_alist);
22510 /* Truncate mode_line_proptrans_alist
22511 to at most 50 elements. */
22512 tem = Fnthcdr (make_number (50),
22513 mode_line_proptrans_alist);
22514 if (! NILP (tem))
22515 XSETCDR (tem, Qnil);
22516 }
22517 }
22518 }
22519
22520 offset = 0;
22521
22522 if (literal)
22523 {
22524 prec = precision - n;
22525 switch (mode_line_target)
22526 {
22527 case MODE_LINE_NOPROP:
22528 case MODE_LINE_TITLE:
22529 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22530 break;
22531 case MODE_LINE_STRING:
22532 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22533 break;
22534 case MODE_LINE_DISPLAY:
22535 n += display_string (NULL, elt, Qnil, 0, 0, it,
22536 0, prec, 0, STRING_MULTIBYTE (elt));
22537 break;
22538 }
22539
22540 break;
22541 }
22542
22543 /* Handle the non-literal case. */
22544
22545 while ((precision <= 0 || n < precision)
22546 && SREF (elt, offset) != 0
22547 && (mode_line_target != MODE_LINE_DISPLAY
22548 || it->current_x < it->last_visible_x))
22549 {
22550 ptrdiff_t last_offset = offset;
22551
22552 /* Advance to end of string or next format specifier. */
22553 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22554 ;
22555
22556 if (offset - 1 != last_offset)
22557 {
22558 ptrdiff_t nchars, nbytes;
22559
22560 /* Output to end of string or up to '%'. Field width
22561 is length of string. Don't output more than
22562 PRECISION allows us. */
22563 offset--;
22564
22565 prec = c_string_width (SDATA (elt) + last_offset,
22566 offset - last_offset, precision - n,
22567 &nchars, &nbytes);
22568
22569 switch (mode_line_target)
22570 {
22571 case MODE_LINE_NOPROP:
22572 case MODE_LINE_TITLE:
22573 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22574 break;
22575 case MODE_LINE_STRING:
22576 {
22577 ptrdiff_t bytepos = last_offset;
22578 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22579 ptrdiff_t endpos = (precision <= 0
22580 ? string_byte_to_char (elt, offset)
22581 : charpos + nchars);
22582 Lisp_Object mode_string
22583 = Fsubstring (elt, make_number (charpos),
22584 make_number (endpos));
22585 n += store_mode_line_string (NULL, mode_string, false,
22586 0, 0, Qnil);
22587 }
22588 break;
22589 case MODE_LINE_DISPLAY:
22590 {
22591 ptrdiff_t bytepos = last_offset;
22592 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22593
22594 if (precision <= 0)
22595 nchars = string_byte_to_char (elt, offset) - charpos;
22596 n += display_string (NULL, elt, Qnil, 0, charpos,
22597 it, 0, nchars, 0,
22598 STRING_MULTIBYTE (elt));
22599 }
22600 break;
22601 }
22602 }
22603 else /* c == '%' */
22604 {
22605 ptrdiff_t percent_position = offset;
22606
22607 /* Get the specified minimum width. Zero means
22608 don't pad. */
22609 field = 0;
22610 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22611 field = field * 10 + c - '0';
22612
22613 /* Don't pad beyond the total padding allowed. */
22614 if (field_width - n > 0 && field > field_width - n)
22615 field = field_width - n;
22616
22617 /* Note that either PRECISION <= 0 or N < PRECISION. */
22618 prec = precision - n;
22619
22620 if (c == 'M')
22621 n += display_mode_element (it, depth, field, prec,
22622 Vglobal_mode_string, props,
22623 risky);
22624 else if (c != 0)
22625 {
22626 bool multibyte;
22627 ptrdiff_t bytepos, charpos;
22628 const char *spec;
22629 Lisp_Object string;
22630
22631 bytepos = percent_position;
22632 charpos = (STRING_MULTIBYTE (elt)
22633 ? string_byte_to_char (elt, bytepos)
22634 : bytepos);
22635 spec = decode_mode_spec (it->w, c, field, &string);
22636 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22637
22638 switch (mode_line_target)
22639 {
22640 case MODE_LINE_NOPROP:
22641 case MODE_LINE_TITLE:
22642 n += store_mode_line_noprop (spec, field, prec);
22643 break;
22644 case MODE_LINE_STRING:
22645 {
22646 Lisp_Object tem = build_string (spec);
22647 props = Ftext_properties_at (make_number (charpos), elt);
22648 /* Should only keep face property in props */
22649 n += store_mode_line_string (NULL, tem, false,
22650 field, prec, props);
22651 }
22652 break;
22653 case MODE_LINE_DISPLAY:
22654 {
22655 int nglyphs_before, nwritten;
22656
22657 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22658 nwritten = display_string (spec, string, elt,
22659 charpos, 0, it,
22660 field, prec, 0,
22661 multibyte);
22662
22663 /* Assign to the glyphs written above the
22664 string where the `%x' came from, position
22665 of the `%'. */
22666 if (nwritten > 0)
22667 {
22668 struct glyph *glyph
22669 = (it->glyph_row->glyphs[TEXT_AREA]
22670 + nglyphs_before);
22671 int i;
22672
22673 for (i = 0; i < nwritten; ++i)
22674 {
22675 glyph[i].object = elt;
22676 glyph[i].charpos = charpos;
22677 }
22678
22679 n += nwritten;
22680 }
22681 }
22682 break;
22683 }
22684 }
22685 else /* c == 0 */
22686 break;
22687 }
22688 }
22689 }
22690 break;
22691
22692 case Lisp_Symbol:
22693 /* A symbol: process the value of the symbol recursively
22694 as if it appeared here directly. Avoid error if symbol void.
22695 Special case: if value of symbol is a string, output the string
22696 literally. */
22697 {
22698 register Lisp_Object tem;
22699
22700 /* If the variable is not marked as risky to set
22701 then its contents are risky to use. */
22702 if (NILP (Fget (elt, Qrisky_local_variable)))
22703 risky = true;
22704
22705 tem = Fboundp (elt);
22706 if (!NILP (tem))
22707 {
22708 tem = Fsymbol_value (elt);
22709 /* If value is a string, output that string literally:
22710 don't check for % within it. */
22711 if (STRINGP (tem))
22712 literal = true;
22713
22714 if (!EQ (tem, elt))
22715 {
22716 /* Give up right away for nil or t. */
22717 elt = tem;
22718 goto tail_recurse;
22719 }
22720 }
22721 }
22722 break;
22723
22724 case Lisp_Cons:
22725 {
22726 register Lisp_Object car, tem;
22727
22728 /* A cons cell: five distinct cases.
22729 If first element is :eval or :propertize, do something special.
22730 If first element is a string or a cons, process all the elements
22731 and effectively concatenate them.
22732 If first element is a negative number, truncate displaying cdr to
22733 at most that many characters. If positive, pad (with spaces)
22734 to at least that many characters.
22735 If first element is a symbol, process the cadr or caddr recursively
22736 according to whether the symbol's value is non-nil or nil. */
22737 car = XCAR (elt);
22738 if (EQ (car, QCeval))
22739 {
22740 /* An element of the form (:eval FORM) means evaluate FORM
22741 and use the result as mode line elements. */
22742
22743 if (risky)
22744 break;
22745
22746 if (CONSP (XCDR (elt)))
22747 {
22748 Lisp_Object spec;
22749 spec = safe__eval (true, XCAR (XCDR (elt)));
22750 n += display_mode_element (it, depth, field_width - n,
22751 precision - n, spec, props,
22752 risky);
22753 }
22754 }
22755 else if (EQ (car, QCpropertize))
22756 {
22757 /* An element of the form (:propertize ELT PROPS...)
22758 means display ELT but applying properties PROPS. */
22759
22760 if (risky)
22761 break;
22762
22763 if (CONSP (XCDR (elt)))
22764 n += display_mode_element (it, depth, field_width - n,
22765 precision - n, XCAR (XCDR (elt)),
22766 XCDR (XCDR (elt)), risky);
22767 }
22768 else if (SYMBOLP (car))
22769 {
22770 tem = Fboundp (car);
22771 elt = XCDR (elt);
22772 if (!CONSP (elt))
22773 goto invalid;
22774 /* elt is now the cdr, and we know it is a cons cell.
22775 Use its car if CAR has a non-nil value. */
22776 if (!NILP (tem))
22777 {
22778 tem = Fsymbol_value (car);
22779 if (!NILP (tem))
22780 {
22781 elt = XCAR (elt);
22782 goto tail_recurse;
22783 }
22784 }
22785 /* Symbol's value is nil (or symbol is unbound)
22786 Get the cddr of the original list
22787 and if possible find the caddr and use that. */
22788 elt = XCDR (elt);
22789 if (NILP (elt))
22790 break;
22791 else if (!CONSP (elt))
22792 goto invalid;
22793 elt = XCAR (elt);
22794 goto tail_recurse;
22795 }
22796 else if (INTEGERP (car))
22797 {
22798 register int lim = XINT (car);
22799 elt = XCDR (elt);
22800 if (lim < 0)
22801 {
22802 /* Negative int means reduce maximum width. */
22803 if (precision <= 0)
22804 precision = -lim;
22805 else
22806 precision = min (precision, -lim);
22807 }
22808 else if (lim > 0)
22809 {
22810 /* Padding specified. Don't let it be more than
22811 current maximum. */
22812 if (precision > 0)
22813 lim = min (precision, lim);
22814
22815 /* If that's more padding than already wanted, queue it.
22816 But don't reduce padding already specified even if
22817 that is beyond the current truncation point. */
22818 field_width = max (lim, field_width);
22819 }
22820 goto tail_recurse;
22821 }
22822 else if (STRINGP (car) || CONSP (car))
22823 {
22824 Lisp_Object halftail = elt;
22825 int len = 0;
22826
22827 while (CONSP (elt)
22828 && (precision <= 0 || n < precision))
22829 {
22830 n += display_mode_element (it, depth,
22831 /* Do padding only after the last
22832 element in the list. */
22833 (! CONSP (XCDR (elt))
22834 ? field_width - n
22835 : 0),
22836 precision - n, XCAR (elt),
22837 props, risky);
22838 elt = XCDR (elt);
22839 len++;
22840 if ((len & 1) == 0)
22841 halftail = XCDR (halftail);
22842 /* Check for cycle. */
22843 if (EQ (halftail, elt))
22844 break;
22845 }
22846 }
22847 }
22848 break;
22849
22850 default:
22851 invalid:
22852 elt = build_string ("*invalid*");
22853 goto tail_recurse;
22854 }
22855
22856 /* Pad to FIELD_WIDTH. */
22857 if (field_width > 0 && n < field_width)
22858 {
22859 switch (mode_line_target)
22860 {
22861 case MODE_LINE_NOPROP:
22862 case MODE_LINE_TITLE:
22863 n += store_mode_line_noprop ("", field_width - n, 0);
22864 break;
22865 case MODE_LINE_STRING:
22866 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
22867 Qnil);
22868 break;
22869 case MODE_LINE_DISPLAY:
22870 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22871 0, 0, 0);
22872 break;
22873 }
22874 }
22875
22876 return n;
22877 }
22878
22879 /* Store a mode-line string element in mode_line_string_list.
22880
22881 If STRING is non-null, display that C string. Otherwise, the Lisp
22882 string LISP_STRING is displayed.
22883
22884 FIELD_WIDTH is the minimum number of output glyphs to produce.
22885 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22886 with spaces. FIELD_WIDTH <= 0 means don't pad.
22887
22888 PRECISION is the maximum number of characters to output from
22889 STRING. PRECISION <= 0 means don't truncate the string.
22890
22891 If COPY_STRING, make a copy of LISP_STRING before adding
22892 properties to the string.
22893
22894 PROPS are the properties to add to the string.
22895 The mode_line_string_face face property is always added to the string.
22896 */
22897
22898 static int
22899 store_mode_line_string (const char *string, Lisp_Object lisp_string,
22900 bool copy_string,
22901 int field_width, int precision, Lisp_Object props)
22902 {
22903 ptrdiff_t len;
22904 int n = 0;
22905
22906 if (string != NULL)
22907 {
22908 len = strlen (string);
22909 if (precision > 0 && len > precision)
22910 len = precision;
22911 lisp_string = make_string (string, len);
22912 if (NILP (props))
22913 props = mode_line_string_face_prop;
22914 else if (!NILP (mode_line_string_face))
22915 {
22916 Lisp_Object face = Fplist_get (props, Qface);
22917 props = Fcopy_sequence (props);
22918 if (NILP (face))
22919 face = mode_line_string_face;
22920 else
22921 face = list2 (face, mode_line_string_face);
22922 props = Fplist_put (props, Qface, face);
22923 }
22924 Fadd_text_properties (make_number (0), make_number (len),
22925 props, lisp_string);
22926 }
22927 else
22928 {
22929 len = XFASTINT (Flength (lisp_string));
22930 if (precision > 0 && len > precision)
22931 {
22932 len = precision;
22933 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22934 precision = -1;
22935 }
22936 if (!NILP (mode_line_string_face))
22937 {
22938 Lisp_Object face;
22939 if (NILP (props))
22940 props = Ftext_properties_at (make_number (0), lisp_string);
22941 face = Fplist_get (props, Qface);
22942 if (NILP (face))
22943 face = mode_line_string_face;
22944 else
22945 face = list2 (face, mode_line_string_face);
22946 props = list2 (Qface, face);
22947 if (copy_string)
22948 lisp_string = Fcopy_sequence (lisp_string);
22949 }
22950 if (!NILP (props))
22951 Fadd_text_properties (make_number (0), make_number (len),
22952 props, lisp_string);
22953 }
22954
22955 if (len > 0)
22956 {
22957 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22958 n += len;
22959 }
22960
22961 if (field_width > len)
22962 {
22963 field_width -= len;
22964 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22965 if (!NILP (props))
22966 Fadd_text_properties (make_number (0), make_number (field_width),
22967 props, lisp_string);
22968 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22969 n += field_width;
22970 }
22971
22972 return n;
22973 }
22974
22975
22976 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22977 1, 4, 0,
22978 doc: /* Format a string out of a mode line format specification.
22979 First arg FORMAT specifies the mode line format (see `mode-line-format'
22980 for details) to use.
22981
22982 By default, the format is evaluated for the currently selected window.
22983
22984 Optional second arg FACE specifies the face property to put on all
22985 characters for which no face is specified. The value nil means the
22986 default face. The value t means whatever face the window's mode line
22987 currently uses (either `mode-line' or `mode-line-inactive',
22988 depending on whether the window is the selected window or not).
22989 An integer value means the value string has no text
22990 properties.
22991
22992 Optional third and fourth args WINDOW and BUFFER specify the window
22993 and buffer to use as the context for the formatting (defaults
22994 are the selected window and the WINDOW's buffer). */)
22995 (Lisp_Object format, Lisp_Object face,
22996 Lisp_Object window, Lisp_Object buffer)
22997 {
22998 struct it it;
22999 int len;
23000 struct window *w;
23001 struct buffer *old_buffer = NULL;
23002 int face_id;
23003 bool no_props = INTEGERP (face);
23004 ptrdiff_t count = SPECPDL_INDEX ();
23005 Lisp_Object str;
23006 int string_start = 0;
23007
23008 w = decode_any_window (window);
23009 XSETWINDOW (window, w);
23010
23011 if (NILP (buffer))
23012 buffer = w->contents;
23013 CHECK_BUFFER (buffer);
23014
23015 /* Make formatting the modeline a non-op when noninteractive, otherwise
23016 there will be problems later caused by a partially initialized frame. */
23017 if (NILP (format) || noninteractive)
23018 return empty_unibyte_string;
23019
23020 if (no_props)
23021 face = Qnil;
23022
23023 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23024 : EQ (face, Qt) ? (EQ (window, selected_window)
23025 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23026 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23027 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23028 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23029 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23030 : DEFAULT_FACE_ID;
23031
23032 old_buffer = current_buffer;
23033
23034 /* Save things including mode_line_proptrans_alist,
23035 and set that to nil so that we don't alter the outer value. */
23036 record_unwind_protect (unwind_format_mode_line,
23037 format_mode_line_unwind_data
23038 (XFRAME (WINDOW_FRAME (w)),
23039 old_buffer, selected_window, true));
23040 mode_line_proptrans_alist = Qnil;
23041
23042 Fselect_window (window, Qt);
23043 set_buffer_internal_1 (XBUFFER (buffer));
23044
23045 init_iterator (&it, w, -1, -1, NULL, face_id);
23046
23047 if (no_props)
23048 {
23049 mode_line_target = MODE_LINE_NOPROP;
23050 mode_line_string_face_prop = Qnil;
23051 mode_line_string_list = Qnil;
23052 string_start = MODE_LINE_NOPROP_LEN (0);
23053 }
23054 else
23055 {
23056 mode_line_target = MODE_LINE_STRING;
23057 mode_line_string_list = Qnil;
23058 mode_line_string_face = face;
23059 mode_line_string_face_prop
23060 = NILP (face) ? Qnil : list2 (Qface, face);
23061 }
23062
23063 push_kboard (FRAME_KBOARD (it.f));
23064 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23065 pop_kboard ();
23066
23067 if (no_props)
23068 {
23069 len = MODE_LINE_NOPROP_LEN (string_start);
23070 str = make_string (mode_line_noprop_buf + string_start, len);
23071 }
23072 else
23073 {
23074 mode_line_string_list = Fnreverse (mode_line_string_list);
23075 str = Fmapconcat (Qidentity, mode_line_string_list,
23076 empty_unibyte_string);
23077 }
23078
23079 unbind_to (count, Qnil);
23080 return str;
23081 }
23082
23083 /* Write a null-terminated, right justified decimal representation of
23084 the positive integer D to BUF using a minimal field width WIDTH. */
23085
23086 static void
23087 pint2str (register char *buf, register int width, register ptrdiff_t d)
23088 {
23089 register char *p = buf;
23090
23091 if (d <= 0)
23092 *p++ = '0';
23093 else
23094 {
23095 while (d > 0)
23096 {
23097 *p++ = d % 10 + '0';
23098 d /= 10;
23099 }
23100 }
23101
23102 for (width -= (int) (p - buf); width > 0; --width)
23103 *p++ = ' ';
23104 *p-- = '\0';
23105 while (p > buf)
23106 {
23107 d = *buf;
23108 *buf++ = *p;
23109 *p-- = d;
23110 }
23111 }
23112
23113 /* Write a null-terminated, right justified decimal and "human
23114 readable" representation of the nonnegative integer D to BUF using
23115 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23116
23117 static const char power_letter[] =
23118 {
23119 0, /* no letter */
23120 'k', /* kilo */
23121 'M', /* mega */
23122 'G', /* giga */
23123 'T', /* tera */
23124 'P', /* peta */
23125 'E', /* exa */
23126 'Z', /* zetta */
23127 'Y' /* yotta */
23128 };
23129
23130 static void
23131 pint2hrstr (char *buf, int width, ptrdiff_t d)
23132 {
23133 /* We aim to represent the nonnegative integer D as
23134 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23135 ptrdiff_t quotient = d;
23136 int remainder = 0;
23137 /* -1 means: do not use TENTHS. */
23138 int tenths = -1;
23139 int exponent = 0;
23140
23141 /* Length of QUOTIENT.TENTHS as a string. */
23142 int length;
23143
23144 char * psuffix;
23145 char * p;
23146
23147 if (quotient >= 1000)
23148 {
23149 /* Scale to the appropriate EXPONENT. */
23150 do
23151 {
23152 remainder = quotient % 1000;
23153 quotient /= 1000;
23154 exponent++;
23155 }
23156 while (quotient >= 1000);
23157
23158 /* Round to nearest and decide whether to use TENTHS or not. */
23159 if (quotient <= 9)
23160 {
23161 tenths = remainder / 100;
23162 if (remainder % 100 >= 50)
23163 {
23164 if (tenths < 9)
23165 tenths++;
23166 else
23167 {
23168 quotient++;
23169 if (quotient == 10)
23170 tenths = -1;
23171 else
23172 tenths = 0;
23173 }
23174 }
23175 }
23176 else
23177 if (remainder >= 500)
23178 {
23179 if (quotient < 999)
23180 quotient++;
23181 else
23182 {
23183 quotient = 1;
23184 exponent++;
23185 tenths = 0;
23186 }
23187 }
23188 }
23189
23190 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23191 if (tenths == -1 && quotient <= 99)
23192 if (quotient <= 9)
23193 length = 1;
23194 else
23195 length = 2;
23196 else
23197 length = 3;
23198 p = psuffix = buf + max (width, length);
23199
23200 /* Print EXPONENT. */
23201 *psuffix++ = power_letter[exponent];
23202 *psuffix = '\0';
23203
23204 /* Print TENTHS. */
23205 if (tenths >= 0)
23206 {
23207 *--p = '0' + tenths;
23208 *--p = '.';
23209 }
23210
23211 /* Print QUOTIENT. */
23212 do
23213 {
23214 int digit = quotient % 10;
23215 *--p = '0' + digit;
23216 }
23217 while ((quotient /= 10) != 0);
23218
23219 /* Print leading spaces. */
23220 while (buf < p)
23221 *--p = ' ';
23222 }
23223
23224 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23225 If EOL_FLAG, set also a mnemonic character for end-of-line
23226 type of CODING_SYSTEM. Return updated pointer into BUF. */
23227
23228 static unsigned char invalid_eol_type[] = "(*invalid*)";
23229
23230 static char *
23231 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23232 {
23233 Lisp_Object val;
23234 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23235 const unsigned char *eol_str;
23236 int eol_str_len;
23237 /* The EOL conversion we are using. */
23238 Lisp_Object eoltype;
23239
23240 val = CODING_SYSTEM_SPEC (coding_system);
23241 eoltype = Qnil;
23242
23243 if (!VECTORP (val)) /* Not yet decided. */
23244 {
23245 *buf++ = multibyte ? '-' : ' ';
23246 if (eol_flag)
23247 eoltype = eol_mnemonic_undecided;
23248 /* Don't mention EOL conversion if it isn't decided. */
23249 }
23250 else
23251 {
23252 Lisp_Object attrs;
23253 Lisp_Object eolvalue;
23254
23255 attrs = AREF (val, 0);
23256 eolvalue = AREF (val, 2);
23257
23258 *buf++ = multibyte
23259 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23260 : ' ';
23261
23262 if (eol_flag)
23263 {
23264 /* The EOL conversion that is normal on this system. */
23265
23266 if (NILP (eolvalue)) /* Not yet decided. */
23267 eoltype = eol_mnemonic_undecided;
23268 else if (VECTORP (eolvalue)) /* Not yet decided. */
23269 eoltype = eol_mnemonic_undecided;
23270 else /* eolvalue is Qunix, Qdos, or Qmac. */
23271 eoltype = (EQ (eolvalue, Qunix)
23272 ? eol_mnemonic_unix
23273 : EQ (eolvalue, Qdos)
23274 ? eol_mnemonic_dos : eol_mnemonic_mac);
23275 }
23276 }
23277
23278 if (eol_flag)
23279 {
23280 /* Mention the EOL conversion if it is not the usual one. */
23281 if (STRINGP (eoltype))
23282 {
23283 eol_str = SDATA (eoltype);
23284 eol_str_len = SBYTES (eoltype);
23285 }
23286 else if (CHARACTERP (eoltype))
23287 {
23288 int c = XFASTINT (eoltype);
23289 return buf + CHAR_STRING (c, (unsigned char *) buf);
23290 }
23291 else
23292 {
23293 eol_str = invalid_eol_type;
23294 eol_str_len = sizeof (invalid_eol_type) - 1;
23295 }
23296 memcpy (buf, eol_str, eol_str_len);
23297 buf += eol_str_len;
23298 }
23299
23300 return buf;
23301 }
23302
23303 /* Return a string for the output of a mode line %-spec for window W,
23304 generated by character C. FIELD_WIDTH > 0 means pad the string
23305 returned with spaces to that value. Return a Lisp string in
23306 *STRING if the resulting string is taken from that Lisp string.
23307
23308 Note we operate on the current buffer for most purposes. */
23309
23310 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23311
23312 static const char *
23313 decode_mode_spec (struct window *w, register int c, int field_width,
23314 Lisp_Object *string)
23315 {
23316 Lisp_Object obj;
23317 struct frame *f = XFRAME (WINDOW_FRAME (w));
23318 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23319 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23320 produce strings from numerical values, so limit preposterously
23321 large values of FIELD_WIDTH to avoid overrunning the buffer's
23322 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23323 bytes plus the terminating null. */
23324 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23325 struct buffer *b = current_buffer;
23326
23327 obj = Qnil;
23328 *string = Qnil;
23329
23330 switch (c)
23331 {
23332 case '*':
23333 if (!NILP (BVAR (b, read_only)))
23334 return "%";
23335 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23336 return "*";
23337 return "-";
23338
23339 case '+':
23340 /* This differs from %* only for a modified read-only buffer. */
23341 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23342 return "*";
23343 if (!NILP (BVAR (b, read_only)))
23344 return "%";
23345 return "-";
23346
23347 case '&':
23348 /* This differs from %* in ignoring read-only-ness. */
23349 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23350 return "*";
23351 return "-";
23352
23353 case '%':
23354 return "%";
23355
23356 case '[':
23357 {
23358 int i;
23359 char *p;
23360
23361 if (command_loop_level > 5)
23362 return "[[[... ";
23363 p = decode_mode_spec_buf;
23364 for (i = 0; i < command_loop_level; i++)
23365 *p++ = '[';
23366 *p = 0;
23367 return decode_mode_spec_buf;
23368 }
23369
23370 case ']':
23371 {
23372 int i;
23373 char *p;
23374
23375 if (command_loop_level > 5)
23376 return " ...]]]";
23377 p = decode_mode_spec_buf;
23378 for (i = 0; i < command_loop_level; i++)
23379 *p++ = ']';
23380 *p = 0;
23381 return decode_mode_spec_buf;
23382 }
23383
23384 case '-':
23385 {
23386 register int i;
23387
23388 /* Let lots_of_dashes be a string of infinite length. */
23389 if (mode_line_target == MODE_LINE_NOPROP
23390 || mode_line_target == MODE_LINE_STRING)
23391 return "--";
23392 if (field_width <= 0
23393 || field_width > sizeof (lots_of_dashes))
23394 {
23395 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23396 decode_mode_spec_buf[i] = '-';
23397 decode_mode_spec_buf[i] = '\0';
23398 return decode_mode_spec_buf;
23399 }
23400 else
23401 return lots_of_dashes;
23402 }
23403
23404 case 'b':
23405 obj = BVAR (b, name);
23406 break;
23407
23408 case 'c':
23409 /* %c and %l are ignored in `frame-title-format'.
23410 (In redisplay_internal, the frame title is drawn _before_ the
23411 windows are updated, so the stuff which depends on actual
23412 window contents (such as %l) may fail to render properly, or
23413 even crash emacs.) */
23414 if (mode_line_target == MODE_LINE_TITLE)
23415 return "";
23416 else
23417 {
23418 ptrdiff_t col = current_column ();
23419 w->column_number_displayed = col;
23420 pint2str (decode_mode_spec_buf, width, col);
23421 return decode_mode_spec_buf;
23422 }
23423
23424 case 'e':
23425 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23426 {
23427 if (NILP (Vmemory_full))
23428 return "";
23429 else
23430 return "!MEM FULL! ";
23431 }
23432 #else
23433 return "";
23434 #endif
23435
23436 case 'F':
23437 /* %F displays the frame name. */
23438 if (!NILP (f->title))
23439 return SSDATA (f->title);
23440 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23441 return SSDATA (f->name);
23442 return "Emacs";
23443
23444 case 'f':
23445 obj = BVAR (b, filename);
23446 break;
23447
23448 case 'i':
23449 {
23450 ptrdiff_t size = ZV - BEGV;
23451 pint2str (decode_mode_spec_buf, width, size);
23452 return decode_mode_spec_buf;
23453 }
23454
23455 case 'I':
23456 {
23457 ptrdiff_t size = ZV - BEGV;
23458 pint2hrstr (decode_mode_spec_buf, width, size);
23459 return decode_mode_spec_buf;
23460 }
23461
23462 case 'l':
23463 {
23464 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23465 ptrdiff_t topline, nlines, height;
23466 ptrdiff_t junk;
23467
23468 /* %c and %l are ignored in `frame-title-format'. */
23469 if (mode_line_target == MODE_LINE_TITLE)
23470 return "";
23471
23472 startpos = marker_position (w->start);
23473 startpos_byte = marker_byte_position (w->start);
23474 height = WINDOW_TOTAL_LINES (w);
23475
23476 /* If we decided that this buffer isn't suitable for line numbers,
23477 don't forget that too fast. */
23478 if (w->base_line_pos == -1)
23479 goto no_value;
23480
23481 /* If the buffer is very big, don't waste time. */
23482 if (INTEGERP (Vline_number_display_limit)
23483 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23484 {
23485 w->base_line_pos = 0;
23486 w->base_line_number = 0;
23487 goto no_value;
23488 }
23489
23490 if (w->base_line_number > 0
23491 && w->base_line_pos > 0
23492 && w->base_line_pos <= startpos)
23493 {
23494 line = w->base_line_number;
23495 linepos = w->base_line_pos;
23496 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23497 }
23498 else
23499 {
23500 line = 1;
23501 linepos = BUF_BEGV (b);
23502 linepos_byte = BUF_BEGV_BYTE (b);
23503 }
23504
23505 /* Count lines from base line to window start position. */
23506 nlines = display_count_lines (linepos_byte,
23507 startpos_byte,
23508 startpos, &junk);
23509
23510 topline = nlines + line;
23511
23512 /* Determine a new base line, if the old one is too close
23513 or too far away, or if we did not have one.
23514 "Too close" means it's plausible a scroll-down would
23515 go back past it. */
23516 if (startpos == BUF_BEGV (b))
23517 {
23518 w->base_line_number = topline;
23519 w->base_line_pos = BUF_BEGV (b);
23520 }
23521 else if (nlines < height + 25 || nlines > height * 3 + 50
23522 || linepos == BUF_BEGV (b))
23523 {
23524 ptrdiff_t limit = BUF_BEGV (b);
23525 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23526 ptrdiff_t position;
23527 ptrdiff_t distance =
23528 (height * 2 + 30) * line_number_display_limit_width;
23529
23530 if (startpos - distance > limit)
23531 {
23532 limit = startpos - distance;
23533 limit_byte = CHAR_TO_BYTE (limit);
23534 }
23535
23536 nlines = display_count_lines (startpos_byte,
23537 limit_byte,
23538 - (height * 2 + 30),
23539 &position);
23540 /* If we couldn't find the lines we wanted within
23541 line_number_display_limit_width chars per line,
23542 give up on line numbers for this window. */
23543 if (position == limit_byte && limit == startpos - distance)
23544 {
23545 w->base_line_pos = -1;
23546 w->base_line_number = 0;
23547 goto no_value;
23548 }
23549
23550 w->base_line_number = topline - nlines;
23551 w->base_line_pos = BYTE_TO_CHAR (position);
23552 }
23553
23554 /* Now count lines from the start pos to point. */
23555 nlines = display_count_lines (startpos_byte,
23556 PT_BYTE, PT, &junk);
23557
23558 /* Record that we did display the line number. */
23559 line_number_displayed = true;
23560
23561 /* Make the string to show. */
23562 pint2str (decode_mode_spec_buf, width, topline + nlines);
23563 return decode_mode_spec_buf;
23564 no_value:
23565 {
23566 char *p = decode_mode_spec_buf;
23567 int pad = width - 2;
23568 while (pad-- > 0)
23569 *p++ = ' ';
23570 *p++ = '?';
23571 *p++ = '?';
23572 *p = '\0';
23573 return decode_mode_spec_buf;
23574 }
23575 }
23576 break;
23577
23578 case 'm':
23579 obj = BVAR (b, mode_name);
23580 break;
23581
23582 case 'n':
23583 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23584 return " Narrow";
23585 break;
23586
23587 case 'p':
23588 {
23589 ptrdiff_t pos = marker_position (w->start);
23590 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23591
23592 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23593 {
23594 if (pos <= BUF_BEGV (b))
23595 return "All";
23596 else
23597 return "Bottom";
23598 }
23599 else if (pos <= BUF_BEGV (b))
23600 return "Top";
23601 else
23602 {
23603 if (total > 1000000)
23604 /* Do it differently for a large value, to avoid overflow. */
23605 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23606 else
23607 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23608 /* We can't normally display a 3-digit number,
23609 so get us a 2-digit number that is close. */
23610 if (total == 100)
23611 total = 99;
23612 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23613 return decode_mode_spec_buf;
23614 }
23615 }
23616
23617 /* Display percentage of size above the bottom of the screen. */
23618 case 'P':
23619 {
23620 ptrdiff_t toppos = marker_position (w->start);
23621 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23622 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23623
23624 if (botpos >= BUF_ZV (b))
23625 {
23626 if (toppos <= BUF_BEGV (b))
23627 return "All";
23628 else
23629 return "Bottom";
23630 }
23631 else
23632 {
23633 if (total > 1000000)
23634 /* Do it differently for a large value, to avoid overflow. */
23635 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23636 else
23637 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23638 /* We can't normally display a 3-digit number,
23639 so get us a 2-digit number that is close. */
23640 if (total == 100)
23641 total = 99;
23642 if (toppos <= BUF_BEGV (b))
23643 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23644 else
23645 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23646 return decode_mode_spec_buf;
23647 }
23648 }
23649
23650 case 's':
23651 /* status of process */
23652 obj = Fget_buffer_process (Fcurrent_buffer ());
23653 if (NILP (obj))
23654 return "no process";
23655 #ifndef MSDOS
23656 obj = Fsymbol_name (Fprocess_status (obj));
23657 #endif
23658 break;
23659
23660 case '@':
23661 {
23662 ptrdiff_t count = inhibit_garbage_collection ();
23663 Lisp_Object curdir = BVAR (current_buffer, directory);
23664 Lisp_Object val = Qnil;
23665
23666 if (STRINGP (curdir))
23667 val = call1 (intern ("file-remote-p"), curdir);
23668
23669 unbind_to (count, Qnil);
23670
23671 if (NILP (val))
23672 return "-";
23673 else
23674 return "@";
23675 }
23676
23677 case 'z':
23678 /* coding-system (not including end-of-line format) */
23679 case 'Z':
23680 /* coding-system (including end-of-line type) */
23681 {
23682 bool eol_flag = (c == 'Z');
23683 char *p = decode_mode_spec_buf;
23684
23685 if (! FRAME_WINDOW_P (f))
23686 {
23687 /* No need to mention EOL here--the terminal never needs
23688 to do EOL conversion. */
23689 p = decode_mode_spec_coding (CODING_ID_NAME
23690 (FRAME_KEYBOARD_CODING (f)->id),
23691 p, false);
23692 p = decode_mode_spec_coding (CODING_ID_NAME
23693 (FRAME_TERMINAL_CODING (f)->id),
23694 p, false);
23695 }
23696 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23697 p, eol_flag);
23698
23699 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23700 #ifdef subprocesses
23701 obj = Fget_buffer_process (Fcurrent_buffer ());
23702 if (PROCESSP (obj))
23703 {
23704 p = decode_mode_spec_coding
23705 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23706 p = decode_mode_spec_coding
23707 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23708 }
23709 #endif /* subprocesses */
23710 #endif /* false */
23711 *p = 0;
23712 return decode_mode_spec_buf;
23713 }
23714 }
23715
23716 if (STRINGP (obj))
23717 {
23718 *string = obj;
23719 return SSDATA (obj);
23720 }
23721 else
23722 return "";
23723 }
23724
23725
23726 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23727 means count lines back from START_BYTE. But don't go beyond
23728 LIMIT_BYTE. Return the number of lines thus found (always
23729 nonnegative).
23730
23731 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23732 either the position COUNT lines after/before START_BYTE, if we
23733 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23734 COUNT lines. */
23735
23736 static ptrdiff_t
23737 display_count_lines (ptrdiff_t start_byte,
23738 ptrdiff_t limit_byte, ptrdiff_t count,
23739 ptrdiff_t *byte_pos_ptr)
23740 {
23741 register unsigned char *cursor;
23742 unsigned char *base;
23743
23744 register ptrdiff_t ceiling;
23745 register unsigned char *ceiling_addr;
23746 ptrdiff_t orig_count = count;
23747
23748 /* If we are not in selective display mode,
23749 check only for newlines. */
23750 bool selective_display
23751 = (!NILP (BVAR (current_buffer, selective_display))
23752 && !INTEGERP (BVAR (current_buffer, selective_display)));
23753
23754 if (count > 0)
23755 {
23756 while (start_byte < limit_byte)
23757 {
23758 ceiling = BUFFER_CEILING_OF (start_byte);
23759 ceiling = min (limit_byte - 1, ceiling);
23760 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23761 base = (cursor = BYTE_POS_ADDR (start_byte));
23762
23763 do
23764 {
23765 if (selective_display)
23766 {
23767 while (*cursor != '\n' && *cursor != 015
23768 && ++cursor != ceiling_addr)
23769 continue;
23770 if (cursor == ceiling_addr)
23771 break;
23772 }
23773 else
23774 {
23775 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23776 if (! cursor)
23777 break;
23778 }
23779
23780 cursor++;
23781
23782 if (--count == 0)
23783 {
23784 start_byte += cursor - base;
23785 *byte_pos_ptr = start_byte;
23786 return orig_count;
23787 }
23788 }
23789 while (cursor < ceiling_addr);
23790
23791 start_byte += ceiling_addr - base;
23792 }
23793 }
23794 else
23795 {
23796 while (start_byte > limit_byte)
23797 {
23798 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23799 ceiling = max (limit_byte, ceiling);
23800 ceiling_addr = BYTE_POS_ADDR (ceiling);
23801 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23802 while (true)
23803 {
23804 if (selective_display)
23805 {
23806 while (--cursor >= ceiling_addr
23807 && *cursor != '\n' && *cursor != 015)
23808 continue;
23809 if (cursor < ceiling_addr)
23810 break;
23811 }
23812 else
23813 {
23814 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23815 if (! cursor)
23816 break;
23817 }
23818
23819 if (++count == 0)
23820 {
23821 start_byte += cursor - base + 1;
23822 *byte_pos_ptr = start_byte;
23823 /* When scanning backwards, we should
23824 not count the newline posterior to which we stop. */
23825 return - orig_count - 1;
23826 }
23827 }
23828 start_byte += ceiling_addr - base;
23829 }
23830 }
23831
23832 *byte_pos_ptr = limit_byte;
23833
23834 if (count < 0)
23835 return - orig_count + count;
23836 return orig_count - count;
23837
23838 }
23839
23840
23841 \f
23842 /***********************************************************************
23843 Displaying strings
23844 ***********************************************************************/
23845
23846 /* Display a NUL-terminated string, starting with index START.
23847
23848 If STRING is non-null, display that C string. Otherwise, the Lisp
23849 string LISP_STRING is displayed. There's a case that STRING is
23850 non-null and LISP_STRING is not nil. It means STRING is a string
23851 data of LISP_STRING. In that case, we display LISP_STRING while
23852 ignoring its text properties.
23853
23854 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23855 FACE_STRING. Display STRING or LISP_STRING with the face at
23856 FACE_STRING_POS in FACE_STRING:
23857
23858 Display the string in the environment given by IT, but use the
23859 standard display table, temporarily.
23860
23861 FIELD_WIDTH is the minimum number of output glyphs to produce.
23862 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23863 with spaces. If STRING has more characters, more than FIELD_WIDTH
23864 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23865
23866 PRECISION is the maximum number of characters to output from
23867 STRING. PRECISION < 0 means don't truncate the string.
23868
23869 This is roughly equivalent to printf format specifiers:
23870
23871 FIELD_WIDTH PRECISION PRINTF
23872 ----------------------------------------
23873 -1 -1 %s
23874 -1 10 %.10s
23875 10 -1 %10s
23876 20 10 %20.10s
23877
23878 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23879 display them, and < 0 means obey the current buffer's value of
23880 enable_multibyte_characters.
23881
23882 Value is the number of columns displayed. */
23883
23884 static int
23885 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23886 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23887 int field_width, int precision, int max_x, int multibyte)
23888 {
23889 int hpos_at_start = it->hpos;
23890 int saved_face_id = it->face_id;
23891 struct glyph_row *row = it->glyph_row;
23892 ptrdiff_t it_charpos;
23893
23894 /* Initialize the iterator IT for iteration over STRING beginning
23895 with index START. */
23896 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23897 precision, field_width, multibyte);
23898 if (string && STRINGP (lisp_string))
23899 /* LISP_STRING is the one returned by decode_mode_spec. We should
23900 ignore its text properties. */
23901 it->stop_charpos = it->end_charpos;
23902
23903 /* If displaying STRING, set up the face of the iterator from
23904 FACE_STRING, if that's given. */
23905 if (STRINGP (face_string))
23906 {
23907 ptrdiff_t endptr;
23908 struct face *face;
23909
23910 it->face_id
23911 = face_at_string_position (it->w, face_string, face_string_pos,
23912 0, &endptr, it->base_face_id, false);
23913 face = FACE_FROM_ID (it->f, it->face_id);
23914 it->face_box_p = face->box != FACE_NO_BOX;
23915 }
23916
23917 /* Set max_x to the maximum allowed X position. Don't let it go
23918 beyond the right edge of the window. */
23919 if (max_x <= 0)
23920 max_x = it->last_visible_x;
23921 else
23922 max_x = min (max_x, it->last_visible_x);
23923
23924 /* Skip over display elements that are not visible. because IT->w is
23925 hscrolled. */
23926 if (it->current_x < it->first_visible_x)
23927 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23928 MOVE_TO_POS | MOVE_TO_X);
23929
23930 row->ascent = it->max_ascent;
23931 row->height = it->max_ascent + it->max_descent;
23932 row->phys_ascent = it->max_phys_ascent;
23933 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23934 row->extra_line_spacing = it->max_extra_line_spacing;
23935
23936 if (STRINGP (it->string))
23937 it_charpos = IT_STRING_CHARPOS (*it);
23938 else
23939 it_charpos = IT_CHARPOS (*it);
23940
23941 /* This condition is for the case that we are called with current_x
23942 past last_visible_x. */
23943 while (it->current_x < max_x)
23944 {
23945 int x_before, x, n_glyphs_before, i, nglyphs;
23946
23947 /* Get the next display element. */
23948 if (!get_next_display_element (it))
23949 break;
23950
23951 /* Produce glyphs. */
23952 x_before = it->current_x;
23953 n_glyphs_before = row->used[TEXT_AREA];
23954 PRODUCE_GLYPHS (it);
23955
23956 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23957 i = 0;
23958 x = x_before;
23959 while (i < nglyphs)
23960 {
23961 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23962
23963 if (it->line_wrap != TRUNCATE
23964 && x + glyph->pixel_width > max_x)
23965 {
23966 /* End of continued line or max_x reached. */
23967 if (CHAR_GLYPH_PADDING_P (*glyph))
23968 {
23969 /* A wide character is unbreakable. */
23970 if (row->reversed_p)
23971 unproduce_glyphs (it, row->used[TEXT_AREA]
23972 - n_glyphs_before);
23973 row->used[TEXT_AREA] = n_glyphs_before;
23974 it->current_x = x_before;
23975 }
23976 else
23977 {
23978 if (row->reversed_p)
23979 unproduce_glyphs (it, row->used[TEXT_AREA]
23980 - (n_glyphs_before + i));
23981 row->used[TEXT_AREA] = n_glyphs_before + i;
23982 it->current_x = x;
23983 }
23984 break;
23985 }
23986 else if (x + glyph->pixel_width >= it->first_visible_x)
23987 {
23988 /* Glyph is at least partially visible. */
23989 ++it->hpos;
23990 if (x < it->first_visible_x)
23991 row->x = x - it->first_visible_x;
23992 }
23993 else
23994 {
23995 /* Glyph is off the left margin of the display area.
23996 Should not happen. */
23997 emacs_abort ();
23998 }
23999
24000 row->ascent = max (row->ascent, it->max_ascent);
24001 row->height = max (row->height, it->max_ascent + it->max_descent);
24002 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24003 row->phys_height = max (row->phys_height,
24004 it->max_phys_ascent + it->max_phys_descent);
24005 row->extra_line_spacing = max (row->extra_line_spacing,
24006 it->max_extra_line_spacing);
24007 x += glyph->pixel_width;
24008 ++i;
24009 }
24010
24011 /* Stop if max_x reached. */
24012 if (i < nglyphs)
24013 break;
24014
24015 /* Stop at line ends. */
24016 if (ITERATOR_AT_END_OF_LINE_P (it))
24017 {
24018 it->continuation_lines_width = 0;
24019 break;
24020 }
24021
24022 set_iterator_to_next (it, true);
24023 if (STRINGP (it->string))
24024 it_charpos = IT_STRING_CHARPOS (*it);
24025 else
24026 it_charpos = IT_CHARPOS (*it);
24027
24028 /* Stop if truncating at the right edge. */
24029 if (it->line_wrap == TRUNCATE
24030 && it->current_x >= it->last_visible_x)
24031 {
24032 /* Add truncation mark, but don't do it if the line is
24033 truncated at a padding space. */
24034 if (it_charpos < it->string_nchars)
24035 {
24036 if (!FRAME_WINDOW_P (it->f))
24037 {
24038 int ii, n;
24039
24040 if (it->current_x > it->last_visible_x)
24041 {
24042 if (!row->reversed_p)
24043 {
24044 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24045 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24046 break;
24047 }
24048 else
24049 {
24050 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24051 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24052 break;
24053 unproduce_glyphs (it, ii + 1);
24054 ii = row->used[TEXT_AREA] - (ii + 1);
24055 }
24056 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24057 {
24058 row->used[TEXT_AREA] = ii;
24059 produce_special_glyphs (it, IT_TRUNCATION);
24060 }
24061 }
24062 produce_special_glyphs (it, IT_TRUNCATION);
24063 }
24064 row->truncated_on_right_p = true;
24065 }
24066 break;
24067 }
24068 }
24069
24070 /* Maybe insert a truncation at the left. */
24071 if (it->first_visible_x
24072 && it_charpos > 0)
24073 {
24074 if (!FRAME_WINDOW_P (it->f)
24075 || (row->reversed_p
24076 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24077 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24078 insert_left_trunc_glyphs (it);
24079 row->truncated_on_left_p = true;
24080 }
24081
24082 it->face_id = saved_face_id;
24083
24084 /* Value is number of columns displayed. */
24085 return it->hpos - hpos_at_start;
24086 }
24087
24088
24089 \f
24090 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24091 appears as an element of LIST or as the car of an element of LIST.
24092 If PROPVAL is a list, compare each element against LIST in that
24093 way, and return 1/2 if any element of PROPVAL is found in LIST.
24094 Otherwise return 0. This function cannot quit.
24095 The return value is 2 if the text is invisible but with an ellipsis
24096 and 1 if it's invisible and without an ellipsis. */
24097
24098 int
24099 invisible_prop (Lisp_Object propval, Lisp_Object list)
24100 {
24101 Lisp_Object tail, proptail;
24102
24103 for (tail = list; CONSP (tail); tail = XCDR (tail))
24104 {
24105 register Lisp_Object tem;
24106 tem = XCAR (tail);
24107 if (EQ (propval, tem))
24108 return 1;
24109 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24110 return NILP (XCDR (tem)) ? 1 : 2;
24111 }
24112
24113 if (CONSP (propval))
24114 {
24115 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
24116 {
24117 Lisp_Object propelt;
24118 propelt = XCAR (proptail);
24119 for (tail = list; CONSP (tail); tail = XCDR (tail))
24120 {
24121 register Lisp_Object tem;
24122 tem = XCAR (tail);
24123 if (EQ (propelt, tem))
24124 return 1;
24125 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
24126 return NILP (XCDR (tem)) ? 1 : 2;
24127 }
24128 }
24129 }
24130
24131 return 0;
24132 }
24133
24134 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
24135 doc: /* Non-nil if the property makes the text invisible.
24136 POS-OR-PROP can be a marker or number, in which case it is taken to be
24137 a position in the current buffer and the value of the `invisible' property
24138 is checked; or it can be some other value, which is then presumed to be the
24139 value of the `invisible' property of the text of interest.
24140 The non-nil value returned can be t for truly invisible text or something
24141 else if the text is replaced by an ellipsis. */)
24142 (Lisp_Object pos_or_prop)
24143 {
24144 Lisp_Object prop
24145 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
24146 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
24147 : pos_or_prop);
24148 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
24149 return (invis == 0 ? Qnil
24150 : invis == 1 ? Qt
24151 : make_number (invis));
24152 }
24153
24154 /* Calculate a width or height in pixels from a specification using
24155 the following elements:
24156
24157 SPEC ::=
24158 NUM - a (fractional) multiple of the default font width/height
24159 (NUM) - specifies exactly NUM pixels
24160 UNIT - a fixed number of pixels, see below.
24161 ELEMENT - size of a display element in pixels, see below.
24162 (NUM . SPEC) - equals NUM * SPEC
24163 (+ SPEC SPEC ...) - add pixel values
24164 (- SPEC SPEC ...) - subtract pixel values
24165 (- SPEC) - negate pixel value
24166
24167 NUM ::=
24168 INT or FLOAT - a number constant
24169 SYMBOL - use symbol's (buffer local) variable binding.
24170
24171 UNIT ::=
24172 in - pixels per inch *)
24173 mm - pixels per 1/1000 meter *)
24174 cm - pixels per 1/100 meter *)
24175 width - width of current font in pixels.
24176 height - height of current font in pixels.
24177
24178 *) using the ratio(s) defined in display-pixels-per-inch.
24179
24180 ELEMENT ::=
24181
24182 left-fringe - left fringe width in pixels
24183 right-fringe - right fringe width in pixels
24184
24185 left-margin - left margin width in pixels
24186 right-margin - right margin width in pixels
24187
24188 scroll-bar - scroll-bar area width in pixels
24189
24190 Examples:
24191
24192 Pixels corresponding to 5 inches:
24193 (5 . in)
24194
24195 Total width of non-text areas on left side of window (if scroll-bar is on left):
24196 '(space :width (+ left-fringe left-margin scroll-bar))
24197
24198 Align to first text column (in header line):
24199 '(space :align-to 0)
24200
24201 Align to middle of text area minus half the width of variable `my-image'
24202 containing a loaded image:
24203 '(space :align-to (0.5 . (- text my-image)))
24204
24205 Width of left margin minus width of 1 character in the default font:
24206 '(space :width (- left-margin 1))
24207
24208 Width of left margin minus width of 2 characters in the current font:
24209 '(space :width (- left-margin (2 . width)))
24210
24211 Center 1 character over left-margin (in header line):
24212 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24213
24214 Different ways to express width of left fringe plus left margin minus one pixel:
24215 '(space :width (- (+ left-fringe left-margin) (1)))
24216 '(space :width (+ left-fringe left-margin (- (1))))
24217 '(space :width (+ left-fringe left-margin (-1)))
24218
24219 */
24220
24221 static bool
24222 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24223 struct font *font, bool width_p, int *align_to)
24224 {
24225 double pixels;
24226
24227 # define OK_PIXELS(val) (*res = (val), true)
24228 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24229
24230 if (NILP (prop))
24231 return OK_PIXELS (0);
24232
24233 eassert (FRAME_LIVE_P (it->f));
24234
24235 if (SYMBOLP (prop))
24236 {
24237 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24238 {
24239 char *unit = SSDATA (SYMBOL_NAME (prop));
24240
24241 if (unit[0] == 'i' && unit[1] == 'n')
24242 pixels = 1.0;
24243 else if (unit[0] == 'm' && unit[1] == 'm')
24244 pixels = 25.4;
24245 else if (unit[0] == 'c' && unit[1] == 'm')
24246 pixels = 2.54;
24247 else
24248 pixels = 0;
24249 if (pixels > 0)
24250 {
24251 double ppi = (width_p ? FRAME_RES_X (it->f)
24252 : FRAME_RES_Y (it->f));
24253
24254 if (ppi > 0)
24255 return OK_PIXELS (ppi / pixels);
24256 return false;
24257 }
24258 }
24259
24260 #ifdef HAVE_WINDOW_SYSTEM
24261 if (EQ (prop, Qheight))
24262 return OK_PIXELS (font
24263 ? normal_char_height (font, -1)
24264 : FRAME_LINE_HEIGHT (it->f));
24265 if (EQ (prop, Qwidth))
24266 return OK_PIXELS (font
24267 ? FONT_WIDTH (font)
24268 : FRAME_COLUMN_WIDTH (it->f));
24269 #else
24270 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24271 return OK_PIXELS (1);
24272 #endif
24273
24274 if (EQ (prop, Qtext))
24275 return OK_PIXELS (width_p
24276 ? window_box_width (it->w, TEXT_AREA)
24277 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24278
24279 if (align_to && *align_to < 0)
24280 {
24281 *res = 0;
24282 if (EQ (prop, Qleft))
24283 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24284 if (EQ (prop, Qright))
24285 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24286 if (EQ (prop, Qcenter))
24287 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24288 + window_box_width (it->w, TEXT_AREA) / 2);
24289 if (EQ (prop, Qleft_fringe))
24290 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24291 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24292 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24293 if (EQ (prop, Qright_fringe))
24294 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24295 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24296 : window_box_right_offset (it->w, TEXT_AREA));
24297 if (EQ (prop, Qleft_margin))
24298 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24299 if (EQ (prop, Qright_margin))
24300 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24301 if (EQ (prop, Qscroll_bar))
24302 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24303 ? 0
24304 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24305 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24306 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24307 : 0)));
24308 }
24309 else
24310 {
24311 if (EQ (prop, Qleft_fringe))
24312 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24313 if (EQ (prop, Qright_fringe))
24314 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24315 if (EQ (prop, Qleft_margin))
24316 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24317 if (EQ (prop, Qright_margin))
24318 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24319 if (EQ (prop, Qscroll_bar))
24320 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24321 }
24322
24323 prop = buffer_local_value (prop, it->w->contents);
24324 if (EQ (prop, Qunbound))
24325 prop = Qnil;
24326 }
24327
24328 if (NUMBERP (prop))
24329 {
24330 int base_unit = (width_p
24331 ? FRAME_COLUMN_WIDTH (it->f)
24332 : FRAME_LINE_HEIGHT (it->f));
24333 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24334 }
24335
24336 if (CONSP (prop))
24337 {
24338 Lisp_Object car = XCAR (prop);
24339 Lisp_Object cdr = XCDR (prop);
24340
24341 if (SYMBOLP (car))
24342 {
24343 #ifdef HAVE_WINDOW_SYSTEM
24344 if (FRAME_WINDOW_P (it->f)
24345 && valid_image_p (prop))
24346 {
24347 ptrdiff_t id = lookup_image (it->f, prop);
24348 struct image *img = IMAGE_FROM_ID (it->f, id);
24349
24350 return OK_PIXELS (width_p ? img->width : img->height);
24351 }
24352 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
24353 {
24354 // TODO: Don't return dummy size.
24355 return OK_PIXELS (100);
24356 }
24357 #endif
24358 if (EQ (car, Qplus) || EQ (car, Qminus))
24359 {
24360 bool first = true;
24361 double px;
24362
24363 pixels = 0;
24364 while (CONSP (cdr))
24365 {
24366 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24367 font, width_p, align_to))
24368 return false;
24369 if (first)
24370 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24371 else
24372 pixels += px;
24373 cdr = XCDR (cdr);
24374 }
24375 if (EQ (car, Qminus))
24376 pixels = -pixels;
24377 return OK_PIXELS (pixels);
24378 }
24379
24380 car = buffer_local_value (car, it->w->contents);
24381 if (EQ (car, Qunbound))
24382 car = Qnil;
24383 }
24384
24385 if (NUMBERP (car))
24386 {
24387 double fact;
24388 pixels = XFLOATINT (car);
24389 if (NILP (cdr))
24390 return OK_PIXELS (pixels);
24391 if (calc_pixel_width_or_height (&fact, it, cdr,
24392 font, width_p, align_to))
24393 return OK_PIXELS (pixels * fact);
24394 return false;
24395 }
24396
24397 return false;
24398 }
24399
24400 return false;
24401 }
24402
24403 void
24404 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24405 {
24406 #ifdef HAVE_WINDOW_SYSTEM
24407 normal_char_ascent_descent (font, -1, ascent, descent);
24408 #else
24409 *ascent = 1;
24410 *descent = 0;
24411 #endif
24412 }
24413
24414 \f
24415 /***********************************************************************
24416 Glyph Display
24417 ***********************************************************************/
24418
24419 #ifdef HAVE_WINDOW_SYSTEM
24420
24421 #ifdef GLYPH_DEBUG
24422
24423 void
24424 dump_glyph_string (struct glyph_string *s)
24425 {
24426 fprintf (stderr, "glyph string\n");
24427 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24428 s->x, s->y, s->width, s->height);
24429 fprintf (stderr, " ybase = %d\n", s->ybase);
24430 fprintf (stderr, " hl = %d\n", s->hl);
24431 fprintf (stderr, " left overhang = %d, right = %d\n",
24432 s->left_overhang, s->right_overhang);
24433 fprintf (stderr, " nchars = %d\n", s->nchars);
24434 fprintf (stderr, " extends to end of line = %d\n",
24435 s->extends_to_end_of_line_p);
24436 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24437 fprintf (stderr, " bg width = %d\n", s->background_width);
24438 }
24439
24440 #endif /* GLYPH_DEBUG */
24441
24442 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24443 of XChar2b structures for S; it can't be allocated in
24444 init_glyph_string because it must be allocated via `alloca'. W
24445 is the window on which S is drawn. ROW and AREA are the glyph row
24446 and area within the row from which S is constructed. START is the
24447 index of the first glyph structure covered by S. HL is a
24448 face-override for drawing S. */
24449
24450 #ifdef HAVE_NTGUI
24451 #define OPTIONAL_HDC(hdc) HDC hdc,
24452 #define DECLARE_HDC(hdc) HDC hdc;
24453 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24454 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24455 #endif
24456
24457 #ifndef OPTIONAL_HDC
24458 #define OPTIONAL_HDC(hdc)
24459 #define DECLARE_HDC(hdc)
24460 #define ALLOCATE_HDC(hdc, f)
24461 #define RELEASE_HDC(hdc, f)
24462 #endif
24463
24464 static void
24465 init_glyph_string (struct glyph_string *s,
24466 OPTIONAL_HDC (hdc)
24467 XChar2b *char2b, struct window *w, struct glyph_row *row,
24468 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24469 {
24470 memset (s, 0, sizeof *s);
24471 s->w = w;
24472 s->f = XFRAME (w->frame);
24473 #ifdef HAVE_NTGUI
24474 s->hdc = hdc;
24475 #endif
24476 s->display = FRAME_X_DISPLAY (s->f);
24477 s->window = FRAME_X_WINDOW (s->f);
24478 s->char2b = char2b;
24479 s->hl = hl;
24480 s->row = row;
24481 s->area = area;
24482 s->first_glyph = row->glyphs[area] + start;
24483 s->height = row->height;
24484 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24485 s->ybase = s->y + row->ascent;
24486 }
24487
24488
24489 /* Append the list of glyph strings with head H and tail T to the list
24490 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24491
24492 static void
24493 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24494 struct glyph_string *h, struct glyph_string *t)
24495 {
24496 if (h)
24497 {
24498 if (*head)
24499 (*tail)->next = h;
24500 else
24501 *head = h;
24502 h->prev = *tail;
24503 *tail = t;
24504 }
24505 }
24506
24507
24508 /* Prepend the list of glyph strings with head H and tail T to the
24509 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24510 result. */
24511
24512 static void
24513 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24514 struct glyph_string *h, struct glyph_string *t)
24515 {
24516 if (h)
24517 {
24518 if (*head)
24519 (*head)->prev = t;
24520 else
24521 *tail = t;
24522 t->next = *head;
24523 *head = h;
24524 }
24525 }
24526
24527
24528 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24529 Set *HEAD and *TAIL to the resulting list. */
24530
24531 static void
24532 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24533 struct glyph_string *s)
24534 {
24535 s->next = s->prev = NULL;
24536 append_glyph_string_lists (head, tail, s, s);
24537 }
24538
24539
24540 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24541 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24542 make sure that X resources for the face returned are allocated.
24543 Value is a pointer to a realized face that is ready for display if
24544 DISPLAY_P. */
24545
24546 static struct face *
24547 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24548 XChar2b *char2b, bool display_p)
24549 {
24550 struct face *face = FACE_FROM_ID (f, face_id);
24551 unsigned code = 0;
24552
24553 if (face->font)
24554 {
24555 code = face->font->driver->encode_char (face->font, c);
24556
24557 if (code == FONT_INVALID_CODE)
24558 code = 0;
24559 }
24560 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24561
24562 /* Make sure X resources of the face are allocated. */
24563 #ifdef HAVE_X_WINDOWS
24564 if (display_p)
24565 #endif
24566 {
24567 eassert (face != NULL);
24568 prepare_face_for_display (f, face);
24569 }
24570
24571 return face;
24572 }
24573
24574
24575 /* Get face and two-byte form of character glyph GLYPH on frame F.
24576 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24577 a pointer to a realized face that is ready for display. */
24578
24579 static struct face *
24580 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24581 XChar2b *char2b)
24582 {
24583 struct face *face;
24584 unsigned code = 0;
24585
24586 eassert (glyph->type == CHAR_GLYPH);
24587 face = FACE_FROM_ID (f, glyph->face_id);
24588
24589 /* Make sure X resources of the face are allocated. */
24590 eassert (face != NULL);
24591 prepare_face_for_display (f, face);
24592
24593 if (face->font)
24594 {
24595 if (CHAR_BYTE8_P (glyph->u.ch))
24596 code = CHAR_TO_BYTE8 (glyph->u.ch);
24597 else
24598 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24599
24600 if (code == FONT_INVALID_CODE)
24601 code = 0;
24602 }
24603
24604 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24605 return face;
24606 }
24607
24608
24609 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24610 Return true iff FONT has a glyph for C. */
24611
24612 static bool
24613 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24614 {
24615 unsigned code;
24616
24617 if (CHAR_BYTE8_P (c))
24618 code = CHAR_TO_BYTE8 (c);
24619 else
24620 code = font->driver->encode_char (font, c);
24621
24622 if (code == FONT_INVALID_CODE)
24623 return false;
24624 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24625 return true;
24626 }
24627
24628
24629 /* Fill glyph string S with composition components specified by S->cmp.
24630
24631 BASE_FACE is the base face of the composition.
24632 S->cmp_from is the index of the first component for S.
24633
24634 OVERLAPS non-zero means S should draw the foreground only, and use
24635 its physical height for clipping. See also draw_glyphs.
24636
24637 Value is the index of a component not in S. */
24638
24639 static int
24640 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24641 int overlaps)
24642 {
24643 int i;
24644 /* For all glyphs of this composition, starting at the offset
24645 S->cmp_from, until we reach the end of the definition or encounter a
24646 glyph that requires the different face, add it to S. */
24647 struct face *face;
24648
24649 eassert (s);
24650
24651 s->for_overlaps = overlaps;
24652 s->face = NULL;
24653 s->font = NULL;
24654 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24655 {
24656 int c = COMPOSITION_GLYPH (s->cmp, i);
24657
24658 /* TAB in a composition means display glyphs with padding space
24659 on the left or right. */
24660 if (c != '\t')
24661 {
24662 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24663 -1, Qnil);
24664
24665 face = get_char_face_and_encoding (s->f, c, face_id,
24666 s->char2b + i, true);
24667 if (face)
24668 {
24669 if (! s->face)
24670 {
24671 s->face = face;
24672 s->font = s->face->font;
24673 }
24674 else if (s->face != face)
24675 break;
24676 }
24677 }
24678 ++s->nchars;
24679 }
24680 s->cmp_to = i;
24681
24682 if (s->face == NULL)
24683 {
24684 s->face = base_face->ascii_face;
24685 s->font = s->face->font;
24686 }
24687
24688 /* All glyph strings for the same composition has the same width,
24689 i.e. the width set for the first component of the composition. */
24690 s->width = s->first_glyph->pixel_width;
24691
24692 /* If the specified font could not be loaded, use the frame's
24693 default font, but record the fact that we couldn't load it in
24694 the glyph string so that we can draw rectangles for the
24695 characters of the glyph string. */
24696 if (s->font == NULL)
24697 {
24698 s->font_not_found_p = true;
24699 s->font = FRAME_FONT (s->f);
24700 }
24701
24702 /* Adjust base line for subscript/superscript text. */
24703 s->ybase += s->first_glyph->voffset;
24704
24705 return s->cmp_to;
24706 }
24707
24708 static int
24709 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24710 int start, int end, int overlaps)
24711 {
24712 struct glyph *glyph, *last;
24713 Lisp_Object lgstring;
24714 int i;
24715
24716 s->for_overlaps = overlaps;
24717 glyph = s->row->glyphs[s->area] + start;
24718 last = s->row->glyphs[s->area] + end;
24719 s->cmp_id = glyph->u.cmp.id;
24720 s->cmp_from = glyph->slice.cmp.from;
24721 s->cmp_to = glyph->slice.cmp.to + 1;
24722 s->face = FACE_FROM_ID (s->f, face_id);
24723 lgstring = composition_gstring_from_id (s->cmp_id);
24724 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24725 glyph++;
24726 while (glyph < last
24727 && glyph->u.cmp.automatic
24728 && glyph->u.cmp.id == s->cmp_id
24729 && s->cmp_to == glyph->slice.cmp.from)
24730 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24731
24732 for (i = s->cmp_from; i < s->cmp_to; i++)
24733 {
24734 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24735 unsigned code = LGLYPH_CODE (lglyph);
24736
24737 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24738 }
24739 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24740 return glyph - s->row->glyphs[s->area];
24741 }
24742
24743
24744 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24745 See the comment of fill_glyph_string for arguments.
24746 Value is the index of the first glyph not in S. */
24747
24748
24749 static int
24750 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24751 int start, int end, int overlaps)
24752 {
24753 struct glyph *glyph, *last;
24754 int voffset;
24755
24756 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24757 s->for_overlaps = overlaps;
24758 glyph = s->row->glyphs[s->area] + start;
24759 last = s->row->glyphs[s->area] + end;
24760 voffset = glyph->voffset;
24761 s->face = FACE_FROM_ID (s->f, face_id);
24762 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24763 s->nchars = 1;
24764 s->width = glyph->pixel_width;
24765 glyph++;
24766 while (glyph < last
24767 && glyph->type == GLYPHLESS_GLYPH
24768 && glyph->voffset == voffset
24769 && glyph->face_id == face_id)
24770 {
24771 s->nchars++;
24772 s->width += glyph->pixel_width;
24773 glyph++;
24774 }
24775 s->ybase += voffset;
24776 return glyph - s->row->glyphs[s->area];
24777 }
24778
24779
24780 /* Fill glyph string S from a sequence of character glyphs.
24781
24782 FACE_ID is the face id of the string. START is the index of the
24783 first glyph to consider, END is the index of the last + 1.
24784 OVERLAPS non-zero means S should draw the foreground only, and use
24785 its physical height for clipping. See also draw_glyphs.
24786
24787 Value is the index of the first glyph not in S. */
24788
24789 static int
24790 fill_glyph_string (struct glyph_string *s, int face_id,
24791 int start, int end, int overlaps)
24792 {
24793 struct glyph *glyph, *last;
24794 int voffset;
24795 bool glyph_not_available_p;
24796
24797 eassert (s->f == XFRAME (s->w->frame));
24798 eassert (s->nchars == 0);
24799 eassert (start >= 0 && end > start);
24800
24801 s->for_overlaps = overlaps;
24802 glyph = s->row->glyphs[s->area] + start;
24803 last = s->row->glyphs[s->area] + end;
24804 voffset = glyph->voffset;
24805 s->padding_p = glyph->padding_p;
24806 glyph_not_available_p = glyph->glyph_not_available_p;
24807
24808 while (glyph < last
24809 && glyph->type == CHAR_GLYPH
24810 && glyph->voffset == voffset
24811 /* Same face id implies same font, nowadays. */
24812 && glyph->face_id == face_id
24813 && glyph->glyph_not_available_p == glyph_not_available_p)
24814 {
24815 s->face = get_glyph_face_and_encoding (s->f, glyph,
24816 s->char2b + s->nchars);
24817 ++s->nchars;
24818 eassert (s->nchars <= end - start);
24819 s->width += glyph->pixel_width;
24820 if (glyph++->padding_p != s->padding_p)
24821 break;
24822 }
24823
24824 s->font = s->face->font;
24825
24826 /* If the specified font could not be loaded, use the frame's font,
24827 but record the fact that we couldn't load it in
24828 S->font_not_found_p so that we can draw rectangles for the
24829 characters of the glyph string. */
24830 if (s->font == NULL || glyph_not_available_p)
24831 {
24832 s->font_not_found_p = true;
24833 s->font = FRAME_FONT (s->f);
24834 }
24835
24836 /* Adjust base line for subscript/superscript text. */
24837 s->ybase += voffset;
24838
24839 eassert (s->face && s->face->gc);
24840 return glyph - s->row->glyphs[s->area];
24841 }
24842
24843
24844 /* Fill glyph string S from image glyph S->first_glyph. */
24845
24846 static void
24847 fill_image_glyph_string (struct glyph_string *s)
24848 {
24849 eassert (s->first_glyph->type == IMAGE_GLYPH);
24850 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24851 eassert (s->img);
24852 s->slice = s->first_glyph->slice.img;
24853 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24854 s->font = s->face->font;
24855 s->width = s->first_glyph->pixel_width;
24856
24857 /* Adjust base line for subscript/superscript text. */
24858 s->ybase += s->first_glyph->voffset;
24859 }
24860
24861
24862 #ifdef HAVE_XWIDGETS
24863 static void
24864 fill_xwidget_glyph_string (struct glyph_string *s)
24865 {
24866 eassert (s->first_glyph->type == XWIDGET_GLYPH);
24867 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24868 s->font = s->face->font;
24869 s->width = s->first_glyph->pixel_width;
24870 s->ybase += s->first_glyph->voffset;
24871 s->xwidget = s->first_glyph->u.xwidget;
24872 }
24873 #endif
24874 /* Fill glyph string S from a sequence of stretch glyphs.
24875
24876 START is the index of the first glyph to consider,
24877 END is the index of the last + 1.
24878
24879 Value is the index of the first glyph not in S. */
24880
24881 static int
24882 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24883 {
24884 struct glyph *glyph, *last;
24885 int voffset, face_id;
24886
24887 eassert (s->first_glyph->type == STRETCH_GLYPH);
24888
24889 glyph = s->row->glyphs[s->area] + start;
24890 last = s->row->glyphs[s->area] + end;
24891 face_id = glyph->face_id;
24892 s->face = FACE_FROM_ID (s->f, face_id);
24893 s->font = s->face->font;
24894 s->width = glyph->pixel_width;
24895 s->nchars = 1;
24896 voffset = glyph->voffset;
24897
24898 for (++glyph;
24899 (glyph < last
24900 && glyph->type == STRETCH_GLYPH
24901 && glyph->voffset == voffset
24902 && glyph->face_id == face_id);
24903 ++glyph)
24904 s->width += glyph->pixel_width;
24905
24906 /* Adjust base line for subscript/superscript text. */
24907 s->ybase += voffset;
24908
24909 /* The case that face->gc == 0 is handled when drawing the glyph
24910 string by calling prepare_face_for_display. */
24911 eassert (s->face);
24912 return glyph - s->row->glyphs[s->area];
24913 }
24914
24915 static struct font_metrics *
24916 get_per_char_metric (struct font *font, XChar2b *char2b)
24917 {
24918 static struct font_metrics metrics;
24919 unsigned code;
24920
24921 if (! font)
24922 return NULL;
24923 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24924 if (code == FONT_INVALID_CODE)
24925 return NULL;
24926 font->driver->text_extents (font, &code, 1, &metrics);
24927 return &metrics;
24928 }
24929
24930 /* A subroutine that computes "normal" values of ASCENT and DESCENT
24931 for FONT. Values are taken from font-global ones, except for fonts
24932 that claim preposterously large values, but whose glyphs actually
24933 have reasonable dimensions. C is the character to use for metrics
24934 if the font-global values are too large; if C is negative, the
24935 function selects a default character. */
24936 static void
24937 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
24938 {
24939 *ascent = FONT_BASE (font);
24940 *descent = FONT_DESCENT (font);
24941
24942 if (FONT_TOO_HIGH (font))
24943 {
24944 XChar2b char2b;
24945
24946 /* Get metrics of C, defaulting to a reasonably sized ASCII
24947 character. */
24948 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
24949 {
24950 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
24951
24952 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
24953 {
24954 /* We add 1 pixel to character dimensions as heuristics
24955 that produces nicer display, e.g. when the face has
24956 the box attribute. */
24957 *ascent = pcm->ascent + 1;
24958 *descent = pcm->descent + 1;
24959 }
24960 }
24961 }
24962 }
24963
24964 /* A subroutine that computes a reasonable "normal character height"
24965 for fonts that claim preposterously large vertical dimensions, but
24966 whose glyphs are actually reasonably sized. C is the character
24967 whose metrics to use for those fonts, or -1 for default
24968 character. */
24969 static int
24970 normal_char_height (struct font *font, int c)
24971 {
24972 int ascent, descent;
24973
24974 normal_char_ascent_descent (font, c, &ascent, &descent);
24975
24976 return ascent + descent;
24977 }
24978
24979 /* EXPORT for RIF:
24980 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24981 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24982 assumed to be zero. */
24983
24984 void
24985 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24986 {
24987 *left = *right = 0;
24988
24989 if (glyph->type == CHAR_GLYPH)
24990 {
24991 XChar2b char2b;
24992 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
24993 if (face->font)
24994 {
24995 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
24996 if (pcm)
24997 {
24998 if (pcm->rbearing > pcm->width)
24999 *right = pcm->rbearing - pcm->width;
25000 if (pcm->lbearing < 0)
25001 *left = -pcm->lbearing;
25002 }
25003 }
25004 }
25005 else if (glyph->type == COMPOSITE_GLYPH)
25006 {
25007 if (! glyph->u.cmp.automatic)
25008 {
25009 struct composition *cmp = composition_table[glyph->u.cmp.id];
25010
25011 if (cmp->rbearing > cmp->pixel_width)
25012 *right = cmp->rbearing - cmp->pixel_width;
25013 if (cmp->lbearing < 0)
25014 *left = - cmp->lbearing;
25015 }
25016 else
25017 {
25018 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25019 struct font_metrics metrics;
25020
25021 composition_gstring_width (gstring, glyph->slice.cmp.from,
25022 glyph->slice.cmp.to + 1, &metrics);
25023 if (metrics.rbearing > metrics.width)
25024 *right = metrics.rbearing - metrics.width;
25025 if (metrics.lbearing < 0)
25026 *left = - metrics.lbearing;
25027 }
25028 }
25029 }
25030
25031
25032 /* Return the index of the first glyph preceding glyph string S that
25033 is overwritten by S because of S's left overhang. Value is -1
25034 if no glyphs are overwritten. */
25035
25036 static int
25037 left_overwritten (struct glyph_string *s)
25038 {
25039 int k;
25040
25041 if (s->left_overhang)
25042 {
25043 int x = 0, i;
25044 struct glyph *glyphs = s->row->glyphs[s->area];
25045 int first = s->first_glyph - glyphs;
25046
25047 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25048 x -= glyphs[i].pixel_width;
25049
25050 k = i + 1;
25051 }
25052 else
25053 k = -1;
25054
25055 return k;
25056 }
25057
25058
25059 /* Return the index of the first glyph preceding glyph string S that
25060 is overwriting S because of its right overhang. Value is -1 if no
25061 glyph in front of S overwrites S. */
25062
25063 static int
25064 left_overwriting (struct glyph_string *s)
25065 {
25066 int i, k, x;
25067 struct glyph *glyphs = s->row->glyphs[s->area];
25068 int first = s->first_glyph - glyphs;
25069
25070 k = -1;
25071 x = 0;
25072 for (i = first - 1; i >= 0; --i)
25073 {
25074 int left, right;
25075 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25076 if (x + right > 0)
25077 k = i;
25078 x -= glyphs[i].pixel_width;
25079 }
25080
25081 return k;
25082 }
25083
25084
25085 /* Return the index of the last glyph following glyph string S that is
25086 overwritten by S because of S's right overhang. Value is -1 if
25087 no such glyph is found. */
25088
25089 static int
25090 right_overwritten (struct glyph_string *s)
25091 {
25092 int k = -1;
25093
25094 if (s->right_overhang)
25095 {
25096 int x = 0, i;
25097 struct glyph *glyphs = s->row->glyphs[s->area];
25098 int first = (s->first_glyph - glyphs
25099 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25100 int end = s->row->used[s->area];
25101
25102 for (i = first; i < end && s->right_overhang > x; ++i)
25103 x += glyphs[i].pixel_width;
25104
25105 k = i;
25106 }
25107
25108 return k;
25109 }
25110
25111
25112 /* Return the index of the last glyph following glyph string S that
25113 overwrites S because of its left overhang. Value is negative
25114 if no such glyph is found. */
25115
25116 static int
25117 right_overwriting (struct glyph_string *s)
25118 {
25119 int i, k, x;
25120 int end = s->row->used[s->area];
25121 struct glyph *glyphs = s->row->glyphs[s->area];
25122 int first = (s->first_glyph - glyphs
25123 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25124
25125 k = -1;
25126 x = 0;
25127 for (i = first; i < end; ++i)
25128 {
25129 int left, right;
25130 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25131 if (x - left < 0)
25132 k = i;
25133 x += glyphs[i].pixel_width;
25134 }
25135
25136 return k;
25137 }
25138
25139
25140 /* Set background width of glyph string S. START is the index of the
25141 first glyph following S. LAST_X is the right-most x-position + 1
25142 in the drawing area. */
25143
25144 static void
25145 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
25146 {
25147 /* If the face of this glyph string has to be drawn to the end of
25148 the drawing area, set S->extends_to_end_of_line_p. */
25149
25150 if (start == s->row->used[s->area]
25151 && ((s->row->fill_line_p
25152 && (s->hl == DRAW_NORMAL_TEXT
25153 || s->hl == DRAW_IMAGE_RAISED
25154 || s->hl == DRAW_IMAGE_SUNKEN))
25155 || s->hl == DRAW_MOUSE_FACE))
25156 s->extends_to_end_of_line_p = true;
25157
25158 /* If S extends its face to the end of the line, set its
25159 background_width to the distance to the right edge of the drawing
25160 area. */
25161 if (s->extends_to_end_of_line_p)
25162 s->background_width = last_x - s->x + 1;
25163 else
25164 s->background_width = s->width;
25165 }
25166
25167
25168 /* Compute overhangs and x-positions for glyph string S and its
25169 predecessors, or successors. X is the starting x-position for S.
25170 BACKWARD_P means process predecessors. */
25171
25172 static void
25173 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
25174 {
25175 if (backward_p)
25176 {
25177 while (s)
25178 {
25179 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25180 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25181 x -= s->width;
25182 s->x = x;
25183 s = s->prev;
25184 }
25185 }
25186 else
25187 {
25188 while (s)
25189 {
25190 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25191 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25192 s->x = x;
25193 x += s->width;
25194 s = s->next;
25195 }
25196 }
25197 }
25198
25199
25200
25201 /* The following macros are only called from draw_glyphs below.
25202 They reference the following parameters of that function directly:
25203 `w', `row', `area', and `overlap_p'
25204 as well as the following local variables:
25205 `s', `f', and `hdc' (in W32) */
25206
25207 #ifdef HAVE_NTGUI
25208 /* On W32, silently add local `hdc' variable to argument list of
25209 init_glyph_string. */
25210 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25211 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25212 #else
25213 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25214 init_glyph_string (s, char2b, w, row, area, start, hl)
25215 #endif
25216
25217 /* Add a glyph string for a stretch glyph to the list of strings
25218 between HEAD and TAIL. START is the index of the stretch glyph in
25219 row area AREA of glyph row ROW. END is the index of the last glyph
25220 in that glyph row area. X is the current output position assigned
25221 to the new glyph string constructed. HL overrides that face of the
25222 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25223 is the right-most x-position of the drawing area. */
25224
25225 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25226 and below -- keep them on one line. */
25227 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25228 do \
25229 { \
25230 s = alloca (sizeof *s); \
25231 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25232 START = fill_stretch_glyph_string (s, START, END); \
25233 append_glyph_string (&HEAD, &TAIL, s); \
25234 s->x = (X); \
25235 } \
25236 while (false)
25237
25238
25239 /* Add a glyph string for an image glyph to the list of strings
25240 between HEAD and TAIL. START is the index of the image glyph in
25241 row area AREA of glyph row ROW. END is the index of the last glyph
25242 in that glyph row area. X is the current output position assigned
25243 to the new glyph string constructed. HL overrides that face of the
25244 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25245 is the right-most x-position of the drawing area. */
25246
25247 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25248 do \
25249 { \
25250 s = alloca (sizeof *s); \
25251 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25252 fill_image_glyph_string (s); \
25253 append_glyph_string (&HEAD, &TAIL, s); \
25254 ++START; \
25255 s->x = (X); \
25256 } \
25257 while (false)
25258
25259 #ifndef HAVE_XWIDGETS
25260 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25261 eassume (false)
25262 #else
25263 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25264 do \
25265 { \
25266 s = alloca (sizeof *s); \
25267 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25268 fill_xwidget_glyph_string (s); \
25269 append_glyph_string (&(HEAD), &(TAIL), s); \
25270 ++(START); \
25271 s->x = (X); \
25272 } \
25273 while (false)
25274 #endif
25275
25276 /* Add a glyph string for a sequence of character glyphs to the list
25277 of strings between HEAD and TAIL. START is the index of the first
25278 glyph in row area AREA of glyph row ROW that is part of the new
25279 glyph string. END is the index of the last glyph in that glyph row
25280 area. X is the current output position assigned to the new glyph
25281 string constructed. HL overrides that face of the glyph; e.g. it
25282 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25283 right-most x-position of the drawing area. */
25284
25285 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25286 do \
25287 { \
25288 int face_id; \
25289 XChar2b *char2b; \
25290 \
25291 face_id = (row)->glyphs[area][START].face_id; \
25292 \
25293 s = alloca (sizeof *s); \
25294 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25295 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25296 append_glyph_string (&HEAD, &TAIL, s); \
25297 s->x = (X); \
25298 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25299 } \
25300 while (false)
25301
25302
25303 /* Add a glyph string for a composite sequence to the list of strings
25304 between HEAD and TAIL. START is the index of the first glyph in
25305 row area AREA of glyph row ROW that is part of the new glyph
25306 string. END is the index of the last glyph in that glyph row area.
25307 X is the current output position assigned to the new glyph string
25308 constructed. HL overrides that face of the glyph; e.g. it is
25309 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25310 x-position of the drawing area. */
25311
25312 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25313 do { \
25314 int face_id = (row)->glyphs[area][START].face_id; \
25315 struct face *base_face = FACE_FROM_ID (f, face_id); \
25316 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25317 struct composition *cmp = composition_table[cmp_id]; \
25318 XChar2b *char2b; \
25319 struct glyph_string *first_s = NULL; \
25320 int n; \
25321 \
25322 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25323 \
25324 /* Make glyph_strings for each glyph sequence that is drawable by \
25325 the same face, and append them to HEAD/TAIL. */ \
25326 for (n = 0; n < cmp->glyph_len;) \
25327 { \
25328 s = alloca (sizeof *s); \
25329 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25330 append_glyph_string (&(HEAD), &(TAIL), s); \
25331 s->cmp = cmp; \
25332 s->cmp_from = n; \
25333 s->x = (X); \
25334 if (n == 0) \
25335 first_s = s; \
25336 n = fill_composite_glyph_string (s, base_face, overlaps); \
25337 } \
25338 \
25339 ++START; \
25340 s = first_s; \
25341 } while (false)
25342
25343
25344 /* Add a glyph string for a glyph-string sequence to the list of strings
25345 between HEAD and TAIL. */
25346
25347 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25348 do { \
25349 int face_id; \
25350 XChar2b *char2b; \
25351 Lisp_Object gstring; \
25352 \
25353 face_id = (row)->glyphs[area][START].face_id; \
25354 gstring = (composition_gstring_from_id \
25355 ((row)->glyphs[area][START].u.cmp.id)); \
25356 s = alloca (sizeof *s); \
25357 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25358 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25359 append_glyph_string (&(HEAD), &(TAIL), s); \
25360 s->x = (X); \
25361 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25362 } while (false)
25363
25364
25365 /* Add a glyph string for a sequence of glyphless character's glyphs
25366 to the list of strings between HEAD and TAIL. The meanings of
25367 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25368
25369 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25370 do \
25371 { \
25372 int face_id; \
25373 \
25374 face_id = (row)->glyphs[area][START].face_id; \
25375 \
25376 s = alloca (sizeof *s); \
25377 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25378 append_glyph_string (&HEAD, &TAIL, s); \
25379 s->x = (X); \
25380 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25381 overlaps); \
25382 } \
25383 while (false)
25384
25385
25386 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25387 of AREA of glyph row ROW on window W between indices START and END.
25388 HL overrides the face for drawing glyph strings, e.g. it is
25389 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25390 x-positions of the drawing area.
25391
25392 This is an ugly monster macro construct because we must use alloca
25393 to allocate glyph strings (because draw_glyphs can be called
25394 asynchronously). */
25395
25396 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25397 do \
25398 { \
25399 HEAD = TAIL = NULL; \
25400 while (START < END) \
25401 { \
25402 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25403 switch (first_glyph->type) \
25404 { \
25405 case CHAR_GLYPH: \
25406 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25407 HL, X, LAST_X); \
25408 break; \
25409 \
25410 case COMPOSITE_GLYPH: \
25411 if (first_glyph->u.cmp.automatic) \
25412 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25413 HL, X, LAST_X); \
25414 else \
25415 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25416 HL, X, LAST_X); \
25417 break; \
25418 \
25419 case STRETCH_GLYPH: \
25420 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25421 HL, X, LAST_X); \
25422 break; \
25423 \
25424 case IMAGE_GLYPH: \
25425 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25426 HL, X, LAST_X); \
25427 break;
25428
25429 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25430 case XWIDGET_GLYPH: \
25431 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
25432 HL, X, LAST_X); \
25433 break;
25434
25435 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
25436 case GLYPHLESS_GLYPH: \
25437 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25438 HL, X, LAST_X); \
25439 break; \
25440 \
25441 default: \
25442 emacs_abort (); \
25443 } \
25444 \
25445 if (s) \
25446 { \
25447 set_glyph_string_background_width (s, START, LAST_X); \
25448 (X) += s->width; \
25449 } \
25450 } \
25451 } while (false)
25452
25453
25454 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25455 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25456 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25457 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
25458
25459
25460 /* Draw glyphs between START and END in AREA of ROW on window W,
25461 starting at x-position X. X is relative to AREA in W. HL is a
25462 face-override with the following meaning:
25463
25464 DRAW_NORMAL_TEXT draw normally
25465 DRAW_CURSOR draw in cursor face
25466 DRAW_MOUSE_FACE draw in mouse face.
25467 DRAW_INVERSE_VIDEO draw in mode line face
25468 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25469 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25470
25471 If OVERLAPS is non-zero, draw only the foreground of characters and
25472 clip to the physical height of ROW. Non-zero value also defines
25473 the overlapping part to be drawn:
25474
25475 OVERLAPS_PRED overlap with preceding rows
25476 OVERLAPS_SUCC overlap with succeeding rows
25477 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25478 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25479
25480 Value is the x-position reached, relative to AREA of W. */
25481
25482 static int
25483 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25484 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25485 enum draw_glyphs_face hl, int overlaps)
25486 {
25487 struct glyph_string *head, *tail;
25488 struct glyph_string *s;
25489 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25490 int i, j, x_reached, last_x, area_left = 0;
25491 struct frame *f = XFRAME (WINDOW_FRAME (w));
25492 DECLARE_HDC (hdc);
25493
25494 ALLOCATE_HDC (hdc, f);
25495
25496 /* Let's rather be paranoid than getting a SEGV. */
25497 end = min (end, row->used[area]);
25498 start = clip_to_bounds (0, start, end);
25499
25500 /* Translate X to frame coordinates. Set last_x to the right
25501 end of the drawing area. */
25502 if (row->full_width_p)
25503 {
25504 /* X is relative to the left edge of W, without scroll bars
25505 or fringes. */
25506 area_left = WINDOW_LEFT_EDGE_X (w);
25507 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25508 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25509 }
25510 else
25511 {
25512 area_left = window_box_left (w, area);
25513 last_x = area_left + window_box_width (w, area);
25514 }
25515 x += area_left;
25516
25517 /* Build a doubly-linked list of glyph_string structures between
25518 head and tail from what we have to draw. Note that the macro
25519 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25520 the reason we use a separate variable `i'. */
25521 i = start;
25522 USE_SAFE_ALLOCA;
25523 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25524 if (tail)
25525 x_reached = tail->x + tail->background_width;
25526 else
25527 x_reached = x;
25528
25529 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25530 the row, redraw some glyphs in front or following the glyph
25531 strings built above. */
25532 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25533 {
25534 struct glyph_string *h, *t;
25535 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25536 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25537 bool check_mouse_face = false;
25538 int dummy_x = 0;
25539
25540 /* If mouse highlighting is on, we may need to draw adjacent
25541 glyphs using mouse-face highlighting. */
25542 if (area == TEXT_AREA && row->mouse_face_p
25543 && hlinfo->mouse_face_beg_row >= 0
25544 && hlinfo->mouse_face_end_row >= 0)
25545 {
25546 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25547
25548 if (row_vpos >= hlinfo->mouse_face_beg_row
25549 && row_vpos <= hlinfo->mouse_face_end_row)
25550 {
25551 check_mouse_face = true;
25552 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25553 ? hlinfo->mouse_face_beg_col : 0;
25554 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25555 ? hlinfo->mouse_face_end_col
25556 : row->used[TEXT_AREA];
25557 }
25558 }
25559
25560 /* Compute overhangs for all glyph strings. */
25561 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25562 for (s = head; s; s = s->next)
25563 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25564
25565 /* Prepend glyph strings for glyphs in front of the first glyph
25566 string that are overwritten because of the first glyph
25567 string's left overhang. The background of all strings
25568 prepended must be drawn because the first glyph string
25569 draws over it. */
25570 i = left_overwritten (head);
25571 if (i >= 0)
25572 {
25573 enum draw_glyphs_face overlap_hl;
25574
25575 /* If this row contains mouse highlighting, attempt to draw
25576 the overlapped glyphs with the correct highlight. This
25577 code fails if the overlap encompasses more than one glyph
25578 and mouse-highlight spans only some of these glyphs.
25579 However, making it work perfectly involves a lot more
25580 code, and I don't know if the pathological case occurs in
25581 practice, so we'll stick to this for now. --- cyd */
25582 if (check_mouse_face
25583 && mouse_beg_col < start && mouse_end_col > i)
25584 overlap_hl = DRAW_MOUSE_FACE;
25585 else
25586 overlap_hl = DRAW_NORMAL_TEXT;
25587
25588 if (hl != overlap_hl)
25589 clip_head = head;
25590 j = i;
25591 BUILD_GLYPH_STRINGS (j, start, h, t,
25592 overlap_hl, dummy_x, last_x);
25593 start = i;
25594 compute_overhangs_and_x (t, head->x, true);
25595 prepend_glyph_string_lists (&head, &tail, h, t);
25596 if (clip_head == NULL)
25597 clip_head = head;
25598 }
25599
25600 /* Prepend glyph strings for glyphs in front of the first glyph
25601 string that overwrite that glyph string because of their
25602 right overhang. For these strings, only the foreground must
25603 be drawn, because it draws over the glyph string at `head'.
25604 The background must not be drawn because this would overwrite
25605 right overhangs of preceding glyphs for which no glyph
25606 strings exist. */
25607 i = left_overwriting (head);
25608 if (i >= 0)
25609 {
25610 enum draw_glyphs_face overlap_hl;
25611
25612 if (check_mouse_face
25613 && mouse_beg_col < start && mouse_end_col > i)
25614 overlap_hl = DRAW_MOUSE_FACE;
25615 else
25616 overlap_hl = DRAW_NORMAL_TEXT;
25617
25618 if (hl == overlap_hl || clip_head == NULL)
25619 clip_head = head;
25620 BUILD_GLYPH_STRINGS (i, start, h, t,
25621 overlap_hl, dummy_x, last_x);
25622 for (s = h; s; s = s->next)
25623 s->background_filled_p = true;
25624 compute_overhangs_and_x (t, head->x, true);
25625 prepend_glyph_string_lists (&head, &tail, h, t);
25626 }
25627
25628 /* Append glyphs strings for glyphs following the last glyph
25629 string tail that are overwritten by tail. The background of
25630 these strings has to be drawn because tail's foreground draws
25631 over it. */
25632 i = right_overwritten (tail);
25633 if (i >= 0)
25634 {
25635 enum draw_glyphs_face overlap_hl;
25636
25637 if (check_mouse_face
25638 && mouse_beg_col < i && mouse_end_col > end)
25639 overlap_hl = DRAW_MOUSE_FACE;
25640 else
25641 overlap_hl = DRAW_NORMAL_TEXT;
25642
25643 if (hl != overlap_hl)
25644 clip_tail = tail;
25645 BUILD_GLYPH_STRINGS (end, i, h, t,
25646 overlap_hl, x, last_x);
25647 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25648 we don't have `end = i;' here. */
25649 compute_overhangs_and_x (h, tail->x + tail->width, false);
25650 append_glyph_string_lists (&head, &tail, h, t);
25651 if (clip_tail == NULL)
25652 clip_tail = tail;
25653 }
25654
25655 /* Append glyph strings for glyphs following the last glyph
25656 string tail that overwrite tail. The foreground of such
25657 glyphs has to be drawn because it writes into the background
25658 of tail. The background must not be drawn because it could
25659 paint over the foreground of following glyphs. */
25660 i = right_overwriting (tail);
25661 if (i >= 0)
25662 {
25663 enum draw_glyphs_face overlap_hl;
25664 if (check_mouse_face
25665 && mouse_beg_col < i && mouse_end_col > end)
25666 overlap_hl = DRAW_MOUSE_FACE;
25667 else
25668 overlap_hl = DRAW_NORMAL_TEXT;
25669
25670 if (hl == overlap_hl || clip_tail == NULL)
25671 clip_tail = tail;
25672 i++; /* We must include the Ith glyph. */
25673 BUILD_GLYPH_STRINGS (end, i, h, t,
25674 overlap_hl, x, last_x);
25675 for (s = h; s; s = s->next)
25676 s->background_filled_p = true;
25677 compute_overhangs_and_x (h, tail->x + tail->width, false);
25678 append_glyph_string_lists (&head, &tail, h, t);
25679 }
25680 if (clip_head || clip_tail)
25681 for (s = head; s; s = s->next)
25682 {
25683 s->clip_head = clip_head;
25684 s->clip_tail = clip_tail;
25685 }
25686 }
25687
25688 /* Draw all strings. */
25689 for (s = head; s; s = s->next)
25690 FRAME_RIF (f)->draw_glyph_string (s);
25691
25692 #ifndef HAVE_NS
25693 /* When focus a sole frame and move horizontally, this clears on_p
25694 causing a failure to erase prev cursor position. */
25695 if (area == TEXT_AREA
25696 && !row->full_width_p
25697 /* When drawing overlapping rows, only the glyph strings'
25698 foreground is drawn, which doesn't erase a cursor
25699 completely. */
25700 && !overlaps)
25701 {
25702 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25703 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25704 : (tail ? tail->x + tail->background_width : x));
25705 x0 -= area_left;
25706 x1 -= area_left;
25707
25708 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25709 row->y, MATRIX_ROW_BOTTOM_Y (row));
25710 }
25711 #endif
25712
25713 /* Value is the x-position up to which drawn, relative to AREA of W.
25714 This doesn't include parts drawn because of overhangs. */
25715 if (row->full_width_p)
25716 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25717 else
25718 x_reached -= area_left;
25719
25720 RELEASE_HDC (hdc, f);
25721
25722 SAFE_FREE ();
25723 return x_reached;
25724 }
25725
25726 /* Expand row matrix if too narrow. Don't expand if area
25727 is not present. */
25728
25729 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25730 { \
25731 if (!it->f->fonts_changed \
25732 && (it->glyph_row->glyphs[area] \
25733 < it->glyph_row->glyphs[area + 1])) \
25734 { \
25735 it->w->ncols_scale_factor++; \
25736 it->f->fonts_changed = true; \
25737 } \
25738 }
25739
25740 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25741 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25742
25743 static void
25744 append_glyph (struct it *it)
25745 {
25746 struct glyph *glyph;
25747 enum glyph_row_area area = it->area;
25748
25749 eassert (it->glyph_row);
25750 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25751
25752 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25753 if (glyph < it->glyph_row->glyphs[area + 1])
25754 {
25755 /* If the glyph row is reversed, we need to prepend the glyph
25756 rather than append it. */
25757 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25758 {
25759 struct glyph *g;
25760
25761 /* Make room for the additional glyph. */
25762 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25763 g[1] = *g;
25764 glyph = it->glyph_row->glyphs[area];
25765 }
25766 glyph->charpos = CHARPOS (it->position);
25767 glyph->object = it->object;
25768 if (it->pixel_width > 0)
25769 {
25770 glyph->pixel_width = it->pixel_width;
25771 glyph->padding_p = false;
25772 }
25773 else
25774 {
25775 /* Assure at least 1-pixel width. Otherwise, cursor can't
25776 be displayed correctly. */
25777 glyph->pixel_width = 1;
25778 glyph->padding_p = true;
25779 }
25780 glyph->ascent = it->ascent;
25781 glyph->descent = it->descent;
25782 glyph->voffset = it->voffset;
25783 glyph->type = CHAR_GLYPH;
25784 glyph->avoid_cursor_p = it->avoid_cursor_p;
25785 glyph->multibyte_p = it->multibyte_p;
25786 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25787 {
25788 /* In R2L rows, the left and the right box edges need to be
25789 drawn in reverse direction. */
25790 glyph->right_box_line_p = it->start_of_box_run_p;
25791 glyph->left_box_line_p = it->end_of_box_run_p;
25792 }
25793 else
25794 {
25795 glyph->left_box_line_p = it->start_of_box_run_p;
25796 glyph->right_box_line_p = it->end_of_box_run_p;
25797 }
25798 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25799 || it->phys_descent > it->descent);
25800 glyph->glyph_not_available_p = it->glyph_not_available_p;
25801 glyph->face_id = it->face_id;
25802 glyph->u.ch = it->char_to_display;
25803 glyph->slice.img = null_glyph_slice;
25804 glyph->font_type = FONT_TYPE_UNKNOWN;
25805 if (it->bidi_p)
25806 {
25807 glyph->resolved_level = it->bidi_it.resolved_level;
25808 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25809 glyph->bidi_type = it->bidi_it.type;
25810 }
25811 else
25812 {
25813 glyph->resolved_level = 0;
25814 glyph->bidi_type = UNKNOWN_BT;
25815 }
25816 ++it->glyph_row->used[area];
25817 }
25818 else
25819 IT_EXPAND_MATRIX_WIDTH (it, area);
25820 }
25821
25822 /* Store one glyph for the composition IT->cmp_it.id in
25823 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25824 non-null. */
25825
25826 static void
25827 append_composite_glyph (struct it *it)
25828 {
25829 struct glyph *glyph;
25830 enum glyph_row_area area = it->area;
25831
25832 eassert (it->glyph_row);
25833
25834 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25835 if (glyph < it->glyph_row->glyphs[area + 1])
25836 {
25837 /* If the glyph row is reversed, we need to prepend the glyph
25838 rather than append it. */
25839 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25840 {
25841 struct glyph *g;
25842
25843 /* Make room for the new glyph. */
25844 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25845 g[1] = *g;
25846 glyph = it->glyph_row->glyphs[it->area];
25847 }
25848 glyph->charpos = it->cmp_it.charpos;
25849 glyph->object = it->object;
25850 glyph->pixel_width = it->pixel_width;
25851 glyph->ascent = it->ascent;
25852 glyph->descent = it->descent;
25853 glyph->voffset = it->voffset;
25854 glyph->type = COMPOSITE_GLYPH;
25855 if (it->cmp_it.ch < 0)
25856 {
25857 glyph->u.cmp.automatic = false;
25858 glyph->u.cmp.id = it->cmp_it.id;
25859 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25860 }
25861 else
25862 {
25863 glyph->u.cmp.automatic = true;
25864 glyph->u.cmp.id = it->cmp_it.id;
25865 glyph->slice.cmp.from = it->cmp_it.from;
25866 glyph->slice.cmp.to = it->cmp_it.to - 1;
25867 }
25868 glyph->avoid_cursor_p = it->avoid_cursor_p;
25869 glyph->multibyte_p = it->multibyte_p;
25870 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25871 {
25872 /* In R2L rows, the left and the right box edges need to be
25873 drawn in reverse direction. */
25874 glyph->right_box_line_p = it->start_of_box_run_p;
25875 glyph->left_box_line_p = it->end_of_box_run_p;
25876 }
25877 else
25878 {
25879 glyph->left_box_line_p = it->start_of_box_run_p;
25880 glyph->right_box_line_p = it->end_of_box_run_p;
25881 }
25882 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25883 || it->phys_descent > it->descent);
25884 glyph->padding_p = false;
25885 glyph->glyph_not_available_p = false;
25886 glyph->face_id = it->face_id;
25887 glyph->font_type = FONT_TYPE_UNKNOWN;
25888 if (it->bidi_p)
25889 {
25890 glyph->resolved_level = it->bidi_it.resolved_level;
25891 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25892 glyph->bidi_type = it->bidi_it.type;
25893 }
25894 ++it->glyph_row->used[area];
25895 }
25896 else
25897 IT_EXPAND_MATRIX_WIDTH (it, area);
25898 }
25899
25900
25901 /* Change IT->ascent and IT->height according to the setting of
25902 IT->voffset. */
25903
25904 static void
25905 take_vertical_position_into_account (struct it *it)
25906 {
25907 if (it->voffset)
25908 {
25909 if (it->voffset < 0)
25910 /* Increase the ascent so that we can display the text higher
25911 in the line. */
25912 it->ascent -= it->voffset;
25913 else
25914 /* Increase the descent so that we can display the text lower
25915 in the line. */
25916 it->descent += it->voffset;
25917 }
25918 }
25919
25920
25921 /* Produce glyphs/get display metrics for the image IT is loaded with.
25922 See the description of struct display_iterator in dispextern.h for
25923 an overview of struct display_iterator. */
25924
25925 static void
25926 produce_image_glyph (struct it *it)
25927 {
25928 struct image *img;
25929 struct face *face;
25930 int glyph_ascent, crop;
25931 struct glyph_slice slice;
25932
25933 eassert (it->what == IT_IMAGE);
25934
25935 face = FACE_FROM_ID (it->f, it->face_id);
25936 eassert (face);
25937 /* Make sure X resources of the face is loaded. */
25938 prepare_face_for_display (it->f, face);
25939
25940 if (it->image_id < 0)
25941 {
25942 /* Fringe bitmap. */
25943 it->ascent = it->phys_ascent = 0;
25944 it->descent = it->phys_descent = 0;
25945 it->pixel_width = 0;
25946 it->nglyphs = 0;
25947 return;
25948 }
25949
25950 img = IMAGE_FROM_ID (it->f, it->image_id);
25951 eassert (img);
25952 /* Make sure X resources of the image is loaded. */
25953 prepare_image_for_display (it->f, img);
25954
25955 slice.x = slice.y = 0;
25956 slice.width = img->width;
25957 slice.height = img->height;
25958
25959 if (INTEGERP (it->slice.x))
25960 slice.x = XINT (it->slice.x);
25961 else if (FLOATP (it->slice.x))
25962 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25963
25964 if (INTEGERP (it->slice.y))
25965 slice.y = XINT (it->slice.y);
25966 else if (FLOATP (it->slice.y))
25967 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25968
25969 if (INTEGERP (it->slice.width))
25970 slice.width = XINT (it->slice.width);
25971 else if (FLOATP (it->slice.width))
25972 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25973
25974 if (INTEGERP (it->slice.height))
25975 slice.height = XINT (it->slice.height);
25976 else if (FLOATP (it->slice.height))
25977 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25978
25979 if (slice.x >= img->width)
25980 slice.x = img->width;
25981 if (slice.y >= img->height)
25982 slice.y = img->height;
25983 if (slice.x + slice.width >= img->width)
25984 slice.width = img->width - slice.x;
25985 if (slice.y + slice.height > img->height)
25986 slice.height = img->height - slice.y;
25987
25988 if (slice.width == 0 || slice.height == 0)
25989 return;
25990
25991 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25992
25993 it->descent = slice.height - glyph_ascent;
25994 if (slice.y == 0)
25995 it->descent += img->vmargin;
25996 if (slice.y + slice.height == img->height)
25997 it->descent += img->vmargin;
25998 it->phys_descent = it->descent;
25999
26000 it->pixel_width = slice.width;
26001 if (slice.x == 0)
26002 it->pixel_width += img->hmargin;
26003 if (slice.x + slice.width == img->width)
26004 it->pixel_width += img->hmargin;
26005
26006 /* It's quite possible for images to have an ascent greater than
26007 their height, so don't get confused in that case. */
26008 if (it->descent < 0)
26009 it->descent = 0;
26010
26011 it->nglyphs = 1;
26012
26013 if (face->box != FACE_NO_BOX)
26014 {
26015 if (face->box_line_width > 0)
26016 {
26017 if (slice.y == 0)
26018 it->ascent += face->box_line_width;
26019 if (slice.y + slice.height == img->height)
26020 it->descent += face->box_line_width;
26021 }
26022
26023 if (it->start_of_box_run_p && slice.x == 0)
26024 it->pixel_width += eabs (face->box_line_width);
26025 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26026 it->pixel_width += eabs (face->box_line_width);
26027 }
26028
26029 take_vertical_position_into_account (it);
26030
26031 /* Automatically crop wide image glyphs at right edge so we can
26032 draw the cursor on same display row. */
26033 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26034 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26035 {
26036 it->pixel_width -= crop;
26037 slice.width -= crop;
26038 }
26039
26040 if (it->glyph_row)
26041 {
26042 struct glyph *glyph;
26043 enum glyph_row_area area = it->area;
26044
26045 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26046 if (it->glyph_row->reversed_p)
26047 {
26048 struct glyph *g;
26049
26050 /* Make room for the new glyph. */
26051 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26052 g[1] = *g;
26053 glyph = it->glyph_row->glyphs[it->area];
26054 }
26055 if (glyph < it->glyph_row->glyphs[area + 1])
26056 {
26057 glyph->charpos = CHARPOS (it->position);
26058 glyph->object = it->object;
26059 glyph->pixel_width = it->pixel_width;
26060 glyph->ascent = glyph_ascent;
26061 glyph->descent = it->descent;
26062 glyph->voffset = it->voffset;
26063 glyph->type = IMAGE_GLYPH;
26064 glyph->avoid_cursor_p = it->avoid_cursor_p;
26065 glyph->multibyte_p = it->multibyte_p;
26066 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26067 {
26068 /* In R2L rows, the left and the right box edges need to be
26069 drawn in reverse direction. */
26070 glyph->right_box_line_p = it->start_of_box_run_p;
26071 glyph->left_box_line_p = it->end_of_box_run_p;
26072 }
26073 else
26074 {
26075 glyph->left_box_line_p = it->start_of_box_run_p;
26076 glyph->right_box_line_p = it->end_of_box_run_p;
26077 }
26078 glyph->overlaps_vertically_p = false;
26079 glyph->padding_p = false;
26080 glyph->glyph_not_available_p = false;
26081 glyph->face_id = it->face_id;
26082 glyph->u.img_id = img->id;
26083 glyph->slice.img = slice;
26084 glyph->font_type = FONT_TYPE_UNKNOWN;
26085 if (it->bidi_p)
26086 {
26087 glyph->resolved_level = it->bidi_it.resolved_level;
26088 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26089 glyph->bidi_type = it->bidi_it.type;
26090 }
26091 ++it->glyph_row->used[area];
26092 }
26093 else
26094 IT_EXPAND_MATRIX_WIDTH (it, area);
26095 }
26096 }
26097
26098 static void
26099 produce_xwidget_glyph (struct it *it)
26100 {
26101 #ifdef HAVE_XWIDGETS
26102 struct xwidget *xw;
26103 int glyph_ascent, crop;
26104 eassert (it->what == IT_XWIDGET);
26105
26106 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26107 eassert (face);
26108 /* Make sure X resources of the face is loaded. */
26109 prepare_face_for_display (it->f, face);
26110
26111 xw = it->xwidget;
26112 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
26113 it->descent = xw->height/2;
26114 it->phys_descent = it->descent;
26115 it->pixel_width = xw->width;
26116 /* It's quite possible for images to have an ascent greater than
26117 their height, so don't get confused in that case. */
26118 if (it->descent < 0)
26119 it->descent = 0;
26120
26121 it->nglyphs = 1;
26122
26123 if (face->box != FACE_NO_BOX)
26124 {
26125 if (face->box_line_width > 0)
26126 {
26127 it->ascent += face->box_line_width;
26128 it->descent += face->box_line_width;
26129 }
26130
26131 if (it->start_of_box_run_p)
26132 it->pixel_width += eabs (face->box_line_width);
26133 it->pixel_width += eabs (face->box_line_width);
26134 }
26135
26136 take_vertical_position_into_account (it);
26137
26138 /* Automatically crop wide image glyphs at right edge so we can
26139 draw the cursor on same display row. */
26140 crop = it->pixel_width - (it->last_visible_x - it->current_x);
26141 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26142 it->pixel_width -= crop;
26143
26144 if (it->glyph_row)
26145 {
26146 enum glyph_row_area area = it->area;
26147 struct glyph *glyph
26148 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26149
26150 if (it->glyph_row->reversed_p)
26151 {
26152 struct glyph *g;
26153
26154 /* Make room for the new glyph. */
26155 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26156 g[1] = *g;
26157 glyph = it->glyph_row->glyphs[it->area];
26158 }
26159 if (glyph < it->glyph_row->glyphs[area + 1])
26160 {
26161 glyph->charpos = CHARPOS (it->position);
26162 glyph->object = it->object;
26163 glyph->pixel_width = it->pixel_width;
26164 glyph->ascent = glyph_ascent;
26165 glyph->descent = it->descent;
26166 glyph->voffset = it->voffset;
26167 glyph->type = XWIDGET_GLYPH;
26168 glyph->avoid_cursor_p = it->avoid_cursor_p;
26169 glyph->multibyte_p = it->multibyte_p;
26170 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26171 {
26172 /* In R2L rows, the left and the right box edges need to be
26173 drawn in reverse direction. */
26174 glyph->right_box_line_p = it->start_of_box_run_p;
26175 glyph->left_box_line_p = it->end_of_box_run_p;
26176 }
26177 else
26178 {
26179 glyph->left_box_line_p = it->start_of_box_run_p;
26180 glyph->right_box_line_p = it->end_of_box_run_p;
26181 }
26182 glyph->overlaps_vertically_p = 0;
26183 glyph->padding_p = 0;
26184 glyph->glyph_not_available_p = 0;
26185 glyph->face_id = it->face_id;
26186 glyph->u.xwidget = it->xwidget;
26187 glyph->font_type = FONT_TYPE_UNKNOWN;
26188 if (it->bidi_p)
26189 {
26190 glyph->resolved_level = it->bidi_it.resolved_level;
26191 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26192 glyph->bidi_type = it->bidi_it.type;
26193 }
26194 ++it->glyph_row->used[area];
26195 }
26196 else
26197 IT_EXPAND_MATRIX_WIDTH (it, area);
26198 }
26199 #endif
26200 }
26201
26202 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
26203 of the glyph, WIDTH and HEIGHT are the width and height of the
26204 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
26205
26206 static void
26207 append_stretch_glyph (struct it *it, Lisp_Object object,
26208 int width, int height, int ascent)
26209 {
26210 struct glyph *glyph;
26211 enum glyph_row_area area = it->area;
26212
26213 eassert (ascent >= 0 && ascent <= height);
26214
26215 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26216 if (glyph < it->glyph_row->glyphs[area + 1])
26217 {
26218 /* If the glyph row is reversed, we need to prepend the glyph
26219 rather than append it. */
26220 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26221 {
26222 struct glyph *g;
26223
26224 /* Make room for the additional glyph. */
26225 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26226 g[1] = *g;
26227 glyph = it->glyph_row->glyphs[area];
26228
26229 /* Decrease the width of the first glyph of the row that
26230 begins before first_visible_x (e.g., due to hscroll).
26231 This is so the overall width of the row becomes smaller
26232 by the scroll amount, and the stretch glyph appended by
26233 extend_face_to_end_of_line will be wider, to shift the
26234 row glyphs to the right. (In L2R rows, the corresponding
26235 left-shift effect is accomplished by setting row->x to a
26236 negative value, which won't work with R2L rows.)
26237
26238 This must leave us with a positive value of WIDTH, since
26239 otherwise the call to move_it_in_display_line_to at the
26240 beginning of display_line would have got past the entire
26241 first glyph, and then it->current_x would have been
26242 greater or equal to it->first_visible_x. */
26243 if (it->current_x < it->first_visible_x)
26244 width -= it->first_visible_x - it->current_x;
26245 eassert (width > 0);
26246 }
26247 glyph->charpos = CHARPOS (it->position);
26248 glyph->object = object;
26249 glyph->pixel_width = width;
26250 glyph->ascent = ascent;
26251 glyph->descent = height - ascent;
26252 glyph->voffset = it->voffset;
26253 glyph->type = STRETCH_GLYPH;
26254 glyph->avoid_cursor_p = it->avoid_cursor_p;
26255 glyph->multibyte_p = it->multibyte_p;
26256 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26257 {
26258 /* In R2L rows, the left and the right box edges need to be
26259 drawn in reverse direction. */
26260 glyph->right_box_line_p = it->start_of_box_run_p;
26261 glyph->left_box_line_p = it->end_of_box_run_p;
26262 }
26263 else
26264 {
26265 glyph->left_box_line_p = it->start_of_box_run_p;
26266 glyph->right_box_line_p = it->end_of_box_run_p;
26267 }
26268 glyph->overlaps_vertically_p = false;
26269 glyph->padding_p = false;
26270 glyph->glyph_not_available_p = false;
26271 glyph->face_id = it->face_id;
26272 glyph->u.stretch.ascent = ascent;
26273 glyph->u.stretch.height = height;
26274 glyph->slice.img = null_glyph_slice;
26275 glyph->font_type = FONT_TYPE_UNKNOWN;
26276 if (it->bidi_p)
26277 {
26278 glyph->resolved_level = it->bidi_it.resolved_level;
26279 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26280 glyph->bidi_type = it->bidi_it.type;
26281 }
26282 else
26283 {
26284 glyph->resolved_level = 0;
26285 glyph->bidi_type = UNKNOWN_BT;
26286 }
26287 ++it->glyph_row->used[area];
26288 }
26289 else
26290 IT_EXPAND_MATRIX_WIDTH (it, area);
26291 }
26292
26293 #endif /* HAVE_WINDOW_SYSTEM */
26294
26295 /* Produce a stretch glyph for iterator IT. IT->object is the value
26296 of the glyph property displayed. The value must be a list
26297 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
26298 being recognized:
26299
26300 1. `:width WIDTH' specifies that the space should be WIDTH *
26301 canonical char width wide. WIDTH may be an integer or floating
26302 point number.
26303
26304 2. `:relative-width FACTOR' specifies that the width of the stretch
26305 should be computed from the width of the first character having the
26306 `glyph' property, and should be FACTOR times that width.
26307
26308 3. `:align-to HPOS' specifies that the space should be wide enough
26309 to reach HPOS, a value in canonical character units.
26310
26311 Exactly one of the above pairs must be present.
26312
26313 4. `:height HEIGHT' specifies that the height of the stretch produced
26314 should be HEIGHT, measured in canonical character units.
26315
26316 5. `:relative-height FACTOR' specifies that the height of the
26317 stretch should be FACTOR times the height of the characters having
26318 the glyph property.
26319
26320 Either none or exactly one of 4 or 5 must be present.
26321
26322 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26323 of the stretch should be used for the ascent of the stretch.
26324 ASCENT must be in the range 0 <= ASCENT <= 100. */
26325
26326 void
26327 produce_stretch_glyph (struct it *it)
26328 {
26329 /* (space :width WIDTH :height HEIGHT ...) */
26330 Lisp_Object prop, plist;
26331 int width = 0, height = 0, align_to = -1;
26332 bool zero_width_ok_p = false;
26333 double tem;
26334 struct font *font = NULL;
26335
26336 #ifdef HAVE_WINDOW_SYSTEM
26337 int ascent = 0;
26338 bool zero_height_ok_p = false;
26339
26340 if (FRAME_WINDOW_P (it->f))
26341 {
26342 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26343 font = face->font ? face->font : FRAME_FONT (it->f);
26344 prepare_face_for_display (it->f, face);
26345 }
26346 #endif
26347
26348 /* List should start with `space'. */
26349 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26350 plist = XCDR (it->object);
26351
26352 /* Compute the width of the stretch. */
26353 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26354 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26355 {
26356 /* Absolute width `:width WIDTH' specified and valid. */
26357 zero_width_ok_p = true;
26358 width = (int)tem;
26359 }
26360 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26361 {
26362 /* Relative width `:relative-width FACTOR' specified and valid.
26363 Compute the width of the characters having the `glyph'
26364 property. */
26365 struct it it2;
26366 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26367
26368 it2 = *it;
26369 if (it->multibyte_p)
26370 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26371 else
26372 {
26373 it2.c = it2.char_to_display = *p, it2.len = 1;
26374 if (! ASCII_CHAR_P (it2.c))
26375 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26376 }
26377
26378 it2.glyph_row = NULL;
26379 it2.what = IT_CHARACTER;
26380 PRODUCE_GLYPHS (&it2);
26381 width = NUMVAL (prop) * it2.pixel_width;
26382 }
26383 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26384 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26385 &align_to))
26386 {
26387 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26388 align_to = (align_to < 0
26389 ? 0
26390 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26391 else if (align_to < 0)
26392 align_to = window_box_left_offset (it->w, TEXT_AREA);
26393 width = max (0, (int)tem + align_to - it->current_x);
26394 zero_width_ok_p = true;
26395 }
26396 else
26397 /* Nothing specified -> width defaults to canonical char width. */
26398 width = FRAME_COLUMN_WIDTH (it->f);
26399
26400 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26401 width = 1;
26402
26403 #ifdef HAVE_WINDOW_SYSTEM
26404 /* Compute height. */
26405 if (FRAME_WINDOW_P (it->f))
26406 {
26407 int default_height = normal_char_height (font, ' ');
26408
26409 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26410 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26411 {
26412 height = (int)tem;
26413 zero_height_ok_p = true;
26414 }
26415 else if (prop = Fplist_get (plist, QCrelative_height),
26416 NUMVAL (prop) > 0)
26417 height = default_height * NUMVAL (prop);
26418 else
26419 height = default_height;
26420
26421 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26422 height = 1;
26423
26424 /* Compute percentage of height used for ascent. If
26425 `:ascent ASCENT' is present and valid, use that. Otherwise,
26426 derive the ascent from the font in use. */
26427 if (prop = Fplist_get (plist, QCascent),
26428 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26429 ascent = height * NUMVAL (prop) / 100.0;
26430 else if (!NILP (prop)
26431 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26432 ascent = min (max (0, (int)tem), height);
26433 else
26434 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26435 }
26436 else
26437 #endif /* HAVE_WINDOW_SYSTEM */
26438 height = 1;
26439
26440 if (width > 0 && it->line_wrap != TRUNCATE
26441 && it->current_x + width > it->last_visible_x)
26442 {
26443 width = it->last_visible_x - it->current_x;
26444 #ifdef HAVE_WINDOW_SYSTEM
26445 /* Subtract one more pixel from the stretch width, but only on
26446 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26447 width -= FRAME_WINDOW_P (it->f);
26448 #endif
26449 }
26450
26451 if (width > 0 && height > 0 && it->glyph_row)
26452 {
26453 Lisp_Object o_object = it->object;
26454 Lisp_Object object = it->stack[it->sp - 1].string;
26455 int n = width;
26456
26457 if (!STRINGP (object))
26458 object = it->w->contents;
26459 #ifdef HAVE_WINDOW_SYSTEM
26460 if (FRAME_WINDOW_P (it->f))
26461 append_stretch_glyph (it, object, width, height, ascent);
26462 else
26463 #endif
26464 {
26465 it->object = object;
26466 it->char_to_display = ' ';
26467 it->pixel_width = it->len = 1;
26468 while (n--)
26469 tty_append_glyph (it);
26470 it->object = o_object;
26471 }
26472 }
26473
26474 it->pixel_width = width;
26475 #ifdef HAVE_WINDOW_SYSTEM
26476 if (FRAME_WINDOW_P (it->f))
26477 {
26478 it->ascent = it->phys_ascent = ascent;
26479 it->descent = it->phys_descent = height - it->ascent;
26480 it->nglyphs = width > 0 && height > 0;
26481 take_vertical_position_into_account (it);
26482 }
26483 else
26484 #endif
26485 it->nglyphs = width;
26486 }
26487
26488 /* Get information about special display element WHAT in an
26489 environment described by IT. WHAT is one of IT_TRUNCATION or
26490 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26491 non-null glyph_row member. This function ensures that fields like
26492 face_id, c, len of IT are left untouched. */
26493
26494 static void
26495 produce_special_glyphs (struct it *it, enum display_element_type what)
26496 {
26497 struct it temp_it;
26498 Lisp_Object gc;
26499 GLYPH glyph;
26500
26501 temp_it = *it;
26502 temp_it.object = Qnil;
26503 memset (&temp_it.current, 0, sizeof temp_it.current);
26504
26505 if (what == IT_CONTINUATION)
26506 {
26507 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26508 if (it->bidi_it.paragraph_dir == R2L)
26509 SET_GLYPH_FROM_CHAR (glyph, '/');
26510 else
26511 SET_GLYPH_FROM_CHAR (glyph, '\\');
26512 if (it->dp
26513 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26514 {
26515 /* FIXME: Should we mirror GC for R2L lines? */
26516 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26517 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26518 }
26519 }
26520 else if (what == IT_TRUNCATION)
26521 {
26522 /* Truncation glyph. */
26523 SET_GLYPH_FROM_CHAR (glyph, '$');
26524 if (it->dp
26525 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26526 {
26527 /* FIXME: Should we mirror GC for R2L lines? */
26528 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26529 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26530 }
26531 }
26532 else
26533 emacs_abort ();
26534
26535 #ifdef HAVE_WINDOW_SYSTEM
26536 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26537 is turned off, we precede the truncation/continuation glyphs by a
26538 stretch glyph whose width is computed such that these special
26539 glyphs are aligned at the window margin, even when very different
26540 fonts are used in different glyph rows. */
26541 if (FRAME_WINDOW_P (temp_it.f)
26542 /* init_iterator calls this with it->glyph_row == NULL, and it
26543 wants only the pixel width of the truncation/continuation
26544 glyphs. */
26545 && temp_it.glyph_row
26546 /* insert_left_trunc_glyphs calls us at the beginning of the
26547 row, and it has its own calculation of the stretch glyph
26548 width. */
26549 && temp_it.glyph_row->used[TEXT_AREA] > 0
26550 && (temp_it.glyph_row->reversed_p
26551 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26552 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26553 {
26554 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26555
26556 if (stretch_width > 0)
26557 {
26558 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26559 struct font *font =
26560 face->font ? face->font : FRAME_FONT (temp_it.f);
26561 int stretch_ascent =
26562 (((temp_it.ascent + temp_it.descent)
26563 * FONT_BASE (font)) / FONT_HEIGHT (font));
26564
26565 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26566 temp_it.ascent + temp_it.descent,
26567 stretch_ascent);
26568 }
26569 }
26570 #endif
26571
26572 temp_it.dp = NULL;
26573 temp_it.what = IT_CHARACTER;
26574 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26575 temp_it.face_id = GLYPH_FACE (glyph);
26576 temp_it.len = CHAR_BYTES (temp_it.c);
26577
26578 PRODUCE_GLYPHS (&temp_it);
26579 it->pixel_width = temp_it.pixel_width;
26580 it->nglyphs = temp_it.nglyphs;
26581 }
26582
26583 #ifdef HAVE_WINDOW_SYSTEM
26584
26585 /* Calculate line-height and line-spacing properties.
26586 An integer value specifies explicit pixel value.
26587 A float value specifies relative value to current face height.
26588 A cons (float . face-name) specifies relative value to
26589 height of specified face font.
26590
26591 Returns height in pixels, or nil. */
26592
26593 static Lisp_Object
26594 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26595 int boff, bool override)
26596 {
26597 Lisp_Object face_name = Qnil;
26598 int ascent, descent, height;
26599
26600 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26601 return val;
26602
26603 if (CONSP (val))
26604 {
26605 face_name = XCAR (val);
26606 val = XCDR (val);
26607 if (!NUMBERP (val))
26608 val = make_number (1);
26609 if (NILP (face_name))
26610 {
26611 height = it->ascent + it->descent;
26612 goto scale;
26613 }
26614 }
26615
26616 if (NILP (face_name))
26617 {
26618 font = FRAME_FONT (it->f);
26619 boff = FRAME_BASELINE_OFFSET (it->f);
26620 }
26621 else if (EQ (face_name, Qt))
26622 {
26623 override = false;
26624 }
26625 else
26626 {
26627 int face_id;
26628 struct face *face;
26629
26630 face_id = lookup_named_face (it->f, face_name, false);
26631 if (face_id < 0)
26632 return make_number (-1);
26633
26634 face = FACE_FROM_ID (it->f, face_id);
26635 font = face->font;
26636 if (font == NULL)
26637 return make_number (-1);
26638 boff = font->baseline_offset;
26639 if (font->vertical_centering)
26640 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26641 }
26642
26643 normal_char_ascent_descent (font, -1, &ascent, &descent);
26644
26645 if (override)
26646 {
26647 it->override_ascent = ascent;
26648 it->override_descent = descent;
26649 it->override_boff = boff;
26650 }
26651
26652 height = ascent + descent;
26653
26654 scale:
26655 if (FLOATP (val))
26656 height = (int)(XFLOAT_DATA (val) * height);
26657 else if (INTEGERP (val))
26658 height *= XINT (val);
26659
26660 return make_number (height);
26661 }
26662
26663
26664 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26665 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26666 and only if this is for a character for which no font was found.
26667
26668 If the display method (it->glyphless_method) is
26669 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26670 length of the acronym or the hexadecimal string, UPPER_XOFF and
26671 UPPER_YOFF are pixel offsets for the upper part of the string,
26672 LOWER_XOFF and LOWER_YOFF are for the lower part.
26673
26674 For the other display methods, LEN through LOWER_YOFF are zero. */
26675
26676 static void
26677 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26678 short upper_xoff, short upper_yoff,
26679 short lower_xoff, short lower_yoff)
26680 {
26681 struct glyph *glyph;
26682 enum glyph_row_area area = it->area;
26683
26684 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26685 if (glyph < it->glyph_row->glyphs[area + 1])
26686 {
26687 /* If the glyph row is reversed, we need to prepend the glyph
26688 rather than append it. */
26689 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26690 {
26691 struct glyph *g;
26692
26693 /* Make room for the additional glyph. */
26694 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26695 g[1] = *g;
26696 glyph = it->glyph_row->glyphs[area];
26697 }
26698 glyph->charpos = CHARPOS (it->position);
26699 glyph->object = it->object;
26700 glyph->pixel_width = it->pixel_width;
26701 glyph->ascent = it->ascent;
26702 glyph->descent = it->descent;
26703 glyph->voffset = it->voffset;
26704 glyph->type = GLYPHLESS_GLYPH;
26705 glyph->u.glyphless.method = it->glyphless_method;
26706 glyph->u.glyphless.for_no_font = for_no_font;
26707 glyph->u.glyphless.len = len;
26708 glyph->u.glyphless.ch = it->c;
26709 glyph->slice.glyphless.upper_xoff = upper_xoff;
26710 glyph->slice.glyphless.upper_yoff = upper_yoff;
26711 glyph->slice.glyphless.lower_xoff = lower_xoff;
26712 glyph->slice.glyphless.lower_yoff = lower_yoff;
26713 glyph->avoid_cursor_p = it->avoid_cursor_p;
26714 glyph->multibyte_p = it->multibyte_p;
26715 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26716 {
26717 /* In R2L rows, the left and the right box edges need to be
26718 drawn in reverse direction. */
26719 glyph->right_box_line_p = it->start_of_box_run_p;
26720 glyph->left_box_line_p = it->end_of_box_run_p;
26721 }
26722 else
26723 {
26724 glyph->left_box_line_p = it->start_of_box_run_p;
26725 glyph->right_box_line_p = it->end_of_box_run_p;
26726 }
26727 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26728 || it->phys_descent > it->descent);
26729 glyph->padding_p = false;
26730 glyph->glyph_not_available_p = false;
26731 glyph->face_id = face_id;
26732 glyph->font_type = FONT_TYPE_UNKNOWN;
26733 if (it->bidi_p)
26734 {
26735 glyph->resolved_level = it->bidi_it.resolved_level;
26736 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26737 glyph->bidi_type = it->bidi_it.type;
26738 }
26739 ++it->glyph_row->used[area];
26740 }
26741 else
26742 IT_EXPAND_MATRIX_WIDTH (it, area);
26743 }
26744
26745
26746 /* Produce a glyph for a glyphless character for iterator IT.
26747 IT->glyphless_method specifies which method to use for displaying
26748 the character. See the description of enum
26749 glyphless_display_method in dispextern.h for the detail.
26750
26751 FOR_NO_FONT is true if and only if this is for a character for
26752 which no font was found. ACRONYM, if non-nil, is an acronym string
26753 for the character. */
26754
26755 static void
26756 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26757 {
26758 int face_id;
26759 struct face *face;
26760 struct font *font;
26761 int base_width, base_height, width, height;
26762 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26763 int len;
26764
26765 /* Get the metrics of the base font. We always refer to the current
26766 ASCII face. */
26767 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26768 font = face->font ? face->font : FRAME_FONT (it->f);
26769 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
26770 it->ascent += font->baseline_offset;
26771 it->descent -= font->baseline_offset;
26772 base_height = it->ascent + it->descent;
26773 base_width = font->average_width;
26774
26775 face_id = merge_glyphless_glyph_face (it);
26776
26777 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26778 {
26779 it->pixel_width = THIN_SPACE_WIDTH;
26780 len = 0;
26781 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26782 }
26783 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26784 {
26785 width = CHAR_WIDTH (it->c);
26786 if (width == 0)
26787 width = 1;
26788 else if (width > 4)
26789 width = 4;
26790 it->pixel_width = base_width * width;
26791 len = 0;
26792 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26793 }
26794 else
26795 {
26796 char buf[7];
26797 const char *str;
26798 unsigned int code[6];
26799 int upper_len;
26800 int ascent, descent;
26801 struct font_metrics metrics_upper, metrics_lower;
26802
26803 face = FACE_FROM_ID (it->f, face_id);
26804 font = face->font ? face->font : FRAME_FONT (it->f);
26805 prepare_face_for_display (it->f, face);
26806
26807 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26808 {
26809 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26810 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26811 if (CONSP (acronym))
26812 acronym = XCAR (acronym);
26813 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26814 }
26815 else
26816 {
26817 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26818 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
26819 str = buf;
26820 }
26821 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26822 code[len] = font->driver->encode_char (font, str[len]);
26823 upper_len = (len + 1) / 2;
26824 font->driver->text_extents (font, code, upper_len,
26825 &metrics_upper);
26826 font->driver->text_extents (font, code + upper_len, len - upper_len,
26827 &metrics_lower);
26828
26829
26830
26831 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26832 width = max (metrics_upper.width, metrics_lower.width) + 4;
26833 upper_xoff = upper_yoff = 2; /* the typical case */
26834 if (base_width >= width)
26835 {
26836 /* Align the upper to the left, the lower to the right. */
26837 it->pixel_width = base_width;
26838 lower_xoff = base_width - 2 - metrics_lower.width;
26839 }
26840 else
26841 {
26842 /* Center the shorter one. */
26843 it->pixel_width = width;
26844 if (metrics_upper.width >= metrics_lower.width)
26845 lower_xoff = (width - metrics_lower.width) / 2;
26846 else
26847 {
26848 /* FIXME: This code doesn't look right. It formerly was
26849 missing the "lower_xoff = 0;", which couldn't have
26850 been right since it left lower_xoff uninitialized. */
26851 lower_xoff = 0;
26852 upper_xoff = (width - metrics_upper.width) / 2;
26853 }
26854 }
26855
26856 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26857 top, bottom, and between upper and lower strings. */
26858 height = (metrics_upper.ascent + metrics_upper.descent
26859 + metrics_lower.ascent + metrics_lower.descent) + 5;
26860 /* Center vertically.
26861 H:base_height, D:base_descent
26862 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26863
26864 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26865 descent = D - H/2 + h/2;
26866 lower_yoff = descent - 2 - ld;
26867 upper_yoff = lower_yoff - la - 1 - ud; */
26868 ascent = - (it->descent - (base_height + height + 1) / 2);
26869 descent = it->descent - (base_height - height) / 2;
26870 lower_yoff = descent - 2 - metrics_lower.descent;
26871 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26872 - metrics_upper.descent);
26873 /* Don't make the height shorter than the base height. */
26874 if (height > base_height)
26875 {
26876 it->ascent = ascent;
26877 it->descent = descent;
26878 }
26879 }
26880
26881 it->phys_ascent = it->ascent;
26882 it->phys_descent = it->descent;
26883 if (it->glyph_row)
26884 append_glyphless_glyph (it, face_id, for_no_font, len,
26885 upper_xoff, upper_yoff,
26886 lower_xoff, lower_yoff);
26887 it->nglyphs = 1;
26888 take_vertical_position_into_account (it);
26889 }
26890
26891
26892 /* RIF:
26893 Produce glyphs/get display metrics for the display element IT is
26894 loaded with. See the description of struct it in dispextern.h
26895 for an overview of struct it. */
26896
26897 void
26898 x_produce_glyphs (struct it *it)
26899 {
26900 int extra_line_spacing = it->extra_line_spacing;
26901
26902 it->glyph_not_available_p = false;
26903
26904 if (it->what == IT_CHARACTER)
26905 {
26906 XChar2b char2b;
26907 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26908 struct font *font = face->font;
26909 struct font_metrics *pcm = NULL;
26910 int boff; /* Baseline offset. */
26911
26912 if (font == NULL)
26913 {
26914 /* When no suitable font is found, display this character by
26915 the method specified in the first extra slot of
26916 Vglyphless_char_display. */
26917 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26918
26919 eassert (it->what == IT_GLYPHLESS);
26920 produce_glyphless_glyph (it, true,
26921 STRINGP (acronym) ? acronym : Qnil);
26922 goto done;
26923 }
26924
26925 boff = font->baseline_offset;
26926 if (font->vertical_centering)
26927 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26928
26929 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26930 {
26931 it->nglyphs = 1;
26932
26933 if (it->override_ascent >= 0)
26934 {
26935 it->ascent = it->override_ascent;
26936 it->descent = it->override_descent;
26937 boff = it->override_boff;
26938 }
26939 else
26940 {
26941 it->ascent = FONT_BASE (font) + boff;
26942 it->descent = FONT_DESCENT (font) - boff;
26943 }
26944
26945 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26946 {
26947 pcm = get_per_char_metric (font, &char2b);
26948 if (pcm->width == 0
26949 && pcm->rbearing == 0 && pcm->lbearing == 0)
26950 pcm = NULL;
26951 }
26952
26953 if (pcm)
26954 {
26955 it->phys_ascent = pcm->ascent + boff;
26956 it->phys_descent = pcm->descent - boff;
26957 it->pixel_width = pcm->width;
26958 /* Don't use font-global values for ascent and descent
26959 if they result in an exceedingly large line height. */
26960 if (it->override_ascent < 0)
26961 {
26962 if (FONT_TOO_HIGH (font))
26963 {
26964 it->ascent = it->phys_ascent;
26965 it->descent = it->phys_descent;
26966 /* These limitations are enforced by an
26967 assertion near the end of this function. */
26968 if (it->ascent < 0)
26969 it->ascent = 0;
26970 if (it->descent < 0)
26971 it->descent = 0;
26972 }
26973 }
26974 }
26975 else
26976 {
26977 it->glyph_not_available_p = true;
26978 it->phys_ascent = it->ascent;
26979 it->phys_descent = it->descent;
26980 it->pixel_width = font->space_width;
26981 }
26982
26983 if (it->constrain_row_ascent_descent_p)
26984 {
26985 if (it->descent > it->max_descent)
26986 {
26987 it->ascent += it->descent - it->max_descent;
26988 it->descent = it->max_descent;
26989 }
26990 if (it->ascent > it->max_ascent)
26991 {
26992 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26993 it->ascent = it->max_ascent;
26994 }
26995 it->phys_ascent = min (it->phys_ascent, it->ascent);
26996 it->phys_descent = min (it->phys_descent, it->descent);
26997 extra_line_spacing = 0;
26998 }
26999
27000 /* If this is a space inside a region of text with
27001 `space-width' property, change its width. */
27002 bool stretched_p
27003 = it->char_to_display == ' ' && !NILP (it->space_width);
27004 if (stretched_p)
27005 it->pixel_width *= XFLOATINT (it->space_width);
27006
27007 /* If face has a box, add the box thickness to the character
27008 height. If character has a box line to the left and/or
27009 right, add the box line width to the character's width. */
27010 if (face->box != FACE_NO_BOX)
27011 {
27012 int thick = face->box_line_width;
27013
27014 if (thick > 0)
27015 {
27016 it->ascent += thick;
27017 it->descent += thick;
27018 }
27019 else
27020 thick = -thick;
27021
27022 if (it->start_of_box_run_p)
27023 it->pixel_width += thick;
27024 if (it->end_of_box_run_p)
27025 it->pixel_width += thick;
27026 }
27027
27028 /* If face has an overline, add the height of the overline
27029 (1 pixel) and a 1 pixel margin to the character height. */
27030 if (face->overline_p)
27031 it->ascent += overline_margin;
27032
27033 if (it->constrain_row_ascent_descent_p)
27034 {
27035 if (it->ascent > it->max_ascent)
27036 it->ascent = it->max_ascent;
27037 if (it->descent > it->max_descent)
27038 it->descent = it->max_descent;
27039 }
27040
27041 take_vertical_position_into_account (it);
27042
27043 /* If we have to actually produce glyphs, do it. */
27044 if (it->glyph_row)
27045 {
27046 if (stretched_p)
27047 {
27048 /* Translate a space with a `space-width' property
27049 into a stretch glyph. */
27050 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27051 / FONT_HEIGHT (font));
27052 append_stretch_glyph (it, it->object, it->pixel_width,
27053 it->ascent + it->descent, ascent);
27054 }
27055 else
27056 append_glyph (it);
27057
27058 /* If characters with lbearing or rbearing are displayed
27059 in this line, record that fact in a flag of the
27060 glyph row. This is used to optimize X output code. */
27061 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27062 it->glyph_row->contains_overlapping_glyphs_p = true;
27063 }
27064 if (! stretched_p && it->pixel_width == 0)
27065 /* We assure that all visible glyphs have at least 1-pixel
27066 width. */
27067 it->pixel_width = 1;
27068 }
27069 else if (it->char_to_display == '\n')
27070 {
27071 /* A newline has no width, but we need the height of the
27072 line. But if previous part of the line sets a height,
27073 don't increase that height. */
27074
27075 Lisp_Object height;
27076 Lisp_Object total_height = Qnil;
27077
27078 it->override_ascent = -1;
27079 it->pixel_width = 0;
27080 it->nglyphs = 0;
27081
27082 height = get_it_property (it, Qline_height);
27083 /* Split (line-height total-height) list. */
27084 if (CONSP (height)
27085 && CONSP (XCDR (height))
27086 && NILP (XCDR (XCDR (height))))
27087 {
27088 total_height = XCAR (XCDR (height));
27089 height = XCAR (height);
27090 }
27091 height = calc_line_height_property (it, height, font, boff, true);
27092
27093 if (it->override_ascent >= 0)
27094 {
27095 it->ascent = it->override_ascent;
27096 it->descent = it->override_descent;
27097 boff = it->override_boff;
27098 }
27099 else
27100 {
27101 if (FONT_TOO_HIGH (font))
27102 {
27103 it->ascent = font->pixel_size + boff - 1;
27104 it->descent = -boff + 1;
27105 if (it->descent < 0)
27106 it->descent = 0;
27107 }
27108 else
27109 {
27110 it->ascent = FONT_BASE (font) + boff;
27111 it->descent = FONT_DESCENT (font) - boff;
27112 }
27113 }
27114
27115 if (EQ (height, Qt))
27116 {
27117 if (it->descent > it->max_descent)
27118 {
27119 it->ascent += it->descent - it->max_descent;
27120 it->descent = it->max_descent;
27121 }
27122 if (it->ascent > it->max_ascent)
27123 {
27124 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27125 it->ascent = it->max_ascent;
27126 }
27127 it->phys_ascent = min (it->phys_ascent, it->ascent);
27128 it->phys_descent = min (it->phys_descent, it->descent);
27129 it->constrain_row_ascent_descent_p = true;
27130 extra_line_spacing = 0;
27131 }
27132 else
27133 {
27134 Lisp_Object spacing;
27135
27136 it->phys_ascent = it->ascent;
27137 it->phys_descent = it->descent;
27138
27139 if ((it->max_ascent > 0 || it->max_descent > 0)
27140 && face->box != FACE_NO_BOX
27141 && face->box_line_width > 0)
27142 {
27143 it->ascent += face->box_line_width;
27144 it->descent += face->box_line_width;
27145 }
27146 if (!NILP (height)
27147 && XINT (height) > it->ascent + it->descent)
27148 it->ascent = XINT (height) - it->descent;
27149
27150 if (!NILP (total_height))
27151 spacing = calc_line_height_property (it, total_height, font,
27152 boff, false);
27153 else
27154 {
27155 spacing = get_it_property (it, Qline_spacing);
27156 spacing = calc_line_height_property (it, spacing, font,
27157 boff, false);
27158 }
27159 if (INTEGERP (spacing))
27160 {
27161 extra_line_spacing = XINT (spacing);
27162 if (!NILP (total_height))
27163 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
27164 }
27165 }
27166 }
27167 else /* i.e. (it->char_to_display == '\t') */
27168 {
27169 if (font->space_width > 0)
27170 {
27171 int tab_width = it->tab_width * font->space_width;
27172 int x = it->current_x + it->continuation_lines_width;
27173 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
27174
27175 /* If the distance from the current position to the next tab
27176 stop is less than a space character width, use the
27177 tab stop after that. */
27178 if (next_tab_x - x < font->space_width)
27179 next_tab_x += tab_width;
27180
27181 it->pixel_width = next_tab_x - x;
27182 it->nglyphs = 1;
27183 if (FONT_TOO_HIGH (font))
27184 {
27185 if (get_char_glyph_code (' ', font, &char2b))
27186 {
27187 pcm = get_per_char_metric (font, &char2b);
27188 if (pcm->width == 0
27189 && pcm->rbearing == 0 && pcm->lbearing == 0)
27190 pcm = NULL;
27191 }
27192
27193 if (pcm)
27194 {
27195 it->ascent = pcm->ascent + boff;
27196 it->descent = pcm->descent - boff;
27197 }
27198 else
27199 {
27200 it->ascent = font->pixel_size + boff - 1;
27201 it->descent = -boff + 1;
27202 }
27203 if (it->ascent < 0)
27204 it->ascent = 0;
27205 if (it->descent < 0)
27206 it->descent = 0;
27207 }
27208 else
27209 {
27210 it->ascent = FONT_BASE (font) + boff;
27211 it->descent = FONT_DESCENT (font) - boff;
27212 }
27213 it->phys_ascent = it->ascent;
27214 it->phys_descent = it->descent;
27215
27216 if (it->glyph_row)
27217 {
27218 append_stretch_glyph (it, it->object, it->pixel_width,
27219 it->ascent + it->descent, it->ascent);
27220 }
27221 }
27222 else
27223 {
27224 it->pixel_width = 0;
27225 it->nglyphs = 1;
27226 }
27227 }
27228
27229 if (FONT_TOO_HIGH (font))
27230 {
27231 int font_ascent, font_descent;
27232
27233 /* For very large fonts, where we ignore the declared font
27234 dimensions, and go by per-character metrics instead,
27235 don't let the row ascent and descent values (and the row
27236 height computed from them) be smaller than the "normal"
27237 character metrics. This avoids unpleasant effects
27238 whereby lines on display would change their height
27239 depending on which characters are shown. */
27240 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27241 it->max_ascent = max (it->max_ascent, font_ascent);
27242 it->max_descent = max (it->max_descent, font_descent);
27243 }
27244 }
27245 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
27246 {
27247 /* A static composition.
27248
27249 Note: A composition is represented as one glyph in the
27250 glyph matrix. There are no padding glyphs.
27251
27252 Important note: pixel_width, ascent, and descent are the
27253 values of what is drawn by draw_glyphs (i.e. the values of
27254 the overall glyphs composed). */
27255 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27256 int boff; /* baseline offset */
27257 struct composition *cmp = composition_table[it->cmp_it.id];
27258 int glyph_len = cmp->glyph_len;
27259 struct font *font = face->font;
27260
27261 it->nglyphs = 1;
27262
27263 /* If we have not yet calculated pixel size data of glyphs of
27264 the composition for the current face font, calculate them
27265 now. Theoretically, we have to check all fonts for the
27266 glyphs, but that requires much time and memory space. So,
27267 here we check only the font of the first glyph. This may
27268 lead to incorrect display, but it's very rare, and C-l
27269 (recenter-top-bottom) can correct the display anyway. */
27270 if (! cmp->font || cmp->font != font)
27271 {
27272 /* Ascent and descent of the font of the first character
27273 of this composition (adjusted by baseline offset).
27274 Ascent and descent of overall glyphs should not be less
27275 than these, respectively. */
27276 int font_ascent, font_descent, font_height;
27277 /* Bounding box of the overall glyphs. */
27278 int leftmost, rightmost, lowest, highest;
27279 int lbearing, rbearing;
27280 int i, width, ascent, descent;
27281 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
27282 XChar2b char2b;
27283 struct font_metrics *pcm;
27284 ptrdiff_t pos;
27285
27286 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
27287 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
27288 break;
27289 bool right_padded = glyph_len < cmp->glyph_len;
27290 for (i = 0; i < glyph_len; i++)
27291 {
27292 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
27293 break;
27294 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27295 }
27296 bool left_padded = i > 0;
27297
27298 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
27299 : IT_CHARPOS (*it));
27300 /* If no suitable font is found, use the default font. */
27301 bool font_not_found_p = font == NULL;
27302 if (font_not_found_p)
27303 {
27304 face = face->ascii_face;
27305 font = face->font;
27306 }
27307 boff = font->baseline_offset;
27308 if (font->vertical_centering)
27309 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27310 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27311 font_ascent += boff;
27312 font_descent -= boff;
27313 font_height = font_ascent + font_descent;
27314
27315 cmp->font = font;
27316
27317 pcm = NULL;
27318 if (! font_not_found_p)
27319 {
27320 get_char_face_and_encoding (it->f, c, it->face_id,
27321 &char2b, false);
27322 pcm = get_per_char_metric (font, &char2b);
27323 }
27324
27325 /* Initialize the bounding box. */
27326 if (pcm)
27327 {
27328 width = cmp->glyph_len > 0 ? pcm->width : 0;
27329 ascent = pcm->ascent;
27330 descent = pcm->descent;
27331 lbearing = pcm->lbearing;
27332 rbearing = pcm->rbearing;
27333 }
27334 else
27335 {
27336 width = cmp->glyph_len > 0 ? font->space_width : 0;
27337 ascent = FONT_BASE (font);
27338 descent = FONT_DESCENT (font);
27339 lbearing = 0;
27340 rbearing = width;
27341 }
27342
27343 rightmost = width;
27344 leftmost = 0;
27345 lowest = - descent + boff;
27346 highest = ascent + boff;
27347
27348 if (! font_not_found_p
27349 && font->default_ascent
27350 && CHAR_TABLE_P (Vuse_default_ascent)
27351 && !NILP (Faref (Vuse_default_ascent,
27352 make_number (it->char_to_display))))
27353 highest = font->default_ascent + boff;
27354
27355 /* Draw the first glyph at the normal position. It may be
27356 shifted to right later if some other glyphs are drawn
27357 at the left. */
27358 cmp->offsets[i * 2] = 0;
27359 cmp->offsets[i * 2 + 1] = boff;
27360 cmp->lbearing = lbearing;
27361 cmp->rbearing = rbearing;
27362
27363 /* Set cmp->offsets for the remaining glyphs. */
27364 for (i++; i < glyph_len; i++)
27365 {
27366 int left, right, btm, top;
27367 int ch = COMPOSITION_GLYPH (cmp, i);
27368 int face_id;
27369 struct face *this_face;
27370
27371 if (ch == '\t')
27372 ch = ' ';
27373 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27374 this_face = FACE_FROM_ID (it->f, face_id);
27375 font = this_face->font;
27376
27377 if (font == NULL)
27378 pcm = NULL;
27379 else
27380 {
27381 get_char_face_and_encoding (it->f, ch, face_id,
27382 &char2b, false);
27383 pcm = get_per_char_metric (font, &char2b);
27384 }
27385 if (! pcm)
27386 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27387 else
27388 {
27389 width = pcm->width;
27390 ascent = pcm->ascent;
27391 descent = pcm->descent;
27392 lbearing = pcm->lbearing;
27393 rbearing = pcm->rbearing;
27394 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27395 {
27396 /* Relative composition with or without
27397 alternate chars. */
27398 left = (leftmost + rightmost - width) / 2;
27399 btm = - descent + boff;
27400 if (font->relative_compose
27401 && (! CHAR_TABLE_P (Vignore_relative_composition)
27402 || NILP (Faref (Vignore_relative_composition,
27403 make_number (ch)))))
27404 {
27405
27406 if (- descent >= font->relative_compose)
27407 /* One extra pixel between two glyphs. */
27408 btm = highest + 1;
27409 else if (ascent <= 0)
27410 /* One extra pixel between two glyphs. */
27411 btm = lowest - 1 - ascent - descent;
27412 }
27413 }
27414 else
27415 {
27416 /* A composition rule is specified by an integer
27417 value that encodes global and new reference
27418 points (GREF and NREF). GREF and NREF are
27419 specified by numbers as below:
27420
27421 0---1---2 -- ascent
27422 | |
27423 | |
27424 | |
27425 9--10--11 -- center
27426 | |
27427 ---3---4---5--- baseline
27428 | |
27429 6---7---8 -- descent
27430 */
27431 int rule = COMPOSITION_RULE (cmp, i);
27432 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27433
27434 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27435 grefx = gref % 3, nrefx = nref % 3;
27436 grefy = gref / 3, nrefy = nref / 3;
27437 if (xoff)
27438 xoff = font_height * (xoff - 128) / 256;
27439 if (yoff)
27440 yoff = font_height * (yoff - 128) / 256;
27441
27442 left = (leftmost
27443 + grefx * (rightmost - leftmost) / 2
27444 - nrefx * width / 2
27445 + xoff);
27446
27447 btm = ((grefy == 0 ? highest
27448 : grefy == 1 ? 0
27449 : grefy == 2 ? lowest
27450 : (highest + lowest) / 2)
27451 - (nrefy == 0 ? ascent + descent
27452 : nrefy == 1 ? descent - boff
27453 : nrefy == 2 ? 0
27454 : (ascent + descent) / 2)
27455 + yoff);
27456 }
27457
27458 cmp->offsets[i * 2] = left;
27459 cmp->offsets[i * 2 + 1] = btm + descent;
27460
27461 /* Update the bounding box of the overall glyphs. */
27462 if (width > 0)
27463 {
27464 right = left + width;
27465 if (left < leftmost)
27466 leftmost = left;
27467 if (right > rightmost)
27468 rightmost = right;
27469 }
27470 top = btm + descent + ascent;
27471 if (top > highest)
27472 highest = top;
27473 if (btm < lowest)
27474 lowest = btm;
27475
27476 if (cmp->lbearing > left + lbearing)
27477 cmp->lbearing = left + lbearing;
27478 if (cmp->rbearing < left + rbearing)
27479 cmp->rbearing = left + rbearing;
27480 }
27481 }
27482
27483 /* If there are glyphs whose x-offsets are negative,
27484 shift all glyphs to the right and make all x-offsets
27485 non-negative. */
27486 if (leftmost < 0)
27487 {
27488 for (i = 0; i < cmp->glyph_len; i++)
27489 cmp->offsets[i * 2] -= leftmost;
27490 rightmost -= leftmost;
27491 cmp->lbearing -= leftmost;
27492 cmp->rbearing -= leftmost;
27493 }
27494
27495 if (left_padded && cmp->lbearing < 0)
27496 {
27497 for (i = 0; i < cmp->glyph_len; i++)
27498 cmp->offsets[i * 2] -= cmp->lbearing;
27499 rightmost -= cmp->lbearing;
27500 cmp->rbearing -= cmp->lbearing;
27501 cmp->lbearing = 0;
27502 }
27503 if (right_padded && rightmost < cmp->rbearing)
27504 {
27505 rightmost = cmp->rbearing;
27506 }
27507
27508 cmp->pixel_width = rightmost;
27509 cmp->ascent = highest;
27510 cmp->descent = - lowest;
27511 if (cmp->ascent < font_ascent)
27512 cmp->ascent = font_ascent;
27513 if (cmp->descent < font_descent)
27514 cmp->descent = font_descent;
27515 }
27516
27517 if (it->glyph_row
27518 && (cmp->lbearing < 0
27519 || cmp->rbearing > cmp->pixel_width))
27520 it->glyph_row->contains_overlapping_glyphs_p = true;
27521
27522 it->pixel_width = cmp->pixel_width;
27523 it->ascent = it->phys_ascent = cmp->ascent;
27524 it->descent = it->phys_descent = cmp->descent;
27525 if (face->box != FACE_NO_BOX)
27526 {
27527 int thick = face->box_line_width;
27528
27529 if (thick > 0)
27530 {
27531 it->ascent += thick;
27532 it->descent += thick;
27533 }
27534 else
27535 thick = - thick;
27536
27537 if (it->start_of_box_run_p)
27538 it->pixel_width += thick;
27539 if (it->end_of_box_run_p)
27540 it->pixel_width += thick;
27541 }
27542
27543 /* If face has an overline, add the height of the overline
27544 (1 pixel) and a 1 pixel margin to the character height. */
27545 if (face->overline_p)
27546 it->ascent += overline_margin;
27547
27548 take_vertical_position_into_account (it);
27549 if (it->ascent < 0)
27550 it->ascent = 0;
27551 if (it->descent < 0)
27552 it->descent = 0;
27553
27554 if (it->glyph_row && cmp->glyph_len > 0)
27555 append_composite_glyph (it);
27556 }
27557 else if (it->what == IT_COMPOSITION)
27558 {
27559 /* A dynamic (automatic) composition. */
27560 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27561 Lisp_Object gstring;
27562 struct font_metrics metrics;
27563
27564 it->nglyphs = 1;
27565
27566 gstring = composition_gstring_from_id (it->cmp_it.id);
27567 it->pixel_width
27568 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27569 &metrics);
27570 if (it->glyph_row
27571 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27572 it->glyph_row->contains_overlapping_glyphs_p = true;
27573 it->ascent = it->phys_ascent = metrics.ascent;
27574 it->descent = it->phys_descent = metrics.descent;
27575 if (face->box != FACE_NO_BOX)
27576 {
27577 int thick = face->box_line_width;
27578
27579 if (thick > 0)
27580 {
27581 it->ascent += thick;
27582 it->descent += thick;
27583 }
27584 else
27585 thick = - thick;
27586
27587 if (it->start_of_box_run_p)
27588 it->pixel_width += thick;
27589 if (it->end_of_box_run_p)
27590 it->pixel_width += thick;
27591 }
27592 /* If face has an overline, add the height of the overline
27593 (1 pixel) and a 1 pixel margin to the character height. */
27594 if (face->overline_p)
27595 it->ascent += overline_margin;
27596 take_vertical_position_into_account (it);
27597 if (it->ascent < 0)
27598 it->ascent = 0;
27599 if (it->descent < 0)
27600 it->descent = 0;
27601
27602 if (it->glyph_row)
27603 append_composite_glyph (it);
27604 }
27605 else if (it->what == IT_GLYPHLESS)
27606 produce_glyphless_glyph (it, false, Qnil);
27607 else if (it->what == IT_IMAGE)
27608 produce_image_glyph (it);
27609 else if (it->what == IT_STRETCH)
27610 produce_stretch_glyph (it);
27611 else if (it->what == IT_XWIDGET)
27612 produce_xwidget_glyph (it);
27613
27614 done:
27615 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27616 because this isn't true for images with `:ascent 100'. */
27617 eassert (it->ascent >= 0 && it->descent >= 0);
27618 if (it->area == TEXT_AREA)
27619 it->current_x += it->pixel_width;
27620
27621 if (extra_line_spacing > 0)
27622 {
27623 it->descent += extra_line_spacing;
27624 if (extra_line_spacing > it->max_extra_line_spacing)
27625 it->max_extra_line_spacing = extra_line_spacing;
27626 }
27627
27628 it->max_ascent = max (it->max_ascent, it->ascent);
27629 it->max_descent = max (it->max_descent, it->descent);
27630 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27631 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27632 }
27633
27634 /* EXPORT for RIF:
27635 Output LEN glyphs starting at START at the nominal cursor position.
27636 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27637 being updated, and UPDATED_AREA is the area of that row being updated. */
27638
27639 void
27640 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27641 struct glyph *start, enum glyph_row_area updated_area, int len)
27642 {
27643 int x, hpos, chpos = w->phys_cursor.hpos;
27644
27645 eassert (updated_row);
27646 /* When the window is hscrolled, cursor hpos can legitimately be out
27647 of bounds, but we draw the cursor at the corresponding window
27648 margin in that case. */
27649 if (!updated_row->reversed_p && chpos < 0)
27650 chpos = 0;
27651 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27652 chpos = updated_row->used[TEXT_AREA] - 1;
27653
27654 block_input ();
27655
27656 /* Write glyphs. */
27657
27658 hpos = start - updated_row->glyphs[updated_area];
27659 x = draw_glyphs (w, w->output_cursor.x,
27660 updated_row, updated_area,
27661 hpos, hpos + len,
27662 DRAW_NORMAL_TEXT, 0);
27663
27664 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27665 if (updated_area == TEXT_AREA
27666 && w->phys_cursor_on_p
27667 && w->phys_cursor.vpos == w->output_cursor.vpos
27668 && chpos >= hpos
27669 && chpos < hpos + len)
27670 w->phys_cursor_on_p = false;
27671
27672 unblock_input ();
27673
27674 /* Advance the output cursor. */
27675 w->output_cursor.hpos += len;
27676 w->output_cursor.x = x;
27677 }
27678
27679
27680 /* EXPORT for RIF:
27681 Insert LEN glyphs from START at the nominal cursor position. */
27682
27683 void
27684 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27685 struct glyph *start, enum glyph_row_area updated_area, int len)
27686 {
27687 struct frame *f;
27688 int line_height, shift_by_width, shifted_region_width;
27689 struct glyph_row *row;
27690 struct glyph *glyph;
27691 int frame_x, frame_y;
27692 ptrdiff_t hpos;
27693
27694 eassert (updated_row);
27695 block_input ();
27696 f = XFRAME (WINDOW_FRAME (w));
27697
27698 /* Get the height of the line we are in. */
27699 row = updated_row;
27700 line_height = row->height;
27701
27702 /* Get the width of the glyphs to insert. */
27703 shift_by_width = 0;
27704 for (glyph = start; glyph < start + len; ++glyph)
27705 shift_by_width += glyph->pixel_width;
27706
27707 /* Get the width of the region to shift right. */
27708 shifted_region_width = (window_box_width (w, updated_area)
27709 - w->output_cursor.x
27710 - shift_by_width);
27711
27712 /* Shift right. */
27713 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27714 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27715
27716 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27717 line_height, shift_by_width);
27718
27719 /* Write the glyphs. */
27720 hpos = start - row->glyphs[updated_area];
27721 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27722 hpos, hpos + len,
27723 DRAW_NORMAL_TEXT, 0);
27724
27725 /* Advance the output cursor. */
27726 w->output_cursor.hpos += len;
27727 w->output_cursor.x += shift_by_width;
27728 unblock_input ();
27729 }
27730
27731
27732 /* EXPORT for RIF:
27733 Erase the current text line from the nominal cursor position
27734 (inclusive) to pixel column TO_X (exclusive). The idea is that
27735 everything from TO_X onward is already erased.
27736
27737 TO_X is a pixel position relative to UPDATED_AREA of currently
27738 updated window W. TO_X == -1 means clear to the end of this area. */
27739
27740 void
27741 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27742 enum glyph_row_area updated_area, int to_x)
27743 {
27744 struct frame *f;
27745 int max_x, min_y, max_y;
27746 int from_x, from_y, to_y;
27747
27748 eassert (updated_row);
27749 f = XFRAME (w->frame);
27750
27751 if (updated_row->full_width_p)
27752 max_x = (WINDOW_PIXEL_WIDTH (w)
27753 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27754 else
27755 max_x = window_box_width (w, updated_area);
27756 max_y = window_text_bottom_y (w);
27757
27758 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27759 of window. For TO_X > 0, truncate to end of drawing area. */
27760 if (to_x == 0)
27761 return;
27762 else if (to_x < 0)
27763 to_x = max_x;
27764 else
27765 to_x = min (to_x, max_x);
27766
27767 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27768
27769 /* Notice if the cursor will be cleared by this operation. */
27770 if (!updated_row->full_width_p)
27771 notice_overwritten_cursor (w, updated_area,
27772 w->output_cursor.x, -1,
27773 updated_row->y,
27774 MATRIX_ROW_BOTTOM_Y (updated_row));
27775
27776 from_x = w->output_cursor.x;
27777
27778 /* Translate to frame coordinates. */
27779 if (updated_row->full_width_p)
27780 {
27781 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27782 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27783 }
27784 else
27785 {
27786 int area_left = window_box_left (w, updated_area);
27787 from_x += area_left;
27788 to_x += area_left;
27789 }
27790
27791 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27792 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27793 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27794
27795 /* Prevent inadvertently clearing to end of the X window. */
27796 if (to_x > from_x && to_y > from_y)
27797 {
27798 block_input ();
27799 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27800 to_x - from_x, to_y - from_y);
27801 unblock_input ();
27802 }
27803 }
27804
27805 #endif /* HAVE_WINDOW_SYSTEM */
27806
27807
27808 \f
27809 /***********************************************************************
27810 Cursor types
27811 ***********************************************************************/
27812
27813 /* Value is the internal representation of the specified cursor type
27814 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27815 of the bar cursor. */
27816
27817 static enum text_cursor_kinds
27818 get_specified_cursor_type (Lisp_Object arg, int *width)
27819 {
27820 enum text_cursor_kinds type;
27821
27822 if (NILP (arg))
27823 return NO_CURSOR;
27824
27825 if (EQ (arg, Qbox))
27826 return FILLED_BOX_CURSOR;
27827
27828 if (EQ (arg, Qhollow))
27829 return HOLLOW_BOX_CURSOR;
27830
27831 if (EQ (arg, Qbar))
27832 {
27833 *width = 2;
27834 return BAR_CURSOR;
27835 }
27836
27837 if (CONSP (arg)
27838 && EQ (XCAR (arg), Qbar)
27839 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27840 {
27841 *width = XINT (XCDR (arg));
27842 return BAR_CURSOR;
27843 }
27844
27845 if (EQ (arg, Qhbar))
27846 {
27847 *width = 2;
27848 return HBAR_CURSOR;
27849 }
27850
27851 if (CONSP (arg)
27852 && EQ (XCAR (arg), Qhbar)
27853 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27854 {
27855 *width = XINT (XCDR (arg));
27856 return HBAR_CURSOR;
27857 }
27858
27859 /* Treat anything unknown as "hollow box cursor".
27860 It was bad to signal an error; people have trouble fixing
27861 .Xdefaults with Emacs, when it has something bad in it. */
27862 type = HOLLOW_BOX_CURSOR;
27863
27864 return type;
27865 }
27866
27867 /* Set the default cursor types for specified frame. */
27868 void
27869 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27870 {
27871 int width = 1;
27872 Lisp_Object tem;
27873
27874 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27875 FRAME_CURSOR_WIDTH (f) = width;
27876
27877 /* By default, set up the blink-off state depending on the on-state. */
27878
27879 tem = Fassoc (arg, Vblink_cursor_alist);
27880 if (!NILP (tem))
27881 {
27882 FRAME_BLINK_OFF_CURSOR (f)
27883 = get_specified_cursor_type (XCDR (tem), &width);
27884 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27885 }
27886 else
27887 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27888
27889 /* Make sure the cursor gets redrawn. */
27890 f->cursor_type_changed = true;
27891 }
27892
27893
27894 #ifdef HAVE_WINDOW_SYSTEM
27895
27896 /* Return the cursor we want to be displayed in window W. Return
27897 width of bar/hbar cursor through WIDTH arg. Return with
27898 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
27899 (i.e. if the `system caret' should track this cursor).
27900
27901 In a mini-buffer window, we want the cursor only to appear if we
27902 are reading input from this window. For the selected window, we
27903 want the cursor type given by the frame parameter or buffer local
27904 setting of cursor-type. If explicitly marked off, draw no cursor.
27905 In all other cases, we want a hollow box cursor. */
27906
27907 static enum text_cursor_kinds
27908 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27909 bool *active_cursor)
27910 {
27911 struct frame *f = XFRAME (w->frame);
27912 struct buffer *b = XBUFFER (w->contents);
27913 int cursor_type = DEFAULT_CURSOR;
27914 Lisp_Object alt_cursor;
27915 bool non_selected = false;
27916
27917 *active_cursor = true;
27918
27919 /* Echo area */
27920 if (cursor_in_echo_area
27921 && FRAME_HAS_MINIBUF_P (f)
27922 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27923 {
27924 if (w == XWINDOW (echo_area_window))
27925 {
27926 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27927 {
27928 *width = FRAME_CURSOR_WIDTH (f);
27929 return FRAME_DESIRED_CURSOR (f);
27930 }
27931 else
27932 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27933 }
27934
27935 *active_cursor = false;
27936 non_selected = true;
27937 }
27938
27939 /* Detect a nonselected window or nonselected frame. */
27940 else if (w != XWINDOW (f->selected_window)
27941 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27942 {
27943 *active_cursor = false;
27944
27945 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27946 return NO_CURSOR;
27947
27948 non_selected = true;
27949 }
27950
27951 /* Never display a cursor in a window in which cursor-type is nil. */
27952 if (NILP (BVAR (b, cursor_type)))
27953 return NO_CURSOR;
27954
27955 /* Get the normal cursor type for this window. */
27956 if (EQ (BVAR (b, cursor_type), Qt))
27957 {
27958 cursor_type = FRAME_DESIRED_CURSOR (f);
27959 *width = FRAME_CURSOR_WIDTH (f);
27960 }
27961 else
27962 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27963
27964 /* Use cursor-in-non-selected-windows instead
27965 for non-selected window or frame. */
27966 if (non_selected)
27967 {
27968 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27969 if (!EQ (Qt, alt_cursor))
27970 return get_specified_cursor_type (alt_cursor, width);
27971 /* t means modify the normal cursor type. */
27972 if (cursor_type == FILLED_BOX_CURSOR)
27973 cursor_type = HOLLOW_BOX_CURSOR;
27974 else if (cursor_type == BAR_CURSOR && *width > 1)
27975 --*width;
27976 return cursor_type;
27977 }
27978
27979 /* Use normal cursor if not blinked off. */
27980 if (!w->cursor_off_p)
27981 {
27982 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
27983 return NO_CURSOR;
27984 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27985 {
27986 if (cursor_type == FILLED_BOX_CURSOR)
27987 {
27988 /* Using a block cursor on large images can be very annoying.
27989 So use a hollow cursor for "large" images.
27990 If image is not transparent (no mask), also use hollow cursor. */
27991 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27992 if (img != NULL && IMAGEP (img->spec))
27993 {
27994 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27995 where N = size of default frame font size.
27996 This should cover most of the "tiny" icons people may use. */
27997 if (!img->mask
27998 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27999 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28000 cursor_type = HOLLOW_BOX_CURSOR;
28001 }
28002 }
28003 else if (cursor_type != NO_CURSOR)
28004 {
28005 /* Display current only supports BOX and HOLLOW cursors for images.
28006 So for now, unconditionally use a HOLLOW cursor when cursor is
28007 not a solid box cursor. */
28008 cursor_type = HOLLOW_BOX_CURSOR;
28009 }
28010 }
28011 return cursor_type;
28012 }
28013
28014 /* Cursor is blinked off, so determine how to "toggle" it. */
28015
28016 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28017 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
28018 return get_specified_cursor_type (XCDR (alt_cursor), width);
28019
28020 /* Then see if frame has specified a specific blink off cursor type. */
28021 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28022 {
28023 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28024 return FRAME_BLINK_OFF_CURSOR (f);
28025 }
28026
28027 #if false
28028 /* Some people liked having a permanently visible blinking cursor,
28029 while others had very strong opinions against it. So it was
28030 decided to remove it. KFS 2003-09-03 */
28031
28032 /* Finally perform built-in cursor blinking:
28033 filled box <-> hollow box
28034 wide [h]bar <-> narrow [h]bar
28035 narrow [h]bar <-> no cursor
28036 other type <-> no cursor */
28037
28038 if (cursor_type == FILLED_BOX_CURSOR)
28039 return HOLLOW_BOX_CURSOR;
28040
28041 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28042 {
28043 *width = 1;
28044 return cursor_type;
28045 }
28046 #endif
28047
28048 return NO_CURSOR;
28049 }
28050
28051
28052 /* Notice when the text cursor of window W has been completely
28053 overwritten by a drawing operation that outputs glyphs in AREA
28054 starting at X0 and ending at X1 in the line starting at Y0 and
28055 ending at Y1. X coordinates are area-relative. X1 < 0 means all
28056 the rest of the line after X0 has been written. Y coordinates
28057 are window-relative. */
28058
28059 static void
28060 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
28061 int x0, int x1, int y0, int y1)
28062 {
28063 int cx0, cx1, cy0, cy1;
28064 struct glyph_row *row;
28065
28066 if (!w->phys_cursor_on_p)
28067 return;
28068 if (area != TEXT_AREA)
28069 return;
28070
28071 if (w->phys_cursor.vpos < 0
28072 || w->phys_cursor.vpos >= w->current_matrix->nrows
28073 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
28074 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
28075 return;
28076
28077 if (row->cursor_in_fringe_p)
28078 {
28079 row->cursor_in_fringe_p = false;
28080 draw_fringe_bitmap (w, row, row->reversed_p);
28081 w->phys_cursor_on_p = false;
28082 return;
28083 }
28084
28085 cx0 = w->phys_cursor.x;
28086 cx1 = cx0 + w->phys_cursor_width;
28087 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
28088 return;
28089
28090 /* The cursor image will be completely removed from the
28091 screen if the output area intersects the cursor area in
28092 y-direction. When we draw in [y0 y1[, and some part of
28093 the cursor is at y < y0, that part must have been drawn
28094 before. When scrolling, the cursor is erased before
28095 actually scrolling, so we don't come here. When not
28096 scrolling, the rows above the old cursor row must have
28097 changed, and in this case these rows must have written
28098 over the cursor image.
28099
28100 Likewise if part of the cursor is below y1, with the
28101 exception of the cursor being in the first blank row at
28102 the buffer and window end because update_text_area
28103 doesn't draw that row. (Except when it does, but
28104 that's handled in update_text_area.) */
28105
28106 cy0 = w->phys_cursor.y;
28107 cy1 = cy0 + w->phys_cursor_height;
28108 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
28109 return;
28110
28111 w->phys_cursor_on_p = false;
28112 }
28113
28114 #endif /* HAVE_WINDOW_SYSTEM */
28115
28116 \f
28117 /************************************************************************
28118 Mouse Face
28119 ************************************************************************/
28120
28121 #ifdef HAVE_WINDOW_SYSTEM
28122
28123 /* EXPORT for RIF:
28124 Fix the display of area AREA of overlapping row ROW in window W
28125 with respect to the overlapping part OVERLAPS. */
28126
28127 void
28128 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
28129 enum glyph_row_area area, int overlaps)
28130 {
28131 int i, x;
28132
28133 block_input ();
28134
28135 x = 0;
28136 for (i = 0; i < row->used[area];)
28137 {
28138 if (row->glyphs[area][i].overlaps_vertically_p)
28139 {
28140 int start = i, start_x = x;
28141
28142 do
28143 {
28144 x += row->glyphs[area][i].pixel_width;
28145 ++i;
28146 }
28147 while (i < row->used[area]
28148 && row->glyphs[area][i].overlaps_vertically_p);
28149
28150 draw_glyphs (w, start_x, row, area,
28151 start, i,
28152 DRAW_NORMAL_TEXT, overlaps);
28153 }
28154 else
28155 {
28156 x += row->glyphs[area][i].pixel_width;
28157 ++i;
28158 }
28159 }
28160
28161 unblock_input ();
28162 }
28163
28164
28165 /* EXPORT:
28166 Draw the cursor glyph of window W in glyph row ROW. See the
28167 comment of draw_glyphs for the meaning of HL. */
28168
28169 void
28170 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
28171 enum draw_glyphs_face hl)
28172 {
28173 /* If cursor hpos is out of bounds, don't draw garbage. This can
28174 happen in mini-buffer windows when switching between echo area
28175 glyphs and mini-buffer. */
28176 if ((row->reversed_p
28177 ? (w->phys_cursor.hpos >= 0)
28178 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
28179 {
28180 bool on_p = w->phys_cursor_on_p;
28181 int x1;
28182 int hpos = w->phys_cursor.hpos;
28183
28184 /* When the window is hscrolled, cursor hpos can legitimately be
28185 out of bounds, but we draw the cursor at the corresponding
28186 window margin in that case. */
28187 if (!row->reversed_p && hpos < 0)
28188 hpos = 0;
28189 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28190 hpos = row->used[TEXT_AREA] - 1;
28191
28192 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
28193 hl, 0);
28194 w->phys_cursor_on_p = on_p;
28195
28196 if (hl == DRAW_CURSOR)
28197 w->phys_cursor_width = x1 - w->phys_cursor.x;
28198 /* When we erase the cursor, and ROW is overlapped by other
28199 rows, make sure that these overlapping parts of other rows
28200 are redrawn. */
28201 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
28202 {
28203 w->phys_cursor_width = x1 - w->phys_cursor.x;
28204
28205 if (row > w->current_matrix->rows
28206 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
28207 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
28208 OVERLAPS_ERASED_CURSOR);
28209
28210 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
28211 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
28212 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
28213 OVERLAPS_ERASED_CURSOR);
28214 }
28215 }
28216 }
28217
28218
28219 /* Erase the image of a cursor of window W from the screen. */
28220
28221 void
28222 erase_phys_cursor (struct window *w)
28223 {
28224 struct frame *f = XFRAME (w->frame);
28225 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28226 int hpos = w->phys_cursor.hpos;
28227 int vpos = w->phys_cursor.vpos;
28228 bool mouse_face_here_p = false;
28229 struct glyph_matrix *active_glyphs = w->current_matrix;
28230 struct glyph_row *cursor_row;
28231 struct glyph *cursor_glyph;
28232 enum draw_glyphs_face hl;
28233
28234 /* No cursor displayed or row invalidated => nothing to do on the
28235 screen. */
28236 if (w->phys_cursor_type == NO_CURSOR)
28237 goto mark_cursor_off;
28238
28239 /* VPOS >= active_glyphs->nrows means that window has been resized.
28240 Don't bother to erase the cursor. */
28241 if (vpos >= active_glyphs->nrows)
28242 goto mark_cursor_off;
28243
28244 /* If row containing cursor is marked invalid, there is nothing we
28245 can do. */
28246 cursor_row = MATRIX_ROW (active_glyphs, vpos);
28247 if (!cursor_row->enabled_p)
28248 goto mark_cursor_off;
28249
28250 /* If line spacing is > 0, old cursor may only be partially visible in
28251 window after split-window. So adjust visible height. */
28252 cursor_row->visible_height = min (cursor_row->visible_height,
28253 window_text_bottom_y (w) - cursor_row->y);
28254
28255 /* If row is completely invisible, don't attempt to delete a cursor which
28256 isn't there. This can happen if cursor is at top of a window, and
28257 we switch to a buffer with a header line in that window. */
28258 if (cursor_row->visible_height <= 0)
28259 goto mark_cursor_off;
28260
28261 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
28262 if (cursor_row->cursor_in_fringe_p)
28263 {
28264 cursor_row->cursor_in_fringe_p = false;
28265 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
28266 goto mark_cursor_off;
28267 }
28268
28269 /* This can happen when the new row is shorter than the old one.
28270 In this case, either draw_glyphs or clear_end_of_line
28271 should have cleared the cursor. Note that we wouldn't be
28272 able to erase the cursor in this case because we don't have a
28273 cursor glyph at hand. */
28274 if ((cursor_row->reversed_p
28275 ? (w->phys_cursor.hpos < 0)
28276 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
28277 goto mark_cursor_off;
28278
28279 /* When the window is hscrolled, cursor hpos can legitimately be out
28280 of bounds, but we draw the cursor at the corresponding window
28281 margin in that case. */
28282 if (!cursor_row->reversed_p && hpos < 0)
28283 hpos = 0;
28284 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
28285 hpos = cursor_row->used[TEXT_AREA] - 1;
28286
28287 /* If the cursor is in the mouse face area, redisplay that when
28288 we clear the cursor. */
28289 if (! NILP (hlinfo->mouse_face_window)
28290 && coords_in_mouse_face_p (w, hpos, vpos)
28291 /* Don't redraw the cursor's spot in mouse face if it is at the
28292 end of a line (on a newline). The cursor appears there, but
28293 mouse highlighting does not. */
28294 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
28295 mouse_face_here_p = true;
28296
28297 /* Maybe clear the display under the cursor. */
28298 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
28299 {
28300 int x, y;
28301 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
28302 int width;
28303
28304 cursor_glyph = get_phys_cursor_glyph (w);
28305 if (cursor_glyph == NULL)
28306 goto mark_cursor_off;
28307
28308 width = cursor_glyph->pixel_width;
28309 x = w->phys_cursor.x;
28310 if (x < 0)
28311 {
28312 width += x;
28313 x = 0;
28314 }
28315 width = min (width, window_box_width (w, TEXT_AREA) - x);
28316 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28317 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28318
28319 if (width > 0)
28320 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28321 }
28322
28323 /* Erase the cursor by redrawing the character underneath it. */
28324 if (mouse_face_here_p)
28325 hl = DRAW_MOUSE_FACE;
28326 else
28327 hl = DRAW_NORMAL_TEXT;
28328 draw_phys_cursor_glyph (w, cursor_row, hl);
28329
28330 mark_cursor_off:
28331 w->phys_cursor_on_p = false;
28332 w->phys_cursor_type = NO_CURSOR;
28333 }
28334
28335
28336 /* Display or clear cursor of window W. If !ON, clear the cursor.
28337 If ON, display the cursor; where to put the cursor is specified by
28338 HPOS, VPOS, X and Y. */
28339
28340 void
28341 display_and_set_cursor (struct window *w, bool on,
28342 int hpos, int vpos, int x, int y)
28343 {
28344 struct frame *f = XFRAME (w->frame);
28345 int new_cursor_type;
28346 int new_cursor_width;
28347 bool active_cursor;
28348 struct glyph_row *glyph_row;
28349 struct glyph *glyph;
28350
28351 /* This is pointless on invisible frames, and dangerous on garbaged
28352 windows and frames; in the latter case, the frame or window may
28353 be in the midst of changing its size, and x and y may be off the
28354 window. */
28355 if (! FRAME_VISIBLE_P (f)
28356 || FRAME_GARBAGED_P (f)
28357 || vpos >= w->current_matrix->nrows
28358 || hpos >= w->current_matrix->matrix_w)
28359 return;
28360
28361 /* If cursor is off and we want it off, return quickly. */
28362 if (!on && !w->phys_cursor_on_p)
28363 return;
28364
28365 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28366 /* If cursor row is not enabled, we don't really know where to
28367 display the cursor. */
28368 if (!glyph_row->enabled_p)
28369 {
28370 w->phys_cursor_on_p = false;
28371 return;
28372 }
28373
28374 glyph = NULL;
28375 if (!glyph_row->exact_window_width_line_p
28376 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
28377 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28378
28379 eassert (input_blocked_p ());
28380
28381 /* Set new_cursor_type to the cursor we want to be displayed. */
28382 new_cursor_type = get_window_cursor_type (w, glyph,
28383 &new_cursor_width, &active_cursor);
28384
28385 /* If cursor is currently being shown and we don't want it to be or
28386 it is in the wrong place, or the cursor type is not what we want,
28387 erase it. */
28388 if (w->phys_cursor_on_p
28389 && (!on
28390 || w->phys_cursor.x != x
28391 || w->phys_cursor.y != y
28392 /* HPOS can be negative in R2L rows whose
28393 exact_window_width_line_p flag is set (i.e. their newline
28394 would "overflow into the fringe"). */
28395 || hpos < 0
28396 || new_cursor_type != w->phys_cursor_type
28397 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28398 && new_cursor_width != w->phys_cursor_width)))
28399 erase_phys_cursor (w);
28400
28401 /* Don't check phys_cursor_on_p here because that flag is only set
28402 to false in some cases where we know that the cursor has been
28403 completely erased, to avoid the extra work of erasing the cursor
28404 twice. In other words, phys_cursor_on_p can be true and the cursor
28405 still not be visible, or it has only been partly erased. */
28406 if (on)
28407 {
28408 w->phys_cursor_ascent = glyph_row->ascent;
28409 w->phys_cursor_height = glyph_row->height;
28410
28411 /* Set phys_cursor_.* before x_draw_.* is called because some
28412 of them may need the information. */
28413 w->phys_cursor.x = x;
28414 w->phys_cursor.y = glyph_row->y;
28415 w->phys_cursor.hpos = hpos;
28416 w->phys_cursor.vpos = vpos;
28417 }
28418
28419 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28420 new_cursor_type, new_cursor_width,
28421 on, active_cursor);
28422 }
28423
28424
28425 /* Switch the display of W's cursor on or off, according to the value
28426 of ON. */
28427
28428 static void
28429 update_window_cursor (struct window *w, bool on)
28430 {
28431 /* Don't update cursor in windows whose frame is in the process
28432 of being deleted. */
28433 if (w->current_matrix)
28434 {
28435 int hpos = w->phys_cursor.hpos;
28436 int vpos = w->phys_cursor.vpos;
28437 struct glyph_row *row;
28438
28439 if (vpos >= w->current_matrix->nrows
28440 || hpos >= w->current_matrix->matrix_w)
28441 return;
28442
28443 row = MATRIX_ROW (w->current_matrix, vpos);
28444
28445 /* When the window is hscrolled, cursor hpos can legitimately be
28446 out of bounds, but we draw the cursor at the corresponding
28447 window margin in that case. */
28448 if (!row->reversed_p && hpos < 0)
28449 hpos = 0;
28450 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28451 hpos = row->used[TEXT_AREA] - 1;
28452
28453 block_input ();
28454 display_and_set_cursor (w, on, hpos, vpos,
28455 w->phys_cursor.x, w->phys_cursor.y);
28456 unblock_input ();
28457 }
28458 }
28459
28460
28461 /* Call update_window_cursor with parameter ON_P on all leaf windows
28462 in the window tree rooted at W. */
28463
28464 static void
28465 update_cursor_in_window_tree (struct window *w, bool on_p)
28466 {
28467 while (w)
28468 {
28469 if (WINDOWP (w->contents))
28470 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28471 else
28472 update_window_cursor (w, on_p);
28473
28474 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28475 }
28476 }
28477
28478
28479 /* EXPORT:
28480 Display the cursor on window W, or clear it, according to ON_P.
28481 Don't change the cursor's position. */
28482
28483 void
28484 x_update_cursor (struct frame *f, bool on_p)
28485 {
28486 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28487 }
28488
28489
28490 /* EXPORT:
28491 Clear the cursor of window W to background color, and mark the
28492 cursor as not shown. This is used when the text where the cursor
28493 is about to be rewritten. */
28494
28495 void
28496 x_clear_cursor (struct window *w)
28497 {
28498 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28499 update_window_cursor (w, false);
28500 }
28501
28502 #endif /* HAVE_WINDOW_SYSTEM */
28503
28504 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28505 and MSDOS. */
28506 static void
28507 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28508 int start_hpos, int end_hpos,
28509 enum draw_glyphs_face draw)
28510 {
28511 #ifdef HAVE_WINDOW_SYSTEM
28512 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28513 {
28514 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28515 return;
28516 }
28517 #endif
28518 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28519 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28520 #endif
28521 }
28522
28523 /* Display the active region described by mouse_face_* according to DRAW. */
28524
28525 static void
28526 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28527 {
28528 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28529 struct frame *f = XFRAME (WINDOW_FRAME (w));
28530
28531 if (/* If window is in the process of being destroyed, don't bother
28532 to do anything. */
28533 w->current_matrix != NULL
28534 /* Don't update mouse highlight if hidden. */
28535 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28536 /* Recognize when we are called to operate on rows that don't exist
28537 anymore. This can happen when a window is split. */
28538 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28539 {
28540 bool phys_cursor_on_p = w->phys_cursor_on_p;
28541 struct glyph_row *row, *first, *last;
28542
28543 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28544 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28545
28546 for (row = first; row <= last && row->enabled_p; ++row)
28547 {
28548 int start_hpos, end_hpos, start_x;
28549
28550 /* For all but the first row, the highlight starts at column 0. */
28551 if (row == first)
28552 {
28553 /* R2L rows have BEG and END in reversed order, but the
28554 screen drawing geometry is always left to right. So
28555 we need to mirror the beginning and end of the
28556 highlighted area in R2L rows. */
28557 if (!row->reversed_p)
28558 {
28559 start_hpos = hlinfo->mouse_face_beg_col;
28560 start_x = hlinfo->mouse_face_beg_x;
28561 }
28562 else if (row == last)
28563 {
28564 start_hpos = hlinfo->mouse_face_end_col;
28565 start_x = hlinfo->mouse_face_end_x;
28566 }
28567 else
28568 {
28569 start_hpos = 0;
28570 start_x = 0;
28571 }
28572 }
28573 else if (row->reversed_p && row == last)
28574 {
28575 start_hpos = hlinfo->mouse_face_end_col;
28576 start_x = hlinfo->mouse_face_end_x;
28577 }
28578 else
28579 {
28580 start_hpos = 0;
28581 start_x = 0;
28582 }
28583
28584 if (row == last)
28585 {
28586 if (!row->reversed_p)
28587 end_hpos = hlinfo->mouse_face_end_col;
28588 else if (row == first)
28589 end_hpos = hlinfo->mouse_face_beg_col;
28590 else
28591 {
28592 end_hpos = row->used[TEXT_AREA];
28593 if (draw == DRAW_NORMAL_TEXT)
28594 row->fill_line_p = true; /* Clear to end of line. */
28595 }
28596 }
28597 else if (row->reversed_p && row == first)
28598 end_hpos = hlinfo->mouse_face_beg_col;
28599 else
28600 {
28601 end_hpos = row->used[TEXT_AREA];
28602 if (draw == DRAW_NORMAL_TEXT)
28603 row->fill_line_p = true; /* Clear to end of line. */
28604 }
28605
28606 if (end_hpos > start_hpos)
28607 {
28608 draw_row_with_mouse_face (w, start_x, row,
28609 start_hpos, end_hpos, draw);
28610
28611 row->mouse_face_p
28612 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28613 }
28614 }
28615
28616 #ifdef HAVE_WINDOW_SYSTEM
28617 /* When we've written over the cursor, arrange for it to
28618 be displayed again. */
28619 if (FRAME_WINDOW_P (f)
28620 && phys_cursor_on_p && !w->phys_cursor_on_p)
28621 {
28622 int hpos = w->phys_cursor.hpos;
28623
28624 /* When the window is hscrolled, cursor hpos can legitimately be
28625 out of bounds, but we draw the cursor at the corresponding
28626 window margin in that case. */
28627 if (!row->reversed_p && hpos < 0)
28628 hpos = 0;
28629 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28630 hpos = row->used[TEXT_AREA] - 1;
28631
28632 block_input ();
28633 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28634 w->phys_cursor.x, w->phys_cursor.y);
28635 unblock_input ();
28636 }
28637 #endif /* HAVE_WINDOW_SYSTEM */
28638 }
28639
28640 #ifdef HAVE_WINDOW_SYSTEM
28641 /* Change the mouse cursor. */
28642 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28643 {
28644 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28645 if (draw == DRAW_NORMAL_TEXT
28646 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28647 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28648 else
28649 #endif
28650 if (draw == DRAW_MOUSE_FACE)
28651 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28652 else
28653 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28654 }
28655 #endif /* HAVE_WINDOW_SYSTEM */
28656 }
28657
28658 /* EXPORT:
28659 Clear out the mouse-highlighted active region.
28660 Redraw it un-highlighted first. Value is true if mouse
28661 face was actually drawn unhighlighted. */
28662
28663 bool
28664 clear_mouse_face (Mouse_HLInfo *hlinfo)
28665 {
28666 bool cleared
28667 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28668 if (cleared)
28669 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28670 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28671 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28672 hlinfo->mouse_face_window = Qnil;
28673 hlinfo->mouse_face_overlay = Qnil;
28674 return cleared;
28675 }
28676
28677 /* Return true if the coordinates HPOS and VPOS on windows W are
28678 within the mouse face on that window. */
28679 static bool
28680 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28681 {
28682 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28683
28684 /* Quickly resolve the easy cases. */
28685 if (!(WINDOWP (hlinfo->mouse_face_window)
28686 && XWINDOW (hlinfo->mouse_face_window) == w))
28687 return false;
28688 if (vpos < hlinfo->mouse_face_beg_row
28689 || vpos > hlinfo->mouse_face_end_row)
28690 return false;
28691 if (vpos > hlinfo->mouse_face_beg_row
28692 && vpos < hlinfo->mouse_face_end_row)
28693 return true;
28694
28695 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28696 {
28697 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28698 {
28699 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28700 return true;
28701 }
28702 else if ((vpos == hlinfo->mouse_face_beg_row
28703 && hpos >= hlinfo->mouse_face_beg_col)
28704 || (vpos == hlinfo->mouse_face_end_row
28705 && hpos < hlinfo->mouse_face_end_col))
28706 return true;
28707 }
28708 else
28709 {
28710 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28711 {
28712 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28713 return true;
28714 }
28715 else if ((vpos == hlinfo->mouse_face_beg_row
28716 && hpos <= hlinfo->mouse_face_beg_col)
28717 || (vpos == hlinfo->mouse_face_end_row
28718 && hpos > hlinfo->mouse_face_end_col))
28719 return true;
28720 }
28721 return false;
28722 }
28723
28724
28725 /* EXPORT:
28726 True if physical cursor of window W is within mouse face. */
28727
28728 bool
28729 cursor_in_mouse_face_p (struct window *w)
28730 {
28731 int hpos = w->phys_cursor.hpos;
28732 int vpos = w->phys_cursor.vpos;
28733 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28734
28735 /* When the window is hscrolled, cursor hpos can legitimately be out
28736 of bounds, but we draw the cursor at the corresponding window
28737 margin in that case. */
28738 if (!row->reversed_p && hpos < 0)
28739 hpos = 0;
28740 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28741 hpos = row->used[TEXT_AREA] - 1;
28742
28743 return coords_in_mouse_face_p (w, hpos, vpos);
28744 }
28745
28746
28747 \f
28748 /* Find the glyph rows START_ROW and END_ROW of window W that display
28749 characters between buffer positions START_CHARPOS and END_CHARPOS
28750 (excluding END_CHARPOS). DISP_STRING is a display string that
28751 covers these buffer positions. This is similar to
28752 row_containing_pos, but is more accurate when bidi reordering makes
28753 buffer positions change non-linearly with glyph rows. */
28754 static void
28755 rows_from_pos_range (struct window *w,
28756 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28757 Lisp_Object disp_string,
28758 struct glyph_row **start, struct glyph_row **end)
28759 {
28760 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28761 int last_y = window_text_bottom_y (w);
28762 struct glyph_row *row;
28763
28764 *start = NULL;
28765 *end = NULL;
28766
28767 while (!first->enabled_p
28768 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28769 first++;
28770
28771 /* Find the START row. */
28772 for (row = first;
28773 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28774 row++)
28775 {
28776 /* A row can potentially be the START row if the range of the
28777 characters it displays intersects the range
28778 [START_CHARPOS..END_CHARPOS). */
28779 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28780 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28781 /* See the commentary in row_containing_pos, for the
28782 explanation of the complicated way to check whether
28783 some position is beyond the end of the characters
28784 displayed by a row. */
28785 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28786 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28787 && !row->ends_at_zv_p
28788 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28789 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28790 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28791 && !row->ends_at_zv_p
28792 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28793 {
28794 /* Found a candidate row. Now make sure at least one of the
28795 glyphs it displays has a charpos from the range
28796 [START_CHARPOS..END_CHARPOS).
28797
28798 This is not obvious because bidi reordering could make
28799 buffer positions of a row be 1,2,3,102,101,100, and if we
28800 want to highlight characters in [50..60), we don't want
28801 this row, even though [50..60) does intersect [1..103),
28802 the range of character positions given by the row's start
28803 and end positions. */
28804 struct glyph *g = row->glyphs[TEXT_AREA];
28805 struct glyph *e = g + row->used[TEXT_AREA];
28806
28807 while (g < e)
28808 {
28809 if (((BUFFERP (g->object) || NILP (g->object))
28810 && start_charpos <= g->charpos && g->charpos < end_charpos)
28811 /* A glyph that comes from DISP_STRING is by
28812 definition to be highlighted. */
28813 || EQ (g->object, disp_string))
28814 *start = row;
28815 g++;
28816 }
28817 if (*start)
28818 break;
28819 }
28820 }
28821
28822 /* Find the END row. */
28823 if (!*start
28824 /* If the last row is partially visible, start looking for END
28825 from that row, instead of starting from FIRST. */
28826 && !(row->enabled_p
28827 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28828 row = first;
28829 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28830 {
28831 struct glyph_row *next = row + 1;
28832 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28833
28834 if (!next->enabled_p
28835 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28836 /* The first row >= START whose range of displayed characters
28837 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28838 is the row END + 1. */
28839 || (start_charpos < next_start
28840 && end_charpos < next_start)
28841 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28842 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28843 && !next->ends_at_zv_p
28844 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28845 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28846 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28847 && !next->ends_at_zv_p
28848 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28849 {
28850 *end = row;
28851 break;
28852 }
28853 else
28854 {
28855 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28856 but none of the characters it displays are in the range, it is
28857 also END + 1. */
28858 struct glyph *g = next->glyphs[TEXT_AREA];
28859 struct glyph *s = g;
28860 struct glyph *e = g + next->used[TEXT_AREA];
28861
28862 while (g < e)
28863 {
28864 if (((BUFFERP (g->object) || NILP (g->object))
28865 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28866 /* If the buffer position of the first glyph in
28867 the row is equal to END_CHARPOS, it means
28868 the last character to be highlighted is the
28869 newline of ROW, and we must consider NEXT as
28870 END, not END+1. */
28871 || (((!next->reversed_p && g == s)
28872 || (next->reversed_p && g == e - 1))
28873 && (g->charpos == end_charpos
28874 /* Special case for when NEXT is an
28875 empty line at ZV. */
28876 || (g->charpos == -1
28877 && !row->ends_at_zv_p
28878 && next_start == end_charpos)))))
28879 /* A glyph that comes from DISP_STRING is by
28880 definition to be highlighted. */
28881 || EQ (g->object, disp_string))
28882 break;
28883 g++;
28884 }
28885 if (g == e)
28886 {
28887 *end = row;
28888 break;
28889 }
28890 /* The first row that ends at ZV must be the last to be
28891 highlighted. */
28892 else if (next->ends_at_zv_p)
28893 {
28894 *end = next;
28895 break;
28896 }
28897 }
28898 }
28899 }
28900
28901 /* This function sets the mouse_face_* elements of HLINFO, assuming
28902 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28903 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28904 for the overlay or run of text properties specifying the mouse
28905 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28906 before-string and after-string that must also be highlighted.
28907 DISP_STRING, if non-nil, is a display string that may cover some
28908 or all of the highlighted text. */
28909
28910 static void
28911 mouse_face_from_buffer_pos (Lisp_Object window,
28912 Mouse_HLInfo *hlinfo,
28913 ptrdiff_t mouse_charpos,
28914 ptrdiff_t start_charpos,
28915 ptrdiff_t end_charpos,
28916 Lisp_Object before_string,
28917 Lisp_Object after_string,
28918 Lisp_Object disp_string)
28919 {
28920 struct window *w = XWINDOW (window);
28921 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28922 struct glyph_row *r1, *r2;
28923 struct glyph *glyph, *end;
28924 ptrdiff_t ignore, pos;
28925 int x;
28926
28927 eassert (NILP (disp_string) || STRINGP (disp_string));
28928 eassert (NILP (before_string) || STRINGP (before_string));
28929 eassert (NILP (after_string) || STRINGP (after_string));
28930
28931 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28932 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28933 if (r1 == NULL)
28934 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28935 /* If the before-string or display-string contains newlines,
28936 rows_from_pos_range skips to its last row. Move back. */
28937 if (!NILP (before_string) || !NILP (disp_string))
28938 {
28939 struct glyph_row *prev;
28940 while ((prev = r1 - 1, prev >= first)
28941 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28942 && prev->used[TEXT_AREA] > 0)
28943 {
28944 struct glyph *beg = prev->glyphs[TEXT_AREA];
28945 glyph = beg + prev->used[TEXT_AREA];
28946 while (--glyph >= beg && NILP (glyph->object));
28947 if (glyph < beg
28948 || !(EQ (glyph->object, before_string)
28949 || EQ (glyph->object, disp_string)))
28950 break;
28951 r1 = prev;
28952 }
28953 }
28954 if (r2 == NULL)
28955 {
28956 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28957 hlinfo->mouse_face_past_end = true;
28958 }
28959 else if (!NILP (after_string))
28960 {
28961 /* If the after-string has newlines, advance to its last row. */
28962 struct glyph_row *next;
28963 struct glyph_row *last
28964 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28965
28966 for (next = r2 + 1;
28967 next <= last
28968 && next->used[TEXT_AREA] > 0
28969 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28970 ++next)
28971 r2 = next;
28972 }
28973 /* The rest of the display engine assumes that mouse_face_beg_row is
28974 either above mouse_face_end_row or identical to it. But with
28975 bidi-reordered continued lines, the row for START_CHARPOS could
28976 be below the row for END_CHARPOS. If so, swap the rows and store
28977 them in correct order. */
28978 if (r1->y > r2->y)
28979 {
28980 struct glyph_row *tem = r2;
28981
28982 r2 = r1;
28983 r1 = tem;
28984 }
28985
28986 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28987 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28988
28989 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28990 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28991 could be anywhere in the row and in any order. The strategy
28992 below is to find the leftmost and the rightmost glyph that
28993 belongs to either of these 3 strings, or whose position is
28994 between START_CHARPOS and END_CHARPOS, and highlight all the
28995 glyphs between those two. This may cover more than just the text
28996 between START_CHARPOS and END_CHARPOS if the range of characters
28997 strides the bidi level boundary, e.g. if the beginning is in R2L
28998 text while the end is in L2R text or vice versa. */
28999 if (!r1->reversed_p)
29000 {
29001 /* This row is in a left to right paragraph. Scan it left to
29002 right. */
29003 glyph = r1->glyphs[TEXT_AREA];
29004 end = glyph + r1->used[TEXT_AREA];
29005 x = r1->x;
29006
29007 /* Skip truncation glyphs at the start of the glyph row. */
29008 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29009 for (; glyph < end
29010 && NILP (glyph->object)
29011 && glyph->charpos < 0;
29012 ++glyph)
29013 x += glyph->pixel_width;
29014
29015 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29016 or DISP_STRING, and the first glyph from buffer whose
29017 position is between START_CHARPOS and END_CHARPOS. */
29018 for (; glyph < end
29019 && !NILP (glyph->object)
29020 && !EQ (glyph->object, disp_string)
29021 && !(BUFFERP (glyph->object)
29022 && (glyph->charpos >= start_charpos
29023 && glyph->charpos < end_charpos));
29024 ++glyph)
29025 {
29026 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29027 are present at buffer positions between START_CHARPOS and
29028 END_CHARPOS, or if they come from an overlay. */
29029 if (EQ (glyph->object, before_string))
29030 {
29031 pos = string_buffer_position (before_string,
29032 start_charpos);
29033 /* If pos == 0, it means before_string came from an
29034 overlay, not from a buffer position. */
29035 if (!pos || (pos >= start_charpos && pos < end_charpos))
29036 break;
29037 }
29038 else if (EQ (glyph->object, after_string))
29039 {
29040 pos = string_buffer_position (after_string, end_charpos);
29041 if (!pos || (pos >= start_charpos && pos < end_charpos))
29042 break;
29043 }
29044 x += glyph->pixel_width;
29045 }
29046 hlinfo->mouse_face_beg_x = x;
29047 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29048 }
29049 else
29050 {
29051 /* This row is in a right to left paragraph. Scan it right to
29052 left. */
29053 struct glyph *g;
29054
29055 end = r1->glyphs[TEXT_AREA] - 1;
29056 glyph = end + r1->used[TEXT_AREA];
29057
29058 /* Skip truncation glyphs at the start of the glyph row. */
29059 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29060 for (; glyph > end
29061 && NILP (glyph->object)
29062 && glyph->charpos < 0;
29063 --glyph)
29064 ;
29065
29066 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29067 or DISP_STRING, and the first glyph from buffer whose
29068 position is between START_CHARPOS and END_CHARPOS. */
29069 for (; glyph > end
29070 && !NILP (glyph->object)
29071 && !EQ (glyph->object, disp_string)
29072 && !(BUFFERP (glyph->object)
29073 && (glyph->charpos >= start_charpos
29074 && glyph->charpos < end_charpos));
29075 --glyph)
29076 {
29077 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29078 are present at buffer positions between START_CHARPOS and
29079 END_CHARPOS, or if they come from an overlay. */
29080 if (EQ (glyph->object, before_string))
29081 {
29082 pos = string_buffer_position (before_string, start_charpos);
29083 /* If pos == 0, it means before_string came from an
29084 overlay, not from a buffer position. */
29085 if (!pos || (pos >= start_charpos && pos < end_charpos))
29086 break;
29087 }
29088 else if (EQ (glyph->object, after_string))
29089 {
29090 pos = string_buffer_position (after_string, end_charpos);
29091 if (!pos || (pos >= start_charpos && pos < end_charpos))
29092 break;
29093 }
29094 }
29095
29096 glyph++; /* first glyph to the right of the highlighted area */
29097 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
29098 x += g->pixel_width;
29099 hlinfo->mouse_face_beg_x = x;
29100 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29101 }
29102
29103 /* If the highlight ends in a different row, compute GLYPH and END
29104 for the end row. Otherwise, reuse the values computed above for
29105 the row where the highlight begins. */
29106 if (r2 != r1)
29107 {
29108 if (!r2->reversed_p)
29109 {
29110 glyph = r2->glyphs[TEXT_AREA];
29111 end = glyph + r2->used[TEXT_AREA];
29112 x = r2->x;
29113 }
29114 else
29115 {
29116 end = r2->glyphs[TEXT_AREA] - 1;
29117 glyph = end + r2->used[TEXT_AREA];
29118 }
29119 }
29120
29121 if (!r2->reversed_p)
29122 {
29123 /* Skip truncation and continuation glyphs near the end of the
29124 row, and also blanks and stretch glyphs inserted by
29125 extend_face_to_end_of_line. */
29126 while (end > glyph
29127 && NILP ((end - 1)->object))
29128 --end;
29129 /* Scan the rest of the glyph row from the end, looking for the
29130 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29131 DISP_STRING, or whose position is between START_CHARPOS
29132 and END_CHARPOS */
29133 for (--end;
29134 end > glyph
29135 && !NILP (end->object)
29136 && !EQ (end->object, disp_string)
29137 && !(BUFFERP (end->object)
29138 && (end->charpos >= start_charpos
29139 && end->charpos < end_charpos));
29140 --end)
29141 {
29142 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29143 are present at buffer positions between START_CHARPOS and
29144 END_CHARPOS, or if they come from an overlay. */
29145 if (EQ (end->object, before_string))
29146 {
29147 pos = string_buffer_position (before_string, start_charpos);
29148 if (!pos || (pos >= start_charpos && pos < end_charpos))
29149 break;
29150 }
29151 else if (EQ (end->object, after_string))
29152 {
29153 pos = string_buffer_position (after_string, end_charpos);
29154 if (!pos || (pos >= start_charpos && pos < end_charpos))
29155 break;
29156 }
29157 }
29158 /* Find the X coordinate of the last glyph to be highlighted. */
29159 for (; glyph <= end; ++glyph)
29160 x += glyph->pixel_width;
29161
29162 hlinfo->mouse_face_end_x = x;
29163 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
29164 }
29165 else
29166 {
29167 /* Skip truncation and continuation glyphs near the end of the
29168 row, and also blanks and stretch glyphs inserted by
29169 extend_face_to_end_of_line. */
29170 x = r2->x;
29171 end++;
29172 while (end < glyph
29173 && NILP (end->object))
29174 {
29175 x += end->pixel_width;
29176 ++end;
29177 }
29178 /* Scan the rest of the glyph row from the end, looking for the
29179 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29180 DISP_STRING, or whose position is between START_CHARPOS
29181 and END_CHARPOS */
29182 for ( ;
29183 end < glyph
29184 && !NILP (end->object)
29185 && !EQ (end->object, disp_string)
29186 && !(BUFFERP (end->object)
29187 && (end->charpos >= start_charpos
29188 && end->charpos < end_charpos));
29189 ++end)
29190 {
29191 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29192 are present at buffer positions between START_CHARPOS and
29193 END_CHARPOS, or if they come from an overlay. */
29194 if (EQ (end->object, before_string))
29195 {
29196 pos = string_buffer_position (before_string, start_charpos);
29197 if (!pos || (pos >= start_charpos && pos < end_charpos))
29198 break;
29199 }
29200 else if (EQ (end->object, after_string))
29201 {
29202 pos = string_buffer_position (after_string, end_charpos);
29203 if (!pos || (pos >= start_charpos && pos < end_charpos))
29204 break;
29205 }
29206 x += end->pixel_width;
29207 }
29208 /* If we exited the above loop because we arrived at the last
29209 glyph of the row, and its buffer position is still not in
29210 range, it means the last character in range is the preceding
29211 newline. Bump the end column and x values to get past the
29212 last glyph. */
29213 if (end == glyph
29214 && BUFFERP (end->object)
29215 && (end->charpos < start_charpos
29216 || end->charpos >= end_charpos))
29217 {
29218 x += end->pixel_width;
29219 ++end;
29220 }
29221 hlinfo->mouse_face_end_x = x;
29222 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
29223 }
29224
29225 hlinfo->mouse_face_window = window;
29226 hlinfo->mouse_face_face_id
29227 = face_at_buffer_position (w, mouse_charpos, &ignore,
29228 mouse_charpos + 1,
29229 !hlinfo->mouse_face_hidden, -1);
29230 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29231 }
29232
29233 /* The following function is not used anymore (replaced with
29234 mouse_face_from_string_pos), but I leave it here for the time
29235 being, in case someone would. */
29236
29237 #if false /* not used */
29238
29239 /* Find the position of the glyph for position POS in OBJECT in
29240 window W's current matrix, and return in *X, *Y the pixel
29241 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
29242
29243 RIGHT_P means return the position of the right edge of the glyph.
29244 !RIGHT_P means return the left edge position.
29245
29246 If no glyph for POS exists in the matrix, return the position of
29247 the glyph with the next smaller position that is in the matrix, if
29248 RIGHT_P is false. If RIGHT_P, and no glyph for POS
29249 exists in the matrix, return the position of the glyph with the
29250 next larger position in OBJECT.
29251
29252 Value is true if a glyph was found. */
29253
29254 static bool
29255 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
29256 int *hpos, int *vpos, int *x, int *y, bool right_p)
29257 {
29258 int yb = window_text_bottom_y (w);
29259 struct glyph_row *r;
29260 struct glyph *best_glyph = NULL;
29261 struct glyph_row *best_row = NULL;
29262 int best_x = 0;
29263
29264 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29265 r->enabled_p && r->y < yb;
29266 ++r)
29267 {
29268 struct glyph *g = r->glyphs[TEXT_AREA];
29269 struct glyph *e = g + r->used[TEXT_AREA];
29270 int gx;
29271
29272 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29273 if (EQ (g->object, object))
29274 {
29275 if (g->charpos == pos)
29276 {
29277 best_glyph = g;
29278 best_x = gx;
29279 best_row = r;
29280 goto found;
29281 }
29282 else if (best_glyph == NULL
29283 || ((eabs (g->charpos - pos)
29284 < eabs (best_glyph->charpos - pos))
29285 && (right_p
29286 ? g->charpos < pos
29287 : g->charpos > pos)))
29288 {
29289 best_glyph = g;
29290 best_x = gx;
29291 best_row = r;
29292 }
29293 }
29294 }
29295
29296 found:
29297
29298 if (best_glyph)
29299 {
29300 *x = best_x;
29301 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
29302
29303 if (right_p)
29304 {
29305 *x += best_glyph->pixel_width;
29306 ++*hpos;
29307 }
29308
29309 *y = best_row->y;
29310 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29311 }
29312
29313 return best_glyph != NULL;
29314 }
29315 #endif /* not used */
29316
29317 /* Find the positions of the first and the last glyphs in window W's
29318 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29319 (assumed to be a string), and return in HLINFO's mouse_face_*
29320 members the pixel and column/row coordinates of those glyphs. */
29321
29322 static void
29323 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29324 Lisp_Object object,
29325 ptrdiff_t startpos, ptrdiff_t endpos)
29326 {
29327 int yb = window_text_bottom_y (w);
29328 struct glyph_row *r;
29329 struct glyph *g, *e;
29330 int gx;
29331 bool found = false;
29332
29333 /* Find the glyph row with at least one position in the range
29334 [STARTPOS..ENDPOS), and the first glyph in that row whose
29335 position belongs to that range. */
29336 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29337 r->enabled_p && r->y < yb;
29338 ++r)
29339 {
29340 if (!r->reversed_p)
29341 {
29342 g = r->glyphs[TEXT_AREA];
29343 e = g + r->used[TEXT_AREA];
29344 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29345 if (EQ (g->object, object)
29346 && startpos <= g->charpos && g->charpos < endpos)
29347 {
29348 hlinfo->mouse_face_beg_row
29349 = MATRIX_ROW_VPOS (r, w->current_matrix);
29350 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29351 hlinfo->mouse_face_beg_x = gx;
29352 found = true;
29353 break;
29354 }
29355 }
29356 else
29357 {
29358 struct glyph *g1;
29359
29360 e = r->glyphs[TEXT_AREA];
29361 g = e + r->used[TEXT_AREA];
29362 for ( ; g > e; --g)
29363 if (EQ ((g-1)->object, object)
29364 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29365 {
29366 hlinfo->mouse_face_beg_row
29367 = MATRIX_ROW_VPOS (r, w->current_matrix);
29368 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29369 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29370 gx += g1->pixel_width;
29371 hlinfo->mouse_face_beg_x = gx;
29372 found = true;
29373 break;
29374 }
29375 }
29376 if (found)
29377 break;
29378 }
29379
29380 if (!found)
29381 return;
29382
29383 /* Starting with the next row, look for the first row which does NOT
29384 include any glyphs whose positions are in the range. */
29385 for (++r; r->enabled_p && r->y < yb; ++r)
29386 {
29387 g = r->glyphs[TEXT_AREA];
29388 e = g + r->used[TEXT_AREA];
29389 found = false;
29390 for ( ; g < e; ++g)
29391 if (EQ (g->object, object)
29392 && startpos <= g->charpos && g->charpos < endpos)
29393 {
29394 found = true;
29395 break;
29396 }
29397 if (!found)
29398 break;
29399 }
29400
29401 /* The highlighted region ends on the previous row. */
29402 r--;
29403
29404 /* Set the end row. */
29405 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29406
29407 /* Compute and set the end column and the end column's horizontal
29408 pixel coordinate. */
29409 if (!r->reversed_p)
29410 {
29411 g = r->glyphs[TEXT_AREA];
29412 e = g + r->used[TEXT_AREA];
29413 for ( ; e > g; --e)
29414 if (EQ ((e-1)->object, object)
29415 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29416 break;
29417 hlinfo->mouse_face_end_col = e - g;
29418
29419 for (gx = r->x; g < e; ++g)
29420 gx += g->pixel_width;
29421 hlinfo->mouse_face_end_x = gx;
29422 }
29423 else
29424 {
29425 e = r->glyphs[TEXT_AREA];
29426 g = e + r->used[TEXT_AREA];
29427 for (gx = r->x ; e < g; ++e)
29428 {
29429 if (EQ (e->object, object)
29430 && startpos <= e->charpos && e->charpos < endpos)
29431 break;
29432 gx += e->pixel_width;
29433 }
29434 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29435 hlinfo->mouse_face_end_x = gx;
29436 }
29437 }
29438
29439 #ifdef HAVE_WINDOW_SYSTEM
29440
29441 /* See if position X, Y is within a hot-spot of an image. */
29442
29443 static bool
29444 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29445 {
29446 if (!CONSP (hot_spot))
29447 return false;
29448
29449 if (EQ (XCAR (hot_spot), Qrect))
29450 {
29451 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29452 Lisp_Object rect = XCDR (hot_spot);
29453 Lisp_Object tem;
29454 if (!CONSP (rect))
29455 return false;
29456 if (!CONSP (XCAR (rect)))
29457 return false;
29458 if (!CONSP (XCDR (rect)))
29459 return false;
29460 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29461 return false;
29462 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29463 return false;
29464 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29465 return false;
29466 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29467 return false;
29468 return true;
29469 }
29470 else if (EQ (XCAR (hot_spot), Qcircle))
29471 {
29472 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29473 Lisp_Object circ = XCDR (hot_spot);
29474 Lisp_Object lr, lx0, ly0;
29475 if (CONSP (circ)
29476 && CONSP (XCAR (circ))
29477 && (lr = XCDR (circ), NUMBERP (lr))
29478 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29479 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29480 {
29481 double r = XFLOATINT (lr);
29482 double dx = XINT (lx0) - x;
29483 double dy = XINT (ly0) - y;
29484 return (dx * dx + dy * dy <= r * r);
29485 }
29486 }
29487 else if (EQ (XCAR (hot_spot), Qpoly))
29488 {
29489 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29490 if (VECTORP (XCDR (hot_spot)))
29491 {
29492 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29493 Lisp_Object *poly = v->contents;
29494 ptrdiff_t n = v->header.size;
29495 ptrdiff_t i;
29496 bool inside = false;
29497 Lisp_Object lx, ly;
29498 int x0, y0;
29499
29500 /* Need an even number of coordinates, and at least 3 edges. */
29501 if (n < 6 || n & 1)
29502 return false;
29503
29504 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29505 If count is odd, we are inside polygon. Pixels on edges
29506 may or may not be included depending on actual geometry of the
29507 polygon. */
29508 if ((lx = poly[n-2], !INTEGERP (lx))
29509 || (ly = poly[n-1], !INTEGERP (lx)))
29510 return false;
29511 x0 = XINT (lx), y0 = XINT (ly);
29512 for (i = 0; i < n; i += 2)
29513 {
29514 int x1 = x0, y1 = y0;
29515 if ((lx = poly[i], !INTEGERP (lx))
29516 || (ly = poly[i+1], !INTEGERP (ly)))
29517 return false;
29518 x0 = XINT (lx), y0 = XINT (ly);
29519
29520 /* Does this segment cross the X line? */
29521 if (x0 >= x)
29522 {
29523 if (x1 >= x)
29524 continue;
29525 }
29526 else if (x1 < x)
29527 continue;
29528 if (y > y0 && y > y1)
29529 continue;
29530 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29531 inside = !inside;
29532 }
29533 return inside;
29534 }
29535 }
29536 return false;
29537 }
29538
29539 Lisp_Object
29540 find_hot_spot (Lisp_Object map, int x, int y)
29541 {
29542 while (CONSP (map))
29543 {
29544 if (CONSP (XCAR (map))
29545 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29546 return XCAR (map);
29547 map = XCDR (map);
29548 }
29549
29550 return Qnil;
29551 }
29552
29553 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29554 3, 3, 0,
29555 doc: /* Lookup in image map MAP coordinates X and Y.
29556 An image map is an alist where each element has the format (AREA ID PLIST).
29557 An AREA is specified as either a rectangle, a circle, or a polygon:
29558 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29559 pixel coordinates of the upper left and bottom right corners.
29560 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29561 and the radius of the circle; r may be a float or integer.
29562 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29563 vector describes one corner in the polygon.
29564 Returns the alist element for the first matching AREA in MAP. */)
29565 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29566 {
29567 if (NILP (map))
29568 return Qnil;
29569
29570 CHECK_NUMBER (x);
29571 CHECK_NUMBER (y);
29572
29573 return find_hot_spot (map,
29574 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29575 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29576 }
29577
29578
29579 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29580 static void
29581 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29582 {
29583 /* Do not change cursor shape while dragging mouse. */
29584 if (EQ (do_mouse_tracking, Qdragging))
29585 return;
29586
29587 if (!NILP (pointer))
29588 {
29589 if (EQ (pointer, Qarrow))
29590 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29591 else if (EQ (pointer, Qhand))
29592 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29593 else if (EQ (pointer, Qtext))
29594 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29595 else if (EQ (pointer, intern ("hdrag")))
29596 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29597 else if (EQ (pointer, intern ("nhdrag")))
29598 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29599 #ifdef HAVE_X_WINDOWS
29600 else if (EQ (pointer, intern ("vdrag")))
29601 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29602 #endif
29603 else if (EQ (pointer, intern ("hourglass")))
29604 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29605 else if (EQ (pointer, Qmodeline))
29606 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29607 else
29608 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29609 }
29610
29611 if (cursor != No_Cursor)
29612 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29613 }
29614
29615 #endif /* HAVE_WINDOW_SYSTEM */
29616
29617 /* Take proper action when mouse has moved to the mode or header line
29618 or marginal area AREA of window W, x-position X and y-position Y.
29619 X is relative to the start of the text display area of W, so the
29620 width of bitmap areas and scroll bars must be subtracted to get a
29621 position relative to the start of the mode line. */
29622
29623 static void
29624 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29625 enum window_part area)
29626 {
29627 struct window *w = XWINDOW (window);
29628 struct frame *f = XFRAME (w->frame);
29629 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29630 #ifdef HAVE_WINDOW_SYSTEM
29631 Display_Info *dpyinfo;
29632 #endif
29633 Cursor cursor = No_Cursor;
29634 Lisp_Object pointer = Qnil;
29635 int dx, dy, width, height;
29636 ptrdiff_t charpos;
29637 Lisp_Object string, object = Qnil;
29638 Lisp_Object pos IF_LINT (= Qnil), help;
29639
29640 Lisp_Object mouse_face;
29641 int original_x_pixel = x;
29642 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29643 struct glyph_row *row IF_LINT (= 0);
29644
29645 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29646 {
29647 int x0;
29648 struct glyph *end;
29649
29650 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29651 returns them in row/column units! */
29652 string = mode_line_string (w, area, &x, &y, &charpos,
29653 &object, &dx, &dy, &width, &height);
29654
29655 row = (area == ON_MODE_LINE
29656 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29657 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29658
29659 /* Find the glyph under the mouse pointer. */
29660 if (row->mode_line_p && row->enabled_p)
29661 {
29662 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29663 end = glyph + row->used[TEXT_AREA];
29664
29665 for (x0 = original_x_pixel;
29666 glyph < end && x0 >= glyph->pixel_width;
29667 ++glyph)
29668 x0 -= glyph->pixel_width;
29669
29670 if (glyph >= end)
29671 glyph = NULL;
29672 }
29673 }
29674 else
29675 {
29676 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29677 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29678 returns them in row/column units! */
29679 string = marginal_area_string (w, area, &x, &y, &charpos,
29680 &object, &dx, &dy, &width, &height);
29681 }
29682
29683 help = Qnil;
29684
29685 #ifdef HAVE_WINDOW_SYSTEM
29686 if (IMAGEP (object))
29687 {
29688 Lisp_Object image_map, hotspot;
29689 if ((image_map = Fplist_get (XCDR (object), QCmap),
29690 !NILP (image_map))
29691 && (hotspot = find_hot_spot (image_map, dx, dy),
29692 CONSP (hotspot))
29693 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29694 {
29695 Lisp_Object plist;
29696
29697 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29698 If so, we could look for mouse-enter, mouse-leave
29699 properties in PLIST (and do something...). */
29700 hotspot = XCDR (hotspot);
29701 if (CONSP (hotspot)
29702 && (plist = XCAR (hotspot), CONSP (plist)))
29703 {
29704 pointer = Fplist_get (plist, Qpointer);
29705 if (NILP (pointer))
29706 pointer = Qhand;
29707 help = Fplist_get (plist, Qhelp_echo);
29708 if (!NILP (help))
29709 {
29710 help_echo_string = help;
29711 XSETWINDOW (help_echo_window, w);
29712 help_echo_object = w->contents;
29713 help_echo_pos = charpos;
29714 }
29715 }
29716 }
29717 if (NILP (pointer))
29718 pointer = Fplist_get (XCDR (object), QCpointer);
29719 }
29720 #endif /* HAVE_WINDOW_SYSTEM */
29721
29722 if (STRINGP (string))
29723 pos = make_number (charpos);
29724
29725 /* Set the help text and mouse pointer. If the mouse is on a part
29726 of the mode line without any text (e.g. past the right edge of
29727 the mode line text), use the default help text and pointer. */
29728 if (STRINGP (string) || area == ON_MODE_LINE)
29729 {
29730 /* Arrange to display the help by setting the global variables
29731 help_echo_string, help_echo_object, and help_echo_pos. */
29732 if (NILP (help))
29733 {
29734 if (STRINGP (string))
29735 help = Fget_text_property (pos, Qhelp_echo, string);
29736
29737 if (!NILP (help))
29738 {
29739 help_echo_string = help;
29740 XSETWINDOW (help_echo_window, w);
29741 help_echo_object = string;
29742 help_echo_pos = charpos;
29743 }
29744 else if (area == ON_MODE_LINE)
29745 {
29746 Lisp_Object default_help
29747 = buffer_local_value (Qmode_line_default_help_echo,
29748 w->contents);
29749
29750 if (STRINGP (default_help))
29751 {
29752 help_echo_string = default_help;
29753 XSETWINDOW (help_echo_window, w);
29754 help_echo_object = Qnil;
29755 help_echo_pos = -1;
29756 }
29757 }
29758 }
29759
29760 #ifdef HAVE_WINDOW_SYSTEM
29761 /* Change the mouse pointer according to what is under it. */
29762 if (FRAME_WINDOW_P (f))
29763 {
29764 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29765 || minibuf_level
29766 || NILP (Vresize_mini_windows));
29767
29768 dpyinfo = FRAME_DISPLAY_INFO (f);
29769 if (STRINGP (string))
29770 {
29771 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29772
29773 if (NILP (pointer))
29774 pointer = Fget_text_property (pos, Qpointer, string);
29775
29776 /* Change the mouse pointer according to what is under X/Y. */
29777 if (NILP (pointer)
29778 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29779 {
29780 Lisp_Object map;
29781 map = Fget_text_property (pos, Qlocal_map, string);
29782 if (!KEYMAPP (map))
29783 map = Fget_text_property (pos, Qkeymap, string);
29784 if (!KEYMAPP (map) && draggable)
29785 cursor = dpyinfo->vertical_scroll_bar_cursor;
29786 }
29787 }
29788 else if (draggable)
29789 /* Default mode-line pointer. */
29790 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29791 }
29792 #endif
29793 }
29794
29795 /* Change the mouse face according to what is under X/Y. */
29796 bool mouse_face_shown = false;
29797 if (STRINGP (string))
29798 {
29799 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29800 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29801 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29802 && glyph)
29803 {
29804 Lisp_Object b, e;
29805
29806 struct glyph * tmp_glyph;
29807
29808 int gpos;
29809 int gseq_length;
29810 int total_pixel_width;
29811 ptrdiff_t begpos, endpos, ignore;
29812
29813 int vpos, hpos;
29814
29815 b = Fprevious_single_property_change (make_number (charpos + 1),
29816 Qmouse_face, string, Qnil);
29817 if (NILP (b))
29818 begpos = 0;
29819 else
29820 begpos = XINT (b);
29821
29822 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29823 if (NILP (e))
29824 endpos = SCHARS (string);
29825 else
29826 endpos = XINT (e);
29827
29828 /* Calculate the glyph position GPOS of GLYPH in the
29829 displayed string, relative to the beginning of the
29830 highlighted part of the string.
29831
29832 Note: GPOS is different from CHARPOS. CHARPOS is the
29833 position of GLYPH in the internal string object. A mode
29834 line string format has structures which are converted to
29835 a flattened string by the Emacs Lisp interpreter. The
29836 internal string is an element of those structures. The
29837 displayed string is the flattened string. */
29838 tmp_glyph = row_start_glyph;
29839 while (tmp_glyph < glyph
29840 && (!(EQ (tmp_glyph->object, glyph->object)
29841 && begpos <= tmp_glyph->charpos
29842 && tmp_glyph->charpos < endpos)))
29843 tmp_glyph++;
29844 gpos = glyph - tmp_glyph;
29845
29846 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29847 the highlighted part of the displayed string to which
29848 GLYPH belongs. Note: GSEQ_LENGTH is different from
29849 SCHARS (STRING), because the latter returns the length of
29850 the internal string. */
29851 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29852 tmp_glyph > glyph
29853 && (!(EQ (tmp_glyph->object, glyph->object)
29854 && begpos <= tmp_glyph->charpos
29855 && tmp_glyph->charpos < endpos));
29856 tmp_glyph--)
29857 ;
29858 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29859
29860 /* Calculate the total pixel width of all the glyphs between
29861 the beginning of the highlighted area and GLYPH. */
29862 total_pixel_width = 0;
29863 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29864 total_pixel_width += tmp_glyph->pixel_width;
29865
29866 /* Pre calculation of re-rendering position. Note: X is in
29867 column units here, after the call to mode_line_string or
29868 marginal_area_string. */
29869 hpos = x - gpos;
29870 vpos = (area == ON_MODE_LINE
29871 ? (w->current_matrix)->nrows - 1
29872 : 0);
29873
29874 /* If GLYPH's position is included in the region that is
29875 already drawn in mouse face, we have nothing to do. */
29876 if ( EQ (window, hlinfo->mouse_face_window)
29877 && (!row->reversed_p
29878 ? (hlinfo->mouse_face_beg_col <= hpos
29879 && hpos < hlinfo->mouse_face_end_col)
29880 /* In R2L rows we swap BEG and END, see below. */
29881 : (hlinfo->mouse_face_end_col <= hpos
29882 && hpos < hlinfo->mouse_face_beg_col))
29883 && hlinfo->mouse_face_beg_row == vpos )
29884 return;
29885
29886 if (clear_mouse_face (hlinfo))
29887 cursor = No_Cursor;
29888
29889 if (!row->reversed_p)
29890 {
29891 hlinfo->mouse_face_beg_col = hpos;
29892 hlinfo->mouse_face_beg_x = original_x_pixel
29893 - (total_pixel_width + dx);
29894 hlinfo->mouse_face_end_col = hpos + gseq_length;
29895 hlinfo->mouse_face_end_x = 0;
29896 }
29897 else
29898 {
29899 /* In R2L rows, show_mouse_face expects BEG and END
29900 coordinates to be swapped. */
29901 hlinfo->mouse_face_end_col = hpos;
29902 hlinfo->mouse_face_end_x = original_x_pixel
29903 - (total_pixel_width + dx);
29904 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29905 hlinfo->mouse_face_beg_x = 0;
29906 }
29907
29908 hlinfo->mouse_face_beg_row = vpos;
29909 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29910 hlinfo->mouse_face_past_end = false;
29911 hlinfo->mouse_face_window = window;
29912
29913 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29914 charpos,
29915 0, &ignore,
29916 glyph->face_id,
29917 true);
29918 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29919 mouse_face_shown = true;
29920
29921 if (NILP (pointer))
29922 pointer = Qhand;
29923 }
29924 }
29925
29926 /* If mouse-face doesn't need to be shown, clear any existing
29927 mouse-face. */
29928 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
29929 clear_mouse_face (hlinfo);
29930
29931 #ifdef HAVE_WINDOW_SYSTEM
29932 if (FRAME_WINDOW_P (f))
29933 define_frame_cursor1 (f, cursor, pointer);
29934 #endif
29935 }
29936
29937
29938 /* EXPORT:
29939 Take proper action when the mouse has moved to position X, Y on
29940 frame F with regards to highlighting portions of display that have
29941 mouse-face properties. Also de-highlight portions of display where
29942 the mouse was before, set the mouse pointer shape as appropriate
29943 for the mouse coordinates, and activate help echo (tooltips).
29944 X and Y can be negative or out of range. */
29945
29946 void
29947 note_mouse_highlight (struct frame *f, int x, int y)
29948 {
29949 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29950 enum window_part part = ON_NOTHING;
29951 Lisp_Object window;
29952 struct window *w;
29953 Cursor cursor = No_Cursor;
29954 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29955 struct buffer *b;
29956
29957 /* When a menu is active, don't highlight because this looks odd. */
29958 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29959 if (popup_activated ())
29960 return;
29961 #endif
29962
29963 if (!f->glyphs_initialized_p
29964 || f->pointer_invisible)
29965 return;
29966
29967 hlinfo->mouse_face_mouse_x = x;
29968 hlinfo->mouse_face_mouse_y = y;
29969 hlinfo->mouse_face_mouse_frame = f;
29970
29971 if (hlinfo->mouse_face_defer)
29972 return;
29973
29974 /* Which window is that in? */
29975 window = window_from_coordinates (f, x, y, &part, true);
29976
29977 /* If displaying active text in another window, clear that. */
29978 if (! EQ (window, hlinfo->mouse_face_window)
29979 /* Also clear if we move out of text area in same window. */
29980 || (!NILP (hlinfo->mouse_face_window)
29981 && !NILP (window)
29982 && part != ON_TEXT
29983 && part != ON_MODE_LINE
29984 && part != ON_HEADER_LINE))
29985 clear_mouse_face (hlinfo);
29986
29987 /* Not on a window -> return. */
29988 if (!WINDOWP (window))
29989 return;
29990
29991 /* Reset help_echo_string. It will get recomputed below. */
29992 help_echo_string = Qnil;
29993
29994 /* Convert to window-relative pixel coordinates. */
29995 w = XWINDOW (window);
29996 frame_to_window_pixel_xy (w, &x, &y);
29997
29998 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29999 /* Handle tool-bar window differently since it doesn't display a
30000 buffer. */
30001 if (EQ (window, f->tool_bar_window))
30002 {
30003 note_tool_bar_highlight (f, x, y);
30004 return;
30005 }
30006 #endif
30007
30008 /* Mouse is on the mode, header line or margin? */
30009 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
30010 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30011 {
30012 note_mode_line_or_margin_highlight (window, x, y, part);
30013
30014 #ifdef HAVE_WINDOW_SYSTEM
30015 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30016 {
30017 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30018 /* Show non-text cursor (Bug#16647). */
30019 goto set_cursor;
30020 }
30021 else
30022 #endif
30023 return;
30024 }
30025
30026 #ifdef HAVE_WINDOW_SYSTEM
30027 if (part == ON_VERTICAL_BORDER)
30028 {
30029 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30030 help_echo_string = build_string ("drag-mouse-1: resize");
30031 }
30032 else if (part == ON_RIGHT_DIVIDER)
30033 {
30034 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30035 help_echo_string = build_string ("drag-mouse-1: resize");
30036 }
30037 else if (part == ON_BOTTOM_DIVIDER)
30038 if (! WINDOW_BOTTOMMOST_P (w)
30039 || minibuf_level
30040 || NILP (Vresize_mini_windows))
30041 {
30042 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30043 help_echo_string = build_string ("drag-mouse-1: resize");
30044 }
30045 else
30046 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30047 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
30048 || part == ON_VERTICAL_SCROLL_BAR
30049 || part == ON_HORIZONTAL_SCROLL_BAR)
30050 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30051 else
30052 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30053 #endif
30054
30055 /* Are we in a window whose display is up to date?
30056 And verify the buffer's text has not changed. */
30057 b = XBUFFER (w->contents);
30058 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
30059 {
30060 int hpos, vpos, dx, dy, area = LAST_AREA;
30061 ptrdiff_t pos;
30062 struct glyph *glyph;
30063 Lisp_Object object;
30064 Lisp_Object mouse_face = Qnil, position;
30065 Lisp_Object *overlay_vec = NULL;
30066 ptrdiff_t i, noverlays;
30067 struct buffer *obuf;
30068 ptrdiff_t obegv, ozv;
30069 bool same_region;
30070
30071 /* Find the glyph under X/Y. */
30072 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
30073
30074 #ifdef HAVE_WINDOW_SYSTEM
30075 /* Look for :pointer property on image. */
30076 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
30077 {
30078 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
30079 if (img != NULL && IMAGEP (img->spec))
30080 {
30081 Lisp_Object image_map, hotspot;
30082 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
30083 !NILP (image_map))
30084 && (hotspot = find_hot_spot (image_map,
30085 glyph->slice.img.x + dx,
30086 glyph->slice.img.y + dy),
30087 CONSP (hotspot))
30088 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30089 {
30090 Lisp_Object plist;
30091
30092 /* Could check XCAR (hotspot) to see if we enter/leave
30093 this hot-spot.
30094 If so, we could look for mouse-enter, mouse-leave
30095 properties in PLIST (and do something...). */
30096 hotspot = XCDR (hotspot);
30097 if (CONSP (hotspot)
30098 && (plist = XCAR (hotspot), CONSP (plist)))
30099 {
30100 pointer = Fplist_get (plist, Qpointer);
30101 if (NILP (pointer))
30102 pointer = Qhand;
30103 help_echo_string = Fplist_get (plist, Qhelp_echo);
30104 if (!NILP (help_echo_string))
30105 {
30106 help_echo_window = window;
30107 help_echo_object = glyph->object;
30108 help_echo_pos = glyph->charpos;
30109 }
30110 }
30111 }
30112 if (NILP (pointer))
30113 pointer = Fplist_get (XCDR (img->spec), QCpointer);
30114 }
30115 }
30116 #endif /* HAVE_WINDOW_SYSTEM */
30117
30118 /* Clear mouse face if X/Y not over text. */
30119 if (glyph == NULL
30120 || area != TEXT_AREA
30121 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
30122 /* Glyph's OBJECT is nil for glyphs inserted by the
30123 display engine for its internal purposes, like truncation
30124 and continuation glyphs and blanks beyond the end of
30125 line's text on text terminals. If we are over such a
30126 glyph, we are not over any text. */
30127 || NILP (glyph->object)
30128 /* R2L rows have a stretch glyph at their front, which
30129 stands for no text, whereas L2R rows have no glyphs at
30130 all beyond the end of text. Treat such stretch glyphs
30131 like we do with NULL glyphs in L2R rows. */
30132 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
30133 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
30134 && glyph->type == STRETCH_GLYPH
30135 && glyph->avoid_cursor_p))
30136 {
30137 if (clear_mouse_face (hlinfo))
30138 cursor = No_Cursor;
30139 #ifdef HAVE_WINDOW_SYSTEM
30140 if (FRAME_WINDOW_P (f) && NILP (pointer))
30141 {
30142 if (area != TEXT_AREA)
30143 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30144 else
30145 pointer = Vvoid_text_area_pointer;
30146 }
30147 #endif
30148 goto set_cursor;
30149 }
30150
30151 pos = glyph->charpos;
30152 object = glyph->object;
30153 if (!STRINGP (object) && !BUFFERP (object))
30154 goto set_cursor;
30155
30156 /* If we get an out-of-range value, return now; avoid an error. */
30157 if (BUFFERP (object) && pos > BUF_Z (b))
30158 goto set_cursor;
30159
30160 /* Make the window's buffer temporarily current for
30161 overlays_at and compute_char_face. */
30162 obuf = current_buffer;
30163 current_buffer = b;
30164 obegv = BEGV;
30165 ozv = ZV;
30166 BEGV = BEG;
30167 ZV = Z;
30168
30169 /* Is this char mouse-active or does it have help-echo? */
30170 position = make_number (pos);
30171
30172 USE_SAFE_ALLOCA;
30173
30174 if (BUFFERP (object))
30175 {
30176 /* Put all the overlays we want in a vector in overlay_vec. */
30177 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
30178 /* Sort overlays into increasing priority order. */
30179 noverlays = sort_overlays (overlay_vec, noverlays, w);
30180 }
30181 else
30182 noverlays = 0;
30183
30184 if (NILP (Vmouse_highlight))
30185 {
30186 clear_mouse_face (hlinfo);
30187 goto check_help_echo;
30188 }
30189
30190 same_region = coords_in_mouse_face_p (w, hpos, vpos);
30191
30192 if (same_region)
30193 cursor = No_Cursor;
30194
30195 /* Check mouse-face highlighting. */
30196 if (! same_region
30197 /* If there exists an overlay with mouse-face overlapping
30198 the one we are currently highlighting, we have to
30199 check if we enter the overlapping overlay, and then
30200 highlight only that. */
30201 || (OVERLAYP (hlinfo->mouse_face_overlay)
30202 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
30203 {
30204 /* Find the highest priority overlay with a mouse-face. */
30205 Lisp_Object overlay = Qnil;
30206 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
30207 {
30208 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
30209 if (!NILP (mouse_face))
30210 overlay = overlay_vec[i];
30211 }
30212
30213 /* If we're highlighting the same overlay as before, there's
30214 no need to do that again. */
30215 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
30216 goto check_help_echo;
30217 hlinfo->mouse_face_overlay = overlay;
30218
30219 /* Clear the display of the old active region, if any. */
30220 if (clear_mouse_face (hlinfo))
30221 cursor = No_Cursor;
30222
30223 /* If no overlay applies, get a text property. */
30224 if (NILP (overlay))
30225 mouse_face = Fget_text_property (position, Qmouse_face, object);
30226
30227 /* Next, compute the bounds of the mouse highlighting and
30228 display it. */
30229 if (!NILP (mouse_face) && STRINGP (object))
30230 {
30231 /* The mouse-highlighting comes from a display string
30232 with a mouse-face. */
30233 Lisp_Object s, e;
30234 ptrdiff_t ignore;
30235
30236 s = Fprevious_single_property_change
30237 (make_number (pos + 1), Qmouse_face, object, Qnil);
30238 e = Fnext_single_property_change
30239 (position, Qmouse_face, object, Qnil);
30240 if (NILP (s))
30241 s = make_number (0);
30242 if (NILP (e))
30243 e = make_number (SCHARS (object));
30244 mouse_face_from_string_pos (w, hlinfo, object,
30245 XINT (s), XINT (e));
30246 hlinfo->mouse_face_past_end = false;
30247 hlinfo->mouse_face_window = window;
30248 hlinfo->mouse_face_face_id
30249 = face_at_string_position (w, object, pos, 0, &ignore,
30250 glyph->face_id, true);
30251 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30252 cursor = No_Cursor;
30253 }
30254 else
30255 {
30256 /* The mouse-highlighting, if any, comes from an overlay
30257 or text property in the buffer. */
30258 Lisp_Object buffer IF_LINT (= Qnil);
30259 Lisp_Object disp_string IF_LINT (= Qnil);
30260
30261 if (STRINGP (object))
30262 {
30263 /* If we are on a display string with no mouse-face,
30264 check if the text under it has one. */
30265 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
30266 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30267 pos = string_buffer_position (object, start);
30268 if (pos > 0)
30269 {
30270 mouse_face = get_char_property_and_overlay
30271 (make_number (pos), Qmouse_face, w->contents, &overlay);
30272 buffer = w->contents;
30273 disp_string = object;
30274 }
30275 }
30276 else
30277 {
30278 buffer = object;
30279 disp_string = Qnil;
30280 }
30281
30282 if (!NILP (mouse_face))
30283 {
30284 Lisp_Object before, after;
30285 Lisp_Object before_string, after_string;
30286 /* To correctly find the limits of mouse highlight
30287 in a bidi-reordered buffer, we must not use the
30288 optimization of limiting the search in
30289 previous-single-property-change and
30290 next-single-property-change, because
30291 rows_from_pos_range needs the real start and end
30292 positions to DTRT in this case. That's because
30293 the first row visible in a window does not
30294 necessarily display the character whose position
30295 is the smallest. */
30296 Lisp_Object lim1
30297 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30298 ? Fmarker_position (w->start)
30299 : Qnil;
30300 Lisp_Object lim2
30301 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30302 ? make_number (BUF_Z (XBUFFER (buffer))
30303 - w->window_end_pos)
30304 : Qnil;
30305
30306 if (NILP (overlay))
30307 {
30308 /* Handle the text property case. */
30309 before = Fprevious_single_property_change
30310 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30311 after = Fnext_single_property_change
30312 (make_number (pos), Qmouse_face, buffer, lim2);
30313 before_string = after_string = Qnil;
30314 }
30315 else
30316 {
30317 /* Handle the overlay case. */
30318 before = Foverlay_start (overlay);
30319 after = Foverlay_end (overlay);
30320 before_string = Foverlay_get (overlay, Qbefore_string);
30321 after_string = Foverlay_get (overlay, Qafter_string);
30322
30323 if (!STRINGP (before_string)) before_string = Qnil;
30324 if (!STRINGP (after_string)) after_string = Qnil;
30325 }
30326
30327 mouse_face_from_buffer_pos (window, hlinfo, pos,
30328 NILP (before)
30329 ? 1
30330 : XFASTINT (before),
30331 NILP (after)
30332 ? BUF_Z (XBUFFER (buffer))
30333 : XFASTINT (after),
30334 before_string, after_string,
30335 disp_string);
30336 cursor = No_Cursor;
30337 }
30338 }
30339 }
30340
30341 check_help_echo:
30342
30343 /* Look for a `help-echo' property. */
30344 if (NILP (help_echo_string)) {
30345 Lisp_Object help, overlay;
30346
30347 /* Check overlays first. */
30348 help = overlay = Qnil;
30349 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30350 {
30351 overlay = overlay_vec[i];
30352 help = Foverlay_get (overlay, Qhelp_echo);
30353 }
30354
30355 if (!NILP (help))
30356 {
30357 help_echo_string = help;
30358 help_echo_window = window;
30359 help_echo_object = overlay;
30360 help_echo_pos = pos;
30361 }
30362 else
30363 {
30364 Lisp_Object obj = glyph->object;
30365 ptrdiff_t charpos = glyph->charpos;
30366
30367 /* Try text properties. */
30368 if (STRINGP (obj)
30369 && charpos >= 0
30370 && charpos < SCHARS (obj))
30371 {
30372 help = Fget_text_property (make_number (charpos),
30373 Qhelp_echo, obj);
30374 if (NILP (help))
30375 {
30376 /* If the string itself doesn't specify a help-echo,
30377 see if the buffer text ``under'' it does. */
30378 struct glyph_row *r
30379 = MATRIX_ROW (w->current_matrix, vpos);
30380 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30381 ptrdiff_t p = string_buffer_position (obj, start);
30382 if (p > 0)
30383 {
30384 help = Fget_char_property (make_number (p),
30385 Qhelp_echo, w->contents);
30386 if (!NILP (help))
30387 {
30388 charpos = p;
30389 obj = w->contents;
30390 }
30391 }
30392 }
30393 }
30394 else if (BUFFERP (obj)
30395 && charpos >= BEGV
30396 && charpos < ZV)
30397 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30398 obj);
30399
30400 if (!NILP (help))
30401 {
30402 help_echo_string = help;
30403 help_echo_window = window;
30404 help_echo_object = obj;
30405 help_echo_pos = charpos;
30406 }
30407 }
30408 }
30409
30410 #ifdef HAVE_WINDOW_SYSTEM
30411 /* Look for a `pointer' property. */
30412 if (FRAME_WINDOW_P (f) && NILP (pointer))
30413 {
30414 /* Check overlays first. */
30415 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30416 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30417
30418 if (NILP (pointer))
30419 {
30420 Lisp_Object obj = glyph->object;
30421 ptrdiff_t charpos = glyph->charpos;
30422
30423 /* Try text properties. */
30424 if (STRINGP (obj)
30425 && charpos >= 0
30426 && charpos < SCHARS (obj))
30427 {
30428 pointer = Fget_text_property (make_number (charpos),
30429 Qpointer, obj);
30430 if (NILP (pointer))
30431 {
30432 /* If the string itself doesn't specify a pointer,
30433 see if the buffer text ``under'' it does. */
30434 struct glyph_row *r
30435 = MATRIX_ROW (w->current_matrix, vpos);
30436 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30437 ptrdiff_t p = string_buffer_position (obj, start);
30438 if (p > 0)
30439 pointer = Fget_char_property (make_number (p),
30440 Qpointer, w->contents);
30441 }
30442 }
30443 else if (BUFFERP (obj)
30444 && charpos >= BEGV
30445 && charpos < ZV)
30446 pointer = Fget_text_property (make_number (charpos),
30447 Qpointer, obj);
30448 }
30449 }
30450 #endif /* HAVE_WINDOW_SYSTEM */
30451
30452 BEGV = obegv;
30453 ZV = ozv;
30454 current_buffer = obuf;
30455 SAFE_FREE ();
30456 }
30457
30458 set_cursor:
30459
30460 #ifdef HAVE_WINDOW_SYSTEM
30461 if (FRAME_WINDOW_P (f))
30462 define_frame_cursor1 (f, cursor, pointer);
30463 #else
30464 /* This is here to prevent a compiler error, about "label at end of
30465 compound statement". */
30466 return;
30467 #endif
30468 }
30469
30470
30471 /* EXPORT for RIF:
30472 Clear any mouse-face on window W. This function is part of the
30473 redisplay interface, and is called from try_window_id and similar
30474 functions to ensure the mouse-highlight is off. */
30475
30476 void
30477 x_clear_window_mouse_face (struct window *w)
30478 {
30479 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30480 Lisp_Object window;
30481
30482 block_input ();
30483 XSETWINDOW (window, w);
30484 if (EQ (window, hlinfo->mouse_face_window))
30485 clear_mouse_face (hlinfo);
30486 unblock_input ();
30487 }
30488
30489
30490 /* EXPORT:
30491 Just discard the mouse face information for frame F, if any.
30492 This is used when the size of F is changed. */
30493
30494 void
30495 cancel_mouse_face (struct frame *f)
30496 {
30497 Lisp_Object window;
30498 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30499
30500 window = hlinfo->mouse_face_window;
30501 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30502 reset_mouse_highlight (hlinfo);
30503 }
30504
30505
30506 \f
30507 /***********************************************************************
30508 Exposure Events
30509 ***********************************************************************/
30510
30511 #ifdef HAVE_WINDOW_SYSTEM
30512
30513 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30514 which intersects rectangle R. R is in window-relative coordinates. */
30515
30516 static void
30517 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30518 enum glyph_row_area area)
30519 {
30520 struct glyph *first = row->glyphs[area];
30521 struct glyph *end = row->glyphs[area] + row->used[area];
30522 struct glyph *last;
30523 int first_x, start_x, x;
30524
30525 if (area == TEXT_AREA && row->fill_line_p)
30526 /* If row extends face to end of line write the whole line. */
30527 draw_glyphs (w, 0, row, area,
30528 0, row->used[area],
30529 DRAW_NORMAL_TEXT, 0);
30530 else
30531 {
30532 /* Set START_X to the window-relative start position for drawing glyphs of
30533 AREA. The first glyph of the text area can be partially visible.
30534 The first glyphs of other areas cannot. */
30535 start_x = window_box_left_offset (w, area);
30536 x = start_x;
30537 if (area == TEXT_AREA)
30538 x += row->x;
30539
30540 /* Find the first glyph that must be redrawn. */
30541 while (first < end
30542 && x + first->pixel_width < r->x)
30543 {
30544 x += first->pixel_width;
30545 ++first;
30546 }
30547
30548 /* Find the last one. */
30549 last = first;
30550 first_x = x;
30551 /* Use a signed int intermediate value to avoid catastrophic
30552 failures due to comparison between signed and unsigned, when
30553 x is negative (can happen for wide images that are hscrolled). */
30554 int r_end = r->x + r->width;
30555 while (last < end && x < r_end)
30556 {
30557 x += last->pixel_width;
30558 ++last;
30559 }
30560
30561 /* Repaint. */
30562 if (last > first)
30563 draw_glyphs (w, first_x - start_x, row, area,
30564 first - row->glyphs[area], last - row->glyphs[area],
30565 DRAW_NORMAL_TEXT, 0);
30566 }
30567 }
30568
30569
30570 /* Redraw the parts of the glyph row ROW on window W intersecting
30571 rectangle R. R is in window-relative coordinates. Value is
30572 true if mouse-face was overwritten. */
30573
30574 static bool
30575 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30576 {
30577 eassert (row->enabled_p);
30578
30579 if (row->mode_line_p || w->pseudo_window_p)
30580 draw_glyphs (w, 0, row, TEXT_AREA,
30581 0, row->used[TEXT_AREA],
30582 DRAW_NORMAL_TEXT, 0);
30583 else
30584 {
30585 if (row->used[LEFT_MARGIN_AREA])
30586 expose_area (w, row, r, LEFT_MARGIN_AREA);
30587 if (row->used[TEXT_AREA])
30588 expose_area (w, row, r, TEXT_AREA);
30589 if (row->used[RIGHT_MARGIN_AREA])
30590 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30591 draw_row_fringe_bitmaps (w, row);
30592 }
30593
30594 return row->mouse_face_p;
30595 }
30596
30597
30598 /* Redraw those parts of glyphs rows during expose event handling that
30599 overlap other rows. Redrawing of an exposed line writes over parts
30600 of lines overlapping that exposed line; this function fixes that.
30601
30602 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30603 row in W's current matrix that is exposed and overlaps other rows.
30604 LAST_OVERLAPPING_ROW is the last such row. */
30605
30606 static void
30607 expose_overlaps (struct window *w,
30608 struct glyph_row *first_overlapping_row,
30609 struct glyph_row *last_overlapping_row,
30610 XRectangle *r)
30611 {
30612 struct glyph_row *row;
30613
30614 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30615 if (row->overlapping_p)
30616 {
30617 eassert (row->enabled_p && !row->mode_line_p);
30618
30619 row->clip = r;
30620 if (row->used[LEFT_MARGIN_AREA])
30621 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30622
30623 if (row->used[TEXT_AREA])
30624 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30625
30626 if (row->used[RIGHT_MARGIN_AREA])
30627 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30628 row->clip = NULL;
30629 }
30630 }
30631
30632
30633 /* Return true if W's cursor intersects rectangle R. */
30634
30635 static bool
30636 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30637 {
30638 XRectangle cr, result;
30639 struct glyph *cursor_glyph;
30640 struct glyph_row *row;
30641
30642 if (w->phys_cursor.vpos >= 0
30643 && w->phys_cursor.vpos < w->current_matrix->nrows
30644 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30645 row->enabled_p)
30646 && row->cursor_in_fringe_p)
30647 {
30648 /* Cursor is in the fringe. */
30649 cr.x = window_box_right_offset (w,
30650 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30651 ? RIGHT_MARGIN_AREA
30652 : TEXT_AREA));
30653 cr.y = row->y;
30654 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30655 cr.height = row->height;
30656 return x_intersect_rectangles (&cr, r, &result);
30657 }
30658
30659 cursor_glyph = get_phys_cursor_glyph (w);
30660 if (cursor_glyph)
30661 {
30662 /* r is relative to W's box, but w->phys_cursor.x is relative
30663 to left edge of W's TEXT area. Adjust it. */
30664 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30665 cr.y = w->phys_cursor.y;
30666 cr.width = cursor_glyph->pixel_width;
30667 cr.height = w->phys_cursor_height;
30668 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30669 I assume the effect is the same -- and this is portable. */
30670 return x_intersect_rectangles (&cr, r, &result);
30671 }
30672 /* If we don't understand the format, pretend we're not in the hot-spot. */
30673 return false;
30674 }
30675
30676
30677 /* EXPORT:
30678 Draw a vertical window border to the right of window W if W doesn't
30679 have vertical scroll bars. */
30680
30681 void
30682 x_draw_vertical_border (struct window *w)
30683 {
30684 struct frame *f = XFRAME (WINDOW_FRAME (w));
30685
30686 /* We could do better, if we knew what type of scroll-bar the adjacent
30687 windows (on either side) have... But we don't :-(
30688 However, I think this works ok. ++KFS 2003-04-25 */
30689
30690 /* Redraw borders between horizontally adjacent windows. Don't
30691 do it for frames with vertical scroll bars because either the
30692 right scroll bar of a window, or the left scroll bar of its
30693 neighbor will suffice as a border. */
30694 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30695 return;
30696
30697 /* Note: It is necessary to redraw both the left and the right
30698 borders, for when only this single window W is being
30699 redisplayed. */
30700 if (!WINDOW_RIGHTMOST_P (w)
30701 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30702 {
30703 int x0, x1, y0, y1;
30704
30705 window_box_edges (w, &x0, &y0, &x1, &y1);
30706 y1 -= 1;
30707
30708 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30709 x1 -= 1;
30710
30711 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30712 }
30713
30714 if (!WINDOW_LEFTMOST_P (w)
30715 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30716 {
30717 int x0, x1, y0, y1;
30718
30719 window_box_edges (w, &x0, &y0, &x1, &y1);
30720 y1 -= 1;
30721
30722 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30723 x0 -= 1;
30724
30725 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30726 }
30727 }
30728
30729
30730 /* Draw window dividers for window W. */
30731
30732 void
30733 x_draw_right_divider (struct window *w)
30734 {
30735 struct frame *f = WINDOW_XFRAME (w);
30736
30737 if (w->mini || w->pseudo_window_p)
30738 return;
30739 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30740 {
30741 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30742 int x1 = WINDOW_RIGHT_EDGE_X (w);
30743 int y0 = WINDOW_TOP_EDGE_Y (w);
30744 /* The bottom divider prevails. */
30745 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30746
30747 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30748 }
30749 }
30750
30751 static void
30752 x_draw_bottom_divider (struct window *w)
30753 {
30754 struct frame *f = XFRAME (WINDOW_FRAME (w));
30755
30756 if (w->mini || w->pseudo_window_p)
30757 return;
30758 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30759 {
30760 int x0 = WINDOW_LEFT_EDGE_X (w);
30761 int x1 = WINDOW_RIGHT_EDGE_X (w);
30762 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30763 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30764
30765 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30766 }
30767 }
30768
30769 /* Redraw the part of window W intersection rectangle FR. Pixel
30770 coordinates in FR are frame-relative. Call this function with
30771 input blocked. Value is true if the exposure overwrites
30772 mouse-face. */
30773
30774 static bool
30775 expose_window (struct window *w, XRectangle *fr)
30776 {
30777 struct frame *f = XFRAME (w->frame);
30778 XRectangle wr, r;
30779 bool mouse_face_overwritten_p = false;
30780
30781 /* If window is not yet fully initialized, do nothing. This can
30782 happen when toolkit scroll bars are used and a window is split.
30783 Reconfiguring the scroll bar will generate an expose for a newly
30784 created window. */
30785 if (w->current_matrix == NULL)
30786 return false;
30787
30788 /* When we're currently updating the window, display and current
30789 matrix usually don't agree. Arrange for a thorough display
30790 later. */
30791 if (w->must_be_updated_p)
30792 {
30793 SET_FRAME_GARBAGED (f);
30794 return false;
30795 }
30796
30797 /* Frame-relative pixel rectangle of W. */
30798 wr.x = WINDOW_LEFT_EDGE_X (w);
30799 wr.y = WINDOW_TOP_EDGE_Y (w);
30800 wr.width = WINDOW_PIXEL_WIDTH (w);
30801 wr.height = WINDOW_PIXEL_HEIGHT (w);
30802
30803 if (x_intersect_rectangles (fr, &wr, &r))
30804 {
30805 int yb = window_text_bottom_y (w);
30806 struct glyph_row *row;
30807 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30808
30809 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30810 r.x, r.y, r.width, r.height));
30811
30812 /* Convert to window coordinates. */
30813 r.x -= WINDOW_LEFT_EDGE_X (w);
30814 r.y -= WINDOW_TOP_EDGE_Y (w);
30815
30816 /* Turn off the cursor. */
30817 bool cursor_cleared_p = (!w->pseudo_window_p
30818 && phys_cursor_in_rect_p (w, &r));
30819 if (cursor_cleared_p)
30820 x_clear_cursor (w);
30821
30822 /* If the row containing the cursor extends face to end of line,
30823 then expose_area might overwrite the cursor outside the
30824 rectangle and thus notice_overwritten_cursor might clear
30825 w->phys_cursor_on_p. We remember the original value and
30826 check later if it is changed. */
30827 bool phys_cursor_on_p = w->phys_cursor_on_p;
30828
30829 /* Use a signed int intermediate value to avoid catastrophic
30830 failures due to comparison between signed and unsigned, when
30831 y0 or y1 is negative (can happen for tall images). */
30832 int r_bottom = r.y + r.height;
30833
30834 /* Update lines intersecting rectangle R. */
30835 first_overlapping_row = last_overlapping_row = NULL;
30836 for (row = w->current_matrix->rows;
30837 row->enabled_p;
30838 ++row)
30839 {
30840 int y0 = row->y;
30841 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30842
30843 if ((y0 >= r.y && y0 < r_bottom)
30844 || (y1 > r.y && y1 < r_bottom)
30845 || (r.y >= y0 && r.y < y1)
30846 || (r_bottom > y0 && r_bottom < y1))
30847 {
30848 /* A header line may be overlapping, but there is no need
30849 to fix overlapping areas for them. KFS 2005-02-12 */
30850 if (row->overlapping_p && !row->mode_line_p)
30851 {
30852 if (first_overlapping_row == NULL)
30853 first_overlapping_row = row;
30854 last_overlapping_row = row;
30855 }
30856
30857 row->clip = fr;
30858 if (expose_line (w, row, &r))
30859 mouse_face_overwritten_p = true;
30860 row->clip = NULL;
30861 }
30862 else if (row->overlapping_p)
30863 {
30864 /* We must redraw a row overlapping the exposed area. */
30865 if (y0 < r.y
30866 ? y0 + row->phys_height > r.y
30867 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30868 {
30869 if (first_overlapping_row == NULL)
30870 first_overlapping_row = row;
30871 last_overlapping_row = row;
30872 }
30873 }
30874
30875 if (y1 >= yb)
30876 break;
30877 }
30878
30879 /* Display the mode line if there is one. */
30880 if (WINDOW_WANTS_MODELINE_P (w)
30881 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30882 row->enabled_p)
30883 && row->y < r_bottom)
30884 {
30885 if (expose_line (w, row, &r))
30886 mouse_face_overwritten_p = true;
30887 }
30888
30889 if (!w->pseudo_window_p)
30890 {
30891 /* Fix the display of overlapping rows. */
30892 if (first_overlapping_row)
30893 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30894 fr);
30895
30896 /* Draw border between windows. */
30897 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30898 x_draw_right_divider (w);
30899 else
30900 x_draw_vertical_border (w);
30901
30902 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30903 x_draw_bottom_divider (w);
30904
30905 /* Turn the cursor on again. */
30906 if (cursor_cleared_p
30907 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30908 update_window_cursor (w, true);
30909 }
30910 }
30911
30912 return mouse_face_overwritten_p;
30913 }
30914
30915
30916
30917 /* Redraw (parts) of all windows in the window tree rooted at W that
30918 intersect R. R contains frame pixel coordinates. Value is
30919 true if the exposure overwrites mouse-face. */
30920
30921 static bool
30922 expose_window_tree (struct window *w, XRectangle *r)
30923 {
30924 struct frame *f = XFRAME (w->frame);
30925 bool mouse_face_overwritten_p = false;
30926
30927 while (w && !FRAME_GARBAGED_P (f))
30928 {
30929 mouse_face_overwritten_p
30930 |= (WINDOWP (w->contents)
30931 ? expose_window_tree (XWINDOW (w->contents), r)
30932 : expose_window (w, r));
30933
30934 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30935 }
30936
30937 return mouse_face_overwritten_p;
30938 }
30939
30940
30941 /* EXPORT:
30942 Redisplay an exposed area of frame F. X and Y are the upper-left
30943 corner of the exposed rectangle. W and H are width and height of
30944 the exposed area. All are pixel values. W or H zero means redraw
30945 the entire frame. */
30946
30947 void
30948 expose_frame (struct frame *f, int x, int y, int w, int h)
30949 {
30950 XRectangle r;
30951 bool mouse_face_overwritten_p = false;
30952
30953 TRACE ((stderr, "expose_frame "));
30954
30955 /* No need to redraw if frame will be redrawn soon. */
30956 if (FRAME_GARBAGED_P (f))
30957 {
30958 TRACE ((stderr, " garbaged\n"));
30959 return;
30960 }
30961
30962 /* If basic faces haven't been realized yet, there is no point in
30963 trying to redraw anything. This can happen when we get an expose
30964 event while Emacs is starting, e.g. by moving another window. */
30965 if (FRAME_FACE_CACHE (f) == NULL
30966 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30967 {
30968 TRACE ((stderr, " no faces\n"));
30969 return;
30970 }
30971
30972 if (w == 0 || h == 0)
30973 {
30974 r.x = r.y = 0;
30975 r.width = FRAME_TEXT_WIDTH (f);
30976 r.height = FRAME_TEXT_HEIGHT (f);
30977 }
30978 else
30979 {
30980 r.x = x;
30981 r.y = y;
30982 r.width = w;
30983 r.height = h;
30984 }
30985
30986 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30987 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30988
30989 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30990 if (WINDOWP (f->tool_bar_window))
30991 mouse_face_overwritten_p
30992 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30993 #endif
30994
30995 #ifdef HAVE_X_WINDOWS
30996 #ifndef MSDOS
30997 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30998 if (WINDOWP (f->menu_bar_window))
30999 mouse_face_overwritten_p
31000 |= expose_window (XWINDOW (f->menu_bar_window), &r);
31001 #endif /* not USE_X_TOOLKIT and not USE_GTK */
31002 #endif
31003 #endif
31004
31005 /* Some window managers support a focus-follows-mouse style with
31006 delayed raising of frames. Imagine a partially obscured frame,
31007 and moving the mouse into partially obscured mouse-face on that
31008 frame. The visible part of the mouse-face will be highlighted,
31009 then the WM raises the obscured frame. With at least one WM, KDE
31010 2.1, Emacs is not getting any event for the raising of the frame
31011 (even tried with SubstructureRedirectMask), only Expose events.
31012 These expose events will draw text normally, i.e. not
31013 highlighted. Which means we must redo the highlight here.
31014 Subsume it under ``we love X''. --gerd 2001-08-15 */
31015 /* Included in Windows version because Windows most likely does not
31016 do the right thing if any third party tool offers
31017 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
31018 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
31019 {
31020 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31021 if (f == hlinfo->mouse_face_mouse_frame)
31022 {
31023 int mouse_x = hlinfo->mouse_face_mouse_x;
31024 int mouse_y = hlinfo->mouse_face_mouse_y;
31025 clear_mouse_face (hlinfo);
31026 note_mouse_highlight (f, mouse_x, mouse_y);
31027 }
31028 }
31029 }
31030
31031
31032 /* EXPORT:
31033 Determine the intersection of two rectangles R1 and R2. Return
31034 the intersection in *RESULT. Value is true if RESULT is not
31035 empty. */
31036
31037 bool
31038 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
31039 {
31040 XRectangle *left, *right;
31041 XRectangle *upper, *lower;
31042 bool intersection_p = false;
31043
31044 /* Rearrange so that R1 is the left-most rectangle. */
31045 if (r1->x < r2->x)
31046 left = r1, right = r2;
31047 else
31048 left = r2, right = r1;
31049
31050 /* X0 of the intersection is right.x0, if this is inside R1,
31051 otherwise there is no intersection. */
31052 if (right->x <= left->x + left->width)
31053 {
31054 result->x = right->x;
31055
31056 /* The right end of the intersection is the minimum of
31057 the right ends of left and right. */
31058 result->width = (min (left->x + left->width, right->x + right->width)
31059 - result->x);
31060
31061 /* Same game for Y. */
31062 if (r1->y < r2->y)
31063 upper = r1, lower = r2;
31064 else
31065 upper = r2, lower = r1;
31066
31067 /* The upper end of the intersection is lower.y0, if this is inside
31068 of upper. Otherwise, there is no intersection. */
31069 if (lower->y <= upper->y + upper->height)
31070 {
31071 result->y = lower->y;
31072
31073 /* The lower end of the intersection is the minimum of the lower
31074 ends of upper and lower. */
31075 result->height = (min (lower->y + lower->height,
31076 upper->y + upper->height)
31077 - result->y);
31078 intersection_p = true;
31079 }
31080 }
31081
31082 return intersection_p;
31083 }
31084
31085 #endif /* HAVE_WINDOW_SYSTEM */
31086
31087 \f
31088 /***********************************************************************
31089 Initialization
31090 ***********************************************************************/
31091
31092 void
31093 syms_of_xdisp (void)
31094 {
31095 Vwith_echo_area_save_vector = Qnil;
31096 staticpro (&Vwith_echo_area_save_vector);
31097
31098 Vmessage_stack = Qnil;
31099 staticpro (&Vmessage_stack);
31100
31101 /* Non-nil means don't actually do any redisplay. */
31102 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
31103
31104 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
31105
31106 DEFVAR_BOOL("inhibit-message", inhibit_message,
31107 doc: /* Non-nil means calls to `message' are not displayed.
31108 They are still logged to the *Messages* buffer. */);
31109 inhibit_message = 0;
31110
31111 message_dolog_marker1 = Fmake_marker ();
31112 staticpro (&message_dolog_marker1);
31113 message_dolog_marker2 = Fmake_marker ();
31114 staticpro (&message_dolog_marker2);
31115 message_dolog_marker3 = Fmake_marker ();
31116 staticpro (&message_dolog_marker3);
31117
31118 #ifdef GLYPH_DEBUG
31119 defsubr (&Sdump_frame_glyph_matrix);
31120 defsubr (&Sdump_glyph_matrix);
31121 defsubr (&Sdump_glyph_row);
31122 defsubr (&Sdump_tool_bar_row);
31123 defsubr (&Strace_redisplay);
31124 defsubr (&Strace_to_stderr);
31125 #endif
31126 #ifdef HAVE_WINDOW_SYSTEM
31127 defsubr (&Stool_bar_height);
31128 defsubr (&Slookup_image_map);
31129 #endif
31130 defsubr (&Sline_pixel_height);
31131 defsubr (&Sformat_mode_line);
31132 defsubr (&Sinvisible_p);
31133 defsubr (&Scurrent_bidi_paragraph_direction);
31134 defsubr (&Swindow_text_pixel_size);
31135 defsubr (&Smove_point_visually);
31136 defsubr (&Sbidi_find_overridden_directionality);
31137
31138 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
31139 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
31140 DEFSYM (Qoverriding_local_map, "overriding-local-map");
31141 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
31142 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
31143 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
31144 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
31145 DEFSYM (Qeval, "eval");
31146 DEFSYM (QCdata, ":data");
31147
31148 /* Names of text properties relevant for redisplay. */
31149 DEFSYM (Qdisplay, "display");
31150 DEFSYM (Qspace_width, "space-width");
31151 DEFSYM (Qraise, "raise");
31152 DEFSYM (Qslice, "slice");
31153 DEFSYM (Qspace, "space");
31154 DEFSYM (Qmargin, "margin");
31155 DEFSYM (Qpointer, "pointer");
31156 DEFSYM (Qleft_margin, "left-margin");
31157 DEFSYM (Qright_margin, "right-margin");
31158 DEFSYM (Qcenter, "center");
31159 DEFSYM (Qline_height, "line-height");
31160 DEFSYM (QCalign_to, ":align-to");
31161 DEFSYM (QCrelative_width, ":relative-width");
31162 DEFSYM (QCrelative_height, ":relative-height");
31163 DEFSYM (QCeval, ":eval");
31164 DEFSYM (QCpropertize, ":propertize");
31165 DEFSYM (QCfile, ":file");
31166 DEFSYM (Qfontified, "fontified");
31167 DEFSYM (Qfontification_functions, "fontification-functions");
31168
31169 /* Name of the face used to highlight trailing whitespace. */
31170 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
31171
31172 /* Name and number of the face used to highlight escape glyphs. */
31173 DEFSYM (Qescape_glyph, "escape-glyph");
31174
31175 /* Name and number of the face used to highlight non-breaking spaces. */
31176 DEFSYM (Qnobreak_space, "nobreak-space");
31177
31178 /* The symbol 'image' which is the car of the lists used to represent
31179 images in Lisp. Also a tool bar style. */
31180 DEFSYM (Qimage, "image");
31181
31182 /* Tool bar styles. */
31183 DEFSYM (Qtext, "text");
31184 DEFSYM (Qboth, "both");
31185 DEFSYM (Qboth_horiz, "both-horiz");
31186 DEFSYM (Qtext_image_horiz, "text-image-horiz");
31187
31188 /* The image map types. */
31189 DEFSYM (QCmap, ":map");
31190 DEFSYM (QCpointer, ":pointer");
31191 DEFSYM (Qrect, "rect");
31192 DEFSYM (Qcircle, "circle");
31193 DEFSYM (Qpoly, "poly");
31194
31195 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
31196
31197 DEFSYM (Qgrow_only, "grow-only");
31198 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
31199 DEFSYM (Qposition, "position");
31200 DEFSYM (Qbuffer_position, "buffer-position");
31201 DEFSYM (Qobject, "object");
31202
31203 /* Cursor shapes. */
31204 DEFSYM (Qbar, "bar");
31205 DEFSYM (Qhbar, "hbar");
31206 DEFSYM (Qbox, "box");
31207 DEFSYM (Qhollow, "hollow");
31208
31209 /* Pointer shapes. */
31210 DEFSYM (Qhand, "hand");
31211 DEFSYM (Qarrow, "arrow");
31212 /* also Qtext */
31213
31214 DEFSYM (Qdragging, "dragging");
31215
31216 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
31217
31218 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
31219 staticpro (&list_of_error);
31220
31221 /* Values of those variables at last redisplay are stored as
31222 properties on 'overlay-arrow-position' symbol. However, if
31223 Voverlay_arrow_position is a marker, last-arrow-position is its
31224 numerical position. */
31225 DEFSYM (Qlast_arrow_position, "last-arrow-position");
31226 DEFSYM (Qlast_arrow_string, "last-arrow-string");
31227
31228 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
31229 properties on a symbol in overlay-arrow-variable-list. */
31230 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
31231 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
31232
31233 echo_buffer[0] = echo_buffer[1] = Qnil;
31234 staticpro (&echo_buffer[0]);
31235 staticpro (&echo_buffer[1]);
31236
31237 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
31238 staticpro (&echo_area_buffer[0]);
31239 staticpro (&echo_area_buffer[1]);
31240
31241 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
31242 staticpro (&Vmessages_buffer_name);
31243
31244 mode_line_proptrans_alist = Qnil;
31245 staticpro (&mode_line_proptrans_alist);
31246 mode_line_string_list = Qnil;
31247 staticpro (&mode_line_string_list);
31248 mode_line_string_face = Qnil;
31249 staticpro (&mode_line_string_face);
31250 mode_line_string_face_prop = Qnil;
31251 staticpro (&mode_line_string_face_prop);
31252 Vmode_line_unwind_vector = Qnil;
31253 staticpro (&Vmode_line_unwind_vector);
31254
31255 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
31256
31257 help_echo_string = Qnil;
31258 staticpro (&help_echo_string);
31259 help_echo_object = Qnil;
31260 staticpro (&help_echo_object);
31261 help_echo_window = Qnil;
31262 staticpro (&help_echo_window);
31263 previous_help_echo_string = Qnil;
31264 staticpro (&previous_help_echo_string);
31265 help_echo_pos = -1;
31266
31267 DEFSYM (Qright_to_left, "right-to-left");
31268 DEFSYM (Qleft_to_right, "left-to-right");
31269 defsubr (&Sbidi_resolved_levels);
31270
31271 #ifdef HAVE_WINDOW_SYSTEM
31272 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
31273 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
31274 For example, if a block cursor is over a tab, it will be drawn as
31275 wide as that tab on the display. */);
31276 x_stretch_cursor_p = 0;
31277 #endif
31278
31279 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
31280 doc: /* Non-nil means highlight trailing whitespace.
31281 The face used for trailing whitespace is `trailing-whitespace'. */);
31282 Vshow_trailing_whitespace = Qnil;
31283
31284 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
31285 doc: /* Control highlighting of non-ASCII space and hyphen chars.
31286 If the value is t, Emacs highlights non-ASCII chars which have the
31287 same appearance as an ASCII space or hyphen, using the `nobreak-space'
31288 or `escape-glyph' face respectively.
31289
31290 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
31291 U+2011 (non-breaking hyphen) are affected.
31292
31293 Any other non-nil value means to display these characters as a escape
31294 glyph followed by an ordinary space or hyphen.
31295
31296 A value of nil means no special handling of these characters. */);
31297 Vnobreak_char_display = Qt;
31298
31299 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
31300 doc: /* The pointer shape to show in void text areas.
31301 A value of nil means to show the text pointer. Other options are
31302 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
31303 `hourglass'. */);
31304 Vvoid_text_area_pointer = Qarrow;
31305
31306 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
31307 doc: /* Non-nil means don't actually do any redisplay.
31308 This is used for internal purposes. */);
31309 Vinhibit_redisplay = Qnil;
31310
31311 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
31312 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
31313 Vglobal_mode_string = Qnil;
31314
31315 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31316 doc: /* Marker for where to display an arrow on top of the buffer text.
31317 This must be the beginning of a line in order to work.
31318 See also `overlay-arrow-string'. */);
31319 Voverlay_arrow_position = Qnil;
31320
31321 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31322 doc: /* String to display as an arrow in non-window frames.
31323 See also `overlay-arrow-position'. */);
31324 Voverlay_arrow_string = build_pure_c_string ("=>");
31325
31326 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31327 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31328 The symbols on this list are examined during redisplay to determine
31329 where to display overlay arrows. */);
31330 Voverlay_arrow_variable_list
31331 = list1 (intern_c_string ("overlay-arrow-position"));
31332
31333 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31334 doc: /* The number of lines to try scrolling a window by when point moves out.
31335 If that fails to bring point back on frame, point is centered instead.
31336 If this is zero, point is always centered after it moves off frame.
31337 If you want scrolling to always be a line at a time, you should set
31338 `scroll-conservatively' to a large value rather than set this to 1. */);
31339
31340 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31341 doc: /* Scroll up to this many lines, to bring point back on screen.
31342 If point moves off-screen, redisplay will scroll by up to
31343 `scroll-conservatively' lines in order to bring point just barely
31344 onto the screen again. If that cannot be done, then redisplay
31345 recenters point as usual.
31346
31347 If the value is greater than 100, redisplay will never recenter point,
31348 but will always scroll just enough text to bring point into view, even
31349 if you move far away.
31350
31351 A value of zero means always recenter point if it moves off screen. */);
31352 scroll_conservatively = 0;
31353
31354 DEFVAR_INT ("scroll-margin", scroll_margin,
31355 doc: /* Number of lines of margin at the top and bottom of a window.
31356 Recenter the window whenever point gets within this many lines
31357 of the top or bottom of the window. */);
31358 scroll_margin = 0;
31359
31360 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31361 doc: /* Pixels per inch value for non-window system displays.
31362 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31363 Vdisplay_pixels_per_inch = make_float (72.0);
31364
31365 #ifdef GLYPH_DEBUG
31366 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31367 #endif
31368
31369 DEFVAR_LISP ("truncate-partial-width-windows",
31370 Vtruncate_partial_width_windows,
31371 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31372 For an integer value, truncate lines in each window narrower than the
31373 full frame width, provided the window width is less than that integer;
31374 otherwise, respect the value of `truncate-lines'.
31375
31376 For any other non-nil value, truncate lines in all windows that do
31377 not span the full frame width.
31378
31379 A value of nil means to respect the value of `truncate-lines'.
31380
31381 If `word-wrap' is enabled, you might want to reduce this. */);
31382 Vtruncate_partial_width_windows = make_number (50);
31383
31384 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31385 doc: /* Maximum buffer size for which line number should be displayed.
31386 If the buffer is bigger than this, the line number does not appear
31387 in the mode line. A value of nil means no limit. */);
31388 Vline_number_display_limit = Qnil;
31389
31390 DEFVAR_INT ("line-number-display-limit-width",
31391 line_number_display_limit_width,
31392 doc: /* Maximum line width (in characters) for line number display.
31393 If the average length of the lines near point is bigger than this, then the
31394 line number may be omitted from the mode line. */);
31395 line_number_display_limit_width = 200;
31396
31397 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31398 doc: /* Non-nil means highlight region even in nonselected windows. */);
31399 highlight_nonselected_windows = false;
31400
31401 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31402 doc: /* Non-nil if more than one frame is visible on this display.
31403 Minibuffer-only frames don't count, but iconified frames do.
31404 This variable is not guaranteed to be accurate except while processing
31405 `frame-title-format' and `icon-title-format'. */);
31406
31407 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31408 doc: /* Template for displaying the title bar of visible frames.
31409 (Assuming the window manager supports this feature.)
31410
31411 This variable has the same structure as `mode-line-format', except that
31412 the %c and %l constructs are ignored. It is used only on frames for
31413 which no explicit name has been set (see `modify-frame-parameters'). */);
31414
31415 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31416 doc: /* Template for displaying the title bar of an iconified frame.
31417 (Assuming the window manager supports this feature.)
31418 This variable has the same structure as `mode-line-format' (which see),
31419 and is used only on frames for which no explicit name has been set
31420 (see `modify-frame-parameters'). */);
31421 Vicon_title_format
31422 = Vframe_title_format
31423 = listn (CONSTYPE_PURE, 3,
31424 intern_c_string ("multiple-frames"),
31425 build_pure_c_string ("%b"),
31426 listn (CONSTYPE_PURE, 4,
31427 empty_unibyte_string,
31428 intern_c_string ("invocation-name"),
31429 build_pure_c_string ("@"),
31430 intern_c_string ("system-name")));
31431
31432 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31433 doc: /* Maximum number of lines to keep in the message log buffer.
31434 If nil, disable message logging. If t, log messages but don't truncate
31435 the buffer when it becomes large. */);
31436 Vmessage_log_max = make_number (1000);
31437
31438 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
31439 doc: /* Functions called during redisplay, if window sizes have changed.
31440 The value should be a list of functions that take one argument.
31441 During the first part of redisplay, for each frame, if any of its windows
31442 have changed size since the last redisplay, or have been split or deleted,
31443 all the functions in the list are called, with the frame as argument.
31444 If redisplay decides to resize the minibuffer window, it calls these
31445 functions on behalf of that as well. */);
31446 Vwindow_size_change_functions = Qnil;
31447
31448 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31449 doc: /* List of functions to call before redisplaying a window with scrolling.
31450 Each function is called with two arguments, the window and its new
31451 display-start position.
31452 These functions are called whenever the `window-start' marker is modified,
31453 either to point into another buffer (e.g. via `set-window-buffer') or another
31454 place in the same buffer.
31455 Note that the value of `window-end' is not valid when these functions are
31456 called.
31457
31458 Warning: Do not use this feature to alter the way the window
31459 is scrolled. It is not designed for that, and such use probably won't
31460 work. */);
31461 Vwindow_scroll_functions = Qnil;
31462
31463 DEFVAR_LISP ("window-text-change-functions",
31464 Vwindow_text_change_functions,
31465 doc: /* Functions to call in redisplay when text in the window might change. */);
31466 Vwindow_text_change_functions = Qnil;
31467
31468 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31469 doc: /* Functions called when redisplay of a window reaches the end trigger.
31470 Each function is called with two arguments, the window and the end trigger value.
31471 See `set-window-redisplay-end-trigger'. */);
31472 Vredisplay_end_trigger_functions = Qnil;
31473
31474 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31475 doc: /* Non-nil means autoselect window with mouse pointer.
31476 If nil, do not autoselect windows.
31477 A positive number means delay autoselection by that many seconds: a
31478 window is autoselected only after the mouse has remained in that
31479 window for the duration of the delay.
31480 A negative number has a similar effect, but causes windows to be
31481 autoselected only after the mouse has stopped moving. (Because of
31482 the way Emacs compares mouse events, you will occasionally wait twice
31483 that time before the window gets selected.)
31484 Any other value means to autoselect window instantaneously when the
31485 mouse pointer enters it.
31486
31487 Autoselection selects the minibuffer only if it is active, and never
31488 unselects the minibuffer if it is active.
31489
31490 When customizing this variable make sure that the actual value of
31491 `focus-follows-mouse' matches the behavior of your window manager. */);
31492 Vmouse_autoselect_window = Qnil;
31493
31494 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31495 doc: /* Non-nil means automatically resize tool-bars.
31496 This dynamically changes the tool-bar's height to the minimum height
31497 that is needed to make all tool-bar items visible.
31498 If value is `grow-only', the tool-bar's height is only increased
31499 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31500 Vauto_resize_tool_bars = Qt;
31501
31502 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31503 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31504 auto_raise_tool_bar_buttons_p = true;
31505
31506 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31507 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31508 make_cursor_line_fully_visible_p = true;
31509
31510 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31511 doc: /* Border below tool-bar in pixels.
31512 If an integer, use it as the height of the border.
31513 If it is one of `internal-border-width' or `border-width', use the
31514 value of the corresponding frame parameter.
31515 Otherwise, no border is added below the tool-bar. */);
31516 Vtool_bar_border = Qinternal_border_width;
31517
31518 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31519 doc: /* Margin around tool-bar buttons in pixels.
31520 If an integer, use that for both horizontal and vertical margins.
31521 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31522 HORZ specifying the horizontal margin, and VERT specifying the
31523 vertical margin. */);
31524 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31525
31526 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31527 doc: /* Relief thickness of tool-bar buttons. */);
31528 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31529
31530 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31531 doc: /* Tool bar style to use.
31532 It can be one of
31533 image - show images only
31534 text - show text only
31535 both - show both, text below image
31536 both-horiz - show text to the right of the image
31537 text-image-horiz - show text to the left of the image
31538 any other - use system default or image if no system default.
31539
31540 This variable only affects the GTK+ toolkit version of Emacs. */);
31541 Vtool_bar_style = Qnil;
31542
31543 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31544 doc: /* Maximum number of characters a label can have to be shown.
31545 The tool bar style must also show labels for this to have any effect, see
31546 `tool-bar-style'. */);
31547 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31548
31549 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31550 doc: /* List of functions to call to fontify regions of text.
31551 Each function is called with one argument POS. Functions must
31552 fontify a region starting at POS in the current buffer, and give
31553 fontified regions the property `fontified'. */);
31554 Vfontification_functions = Qnil;
31555 Fmake_variable_buffer_local (Qfontification_functions);
31556
31557 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31558 unibyte_display_via_language_environment,
31559 doc: /* Non-nil means display unibyte text according to language environment.
31560 Specifically, this means that raw bytes in the range 160-255 decimal
31561 are displayed by converting them to the equivalent multibyte characters
31562 according to the current language environment. As a result, they are
31563 displayed according to the current fontset.
31564
31565 Note that this variable affects only how these bytes are displayed,
31566 but does not change the fact they are interpreted as raw bytes. */);
31567 unibyte_display_via_language_environment = false;
31568
31569 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31570 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31571 If a float, it specifies a fraction of the mini-window frame's height.
31572 If an integer, it specifies a number of lines. */);
31573 Vmax_mini_window_height = make_float (0.25);
31574
31575 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31576 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31577 A value of nil means don't automatically resize mini-windows.
31578 A value of t means resize them to fit the text displayed in them.
31579 A value of `grow-only', the default, means let mini-windows grow only;
31580 they return to their normal size when the minibuffer is closed, or the
31581 echo area becomes empty. */);
31582 Vresize_mini_windows = Qgrow_only;
31583
31584 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31585 doc: /* Alist specifying how to blink the cursor off.
31586 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31587 `cursor-type' frame-parameter or variable equals ON-STATE,
31588 comparing using `equal', Emacs uses OFF-STATE to specify
31589 how to blink it off. ON-STATE and OFF-STATE are values for
31590 the `cursor-type' frame parameter.
31591
31592 If a frame's ON-STATE has no entry in this list,
31593 the frame's other specifications determine how to blink the cursor off. */);
31594 Vblink_cursor_alist = Qnil;
31595
31596 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31597 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31598 If non-nil, windows are automatically scrolled horizontally to make
31599 point visible. */);
31600 automatic_hscrolling_p = true;
31601 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31602
31603 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31604 doc: /* How many columns away from the window edge point is allowed to get
31605 before automatic hscrolling will horizontally scroll the window. */);
31606 hscroll_margin = 5;
31607
31608 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31609 doc: /* How many columns to scroll the window when point gets too close to the edge.
31610 When point is less than `hscroll-margin' columns from the window
31611 edge, automatic hscrolling will scroll the window by the amount of columns
31612 determined by this variable. If its value is a positive integer, scroll that
31613 many columns. If it's a positive floating-point number, it specifies the
31614 fraction of the window's width to scroll. If it's nil or zero, point will be
31615 centered horizontally after the scroll. Any other value, including negative
31616 numbers, are treated as if the value were zero.
31617
31618 Automatic hscrolling always moves point outside the scroll margin, so if
31619 point was more than scroll step columns inside the margin, the window will
31620 scroll more than the value given by the scroll step.
31621
31622 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31623 and `scroll-right' overrides this variable's effect. */);
31624 Vhscroll_step = make_number (0);
31625
31626 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31627 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31628 Bind this around calls to `message' to let it take effect. */);
31629 message_truncate_lines = false;
31630
31631 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31632 doc: /* Normal hook run to update the menu bar definitions.
31633 Redisplay runs this hook before it redisplays the menu bar.
31634 This is used to update menus such as Buffers, whose contents depend on
31635 various data. */);
31636 Vmenu_bar_update_hook = Qnil;
31637
31638 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31639 doc: /* Frame for which we are updating a menu.
31640 The enable predicate for a menu binding should check this variable. */);
31641 Vmenu_updating_frame = Qnil;
31642
31643 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31644 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31645 inhibit_menubar_update = false;
31646
31647 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31648 doc: /* Prefix prepended to all continuation lines at display time.
31649 The value may be a string, an image, or a stretch-glyph; it is
31650 interpreted in the same way as the value of a `display' text property.
31651
31652 This variable is overridden by any `wrap-prefix' text or overlay
31653 property.
31654
31655 To add a prefix to non-continuation lines, use `line-prefix'. */);
31656 Vwrap_prefix = Qnil;
31657 DEFSYM (Qwrap_prefix, "wrap-prefix");
31658 Fmake_variable_buffer_local (Qwrap_prefix);
31659
31660 DEFVAR_LISP ("line-prefix", Vline_prefix,
31661 doc: /* Prefix prepended to all non-continuation lines at display time.
31662 The value may be a string, an image, or a stretch-glyph; it is
31663 interpreted in the same way as the value of a `display' text property.
31664
31665 This variable is overridden by any `line-prefix' text or overlay
31666 property.
31667
31668 To add a prefix to continuation lines, use `wrap-prefix'. */);
31669 Vline_prefix = Qnil;
31670 DEFSYM (Qline_prefix, "line-prefix");
31671 Fmake_variable_buffer_local (Qline_prefix);
31672
31673 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31674 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31675 inhibit_eval_during_redisplay = false;
31676
31677 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31678 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31679 inhibit_free_realized_faces = false;
31680
31681 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31682 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31683 Intended for use during debugging and for testing bidi display;
31684 see biditest.el in the test suite. */);
31685 inhibit_bidi_mirroring = false;
31686
31687 #ifdef GLYPH_DEBUG
31688 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31689 doc: /* Inhibit try_window_id display optimization. */);
31690 inhibit_try_window_id = false;
31691
31692 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31693 doc: /* Inhibit try_window_reusing display optimization. */);
31694 inhibit_try_window_reusing = false;
31695
31696 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31697 doc: /* Inhibit try_cursor_movement display optimization. */);
31698 inhibit_try_cursor_movement = false;
31699 #endif /* GLYPH_DEBUG */
31700
31701 DEFVAR_INT ("overline-margin", overline_margin,
31702 doc: /* Space between overline and text, in pixels.
31703 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31704 margin to the character height. */);
31705 overline_margin = 2;
31706
31707 DEFVAR_INT ("underline-minimum-offset",
31708 underline_minimum_offset,
31709 doc: /* Minimum distance between baseline and underline.
31710 This can improve legibility of underlined text at small font sizes,
31711 particularly when using variable `x-use-underline-position-properties'
31712 with fonts that specify an UNDERLINE_POSITION relatively close to the
31713 baseline. The default value is 1. */);
31714 underline_minimum_offset = 1;
31715
31716 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31717 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31718 This feature only works when on a window system that can change
31719 cursor shapes. */);
31720 display_hourglass_p = true;
31721
31722 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31723 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31724 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31725
31726 #ifdef HAVE_WINDOW_SYSTEM
31727 hourglass_atimer = NULL;
31728 hourglass_shown_p = false;
31729 #endif /* HAVE_WINDOW_SYSTEM */
31730
31731 /* Name of the face used to display glyphless characters. */
31732 DEFSYM (Qglyphless_char, "glyphless-char");
31733
31734 /* Method symbols for Vglyphless_char_display. */
31735 DEFSYM (Qhex_code, "hex-code");
31736 DEFSYM (Qempty_box, "empty-box");
31737 DEFSYM (Qthin_space, "thin-space");
31738 DEFSYM (Qzero_width, "zero-width");
31739
31740 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31741 doc: /* Function run just before redisplay.
31742 It is called with one argument, which is the set of windows that are to
31743 be redisplayed. This set can be nil (meaning, only the selected window),
31744 or t (meaning all windows). */);
31745 Vpre_redisplay_function = intern ("ignore");
31746
31747 /* Symbol for the purpose of Vglyphless_char_display. */
31748 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31749 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31750
31751 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31752 doc: /* Char-table defining glyphless characters.
31753 Each element, if non-nil, should be one of the following:
31754 an ASCII acronym string: display this string in a box
31755 `hex-code': display the hexadecimal code of a character in a box
31756 `empty-box': display as an empty box
31757 `thin-space': display as 1-pixel width space
31758 `zero-width': don't display
31759 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31760 display method for graphical terminals and text terminals respectively.
31761 GRAPHICAL and TEXT should each have one of the values listed above.
31762
31763 The char-table has one extra slot to control the display of a character for
31764 which no font is found. This slot only takes effect on graphical terminals.
31765 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31766 `thin-space'. The default is `empty-box'.
31767
31768 If a character has a non-nil entry in an active display table, the
31769 display table takes effect; in this case, Emacs does not consult
31770 `glyphless-char-display' at all. */);
31771 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31772 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31773 Qempty_box);
31774
31775 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31776 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31777 Vdebug_on_message = Qnil;
31778
31779 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31780 doc: /* */);
31781 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
31782
31783 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31784 doc: /* */);
31785 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
31786
31787 DEFVAR_LISP ("redisplay--variables", Vredisplay__variables,
31788 doc: /* A hash-table of variables changing which triggers a thorough redisplay. */);
31789 Vredisplay__variables = Qnil;
31790 }
31791
31792
31793 /* Initialize this module when Emacs starts. */
31794
31795 void
31796 init_xdisp (void)
31797 {
31798 CHARPOS (this_line_start_pos) = 0;
31799
31800 if (!noninteractive)
31801 {
31802 struct window *m = XWINDOW (minibuf_window);
31803 Lisp_Object frame = m->frame;
31804 struct frame *f = XFRAME (frame);
31805 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31806 struct window *r = XWINDOW (root);
31807 int i;
31808
31809 echo_area_window = minibuf_window;
31810
31811 r->top_line = FRAME_TOP_MARGIN (f);
31812 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31813 r->total_cols = FRAME_COLS (f);
31814 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31815 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31816 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31817
31818 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31819 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31820 m->total_cols = FRAME_COLS (f);
31821 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31822 m->total_lines = 1;
31823 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31824
31825 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31826 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31827 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31828
31829 /* The default ellipsis glyphs `...'. */
31830 for (i = 0; i < 3; ++i)
31831 default_invis_vector[i] = make_number ('.');
31832 }
31833
31834 {
31835 /* Allocate the buffer for frame titles.
31836 Also used for `format-mode-line'. */
31837 int size = 100;
31838 mode_line_noprop_buf = xmalloc (size);
31839 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31840 mode_line_noprop_ptr = mode_line_noprop_buf;
31841 mode_line_target = MODE_LINE_DISPLAY;
31842 }
31843
31844 help_echo_showing_p = false;
31845 }
31846
31847 #ifdef HAVE_WINDOW_SYSTEM
31848
31849 /* Platform-independent portion of hourglass implementation. */
31850
31851 /* Timer function of hourglass_atimer. */
31852
31853 static void
31854 show_hourglass (struct atimer *timer)
31855 {
31856 /* The timer implementation will cancel this timer automatically
31857 after this function has run. Set hourglass_atimer to null
31858 so that we know the timer doesn't have to be canceled. */
31859 hourglass_atimer = NULL;
31860
31861 if (!hourglass_shown_p)
31862 {
31863 Lisp_Object tail, frame;
31864
31865 block_input ();
31866
31867 FOR_EACH_FRAME (tail, frame)
31868 {
31869 struct frame *f = XFRAME (frame);
31870
31871 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31872 && FRAME_RIF (f)->show_hourglass)
31873 FRAME_RIF (f)->show_hourglass (f);
31874 }
31875
31876 hourglass_shown_p = true;
31877 unblock_input ();
31878 }
31879 }
31880
31881 /* Cancel a currently active hourglass timer, and start a new one. */
31882
31883 void
31884 start_hourglass (void)
31885 {
31886 struct timespec delay;
31887
31888 cancel_hourglass ();
31889
31890 if (INTEGERP (Vhourglass_delay)
31891 && XINT (Vhourglass_delay) > 0)
31892 delay = make_timespec (min (XINT (Vhourglass_delay),
31893 TYPE_MAXIMUM (time_t)),
31894 0);
31895 else if (FLOATP (Vhourglass_delay)
31896 && XFLOAT_DATA (Vhourglass_delay) > 0)
31897 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31898 else
31899 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31900
31901 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31902 show_hourglass, NULL);
31903 }
31904
31905 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31906 shown. */
31907
31908 void
31909 cancel_hourglass (void)
31910 {
31911 if (hourglass_atimer)
31912 {
31913 cancel_atimer (hourglass_atimer);
31914 hourglass_atimer = NULL;
31915 }
31916
31917 if (hourglass_shown_p)
31918 {
31919 Lisp_Object tail, frame;
31920
31921 block_input ();
31922
31923 FOR_EACH_FRAME (tail, frame)
31924 {
31925 struct frame *f = XFRAME (frame);
31926
31927 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31928 && FRAME_RIF (f)->hide_hourglass)
31929 FRAME_RIF (f)->hide_hourglass (f);
31930 #ifdef HAVE_NTGUI
31931 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31932 else if (!FRAME_W32_P (f))
31933 w32_arrow_cursor ();
31934 #endif
31935 }
31936
31937 hourglass_shown_p = false;
31938 unblock_input ();
31939 }
31940 }
31941
31942 #endif /* HAVE_WINDOW_SYSTEM */