]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merge from origin/emacs-25
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2016 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
104
105 . try_window
106
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
112
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
117
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
130
131 Desired matrices.
132
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
139
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
146
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
156
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
163
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
175
176 Frame matrices.
177
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
184
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
196
197 Bidirectional display.
198
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
211
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
220
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
239
240 Bidirectional display and character compositions
241
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
246
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
266
267 Moving an iterator in bidirectional text
268 without producing glyphs
269
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
288
289 #include <config.h>
290 #include <stdio.h>
291 #include <limits.h>
292
293 #include "lisp.h"
294 #include "atimer.h"
295 #include "composite.h"
296 #include "keyboard.h"
297 #include "systime.h"
298 #include "frame.h"
299 #include "window.h"
300 #include "termchar.h"
301 #include "dispextern.h"
302 #include "character.h"
303 #include "buffer.h"
304 #include "charset.h"
305 #include "indent.h"
306 #include "commands.h"
307 #include "keymap.h"
308 #include "disptab.h"
309 #include "termhooks.h"
310 #include "termopts.h"
311 #include "intervals.h"
312 #include "coding.h"
313 #include "region-cache.h"
314 #include "font.h"
315 #include "fontset.h"
316 #include "blockinput.h"
317 #ifdef HAVE_WINDOW_SYSTEM
318 #include TERM_HEADER
319 #endif /* HAVE_WINDOW_SYSTEM */
320
321 #ifndef FRAME_X_OUTPUT
322 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
323 #endif
324
325 #define INFINITY 10000000
326
327 /* Holds the list (error). */
328 static Lisp_Object list_of_error;
329
330 #ifdef HAVE_WINDOW_SYSTEM
331
332 /* Test if overflow newline into fringe. Called with iterator IT
333 at or past right window margin, and with IT->current_x set. */
334
335 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
336 (!NILP (Voverflow_newline_into_fringe) \
337 && FRAME_WINDOW_P ((IT)->f) \
338 && ((IT)->bidi_it.paragraph_dir == R2L \
339 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
340 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
341 && (IT)->current_x == (IT)->last_visible_x)
342
343 #else /* !HAVE_WINDOW_SYSTEM */
344 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
345 #endif /* HAVE_WINDOW_SYSTEM */
346
347 /* Test if the display element loaded in IT, or the underlying buffer
348 or string character, is a space or a TAB character. This is used
349 to determine where word wrapping can occur. */
350
351 #define IT_DISPLAYING_WHITESPACE(it) \
352 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
353 || ((STRINGP (it->string) \
354 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
355 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
356 || (it->s \
357 && (it->s[IT_BYTEPOS (*it)] == ' ' \
358 || it->s[IT_BYTEPOS (*it)] == '\t')) \
359 || (IT_BYTEPOS (*it) < ZV_BYTE \
360 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
361 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
362
363 /* True means print newline to stdout before next mini-buffer message. */
364
365 bool noninteractive_need_newline;
366
367 /* True means print newline to message log before next message. */
368
369 static bool message_log_need_newline;
370
371 /* Three markers that message_dolog uses.
372 It could allocate them itself, but that causes trouble
373 in handling memory-full errors. */
374 static Lisp_Object message_dolog_marker1;
375 static Lisp_Object message_dolog_marker2;
376 static Lisp_Object message_dolog_marker3;
377 \f
378 /* The buffer position of the first character appearing entirely or
379 partially on the line of the selected window which contains the
380 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
381 redisplay optimization in redisplay_internal. */
382
383 static struct text_pos this_line_start_pos;
384
385 /* Number of characters past the end of the line above, including the
386 terminating newline. */
387
388 static struct text_pos this_line_end_pos;
389
390 /* The vertical positions and the height of this line. */
391
392 static int this_line_vpos;
393 static int this_line_y;
394 static int this_line_pixel_height;
395
396 /* X position at which this display line starts. Usually zero;
397 negative if first character is partially visible. */
398
399 static int this_line_start_x;
400
401 /* The smallest character position seen by move_it_* functions as they
402 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
403 hscrolled lines, see display_line. */
404
405 static struct text_pos this_line_min_pos;
406
407 /* Buffer that this_line_.* variables are referring to. */
408
409 static struct buffer *this_line_buffer;
410
411 /* True if an overlay arrow has been displayed in this window. */
412
413 static bool overlay_arrow_seen;
414
415 /* Vector containing glyphs for an ellipsis `...'. */
416
417 static Lisp_Object default_invis_vector[3];
418
419 /* This is the window where the echo area message was displayed. It
420 is always a mini-buffer window, but it may not be the same window
421 currently active as a mini-buffer. */
422
423 Lisp_Object echo_area_window;
424
425 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
426 pushes the current message and the value of
427 message_enable_multibyte on the stack, the function restore_message
428 pops the stack and displays MESSAGE again. */
429
430 static Lisp_Object Vmessage_stack;
431
432 /* True means multibyte characters were enabled when the echo area
433 message was specified. */
434
435 static bool message_enable_multibyte;
436
437 /* At each redisplay cycle, we should refresh everything there is to refresh.
438 To do that efficiently, we use many optimizations that try to make sure we
439 don't waste too much time updating things that haven't changed.
440 The coarsest such optimization is that, in the most common cases, we only
441 look at the selected-window.
442
443 To know whether other windows should be considered for redisplay, we use the
444 variable windows_or_buffers_changed: as long as it is 0, it means that we
445 have not noticed anything that should require updating anything else than
446 the selected-window. If it is set to REDISPLAY_SOME, it means that since
447 last redisplay, some changes have been made which could impact other
448 windows. To know which ones need redisplay, every buffer, window, and frame
449 has a `redisplay' bit, which (if true) means that this object needs to be
450 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
451 looking for those `redisplay' bits (actually, there might be some such bits
452 set, but then only on objects which aren't displayed anyway).
453
454 OTOH if it's non-zero we wil have to loop through all windows and then check
455 the `redisplay' bit of the corresponding window, frame, and buffer, in order
456 to decide whether that window needs attention or not. Note that we can't
457 just look at the frame's redisplay bit to decide that the whole frame can be
458 skipped, since even if the frame's redisplay bit is unset, some of its
459 windows's redisplay bits may be set.
460
461 Mostly for historical reasons, windows_or_buffers_changed can also take
462 other non-zero values. In that case, the precise value doesn't matter (it
463 encodes the cause of the setting but is only used for debugging purposes),
464 and what it means is that we shouldn't pay attention to any `redisplay' bits
465 and we should simply try and redisplay every window out there. */
466
467 int windows_or_buffers_changed;
468
469 /* Nonzero if we should redraw the mode lines on the next redisplay.
470 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
471 then only redisplay the mode lines in those buffers/windows/frames where the
472 `redisplay' bit has been set.
473 For any other value, redisplay all mode lines (the number used is then only
474 used to track down the cause for this full-redisplay).
475
476 Since the frame title uses the same %-constructs as the mode line
477 (except %c and %l), if this variable is non-zero, we also consider
478 redisplaying the title of each frame, see x_consider_frame_title.
479
480 The `redisplay' bits are the same as those used for
481 windows_or_buffers_changed, and setting windows_or_buffers_changed also
482 causes recomputation of the mode lines of all those windows. IOW this
483 variable only has an effect if windows_or_buffers_changed is zero, in which
484 case we should only need to redisplay the mode-line of those objects with
485 a `redisplay' bit set but not the window's text content (tho we may still
486 need to refresh the text content of the selected-window). */
487
488 int update_mode_lines;
489
490 /* True after display_mode_line if %l was used and it displayed a
491 line number. */
492
493 static bool line_number_displayed;
494
495 /* The name of the *Messages* buffer, a string. */
496
497 static Lisp_Object Vmessages_buffer_name;
498
499 /* Current, index 0, and last displayed echo area message. Either
500 buffers from echo_buffers, or nil to indicate no message. */
501
502 Lisp_Object echo_area_buffer[2];
503
504 /* The buffers referenced from echo_area_buffer. */
505
506 static Lisp_Object echo_buffer[2];
507
508 /* A vector saved used in with_area_buffer to reduce consing. */
509
510 static Lisp_Object Vwith_echo_area_save_vector;
511
512 /* True means display_echo_area should display the last echo area
513 message again. Set by redisplay_preserve_echo_area. */
514
515 static bool display_last_displayed_message_p;
516
517 /* True if echo area is being used by print; false if being used by
518 message. */
519
520 static bool message_buf_print;
521
522 /* Set to true in clear_message to make redisplay_internal aware
523 of an emptied echo area. */
524
525 static bool message_cleared_p;
526
527 /* A scratch glyph row with contents used for generating truncation
528 glyphs. Also used in direct_output_for_insert. */
529
530 #define MAX_SCRATCH_GLYPHS 100
531 static struct glyph_row scratch_glyph_row;
532 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
533
534 /* Ascent and height of the last line processed by move_it_to. */
535
536 static int last_height;
537
538 /* True if there's a help-echo in the echo area. */
539
540 bool help_echo_showing_p;
541
542 /* The maximum distance to look ahead for text properties. Values
543 that are too small let us call compute_char_face and similar
544 functions too often which is expensive. Values that are too large
545 let us call compute_char_face and alike too often because we
546 might not be interested in text properties that far away. */
547
548 #define TEXT_PROP_DISTANCE_LIMIT 100
549
550 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
551 iterator state and later restore it. This is needed because the
552 bidi iterator on bidi.c keeps a stacked cache of its states, which
553 is really a singleton. When we use scratch iterator objects to
554 move around the buffer, we can cause the bidi cache to be pushed or
555 popped, and therefore we need to restore the cache state when we
556 return to the original iterator. */
557 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
558 do { \
559 if (CACHE) \
560 bidi_unshelve_cache (CACHE, true); \
561 ITCOPY = ITORIG; \
562 CACHE = bidi_shelve_cache (); \
563 } while (false)
564
565 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
566 do { \
567 if (pITORIG != pITCOPY) \
568 *(pITORIG) = *(pITCOPY); \
569 bidi_unshelve_cache (CACHE, false); \
570 CACHE = NULL; \
571 } while (false)
572
573 /* Functions to mark elements as needing redisplay. */
574 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
575
576 void
577 redisplay_other_windows (void)
578 {
579 if (!windows_or_buffers_changed)
580 windows_or_buffers_changed = REDISPLAY_SOME;
581 }
582
583 void
584 wset_redisplay (struct window *w)
585 {
586 /* Beware: selected_window can be nil during early stages. */
587 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
588 redisplay_other_windows ();
589 w->redisplay = true;
590 }
591
592 void
593 fset_redisplay (struct frame *f)
594 {
595 redisplay_other_windows ();
596 f->redisplay = true;
597 }
598
599 void
600 bset_redisplay (struct buffer *b)
601 {
602 int count = buffer_window_count (b);
603 if (count > 0)
604 {
605 /* ... it's visible in other window than selected, */
606 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
607 redisplay_other_windows ();
608 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
609 so that if we later set windows_or_buffers_changed, this buffer will
610 not be omitted. */
611 b->text->redisplay = true;
612 }
613 }
614
615 void
616 bset_update_mode_line (struct buffer *b)
617 {
618 if (!update_mode_lines)
619 update_mode_lines = REDISPLAY_SOME;
620 b->text->redisplay = true;
621 }
622
623 void
624 maybe_set_redisplay (Lisp_Object symbol)
625 {
626 if (HASH_TABLE_P (Vredisplay__variables)
627 && hash_lookup (XHASH_TABLE (Vredisplay__variables), symbol, NULL) >= 0)
628 {
629 bset_update_mode_line (current_buffer);
630 current_buffer->prevent_redisplay_optimizations_p = true;
631 }
632 }
633
634 #ifdef GLYPH_DEBUG
635
636 /* True means print traces of redisplay if compiled with
637 GLYPH_DEBUG defined. */
638
639 bool trace_redisplay_p;
640
641 #endif /* GLYPH_DEBUG */
642
643 #ifdef DEBUG_TRACE_MOVE
644 /* True means trace with TRACE_MOVE to stderr. */
645 static bool trace_move;
646
647 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
648 #else
649 #define TRACE_MOVE(x) (void) 0
650 #endif
651
652 /* Buffer being redisplayed -- for redisplay_window_error. */
653
654 static struct buffer *displayed_buffer;
655
656 /* Value returned from text property handlers (see below). */
657
658 enum prop_handled
659 {
660 HANDLED_NORMALLY,
661 HANDLED_RECOMPUTE_PROPS,
662 HANDLED_OVERLAY_STRING_CONSUMED,
663 HANDLED_RETURN
664 };
665
666 /* A description of text properties that redisplay is interested
667 in. */
668
669 struct props
670 {
671 /* The symbol index of the name of the property. */
672 short name;
673
674 /* A unique index for the property. */
675 enum prop_idx idx;
676
677 /* A handler function called to set up iterator IT from the property
678 at IT's current position. Value is used to steer handle_stop. */
679 enum prop_handled (*handler) (struct it *it);
680 };
681
682 static enum prop_handled handle_face_prop (struct it *);
683 static enum prop_handled handle_invisible_prop (struct it *);
684 static enum prop_handled handle_display_prop (struct it *);
685 static enum prop_handled handle_composition_prop (struct it *);
686 static enum prop_handled handle_overlay_change (struct it *);
687 static enum prop_handled handle_fontified_prop (struct it *);
688
689 /* Properties handled by iterators. */
690
691 static struct props it_props[] =
692 {
693 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
694 /* Handle `face' before `display' because some sub-properties of
695 `display' need to know the face. */
696 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
697 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
698 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
699 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
700 {0, 0, NULL}
701 };
702
703 /* Value is the position described by X. If X is a marker, value is
704 the marker_position of X. Otherwise, value is X. */
705
706 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
707
708 /* Enumeration returned by some move_it_.* functions internally. */
709
710 enum move_it_result
711 {
712 /* Not used. Undefined value. */
713 MOVE_UNDEFINED,
714
715 /* Move ended at the requested buffer position or ZV. */
716 MOVE_POS_MATCH_OR_ZV,
717
718 /* Move ended at the requested X pixel position. */
719 MOVE_X_REACHED,
720
721 /* Move within a line ended at the end of a line that must be
722 continued. */
723 MOVE_LINE_CONTINUED,
724
725 /* Move within a line ended at the end of a line that would
726 be displayed truncated. */
727 MOVE_LINE_TRUNCATED,
728
729 /* Move within a line ended at a line end. */
730 MOVE_NEWLINE_OR_CR
731 };
732
733 /* This counter is used to clear the face cache every once in a while
734 in redisplay_internal. It is incremented for each redisplay.
735 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
736 cleared. */
737
738 #define CLEAR_FACE_CACHE_COUNT 500
739 static int clear_face_cache_count;
740
741 /* Similarly for the image cache. */
742
743 #ifdef HAVE_WINDOW_SYSTEM
744 #define CLEAR_IMAGE_CACHE_COUNT 101
745 static int clear_image_cache_count;
746
747 /* Null glyph slice */
748 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
749 #endif
750
751 /* True while redisplay_internal is in progress. */
752
753 bool redisplaying_p;
754
755 /* If a string, XTread_socket generates an event to display that string.
756 (The display is done in read_char.) */
757
758 Lisp_Object help_echo_string;
759 Lisp_Object help_echo_window;
760 Lisp_Object help_echo_object;
761 ptrdiff_t help_echo_pos;
762
763 /* Temporary variable for XTread_socket. */
764
765 Lisp_Object previous_help_echo_string;
766
767 /* Platform-independent portion of hourglass implementation. */
768
769 #ifdef HAVE_WINDOW_SYSTEM
770
771 /* True means an hourglass cursor is currently shown. */
772 static bool hourglass_shown_p;
773
774 /* If non-null, an asynchronous timer that, when it expires, displays
775 an hourglass cursor on all frames. */
776 static struct atimer *hourglass_atimer;
777
778 #endif /* HAVE_WINDOW_SYSTEM */
779
780 /* Default number of seconds to wait before displaying an hourglass
781 cursor. */
782 #define DEFAULT_HOURGLASS_DELAY 1
783
784 #ifdef HAVE_WINDOW_SYSTEM
785
786 /* Default pixel width of `thin-space' display method. */
787 #define THIN_SPACE_WIDTH 1
788
789 #endif /* HAVE_WINDOW_SYSTEM */
790
791 /* Function prototypes. */
792
793 static void setup_for_ellipsis (struct it *, int);
794 static void set_iterator_to_next (struct it *, bool);
795 static void mark_window_display_accurate_1 (struct window *, bool);
796 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
797 static bool cursor_row_p (struct glyph_row *);
798 static int redisplay_mode_lines (Lisp_Object, bool);
799
800 static void handle_line_prefix (struct it *);
801
802 static void handle_stop_backwards (struct it *, ptrdiff_t);
803 static void unwind_with_echo_area_buffer (Lisp_Object);
804 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
805 static bool current_message_1 (ptrdiff_t, Lisp_Object);
806 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
807 static void set_message (Lisp_Object);
808 static bool set_message_1 (ptrdiff_t, Lisp_Object);
809 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
810 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
811 static void unwind_redisplay (void);
812 static void extend_face_to_end_of_line (struct it *);
813 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
814 static void push_it (struct it *, struct text_pos *);
815 static void iterate_out_of_display_property (struct it *);
816 static void pop_it (struct it *);
817 static void redisplay_internal (void);
818 static void echo_area_display (bool);
819 static void redisplay_windows (Lisp_Object);
820 static void redisplay_window (Lisp_Object, bool);
821 static Lisp_Object redisplay_window_error (Lisp_Object);
822 static Lisp_Object redisplay_window_0 (Lisp_Object);
823 static Lisp_Object redisplay_window_1 (Lisp_Object);
824 static bool set_cursor_from_row (struct window *, struct glyph_row *,
825 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
826 int, int);
827 static bool update_menu_bar (struct frame *, bool, bool);
828 static bool try_window_reusing_current_matrix (struct window *);
829 static int try_window_id (struct window *);
830 static bool display_line (struct it *);
831 static int display_mode_lines (struct window *);
832 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
833 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
834 Lisp_Object, bool);
835 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
836 Lisp_Object);
837 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
838 static void display_menu_bar (struct window *);
839 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
840 ptrdiff_t *);
841 static int display_string (const char *, Lisp_Object, Lisp_Object,
842 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
843 static void compute_line_metrics (struct it *);
844 static void run_redisplay_end_trigger_hook (struct it *);
845 static bool get_overlay_strings (struct it *, ptrdiff_t);
846 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
847 static void next_overlay_string (struct it *);
848 static void reseat (struct it *, struct text_pos, bool);
849 static void reseat_1 (struct it *, struct text_pos, bool);
850 static bool next_element_from_display_vector (struct it *);
851 static bool next_element_from_string (struct it *);
852 static bool next_element_from_c_string (struct it *);
853 static bool next_element_from_buffer (struct it *);
854 static bool next_element_from_composition (struct it *);
855 static bool next_element_from_image (struct it *);
856 static bool next_element_from_stretch (struct it *);
857 static void load_overlay_strings (struct it *, ptrdiff_t);
858 static bool get_next_display_element (struct it *);
859 static enum move_it_result
860 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
861 enum move_operation_enum);
862 static void get_visually_first_element (struct it *);
863 static void compute_stop_pos (struct it *);
864 static int face_before_or_after_it_pos (struct it *, bool);
865 static ptrdiff_t next_overlay_change (ptrdiff_t);
866 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
867 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
868 static int handle_single_display_spec (struct it *, Lisp_Object,
869 Lisp_Object, Lisp_Object,
870 struct text_pos *, ptrdiff_t, int, bool);
871 static int underlying_face_id (struct it *);
872
873 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
874 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
875
876 #ifdef HAVE_WINDOW_SYSTEM
877
878 static void update_tool_bar (struct frame *, bool);
879 static void x_draw_bottom_divider (struct window *w);
880 static void notice_overwritten_cursor (struct window *,
881 enum glyph_row_area,
882 int, int, int, int);
883 static int normal_char_height (struct font *, int);
884 static void normal_char_ascent_descent (struct font *, int, int *, int *);
885
886 static void append_stretch_glyph (struct it *, Lisp_Object,
887 int, int, int);
888
889 static Lisp_Object get_it_property (struct it *, Lisp_Object);
890 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
891 struct font *, int, bool);
892
893 #endif /* HAVE_WINDOW_SYSTEM */
894
895 static void produce_special_glyphs (struct it *, enum display_element_type);
896 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
897 static bool coords_in_mouse_face_p (struct window *, int, int);
898
899
900 \f
901 /***********************************************************************
902 Window display dimensions
903 ***********************************************************************/
904
905 /* Return the bottom boundary y-position for text lines in window W.
906 This is the first y position at which a line cannot start.
907 It is relative to the top of the window.
908
909 This is the height of W minus the height of a mode line, if any. */
910
911 int
912 window_text_bottom_y (struct window *w)
913 {
914 int height = WINDOW_PIXEL_HEIGHT (w);
915
916 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
917
918 if (WINDOW_WANTS_MODELINE_P (w))
919 height -= CURRENT_MODE_LINE_HEIGHT (w);
920
921 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
922
923 return height;
924 }
925
926 /* Return the pixel width of display area AREA of window W.
927 ANY_AREA means return the total width of W, not including
928 fringes to the left and right of the window. */
929
930 int
931 window_box_width (struct window *w, enum glyph_row_area area)
932 {
933 int width = w->pixel_width;
934
935 if (!w->pseudo_window_p)
936 {
937 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
938 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
939
940 if (area == TEXT_AREA)
941 width -= (WINDOW_MARGINS_WIDTH (w)
942 + WINDOW_FRINGES_WIDTH (w));
943 else if (area == LEFT_MARGIN_AREA)
944 width = WINDOW_LEFT_MARGIN_WIDTH (w);
945 else if (area == RIGHT_MARGIN_AREA)
946 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
947 }
948
949 /* With wide margins, fringes, etc. we might end up with a negative
950 width, correct that here. */
951 return max (0, width);
952 }
953
954
955 /* Return the pixel height of the display area of window W, not
956 including mode lines of W, if any. */
957
958 int
959 window_box_height (struct window *w)
960 {
961 struct frame *f = XFRAME (w->frame);
962 int height = WINDOW_PIXEL_HEIGHT (w);
963
964 eassert (height >= 0);
965
966 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
967 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
968
969 /* Note: the code below that determines the mode-line/header-line
970 height is essentially the same as that contained in the macro
971 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
972 the appropriate glyph row has its `mode_line_p' flag set,
973 and if it doesn't, uses estimate_mode_line_height instead. */
974
975 if (WINDOW_WANTS_MODELINE_P (w))
976 {
977 struct glyph_row *ml_row
978 = (w->current_matrix && w->current_matrix->rows
979 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
980 : 0);
981 if (ml_row && ml_row->mode_line_p)
982 height -= ml_row->height;
983 else
984 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
985 }
986
987 if (WINDOW_WANTS_HEADER_LINE_P (w))
988 {
989 struct glyph_row *hl_row
990 = (w->current_matrix && w->current_matrix->rows
991 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
992 : 0);
993 if (hl_row && hl_row->mode_line_p)
994 height -= hl_row->height;
995 else
996 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
997 }
998
999 /* With a very small font and a mode-line that's taller than
1000 default, we might end up with a negative height. */
1001 return max (0, height);
1002 }
1003
1004 /* Return the window-relative coordinate of the left edge of display
1005 area AREA of window W. ANY_AREA means return the left edge of the
1006 whole window, to the right of the left fringe of W. */
1007
1008 int
1009 window_box_left_offset (struct window *w, enum glyph_row_area area)
1010 {
1011 int x;
1012
1013 if (w->pseudo_window_p)
1014 return 0;
1015
1016 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1017
1018 if (area == TEXT_AREA)
1019 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1020 + window_box_width (w, LEFT_MARGIN_AREA));
1021 else if (area == RIGHT_MARGIN_AREA)
1022 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1023 + window_box_width (w, LEFT_MARGIN_AREA)
1024 + window_box_width (w, TEXT_AREA)
1025 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1026 ? 0
1027 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1028 else if (area == LEFT_MARGIN_AREA
1029 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1030 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1031
1032 /* Don't return more than the window's pixel width. */
1033 return min (x, w->pixel_width);
1034 }
1035
1036
1037 /* Return the window-relative coordinate of the right edge of display
1038 area AREA of window W. ANY_AREA means return the right edge of the
1039 whole window, to the left of the right fringe of W. */
1040
1041 static int
1042 window_box_right_offset (struct window *w, enum glyph_row_area area)
1043 {
1044 /* Don't return more than the window's pixel width. */
1045 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1046 w->pixel_width);
1047 }
1048
1049 /* Return the frame-relative coordinate of the left edge of display
1050 area AREA of window W. ANY_AREA means return the left edge of the
1051 whole window, to the right of the left fringe of W. */
1052
1053 int
1054 window_box_left (struct window *w, enum glyph_row_area area)
1055 {
1056 struct frame *f = XFRAME (w->frame);
1057 int x;
1058
1059 if (w->pseudo_window_p)
1060 return FRAME_INTERNAL_BORDER_WIDTH (f);
1061
1062 x = (WINDOW_LEFT_EDGE_X (w)
1063 + window_box_left_offset (w, area));
1064
1065 return x;
1066 }
1067
1068
1069 /* Return the frame-relative coordinate of the right edge of display
1070 area AREA of window W. ANY_AREA means return the right edge of the
1071 whole window, to the left of the right fringe of W. */
1072
1073 int
1074 window_box_right (struct window *w, enum glyph_row_area area)
1075 {
1076 return window_box_left (w, area) + window_box_width (w, area);
1077 }
1078
1079 /* Get the bounding box of the display area AREA of window W, without
1080 mode lines, in frame-relative coordinates. ANY_AREA means the
1081 whole window, not including the left and right fringes of
1082 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1083 coordinates of the upper-left corner of the box. Return in
1084 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1085
1086 void
1087 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1088 int *box_y, int *box_width, int *box_height)
1089 {
1090 if (box_width)
1091 *box_width = window_box_width (w, area);
1092 if (box_height)
1093 *box_height = window_box_height (w);
1094 if (box_x)
1095 *box_x = window_box_left (w, area);
1096 if (box_y)
1097 {
1098 *box_y = WINDOW_TOP_EDGE_Y (w);
1099 if (WINDOW_WANTS_HEADER_LINE_P (w))
1100 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1101 }
1102 }
1103
1104 #ifdef HAVE_WINDOW_SYSTEM
1105
1106 /* Get the bounding box of the display area AREA of window W, without
1107 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1108 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1109 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1110 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1111 box. */
1112
1113 static void
1114 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1115 int *bottom_right_x, int *bottom_right_y)
1116 {
1117 window_box (w, ANY_AREA, top_left_x, top_left_y,
1118 bottom_right_x, bottom_right_y);
1119 *bottom_right_x += *top_left_x;
1120 *bottom_right_y += *top_left_y;
1121 }
1122
1123 #endif /* HAVE_WINDOW_SYSTEM */
1124
1125 /***********************************************************************
1126 Utilities
1127 ***********************************************************************/
1128
1129 /* Return the bottom y-position of the line the iterator IT is in.
1130 This can modify IT's settings. */
1131
1132 int
1133 line_bottom_y (struct it *it)
1134 {
1135 int line_height = it->max_ascent + it->max_descent;
1136 int line_top_y = it->current_y;
1137
1138 if (line_height == 0)
1139 {
1140 if (last_height)
1141 line_height = last_height;
1142 else if (IT_CHARPOS (*it) < ZV)
1143 {
1144 move_it_by_lines (it, 1);
1145 line_height = (it->max_ascent || it->max_descent
1146 ? it->max_ascent + it->max_descent
1147 : last_height);
1148 }
1149 else
1150 {
1151 struct glyph_row *row = it->glyph_row;
1152
1153 /* Use the default character height. */
1154 it->glyph_row = NULL;
1155 it->what = IT_CHARACTER;
1156 it->c = ' ';
1157 it->len = 1;
1158 PRODUCE_GLYPHS (it);
1159 line_height = it->ascent + it->descent;
1160 it->glyph_row = row;
1161 }
1162 }
1163
1164 return line_top_y + line_height;
1165 }
1166
1167 DEFUN ("line-pixel-height", Fline_pixel_height,
1168 Sline_pixel_height, 0, 0, 0,
1169 doc: /* Return height in pixels of text line in the selected window.
1170
1171 Value is the height in pixels of the line at point. */)
1172 (void)
1173 {
1174 struct it it;
1175 struct text_pos pt;
1176 struct window *w = XWINDOW (selected_window);
1177 struct buffer *old_buffer = NULL;
1178 Lisp_Object result;
1179
1180 if (XBUFFER (w->contents) != current_buffer)
1181 {
1182 old_buffer = current_buffer;
1183 set_buffer_internal_1 (XBUFFER (w->contents));
1184 }
1185 SET_TEXT_POS (pt, PT, PT_BYTE);
1186 start_display (&it, w, pt);
1187 it.vpos = it.current_y = 0;
1188 last_height = 0;
1189 result = make_number (line_bottom_y (&it));
1190 if (old_buffer)
1191 set_buffer_internal_1 (old_buffer);
1192
1193 return result;
1194 }
1195
1196 /* Return the default pixel height of text lines in window W. The
1197 value is the canonical height of the W frame's default font, plus
1198 any extra space required by the line-spacing variable or frame
1199 parameter.
1200
1201 Implementation note: this ignores any line-spacing text properties
1202 put on the newline characters. This is because those properties
1203 only affect the _screen_ line ending in the newline (i.e., in a
1204 continued line, only the last screen line will be affected), which
1205 means only a small number of lines in a buffer can ever use this
1206 feature. Since this function is used to compute the default pixel
1207 equivalent of text lines in a window, we can safely ignore those
1208 few lines. For the same reasons, we ignore the line-height
1209 properties. */
1210 int
1211 default_line_pixel_height (struct window *w)
1212 {
1213 struct frame *f = WINDOW_XFRAME (w);
1214 int height = FRAME_LINE_HEIGHT (f);
1215
1216 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1217 {
1218 struct buffer *b = XBUFFER (w->contents);
1219 Lisp_Object val = BVAR (b, extra_line_spacing);
1220
1221 if (NILP (val))
1222 val = BVAR (&buffer_defaults, extra_line_spacing);
1223 if (!NILP (val))
1224 {
1225 if (RANGED_INTEGERP (0, val, INT_MAX))
1226 height += XFASTINT (val);
1227 else if (FLOATP (val))
1228 {
1229 int addon = XFLOAT_DATA (val) * height + 0.5;
1230
1231 if (addon >= 0)
1232 height += addon;
1233 }
1234 }
1235 else
1236 height += f->extra_line_spacing;
1237 }
1238
1239 return height;
1240 }
1241
1242 /* Subroutine of pos_visible_p below. Extracts a display string, if
1243 any, from the display spec given as its argument. */
1244 static Lisp_Object
1245 string_from_display_spec (Lisp_Object spec)
1246 {
1247 if (CONSP (spec))
1248 {
1249 while (CONSP (spec))
1250 {
1251 if (STRINGP (XCAR (spec)))
1252 return XCAR (spec);
1253 spec = XCDR (spec);
1254 }
1255 }
1256 else if (VECTORP (spec))
1257 {
1258 ptrdiff_t i;
1259
1260 for (i = 0; i < ASIZE (spec); i++)
1261 {
1262 if (STRINGP (AREF (spec, i)))
1263 return AREF (spec, i);
1264 }
1265 return Qnil;
1266 }
1267
1268 return spec;
1269 }
1270
1271
1272 /* Limit insanely large values of W->hscroll on frame F to the largest
1273 value that will still prevent first_visible_x and last_visible_x of
1274 'struct it' from overflowing an int. */
1275 static int
1276 window_hscroll_limited (struct window *w, struct frame *f)
1277 {
1278 ptrdiff_t window_hscroll = w->hscroll;
1279 int window_text_width = window_box_width (w, TEXT_AREA);
1280 int colwidth = FRAME_COLUMN_WIDTH (f);
1281
1282 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1283 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1284
1285 return window_hscroll;
1286 }
1287
1288 /* Return true if position CHARPOS is visible in window W.
1289 CHARPOS < 0 means return info about WINDOW_END position.
1290 If visible, set *X and *Y to pixel coordinates of top left corner.
1291 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1292 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1293
1294 bool
1295 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1296 int *rtop, int *rbot, int *rowh, int *vpos)
1297 {
1298 struct it it;
1299 void *itdata = bidi_shelve_cache ();
1300 struct text_pos top;
1301 bool visible_p = false;
1302 struct buffer *old_buffer = NULL;
1303 bool r2l = false;
1304
1305 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1306 return visible_p;
1307
1308 if (XBUFFER (w->contents) != current_buffer)
1309 {
1310 old_buffer = current_buffer;
1311 set_buffer_internal_1 (XBUFFER (w->contents));
1312 }
1313
1314 SET_TEXT_POS_FROM_MARKER (top, w->start);
1315 /* Scrolling a minibuffer window via scroll bar when the echo area
1316 shows long text sometimes resets the minibuffer contents behind
1317 our backs. */
1318 if (CHARPOS (top) > ZV)
1319 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1320
1321 /* Compute exact mode line heights. */
1322 if (WINDOW_WANTS_MODELINE_P (w))
1323 w->mode_line_height
1324 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1325 BVAR (current_buffer, mode_line_format));
1326
1327 if (WINDOW_WANTS_HEADER_LINE_P (w))
1328 w->header_line_height
1329 = display_mode_line (w, HEADER_LINE_FACE_ID,
1330 BVAR (current_buffer, header_line_format));
1331
1332 start_display (&it, w, top);
1333 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1334 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1335
1336 if (charpos >= 0
1337 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1338 && IT_CHARPOS (it) >= charpos)
1339 /* When scanning backwards under bidi iteration, move_it_to
1340 stops at or _before_ CHARPOS, because it stops at or to
1341 the _right_ of the character at CHARPOS. */
1342 || (it.bidi_p && it.bidi_it.scan_dir == -1
1343 && IT_CHARPOS (it) <= charpos)))
1344 {
1345 /* We have reached CHARPOS, or passed it. How the call to
1346 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1347 or covered by a display property, move_it_to stops at the end
1348 of the invisible text, to the right of CHARPOS. (ii) If
1349 CHARPOS is in a display vector, move_it_to stops on its last
1350 glyph. */
1351 int top_x = it.current_x;
1352 int top_y = it.current_y;
1353 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1354 int bottom_y;
1355 struct it save_it;
1356 void *save_it_data = NULL;
1357
1358 /* Calling line_bottom_y may change it.method, it.position, etc. */
1359 SAVE_IT (save_it, it, save_it_data);
1360 last_height = 0;
1361 bottom_y = line_bottom_y (&it);
1362 if (top_y < window_top_y)
1363 visible_p = bottom_y > window_top_y;
1364 else if (top_y < it.last_visible_y)
1365 visible_p = true;
1366 if (bottom_y >= it.last_visible_y
1367 && it.bidi_p && it.bidi_it.scan_dir == -1
1368 && IT_CHARPOS (it) < charpos)
1369 {
1370 /* When the last line of the window is scanned backwards
1371 under bidi iteration, we could be duped into thinking
1372 that we have passed CHARPOS, when in fact move_it_to
1373 simply stopped short of CHARPOS because it reached
1374 last_visible_y. To see if that's what happened, we call
1375 move_it_to again with a slightly larger vertical limit,
1376 and see if it actually moved vertically; if it did, we
1377 didn't really reach CHARPOS, which is beyond window end. */
1378 /* Why 10? because we don't know how many canonical lines
1379 will the height of the next line(s) be. So we guess. */
1380 int ten_more_lines = 10 * default_line_pixel_height (w);
1381
1382 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1383 MOVE_TO_POS | MOVE_TO_Y);
1384 if (it.current_y > top_y)
1385 visible_p = false;
1386
1387 }
1388 RESTORE_IT (&it, &save_it, save_it_data);
1389 if (visible_p)
1390 {
1391 if (it.method == GET_FROM_DISPLAY_VECTOR)
1392 {
1393 /* We stopped on the last glyph of a display vector.
1394 Try and recompute. Hack alert! */
1395 if (charpos < 2 || top.charpos >= charpos)
1396 top_x = it.glyph_row->x;
1397 else
1398 {
1399 struct it it2, it2_prev;
1400 /* The idea is to get to the previous buffer
1401 position, consume the character there, and use
1402 the pixel coordinates we get after that. But if
1403 the previous buffer position is also displayed
1404 from a display vector, we need to consume all of
1405 the glyphs from that display vector. */
1406 start_display (&it2, w, top);
1407 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1408 /* If we didn't get to CHARPOS - 1, there's some
1409 replacing display property at that position, and
1410 we stopped after it. That is exactly the place
1411 whose coordinates we want. */
1412 if (IT_CHARPOS (it2) != charpos - 1)
1413 it2_prev = it2;
1414 else
1415 {
1416 /* Iterate until we get out of the display
1417 vector that displays the character at
1418 CHARPOS - 1. */
1419 do {
1420 get_next_display_element (&it2);
1421 PRODUCE_GLYPHS (&it2);
1422 it2_prev = it2;
1423 set_iterator_to_next (&it2, true);
1424 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1425 && IT_CHARPOS (it2) < charpos);
1426 }
1427 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1428 || it2_prev.current_x > it2_prev.last_visible_x)
1429 top_x = it.glyph_row->x;
1430 else
1431 {
1432 top_x = it2_prev.current_x;
1433 top_y = it2_prev.current_y;
1434 }
1435 }
1436 }
1437 else if (IT_CHARPOS (it) != charpos)
1438 {
1439 Lisp_Object cpos = make_number (charpos);
1440 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1441 Lisp_Object string = string_from_display_spec (spec);
1442 struct text_pos tpos;
1443 bool newline_in_string
1444 = (STRINGP (string)
1445 && memchr (SDATA (string), '\n', SBYTES (string)));
1446
1447 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1448 bool replacing_spec_p
1449 = (!NILP (spec)
1450 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1451 charpos, FRAME_WINDOW_P (it.f)));
1452 /* The tricky code below is needed because there's a
1453 discrepancy between move_it_to and how we set cursor
1454 when PT is at the beginning of a portion of text
1455 covered by a display property or an overlay with a
1456 display property, or the display line ends in a
1457 newline from a display string. move_it_to will stop
1458 _after_ such display strings, whereas
1459 set_cursor_from_row conspires with cursor_row_p to
1460 place the cursor on the first glyph produced from the
1461 display string. */
1462
1463 /* We have overshoot PT because it is covered by a
1464 display property that replaces the text it covers.
1465 If the string includes embedded newlines, we are also
1466 in the wrong display line. Backtrack to the correct
1467 line, where the display property begins. */
1468 if (replacing_spec_p)
1469 {
1470 Lisp_Object startpos, endpos;
1471 EMACS_INT start, end;
1472 struct it it3;
1473
1474 /* Find the first and the last buffer positions
1475 covered by the display string. */
1476 endpos =
1477 Fnext_single_char_property_change (cpos, Qdisplay,
1478 Qnil, Qnil);
1479 startpos =
1480 Fprevious_single_char_property_change (endpos, Qdisplay,
1481 Qnil, Qnil);
1482 start = XFASTINT (startpos);
1483 end = XFASTINT (endpos);
1484 /* Move to the last buffer position before the
1485 display property. */
1486 start_display (&it3, w, top);
1487 if (start > CHARPOS (top))
1488 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1489 /* Move forward one more line if the position before
1490 the display string is a newline or if it is the
1491 rightmost character on a line that is
1492 continued or word-wrapped. */
1493 if (it3.method == GET_FROM_BUFFER
1494 && (it3.c == '\n'
1495 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1496 move_it_by_lines (&it3, 1);
1497 else if (move_it_in_display_line_to (&it3, -1,
1498 it3.current_x
1499 + it3.pixel_width,
1500 MOVE_TO_X)
1501 == MOVE_LINE_CONTINUED)
1502 {
1503 move_it_by_lines (&it3, 1);
1504 /* When we are under word-wrap, the #$@%!
1505 move_it_by_lines moves 2 lines, so we need to
1506 fix that up. */
1507 if (it3.line_wrap == WORD_WRAP)
1508 move_it_by_lines (&it3, -1);
1509 }
1510
1511 /* Record the vertical coordinate of the display
1512 line where we wound up. */
1513 top_y = it3.current_y;
1514 if (it3.bidi_p)
1515 {
1516 /* When characters are reordered for display,
1517 the character displayed to the left of the
1518 display string could be _after_ the display
1519 property in the logical order. Use the
1520 smallest vertical position of these two. */
1521 start_display (&it3, w, top);
1522 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1523 if (it3.current_y < top_y)
1524 top_y = it3.current_y;
1525 }
1526 /* Move from the top of the window to the beginning
1527 of the display line where the display string
1528 begins. */
1529 start_display (&it3, w, top);
1530 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1531 /* If it3_moved stays false after the 'while' loop
1532 below, that means we already were at a newline
1533 before the loop (e.g., the display string begins
1534 with a newline), so we don't need to (and cannot)
1535 inspect the glyphs of it3.glyph_row, because
1536 PRODUCE_GLYPHS will not produce anything for a
1537 newline, and thus it3.glyph_row stays at its
1538 stale content it got at top of the window. */
1539 bool it3_moved = false;
1540 /* Finally, advance the iterator until we hit the
1541 first display element whose character position is
1542 CHARPOS, or until the first newline from the
1543 display string, which signals the end of the
1544 display line. */
1545 while (get_next_display_element (&it3))
1546 {
1547 PRODUCE_GLYPHS (&it3);
1548 if (IT_CHARPOS (it3) == charpos
1549 || ITERATOR_AT_END_OF_LINE_P (&it3))
1550 break;
1551 it3_moved = true;
1552 set_iterator_to_next (&it3, false);
1553 }
1554 top_x = it3.current_x - it3.pixel_width;
1555 /* Normally, we would exit the above loop because we
1556 found the display element whose character
1557 position is CHARPOS. For the contingency that we
1558 didn't, and stopped at the first newline from the
1559 display string, move back over the glyphs
1560 produced from the string, until we find the
1561 rightmost glyph not from the string. */
1562 if (it3_moved
1563 && newline_in_string
1564 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1565 {
1566 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1567 + it3.glyph_row->used[TEXT_AREA];
1568
1569 while (EQ ((g - 1)->object, string))
1570 {
1571 --g;
1572 top_x -= g->pixel_width;
1573 }
1574 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1575 + it3.glyph_row->used[TEXT_AREA]);
1576 }
1577 }
1578 }
1579
1580 *x = top_x;
1581 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1582 *rtop = max (0, window_top_y - top_y);
1583 *rbot = max (0, bottom_y - it.last_visible_y);
1584 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1585 - max (top_y, window_top_y)));
1586 *vpos = it.vpos;
1587 if (it.bidi_it.paragraph_dir == R2L)
1588 r2l = true;
1589 }
1590 }
1591 else
1592 {
1593 /* Either we were asked to provide info about WINDOW_END, or
1594 CHARPOS is in the partially visible glyph row at end of
1595 window. */
1596 struct it it2;
1597 void *it2data = NULL;
1598
1599 SAVE_IT (it2, it, it2data);
1600 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1601 move_it_by_lines (&it, 1);
1602 if (charpos < IT_CHARPOS (it)
1603 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1604 {
1605 visible_p = true;
1606 RESTORE_IT (&it2, &it2, it2data);
1607 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1608 *x = it2.current_x;
1609 *y = it2.current_y + it2.max_ascent - it2.ascent;
1610 *rtop = max (0, -it2.current_y);
1611 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1612 - it.last_visible_y));
1613 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1614 it.last_visible_y)
1615 - max (it2.current_y,
1616 WINDOW_HEADER_LINE_HEIGHT (w))));
1617 *vpos = it2.vpos;
1618 if (it2.bidi_it.paragraph_dir == R2L)
1619 r2l = true;
1620 }
1621 else
1622 bidi_unshelve_cache (it2data, true);
1623 }
1624 bidi_unshelve_cache (itdata, false);
1625
1626 if (old_buffer)
1627 set_buffer_internal_1 (old_buffer);
1628
1629 if (visible_p)
1630 {
1631 if (w->hscroll > 0)
1632 *x -=
1633 window_hscroll_limited (w, WINDOW_XFRAME (w))
1634 * WINDOW_FRAME_COLUMN_WIDTH (w);
1635 /* For lines in an R2L paragraph, we need to mirror the X pixel
1636 coordinate wrt the text area. For the reasons, see the
1637 commentary in buffer_posn_from_coords and the explanation of
1638 the geometry used by the move_it_* functions at the end of
1639 the large commentary near the beginning of this file. */
1640 if (r2l)
1641 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1642 }
1643
1644 #if false
1645 /* Debugging code. */
1646 if (visible_p)
1647 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1648 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1649 else
1650 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1651 #endif
1652
1653 return visible_p;
1654 }
1655
1656
1657 /* Return the next character from STR. Return in *LEN the length of
1658 the character. This is like STRING_CHAR_AND_LENGTH but never
1659 returns an invalid character. If we find one, we return a `?', but
1660 with the length of the invalid character. */
1661
1662 static int
1663 string_char_and_length (const unsigned char *str, int *len)
1664 {
1665 int c;
1666
1667 c = STRING_CHAR_AND_LENGTH (str, *len);
1668 if (!CHAR_VALID_P (c))
1669 /* We may not change the length here because other places in Emacs
1670 don't use this function, i.e. they silently accept invalid
1671 characters. */
1672 c = '?';
1673
1674 return c;
1675 }
1676
1677
1678
1679 /* Given a position POS containing a valid character and byte position
1680 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1681
1682 static struct text_pos
1683 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1684 {
1685 eassert (STRINGP (string) && nchars >= 0);
1686
1687 if (STRING_MULTIBYTE (string))
1688 {
1689 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1690 int len;
1691
1692 while (nchars--)
1693 {
1694 string_char_and_length (p, &len);
1695 p += len;
1696 CHARPOS (pos) += 1;
1697 BYTEPOS (pos) += len;
1698 }
1699 }
1700 else
1701 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1702
1703 return pos;
1704 }
1705
1706
1707 /* Value is the text position, i.e. character and byte position,
1708 for character position CHARPOS in STRING. */
1709
1710 static struct text_pos
1711 string_pos (ptrdiff_t charpos, Lisp_Object string)
1712 {
1713 struct text_pos pos;
1714 eassert (STRINGP (string));
1715 eassert (charpos >= 0);
1716 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1717 return pos;
1718 }
1719
1720
1721 /* Value is a text position, i.e. character and byte position, for
1722 character position CHARPOS in C string S. MULTIBYTE_P
1723 means recognize multibyte characters. */
1724
1725 static struct text_pos
1726 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1727 {
1728 struct text_pos pos;
1729
1730 eassert (s != NULL);
1731 eassert (charpos >= 0);
1732
1733 if (multibyte_p)
1734 {
1735 int len;
1736
1737 SET_TEXT_POS (pos, 0, 0);
1738 while (charpos--)
1739 {
1740 string_char_and_length ((const unsigned char *) s, &len);
1741 s += len;
1742 CHARPOS (pos) += 1;
1743 BYTEPOS (pos) += len;
1744 }
1745 }
1746 else
1747 SET_TEXT_POS (pos, charpos, charpos);
1748
1749 return pos;
1750 }
1751
1752
1753 /* Value is the number of characters in C string S. MULTIBYTE_P
1754 means recognize multibyte characters. */
1755
1756 static ptrdiff_t
1757 number_of_chars (const char *s, bool multibyte_p)
1758 {
1759 ptrdiff_t nchars;
1760
1761 if (multibyte_p)
1762 {
1763 ptrdiff_t rest = strlen (s);
1764 int len;
1765 const unsigned char *p = (const unsigned char *) s;
1766
1767 for (nchars = 0; rest > 0; ++nchars)
1768 {
1769 string_char_and_length (p, &len);
1770 rest -= len, p += len;
1771 }
1772 }
1773 else
1774 nchars = strlen (s);
1775
1776 return nchars;
1777 }
1778
1779
1780 /* Compute byte position NEWPOS->bytepos corresponding to
1781 NEWPOS->charpos. POS is a known position in string STRING.
1782 NEWPOS->charpos must be >= POS.charpos. */
1783
1784 static void
1785 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1786 {
1787 eassert (STRINGP (string));
1788 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1789
1790 if (STRING_MULTIBYTE (string))
1791 *newpos = string_pos_nchars_ahead (pos, string,
1792 CHARPOS (*newpos) - CHARPOS (pos));
1793 else
1794 BYTEPOS (*newpos) = CHARPOS (*newpos);
1795 }
1796
1797 /* EXPORT:
1798 Return an estimation of the pixel height of mode or header lines on
1799 frame F. FACE_ID specifies what line's height to estimate. */
1800
1801 int
1802 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1803 {
1804 #ifdef HAVE_WINDOW_SYSTEM
1805 if (FRAME_WINDOW_P (f))
1806 {
1807 int height = FONT_HEIGHT (FRAME_FONT (f));
1808
1809 /* This function is called so early when Emacs starts that the face
1810 cache and mode line face are not yet initialized. */
1811 if (FRAME_FACE_CACHE (f))
1812 {
1813 struct face *face = FACE_FROM_ID (f, face_id);
1814 if (face)
1815 {
1816 if (face->font)
1817 height = normal_char_height (face->font, -1);
1818 if (face->box_line_width > 0)
1819 height += 2 * face->box_line_width;
1820 }
1821 }
1822
1823 return height;
1824 }
1825 #endif
1826
1827 return 1;
1828 }
1829
1830 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1831 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1832 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1833 not force the value into range. */
1834
1835 void
1836 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1837 NativeRectangle *bounds, bool noclip)
1838 {
1839
1840 #ifdef HAVE_WINDOW_SYSTEM
1841 if (FRAME_WINDOW_P (f))
1842 {
1843 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1844 even for negative values. */
1845 if (pix_x < 0)
1846 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1847 if (pix_y < 0)
1848 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1849
1850 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1851 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1852
1853 if (bounds)
1854 STORE_NATIVE_RECT (*bounds,
1855 FRAME_COL_TO_PIXEL_X (f, pix_x),
1856 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1857 FRAME_COLUMN_WIDTH (f) - 1,
1858 FRAME_LINE_HEIGHT (f) - 1);
1859
1860 /* PXW: Should we clip pixels before converting to columns/lines? */
1861 if (!noclip)
1862 {
1863 if (pix_x < 0)
1864 pix_x = 0;
1865 else if (pix_x > FRAME_TOTAL_COLS (f))
1866 pix_x = FRAME_TOTAL_COLS (f);
1867
1868 if (pix_y < 0)
1869 pix_y = 0;
1870 else if (pix_y > FRAME_TOTAL_LINES (f))
1871 pix_y = FRAME_TOTAL_LINES (f);
1872 }
1873 }
1874 #endif
1875
1876 *x = pix_x;
1877 *y = pix_y;
1878 }
1879
1880
1881 /* Find the glyph under window-relative coordinates X/Y in window W.
1882 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1883 strings. Return in *HPOS and *VPOS the row and column number of
1884 the glyph found. Return in *AREA the glyph area containing X.
1885 Value is a pointer to the glyph found or null if X/Y is not on
1886 text, or we can't tell because W's current matrix is not up to
1887 date. */
1888
1889 static struct glyph *
1890 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1891 int *dx, int *dy, int *area)
1892 {
1893 struct glyph *glyph, *end;
1894 struct glyph_row *row = NULL;
1895 int x0, i;
1896
1897 /* Find row containing Y. Give up if some row is not enabled. */
1898 for (i = 0; i < w->current_matrix->nrows; ++i)
1899 {
1900 row = MATRIX_ROW (w->current_matrix, i);
1901 if (!row->enabled_p)
1902 return NULL;
1903 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1904 break;
1905 }
1906
1907 *vpos = i;
1908 *hpos = 0;
1909
1910 /* Give up if Y is not in the window. */
1911 if (i == w->current_matrix->nrows)
1912 return NULL;
1913
1914 /* Get the glyph area containing X. */
1915 if (w->pseudo_window_p)
1916 {
1917 *area = TEXT_AREA;
1918 x0 = 0;
1919 }
1920 else
1921 {
1922 if (x < window_box_left_offset (w, TEXT_AREA))
1923 {
1924 *area = LEFT_MARGIN_AREA;
1925 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1926 }
1927 else if (x < window_box_right_offset (w, TEXT_AREA))
1928 {
1929 *area = TEXT_AREA;
1930 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1931 }
1932 else
1933 {
1934 *area = RIGHT_MARGIN_AREA;
1935 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1936 }
1937 }
1938
1939 /* Find glyph containing X. */
1940 glyph = row->glyphs[*area];
1941 end = glyph + row->used[*area];
1942 x -= x0;
1943 while (glyph < end && x >= glyph->pixel_width)
1944 {
1945 x -= glyph->pixel_width;
1946 ++glyph;
1947 }
1948
1949 if (glyph == end)
1950 return NULL;
1951
1952 if (dx)
1953 {
1954 *dx = x;
1955 *dy = y - (row->y + row->ascent - glyph->ascent);
1956 }
1957
1958 *hpos = glyph - row->glyphs[*area];
1959 return glyph;
1960 }
1961
1962 /* Convert frame-relative x/y to coordinates relative to window W.
1963 Takes pseudo-windows into account. */
1964
1965 static void
1966 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1967 {
1968 if (w->pseudo_window_p)
1969 {
1970 /* A pseudo-window is always full-width, and starts at the
1971 left edge of the frame, plus a frame border. */
1972 struct frame *f = XFRAME (w->frame);
1973 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1974 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1975 }
1976 else
1977 {
1978 *x -= WINDOW_LEFT_EDGE_X (w);
1979 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1980 }
1981 }
1982
1983 #ifdef HAVE_WINDOW_SYSTEM
1984
1985 /* EXPORT:
1986 Return in RECTS[] at most N clipping rectangles for glyph string S.
1987 Return the number of stored rectangles. */
1988
1989 int
1990 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1991 {
1992 XRectangle r;
1993
1994 if (n <= 0)
1995 return 0;
1996
1997 if (s->row->full_width_p)
1998 {
1999 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2000 r.x = WINDOW_LEFT_EDGE_X (s->w);
2001 if (s->row->mode_line_p)
2002 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2003 else
2004 r.width = WINDOW_PIXEL_WIDTH (s->w);
2005
2006 /* Unless displaying a mode or menu bar line, which are always
2007 fully visible, clip to the visible part of the row. */
2008 if (s->w->pseudo_window_p)
2009 r.height = s->row->visible_height;
2010 else
2011 r.height = s->height;
2012 }
2013 else
2014 {
2015 /* This is a text line that may be partially visible. */
2016 r.x = window_box_left (s->w, s->area);
2017 r.width = window_box_width (s->w, s->area);
2018 r.height = s->row->visible_height;
2019 }
2020
2021 if (s->clip_head)
2022 if (r.x < s->clip_head->x)
2023 {
2024 if (r.width >= s->clip_head->x - r.x)
2025 r.width -= s->clip_head->x - r.x;
2026 else
2027 r.width = 0;
2028 r.x = s->clip_head->x;
2029 }
2030 if (s->clip_tail)
2031 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2032 {
2033 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2034 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2035 else
2036 r.width = 0;
2037 }
2038
2039 /* If S draws overlapping rows, it's sufficient to use the top and
2040 bottom of the window for clipping because this glyph string
2041 intentionally draws over other lines. */
2042 if (s->for_overlaps)
2043 {
2044 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2045 r.height = window_text_bottom_y (s->w) - r.y;
2046
2047 /* Alas, the above simple strategy does not work for the
2048 environments with anti-aliased text: if the same text is
2049 drawn onto the same place multiple times, it gets thicker.
2050 If the overlap we are processing is for the erased cursor, we
2051 take the intersection with the rectangle of the cursor. */
2052 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2053 {
2054 XRectangle rc, r_save = r;
2055
2056 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2057 rc.y = s->w->phys_cursor.y;
2058 rc.width = s->w->phys_cursor_width;
2059 rc.height = s->w->phys_cursor_height;
2060
2061 x_intersect_rectangles (&r_save, &rc, &r);
2062 }
2063 }
2064 else
2065 {
2066 /* Don't use S->y for clipping because it doesn't take partially
2067 visible lines into account. For example, it can be negative for
2068 partially visible lines at the top of a window. */
2069 if (!s->row->full_width_p
2070 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2071 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2072 else
2073 r.y = max (0, s->row->y);
2074 }
2075
2076 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2077
2078 /* If drawing the cursor, don't let glyph draw outside its
2079 advertised boundaries. Cleartype does this under some circumstances. */
2080 if (s->hl == DRAW_CURSOR)
2081 {
2082 struct glyph *glyph = s->first_glyph;
2083 int height, max_y;
2084
2085 if (s->x > r.x)
2086 {
2087 if (r.width >= s->x - r.x)
2088 r.width -= s->x - r.x;
2089 else /* R2L hscrolled row with cursor outside text area */
2090 r.width = 0;
2091 r.x = s->x;
2092 }
2093 r.width = min (r.width, glyph->pixel_width);
2094
2095 /* If r.y is below window bottom, ensure that we still see a cursor. */
2096 height = min (glyph->ascent + glyph->descent,
2097 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2098 max_y = window_text_bottom_y (s->w) - height;
2099 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2100 if (s->ybase - glyph->ascent > max_y)
2101 {
2102 r.y = max_y;
2103 r.height = height;
2104 }
2105 else
2106 {
2107 /* Don't draw cursor glyph taller than our actual glyph. */
2108 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2109 if (height < r.height)
2110 {
2111 max_y = r.y + r.height;
2112 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2113 r.height = min (max_y - r.y, height);
2114 }
2115 }
2116 }
2117
2118 if (s->row->clip)
2119 {
2120 XRectangle r_save = r;
2121
2122 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2123 r.width = 0;
2124 }
2125
2126 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2127 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2128 {
2129 #ifdef CONVERT_FROM_XRECT
2130 CONVERT_FROM_XRECT (r, *rects);
2131 #else
2132 *rects = r;
2133 #endif
2134 return 1;
2135 }
2136 else
2137 {
2138 /* If we are processing overlapping and allowed to return
2139 multiple clipping rectangles, we exclude the row of the glyph
2140 string from the clipping rectangle. This is to avoid drawing
2141 the same text on the environment with anti-aliasing. */
2142 #ifdef CONVERT_FROM_XRECT
2143 XRectangle rs[2];
2144 #else
2145 XRectangle *rs = rects;
2146 #endif
2147 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2148
2149 if (s->for_overlaps & OVERLAPS_PRED)
2150 {
2151 rs[i] = r;
2152 if (r.y + r.height > row_y)
2153 {
2154 if (r.y < row_y)
2155 rs[i].height = row_y - r.y;
2156 else
2157 rs[i].height = 0;
2158 }
2159 i++;
2160 }
2161 if (s->for_overlaps & OVERLAPS_SUCC)
2162 {
2163 rs[i] = r;
2164 if (r.y < row_y + s->row->visible_height)
2165 {
2166 if (r.y + r.height > row_y + s->row->visible_height)
2167 {
2168 rs[i].y = row_y + s->row->visible_height;
2169 rs[i].height = r.y + r.height - rs[i].y;
2170 }
2171 else
2172 rs[i].height = 0;
2173 }
2174 i++;
2175 }
2176
2177 n = i;
2178 #ifdef CONVERT_FROM_XRECT
2179 for (i = 0; i < n; i++)
2180 CONVERT_FROM_XRECT (rs[i], rects[i]);
2181 #endif
2182 return n;
2183 }
2184 }
2185
2186 /* EXPORT:
2187 Return in *NR the clipping rectangle for glyph string S. */
2188
2189 void
2190 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2191 {
2192 get_glyph_string_clip_rects (s, nr, 1);
2193 }
2194
2195
2196 /* EXPORT:
2197 Return the position and height of the phys cursor in window W.
2198 Set w->phys_cursor_width to width of phys cursor.
2199 */
2200
2201 void
2202 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2203 struct glyph *glyph, int *xp, int *yp, int *heightp)
2204 {
2205 struct frame *f = XFRAME (WINDOW_FRAME (w));
2206 int x, y, wd, h, h0, y0, ascent;
2207
2208 /* Compute the width of the rectangle to draw. If on a stretch
2209 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2210 rectangle as wide as the glyph, but use a canonical character
2211 width instead. */
2212 wd = glyph->pixel_width;
2213
2214 x = w->phys_cursor.x;
2215 if (x < 0)
2216 {
2217 wd += x;
2218 x = 0;
2219 }
2220
2221 if (glyph->type == STRETCH_GLYPH
2222 && !x_stretch_cursor_p)
2223 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2224 w->phys_cursor_width = wd;
2225
2226 /* Don't let the hollow cursor glyph descend below the glyph row's
2227 ascent value, lest the hollow cursor looks funny. */
2228 y = w->phys_cursor.y;
2229 ascent = row->ascent;
2230 if (row->ascent < glyph->ascent)
2231 {
2232 y =- glyph->ascent - row->ascent;
2233 ascent = glyph->ascent;
2234 }
2235
2236 /* If y is below window bottom, ensure that we still see a cursor. */
2237 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2238
2239 h = max (h0, ascent + glyph->descent);
2240 h0 = min (h0, ascent + glyph->descent);
2241
2242 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2243 if (y < y0)
2244 {
2245 h = max (h - (y0 - y) + 1, h0);
2246 y = y0 - 1;
2247 }
2248 else
2249 {
2250 y0 = window_text_bottom_y (w) - h0;
2251 if (y > y0)
2252 {
2253 h += y - y0;
2254 y = y0;
2255 }
2256 }
2257
2258 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2259 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2260 *heightp = h;
2261 }
2262
2263 /*
2264 * Remember which glyph the mouse is over.
2265 */
2266
2267 void
2268 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2269 {
2270 Lisp_Object window;
2271 struct window *w;
2272 struct glyph_row *r, *gr, *end_row;
2273 enum window_part part;
2274 enum glyph_row_area area;
2275 int x, y, width, height;
2276
2277 /* Try to determine frame pixel position and size of the glyph under
2278 frame pixel coordinates X/Y on frame F. */
2279
2280 if (window_resize_pixelwise)
2281 {
2282 width = height = 1;
2283 goto virtual_glyph;
2284 }
2285 else if (!f->glyphs_initialized_p
2286 || (window = window_from_coordinates (f, gx, gy, &part, false),
2287 NILP (window)))
2288 {
2289 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2290 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2291 goto virtual_glyph;
2292 }
2293
2294 w = XWINDOW (window);
2295 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2296 height = WINDOW_FRAME_LINE_HEIGHT (w);
2297
2298 x = window_relative_x_coord (w, part, gx);
2299 y = gy - WINDOW_TOP_EDGE_Y (w);
2300
2301 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2302 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2303
2304 if (w->pseudo_window_p)
2305 {
2306 area = TEXT_AREA;
2307 part = ON_MODE_LINE; /* Don't adjust margin. */
2308 goto text_glyph;
2309 }
2310
2311 switch (part)
2312 {
2313 case ON_LEFT_MARGIN:
2314 area = LEFT_MARGIN_AREA;
2315 goto text_glyph;
2316
2317 case ON_RIGHT_MARGIN:
2318 area = RIGHT_MARGIN_AREA;
2319 goto text_glyph;
2320
2321 case ON_HEADER_LINE:
2322 case ON_MODE_LINE:
2323 gr = (part == ON_HEADER_LINE
2324 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2325 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2326 gy = gr->y;
2327 area = TEXT_AREA;
2328 goto text_glyph_row_found;
2329
2330 case ON_TEXT:
2331 area = TEXT_AREA;
2332
2333 text_glyph:
2334 gr = 0; gy = 0;
2335 for (; r <= end_row && r->enabled_p; ++r)
2336 if (r->y + r->height > y)
2337 {
2338 gr = r; gy = r->y;
2339 break;
2340 }
2341
2342 text_glyph_row_found:
2343 if (gr && gy <= y)
2344 {
2345 struct glyph *g = gr->glyphs[area];
2346 struct glyph *end = g + gr->used[area];
2347
2348 height = gr->height;
2349 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2350 if (gx + g->pixel_width > x)
2351 break;
2352
2353 if (g < end)
2354 {
2355 if (g->type == IMAGE_GLYPH)
2356 {
2357 /* Don't remember when mouse is over image, as
2358 image may have hot-spots. */
2359 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2360 return;
2361 }
2362 width = g->pixel_width;
2363 }
2364 else
2365 {
2366 /* Use nominal char spacing at end of line. */
2367 x -= gx;
2368 gx += (x / width) * width;
2369 }
2370
2371 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2372 {
2373 gx += window_box_left_offset (w, area);
2374 /* Don't expand over the modeline to make sure the vertical
2375 drag cursor is shown early enough. */
2376 height = min (height,
2377 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2378 }
2379 }
2380 else
2381 {
2382 /* Use nominal line height at end of window. */
2383 gx = (x / width) * width;
2384 y -= gy;
2385 gy += (y / height) * height;
2386 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2387 /* See comment above. */
2388 height = min (height,
2389 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2390 }
2391 break;
2392
2393 case ON_LEFT_FRINGE:
2394 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2395 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2396 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2397 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2398 goto row_glyph;
2399
2400 case ON_RIGHT_FRINGE:
2401 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2402 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2403 : window_box_right_offset (w, TEXT_AREA));
2404 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2405 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2406 && !WINDOW_RIGHTMOST_P (w))
2407 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2408 /* Make sure the vertical border can get her own glyph to the
2409 right of the one we build here. */
2410 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2411 else
2412 width = WINDOW_PIXEL_WIDTH (w) - gx;
2413 else
2414 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2415
2416 goto row_glyph;
2417
2418 case ON_VERTICAL_BORDER:
2419 gx = WINDOW_PIXEL_WIDTH (w) - width;
2420 goto row_glyph;
2421
2422 case ON_VERTICAL_SCROLL_BAR:
2423 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2424 ? 0
2425 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2426 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2427 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2428 : 0)));
2429 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2430
2431 row_glyph:
2432 gr = 0, gy = 0;
2433 for (; r <= end_row && r->enabled_p; ++r)
2434 if (r->y + r->height > y)
2435 {
2436 gr = r; gy = r->y;
2437 break;
2438 }
2439
2440 if (gr && gy <= y)
2441 height = gr->height;
2442 else
2443 {
2444 /* Use nominal line height at end of window. */
2445 y -= gy;
2446 gy += (y / height) * height;
2447 }
2448 break;
2449
2450 case ON_RIGHT_DIVIDER:
2451 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2452 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2453 gy = 0;
2454 /* The bottom divider prevails. */
2455 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2456 goto add_edge;
2457
2458 case ON_BOTTOM_DIVIDER:
2459 gx = 0;
2460 width = WINDOW_PIXEL_WIDTH (w);
2461 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2462 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2463 goto add_edge;
2464
2465 default:
2466 ;
2467 virtual_glyph:
2468 /* If there is no glyph under the mouse, then we divide the screen
2469 into a grid of the smallest glyph in the frame, and use that
2470 as our "glyph". */
2471
2472 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2473 round down even for negative values. */
2474 if (gx < 0)
2475 gx -= width - 1;
2476 if (gy < 0)
2477 gy -= height - 1;
2478
2479 gx = (gx / width) * width;
2480 gy = (gy / height) * height;
2481
2482 goto store_rect;
2483 }
2484
2485 add_edge:
2486 gx += WINDOW_LEFT_EDGE_X (w);
2487 gy += WINDOW_TOP_EDGE_Y (w);
2488
2489 store_rect:
2490 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2491
2492 /* Visible feedback for debugging. */
2493 #if false && defined HAVE_X_WINDOWS
2494 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2495 f->output_data.x->normal_gc,
2496 gx, gy, width, height);
2497 #endif
2498 }
2499
2500
2501 #endif /* HAVE_WINDOW_SYSTEM */
2502
2503 static void
2504 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2505 {
2506 eassert (w);
2507 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2508 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2509 w->window_end_vpos
2510 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2511 }
2512
2513 /***********************************************************************
2514 Lisp form evaluation
2515 ***********************************************************************/
2516
2517 /* Error handler for safe_eval and safe_call. */
2518
2519 static Lisp_Object
2520 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2521 {
2522 add_to_log ("Error during redisplay: %S signaled %S",
2523 Flist (nargs, args), arg);
2524 return Qnil;
2525 }
2526
2527 /* Call function FUNC with the rest of NARGS - 1 arguments
2528 following. Return the result, or nil if something went
2529 wrong. Prevent redisplay during the evaluation. */
2530
2531 static Lisp_Object
2532 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2533 {
2534 Lisp_Object val;
2535
2536 if (inhibit_eval_during_redisplay)
2537 val = Qnil;
2538 else
2539 {
2540 ptrdiff_t i;
2541 ptrdiff_t count = SPECPDL_INDEX ();
2542 Lisp_Object *args;
2543 USE_SAFE_ALLOCA;
2544 SAFE_ALLOCA_LISP (args, nargs);
2545
2546 args[0] = func;
2547 for (i = 1; i < nargs; i++)
2548 args[i] = va_arg (ap, Lisp_Object);
2549
2550 specbind (Qinhibit_redisplay, Qt);
2551 if (inhibit_quit)
2552 specbind (Qinhibit_quit, Qt);
2553 /* Use Qt to ensure debugger does not run,
2554 so there is no possibility of wanting to redisplay. */
2555 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2556 safe_eval_handler);
2557 SAFE_FREE ();
2558 val = unbind_to (count, val);
2559 }
2560
2561 return val;
2562 }
2563
2564 Lisp_Object
2565 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2566 {
2567 Lisp_Object retval;
2568 va_list ap;
2569
2570 va_start (ap, func);
2571 retval = safe__call (false, nargs, func, ap);
2572 va_end (ap);
2573 return retval;
2574 }
2575
2576 /* Call function FN with one argument ARG.
2577 Return the result, or nil if something went wrong. */
2578
2579 Lisp_Object
2580 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2581 {
2582 return safe_call (2, fn, arg);
2583 }
2584
2585 static Lisp_Object
2586 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2587 {
2588 Lisp_Object retval;
2589 va_list ap;
2590
2591 va_start (ap, fn);
2592 retval = safe__call (inhibit_quit, 2, fn, ap);
2593 va_end (ap);
2594 return retval;
2595 }
2596
2597 Lisp_Object
2598 safe_eval (Lisp_Object sexpr)
2599 {
2600 return safe__call1 (false, Qeval, sexpr);
2601 }
2602
2603 static Lisp_Object
2604 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2605 {
2606 return safe__call1 (inhibit_quit, Qeval, sexpr);
2607 }
2608
2609 /* Call function FN with two arguments ARG1 and ARG2.
2610 Return the result, or nil if something went wrong. */
2611
2612 Lisp_Object
2613 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2614 {
2615 return safe_call (3, fn, arg1, arg2);
2616 }
2617
2618
2619 \f
2620 /***********************************************************************
2621 Debugging
2622 ***********************************************************************/
2623
2624 /* Define CHECK_IT to perform sanity checks on iterators.
2625 This is for debugging. It is too slow to do unconditionally. */
2626
2627 static void
2628 CHECK_IT (struct it *it)
2629 {
2630 #if false
2631 if (it->method == GET_FROM_STRING)
2632 {
2633 eassert (STRINGP (it->string));
2634 eassert (IT_STRING_CHARPOS (*it) >= 0);
2635 }
2636 else
2637 {
2638 eassert (IT_STRING_CHARPOS (*it) < 0);
2639 if (it->method == GET_FROM_BUFFER)
2640 {
2641 /* Check that character and byte positions agree. */
2642 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2643 }
2644 }
2645
2646 if (it->dpvec)
2647 eassert (it->current.dpvec_index >= 0);
2648 else
2649 eassert (it->current.dpvec_index < 0);
2650 #endif
2651 }
2652
2653
2654 /* Check that the window end of window W is what we expect it
2655 to be---the last row in the current matrix displaying text. */
2656
2657 static void
2658 CHECK_WINDOW_END (struct window *w)
2659 {
2660 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2661 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2662 {
2663 struct glyph_row *row;
2664 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2665 !row->enabled_p
2666 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2667 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2668 }
2669 #endif
2670 }
2671
2672 /***********************************************************************
2673 Iterator initialization
2674 ***********************************************************************/
2675
2676 /* Initialize IT for displaying current_buffer in window W, starting
2677 at character position CHARPOS. CHARPOS < 0 means that no buffer
2678 position is specified which is useful when the iterator is assigned
2679 a position later. BYTEPOS is the byte position corresponding to
2680 CHARPOS.
2681
2682 If ROW is not null, calls to produce_glyphs with IT as parameter
2683 will produce glyphs in that row.
2684
2685 BASE_FACE_ID is the id of a base face to use. It must be one of
2686 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2687 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2688 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2689
2690 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2691 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2692 will be initialized to use the corresponding mode line glyph row of
2693 the desired matrix of W. */
2694
2695 void
2696 init_iterator (struct it *it, struct window *w,
2697 ptrdiff_t charpos, ptrdiff_t bytepos,
2698 struct glyph_row *row, enum face_id base_face_id)
2699 {
2700 enum face_id remapped_base_face_id = base_face_id;
2701
2702 /* Some precondition checks. */
2703 eassert (w != NULL && it != NULL);
2704 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2705 && charpos <= ZV));
2706
2707 /* If face attributes have been changed since the last redisplay,
2708 free realized faces now because they depend on face definitions
2709 that might have changed. Don't free faces while there might be
2710 desired matrices pending which reference these faces. */
2711 if (!inhibit_free_realized_faces)
2712 {
2713 if (face_change)
2714 {
2715 face_change = false;
2716 free_all_realized_faces (Qnil);
2717 }
2718 else if (XFRAME (w->frame)->face_change)
2719 {
2720 XFRAME (w->frame)->face_change = 0;
2721 free_all_realized_faces (w->frame);
2722 }
2723 }
2724
2725 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2726 if (! NILP (Vface_remapping_alist))
2727 remapped_base_face_id
2728 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2729
2730 /* Use one of the mode line rows of W's desired matrix if
2731 appropriate. */
2732 if (row == NULL)
2733 {
2734 if (base_face_id == MODE_LINE_FACE_ID
2735 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2736 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2737 else if (base_face_id == HEADER_LINE_FACE_ID)
2738 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2739 }
2740
2741 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2742 Other parts of redisplay rely on that. */
2743 memclear (it, sizeof *it);
2744 it->current.overlay_string_index = -1;
2745 it->current.dpvec_index = -1;
2746 it->base_face_id = remapped_base_face_id;
2747 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2748 it->paragraph_embedding = L2R;
2749 it->bidi_it.w = w;
2750
2751 /* The window in which we iterate over current_buffer: */
2752 XSETWINDOW (it->window, w);
2753 it->w = w;
2754 it->f = XFRAME (w->frame);
2755
2756 it->cmp_it.id = -1;
2757
2758 /* Extra space between lines (on window systems only). */
2759 if (base_face_id == DEFAULT_FACE_ID
2760 && FRAME_WINDOW_P (it->f))
2761 {
2762 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2763 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2764 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2765 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2766 * FRAME_LINE_HEIGHT (it->f));
2767 else if (it->f->extra_line_spacing > 0)
2768 it->extra_line_spacing = it->f->extra_line_spacing;
2769 }
2770
2771 /* If realized faces have been removed, e.g. because of face
2772 attribute changes of named faces, recompute them. When running
2773 in batch mode, the face cache of the initial frame is null. If
2774 we happen to get called, make a dummy face cache. */
2775 if (FRAME_FACE_CACHE (it->f) == NULL)
2776 init_frame_faces (it->f);
2777 if (FRAME_FACE_CACHE (it->f)->used == 0)
2778 recompute_basic_faces (it->f);
2779
2780 it->override_ascent = -1;
2781
2782 /* Are control characters displayed as `^C'? */
2783 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2784
2785 /* -1 means everything between a CR and the following line end
2786 is invisible. >0 means lines indented more than this value are
2787 invisible. */
2788 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2789 ? (clip_to_bounds
2790 (-1, XINT (BVAR (current_buffer, selective_display)),
2791 PTRDIFF_MAX))
2792 : (!NILP (BVAR (current_buffer, selective_display))
2793 ? -1 : 0));
2794 it->selective_display_ellipsis_p
2795 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2796
2797 /* Display table to use. */
2798 it->dp = window_display_table (w);
2799
2800 /* Are multibyte characters enabled in current_buffer? */
2801 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2802
2803 /* Get the position at which the redisplay_end_trigger hook should
2804 be run, if it is to be run at all. */
2805 if (MARKERP (w->redisplay_end_trigger)
2806 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2807 it->redisplay_end_trigger_charpos
2808 = marker_position (w->redisplay_end_trigger);
2809 else if (INTEGERP (w->redisplay_end_trigger))
2810 it->redisplay_end_trigger_charpos
2811 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2812 PTRDIFF_MAX);
2813
2814 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2815
2816 /* Are lines in the display truncated? */
2817 if (TRUNCATE != 0)
2818 it->line_wrap = TRUNCATE;
2819 if (base_face_id == DEFAULT_FACE_ID
2820 && !it->w->hscroll
2821 && (WINDOW_FULL_WIDTH_P (it->w)
2822 || NILP (Vtruncate_partial_width_windows)
2823 || (INTEGERP (Vtruncate_partial_width_windows)
2824 /* PXW: Shall we do something about this? */
2825 && (XINT (Vtruncate_partial_width_windows)
2826 <= WINDOW_TOTAL_COLS (it->w))))
2827 && NILP (BVAR (current_buffer, truncate_lines)))
2828 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2829 ? WINDOW_WRAP : WORD_WRAP;
2830
2831 /* Get dimensions of truncation and continuation glyphs. These are
2832 displayed as fringe bitmaps under X, but we need them for such
2833 frames when the fringes are turned off. But leave the dimensions
2834 zero for tooltip frames, as these glyphs look ugly there and also
2835 sabotage calculations of tooltip dimensions in x-show-tip. */
2836 #ifdef HAVE_WINDOW_SYSTEM
2837 if (!(FRAME_WINDOW_P (it->f)
2838 && FRAMEP (tip_frame)
2839 && it->f == XFRAME (tip_frame)))
2840 #endif
2841 {
2842 if (it->line_wrap == TRUNCATE)
2843 {
2844 /* We will need the truncation glyph. */
2845 eassert (it->glyph_row == NULL);
2846 produce_special_glyphs (it, IT_TRUNCATION);
2847 it->truncation_pixel_width = it->pixel_width;
2848 }
2849 else
2850 {
2851 /* We will need the continuation glyph. */
2852 eassert (it->glyph_row == NULL);
2853 produce_special_glyphs (it, IT_CONTINUATION);
2854 it->continuation_pixel_width = it->pixel_width;
2855 }
2856 }
2857
2858 /* Reset these values to zero because the produce_special_glyphs
2859 above has changed them. */
2860 it->pixel_width = it->ascent = it->descent = 0;
2861 it->phys_ascent = it->phys_descent = 0;
2862
2863 /* Set this after getting the dimensions of truncation and
2864 continuation glyphs, so that we don't produce glyphs when calling
2865 produce_special_glyphs, above. */
2866 it->glyph_row = row;
2867 it->area = TEXT_AREA;
2868
2869 /* Get the dimensions of the display area. The display area
2870 consists of the visible window area plus a horizontally scrolled
2871 part to the left of the window. All x-values are relative to the
2872 start of this total display area. */
2873 if (base_face_id != DEFAULT_FACE_ID)
2874 {
2875 /* Mode lines, menu bar in terminal frames. */
2876 it->first_visible_x = 0;
2877 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2878 }
2879 else
2880 {
2881 it->first_visible_x
2882 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2883 it->last_visible_x = (it->first_visible_x
2884 + window_box_width (w, TEXT_AREA));
2885
2886 /* If we truncate lines, leave room for the truncation glyph(s) at
2887 the right margin. Otherwise, leave room for the continuation
2888 glyph(s). Done only if the window has no right fringe. */
2889 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2890 {
2891 if (it->line_wrap == TRUNCATE)
2892 it->last_visible_x -= it->truncation_pixel_width;
2893 else
2894 it->last_visible_x -= it->continuation_pixel_width;
2895 }
2896
2897 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2898 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2899 }
2900
2901 /* Leave room for a border glyph. */
2902 if (!FRAME_WINDOW_P (it->f)
2903 && !WINDOW_RIGHTMOST_P (it->w))
2904 it->last_visible_x -= 1;
2905
2906 it->last_visible_y = window_text_bottom_y (w);
2907
2908 /* For mode lines and alike, arrange for the first glyph having a
2909 left box line if the face specifies a box. */
2910 if (base_face_id != DEFAULT_FACE_ID)
2911 {
2912 struct face *face;
2913
2914 it->face_id = remapped_base_face_id;
2915
2916 /* If we have a boxed mode line, make the first character appear
2917 with a left box line. */
2918 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2919 if (face && face->box != FACE_NO_BOX)
2920 it->start_of_box_run_p = true;
2921 }
2922
2923 /* If a buffer position was specified, set the iterator there,
2924 getting overlays and face properties from that position. */
2925 if (charpos >= BUF_BEG (current_buffer))
2926 {
2927 it->stop_charpos = charpos;
2928 it->end_charpos = ZV;
2929 eassert (charpos == BYTE_TO_CHAR (bytepos));
2930 IT_CHARPOS (*it) = charpos;
2931 IT_BYTEPOS (*it) = bytepos;
2932
2933 /* We will rely on `reseat' to set this up properly, via
2934 handle_face_prop. */
2935 it->face_id = it->base_face_id;
2936
2937 it->start = it->current;
2938 /* Do we need to reorder bidirectional text? Not if this is a
2939 unibyte buffer: by definition, none of the single-byte
2940 characters are strong R2L, so no reordering is needed. And
2941 bidi.c doesn't support unibyte buffers anyway. Also, don't
2942 reorder while we are loading loadup.el, since the tables of
2943 character properties needed for reordering are not yet
2944 available. */
2945 it->bidi_p =
2946 NILP (Vpurify_flag)
2947 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2948 && it->multibyte_p;
2949
2950 /* If we are to reorder bidirectional text, init the bidi
2951 iterator. */
2952 if (it->bidi_p)
2953 {
2954 /* Since we don't know at this point whether there will be
2955 any R2L lines in the window, we reserve space for
2956 truncation/continuation glyphs even if only the left
2957 fringe is absent. */
2958 if (base_face_id == DEFAULT_FACE_ID
2959 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2960 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2961 {
2962 if (it->line_wrap == TRUNCATE)
2963 it->last_visible_x -= it->truncation_pixel_width;
2964 else
2965 it->last_visible_x -= it->continuation_pixel_width;
2966 }
2967 /* Note the paragraph direction that this buffer wants to
2968 use. */
2969 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2970 Qleft_to_right))
2971 it->paragraph_embedding = L2R;
2972 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2973 Qright_to_left))
2974 it->paragraph_embedding = R2L;
2975 else
2976 it->paragraph_embedding = NEUTRAL_DIR;
2977 bidi_unshelve_cache (NULL, false);
2978 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2979 &it->bidi_it);
2980 }
2981
2982 /* Compute faces etc. */
2983 reseat (it, it->current.pos, true);
2984 }
2985
2986 CHECK_IT (it);
2987 }
2988
2989
2990 /* Initialize IT for the display of window W with window start POS. */
2991
2992 void
2993 start_display (struct it *it, struct window *w, struct text_pos pos)
2994 {
2995 struct glyph_row *row;
2996 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
2997
2998 row = w->desired_matrix->rows + first_vpos;
2999 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3000 it->first_vpos = first_vpos;
3001
3002 /* Don't reseat to previous visible line start if current start
3003 position is in a string or image. */
3004 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3005 {
3006 int first_y = it->current_y;
3007
3008 /* If window start is not at a line start, skip forward to POS to
3009 get the correct continuation lines width. */
3010 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3011 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3012 if (!start_at_line_beg_p)
3013 {
3014 int new_x;
3015
3016 reseat_at_previous_visible_line_start (it);
3017 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3018
3019 new_x = it->current_x + it->pixel_width;
3020
3021 /* If lines are continued, this line may end in the middle
3022 of a multi-glyph character (e.g. a control character
3023 displayed as \003, or in the middle of an overlay
3024 string). In this case move_it_to above will not have
3025 taken us to the start of the continuation line but to the
3026 end of the continued line. */
3027 if (it->current_x > 0
3028 && it->line_wrap != TRUNCATE /* Lines are continued. */
3029 && (/* And glyph doesn't fit on the line. */
3030 new_x > it->last_visible_x
3031 /* Or it fits exactly and we're on a window
3032 system frame. */
3033 || (new_x == it->last_visible_x
3034 && FRAME_WINDOW_P (it->f)
3035 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3036 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3037 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3038 {
3039 if ((it->current.dpvec_index >= 0
3040 || it->current.overlay_string_index >= 0)
3041 /* If we are on a newline from a display vector or
3042 overlay string, then we are already at the end of
3043 a screen line; no need to go to the next line in
3044 that case, as this line is not really continued.
3045 (If we do go to the next line, C-e will not DTRT.) */
3046 && it->c != '\n')
3047 {
3048 set_iterator_to_next (it, true);
3049 move_it_in_display_line_to (it, -1, -1, 0);
3050 }
3051
3052 it->continuation_lines_width += it->current_x;
3053 }
3054 /* If the character at POS is displayed via a display
3055 vector, move_it_to above stops at the final glyph of
3056 IT->dpvec. To make the caller redisplay that character
3057 again (a.k.a. start at POS), we need to reset the
3058 dpvec_index to the beginning of IT->dpvec. */
3059 else if (it->current.dpvec_index >= 0)
3060 it->current.dpvec_index = 0;
3061
3062 /* We're starting a new display line, not affected by the
3063 height of the continued line, so clear the appropriate
3064 fields in the iterator structure. */
3065 it->max_ascent = it->max_descent = 0;
3066 it->max_phys_ascent = it->max_phys_descent = 0;
3067
3068 it->current_y = first_y;
3069 it->vpos = 0;
3070 it->current_x = it->hpos = 0;
3071 }
3072 }
3073 }
3074
3075
3076 /* Return true if POS is a position in ellipses displayed for invisible
3077 text. W is the window we display, for text property lookup. */
3078
3079 static bool
3080 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3081 {
3082 Lisp_Object prop, window;
3083 bool ellipses_p = false;
3084 ptrdiff_t charpos = CHARPOS (pos->pos);
3085
3086 /* If POS specifies a position in a display vector, this might
3087 be for an ellipsis displayed for invisible text. We won't
3088 get the iterator set up for delivering that ellipsis unless
3089 we make sure that it gets aware of the invisible text. */
3090 if (pos->dpvec_index >= 0
3091 && pos->overlay_string_index < 0
3092 && CHARPOS (pos->string_pos) < 0
3093 && charpos > BEGV
3094 && (XSETWINDOW (window, w),
3095 prop = Fget_char_property (make_number (charpos),
3096 Qinvisible, window),
3097 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3098 {
3099 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3100 window);
3101 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3102 }
3103
3104 return ellipses_p;
3105 }
3106
3107
3108 /* Initialize IT for stepping through current_buffer in window W,
3109 starting at position POS that includes overlay string and display
3110 vector/ control character translation position information. Value
3111 is false if there are overlay strings with newlines at POS. */
3112
3113 static bool
3114 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3115 {
3116 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3117 int i;
3118 bool overlay_strings_with_newlines = false;
3119
3120 /* If POS specifies a position in a display vector, this might
3121 be for an ellipsis displayed for invisible text. We won't
3122 get the iterator set up for delivering that ellipsis unless
3123 we make sure that it gets aware of the invisible text. */
3124 if (in_ellipses_for_invisible_text_p (pos, w))
3125 {
3126 --charpos;
3127 bytepos = 0;
3128 }
3129
3130 /* Keep in mind: the call to reseat in init_iterator skips invisible
3131 text, so we might end up at a position different from POS. This
3132 is only a problem when POS is a row start after a newline and an
3133 overlay starts there with an after-string, and the overlay has an
3134 invisible property. Since we don't skip invisible text in
3135 display_line and elsewhere immediately after consuming the
3136 newline before the row start, such a POS will not be in a string,
3137 but the call to init_iterator below will move us to the
3138 after-string. */
3139 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3140
3141 /* This only scans the current chunk -- it should scan all chunks.
3142 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3143 to 16 in 22.1 to make this a lesser problem. */
3144 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3145 {
3146 const char *s = SSDATA (it->overlay_strings[i]);
3147 const char *e = s + SBYTES (it->overlay_strings[i]);
3148
3149 while (s < e && *s != '\n')
3150 ++s;
3151
3152 if (s < e)
3153 {
3154 overlay_strings_with_newlines = true;
3155 break;
3156 }
3157 }
3158
3159 /* If position is within an overlay string, set up IT to the right
3160 overlay string. */
3161 if (pos->overlay_string_index >= 0)
3162 {
3163 int relative_index;
3164
3165 /* If the first overlay string happens to have a `display'
3166 property for an image, the iterator will be set up for that
3167 image, and we have to undo that setup first before we can
3168 correct the overlay string index. */
3169 if (it->method == GET_FROM_IMAGE)
3170 pop_it (it);
3171
3172 /* We already have the first chunk of overlay strings in
3173 IT->overlay_strings. Load more until the one for
3174 pos->overlay_string_index is in IT->overlay_strings. */
3175 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3176 {
3177 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3178 it->current.overlay_string_index = 0;
3179 while (n--)
3180 {
3181 load_overlay_strings (it, 0);
3182 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3183 }
3184 }
3185
3186 it->current.overlay_string_index = pos->overlay_string_index;
3187 relative_index = (it->current.overlay_string_index
3188 % OVERLAY_STRING_CHUNK_SIZE);
3189 it->string = it->overlay_strings[relative_index];
3190 eassert (STRINGP (it->string));
3191 it->current.string_pos = pos->string_pos;
3192 it->method = GET_FROM_STRING;
3193 it->end_charpos = SCHARS (it->string);
3194 /* Set up the bidi iterator for this overlay string. */
3195 if (it->bidi_p)
3196 {
3197 it->bidi_it.string.lstring = it->string;
3198 it->bidi_it.string.s = NULL;
3199 it->bidi_it.string.schars = SCHARS (it->string);
3200 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3201 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3202 it->bidi_it.string.unibyte = !it->multibyte_p;
3203 it->bidi_it.w = it->w;
3204 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3205 FRAME_WINDOW_P (it->f), &it->bidi_it);
3206
3207 /* Synchronize the state of the bidi iterator with
3208 pos->string_pos. For any string position other than
3209 zero, this will be done automagically when we resume
3210 iteration over the string and get_visually_first_element
3211 is called. But if string_pos is zero, and the string is
3212 to be reordered for display, we need to resync manually,
3213 since it could be that the iteration state recorded in
3214 pos ended at string_pos of 0 moving backwards in string. */
3215 if (CHARPOS (pos->string_pos) == 0)
3216 {
3217 get_visually_first_element (it);
3218 if (IT_STRING_CHARPOS (*it) != 0)
3219 do {
3220 /* Paranoia. */
3221 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3222 bidi_move_to_visually_next (&it->bidi_it);
3223 } while (it->bidi_it.charpos != 0);
3224 }
3225 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3226 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3227 }
3228 }
3229
3230 if (CHARPOS (pos->string_pos) >= 0)
3231 {
3232 /* Recorded position is not in an overlay string, but in another
3233 string. This can only be a string from a `display' property.
3234 IT should already be filled with that string. */
3235 it->current.string_pos = pos->string_pos;
3236 eassert (STRINGP (it->string));
3237 if (it->bidi_p)
3238 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3239 FRAME_WINDOW_P (it->f), &it->bidi_it);
3240 }
3241
3242 /* Restore position in display vector translations, control
3243 character translations or ellipses. */
3244 if (pos->dpvec_index >= 0)
3245 {
3246 if (it->dpvec == NULL)
3247 get_next_display_element (it);
3248 eassert (it->dpvec && it->current.dpvec_index == 0);
3249 it->current.dpvec_index = pos->dpvec_index;
3250 }
3251
3252 CHECK_IT (it);
3253 return !overlay_strings_with_newlines;
3254 }
3255
3256
3257 /* Initialize IT for stepping through current_buffer in window W
3258 starting at ROW->start. */
3259
3260 static void
3261 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3262 {
3263 init_from_display_pos (it, w, &row->start);
3264 it->start = row->start;
3265 it->continuation_lines_width = row->continuation_lines_width;
3266 CHECK_IT (it);
3267 }
3268
3269
3270 /* Initialize IT for stepping through current_buffer in window W
3271 starting in the line following ROW, i.e. starting at ROW->end.
3272 Value is false if there are overlay strings with newlines at ROW's
3273 end position. */
3274
3275 static bool
3276 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3277 {
3278 bool success = false;
3279
3280 if (init_from_display_pos (it, w, &row->end))
3281 {
3282 if (row->continued_p)
3283 it->continuation_lines_width
3284 = row->continuation_lines_width + row->pixel_width;
3285 CHECK_IT (it);
3286 success = true;
3287 }
3288
3289 return success;
3290 }
3291
3292
3293
3294 \f
3295 /***********************************************************************
3296 Text properties
3297 ***********************************************************************/
3298
3299 /* Called when IT reaches IT->stop_charpos. Handle text property and
3300 overlay changes. Set IT->stop_charpos to the next position where
3301 to stop. */
3302
3303 static void
3304 handle_stop (struct it *it)
3305 {
3306 enum prop_handled handled;
3307 bool handle_overlay_change_p;
3308 struct props *p;
3309
3310 it->dpvec = NULL;
3311 it->current.dpvec_index = -1;
3312 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3313 it->ellipsis_p = false;
3314
3315 /* Use face of preceding text for ellipsis (if invisible) */
3316 if (it->selective_display_ellipsis_p)
3317 it->saved_face_id = it->face_id;
3318
3319 /* Here's the description of the semantics of, and the logic behind,
3320 the various HANDLED_* statuses:
3321
3322 HANDLED_NORMALLY means the handler did its job, and the loop
3323 should proceed to calling the next handler in order.
3324
3325 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3326 change in the properties and overlays at current position, so the
3327 loop should be restarted, to re-invoke the handlers that were
3328 already called. This happens when fontification-functions were
3329 called by handle_fontified_prop, and actually fontified
3330 something. Another case where HANDLED_RECOMPUTE_PROPS is
3331 returned is when we discover overlay strings that need to be
3332 displayed right away. The loop below will continue for as long
3333 as the status is HANDLED_RECOMPUTE_PROPS.
3334
3335 HANDLED_RETURN means return immediately to the caller, to
3336 continue iteration without calling any further handlers. This is
3337 used when we need to act on some property right away, for example
3338 when we need to display the ellipsis or a replacing display
3339 property, such as display string or image.
3340
3341 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3342 consumed, and the handler switched to the next overlay string.
3343 This signals the loop below to refrain from looking for more
3344 overlays before all the overlay strings of the current overlay
3345 are processed.
3346
3347 Some of the handlers called by the loop push the iterator state
3348 onto the stack (see 'push_it'), and arrange for the iteration to
3349 continue with another object, such as an image, a display string,
3350 or an overlay string. In most such cases, it->stop_charpos is
3351 set to the first character of the string, so that when the
3352 iteration resumes, this function will immediately be called
3353 again, to examine the properties at the beginning of the string.
3354
3355 When a display or overlay string is exhausted, the iterator state
3356 is popped (see 'pop_it'), and iteration continues with the
3357 previous object. Again, in many such cases this function is
3358 called again to find the next position where properties might
3359 change. */
3360
3361 do
3362 {
3363 handled = HANDLED_NORMALLY;
3364
3365 /* Call text property handlers. */
3366 for (p = it_props; p->handler; ++p)
3367 {
3368 handled = p->handler (it);
3369
3370 if (handled == HANDLED_RECOMPUTE_PROPS)
3371 break;
3372 else if (handled == HANDLED_RETURN)
3373 {
3374 /* We still want to show before and after strings from
3375 overlays even if the actual buffer text is replaced. */
3376 if (!handle_overlay_change_p
3377 || it->sp > 1
3378 /* Don't call get_overlay_strings_1 if we already
3379 have overlay strings loaded, because doing so
3380 will load them again and push the iterator state
3381 onto the stack one more time, which is not
3382 expected by the rest of the code that processes
3383 overlay strings. */
3384 || (it->current.overlay_string_index < 0
3385 && !get_overlay_strings_1 (it, 0, false)))
3386 {
3387 if (it->ellipsis_p)
3388 setup_for_ellipsis (it, 0);
3389 /* When handling a display spec, we might load an
3390 empty string. In that case, discard it here. We
3391 used to discard it in handle_single_display_spec,
3392 but that causes get_overlay_strings_1, above, to
3393 ignore overlay strings that we must check. */
3394 if (STRINGP (it->string) && !SCHARS (it->string))
3395 pop_it (it);
3396 return;
3397 }
3398 else if (STRINGP (it->string) && !SCHARS (it->string))
3399 pop_it (it);
3400 else
3401 {
3402 it->string_from_display_prop_p = false;
3403 it->from_disp_prop_p = false;
3404 handle_overlay_change_p = false;
3405 }
3406 handled = HANDLED_RECOMPUTE_PROPS;
3407 break;
3408 }
3409 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3410 handle_overlay_change_p = false;
3411 }
3412
3413 if (handled != HANDLED_RECOMPUTE_PROPS)
3414 {
3415 /* Don't check for overlay strings below when set to deliver
3416 characters from a display vector. */
3417 if (it->method == GET_FROM_DISPLAY_VECTOR)
3418 handle_overlay_change_p = false;
3419
3420 /* Handle overlay changes.
3421 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3422 if it finds overlays. */
3423 if (handle_overlay_change_p)
3424 handled = handle_overlay_change (it);
3425 }
3426
3427 if (it->ellipsis_p)
3428 {
3429 setup_for_ellipsis (it, 0);
3430 break;
3431 }
3432 }
3433 while (handled == HANDLED_RECOMPUTE_PROPS);
3434
3435 /* Determine where to stop next. */
3436 if (handled == HANDLED_NORMALLY)
3437 compute_stop_pos (it);
3438 }
3439
3440
3441 /* Compute IT->stop_charpos from text property and overlay change
3442 information for IT's current position. */
3443
3444 static void
3445 compute_stop_pos (struct it *it)
3446 {
3447 register INTERVAL iv, next_iv;
3448 Lisp_Object object, limit, position;
3449 ptrdiff_t charpos, bytepos;
3450
3451 if (STRINGP (it->string))
3452 {
3453 /* Strings are usually short, so don't limit the search for
3454 properties. */
3455 it->stop_charpos = it->end_charpos;
3456 object = it->string;
3457 limit = Qnil;
3458 charpos = IT_STRING_CHARPOS (*it);
3459 bytepos = IT_STRING_BYTEPOS (*it);
3460 }
3461 else
3462 {
3463 ptrdiff_t pos;
3464
3465 /* If end_charpos is out of range for some reason, such as a
3466 misbehaving display function, rationalize it (Bug#5984). */
3467 if (it->end_charpos > ZV)
3468 it->end_charpos = ZV;
3469 it->stop_charpos = it->end_charpos;
3470
3471 /* If next overlay change is in front of the current stop pos
3472 (which is IT->end_charpos), stop there. Note: value of
3473 next_overlay_change is point-max if no overlay change
3474 follows. */
3475 charpos = IT_CHARPOS (*it);
3476 bytepos = IT_BYTEPOS (*it);
3477 pos = next_overlay_change (charpos);
3478 if (pos < it->stop_charpos)
3479 it->stop_charpos = pos;
3480
3481 /* Set up variables for computing the stop position from text
3482 property changes. */
3483 XSETBUFFER (object, current_buffer);
3484 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3485 }
3486
3487 /* Get the interval containing IT's position. Value is a null
3488 interval if there isn't such an interval. */
3489 position = make_number (charpos);
3490 iv = validate_interval_range (object, &position, &position, false);
3491 if (iv)
3492 {
3493 Lisp_Object values_here[LAST_PROP_IDX];
3494 struct props *p;
3495
3496 /* Get properties here. */
3497 for (p = it_props; p->handler; ++p)
3498 values_here[p->idx] = textget (iv->plist,
3499 builtin_lisp_symbol (p->name));
3500
3501 /* Look for an interval following iv that has different
3502 properties. */
3503 for (next_iv = next_interval (iv);
3504 (next_iv
3505 && (NILP (limit)
3506 || XFASTINT (limit) > next_iv->position));
3507 next_iv = next_interval (next_iv))
3508 {
3509 for (p = it_props; p->handler; ++p)
3510 {
3511 Lisp_Object new_value = textget (next_iv->plist,
3512 builtin_lisp_symbol (p->name));
3513 if (!EQ (values_here[p->idx], new_value))
3514 break;
3515 }
3516
3517 if (p->handler)
3518 break;
3519 }
3520
3521 if (next_iv)
3522 {
3523 if (INTEGERP (limit)
3524 && next_iv->position >= XFASTINT (limit))
3525 /* No text property change up to limit. */
3526 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3527 else
3528 /* Text properties change in next_iv. */
3529 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3530 }
3531 }
3532
3533 if (it->cmp_it.id < 0)
3534 {
3535 ptrdiff_t stoppos = it->end_charpos;
3536
3537 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3538 stoppos = -1;
3539 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3540 stoppos, it->string);
3541 }
3542
3543 eassert (STRINGP (it->string)
3544 || (it->stop_charpos >= BEGV
3545 && it->stop_charpos >= IT_CHARPOS (*it)));
3546 }
3547
3548
3549 /* Return the position of the next overlay change after POS in
3550 current_buffer. Value is point-max if no overlay change
3551 follows. This is like `next-overlay-change' but doesn't use
3552 xmalloc. */
3553
3554 static ptrdiff_t
3555 next_overlay_change (ptrdiff_t pos)
3556 {
3557 ptrdiff_t i, noverlays;
3558 ptrdiff_t endpos;
3559 Lisp_Object *overlays;
3560 USE_SAFE_ALLOCA;
3561
3562 /* Get all overlays at the given position. */
3563 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3564
3565 /* If any of these overlays ends before endpos,
3566 use its ending point instead. */
3567 for (i = 0; i < noverlays; ++i)
3568 {
3569 Lisp_Object oend;
3570 ptrdiff_t oendpos;
3571
3572 oend = OVERLAY_END (overlays[i]);
3573 oendpos = OVERLAY_POSITION (oend);
3574 endpos = min (endpos, oendpos);
3575 }
3576
3577 SAFE_FREE ();
3578 return endpos;
3579 }
3580
3581 /* How many characters forward to search for a display property or
3582 display string. Searching too far forward makes the bidi display
3583 sluggish, especially in small windows. */
3584 #define MAX_DISP_SCAN 250
3585
3586 /* Return the character position of a display string at or after
3587 position specified by POSITION. If no display string exists at or
3588 after POSITION, return ZV. A display string is either an overlay
3589 with `display' property whose value is a string, or a `display'
3590 text property whose value is a string. STRING is data about the
3591 string to iterate; if STRING->lstring is nil, we are iterating a
3592 buffer. FRAME_WINDOW_P is true when we are displaying a window
3593 on a GUI frame. DISP_PROP is set to zero if we searched
3594 MAX_DISP_SCAN characters forward without finding any display
3595 strings, non-zero otherwise. It is set to 2 if the display string
3596 uses any kind of `(space ...)' spec that will produce a stretch of
3597 white space in the text area. */
3598 ptrdiff_t
3599 compute_display_string_pos (struct text_pos *position,
3600 struct bidi_string_data *string,
3601 struct window *w,
3602 bool frame_window_p, int *disp_prop)
3603 {
3604 /* OBJECT = nil means current buffer. */
3605 Lisp_Object object, object1;
3606 Lisp_Object pos, spec, limpos;
3607 bool string_p = string && (STRINGP (string->lstring) || string->s);
3608 ptrdiff_t eob = string_p ? string->schars : ZV;
3609 ptrdiff_t begb = string_p ? 0 : BEGV;
3610 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3611 ptrdiff_t lim =
3612 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3613 struct text_pos tpos;
3614 int rv = 0;
3615
3616 if (string && STRINGP (string->lstring))
3617 object1 = object = string->lstring;
3618 else if (w && !string_p)
3619 {
3620 XSETWINDOW (object, w);
3621 object1 = Qnil;
3622 }
3623 else
3624 object1 = object = Qnil;
3625
3626 *disp_prop = 1;
3627
3628 if (charpos >= eob
3629 /* We don't support display properties whose values are strings
3630 that have display string properties. */
3631 || string->from_disp_str
3632 /* C strings cannot have display properties. */
3633 || (string->s && !STRINGP (object)))
3634 {
3635 *disp_prop = 0;
3636 return eob;
3637 }
3638
3639 /* If the character at CHARPOS is where the display string begins,
3640 return CHARPOS. */
3641 pos = make_number (charpos);
3642 if (STRINGP (object))
3643 bufpos = string->bufpos;
3644 else
3645 bufpos = charpos;
3646 tpos = *position;
3647 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3648 && (charpos <= begb
3649 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3650 object),
3651 spec))
3652 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3653 frame_window_p)))
3654 {
3655 if (rv == 2)
3656 *disp_prop = 2;
3657 return charpos;
3658 }
3659
3660 /* Look forward for the first character with a `display' property
3661 that will replace the underlying text when displayed. */
3662 limpos = make_number (lim);
3663 do {
3664 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3665 CHARPOS (tpos) = XFASTINT (pos);
3666 if (CHARPOS (tpos) >= lim)
3667 {
3668 *disp_prop = 0;
3669 break;
3670 }
3671 if (STRINGP (object))
3672 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3673 else
3674 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3675 spec = Fget_char_property (pos, Qdisplay, object);
3676 if (!STRINGP (object))
3677 bufpos = CHARPOS (tpos);
3678 } while (NILP (spec)
3679 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3680 bufpos, frame_window_p)));
3681 if (rv == 2)
3682 *disp_prop = 2;
3683
3684 return CHARPOS (tpos);
3685 }
3686
3687 /* Return the character position of the end of the display string that
3688 started at CHARPOS. If there's no display string at CHARPOS,
3689 return -1. A display string is either an overlay with `display'
3690 property whose value is a string or a `display' text property whose
3691 value is a string. */
3692 ptrdiff_t
3693 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3694 {
3695 /* OBJECT = nil means current buffer. */
3696 Lisp_Object object =
3697 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3698 Lisp_Object pos = make_number (charpos);
3699 ptrdiff_t eob =
3700 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3701
3702 if (charpos >= eob || (string->s && !STRINGP (object)))
3703 return eob;
3704
3705 /* It could happen that the display property or overlay was removed
3706 since we found it in compute_display_string_pos above. One way
3707 this can happen is if JIT font-lock was called (through
3708 handle_fontified_prop), and jit-lock-functions remove text
3709 properties or overlays from the portion of buffer that includes
3710 CHARPOS. Muse mode is known to do that, for example. In this
3711 case, we return -1 to the caller, to signal that no display
3712 string is actually present at CHARPOS. See bidi_fetch_char for
3713 how this is handled.
3714
3715 An alternative would be to never look for display properties past
3716 it->stop_charpos. But neither compute_display_string_pos nor
3717 bidi_fetch_char that calls it know or care where the next
3718 stop_charpos is. */
3719 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3720 return -1;
3721
3722 /* Look forward for the first character where the `display' property
3723 changes. */
3724 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3725
3726 return XFASTINT (pos);
3727 }
3728
3729
3730 \f
3731 /***********************************************************************
3732 Fontification
3733 ***********************************************************************/
3734
3735 /* Handle changes in the `fontified' property of the current buffer by
3736 calling hook functions from Qfontification_functions to fontify
3737 regions of text. */
3738
3739 static enum prop_handled
3740 handle_fontified_prop (struct it *it)
3741 {
3742 Lisp_Object prop, pos;
3743 enum prop_handled handled = HANDLED_NORMALLY;
3744
3745 if (!NILP (Vmemory_full))
3746 return handled;
3747
3748 /* Get the value of the `fontified' property at IT's current buffer
3749 position. (The `fontified' property doesn't have a special
3750 meaning in strings.) If the value is nil, call functions from
3751 Qfontification_functions. */
3752 if (!STRINGP (it->string)
3753 && it->s == NULL
3754 && !NILP (Vfontification_functions)
3755 && !NILP (Vrun_hooks)
3756 && (pos = make_number (IT_CHARPOS (*it)),
3757 prop = Fget_char_property (pos, Qfontified, Qnil),
3758 /* Ignore the special cased nil value always present at EOB since
3759 no amount of fontifying will be able to change it. */
3760 NILP (prop) && IT_CHARPOS (*it) < Z))
3761 {
3762 ptrdiff_t count = SPECPDL_INDEX ();
3763 Lisp_Object val;
3764 struct buffer *obuf = current_buffer;
3765 ptrdiff_t begv = BEGV, zv = ZV;
3766 bool old_clip_changed = current_buffer->clip_changed;
3767
3768 val = Vfontification_functions;
3769 specbind (Qfontification_functions, Qnil);
3770
3771 eassert (it->end_charpos == ZV);
3772
3773 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3774 safe_call1 (val, pos);
3775 else
3776 {
3777 Lisp_Object fns, fn;
3778
3779 fns = Qnil;
3780
3781 for (; CONSP (val); val = XCDR (val))
3782 {
3783 fn = XCAR (val);
3784
3785 if (EQ (fn, Qt))
3786 {
3787 /* A value of t indicates this hook has a local
3788 binding; it means to run the global binding too.
3789 In a global value, t should not occur. If it
3790 does, we must ignore it to avoid an endless
3791 loop. */
3792 for (fns = Fdefault_value (Qfontification_functions);
3793 CONSP (fns);
3794 fns = XCDR (fns))
3795 {
3796 fn = XCAR (fns);
3797 if (!EQ (fn, Qt))
3798 safe_call1 (fn, pos);
3799 }
3800 }
3801 else
3802 safe_call1 (fn, pos);
3803 }
3804 }
3805
3806 unbind_to (count, Qnil);
3807
3808 /* Fontification functions routinely call `save-restriction'.
3809 Normally, this tags clip_changed, which can confuse redisplay
3810 (see discussion in Bug#6671). Since we don't perform any
3811 special handling of fontification changes in the case where
3812 `save-restriction' isn't called, there's no point doing so in
3813 this case either. So, if the buffer's restrictions are
3814 actually left unchanged, reset clip_changed. */
3815 if (obuf == current_buffer)
3816 {
3817 if (begv == BEGV && zv == ZV)
3818 current_buffer->clip_changed = old_clip_changed;
3819 }
3820 /* There isn't much we can reasonably do to protect against
3821 misbehaving fontification, but here's a fig leaf. */
3822 else if (BUFFER_LIVE_P (obuf))
3823 set_buffer_internal_1 (obuf);
3824
3825 /* The fontification code may have added/removed text.
3826 It could do even a lot worse, but let's at least protect against
3827 the most obvious case where only the text past `pos' gets changed',
3828 as is/was done in grep.el where some escapes sequences are turned
3829 into face properties (bug#7876). */
3830 it->end_charpos = ZV;
3831
3832 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3833 something. This avoids an endless loop if they failed to
3834 fontify the text for which reason ever. */
3835 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3836 handled = HANDLED_RECOMPUTE_PROPS;
3837 }
3838
3839 return handled;
3840 }
3841
3842
3843 \f
3844 /***********************************************************************
3845 Faces
3846 ***********************************************************************/
3847
3848 /* Set up iterator IT from face properties at its current position.
3849 Called from handle_stop. */
3850
3851 static enum prop_handled
3852 handle_face_prop (struct it *it)
3853 {
3854 int new_face_id;
3855 ptrdiff_t next_stop;
3856
3857 if (!STRINGP (it->string))
3858 {
3859 new_face_id
3860 = face_at_buffer_position (it->w,
3861 IT_CHARPOS (*it),
3862 &next_stop,
3863 (IT_CHARPOS (*it)
3864 + TEXT_PROP_DISTANCE_LIMIT),
3865 false, it->base_face_id);
3866
3867 /* Is this a start of a run of characters with box face?
3868 Caveat: this can be called for a freshly initialized
3869 iterator; face_id is -1 in this case. We know that the new
3870 face will not change until limit, i.e. if the new face has a
3871 box, all characters up to limit will have one. But, as
3872 usual, we don't know whether limit is really the end. */
3873 if (new_face_id != it->face_id)
3874 {
3875 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3876 /* If it->face_id is -1, old_face below will be NULL, see
3877 the definition of FACE_FROM_ID. This will happen if this
3878 is the initial call that gets the face. */
3879 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3880
3881 /* If the value of face_id of the iterator is -1, we have to
3882 look in front of IT's position and see whether there is a
3883 face there that's different from new_face_id. */
3884 if (!old_face && IT_CHARPOS (*it) > BEG)
3885 {
3886 int prev_face_id = face_before_it_pos (it);
3887
3888 old_face = FACE_FROM_ID (it->f, prev_face_id);
3889 }
3890
3891 /* If the new face has a box, but the old face does not,
3892 this is the start of a run of characters with box face,
3893 i.e. this character has a shadow on the left side. */
3894 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3895 && (old_face == NULL || !old_face->box));
3896 it->face_box_p = new_face->box != FACE_NO_BOX;
3897 }
3898 }
3899 else
3900 {
3901 int base_face_id;
3902 ptrdiff_t bufpos;
3903 int i;
3904 Lisp_Object from_overlay
3905 = (it->current.overlay_string_index >= 0
3906 ? it->string_overlays[it->current.overlay_string_index
3907 % OVERLAY_STRING_CHUNK_SIZE]
3908 : Qnil);
3909
3910 /* See if we got to this string directly or indirectly from
3911 an overlay property. That includes the before-string or
3912 after-string of an overlay, strings in display properties
3913 provided by an overlay, their text properties, etc.
3914
3915 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3916 if (! NILP (from_overlay))
3917 for (i = it->sp - 1; i >= 0; i--)
3918 {
3919 if (it->stack[i].current.overlay_string_index >= 0)
3920 from_overlay
3921 = it->string_overlays[it->stack[i].current.overlay_string_index
3922 % OVERLAY_STRING_CHUNK_SIZE];
3923 else if (! NILP (it->stack[i].from_overlay))
3924 from_overlay = it->stack[i].from_overlay;
3925
3926 if (!NILP (from_overlay))
3927 break;
3928 }
3929
3930 if (! NILP (from_overlay))
3931 {
3932 bufpos = IT_CHARPOS (*it);
3933 /* For a string from an overlay, the base face depends
3934 only on text properties and ignores overlays. */
3935 base_face_id
3936 = face_for_overlay_string (it->w,
3937 IT_CHARPOS (*it),
3938 &next_stop,
3939 (IT_CHARPOS (*it)
3940 + TEXT_PROP_DISTANCE_LIMIT),
3941 false,
3942 from_overlay);
3943 }
3944 else
3945 {
3946 bufpos = 0;
3947
3948 /* For strings from a `display' property, use the face at
3949 IT's current buffer position as the base face to merge
3950 with, so that overlay strings appear in the same face as
3951 surrounding text, unless they specify their own faces.
3952 For strings from wrap-prefix and line-prefix properties,
3953 use the default face, possibly remapped via
3954 Vface_remapping_alist. */
3955 /* Note that the fact that we use the face at _buffer_
3956 position means that a 'display' property on an overlay
3957 string will not inherit the face of that overlay string,
3958 but will instead revert to the face of buffer text
3959 covered by the overlay. This is visible, e.g., when the
3960 overlay specifies a box face, but neither the buffer nor
3961 the display string do. This sounds like a design bug,
3962 but Emacs always did that since v21.1, so changing that
3963 might be a big deal. */
3964 base_face_id = it->string_from_prefix_prop_p
3965 ? (!NILP (Vface_remapping_alist)
3966 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3967 : DEFAULT_FACE_ID)
3968 : underlying_face_id (it);
3969 }
3970
3971 new_face_id = face_at_string_position (it->w,
3972 it->string,
3973 IT_STRING_CHARPOS (*it),
3974 bufpos,
3975 &next_stop,
3976 base_face_id, false);
3977
3978 /* Is this a start of a run of characters with box? Caveat:
3979 this can be called for a freshly allocated iterator; face_id
3980 is -1 is this case. We know that the new face will not
3981 change until the next check pos, i.e. if the new face has a
3982 box, all characters up to that position will have a
3983 box. But, as usual, we don't know whether that position
3984 is really the end. */
3985 if (new_face_id != it->face_id)
3986 {
3987 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3988 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3989
3990 /* If new face has a box but old face hasn't, this is the
3991 start of a run of characters with box, i.e. it has a
3992 shadow on the left side. */
3993 it->start_of_box_run_p
3994 = new_face->box && (old_face == NULL || !old_face->box);
3995 it->face_box_p = new_face->box != FACE_NO_BOX;
3996 }
3997 }
3998
3999 it->face_id = new_face_id;
4000 return HANDLED_NORMALLY;
4001 }
4002
4003
4004 /* Return the ID of the face ``underlying'' IT's current position,
4005 which is in a string. If the iterator is associated with a
4006 buffer, return the face at IT's current buffer position.
4007 Otherwise, use the iterator's base_face_id. */
4008
4009 static int
4010 underlying_face_id (struct it *it)
4011 {
4012 int face_id = it->base_face_id, i;
4013
4014 eassert (STRINGP (it->string));
4015
4016 for (i = it->sp - 1; i >= 0; --i)
4017 if (NILP (it->stack[i].string))
4018 face_id = it->stack[i].face_id;
4019
4020 return face_id;
4021 }
4022
4023
4024 /* Compute the face one character before or after the current position
4025 of IT, in the visual order. BEFORE_P means get the face
4026 in front (to the left in L2R paragraphs, to the right in R2L
4027 paragraphs) of IT's screen position. Value is the ID of the face. */
4028
4029 static int
4030 face_before_or_after_it_pos (struct it *it, bool before_p)
4031 {
4032 int face_id, limit;
4033 ptrdiff_t next_check_charpos;
4034 struct it it_copy;
4035 void *it_copy_data = NULL;
4036
4037 eassert (it->s == NULL);
4038
4039 if (STRINGP (it->string))
4040 {
4041 ptrdiff_t bufpos, charpos;
4042 int base_face_id;
4043
4044 /* No face change past the end of the string (for the case
4045 we are padding with spaces). No face change before the
4046 string start. */
4047 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4048 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4049 return it->face_id;
4050
4051 if (!it->bidi_p)
4052 {
4053 /* Set charpos to the position before or after IT's current
4054 position, in the logical order, which in the non-bidi
4055 case is the same as the visual order. */
4056 if (before_p)
4057 charpos = IT_STRING_CHARPOS (*it) - 1;
4058 else if (it->what == IT_COMPOSITION)
4059 /* For composition, we must check the character after the
4060 composition. */
4061 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4062 else
4063 charpos = IT_STRING_CHARPOS (*it) + 1;
4064 }
4065 else
4066 {
4067 if (before_p)
4068 {
4069 /* With bidi iteration, the character before the current
4070 in the visual order cannot be found by simple
4071 iteration, because "reverse" reordering is not
4072 supported. Instead, we need to start from the string
4073 beginning and go all the way to the current string
4074 position, remembering the previous position. */
4075 /* Ignore face changes before the first visible
4076 character on this display line. */
4077 if (it->current_x <= it->first_visible_x)
4078 return it->face_id;
4079 SAVE_IT (it_copy, *it, it_copy_data);
4080 IT_STRING_CHARPOS (it_copy) = 0;
4081 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4082
4083 do
4084 {
4085 charpos = IT_STRING_CHARPOS (it_copy);
4086 if (charpos >= SCHARS (it->string))
4087 break;
4088 bidi_move_to_visually_next (&it_copy.bidi_it);
4089 }
4090 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4091
4092 RESTORE_IT (it, it, it_copy_data);
4093 }
4094 else
4095 {
4096 /* Set charpos to the string position of the character
4097 that comes after IT's current position in the visual
4098 order. */
4099 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4100
4101 it_copy = *it;
4102 while (n--)
4103 bidi_move_to_visually_next (&it_copy.bidi_it);
4104
4105 charpos = it_copy.bidi_it.charpos;
4106 }
4107 }
4108 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4109
4110 if (it->current.overlay_string_index >= 0)
4111 bufpos = IT_CHARPOS (*it);
4112 else
4113 bufpos = 0;
4114
4115 base_face_id = underlying_face_id (it);
4116
4117 /* Get the face for ASCII, or unibyte. */
4118 face_id = face_at_string_position (it->w,
4119 it->string,
4120 charpos,
4121 bufpos,
4122 &next_check_charpos,
4123 base_face_id, false);
4124
4125 /* Correct the face for charsets different from ASCII. Do it
4126 for the multibyte case only. The face returned above is
4127 suitable for unibyte text if IT->string is unibyte. */
4128 if (STRING_MULTIBYTE (it->string))
4129 {
4130 struct text_pos pos1 = string_pos (charpos, it->string);
4131 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4132 int c, len;
4133 struct face *face = FACE_FROM_ID (it->f, face_id);
4134
4135 c = string_char_and_length (p, &len);
4136 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4137 }
4138 }
4139 else
4140 {
4141 struct text_pos pos;
4142
4143 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4144 || (IT_CHARPOS (*it) <= BEGV && before_p))
4145 return it->face_id;
4146
4147 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4148 pos = it->current.pos;
4149
4150 if (!it->bidi_p)
4151 {
4152 if (before_p)
4153 DEC_TEXT_POS (pos, it->multibyte_p);
4154 else
4155 {
4156 if (it->what == IT_COMPOSITION)
4157 {
4158 /* For composition, we must check the position after
4159 the composition. */
4160 pos.charpos += it->cmp_it.nchars;
4161 pos.bytepos += it->len;
4162 }
4163 else
4164 INC_TEXT_POS (pos, it->multibyte_p);
4165 }
4166 }
4167 else
4168 {
4169 if (before_p)
4170 {
4171 int current_x;
4172
4173 /* With bidi iteration, the character before the current
4174 in the visual order cannot be found by simple
4175 iteration, because "reverse" reordering is not
4176 supported. Instead, we need to use the move_it_*
4177 family of functions, and move to the previous
4178 character starting from the beginning of the visual
4179 line. */
4180 /* Ignore face changes before the first visible
4181 character on this display line. */
4182 if (it->current_x <= it->first_visible_x)
4183 return it->face_id;
4184 SAVE_IT (it_copy, *it, it_copy_data);
4185 /* Implementation note: Since move_it_in_display_line
4186 works in the iterator geometry, and thinks the first
4187 character is always the leftmost, even in R2L lines,
4188 we don't need to distinguish between the R2L and L2R
4189 cases here. */
4190 current_x = it_copy.current_x;
4191 move_it_vertically_backward (&it_copy, 0);
4192 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4193 pos = it_copy.current.pos;
4194 RESTORE_IT (it, it, it_copy_data);
4195 }
4196 else
4197 {
4198 /* Set charpos to the buffer position of the character
4199 that comes after IT's current position in the visual
4200 order. */
4201 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4202
4203 it_copy = *it;
4204 while (n--)
4205 bidi_move_to_visually_next (&it_copy.bidi_it);
4206
4207 SET_TEXT_POS (pos,
4208 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4209 }
4210 }
4211 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4212
4213 /* Determine face for CHARSET_ASCII, or unibyte. */
4214 face_id = face_at_buffer_position (it->w,
4215 CHARPOS (pos),
4216 &next_check_charpos,
4217 limit, false, -1);
4218
4219 /* Correct the face for charsets different from ASCII. Do it
4220 for the multibyte case only. The face returned above is
4221 suitable for unibyte text if current_buffer is unibyte. */
4222 if (it->multibyte_p)
4223 {
4224 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4225 struct face *face = FACE_FROM_ID (it->f, face_id);
4226 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4227 }
4228 }
4229
4230 return face_id;
4231 }
4232
4233
4234 \f
4235 /***********************************************************************
4236 Invisible text
4237 ***********************************************************************/
4238
4239 /* Set up iterator IT from invisible properties at its current
4240 position. Called from handle_stop. */
4241
4242 static enum prop_handled
4243 handle_invisible_prop (struct it *it)
4244 {
4245 enum prop_handled handled = HANDLED_NORMALLY;
4246 int invis;
4247 Lisp_Object prop;
4248
4249 if (STRINGP (it->string))
4250 {
4251 Lisp_Object end_charpos, limit;
4252
4253 /* Get the value of the invisible text property at the
4254 current position. Value will be nil if there is no such
4255 property. */
4256 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4257 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4258 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4259
4260 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4261 {
4262 /* Record whether we have to display an ellipsis for the
4263 invisible text. */
4264 bool display_ellipsis_p = (invis == 2);
4265 ptrdiff_t len, endpos;
4266
4267 handled = HANDLED_RECOMPUTE_PROPS;
4268
4269 /* Get the position at which the next visible text can be
4270 found in IT->string, if any. */
4271 endpos = len = SCHARS (it->string);
4272 XSETINT (limit, len);
4273 do
4274 {
4275 end_charpos
4276 = Fnext_single_property_change (end_charpos, Qinvisible,
4277 it->string, limit);
4278 /* Since LIMIT is always an integer, so should be the
4279 value returned by Fnext_single_property_change. */
4280 eassert (INTEGERP (end_charpos));
4281 if (INTEGERP (end_charpos))
4282 {
4283 endpos = XFASTINT (end_charpos);
4284 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4285 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4286 if (invis == 2)
4287 display_ellipsis_p = true;
4288 }
4289 else /* Should never happen; but if it does, exit the loop. */
4290 endpos = len;
4291 }
4292 while (invis != 0 && endpos < len);
4293
4294 if (display_ellipsis_p)
4295 it->ellipsis_p = true;
4296
4297 if (endpos < len)
4298 {
4299 /* Text at END_CHARPOS is visible. Move IT there. */
4300 struct text_pos old;
4301 ptrdiff_t oldpos;
4302
4303 old = it->current.string_pos;
4304 oldpos = CHARPOS (old);
4305 if (it->bidi_p)
4306 {
4307 if (it->bidi_it.first_elt
4308 && it->bidi_it.charpos < SCHARS (it->string))
4309 bidi_paragraph_init (it->paragraph_embedding,
4310 &it->bidi_it, true);
4311 /* Bidi-iterate out of the invisible text. */
4312 do
4313 {
4314 bidi_move_to_visually_next (&it->bidi_it);
4315 }
4316 while (oldpos <= it->bidi_it.charpos
4317 && it->bidi_it.charpos < endpos);
4318
4319 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4320 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4321 if (IT_CHARPOS (*it) >= endpos)
4322 it->prev_stop = endpos;
4323 }
4324 else
4325 {
4326 IT_STRING_CHARPOS (*it) = endpos;
4327 compute_string_pos (&it->current.string_pos, old, it->string);
4328 }
4329 }
4330 else
4331 {
4332 /* The rest of the string is invisible. If this is an
4333 overlay string, proceed with the next overlay string
4334 or whatever comes and return a character from there. */
4335 if (it->current.overlay_string_index >= 0
4336 && !display_ellipsis_p)
4337 {
4338 next_overlay_string (it);
4339 /* Don't check for overlay strings when we just
4340 finished processing them. */
4341 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4342 }
4343 else
4344 {
4345 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4346 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4347 }
4348 }
4349 }
4350 }
4351 else
4352 {
4353 ptrdiff_t newpos, next_stop, start_charpos, tem;
4354 Lisp_Object pos, overlay;
4355
4356 /* First of all, is there invisible text at this position? */
4357 tem = start_charpos = IT_CHARPOS (*it);
4358 pos = make_number (tem);
4359 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4360 &overlay);
4361 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4362
4363 /* If we are on invisible text, skip over it. */
4364 if (invis != 0 && start_charpos < it->end_charpos)
4365 {
4366 /* Record whether we have to display an ellipsis for the
4367 invisible text. */
4368 bool display_ellipsis_p = invis == 2;
4369
4370 handled = HANDLED_RECOMPUTE_PROPS;
4371
4372 /* Loop skipping over invisible text. The loop is left at
4373 ZV or with IT on the first char being visible again. */
4374 do
4375 {
4376 /* Try to skip some invisible text. Return value is the
4377 position reached which can be equal to where we start
4378 if there is nothing invisible there. This skips both
4379 over invisible text properties and overlays with
4380 invisible property. */
4381 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4382
4383 /* If we skipped nothing at all we weren't at invisible
4384 text in the first place. If everything to the end of
4385 the buffer was skipped, end the loop. */
4386 if (newpos == tem || newpos >= ZV)
4387 invis = 0;
4388 else
4389 {
4390 /* We skipped some characters but not necessarily
4391 all there are. Check if we ended up on visible
4392 text. Fget_char_property returns the property of
4393 the char before the given position, i.e. if we
4394 get invis = 0, this means that the char at
4395 newpos is visible. */
4396 pos = make_number (newpos);
4397 prop = Fget_char_property (pos, Qinvisible, it->window);
4398 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4399 }
4400
4401 /* If we ended up on invisible text, proceed to
4402 skip starting with next_stop. */
4403 if (invis != 0)
4404 tem = next_stop;
4405
4406 /* If there are adjacent invisible texts, don't lose the
4407 second one's ellipsis. */
4408 if (invis == 2)
4409 display_ellipsis_p = true;
4410 }
4411 while (invis != 0);
4412
4413 /* The position newpos is now either ZV or on visible text. */
4414 if (it->bidi_p)
4415 {
4416 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4417 bool on_newline
4418 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4419 bool after_newline
4420 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4421
4422 /* If the invisible text ends on a newline or on a
4423 character after a newline, we can avoid the costly,
4424 character by character, bidi iteration to NEWPOS, and
4425 instead simply reseat the iterator there. That's
4426 because all bidi reordering information is tossed at
4427 the newline. This is a big win for modes that hide
4428 complete lines, like Outline, Org, etc. */
4429 if (on_newline || after_newline)
4430 {
4431 struct text_pos tpos;
4432 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4433
4434 SET_TEXT_POS (tpos, newpos, bpos);
4435 reseat_1 (it, tpos, false);
4436 /* If we reseat on a newline/ZV, we need to prep the
4437 bidi iterator for advancing to the next character
4438 after the newline/EOB, keeping the current paragraph
4439 direction (so that PRODUCE_GLYPHS does TRT wrt
4440 prepending/appending glyphs to a glyph row). */
4441 if (on_newline)
4442 {
4443 it->bidi_it.first_elt = false;
4444 it->bidi_it.paragraph_dir = pdir;
4445 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4446 it->bidi_it.nchars = 1;
4447 it->bidi_it.ch_len = 1;
4448 }
4449 }
4450 else /* Must use the slow method. */
4451 {
4452 /* With bidi iteration, the region of invisible text
4453 could start and/or end in the middle of a
4454 non-base embedding level. Therefore, we need to
4455 skip invisible text using the bidi iterator,
4456 starting at IT's current position, until we find
4457 ourselves outside of the invisible text.
4458 Skipping invisible text _after_ bidi iteration
4459 avoids affecting the visual order of the
4460 displayed text when invisible properties are
4461 added or removed. */
4462 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4463 {
4464 /* If we were `reseat'ed to a new paragraph,
4465 determine the paragraph base direction. We
4466 need to do it now because
4467 next_element_from_buffer may not have a
4468 chance to do it, if we are going to skip any
4469 text at the beginning, which resets the
4470 FIRST_ELT flag. */
4471 bidi_paragraph_init (it->paragraph_embedding,
4472 &it->bidi_it, true);
4473 }
4474 do
4475 {
4476 bidi_move_to_visually_next (&it->bidi_it);
4477 }
4478 while (it->stop_charpos <= it->bidi_it.charpos
4479 && it->bidi_it.charpos < newpos);
4480 IT_CHARPOS (*it) = it->bidi_it.charpos;
4481 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4482 /* If we overstepped NEWPOS, record its position in
4483 the iterator, so that we skip invisible text if
4484 later the bidi iteration lands us in the
4485 invisible region again. */
4486 if (IT_CHARPOS (*it) >= newpos)
4487 it->prev_stop = newpos;
4488 }
4489 }
4490 else
4491 {
4492 IT_CHARPOS (*it) = newpos;
4493 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4494 }
4495
4496 if (display_ellipsis_p)
4497 {
4498 /* Make sure that the glyphs of the ellipsis will get
4499 correct `charpos' values. If we would not update
4500 it->position here, the glyphs would belong to the
4501 last visible character _before_ the invisible
4502 text, which confuses `set_cursor_from_row'.
4503
4504 We use the last invisible position instead of the
4505 first because this way the cursor is always drawn on
4506 the first "." of the ellipsis, whenever PT is inside
4507 the invisible text. Otherwise the cursor would be
4508 placed _after_ the ellipsis when the point is after the
4509 first invisible character. */
4510 if (!STRINGP (it->object))
4511 {
4512 it->position.charpos = newpos - 1;
4513 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4514 }
4515 }
4516
4517 /* If there are before-strings at the start of invisible
4518 text, and the text is invisible because of a text
4519 property, arrange to show before-strings because 20.x did
4520 it that way. (If the text is invisible because of an
4521 overlay property instead of a text property, this is
4522 already handled in the overlay code.) */
4523 if (NILP (overlay)
4524 && get_overlay_strings (it, it->stop_charpos))
4525 {
4526 handled = HANDLED_RECOMPUTE_PROPS;
4527 if (it->sp > 0)
4528 {
4529 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4530 /* The call to get_overlay_strings above recomputes
4531 it->stop_charpos, but it only considers changes
4532 in properties and overlays beyond iterator's
4533 current position. This causes us to miss changes
4534 that happen exactly where the invisible property
4535 ended. So we play it safe here and force the
4536 iterator to check for potential stop positions
4537 immediately after the invisible text. Note that
4538 if get_overlay_strings returns true, it
4539 normally also pushed the iterator stack, so we
4540 need to update the stop position in the slot
4541 below the current one. */
4542 it->stack[it->sp - 1].stop_charpos
4543 = CHARPOS (it->stack[it->sp - 1].current.pos);
4544 }
4545 }
4546 else if (display_ellipsis_p)
4547 {
4548 it->ellipsis_p = true;
4549 /* Let the ellipsis display before
4550 considering any properties of the following char.
4551 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4552 handled = HANDLED_RETURN;
4553 }
4554 }
4555 }
4556
4557 return handled;
4558 }
4559
4560
4561 /* Make iterator IT return `...' next.
4562 Replaces LEN characters from buffer. */
4563
4564 static void
4565 setup_for_ellipsis (struct it *it, int len)
4566 {
4567 /* Use the display table definition for `...'. Invalid glyphs
4568 will be handled by the method returning elements from dpvec. */
4569 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4570 {
4571 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4572 it->dpvec = v->contents;
4573 it->dpend = v->contents + v->header.size;
4574 }
4575 else
4576 {
4577 /* Default `...'. */
4578 it->dpvec = default_invis_vector;
4579 it->dpend = default_invis_vector + 3;
4580 }
4581
4582 it->dpvec_char_len = len;
4583 it->current.dpvec_index = 0;
4584 it->dpvec_face_id = -1;
4585
4586 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4587 face as the preceding text. IT->saved_face_id was set in
4588 handle_stop to the face of the preceding character, and will be
4589 different from IT->face_id only if the invisible text skipped in
4590 handle_invisible_prop has some non-default face on its first
4591 character. We thus ignore the face of the invisible text when we
4592 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4593 if (it->saved_face_id >= 0)
4594 it->face_id = it->saved_face_id;
4595
4596 /* If the ellipsis represents buffer text, it means we advanced in
4597 the buffer, so we should no longer ignore overlay strings. */
4598 if (it->method == GET_FROM_BUFFER)
4599 it->ignore_overlay_strings_at_pos_p = false;
4600
4601 it->method = GET_FROM_DISPLAY_VECTOR;
4602 it->ellipsis_p = true;
4603 }
4604
4605
4606 \f
4607 /***********************************************************************
4608 'display' property
4609 ***********************************************************************/
4610
4611 /* Set up iterator IT from `display' property at its current position.
4612 Called from handle_stop.
4613 We return HANDLED_RETURN if some part of the display property
4614 overrides the display of the buffer text itself.
4615 Otherwise we return HANDLED_NORMALLY. */
4616
4617 static enum prop_handled
4618 handle_display_prop (struct it *it)
4619 {
4620 Lisp_Object propval, object, overlay;
4621 struct text_pos *position;
4622 ptrdiff_t bufpos;
4623 /* Nonzero if some property replaces the display of the text itself. */
4624 int display_replaced = 0;
4625
4626 if (STRINGP (it->string))
4627 {
4628 object = it->string;
4629 position = &it->current.string_pos;
4630 bufpos = CHARPOS (it->current.pos);
4631 }
4632 else
4633 {
4634 XSETWINDOW (object, it->w);
4635 position = &it->current.pos;
4636 bufpos = CHARPOS (*position);
4637 }
4638
4639 /* Reset those iterator values set from display property values. */
4640 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4641 it->space_width = Qnil;
4642 it->font_height = Qnil;
4643 it->voffset = 0;
4644
4645 /* We don't support recursive `display' properties, i.e. string
4646 values that have a string `display' property, that have a string
4647 `display' property etc. */
4648 if (!it->string_from_display_prop_p)
4649 it->area = TEXT_AREA;
4650
4651 propval = get_char_property_and_overlay (make_number (position->charpos),
4652 Qdisplay, object, &overlay);
4653 if (NILP (propval))
4654 return HANDLED_NORMALLY;
4655 /* Now OVERLAY is the overlay that gave us this property, or nil
4656 if it was a text property. */
4657
4658 if (!STRINGP (it->string))
4659 object = it->w->contents;
4660
4661 display_replaced = handle_display_spec (it, propval, object, overlay,
4662 position, bufpos,
4663 FRAME_WINDOW_P (it->f));
4664 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4665 }
4666
4667 /* Subroutine of handle_display_prop. Returns non-zero if the display
4668 specification in SPEC is a replacing specification, i.e. it would
4669 replace the text covered by `display' property with something else,
4670 such as an image or a display string. If SPEC includes any kind or
4671 `(space ...) specification, the value is 2; this is used by
4672 compute_display_string_pos, which see.
4673
4674 See handle_single_display_spec for documentation of arguments.
4675 FRAME_WINDOW_P is true if the window being redisplayed is on a
4676 GUI frame; this argument is used only if IT is NULL, see below.
4677
4678 IT can be NULL, if this is called by the bidi reordering code
4679 through compute_display_string_pos, which see. In that case, this
4680 function only examines SPEC, but does not otherwise "handle" it, in
4681 the sense that it doesn't set up members of IT from the display
4682 spec. */
4683 static int
4684 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4685 Lisp_Object overlay, struct text_pos *position,
4686 ptrdiff_t bufpos, bool frame_window_p)
4687 {
4688 int replacing = 0;
4689
4690 if (CONSP (spec)
4691 /* Simple specifications. */
4692 && !EQ (XCAR (spec), Qimage)
4693 && !EQ (XCAR (spec), Qspace)
4694 && !EQ (XCAR (spec), Qwhen)
4695 && !EQ (XCAR (spec), Qslice)
4696 && !EQ (XCAR (spec), Qspace_width)
4697 && !EQ (XCAR (spec), Qheight)
4698 && !EQ (XCAR (spec), Qraise)
4699 /* Marginal area specifications. */
4700 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4701 && !EQ (XCAR (spec), Qleft_fringe)
4702 && !EQ (XCAR (spec), Qright_fringe)
4703 && !NILP (XCAR (spec)))
4704 {
4705 for (; CONSP (spec); spec = XCDR (spec))
4706 {
4707 int rv = handle_single_display_spec (it, XCAR (spec), object,
4708 overlay, position, bufpos,
4709 replacing, frame_window_p);
4710 if (rv != 0)
4711 {
4712 replacing = rv;
4713 /* If some text in a string is replaced, `position' no
4714 longer points to the position of `object'. */
4715 if (!it || STRINGP (object))
4716 break;
4717 }
4718 }
4719 }
4720 else if (VECTORP (spec))
4721 {
4722 ptrdiff_t i;
4723 for (i = 0; i < ASIZE (spec); ++i)
4724 {
4725 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4726 overlay, position, bufpos,
4727 replacing, frame_window_p);
4728 if (rv != 0)
4729 {
4730 replacing = rv;
4731 /* If some text in a string is replaced, `position' no
4732 longer points to the position of `object'. */
4733 if (!it || STRINGP (object))
4734 break;
4735 }
4736 }
4737 }
4738 else
4739 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4740 bufpos, 0, frame_window_p);
4741 return replacing;
4742 }
4743
4744 /* Value is the position of the end of the `display' property starting
4745 at START_POS in OBJECT. */
4746
4747 static struct text_pos
4748 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4749 {
4750 Lisp_Object end;
4751 struct text_pos end_pos;
4752
4753 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4754 Qdisplay, object, Qnil);
4755 CHARPOS (end_pos) = XFASTINT (end);
4756 if (STRINGP (object))
4757 compute_string_pos (&end_pos, start_pos, it->string);
4758 else
4759 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4760
4761 return end_pos;
4762 }
4763
4764
4765 /* Set up IT from a single `display' property specification SPEC. OBJECT
4766 is the object in which the `display' property was found. *POSITION
4767 is the position in OBJECT at which the `display' property was found.
4768 BUFPOS is the buffer position of OBJECT (different from POSITION if
4769 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4770 previously saw a display specification which already replaced text
4771 display with something else, for example an image; we ignore such
4772 properties after the first one has been processed.
4773
4774 OVERLAY is the overlay this `display' property came from,
4775 or nil if it was a text property.
4776
4777 If SPEC is a `space' or `image' specification, and in some other
4778 cases too, set *POSITION to the position where the `display'
4779 property ends.
4780
4781 If IT is NULL, only examine the property specification in SPEC, but
4782 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4783 is intended to be displayed in a window on a GUI frame.
4784
4785 Value is non-zero if something was found which replaces the display
4786 of buffer or string text. */
4787
4788 static int
4789 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4790 Lisp_Object overlay, struct text_pos *position,
4791 ptrdiff_t bufpos, int display_replaced,
4792 bool frame_window_p)
4793 {
4794 Lisp_Object form;
4795 Lisp_Object location, value;
4796 struct text_pos start_pos = *position;
4797
4798 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4799 If the result is non-nil, use VALUE instead of SPEC. */
4800 form = Qt;
4801 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4802 {
4803 spec = XCDR (spec);
4804 if (!CONSP (spec))
4805 return 0;
4806 form = XCAR (spec);
4807 spec = XCDR (spec);
4808 }
4809
4810 if (!NILP (form) && !EQ (form, Qt))
4811 {
4812 ptrdiff_t count = SPECPDL_INDEX ();
4813
4814 /* Bind `object' to the object having the `display' property, a
4815 buffer or string. Bind `position' to the position in the
4816 object where the property was found, and `buffer-position'
4817 to the current position in the buffer. */
4818
4819 if (NILP (object))
4820 XSETBUFFER (object, current_buffer);
4821 specbind (Qobject, object);
4822 specbind (Qposition, make_number (CHARPOS (*position)));
4823 specbind (Qbuffer_position, make_number (bufpos));
4824 form = safe_eval (form);
4825 unbind_to (count, Qnil);
4826 }
4827
4828 if (NILP (form))
4829 return 0;
4830
4831 /* Handle `(height HEIGHT)' specifications. */
4832 if (CONSP (spec)
4833 && EQ (XCAR (spec), Qheight)
4834 && CONSP (XCDR (spec)))
4835 {
4836 if (it)
4837 {
4838 if (!FRAME_WINDOW_P (it->f))
4839 return 0;
4840
4841 it->font_height = XCAR (XCDR (spec));
4842 if (!NILP (it->font_height))
4843 {
4844 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4845 int new_height = -1;
4846
4847 if (CONSP (it->font_height)
4848 && (EQ (XCAR (it->font_height), Qplus)
4849 || EQ (XCAR (it->font_height), Qminus))
4850 && CONSP (XCDR (it->font_height))
4851 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4852 {
4853 /* `(+ N)' or `(- N)' where N is an integer. */
4854 int steps = XINT (XCAR (XCDR (it->font_height)));
4855 if (EQ (XCAR (it->font_height), Qplus))
4856 steps = - steps;
4857 it->face_id = smaller_face (it->f, it->face_id, steps);
4858 }
4859 else if (FUNCTIONP (it->font_height))
4860 {
4861 /* Call function with current height as argument.
4862 Value is the new height. */
4863 Lisp_Object height;
4864 height = safe_call1 (it->font_height,
4865 face->lface[LFACE_HEIGHT_INDEX]);
4866 if (NUMBERP (height))
4867 new_height = XFLOATINT (height);
4868 }
4869 else if (NUMBERP (it->font_height))
4870 {
4871 /* Value is a multiple of the canonical char height. */
4872 struct face *f;
4873
4874 f = FACE_FROM_ID (it->f,
4875 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4876 new_height = (XFLOATINT (it->font_height)
4877 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4878 }
4879 else
4880 {
4881 /* Evaluate IT->font_height with `height' bound to the
4882 current specified height to get the new height. */
4883 ptrdiff_t count = SPECPDL_INDEX ();
4884
4885 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4886 value = safe_eval (it->font_height);
4887 unbind_to (count, Qnil);
4888
4889 if (NUMBERP (value))
4890 new_height = XFLOATINT (value);
4891 }
4892
4893 if (new_height > 0)
4894 it->face_id = face_with_height (it->f, it->face_id, new_height);
4895 }
4896 }
4897
4898 return 0;
4899 }
4900
4901 /* Handle `(space-width WIDTH)'. */
4902 if (CONSP (spec)
4903 && EQ (XCAR (spec), Qspace_width)
4904 && CONSP (XCDR (spec)))
4905 {
4906 if (it)
4907 {
4908 if (!FRAME_WINDOW_P (it->f))
4909 return 0;
4910
4911 value = XCAR (XCDR (spec));
4912 if (NUMBERP (value) && XFLOATINT (value) > 0)
4913 it->space_width = value;
4914 }
4915
4916 return 0;
4917 }
4918
4919 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4920 if (CONSP (spec)
4921 && EQ (XCAR (spec), Qslice))
4922 {
4923 Lisp_Object tem;
4924
4925 if (it)
4926 {
4927 if (!FRAME_WINDOW_P (it->f))
4928 return 0;
4929
4930 if (tem = XCDR (spec), CONSP (tem))
4931 {
4932 it->slice.x = XCAR (tem);
4933 if (tem = XCDR (tem), CONSP (tem))
4934 {
4935 it->slice.y = XCAR (tem);
4936 if (tem = XCDR (tem), CONSP (tem))
4937 {
4938 it->slice.width = XCAR (tem);
4939 if (tem = XCDR (tem), CONSP (tem))
4940 it->slice.height = XCAR (tem);
4941 }
4942 }
4943 }
4944 }
4945
4946 return 0;
4947 }
4948
4949 /* Handle `(raise FACTOR)'. */
4950 if (CONSP (spec)
4951 && EQ (XCAR (spec), Qraise)
4952 && CONSP (XCDR (spec)))
4953 {
4954 if (it)
4955 {
4956 if (!FRAME_WINDOW_P (it->f))
4957 return 0;
4958
4959 #ifdef HAVE_WINDOW_SYSTEM
4960 value = XCAR (XCDR (spec));
4961 if (NUMBERP (value))
4962 {
4963 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4964 it->voffset = - (XFLOATINT (value)
4965 * (normal_char_height (face->font, -1)));
4966 }
4967 #endif /* HAVE_WINDOW_SYSTEM */
4968 }
4969
4970 return 0;
4971 }
4972
4973 /* Don't handle the other kinds of display specifications
4974 inside a string that we got from a `display' property. */
4975 if (it && it->string_from_display_prop_p)
4976 return 0;
4977
4978 /* Characters having this form of property are not displayed, so
4979 we have to find the end of the property. */
4980 if (it)
4981 {
4982 start_pos = *position;
4983 *position = display_prop_end (it, object, start_pos);
4984 /* If the display property comes from an overlay, don't consider
4985 any potential stop_charpos values before the end of that
4986 overlay. Since display_prop_end will happily find another
4987 'display' property coming from some other overlay or text
4988 property on buffer positions before this overlay's end, we
4989 need to ignore them, or else we risk displaying this
4990 overlay's display string/image twice. */
4991 if (!NILP (overlay))
4992 {
4993 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4994
4995 if (ovendpos > CHARPOS (*position))
4996 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
4997 }
4998 }
4999 value = Qnil;
5000
5001 /* Stop the scan at that end position--we assume that all
5002 text properties change there. */
5003 if (it)
5004 it->stop_charpos = position->charpos;
5005
5006 /* Handle `(left-fringe BITMAP [FACE])'
5007 and `(right-fringe BITMAP [FACE])'. */
5008 if (CONSP (spec)
5009 && (EQ (XCAR (spec), Qleft_fringe)
5010 || EQ (XCAR (spec), Qright_fringe))
5011 && CONSP (XCDR (spec)))
5012 {
5013 int fringe_bitmap;
5014
5015 if (it)
5016 {
5017 if (!FRAME_WINDOW_P (it->f))
5018 /* If we return here, POSITION has been advanced
5019 across the text with this property. */
5020 {
5021 /* Synchronize the bidi iterator with POSITION. This is
5022 needed because we are not going to push the iterator
5023 on behalf of this display property, so there will be
5024 no pop_it call to do this synchronization for us. */
5025 if (it->bidi_p)
5026 {
5027 it->position = *position;
5028 iterate_out_of_display_property (it);
5029 *position = it->position;
5030 }
5031 return 1;
5032 }
5033 }
5034 else if (!frame_window_p)
5035 return 1;
5036
5037 #ifdef HAVE_WINDOW_SYSTEM
5038 value = XCAR (XCDR (spec));
5039 if (!SYMBOLP (value)
5040 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
5041 /* If we return here, POSITION has been advanced
5042 across the text with this property. */
5043 {
5044 if (it && it->bidi_p)
5045 {
5046 it->position = *position;
5047 iterate_out_of_display_property (it);
5048 *position = it->position;
5049 }
5050 return 1;
5051 }
5052
5053 if (it)
5054 {
5055 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5056
5057 if (CONSP (XCDR (XCDR (spec))))
5058 {
5059 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5060 int face_id2 = lookup_derived_face (it->f, face_name,
5061 FRINGE_FACE_ID, false);
5062 if (face_id2 >= 0)
5063 face_id = face_id2;
5064 }
5065
5066 /* Save current settings of IT so that we can restore them
5067 when we are finished with the glyph property value. */
5068 push_it (it, position);
5069
5070 it->area = TEXT_AREA;
5071 it->what = IT_IMAGE;
5072 it->image_id = -1; /* no image */
5073 it->position = start_pos;
5074 it->object = NILP (object) ? it->w->contents : object;
5075 it->method = GET_FROM_IMAGE;
5076 it->from_overlay = Qnil;
5077 it->face_id = face_id;
5078 it->from_disp_prop_p = true;
5079
5080 /* Say that we haven't consumed the characters with
5081 `display' property yet. The call to pop_it in
5082 set_iterator_to_next will clean this up. */
5083 *position = start_pos;
5084
5085 if (EQ (XCAR (spec), Qleft_fringe))
5086 {
5087 it->left_user_fringe_bitmap = fringe_bitmap;
5088 it->left_user_fringe_face_id = face_id;
5089 }
5090 else
5091 {
5092 it->right_user_fringe_bitmap = fringe_bitmap;
5093 it->right_user_fringe_face_id = face_id;
5094 }
5095 }
5096 #endif /* HAVE_WINDOW_SYSTEM */
5097 return 1;
5098 }
5099
5100 /* Prepare to handle `((margin left-margin) ...)',
5101 `((margin right-margin) ...)' and `((margin nil) ...)'
5102 prefixes for display specifications. */
5103 location = Qunbound;
5104 if (CONSP (spec) && CONSP (XCAR (spec)))
5105 {
5106 Lisp_Object tem;
5107
5108 value = XCDR (spec);
5109 if (CONSP (value))
5110 value = XCAR (value);
5111
5112 tem = XCAR (spec);
5113 if (EQ (XCAR (tem), Qmargin)
5114 && (tem = XCDR (tem),
5115 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5116 (NILP (tem)
5117 || EQ (tem, Qleft_margin)
5118 || EQ (tem, Qright_margin))))
5119 location = tem;
5120 }
5121
5122 if (EQ (location, Qunbound))
5123 {
5124 location = Qnil;
5125 value = spec;
5126 }
5127
5128 /* After this point, VALUE is the property after any
5129 margin prefix has been stripped. It must be a string,
5130 an image specification, or `(space ...)'.
5131
5132 LOCATION specifies where to display: `left-margin',
5133 `right-margin' or nil. */
5134
5135 bool valid_p = (STRINGP (value)
5136 #ifdef HAVE_WINDOW_SYSTEM
5137 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5138 && valid_image_p (value))
5139 #endif /* not HAVE_WINDOW_SYSTEM */
5140 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5141
5142 if (valid_p && display_replaced == 0)
5143 {
5144 int retval = 1;
5145
5146 if (!it)
5147 {
5148 /* Callers need to know whether the display spec is any kind
5149 of `(space ...)' spec that is about to affect text-area
5150 display. */
5151 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5152 retval = 2;
5153 return retval;
5154 }
5155
5156 /* Save current settings of IT so that we can restore them
5157 when we are finished with the glyph property value. */
5158 push_it (it, position);
5159 it->from_overlay = overlay;
5160 it->from_disp_prop_p = true;
5161
5162 if (NILP (location))
5163 it->area = TEXT_AREA;
5164 else if (EQ (location, Qleft_margin))
5165 it->area = LEFT_MARGIN_AREA;
5166 else
5167 it->area = RIGHT_MARGIN_AREA;
5168
5169 if (STRINGP (value))
5170 {
5171 it->string = value;
5172 it->multibyte_p = STRING_MULTIBYTE (it->string);
5173 it->current.overlay_string_index = -1;
5174 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5175 it->end_charpos = it->string_nchars = SCHARS (it->string);
5176 it->method = GET_FROM_STRING;
5177 it->stop_charpos = 0;
5178 it->prev_stop = 0;
5179 it->base_level_stop = 0;
5180 it->string_from_display_prop_p = true;
5181 /* Say that we haven't consumed the characters with
5182 `display' property yet. The call to pop_it in
5183 set_iterator_to_next will clean this up. */
5184 if (BUFFERP (object))
5185 *position = start_pos;
5186
5187 /* Force paragraph direction to be that of the parent
5188 object. If the parent object's paragraph direction is
5189 not yet determined, default to L2R. */
5190 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5191 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5192 else
5193 it->paragraph_embedding = L2R;
5194
5195 /* Set up the bidi iterator for this display string. */
5196 if (it->bidi_p)
5197 {
5198 it->bidi_it.string.lstring = it->string;
5199 it->bidi_it.string.s = NULL;
5200 it->bidi_it.string.schars = it->end_charpos;
5201 it->bidi_it.string.bufpos = bufpos;
5202 it->bidi_it.string.from_disp_str = true;
5203 it->bidi_it.string.unibyte = !it->multibyte_p;
5204 it->bidi_it.w = it->w;
5205 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5206 }
5207 }
5208 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5209 {
5210 it->method = GET_FROM_STRETCH;
5211 it->object = value;
5212 *position = it->position = start_pos;
5213 retval = 1 + (it->area == TEXT_AREA);
5214 }
5215 #ifdef HAVE_WINDOW_SYSTEM
5216 else
5217 {
5218 it->what = IT_IMAGE;
5219 it->image_id = lookup_image (it->f, value);
5220 it->position = start_pos;
5221 it->object = NILP (object) ? it->w->contents : object;
5222 it->method = GET_FROM_IMAGE;
5223
5224 /* Say that we haven't consumed the characters with
5225 `display' property yet. The call to pop_it in
5226 set_iterator_to_next will clean this up. */
5227 *position = start_pos;
5228 }
5229 #endif /* HAVE_WINDOW_SYSTEM */
5230
5231 return retval;
5232 }
5233
5234 /* Invalid property or property not supported. Restore
5235 POSITION to what it was before. */
5236 *position = start_pos;
5237 return 0;
5238 }
5239
5240 /* Check if PROP is a display property value whose text should be
5241 treated as intangible. OVERLAY is the overlay from which PROP
5242 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5243 specify the buffer position covered by PROP. */
5244
5245 bool
5246 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5247 ptrdiff_t charpos, ptrdiff_t bytepos)
5248 {
5249 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5250 struct text_pos position;
5251
5252 SET_TEXT_POS (position, charpos, bytepos);
5253 return (handle_display_spec (NULL, prop, Qnil, overlay,
5254 &position, charpos, frame_window_p)
5255 != 0);
5256 }
5257
5258
5259 /* Return true if PROP is a display sub-property value containing STRING.
5260
5261 Implementation note: this and the following function are really
5262 special cases of handle_display_spec and
5263 handle_single_display_spec, and should ideally use the same code.
5264 Until they do, these two pairs must be consistent and must be
5265 modified in sync. */
5266
5267 static bool
5268 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5269 {
5270 if (EQ (string, prop))
5271 return true;
5272
5273 /* Skip over `when FORM'. */
5274 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5275 {
5276 prop = XCDR (prop);
5277 if (!CONSP (prop))
5278 return false;
5279 /* Actually, the condition following `when' should be eval'ed,
5280 like handle_single_display_spec does, and we should return
5281 false if it evaluates to nil. However, this function is
5282 called only when the buffer was already displayed and some
5283 glyph in the glyph matrix was found to come from a display
5284 string. Therefore, the condition was already evaluated, and
5285 the result was non-nil, otherwise the display string wouldn't
5286 have been displayed and we would have never been called for
5287 this property. Thus, we can skip the evaluation and assume
5288 its result is non-nil. */
5289 prop = XCDR (prop);
5290 }
5291
5292 if (CONSP (prop))
5293 /* Skip over `margin LOCATION'. */
5294 if (EQ (XCAR (prop), Qmargin))
5295 {
5296 prop = XCDR (prop);
5297 if (!CONSP (prop))
5298 return false;
5299
5300 prop = XCDR (prop);
5301 if (!CONSP (prop))
5302 return false;
5303 }
5304
5305 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5306 }
5307
5308
5309 /* Return true if STRING appears in the `display' property PROP. */
5310
5311 static bool
5312 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5313 {
5314 if (CONSP (prop)
5315 && !EQ (XCAR (prop), Qwhen)
5316 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5317 {
5318 /* A list of sub-properties. */
5319 while (CONSP (prop))
5320 {
5321 if (single_display_spec_string_p (XCAR (prop), string))
5322 return true;
5323 prop = XCDR (prop);
5324 }
5325 }
5326 else if (VECTORP (prop))
5327 {
5328 /* A vector of sub-properties. */
5329 ptrdiff_t i;
5330 for (i = 0; i < ASIZE (prop); ++i)
5331 if (single_display_spec_string_p (AREF (prop, i), string))
5332 return true;
5333 }
5334 else
5335 return single_display_spec_string_p (prop, string);
5336
5337 return false;
5338 }
5339
5340 /* Look for STRING in overlays and text properties in the current
5341 buffer, between character positions FROM and TO (excluding TO).
5342 BACK_P means look back (in this case, TO is supposed to be
5343 less than FROM).
5344 Value is the first character position where STRING was found, or
5345 zero if it wasn't found before hitting TO.
5346
5347 This function may only use code that doesn't eval because it is
5348 called asynchronously from note_mouse_highlight. */
5349
5350 static ptrdiff_t
5351 string_buffer_position_lim (Lisp_Object string,
5352 ptrdiff_t from, ptrdiff_t to, bool back_p)
5353 {
5354 Lisp_Object limit, prop, pos;
5355 bool found = false;
5356
5357 pos = make_number (max (from, BEGV));
5358
5359 if (!back_p) /* looking forward */
5360 {
5361 limit = make_number (min (to, ZV));
5362 while (!found && !EQ (pos, limit))
5363 {
5364 prop = Fget_char_property (pos, Qdisplay, Qnil);
5365 if (!NILP (prop) && display_prop_string_p (prop, string))
5366 found = true;
5367 else
5368 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5369 limit);
5370 }
5371 }
5372 else /* looking back */
5373 {
5374 limit = make_number (max (to, BEGV));
5375 while (!found && !EQ (pos, limit))
5376 {
5377 prop = Fget_char_property (pos, Qdisplay, Qnil);
5378 if (!NILP (prop) && display_prop_string_p (prop, string))
5379 found = true;
5380 else
5381 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5382 limit);
5383 }
5384 }
5385
5386 return found ? XINT (pos) : 0;
5387 }
5388
5389 /* Determine which buffer position in current buffer STRING comes from.
5390 AROUND_CHARPOS is an approximate position where it could come from.
5391 Value is the buffer position or 0 if it couldn't be determined.
5392
5393 This function is necessary because we don't record buffer positions
5394 in glyphs generated from strings (to keep struct glyph small).
5395 This function may only use code that doesn't eval because it is
5396 called asynchronously from note_mouse_highlight. */
5397
5398 static ptrdiff_t
5399 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5400 {
5401 const int MAX_DISTANCE = 1000;
5402 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5403 around_charpos + MAX_DISTANCE,
5404 false);
5405
5406 if (!found)
5407 found = string_buffer_position_lim (string, around_charpos,
5408 around_charpos - MAX_DISTANCE, true);
5409 return found;
5410 }
5411
5412
5413 \f
5414 /***********************************************************************
5415 `composition' property
5416 ***********************************************************************/
5417
5418 /* Set up iterator IT from `composition' property at its current
5419 position. Called from handle_stop. */
5420
5421 static enum prop_handled
5422 handle_composition_prop (struct it *it)
5423 {
5424 Lisp_Object prop, string;
5425 ptrdiff_t pos, pos_byte, start, end;
5426
5427 if (STRINGP (it->string))
5428 {
5429 unsigned char *s;
5430
5431 pos = IT_STRING_CHARPOS (*it);
5432 pos_byte = IT_STRING_BYTEPOS (*it);
5433 string = it->string;
5434 s = SDATA (string) + pos_byte;
5435 it->c = STRING_CHAR (s);
5436 }
5437 else
5438 {
5439 pos = IT_CHARPOS (*it);
5440 pos_byte = IT_BYTEPOS (*it);
5441 string = Qnil;
5442 it->c = FETCH_CHAR (pos_byte);
5443 }
5444
5445 /* If there's a valid composition and point is not inside of the
5446 composition (in the case that the composition is from the current
5447 buffer), draw a glyph composed from the composition components. */
5448 if (find_composition (pos, -1, &start, &end, &prop, string)
5449 && composition_valid_p (start, end, prop)
5450 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5451 {
5452 if (start < pos)
5453 /* As we can't handle this situation (perhaps font-lock added
5454 a new composition), we just return here hoping that next
5455 redisplay will detect this composition much earlier. */
5456 return HANDLED_NORMALLY;
5457 if (start != pos)
5458 {
5459 if (STRINGP (it->string))
5460 pos_byte = string_char_to_byte (it->string, start);
5461 else
5462 pos_byte = CHAR_TO_BYTE (start);
5463 }
5464 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5465 prop, string);
5466
5467 if (it->cmp_it.id >= 0)
5468 {
5469 it->cmp_it.ch = -1;
5470 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5471 it->cmp_it.nglyphs = -1;
5472 }
5473 }
5474
5475 return HANDLED_NORMALLY;
5476 }
5477
5478
5479 \f
5480 /***********************************************************************
5481 Overlay strings
5482 ***********************************************************************/
5483
5484 /* The following structure is used to record overlay strings for
5485 later sorting in load_overlay_strings. */
5486
5487 struct overlay_entry
5488 {
5489 Lisp_Object overlay;
5490 Lisp_Object string;
5491 EMACS_INT priority;
5492 bool after_string_p;
5493 };
5494
5495
5496 /* Set up iterator IT from overlay strings at its current position.
5497 Called from handle_stop. */
5498
5499 static enum prop_handled
5500 handle_overlay_change (struct it *it)
5501 {
5502 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5503 return HANDLED_RECOMPUTE_PROPS;
5504 else
5505 return HANDLED_NORMALLY;
5506 }
5507
5508
5509 /* Set up the next overlay string for delivery by IT, if there is an
5510 overlay string to deliver. Called by set_iterator_to_next when the
5511 end of the current overlay string is reached. If there are more
5512 overlay strings to display, IT->string and
5513 IT->current.overlay_string_index are set appropriately here.
5514 Otherwise IT->string is set to nil. */
5515
5516 static void
5517 next_overlay_string (struct it *it)
5518 {
5519 ++it->current.overlay_string_index;
5520 if (it->current.overlay_string_index == it->n_overlay_strings)
5521 {
5522 /* No more overlay strings. Restore IT's settings to what
5523 they were before overlay strings were processed, and
5524 continue to deliver from current_buffer. */
5525
5526 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5527 pop_it (it);
5528 eassert (it->sp > 0
5529 || (NILP (it->string)
5530 && it->method == GET_FROM_BUFFER
5531 && it->stop_charpos >= BEGV
5532 && it->stop_charpos <= it->end_charpos));
5533 it->current.overlay_string_index = -1;
5534 it->n_overlay_strings = 0;
5535 /* If there's an empty display string on the stack, pop the
5536 stack, to resync the bidi iterator with IT's position. Such
5537 empty strings are pushed onto the stack in
5538 get_overlay_strings_1. */
5539 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5540 pop_it (it);
5541
5542 /* Since we've exhausted overlay strings at this buffer
5543 position, set the flag to ignore overlays until we move to
5544 another position. The flag is reset in
5545 next_element_from_buffer. */
5546 it->ignore_overlay_strings_at_pos_p = true;
5547
5548 /* If we're at the end of the buffer, record that we have
5549 processed the overlay strings there already, so that
5550 next_element_from_buffer doesn't try it again. */
5551 if (NILP (it->string)
5552 && IT_CHARPOS (*it) >= it->end_charpos
5553 && it->overlay_strings_charpos >= it->end_charpos)
5554 it->overlay_strings_at_end_processed_p = true;
5555 /* Note: we reset overlay_strings_charpos only here, to make
5556 sure the just-processed overlays were indeed at EOB.
5557 Otherwise, overlays on text with invisible text property,
5558 which are processed with IT's position past the invisible
5559 text, might fool us into thinking the overlays at EOB were
5560 already processed (linum-mode can cause this, for
5561 example). */
5562 it->overlay_strings_charpos = -1;
5563 }
5564 else
5565 {
5566 /* There are more overlay strings to process. If
5567 IT->current.overlay_string_index has advanced to a position
5568 where we must load IT->overlay_strings with more strings, do
5569 it. We must load at the IT->overlay_strings_charpos where
5570 IT->n_overlay_strings was originally computed; when invisible
5571 text is present, this might not be IT_CHARPOS (Bug#7016). */
5572 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5573
5574 if (it->current.overlay_string_index && i == 0)
5575 load_overlay_strings (it, it->overlay_strings_charpos);
5576
5577 /* Initialize IT to deliver display elements from the overlay
5578 string. */
5579 it->string = it->overlay_strings[i];
5580 it->multibyte_p = STRING_MULTIBYTE (it->string);
5581 SET_TEXT_POS (it->current.string_pos, 0, 0);
5582 it->method = GET_FROM_STRING;
5583 it->stop_charpos = 0;
5584 it->end_charpos = SCHARS (it->string);
5585 if (it->cmp_it.stop_pos >= 0)
5586 it->cmp_it.stop_pos = 0;
5587 it->prev_stop = 0;
5588 it->base_level_stop = 0;
5589
5590 /* Set up the bidi iterator for this overlay string. */
5591 if (it->bidi_p)
5592 {
5593 it->bidi_it.string.lstring = it->string;
5594 it->bidi_it.string.s = NULL;
5595 it->bidi_it.string.schars = SCHARS (it->string);
5596 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5597 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5598 it->bidi_it.string.unibyte = !it->multibyte_p;
5599 it->bidi_it.w = it->w;
5600 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5601 }
5602 }
5603
5604 CHECK_IT (it);
5605 }
5606
5607
5608 /* Compare two overlay_entry structures E1 and E2. Used as a
5609 comparison function for qsort in load_overlay_strings. Overlay
5610 strings for the same position are sorted so that
5611
5612 1. All after-strings come in front of before-strings, except
5613 when they come from the same overlay.
5614
5615 2. Within after-strings, strings are sorted so that overlay strings
5616 from overlays with higher priorities come first.
5617
5618 2. Within before-strings, strings are sorted so that overlay
5619 strings from overlays with higher priorities come last.
5620
5621 Value is analogous to strcmp. */
5622
5623
5624 static int
5625 compare_overlay_entries (const void *e1, const void *e2)
5626 {
5627 struct overlay_entry const *entry1 = e1;
5628 struct overlay_entry const *entry2 = e2;
5629 int result;
5630
5631 if (entry1->after_string_p != entry2->after_string_p)
5632 {
5633 /* Let after-strings appear in front of before-strings if
5634 they come from different overlays. */
5635 if (EQ (entry1->overlay, entry2->overlay))
5636 result = entry1->after_string_p ? 1 : -1;
5637 else
5638 result = entry1->after_string_p ? -1 : 1;
5639 }
5640 else if (entry1->priority != entry2->priority)
5641 {
5642 if (entry1->after_string_p)
5643 /* After-strings sorted in order of decreasing priority. */
5644 result = entry2->priority < entry1->priority ? -1 : 1;
5645 else
5646 /* Before-strings sorted in order of increasing priority. */
5647 result = entry1->priority < entry2->priority ? -1 : 1;
5648 }
5649 else
5650 result = 0;
5651
5652 return result;
5653 }
5654
5655
5656 /* Load the vector IT->overlay_strings with overlay strings from IT's
5657 current buffer position, or from CHARPOS if that is > 0. Set
5658 IT->n_overlays to the total number of overlay strings found.
5659
5660 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5661 a time. On entry into load_overlay_strings,
5662 IT->current.overlay_string_index gives the number of overlay
5663 strings that have already been loaded by previous calls to this
5664 function.
5665
5666 IT->add_overlay_start contains an additional overlay start
5667 position to consider for taking overlay strings from, if non-zero.
5668 This position comes into play when the overlay has an `invisible'
5669 property, and both before and after-strings. When we've skipped to
5670 the end of the overlay, because of its `invisible' property, we
5671 nevertheless want its before-string to appear.
5672 IT->add_overlay_start will contain the overlay start position
5673 in this case.
5674
5675 Overlay strings are sorted so that after-string strings come in
5676 front of before-string strings. Within before and after-strings,
5677 strings are sorted by overlay priority. See also function
5678 compare_overlay_entries. */
5679
5680 static void
5681 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5682 {
5683 Lisp_Object overlay, window, str, invisible;
5684 struct Lisp_Overlay *ov;
5685 ptrdiff_t start, end;
5686 ptrdiff_t n = 0, i, j;
5687 int invis;
5688 struct overlay_entry entriesbuf[20];
5689 ptrdiff_t size = ARRAYELTS (entriesbuf);
5690 struct overlay_entry *entries = entriesbuf;
5691 USE_SAFE_ALLOCA;
5692
5693 if (charpos <= 0)
5694 charpos = IT_CHARPOS (*it);
5695
5696 /* Append the overlay string STRING of overlay OVERLAY to vector
5697 `entries' which has size `size' and currently contains `n'
5698 elements. AFTER_P means STRING is an after-string of
5699 OVERLAY. */
5700 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5701 do \
5702 { \
5703 Lisp_Object priority; \
5704 \
5705 if (n == size) \
5706 { \
5707 struct overlay_entry *old = entries; \
5708 SAFE_NALLOCA (entries, 2, size); \
5709 memcpy (entries, old, size * sizeof *entries); \
5710 size *= 2; \
5711 } \
5712 \
5713 entries[n].string = (STRING); \
5714 entries[n].overlay = (OVERLAY); \
5715 priority = Foverlay_get ((OVERLAY), Qpriority); \
5716 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5717 entries[n].after_string_p = (AFTER_P); \
5718 ++n; \
5719 } \
5720 while (false)
5721
5722 /* Process overlay before the overlay center. */
5723 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5724 {
5725 XSETMISC (overlay, ov);
5726 eassert (OVERLAYP (overlay));
5727 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5728 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5729
5730 if (end < charpos)
5731 break;
5732
5733 /* Skip this overlay if it doesn't start or end at IT's current
5734 position. */
5735 if (end != charpos && start != charpos)
5736 continue;
5737
5738 /* Skip this overlay if it doesn't apply to IT->w. */
5739 window = Foverlay_get (overlay, Qwindow);
5740 if (WINDOWP (window) && XWINDOW (window) != it->w)
5741 continue;
5742
5743 /* If the text ``under'' the overlay is invisible, both before-
5744 and after-strings from this overlay are visible; start and
5745 end position are indistinguishable. */
5746 invisible = Foverlay_get (overlay, Qinvisible);
5747 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5748
5749 /* If overlay has a non-empty before-string, record it. */
5750 if ((start == charpos || (end == charpos && invis != 0))
5751 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5752 && SCHARS (str))
5753 RECORD_OVERLAY_STRING (overlay, str, false);
5754
5755 /* If overlay has a non-empty after-string, record it. */
5756 if ((end == charpos || (start == charpos && invis != 0))
5757 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5758 && SCHARS (str))
5759 RECORD_OVERLAY_STRING (overlay, str, true);
5760 }
5761
5762 /* Process overlays after the overlay center. */
5763 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5764 {
5765 XSETMISC (overlay, ov);
5766 eassert (OVERLAYP (overlay));
5767 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5768 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5769
5770 if (start > charpos)
5771 break;
5772
5773 /* Skip this overlay if it doesn't start or end at IT's current
5774 position. */
5775 if (end != charpos && start != charpos)
5776 continue;
5777
5778 /* Skip this overlay if it doesn't apply to IT->w. */
5779 window = Foverlay_get (overlay, Qwindow);
5780 if (WINDOWP (window) && XWINDOW (window) != it->w)
5781 continue;
5782
5783 /* If the text ``under'' the overlay is invisible, it has a zero
5784 dimension, and both before- and after-strings apply. */
5785 invisible = Foverlay_get (overlay, Qinvisible);
5786 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5787
5788 /* If overlay has a non-empty before-string, record it. */
5789 if ((start == charpos || (end == charpos && invis != 0))
5790 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5791 && SCHARS (str))
5792 RECORD_OVERLAY_STRING (overlay, str, false);
5793
5794 /* If overlay has a non-empty after-string, record it. */
5795 if ((end == charpos || (start == charpos && invis != 0))
5796 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5797 && SCHARS (str))
5798 RECORD_OVERLAY_STRING (overlay, str, true);
5799 }
5800
5801 #undef RECORD_OVERLAY_STRING
5802
5803 /* Sort entries. */
5804 if (n > 1)
5805 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5806
5807 /* Record number of overlay strings, and where we computed it. */
5808 it->n_overlay_strings = n;
5809 it->overlay_strings_charpos = charpos;
5810
5811 /* IT->current.overlay_string_index is the number of overlay strings
5812 that have already been consumed by IT. Copy some of the
5813 remaining overlay strings to IT->overlay_strings. */
5814 i = 0;
5815 j = it->current.overlay_string_index;
5816 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5817 {
5818 it->overlay_strings[i] = entries[j].string;
5819 it->string_overlays[i++] = entries[j++].overlay;
5820 }
5821
5822 CHECK_IT (it);
5823 SAFE_FREE ();
5824 }
5825
5826
5827 /* Get the first chunk of overlay strings at IT's current buffer
5828 position, or at CHARPOS if that is > 0. Value is true if at
5829 least one overlay string was found. */
5830
5831 static bool
5832 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5833 {
5834 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5835 process. This fills IT->overlay_strings with strings, and sets
5836 IT->n_overlay_strings to the total number of strings to process.
5837 IT->pos.overlay_string_index has to be set temporarily to zero
5838 because load_overlay_strings needs this; it must be set to -1
5839 when no overlay strings are found because a zero value would
5840 indicate a position in the first overlay string. */
5841 it->current.overlay_string_index = 0;
5842 load_overlay_strings (it, charpos);
5843
5844 /* If we found overlay strings, set up IT to deliver display
5845 elements from the first one. Otherwise set up IT to deliver
5846 from current_buffer. */
5847 if (it->n_overlay_strings)
5848 {
5849 /* Make sure we know settings in current_buffer, so that we can
5850 restore meaningful values when we're done with the overlay
5851 strings. */
5852 if (compute_stop_p)
5853 compute_stop_pos (it);
5854 eassert (it->face_id >= 0);
5855
5856 /* Save IT's settings. They are restored after all overlay
5857 strings have been processed. */
5858 eassert (!compute_stop_p || it->sp == 0);
5859
5860 /* When called from handle_stop, there might be an empty display
5861 string loaded. In that case, don't bother saving it. But
5862 don't use this optimization with the bidi iterator, since we
5863 need the corresponding pop_it call to resync the bidi
5864 iterator's position with IT's position, after we are done
5865 with the overlay strings. (The corresponding call to pop_it
5866 in case of an empty display string is in
5867 next_overlay_string.) */
5868 if (!(!it->bidi_p
5869 && STRINGP (it->string) && !SCHARS (it->string)))
5870 push_it (it, NULL);
5871
5872 /* Set up IT to deliver display elements from the first overlay
5873 string. */
5874 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5875 it->string = it->overlay_strings[0];
5876 it->from_overlay = Qnil;
5877 it->stop_charpos = 0;
5878 eassert (STRINGP (it->string));
5879 it->end_charpos = SCHARS (it->string);
5880 it->prev_stop = 0;
5881 it->base_level_stop = 0;
5882 it->multibyte_p = STRING_MULTIBYTE (it->string);
5883 it->method = GET_FROM_STRING;
5884 it->from_disp_prop_p = 0;
5885
5886 /* Force paragraph direction to be that of the parent
5887 buffer. */
5888 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5889 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5890 else
5891 it->paragraph_embedding = L2R;
5892
5893 /* Set up the bidi iterator for this overlay string. */
5894 if (it->bidi_p)
5895 {
5896 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5897
5898 it->bidi_it.string.lstring = it->string;
5899 it->bidi_it.string.s = NULL;
5900 it->bidi_it.string.schars = SCHARS (it->string);
5901 it->bidi_it.string.bufpos = pos;
5902 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5903 it->bidi_it.string.unibyte = !it->multibyte_p;
5904 it->bidi_it.w = it->w;
5905 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5906 }
5907 return true;
5908 }
5909
5910 it->current.overlay_string_index = -1;
5911 return false;
5912 }
5913
5914 static bool
5915 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5916 {
5917 it->string = Qnil;
5918 it->method = GET_FROM_BUFFER;
5919
5920 get_overlay_strings_1 (it, charpos, true);
5921
5922 CHECK_IT (it);
5923
5924 /* Value is true if we found at least one overlay string. */
5925 return STRINGP (it->string);
5926 }
5927
5928
5929 \f
5930 /***********************************************************************
5931 Saving and restoring state
5932 ***********************************************************************/
5933
5934 /* Save current settings of IT on IT->stack. Called, for example,
5935 before setting up IT for an overlay string, to be able to restore
5936 IT's settings to what they were after the overlay string has been
5937 processed. If POSITION is non-NULL, it is the position to save on
5938 the stack instead of IT->position. */
5939
5940 static void
5941 push_it (struct it *it, struct text_pos *position)
5942 {
5943 struct iterator_stack_entry *p;
5944
5945 eassert (it->sp < IT_STACK_SIZE);
5946 p = it->stack + it->sp;
5947
5948 p->stop_charpos = it->stop_charpos;
5949 p->prev_stop = it->prev_stop;
5950 p->base_level_stop = it->base_level_stop;
5951 p->cmp_it = it->cmp_it;
5952 eassert (it->face_id >= 0);
5953 p->face_id = it->face_id;
5954 p->string = it->string;
5955 p->method = it->method;
5956 p->from_overlay = it->from_overlay;
5957 switch (p->method)
5958 {
5959 case GET_FROM_IMAGE:
5960 p->u.image.object = it->object;
5961 p->u.image.image_id = it->image_id;
5962 p->u.image.slice = it->slice;
5963 break;
5964 case GET_FROM_STRETCH:
5965 p->u.stretch.object = it->object;
5966 break;
5967 case GET_FROM_BUFFER:
5968 case GET_FROM_DISPLAY_VECTOR:
5969 case GET_FROM_STRING:
5970 case GET_FROM_C_STRING:
5971 break;
5972 default:
5973 emacs_abort ();
5974 }
5975 p->position = position ? *position : it->position;
5976 p->current = it->current;
5977 p->end_charpos = it->end_charpos;
5978 p->string_nchars = it->string_nchars;
5979 p->area = it->area;
5980 p->multibyte_p = it->multibyte_p;
5981 p->avoid_cursor_p = it->avoid_cursor_p;
5982 p->space_width = it->space_width;
5983 p->font_height = it->font_height;
5984 p->voffset = it->voffset;
5985 p->string_from_display_prop_p = it->string_from_display_prop_p;
5986 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5987 p->display_ellipsis_p = false;
5988 p->line_wrap = it->line_wrap;
5989 p->bidi_p = it->bidi_p;
5990 p->paragraph_embedding = it->paragraph_embedding;
5991 p->from_disp_prop_p = it->from_disp_prop_p;
5992 ++it->sp;
5993
5994 /* Save the state of the bidi iterator as well. */
5995 if (it->bidi_p)
5996 bidi_push_it (&it->bidi_it);
5997 }
5998
5999 static void
6000 iterate_out_of_display_property (struct it *it)
6001 {
6002 bool buffer_p = !STRINGP (it->string);
6003 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6004 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6005
6006 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6007
6008 /* Maybe initialize paragraph direction. If we are at the beginning
6009 of a new paragraph, next_element_from_buffer may not have a
6010 chance to do that. */
6011 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6012 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6013 /* prev_stop can be zero, so check against BEGV as well. */
6014 while (it->bidi_it.charpos >= bob
6015 && it->prev_stop <= it->bidi_it.charpos
6016 && it->bidi_it.charpos < CHARPOS (it->position)
6017 && it->bidi_it.charpos < eob)
6018 bidi_move_to_visually_next (&it->bidi_it);
6019 /* Record the stop_pos we just crossed, for when we cross it
6020 back, maybe. */
6021 if (it->bidi_it.charpos > CHARPOS (it->position))
6022 it->prev_stop = CHARPOS (it->position);
6023 /* If we ended up not where pop_it put us, resync IT's
6024 positional members with the bidi iterator. */
6025 if (it->bidi_it.charpos != CHARPOS (it->position))
6026 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6027 if (buffer_p)
6028 it->current.pos = it->position;
6029 else
6030 it->current.string_pos = it->position;
6031 }
6032
6033 /* Restore IT's settings from IT->stack. Called, for example, when no
6034 more overlay strings must be processed, and we return to delivering
6035 display elements from a buffer, or when the end of a string from a
6036 `display' property is reached and we return to delivering display
6037 elements from an overlay string, or from a buffer. */
6038
6039 static void
6040 pop_it (struct it *it)
6041 {
6042 struct iterator_stack_entry *p;
6043 bool from_display_prop = it->from_disp_prop_p;
6044 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6045
6046 eassert (it->sp > 0);
6047 --it->sp;
6048 p = it->stack + it->sp;
6049 it->stop_charpos = p->stop_charpos;
6050 it->prev_stop = p->prev_stop;
6051 it->base_level_stop = p->base_level_stop;
6052 it->cmp_it = p->cmp_it;
6053 it->face_id = p->face_id;
6054 it->current = p->current;
6055 it->position = p->position;
6056 it->string = p->string;
6057 it->from_overlay = p->from_overlay;
6058 if (NILP (it->string))
6059 SET_TEXT_POS (it->current.string_pos, -1, -1);
6060 it->method = p->method;
6061 switch (it->method)
6062 {
6063 case GET_FROM_IMAGE:
6064 it->image_id = p->u.image.image_id;
6065 it->object = p->u.image.object;
6066 it->slice = p->u.image.slice;
6067 break;
6068 case GET_FROM_STRETCH:
6069 it->object = p->u.stretch.object;
6070 break;
6071 case GET_FROM_BUFFER:
6072 it->object = it->w->contents;
6073 break;
6074 case GET_FROM_STRING:
6075 {
6076 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6077
6078 /* Restore the face_box_p flag, since it could have been
6079 overwritten by the face of the object that we just finished
6080 displaying. */
6081 if (face)
6082 it->face_box_p = face->box != FACE_NO_BOX;
6083 it->object = it->string;
6084 }
6085 break;
6086 case GET_FROM_DISPLAY_VECTOR:
6087 if (it->s)
6088 it->method = GET_FROM_C_STRING;
6089 else if (STRINGP (it->string))
6090 it->method = GET_FROM_STRING;
6091 else
6092 {
6093 it->method = GET_FROM_BUFFER;
6094 it->object = it->w->contents;
6095 }
6096 break;
6097 case GET_FROM_C_STRING:
6098 break;
6099 default:
6100 emacs_abort ();
6101 }
6102 it->end_charpos = p->end_charpos;
6103 it->string_nchars = p->string_nchars;
6104 it->area = p->area;
6105 it->multibyte_p = p->multibyte_p;
6106 it->avoid_cursor_p = p->avoid_cursor_p;
6107 it->space_width = p->space_width;
6108 it->font_height = p->font_height;
6109 it->voffset = p->voffset;
6110 it->string_from_display_prop_p = p->string_from_display_prop_p;
6111 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6112 it->line_wrap = p->line_wrap;
6113 it->bidi_p = p->bidi_p;
6114 it->paragraph_embedding = p->paragraph_embedding;
6115 it->from_disp_prop_p = p->from_disp_prop_p;
6116 if (it->bidi_p)
6117 {
6118 bidi_pop_it (&it->bidi_it);
6119 /* Bidi-iterate until we get out of the portion of text, if any,
6120 covered by a `display' text property or by an overlay with
6121 `display' property. (We cannot just jump there, because the
6122 internal coherency of the bidi iterator state can not be
6123 preserved across such jumps.) We also must determine the
6124 paragraph base direction if the overlay we just processed is
6125 at the beginning of a new paragraph. */
6126 if (from_display_prop
6127 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6128 iterate_out_of_display_property (it);
6129
6130 eassert ((BUFFERP (it->object)
6131 && IT_CHARPOS (*it) == it->bidi_it.charpos
6132 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6133 || (STRINGP (it->object)
6134 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6135 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6136 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6137 }
6138 /* If we move the iterator over text covered by a display property
6139 to a new buffer position, any info about previously seen overlays
6140 is no longer valid. */
6141 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6142 it->ignore_overlay_strings_at_pos_p = false;
6143 }
6144
6145
6146 \f
6147 /***********************************************************************
6148 Moving over lines
6149 ***********************************************************************/
6150
6151 /* Set IT's current position to the previous line start. */
6152
6153 static void
6154 back_to_previous_line_start (struct it *it)
6155 {
6156 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6157
6158 DEC_BOTH (cp, bp);
6159 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6160 }
6161
6162
6163 /* Move IT to the next line start.
6164
6165 Value is true if a newline was found. Set *SKIPPED_P to true if
6166 we skipped over part of the text (as opposed to moving the iterator
6167 continuously over the text). Otherwise, don't change the value
6168 of *SKIPPED_P.
6169
6170 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6171 iterator on the newline, if it was found.
6172
6173 Newlines may come from buffer text, overlay strings, or strings
6174 displayed via the `display' property. That's the reason we can't
6175 simply use find_newline_no_quit.
6176
6177 Note that this function may not skip over invisible text that is so
6178 because of text properties and immediately follows a newline. If
6179 it would, function reseat_at_next_visible_line_start, when called
6180 from set_iterator_to_next, would effectively make invisible
6181 characters following a newline part of the wrong glyph row, which
6182 leads to wrong cursor motion. */
6183
6184 static bool
6185 forward_to_next_line_start (struct it *it, bool *skipped_p,
6186 struct bidi_it *bidi_it_prev)
6187 {
6188 ptrdiff_t old_selective;
6189 bool newline_found_p = false;
6190 int n;
6191 const int MAX_NEWLINE_DISTANCE = 500;
6192
6193 /* If already on a newline, just consume it to avoid unintended
6194 skipping over invisible text below. */
6195 if (it->what == IT_CHARACTER
6196 && it->c == '\n'
6197 && CHARPOS (it->position) == IT_CHARPOS (*it))
6198 {
6199 if (it->bidi_p && bidi_it_prev)
6200 *bidi_it_prev = it->bidi_it;
6201 set_iterator_to_next (it, false);
6202 it->c = 0;
6203 return true;
6204 }
6205
6206 /* Don't handle selective display in the following. It's (a)
6207 unnecessary because it's done by the caller, and (b) leads to an
6208 infinite recursion because next_element_from_ellipsis indirectly
6209 calls this function. */
6210 old_selective = it->selective;
6211 it->selective = 0;
6212
6213 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6214 from buffer text. */
6215 for (n = 0;
6216 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6217 n += !STRINGP (it->string))
6218 {
6219 if (!get_next_display_element (it))
6220 return false;
6221 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6222 if (newline_found_p && it->bidi_p && bidi_it_prev)
6223 *bidi_it_prev = it->bidi_it;
6224 set_iterator_to_next (it, false);
6225 }
6226
6227 /* If we didn't find a newline near enough, see if we can use a
6228 short-cut. */
6229 if (!newline_found_p)
6230 {
6231 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6232 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6233 1, &bytepos);
6234 Lisp_Object pos;
6235
6236 eassert (!STRINGP (it->string));
6237
6238 /* If there isn't any `display' property in sight, and no
6239 overlays, we can just use the position of the newline in
6240 buffer text. */
6241 if (it->stop_charpos >= limit
6242 || ((pos = Fnext_single_property_change (make_number (start),
6243 Qdisplay, Qnil,
6244 make_number (limit)),
6245 NILP (pos))
6246 && next_overlay_change (start) == ZV))
6247 {
6248 if (!it->bidi_p)
6249 {
6250 IT_CHARPOS (*it) = limit;
6251 IT_BYTEPOS (*it) = bytepos;
6252 }
6253 else
6254 {
6255 struct bidi_it bprev;
6256
6257 /* Help bidi.c avoid expensive searches for display
6258 properties and overlays, by telling it that there are
6259 none up to `limit'. */
6260 if (it->bidi_it.disp_pos < limit)
6261 {
6262 it->bidi_it.disp_pos = limit;
6263 it->bidi_it.disp_prop = 0;
6264 }
6265 do {
6266 bprev = it->bidi_it;
6267 bidi_move_to_visually_next (&it->bidi_it);
6268 } while (it->bidi_it.charpos != limit);
6269 IT_CHARPOS (*it) = limit;
6270 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6271 if (bidi_it_prev)
6272 *bidi_it_prev = bprev;
6273 }
6274 *skipped_p = newline_found_p = true;
6275 }
6276 else
6277 {
6278 while (get_next_display_element (it)
6279 && !newline_found_p)
6280 {
6281 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6282 if (newline_found_p && it->bidi_p && bidi_it_prev)
6283 *bidi_it_prev = it->bidi_it;
6284 set_iterator_to_next (it, false);
6285 }
6286 }
6287 }
6288
6289 it->selective = old_selective;
6290 return newline_found_p;
6291 }
6292
6293
6294 /* Set IT's current position to the previous visible line start. Skip
6295 invisible text that is so either due to text properties or due to
6296 selective display. Caution: this does not change IT->current_x and
6297 IT->hpos. */
6298
6299 static void
6300 back_to_previous_visible_line_start (struct it *it)
6301 {
6302 while (IT_CHARPOS (*it) > BEGV)
6303 {
6304 back_to_previous_line_start (it);
6305
6306 if (IT_CHARPOS (*it) <= BEGV)
6307 break;
6308
6309 /* If selective > 0, then lines indented more than its value are
6310 invisible. */
6311 if (it->selective > 0
6312 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6313 it->selective))
6314 continue;
6315
6316 /* Check the newline before point for invisibility. */
6317 {
6318 Lisp_Object prop;
6319 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6320 Qinvisible, it->window);
6321 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6322 continue;
6323 }
6324
6325 if (IT_CHARPOS (*it) <= BEGV)
6326 break;
6327
6328 {
6329 struct it it2;
6330 void *it2data = NULL;
6331 ptrdiff_t pos;
6332 ptrdiff_t beg, end;
6333 Lisp_Object val, overlay;
6334
6335 SAVE_IT (it2, *it, it2data);
6336
6337 /* If newline is part of a composition, continue from start of composition */
6338 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6339 && beg < IT_CHARPOS (*it))
6340 goto replaced;
6341
6342 /* If newline is replaced by a display property, find start of overlay
6343 or interval and continue search from that point. */
6344 pos = --IT_CHARPOS (it2);
6345 --IT_BYTEPOS (it2);
6346 it2.sp = 0;
6347 bidi_unshelve_cache (NULL, false);
6348 it2.string_from_display_prop_p = false;
6349 it2.from_disp_prop_p = false;
6350 if (handle_display_prop (&it2) == HANDLED_RETURN
6351 && !NILP (val = get_char_property_and_overlay
6352 (make_number (pos), Qdisplay, Qnil, &overlay))
6353 && (OVERLAYP (overlay)
6354 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6355 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6356 {
6357 RESTORE_IT (it, it, it2data);
6358 goto replaced;
6359 }
6360
6361 /* Newline is not replaced by anything -- so we are done. */
6362 RESTORE_IT (it, it, it2data);
6363 break;
6364
6365 replaced:
6366 if (beg < BEGV)
6367 beg = BEGV;
6368 IT_CHARPOS (*it) = beg;
6369 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6370 }
6371 }
6372
6373 it->continuation_lines_width = 0;
6374
6375 eassert (IT_CHARPOS (*it) >= BEGV);
6376 eassert (IT_CHARPOS (*it) == BEGV
6377 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6378 CHECK_IT (it);
6379 }
6380
6381
6382 /* Reseat iterator IT at the previous visible line start. Skip
6383 invisible text that is so either due to text properties or due to
6384 selective display. At the end, update IT's overlay information,
6385 face information etc. */
6386
6387 void
6388 reseat_at_previous_visible_line_start (struct it *it)
6389 {
6390 back_to_previous_visible_line_start (it);
6391 reseat (it, it->current.pos, true);
6392 CHECK_IT (it);
6393 }
6394
6395
6396 /* Reseat iterator IT on the next visible line start in the current
6397 buffer. ON_NEWLINE_P means position IT on the newline
6398 preceding the line start. Skip over invisible text that is so
6399 because of selective display. Compute faces, overlays etc at the
6400 new position. Note that this function does not skip over text that
6401 is invisible because of text properties. */
6402
6403 static void
6404 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6405 {
6406 bool skipped_p = false;
6407 struct bidi_it bidi_it_prev;
6408 bool newline_found_p
6409 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6410
6411 /* Skip over lines that are invisible because they are indented
6412 more than the value of IT->selective. */
6413 if (it->selective > 0)
6414 while (IT_CHARPOS (*it) < ZV
6415 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6416 it->selective))
6417 {
6418 eassert (IT_BYTEPOS (*it) == BEGV
6419 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6420 newline_found_p =
6421 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6422 }
6423
6424 /* Position on the newline if that's what's requested. */
6425 if (on_newline_p && newline_found_p)
6426 {
6427 if (STRINGP (it->string))
6428 {
6429 if (IT_STRING_CHARPOS (*it) > 0)
6430 {
6431 if (!it->bidi_p)
6432 {
6433 --IT_STRING_CHARPOS (*it);
6434 --IT_STRING_BYTEPOS (*it);
6435 }
6436 else
6437 {
6438 /* We need to restore the bidi iterator to the state
6439 it had on the newline, and resync the IT's
6440 position with that. */
6441 it->bidi_it = bidi_it_prev;
6442 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6443 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6444 }
6445 }
6446 }
6447 else if (IT_CHARPOS (*it) > BEGV)
6448 {
6449 if (!it->bidi_p)
6450 {
6451 --IT_CHARPOS (*it);
6452 --IT_BYTEPOS (*it);
6453 }
6454 else
6455 {
6456 /* We need to restore the bidi iterator to the state it
6457 had on the newline and resync IT with that. */
6458 it->bidi_it = bidi_it_prev;
6459 IT_CHARPOS (*it) = it->bidi_it.charpos;
6460 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6461 }
6462 reseat (it, it->current.pos, false);
6463 }
6464 }
6465 else if (skipped_p)
6466 reseat (it, it->current.pos, false);
6467
6468 CHECK_IT (it);
6469 }
6470
6471
6472 \f
6473 /***********************************************************************
6474 Changing an iterator's position
6475 ***********************************************************************/
6476
6477 /* Change IT's current position to POS in current_buffer.
6478 If FORCE_P, always check for text properties at the new position.
6479 Otherwise, text properties are only looked up if POS >=
6480 IT->check_charpos of a property. */
6481
6482 static void
6483 reseat (struct it *it, struct text_pos pos, bool force_p)
6484 {
6485 ptrdiff_t original_pos = IT_CHARPOS (*it);
6486
6487 reseat_1 (it, pos, false);
6488
6489 /* Determine where to check text properties. Avoid doing it
6490 where possible because text property lookup is very expensive. */
6491 if (force_p
6492 || CHARPOS (pos) > it->stop_charpos
6493 || CHARPOS (pos) < original_pos)
6494 {
6495 if (it->bidi_p)
6496 {
6497 /* For bidi iteration, we need to prime prev_stop and
6498 base_level_stop with our best estimations. */
6499 /* Implementation note: Of course, POS is not necessarily a
6500 stop position, so assigning prev_pos to it is a lie; we
6501 should have called compute_stop_backwards. However, if
6502 the current buffer does not include any R2L characters,
6503 that call would be a waste of cycles, because the
6504 iterator will never move back, and thus never cross this
6505 "fake" stop position. So we delay that backward search
6506 until the time we really need it, in next_element_from_buffer. */
6507 if (CHARPOS (pos) != it->prev_stop)
6508 it->prev_stop = CHARPOS (pos);
6509 if (CHARPOS (pos) < it->base_level_stop)
6510 it->base_level_stop = 0; /* meaning it's unknown */
6511 handle_stop (it);
6512 }
6513 else
6514 {
6515 handle_stop (it);
6516 it->prev_stop = it->base_level_stop = 0;
6517 }
6518
6519 }
6520
6521 CHECK_IT (it);
6522 }
6523
6524
6525 /* Change IT's buffer position to POS. SET_STOP_P means set
6526 IT->stop_pos to POS, also. */
6527
6528 static void
6529 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6530 {
6531 /* Don't call this function when scanning a C string. */
6532 eassert (it->s == NULL);
6533
6534 /* POS must be a reasonable value. */
6535 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6536
6537 it->current.pos = it->position = pos;
6538 it->end_charpos = ZV;
6539 it->dpvec = NULL;
6540 it->current.dpvec_index = -1;
6541 it->current.overlay_string_index = -1;
6542 IT_STRING_CHARPOS (*it) = -1;
6543 IT_STRING_BYTEPOS (*it) = -1;
6544 it->string = Qnil;
6545 it->method = GET_FROM_BUFFER;
6546 it->object = it->w->contents;
6547 it->area = TEXT_AREA;
6548 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6549 it->sp = 0;
6550 it->string_from_display_prop_p = false;
6551 it->string_from_prefix_prop_p = false;
6552
6553 it->from_disp_prop_p = false;
6554 it->face_before_selective_p = false;
6555 if (it->bidi_p)
6556 {
6557 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6558 &it->bidi_it);
6559 bidi_unshelve_cache (NULL, false);
6560 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6561 it->bidi_it.string.s = NULL;
6562 it->bidi_it.string.lstring = Qnil;
6563 it->bidi_it.string.bufpos = 0;
6564 it->bidi_it.string.from_disp_str = false;
6565 it->bidi_it.string.unibyte = false;
6566 it->bidi_it.w = it->w;
6567 }
6568
6569 if (set_stop_p)
6570 {
6571 it->stop_charpos = CHARPOS (pos);
6572 it->base_level_stop = CHARPOS (pos);
6573 }
6574 /* This make the information stored in it->cmp_it invalidate. */
6575 it->cmp_it.id = -1;
6576 }
6577
6578
6579 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6580 If S is non-null, it is a C string to iterate over. Otherwise,
6581 STRING gives a Lisp string to iterate over.
6582
6583 If PRECISION > 0, don't return more then PRECISION number of
6584 characters from the string.
6585
6586 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6587 characters have been returned. FIELD_WIDTH < 0 means an infinite
6588 field width.
6589
6590 MULTIBYTE = 0 means disable processing of multibyte characters,
6591 MULTIBYTE > 0 means enable it,
6592 MULTIBYTE < 0 means use IT->multibyte_p.
6593
6594 IT must be initialized via a prior call to init_iterator before
6595 calling this function. */
6596
6597 static void
6598 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6599 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6600 int multibyte)
6601 {
6602 /* No text property checks performed by default, but see below. */
6603 it->stop_charpos = -1;
6604
6605 /* Set iterator position and end position. */
6606 memset (&it->current, 0, sizeof it->current);
6607 it->current.overlay_string_index = -1;
6608 it->current.dpvec_index = -1;
6609 eassert (charpos >= 0);
6610
6611 /* If STRING is specified, use its multibyteness, otherwise use the
6612 setting of MULTIBYTE, if specified. */
6613 if (multibyte >= 0)
6614 it->multibyte_p = multibyte > 0;
6615
6616 /* Bidirectional reordering of strings is controlled by the default
6617 value of bidi-display-reordering. Don't try to reorder while
6618 loading loadup.el, as the necessary character property tables are
6619 not yet available. */
6620 it->bidi_p =
6621 NILP (Vpurify_flag)
6622 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6623
6624 if (s == NULL)
6625 {
6626 eassert (STRINGP (string));
6627 it->string = string;
6628 it->s = NULL;
6629 it->end_charpos = it->string_nchars = SCHARS (string);
6630 it->method = GET_FROM_STRING;
6631 it->current.string_pos = string_pos (charpos, string);
6632
6633 if (it->bidi_p)
6634 {
6635 it->bidi_it.string.lstring = string;
6636 it->bidi_it.string.s = NULL;
6637 it->bidi_it.string.schars = it->end_charpos;
6638 it->bidi_it.string.bufpos = 0;
6639 it->bidi_it.string.from_disp_str = false;
6640 it->bidi_it.string.unibyte = !it->multibyte_p;
6641 it->bidi_it.w = it->w;
6642 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6643 FRAME_WINDOW_P (it->f), &it->bidi_it);
6644 }
6645 }
6646 else
6647 {
6648 it->s = (const unsigned char *) s;
6649 it->string = Qnil;
6650
6651 /* Note that we use IT->current.pos, not it->current.string_pos,
6652 for displaying C strings. */
6653 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6654 if (it->multibyte_p)
6655 {
6656 it->current.pos = c_string_pos (charpos, s, true);
6657 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6658 }
6659 else
6660 {
6661 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6662 it->end_charpos = it->string_nchars = strlen (s);
6663 }
6664
6665 if (it->bidi_p)
6666 {
6667 it->bidi_it.string.lstring = Qnil;
6668 it->bidi_it.string.s = (const unsigned char *) s;
6669 it->bidi_it.string.schars = it->end_charpos;
6670 it->bidi_it.string.bufpos = 0;
6671 it->bidi_it.string.from_disp_str = false;
6672 it->bidi_it.string.unibyte = !it->multibyte_p;
6673 it->bidi_it.w = it->w;
6674 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6675 &it->bidi_it);
6676 }
6677 it->method = GET_FROM_C_STRING;
6678 }
6679
6680 /* PRECISION > 0 means don't return more than PRECISION characters
6681 from the string. */
6682 if (precision > 0 && it->end_charpos - charpos > precision)
6683 {
6684 it->end_charpos = it->string_nchars = charpos + precision;
6685 if (it->bidi_p)
6686 it->bidi_it.string.schars = it->end_charpos;
6687 }
6688
6689 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6690 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6691 FIELD_WIDTH < 0 means infinite field width. This is useful for
6692 padding with `-' at the end of a mode line. */
6693 if (field_width < 0)
6694 field_width = INFINITY;
6695 /* Implementation note: We deliberately don't enlarge
6696 it->bidi_it.string.schars here to fit it->end_charpos, because
6697 the bidi iterator cannot produce characters out of thin air. */
6698 if (field_width > it->end_charpos - charpos)
6699 it->end_charpos = charpos + field_width;
6700
6701 /* Use the standard display table for displaying strings. */
6702 if (DISP_TABLE_P (Vstandard_display_table))
6703 it->dp = XCHAR_TABLE (Vstandard_display_table);
6704
6705 it->stop_charpos = charpos;
6706 it->prev_stop = charpos;
6707 it->base_level_stop = 0;
6708 if (it->bidi_p)
6709 {
6710 it->bidi_it.first_elt = true;
6711 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6712 it->bidi_it.disp_pos = -1;
6713 }
6714 if (s == NULL && it->multibyte_p)
6715 {
6716 ptrdiff_t endpos = SCHARS (it->string);
6717 if (endpos > it->end_charpos)
6718 endpos = it->end_charpos;
6719 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6720 it->string);
6721 }
6722 CHECK_IT (it);
6723 }
6724
6725
6726 \f
6727 /***********************************************************************
6728 Iteration
6729 ***********************************************************************/
6730
6731 /* Map enum it_method value to corresponding next_element_from_* function. */
6732
6733 typedef bool (*next_element_function) (struct it *);
6734
6735 static next_element_function const get_next_element[NUM_IT_METHODS] =
6736 {
6737 next_element_from_buffer,
6738 next_element_from_display_vector,
6739 next_element_from_string,
6740 next_element_from_c_string,
6741 next_element_from_image,
6742 next_element_from_stretch
6743 };
6744
6745 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6746
6747
6748 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6749 (possibly with the following characters). */
6750
6751 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6752 ((IT)->cmp_it.id >= 0 \
6753 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6754 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6755 END_CHARPOS, (IT)->w, \
6756 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6757 (IT)->string)))
6758
6759
6760 /* Lookup the char-table Vglyphless_char_display for character C (-1
6761 if we want information for no-font case), and return the display
6762 method symbol. By side-effect, update it->what and
6763 it->glyphless_method. This function is called from
6764 get_next_display_element for each character element, and from
6765 x_produce_glyphs when no suitable font was found. */
6766
6767 Lisp_Object
6768 lookup_glyphless_char_display (int c, struct it *it)
6769 {
6770 Lisp_Object glyphless_method = Qnil;
6771
6772 if (CHAR_TABLE_P (Vglyphless_char_display)
6773 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6774 {
6775 if (c >= 0)
6776 {
6777 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6778 if (CONSP (glyphless_method))
6779 glyphless_method = FRAME_WINDOW_P (it->f)
6780 ? XCAR (glyphless_method)
6781 : XCDR (glyphless_method);
6782 }
6783 else
6784 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6785 }
6786
6787 retry:
6788 if (NILP (glyphless_method))
6789 {
6790 if (c >= 0)
6791 /* The default is to display the character by a proper font. */
6792 return Qnil;
6793 /* The default for the no-font case is to display an empty box. */
6794 glyphless_method = Qempty_box;
6795 }
6796 if (EQ (glyphless_method, Qzero_width))
6797 {
6798 if (c >= 0)
6799 return glyphless_method;
6800 /* This method can't be used for the no-font case. */
6801 glyphless_method = Qempty_box;
6802 }
6803 if (EQ (glyphless_method, Qthin_space))
6804 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6805 else if (EQ (glyphless_method, Qempty_box))
6806 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6807 else if (EQ (glyphless_method, Qhex_code))
6808 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6809 else if (STRINGP (glyphless_method))
6810 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6811 else
6812 {
6813 /* Invalid value. We use the default method. */
6814 glyphless_method = Qnil;
6815 goto retry;
6816 }
6817 it->what = IT_GLYPHLESS;
6818 return glyphless_method;
6819 }
6820
6821 /* Merge escape glyph face and cache the result. */
6822
6823 static struct frame *last_escape_glyph_frame = NULL;
6824 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6825 static int last_escape_glyph_merged_face_id = 0;
6826
6827 static int
6828 merge_escape_glyph_face (struct it *it)
6829 {
6830 int face_id;
6831
6832 if (it->f == last_escape_glyph_frame
6833 && it->face_id == last_escape_glyph_face_id)
6834 face_id = last_escape_glyph_merged_face_id;
6835 else
6836 {
6837 /* Merge the `escape-glyph' face into the current face. */
6838 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6839 last_escape_glyph_frame = it->f;
6840 last_escape_glyph_face_id = it->face_id;
6841 last_escape_glyph_merged_face_id = face_id;
6842 }
6843 return face_id;
6844 }
6845
6846 /* Likewise for glyphless glyph face. */
6847
6848 static struct frame *last_glyphless_glyph_frame = NULL;
6849 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6850 static int last_glyphless_glyph_merged_face_id = 0;
6851
6852 int
6853 merge_glyphless_glyph_face (struct it *it)
6854 {
6855 int face_id;
6856
6857 if (it->f == last_glyphless_glyph_frame
6858 && it->face_id == last_glyphless_glyph_face_id)
6859 face_id = last_glyphless_glyph_merged_face_id;
6860 else
6861 {
6862 /* Merge the `glyphless-char' face into the current face. */
6863 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6864 last_glyphless_glyph_frame = it->f;
6865 last_glyphless_glyph_face_id = it->face_id;
6866 last_glyphless_glyph_merged_face_id = face_id;
6867 }
6868 return face_id;
6869 }
6870
6871 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6872 be called before redisplaying windows, and when the frame's face
6873 cache is freed. */
6874 void
6875 forget_escape_and_glyphless_faces (void)
6876 {
6877 last_escape_glyph_frame = NULL;
6878 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6879 last_glyphless_glyph_frame = NULL;
6880 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6881 }
6882
6883 /* Load IT's display element fields with information about the next
6884 display element from the current position of IT. Value is false if
6885 end of buffer (or C string) is reached. */
6886
6887 static bool
6888 get_next_display_element (struct it *it)
6889 {
6890 /* True means that we found a display element. False means that
6891 we hit the end of what we iterate over. Performance note: the
6892 function pointer `method' used here turns out to be faster than
6893 using a sequence of if-statements. */
6894 bool success_p;
6895
6896 get_next:
6897 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6898
6899 if (it->what == IT_CHARACTER)
6900 {
6901 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6902 and only if (a) the resolved directionality of that character
6903 is R..." */
6904 /* FIXME: Do we need an exception for characters from display
6905 tables? */
6906 if (it->bidi_p && it->bidi_it.type == STRONG_R
6907 && !inhibit_bidi_mirroring)
6908 it->c = bidi_mirror_char (it->c);
6909 /* Map via display table or translate control characters.
6910 IT->c, IT->len etc. have been set to the next character by
6911 the function call above. If we have a display table, and it
6912 contains an entry for IT->c, translate it. Don't do this if
6913 IT->c itself comes from a display table, otherwise we could
6914 end up in an infinite recursion. (An alternative could be to
6915 count the recursion depth of this function and signal an
6916 error when a certain maximum depth is reached.) Is it worth
6917 it? */
6918 if (success_p && it->dpvec == NULL)
6919 {
6920 Lisp_Object dv;
6921 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6922 bool nonascii_space_p = false;
6923 bool nonascii_hyphen_p = false;
6924 int c = it->c; /* This is the character to display. */
6925
6926 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6927 {
6928 eassert (SINGLE_BYTE_CHAR_P (c));
6929 if (unibyte_display_via_language_environment)
6930 {
6931 c = DECODE_CHAR (unibyte, c);
6932 if (c < 0)
6933 c = BYTE8_TO_CHAR (it->c);
6934 }
6935 else
6936 c = BYTE8_TO_CHAR (it->c);
6937 }
6938
6939 if (it->dp
6940 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6941 VECTORP (dv)))
6942 {
6943 struct Lisp_Vector *v = XVECTOR (dv);
6944
6945 /* Return the first character from the display table
6946 entry, if not empty. If empty, don't display the
6947 current character. */
6948 if (v->header.size)
6949 {
6950 it->dpvec_char_len = it->len;
6951 it->dpvec = v->contents;
6952 it->dpend = v->contents + v->header.size;
6953 it->current.dpvec_index = 0;
6954 it->dpvec_face_id = -1;
6955 it->saved_face_id = it->face_id;
6956 it->method = GET_FROM_DISPLAY_VECTOR;
6957 it->ellipsis_p = false;
6958 }
6959 else
6960 {
6961 set_iterator_to_next (it, false);
6962 }
6963 goto get_next;
6964 }
6965
6966 if (! NILP (lookup_glyphless_char_display (c, it)))
6967 {
6968 if (it->what == IT_GLYPHLESS)
6969 goto done;
6970 /* Don't display this character. */
6971 set_iterator_to_next (it, false);
6972 goto get_next;
6973 }
6974
6975 /* If `nobreak-char-display' is non-nil, we display
6976 non-ASCII spaces and hyphens specially. */
6977 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6978 {
6979 if (c == NO_BREAK_SPACE)
6980 nonascii_space_p = true;
6981 else if (c == SOFT_HYPHEN || c == HYPHEN
6982 || c == NON_BREAKING_HYPHEN)
6983 nonascii_hyphen_p = true;
6984 }
6985
6986 /* Translate control characters into `\003' or `^C' form.
6987 Control characters coming from a display table entry are
6988 currently not translated because we use IT->dpvec to hold
6989 the translation. This could easily be changed but I
6990 don't believe that it is worth doing.
6991
6992 The characters handled by `nobreak-char-display' must be
6993 translated too.
6994
6995 Non-printable characters and raw-byte characters are also
6996 translated to octal form. */
6997 if (((c < ' ' || c == 127) /* ASCII control chars. */
6998 ? (it->area != TEXT_AREA
6999 /* In mode line, treat \n, \t like other crl chars. */
7000 || (c != '\t'
7001 && it->glyph_row
7002 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7003 || (c != '\n' && c != '\t'))
7004 : (nonascii_space_p
7005 || nonascii_hyphen_p
7006 || CHAR_BYTE8_P (c)
7007 || ! CHAR_PRINTABLE_P (c))))
7008 {
7009 /* C is a control character, non-ASCII space/hyphen,
7010 raw-byte, or a non-printable character which must be
7011 displayed either as '\003' or as `^C' where the '\\'
7012 and '^' can be defined in the display table. Fill
7013 IT->ctl_chars with glyphs for what we have to
7014 display. Then, set IT->dpvec to these glyphs. */
7015 Lisp_Object gc;
7016 int ctl_len;
7017 int face_id;
7018 int lface_id = 0;
7019 int escape_glyph;
7020
7021 /* Handle control characters with ^. */
7022
7023 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7024 {
7025 int g;
7026
7027 g = '^'; /* default glyph for Control */
7028 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7029 if (it->dp
7030 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7031 {
7032 g = GLYPH_CODE_CHAR (gc);
7033 lface_id = GLYPH_CODE_FACE (gc);
7034 }
7035
7036 face_id = (lface_id
7037 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7038 : merge_escape_glyph_face (it));
7039
7040 XSETINT (it->ctl_chars[0], g);
7041 XSETINT (it->ctl_chars[1], c ^ 0100);
7042 ctl_len = 2;
7043 goto display_control;
7044 }
7045
7046 /* Handle non-ascii space in the mode where it only gets
7047 highlighting. */
7048
7049 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7050 {
7051 /* Merge `nobreak-space' into the current face. */
7052 face_id = merge_faces (it->f, Qnobreak_space, 0,
7053 it->face_id);
7054 XSETINT (it->ctl_chars[0], ' ');
7055 ctl_len = 1;
7056 goto display_control;
7057 }
7058
7059 /* Handle sequences that start with the "escape glyph". */
7060
7061 /* the default escape glyph is \. */
7062 escape_glyph = '\\';
7063
7064 if (it->dp
7065 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7066 {
7067 escape_glyph = GLYPH_CODE_CHAR (gc);
7068 lface_id = GLYPH_CODE_FACE (gc);
7069 }
7070
7071 face_id = (lface_id
7072 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7073 : merge_escape_glyph_face (it));
7074
7075 /* Draw non-ASCII hyphen with just highlighting: */
7076
7077 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7078 {
7079 XSETINT (it->ctl_chars[0], '-');
7080 ctl_len = 1;
7081 goto display_control;
7082 }
7083
7084 /* Draw non-ASCII space/hyphen with escape glyph: */
7085
7086 if (nonascii_space_p || nonascii_hyphen_p)
7087 {
7088 XSETINT (it->ctl_chars[0], escape_glyph);
7089 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7090 ctl_len = 2;
7091 goto display_control;
7092 }
7093
7094 {
7095 char str[10];
7096 int len, i;
7097
7098 if (CHAR_BYTE8_P (c))
7099 /* Display \200 instead of \17777600. */
7100 c = CHAR_TO_BYTE8 (c);
7101 len = sprintf (str, "%03o", c + 0u);
7102
7103 XSETINT (it->ctl_chars[0], escape_glyph);
7104 for (i = 0; i < len; i++)
7105 XSETINT (it->ctl_chars[i + 1], str[i]);
7106 ctl_len = len + 1;
7107 }
7108
7109 display_control:
7110 /* Set up IT->dpvec and return first character from it. */
7111 it->dpvec_char_len = it->len;
7112 it->dpvec = it->ctl_chars;
7113 it->dpend = it->dpvec + ctl_len;
7114 it->current.dpvec_index = 0;
7115 it->dpvec_face_id = face_id;
7116 it->saved_face_id = it->face_id;
7117 it->method = GET_FROM_DISPLAY_VECTOR;
7118 it->ellipsis_p = false;
7119 goto get_next;
7120 }
7121 it->char_to_display = c;
7122 }
7123 else if (success_p)
7124 {
7125 it->char_to_display = it->c;
7126 }
7127 }
7128
7129 #ifdef HAVE_WINDOW_SYSTEM
7130 /* Adjust face id for a multibyte character. There are no multibyte
7131 character in unibyte text. */
7132 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7133 && it->multibyte_p
7134 && success_p
7135 && FRAME_WINDOW_P (it->f))
7136 {
7137 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7138
7139 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7140 {
7141 /* Automatic composition with glyph-string. */
7142 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7143
7144 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7145 }
7146 else
7147 {
7148 ptrdiff_t pos = (it->s ? -1
7149 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7150 : IT_CHARPOS (*it));
7151 int c;
7152
7153 if (it->what == IT_CHARACTER)
7154 c = it->char_to_display;
7155 else
7156 {
7157 struct composition *cmp = composition_table[it->cmp_it.id];
7158 int i;
7159
7160 c = ' ';
7161 for (i = 0; i < cmp->glyph_len; i++)
7162 /* TAB in a composition means display glyphs with
7163 padding space on the left or right. */
7164 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7165 break;
7166 }
7167 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7168 }
7169 }
7170 #endif /* HAVE_WINDOW_SYSTEM */
7171
7172 done:
7173 /* Is this character the last one of a run of characters with
7174 box? If yes, set IT->end_of_box_run_p to true. */
7175 if (it->face_box_p
7176 && it->s == NULL)
7177 {
7178 if (it->method == GET_FROM_STRING && it->sp)
7179 {
7180 int face_id = underlying_face_id (it);
7181 struct face *face = FACE_FROM_ID (it->f, face_id);
7182
7183 if (face)
7184 {
7185 if (face->box == FACE_NO_BOX)
7186 {
7187 /* If the box comes from face properties in a
7188 display string, check faces in that string. */
7189 int string_face_id = face_after_it_pos (it);
7190 it->end_of_box_run_p
7191 = (FACE_FROM_ID (it->f, string_face_id)->box
7192 == FACE_NO_BOX);
7193 }
7194 /* Otherwise, the box comes from the underlying face.
7195 If this is the last string character displayed, check
7196 the next buffer location. */
7197 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7198 /* n_overlay_strings is unreliable unless
7199 overlay_string_index is non-negative. */
7200 && ((it->current.overlay_string_index >= 0
7201 && (it->current.overlay_string_index
7202 == it->n_overlay_strings - 1))
7203 /* A string from display property. */
7204 || it->from_disp_prop_p))
7205 {
7206 ptrdiff_t ignore;
7207 int next_face_id;
7208 struct text_pos pos = it->current.pos;
7209
7210 /* For a string from a display property, the next
7211 buffer position is stored in the 'position'
7212 member of the iteration stack slot below the
7213 current one, see handle_single_display_spec. By
7214 contrast, it->current.pos was is not yet updated
7215 to point to that buffer position; that will
7216 happen in pop_it, after we finish displaying the
7217 current string. Note that we already checked
7218 above that it->sp is positive, so subtracting one
7219 from it is safe. */
7220 if (it->from_disp_prop_p)
7221 pos = (it->stack + it->sp - 1)->position;
7222 else
7223 INC_TEXT_POS (pos, it->multibyte_p);
7224
7225 if (CHARPOS (pos) >= ZV)
7226 it->end_of_box_run_p = true;
7227 else
7228 {
7229 next_face_id = face_at_buffer_position
7230 (it->w, CHARPOS (pos), &ignore,
7231 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, false, -1);
7232 it->end_of_box_run_p
7233 = (FACE_FROM_ID (it->f, next_face_id)->box
7234 == FACE_NO_BOX);
7235 }
7236 }
7237 }
7238 }
7239 /* next_element_from_display_vector sets this flag according to
7240 faces of the display vector glyphs, see there. */
7241 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7242 {
7243 int face_id = face_after_it_pos (it);
7244 it->end_of_box_run_p
7245 = (face_id != it->face_id
7246 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7247 }
7248 }
7249 /* If we reached the end of the object we've been iterating (e.g., a
7250 display string or an overlay string), and there's something on
7251 IT->stack, proceed with what's on the stack. It doesn't make
7252 sense to return false if there's unprocessed stuff on the stack,
7253 because otherwise that stuff will never be displayed. */
7254 if (!success_p && it->sp > 0)
7255 {
7256 set_iterator_to_next (it, false);
7257 success_p = get_next_display_element (it);
7258 }
7259
7260 /* Value is false if end of buffer or string reached. */
7261 return success_p;
7262 }
7263
7264
7265 /* Move IT to the next display element.
7266
7267 RESEAT_P means if called on a newline in buffer text,
7268 skip to the next visible line start.
7269
7270 Functions get_next_display_element and set_iterator_to_next are
7271 separate because I find this arrangement easier to handle than a
7272 get_next_display_element function that also increments IT's
7273 position. The way it is we can first look at an iterator's current
7274 display element, decide whether it fits on a line, and if it does,
7275 increment the iterator position. The other way around we probably
7276 would either need a flag indicating whether the iterator has to be
7277 incremented the next time, or we would have to implement a
7278 decrement position function which would not be easy to write. */
7279
7280 void
7281 set_iterator_to_next (struct it *it, bool reseat_p)
7282 {
7283 /* Reset flags indicating start and end of a sequence of characters
7284 with box. Reset them at the start of this function because
7285 moving the iterator to a new position might set them. */
7286 it->start_of_box_run_p = it->end_of_box_run_p = false;
7287
7288 switch (it->method)
7289 {
7290 case GET_FROM_BUFFER:
7291 /* The current display element of IT is a character from
7292 current_buffer. Advance in the buffer, and maybe skip over
7293 invisible lines that are so because of selective display. */
7294 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7295 reseat_at_next_visible_line_start (it, false);
7296 else if (it->cmp_it.id >= 0)
7297 {
7298 /* We are currently getting glyphs from a composition. */
7299 if (! it->bidi_p)
7300 {
7301 IT_CHARPOS (*it) += it->cmp_it.nchars;
7302 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7303 }
7304 else
7305 {
7306 int i;
7307
7308 /* Update IT's char/byte positions to point to the first
7309 character of the next grapheme cluster, or to the
7310 character visually after the current composition. */
7311 for (i = 0; i < it->cmp_it.nchars; i++)
7312 bidi_move_to_visually_next (&it->bidi_it);
7313 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7314 IT_CHARPOS (*it) = it->bidi_it.charpos;
7315 }
7316
7317 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7318 && it->cmp_it.to < it->cmp_it.nglyphs)
7319 {
7320 /* Composition created while scanning forward. Proceed
7321 to the next grapheme cluster. */
7322 it->cmp_it.from = it->cmp_it.to;
7323 }
7324 else if ((it->bidi_p && it->cmp_it.reversed_p)
7325 && it->cmp_it.from > 0)
7326 {
7327 /* Composition created while scanning backward. Proceed
7328 to the previous grapheme cluster. */
7329 it->cmp_it.to = it->cmp_it.from;
7330 }
7331 else
7332 {
7333 /* No more grapheme clusters in this composition.
7334 Find the next stop position. */
7335 ptrdiff_t stop = it->end_charpos;
7336
7337 if (it->bidi_it.scan_dir < 0)
7338 /* Now we are scanning backward and don't know
7339 where to stop. */
7340 stop = -1;
7341 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7342 IT_BYTEPOS (*it), stop, Qnil);
7343 }
7344 }
7345 else
7346 {
7347 eassert (it->len != 0);
7348
7349 if (!it->bidi_p)
7350 {
7351 IT_BYTEPOS (*it) += it->len;
7352 IT_CHARPOS (*it) += 1;
7353 }
7354 else
7355 {
7356 int prev_scan_dir = it->bidi_it.scan_dir;
7357 /* If this is a new paragraph, determine its base
7358 direction (a.k.a. its base embedding level). */
7359 if (it->bidi_it.new_paragraph)
7360 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7361 false);
7362 bidi_move_to_visually_next (&it->bidi_it);
7363 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7364 IT_CHARPOS (*it) = it->bidi_it.charpos;
7365 if (prev_scan_dir != it->bidi_it.scan_dir)
7366 {
7367 /* As the scan direction was changed, we must
7368 re-compute the stop position for composition. */
7369 ptrdiff_t stop = it->end_charpos;
7370 if (it->bidi_it.scan_dir < 0)
7371 stop = -1;
7372 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7373 IT_BYTEPOS (*it), stop, Qnil);
7374 }
7375 }
7376 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7377 }
7378 break;
7379
7380 case GET_FROM_C_STRING:
7381 /* Current display element of IT is from a C string. */
7382 if (!it->bidi_p
7383 /* If the string position is beyond string's end, it means
7384 next_element_from_c_string is padding the string with
7385 blanks, in which case we bypass the bidi iterator,
7386 because it cannot deal with such virtual characters. */
7387 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7388 {
7389 IT_BYTEPOS (*it) += it->len;
7390 IT_CHARPOS (*it) += 1;
7391 }
7392 else
7393 {
7394 bidi_move_to_visually_next (&it->bidi_it);
7395 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7396 IT_CHARPOS (*it) = it->bidi_it.charpos;
7397 }
7398 break;
7399
7400 case GET_FROM_DISPLAY_VECTOR:
7401 /* Current display element of IT is from a display table entry.
7402 Advance in the display table definition. Reset it to null if
7403 end reached, and continue with characters from buffers/
7404 strings. */
7405 ++it->current.dpvec_index;
7406
7407 /* Restore face of the iterator to what they were before the
7408 display vector entry (these entries may contain faces). */
7409 it->face_id = it->saved_face_id;
7410
7411 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7412 {
7413 bool recheck_faces = it->ellipsis_p;
7414
7415 if (it->s)
7416 it->method = GET_FROM_C_STRING;
7417 else if (STRINGP (it->string))
7418 it->method = GET_FROM_STRING;
7419 else
7420 {
7421 it->method = GET_FROM_BUFFER;
7422 it->object = it->w->contents;
7423 }
7424
7425 it->dpvec = NULL;
7426 it->current.dpvec_index = -1;
7427
7428 /* Skip over characters which were displayed via IT->dpvec. */
7429 if (it->dpvec_char_len < 0)
7430 reseat_at_next_visible_line_start (it, true);
7431 else if (it->dpvec_char_len > 0)
7432 {
7433 it->len = it->dpvec_char_len;
7434 set_iterator_to_next (it, reseat_p);
7435 }
7436
7437 /* Maybe recheck faces after display vector. */
7438 if (recheck_faces)
7439 {
7440 if (it->method == GET_FROM_STRING)
7441 it->stop_charpos = IT_STRING_CHARPOS (*it);
7442 else
7443 it->stop_charpos = IT_CHARPOS (*it);
7444 }
7445 }
7446 break;
7447
7448 case GET_FROM_STRING:
7449 /* Current display element is a character from a Lisp string. */
7450 eassert (it->s == NULL && STRINGP (it->string));
7451 /* Don't advance past string end. These conditions are true
7452 when set_iterator_to_next is called at the end of
7453 get_next_display_element, in which case the Lisp string is
7454 already exhausted, and all we want is pop the iterator
7455 stack. */
7456 if (it->current.overlay_string_index >= 0)
7457 {
7458 /* This is an overlay string, so there's no padding with
7459 spaces, and the number of characters in the string is
7460 where the string ends. */
7461 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7462 goto consider_string_end;
7463 }
7464 else
7465 {
7466 /* Not an overlay string. There could be padding, so test
7467 against it->end_charpos. */
7468 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7469 goto consider_string_end;
7470 }
7471 if (it->cmp_it.id >= 0)
7472 {
7473 /* We are delivering display elements from a composition.
7474 Update the string position past the grapheme cluster
7475 we've just processed. */
7476 if (! it->bidi_p)
7477 {
7478 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7479 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7480 }
7481 else
7482 {
7483 int i;
7484
7485 for (i = 0; i < it->cmp_it.nchars; i++)
7486 bidi_move_to_visually_next (&it->bidi_it);
7487 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7488 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7489 }
7490
7491 /* Did we exhaust all the grapheme clusters of this
7492 composition? */
7493 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7494 && (it->cmp_it.to < it->cmp_it.nglyphs))
7495 {
7496 /* Not all the grapheme clusters were processed yet;
7497 advance to the next cluster. */
7498 it->cmp_it.from = it->cmp_it.to;
7499 }
7500 else if ((it->bidi_p && it->cmp_it.reversed_p)
7501 && it->cmp_it.from > 0)
7502 {
7503 /* Likewise: advance to the next cluster, but going in
7504 the reverse direction. */
7505 it->cmp_it.to = it->cmp_it.from;
7506 }
7507 else
7508 {
7509 /* This composition was fully processed; find the next
7510 candidate place for checking for composed
7511 characters. */
7512 /* Always limit string searches to the string length;
7513 any padding spaces are not part of the string, and
7514 there cannot be any compositions in that padding. */
7515 ptrdiff_t stop = SCHARS (it->string);
7516
7517 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7518 stop = -1;
7519 else if (it->end_charpos < stop)
7520 {
7521 /* Cf. PRECISION in reseat_to_string: we might be
7522 limited in how many of the string characters we
7523 need to deliver. */
7524 stop = it->end_charpos;
7525 }
7526 composition_compute_stop_pos (&it->cmp_it,
7527 IT_STRING_CHARPOS (*it),
7528 IT_STRING_BYTEPOS (*it), stop,
7529 it->string);
7530 }
7531 }
7532 else
7533 {
7534 if (!it->bidi_p
7535 /* If the string position is beyond string's end, it
7536 means next_element_from_string is padding the string
7537 with blanks, in which case we bypass the bidi
7538 iterator, because it cannot deal with such virtual
7539 characters. */
7540 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7541 {
7542 IT_STRING_BYTEPOS (*it) += it->len;
7543 IT_STRING_CHARPOS (*it) += 1;
7544 }
7545 else
7546 {
7547 int prev_scan_dir = it->bidi_it.scan_dir;
7548
7549 bidi_move_to_visually_next (&it->bidi_it);
7550 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7551 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7552 /* If the scan direction changes, we may need to update
7553 the place where to check for composed characters. */
7554 if (prev_scan_dir != it->bidi_it.scan_dir)
7555 {
7556 ptrdiff_t stop = SCHARS (it->string);
7557
7558 if (it->bidi_it.scan_dir < 0)
7559 stop = -1;
7560 else if (it->end_charpos < stop)
7561 stop = it->end_charpos;
7562
7563 composition_compute_stop_pos (&it->cmp_it,
7564 IT_STRING_CHARPOS (*it),
7565 IT_STRING_BYTEPOS (*it), stop,
7566 it->string);
7567 }
7568 }
7569 }
7570
7571 consider_string_end:
7572
7573 if (it->current.overlay_string_index >= 0)
7574 {
7575 /* IT->string is an overlay string. Advance to the
7576 next, if there is one. */
7577 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7578 {
7579 it->ellipsis_p = false;
7580 next_overlay_string (it);
7581 if (it->ellipsis_p)
7582 setup_for_ellipsis (it, 0);
7583 }
7584 }
7585 else
7586 {
7587 /* IT->string is not an overlay string. If we reached
7588 its end, and there is something on IT->stack, proceed
7589 with what is on the stack. This can be either another
7590 string, this time an overlay string, or a buffer. */
7591 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7592 && it->sp > 0)
7593 {
7594 pop_it (it);
7595 if (it->method == GET_FROM_STRING)
7596 goto consider_string_end;
7597 }
7598 }
7599 break;
7600
7601 case GET_FROM_IMAGE:
7602 case GET_FROM_STRETCH:
7603 /* The position etc with which we have to proceed are on
7604 the stack. The position may be at the end of a string,
7605 if the `display' property takes up the whole string. */
7606 eassert (it->sp > 0);
7607 pop_it (it);
7608 if (it->method == GET_FROM_STRING)
7609 goto consider_string_end;
7610 break;
7611
7612 default:
7613 /* There are no other methods defined, so this should be a bug. */
7614 emacs_abort ();
7615 }
7616
7617 eassert (it->method != GET_FROM_STRING
7618 || (STRINGP (it->string)
7619 && IT_STRING_CHARPOS (*it) >= 0));
7620 }
7621
7622 /* Load IT's display element fields with information about the next
7623 display element which comes from a display table entry or from the
7624 result of translating a control character to one of the forms `^C'
7625 or `\003'.
7626
7627 IT->dpvec holds the glyphs to return as characters.
7628 IT->saved_face_id holds the face id before the display vector--it
7629 is restored into IT->face_id in set_iterator_to_next. */
7630
7631 static bool
7632 next_element_from_display_vector (struct it *it)
7633 {
7634 Lisp_Object gc;
7635 int prev_face_id = it->face_id;
7636 int next_face_id;
7637
7638 /* Precondition. */
7639 eassert (it->dpvec && it->current.dpvec_index >= 0);
7640
7641 it->face_id = it->saved_face_id;
7642
7643 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7644 That seemed totally bogus - so I changed it... */
7645 gc = it->dpvec[it->current.dpvec_index];
7646
7647 if (GLYPH_CODE_P (gc))
7648 {
7649 struct face *this_face, *prev_face, *next_face;
7650
7651 it->c = GLYPH_CODE_CHAR (gc);
7652 it->len = CHAR_BYTES (it->c);
7653
7654 /* The entry may contain a face id to use. Such a face id is
7655 the id of a Lisp face, not a realized face. A face id of
7656 zero means no face is specified. */
7657 if (it->dpvec_face_id >= 0)
7658 it->face_id = it->dpvec_face_id;
7659 else
7660 {
7661 int lface_id = GLYPH_CODE_FACE (gc);
7662 if (lface_id > 0)
7663 it->face_id = merge_faces (it->f, Qt, lface_id,
7664 it->saved_face_id);
7665 }
7666
7667 /* Glyphs in the display vector could have the box face, so we
7668 need to set the related flags in the iterator, as
7669 appropriate. */
7670 this_face = FACE_FROM_ID (it->f, it->face_id);
7671 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7672
7673 /* Is this character the first character of a box-face run? */
7674 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7675 && (!prev_face
7676 || prev_face->box == FACE_NO_BOX));
7677
7678 /* For the last character of the box-face run, we need to look
7679 either at the next glyph from the display vector, or at the
7680 face we saw before the display vector. */
7681 next_face_id = it->saved_face_id;
7682 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7683 {
7684 if (it->dpvec_face_id >= 0)
7685 next_face_id = it->dpvec_face_id;
7686 else
7687 {
7688 int lface_id =
7689 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7690
7691 if (lface_id > 0)
7692 next_face_id = merge_faces (it->f, Qt, lface_id,
7693 it->saved_face_id);
7694 }
7695 }
7696 next_face = FACE_FROM_ID (it->f, next_face_id);
7697 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7698 && (!next_face
7699 || next_face->box == FACE_NO_BOX));
7700 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7701 }
7702 else
7703 /* Display table entry is invalid. Return a space. */
7704 it->c = ' ', it->len = 1;
7705
7706 /* Don't change position and object of the iterator here. They are
7707 still the values of the character that had this display table
7708 entry or was translated, and that's what we want. */
7709 it->what = IT_CHARACTER;
7710 return true;
7711 }
7712
7713 /* Get the first element of string/buffer in the visual order, after
7714 being reseated to a new position in a string or a buffer. */
7715 static void
7716 get_visually_first_element (struct it *it)
7717 {
7718 bool string_p = STRINGP (it->string) || it->s;
7719 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7720 ptrdiff_t bob = (string_p ? 0 : BEGV);
7721
7722 if (STRINGP (it->string))
7723 {
7724 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7725 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7726 }
7727 else
7728 {
7729 it->bidi_it.charpos = IT_CHARPOS (*it);
7730 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7731 }
7732
7733 if (it->bidi_it.charpos == eob)
7734 {
7735 /* Nothing to do, but reset the FIRST_ELT flag, like
7736 bidi_paragraph_init does, because we are not going to
7737 call it. */
7738 it->bidi_it.first_elt = false;
7739 }
7740 else if (it->bidi_it.charpos == bob
7741 || (!string_p
7742 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7743 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7744 {
7745 /* If we are at the beginning of a line/string, we can produce
7746 the next element right away. */
7747 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7748 bidi_move_to_visually_next (&it->bidi_it);
7749 }
7750 else
7751 {
7752 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7753
7754 /* We need to prime the bidi iterator starting at the line's or
7755 string's beginning, before we will be able to produce the
7756 next element. */
7757 if (string_p)
7758 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7759 else
7760 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7761 IT_BYTEPOS (*it), -1,
7762 &it->bidi_it.bytepos);
7763 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7764 do
7765 {
7766 /* Now return to buffer/string position where we were asked
7767 to get the next display element, and produce that. */
7768 bidi_move_to_visually_next (&it->bidi_it);
7769 }
7770 while (it->bidi_it.bytepos != orig_bytepos
7771 && it->bidi_it.charpos < eob);
7772 }
7773
7774 /* Adjust IT's position information to where we ended up. */
7775 if (STRINGP (it->string))
7776 {
7777 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7778 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7779 }
7780 else
7781 {
7782 IT_CHARPOS (*it) = it->bidi_it.charpos;
7783 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7784 }
7785
7786 if (STRINGP (it->string) || !it->s)
7787 {
7788 ptrdiff_t stop, charpos, bytepos;
7789
7790 if (STRINGP (it->string))
7791 {
7792 eassert (!it->s);
7793 stop = SCHARS (it->string);
7794 if (stop > it->end_charpos)
7795 stop = it->end_charpos;
7796 charpos = IT_STRING_CHARPOS (*it);
7797 bytepos = IT_STRING_BYTEPOS (*it);
7798 }
7799 else
7800 {
7801 stop = it->end_charpos;
7802 charpos = IT_CHARPOS (*it);
7803 bytepos = IT_BYTEPOS (*it);
7804 }
7805 if (it->bidi_it.scan_dir < 0)
7806 stop = -1;
7807 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7808 it->string);
7809 }
7810 }
7811
7812 /* Load IT with the next display element from Lisp string IT->string.
7813 IT->current.string_pos is the current position within the string.
7814 If IT->current.overlay_string_index >= 0, the Lisp string is an
7815 overlay string. */
7816
7817 static bool
7818 next_element_from_string (struct it *it)
7819 {
7820 struct text_pos position;
7821
7822 eassert (STRINGP (it->string));
7823 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7824 eassert (IT_STRING_CHARPOS (*it) >= 0);
7825 position = it->current.string_pos;
7826
7827 /* With bidi reordering, the character to display might not be the
7828 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7829 that we were reseat()ed to a new string, whose paragraph
7830 direction is not known. */
7831 if (it->bidi_p && it->bidi_it.first_elt)
7832 {
7833 get_visually_first_element (it);
7834 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7835 }
7836
7837 /* Time to check for invisible text? */
7838 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7839 {
7840 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7841 {
7842 if (!(!it->bidi_p
7843 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7844 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7845 {
7846 /* With bidi non-linear iteration, we could find
7847 ourselves far beyond the last computed stop_charpos,
7848 with several other stop positions in between that we
7849 missed. Scan them all now, in buffer's logical
7850 order, until we find and handle the last stop_charpos
7851 that precedes our current position. */
7852 handle_stop_backwards (it, it->stop_charpos);
7853 return GET_NEXT_DISPLAY_ELEMENT (it);
7854 }
7855 else
7856 {
7857 if (it->bidi_p)
7858 {
7859 /* Take note of the stop position we just moved
7860 across, for when we will move back across it. */
7861 it->prev_stop = it->stop_charpos;
7862 /* If we are at base paragraph embedding level, take
7863 note of the last stop position seen at this
7864 level. */
7865 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7866 it->base_level_stop = it->stop_charpos;
7867 }
7868 handle_stop (it);
7869
7870 /* Since a handler may have changed IT->method, we must
7871 recurse here. */
7872 return GET_NEXT_DISPLAY_ELEMENT (it);
7873 }
7874 }
7875 else if (it->bidi_p
7876 /* If we are before prev_stop, we may have overstepped
7877 on our way backwards a stop_pos, and if so, we need
7878 to handle that stop_pos. */
7879 && IT_STRING_CHARPOS (*it) < it->prev_stop
7880 /* We can sometimes back up for reasons that have nothing
7881 to do with bidi reordering. E.g., compositions. The
7882 code below is only needed when we are above the base
7883 embedding level, so test for that explicitly. */
7884 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7885 {
7886 /* If we lost track of base_level_stop, we have no better
7887 place for handle_stop_backwards to start from than string
7888 beginning. This happens, e.g., when we were reseated to
7889 the previous screenful of text by vertical-motion. */
7890 if (it->base_level_stop <= 0
7891 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7892 it->base_level_stop = 0;
7893 handle_stop_backwards (it, it->base_level_stop);
7894 return GET_NEXT_DISPLAY_ELEMENT (it);
7895 }
7896 }
7897
7898 if (it->current.overlay_string_index >= 0)
7899 {
7900 /* Get the next character from an overlay string. In overlay
7901 strings, there is no field width or padding with spaces to
7902 do. */
7903 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7904 {
7905 it->what = IT_EOB;
7906 return false;
7907 }
7908 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7909 IT_STRING_BYTEPOS (*it),
7910 it->bidi_it.scan_dir < 0
7911 ? -1
7912 : SCHARS (it->string))
7913 && next_element_from_composition (it))
7914 {
7915 return true;
7916 }
7917 else if (STRING_MULTIBYTE (it->string))
7918 {
7919 const unsigned char *s = (SDATA (it->string)
7920 + IT_STRING_BYTEPOS (*it));
7921 it->c = string_char_and_length (s, &it->len);
7922 }
7923 else
7924 {
7925 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7926 it->len = 1;
7927 }
7928 }
7929 else
7930 {
7931 /* Get the next character from a Lisp string that is not an
7932 overlay string. Such strings come from the mode line, for
7933 example. We may have to pad with spaces, or truncate the
7934 string. See also next_element_from_c_string. */
7935 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7936 {
7937 it->what = IT_EOB;
7938 return false;
7939 }
7940 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7941 {
7942 /* Pad with spaces. */
7943 it->c = ' ', it->len = 1;
7944 CHARPOS (position) = BYTEPOS (position) = -1;
7945 }
7946 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7947 IT_STRING_BYTEPOS (*it),
7948 it->bidi_it.scan_dir < 0
7949 ? -1
7950 : it->string_nchars)
7951 && next_element_from_composition (it))
7952 {
7953 return true;
7954 }
7955 else if (STRING_MULTIBYTE (it->string))
7956 {
7957 const unsigned char *s = (SDATA (it->string)
7958 + IT_STRING_BYTEPOS (*it));
7959 it->c = string_char_and_length (s, &it->len);
7960 }
7961 else
7962 {
7963 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7964 it->len = 1;
7965 }
7966 }
7967
7968 /* Record what we have and where it came from. */
7969 it->what = IT_CHARACTER;
7970 it->object = it->string;
7971 it->position = position;
7972 return true;
7973 }
7974
7975
7976 /* Load IT with next display element from C string IT->s.
7977 IT->string_nchars is the maximum number of characters to return
7978 from the string. IT->end_charpos may be greater than
7979 IT->string_nchars when this function is called, in which case we
7980 may have to return padding spaces. Value is false if end of string
7981 reached, including padding spaces. */
7982
7983 static bool
7984 next_element_from_c_string (struct it *it)
7985 {
7986 bool success_p = true;
7987
7988 eassert (it->s);
7989 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7990 it->what = IT_CHARACTER;
7991 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7992 it->object = make_number (0);
7993
7994 /* With bidi reordering, the character to display might not be the
7995 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
7996 we were reseated to a new string, whose paragraph direction is
7997 not known. */
7998 if (it->bidi_p && it->bidi_it.first_elt)
7999 get_visually_first_element (it);
8000
8001 /* IT's position can be greater than IT->string_nchars in case a
8002 field width or precision has been specified when the iterator was
8003 initialized. */
8004 if (IT_CHARPOS (*it) >= it->end_charpos)
8005 {
8006 /* End of the game. */
8007 it->what = IT_EOB;
8008 success_p = false;
8009 }
8010 else if (IT_CHARPOS (*it) >= it->string_nchars)
8011 {
8012 /* Pad with spaces. */
8013 it->c = ' ', it->len = 1;
8014 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8015 }
8016 else if (it->multibyte_p)
8017 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8018 else
8019 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8020
8021 return success_p;
8022 }
8023
8024
8025 /* Set up IT to return characters from an ellipsis, if appropriate.
8026 The definition of the ellipsis glyphs may come from a display table
8027 entry. This function fills IT with the first glyph from the
8028 ellipsis if an ellipsis is to be displayed. */
8029
8030 static bool
8031 next_element_from_ellipsis (struct it *it)
8032 {
8033 if (it->selective_display_ellipsis_p)
8034 setup_for_ellipsis (it, it->len);
8035 else
8036 {
8037 /* The face at the current position may be different from the
8038 face we find after the invisible text. Remember what it
8039 was in IT->saved_face_id, and signal that it's there by
8040 setting face_before_selective_p. */
8041 it->saved_face_id = it->face_id;
8042 it->method = GET_FROM_BUFFER;
8043 it->object = it->w->contents;
8044 reseat_at_next_visible_line_start (it, true);
8045 it->face_before_selective_p = true;
8046 }
8047
8048 return GET_NEXT_DISPLAY_ELEMENT (it);
8049 }
8050
8051
8052 /* Deliver an image display element. The iterator IT is already
8053 filled with image information (done in handle_display_prop). Value
8054 is always true. */
8055
8056
8057 static bool
8058 next_element_from_image (struct it *it)
8059 {
8060 it->what = IT_IMAGE;
8061 return true;
8062 }
8063
8064
8065 /* Fill iterator IT with next display element from a stretch glyph
8066 property. IT->object is the value of the text property. Value is
8067 always true. */
8068
8069 static bool
8070 next_element_from_stretch (struct it *it)
8071 {
8072 it->what = IT_STRETCH;
8073 return true;
8074 }
8075
8076 /* Scan backwards from IT's current position until we find a stop
8077 position, or until BEGV. This is called when we find ourself
8078 before both the last known prev_stop and base_level_stop while
8079 reordering bidirectional text. */
8080
8081 static void
8082 compute_stop_pos_backwards (struct it *it)
8083 {
8084 const int SCAN_BACK_LIMIT = 1000;
8085 struct text_pos pos;
8086 struct display_pos save_current = it->current;
8087 struct text_pos save_position = it->position;
8088 ptrdiff_t charpos = IT_CHARPOS (*it);
8089 ptrdiff_t where_we_are = charpos;
8090 ptrdiff_t save_stop_pos = it->stop_charpos;
8091 ptrdiff_t save_end_pos = it->end_charpos;
8092
8093 eassert (NILP (it->string) && !it->s);
8094 eassert (it->bidi_p);
8095 it->bidi_p = false;
8096 do
8097 {
8098 it->end_charpos = min (charpos + 1, ZV);
8099 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8100 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8101 reseat_1 (it, pos, false);
8102 compute_stop_pos (it);
8103 /* We must advance forward, right? */
8104 if (it->stop_charpos <= charpos)
8105 emacs_abort ();
8106 }
8107 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8108
8109 if (it->stop_charpos <= where_we_are)
8110 it->prev_stop = it->stop_charpos;
8111 else
8112 it->prev_stop = BEGV;
8113 it->bidi_p = true;
8114 it->current = save_current;
8115 it->position = save_position;
8116 it->stop_charpos = save_stop_pos;
8117 it->end_charpos = save_end_pos;
8118 }
8119
8120 /* Scan forward from CHARPOS in the current buffer/string, until we
8121 find a stop position > current IT's position. Then handle the stop
8122 position before that. This is called when we bump into a stop
8123 position while reordering bidirectional text. CHARPOS should be
8124 the last previously processed stop_pos (or BEGV/0, if none were
8125 processed yet) whose position is less that IT's current
8126 position. */
8127
8128 static void
8129 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8130 {
8131 bool bufp = !STRINGP (it->string);
8132 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8133 struct display_pos save_current = it->current;
8134 struct text_pos save_position = it->position;
8135 struct text_pos pos1;
8136 ptrdiff_t next_stop;
8137
8138 /* Scan in strict logical order. */
8139 eassert (it->bidi_p);
8140 it->bidi_p = false;
8141 do
8142 {
8143 it->prev_stop = charpos;
8144 if (bufp)
8145 {
8146 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8147 reseat_1 (it, pos1, false);
8148 }
8149 else
8150 it->current.string_pos = string_pos (charpos, it->string);
8151 compute_stop_pos (it);
8152 /* We must advance forward, right? */
8153 if (it->stop_charpos <= it->prev_stop)
8154 emacs_abort ();
8155 charpos = it->stop_charpos;
8156 }
8157 while (charpos <= where_we_are);
8158
8159 it->bidi_p = true;
8160 it->current = save_current;
8161 it->position = save_position;
8162 next_stop = it->stop_charpos;
8163 it->stop_charpos = it->prev_stop;
8164 handle_stop (it);
8165 it->stop_charpos = next_stop;
8166 }
8167
8168 /* Load IT with the next display element from current_buffer. Value
8169 is false if end of buffer reached. IT->stop_charpos is the next
8170 position at which to stop and check for text properties or buffer
8171 end. */
8172
8173 static bool
8174 next_element_from_buffer (struct it *it)
8175 {
8176 bool success_p = true;
8177
8178 eassert (IT_CHARPOS (*it) >= BEGV);
8179 eassert (NILP (it->string) && !it->s);
8180 eassert (!it->bidi_p
8181 || (EQ (it->bidi_it.string.lstring, Qnil)
8182 && it->bidi_it.string.s == NULL));
8183
8184 /* With bidi reordering, the character to display might not be the
8185 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8186 we were reseat()ed to a new buffer position, which is potentially
8187 a different paragraph. */
8188 if (it->bidi_p && it->bidi_it.first_elt)
8189 {
8190 get_visually_first_element (it);
8191 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8192 }
8193
8194 if (IT_CHARPOS (*it) >= it->stop_charpos)
8195 {
8196 if (IT_CHARPOS (*it) >= it->end_charpos)
8197 {
8198 bool overlay_strings_follow_p;
8199
8200 /* End of the game, except when overlay strings follow that
8201 haven't been returned yet. */
8202 if (it->overlay_strings_at_end_processed_p)
8203 overlay_strings_follow_p = false;
8204 else
8205 {
8206 it->overlay_strings_at_end_processed_p = true;
8207 overlay_strings_follow_p = get_overlay_strings (it, 0);
8208 }
8209
8210 if (overlay_strings_follow_p)
8211 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8212 else
8213 {
8214 it->what = IT_EOB;
8215 it->position = it->current.pos;
8216 success_p = false;
8217 }
8218 }
8219 else if (!(!it->bidi_p
8220 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8221 || IT_CHARPOS (*it) == it->stop_charpos))
8222 {
8223 /* With bidi non-linear iteration, we could find ourselves
8224 far beyond the last computed stop_charpos, with several
8225 other stop positions in between that we missed. Scan
8226 them all now, in buffer's logical order, until we find
8227 and handle the last stop_charpos that precedes our
8228 current position. */
8229 handle_stop_backwards (it, it->stop_charpos);
8230 it->ignore_overlay_strings_at_pos_p = false;
8231 return GET_NEXT_DISPLAY_ELEMENT (it);
8232 }
8233 else
8234 {
8235 if (it->bidi_p)
8236 {
8237 /* Take note of the stop position we just moved across,
8238 for when we will move back across it. */
8239 it->prev_stop = it->stop_charpos;
8240 /* If we are at base paragraph embedding level, take
8241 note of the last stop position seen at this
8242 level. */
8243 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8244 it->base_level_stop = it->stop_charpos;
8245 }
8246 handle_stop (it);
8247 it->ignore_overlay_strings_at_pos_p = false;
8248 return GET_NEXT_DISPLAY_ELEMENT (it);
8249 }
8250 }
8251 else if (it->bidi_p
8252 /* If we are before prev_stop, we may have overstepped on
8253 our way backwards a stop_pos, and if so, we need to
8254 handle that stop_pos. */
8255 && IT_CHARPOS (*it) < it->prev_stop
8256 /* We can sometimes back up for reasons that have nothing
8257 to do with bidi reordering. E.g., compositions. The
8258 code below is only needed when we are above the base
8259 embedding level, so test for that explicitly. */
8260 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8261 {
8262 if (it->base_level_stop <= 0
8263 || IT_CHARPOS (*it) < it->base_level_stop)
8264 {
8265 /* If we lost track of base_level_stop, we need to find
8266 prev_stop by looking backwards. This happens, e.g., when
8267 we were reseated to the previous screenful of text by
8268 vertical-motion. */
8269 it->base_level_stop = BEGV;
8270 compute_stop_pos_backwards (it);
8271 handle_stop_backwards (it, it->prev_stop);
8272 }
8273 else
8274 handle_stop_backwards (it, it->base_level_stop);
8275 it->ignore_overlay_strings_at_pos_p = false;
8276 return GET_NEXT_DISPLAY_ELEMENT (it);
8277 }
8278 else
8279 {
8280 /* No face changes, overlays etc. in sight, so just return a
8281 character from current_buffer. */
8282 unsigned char *p;
8283 ptrdiff_t stop;
8284
8285 /* We moved to the next buffer position, so any info about
8286 previously seen overlays is no longer valid. */
8287 it->ignore_overlay_strings_at_pos_p = false;
8288
8289 /* Maybe run the redisplay end trigger hook. Performance note:
8290 This doesn't seem to cost measurable time. */
8291 if (it->redisplay_end_trigger_charpos
8292 && it->glyph_row
8293 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8294 run_redisplay_end_trigger_hook (it);
8295
8296 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8297 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8298 stop)
8299 && next_element_from_composition (it))
8300 {
8301 return true;
8302 }
8303
8304 /* Get the next character, maybe multibyte. */
8305 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8306 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8307 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8308 else
8309 it->c = *p, it->len = 1;
8310
8311 /* Record what we have and where it came from. */
8312 it->what = IT_CHARACTER;
8313 it->object = it->w->contents;
8314 it->position = it->current.pos;
8315
8316 /* Normally we return the character found above, except when we
8317 really want to return an ellipsis for selective display. */
8318 if (it->selective)
8319 {
8320 if (it->c == '\n')
8321 {
8322 /* A value of selective > 0 means hide lines indented more
8323 than that number of columns. */
8324 if (it->selective > 0
8325 && IT_CHARPOS (*it) + 1 < ZV
8326 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8327 IT_BYTEPOS (*it) + 1,
8328 it->selective))
8329 {
8330 success_p = next_element_from_ellipsis (it);
8331 it->dpvec_char_len = -1;
8332 }
8333 }
8334 else if (it->c == '\r' && it->selective == -1)
8335 {
8336 /* A value of selective == -1 means that everything from the
8337 CR to the end of the line is invisible, with maybe an
8338 ellipsis displayed for it. */
8339 success_p = next_element_from_ellipsis (it);
8340 it->dpvec_char_len = -1;
8341 }
8342 }
8343 }
8344
8345 /* Value is false if end of buffer reached. */
8346 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8347 return success_p;
8348 }
8349
8350
8351 /* Run the redisplay end trigger hook for IT. */
8352
8353 static void
8354 run_redisplay_end_trigger_hook (struct it *it)
8355 {
8356 /* IT->glyph_row should be non-null, i.e. we should be actually
8357 displaying something, or otherwise we should not run the hook. */
8358 eassert (it->glyph_row);
8359
8360 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8361 it->redisplay_end_trigger_charpos = 0;
8362
8363 /* Since we are *trying* to run these functions, don't try to run
8364 them again, even if they get an error. */
8365 wset_redisplay_end_trigger (it->w, Qnil);
8366 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8367 make_number (charpos));
8368
8369 /* Notice if it changed the face of the character we are on. */
8370 handle_face_prop (it);
8371 }
8372
8373
8374 /* Deliver a composition display element. Unlike the other
8375 next_element_from_XXX, this function is not registered in the array
8376 get_next_element[]. It is called from next_element_from_buffer and
8377 next_element_from_string when necessary. */
8378
8379 static bool
8380 next_element_from_composition (struct it *it)
8381 {
8382 it->what = IT_COMPOSITION;
8383 it->len = it->cmp_it.nbytes;
8384 if (STRINGP (it->string))
8385 {
8386 if (it->c < 0)
8387 {
8388 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8389 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8390 return false;
8391 }
8392 it->position = it->current.string_pos;
8393 it->object = it->string;
8394 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8395 IT_STRING_BYTEPOS (*it), it->string);
8396 }
8397 else
8398 {
8399 if (it->c < 0)
8400 {
8401 IT_CHARPOS (*it) += it->cmp_it.nchars;
8402 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8403 if (it->bidi_p)
8404 {
8405 if (it->bidi_it.new_paragraph)
8406 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8407 false);
8408 /* Resync the bidi iterator with IT's new position.
8409 FIXME: this doesn't support bidirectional text. */
8410 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8411 bidi_move_to_visually_next (&it->bidi_it);
8412 }
8413 return false;
8414 }
8415 it->position = it->current.pos;
8416 it->object = it->w->contents;
8417 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8418 IT_BYTEPOS (*it), Qnil);
8419 }
8420 return true;
8421 }
8422
8423
8424 \f
8425 /***********************************************************************
8426 Moving an iterator without producing glyphs
8427 ***********************************************************************/
8428
8429 /* Check if iterator is at a position corresponding to a valid buffer
8430 position after some move_it_ call. */
8431
8432 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8433 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8434
8435
8436 /* Move iterator IT to a specified buffer or X position within one
8437 line on the display without producing glyphs.
8438
8439 OP should be a bit mask including some or all of these bits:
8440 MOVE_TO_X: Stop upon reaching x-position TO_X.
8441 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8442 Regardless of OP's value, stop upon reaching the end of the display line.
8443
8444 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8445 This means, in particular, that TO_X includes window's horizontal
8446 scroll amount.
8447
8448 The return value has several possible values that
8449 say what condition caused the scan to stop:
8450
8451 MOVE_POS_MATCH_OR_ZV
8452 - when TO_POS or ZV was reached.
8453
8454 MOVE_X_REACHED
8455 -when TO_X was reached before TO_POS or ZV were reached.
8456
8457 MOVE_LINE_CONTINUED
8458 - when we reached the end of the display area and the line must
8459 be continued.
8460
8461 MOVE_LINE_TRUNCATED
8462 - when we reached the end of the display area and the line is
8463 truncated.
8464
8465 MOVE_NEWLINE_OR_CR
8466 - when we stopped at a line end, i.e. a newline or a CR and selective
8467 display is on. */
8468
8469 static enum move_it_result
8470 move_it_in_display_line_to (struct it *it,
8471 ptrdiff_t to_charpos, int to_x,
8472 enum move_operation_enum op)
8473 {
8474 enum move_it_result result = MOVE_UNDEFINED;
8475 struct glyph_row *saved_glyph_row;
8476 struct it wrap_it, atpos_it, atx_it, ppos_it;
8477 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8478 void *ppos_data = NULL;
8479 bool may_wrap = false;
8480 enum it_method prev_method = it->method;
8481 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8482 bool saw_smaller_pos = prev_pos < to_charpos;
8483
8484 /* Don't produce glyphs in produce_glyphs. */
8485 saved_glyph_row = it->glyph_row;
8486 it->glyph_row = NULL;
8487
8488 /* Use wrap_it to save a copy of IT wherever a word wrap could
8489 occur. Use atpos_it to save a copy of IT at the desired buffer
8490 position, if found, so that we can scan ahead and check if the
8491 word later overshoots the window edge. Use atx_it similarly, for
8492 pixel positions. */
8493 wrap_it.sp = -1;
8494 atpos_it.sp = -1;
8495 atx_it.sp = -1;
8496
8497 /* Use ppos_it under bidi reordering to save a copy of IT for the
8498 initial position. We restore that position in IT when we have
8499 scanned the entire display line without finding a match for
8500 TO_CHARPOS and all the character positions are greater than
8501 TO_CHARPOS. We then restart the scan from the initial position,
8502 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8503 the closest to TO_CHARPOS. */
8504 if (it->bidi_p)
8505 {
8506 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8507 {
8508 SAVE_IT (ppos_it, *it, ppos_data);
8509 closest_pos = IT_CHARPOS (*it);
8510 }
8511 else
8512 closest_pos = ZV;
8513 }
8514
8515 #define BUFFER_POS_REACHED_P() \
8516 ((op & MOVE_TO_POS) != 0 \
8517 && BUFFERP (it->object) \
8518 && (IT_CHARPOS (*it) == to_charpos \
8519 || ((!it->bidi_p \
8520 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8521 && IT_CHARPOS (*it) > to_charpos) \
8522 || (it->what == IT_COMPOSITION \
8523 && ((IT_CHARPOS (*it) > to_charpos \
8524 && to_charpos >= it->cmp_it.charpos) \
8525 || (IT_CHARPOS (*it) < to_charpos \
8526 && to_charpos <= it->cmp_it.charpos)))) \
8527 && (it->method == GET_FROM_BUFFER \
8528 || (it->method == GET_FROM_DISPLAY_VECTOR \
8529 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8530
8531 /* If there's a line-/wrap-prefix, handle it. */
8532 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8533 && it->current_y < it->last_visible_y)
8534 handle_line_prefix (it);
8535
8536 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8537 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8538
8539 while (true)
8540 {
8541 int x, i, ascent = 0, descent = 0;
8542
8543 /* Utility macro to reset an iterator with x, ascent, and descent. */
8544 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8545 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8546 (IT)->max_descent = descent)
8547
8548 /* Stop if we move beyond TO_CHARPOS (after an image or a
8549 display string or stretch glyph). */
8550 if ((op & MOVE_TO_POS) != 0
8551 && BUFFERP (it->object)
8552 && it->method == GET_FROM_BUFFER
8553 && (((!it->bidi_p
8554 /* When the iterator is at base embedding level, we
8555 are guaranteed that characters are delivered for
8556 display in strictly increasing order of their
8557 buffer positions. */
8558 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8559 && IT_CHARPOS (*it) > to_charpos)
8560 || (it->bidi_p
8561 && (prev_method == GET_FROM_IMAGE
8562 || prev_method == GET_FROM_STRETCH
8563 || prev_method == GET_FROM_STRING)
8564 /* Passed TO_CHARPOS from left to right. */
8565 && ((prev_pos < to_charpos
8566 && IT_CHARPOS (*it) > to_charpos)
8567 /* Passed TO_CHARPOS from right to left. */
8568 || (prev_pos > to_charpos
8569 && IT_CHARPOS (*it) < to_charpos)))))
8570 {
8571 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8572 {
8573 result = MOVE_POS_MATCH_OR_ZV;
8574 break;
8575 }
8576 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8577 /* If wrap_it is valid, the current position might be in a
8578 word that is wrapped. So, save the iterator in
8579 atpos_it and continue to see if wrapping happens. */
8580 SAVE_IT (atpos_it, *it, atpos_data);
8581 }
8582
8583 /* Stop when ZV reached.
8584 We used to stop here when TO_CHARPOS reached as well, but that is
8585 too soon if this glyph does not fit on this line. So we handle it
8586 explicitly below. */
8587 if (!get_next_display_element (it))
8588 {
8589 result = MOVE_POS_MATCH_OR_ZV;
8590 break;
8591 }
8592
8593 if (it->line_wrap == TRUNCATE)
8594 {
8595 if (BUFFER_POS_REACHED_P ())
8596 {
8597 result = MOVE_POS_MATCH_OR_ZV;
8598 break;
8599 }
8600 }
8601 else
8602 {
8603 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8604 {
8605 if (IT_DISPLAYING_WHITESPACE (it))
8606 may_wrap = true;
8607 else if (may_wrap)
8608 {
8609 /* We have reached a glyph that follows one or more
8610 whitespace characters. If the position is
8611 already found, we are done. */
8612 if (atpos_it.sp >= 0)
8613 {
8614 RESTORE_IT (it, &atpos_it, atpos_data);
8615 result = MOVE_POS_MATCH_OR_ZV;
8616 goto done;
8617 }
8618 if (atx_it.sp >= 0)
8619 {
8620 RESTORE_IT (it, &atx_it, atx_data);
8621 result = MOVE_X_REACHED;
8622 goto done;
8623 }
8624 /* Otherwise, we can wrap here. */
8625 SAVE_IT (wrap_it, *it, wrap_data);
8626 may_wrap = false;
8627 }
8628 }
8629 }
8630
8631 /* Remember the line height for the current line, in case
8632 the next element doesn't fit on the line. */
8633 ascent = it->max_ascent;
8634 descent = it->max_descent;
8635
8636 /* The call to produce_glyphs will get the metrics of the
8637 display element IT is loaded with. Record the x-position
8638 before this display element, in case it doesn't fit on the
8639 line. */
8640 x = it->current_x;
8641
8642 PRODUCE_GLYPHS (it);
8643
8644 if (it->area != TEXT_AREA)
8645 {
8646 prev_method = it->method;
8647 if (it->method == GET_FROM_BUFFER)
8648 prev_pos = IT_CHARPOS (*it);
8649 set_iterator_to_next (it, true);
8650 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8651 SET_TEXT_POS (this_line_min_pos,
8652 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8653 if (it->bidi_p
8654 && (op & MOVE_TO_POS)
8655 && IT_CHARPOS (*it) > to_charpos
8656 && IT_CHARPOS (*it) < closest_pos)
8657 closest_pos = IT_CHARPOS (*it);
8658 continue;
8659 }
8660
8661 /* The number of glyphs we get back in IT->nglyphs will normally
8662 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8663 character on a terminal frame, or (iii) a line end. For the
8664 second case, IT->nglyphs - 1 padding glyphs will be present.
8665 (On X frames, there is only one glyph produced for a
8666 composite character.)
8667
8668 The behavior implemented below means, for continuation lines,
8669 that as many spaces of a TAB as fit on the current line are
8670 displayed there. For terminal frames, as many glyphs of a
8671 multi-glyph character are displayed in the current line, too.
8672 This is what the old redisplay code did, and we keep it that
8673 way. Under X, the whole shape of a complex character must
8674 fit on the line or it will be completely displayed in the
8675 next line.
8676
8677 Note that both for tabs and padding glyphs, all glyphs have
8678 the same width. */
8679 if (it->nglyphs)
8680 {
8681 /* More than one glyph or glyph doesn't fit on line. All
8682 glyphs have the same width. */
8683 int single_glyph_width = it->pixel_width / it->nglyphs;
8684 int new_x;
8685 int x_before_this_char = x;
8686 int hpos_before_this_char = it->hpos;
8687
8688 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8689 {
8690 new_x = x + single_glyph_width;
8691
8692 /* We want to leave anything reaching TO_X to the caller. */
8693 if ((op & MOVE_TO_X) && new_x > to_x)
8694 {
8695 if (BUFFER_POS_REACHED_P ())
8696 {
8697 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8698 goto buffer_pos_reached;
8699 if (atpos_it.sp < 0)
8700 {
8701 SAVE_IT (atpos_it, *it, atpos_data);
8702 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8703 }
8704 }
8705 else
8706 {
8707 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8708 {
8709 it->current_x = x;
8710 result = MOVE_X_REACHED;
8711 break;
8712 }
8713 if (atx_it.sp < 0)
8714 {
8715 SAVE_IT (atx_it, *it, atx_data);
8716 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8717 }
8718 }
8719 }
8720
8721 if (/* Lines are continued. */
8722 it->line_wrap != TRUNCATE
8723 && (/* And glyph doesn't fit on the line. */
8724 new_x > it->last_visible_x
8725 /* Or it fits exactly and we're on a window
8726 system frame. */
8727 || (new_x == it->last_visible_x
8728 && FRAME_WINDOW_P (it->f)
8729 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8730 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8731 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8732 {
8733 if (/* IT->hpos == 0 means the very first glyph
8734 doesn't fit on the line, e.g. a wide image. */
8735 it->hpos == 0
8736 || (new_x == it->last_visible_x
8737 && FRAME_WINDOW_P (it->f)))
8738 {
8739 ++it->hpos;
8740 it->current_x = new_x;
8741
8742 /* The character's last glyph just barely fits
8743 in this row. */
8744 if (i == it->nglyphs - 1)
8745 {
8746 /* If this is the destination position,
8747 return a position *before* it in this row,
8748 now that we know it fits in this row. */
8749 if (BUFFER_POS_REACHED_P ())
8750 {
8751 if (it->line_wrap != WORD_WRAP
8752 || wrap_it.sp < 0
8753 /* If we've just found whitespace to
8754 wrap, effectively ignore the
8755 previous wrap point -- it is no
8756 longer relevant, but we won't
8757 have an opportunity to update it,
8758 since we've reached the edge of
8759 this screen line. */
8760 || (may_wrap
8761 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8762 {
8763 it->hpos = hpos_before_this_char;
8764 it->current_x = x_before_this_char;
8765 result = MOVE_POS_MATCH_OR_ZV;
8766 break;
8767 }
8768 if (it->line_wrap == WORD_WRAP
8769 && atpos_it.sp < 0)
8770 {
8771 SAVE_IT (atpos_it, *it, atpos_data);
8772 atpos_it.current_x = x_before_this_char;
8773 atpos_it.hpos = hpos_before_this_char;
8774 }
8775 }
8776
8777 prev_method = it->method;
8778 if (it->method == GET_FROM_BUFFER)
8779 prev_pos = IT_CHARPOS (*it);
8780 set_iterator_to_next (it, true);
8781 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8782 SET_TEXT_POS (this_line_min_pos,
8783 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8784 /* On graphical terminals, newlines may
8785 "overflow" into the fringe if
8786 overflow-newline-into-fringe is non-nil.
8787 On text terminals, and on graphical
8788 terminals with no right margin, newlines
8789 may overflow into the last glyph on the
8790 display line.*/
8791 if (!FRAME_WINDOW_P (it->f)
8792 || ((it->bidi_p
8793 && it->bidi_it.paragraph_dir == R2L)
8794 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8795 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8796 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8797 {
8798 if (!get_next_display_element (it))
8799 {
8800 result = MOVE_POS_MATCH_OR_ZV;
8801 break;
8802 }
8803 if (BUFFER_POS_REACHED_P ())
8804 {
8805 if (ITERATOR_AT_END_OF_LINE_P (it))
8806 result = MOVE_POS_MATCH_OR_ZV;
8807 else
8808 result = MOVE_LINE_CONTINUED;
8809 break;
8810 }
8811 if (ITERATOR_AT_END_OF_LINE_P (it)
8812 && (it->line_wrap != WORD_WRAP
8813 || wrap_it.sp < 0
8814 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8815 {
8816 result = MOVE_NEWLINE_OR_CR;
8817 break;
8818 }
8819 }
8820 }
8821 }
8822 else
8823 IT_RESET_X_ASCENT_DESCENT (it);
8824
8825 /* If the screen line ends with whitespace, and we
8826 are under word-wrap, don't use wrap_it: it is no
8827 longer relevant, but we won't have an opportunity
8828 to update it, since we are done with this screen
8829 line. */
8830 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8831 {
8832 /* If we've found TO_X, go back there, as we now
8833 know the last word fits on this screen line. */
8834 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8835 && atx_it.sp >= 0)
8836 {
8837 RESTORE_IT (it, &atx_it, atx_data);
8838 atpos_it.sp = -1;
8839 atx_it.sp = -1;
8840 result = MOVE_X_REACHED;
8841 break;
8842 }
8843 }
8844 else if (wrap_it.sp >= 0)
8845 {
8846 RESTORE_IT (it, &wrap_it, wrap_data);
8847 atpos_it.sp = -1;
8848 atx_it.sp = -1;
8849 }
8850
8851 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8852 IT_CHARPOS (*it)));
8853 result = MOVE_LINE_CONTINUED;
8854 break;
8855 }
8856
8857 if (BUFFER_POS_REACHED_P ())
8858 {
8859 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8860 goto buffer_pos_reached;
8861 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8862 {
8863 SAVE_IT (atpos_it, *it, atpos_data);
8864 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8865 }
8866 }
8867
8868 if (new_x > it->first_visible_x)
8869 {
8870 /* Glyph is visible. Increment number of glyphs that
8871 would be displayed. */
8872 ++it->hpos;
8873 }
8874 }
8875
8876 if (result != MOVE_UNDEFINED)
8877 break;
8878 }
8879 else if (BUFFER_POS_REACHED_P ())
8880 {
8881 buffer_pos_reached:
8882 IT_RESET_X_ASCENT_DESCENT (it);
8883 result = MOVE_POS_MATCH_OR_ZV;
8884 break;
8885 }
8886 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8887 {
8888 /* Stop when TO_X specified and reached. This check is
8889 necessary here because of lines consisting of a line end,
8890 only. The line end will not produce any glyphs and we
8891 would never get MOVE_X_REACHED. */
8892 eassert (it->nglyphs == 0);
8893 result = MOVE_X_REACHED;
8894 break;
8895 }
8896
8897 /* Is this a line end? If yes, we're done. */
8898 if (ITERATOR_AT_END_OF_LINE_P (it))
8899 {
8900 /* If we are past TO_CHARPOS, but never saw any character
8901 positions smaller than TO_CHARPOS, return
8902 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8903 did. */
8904 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8905 {
8906 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8907 {
8908 if (closest_pos < ZV)
8909 {
8910 RESTORE_IT (it, &ppos_it, ppos_data);
8911 /* Don't recurse if closest_pos is equal to
8912 to_charpos, since we have just tried that. */
8913 if (closest_pos != to_charpos)
8914 move_it_in_display_line_to (it, closest_pos, -1,
8915 MOVE_TO_POS);
8916 result = MOVE_POS_MATCH_OR_ZV;
8917 }
8918 else
8919 goto buffer_pos_reached;
8920 }
8921 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8922 && IT_CHARPOS (*it) > to_charpos)
8923 goto buffer_pos_reached;
8924 else
8925 result = MOVE_NEWLINE_OR_CR;
8926 }
8927 else
8928 result = MOVE_NEWLINE_OR_CR;
8929 break;
8930 }
8931
8932 prev_method = it->method;
8933 if (it->method == GET_FROM_BUFFER)
8934 prev_pos = IT_CHARPOS (*it);
8935 /* The current display element has been consumed. Advance
8936 to the next. */
8937 set_iterator_to_next (it, true);
8938 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8939 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8940 if (IT_CHARPOS (*it) < to_charpos)
8941 saw_smaller_pos = true;
8942 if (it->bidi_p
8943 && (op & MOVE_TO_POS)
8944 && IT_CHARPOS (*it) >= to_charpos
8945 && IT_CHARPOS (*it) < closest_pos)
8946 closest_pos = IT_CHARPOS (*it);
8947
8948 /* Stop if lines are truncated and IT's current x-position is
8949 past the right edge of the window now. */
8950 if (it->line_wrap == TRUNCATE
8951 && it->current_x >= it->last_visible_x)
8952 {
8953 if (!FRAME_WINDOW_P (it->f)
8954 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8955 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8956 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8957 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8958 {
8959 bool at_eob_p = false;
8960
8961 if ((at_eob_p = !get_next_display_element (it))
8962 || BUFFER_POS_REACHED_P ()
8963 /* If we are past TO_CHARPOS, but never saw any
8964 character positions smaller than TO_CHARPOS,
8965 return MOVE_POS_MATCH_OR_ZV, like the
8966 unidirectional display did. */
8967 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8968 && !saw_smaller_pos
8969 && IT_CHARPOS (*it) > to_charpos))
8970 {
8971 if (it->bidi_p
8972 && !BUFFER_POS_REACHED_P ()
8973 && !at_eob_p && closest_pos < ZV)
8974 {
8975 RESTORE_IT (it, &ppos_it, ppos_data);
8976 if (closest_pos != to_charpos)
8977 move_it_in_display_line_to (it, closest_pos, -1,
8978 MOVE_TO_POS);
8979 }
8980 result = MOVE_POS_MATCH_OR_ZV;
8981 break;
8982 }
8983 if (ITERATOR_AT_END_OF_LINE_P (it))
8984 {
8985 result = MOVE_NEWLINE_OR_CR;
8986 break;
8987 }
8988 }
8989 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8990 && !saw_smaller_pos
8991 && IT_CHARPOS (*it) > to_charpos)
8992 {
8993 if (closest_pos < ZV)
8994 {
8995 RESTORE_IT (it, &ppos_it, ppos_data);
8996 if (closest_pos != to_charpos)
8997 move_it_in_display_line_to (it, closest_pos, -1,
8998 MOVE_TO_POS);
8999 }
9000 result = MOVE_POS_MATCH_OR_ZV;
9001 break;
9002 }
9003 result = MOVE_LINE_TRUNCATED;
9004 break;
9005 }
9006 #undef IT_RESET_X_ASCENT_DESCENT
9007 }
9008
9009 #undef BUFFER_POS_REACHED_P
9010
9011 /* If we scanned beyond to_pos and didn't find a point to wrap at,
9012 restore the saved iterator. */
9013 if (atpos_it.sp >= 0)
9014 RESTORE_IT (it, &atpos_it, atpos_data);
9015 else if (atx_it.sp >= 0)
9016 RESTORE_IT (it, &atx_it, atx_data);
9017
9018 done:
9019
9020 if (atpos_data)
9021 bidi_unshelve_cache (atpos_data, true);
9022 if (atx_data)
9023 bidi_unshelve_cache (atx_data, true);
9024 if (wrap_data)
9025 bidi_unshelve_cache (wrap_data, true);
9026 if (ppos_data)
9027 bidi_unshelve_cache (ppos_data, true);
9028
9029 /* Restore the iterator settings altered at the beginning of this
9030 function. */
9031 it->glyph_row = saved_glyph_row;
9032 return result;
9033 }
9034
9035 /* For external use. */
9036 void
9037 move_it_in_display_line (struct it *it,
9038 ptrdiff_t to_charpos, int to_x,
9039 enum move_operation_enum op)
9040 {
9041 if (it->line_wrap == WORD_WRAP
9042 && (op & MOVE_TO_X))
9043 {
9044 struct it save_it;
9045 void *save_data = NULL;
9046 int skip;
9047
9048 SAVE_IT (save_it, *it, save_data);
9049 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9050 /* When word-wrap is on, TO_X may lie past the end
9051 of a wrapped line. Then it->current is the
9052 character on the next line, so backtrack to the
9053 space before the wrap point. */
9054 if (skip == MOVE_LINE_CONTINUED)
9055 {
9056 int prev_x = max (it->current_x - 1, 0);
9057 RESTORE_IT (it, &save_it, save_data);
9058 move_it_in_display_line_to
9059 (it, -1, prev_x, MOVE_TO_X);
9060 }
9061 else
9062 bidi_unshelve_cache (save_data, true);
9063 }
9064 else
9065 move_it_in_display_line_to (it, to_charpos, to_x, op);
9066 }
9067
9068
9069 /* Move IT forward until it satisfies one or more of the criteria in
9070 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9071
9072 OP is a bit-mask that specifies where to stop, and in particular,
9073 which of those four position arguments makes a difference. See the
9074 description of enum move_operation_enum.
9075
9076 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9077 screen line, this function will set IT to the next position that is
9078 displayed to the right of TO_CHARPOS on the screen.
9079
9080 Return the maximum pixel length of any line scanned but never more
9081 than it.last_visible_x. */
9082
9083 int
9084 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9085 {
9086 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9087 int line_height, line_start_x = 0, reached = 0;
9088 int max_current_x = 0;
9089 void *backup_data = NULL;
9090
9091 for (;;)
9092 {
9093 if (op & MOVE_TO_VPOS)
9094 {
9095 /* If no TO_CHARPOS and no TO_X specified, stop at the
9096 start of the line TO_VPOS. */
9097 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9098 {
9099 if (it->vpos == to_vpos)
9100 {
9101 reached = 1;
9102 break;
9103 }
9104 else
9105 skip = move_it_in_display_line_to (it, -1, -1, 0);
9106 }
9107 else
9108 {
9109 /* TO_VPOS >= 0 means stop at TO_X in the line at
9110 TO_VPOS, or at TO_POS, whichever comes first. */
9111 if (it->vpos == to_vpos)
9112 {
9113 reached = 2;
9114 break;
9115 }
9116
9117 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9118
9119 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9120 {
9121 reached = 3;
9122 break;
9123 }
9124 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9125 {
9126 /* We have reached TO_X but not in the line we want. */
9127 skip = move_it_in_display_line_to (it, to_charpos,
9128 -1, MOVE_TO_POS);
9129 if (skip == MOVE_POS_MATCH_OR_ZV)
9130 {
9131 reached = 4;
9132 break;
9133 }
9134 }
9135 }
9136 }
9137 else if (op & MOVE_TO_Y)
9138 {
9139 struct it it_backup;
9140
9141 if (it->line_wrap == WORD_WRAP)
9142 SAVE_IT (it_backup, *it, backup_data);
9143
9144 /* TO_Y specified means stop at TO_X in the line containing
9145 TO_Y---or at TO_CHARPOS if this is reached first. The
9146 problem is that we can't really tell whether the line
9147 contains TO_Y before we have completely scanned it, and
9148 this may skip past TO_X. What we do is to first scan to
9149 TO_X.
9150
9151 If TO_X is not specified, use a TO_X of zero. The reason
9152 is to make the outcome of this function more predictable.
9153 If we didn't use TO_X == 0, we would stop at the end of
9154 the line which is probably not what a caller would expect
9155 to happen. */
9156 skip = move_it_in_display_line_to
9157 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9158 (MOVE_TO_X | (op & MOVE_TO_POS)));
9159
9160 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9161 if (skip == MOVE_POS_MATCH_OR_ZV)
9162 reached = 5;
9163 else if (skip == MOVE_X_REACHED)
9164 {
9165 /* If TO_X was reached, we want to know whether TO_Y is
9166 in the line. We know this is the case if the already
9167 scanned glyphs make the line tall enough. Otherwise,
9168 we must check by scanning the rest of the line. */
9169 line_height = it->max_ascent + it->max_descent;
9170 if (to_y >= it->current_y
9171 && to_y < it->current_y + line_height)
9172 {
9173 reached = 6;
9174 break;
9175 }
9176 SAVE_IT (it_backup, *it, backup_data);
9177 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9178 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9179 op & MOVE_TO_POS);
9180 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9181 line_height = it->max_ascent + it->max_descent;
9182 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9183
9184 if (to_y >= it->current_y
9185 && to_y < it->current_y + line_height)
9186 {
9187 /* If TO_Y is in this line and TO_X was reached
9188 above, we scanned too far. We have to restore
9189 IT's settings to the ones before skipping. But
9190 keep the more accurate values of max_ascent and
9191 max_descent we've found while skipping the rest
9192 of the line, for the sake of callers, such as
9193 pos_visible_p, that need to know the line
9194 height. */
9195 int max_ascent = it->max_ascent;
9196 int max_descent = it->max_descent;
9197
9198 RESTORE_IT (it, &it_backup, backup_data);
9199 it->max_ascent = max_ascent;
9200 it->max_descent = max_descent;
9201 reached = 6;
9202 }
9203 else
9204 {
9205 skip = skip2;
9206 if (skip == MOVE_POS_MATCH_OR_ZV)
9207 reached = 7;
9208 }
9209 }
9210 else
9211 {
9212 /* Check whether TO_Y is in this line. */
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 > it->current_y)
9220 max_current_x = max (it->current_x, max_current_x);
9221
9222 /* When word-wrap is on, TO_X may lie past the end
9223 of a wrapped line. Then it->current is the
9224 character on the next line, so backtrack to the
9225 space before the wrap point. */
9226 if (skip == MOVE_LINE_CONTINUED
9227 && it->line_wrap == WORD_WRAP)
9228 {
9229 int prev_x = max (it->current_x - 1, 0);
9230 RESTORE_IT (it, &it_backup, backup_data);
9231 skip = move_it_in_display_line_to
9232 (it, -1, prev_x, MOVE_TO_X);
9233 }
9234
9235 reached = 6;
9236 }
9237 }
9238
9239 if (reached)
9240 {
9241 max_current_x = max (it->current_x, max_current_x);
9242 break;
9243 }
9244 }
9245 else if (BUFFERP (it->object)
9246 && (it->method == GET_FROM_BUFFER
9247 || it->method == GET_FROM_STRETCH)
9248 && IT_CHARPOS (*it) >= to_charpos
9249 /* Under bidi iteration, a call to set_iterator_to_next
9250 can scan far beyond to_charpos if the initial
9251 portion of the next line needs to be reordered. In
9252 that case, give move_it_in_display_line_to another
9253 chance below. */
9254 && !(it->bidi_p
9255 && it->bidi_it.scan_dir == -1))
9256 skip = MOVE_POS_MATCH_OR_ZV;
9257 else
9258 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9259
9260 switch (skip)
9261 {
9262 case MOVE_POS_MATCH_OR_ZV:
9263 max_current_x = max (it->current_x, max_current_x);
9264 reached = 8;
9265 goto out;
9266
9267 case MOVE_NEWLINE_OR_CR:
9268 max_current_x = max (it->current_x, max_current_x);
9269 set_iterator_to_next (it, true);
9270 it->continuation_lines_width = 0;
9271 break;
9272
9273 case MOVE_LINE_TRUNCATED:
9274 max_current_x = it->last_visible_x;
9275 it->continuation_lines_width = 0;
9276 reseat_at_next_visible_line_start (it, false);
9277 if ((op & MOVE_TO_POS) != 0
9278 && IT_CHARPOS (*it) > to_charpos)
9279 {
9280 reached = 9;
9281 goto out;
9282 }
9283 break;
9284
9285 case MOVE_LINE_CONTINUED:
9286 max_current_x = it->last_visible_x;
9287 /* For continued lines ending in a tab, some of the glyphs
9288 associated with the tab are displayed on the current
9289 line. Since it->current_x does not include these glyphs,
9290 we use it->last_visible_x instead. */
9291 if (it->c == '\t')
9292 {
9293 it->continuation_lines_width += it->last_visible_x;
9294 /* When moving by vpos, ensure that the iterator really
9295 advances to the next line (bug#847, bug#969). Fixme:
9296 do we need to do this in other circumstances? */
9297 if (it->current_x != it->last_visible_x
9298 && (op & MOVE_TO_VPOS)
9299 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9300 {
9301 line_start_x = it->current_x + it->pixel_width
9302 - it->last_visible_x;
9303 if (FRAME_WINDOW_P (it->f))
9304 {
9305 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9306 struct font *face_font = face->font;
9307
9308 /* When display_line produces a continued line
9309 that ends in a TAB, it skips a tab stop that
9310 is closer than the font's space character
9311 width (see x_produce_glyphs where it produces
9312 the stretch glyph which represents a TAB).
9313 We need to reproduce the same logic here. */
9314 eassert (face_font);
9315 if (face_font)
9316 {
9317 if (line_start_x < face_font->space_width)
9318 line_start_x
9319 += it->tab_width * face_font->space_width;
9320 }
9321 }
9322 set_iterator_to_next (it, false);
9323 }
9324 }
9325 else
9326 it->continuation_lines_width += it->current_x;
9327 break;
9328
9329 default:
9330 emacs_abort ();
9331 }
9332
9333 /* Reset/increment for the next run. */
9334 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9335 it->current_x = line_start_x;
9336 line_start_x = 0;
9337 it->hpos = 0;
9338 it->current_y += it->max_ascent + it->max_descent;
9339 ++it->vpos;
9340 last_height = it->max_ascent + it->max_descent;
9341 it->max_ascent = it->max_descent = 0;
9342 }
9343
9344 out:
9345
9346 /* On text terminals, we may stop at the end of a line in the middle
9347 of a multi-character glyph. If the glyph itself is continued,
9348 i.e. it is actually displayed on the next line, don't treat this
9349 stopping point as valid; move to the next line instead (unless
9350 that brings us offscreen). */
9351 if (!FRAME_WINDOW_P (it->f)
9352 && op & MOVE_TO_POS
9353 && IT_CHARPOS (*it) == to_charpos
9354 && it->what == IT_CHARACTER
9355 && it->nglyphs > 1
9356 && it->line_wrap == WINDOW_WRAP
9357 && it->current_x == it->last_visible_x - 1
9358 && it->c != '\n'
9359 && it->c != '\t'
9360 && it->w->window_end_valid
9361 && it->vpos < it->w->window_end_vpos)
9362 {
9363 it->continuation_lines_width += it->current_x;
9364 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9365 it->current_y += it->max_ascent + it->max_descent;
9366 ++it->vpos;
9367 last_height = it->max_ascent + it->max_descent;
9368 }
9369
9370 if (backup_data)
9371 bidi_unshelve_cache (backup_data, true);
9372
9373 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9374
9375 return max_current_x;
9376 }
9377
9378
9379 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9380
9381 If DY > 0, move IT backward at least that many pixels. DY = 0
9382 means move IT backward to the preceding line start or BEGV. This
9383 function may move over more than DY pixels if IT->current_y - DY
9384 ends up in the middle of a line; in this case IT->current_y will be
9385 set to the top of the line moved to. */
9386
9387 void
9388 move_it_vertically_backward (struct it *it, int dy)
9389 {
9390 int nlines, h;
9391 struct it it2, it3;
9392 void *it2data = NULL, *it3data = NULL;
9393 ptrdiff_t start_pos;
9394 int nchars_per_row
9395 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9396 ptrdiff_t pos_limit;
9397
9398 move_further_back:
9399 eassert (dy >= 0);
9400
9401 start_pos = IT_CHARPOS (*it);
9402
9403 /* Estimate how many newlines we must move back. */
9404 nlines = max (1, dy / default_line_pixel_height (it->w));
9405 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9406 pos_limit = BEGV;
9407 else
9408 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9409
9410 /* Set the iterator's position that many lines back. But don't go
9411 back more than NLINES full screen lines -- this wins a day with
9412 buffers which have very long lines. */
9413 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9414 back_to_previous_visible_line_start (it);
9415
9416 /* Reseat the iterator here. When moving backward, we don't want
9417 reseat to skip forward over invisible text, set up the iterator
9418 to deliver from overlay strings at the new position etc. So,
9419 use reseat_1 here. */
9420 reseat_1 (it, it->current.pos, true);
9421
9422 /* We are now surely at a line start. */
9423 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9424 reordering is in effect. */
9425 it->continuation_lines_width = 0;
9426
9427 /* Move forward and see what y-distance we moved. First move to the
9428 start of the next line so that we get its height. We need this
9429 height to be able to tell whether we reached the specified
9430 y-distance. */
9431 SAVE_IT (it2, *it, it2data);
9432 it2.max_ascent = it2.max_descent = 0;
9433 do
9434 {
9435 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9436 MOVE_TO_POS | MOVE_TO_VPOS);
9437 }
9438 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9439 /* If we are in a display string which starts at START_POS,
9440 and that display string includes a newline, and we are
9441 right after that newline (i.e. at the beginning of a
9442 display line), exit the loop, because otherwise we will
9443 infloop, since move_it_to will see that it is already at
9444 START_POS and will not move. */
9445 || (it2.method == GET_FROM_STRING
9446 && IT_CHARPOS (it2) == start_pos
9447 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9448 eassert (IT_CHARPOS (*it) >= BEGV);
9449 SAVE_IT (it3, it2, it3data);
9450
9451 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9452 eassert (IT_CHARPOS (*it) >= BEGV);
9453 /* H is the actual vertical distance from the position in *IT
9454 and the starting position. */
9455 h = it2.current_y - it->current_y;
9456 /* NLINES is the distance in number of lines. */
9457 nlines = it2.vpos - it->vpos;
9458
9459 /* Correct IT's y and vpos position
9460 so that they are relative to the starting point. */
9461 it->vpos -= nlines;
9462 it->current_y -= h;
9463
9464 if (dy == 0)
9465 {
9466 /* DY == 0 means move to the start of the screen line. The
9467 value of nlines is > 0 if continuation lines were involved,
9468 or if the original IT position was at start of a line. */
9469 RESTORE_IT (it, it, it2data);
9470 if (nlines > 0)
9471 move_it_by_lines (it, nlines);
9472 /* The above code moves us to some position NLINES down,
9473 usually to its first glyph (leftmost in an L2R line), but
9474 that's not necessarily the start of the line, under bidi
9475 reordering. We want to get to the character position
9476 that is immediately after the newline of the previous
9477 line. */
9478 if (it->bidi_p
9479 && !it->continuation_lines_width
9480 && !STRINGP (it->string)
9481 && IT_CHARPOS (*it) > BEGV
9482 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9483 {
9484 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9485
9486 DEC_BOTH (cp, bp);
9487 cp = find_newline_no_quit (cp, bp, -1, NULL);
9488 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9489 }
9490 bidi_unshelve_cache (it3data, true);
9491 }
9492 else
9493 {
9494 /* The y-position we try to reach, relative to *IT.
9495 Note that H has been subtracted in front of the if-statement. */
9496 int target_y = it->current_y + h - dy;
9497 int y0 = it3.current_y;
9498 int y1;
9499 int line_height;
9500
9501 RESTORE_IT (&it3, &it3, it3data);
9502 y1 = line_bottom_y (&it3);
9503 line_height = y1 - y0;
9504 RESTORE_IT (it, it, it2data);
9505 /* If we did not reach target_y, try to move further backward if
9506 we can. If we moved too far backward, try to move forward. */
9507 if (target_y < it->current_y
9508 /* This is heuristic. In a window that's 3 lines high, with
9509 a line height of 13 pixels each, recentering with point
9510 on the bottom line will try to move -39/2 = 19 pixels
9511 backward. Try to avoid moving into the first line. */
9512 && (it->current_y - target_y
9513 > min (window_box_height (it->w), line_height * 2 / 3))
9514 && IT_CHARPOS (*it) > BEGV)
9515 {
9516 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9517 target_y - it->current_y));
9518 dy = it->current_y - target_y;
9519 goto move_further_back;
9520 }
9521 else if (target_y >= it->current_y + line_height
9522 && IT_CHARPOS (*it) < ZV)
9523 {
9524 /* Should move forward by at least one line, maybe more.
9525
9526 Note: Calling move_it_by_lines can be expensive on
9527 terminal frames, where compute_motion is used (via
9528 vmotion) to do the job, when there are very long lines
9529 and truncate-lines is nil. That's the reason for
9530 treating terminal frames specially here. */
9531
9532 if (!FRAME_WINDOW_P (it->f))
9533 move_it_vertically (it, target_y - it->current_y);
9534 else
9535 {
9536 do
9537 {
9538 move_it_by_lines (it, 1);
9539 }
9540 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9541 }
9542 }
9543 }
9544 }
9545
9546
9547 /* Move IT by a specified amount of pixel lines DY. DY negative means
9548 move backwards. DY = 0 means move to start of screen line. At the
9549 end, IT will be on the start of a screen line. */
9550
9551 void
9552 move_it_vertically (struct it *it, int dy)
9553 {
9554 if (dy <= 0)
9555 move_it_vertically_backward (it, -dy);
9556 else
9557 {
9558 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9559 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9560 MOVE_TO_POS | MOVE_TO_Y);
9561 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9562
9563 /* If buffer ends in ZV without a newline, move to the start of
9564 the line to satisfy the post-condition. */
9565 if (IT_CHARPOS (*it) == ZV
9566 && ZV > BEGV
9567 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9568 move_it_by_lines (it, 0);
9569 }
9570 }
9571
9572
9573 /* Move iterator IT past the end of the text line it is in. */
9574
9575 void
9576 move_it_past_eol (struct it *it)
9577 {
9578 enum move_it_result rc;
9579
9580 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9581 if (rc == MOVE_NEWLINE_OR_CR)
9582 set_iterator_to_next (it, false);
9583 }
9584
9585
9586 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9587 negative means move up. DVPOS == 0 means move to the start of the
9588 screen line.
9589
9590 Optimization idea: If we would know that IT->f doesn't use
9591 a face with proportional font, we could be faster for
9592 truncate-lines nil. */
9593
9594 void
9595 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9596 {
9597
9598 /* The commented-out optimization uses vmotion on terminals. This
9599 gives bad results, because elements like it->what, on which
9600 callers such as pos_visible_p rely, aren't updated. */
9601 /* struct position pos;
9602 if (!FRAME_WINDOW_P (it->f))
9603 {
9604 struct text_pos textpos;
9605
9606 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9607 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9608 reseat (it, textpos, true);
9609 it->vpos += pos.vpos;
9610 it->current_y += pos.vpos;
9611 }
9612 else */
9613
9614 if (dvpos == 0)
9615 {
9616 /* DVPOS == 0 means move to the start of the screen line. */
9617 move_it_vertically_backward (it, 0);
9618 /* Let next call to line_bottom_y calculate real line height. */
9619 last_height = 0;
9620 }
9621 else if (dvpos > 0)
9622 {
9623 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9624 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9625 {
9626 /* Only move to the next buffer position if we ended up in a
9627 string from display property, not in an overlay string
9628 (before-string or after-string). That is because the
9629 latter don't conceal the underlying buffer position, so
9630 we can ask to move the iterator to the exact position we
9631 are interested in. Note that, even if we are already at
9632 IT_CHARPOS (*it), the call below is not a no-op, as it
9633 will detect that we are at the end of the string, pop the
9634 iterator, and compute it->current_x and it->hpos
9635 correctly. */
9636 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9637 -1, -1, -1, MOVE_TO_POS);
9638 }
9639 }
9640 else
9641 {
9642 struct it it2;
9643 void *it2data = NULL;
9644 ptrdiff_t start_charpos, i;
9645 int nchars_per_row
9646 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9647 bool hit_pos_limit = false;
9648 ptrdiff_t pos_limit;
9649
9650 /* Start at the beginning of the screen line containing IT's
9651 position. This may actually move vertically backwards,
9652 in case of overlays, so adjust dvpos accordingly. */
9653 dvpos += it->vpos;
9654 move_it_vertically_backward (it, 0);
9655 dvpos -= it->vpos;
9656
9657 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9658 screen lines, and reseat the iterator there. */
9659 start_charpos = IT_CHARPOS (*it);
9660 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9661 pos_limit = BEGV;
9662 else
9663 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9664
9665 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9666 back_to_previous_visible_line_start (it);
9667 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9668 hit_pos_limit = true;
9669 reseat (it, it->current.pos, true);
9670
9671 /* Move further back if we end up in a string or an image. */
9672 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9673 {
9674 /* First try to move to start of display line. */
9675 dvpos += it->vpos;
9676 move_it_vertically_backward (it, 0);
9677 dvpos -= it->vpos;
9678 if (IT_POS_VALID_AFTER_MOVE_P (it))
9679 break;
9680 /* If start of line is still in string or image,
9681 move further back. */
9682 back_to_previous_visible_line_start (it);
9683 reseat (it, it->current.pos, true);
9684 dvpos--;
9685 }
9686
9687 it->current_x = it->hpos = 0;
9688
9689 /* Above call may have moved too far if continuation lines
9690 are involved. Scan forward and see if it did. */
9691 SAVE_IT (it2, *it, it2data);
9692 it2.vpos = it2.current_y = 0;
9693 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9694 it->vpos -= it2.vpos;
9695 it->current_y -= it2.current_y;
9696 it->current_x = it->hpos = 0;
9697
9698 /* If we moved too far back, move IT some lines forward. */
9699 if (it2.vpos > -dvpos)
9700 {
9701 int delta = it2.vpos + dvpos;
9702
9703 RESTORE_IT (&it2, &it2, it2data);
9704 SAVE_IT (it2, *it, it2data);
9705 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9706 /* Move back again if we got too far ahead. */
9707 if (IT_CHARPOS (*it) >= start_charpos)
9708 RESTORE_IT (it, &it2, it2data);
9709 else
9710 bidi_unshelve_cache (it2data, true);
9711 }
9712 else if (hit_pos_limit && pos_limit > BEGV
9713 && dvpos < 0 && it2.vpos < -dvpos)
9714 {
9715 /* If we hit the limit, but still didn't make it far enough
9716 back, that means there's a display string with a newline
9717 covering a large chunk of text, and that caused
9718 back_to_previous_visible_line_start try to go too far.
9719 Punish those who commit such atrocities by going back
9720 until we've reached DVPOS, after lifting the limit, which
9721 could make it slow for very long lines. "If it hurts,
9722 don't do that!" */
9723 dvpos += it2.vpos;
9724 RESTORE_IT (it, it, it2data);
9725 for (i = -dvpos; i > 0; --i)
9726 {
9727 back_to_previous_visible_line_start (it);
9728 it->vpos--;
9729 }
9730 reseat_1 (it, it->current.pos, true);
9731 }
9732 else
9733 RESTORE_IT (it, it, it2data);
9734 }
9735 }
9736
9737 /* Return true if IT points into the middle of a display vector. */
9738
9739 bool
9740 in_display_vector_p (struct it *it)
9741 {
9742 return (it->method == GET_FROM_DISPLAY_VECTOR
9743 && it->current.dpvec_index > 0
9744 && it->dpvec + it->current.dpvec_index != it->dpend);
9745 }
9746
9747 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9748 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9749 WINDOW must be a live window and defaults to the selected one. The
9750 return value is a cons of the maximum pixel-width of any text line and
9751 the maximum pixel-height of all text lines.
9752
9753 The optional argument FROM, if non-nil, specifies the first text
9754 position and defaults to the minimum accessible position of the buffer.
9755 If FROM is t, use the minimum accessible position that is not a newline
9756 character. TO, if non-nil, specifies the last text position and
9757 defaults to the maximum accessible position of the buffer. If TO is t,
9758 use the maximum accessible position that is not a newline character.
9759
9760 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9761 width that can be returned. X-LIMIT nil or omitted, means to use the
9762 pixel-width of WINDOW's body; use this if you do not intend to change
9763 the width of WINDOW. Use the maximum width WINDOW may assume if you
9764 intend to change WINDOW's width. In any case, text whose x-coordinate
9765 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9766 can take some time, it's always a good idea to make this argument as
9767 small as possible; in particular, if the buffer contains long lines that
9768 shall be truncated anyway.
9769
9770 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9771 height that can be returned. Text lines whose y-coordinate is beyond
9772 Y-LIMIT are ignored. Since calculating the text height of a large
9773 buffer can take some time, it makes sense to specify this argument if
9774 the size of the buffer is unknown.
9775
9776 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9777 include the height of the mode- or header-line of WINDOW in the return
9778 value. If it is either the symbol `mode-line' or `header-line', include
9779 only the height of that line, if present, in the return value. If t,
9780 include the height of both, if present, in the return value. */)
9781 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9782 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9783 {
9784 struct window *w = decode_live_window (window);
9785 Lisp_Object buffer = w->contents;
9786 struct buffer *b;
9787 struct it it;
9788 struct buffer *old_b = NULL;
9789 ptrdiff_t start, end, pos;
9790 struct text_pos startp;
9791 void *itdata = NULL;
9792 int c, max_y = -1, x = 0, y = 0;
9793
9794 CHECK_BUFFER (buffer);
9795 b = XBUFFER (buffer);
9796
9797 if (b != current_buffer)
9798 {
9799 old_b = current_buffer;
9800 set_buffer_internal (b);
9801 }
9802
9803 if (NILP (from))
9804 start = BEGV;
9805 else if (EQ (from, Qt))
9806 {
9807 start = pos = BEGV;
9808 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9809 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9810 start = pos;
9811 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9812 start = pos;
9813 }
9814 else
9815 {
9816 CHECK_NUMBER_COERCE_MARKER (from);
9817 start = min (max (XINT (from), BEGV), ZV);
9818 }
9819
9820 if (NILP (to))
9821 end = ZV;
9822 else if (EQ (to, Qt))
9823 {
9824 end = pos = ZV;
9825 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9826 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9827 end = pos;
9828 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9829 end = pos;
9830 }
9831 else
9832 {
9833 CHECK_NUMBER_COERCE_MARKER (to);
9834 end = max (start, min (XINT (to), ZV));
9835 }
9836
9837 if (!NILP (y_limit))
9838 {
9839 CHECK_NUMBER (y_limit);
9840 max_y = min (XINT (y_limit), INT_MAX);
9841 }
9842
9843 itdata = bidi_shelve_cache ();
9844 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9845 start_display (&it, w, startp);
9846
9847 if (NILP (x_limit))
9848 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9849 else
9850 {
9851 CHECK_NUMBER (x_limit);
9852 it.last_visible_x = min (XINT (x_limit), INFINITY);
9853 /* Actually, we never want move_it_to stop at to_x. But to make
9854 sure that move_it_in_display_line_to always moves far enough,
9855 we set it to INT_MAX and specify MOVE_TO_X. */
9856 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9857 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9858 }
9859
9860 y = it.current_y + it.max_ascent + it.max_descent;
9861
9862 if (!EQ (mode_and_header_line, Qheader_line)
9863 && !EQ (mode_and_header_line, Qt))
9864 /* Do not count the header-line which was counted automatically by
9865 start_display. */
9866 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9867
9868 if (EQ (mode_and_header_line, Qmode_line)
9869 || EQ (mode_and_header_line, Qt))
9870 /* Do count the mode-line which is not included automatically by
9871 start_display. */
9872 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9873
9874 bidi_unshelve_cache (itdata, false);
9875
9876 if (old_b)
9877 set_buffer_internal (old_b);
9878
9879 return Fcons (make_number (x), make_number (y));
9880 }
9881 \f
9882 /***********************************************************************
9883 Messages
9884 ***********************************************************************/
9885
9886 /* Return the number of arguments the format string FORMAT needs. */
9887
9888 static ptrdiff_t
9889 format_nargs (char const *format)
9890 {
9891 ptrdiff_t nargs = 0;
9892 for (char const *p = format; (p = strchr (p, '%')); p++)
9893 if (p[1] == '%')
9894 p++;
9895 else
9896 nargs++;
9897 return nargs;
9898 }
9899
9900 /* Add a message with format string FORMAT and formatted arguments
9901 to *Messages*. */
9902
9903 void
9904 add_to_log (const char *format, ...)
9905 {
9906 va_list ap;
9907 va_start (ap, format);
9908 vadd_to_log (format, ap);
9909 va_end (ap);
9910 }
9911
9912 void
9913 vadd_to_log (char const *format, va_list ap)
9914 {
9915 ptrdiff_t form_nargs = format_nargs (format);
9916 ptrdiff_t nargs = 1 + form_nargs;
9917 Lisp_Object args[10];
9918 eassert (nargs <= ARRAYELTS (args));
9919 AUTO_STRING (args0, format);
9920 args[0] = args0;
9921 for (ptrdiff_t i = 1; i <= nargs; i++)
9922 args[i] = va_arg (ap, Lisp_Object);
9923 Lisp_Object msg = Qnil;
9924 msg = Fformat_message (nargs, args);
9925
9926 ptrdiff_t len = SBYTES (msg) + 1;
9927 USE_SAFE_ALLOCA;
9928 char *buffer = SAFE_ALLOCA (len);
9929 memcpy (buffer, SDATA (msg), len);
9930
9931 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
9932 SAFE_FREE ();
9933 }
9934
9935
9936 /* Output a newline in the *Messages* buffer if "needs" one. */
9937
9938 void
9939 message_log_maybe_newline (void)
9940 {
9941 if (message_log_need_newline)
9942 message_dolog ("", 0, true, false);
9943 }
9944
9945
9946 /* Add a string M of length NBYTES to the message log, optionally
9947 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9948 true, means interpret the contents of M as multibyte. This
9949 function calls low-level routines in order to bypass text property
9950 hooks, etc. which might not be safe to run.
9951
9952 This may GC (insert may run before/after change hooks),
9953 so the buffer M must NOT point to a Lisp string. */
9954
9955 void
9956 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9957 {
9958 const unsigned char *msg = (const unsigned char *) m;
9959
9960 if (!NILP (Vmemory_full))
9961 return;
9962
9963 if (!NILP (Vmessage_log_max))
9964 {
9965 struct buffer *oldbuf;
9966 Lisp_Object oldpoint, oldbegv, oldzv;
9967 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9968 ptrdiff_t point_at_end = 0;
9969 ptrdiff_t zv_at_end = 0;
9970 Lisp_Object old_deactivate_mark;
9971
9972 old_deactivate_mark = Vdeactivate_mark;
9973 oldbuf = current_buffer;
9974
9975 /* Ensure the Messages buffer exists, and switch to it.
9976 If we created it, set the major-mode. */
9977 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
9978 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9979 if (newbuffer
9980 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9981 call0 (intern ("messages-buffer-mode"));
9982
9983 bset_undo_list (current_buffer, Qt);
9984 bset_cache_long_scans (current_buffer, Qnil);
9985
9986 oldpoint = message_dolog_marker1;
9987 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9988 oldbegv = message_dolog_marker2;
9989 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9990 oldzv = message_dolog_marker3;
9991 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9992
9993 if (PT == Z)
9994 point_at_end = 1;
9995 if (ZV == Z)
9996 zv_at_end = 1;
9997
9998 BEGV = BEG;
9999 BEGV_BYTE = BEG_BYTE;
10000 ZV = Z;
10001 ZV_BYTE = Z_BYTE;
10002 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10003
10004 /* Insert the string--maybe converting multibyte to single byte
10005 or vice versa, so that all the text fits the buffer. */
10006 if (multibyte
10007 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10008 {
10009 ptrdiff_t i;
10010 int c, char_bytes;
10011 char work[1];
10012
10013 /* Convert a multibyte string to single-byte
10014 for the *Message* buffer. */
10015 for (i = 0; i < nbytes; i += char_bytes)
10016 {
10017 c = string_char_and_length (msg + i, &char_bytes);
10018 work[0] = CHAR_TO_BYTE8 (c);
10019 insert_1_both (work, 1, 1, true, false, false);
10020 }
10021 }
10022 else if (! multibyte
10023 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10024 {
10025 ptrdiff_t i;
10026 int c, char_bytes;
10027 unsigned char str[MAX_MULTIBYTE_LENGTH];
10028 /* Convert a single-byte string to multibyte
10029 for the *Message* buffer. */
10030 for (i = 0; i < nbytes; i++)
10031 {
10032 c = msg[i];
10033 MAKE_CHAR_MULTIBYTE (c);
10034 char_bytes = CHAR_STRING (c, str);
10035 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10036 }
10037 }
10038 else if (nbytes)
10039 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10040 true, false, false);
10041
10042 if (nlflag)
10043 {
10044 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10045 printmax_t dups;
10046
10047 insert_1_both ("\n", 1, 1, true, false, false);
10048
10049 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10050 this_bol = PT;
10051 this_bol_byte = PT_BYTE;
10052
10053 /* See if this line duplicates the previous one.
10054 If so, combine duplicates. */
10055 if (this_bol > BEG)
10056 {
10057 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10058 prev_bol = PT;
10059 prev_bol_byte = PT_BYTE;
10060
10061 dups = message_log_check_duplicate (prev_bol_byte,
10062 this_bol_byte);
10063 if (dups)
10064 {
10065 del_range_both (prev_bol, prev_bol_byte,
10066 this_bol, this_bol_byte, false);
10067 if (dups > 1)
10068 {
10069 char dupstr[sizeof " [ times]"
10070 + INT_STRLEN_BOUND (printmax_t)];
10071
10072 /* If you change this format, don't forget to also
10073 change message_log_check_duplicate. */
10074 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10075 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10076 insert_1_both (dupstr, duplen, duplen,
10077 true, false, true);
10078 }
10079 }
10080 }
10081
10082 /* If we have more than the desired maximum number of lines
10083 in the *Messages* buffer now, delete the oldest ones.
10084 This is safe because we don't have undo in this buffer. */
10085
10086 if (NATNUMP (Vmessage_log_max))
10087 {
10088 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10089 -XFASTINT (Vmessage_log_max) - 1, false);
10090 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10091 }
10092 }
10093 BEGV = marker_position (oldbegv);
10094 BEGV_BYTE = marker_byte_position (oldbegv);
10095
10096 if (zv_at_end)
10097 {
10098 ZV = Z;
10099 ZV_BYTE = Z_BYTE;
10100 }
10101 else
10102 {
10103 ZV = marker_position (oldzv);
10104 ZV_BYTE = marker_byte_position (oldzv);
10105 }
10106
10107 if (point_at_end)
10108 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10109 else
10110 /* We can't do Fgoto_char (oldpoint) because it will run some
10111 Lisp code. */
10112 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10113 marker_byte_position (oldpoint));
10114
10115 unchain_marker (XMARKER (oldpoint));
10116 unchain_marker (XMARKER (oldbegv));
10117 unchain_marker (XMARKER (oldzv));
10118
10119 /* We called insert_1_both above with its 5th argument (PREPARE)
10120 false, which prevents insert_1_both from calling
10121 prepare_to_modify_buffer, which in turns prevents us from
10122 incrementing windows_or_buffers_changed even if *Messages* is
10123 shown in some window. So we must manually set
10124 windows_or_buffers_changed here to make up for that. */
10125 windows_or_buffers_changed = old_windows_or_buffers_changed;
10126 bset_redisplay (current_buffer);
10127
10128 set_buffer_internal (oldbuf);
10129
10130 message_log_need_newline = !nlflag;
10131 Vdeactivate_mark = old_deactivate_mark;
10132 }
10133 }
10134
10135
10136 /* We are at the end of the buffer after just having inserted a newline.
10137 (Note: We depend on the fact we won't be crossing the gap.)
10138 Check to see if the most recent message looks a lot like the previous one.
10139 Return 0 if different, 1 if the new one should just replace it, or a
10140 value N > 1 if we should also append " [N times]". */
10141
10142 static intmax_t
10143 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10144 {
10145 ptrdiff_t i;
10146 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10147 bool seen_dots = false;
10148 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10149 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10150
10151 for (i = 0; i < len; i++)
10152 {
10153 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10154 seen_dots = true;
10155 if (p1[i] != p2[i])
10156 return seen_dots;
10157 }
10158 p1 += len;
10159 if (*p1 == '\n')
10160 return 2;
10161 if (*p1++ == ' ' && *p1++ == '[')
10162 {
10163 char *pend;
10164 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10165 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10166 return n + 1;
10167 }
10168 return 0;
10169 }
10170 \f
10171
10172 /* Display an echo area message M with a specified length of NBYTES
10173 bytes. The string may include null characters. If M is not a
10174 string, clear out any existing message, and let the mini-buffer
10175 text show through.
10176
10177 This function cancels echoing. */
10178
10179 void
10180 message3 (Lisp_Object m)
10181 {
10182 clear_message (true, true);
10183 cancel_echoing ();
10184
10185 /* First flush out any partial line written with print. */
10186 message_log_maybe_newline ();
10187 if (STRINGP (m))
10188 {
10189 ptrdiff_t nbytes = SBYTES (m);
10190 bool multibyte = STRING_MULTIBYTE (m);
10191 char *buffer;
10192 USE_SAFE_ALLOCA;
10193 SAFE_ALLOCA_STRING (buffer, m);
10194 message_dolog (buffer, nbytes, true, multibyte);
10195 SAFE_FREE ();
10196 }
10197 if (! inhibit_message)
10198 message3_nolog (m);
10199 }
10200
10201 /* Log the message M to stderr. Log an empty line if M is not a string. */
10202
10203 static void
10204 message_to_stderr (Lisp_Object m)
10205 {
10206 if (noninteractive_need_newline)
10207 {
10208 noninteractive_need_newline = false;
10209 fputc ('\n', stderr);
10210 }
10211 if (STRINGP (m))
10212 {
10213 Lisp_Object coding_system = Vlocale_coding_system;
10214 Lisp_Object s;
10215
10216 if (!NILP (Vcoding_system_for_write))
10217 coding_system = Vcoding_system_for_write;
10218 if (!NILP (coding_system))
10219 s = code_convert_string_norecord (m, coding_system, true);
10220 else
10221 s = m;
10222
10223 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10224 }
10225 if (!cursor_in_echo_area)
10226 fputc ('\n', stderr);
10227 fflush (stderr);
10228 }
10229
10230 /* The non-logging version of message3.
10231 This does not cancel echoing, because it is used for echoing.
10232 Perhaps we need to make a separate function for echoing
10233 and make this cancel echoing. */
10234
10235 void
10236 message3_nolog (Lisp_Object m)
10237 {
10238 struct frame *sf = SELECTED_FRAME ();
10239
10240 if (FRAME_INITIAL_P (sf))
10241 message_to_stderr (m);
10242 /* Error messages get reported properly by cmd_error, so this must be just an
10243 informative message; if the frame hasn't really been initialized yet, just
10244 toss it. */
10245 else if (INTERACTIVE && sf->glyphs_initialized_p)
10246 {
10247 /* Get the frame containing the mini-buffer
10248 that the selected frame is using. */
10249 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10250 Lisp_Object frame = XWINDOW (mini_window)->frame;
10251 struct frame *f = XFRAME (frame);
10252
10253 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10254 Fmake_frame_visible (frame);
10255
10256 if (STRINGP (m) && SCHARS (m) > 0)
10257 {
10258 set_message (m);
10259 if (minibuffer_auto_raise)
10260 Fraise_frame (frame);
10261 /* Assume we are not echoing.
10262 (If we are, echo_now will override this.) */
10263 echo_message_buffer = Qnil;
10264 }
10265 else
10266 clear_message (true, true);
10267
10268 do_pending_window_change (false);
10269 echo_area_display (true);
10270 do_pending_window_change (false);
10271 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10272 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10273 }
10274 }
10275
10276
10277 /* Display a null-terminated echo area message M. If M is 0, clear
10278 out any existing message, and let the mini-buffer text show through.
10279
10280 The buffer M must continue to exist until after the echo area gets
10281 cleared or some other message gets displayed there. Do not pass
10282 text that is stored in a Lisp string. Do not pass text in a buffer
10283 that was alloca'd. */
10284
10285 void
10286 message1 (const char *m)
10287 {
10288 message3 (m ? build_unibyte_string (m) : Qnil);
10289 }
10290
10291
10292 /* The non-logging counterpart of message1. */
10293
10294 void
10295 message1_nolog (const char *m)
10296 {
10297 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10298 }
10299
10300 /* Display a message M which contains a single %s
10301 which gets replaced with STRING. */
10302
10303 void
10304 message_with_string (const char *m, Lisp_Object string, bool log)
10305 {
10306 CHECK_STRING (string);
10307
10308 bool need_message;
10309 if (noninteractive)
10310 need_message = !!m;
10311 else if (!INTERACTIVE)
10312 need_message = false;
10313 else
10314 {
10315 /* The frame whose minibuffer we're going to display the message on.
10316 It may be larger than the selected frame, so we need
10317 to use its buffer, not the selected frame's buffer. */
10318 Lisp_Object mini_window;
10319 struct frame *f, *sf = SELECTED_FRAME ();
10320
10321 /* Get the frame containing the minibuffer
10322 that the selected frame is using. */
10323 mini_window = FRAME_MINIBUF_WINDOW (sf);
10324 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10325
10326 /* Error messages get reported properly by cmd_error, so this must be
10327 just an informative message; if the frame hasn't really been
10328 initialized yet, just toss it. */
10329 need_message = f->glyphs_initialized_p;
10330 }
10331
10332 if (need_message)
10333 {
10334 AUTO_STRING (fmt, m);
10335 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10336
10337 if (noninteractive)
10338 message_to_stderr (msg);
10339 else
10340 {
10341 if (log)
10342 message3 (msg);
10343 else
10344 message3_nolog (msg);
10345
10346 /* Print should start at the beginning of the message
10347 buffer next time. */
10348 message_buf_print = false;
10349 }
10350 }
10351 }
10352
10353
10354 /* Dump an informative message to the minibuf. If M is 0, clear out
10355 any existing message, and let the mini-buffer text show through.
10356
10357 The message must be safe ASCII and the format must not contain ` or
10358 '. If your message and format do not fit into this category,
10359 convert your arguments to Lisp objects and use Fmessage instead. */
10360
10361 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10362 vmessage (const char *m, va_list ap)
10363 {
10364 if (noninteractive)
10365 {
10366 if (m)
10367 {
10368 if (noninteractive_need_newline)
10369 putc ('\n', stderr);
10370 noninteractive_need_newline = false;
10371 vfprintf (stderr, m, ap);
10372 if (!cursor_in_echo_area)
10373 fprintf (stderr, "\n");
10374 fflush (stderr);
10375 }
10376 }
10377 else if (INTERACTIVE)
10378 {
10379 /* The frame whose mini-buffer we're going to display the message
10380 on. It may be larger than the selected frame, so we need to
10381 use its buffer, not the selected frame's buffer. */
10382 Lisp_Object mini_window;
10383 struct frame *f, *sf = SELECTED_FRAME ();
10384
10385 /* Get the frame containing the mini-buffer
10386 that the selected frame is using. */
10387 mini_window = FRAME_MINIBUF_WINDOW (sf);
10388 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10389
10390 /* Error messages get reported properly by cmd_error, so this must be
10391 just an informative message; if the frame hasn't really been
10392 initialized yet, just toss it. */
10393 if (f->glyphs_initialized_p)
10394 {
10395 if (m)
10396 {
10397 ptrdiff_t len;
10398 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10399 USE_SAFE_ALLOCA;
10400 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10401
10402 len = doprnt (message_buf, maxsize, m, 0, ap);
10403
10404 message3 (make_string (message_buf, len));
10405 SAFE_FREE ();
10406 }
10407 else
10408 message1 (0);
10409
10410 /* Print should start at the beginning of the message
10411 buffer next time. */
10412 message_buf_print = false;
10413 }
10414 }
10415 }
10416
10417 void
10418 message (const char *m, ...)
10419 {
10420 va_list ap;
10421 va_start (ap, m);
10422 vmessage (m, ap);
10423 va_end (ap);
10424 }
10425
10426
10427 /* Display the current message in the current mini-buffer. This is
10428 only called from error handlers in process.c, and is not time
10429 critical. */
10430
10431 void
10432 update_echo_area (void)
10433 {
10434 if (!NILP (echo_area_buffer[0]))
10435 {
10436 Lisp_Object string;
10437 string = Fcurrent_message ();
10438 message3 (string);
10439 }
10440 }
10441
10442
10443 /* Make sure echo area buffers in `echo_buffers' are live.
10444 If they aren't, make new ones. */
10445
10446 static void
10447 ensure_echo_area_buffers (void)
10448 {
10449 int i;
10450
10451 for (i = 0; i < 2; ++i)
10452 if (!BUFFERP (echo_buffer[i])
10453 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10454 {
10455 char name[30];
10456 Lisp_Object old_buffer;
10457 int j;
10458
10459 old_buffer = echo_buffer[i];
10460 echo_buffer[i] = Fget_buffer_create
10461 (make_formatted_string (name, " *Echo Area %d*", i));
10462 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10463 /* to force word wrap in echo area -
10464 it was decided to postpone this*/
10465 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10466
10467 for (j = 0; j < 2; ++j)
10468 if (EQ (old_buffer, echo_area_buffer[j]))
10469 echo_area_buffer[j] = echo_buffer[i];
10470 }
10471 }
10472
10473
10474 /* Call FN with args A1..A2 with either the current or last displayed
10475 echo_area_buffer as current buffer.
10476
10477 WHICH zero means use the current message buffer
10478 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10479 from echo_buffer[] and clear it.
10480
10481 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10482 suitable buffer from echo_buffer[] and clear it.
10483
10484 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10485 that the current message becomes the last displayed one, make
10486 choose a suitable buffer for echo_area_buffer[0], and clear it.
10487
10488 Value is what FN returns. */
10489
10490 static bool
10491 with_echo_area_buffer (struct window *w, int which,
10492 bool (*fn) (ptrdiff_t, Lisp_Object),
10493 ptrdiff_t a1, Lisp_Object a2)
10494 {
10495 Lisp_Object buffer;
10496 bool this_one, the_other, clear_buffer_p, rc;
10497 ptrdiff_t count = SPECPDL_INDEX ();
10498
10499 /* If buffers aren't live, make new ones. */
10500 ensure_echo_area_buffers ();
10501
10502 clear_buffer_p = false;
10503
10504 if (which == 0)
10505 this_one = false, the_other = true;
10506 else if (which > 0)
10507 this_one = true, the_other = false;
10508 else
10509 {
10510 this_one = false, the_other = true;
10511 clear_buffer_p = true;
10512
10513 /* We need a fresh one in case the current echo buffer equals
10514 the one containing the last displayed echo area message. */
10515 if (!NILP (echo_area_buffer[this_one])
10516 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10517 echo_area_buffer[this_one] = Qnil;
10518 }
10519
10520 /* Choose a suitable buffer from echo_buffer[] is we don't
10521 have one. */
10522 if (NILP (echo_area_buffer[this_one]))
10523 {
10524 echo_area_buffer[this_one]
10525 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10526 ? echo_buffer[the_other]
10527 : echo_buffer[this_one]);
10528 clear_buffer_p = true;
10529 }
10530
10531 buffer = echo_area_buffer[this_one];
10532
10533 /* Don't get confused by reusing the buffer used for echoing
10534 for a different purpose. */
10535 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10536 cancel_echoing ();
10537
10538 record_unwind_protect (unwind_with_echo_area_buffer,
10539 with_echo_area_buffer_unwind_data (w));
10540
10541 /* Make the echo area buffer current. Note that for display
10542 purposes, it is not necessary that the displayed window's buffer
10543 == current_buffer, except for text property lookup. So, let's
10544 only set that buffer temporarily here without doing a full
10545 Fset_window_buffer. We must also change w->pointm, though,
10546 because otherwise an assertions in unshow_buffer fails, and Emacs
10547 aborts. */
10548 set_buffer_internal_1 (XBUFFER (buffer));
10549 if (w)
10550 {
10551 wset_buffer (w, buffer);
10552 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10553 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10554 }
10555
10556 bset_undo_list (current_buffer, Qt);
10557 bset_read_only (current_buffer, Qnil);
10558 specbind (Qinhibit_read_only, Qt);
10559 specbind (Qinhibit_modification_hooks, Qt);
10560
10561 if (clear_buffer_p && Z > BEG)
10562 del_range (BEG, Z);
10563
10564 eassert (BEGV >= BEG);
10565 eassert (ZV <= Z && ZV >= BEGV);
10566
10567 rc = fn (a1, a2);
10568
10569 eassert (BEGV >= BEG);
10570 eassert (ZV <= Z && ZV >= BEGV);
10571
10572 unbind_to (count, Qnil);
10573 return rc;
10574 }
10575
10576
10577 /* Save state that should be preserved around the call to the function
10578 FN called in with_echo_area_buffer. */
10579
10580 static Lisp_Object
10581 with_echo_area_buffer_unwind_data (struct window *w)
10582 {
10583 int i = 0;
10584 Lisp_Object vector, tmp;
10585
10586 /* Reduce consing by keeping one vector in
10587 Vwith_echo_area_save_vector. */
10588 vector = Vwith_echo_area_save_vector;
10589 Vwith_echo_area_save_vector = Qnil;
10590
10591 if (NILP (vector))
10592 vector = Fmake_vector (make_number (11), Qnil);
10593
10594 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10595 ASET (vector, i, Vdeactivate_mark); ++i;
10596 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10597
10598 if (w)
10599 {
10600 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10601 ASET (vector, i, w->contents); ++i;
10602 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10603 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10604 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10605 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10606 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10607 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10608 }
10609 else
10610 {
10611 int end = i + 8;
10612 for (; i < end; ++i)
10613 ASET (vector, i, Qnil);
10614 }
10615
10616 eassert (i == ASIZE (vector));
10617 return vector;
10618 }
10619
10620
10621 /* Restore global state from VECTOR which was created by
10622 with_echo_area_buffer_unwind_data. */
10623
10624 static void
10625 unwind_with_echo_area_buffer (Lisp_Object vector)
10626 {
10627 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10628 Vdeactivate_mark = AREF (vector, 1);
10629 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10630
10631 if (WINDOWP (AREF (vector, 3)))
10632 {
10633 struct window *w;
10634 Lisp_Object buffer;
10635
10636 w = XWINDOW (AREF (vector, 3));
10637 buffer = AREF (vector, 4);
10638
10639 wset_buffer (w, buffer);
10640 set_marker_both (w->pointm, buffer,
10641 XFASTINT (AREF (vector, 5)),
10642 XFASTINT (AREF (vector, 6)));
10643 set_marker_both (w->old_pointm, buffer,
10644 XFASTINT (AREF (vector, 7)),
10645 XFASTINT (AREF (vector, 8)));
10646 set_marker_both (w->start, buffer,
10647 XFASTINT (AREF (vector, 9)),
10648 XFASTINT (AREF (vector, 10)));
10649 }
10650
10651 Vwith_echo_area_save_vector = vector;
10652 }
10653
10654
10655 /* Set up the echo area for use by print functions. MULTIBYTE_P
10656 means we will print multibyte. */
10657
10658 void
10659 setup_echo_area_for_printing (bool multibyte_p)
10660 {
10661 /* If we can't find an echo area any more, exit. */
10662 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10663 Fkill_emacs (Qnil);
10664
10665 ensure_echo_area_buffers ();
10666
10667 if (!message_buf_print)
10668 {
10669 /* A message has been output since the last time we printed.
10670 Choose a fresh echo area buffer. */
10671 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10672 echo_area_buffer[0] = echo_buffer[1];
10673 else
10674 echo_area_buffer[0] = echo_buffer[0];
10675
10676 /* Switch to that buffer and clear it. */
10677 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10678 bset_truncate_lines (current_buffer, Qnil);
10679
10680 if (Z > BEG)
10681 {
10682 ptrdiff_t count = SPECPDL_INDEX ();
10683 specbind (Qinhibit_read_only, Qt);
10684 /* Note that undo recording is always disabled. */
10685 del_range (BEG, Z);
10686 unbind_to (count, Qnil);
10687 }
10688 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10689
10690 /* Set up the buffer for the multibyteness we need. */
10691 if (multibyte_p
10692 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10693 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10694
10695 /* Raise the frame containing the echo area. */
10696 if (minibuffer_auto_raise)
10697 {
10698 struct frame *sf = SELECTED_FRAME ();
10699 Lisp_Object mini_window;
10700 mini_window = FRAME_MINIBUF_WINDOW (sf);
10701 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10702 }
10703
10704 message_log_maybe_newline ();
10705 message_buf_print = true;
10706 }
10707 else
10708 {
10709 if (NILP (echo_area_buffer[0]))
10710 {
10711 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10712 echo_area_buffer[0] = echo_buffer[1];
10713 else
10714 echo_area_buffer[0] = echo_buffer[0];
10715 }
10716
10717 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10718 {
10719 /* Someone switched buffers between print requests. */
10720 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10721 bset_truncate_lines (current_buffer, Qnil);
10722 }
10723 }
10724 }
10725
10726
10727 /* Display an echo area message in window W. Value is true if W's
10728 height is changed. If display_last_displayed_message_p,
10729 display the message that was last displayed, otherwise
10730 display the current message. */
10731
10732 static bool
10733 display_echo_area (struct window *w)
10734 {
10735 bool no_message_p, window_height_changed_p;
10736
10737 /* Temporarily disable garbage collections while displaying the echo
10738 area. This is done because a GC can print a message itself.
10739 That message would modify the echo area buffer's contents while a
10740 redisplay of the buffer is going on, and seriously confuse
10741 redisplay. */
10742 ptrdiff_t count = inhibit_garbage_collection ();
10743
10744 /* If there is no message, we must call display_echo_area_1
10745 nevertheless because it resizes the window. But we will have to
10746 reset the echo_area_buffer in question to nil at the end because
10747 with_echo_area_buffer will sets it to an empty buffer. */
10748 bool i = display_last_displayed_message_p;
10749 /* According to the C99, C11 and C++11 standards, the integral value
10750 of a "bool" is always 0 or 1, so this array access is safe here,
10751 if oddly typed. */
10752 no_message_p = NILP (echo_area_buffer[i]);
10753
10754 window_height_changed_p
10755 = with_echo_area_buffer (w, display_last_displayed_message_p,
10756 display_echo_area_1,
10757 (intptr_t) w, Qnil);
10758
10759 if (no_message_p)
10760 echo_area_buffer[i] = Qnil;
10761
10762 unbind_to (count, Qnil);
10763 return window_height_changed_p;
10764 }
10765
10766
10767 /* Helper for display_echo_area. Display the current buffer which
10768 contains the current echo area message in window W, a mini-window,
10769 a pointer to which is passed in A1. A2..A4 are currently not used.
10770 Change the height of W so that all of the message is displayed.
10771 Value is true if height of W was changed. */
10772
10773 static bool
10774 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10775 {
10776 intptr_t i1 = a1;
10777 struct window *w = (struct window *) i1;
10778 Lisp_Object window;
10779 struct text_pos start;
10780
10781 /* We are about to enter redisplay without going through
10782 redisplay_internal, so we need to forget these faces by hand
10783 here. */
10784 forget_escape_and_glyphless_faces ();
10785
10786 /* Do this before displaying, so that we have a large enough glyph
10787 matrix for the display. If we can't get enough space for the
10788 whole text, display the last N lines. That works by setting w->start. */
10789 bool window_height_changed_p = resize_mini_window (w, false);
10790
10791 /* Use the starting position chosen by resize_mini_window. */
10792 SET_TEXT_POS_FROM_MARKER (start, w->start);
10793
10794 /* Display. */
10795 clear_glyph_matrix (w->desired_matrix);
10796 XSETWINDOW (window, w);
10797 try_window (window, start, 0);
10798
10799 return window_height_changed_p;
10800 }
10801
10802
10803 /* Resize the echo area window to exactly the size needed for the
10804 currently displayed message, if there is one. If a mini-buffer
10805 is active, don't shrink it. */
10806
10807 void
10808 resize_echo_area_exactly (void)
10809 {
10810 if (BUFFERP (echo_area_buffer[0])
10811 && WINDOWP (echo_area_window))
10812 {
10813 struct window *w = XWINDOW (echo_area_window);
10814 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10815 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10816 (intptr_t) w, resize_exactly);
10817 if (resized_p)
10818 {
10819 windows_or_buffers_changed = 42;
10820 update_mode_lines = 30;
10821 redisplay_internal ();
10822 }
10823 }
10824 }
10825
10826
10827 /* Callback function for with_echo_area_buffer, when used from
10828 resize_echo_area_exactly. A1 contains a pointer to the window to
10829 resize, EXACTLY non-nil means resize the mini-window exactly to the
10830 size of the text displayed. A3 and A4 are not used. Value is what
10831 resize_mini_window returns. */
10832
10833 static bool
10834 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10835 {
10836 intptr_t i1 = a1;
10837 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10838 }
10839
10840
10841 /* Resize mini-window W to fit the size of its contents. EXACT_P
10842 means size the window exactly to the size needed. Otherwise, it's
10843 only enlarged until W's buffer is empty.
10844
10845 Set W->start to the right place to begin display. If the whole
10846 contents fit, start at the beginning. Otherwise, start so as
10847 to make the end of the contents appear. This is particularly
10848 important for y-or-n-p, but seems desirable generally.
10849
10850 Value is true if the window height has been changed. */
10851
10852 bool
10853 resize_mini_window (struct window *w, bool exact_p)
10854 {
10855 struct frame *f = XFRAME (w->frame);
10856 bool window_height_changed_p = false;
10857
10858 eassert (MINI_WINDOW_P (w));
10859
10860 /* By default, start display at the beginning. */
10861 set_marker_both (w->start, w->contents,
10862 BUF_BEGV (XBUFFER (w->contents)),
10863 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10864
10865 /* Don't resize windows while redisplaying a window; it would
10866 confuse redisplay functions when the size of the window they are
10867 displaying changes from under them. Such a resizing can happen,
10868 for instance, when which-func prints a long message while
10869 we are running fontification-functions. We're running these
10870 functions with safe_call which binds inhibit-redisplay to t. */
10871 if (!NILP (Vinhibit_redisplay))
10872 return false;
10873
10874 /* Nil means don't try to resize. */
10875 if (NILP (Vresize_mini_windows)
10876 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10877 return false;
10878
10879 if (!FRAME_MINIBUF_ONLY_P (f))
10880 {
10881 struct it it;
10882 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10883 + WINDOW_PIXEL_HEIGHT (w));
10884 int unit = FRAME_LINE_HEIGHT (f);
10885 int height, max_height;
10886 struct text_pos start;
10887 struct buffer *old_current_buffer = NULL;
10888
10889 if (current_buffer != XBUFFER (w->contents))
10890 {
10891 old_current_buffer = current_buffer;
10892 set_buffer_internal (XBUFFER (w->contents));
10893 }
10894
10895 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10896
10897 /* Compute the max. number of lines specified by the user. */
10898 if (FLOATP (Vmax_mini_window_height))
10899 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10900 else if (INTEGERP (Vmax_mini_window_height))
10901 max_height = XINT (Vmax_mini_window_height) * unit;
10902 else
10903 max_height = total_height / 4;
10904
10905 /* Correct that max. height if it's bogus. */
10906 max_height = clip_to_bounds (unit, max_height, total_height);
10907
10908 /* Find out the height of the text in the window. */
10909 if (it.line_wrap == TRUNCATE)
10910 height = unit;
10911 else
10912 {
10913 last_height = 0;
10914 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10915 if (it.max_ascent == 0 && it.max_descent == 0)
10916 height = it.current_y + last_height;
10917 else
10918 height = it.current_y + it.max_ascent + it.max_descent;
10919 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10920 }
10921
10922 /* Compute a suitable window start. */
10923 if (height > max_height)
10924 {
10925 height = (max_height / unit) * unit;
10926 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10927 move_it_vertically_backward (&it, height - unit);
10928 start = it.current.pos;
10929 }
10930 else
10931 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10932 SET_MARKER_FROM_TEXT_POS (w->start, start);
10933
10934 if (EQ (Vresize_mini_windows, Qgrow_only))
10935 {
10936 /* Let it grow only, until we display an empty message, in which
10937 case the window shrinks again. */
10938 if (height > WINDOW_PIXEL_HEIGHT (w))
10939 {
10940 int old_height = WINDOW_PIXEL_HEIGHT (w);
10941
10942 FRAME_WINDOWS_FROZEN (f) = true;
10943 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10944 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10945 }
10946 else if (height < WINDOW_PIXEL_HEIGHT (w)
10947 && (exact_p || BEGV == ZV))
10948 {
10949 int old_height = WINDOW_PIXEL_HEIGHT (w);
10950
10951 FRAME_WINDOWS_FROZEN (f) = false;
10952 shrink_mini_window (w, true);
10953 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10954 }
10955 }
10956 else
10957 {
10958 /* Always resize to exact size needed. */
10959 if (height > WINDOW_PIXEL_HEIGHT (w))
10960 {
10961 int old_height = WINDOW_PIXEL_HEIGHT (w);
10962
10963 FRAME_WINDOWS_FROZEN (f) = true;
10964 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10965 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10966 }
10967 else if (height < WINDOW_PIXEL_HEIGHT (w))
10968 {
10969 int old_height = WINDOW_PIXEL_HEIGHT (w);
10970
10971 FRAME_WINDOWS_FROZEN (f) = false;
10972 shrink_mini_window (w, true);
10973
10974 if (height)
10975 {
10976 FRAME_WINDOWS_FROZEN (f) = true;
10977 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10978 }
10979
10980 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10981 }
10982 }
10983
10984 if (old_current_buffer)
10985 set_buffer_internal (old_current_buffer);
10986 }
10987
10988 return window_height_changed_p;
10989 }
10990
10991
10992 /* Value is the current message, a string, or nil if there is no
10993 current message. */
10994
10995 Lisp_Object
10996 current_message (void)
10997 {
10998 Lisp_Object msg;
10999
11000 if (!BUFFERP (echo_area_buffer[0]))
11001 msg = Qnil;
11002 else
11003 {
11004 with_echo_area_buffer (0, 0, current_message_1,
11005 (intptr_t) &msg, Qnil);
11006 if (NILP (msg))
11007 echo_area_buffer[0] = Qnil;
11008 }
11009
11010 return msg;
11011 }
11012
11013
11014 static bool
11015 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11016 {
11017 intptr_t i1 = a1;
11018 Lisp_Object *msg = (Lisp_Object *) i1;
11019
11020 if (Z > BEG)
11021 *msg = make_buffer_string (BEG, Z, true);
11022 else
11023 *msg = Qnil;
11024 return false;
11025 }
11026
11027
11028 /* Push the current message on Vmessage_stack for later restoration
11029 by restore_message. Value is true if the current message isn't
11030 empty. This is a relatively infrequent operation, so it's not
11031 worth optimizing. */
11032
11033 bool
11034 push_message (void)
11035 {
11036 Lisp_Object msg = current_message ();
11037 Vmessage_stack = Fcons (msg, Vmessage_stack);
11038 return STRINGP (msg);
11039 }
11040
11041
11042 /* Restore message display from the top of Vmessage_stack. */
11043
11044 void
11045 restore_message (void)
11046 {
11047 eassert (CONSP (Vmessage_stack));
11048 message3_nolog (XCAR (Vmessage_stack));
11049 }
11050
11051
11052 /* Handler for unwind-protect calling pop_message. */
11053
11054 void
11055 pop_message_unwind (void)
11056 {
11057 /* Pop the top-most entry off Vmessage_stack. */
11058 eassert (CONSP (Vmessage_stack));
11059 Vmessage_stack = XCDR (Vmessage_stack);
11060 }
11061
11062
11063 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11064 exits. If the stack is not empty, we have a missing pop_message
11065 somewhere. */
11066
11067 void
11068 check_message_stack (void)
11069 {
11070 if (!NILP (Vmessage_stack))
11071 emacs_abort ();
11072 }
11073
11074
11075 /* Truncate to NCHARS what will be displayed in the echo area the next
11076 time we display it---but don't redisplay it now. */
11077
11078 void
11079 truncate_echo_area (ptrdiff_t nchars)
11080 {
11081 if (nchars == 0)
11082 echo_area_buffer[0] = Qnil;
11083 else if (!noninteractive
11084 && INTERACTIVE
11085 && !NILP (echo_area_buffer[0]))
11086 {
11087 struct frame *sf = SELECTED_FRAME ();
11088 /* Error messages get reported properly by cmd_error, so this must be
11089 just an informative message; if the frame hasn't really been
11090 initialized yet, just toss it. */
11091 if (sf->glyphs_initialized_p)
11092 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11093 }
11094 }
11095
11096
11097 /* Helper function for truncate_echo_area. Truncate the current
11098 message to at most NCHARS characters. */
11099
11100 static bool
11101 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11102 {
11103 if (BEG + nchars < Z)
11104 del_range (BEG + nchars, Z);
11105 if (Z == BEG)
11106 echo_area_buffer[0] = Qnil;
11107 return false;
11108 }
11109
11110 /* Set the current message to STRING. */
11111
11112 static void
11113 set_message (Lisp_Object string)
11114 {
11115 eassert (STRINGP (string));
11116
11117 message_enable_multibyte = STRING_MULTIBYTE (string);
11118
11119 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11120 message_buf_print = false;
11121 help_echo_showing_p = false;
11122
11123 if (STRINGP (Vdebug_on_message)
11124 && STRINGP (string)
11125 && fast_string_match (Vdebug_on_message, string) >= 0)
11126 call_debugger (list2 (Qerror, string));
11127 }
11128
11129
11130 /* Helper function for set_message. First argument is ignored and second
11131 argument has the same meaning as for set_message.
11132 This function is called with the echo area buffer being current. */
11133
11134 static bool
11135 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11136 {
11137 eassert (STRINGP (string));
11138
11139 /* Change multibyteness of the echo buffer appropriately. */
11140 if (message_enable_multibyte
11141 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11142 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11143
11144 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11145 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11146 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11147
11148 /* Insert new message at BEG. */
11149 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11150
11151 /* This function takes care of single/multibyte conversion.
11152 We just have to ensure that the echo area buffer has the right
11153 setting of enable_multibyte_characters. */
11154 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11155
11156 return false;
11157 }
11158
11159
11160 /* Clear messages. CURRENT_P means clear the current message.
11161 LAST_DISPLAYED_P means clear the message last displayed. */
11162
11163 void
11164 clear_message (bool current_p, bool last_displayed_p)
11165 {
11166 if (current_p)
11167 {
11168 echo_area_buffer[0] = Qnil;
11169 message_cleared_p = true;
11170 }
11171
11172 if (last_displayed_p)
11173 echo_area_buffer[1] = Qnil;
11174
11175 message_buf_print = false;
11176 }
11177
11178 /* Clear garbaged frames.
11179
11180 This function is used where the old redisplay called
11181 redraw_garbaged_frames which in turn called redraw_frame which in
11182 turn called clear_frame. The call to clear_frame was a source of
11183 flickering. I believe a clear_frame is not necessary. It should
11184 suffice in the new redisplay to invalidate all current matrices,
11185 and ensure a complete redisplay of all windows. */
11186
11187 static void
11188 clear_garbaged_frames (void)
11189 {
11190 if (frame_garbaged)
11191 {
11192 Lisp_Object tail, frame;
11193
11194 FOR_EACH_FRAME (tail, frame)
11195 {
11196 struct frame *f = XFRAME (frame);
11197
11198 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11199 {
11200 if (f->resized_p)
11201 redraw_frame (f);
11202 else
11203 clear_current_matrices (f);
11204 fset_redisplay (f);
11205 f->garbaged = false;
11206 f->resized_p = false;
11207 }
11208 }
11209
11210 frame_garbaged = false;
11211 }
11212 }
11213
11214
11215 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11216 selected_frame. */
11217
11218 static void
11219 echo_area_display (bool update_frame_p)
11220 {
11221 Lisp_Object mini_window;
11222 struct window *w;
11223 struct frame *f;
11224 bool window_height_changed_p = false;
11225 struct frame *sf = SELECTED_FRAME ();
11226
11227 mini_window = FRAME_MINIBUF_WINDOW (sf);
11228 w = XWINDOW (mini_window);
11229 f = XFRAME (WINDOW_FRAME (w));
11230
11231 /* Don't display if frame is invisible or not yet initialized. */
11232 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11233 return;
11234
11235 #ifdef HAVE_WINDOW_SYSTEM
11236 /* When Emacs starts, selected_frame may be the initial terminal
11237 frame. If we let this through, a message would be displayed on
11238 the terminal. */
11239 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11240 return;
11241 #endif /* HAVE_WINDOW_SYSTEM */
11242
11243 /* Redraw garbaged frames. */
11244 clear_garbaged_frames ();
11245
11246 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11247 {
11248 echo_area_window = mini_window;
11249 window_height_changed_p = display_echo_area (w);
11250 w->must_be_updated_p = true;
11251
11252 /* Update the display, unless called from redisplay_internal.
11253 Also don't update the screen during redisplay itself. The
11254 update will happen at the end of redisplay, and an update
11255 here could cause confusion. */
11256 if (update_frame_p && !redisplaying_p)
11257 {
11258 int n = 0;
11259
11260 /* If the display update has been interrupted by pending
11261 input, update mode lines in the frame. Due to the
11262 pending input, it might have been that redisplay hasn't
11263 been called, so that mode lines above the echo area are
11264 garbaged. This looks odd, so we prevent it here. */
11265 if (!display_completed)
11266 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11267
11268 if (window_height_changed_p
11269 /* Don't do this if Emacs is shutting down. Redisplay
11270 needs to run hooks. */
11271 && !NILP (Vrun_hooks))
11272 {
11273 /* Must update other windows. Likewise as in other
11274 cases, don't let this update be interrupted by
11275 pending input. */
11276 ptrdiff_t count = SPECPDL_INDEX ();
11277 specbind (Qredisplay_dont_pause, Qt);
11278 fset_redisplay (f);
11279 redisplay_internal ();
11280 unbind_to (count, Qnil);
11281 }
11282 else if (FRAME_WINDOW_P (f) && n == 0)
11283 {
11284 /* Window configuration is the same as before.
11285 Can do with a display update of the echo area,
11286 unless we displayed some mode lines. */
11287 update_single_window (w);
11288 flush_frame (f);
11289 }
11290 else
11291 update_frame (f, true, true);
11292
11293 /* If cursor is in the echo area, make sure that the next
11294 redisplay displays the minibuffer, so that the cursor will
11295 be replaced with what the minibuffer wants. */
11296 if (cursor_in_echo_area)
11297 wset_redisplay (XWINDOW (mini_window));
11298 }
11299 }
11300 else if (!EQ (mini_window, selected_window))
11301 wset_redisplay (XWINDOW (mini_window));
11302
11303 /* Last displayed message is now the current message. */
11304 echo_area_buffer[1] = echo_area_buffer[0];
11305 /* Inform read_char that we're not echoing. */
11306 echo_message_buffer = Qnil;
11307
11308 /* Prevent redisplay optimization in redisplay_internal by resetting
11309 this_line_start_pos. This is done because the mini-buffer now
11310 displays the message instead of its buffer text. */
11311 if (EQ (mini_window, selected_window))
11312 CHARPOS (this_line_start_pos) = 0;
11313
11314 if (window_height_changed_p)
11315 {
11316 fset_redisplay (f);
11317
11318 /* If window configuration was changed, frames may have been
11319 marked garbaged. Clear them or we will experience
11320 surprises wrt scrolling.
11321 FIXME: How/why/when? */
11322 clear_garbaged_frames ();
11323 }
11324 }
11325
11326 /* True if W's buffer was changed but not saved. */
11327
11328 static bool
11329 window_buffer_changed (struct window *w)
11330 {
11331 struct buffer *b = XBUFFER (w->contents);
11332
11333 eassert (BUFFER_LIVE_P (b));
11334
11335 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11336 }
11337
11338 /* True if W has %c in its mode line and mode line should be updated. */
11339
11340 static bool
11341 mode_line_update_needed (struct window *w)
11342 {
11343 return (w->column_number_displayed != -1
11344 && !(PT == w->last_point && !window_outdated (w))
11345 && (w->column_number_displayed != current_column ()));
11346 }
11347
11348 /* True if window start of W is frozen and may not be changed during
11349 redisplay. */
11350
11351 static bool
11352 window_frozen_p (struct window *w)
11353 {
11354 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11355 {
11356 Lisp_Object window;
11357
11358 XSETWINDOW (window, w);
11359 if (MINI_WINDOW_P (w))
11360 return false;
11361 else if (EQ (window, selected_window))
11362 return false;
11363 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11364 && EQ (window, Vminibuf_scroll_window))
11365 /* This special window can't be frozen too. */
11366 return false;
11367 else
11368 return true;
11369 }
11370 return false;
11371 }
11372
11373 /***********************************************************************
11374 Mode Lines and Frame Titles
11375 ***********************************************************************/
11376
11377 /* A buffer for constructing non-propertized mode-line strings and
11378 frame titles in it; allocated from the heap in init_xdisp and
11379 resized as needed in store_mode_line_noprop_char. */
11380
11381 static char *mode_line_noprop_buf;
11382
11383 /* The buffer's end, and a current output position in it. */
11384
11385 static char *mode_line_noprop_buf_end;
11386 static char *mode_line_noprop_ptr;
11387
11388 #define MODE_LINE_NOPROP_LEN(start) \
11389 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11390
11391 static enum {
11392 MODE_LINE_DISPLAY = 0,
11393 MODE_LINE_TITLE,
11394 MODE_LINE_NOPROP,
11395 MODE_LINE_STRING
11396 } mode_line_target;
11397
11398 /* Alist that caches the results of :propertize.
11399 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11400 static Lisp_Object mode_line_proptrans_alist;
11401
11402 /* List of strings making up the mode-line. */
11403 static Lisp_Object mode_line_string_list;
11404
11405 /* Base face property when building propertized mode line string. */
11406 static Lisp_Object mode_line_string_face;
11407 static Lisp_Object mode_line_string_face_prop;
11408
11409
11410 /* Unwind data for mode line strings */
11411
11412 static Lisp_Object Vmode_line_unwind_vector;
11413
11414 static Lisp_Object
11415 format_mode_line_unwind_data (struct frame *target_frame,
11416 struct buffer *obuf,
11417 Lisp_Object owin,
11418 bool save_proptrans)
11419 {
11420 Lisp_Object vector, tmp;
11421
11422 /* Reduce consing by keeping one vector in
11423 Vwith_echo_area_save_vector. */
11424 vector = Vmode_line_unwind_vector;
11425 Vmode_line_unwind_vector = Qnil;
11426
11427 if (NILP (vector))
11428 vector = Fmake_vector (make_number (10), Qnil);
11429
11430 ASET (vector, 0, make_number (mode_line_target));
11431 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11432 ASET (vector, 2, mode_line_string_list);
11433 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11434 ASET (vector, 4, mode_line_string_face);
11435 ASET (vector, 5, mode_line_string_face_prop);
11436
11437 if (obuf)
11438 XSETBUFFER (tmp, obuf);
11439 else
11440 tmp = Qnil;
11441 ASET (vector, 6, tmp);
11442 ASET (vector, 7, owin);
11443 if (target_frame)
11444 {
11445 /* Similarly to `with-selected-window', if the operation selects
11446 a window on another frame, we must restore that frame's
11447 selected window, and (for a tty) the top-frame. */
11448 ASET (vector, 8, target_frame->selected_window);
11449 if (FRAME_TERMCAP_P (target_frame))
11450 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11451 }
11452
11453 return vector;
11454 }
11455
11456 static void
11457 unwind_format_mode_line (Lisp_Object vector)
11458 {
11459 Lisp_Object old_window = AREF (vector, 7);
11460 Lisp_Object target_frame_window = AREF (vector, 8);
11461 Lisp_Object old_top_frame = AREF (vector, 9);
11462
11463 mode_line_target = XINT (AREF (vector, 0));
11464 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11465 mode_line_string_list = AREF (vector, 2);
11466 if (! EQ (AREF (vector, 3), Qt))
11467 mode_line_proptrans_alist = AREF (vector, 3);
11468 mode_line_string_face = AREF (vector, 4);
11469 mode_line_string_face_prop = AREF (vector, 5);
11470
11471 /* Select window before buffer, since it may change the buffer. */
11472 if (!NILP (old_window))
11473 {
11474 /* If the operation that we are unwinding had selected a window
11475 on a different frame, reset its frame-selected-window. For a
11476 text terminal, reset its top-frame if necessary. */
11477 if (!NILP (target_frame_window))
11478 {
11479 Lisp_Object frame
11480 = WINDOW_FRAME (XWINDOW (target_frame_window));
11481
11482 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11483 Fselect_window (target_frame_window, Qt);
11484
11485 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11486 Fselect_frame (old_top_frame, Qt);
11487 }
11488
11489 Fselect_window (old_window, Qt);
11490 }
11491
11492 if (!NILP (AREF (vector, 6)))
11493 {
11494 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11495 ASET (vector, 6, Qnil);
11496 }
11497
11498 Vmode_line_unwind_vector = vector;
11499 }
11500
11501
11502 /* Store a single character C for the frame title in mode_line_noprop_buf.
11503 Re-allocate mode_line_noprop_buf if necessary. */
11504
11505 static void
11506 store_mode_line_noprop_char (char c)
11507 {
11508 /* If output position has reached the end of the allocated buffer,
11509 increase the buffer's size. */
11510 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11511 {
11512 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11513 ptrdiff_t size = len;
11514 mode_line_noprop_buf =
11515 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11516 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11517 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11518 }
11519
11520 *mode_line_noprop_ptr++ = c;
11521 }
11522
11523
11524 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11525 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11526 characters that yield more columns than PRECISION; PRECISION <= 0
11527 means copy the whole string. Pad with spaces until FIELD_WIDTH
11528 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11529 pad. Called from display_mode_element when it is used to build a
11530 frame title. */
11531
11532 static int
11533 store_mode_line_noprop (const char *string, int field_width, int precision)
11534 {
11535 const unsigned char *str = (const unsigned char *) string;
11536 int n = 0;
11537 ptrdiff_t dummy, nbytes;
11538
11539 /* Copy at most PRECISION chars from STR. */
11540 nbytes = strlen (string);
11541 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11542 while (nbytes--)
11543 store_mode_line_noprop_char (*str++);
11544
11545 /* Fill up with spaces until FIELD_WIDTH reached. */
11546 while (field_width > 0
11547 && n < field_width)
11548 {
11549 store_mode_line_noprop_char (' ');
11550 ++n;
11551 }
11552
11553 return n;
11554 }
11555
11556 /***********************************************************************
11557 Frame Titles
11558 ***********************************************************************/
11559
11560 #ifdef HAVE_WINDOW_SYSTEM
11561
11562 /* Set the title of FRAME, if it has changed. The title format is
11563 Vicon_title_format if FRAME is iconified, otherwise it is
11564 frame_title_format. */
11565
11566 static void
11567 x_consider_frame_title (Lisp_Object frame)
11568 {
11569 struct frame *f = XFRAME (frame);
11570
11571 if ((FRAME_WINDOW_P (f)
11572 || FRAME_MINIBUF_ONLY_P (f)
11573 || f->explicit_name)
11574 && NILP (Fframe_parameter (frame, Qtooltip)))
11575 {
11576 /* Do we have more than one visible frame on this X display? */
11577 Lisp_Object tail, other_frame, fmt;
11578 ptrdiff_t title_start;
11579 char *title;
11580 ptrdiff_t len;
11581 struct it it;
11582 ptrdiff_t count = SPECPDL_INDEX ();
11583
11584 FOR_EACH_FRAME (tail, other_frame)
11585 {
11586 struct frame *tf = XFRAME (other_frame);
11587
11588 if (tf != f
11589 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11590 && !FRAME_MINIBUF_ONLY_P (tf)
11591 && !EQ (other_frame, tip_frame)
11592 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11593 break;
11594 }
11595
11596 /* Set global variable indicating that multiple frames exist. */
11597 multiple_frames = CONSP (tail);
11598
11599 /* Switch to the buffer of selected window of the frame. Set up
11600 mode_line_target so that display_mode_element will output into
11601 mode_line_noprop_buf; then display the title. */
11602 record_unwind_protect (unwind_format_mode_line,
11603 format_mode_line_unwind_data
11604 (f, current_buffer, selected_window, false));
11605
11606 Fselect_window (f->selected_window, Qt);
11607 set_buffer_internal_1
11608 (XBUFFER (XWINDOW (f->selected_window)->contents));
11609 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11610
11611 mode_line_target = MODE_LINE_TITLE;
11612 title_start = MODE_LINE_NOPROP_LEN (0);
11613 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11614 NULL, DEFAULT_FACE_ID);
11615 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11616 len = MODE_LINE_NOPROP_LEN (title_start);
11617 title = mode_line_noprop_buf + title_start;
11618 unbind_to (count, Qnil);
11619
11620 /* Set the title only if it's changed. This avoids consing in
11621 the common case where it hasn't. (If it turns out that we've
11622 already wasted too much time by walking through the list with
11623 display_mode_element, then we might need to optimize at a
11624 higher level than this.) */
11625 if (! STRINGP (f->name)
11626 || SBYTES (f->name) != len
11627 || memcmp (title, SDATA (f->name), len) != 0)
11628 x_implicitly_set_name (f, make_string (title, len), Qnil);
11629 }
11630 }
11631
11632 #endif /* not HAVE_WINDOW_SYSTEM */
11633
11634 \f
11635 /***********************************************************************
11636 Menu Bars
11637 ***********************************************************************/
11638
11639 /* True if we will not redisplay all visible windows. */
11640 #define REDISPLAY_SOME_P() \
11641 ((windows_or_buffers_changed == 0 \
11642 || windows_or_buffers_changed == REDISPLAY_SOME) \
11643 && (update_mode_lines == 0 \
11644 || update_mode_lines == REDISPLAY_SOME))
11645
11646 /* Prepare for redisplay by updating menu-bar item lists when
11647 appropriate. This can call eval. */
11648
11649 static void
11650 prepare_menu_bars (void)
11651 {
11652 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11653 bool some_windows = REDISPLAY_SOME_P ();
11654 Lisp_Object tooltip_frame;
11655
11656 #ifdef HAVE_WINDOW_SYSTEM
11657 tooltip_frame = tip_frame;
11658 #else
11659 tooltip_frame = Qnil;
11660 #endif
11661
11662 if (FUNCTIONP (Vpre_redisplay_function))
11663 {
11664 Lisp_Object windows = all_windows ? Qt : Qnil;
11665 if (all_windows && some_windows)
11666 {
11667 Lisp_Object ws = window_list ();
11668 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11669 {
11670 Lisp_Object this = XCAR (ws);
11671 struct window *w = XWINDOW (this);
11672 if (w->redisplay
11673 || XFRAME (w->frame)->redisplay
11674 || XBUFFER (w->contents)->text->redisplay)
11675 {
11676 windows = Fcons (this, windows);
11677 }
11678 }
11679 }
11680 safe__call1 (true, Vpre_redisplay_function, windows);
11681 }
11682
11683 /* Update all frame titles based on their buffer names, etc. We do
11684 this before the menu bars so that the buffer-menu will show the
11685 up-to-date frame titles. */
11686 #ifdef HAVE_WINDOW_SYSTEM
11687 if (all_windows)
11688 {
11689 Lisp_Object tail, frame;
11690
11691 FOR_EACH_FRAME (tail, frame)
11692 {
11693 struct frame *f = XFRAME (frame);
11694 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11695 if (some_windows
11696 && !f->redisplay
11697 && !w->redisplay
11698 && !XBUFFER (w->contents)->text->redisplay)
11699 continue;
11700
11701 if (!EQ (frame, tooltip_frame)
11702 && (FRAME_ICONIFIED_P (f)
11703 || FRAME_VISIBLE_P (f) == 1
11704 /* Exclude TTY frames that are obscured because they
11705 are not the top frame on their console. This is
11706 because x_consider_frame_title actually switches
11707 to the frame, which for TTY frames means it is
11708 marked as garbaged, and will be completely
11709 redrawn on the next redisplay cycle. This causes
11710 TTY frames to be completely redrawn, when there
11711 are more than one of them, even though nothing
11712 should be changed on display. */
11713 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11714 x_consider_frame_title (frame);
11715 }
11716 }
11717 #endif /* HAVE_WINDOW_SYSTEM */
11718
11719 /* Update the menu bar item lists, if appropriate. This has to be
11720 done before any actual redisplay or generation of display lines. */
11721
11722 if (all_windows)
11723 {
11724 Lisp_Object tail, frame;
11725 ptrdiff_t count = SPECPDL_INDEX ();
11726 /* True means that update_menu_bar has run its hooks
11727 so any further calls to update_menu_bar shouldn't do so again. */
11728 bool menu_bar_hooks_run = false;
11729
11730 record_unwind_save_match_data ();
11731
11732 FOR_EACH_FRAME (tail, frame)
11733 {
11734 struct frame *f = XFRAME (frame);
11735 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11736
11737 /* Ignore tooltip frame. */
11738 if (EQ (frame, tooltip_frame))
11739 continue;
11740
11741 if (some_windows
11742 && !f->redisplay
11743 && !w->redisplay
11744 && !XBUFFER (w->contents)->text->redisplay)
11745 continue;
11746
11747 /* If a window on this frame changed size, report that to
11748 the user and clear the size-change flag. */
11749 if (FRAME_WINDOW_SIZES_CHANGED (f))
11750 {
11751 Lisp_Object functions;
11752
11753 /* Clear flag first in case we get an error below. */
11754 FRAME_WINDOW_SIZES_CHANGED (f) = false;
11755 functions = Vwindow_size_change_functions;
11756
11757 while (CONSP (functions))
11758 {
11759 if (!EQ (XCAR (functions), Qt))
11760 call1 (XCAR (functions), frame);
11761 functions = XCDR (functions);
11762 }
11763 }
11764
11765 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11766 #ifdef HAVE_WINDOW_SYSTEM
11767 update_tool_bar (f, false);
11768 #endif
11769 }
11770
11771 unbind_to (count, Qnil);
11772 }
11773 else
11774 {
11775 struct frame *sf = SELECTED_FRAME ();
11776 update_menu_bar (sf, true, false);
11777 #ifdef HAVE_WINDOW_SYSTEM
11778 update_tool_bar (sf, true);
11779 #endif
11780 }
11781 }
11782
11783
11784 /* Update the menu bar item list for frame F. This has to be done
11785 before we start to fill in any display lines, because it can call
11786 eval.
11787
11788 If SAVE_MATCH_DATA, we must save and restore it here.
11789
11790 If HOOKS_RUN, a previous call to update_menu_bar
11791 already ran the menu bar hooks for this redisplay, so there
11792 is no need to run them again. The return value is the
11793 updated value of this flag, to pass to the next call. */
11794
11795 static bool
11796 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11797 {
11798 Lisp_Object window;
11799 struct window *w;
11800
11801 /* If called recursively during a menu update, do nothing. This can
11802 happen when, for instance, an activate-menubar-hook causes a
11803 redisplay. */
11804 if (inhibit_menubar_update)
11805 return hooks_run;
11806
11807 window = FRAME_SELECTED_WINDOW (f);
11808 w = XWINDOW (window);
11809
11810 if (FRAME_WINDOW_P (f)
11811 ?
11812 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11813 || defined (HAVE_NS) || defined (USE_GTK)
11814 FRAME_EXTERNAL_MENU_BAR (f)
11815 #else
11816 FRAME_MENU_BAR_LINES (f) > 0
11817 #endif
11818 : FRAME_MENU_BAR_LINES (f) > 0)
11819 {
11820 /* If the user has switched buffers or windows, we need to
11821 recompute to reflect the new bindings. But we'll
11822 recompute when update_mode_lines is set too; that means
11823 that people can use force-mode-line-update to request
11824 that the menu bar be recomputed. The adverse effect on
11825 the rest of the redisplay algorithm is about the same as
11826 windows_or_buffers_changed anyway. */
11827 if (windows_or_buffers_changed
11828 /* This used to test w->update_mode_line, but we believe
11829 there is no need to recompute the menu in that case. */
11830 || update_mode_lines
11831 || window_buffer_changed (w))
11832 {
11833 struct buffer *prev = current_buffer;
11834 ptrdiff_t count = SPECPDL_INDEX ();
11835
11836 specbind (Qinhibit_menubar_update, Qt);
11837
11838 set_buffer_internal_1 (XBUFFER (w->contents));
11839 if (save_match_data)
11840 record_unwind_save_match_data ();
11841 if (NILP (Voverriding_local_map_menu_flag))
11842 {
11843 specbind (Qoverriding_terminal_local_map, Qnil);
11844 specbind (Qoverriding_local_map, Qnil);
11845 }
11846
11847 if (!hooks_run)
11848 {
11849 /* Run the Lucid hook. */
11850 safe_run_hooks (Qactivate_menubar_hook);
11851
11852 /* If it has changed current-menubar from previous value,
11853 really recompute the menu-bar from the value. */
11854 if (! NILP (Vlucid_menu_bar_dirty_flag))
11855 call0 (Qrecompute_lucid_menubar);
11856
11857 safe_run_hooks (Qmenu_bar_update_hook);
11858
11859 hooks_run = true;
11860 }
11861
11862 XSETFRAME (Vmenu_updating_frame, f);
11863 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11864
11865 /* Redisplay the menu bar in case we changed it. */
11866 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11867 || defined (HAVE_NS) || defined (USE_GTK)
11868 if (FRAME_WINDOW_P (f))
11869 {
11870 #if defined (HAVE_NS)
11871 /* All frames on Mac OS share the same menubar. So only
11872 the selected frame should be allowed to set it. */
11873 if (f == SELECTED_FRAME ())
11874 #endif
11875 set_frame_menubar (f, false, false);
11876 }
11877 else
11878 /* On a terminal screen, the menu bar is an ordinary screen
11879 line, and this makes it get updated. */
11880 w->update_mode_line = true;
11881 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11882 /* In the non-toolkit version, the menu bar is an ordinary screen
11883 line, and this makes it get updated. */
11884 w->update_mode_line = true;
11885 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11886
11887 unbind_to (count, Qnil);
11888 set_buffer_internal_1 (prev);
11889 }
11890 }
11891
11892 return hooks_run;
11893 }
11894
11895 /***********************************************************************
11896 Tool-bars
11897 ***********************************************************************/
11898
11899 #ifdef HAVE_WINDOW_SYSTEM
11900
11901 /* Select `frame' temporarily without running all the code in
11902 do_switch_frame.
11903 FIXME: Maybe do_switch_frame should be trimmed down similarly
11904 when `norecord' is set. */
11905 static void
11906 fast_set_selected_frame (Lisp_Object frame)
11907 {
11908 if (!EQ (selected_frame, frame))
11909 {
11910 selected_frame = frame;
11911 selected_window = XFRAME (frame)->selected_window;
11912 }
11913 }
11914
11915 /* Update the tool-bar item list for frame F. This has to be done
11916 before we start to fill in any display lines. Called from
11917 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
11918 and restore it here. */
11919
11920 static void
11921 update_tool_bar (struct frame *f, bool save_match_data)
11922 {
11923 #if defined (USE_GTK) || defined (HAVE_NS)
11924 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11925 #else
11926 bool do_update = (WINDOWP (f->tool_bar_window)
11927 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11928 #endif
11929
11930 if (do_update)
11931 {
11932 Lisp_Object window;
11933 struct window *w;
11934
11935 window = FRAME_SELECTED_WINDOW (f);
11936 w = XWINDOW (window);
11937
11938 /* If the user has switched buffers or windows, we need to
11939 recompute to reflect the new bindings. But we'll
11940 recompute when update_mode_lines is set too; that means
11941 that people can use force-mode-line-update to request
11942 that the menu bar be recomputed. The adverse effect on
11943 the rest of the redisplay algorithm is about the same as
11944 windows_or_buffers_changed anyway. */
11945 if (windows_or_buffers_changed
11946 || w->update_mode_line
11947 || update_mode_lines
11948 || window_buffer_changed (w))
11949 {
11950 struct buffer *prev = current_buffer;
11951 ptrdiff_t count = SPECPDL_INDEX ();
11952 Lisp_Object frame, new_tool_bar;
11953 int new_n_tool_bar;
11954
11955 /* Set current_buffer to the buffer of the selected
11956 window of the frame, so that we get the right local
11957 keymaps. */
11958 set_buffer_internal_1 (XBUFFER (w->contents));
11959
11960 /* Save match data, if we must. */
11961 if (save_match_data)
11962 record_unwind_save_match_data ();
11963
11964 /* Make sure that we don't accidentally use bogus keymaps. */
11965 if (NILP (Voverriding_local_map_menu_flag))
11966 {
11967 specbind (Qoverriding_terminal_local_map, Qnil);
11968 specbind (Qoverriding_local_map, Qnil);
11969 }
11970
11971 /* We must temporarily set the selected frame to this frame
11972 before calling tool_bar_items, because the calculation of
11973 the tool-bar keymap uses the selected frame (see
11974 `tool-bar-make-keymap' in tool-bar.el). */
11975 eassert (EQ (selected_window,
11976 /* Since we only explicitly preserve selected_frame,
11977 check that selected_window would be redundant. */
11978 XFRAME (selected_frame)->selected_window));
11979 record_unwind_protect (fast_set_selected_frame, selected_frame);
11980 XSETFRAME (frame, f);
11981 fast_set_selected_frame (frame);
11982
11983 /* Build desired tool-bar items from keymaps. */
11984 new_tool_bar
11985 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11986 &new_n_tool_bar);
11987
11988 /* Redisplay the tool-bar if we changed it. */
11989 if (new_n_tool_bar != f->n_tool_bar_items
11990 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11991 {
11992 /* Redisplay that happens asynchronously due to an expose event
11993 may access f->tool_bar_items. Make sure we update both
11994 variables within BLOCK_INPUT so no such event interrupts. */
11995 block_input ();
11996 fset_tool_bar_items (f, new_tool_bar);
11997 f->n_tool_bar_items = new_n_tool_bar;
11998 w->update_mode_line = true;
11999 unblock_input ();
12000 }
12001
12002 unbind_to (count, Qnil);
12003 set_buffer_internal_1 (prev);
12004 }
12005 }
12006 }
12007
12008 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12009
12010 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12011 F's desired tool-bar contents. F->tool_bar_items must have
12012 been set up previously by calling prepare_menu_bars. */
12013
12014 static void
12015 build_desired_tool_bar_string (struct frame *f)
12016 {
12017 int i, size, size_needed;
12018 Lisp_Object image, plist;
12019
12020 image = plist = Qnil;
12021
12022 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12023 Otherwise, make a new string. */
12024
12025 /* The size of the string we might be able to reuse. */
12026 size = (STRINGP (f->desired_tool_bar_string)
12027 ? SCHARS (f->desired_tool_bar_string)
12028 : 0);
12029
12030 /* We need one space in the string for each image. */
12031 size_needed = f->n_tool_bar_items;
12032
12033 /* Reuse f->desired_tool_bar_string, if possible. */
12034 if (size < size_needed || NILP (f->desired_tool_bar_string))
12035 fset_desired_tool_bar_string
12036 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12037 else
12038 {
12039 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12040 Fremove_text_properties (make_number (0), make_number (size),
12041 props, f->desired_tool_bar_string);
12042 }
12043
12044 /* Put a `display' property on the string for the images to display,
12045 put a `menu_item' property on tool-bar items with a value that
12046 is the index of the item in F's tool-bar item vector. */
12047 for (i = 0; i < f->n_tool_bar_items; ++i)
12048 {
12049 #define PROP(IDX) \
12050 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12051
12052 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12053 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12054 int hmargin, vmargin, relief, idx, end;
12055
12056 /* If image is a vector, choose the image according to the
12057 button state. */
12058 image = PROP (TOOL_BAR_ITEM_IMAGES);
12059 if (VECTORP (image))
12060 {
12061 if (enabled_p)
12062 idx = (selected_p
12063 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12064 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12065 else
12066 idx = (selected_p
12067 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12068 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12069
12070 eassert (ASIZE (image) >= idx);
12071 image = AREF (image, idx);
12072 }
12073 else
12074 idx = -1;
12075
12076 /* Ignore invalid image specifications. */
12077 if (!valid_image_p (image))
12078 continue;
12079
12080 /* Display the tool-bar button pressed, or depressed. */
12081 plist = Fcopy_sequence (XCDR (image));
12082
12083 /* Compute margin and relief to draw. */
12084 relief = (tool_bar_button_relief >= 0
12085 ? tool_bar_button_relief
12086 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12087 hmargin = vmargin = relief;
12088
12089 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12090 INT_MAX - max (hmargin, vmargin)))
12091 {
12092 hmargin += XFASTINT (Vtool_bar_button_margin);
12093 vmargin += XFASTINT (Vtool_bar_button_margin);
12094 }
12095 else if (CONSP (Vtool_bar_button_margin))
12096 {
12097 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12098 INT_MAX - hmargin))
12099 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12100
12101 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12102 INT_MAX - vmargin))
12103 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12104 }
12105
12106 if (auto_raise_tool_bar_buttons_p)
12107 {
12108 /* Add a `:relief' property to the image spec if the item is
12109 selected. */
12110 if (selected_p)
12111 {
12112 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12113 hmargin -= relief;
12114 vmargin -= relief;
12115 }
12116 }
12117 else
12118 {
12119 /* If image is selected, display it pressed, i.e. with a
12120 negative relief. If it's not selected, display it with a
12121 raised relief. */
12122 plist = Fplist_put (plist, QCrelief,
12123 (selected_p
12124 ? make_number (-relief)
12125 : make_number (relief)));
12126 hmargin -= relief;
12127 vmargin -= relief;
12128 }
12129
12130 /* Put a margin around the image. */
12131 if (hmargin || vmargin)
12132 {
12133 if (hmargin == vmargin)
12134 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12135 else
12136 plist = Fplist_put (plist, QCmargin,
12137 Fcons (make_number (hmargin),
12138 make_number (vmargin)));
12139 }
12140
12141 /* If button is not enabled, and we don't have special images
12142 for the disabled state, make the image appear disabled by
12143 applying an appropriate algorithm to it. */
12144 if (!enabled_p && idx < 0)
12145 plist = Fplist_put (plist, QCconversion, Qdisabled);
12146
12147 /* Put a `display' text property on the string for the image to
12148 display. Put a `menu-item' property on the string that gives
12149 the start of this item's properties in the tool-bar items
12150 vector. */
12151 image = Fcons (Qimage, plist);
12152 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12153 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12154
12155 /* Let the last image hide all remaining spaces in the tool bar
12156 string. The string can be longer than needed when we reuse a
12157 previous string. */
12158 if (i + 1 == f->n_tool_bar_items)
12159 end = SCHARS (f->desired_tool_bar_string);
12160 else
12161 end = i + 1;
12162 Fadd_text_properties (make_number (i), make_number (end),
12163 props, f->desired_tool_bar_string);
12164 #undef PROP
12165 }
12166 }
12167
12168
12169 /* Display one line of the tool-bar of frame IT->f.
12170
12171 HEIGHT specifies the desired height of the tool-bar line.
12172 If the actual height of the glyph row is less than HEIGHT, the
12173 row's height is increased to HEIGHT, and the icons are centered
12174 vertically in the new height.
12175
12176 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12177 count a final empty row in case the tool-bar width exactly matches
12178 the window width.
12179 */
12180
12181 static void
12182 display_tool_bar_line (struct it *it, int height)
12183 {
12184 struct glyph_row *row = it->glyph_row;
12185 int max_x = it->last_visible_x;
12186 struct glyph *last;
12187
12188 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12189 clear_glyph_row (row);
12190 row->enabled_p = true;
12191 row->y = it->current_y;
12192
12193 /* Note that this isn't made use of if the face hasn't a box,
12194 so there's no need to check the face here. */
12195 it->start_of_box_run_p = true;
12196
12197 while (it->current_x < max_x)
12198 {
12199 int x, n_glyphs_before, i, nglyphs;
12200 struct it it_before;
12201
12202 /* Get the next display element. */
12203 if (!get_next_display_element (it))
12204 {
12205 /* Don't count empty row if we are counting needed tool-bar lines. */
12206 if (height < 0 && !it->hpos)
12207 return;
12208 break;
12209 }
12210
12211 /* Produce glyphs. */
12212 n_glyphs_before = row->used[TEXT_AREA];
12213 it_before = *it;
12214
12215 PRODUCE_GLYPHS (it);
12216
12217 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12218 i = 0;
12219 x = it_before.current_x;
12220 while (i < nglyphs)
12221 {
12222 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12223
12224 if (x + glyph->pixel_width > max_x)
12225 {
12226 /* Glyph doesn't fit on line. Backtrack. */
12227 row->used[TEXT_AREA] = n_glyphs_before;
12228 *it = it_before;
12229 /* If this is the only glyph on this line, it will never fit on the
12230 tool-bar, so skip it. But ensure there is at least one glyph,
12231 so we don't accidentally disable the tool-bar. */
12232 if (n_glyphs_before == 0
12233 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12234 break;
12235 goto out;
12236 }
12237
12238 ++it->hpos;
12239 x += glyph->pixel_width;
12240 ++i;
12241 }
12242
12243 /* Stop at line end. */
12244 if (ITERATOR_AT_END_OF_LINE_P (it))
12245 break;
12246
12247 set_iterator_to_next (it, true);
12248 }
12249
12250 out:;
12251
12252 row->displays_text_p = row->used[TEXT_AREA] != 0;
12253
12254 /* Use default face for the border below the tool bar.
12255
12256 FIXME: When auto-resize-tool-bars is grow-only, there is
12257 no additional border below the possibly empty tool-bar lines.
12258 So to make the extra empty lines look "normal", we have to
12259 use the tool-bar face for the border too. */
12260 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12261 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12262 it->face_id = DEFAULT_FACE_ID;
12263
12264 extend_face_to_end_of_line (it);
12265 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12266 last->right_box_line_p = true;
12267 if (last == row->glyphs[TEXT_AREA])
12268 last->left_box_line_p = true;
12269
12270 /* Make line the desired height and center it vertically. */
12271 if ((height -= it->max_ascent + it->max_descent) > 0)
12272 {
12273 /* Don't add more than one line height. */
12274 height %= FRAME_LINE_HEIGHT (it->f);
12275 it->max_ascent += height / 2;
12276 it->max_descent += (height + 1) / 2;
12277 }
12278
12279 compute_line_metrics (it);
12280
12281 /* If line is empty, make it occupy the rest of the tool-bar. */
12282 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12283 {
12284 row->height = row->phys_height = it->last_visible_y - row->y;
12285 row->visible_height = row->height;
12286 row->ascent = row->phys_ascent = 0;
12287 row->extra_line_spacing = 0;
12288 }
12289
12290 row->full_width_p = true;
12291 row->continued_p = false;
12292 row->truncated_on_left_p = false;
12293 row->truncated_on_right_p = false;
12294
12295 it->current_x = it->hpos = 0;
12296 it->current_y += row->height;
12297 ++it->vpos;
12298 ++it->glyph_row;
12299 }
12300
12301
12302 /* Value is the number of pixels needed to make all tool-bar items of
12303 frame F visible. The actual number of glyph rows needed is
12304 returned in *N_ROWS if non-NULL. */
12305 static int
12306 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12307 {
12308 struct window *w = XWINDOW (f->tool_bar_window);
12309 struct it it;
12310 /* tool_bar_height is called from redisplay_tool_bar after building
12311 the desired matrix, so use (unused) mode-line row as temporary row to
12312 avoid destroying the first tool-bar row. */
12313 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12314
12315 /* Initialize an iterator for iteration over
12316 F->desired_tool_bar_string in the tool-bar window of frame F. */
12317 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12318 temp_row->reversed_p = false;
12319 it.first_visible_x = 0;
12320 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12321 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12322 it.paragraph_embedding = L2R;
12323
12324 while (!ITERATOR_AT_END_P (&it))
12325 {
12326 clear_glyph_row (temp_row);
12327 it.glyph_row = temp_row;
12328 display_tool_bar_line (&it, -1);
12329 }
12330 clear_glyph_row (temp_row);
12331
12332 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12333 if (n_rows)
12334 *n_rows = it.vpos > 0 ? it.vpos : -1;
12335
12336 if (pixelwise)
12337 return it.current_y;
12338 else
12339 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12340 }
12341
12342 #endif /* !USE_GTK && !HAVE_NS */
12343
12344 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12345 0, 2, 0,
12346 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12347 If FRAME is nil or omitted, use the selected frame. Optional argument
12348 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12349 (Lisp_Object frame, Lisp_Object pixelwise)
12350 {
12351 int height = 0;
12352
12353 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12354 struct frame *f = decode_any_frame (frame);
12355
12356 if (WINDOWP (f->tool_bar_window)
12357 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12358 {
12359 update_tool_bar (f, true);
12360 if (f->n_tool_bar_items)
12361 {
12362 build_desired_tool_bar_string (f);
12363 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12364 }
12365 }
12366 #endif
12367
12368 return make_number (height);
12369 }
12370
12371
12372 /* Display the tool-bar of frame F. Value is true if tool-bar's
12373 height should be changed. */
12374 static bool
12375 redisplay_tool_bar (struct frame *f)
12376 {
12377 f->tool_bar_redisplayed = true;
12378 #if defined (USE_GTK) || defined (HAVE_NS)
12379
12380 if (FRAME_EXTERNAL_TOOL_BAR (f))
12381 update_frame_tool_bar (f);
12382 return false;
12383
12384 #else /* !USE_GTK && !HAVE_NS */
12385
12386 struct window *w;
12387 struct it it;
12388 struct glyph_row *row;
12389
12390 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12391 do anything. This means you must start with tool-bar-lines
12392 non-zero to get the auto-sizing effect. Or in other words, you
12393 can turn off tool-bars by specifying tool-bar-lines zero. */
12394 if (!WINDOWP (f->tool_bar_window)
12395 || (w = XWINDOW (f->tool_bar_window),
12396 WINDOW_TOTAL_LINES (w) == 0))
12397 return false;
12398
12399 /* Set up an iterator for the tool-bar window. */
12400 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12401 it.first_visible_x = 0;
12402 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12403 row = it.glyph_row;
12404 row->reversed_p = false;
12405
12406 /* Build a string that represents the contents of the tool-bar. */
12407 build_desired_tool_bar_string (f);
12408 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12409 /* FIXME: This should be controlled by a user option. But it
12410 doesn't make sense to have an R2L tool bar if the menu bar cannot
12411 be drawn also R2L, and making the menu bar R2L is tricky due
12412 toolkit-specific code that implements it. If an R2L tool bar is
12413 ever supported, display_tool_bar_line should also be augmented to
12414 call unproduce_glyphs like display_line and display_string
12415 do. */
12416 it.paragraph_embedding = L2R;
12417
12418 if (f->n_tool_bar_rows == 0)
12419 {
12420 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12421
12422 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12423 {
12424 x_change_tool_bar_height (f, new_height);
12425 frame_default_tool_bar_height = new_height;
12426 /* Always do that now. */
12427 clear_glyph_matrix (w->desired_matrix);
12428 f->fonts_changed = true;
12429 return true;
12430 }
12431 }
12432
12433 /* Display as many lines as needed to display all tool-bar items. */
12434
12435 if (f->n_tool_bar_rows > 0)
12436 {
12437 int border, rows, height, extra;
12438
12439 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12440 border = XINT (Vtool_bar_border);
12441 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12442 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12443 else if (EQ (Vtool_bar_border, Qborder_width))
12444 border = f->border_width;
12445 else
12446 border = 0;
12447 if (border < 0)
12448 border = 0;
12449
12450 rows = f->n_tool_bar_rows;
12451 height = max (1, (it.last_visible_y - border) / rows);
12452 extra = it.last_visible_y - border - height * rows;
12453
12454 while (it.current_y < it.last_visible_y)
12455 {
12456 int h = 0;
12457 if (extra > 0 && rows-- > 0)
12458 {
12459 h = (extra + rows - 1) / rows;
12460 extra -= h;
12461 }
12462 display_tool_bar_line (&it, height + h);
12463 }
12464 }
12465 else
12466 {
12467 while (it.current_y < it.last_visible_y)
12468 display_tool_bar_line (&it, 0);
12469 }
12470
12471 /* It doesn't make much sense to try scrolling in the tool-bar
12472 window, so don't do it. */
12473 w->desired_matrix->no_scrolling_p = true;
12474 w->must_be_updated_p = true;
12475
12476 if (!NILP (Vauto_resize_tool_bars))
12477 {
12478 bool change_height_p = true;
12479
12480 /* If we couldn't display everything, change the tool-bar's
12481 height if there is room for more. */
12482 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12483 change_height_p = true;
12484
12485 /* We subtract 1 because display_tool_bar_line advances the
12486 glyph_row pointer before returning to its caller. We want to
12487 examine the last glyph row produced by
12488 display_tool_bar_line. */
12489 row = it.glyph_row - 1;
12490
12491 /* If there are blank lines at the end, except for a partially
12492 visible blank line at the end that is smaller than
12493 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12494 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12495 && row->height >= FRAME_LINE_HEIGHT (f))
12496 change_height_p = true;
12497
12498 /* If row displays tool-bar items, but is partially visible,
12499 change the tool-bar's height. */
12500 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12501 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12502 change_height_p = true;
12503
12504 /* Resize windows as needed by changing the `tool-bar-lines'
12505 frame parameter. */
12506 if (change_height_p)
12507 {
12508 int nrows;
12509 int new_height = tool_bar_height (f, &nrows, true);
12510
12511 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12512 && !f->minimize_tool_bar_window_p)
12513 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12514 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12515 f->minimize_tool_bar_window_p = false;
12516
12517 if (change_height_p)
12518 {
12519 x_change_tool_bar_height (f, new_height);
12520 frame_default_tool_bar_height = new_height;
12521 clear_glyph_matrix (w->desired_matrix);
12522 f->n_tool_bar_rows = nrows;
12523 f->fonts_changed = true;
12524
12525 return true;
12526 }
12527 }
12528 }
12529
12530 f->minimize_tool_bar_window_p = false;
12531 return false;
12532
12533 #endif /* USE_GTK || HAVE_NS */
12534 }
12535
12536 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12537
12538 /* Get information about the tool-bar item which is displayed in GLYPH
12539 on frame F. Return in *PROP_IDX the index where tool-bar item
12540 properties start in F->tool_bar_items. Value is false if
12541 GLYPH doesn't display a tool-bar item. */
12542
12543 static bool
12544 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12545 {
12546 Lisp_Object prop;
12547 int charpos;
12548
12549 /* This function can be called asynchronously, which means we must
12550 exclude any possibility that Fget_text_property signals an
12551 error. */
12552 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12553 charpos = max (0, charpos);
12554
12555 /* Get the text property `menu-item' at pos. The value of that
12556 property is the start index of this item's properties in
12557 F->tool_bar_items. */
12558 prop = Fget_text_property (make_number (charpos),
12559 Qmenu_item, f->current_tool_bar_string);
12560 if (! INTEGERP (prop))
12561 return false;
12562 *prop_idx = XINT (prop);
12563 return true;
12564 }
12565
12566 \f
12567 /* Get information about the tool-bar item at position X/Y on frame F.
12568 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12569 the current matrix of the tool-bar window of F, or NULL if not
12570 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12571 item in F->tool_bar_items. Value is
12572
12573 -1 if X/Y is not on a tool-bar item
12574 0 if X/Y is on the same item that was highlighted before.
12575 1 otherwise. */
12576
12577 static int
12578 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12579 int *hpos, int *vpos, int *prop_idx)
12580 {
12581 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12582 struct window *w = XWINDOW (f->tool_bar_window);
12583 int area;
12584
12585 /* Find the glyph under X/Y. */
12586 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12587 if (*glyph == NULL)
12588 return -1;
12589
12590 /* Get the start of this tool-bar item's properties in
12591 f->tool_bar_items. */
12592 if (!tool_bar_item_info (f, *glyph, prop_idx))
12593 return -1;
12594
12595 /* Is mouse on the highlighted item? */
12596 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12597 && *vpos >= hlinfo->mouse_face_beg_row
12598 && *vpos <= hlinfo->mouse_face_end_row
12599 && (*vpos > hlinfo->mouse_face_beg_row
12600 || *hpos >= hlinfo->mouse_face_beg_col)
12601 && (*vpos < hlinfo->mouse_face_end_row
12602 || *hpos < hlinfo->mouse_face_end_col
12603 || hlinfo->mouse_face_past_end))
12604 return 0;
12605
12606 return 1;
12607 }
12608
12609
12610 /* EXPORT:
12611 Handle mouse button event on the tool-bar of frame F, at
12612 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12613 false for button release. MODIFIERS is event modifiers for button
12614 release. */
12615
12616 void
12617 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12618 int modifiers)
12619 {
12620 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12621 struct window *w = XWINDOW (f->tool_bar_window);
12622 int hpos, vpos, prop_idx;
12623 struct glyph *glyph;
12624 Lisp_Object enabled_p;
12625 int ts;
12626
12627 /* If not on the highlighted tool-bar item, and mouse-highlight is
12628 non-nil, return. This is so we generate the tool-bar button
12629 click only when the mouse button is released on the same item as
12630 where it was pressed. However, when mouse-highlight is disabled,
12631 generate the click when the button is released regardless of the
12632 highlight, since tool-bar items are not highlighted in that
12633 case. */
12634 frame_to_window_pixel_xy (w, &x, &y);
12635 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12636 if (ts == -1
12637 || (ts != 0 && !NILP (Vmouse_highlight)))
12638 return;
12639
12640 /* When mouse-highlight is off, generate the click for the item
12641 where the button was pressed, disregarding where it was
12642 released. */
12643 if (NILP (Vmouse_highlight) && !down_p)
12644 prop_idx = f->last_tool_bar_item;
12645
12646 /* If item is disabled, do nothing. */
12647 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12648 if (NILP (enabled_p))
12649 return;
12650
12651 if (down_p)
12652 {
12653 /* Show item in pressed state. */
12654 if (!NILP (Vmouse_highlight))
12655 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12656 f->last_tool_bar_item = prop_idx;
12657 }
12658 else
12659 {
12660 Lisp_Object key, frame;
12661 struct input_event event;
12662 EVENT_INIT (event);
12663
12664 /* Show item in released state. */
12665 if (!NILP (Vmouse_highlight))
12666 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12667
12668 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12669
12670 XSETFRAME (frame, f);
12671 event.kind = TOOL_BAR_EVENT;
12672 event.frame_or_window = frame;
12673 event.arg = frame;
12674 kbd_buffer_store_event (&event);
12675
12676 event.kind = TOOL_BAR_EVENT;
12677 event.frame_or_window = frame;
12678 event.arg = key;
12679 event.modifiers = modifiers;
12680 kbd_buffer_store_event (&event);
12681 f->last_tool_bar_item = -1;
12682 }
12683 }
12684
12685
12686 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12687 tool-bar window-relative coordinates X/Y. Called from
12688 note_mouse_highlight. */
12689
12690 static void
12691 note_tool_bar_highlight (struct frame *f, int x, int y)
12692 {
12693 Lisp_Object window = f->tool_bar_window;
12694 struct window *w = XWINDOW (window);
12695 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12696 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12697 int hpos, vpos;
12698 struct glyph *glyph;
12699 struct glyph_row *row;
12700 int i;
12701 Lisp_Object enabled_p;
12702 int prop_idx;
12703 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12704 bool mouse_down_p;
12705 int rc;
12706
12707 /* Function note_mouse_highlight is called with negative X/Y
12708 values when mouse moves outside of the frame. */
12709 if (x <= 0 || y <= 0)
12710 {
12711 clear_mouse_face (hlinfo);
12712 return;
12713 }
12714
12715 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12716 if (rc < 0)
12717 {
12718 /* Not on tool-bar item. */
12719 clear_mouse_face (hlinfo);
12720 return;
12721 }
12722 else if (rc == 0)
12723 /* On same tool-bar item as before. */
12724 goto set_help_echo;
12725
12726 clear_mouse_face (hlinfo);
12727
12728 /* Mouse is down, but on different tool-bar item? */
12729 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12730 && f == dpyinfo->last_mouse_frame);
12731
12732 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12733 return;
12734
12735 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12736
12737 /* If tool-bar item is not enabled, don't highlight it. */
12738 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12739 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12740 {
12741 /* Compute the x-position of the glyph. In front and past the
12742 image is a space. We include this in the highlighted area. */
12743 row = MATRIX_ROW (w->current_matrix, vpos);
12744 for (i = x = 0; i < hpos; ++i)
12745 x += row->glyphs[TEXT_AREA][i].pixel_width;
12746
12747 /* Record this as the current active region. */
12748 hlinfo->mouse_face_beg_col = hpos;
12749 hlinfo->mouse_face_beg_row = vpos;
12750 hlinfo->mouse_face_beg_x = x;
12751 hlinfo->mouse_face_past_end = false;
12752
12753 hlinfo->mouse_face_end_col = hpos + 1;
12754 hlinfo->mouse_face_end_row = vpos;
12755 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12756 hlinfo->mouse_face_window = window;
12757 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12758
12759 /* Display it as active. */
12760 show_mouse_face (hlinfo, draw);
12761 }
12762
12763 set_help_echo:
12764
12765 /* Set help_echo_string to a help string to display for this tool-bar item.
12766 XTread_socket does the rest. */
12767 help_echo_object = help_echo_window = Qnil;
12768 help_echo_pos = -1;
12769 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12770 if (NILP (help_echo_string))
12771 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12772 }
12773
12774 #endif /* !USE_GTK && !HAVE_NS */
12775
12776 #endif /* HAVE_WINDOW_SYSTEM */
12777
12778
12779 \f
12780 /************************************************************************
12781 Horizontal scrolling
12782 ************************************************************************/
12783
12784 /* For all leaf windows in the window tree rooted at WINDOW, set their
12785 hscroll value so that PT is (i) visible in the window, and (ii) so
12786 that it is not within a certain margin at the window's left and
12787 right border. Value is true if any window's hscroll has been
12788 changed. */
12789
12790 static bool
12791 hscroll_window_tree (Lisp_Object window)
12792 {
12793 bool hscrolled_p = false;
12794 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12795 int hscroll_step_abs = 0;
12796 double hscroll_step_rel = 0;
12797
12798 if (hscroll_relative_p)
12799 {
12800 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12801 if (hscroll_step_rel < 0)
12802 {
12803 hscroll_relative_p = false;
12804 hscroll_step_abs = 0;
12805 }
12806 }
12807 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12808 {
12809 hscroll_step_abs = XINT (Vhscroll_step);
12810 if (hscroll_step_abs < 0)
12811 hscroll_step_abs = 0;
12812 }
12813 else
12814 hscroll_step_abs = 0;
12815
12816 while (WINDOWP (window))
12817 {
12818 struct window *w = XWINDOW (window);
12819
12820 if (WINDOWP (w->contents))
12821 hscrolled_p |= hscroll_window_tree (w->contents);
12822 else if (w->cursor.vpos >= 0)
12823 {
12824 int h_margin;
12825 int text_area_width;
12826 struct glyph_row *cursor_row;
12827 struct glyph_row *bottom_row;
12828
12829 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12830 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12831 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12832 else
12833 cursor_row = bottom_row - 1;
12834
12835 if (!cursor_row->enabled_p)
12836 {
12837 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12838 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12839 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12840 else
12841 cursor_row = bottom_row - 1;
12842 }
12843 bool row_r2l_p = cursor_row->reversed_p;
12844
12845 text_area_width = window_box_width (w, TEXT_AREA);
12846
12847 /* Scroll when cursor is inside this scroll margin. */
12848 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12849
12850 /* If the position of this window's point has explicitly
12851 changed, no more suspend auto hscrolling. */
12852 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12853 w->suspend_auto_hscroll = false;
12854
12855 /* Remember window point. */
12856 Fset_marker (w->old_pointm,
12857 ((w == XWINDOW (selected_window))
12858 ? make_number (BUF_PT (XBUFFER (w->contents)))
12859 : Fmarker_position (w->pointm)),
12860 w->contents);
12861
12862 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12863 && !w->suspend_auto_hscroll
12864 /* In some pathological cases, like restoring a window
12865 configuration into a frame that is much smaller than
12866 the one from which the configuration was saved, we
12867 get glyph rows whose start and end have zero buffer
12868 positions, which we cannot handle below. Just skip
12869 such windows. */
12870 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12871 /* For left-to-right rows, hscroll when cursor is either
12872 (i) inside the right hscroll margin, or (ii) if it is
12873 inside the left margin and the window is already
12874 hscrolled. */
12875 && ((!row_r2l_p
12876 && ((w->hscroll && w->cursor.x <= h_margin)
12877 || (cursor_row->enabled_p
12878 && cursor_row->truncated_on_right_p
12879 && (w->cursor.x >= text_area_width - h_margin))))
12880 /* For right-to-left rows, the logic is similar,
12881 except that rules for scrolling to left and right
12882 are reversed. E.g., if cursor.x <= h_margin, we
12883 need to hscroll "to the right" unconditionally,
12884 and that will scroll the screen to the left so as
12885 to reveal the next portion of the row. */
12886 || (row_r2l_p
12887 && ((cursor_row->enabled_p
12888 /* FIXME: It is confusing to set the
12889 truncated_on_right_p flag when R2L rows
12890 are actually truncated on the left. */
12891 && cursor_row->truncated_on_right_p
12892 && w->cursor.x <= h_margin)
12893 || (w->hscroll
12894 && (w->cursor.x >= text_area_width - h_margin))))))
12895 {
12896 struct it it;
12897 ptrdiff_t hscroll;
12898 struct buffer *saved_current_buffer;
12899 ptrdiff_t pt;
12900 int wanted_x;
12901
12902 /* Find point in a display of infinite width. */
12903 saved_current_buffer = current_buffer;
12904 current_buffer = XBUFFER (w->contents);
12905
12906 if (w == XWINDOW (selected_window))
12907 pt = PT;
12908 else
12909 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12910
12911 /* Move iterator to pt starting at cursor_row->start in
12912 a line with infinite width. */
12913 init_to_row_start (&it, w, cursor_row);
12914 it.last_visible_x = INFINITY;
12915 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12916 current_buffer = saved_current_buffer;
12917
12918 /* Position cursor in window. */
12919 if (!hscroll_relative_p && hscroll_step_abs == 0)
12920 hscroll = max (0, (it.current_x
12921 - (ITERATOR_AT_END_OF_LINE_P (&it)
12922 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12923 : (text_area_width / 2))))
12924 / FRAME_COLUMN_WIDTH (it.f);
12925 else if ((!row_r2l_p
12926 && w->cursor.x >= text_area_width - h_margin)
12927 || (row_r2l_p && w->cursor.x <= h_margin))
12928 {
12929 if (hscroll_relative_p)
12930 wanted_x = text_area_width * (1 - hscroll_step_rel)
12931 - h_margin;
12932 else
12933 wanted_x = text_area_width
12934 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12935 - h_margin;
12936 hscroll
12937 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12938 }
12939 else
12940 {
12941 if (hscroll_relative_p)
12942 wanted_x = text_area_width * hscroll_step_rel
12943 + h_margin;
12944 else
12945 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12946 + h_margin;
12947 hscroll
12948 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12949 }
12950 hscroll = max (hscroll, w->min_hscroll);
12951
12952 /* Don't prevent redisplay optimizations if hscroll
12953 hasn't changed, as it will unnecessarily slow down
12954 redisplay. */
12955 if (w->hscroll != hscroll)
12956 {
12957 struct buffer *b = XBUFFER (w->contents);
12958 b->prevent_redisplay_optimizations_p = true;
12959 w->hscroll = hscroll;
12960 hscrolled_p = true;
12961 }
12962 }
12963 }
12964
12965 window = w->next;
12966 }
12967
12968 /* Value is true if hscroll of any leaf window has been changed. */
12969 return hscrolled_p;
12970 }
12971
12972
12973 /* Set hscroll so that cursor is visible and not inside horizontal
12974 scroll margins for all windows in the tree rooted at WINDOW. See
12975 also hscroll_window_tree above. Value is true if any window's
12976 hscroll has been changed. If it has, desired matrices on the frame
12977 of WINDOW are cleared. */
12978
12979 static bool
12980 hscroll_windows (Lisp_Object window)
12981 {
12982 bool hscrolled_p = hscroll_window_tree (window);
12983 if (hscrolled_p)
12984 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12985 return hscrolled_p;
12986 }
12987
12988
12989 \f
12990 /************************************************************************
12991 Redisplay
12992 ************************************************************************/
12993
12994 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
12995 This is sometimes handy to have in a debugger session. */
12996
12997 #ifdef GLYPH_DEBUG
12998
12999 /* First and last unchanged row for try_window_id. */
13000
13001 static int debug_first_unchanged_at_end_vpos;
13002 static int debug_last_unchanged_at_beg_vpos;
13003
13004 /* Delta vpos and y. */
13005
13006 static int debug_dvpos, debug_dy;
13007
13008 /* Delta in characters and bytes for try_window_id. */
13009
13010 static ptrdiff_t debug_delta, debug_delta_bytes;
13011
13012 /* Values of window_end_pos and window_end_vpos at the end of
13013 try_window_id. */
13014
13015 static ptrdiff_t debug_end_vpos;
13016
13017 /* Append a string to W->desired_matrix->method. FMT is a printf
13018 format string. If trace_redisplay_p is true also printf the
13019 resulting string to stderr. */
13020
13021 static void debug_method_add (struct window *, char const *, ...)
13022 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13023
13024 static void
13025 debug_method_add (struct window *w, char const *fmt, ...)
13026 {
13027 void *ptr = w;
13028 char *method = w->desired_matrix->method;
13029 int len = strlen (method);
13030 int size = sizeof w->desired_matrix->method;
13031 int remaining = size - len - 1;
13032 va_list ap;
13033
13034 if (len && remaining)
13035 {
13036 method[len] = '|';
13037 --remaining, ++len;
13038 }
13039
13040 va_start (ap, fmt);
13041 vsnprintf (method + len, remaining + 1, fmt, ap);
13042 va_end (ap);
13043
13044 if (trace_redisplay_p)
13045 fprintf (stderr, "%p (%s): %s\n",
13046 ptr,
13047 ((BUFFERP (w->contents)
13048 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13049 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13050 : "no buffer"),
13051 method + len);
13052 }
13053
13054 #endif /* GLYPH_DEBUG */
13055
13056
13057 /* Value is true if all changes in window W, which displays
13058 current_buffer, are in the text between START and END. START is a
13059 buffer position, END is given as a distance from Z. Used in
13060 redisplay_internal for display optimization. */
13061
13062 static bool
13063 text_outside_line_unchanged_p (struct window *w,
13064 ptrdiff_t start, ptrdiff_t end)
13065 {
13066 bool unchanged_p = true;
13067
13068 /* If text or overlays have changed, see where. */
13069 if (window_outdated (w))
13070 {
13071 /* Gap in the line? */
13072 if (GPT < start || Z - GPT < end)
13073 unchanged_p = false;
13074
13075 /* Changes start in front of the line, or end after it? */
13076 if (unchanged_p
13077 && (BEG_UNCHANGED < start - 1
13078 || END_UNCHANGED < end))
13079 unchanged_p = false;
13080
13081 /* If selective display, can't optimize if changes start at the
13082 beginning of the line. */
13083 if (unchanged_p
13084 && INTEGERP (BVAR (current_buffer, selective_display))
13085 && XINT (BVAR (current_buffer, selective_display)) > 0
13086 && (BEG_UNCHANGED < start || GPT <= start))
13087 unchanged_p = false;
13088
13089 /* If there are overlays at the start or end of the line, these
13090 may have overlay strings with newlines in them. A change at
13091 START, for instance, may actually concern the display of such
13092 overlay strings as well, and they are displayed on different
13093 lines. So, quickly rule out this case. (For the future, it
13094 might be desirable to implement something more telling than
13095 just BEG/END_UNCHANGED.) */
13096 if (unchanged_p)
13097 {
13098 if (BEG + BEG_UNCHANGED == start
13099 && overlay_touches_p (start))
13100 unchanged_p = false;
13101 if (END_UNCHANGED == end
13102 && overlay_touches_p (Z - end))
13103 unchanged_p = false;
13104 }
13105
13106 /* Under bidi reordering, adding or deleting a character in the
13107 beginning of a paragraph, before the first strong directional
13108 character, can change the base direction of the paragraph (unless
13109 the buffer specifies a fixed paragraph direction), which will
13110 require to redisplay the whole paragraph. It might be worthwhile
13111 to find the paragraph limits and widen the range of redisplayed
13112 lines to that, but for now just give up this optimization. */
13113 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13114 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13115 unchanged_p = false;
13116 }
13117
13118 return unchanged_p;
13119 }
13120
13121
13122 /* Do a frame update, taking possible shortcuts into account. This is
13123 the main external entry point for redisplay.
13124
13125 If the last redisplay displayed an echo area message and that message
13126 is no longer requested, we clear the echo area or bring back the
13127 mini-buffer if that is in use. */
13128
13129 void
13130 redisplay (void)
13131 {
13132 redisplay_internal ();
13133 }
13134
13135
13136 static Lisp_Object
13137 overlay_arrow_string_or_property (Lisp_Object var)
13138 {
13139 Lisp_Object val;
13140
13141 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13142 return val;
13143
13144 return Voverlay_arrow_string;
13145 }
13146
13147 /* Return true if there are any overlay-arrows in current_buffer. */
13148 static bool
13149 overlay_arrow_in_current_buffer_p (void)
13150 {
13151 Lisp_Object vlist;
13152
13153 for (vlist = Voverlay_arrow_variable_list;
13154 CONSP (vlist);
13155 vlist = XCDR (vlist))
13156 {
13157 Lisp_Object var = XCAR (vlist);
13158 Lisp_Object val;
13159
13160 if (!SYMBOLP (var))
13161 continue;
13162 val = find_symbol_value (var);
13163 if (MARKERP (val)
13164 && current_buffer == XMARKER (val)->buffer)
13165 return true;
13166 }
13167 return false;
13168 }
13169
13170
13171 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13172 has changed. */
13173
13174 static bool
13175 overlay_arrows_changed_p (void)
13176 {
13177 Lisp_Object vlist;
13178
13179 for (vlist = Voverlay_arrow_variable_list;
13180 CONSP (vlist);
13181 vlist = XCDR (vlist))
13182 {
13183 Lisp_Object var = XCAR (vlist);
13184 Lisp_Object val, pstr;
13185
13186 if (!SYMBOLP (var))
13187 continue;
13188 val = find_symbol_value (var);
13189 if (!MARKERP (val))
13190 continue;
13191 if (! EQ (COERCE_MARKER (val),
13192 Fget (var, Qlast_arrow_position))
13193 || ! (pstr = overlay_arrow_string_or_property (var),
13194 EQ (pstr, Fget (var, Qlast_arrow_string))))
13195 return true;
13196 }
13197 return false;
13198 }
13199
13200 /* Mark overlay arrows to be updated on next redisplay. */
13201
13202 static void
13203 update_overlay_arrows (int up_to_date)
13204 {
13205 Lisp_Object vlist;
13206
13207 for (vlist = Voverlay_arrow_variable_list;
13208 CONSP (vlist);
13209 vlist = XCDR (vlist))
13210 {
13211 Lisp_Object var = XCAR (vlist);
13212
13213 if (!SYMBOLP (var))
13214 continue;
13215
13216 if (up_to_date > 0)
13217 {
13218 Lisp_Object val = find_symbol_value (var);
13219 Fput (var, Qlast_arrow_position,
13220 COERCE_MARKER (val));
13221 Fput (var, Qlast_arrow_string,
13222 overlay_arrow_string_or_property (var));
13223 }
13224 else if (up_to_date < 0
13225 || !NILP (Fget (var, Qlast_arrow_position)))
13226 {
13227 Fput (var, Qlast_arrow_position, Qt);
13228 Fput (var, Qlast_arrow_string, Qt);
13229 }
13230 }
13231 }
13232
13233
13234 /* Return overlay arrow string to display at row.
13235 Return integer (bitmap number) for arrow bitmap in left fringe.
13236 Return nil if no overlay arrow. */
13237
13238 static Lisp_Object
13239 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13240 {
13241 Lisp_Object vlist;
13242
13243 for (vlist = Voverlay_arrow_variable_list;
13244 CONSP (vlist);
13245 vlist = XCDR (vlist))
13246 {
13247 Lisp_Object var = XCAR (vlist);
13248 Lisp_Object val;
13249
13250 if (!SYMBOLP (var))
13251 continue;
13252
13253 val = find_symbol_value (var);
13254
13255 if (MARKERP (val)
13256 && current_buffer == XMARKER (val)->buffer
13257 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13258 {
13259 if (FRAME_WINDOW_P (it->f)
13260 /* FIXME: if ROW->reversed_p is set, this should test
13261 the right fringe, not the left one. */
13262 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13263 {
13264 #ifdef HAVE_WINDOW_SYSTEM
13265 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13266 {
13267 int fringe_bitmap = lookup_fringe_bitmap (val);
13268 if (fringe_bitmap != 0)
13269 return make_number (fringe_bitmap);
13270 }
13271 #endif
13272 return make_number (-1); /* Use default arrow bitmap. */
13273 }
13274 return overlay_arrow_string_or_property (var);
13275 }
13276 }
13277
13278 return Qnil;
13279 }
13280
13281 /* Return true if point moved out of or into a composition. Otherwise
13282 return false. PREV_BUF and PREV_PT are the last point buffer and
13283 position. BUF and PT are the current point buffer and position. */
13284
13285 static bool
13286 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13287 struct buffer *buf, ptrdiff_t pt)
13288 {
13289 ptrdiff_t start, end;
13290 Lisp_Object prop;
13291 Lisp_Object buffer;
13292
13293 XSETBUFFER (buffer, buf);
13294 /* Check a composition at the last point if point moved within the
13295 same buffer. */
13296 if (prev_buf == buf)
13297 {
13298 if (prev_pt == pt)
13299 /* Point didn't move. */
13300 return false;
13301
13302 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13303 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13304 && composition_valid_p (start, end, prop)
13305 && start < prev_pt && end > prev_pt)
13306 /* The last point was within the composition. Return true iff
13307 point moved out of the composition. */
13308 return (pt <= start || pt >= end);
13309 }
13310
13311 /* Check a composition at the current point. */
13312 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13313 && find_composition (pt, -1, &start, &end, &prop, buffer)
13314 && composition_valid_p (start, end, prop)
13315 && start < pt && end > pt);
13316 }
13317
13318 /* Reconsider the clip changes of buffer which is displayed in W. */
13319
13320 static void
13321 reconsider_clip_changes (struct window *w)
13322 {
13323 struct buffer *b = XBUFFER (w->contents);
13324
13325 if (b->clip_changed
13326 && w->window_end_valid
13327 && w->current_matrix->buffer == b
13328 && w->current_matrix->zv == BUF_ZV (b)
13329 && w->current_matrix->begv == BUF_BEGV (b))
13330 b->clip_changed = false;
13331
13332 /* If display wasn't paused, and W is not a tool bar window, see if
13333 point has been moved into or out of a composition. In that case,
13334 set b->clip_changed to force updating the screen. If
13335 b->clip_changed has already been set, skip this check. */
13336 if (!b->clip_changed && w->window_end_valid)
13337 {
13338 ptrdiff_t pt = (w == XWINDOW (selected_window)
13339 ? PT : marker_position (w->pointm));
13340
13341 if ((w->current_matrix->buffer != b || pt != w->last_point)
13342 && check_point_in_composition (w->current_matrix->buffer,
13343 w->last_point, b, pt))
13344 b->clip_changed = true;
13345 }
13346 }
13347
13348 static void
13349 propagate_buffer_redisplay (void)
13350 { /* Resetting b->text->redisplay is problematic!
13351 We can't just reset it in the case that some window that displays
13352 it has not been redisplayed; and such a window can stay
13353 unredisplayed for a long time if it's currently invisible.
13354 But we do want to reset it at the end of redisplay otherwise
13355 its displayed windows will keep being redisplayed over and over
13356 again.
13357 So we copy all b->text->redisplay flags up to their windows here,
13358 such that mark_window_display_accurate can safely reset
13359 b->text->redisplay. */
13360 Lisp_Object ws = window_list ();
13361 for (; CONSP (ws); ws = XCDR (ws))
13362 {
13363 struct window *thisw = XWINDOW (XCAR (ws));
13364 struct buffer *thisb = XBUFFER (thisw->contents);
13365 if (thisb->text->redisplay)
13366 thisw->redisplay = true;
13367 }
13368 }
13369
13370 #define STOP_POLLING \
13371 do { if (! polling_stopped_here) stop_polling (); \
13372 polling_stopped_here = true; } while (false)
13373
13374 #define RESUME_POLLING \
13375 do { if (polling_stopped_here) start_polling (); \
13376 polling_stopped_here = false; } while (false)
13377
13378
13379 /* Perhaps in the future avoid recentering windows if it
13380 is not necessary; currently that causes some problems. */
13381
13382 static void
13383 redisplay_internal (void)
13384 {
13385 struct window *w = XWINDOW (selected_window);
13386 struct window *sw;
13387 struct frame *fr;
13388 bool pending;
13389 bool must_finish = false, match_p;
13390 struct text_pos tlbufpos, tlendpos;
13391 int number_of_visible_frames;
13392 ptrdiff_t count;
13393 struct frame *sf;
13394 bool polling_stopped_here = false;
13395 Lisp_Object tail, frame;
13396
13397 /* True means redisplay has to consider all windows on all
13398 frames. False, only selected_window is considered. */
13399 bool consider_all_windows_p;
13400
13401 /* True means redisplay has to redisplay the miniwindow. */
13402 bool update_miniwindow_p = false;
13403
13404 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13405
13406 /* No redisplay if running in batch mode or frame is not yet fully
13407 initialized, or redisplay is explicitly turned off by setting
13408 Vinhibit_redisplay. */
13409 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13410 || !NILP (Vinhibit_redisplay))
13411 return;
13412
13413 /* Don't examine these until after testing Vinhibit_redisplay.
13414 When Emacs is shutting down, perhaps because its connection to
13415 X has dropped, we should not look at them at all. */
13416 fr = XFRAME (w->frame);
13417 sf = SELECTED_FRAME ();
13418
13419 if (!fr->glyphs_initialized_p)
13420 return;
13421
13422 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13423 if (popup_activated ())
13424 return;
13425 #endif
13426
13427 /* I don't think this happens but let's be paranoid. */
13428 if (redisplaying_p)
13429 return;
13430
13431 /* Record a function that clears redisplaying_p
13432 when we leave this function. */
13433 count = SPECPDL_INDEX ();
13434 record_unwind_protect_void (unwind_redisplay);
13435 redisplaying_p = true;
13436 specbind (Qinhibit_free_realized_faces, Qnil);
13437
13438 /* Record this function, so it appears on the profiler's backtraces. */
13439 record_in_backtrace (Qredisplay_internal, 0, 0);
13440
13441 FOR_EACH_FRAME (tail, frame)
13442 XFRAME (frame)->already_hscrolled_p = false;
13443
13444 retry:
13445 /* Remember the currently selected window. */
13446 sw = w;
13447
13448 pending = false;
13449 forget_escape_and_glyphless_faces ();
13450
13451 inhibit_free_realized_faces = false;
13452
13453 /* If face_change, init_iterator will free all realized faces, which
13454 includes the faces referenced from current matrices. So, we
13455 can't reuse current matrices in this case. */
13456 if (face_change)
13457 windows_or_buffers_changed = 47;
13458
13459 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13460 && FRAME_TTY (sf)->previous_frame != sf)
13461 {
13462 /* Since frames on a single ASCII terminal share the same
13463 display area, displaying a different frame means redisplay
13464 the whole thing. */
13465 SET_FRAME_GARBAGED (sf);
13466 #ifndef DOS_NT
13467 set_tty_color_mode (FRAME_TTY (sf), sf);
13468 #endif
13469 FRAME_TTY (sf)->previous_frame = sf;
13470 }
13471
13472 /* Set the visible flags for all frames. Do this before checking for
13473 resized or garbaged frames; they want to know if their frames are
13474 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13475 number_of_visible_frames = 0;
13476
13477 FOR_EACH_FRAME (tail, frame)
13478 {
13479 struct frame *f = XFRAME (frame);
13480
13481 if (FRAME_VISIBLE_P (f))
13482 {
13483 ++number_of_visible_frames;
13484 /* Adjust matrices for visible frames only. */
13485 if (f->fonts_changed)
13486 {
13487 adjust_frame_glyphs (f);
13488 /* Disable all redisplay optimizations for this frame.
13489 This is because adjust_frame_glyphs resets the
13490 enabled_p flag for all glyph rows of all windows, so
13491 many optimizations will fail anyway, and some might
13492 fail to test that flag and do bogus things as
13493 result. */
13494 SET_FRAME_GARBAGED (f);
13495 f->fonts_changed = false;
13496 }
13497 /* If cursor type has been changed on the frame
13498 other than selected, consider all frames. */
13499 if (f != sf && f->cursor_type_changed)
13500 fset_redisplay (f);
13501 }
13502 clear_desired_matrices (f);
13503 }
13504
13505 /* Notice any pending interrupt request to change frame size. */
13506 do_pending_window_change (true);
13507
13508 /* do_pending_window_change could change the selected_window due to
13509 frame resizing which makes the selected window too small. */
13510 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13511 sw = w;
13512
13513 /* Clear frames marked as garbaged. */
13514 clear_garbaged_frames ();
13515
13516 /* Build menubar and tool-bar items. */
13517 if (NILP (Vmemory_full))
13518 prepare_menu_bars ();
13519
13520 reconsider_clip_changes (w);
13521
13522 /* In most cases selected window displays current buffer. */
13523 match_p = XBUFFER (w->contents) == current_buffer;
13524 if (match_p)
13525 {
13526 /* Detect case that we need to write or remove a star in the mode line. */
13527 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13528 w->update_mode_line = true;
13529
13530 if (mode_line_update_needed (w))
13531 w->update_mode_line = true;
13532
13533 /* If reconsider_clip_changes above decided that the narrowing
13534 in the current buffer changed, make sure all other windows
13535 showing that buffer will be redisplayed. */
13536 if (current_buffer->clip_changed)
13537 bset_update_mode_line (current_buffer);
13538 }
13539
13540 /* Normally the message* functions will have already displayed and
13541 updated the echo area, but the frame may have been trashed, or
13542 the update may have been preempted, so display the echo area
13543 again here. Checking message_cleared_p captures the case that
13544 the echo area should be cleared. */
13545 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13546 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13547 || (message_cleared_p
13548 && minibuf_level == 0
13549 /* If the mini-window is currently selected, this means the
13550 echo-area doesn't show through. */
13551 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13552 {
13553 echo_area_display (false);
13554
13555 /* If echo_area_display resizes the mini-window, the redisplay and
13556 window_sizes_changed flags of the selected frame are set, but
13557 it's too late for the hooks in window-size-change-functions,
13558 which have been examined already in prepare_menu_bars. So in
13559 that case we call the hooks here only for the selected frame. */
13560 if (sf->redisplay && FRAME_WINDOW_SIZES_CHANGED (sf))
13561 {
13562 Lisp_Object functions;
13563 ptrdiff_t count1 = SPECPDL_INDEX ();
13564
13565 record_unwind_save_match_data ();
13566
13567 /* Clear flag first in case we get an error below. */
13568 FRAME_WINDOW_SIZES_CHANGED (sf) = false;
13569 functions = Vwindow_size_change_functions;
13570
13571 while (CONSP (functions))
13572 {
13573 if (!EQ (XCAR (functions), Qt))
13574 call1 (XCAR (functions), selected_frame);
13575 functions = XCDR (functions);
13576 }
13577
13578 unbind_to (count1, Qnil);
13579 }
13580
13581 if (message_cleared_p)
13582 update_miniwindow_p = true;
13583
13584 must_finish = true;
13585
13586 /* If we don't display the current message, don't clear the
13587 message_cleared_p flag, because, if we did, we wouldn't clear
13588 the echo area in the next redisplay which doesn't preserve
13589 the echo area. */
13590 if (!display_last_displayed_message_p)
13591 message_cleared_p = false;
13592 }
13593 else if (EQ (selected_window, minibuf_window)
13594 && (current_buffer->clip_changed || window_outdated (w))
13595 && resize_mini_window (w, false))
13596 {
13597 if (sf->redisplay)
13598 {
13599 Lisp_Object functions;
13600 ptrdiff_t count1 = SPECPDL_INDEX ();
13601
13602 record_unwind_save_match_data ();
13603
13604 /* Clear flag first in case we get an error below. */
13605 FRAME_WINDOW_SIZES_CHANGED (sf) = false;
13606 functions = Vwindow_size_change_functions;
13607
13608 while (CONSP (functions))
13609 {
13610 if (!EQ (XCAR (functions), Qt))
13611 call1 (XCAR (functions), selected_frame);
13612 functions = XCDR (functions);
13613 }
13614
13615 unbind_to (count1, Qnil);
13616 }
13617
13618 /* Resized active mini-window to fit the size of what it is
13619 showing if its contents might have changed. */
13620 must_finish = true;
13621
13622 /* If window configuration was changed, frames may have been
13623 marked garbaged. Clear them or we will experience
13624 surprises wrt scrolling. */
13625 clear_garbaged_frames ();
13626 }
13627
13628 if (windows_or_buffers_changed && !update_mode_lines)
13629 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13630 only the windows's contents needs to be refreshed, or whether the
13631 mode-lines also need a refresh. */
13632 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13633 ? REDISPLAY_SOME : 32);
13634
13635 /* If specs for an arrow have changed, do thorough redisplay
13636 to ensure we remove any arrow that should no longer exist. */
13637 if (overlay_arrows_changed_p ())
13638 /* Apparently, this is the only case where we update other windows,
13639 without updating other mode-lines. */
13640 windows_or_buffers_changed = 49;
13641
13642 consider_all_windows_p = (update_mode_lines
13643 || windows_or_buffers_changed);
13644
13645 #define AINC(a,i) \
13646 { \
13647 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13648 if (INTEGERP (entry)) \
13649 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13650 }
13651
13652 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13653 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13654
13655 /* Optimize the case that only the line containing the cursor in the
13656 selected window has changed. Variables starting with this_ are
13657 set in display_line and record information about the line
13658 containing the cursor. */
13659 tlbufpos = this_line_start_pos;
13660 tlendpos = this_line_end_pos;
13661 if (!consider_all_windows_p
13662 && CHARPOS (tlbufpos) > 0
13663 && !w->update_mode_line
13664 && !current_buffer->clip_changed
13665 && !current_buffer->prevent_redisplay_optimizations_p
13666 && FRAME_VISIBLE_P (XFRAME (w->frame))
13667 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13668 && !XFRAME (w->frame)->cursor_type_changed
13669 && !XFRAME (w->frame)->face_change
13670 /* Make sure recorded data applies to current buffer, etc. */
13671 && this_line_buffer == current_buffer
13672 && match_p
13673 && !w->force_start
13674 && !w->optional_new_start
13675 /* Point must be on the line that we have info recorded about. */
13676 && PT >= CHARPOS (tlbufpos)
13677 && PT <= Z - CHARPOS (tlendpos)
13678 /* All text outside that line, including its final newline,
13679 must be unchanged. */
13680 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13681 CHARPOS (tlendpos)))
13682 {
13683 if (CHARPOS (tlbufpos) > BEGV
13684 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13685 && (CHARPOS (tlbufpos) == ZV
13686 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13687 /* Former continuation line has disappeared by becoming empty. */
13688 goto cancel;
13689 else if (window_outdated (w) || MINI_WINDOW_P (w))
13690 {
13691 /* We have to handle the case of continuation around a
13692 wide-column character (see the comment in indent.c around
13693 line 1340).
13694
13695 For instance, in the following case:
13696
13697 -------- Insert --------
13698 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13699 J_I_ ==> J_I_ `^^' are cursors.
13700 ^^ ^^
13701 -------- --------
13702
13703 As we have to redraw the line above, we cannot use this
13704 optimization. */
13705
13706 struct it it;
13707 int line_height_before = this_line_pixel_height;
13708
13709 /* Note that start_display will handle the case that the
13710 line starting at tlbufpos is a continuation line. */
13711 start_display (&it, w, tlbufpos);
13712
13713 /* Implementation note: It this still necessary? */
13714 if (it.current_x != this_line_start_x)
13715 goto cancel;
13716
13717 TRACE ((stderr, "trying display optimization 1\n"));
13718 w->cursor.vpos = -1;
13719 overlay_arrow_seen = false;
13720 it.vpos = this_line_vpos;
13721 it.current_y = this_line_y;
13722 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13723 display_line (&it);
13724
13725 /* If line contains point, is not continued,
13726 and ends at same distance from eob as before, we win. */
13727 if (w->cursor.vpos >= 0
13728 /* Line is not continued, otherwise this_line_start_pos
13729 would have been set to 0 in display_line. */
13730 && CHARPOS (this_line_start_pos)
13731 /* Line ends as before. */
13732 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13733 /* Line has same height as before. Otherwise other lines
13734 would have to be shifted up or down. */
13735 && this_line_pixel_height == line_height_before)
13736 {
13737 /* If this is not the window's last line, we must adjust
13738 the charstarts of the lines below. */
13739 if (it.current_y < it.last_visible_y)
13740 {
13741 struct glyph_row *row
13742 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13743 ptrdiff_t delta, delta_bytes;
13744
13745 /* We used to distinguish between two cases here,
13746 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13747 when the line ends in a newline or the end of the
13748 buffer's accessible portion. But both cases did
13749 the same, so they were collapsed. */
13750 delta = (Z
13751 - CHARPOS (tlendpos)
13752 - MATRIX_ROW_START_CHARPOS (row));
13753 delta_bytes = (Z_BYTE
13754 - BYTEPOS (tlendpos)
13755 - MATRIX_ROW_START_BYTEPOS (row));
13756
13757 increment_matrix_positions (w->current_matrix,
13758 this_line_vpos + 1,
13759 w->current_matrix->nrows,
13760 delta, delta_bytes);
13761 }
13762
13763 /* If this row displays text now but previously didn't,
13764 or vice versa, w->window_end_vpos may have to be
13765 adjusted. */
13766 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13767 {
13768 if (w->window_end_vpos < this_line_vpos)
13769 w->window_end_vpos = this_line_vpos;
13770 }
13771 else if (w->window_end_vpos == this_line_vpos
13772 && this_line_vpos > 0)
13773 w->window_end_vpos = this_line_vpos - 1;
13774 w->window_end_valid = false;
13775
13776 /* Update hint: No need to try to scroll in update_window. */
13777 w->desired_matrix->no_scrolling_p = true;
13778
13779 #ifdef GLYPH_DEBUG
13780 *w->desired_matrix->method = 0;
13781 debug_method_add (w, "optimization 1");
13782 #endif
13783 #ifdef HAVE_WINDOW_SYSTEM
13784 update_window_fringes (w, false);
13785 #endif
13786 goto update;
13787 }
13788 else
13789 goto cancel;
13790 }
13791 else if (/* Cursor position hasn't changed. */
13792 PT == w->last_point
13793 /* Make sure the cursor was last displayed
13794 in this window. Otherwise we have to reposition it. */
13795
13796 /* PXW: Must be converted to pixels, probably. */
13797 && 0 <= w->cursor.vpos
13798 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13799 {
13800 if (!must_finish)
13801 {
13802 do_pending_window_change (true);
13803 /* If selected_window changed, redisplay again. */
13804 if (WINDOWP (selected_window)
13805 && (w = XWINDOW (selected_window)) != sw)
13806 goto retry;
13807
13808 /* We used to always goto end_of_redisplay here, but this
13809 isn't enough if we have a blinking cursor. */
13810 if (w->cursor_off_p == w->last_cursor_off_p)
13811 goto end_of_redisplay;
13812 }
13813 goto update;
13814 }
13815 /* If highlighting the region, or if the cursor is in the echo area,
13816 then we can't just move the cursor. */
13817 else if (NILP (Vshow_trailing_whitespace)
13818 && !cursor_in_echo_area)
13819 {
13820 struct it it;
13821 struct glyph_row *row;
13822
13823 /* Skip from tlbufpos to PT and see where it is. Note that
13824 PT may be in invisible text. If so, we will end at the
13825 next visible position. */
13826 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13827 NULL, DEFAULT_FACE_ID);
13828 it.current_x = this_line_start_x;
13829 it.current_y = this_line_y;
13830 it.vpos = this_line_vpos;
13831
13832 /* The call to move_it_to stops in front of PT, but
13833 moves over before-strings. */
13834 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13835
13836 if (it.vpos == this_line_vpos
13837 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13838 row->enabled_p))
13839 {
13840 eassert (this_line_vpos == it.vpos);
13841 eassert (this_line_y == it.current_y);
13842 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13843 #ifdef GLYPH_DEBUG
13844 *w->desired_matrix->method = 0;
13845 debug_method_add (w, "optimization 3");
13846 #endif
13847 goto update;
13848 }
13849 else
13850 goto cancel;
13851 }
13852
13853 cancel:
13854 /* Text changed drastically or point moved off of line. */
13855 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13856 }
13857
13858 CHARPOS (this_line_start_pos) = 0;
13859 ++clear_face_cache_count;
13860 #ifdef HAVE_WINDOW_SYSTEM
13861 ++clear_image_cache_count;
13862 #endif
13863
13864 /* Build desired matrices, and update the display. If
13865 consider_all_windows_p, do it for all windows on all frames that
13866 require redisplay, as specified by their 'redisplay' flag.
13867 Otherwise do it for selected_window, only. */
13868
13869 if (consider_all_windows_p)
13870 {
13871 FOR_EACH_FRAME (tail, frame)
13872 XFRAME (frame)->updated_p = false;
13873
13874 propagate_buffer_redisplay ();
13875
13876 FOR_EACH_FRAME (tail, frame)
13877 {
13878 struct frame *f = XFRAME (frame);
13879
13880 /* We don't have to do anything for unselected terminal
13881 frames. */
13882 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13883 && !EQ (FRAME_TTY (f)->top_frame, frame))
13884 continue;
13885
13886 retry_frame:
13887 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13888 {
13889 bool gcscrollbars
13890 /* Only GC scrollbars when we redisplay the whole frame. */
13891 = f->redisplay || !REDISPLAY_SOME_P ();
13892 bool f_redisplay_flag = f->redisplay;
13893 /* Mark all the scroll bars to be removed; we'll redeem
13894 the ones we want when we redisplay their windows. */
13895 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13896 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13897
13898 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13899 redisplay_windows (FRAME_ROOT_WINDOW (f));
13900 /* Remember that the invisible frames need to be redisplayed next
13901 time they're visible. */
13902 else if (!REDISPLAY_SOME_P ())
13903 f->redisplay = true;
13904
13905 /* The X error handler may have deleted that frame. */
13906 if (!FRAME_LIVE_P (f))
13907 continue;
13908
13909 /* Any scroll bars which redisplay_windows should have
13910 nuked should now go away. */
13911 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13912 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13913
13914 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13915 {
13916 /* If fonts changed on visible frame, display again. */
13917 if (f->fonts_changed)
13918 {
13919 adjust_frame_glyphs (f);
13920 /* Disable all redisplay optimizations for this
13921 frame. For the reasons, see the comment near
13922 the previous call to adjust_frame_glyphs above. */
13923 SET_FRAME_GARBAGED (f);
13924 f->fonts_changed = false;
13925 goto retry_frame;
13926 }
13927
13928 /* See if we have to hscroll. */
13929 if (!f->already_hscrolled_p)
13930 {
13931 f->already_hscrolled_p = true;
13932 if (hscroll_windows (f->root_window))
13933 goto retry_frame;
13934 }
13935
13936 /* If the frame's redisplay flag was not set before
13937 we went about redisplaying its windows, but it is
13938 set now, that means we employed some redisplay
13939 optimizations inside redisplay_windows, and
13940 bypassed producing some screen lines. But if
13941 f->redisplay is now set, it might mean the old
13942 faces are no longer valid (e.g., if redisplaying
13943 some window called some Lisp which defined a new
13944 face or redefined an existing face), so trying to
13945 use them in update_frame will segfault.
13946 Therefore, we must redisplay this frame. */
13947 if (!f_redisplay_flag && f->redisplay)
13948 goto retry_frame;
13949
13950 /* Prevent various kinds of signals during display
13951 update. stdio is not robust about handling
13952 signals, which can cause an apparent I/O error. */
13953 if (interrupt_input)
13954 unrequest_sigio ();
13955 STOP_POLLING;
13956
13957 pending |= update_frame (f, false, false);
13958 f->cursor_type_changed = false;
13959 f->updated_p = true;
13960 }
13961 }
13962 }
13963
13964 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13965
13966 if (!pending)
13967 {
13968 /* Do the mark_window_display_accurate after all windows have
13969 been redisplayed because this call resets flags in buffers
13970 which are needed for proper redisplay. */
13971 FOR_EACH_FRAME (tail, frame)
13972 {
13973 struct frame *f = XFRAME (frame);
13974 if (f->updated_p)
13975 {
13976 f->redisplay = false;
13977 mark_window_display_accurate (f->root_window, true);
13978 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13979 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13980 }
13981 }
13982 }
13983 }
13984 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13985 {
13986 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13987 struct frame *mini_frame;
13988
13989 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13990 /* Use list_of_error, not Qerror, so that
13991 we catch only errors and don't run the debugger. */
13992 internal_condition_case_1 (redisplay_window_1, selected_window,
13993 list_of_error,
13994 redisplay_window_error);
13995 if (update_miniwindow_p)
13996 internal_condition_case_1 (redisplay_window_1, mini_window,
13997 list_of_error,
13998 redisplay_window_error);
13999
14000 /* Compare desired and current matrices, perform output. */
14001
14002 update:
14003 /* If fonts changed, display again. Likewise if redisplay_window_1
14004 above caused some change (e.g., a change in faces) that requires
14005 considering the entire frame again. */
14006 if (sf->fonts_changed || sf->redisplay)
14007 {
14008 if (sf->redisplay)
14009 {
14010 /* Set this to force a more thorough redisplay.
14011 Otherwise, we might immediately loop back to the
14012 above "else-if" clause (since all the conditions that
14013 led here might still be true), and we will then
14014 infloop, because the selected-frame's redisplay flag
14015 is not (and cannot be) reset. */
14016 windows_or_buffers_changed = 50;
14017 }
14018 goto retry;
14019 }
14020
14021 /* Prevent freeing of realized faces, since desired matrices are
14022 pending that reference the faces we computed and cached. */
14023 inhibit_free_realized_faces = true;
14024
14025 /* Prevent various kinds of signals during display update.
14026 stdio is not robust about handling signals,
14027 which can cause an apparent I/O error. */
14028 if (interrupt_input)
14029 unrequest_sigio ();
14030 STOP_POLLING;
14031
14032 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14033 {
14034 if (hscroll_windows (selected_window))
14035 goto retry;
14036
14037 XWINDOW (selected_window)->must_be_updated_p = true;
14038 pending = update_frame (sf, false, false);
14039 sf->cursor_type_changed = false;
14040 }
14041
14042 /* We may have called echo_area_display at the top of this
14043 function. If the echo area is on another frame, that may
14044 have put text on a frame other than the selected one, so the
14045 above call to update_frame would not have caught it. Catch
14046 it here. */
14047 mini_window = FRAME_MINIBUF_WINDOW (sf);
14048 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14049
14050 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14051 {
14052 XWINDOW (mini_window)->must_be_updated_p = true;
14053 pending |= update_frame (mini_frame, false, false);
14054 mini_frame->cursor_type_changed = false;
14055 if (!pending && hscroll_windows (mini_window))
14056 goto retry;
14057 }
14058 }
14059
14060 /* If display was paused because of pending input, make sure we do a
14061 thorough update the next time. */
14062 if (pending)
14063 {
14064 /* Prevent the optimization at the beginning of
14065 redisplay_internal that tries a single-line update of the
14066 line containing the cursor in the selected window. */
14067 CHARPOS (this_line_start_pos) = 0;
14068
14069 /* Let the overlay arrow be updated the next time. */
14070 update_overlay_arrows (0);
14071
14072 /* If we pause after scrolling, some rows in the current
14073 matrices of some windows are not valid. */
14074 if (!WINDOW_FULL_WIDTH_P (w)
14075 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14076 update_mode_lines = 36;
14077 }
14078 else
14079 {
14080 if (!consider_all_windows_p)
14081 {
14082 /* This has already been done above if
14083 consider_all_windows_p is set. */
14084 if (XBUFFER (w->contents)->text->redisplay
14085 && buffer_window_count (XBUFFER (w->contents)) > 1)
14086 /* This can happen if b->text->redisplay was set during
14087 jit-lock. */
14088 propagate_buffer_redisplay ();
14089 mark_window_display_accurate_1 (w, true);
14090
14091 /* Say overlay arrows are up to date. */
14092 update_overlay_arrows (1);
14093
14094 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14095 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14096 }
14097
14098 update_mode_lines = 0;
14099 windows_or_buffers_changed = 0;
14100 }
14101
14102 /* Start SIGIO interrupts coming again. Having them off during the
14103 code above makes it less likely one will discard output, but not
14104 impossible, since there might be stuff in the system buffer here.
14105 But it is much hairier to try to do anything about that. */
14106 if (interrupt_input)
14107 request_sigio ();
14108 RESUME_POLLING;
14109
14110 /* If a frame has become visible which was not before, redisplay
14111 again, so that we display it. Expose events for such a frame
14112 (which it gets when becoming visible) don't call the parts of
14113 redisplay constructing glyphs, so simply exposing a frame won't
14114 display anything in this case. So, we have to display these
14115 frames here explicitly. */
14116 if (!pending)
14117 {
14118 int new_count = 0;
14119
14120 FOR_EACH_FRAME (tail, frame)
14121 {
14122 if (XFRAME (frame)->visible)
14123 new_count++;
14124 }
14125
14126 if (new_count != number_of_visible_frames)
14127 windows_or_buffers_changed = 52;
14128 }
14129
14130 /* Change frame size now if a change is pending. */
14131 do_pending_window_change (true);
14132
14133 /* If we just did a pending size change, or have additional
14134 visible frames, or selected_window changed, redisplay again. */
14135 if ((windows_or_buffers_changed && !pending)
14136 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14137 goto retry;
14138
14139 /* Clear the face and image caches.
14140
14141 We used to do this only if consider_all_windows_p. But the cache
14142 needs to be cleared if a timer creates images in the current
14143 buffer (e.g. the test case in Bug#6230). */
14144
14145 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14146 {
14147 clear_face_cache (false);
14148 clear_face_cache_count = 0;
14149 }
14150
14151 #ifdef HAVE_WINDOW_SYSTEM
14152 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14153 {
14154 clear_image_caches (Qnil);
14155 clear_image_cache_count = 0;
14156 }
14157 #endif /* HAVE_WINDOW_SYSTEM */
14158
14159 end_of_redisplay:
14160 #ifdef HAVE_NS
14161 ns_set_doc_edited ();
14162 #endif
14163 if (interrupt_input && interrupts_deferred)
14164 request_sigio ();
14165
14166 unbind_to (count, Qnil);
14167 RESUME_POLLING;
14168 }
14169
14170
14171 /* Redisplay, but leave alone any recent echo area message unless
14172 another message has been requested in its place.
14173
14174 This is useful in situations where you need to redisplay but no
14175 user action has occurred, making it inappropriate for the message
14176 area to be cleared. See tracking_off and
14177 wait_reading_process_output for examples of these situations.
14178
14179 FROM_WHERE is an integer saying from where this function was
14180 called. This is useful for debugging. */
14181
14182 void
14183 redisplay_preserve_echo_area (int from_where)
14184 {
14185 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14186
14187 if (!NILP (echo_area_buffer[1]))
14188 {
14189 /* We have a previously displayed message, but no current
14190 message. Redisplay the previous message. */
14191 display_last_displayed_message_p = true;
14192 redisplay_internal ();
14193 display_last_displayed_message_p = false;
14194 }
14195 else
14196 redisplay_internal ();
14197
14198 flush_frame (SELECTED_FRAME ());
14199 }
14200
14201
14202 /* Function registered with record_unwind_protect in redisplay_internal. */
14203
14204 static void
14205 unwind_redisplay (void)
14206 {
14207 redisplaying_p = false;
14208 }
14209
14210
14211 /* Mark the display of leaf window W as accurate or inaccurate.
14212 If ACCURATE_P, mark display of W as accurate.
14213 If !ACCURATE_P, arrange for W to be redisplayed the next
14214 time redisplay_internal is called. */
14215
14216 static void
14217 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14218 {
14219 struct buffer *b = XBUFFER (w->contents);
14220
14221 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14222 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14223 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14224
14225 if (accurate_p)
14226 {
14227 b->clip_changed = false;
14228 b->prevent_redisplay_optimizations_p = false;
14229 eassert (buffer_window_count (b) > 0);
14230 /* Resetting b->text->redisplay is problematic!
14231 In order to make it safer to do it here, redisplay_internal must
14232 have copied all b->text->redisplay to their respective windows. */
14233 b->text->redisplay = false;
14234
14235 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14236 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14237 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14238 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14239
14240 w->current_matrix->buffer = b;
14241 w->current_matrix->begv = BUF_BEGV (b);
14242 w->current_matrix->zv = BUF_ZV (b);
14243
14244 w->last_cursor_vpos = w->cursor.vpos;
14245 w->last_cursor_off_p = w->cursor_off_p;
14246
14247 if (w == XWINDOW (selected_window))
14248 w->last_point = BUF_PT (b);
14249 else
14250 w->last_point = marker_position (w->pointm);
14251
14252 w->window_end_valid = true;
14253 w->update_mode_line = false;
14254 }
14255
14256 w->redisplay = !accurate_p;
14257 }
14258
14259
14260 /* Mark the display of windows in the window tree rooted at WINDOW as
14261 accurate or inaccurate. If ACCURATE_P, mark display of
14262 windows as accurate. If !ACCURATE_P, arrange for windows to
14263 be redisplayed the next time redisplay_internal is called. */
14264
14265 void
14266 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14267 {
14268 struct window *w;
14269
14270 for (; !NILP (window); window = w->next)
14271 {
14272 w = XWINDOW (window);
14273 if (WINDOWP (w->contents))
14274 mark_window_display_accurate (w->contents, accurate_p);
14275 else
14276 mark_window_display_accurate_1 (w, accurate_p);
14277 }
14278
14279 if (accurate_p)
14280 update_overlay_arrows (1);
14281 else
14282 /* Force a thorough redisplay the next time by setting
14283 last_arrow_position and last_arrow_string to t, which is
14284 unequal to any useful value of Voverlay_arrow_... */
14285 update_overlay_arrows (-1);
14286 }
14287
14288
14289 /* Return value in display table DP (Lisp_Char_Table *) for character
14290 C. Since a display table doesn't have any parent, we don't have to
14291 follow parent. Do not call this function directly but use the
14292 macro DISP_CHAR_VECTOR. */
14293
14294 Lisp_Object
14295 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14296 {
14297 Lisp_Object val;
14298
14299 if (ASCII_CHAR_P (c))
14300 {
14301 val = dp->ascii;
14302 if (SUB_CHAR_TABLE_P (val))
14303 val = XSUB_CHAR_TABLE (val)->contents[c];
14304 }
14305 else
14306 {
14307 Lisp_Object table;
14308
14309 XSETCHAR_TABLE (table, dp);
14310 val = char_table_ref (table, c);
14311 }
14312 if (NILP (val))
14313 val = dp->defalt;
14314 return val;
14315 }
14316
14317
14318 \f
14319 /***********************************************************************
14320 Window Redisplay
14321 ***********************************************************************/
14322
14323 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14324
14325 static void
14326 redisplay_windows (Lisp_Object window)
14327 {
14328 while (!NILP (window))
14329 {
14330 struct window *w = XWINDOW (window);
14331
14332 if (WINDOWP (w->contents))
14333 redisplay_windows (w->contents);
14334 else if (BUFFERP (w->contents))
14335 {
14336 displayed_buffer = XBUFFER (w->contents);
14337 /* Use list_of_error, not Qerror, so that
14338 we catch only errors and don't run the debugger. */
14339 internal_condition_case_1 (redisplay_window_0, window,
14340 list_of_error,
14341 redisplay_window_error);
14342 }
14343
14344 window = w->next;
14345 }
14346 }
14347
14348 static Lisp_Object
14349 redisplay_window_error (Lisp_Object ignore)
14350 {
14351 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14352 return Qnil;
14353 }
14354
14355 static Lisp_Object
14356 redisplay_window_0 (Lisp_Object window)
14357 {
14358 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14359 redisplay_window (window, false);
14360 return Qnil;
14361 }
14362
14363 static Lisp_Object
14364 redisplay_window_1 (Lisp_Object window)
14365 {
14366 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14367 redisplay_window (window, true);
14368 return Qnil;
14369 }
14370 \f
14371
14372 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14373 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14374 which positions recorded in ROW differ from current buffer
14375 positions.
14376
14377 Return true iff cursor is on this row. */
14378
14379 static bool
14380 set_cursor_from_row (struct window *w, struct glyph_row *row,
14381 struct glyph_matrix *matrix,
14382 ptrdiff_t delta, ptrdiff_t delta_bytes,
14383 int dy, int dvpos)
14384 {
14385 struct glyph *glyph = row->glyphs[TEXT_AREA];
14386 struct glyph *end = glyph + row->used[TEXT_AREA];
14387 struct glyph *cursor = NULL;
14388 /* The last known character position in row. */
14389 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14390 int x = row->x;
14391 ptrdiff_t pt_old = PT - delta;
14392 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14393 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14394 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14395 /* A glyph beyond the edge of TEXT_AREA which we should never
14396 touch. */
14397 struct glyph *glyphs_end = end;
14398 /* True means we've found a match for cursor position, but that
14399 glyph has the avoid_cursor_p flag set. */
14400 bool match_with_avoid_cursor = false;
14401 /* True means we've seen at least one glyph that came from a
14402 display string. */
14403 bool string_seen = false;
14404 /* Largest and smallest buffer positions seen so far during scan of
14405 glyph row. */
14406 ptrdiff_t bpos_max = pos_before;
14407 ptrdiff_t bpos_min = pos_after;
14408 /* Last buffer position covered by an overlay string with an integer
14409 `cursor' property. */
14410 ptrdiff_t bpos_covered = 0;
14411 /* True means the display string on which to display the cursor
14412 comes from a text property, not from an overlay. */
14413 bool string_from_text_prop = false;
14414
14415 /* Don't even try doing anything if called for a mode-line or
14416 header-line row, since the rest of the code isn't prepared to
14417 deal with such calamities. */
14418 eassert (!row->mode_line_p);
14419 if (row->mode_line_p)
14420 return false;
14421
14422 /* Skip over glyphs not having an object at the start and the end of
14423 the row. These are special glyphs like truncation marks on
14424 terminal frames. */
14425 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14426 {
14427 if (!row->reversed_p)
14428 {
14429 while (glyph < end
14430 && NILP (glyph->object)
14431 && glyph->charpos < 0)
14432 {
14433 x += glyph->pixel_width;
14434 ++glyph;
14435 }
14436 while (end > glyph
14437 && NILP ((end - 1)->object)
14438 /* CHARPOS is zero for blanks and stretch glyphs
14439 inserted by extend_face_to_end_of_line. */
14440 && (end - 1)->charpos <= 0)
14441 --end;
14442 glyph_before = glyph - 1;
14443 glyph_after = end;
14444 }
14445 else
14446 {
14447 struct glyph *g;
14448
14449 /* If the glyph row is reversed, we need to process it from back
14450 to front, so swap the edge pointers. */
14451 glyphs_end = end = glyph - 1;
14452 glyph += row->used[TEXT_AREA] - 1;
14453
14454 while (glyph > end + 1
14455 && NILP (glyph->object)
14456 && glyph->charpos < 0)
14457 {
14458 --glyph;
14459 x -= glyph->pixel_width;
14460 }
14461 if (NILP (glyph->object) && glyph->charpos < 0)
14462 --glyph;
14463 /* By default, in reversed rows we put the cursor on the
14464 rightmost (first in the reading order) glyph. */
14465 for (g = end + 1; g < glyph; g++)
14466 x += g->pixel_width;
14467 while (end < glyph
14468 && NILP ((end + 1)->object)
14469 && (end + 1)->charpos <= 0)
14470 ++end;
14471 glyph_before = glyph + 1;
14472 glyph_after = end;
14473 }
14474 }
14475 else if (row->reversed_p)
14476 {
14477 /* In R2L rows that don't display text, put the cursor on the
14478 rightmost glyph. Case in point: an empty last line that is
14479 part of an R2L paragraph. */
14480 cursor = end - 1;
14481 /* Avoid placing the cursor on the last glyph of the row, where
14482 on terminal frames we hold the vertical border between
14483 adjacent windows. */
14484 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14485 && !WINDOW_RIGHTMOST_P (w)
14486 && cursor == row->glyphs[LAST_AREA] - 1)
14487 cursor--;
14488 x = -1; /* will be computed below, at label compute_x */
14489 }
14490
14491 /* Step 1: Try to find the glyph whose character position
14492 corresponds to point. If that's not possible, find 2 glyphs
14493 whose character positions are the closest to point, one before
14494 point, the other after it. */
14495 if (!row->reversed_p)
14496 while (/* not marched to end of glyph row */
14497 glyph < end
14498 /* glyph was not inserted by redisplay for internal purposes */
14499 && !NILP (glyph->object))
14500 {
14501 if (BUFFERP (glyph->object))
14502 {
14503 ptrdiff_t dpos = glyph->charpos - pt_old;
14504
14505 if (glyph->charpos > bpos_max)
14506 bpos_max = glyph->charpos;
14507 if (glyph->charpos < bpos_min)
14508 bpos_min = glyph->charpos;
14509 if (!glyph->avoid_cursor_p)
14510 {
14511 /* If we hit point, we've found the glyph on which to
14512 display the cursor. */
14513 if (dpos == 0)
14514 {
14515 match_with_avoid_cursor = false;
14516 break;
14517 }
14518 /* See if we've found a better approximation to
14519 POS_BEFORE or to POS_AFTER. */
14520 if (0 > dpos && dpos > pos_before - pt_old)
14521 {
14522 pos_before = glyph->charpos;
14523 glyph_before = glyph;
14524 }
14525 else if (0 < dpos && dpos < pos_after - pt_old)
14526 {
14527 pos_after = glyph->charpos;
14528 glyph_after = glyph;
14529 }
14530 }
14531 else if (dpos == 0)
14532 match_with_avoid_cursor = true;
14533 }
14534 else if (STRINGP (glyph->object))
14535 {
14536 Lisp_Object chprop;
14537 ptrdiff_t glyph_pos = glyph->charpos;
14538
14539 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14540 glyph->object);
14541 if (!NILP (chprop))
14542 {
14543 /* If the string came from a `display' text property,
14544 look up the buffer position of that property and
14545 use that position to update bpos_max, as if we
14546 actually saw such a position in one of the row's
14547 glyphs. This helps with supporting integer values
14548 of `cursor' property on the display string in
14549 situations where most or all of the row's buffer
14550 text is completely covered by display properties,
14551 so that no glyph with valid buffer positions is
14552 ever seen in the row. */
14553 ptrdiff_t prop_pos =
14554 string_buffer_position_lim (glyph->object, pos_before,
14555 pos_after, false);
14556
14557 if (prop_pos >= pos_before)
14558 bpos_max = prop_pos;
14559 }
14560 if (INTEGERP (chprop))
14561 {
14562 bpos_covered = bpos_max + XINT (chprop);
14563 /* If the `cursor' property covers buffer positions up
14564 to and including point, we should display cursor on
14565 this glyph. Note that, if a `cursor' property on one
14566 of the string's characters has an integer value, we
14567 will break out of the loop below _before_ we get to
14568 the position match above. IOW, integer values of
14569 the `cursor' property override the "exact match for
14570 point" strategy of positioning the cursor. */
14571 /* Implementation note: bpos_max == pt_old when, e.g.,
14572 we are in an empty line, where bpos_max is set to
14573 MATRIX_ROW_START_CHARPOS, see above. */
14574 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14575 {
14576 cursor = glyph;
14577 break;
14578 }
14579 }
14580
14581 string_seen = true;
14582 }
14583 x += glyph->pixel_width;
14584 ++glyph;
14585 }
14586 else if (glyph > end) /* row is reversed */
14587 while (!NILP (glyph->object))
14588 {
14589 if (BUFFERP (glyph->object))
14590 {
14591 ptrdiff_t dpos = glyph->charpos - pt_old;
14592
14593 if (glyph->charpos > bpos_max)
14594 bpos_max = glyph->charpos;
14595 if (glyph->charpos < bpos_min)
14596 bpos_min = glyph->charpos;
14597 if (!glyph->avoid_cursor_p)
14598 {
14599 if (dpos == 0)
14600 {
14601 match_with_avoid_cursor = false;
14602 break;
14603 }
14604 if (0 > dpos && dpos > pos_before - pt_old)
14605 {
14606 pos_before = glyph->charpos;
14607 glyph_before = glyph;
14608 }
14609 else if (0 < dpos && dpos < pos_after - pt_old)
14610 {
14611 pos_after = glyph->charpos;
14612 glyph_after = glyph;
14613 }
14614 }
14615 else if (dpos == 0)
14616 match_with_avoid_cursor = true;
14617 }
14618 else if (STRINGP (glyph->object))
14619 {
14620 Lisp_Object chprop;
14621 ptrdiff_t glyph_pos = glyph->charpos;
14622
14623 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14624 glyph->object);
14625 if (!NILP (chprop))
14626 {
14627 ptrdiff_t prop_pos =
14628 string_buffer_position_lim (glyph->object, pos_before,
14629 pos_after, false);
14630
14631 if (prop_pos >= pos_before)
14632 bpos_max = prop_pos;
14633 }
14634 if (INTEGERP (chprop))
14635 {
14636 bpos_covered = bpos_max + XINT (chprop);
14637 /* If the `cursor' property covers buffer positions up
14638 to and including point, we should display cursor on
14639 this glyph. */
14640 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14641 {
14642 cursor = glyph;
14643 break;
14644 }
14645 }
14646 string_seen = true;
14647 }
14648 --glyph;
14649 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14650 {
14651 x--; /* can't use any pixel_width */
14652 break;
14653 }
14654 x -= glyph->pixel_width;
14655 }
14656
14657 /* Step 2: If we didn't find an exact match for point, we need to
14658 look for a proper place to put the cursor among glyphs between
14659 GLYPH_BEFORE and GLYPH_AFTER. */
14660 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14661 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14662 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14663 {
14664 /* An empty line has a single glyph whose OBJECT is nil and
14665 whose CHARPOS is the position of a newline on that line.
14666 Note that on a TTY, there are more glyphs after that, which
14667 were produced by extend_face_to_end_of_line, but their
14668 CHARPOS is zero or negative. */
14669 bool empty_line_p =
14670 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14671 && NILP (glyph->object) && glyph->charpos > 0
14672 /* On a TTY, continued and truncated rows also have a glyph at
14673 their end whose OBJECT is nil and whose CHARPOS is
14674 positive (the continuation and truncation glyphs), but such
14675 rows are obviously not "empty". */
14676 && !(row->continued_p || row->truncated_on_right_p));
14677
14678 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14679 {
14680 ptrdiff_t ellipsis_pos;
14681
14682 /* Scan back over the ellipsis glyphs. */
14683 if (!row->reversed_p)
14684 {
14685 ellipsis_pos = (glyph - 1)->charpos;
14686 while (glyph > row->glyphs[TEXT_AREA]
14687 && (glyph - 1)->charpos == ellipsis_pos)
14688 glyph--, x -= glyph->pixel_width;
14689 /* That loop always goes one position too far, including
14690 the glyph before the ellipsis. So scan forward over
14691 that one. */
14692 x += glyph->pixel_width;
14693 glyph++;
14694 }
14695 else /* row is reversed */
14696 {
14697 ellipsis_pos = (glyph + 1)->charpos;
14698 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14699 && (glyph + 1)->charpos == ellipsis_pos)
14700 glyph++, x += glyph->pixel_width;
14701 x -= glyph->pixel_width;
14702 glyph--;
14703 }
14704 }
14705 else if (match_with_avoid_cursor)
14706 {
14707 cursor = glyph_after;
14708 x = -1;
14709 }
14710 else if (string_seen)
14711 {
14712 int incr = row->reversed_p ? -1 : +1;
14713
14714 /* Need to find the glyph that came out of a string which is
14715 present at point. That glyph is somewhere between
14716 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14717 positioned between POS_BEFORE and POS_AFTER in the
14718 buffer. */
14719 struct glyph *start, *stop;
14720 ptrdiff_t pos = pos_before;
14721
14722 x = -1;
14723
14724 /* If the row ends in a newline from a display string,
14725 reordering could have moved the glyphs belonging to the
14726 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14727 in this case we extend the search to the last glyph in
14728 the row that was not inserted by redisplay. */
14729 if (row->ends_in_newline_from_string_p)
14730 {
14731 glyph_after = end;
14732 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14733 }
14734
14735 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14736 correspond to POS_BEFORE and POS_AFTER, respectively. We
14737 need START and STOP in the order that corresponds to the
14738 row's direction as given by its reversed_p flag. If the
14739 directionality of characters between POS_BEFORE and
14740 POS_AFTER is the opposite of the row's base direction,
14741 these characters will have been reordered for display,
14742 and we need to reverse START and STOP. */
14743 if (!row->reversed_p)
14744 {
14745 start = min (glyph_before, glyph_after);
14746 stop = max (glyph_before, glyph_after);
14747 }
14748 else
14749 {
14750 start = max (glyph_before, glyph_after);
14751 stop = min (glyph_before, glyph_after);
14752 }
14753 for (glyph = start + incr;
14754 row->reversed_p ? glyph > stop : glyph < stop; )
14755 {
14756
14757 /* Any glyphs that come from the buffer are here because
14758 of bidi reordering. Skip them, and only pay
14759 attention to glyphs that came from some string. */
14760 if (STRINGP (glyph->object))
14761 {
14762 Lisp_Object str;
14763 ptrdiff_t tem;
14764 /* If the display property covers the newline, we
14765 need to search for it one position farther. */
14766 ptrdiff_t lim = pos_after
14767 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14768
14769 string_from_text_prop = false;
14770 str = glyph->object;
14771 tem = string_buffer_position_lim (str, pos, lim, false);
14772 if (tem == 0 /* from overlay */
14773 || pos <= tem)
14774 {
14775 /* If the string from which this glyph came is
14776 found in the buffer at point, or at position
14777 that is closer to point than pos_after, then
14778 we've found the glyph we've been looking for.
14779 If it comes from an overlay (tem == 0), and
14780 it has the `cursor' property on one of its
14781 glyphs, record that glyph as a candidate for
14782 displaying the cursor. (As in the
14783 unidirectional version, we will display the
14784 cursor on the last candidate we find.) */
14785 if (tem == 0
14786 || tem == pt_old
14787 || (tem - pt_old > 0 && tem < pos_after))
14788 {
14789 /* The glyphs from this string could have
14790 been reordered. Find the one with the
14791 smallest string position. Or there could
14792 be a character in the string with the
14793 `cursor' property, which means display
14794 cursor on that character's glyph. */
14795 ptrdiff_t strpos = glyph->charpos;
14796
14797 if (tem)
14798 {
14799 cursor = glyph;
14800 string_from_text_prop = true;
14801 }
14802 for ( ;
14803 (row->reversed_p ? glyph > stop : glyph < stop)
14804 && EQ (glyph->object, str);
14805 glyph += incr)
14806 {
14807 Lisp_Object cprop;
14808 ptrdiff_t gpos = glyph->charpos;
14809
14810 cprop = Fget_char_property (make_number (gpos),
14811 Qcursor,
14812 glyph->object);
14813 if (!NILP (cprop))
14814 {
14815 cursor = glyph;
14816 break;
14817 }
14818 if (tem && glyph->charpos < strpos)
14819 {
14820 strpos = glyph->charpos;
14821 cursor = glyph;
14822 }
14823 }
14824
14825 if (tem == pt_old
14826 || (tem - pt_old > 0 && tem < pos_after))
14827 goto compute_x;
14828 }
14829 if (tem)
14830 pos = tem + 1; /* don't find previous instances */
14831 }
14832 /* This string is not what we want; skip all of the
14833 glyphs that came from it. */
14834 while ((row->reversed_p ? glyph > stop : glyph < stop)
14835 && EQ (glyph->object, str))
14836 glyph += incr;
14837 }
14838 else
14839 glyph += incr;
14840 }
14841
14842 /* If we reached the end of the line, and END was from a string,
14843 the cursor is not on this line. */
14844 if (cursor == NULL
14845 && (row->reversed_p ? glyph <= end : glyph >= end)
14846 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14847 && STRINGP (end->object)
14848 && row->continued_p)
14849 return false;
14850 }
14851 /* A truncated row may not include PT among its character positions.
14852 Setting the cursor inside the scroll margin will trigger
14853 recalculation of hscroll in hscroll_window_tree. But if a
14854 display string covers point, defer to the string-handling
14855 code below to figure this out. */
14856 else if (row->truncated_on_left_p && pt_old < bpos_min)
14857 {
14858 cursor = glyph_before;
14859 x = -1;
14860 }
14861 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14862 /* Zero-width characters produce no glyphs. */
14863 || (!empty_line_p
14864 && (row->reversed_p
14865 ? glyph_after > glyphs_end
14866 : glyph_after < glyphs_end)))
14867 {
14868 cursor = glyph_after;
14869 x = -1;
14870 }
14871 }
14872
14873 compute_x:
14874 if (cursor != NULL)
14875 glyph = cursor;
14876 else if (glyph == glyphs_end
14877 && pos_before == pos_after
14878 && STRINGP ((row->reversed_p
14879 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14880 : row->glyphs[TEXT_AREA])->object))
14881 {
14882 /* If all the glyphs of this row came from strings, put the
14883 cursor on the first glyph of the row. This avoids having the
14884 cursor outside of the text area in this very rare and hard
14885 use case. */
14886 glyph =
14887 row->reversed_p
14888 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14889 : row->glyphs[TEXT_AREA];
14890 }
14891 if (x < 0)
14892 {
14893 struct glyph *g;
14894
14895 /* Need to compute x that corresponds to GLYPH. */
14896 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14897 {
14898 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14899 emacs_abort ();
14900 x += g->pixel_width;
14901 }
14902 }
14903
14904 /* ROW could be part of a continued line, which, under bidi
14905 reordering, might have other rows whose start and end charpos
14906 occlude point. Only set w->cursor if we found a better
14907 approximation to the cursor position than we have from previously
14908 examined candidate rows belonging to the same continued line. */
14909 if (/* We already have a candidate row. */
14910 w->cursor.vpos >= 0
14911 /* That candidate is not the row we are processing. */
14912 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14913 /* Make sure cursor.vpos specifies a row whose start and end
14914 charpos occlude point, and it is valid candidate for being a
14915 cursor-row. This is because some callers of this function
14916 leave cursor.vpos at the row where the cursor was displayed
14917 during the last redisplay cycle. */
14918 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14919 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14920 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14921 {
14922 struct glyph *g1
14923 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14924
14925 /* Don't consider glyphs that are outside TEXT_AREA. */
14926 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14927 return false;
14928 /* Keep the candidate whose buffer position is the closest to
14929 point or has the `cursor' property. */
14930 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14931 w->cursor.hpos >= 0
14932 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14933 && ((BUFFERP (g1->object)
14934 && (g1->charpos == pt_old /* An exact match always wins. */
14935 || (BUFFERP (glyph->object)
14936 && eabs (g1->charpos - pt_old)
14937 < eabs (glyph->charpos - pt_old))))
14938 /* Previous candidate is a glyph from a string that has
14939 a non-nil `cursor' property. */
14940 || (STRINGP (g1->object)
14941 && (!NILP (Fget_char_property (make_number (g1->charpos),
14942 Qcursor, g1->object))
14943 /* Previous candidate is from the same display
14944 string as this one, and the display string
14945 came from a text property. */
14946 || (EQ (g1->object, glyph->object)
14947 && string_from_text_prop)
14948 /* this candidate is from newline and its
14949 position is not an exact match */
14950 || (NILP (glyph->object)
14951 && glyph->charpos != pt_old)))))
14952 return false;
14953 /* If this candidate gives an exact match, use that. */
14954 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14955 /* If this candidate is a glyph created for the
14956 terminating newline of a line, and point is on that
14957 newline, it wins because it's an exact match. */
14958 || (!row->continued_p
14959 && NILP (glyph->object)
14960 && glyph->charpos == 0
14961 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14962 /* Otherwise, keep the candidate that comes from a row
14963 spanning less buffer positions. This may win when one or
14964 both candidate positions are on glyphs that came from
14965 display strings, for which we cannot compare buffer
14966 positions. */
14967 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14968 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14969 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14970 return false;
14971 }
14972 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14973 w->cursor.x = x;
14974 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14975 w->cursor.y = row->y + dy;
14976
14977 if (w == XWINDOW (selected_window))
14978 {
14979 if (!row->continued_p
14980 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14981 && row->x == 0)
14982 {
14983 this_line_buffer = XBUFFER (w->contents);
14984
14985 CHARPOS (this_line_start_pos)
14986 = MATRIX_ROW_START_CHARPOS (row) + delta;
14987 BYTEPOS (this_line_start_pos)
14988 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14989
14990 CHARPOS (this_line_end_pos)
14991 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14992 BYTEPOS (this_line_end_pos)
14993 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14994
14995 this_line_y = w->cursor.y;
14996 this_line_pixel_height = row->height;
14997 this_line_vpos = w->cursor.vpos;
14998 this_line_start_x = row->x;
14999 }
15000 else
15001 CHARPOS (this_line_start_pos) = 0;
15002 }
15003
15004 return true;
15005 }
15006
15007
15008 /* Run window scroll functions, if any, for WINDOW with new window
15009 start STARTP. Sets the window start of WINDOW to that position.
15010
15011 We assume that the window's buffer is really current. */
15012
15013 static struct text_pos
15014 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15015 {
15016 struct window *w = XWINDOW (window);
15017 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15018
15019 eassert (current_buffer == XBUFFER (w->contents));
15020
15021 if (!NILP (Vwindow_scroll_functions))
15022 {
15023 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15024 make_number (CHARPOS (startp)));
15025 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15026 /* In case the hook functions switch buffers. */
15027 set_buffer_internal (XBUFFER (w->contents));
15028 }
15029
15030 return startp;
15031 }
15032
15033
15034 /* Make sure the line containing the cursor is fully visible.
15035 A value of true means there is nothing to be done.
15036 (Either the line is fully visible, or it cannot be made so,
15037 or we cannot tell.)
15038
15039 If FORCE_P, return false even if partial visible cursor row
15040 is higher than window.
15041
15042 If CURRENT_MATRIX_P, use the information from the
15043 window's current glyph matrix; otherwise use the desired glyph
15044 matrix.
15045
15046 A value of false means the caller should do scrolling
15047 as if point had gone off the screen. */
15048
15049 static bool
15050 cursor_row_fully_visible_p (struct window *w, bool force_p,
15051 bool current_matrix_p)
15052 {
15053 struct glyph_matrix *matrix;
15054 struct glyph_row *row;
15055 int window_height;
15056
15057 if (!make_cursor_line_fully_visible_p)
15058 return true;
15059
15060 /* It's not always possible to find the cursor, e.g, when a window
15061 is full of overlay strings. Don't do anything in that case. */
15062 if (w->cursor.vpos < 0)
15063 return true;
15064
15065 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15066 row = MATRIX_ROW (matrix, w->cursor.vpos);
15067
15068 /* If the cursor row is not partially visible, there's nothing to do. */
15069 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15070 return true;
15071
15072 /* If the row the cursor is in is taller than the window's height,
15073 it's not clear what to do, so do nothing. */
15074 window_height = window_box_height (w);
15075 if (row->height >= window_height)
15076 {
15077 if (!force_p || MINI_WINDOW_P (w)
15078 || w->vscroll || w->cursor.vpos == 0)
15079 return true;
15080 }
15081 return false;
15082 }
15083
15084
15085 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15086 means only WINDOW is redisplayed in redisplay_internal.
15087 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15088 in redisplay_window to bring a partially visible line into view in
15089 the case that only the cursor has moved.
15090
15091 LAST_LINE_MISFIT should be true if we're scrolling because the
15092 last screen line's vertical height extends past the end of the screen.
15093
15094 Value is
15095
15096 1 if scrolling succeeded
15097
15098 0 if scrolling didn't find point.
15099
15100 -1 if new fonts have been loaded so that we must interrupt
15101 redisplay, adjust glyph matrices, and try again. */
15102
15103 enum
15104 {
15105 SCROLLING_SUCCESS,
15106 SCROLLING_FAILED,
15107 SCROLLING_NEED_LARGER_MATRICES
15108 };
15109
15110 /* If scroll-conservatively is more than this, never recenter.
15111
15112 If you change this, don't forget to update the doc string of
15113 `scroll-conservatively' and the Emacs manual. */
15114 #define SCROLL_LIMIT 100
15115
15116 static int
15117 try_scrolling (Lisp_Object window, bool just_this_one_p,
15118 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15119 bool temp_scroll_step, bool last_line_misfit)
15120 {
15121 struct window *w = XWINDOW (window);
15122 struct frame *f = XFRAME (w->frame);
15123 struct text_pos pos, startp;
15124 struct it it;
15125 int this_scroll_margin, scroll_max, rc, height;
15126 int dy = 0, amount_to_scroll = 0;
15127 bool scroll_down_p = false;
15128 int extra_scroll_margin_lines = last_line_misfit;
15129 Lisp_Object aggressive;
15130 /* We will never try scrolling more than this number of lines. */
15131 int scroll_limit = SCROLL_LIMIT;
15132 int frame_line_height = default_line_pixel_height (w);
15133 int window_total_lines
15134 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15135
15136 #ifdef GLYPH_DEBUG
15137 debug_method_add (w, "try_scrolling");
15138 #endif
15139
15140 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15141
15142 /* Compute scroll margin height in pixels. We scroll when point is
15143 within this distance from the top or bottom of the window. */
15144 if (scroll_margin > 0)
15145 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15146 * frame_line_height;
15147 else
15148 this_scroll_margin = 0;
15149
15150 /* Force arg_scroll_conservatively to have a reasonable value, to
15151 avoid scrolling too far away with slow move_it_* functions. Note
15152 that the user can supply scroll-conservatively equal to
15153 `most-positive-fixnum', which can be larger than INT_MAX. */
15154 if (arg_scroll_conservatively > scroll_limit)
15155 {
15156 arg_scroll_conservatively = scroll_limit + 1;
15157 scroll_max = scroll_limit * frame_line_height;
15158 }
15159 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15160 /* Compute how much we should try to scroll maximally to bring
15161 point into view. */
15162 scroll_max = (max (scroll_step,
15163 max (arg_scroll_conservatively, temp_scroll_step))
15164 * frame_line_height);
15165 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15166 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15167 /* We're trying to scroll because of aggressive scrolling but no
15168 scroll_step is set. Choose an arbitrary one. */
15169 scroll_max = 10 * frame_line_height;
15170 else
15171 scroll_max = 0;
15172
15173 too_near_end:
15174
15175 /* Decide whether to scroll down. */
15176 if (PT > CHARPOS (startp))
15177 {
15178 int scroll_margin_y;
15179
15180 /* Compute the pixel ypos of the scroll margin, then move IT to
15181 either that ypos or PT, whichever comes first. */
15182 start_display (&it, w, startp);
15183 scroll_margin_y = it.last_visible_y - this_scroll_margin
15184 - frame_line_height * extra_scroll_margin_lines;
15185 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15186 (MOVE_TO_POS | MOVE_TO_Y));
15187
15188 if (PT > CHARPOS (it.current.pos))
15189 {
15190 int y0 = line_bottom_y (&it);
15191 /* Compute how many pixels below window bottom to stop searching
15192 for PT. This avoids costly search for PT that is far away if
15193 the user limited scrolling by a small number of lines, but
15194 always finds PT if scroll_conservatively is set to a large
15195 number, such as most-positive-fixnum. */
15196 int slack = max (scroll_max, 10 * frame_line_height);
15197 int y_to_move = it.last_visible_y + slack;
15198
15199 /* Compute the distance from the scroll margin to PT or to
15200 the scroll limit, whichever comes first. This should
15201 include the height of the cursor line, to make that line
15202 fully visible. */
15203 move_it_to (&it, PT, -1, y_to_move,
15204 -1, MOVE_TO_POS | MOVE_TO_Y);
15205 dy = line_bottom_y (&it) - y0;
15206
15207 if (dy > scroll_max)
15208 return SCROLLING_FAILED;
15209
15210 if (dy > 0)
15211 scroll_down_p = true;
15212 }
15213 }
15214
15215 if (scroll_down_p)
15216 {
15217 /* Point is in or below the bottom scroll margin, so move the
15218 window start down. If scrolling conservatively, move it just
15219 enough down to make point visible. If scroll_step is set,
15220 move it down by scroll_step. */
15221 if (arg_scroll_conservatively)
15222 amount_to_scroll
15223 = min (max (dy, frame_line_height),
15224 frame_line_height * arg_scroll_conservatively);
15225 else if (scroll_step || temp_scroll_step)
15226 amount_to_scroll = scroll_max;
15227 else
15228 {
15229 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15230 height = WINDOW_BOX_TEXT_HEIGHT (w);
15231 if (NUMBERP (aggressive))
15232 {
15233 double float_amount = XFLOATINT (aggressive) * height;
15234 int aggressive_scroll = float_amount;
15235 if (aggressive_scroll == 0 && float_amount > 0)
15236 aggressive_scroll = 1;
15237 /* Don't let point enter the scroll margin near top of
15238 the window. This could happen if the value of
15239 scroll_up_aggressively is too large and there are
15240 non-zero margins, because scroll_up_aggressively
15241 means put point that fraction of window height
15242 _from_the_bottom_margin_. */
15243 if (aggressive_scroll + 2 * this_scroll_margin > height)
15244 aggressive_scroll = height - 2 * this_scroll_margin;
15245 amount_to_scroll = dy + aggressive_scroll;
15246 }
15247 }
15248
15249 if (amount_to_scroll <= 0)
15250 return SCROLLING_FAILED;
15251
15252 start_display (&it, w, startp);
15253 if (arg_scroll_conservatively <= scroll_limit)
15254 move_it_vertically (&it, amount_to_scroll);
15255 else
15256 {
15257 /* Extra precision for users who set scroll-conservatively
15258 to a large number: make sure the amount we scroll
15259 the window start is never less than amount_to_scroll,
15260 which was computed as distance from window bottom to
15261 point. This matters when lines at window top and lines
15262 below window bottom have different height. */
15263 struct it it1;
15264 void *it1data = NULL;
15265 /* We use a temporary it1 because line_bottom_y can modify
15266 its argument, if it moves one line down; see there. */
15267 int start_y;
15268
15269 SAVE_IT (it1, it, it1data);
15270 start_y = line_bottom_y (&it1);
15271 do {
15272 RESTORE_IT (&it, &it, it1data);
15273 move_it_by_lines (&it, 1);
15274 SAVE_IT (it1, it, it1data);
15275 } while (IT_CHARPOS (it) < ZV
15276 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15277 bidi_unshelve_cache (it1data, true);
15278 }
15279
15280 /* If STARTP is unchanged, move it down another screen line. */
15281 if (IT_CHARPOS (it) == CHARPOS (startp))
15282 move_it_by_lines (&it, 1);
15283 startp = it.current.pos;
15284 }
15285 else
15286 {
15287 struct text_pos scroll_margin_pos = startp;
15288 int y_offset = 0;
15289
15290 /* See if point is inside the scroll margin at the top of the
15291 window. */
15292 if (this_scroll_margin)
15293 {
15294 int y_start;
15295
15296 start_display (&it, w, startp);
15297 y_start = it.current_y;
15298 move_it_vertically (&it, this_scroll_margin);
15299 scroll_margin_pos = it.current.pos;
15300 /* If we didn't move enough before hitting ZV, request
15301 additional amount of scroll, to move point out of the
15302 scroll margin. */
15303 if (IT_CHARPOS (it) == ZV
15304 && it.current_y - y_start < this_scroll_margin)
15305 y_offset = this_scroll_margin - (it.current_y - y_start);
15306 }
15307
15308 if (PT < CHARPOS (scroll_margin_pos))
15309 {
15310 /* Point is in the scroll margin at the top of the window or
15311 above what is displayed in the window. */
15312 int y0, y_to_move;
15313
15314 /* Compute the vertical distance from PT to the scroll
15315 margin position. Move as far as scroll_max allows, or
15316 one screenful, or 10 screen lines, whichever is largest.
15317 Give up if distance is greater than scroll_max or if we
15318 didn't reach the scroll margin position. */
15319 SET_TEXT_POS (pos, PT, PT_BYTE);
15320 start_display (&it, w, pos);
15321 y0 = it.current_y;
15322 y_to_move = max (it.last_visible_y,
15323 max (scroll_max, 10 * frame_line_height));
15324 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15325 y_to_move, -1,
15326 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15327 dy = it.current_y - y0;
15328 if (dy > scroll_max
15329 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15330 return SCROLLING_FAILED;
15331
15332 /* Additional scroll for when ZV was too close to point. */
15333 dy += y_offset;
15334
15335 /* Compute new window start. */
15336 start_display (&it, w, startp);
15337
15338 if (arg_scroll_conservatively)
15339 amount_to_scroll = max (dy, frame_line_height
15340 * max (scroll_step, temp_scroll_step));
15341 else if (scroll_step || temp_scroll_step)
15342 amount_to_scroll = scroll_max;
15343 else
15344 {
15345 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15346 height = WINDOW_BOX_TEXT_HEIGHT (w);
15347 if (NUMBERP (aggressive))
15348 {
15349 double float_amount = XFLOATINT (aggressive) * height;
15350 int aggressive_scroll = float_amount;
15351 if (aggressive_scroll == 0 && float_amount > 0)
15352 aggressive_scroll = 1;
15353 /* Don't let point enter the scroll margin near
15354 bottom of the window, if the value of
15355 scroll_down_aggressively happens to be too
15356 large. */
15357 if (aggressive_scroll + 2 * this_scroll_margin > height)
15358 aggressive_scroll = height - 2 * this_scroll_margin;
15359 amount_to_scroll = dy + aggressive_scroll;
15360 }
15361 }
15362
15363 if (amount_to_scroll <= 0)
15364 return SCROLLING_FAILED;
15365
15366 move_it_vertically_backward (&it, amount_to_scroll);
15367 startp = it.current.pos;
15368 }
15369 }
15370
15371 /* Run window scroll functions. */
15372 startp = run_window_scroll_functions (window, startp);
15373
15374 /* Display the window. Give up if new fonts are loaded, or if point
15375 doesn't appear. */
15376 if (!try_window (window, startp, 0))
15377 rc = SCROLLING_NEED_LARGER_MATRICES;
15378 else if (w->cursor.vpos < 0)
15379 {
15380 clear_glyph_matrix (w->desired_matrix);
15381 rc = SCROLLING_FAILED;
15382 }
15383 else
15384 {
15385 /* Maybe forget recorded base line for line number display. */
15386 if (!just_this_one_p
15387 || current_buffer->clip_changed
15388 || BEG_UNCHANGED < CHARPOS (startp))
15389 w->base_line_number = 0;
15390
15391 /* If cursor ends up on a partially visible line,
15392 treat that as being off the bottom of the screen. */
15393 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15394 false)
15395 /* It's possible that the cursor is on the first line of the
15396 buffer, which is partially obscured due to a vscroll
15397 (Bug#7537). In that case, avoid looping forever. */
15398 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15399 {
15400 clear_glyph_matrix (w->desired_matrix);
15401 ++extra_scroll_margin_lines;
15402 goto too_near_end;
15403 }
15404 rc = SCROLLING_SUCCESS;
15405 }
15406
15407 return rc;
15408 }
15409
15410
15411 /* Compute a suitable window start for window W if display of W starts
15412 on a continuation line. Value is true if a new window start
15413 was computed.
15414
15415 The new window start will be computed, based on W's width, starting
15416 from the start of the continued line. It is the start of the
15417 screen line with the minimum distance from the old start W->start. */
15418
15419 static bool
15420 compute_window_start_on_continuation_line (struct window *w)
15421 {
15422 struct text_pos pos, start_pos;
15423 bool window_start_changed_p = false;
15424
15425 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15426
15427 /* If window start is on a continuation line... Window start may be
15428 < BEGV in case there's invisible text at the start of the
15429 buffer (M-x rmail, for example). */
15430 if (CHARPOS (start_pos) > BEGV
15431 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15432 {
15433 struct it it;
15434 struct glyph_row *row;
15435
15436 /* Handle the case that the window start is out of range. */
15437 if (CHARPOS (start_pos) < BEGV)
15438 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15439 else if (CHARPOS (start_pos) > ZV)
15440 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15441
15442 /* Find the start of the continued line. This should be fast
15443 because find_newline is fast (newline cache). */
15444 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15445 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15446 row, DEFAULT_FACE_ID);
15447 reseat_at_previous_visible_line_start (&it);
15448
15449 /* If the line start is "too far" away from the window start,
15450 say it takes too much time to compute a new window start. */
15451 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15452 /* PXW: Do we need upper bounds here? */
15453 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15454 {
15455 int min_distance, distance;
15456
15457 /* Move forward by display lines to find the new window
15458 start. If window width was enlarged, the new start can
15459 be expected to be > the old start. If window width was
15460 decreased, the new window start will be < the old start.
15461 So, we're looking for the display line start with the
15462 minimum distance from the old window start. */
15463 pos = it.current.pos;
15464 min_distance = INFINITY;
15465 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15466 distance < min_distance)
15467 {
15468 min_distance = distance;
15469 pos = it.current.pos;
15470 if (it.line_wrap == WORD_WRAP)
15471 {
15472 /* Under WORD_WRAP, move_it_by_lines is likely to
15473 overshoot and stop not at the first, but the
15474 second character from the left margin. So in
15475 that case, we need a more tight control on the X
15476 coordinate of the iterator than move_it_by_lines
15477 promises in its contract. The method is to first
15478 go to the last (rightmost) visible character of a
15479 line, then move to the leftmost character on the
15480 next line in a separate call. */
15481 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15482 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15483 move_it_to (&it, ZV, 0,
15484 it.current_y + it.max_ascent + it.max_descent, -1,
15485 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15486 }
15487 else
15488 move_it_by_lines (&it, 1);
15489 }
15490
15491 /* Set the window start there. */
15492 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15493 window_start_changed_p = true;
15494 }
15495 }
15496
15497 return window_start_changed_p;
15498 }
15499
15500
15501 /* Try cursor movement in case text has not changed in window WINDOW,
15502 with window start STARTP. Value is
15503
15504 CURSOR_MOVEMENT_SUCCESS if successful
15505
15506 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15507
15508 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15509 display. *SCROLL_STEP is set to true, under certain circumstances, if
15510 we want to scroll as if scroll-step were set to 1. See the code.
15511
15512 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15513 which case we have to abort this redisplay, and adjust matrices
15514 first. */
15515
15516 enum
15517 {
15518 CURSOR_MOVEMENT_SUCCESS,
15519 CURSOR_MOVEMENT_CANNOT_BE_USED,
15520 CURSOR_MOVEMENT_MUST_SCROLL,
15521 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15522 };
15523
15524 static int
15525 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15526 bool *scroll_step)
15527 {
15528 struct window *w = XWINDOW (window);
15529 struct frame *f = XFRAME (w->frame);
15530 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15531
15532 #ifdef GLYPH_DEBUG
15533 if (inhibit_try_cursor_movement)
15534 return rc;
15535 #endif
15536
15537 /* Previously, there was a check for Lisp integer in the
15538 if-statement below. Now, this field is converted to
15539 ptrdiff_t, thus zero means invalid position in a buffer. */
15540 eassert (w->last_point > 0);
15541 /* Likewise there was a check whether window_end_vpos is nil or larger
15542 than the window. Now window_end_vpos is int and so never nil, but
15543 let's leave eassert to check whether it fits in the window. */
15544 eassert (!w->window_end_valid
15545 || w->window_end_vpos < w->current_matrix->nrows);
15546
15547 /* Handle case where text has not changed, only point, and it has
15548 not moved off the frame. */
15549 if (/* Point may be in this window. */
15550 PT >= CHARPOS (startp)
15551 /* Selective display hasn't changed. */
15552 && !current_buffer->clip_changed
15553 /* Function force-mode-line-update is used to force a thorough
15554 redisplay. It sets either windows_or_buffers_changed or
15555 update_mode_lines. So don't take a shortcut here for these
15556 cases. */
15557 && !update_mode_lines
15558 && !windows_or_buffers_changed
15559 && !f->cursor_type_changed
15560 && NILP (Vshow_trailing_whitespace)
15561 /* This code is not used for mini-buffer for the sake of the case
15562 of redisplaying to replace an echo area message; since in
15563 that case the mini-buffer contents per se are usually
15564 unchanged. This code is of no real use in the mini-buffer
15565 since the handling of this_line_start_pos, etc., in redisplay
15566 handles the same cases. */
15567 && !EQ (window, minibuf_window)
15568 && (FRAME_WINDOW_P (f)
15569 || !overlay_arrow_in_current_buffer_p ()))
15570 {
15571 int this_scroll_margin, top_scroll_margin;
15572 struct glyph_row *row = NULL;
15573 int frame_line_height = default_line_pixel_height (w);
15574 int window_total_lines
15575 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15576
15577 #ifdef GLYPH_DEBUG
15578 debug_method_add (w, "cursor movement");
15579 #endif
15580
15581 /* Scroll if point within this distance from the top or bottom
15582 of the window. This is a pixel value. */
15583 if (scroll_margin > 0)
15584 {
15585 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15586 this_scroll_margin *= frame_line_height;
15587 }
15588 else
15589 this_scroll_margin = 0;
15590
15591 top_scroll_margin = this_scroll_margin;
15592 if (WINDOW_WANTS_HEADER_LINE_P (w))
15593 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15594
15595 /* Start with the row the cursor was displayed during the last
15596 not paused redisplay. Give up if that row is not valid. */
15597 if (w->last_cursor_vpos < 0
15598 || w->last_cursor_vpos >= w->current_matrix->nrows)
15599 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15600 else
15601 {
15602 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15603 if (row->mode_line_p)
15604 ++row;
15605 if (!row->enabled_p)
15606 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15607 }
15608
15609 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15610 {
15611 bool scroll_p = false, must_scroll = false;
15612 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15613
15614 if (PT > w->last_point)
15615 {
15616 /* Point has moved forward. */
15617 while (MATRIX_ROW_END_CHARPOS (row) < PT
15618 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15619 {
15620 eassert (row->enabled_p);
15621 ++row;
15622 }
15623
15624 /* If the end position of a row equals the start
15625 position of the next row, and PT is at that position,
15626 we would rather display cursor in the next line. */
15627 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15628 && MATRIX_ROW_END_CHARPOS (row) == PT
15629 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15630 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15631 && !cursor_row_p (row))
15632 ++row;
15633
15634 /* If within the scroll margin, scroll. Note that
15635 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15636 the next line would be drawn, and that
15637 this_scroll_margin can be zero. */
15638 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15639 || PT > MATRIX_ROW_END_CHARPOS (row)
15640 /* Line is completely visible last line in window
15641 and PT is to be set in the next line. */
15642 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15643 && PT == MATRIX_ROW_END_CHARPOS (row)
15644 && !row->ends_at_zv_p
15645 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15646 scroll_p = true;
15647 }
15648 else if (PT < w->last_point)
15649 {
15650 /* Cursor has to be moved backward. Note that PT >=
15651 CHARPOS (startp) because of the outer if-statement. */
15652 while (!row->mode_line_p
15653 && (MATRIX_ROW_START_CHARPOS (row) > PT
15654 || (MATRIX_ROW_START_CHARPOS (row) == PT
15655 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15656 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15657 row > w->current_matrix->rows
15658 && (row-1)->ends_in_newline_from_string_p))))
15659 && (row->y > top_scroll_margin
15660 || CHARPOS (startp) == BEGV))
15661 {
15662 eassert (row->enabled_p);
15663 --row;
15664 }
15665
15666 /* Consider the following case: Window starts at BEGV,
15667 there is invisible, intangible text at BEGV, so that
15668 display starts at some point START > BEGV. It can
15669 happen that we are called with PT somewhere between
15670 BEGV and START. Try to handle that case. */
15671 if (row < w->current_matrix->rows
15672 || row->mode_line_p)
15673 {
15674 row = w->current_matrix->rows;
15675 if (row->mode_line_p)
15676 ++row;
15677 }
15678
15679 /* Due to newlines in overlay strings, we may have to
15680 skip forward over overlay strings. */
15681 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15682 && MATRIX_ROW_END_CHARPOS (row) == PT
15683 && !cursor_row_p (row))
15684 ++row;
15685
15686 /* If within the scroll margin, scroll. */
15687 if (row->y < top_scroll_margin
15688 && CHARPOS (startp) != BEGV)
15689 scroll_p = true;
15690 }
15691 else
15692 {
15693 /* Cursor did not move. So don't scroll even if cursor line
15694 is partially visible, as it was so before. */
15695 rc = CURSOR_MOVEMENT_SUCCESS;
15696 }
15697
15698 if (PT < MATRIX_ROW_START_CHARPOS (row)
15699 || PT > MATRIX_ROW_END_CHARPOS (row))
15700 {
15701 /* if PT is not in the glyph row, give up. */
15702 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15703 must_scroll = true;
15704 }
15705 else if (rc != CURSOR_MOVEMENT_SUCCESS
15706 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15707 {
15708 struct glyph_row *row1;
15709
15710 /* If rows are bidi-reordered and point moved, back up
15711 until we find a row that does not belong to a
15712 continuation line. This is because we must consider
15713 all rows of a continued line as candidates for the
15714 new cursor positioning, since row start and end
15715 positions change non-linearly with vertical position
15716 in such rows. */
15717 /* FIXME: Revisit this when glyph ``spilling'' in
15718 continuation lines' rows is implemented for
15719 bidi-reordered rows. */
15720 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15721 MATRIX_ROW_CONTINUATION_LINE_P (row);
15722 --row)
15723 {
15724 /* If we hit the beginning of the displayed portion
15725 without finding the first row of a continued
15726 line, give up. */
15727 if (row <= row1)
15728 {
15729 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15730 break;
15731 }
15732 eassert (row->enabled_p);
15733 }
15734 }
15735 if (must_scroll)
15736 ;
15737 else if (rc != CURSOR_MOVEMENT_SUCCESS
15738 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15739 /* Make sure this isn't a header line by any chance, since
15740 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15741 && !row->mode_line_p
15742 && make_cursor_line_fully_visible_p)
15743 {
15744 if (PT == MATRIX_ROW_END_CHARPOS (row)
15745 && !row->ends_at_zv_p
15746 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15747 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15748 else if (row->height > window_box_height (w))
15749 {
15750 /* If we end up in a partially visible line, let's
15751 make it fully visible, except when it's taller
15752 than the window, in which case we can't do much
15753 about it. */
15754 *scroll_step = true;
15755 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15756 }
15757 else
15758 {
15759 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15760 if (!cursor_row_fully_visible_p (w, false, true))
15761 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15762 else
15763 rc = CURSOR_MOVEMENT_SUCCESS;
15764 }
15765 }
15766 else if (scroll_p)
15767 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15768 else if (rc != CURSOR_MOVEMENT_SUCCESS
15769 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15770 {
15771 /* With bidi-reordered rows, there could be more than
15772 one candidate row whose start and end positions
15773 occlude point. We need to let set_cursor_from_row
15774 find the best candidate. */
15775 /* FIXME: Revisit this when glyph ``spilling'' in
15776 continuation lines' rows is implemented for
15777 bidi-reordered rows. */
15778 bool rv = false;
15779
15780 do
15781 {
15782 bool at_zv_p = false, exact_match_p = false;
15783
15784 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15785 && PT <= MATRIX_ROW_END_CHARPOS (row)
15786 && cursor_row_p (row))
15787 rv |= set_cursor_from_row (w, row, w->current_matrix,
15788 0, 0, 0, 0);
15789 /* As soon as we've found the exact match for point,
15790 or the first suitable row whose ends_at_zv_p flag
15791 is set, we are done. */
15792 if (rv)
15793 {
15794 at_zv_p = MATRIX_ROW (w->current_matrix,
15795 w->cursor.vpos)->ends_at_zv_p;
15796 if (!at_zv_p
15797 && w->cursor.hpos >= 0
15798 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15799 w->cursor.vpos))
15800 {
15801 struct glyph_row *candidate =
15802 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15803 struct glyph *g =
15804 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15805 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15806
15807 exact_match_p =
15808 (BUFFERP (g->object) && g->charpos == PT)
15809 || (NILP (g->object)
15810 && (g->charpos == PT
15811 || (g->charpos == 0 && endpos - 1 == PT)));
15812 }
15813 if (at_zv_p || exact_match_p)
15814 {
15815 rc = CURSOR_MOVEMENT_SUCCESS;
15816 break;
15817 }
15818 }
15819 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15820 break;
15821 ++row;
15822 }
15823 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15824 || row->continued_p)
15825 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15826 || (MATRIX_ROW_START_CHARPOS (row) == PT
15827 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15828 /* If we didn't find any candidate rows, or exited the
15829 loop before all the candidates were examined, signal
15830 to the caller that this method failed. */
15831 if (rc != CURSOR_MOVEMENT_SUCCESS
15832 && !(rv
15833 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15834 && !row->continued_p))
15835 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15836 else if (rv)
15837 rc = CURSOR_MOVEMENT_SUCCESS;
15838 }
15839 else
15840 {
15841 do
15842 {
15843 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15844 {
15845 rc = CURSOR_MOVEMENT_SUCCESS;
15846 break;
15847 }
15848 ++row;
15849 }
15850 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15851 && MATRIX_ROW_START_CHARPOS (row) == PT
15852 && cursor_row_p (row));
15853 }
15854 }
15855 }
15856
15857 return rc;
15858 }
15859
15860
15861 void
15862 set_vertical_scroll_bar (struct window *w)
15863 {
15864 ptrdiff_t start, end, whole;
15865
15866 /* Calculate the start and end positions for the current window.
15867 At some point, it would be nice to choose between scrollbars
15868 which reflect the whole buffer size, with special markers
15869 indicating narrowing, and scrollbars which reflect only the
15870 visible region.
15871
15872 Note that mini-buffers sometimes aren't displaying any text. */
15873 if (!MINI_WINDOW_P (w)
15874 || (w == XWINDOW (minibuf_window)
15875 && NILP (echo_area_buffer[0])))
15876 {
15877 struct buffer *buf = XBUFFER (w->contents);
15878 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15879 start = marker_position (w->start) - BUF_BEGV (buf);
15880 /* I don't think this is guaranteed to be right. For the
15881 moment, we'll pretend it is. */
15882 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15883
15884 if (end < start)
15885 end = start;
15886 if (whole < (end - start))
15887 whole = end - start;
15888 }
15889 else
15890 start = end = whole = 0;
15891
15892 /* Indicate what this scroll bar ought to be displaying now. */
15893 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15894 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15895 (w, end - start, whole, start);
15896 }
15897
15898
15899 void
15900 set_horizontal_scroll_bar (struct window *w)
15901 {
15902 int start, end, whole, portion;
15903
15904 if (!MINI_WINDOW_P (w)
15905 || (w == XWINDOW (minibuf_window)
15906 && NILP (echo_area_buffer[0])))
15907 {
15908 struct buffer *b = XBUFFER (w->contents);
15909 struct buffer *old_buffer = NULL;
15910 struct it it;
15911 struct text_pos startp;
15912
15913 if (b != current_buffer)
15914 {
15915 old_buffer = current_buffer;
15916 set_buffer_internal (b);
15917 }
15918
15919 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15920 start_display (&it, w, startp);
15921 it.last_visible_x = INT_MAX;
15922 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15923 MOVE_TO_X | MOVE_TO_Y);
15924 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15925 window_box_height (w), -1,
15926 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15927
15928 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15929 end = start + window_box_width (w, TEXT_AREA);
15930 portion = end - start;
15931 /* After enlarging a horizontally scrolled window such that it
15932 gets at least as wide as the text it contains, make sure that
15933 the thumb doesn't fill the entire scroll bar so we can still
15934 drag it back to see the entire text. */
15935 whole = max (whole, end);
15936
15937 if (it.bidi_p)
15938 {
15939 Lisp_Object pdir;
15940
15941 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15942 if (EQ (pdir, Qright_to_left))
15943 {
15944 start = whole - end;
15945 end = start + portion;
15946 }
15947 }
15948
15949 if (old_buffer)
15950 set_buffer_internal (old_buffer);
15951 }
15952 else
15953 start = end = whole = portion = 0;
15954
15955 w->hscroll_whole = whole;
15956
15957 /* Indicate what this scroll bar ought to be displaying now. */
15958 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15959 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15960 (w, portion, whole, start);
15961 }
15962
15963
15964 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
15965 selected_window is redisplayed.
15966
15967 We can return without actually redisplaying the window if fonts has been
15968 changed on window's frame. In that case, redisplay_internal will retry.
15969
15970 As one of the important parts of redisplaying a window, we need to
15971 decide whether the previous window-start position (stored in the
15972 window's w->start marker position) is still valid, and if it isn't,
15973 recompute it. Some details about that:
15974
15975 . The previous window-start could be in a continuation line, in
15976 which case we need to recompute it when the window width
15977 changes. See compute_window_start_on_continuation_line and its
15978 call below.
15979
15980 . The text that changed since last redisplay could include the
15981 previous window-start position. In that case, we try to salvage
15982 what we can from the current glyph matrix by calling
15983 try_scrolling, which see.
15984
15985 . Some Emacs command could force us to use a specific window-start
15986 position by setting the window's force_start flag, or gently
15987 propose doing that by setting the window's optional_new_start
15988 flag. In these cases, we try using the specified start point if
15989 that succeeds (i.e. the window desired matrix is successfully
15990 recomputed, and point location is within the window). In case
15991 of optional_new_start, we first check if the specified start
15992 position is feasible, i.e. if it will allow point to be
15993 displayed in the window. If using the specified start point
15994 fails, e.g., if new fonts are needed to be loaded, we abort the
15995 redisplay cycle and leave it up to the next cycle to figure out
15996 things.
15997
15998 . Note that the window's force_start flag is sometimes set by
15999 redisplay itself, when it decides that the previous window start
16000 point is fine and should be kept. Search for "goto force_start"
16001 below to see the details. Like the values of window-start
16002 specified outside of redisplay, these internally-deduced values
16003 are tested for feasibility, and ignored if found to be
16004 unfeasible.
16005
16006 . Note that the function try_window, used to completely redisplay
16007 a window, accepts the window's start point as its argument.
16008 This is used several times in the redisplay code to control
16009 where the window start will be, according to user options such
16010 as scroll-conservatively, and also to ensure the screen line
16011 showing point will be fully (as opposed to partially) visible on
16012 display. */
16013
16014 static void
16015 redisplay_window (Lisp_Object window, bool just_this_one_p)
16016 {
16017 struct window *w = XWINDOW (window);
16018 struct frame *f = XFRAME (w->frame);
16019 struct buffer *buffer = XBUFFER (w->contents);
16020 struct buffer *old = current_buffer;
16021 struct text_pos lpoint, opoint, startp;
16022 bool update_mode_line;
16023 int tem;
16024 struct it it;
16025 /* Record it now because it's overwritten. */
16026 bool current_matrix_up_to_date_p = false;
16027 bool used_current_matrix_p = false;
16028 /* This is less strict than current_matrix_up_to_date_p.
16029 It indicates that the buffer contents and narrowing are unchanged. */
16030 bool buffer_unchanged_p = false;
16031 bool temp_scroll_step = false;
16032 ptrdiff_t count = SPECPDL_INDEX ();
16033 int rc;
16034 int centering_position = -1;
16035 bool last_line_misfit = false;
16036 ptrdiff_t beg_unchanged, end_unchanged;
16037 int frame_line_height;
16038
16039 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16040 opoint = lpoint;
16041
16042 #ifdef GLYPH_DEBUG
16043 *w->desired_matrix->method = 0;
16044 #endif
16045
16046 if (!just_this_one_p
16047 && REDISPLAY_SOME_P ()
16048 && !w->redisplay
16049 && !w->update_mode_line
16050 && !f->face_change
16051 && !f->redisplay
16052 && !buffer->text->redisplay
16053 && BUF_PT (buffer) == w->last_point)
16054 return;
16055
16056 /* Make sure that both W's markers are valid. */
16057 eassert (XMARKER (w->start)->buffer == buffer);
16058 eassert (XMARKER (w->pointm)->buffer == buffer);
16059
16060 /* We come here again if we need to run window-text-change-functions
16061 below. */
16062 restart:
16063 reconsider_clip_changes (w);
16064 frame_line_height = default_line_pixel_height (w);
16065
16066 /* Has the mode line to be updated? */
16067 update_mode_line = (w->update_mode_line
16068 || update_mode_lines
16069 || buffer->clip_changed
16070 || buffer->prevent_redisplay_optimizations_p);
16071
16072 if (!just_this_one_p)
16073 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16074 cleverly elsewhere. */
16075 w->must_be_updated_p = true;
16076
16077 if (MINI_WINDOW_P (w))
16078 {
16079 if (w == XWINDOW (echo_area_window)
16080 && !NILP (echo_area_buffer[0]))
16081 {
16082 if (update_mode_line)
16083 /* We may have to update a tty frame's menu bar or a
16084 tool-bar. Example `M-x C-h C-h C-g'. */
16085 goto finish_menu_bars;
16086 else
16087 /* We've already displayed the echo area glyphs in this window. */
16088 goto finish_scroll_bars;
16089 }
16090 else if ((w != XWINDOW (minibuf_window)
16091 || minibuf_level == 0)
16092 /* When buffer is nonempty, redisplay window normally. */
16093 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16094 /* Quail displays non-mini buffers in minibuffer window.
16095 In that case, redisplay the window normally. */
16096 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16097 {
16098 /* W is a mini-buffer window, but it's not active, so clear
16099 it. */
16100 int yb = window_text_bottom_y (w);
16101 struct glyph_row *row;
16102 int y;
16103
16104 for (y = 0, row = w->desired_matrix->rows;
16105 y < yb;
16106 y += row->height, ++row)
16107 blank_row (w, row, y);
16108 goto finish_scroll_bars;
16109 }
16110
16111 clear_glyph_matrix (w->desired_matrix);
16112 }
16113
16114 /* Otherwise set up data on this window; select its buffer and point
16115 value. */
16116 /* Really select the buffer, for the sake of buffer-local
16117 variables. */
16118 set_buffer_internal_1 (XBUFFER (w->contents));
16119
16120 current_matrix_up_to_date_p
16121 = (w->window_end_valid
16122 && !current_buffer->clip_changed
16123 && !current_buffer->prevent_redisplay_optimizations_p
16124 && !window_outdated (w));
16125
16126 /* Run the window-text-change-functions
16127 if it is possible that the text on the screen has changed
16128 (either due to modification of the text, or any other reason). */
16129 if (!current_matrix_up_to_date_p
16130 && !NILP (Vwindow_text_change_functions))
16131 {
16132 safe_run_hooks (Qwindow_text_change_functions);
16133 goto restart;
16134 }
16135
16136 beg_unchanged = BEG_UNCHANGED;
16137 end_unchanged = END_UNCHANGED;
16138
16139 SET_TEXT_POS (opoint, PT, PT_BYTE);
16140
16141 specbind (Qinhibit_point_motion_hooks, Qt);
16142
16143 buffer_unchanged_p
16144 = (w->window_end_valid
16145 && !current_buffer->clip_changed
16146 && !window_outdated (w));
16147
16148 /* When windows_or_buffers_changed is non-zero, we can't rely
16149 on the window end being valid, so set it to zero there. */
16150 if (windows_or_buffers_changed)
16151 {
16152 /* If window starts on a continuation line, maybe adjust the
16153 window start in case the window's width changed. */
16154 if (XMARKER (w->start)->buffer == current_buffer)
16155 compute_window_start_on_continuation_line (w);
16156
16157 w->window_end_valid = false;
16158 /* If so, we also can't rely on current matrix
16159 and should not fool try_cursor_movement below. */
16160 current_matrix_up_to_date_p = false;
16161 }
16162
16163 /* Some sanity checks. */
16164 CHECK_WINDOW_END (w);
16165 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16166 emacs_abort ();
16167 if (BYTEPOS (opoint) < CHARPOS (opoint))
16168 emacs_abort ();
16169
16170 if (mode_line_update_needed (w))
16171 update_mode_line = true;
16172
16173 /* Point refers normally to the selected window. For any other
16174 window, set up appropriate value. */
16175 if (!EQ (window, selected_window))
16176 {
16177 ptrdiff_t new_pt = marker_position (w->pointm);
16178 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16179
16180 if (new_pt < BEGV)
16181 {
16182 new_pt = BEGV;
16183 new_pt_byte = BEGV_BYTE;
16184 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16185 }
16186 else if (new_pt > (ZV - 1))
16187 {
16188 new_pt = ZV;
16189 new_pt_byte = ZV_BYTE;
16190 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16191 }
16192
16193 /* We don't use SET_PT so that the point-motion hooks don't run. */
16194 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16195 }
16196
16197 /* If any of the character widths specified in the display table
16198 have changed, invalidate the width run cache. It's true that
16199 this may be a bit late to catch such changes, but the rest of
16200 redisplay goes (non-fatally) haywire when the display table is
16201 changed, so why should we worry about doing any better? */
16202 if (current_buffer->width_run_cache
16203 || (current_buffer->base_buffer
16204 && current_buffer->base_buffer->width_run_cache))
16205 {
16206 struct Lisp_Char_Table *disptab = buffer_display_table ();
16207
16208 if (! disptab_matches_widthtab
16209 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16210 {
16211 struct buffer *buf = current_buffer;
16212
16213 if (buf->base_buffer)
16214 buf = buf->base_buffer;
16215 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16216 recompute_width_table (current_buffer, disptab);
16217 }
16218 }
16219
16220 /* If window-start is screwed up, choose a new one. */
16221 if (XMARKER (w->start)->buffer != current_buffer)
16222 goto recenter;
16223
16224 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16225
16226 /* If someone specified a new starting point but did not insist,
16227 check whether it can be used. */
16228 if ((w->optional_new_start || window_frozen_p (w))
16229 && CHARPOS (startp) >= BEGV
16230 && CHARPOS (startp) <= ZV)
16231 {
16232 ptrdiff_t it_charpos;
16233
16234 w->optional_new_start = false;
16235 start_display (&it, w, startp);
16236 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16237 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16238 /* Record IT's position now, since line_bottom_y might change
16239 that. */
16240 it_charpos = IT_CHARPOS (it);
16241 /* Make sure we set the force_start flag only if the cursor row
16242 will be fully visible. Otherwise, the code under force_start
16243 label below will try to move point back into view, which is
16244 not what the code which sets optional_new_start wants. */
16245 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16246 && !w->force_start)
16247 {
16248 if (it_charpos == PT)
16249 w->force_start = true;
16250 /* IT may overshoot PT if text at PT is invisible. */
16251 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16252 w->force_start = true;
16253 #ifdef GLYPH_DEBUG
16254 if (w->force_start)
16255 {
16256 if (window_frozen_p (w))
16257 debug_method_add (w, "set force_start from frozen window start");
16258 else
16259 debug_method_add (w, "set force_start from optional_new_start");
16260 }
16261 #endif
16262 }
16263 }
16264
16265 force_start:
16266
16267 /* Handle case where place to start displaying has been specified,
16268 unless the specified location is outside the accessible range. */
16269 if (w->force_start)
16270 {
16271 /* We set this later on if we have to adjust point. */
16272 int new_vpos = -1;
16273
16274 w->force_start = false;
16275 w->vscroll = 0;
16276 w->window_end_valid = false;
16277
16278 /* Forget any recorded base line for line number display. */
16279 if (!buffer_unchanged_p)
16280 w->base_line_number = 0;
16281
16282 /* Redisplay the mode line. Select the buffer properly for that.
16283 Also, run the hook window-scroll-functions
16284 because we have scrolled. */
16285 /* Note, we do this after clearing force_start because
16286 if there's an error, it is better to forget about force_start
16287 than to get into an infinite loop calling the hook functions
16288 and having them get more errors. */
16289 if (!update_mode_line
16290 || ! NILP (Vwindow_scroll_functions))
16291 {
16292 update_mode_line = true;
16293 w->update_mode_line = true;
16294 startp = run_window_scroll_functions (window, startp);
16295 }
16296
16297 if (CHARPOS (startp) < BEGV)
16298 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16299 else if (CHARPOS (startp) > ZV)
16300 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16301
16302 /* Redisplay, then check if cursor has been set during the
16303 redisplay. Give up if new fonts were loaded. */
16304 /* We used to issue a CHECK_MARGINS argument to try_window here,
16305 but this causes scrolling to fail when point begins inside
16306 the scroll margin (bug#148) -- cyd */
16307 if (!try_window (window, startp, 0))
16308 {
16309 w->force_start = true;
16310 clear_glyph_matrix (w->desired_matrix);
16311 goto need_larger_matrices;
16312 }
16313
16314 if (w->cursor.vpos < 0)
16315 {
16316 /* If point does not appear, try to move point so it does
16317 appear. The desired matrix has been built above, so we
16318 can use it here. First see if point is in invisible
16319 text, and if so, move it to the first visible buffer
16320 position past that. */
16321 struct glyph_row *r = NULL;
16322 Lisp_Object invprop =
16323 get_char_property_and_overlay (make_number (PT), Qinvisible,
16324 Qnil, NULL);
16325
16326 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16327 {
16328 ptrdiff_t alt_pt;
16329 Lisp_Object invprop_end =
16330 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16331 Qnil, Qnil);
16332
16333 if (NATNUMP (invprop_end))
16334 alt_pt = XFASTINT (invprop_end);
16335 else
16336 alt_pt = ZV;
16337 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16338 NULL, 0);
16339 }
16340 if (r)
16341 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16342 else /* Give up and just move to the middle of the window. */
16343 new_vpos = window_box_height (w) / 2;
16344 }
16345
16346 if (!cursor_row_fully_visible_p (w, false, false))
16347 {
16348 /* Point does appear, but on a line partly visible at end of window.
16349 Move it back to a fully-visible line. */
16350 new_vpos = window_box_height (w);
16351 /* But if window_box_height suggests a Y coordinate that is
16352 not less than we already have, that line will clearly not
16353 be fully visible, so give up and scroll the display.
16354 This can happen when the default face uses a font whose
16355 dimensions are different from the frame's default
16356 font. */
16357 if (new_vpos >= w->cursor.y)
16358 {
16359 w->cursor.vpos = -1;
16360 clear_glyph_matrix (w->desired_matrix);
16361 goto try_to_scroll;
16362 }
16363 }
16364 else if (w->cursor.vpos >= 0)
16365 {
16366 /* Some people insist on not letting point enter the scroll
16367 margin, even though this part handles windows that didn't
16368 scroll at all. */
16369 int window_total_lines
16370 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16371 int margin = min (scroll_margin, window_total_lines / 4);
16372 int pixel_margin = margin * frame_line_height;
16373 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16374
16375 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16376 below, which finds the row to move point to, advances by
16377 the Y coordinate of the _next_ row, see the definition of
16378 MATRIX_ROW_BOTTOM_Y. */
16379 if (w->cursor.vpos < margin + header_line)
16380 {
16381 w->cursor.vpos = -1;
16382 clear_glyph_matrix (w->desired_matrix);
16383 goto try_to_scroll;
16384 }
16385 else
16386 {
16387 int window_height = window_box_height (w);
16388
16389 if (header_line)
16390 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16391 if (w->cursor.y >= window_height - pixel_margin)
16392 {
16393 w->cursor.vpos = -1;
16394 clear_glyph_matrix (w->desired_matrix);
16395 goto try_to_scroll;
16396 }
16397 }
16398 }
16399
16400 /* If we need to move point for either of the above reasons,
16401 now actually do it. */
16402 if (new_vpos >= 0)
16403 {
16404 struct glyph_row *row;
16405
16406 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16407 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16408 ++row;
16409
16410 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16411 MATRIX_ROW_START_BYTEPOS (row));
16412
16413 if (w != XWINDOW (selected_window))
16414 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16415 else if (current_buffer == old)
16416 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16417
16418 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16419
16420 /* Re-run pre-redisplay-function so it can update the region
16421 according to the new position of point. */
16422 /* Other than the cursor, w's redisplay is done so we can set its
16423 redisplay to false. Also the buffer's redisplay can be set to
16424 false, since propagate_buffer_redisplay should have already
16425 propagated its info to `w' anyway. */
16426 w->redisplay = false;
16427 XBUFFER (w->contents)->text->redisplay = false;
16428 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16429
16430 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16431 {
16432 /* pre-redisplay-function made changes (e.g. move the region)
16433 that require another round of redisplay. */
16434 clear_glyph_matrix (w->desired_matrix);
16435 if (!try_window (window, startp, 0))
16436 goto need_larger_matrices;
16437 }
16438 }
16439 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16440 {
16441 clear_glyph_matrix (w->desired_matrix);
16442 goto try_to_scroll;
16443 }
16444
16445 #ifdef GLYPH_DEBUG
16446 debug_method_add (w, "forced window start");
16447 #endif
16448 goto done;
16449 }
16450
16451 /* Handle case where text has not changed, only point, and it has
16452 not moved off the frame, and we are not retrying after hscroll.
16453 (current_matrix_up_to_date_p is true when retrying.) */
16454 if (current_matrix_up_to_date_p
16455 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16456 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16457 {
16458 switch (rc)
16459 {
16460 case CURSOR_MOVEMENT_SUCCESS:
16461 used_current_matrix_p = true;
16462 goto done;
16463
16464 case CURSOR_MOVEMENT_MUST_SCROLL:
16465 goto try_to_scroll;
16466
16467 default:
16468 emacs_abort ();
16469 }
16470 }
16471 /* If current starting point was originally the beginning of a line
16472 but no longer is, find a new starting point. */
16473 else if (w->start_at_line_beg
16474 && !(CHARPOS (startp) <= BEGV
16475 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16476 {
16477 #ifdef GLYPH_DEBUG
16478 debug_method_add (w, "recenter 1");
16479 #endif
16480 goto recenter;
16481 }
16482
16483 /* Try scrolling with try_window_id. Value is > 0 if update has
16484 been done, it is -1 if we know that the same window start will
16485 not work. It is 0 if unsuccessful for some other reason. */
16486 else if ((tem = try_window_id (w)) != 0)
16487 {
16488 #ifdef GLYPH_DEBUG
16489 debug_method_add (w, "try_window_id %d", tem);
16490 #endif
16491
16492 if (f->fonts_changed)
16493 goto need_larger_matrices;
16494 if (tem > 0)
16495 goto done;
16496
16497 /* Otherwise try_window_id has returned -1 which means that we
16498 don't want the alternative below this comment to execute. */
16499 }
16500 else if (CHARPOS (startp) >= BEGV
16501 && CHARPOS (startp) <= ZV
16502 && PT >= CHARPOS (startp)
16503 && (CHARPOS (startp) < ZV
16504 /* Avoid starting at end of buffer. */
16505 || CHARPOS (startp) == BEGV
16506 || !window_outdated (w)))
16507 {
16508 int d1, d2, d5, d6;
16509 int rtop, rbot;
16510
16511 /* If first window line is a continuation line, and window start
16512 is inside the modified region, but the first change is before
16513 current window start, we must select a new window start.
16514
16515 However, if this is the result of a down-mouse event (e.g. by
16516 extending the mouse-drag-overlay), we don't want to select a
16517 new window start, since that would change the position under
16518 the mouse, resulting in an unwanted mouse-movement rather
16519 than a simple mouse-click. */
16520 if (!w->start_at_line_beg
16521 && NILP (do_mouse_tracking)
16522 && CHARPOS (startp) > BEGV
16523 && CHARPOS (startp) > BEG + beg_unchanged
16524 && CHARPOS (startp) <= Z - end_unchanged
16525 /* Even if w->start_at_line_beg is nil, a new window may
16526 start at a line_beg, since that's how set_buffer_window
16527 sets it. So, we need to check the return value of
16528 compute_window_start_on_continuation_line. (See also
16529 bug#197). */
16530 && XMARKER (w->start)->buffer == current_buffer
16531 && compute_window_start_on_continuation_line (w)
16532 /* It doesn't make sense to force the window start like we
16533 do at label force_start if it is already known that point
16534 will not be fully visible in the resulting window, because
16535 doing so will move point from its correct position
16536 instead of scrolling the window to bring point into view.
16537 See bug#9324. */
16538 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16539 /* A very tall row could need more than the window height,
16540 in which case we accept that it is partially visible. */
16541 && (rtop != 0) == (rbot != 0))
16542 {
16543 w->force_start = true;
16544 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16545 #ifdef GLYPH_DEBUG
16546 debug_method_add (w, "recomputed window start in continuation line");
16547 #endif
16548 goto force_start;
16549 }
16550
16551 #ifdef GLYPH_DEBUG
16552 debug_method_add (w, "same window start");
16553 #endif
16554
16555 /* Try to redisplay starting at same place as before.
16556 If point has not moved off frame, accept the results. */
16557 if (!current_matrix_up_to_date_p
16558 /* Don't use try_window_reusing_current_matrix in this case
16559 because a window scroll function can have changed the
16560 buffer. */
16561 || !NILP (Vwindow_scroll_functions)
16562 || MINI_WINDOW_P (w)
16563 || !(used_current_matrix_p
16564 = try_window_reusing_current_matrix (w)))
16565 {
16566 IF_DEBUG (debug_method_add (w, "1"));
16567 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16568 /* -1 means we need to scroll.
16569 0 means we need new matrices, but fonts_changed
16570 is set in that case, so we will detect it below. */
16571 goto try_to_scroll;
16572 }
16573
16574 if (f->fonts_changed)
16575 goto need_larger_matrices;
16576
16577 if (w->cursor.vpos >= 0)
16578 {
16579 if (!just_this_one_p
16580 || current_buffer->clip_changed
16581 || BEG_UNCHANGED < CHARPOS (startp))
16582 /* Forget any recorded base line for line number display. */
16583 w->base_line_number = 0;
16584
16585 if (!cursor_row_fully_visible_p (w, true, false))
16586 {
16587 clear_glyph_matrix (w->desired_matrix);
16588 last_line_misfit = true;
16589 }
16590 /* Drop through and scroll. */
16591 else
16592 goto done;
16593 }
16594 else
16595 clear_glyph_matrix (w->desired_matrix);
16596 }
16597
16598 try_to_scroll:
16599
16600 /* Redisplay the mode line. Select the buffer properly for that. */
16601 if (!update_mode_line)
16602 {
16603 update_mode_line = true;
16604 w->update_mode_line = true;
16605 }
16606
16607 /* Try to scroll by specified few lines. */
16608 if ((scroll_conservatively
16609 || emacs_scroll_step
16610 || temp_scroll_step
16611 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16612 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16613 && CHARPOS (startp) >= BEGV
16614 && CHARPOS (startp) <= ZV)
16615 {
16616 /* The function returns -1 if new fonts were loaded, 1 if
16617 successful, 0 if not successful. */
16618 int ss = try_scrolling (window, just_this_one_p,
16619 scroll_conservatively,
16620 emacs_scroll_step,
16621 temp_scroll_step, last_line_misfit);
16622 switch (ss)
16623 {
16624 case SCROLLING_SUCCESS:
16625 goto done;
16626
16627 case SCROLLING_NEED_LARGER_MATRICES:
16628 goto need_larger_matrices;
16629
16630 case SCROLLING_FAILED:
16631 break;
16632
16633 default:
16634 emacs_abort ();
16635 }
16636 }
16637
16638 /* Finally, just choose a place to start which positions point
16639 according to user preferences. */
16640
16641 recenter:
16642
16643 #ifdef GLYPH_DEBUG
16644 debug_method_add (w, "recenter");
16645 #endif
16646
16647 /* Forget any previously recorded base line for line number display. */
16648 if (!buffer_unchanged_p)
16649 w->base_line_number = 0;
16650
16651 /* Determine the window start relative to point. */
16652 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16653 it.current_y = it.last_visible_y;
16654 if (centering_position < 0)
16655 {
16656 int window_total_lines
16657 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16658 int margin
16659 = scroll_margin > 0
16660 ? min (scroll_margin, window_total_lines / 4)
16661 : 0;
16662 ptrdiff_t margin_pos = CHARPOS (startp);
16663 Lisp_Object aggressive;
16664 bool scrolling_up;
16665
16666 /* If there is a scroll margin at the top of the window, find
16667 its character position. */
16668 if (margin
16669 /* Cannot call start_display if startp is not in the
16670 accessible region of the buffer. This can happen when we
16671 have just switched to a different buffer and/or changed
16672 its restriction. In that case, startp is initialized to
16673 the character position 1 (BEGV) because we did not yet
16674 have chance to display the buffer even once. */
16675 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16676 {
16677 struct it it1;
16678 void *it1data = NULL;
16679
16680 SAVE_IT (it1, it, it1data);
16681 start_display (&it1, w, startp);
16682 move_it_vertically (&it1, margin * frame_line_height);
16683 margin_pos = IT_CHARPOS (it1);
16684 RESTORE_IT (&it, &it, it1data);
16685 }
16686 scrolling_up = PT > margin_pos;
16687 aggressive =
16688 scrolling_up
16689 ? BVAR (current_buffer, scroll_up_aggressively)
16690 : BVAR (current_buffer, scroll_down_aggressively);
16691
16692 if (!MINI_WINDOW_P (w)
16693 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16694 {
16695 int pt_offset = 0;
16696
16697 /* Setting scroll-conservatively overrides
16698 scroll-*-aggressively. */
16699 if (!scroll_conservatively && NUMBERP (aggressive))
16700 {
16701 double float_amount = XFLOATINT (aggressive);
16702
16703 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16704 if (pt_offset == 0 && float_amount > 0)
16705 pt_offset = 1;
16706 if (pt_offset && margin > 0)
16707 margin -= 1;
16708 }
16709 /* Compute how much to move the window start backward from
16710 point so that point will be displayed where the user
16711 wants it. */
16712 if (scrolling_up)
16713 {
16714 centering_position = it.last_visible_y;
16715 if (pt_offset)
16716 centering_position -= pt_offset;
16717 centering_position -=
16718 (frame_line_height * (1 + margin + last_line_misfit)
16719 + WINDOW_HEADER_LINE_HEIGHT (w));
16720 /* Don't let point enter the scroll margin near top of
16721 the window. */
16722 if (centering_position < margin * frame_line_height)
16723 centering_position = margin * frame_line_height;
16724 }
16725 else
16726 centering_position = margin * frame_line_height + pt_offset;
16727 }
16728 else
16729 /* Set the window start half the height of the window backward
16730 from point. */
16731 centering_position = window_box_height (w) / 2;
16732 }
16733 move_it_vertically_backward (&it, centering_position);
16734
16735 eassert (IT_CHARPOS (it) >= BEGV);
16736
16737 /* The function move_it_vertically_backward may move over more
16738 than the specified y-distance. If it->w is small, e.g. a
16739 mini-buffer window, we may end up in front of the window's
16740 display area. Start displaying at the start of the line
16741 containing PT in this case. */
16742 if (it.current_y <= 0)
16743 {
16744 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16745 move_it_vertically_backward (&it, 0);
16746 it.current_y = 0;
16747 }
16748
16749 it.current_x = it.hpos = 0;
16750
16751 /* Set the window start position here explicitly, to avoid an
16752 infinite loop in case the functions in window-scroll-functions
16753 get errors. */
16754 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16755
16756 /* Run scroll hooks. */
16757 startp = run_window_scroll_functions (window, it.current.pos);
16758
16759 /* Redisplay the window. */
16760 bool use_desired_matrix = false;
16761 if (!current_matrix_up_to_date_p
16762 || windows_or_buffers_changed
16763 || f->cursor_type_changed
16764 /* Don't use try_window_reusing_current_matrix in this case
16765 because it can have changed the buffer. */
16766 || !NILP (Vwindow_scroll_functions)
16767 || !just_this_one_p
16768 || MINI_WINDOW_P (w)
16769 || !(used_current_matrix_p
16770 = try_window_reusing_current_matrix (w)))
16771 use_desired_matrix = (try_window (window, startp, 0) == 1);
16772
16773 /* If new fonts have been loaded (due to fontsets), give up. We
16774 have to start a new redisplay since we need to re-adjust glyph
16775 matrices. */
16776 if (f->fonts_changed)
16777 goto need_larger_matrices;
16778
16779 /* If cursor did not appear assume that the middle of the window is
16780 in the first line of the window. Do it again with the next line.
16781 (Imagine a window of height 100, displaying two lines of height
16782 60. Moving back 50 from it->last_visible_y will end in the first
16783 line.) */
16784 if (w->cursor.vpos < 0)
16785 {
16786 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16787 {
16788 clear_glyph_matrix (w->desired_matrix);
16789 move_it_by_lines (&it, 1);
16790 try_window (window, it.current.pos, 0);
16791 }
16792 else if (PT < IT_CHARPOS (it))
16793 {
16794 clear_glyph_matrix (w->desired_matrix);
16795 move_it_by_lines (&it, -1);
16796 try_window (window, it.current.pos, 0);
16797 }
16798 else
16799 {
16800 /* Not much we can do about it. */
16801 }
16802 }
16803
16804 /* Consider the following case: Window starts at BEGV, there is
16805 invisible, intangible text at BEGV, so that display starts at
16806 some point START > BEGV. It can happen that we are called with
16807 PT somewhere between BEGV and START. Try to handle that case,
16808 and similar ones. */
16809 if (w->cursor.vpos < 0)
16810 {
16811 /* Prefer the desired matrix to the current matrix, if possible,
16812 in the fallback calculations below. This is because using
16813 the current matrix might completely goof, e.g. if its first
16814 row is after point. */
16815 struct glyph_matrix *matrix =
16816 use_desired_matrix ? w->desired_matrix : w->current_matrix;
16817 /* First, try locating the proper glyph row for PT. */
16818 struct glyph_row *row =
16819 row_containing_pos (w, PT, matrix->rows, NULL, 0);
16820
16821 /* Sometimes point is at the beginning of invisible text that is
16822 before the 1st character displayed in the row. In that case,
16823 row_containing_pos fails to find the row, because no glyphs
16824 with appropriate buffer positions are present in the row.
16825 Therefore, we next try to find the row which shows the 1st
16826 position after the invisible text. */
16827 if (!row)
16828 {
16829 Lisp_Object val =
16830 get_char_property_and_overlay (make_number (PT), Qinvisible,
16831 Qnil, NULL);
16832
16833 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
16834 {
16835 ptrdiff_t alt_pos;
16836 Lisp_Object invis_end =
16837 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16838 Qnil, Qnil);
16839
16840 if (NATNUMP (invis_end))
16841 alt_pos = XFASTINT (invis_end);
16842 else
16843 alt_pos = ZV;
16844 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
16845 }
16846 }
16847 /* Finally, fall back on the first row of the window after the
16848 header line (if any). This is slightly better than not
16849 displaying the cursor at all. */
16850 if (!row)
16851 {
16852 row = matrix->rows;
16853 if (row->mode_line_p)
16854 ++row;
16855 }
16856 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
16857 }
16858
16859 if (!cursor_row_fully_visible_p (w, false, false))
16860 {
16861 /* If vscroll is enabled, disable it and try again. */
16862 if (w->vscroll)
16863 {
16864 w->vscroll = 0;
16865 clear_glyph_matrix (w->desired_matrix);
16866 goto recenter;
16867 }
16868
16869 /* Users who set scroll-conservatively to a large number want
16870 point just above/below the scroll margin. If we ended up
16871 with point's row partially visible, move the window start to
16872 make that row fully visible and out of the margin. */
16873 if (scroll_conservatively > SCROLL_LIMIT)
16874 {
16875 int window_total_lines
16876 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16877 int margin =
16878 scroll_margin > 0
16879 ? min (scroll_margin, window_total_lines / 4)
16880 : 0;
16881 bool move_down = w->cursor.vpos >= window_total_lines / 2;
16882
16883 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16884 clear_glyph_matrix (w->desired_matrix);
16885 if (1 == try_window (window, it.current.pos,
16886 TRY_WINDOW_CHECK_MARGINS))
16887 goto done;
16888 }
16889
16890 /* If centering point failed to make the whole line visible,
16891 put point at the top instead. That has to make the whole line
16892 visible, if it can be done. */
16893 if (centering_position == 0)
16894 goto done;
16895
16896 clear_glyph_matrix (w->desired_matrix);
16897 centering_position = 0;
16898 goto recenter;
16899 }
16900
16901 done:
16902
16903 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16904 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16905 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16906
16907 /* Display the mode line, if we must. */
16908 if ((update_mode_line
16909 /* If window not full width, must redo its mode line
16910 if (a) the window to its side is being redone and
16911 (b) we do a frame-based redisplay. This is a consequence
16912 of how inverted lines are drawn in frame-based redisplay. */
16913 || (!just_this_one_p
16914 && !FRAME_WINDOW_P (f)
16915 && !WINDOW_FULL_WIDTH_P (w))
16916 /* Line number to display. */
16917 || w->base_line_pos > 0
16918 /* Column number is displayed and different from the one displayed. */
16919 || (w->column_number_displayed != -1
16920 && (w->column_number_displayed != current_column ())))
16921 /* This means that the window has a mode line. */
16922 && (WINDOW_WANTS_MODELINE_P (w)
16923 || WINDOW_WANTS_HEADER_LINE_P (w)))
16924 {
16925
16926 display_mode_lines (w);
16927
16928 /* If mode line height has changed, arrange for a thorough
16929 immediate redisplay using the correct mode line height. */
16930 if (WINDOW_WANTS_MODELINE_P (w)
16931 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16932 {
16933 f->fonts_changed = true;
16934 w->mode_line_height = -1;
16935 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16936 = DESIRED_MODE_LINE_HEIGHT (w);
16937 }
16938
16939 /* If header line height has changed, arrange for a thorough
16940 immediate redisplay using the correct header line height. */
16941 if (WINDOW_WANTS_HEADER_LINE_P (w)
16942 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16943 {
16944 f->fonts_changed = true;
16945 w->header_line_height = -1;
16946 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16947 = DESIRED_HEADER_LINE_HEIGHT (w);
16948 }
16949
16950 if (f->fonts_changed)
16951 goto need_larger_matrices;
16952 }
16953
16954 if (!line_number_displayed && w->base_line_pos != -1)
16955 {
16956 w->base_line_pos = 0;
16957 w->base_line_number = 0;
16958 }
16959
16960 finish_menu_bars:
16961
16962 /* When we reach a frame's selected window, redo the frame's menu
16963 bar and the frame's title. */
16964 if (update_mode_line
16965 && EQ (FRAME_SELECTED_WINDOW (f), window))
16966 {
16967 bool redisplay_menu_p;
16968
16969 if (FRAME_WINDOW_P (f))
16970 {
16971 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16972 || defined (HAVE_NS) || defined (USE_GTK)
16973 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16974 #else
16975 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16976 #endif
16977 }
16978 else
16979 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16980
16981 if (redisplay_menu_p)
16982 display_menu_bar (w);
16983
16984 #ifdef HAVE_WINDOW_SYSTEM
16985 if (FRAME_WINDOW_P (f))
16986 {
16987 #if defined (USE_GTK) || defined (HAVE_NS)
16988 if (FRAME_EXTERNAL_TOOL_BAR (f))
16989 redisplay_tool_bar (f);
16990 #else
16991 if (WINDOWP (f->tool_bar_window)
16992 && (FRAME_TOOL_BAR_LINES (f) > 0
16993 || !NILP (Vauto_resize_tool_bars))
16994 && redisplay_tool_bar (f))
16995 ignore_mouse_drag_p = true;
16996 #endif
16997 }
16998 x_consider_frame_title (w->frame);
16999 #endif
17000 }
17001
17002 #ifdef HAVE_WINDOW_SYSTEM
17003 if (FRAME_WINDOW_P (f)
17004 && update_window_fringes (w, (just_this_one_p
17005 || (!used_current_matrix_p && !overlay_arrow_seen)
17006 || w->pseudo_window_p)))
17007 {
17008 update_begin (f);
17009 block_input ();
17010 if (draw_window_fringes (w, true))
17011 {
17012 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17013 x_draw_right_divider (w);
17014 else
17015 x_draw_vertical_border (w);
17016 }
17017 unblock_input ();
17018 update_end (f);
17019 }
17020
17021 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17022 x_draw_bottom_divider (w);
17023 #endif /* HAVE_WINDOW_SYSTEM */
17024
17025 /* We go to this label, with fonts_changed set, if it is
17026 necessary to try again using larger glyph matrices.
17027 We have to redeem the scroll bar even in this case,
17028 because the loop in redisplay_internal expects that. */
17029 need_larger_matrices:
17030 ;
17031 finish_scroll_bars:
17032
17033 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17034 {
17035 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17036 /* Set the thumb's position and size. */
17037 set_vertical_scroll_bar (w);
17038
17039 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17040 /* Set the thumb's position and size. */
17041 set_horizontal_scroll_bar (w);
17042
17043 /* Note that we actually used the scroll bar attached to this
17044 window, so it shouldn't be deleted at the end of redisplay. */
17045 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17046 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17047 }
17048
17049 /* Restore current_buffer and value of point in it. The window
17050 update may have changed the buffer, so first make sure `opoint'
17051 is still valid (Bug#6177). */
17052 if (CHARPOS (opoint) < BEGV)
17053 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17054 else if (CHARPOS (opoint) > ZV)
17055 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17056 else
17057 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17058
17059 set_buffer_internal_1 (old);
17060 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17061 shorter. This can be caused by log truncation in *Messages*. */
17062 if (CHARPOS (lpoint) <= ZV)
17063 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17064
17065 unbind_to (count, Qnil);
17066 }
17067
17068
17069 /* Build the complete desired matrix of WINDOW with a window start
17070 buffer position POS.
17071
17072 Value is 1 if successful. It is zero if fonts were loaded during
17073 redisplay which makes re-adjusting glyph matrices necessary, and -1
17074 if point would appear in the scroll margins.
17075 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17076 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17077 set in FLAGS.) */
17078
17079 int
17080 try_window (Lisp_Object window, struct text_pos pos, int flags)
17081 {
17082 struct window *w = XWINDOW (window);
17083 struct it it;
17084 struct glyph_row *last_text_row = NULL;
17085 struct frame *f = XFRAME (w->frame);
17086 int frame_line_height = default_line_pixel_height (w);
17087
17088 /* Make POS the new window start. */
17089 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17090
17091 /* Mark cursor position as unknown. No overlay arrow seen. */
17092 w->cursor.vpos = -1;
17093 overlay_arrow_seen = false;
17094
17095 /* Initialize iterator and info to start at POS. */
17096 start_display (&it, w, pos);
17097 it.glyph_row->reversed_p = false;
17098
17099 /* Display all lines of W. */
17100 while (it.current_y < it.last_visible_y)
17101 {
17102 if (display_line (&it))
17103 last_text_row = it.glyph_row - 1;
17104 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17105 return 0;
17106 }
17107
17108 /* Don't let the cursor end in the scroll margins. */
17109 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17110 && !MINI_WINDOW_P (w))
17111 {
17112 int this_scroll_margin;
17113 int window_total_lines
17114 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17115
17116 if (scroll_margin > 0)
17117 {
17118 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17119 this_scroll_margin *= frame_line_height;
17120 }
17121 else
17122 this_scroll_margin = 0;
17123
17124 if ((w->cursor.y >= 0 /* not vscrolled */
17125 && w->cursor.y < this_scroll_margin
17126 && CHARPOS (pos) > BEGV
17127 && IT_CHARPOS (it) < ZV)
17128 /* rms: considering make_cursor_line_fully_visible_p here
17129 seems to give wrong results. We don't want to recenter
17130 when the last line is partly visible, we want to allow
17131 that case to be handled in the usual way. */
17132 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17133 {
17134 w->cursor.vpos = -1;
17135 clear_glyph_matrix (w->desired_matrix);
17136 return -1;
17137 }
17138 }
17139
17140 /* If bottom moved off end of frame, change mode line percentage. */
17141 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17142 w->update_mode_line = true;
17143
17144 /* Set window_end_pos to the offset of the last character displayed
17145 on the window from the end of current_buffer. Set
17146 window_end_vpos to its row number. */
17147 if (last_text_row)
17148 {
17149 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17150 adjust_window_ends (w, last_text_row, false);
17151 eassert
17152 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17153 w->window_end_vpos)));
17154 }
17155 else
17156 {
17157 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17158 w->window_end_pos = Z - ZV;
17159 w->window_end_vpos = 0;
17160 }
17161
17162 /* But that is not valid info until redisplay finishes. */
17163 w->window_end_valid = false;
17164 return 1;
17165 }
17166
17167
17168 \f
17169 /************************************************************************
17170 Window redisplay reusing current matrix when buffer has not changed
17171 ************************************************************************/
17172
17173 /* Try redisplay of window W showing an unchanged buffer with a
17174 different window start than the last time it was displayed by
17175 reusing its current matrix. Value is true if successful.
17176 W->start is the new window start. */
17177
17178 static bool
17179 try_window_reusing_current_matrix (struct window *w)
17180 {
17181 struct frame *f = XFRAME (w->frame);
17182 struct glyph_row *bottom_row;
17183 struct it it;
17184 struct run run;
17185 struct text_pos start, new_start;
17186 int nrows_scrolled, i;
17187 struct glyph_row *last_text_row;
17188 struct glyph_row *last_reused_text_row;
17189 struct glyph_row *start_row;
17190 int start_vpos, min_y, max_y;
17191
17192 #ifdef GLYPH_DEBUG
17193 if (inhibit_try_window_reusing)
17194 return false;
17195 #endif
17196
17197 if (/* This function doesn't handle terminal frames. */
17198 !FRAME_WINDOW_P (f)
17199 /* Don't try to reuse the display if windows have been split
17200 or such. */
17201 || windows_or_buffers_changed
17202 || f->cursor_type_changed)
17203 return false;
17204
17205 /* Can't do this if showing trailing whitespace. */
17206 if (!NILP (Vshow_trailing_whitespace))
17207 return false;
17208
17209 /* If top-line visibility has changed, give up. */
17210 if (WINDOW_WANTS_HEADER_LINE_P (w)
17211 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17212 return false;
17213
17214 /* Give up if old or new display is scrolled vertically. We could
17215 make this function handle this, but right now it doesn't. */
17216 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17217 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17218 return false;
17219
17220 /* The variable new_start now holds the new window start. The old
17221 start `start' can be determined from the current matrix. */
17222 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17223 start = start_row->minpos;
17224 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17225
17226 /* Clear the desired matrix for the display below. */
17227 clear_glyph_matrix (w->desired_matrix);
17228
17229 if (CHARPOS (new_start) <= CHARPOS (start))
17230 {
17231 /* Don't use this method if the display starts with an ellipsis
17232 displayed for invisible text. It's not easy to handle that case
17233 below, and it's certainly not worth the effort since this is
17234 not a frequent case. */
17235 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17236 return false;
17237
17238 IF_DEBUG (debug_method_add (w, "twu1"));
17239
17240 /* Display up to a row that can be reused. The variable
17241 last_text_row is set to the last row displayed that displays
17242 text. Note that it.vpos == 0 if or if not there is a
17243 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17244 start_display (&it, w, new_start);
17245 w->cursor.vpos = -1;
17246 last_text_row = last_reused_text_row = NULL;
17247
17248 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17249 {
17250 /* If we have reached into the characters in the START row,
17251 that means the line boundaries have changed. So we
17252 can't start copying with the row START. Maybe it will
17253 work to start copying with the following row. */
17254 while (IT_CHARPOS (it) > CHARPOS (start))
17255 {
17256 /* Advance to the next row as the "start". */
17257 start_row++;
17258 start = start_row->minpos;
17259 /* If there are no more rows to try, or just one, give up. */
17260 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17261 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17262 || CHARPOS (start) == ZV)
17263 {
17264 clear_glyph_matrix (w->desired_matrix);
17265 return false;
17266 }
17267
17268 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17269 }
17270 /* If we have reached alignment, we can copy the rest of the
17271 rows. */
17272 if (IT_CHARPOS (it) == CHARPOS (start)
17273 /* Don't accept "alignment" inside a display vector,
17274 since start_row could have started in the middle of
17275 that same display vector (thus their character
17276 positions match), and we have no way of telling if
17277 that is the case. */
17278 && it.current.dpvec_index < 0)
17279 break;
17280
17281 it.glyph_row->reversed_p = false;
17282 if (display_line (&it))
17283 last_text_row = it.glyph_row - 1;
17284
17285 }
17286
17287 /* A value of current_y < last_visible_y means that we stopped
17288 at the previous window start, which in turn means that we
17289 have at least one reusable row. */
17290 if (it.current_y < it.last_visible_y)
17291 {
17292 struct glyph_row *row;
17293
17294 /* IT.vpos always starts from 0; it counts text lines. */
17295 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17296
17297 /* Find PT if not already found in the lines displayed. */
17298 if (w->cursor.vpos < 0)
17299 {
17300 int dy = it.current_y - start_row->y;
17301
17302 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17303 row = row_containing_pos (w, PT, row, NULL, dy);
17304 if (row)
17305 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17306 dy, nrows_scrolled);
17307 else
17308 {
17309 clear_glyph_matrix (w->desired_matrix);
17310 return false;
17311 }
17312 }
17313
17314 /* Scroll the display. Do it before the current matrix is
17315 changed. The problem here is that update has not yet
17316 run, i.e. part of the current matrix is not up to date.
17317 scroll_run_hook will clear the cursor, and use the
17318 current matrix to get the height of the row the cursor is
17319 in. */
17320 run.current_y = start_row->y;
17321 run.desired_y = it.current_y;
17322 run.height = it.last_visible_y - it.current_y;
17323
17324 if (run.height > 0 && run.current_y != run.desired_y)
17325 {
17326 update_begin (f);
17327 FRAME_RIF (f)->update_window_begin_hook (w);
17328 FRAME_RIF (f)->clear_window_mouse_face (w);
17329 FRAME_RIF (f)->scroll_run_hook (w, &run);
17330 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17331 update_end (f);
17332 }
17333
17334 /* Shift current matrix down by nrows_scrolled lines. */
17335 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17336 rotate_matrix (w->current_matrix,
17337 start_vpos,
17338 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17339 nrows_scrolled);
17340
17341 /* Disable lines that must be updated. */
17342 for (i = 0; i < nrows_scrolled; ++i)
17343 (start_row + i)->enabled_p = false;
17344
17345 /* Re-compute Y positions. */
17346 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17347 max_y = it.last_visible_y;
17348 for (row = start_row + nrows_scrolled;
17349 row < bottom_row;
17350 ++row)
17351 {
17352 row->y = it.current_y;
17353 row->visible_height = row->height;
17354
17355 if (row->y < min_y)
17356 row->visible_height -= min_y - row->y;
17357 if (row->y + row->height > max_y)
17358 row->visible_height -= row->y + row->height - max_y;
17359 if (row->fringe_bitmap_periodic_p)
17360 row->redraw_fringe_bitmaps_p = true;
17361
17362 it.current_y += row->height;
17363
17364 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17365 last_reused_text_row = row;
17366 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17367 break;
17368 }
17369
17370 /* Disable lines in the current matrix which are now
17371 below the window. */
17372 for (++row; row < bottom_row; ++row)
17373 row->enabled_p = row->mode_line_p = false;
17374 }
17375
17376 /* Update window_end_pos etc.; last_reused_text_row is the last
17377 reused row from the current matrix containing text, if any.
17378 The value of last_text_row is the last displayed line
17379 containing text. */
17380 if (last_reused_text_row)
17381 adjust_window_ends (w, last_reused_text_row, true);
17382 else if (last_text_row)
17383 adjust_window_ends (w, last_text_row, false);
17384 else
17385 {
17386 /* This window must be completely empty. */
17387 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17388 w->window_end_pos = Z - ZV;
17389 w->window_end_vpos = 0;
17390 }
17391 w->window_end_valid = false;
17392
17393 /* Update hint: don't try scrolling again in update_window. */
17394 w->desired_matrix->no_scrolling_p = true;
17395
17396 #ifdef GLYPH_DEBUG
17397 debug_method_add (w, "try_window_reusing_current_matrix 1");
17398 #endif
17399 return true;
17400 }
17401 else if (CHARPOS (new_start) > CHARPOS (start))
17402 {
17403 struct glyph_row *pt_row, *row;
17404 struct glyph_row *first_reusable_row;
17405 struct glyph_row *first_row_to_display;
17406 int dy;
17407 int yb = window_text_bottom_y (w);
17408
17409 /* Find the row starting at new_start, if there is one. Don't
17410 reuse a partially visible line at the end. */
17411 first_reusable_row = start_row;
17412 while (first_reusable_row->enabled_p
17413 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17414 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17415 < CHARPOS (new_start)))
17416 ++first_reusable_row;
17417
17418 /* Give up if there is no row to reuse. */
17419 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17420 || !first_reusable_row->enabled_p
17421 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17422 != CHARPOS (new_start)))
17423 return false;
17424
17425 /* We can reuse fully visible rows beginning with
17426 first_reusable_row to the end of the window. Set
17427 first_row_to_display to the first row that cannot be reused.
17428 Set pt_row to the row containing point, if there is any. */
17429 pt_row = NULL;
17430 for (first_row_to_display = first_reusable_row;
17431 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17432 ++first_row_to_display)
17433 {
17434 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17435 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17436 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17437 && first_row_to_display->ends_at_zv_p
17438 && pt_row == NULL)))
17439 pt_row = first_row_to_display;
17440 }
17441
17442 /* Start displaying at the start of first_row_to_display. */
17443 eassert (first_row_to_display->y < yb);
17444 init_to_row_start (&it, w, first_row_to_display);
17445
17446 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17447 - start_vpos);
17448 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17449 - nrows_scrolled);
17450 it.current_y = (first_row_to_display->y - first_reusable_row->y
17451 + WINDOW_HEADER_LINE_HEIGHT (w));
17452
17453 /* Display lines beginning with first_row_to_display in the
17454 desired matrix. Set last_text_row to the last row displayed
17455 that displays text. */
17456 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17457 if (pt_row == NULL)
17458 w->cursor.vpos = -1;
17459 last_text_row = NULL;
17460 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17461 if (display_line (&it))
17462 last_text_row = it.glyph_row - 1;
17463
17464 /* If point is in a reused row, adjust y and vpos of the cursor
17465 position. */
17466 if (pt_row)
17467 {
17468 w->cursor.vpos -= nrows_scrolled;
17469 w->cursor.y -= first_reusable_row->y - start_row->y;
17470 }
17471
17472 /* Give up if point isn't in a row displayed or reused. (This
17473 also handles the case where w->cursor.vpos < nrows_scrolled
17474 after the calls to display_line, which can happen with scroll
17475 margins. See bug#1295.) */
17476 if (w->cursor.vpos < 0)
17477 {
17478 clear_glyph_matrix (w->desired_matrix);
17479 return false;
17480 }
17481
17482 /* Scroll the display. */
17483 run.current_y = first_reusable_row->y;
17484 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17485 run.height = it.last_visible_y - run.current_y;
17486 dy = run.current_y - run.desired_y;
17487
17488 if (run.height)
17489 {
17490 update_begin (f);
17491 FRAME_RIF (f)->update_window_begin_hook (w);
17492 FRAME_RIF (f)->clear_window_mouse_face (w);
17493 FRAME_RIF (f)->scroll_run_hook (w, &run);
17494 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17495 update_end (f);
17496 }
17497
17498 /* Adjust Y positions of reused rows. */
17499 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17500 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17501 max_y = it.last_visible_y;
17502 for (row = first_reusable_row; row < first_row_to_display; ++row)
17503 {
17504 row->y -= dy;
17505 row->visible_height = row->height;
17506 if (row->y < min_y)
17507 row->visible_height -= min_y - row->y;
17508 if (row->y + row->height > max_y)
17509 row->visible_height -= row->y + row->height - max_y;
17510 if (row->fringe_bitmap_periodic_p)
17511 row->redraw_fringe_bitmaps_p = true;
17512 }
17513
17514 /* Scroll the current matrix. */
17515 eassert (nrows_scrolled > 0);
17516 rotate_matrix (w->current_matrix,
17517 start_vpos,
17518 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17519 -nrows_scrolled);
17520
17521 /* Disable rows not reused. */
17522 for (row -= nrows_scrolled; row < bottom_row; ++row)
17523 row->enabled_p = false;
17524
17525 /* Point may have moved to a different line, so we cannot assume that
17526 the previous cursor position is valid; locate the correct row. */
17527 if (pt_row)
17528 {
17529 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17530 row < bottom_row
17531 && PT >= MATRIX_ROW_END_CHARPOS (row)
17532 && !row->ends_at_zv_p;
17533 row++)
17534 {
17535 w->cursor.vpos++;
17536 w->cursor.y = row->y;
17537 }
17538 if (row < bottom_row)
17539 {
17540 /* Can't simply scan the row for point with
17541 bidi-reordered glyph rows. Let set_cursor_from_row
17542 figure out where to put the cursor, and if it fails,
17543 give up. */
17544 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17545 {
17546 if (!set_cursor_from_row (w, row, w->current_matrix,
17547 0, 0, 0, 0))
17548 {
17549 clear_glyph_matrix (w->desired_matrix);
17550 return false;
17551 }
17552 }
17553 else
17554 {
17555 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17556 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17557
17558 for (; glyph < end
17559 && (!BUFFERP (glyph->object)
17560 || glyph->charpos < PT);
17561 glyph++)
17562 {
17563 w->cursor.hpos++;
17564 w->cursor.x += glyph->pixel_width;
17565 }
17566 }
17567 }
17568 }
17569
17570 /* Adjust window end. A null value of last_text_row means that
17571 the window end is in reused rows which in turn means that
17572 only its vpos can have changed. */
17573 if (last_text_row)
17574 adjust_window_ends (w, last_text_row, false);
17575 else
17576 w->window_end_vpos -= nrows_scrolled;
17577
17578 w->window_end_valid = false;
17579 w->desired_matrix->no_scrolling_p = true;
17580
17581 #ifdef GLYPH_DEBUG
17582 debug_method_add (w, "try_window_reusing_current_matrix 2");
17583 #endif
17584 return true;
17585 }
17586
17587 return false;
17588 }
17589
17590
17591 \f
17592 /************************************************************************
17593 Window redisplay reusing current matrix when buffer has changed
17594 ************************************************************************/
17595
17596 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17597 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17598 ptrdiff_t *, ptrdiff_t *);
17599 static struct glyph_row *
17600 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17601 struct glyph_row *);
17602
17603
17604 /* Return the last row in MATRIX displaying text. If row START is
17605 non-null, start searching with that row. IT gives the dimensions
17606 of the display. Value is null if matrix is empty; otherwise it is
17607 a pointer to the row found. */
17608
17609 static struct glyph_row *
17610 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17611 struct glyph_row *start)
17612 {
17613 struct glyph_row *row, *row_found;
17614
17615 /* Set row_found to the last row in IT->w's current matrix
17616 displaying text. The loop looks funny but think of partially
17617 visible lines. */
17618 row_found = NULL;
17619 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17620 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17621 {
17622 eassert (row->enabled_p);
17623 row_found = row;
17624 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17625 break;
17626 ++row;
17627 }
17628
17629 return row_found;
17630 }
17631
17632
17633 /* Return the last row in the current matrix of W that is not affected
17634 by changes at the start of current_buffer that occurred since W's
17635 current matrix was built. Value is null if no such row exists.
17636
17637 BEG_UNCHANGED us the number of characters unchanged at the start of
17638 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17639 first changed character in current_buffer. Characters at positions <
17640 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17641 when the current matrix was built. */
17642
17643 static struct glyph_row *
17644 find_last_unchanged_at_beg_row (struct window *w)
17645 {
17646 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17647 struct glyph_row *row;
17648 struct glyph_row *row_found = NULL;
17649 int yb = window_text_bottom_y (w);
17650
17651 /* Find the last row displaying unchanged text. */
17652 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17653 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17654 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17655 ++row)
17656 {
17657 if (/* If row ends before first_changed_pos, it is unchanged,
17658 except in some case. */
17659 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17660 /* When row ends in ZV and we write at ZV it is not
17661 unchanged. */
17662 && !row->ends_at_zv_p
17663 /* When first_changed_pos is the end of a continued line,
17664 row is not unchanged because it may be no longer
17665 continued. */
17666 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17667 && (row->continued_p
17668 || row->exact_window_width_line_p))
17669 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17670 needs to be recomputed, so don't consider this row as
17671 unchanged. This happens when the last line was
17672 bidi-reordered and was killed immediately before this
17673 redisplay cycle. In that case, ROW->end stores the
17674 buffer position of the first visual-order character of
17675 the killed text, which is now beyond ZV. */
17676 && CHARPOS (row->end.pos) <= ZV)
17677 row_found = row;
17678
17679 /* Stop if last visible row. */
17680 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17681 break;
17682 }
17683
17684 return row_found;
17685 }
17686
17687
17688 /* Find the first glyph row in the current matrix of W that is not
17689 affected by changes at the end of current_buffer since the
17690 time W's current matrix was built.
17691
17692 Return in *DELTA the number of chars by which buffer positions in
17693 unchanged text at the end of current_buffer must be adjusted.
17694
17695 Return in *DELTA_BYTES the corresponding number of bytes.
17696
17697 Value is null if no such row exists, i.e. all rows are affected by
17698 changes. */
17699
17700 static struct glyph_row *
17701 find_first_unchanged_at_end_row (struct window *w,
17702 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17703 {
17704 struct glyph_row *row;
17705 struct glyph_row *row_found = NULL;
17706
17707 *delta = *delta_bytes = 0;
17708
17709 /* Display must not have been paused, otherwise the current matrix
17710 is not up to date. */
17711 eassert (w->window_end_valid);
17712
17713 /* A value of window_end_pos >= END_UNCHANGED means that the window
17714 end is in the range of changed text. If so, there is no
17715 unchanged row at the end of W's current matrix. */
17716 if (w->window_end_pos >= END_UNCHANGED)
17717 return NULL;
17718
17719 /* Set row to the last row in W's current matrix displaying text. */
17720 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17721
17722 /* If matrix is entirely empty, no unchanged row exists. */
17723 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17724 {
17725 /* The value of row is the last glyph row in the matrix having a
17726 meaningful buffer position in it. The end position of row
17727 corresponds to window_end_pos. This allows us to translate
17728 buffer positions in the current matrix to current buffer
17729 positions for characters not in changed text. */
17730 ptrdiff_t Z_old =
17731 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17732 ptrdiff_t Z_BYTE_old =
17733 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17734 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17735 struct glyph_row *first_text_row
17736 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17737
17738 *delta = Z - Z_old;
17739 *delta_bytes = Z_BYTE - Z_BYTE_old;
17740
17741 /* Set last_unchanged_pos to the buffer position of the last
17742 character in the buffer that has not been changed. Z is the
17743 index + 1 of the last character in current_buffer, i.e. by
17744 subtracting END_UNCHANGED we get the index of the last
17745 unchanged character, and we have to add BEG to get its buffer
17746 position. */
17747 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17748 last_unchanged_pos_old = last_unchanged_pos - *delta;
17749
17750 /* Search backward from ROW for a row displaying a line that
17751 starts at a minimum position >= last_unchanged_pos_old. */
17752 for (; row > first_text_row; --row)
17753 {
17754 /* This used to abort, but it can happen.
17755 It is ok to just stop the search instead here. KFS. */
17756 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17757 break;
17758
17759 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17760 row_found = row;
17761 }
17762 }
17763
17764 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17765
17766 return row_found;
17767 }
17768
17769
17770 /* Make sure that glyph rows in the current matrix of window W
17771 reference the same glyph memory as corresponding rows in the
17772 frame's frame matrix. This function is called after scrolling W's
17773 current matrix on a terminal frame in try_window_id and
17774 try_window_reusing_current_matrix. */
17775
17776 static void
17777 sync_frame_with_window_matrix_rows (struct window *w)
17778 {
17779 struct frame *f = XFRAME (w->frame);
17780 struct glyph_row *window_row, *window_row_end, *frame_row;
17781
17782 /* Preconditions: W must be a leaf window and full-width. Its frame
17783 must have a frame matrix. */
17784 eassert (BUFFERP (w->contents));
17785 eassert (WINDOW_FULL_WIDTH_P (w));
17786 eassert (!FRAME_WINDOW_P (f));
17787
17788 /* If W is a full-width window, glyph pointers in W's current matrix
17789 have, by definition, to be the same as glyph pointers in the
17790 corresponding frame matrix. Note that frame matrices have no
17791 marginal areas (see build_frame_matrix). */
17792 window_row = w->current_matrix->rows;
17793 window_row_end = window_row + w->current_matrix->nrows;
17794 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17795 while (window_row < window_row_end)
17796 {
17797 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17798 struct glyph *end = window_row->glyphs[LAST_AREA];
17799
17800 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17801 frame_row->glyphs[TEXT_AREA] = start;
17802 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17803 frame_row->glyphs[LAST_AREA] = end;
17804
17805 /* Disable frame rows whose corresponding window rows have
17806 been disabled in try_window_id. */
17807 if (!window_row->enabled_p)
17808 frame_row->enabled_p = false;
17809
17810 ++window_row, ++frame_row;
17811 }
17812 }
17813
17814
17815 /* Find the glyph row in window W containing CHARPOS. Consider all
17816 rows between START and END (not inclusive). END null means search
17817 all rows to the end of the display area of W. Value is the row
17818 containing CHARPOS or null. */
17819
17820 struct glyph_row *
17821 row_containing_pos (struct window *w, ptrdiff_t charpos,
17822 struct glyph_row *start, struct glyph_row *end, int dy)
17823 {
17824 struct glyph_row *row = start;
17825 struct glyph_row *best_row = NULL;
17826 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17827 int last_y;
17828
17829 /* If we happen to start on a header-line, skip that. */
17830 if (row->mode_line_p)
17831 ++row;
17832
17833 if ((end && row >= end) || !row->enabled_p)
17834 return NULL;
17835
17836 last_y = window_text_bottom_y (w) - dy;
17837
17838 while (true)
17839 {
17840 /* Give up if we have gone too far. */
17841 if ((end && row >= end) || !row->enabled_p)
17842 return NULL;
17843 /* This formerly returned if they were equal.
17844 I think that both quantities are of a "last plus one" type;
17845 if so, when they are equal, the row is within the screen. -- rms. */
17846 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17847 return NULL;
17848
17849 /* If it is in this row, return this row. */
17850 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17851 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17852 /* The end position of a row equals the start
17853 position of the next row. If CHARPOS is there, we
17854 would rather consider it displayed in the next
17855 line, except when this line ends in ZV. */
17856 && !row_for_charpos_p (row, charpos)))
17857 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17858 {
17859 struct glyph *g;
17860
17861 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17862 || (!best_row && !row->continued_p))
17863 return row;
17864 /* In bidi-reordered rows, there could be several rows whose
17865 edges surround CHARPOS, all of these rows belonging to
17866 the same continued line. We need to find the row which
17867 fits CHARPOS the best. */
17868 for (g = row->glyphs[TEXT_AREA];
17869 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17870 g++)
17871 {
17872 if (!STRINGP (g->object))
17873 {
17874 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17875 {
17876 mindif = eabs (g->charpos - charpos);
17877 best_row = row;
17878 /* Exact match always wins. */
17879 if (mindif == 0)
17880 return best_row;
17881 }
17882 }
17883 }
17884 }
17885 else if (best_row && !row->continued_p)
17886 return best_row;
17887 ++row;
17888 }
17889 }
17890
17891
17892 /* Try to redisplay window W by reusing its existing display. W's
17893 current matrix must be up to date when this function is called,
17894 i.e., window_end_valid must be true.
17895
17896 Value is
17897
17898 >= 1 if successful, i.e. display has been updated
17899 specifically:
17900 1 means the changes were in front of a newline that precedes
17901 the window start, and the whole current matrix was reused
17902 2 means the changes were after the last position displayed
17903 in the window, and the whole current matrix was reused
17904 3 means portions of the current matrix were reused, while
17905 some of the screen lines were redrawn
17906 -1 if redisplay with same window start is known not to succeed
17907 0 if otherwise unsuccessful
17908
17909 The following steps are performed:
17910
17911 1. Find the last row in the current matrix of W that is not
17912 affected by changes at the start of current_buffer. If no such row
17913 is found, give up.
17914
17915 2. Find the first row in W's current matrix that is not affected by
17916 changes at the end of current_buffer. Maybe there is no such row.
17917
17918 3. Display lines beginning with the row + 1 found in step 1 to the
17919 row found in step 2 or, if step 2 didn't find a row, to the end of
17920 the window.
17921
17922 4. If cursor is not known to appear on the window, give up.
17923
17924 5. If display stopped at the row found in step 2, scroll the
17925 display and current matrix as needed.
17926
17927 6. Maybe display some lines at the end of W, if we must. This can
17928 happen under various circumstances, like a partially visible line
17929 becoming fully visible, or because newly displayed lines are displayed
17930 in smaller font sizes.
17931
17932 7. Update W's window end information. */
17933
17934 static int
17935 try_window_id (struct window *w)
17936 {
17937 struct frame *f = XFRAME (w->frame);
17938 struct glyph_matrix *current_matrix = w->current_matrix;
17939 struct glyph_matrix *desired_matrix = w->desired_matrix;
17940 struct glyph_row *last_unchanged_at_beg_row;
17941 struct glyph_row *first_unchanged_at_end_row;
17942 struct glyph_row *row;
17943 struct glyph_row *bottom_row;
17944 int bottom_vpos;
17945 struct it it;
17946 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17947 int dvpos, dy;
17948 struct text_pos start_pos;
17949 struct run run;
17950 int first_unchanged_at_end_vpos = 0;
17951 struct glyph_row *last_text_row, *last_text_row_at_end;
17952 struct text_pos start;
17953 ptrdiff_t first_changed_charpos, last_changed_charpos;
17954
17955 #ifdef GLYPH_DEBUG
17956 if (inhibit_try_window_id)
17957 return 0;
17958 #endif
17959
17960 /* This is handy for debugging. */
17961 #if false
17962 #define GIVE_UP(X) \
17963 do { \
17964 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
17965 return 0; \
17966 } while (false)
17967 #else
17968 #define GIVE_UP(X) return 0
17969 #endif
17970
17971 SET_TEXT_POS_FROM_MARKER (start, w->start);
17972
17973 /* Don't use this for mini-windows because these can show
17974 messages and mini-buffers, and we don't handle that here. */
17975 if (MINI_WINDOW_P (w))
17976 GIVE_UP (1);
17977
17978 /* This flag is used to prevent redisplay optimizations. */
17979 if (windows_or_buffers_changed || f->cursor_type_changed)
17980 GIVE_UP (2);
17981
17982 /* This function's optimizations cannot be used if overlays have
17983 changed in the buffer displayed by the window, so give up if they
17984 have. */
17985 if (w->last_overlay_modified != OVERLAY_MODIFF)
17986 GIVE_UP (200);
17987
17988 /* Verify that narrowing has not changed.
17989 Also verify that we were not told to prevent redisplay optimizations.
17990 It would be nice to further
17991 reduce the number of cases where this prevents try_window_id. */
17992 if (current_buffer->clip_changed
17993 || current_buffer->prevent_redisplay_optimizations_p)
17994 GIVE_UP (3);
17995
17996 /* Window must either use window-based redisplay or be full width. */
17997 if (!FRAME_WINDOW_P (f)
17998 && (!FRAME_LINE_INS_DEL_OK (f)
17999 || !WINDOW_FULL_WIDTH_P (w)))
18000 GIVE_UP (4);
18001
18002 /* Give up if point is known NOT to appear in W. */
18003 if (PT < CHARPOS (start))
18004 GIVE_UP (5);
18005
18006 /* Another way to prevent redisplay optimizations. */
18007 if (w->last_modified == 0)
18008 GIVE_UP (6);
18009
18010 /* Verify that window is not hscrolled. */
18011 if (w->hscroll != 0)
18012 GIVE_UP (7);
18013
18014 /* Verify that display wasn't paused. */
18015 if (!w->window_end_valid)
18016 GIVE_UP (8);
18017
18018 /* Likewise if highlighting trailing whitespace. */
18019 if (!NILP (Vshow_trailing_whitespace))
18020 GIVE_UP (11);
18021
18022 /* Can't use this if overlay arrow position and/or string have
18023 changed. */
18024 if (overlay_arrows_changed_p ())
18025 GIVE_UP (12);
18026
18027 /* When word-wrap is on, adding a space to the first word of a
18028 wrapped line can change the wrap position, altering the line
18029 above it. It might be worthwhile to handle this more
18030 intelligently, but for now just redisplay from scratch. */
18031 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18032 GIVE_UP (21);
18033
18034 /* Under bidi reordering, adding or deleting a character in the
18035 beginning of a paragraph, before the first strong directional
18036 character, can change the base direction of the paragraph (unless
18037 the buffer specifies a fixed paragraph direction), which will
18038 require to redisplay the whole paragraph. It might be worthwhile
18039 to find the paragraph limits and widen the range of redisplayed
18040 lines to that, but for now just give up this optimization and
18041 redisplay from scratch. */
18042 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18043 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18044 GIVE_UP (22);
18045
18046 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18047 to that variable require thorough redisplay. */
18048 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18049 GIVE_UP (23);
18050
18051 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18052 only if buffer has really changed. The reason is that the gap is
18053 initially at Z for freshly visited files. The code below would
18054 set end_unchanged to 0 in that case. */
18055 if (MODIFF > SAVE_MODIFF
18056 /* This seems to happen sometimes after saving a buffer. */
18057 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18058 {
18059 if (GPT - BEG < BEG_UNCHANGED)
18060 BEG_UNCHANGED = GPT - BEG;
18061 if (Z - GPT < END_UNCHANGED)
18062 END_UNCHANGED = Z - GPT;
18063 }
18064
18065 /* The position of the first and last character that has been changed. */
18066 first_changed_charpos = BEG + BEG_UNCHANGED;
18067 last_changed_charpos = Z - END_UNCHANGED;
18068
18069 /* If window starts after a line end, and the last change is in
18070 front of that newline, then changes don't affect the display.
18071 This case happens with stealth-fontification. Note that although
18072 the display is unchanged, glyph positions in the matrix have to
18073 be adjusted, of course. */
18074 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18075 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18076 && ((last_changed_charpos < CHARPOS (start)
18077 && CHARPOS (start) == BEGV)
18078 || (last_changed_charpos < CHARPOS (start) - 1
18079 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18080 {
18081 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18082 struct glyph_row *r0;
18083
18084 /* Compute how many chars/bytes have been added to or removed
18085 from the buffer. */
18086 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18087 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18088 Z_delta = Z - Z_old;
18089 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18090
18091 /* Give up if PT is not in the window. Note that it already has
18092 been checked at the start of try_window_id that PT is not in
18093 front of the window start. */
18094 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18095 GIVE_UP (13);
18096
18097 /* If window start is unchanged, we can reuse the whole matrix
18098 as is, after adjusting glyph positions. No need to compute
18099 the window end again, since its offset from Z hasn't changed. */
18100 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18101 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18102 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18103 /* PT must not be in a partially visible line. */
18104 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18105 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18106 {
18107 /* Adjust positions in the glyph matrix. */
18108 if (Z_delta || Z_delta_bytes)
18109 {
18110 struct glyph_row *r1
18111 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18112 increment_matrix_positions (w->current_matrix,
18113 MATRIX_ROW_VPOS (r0, current_matrix),
18114 MATRIX_ROW_VPOS (r1, current_matrix),
18115 Z_delta, Z_delta_bytes);
18116 }
18117
18118 /* Set the cursor. */
18119 row = row_containing_pos (w, PT, r0, NULL, 0);
18120 if (row)
18121 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18122 return 1;
18123 }
18124 }
18125
18126 /* Handle the case that changes are all below what is displayed in
18127 the window, and that PT is in the window. This shortcut cannot
18128 be taken if ZV is visible in the window, and text has been added
18129 there that is visible in the window. */
18130 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18131 /* ZV is not visible in the window, or there are no
18132 changes at ZV, actually. */
18133 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18134 || first_changed_charpos == last_changed_charpos))
18135 {
18136 struct glyph_row *r0;
18137
18138 /* Give up if PT is not in the window. Note that it already has
18139 been checked at the start of try_window_id that PT is not in
18140 front of the window start. */
18141 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18142 GIVE_UP (14);
18143
18144 /* If window start is unchanged, we can reuse the whole matrix
18145 as is, without changing glyph positions since no text has
18146 been added/removed in front of the window end. */
18147 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18148 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18149 /* PT must not be in a partially visible line. */
18150 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18151 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18152 {
18153 /* We have to compute the window end anew since text
18154 could have been added/removed after it. */
18155 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18156 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18157
18158 /* Set the cursor. */
18159 row = row_containing_pos (w, PT, r0, NULL, 0);
18160 if (row)
18161 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18162 return 2;
18163 }
18164 }
18165
18166 /* Give up if window start is in the changed area.
18167
18168 The condition used to read
18169
18170 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18171
18172 but why that was tested escapes me at the moment. */
18173 if (CHARPOS (start) >= first_changed_charpos
18174 && CHARPOS (start) <= last_changed_charpos)
18175 GIVE_UP (15);
18176
18177 /* Check that window start agrees with the start of the first glyph
18178 row in its current matrix. Check this after we know the window
18179 start is not in changed text, otherwise positions would not be
18180 comparable. */
18181 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18182 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18183 GIVE_UP (16);
18184
18185 /* Give up if the window ends in strings. Overlay strings
18186 at the end are difficult to handle, so don't try. */
18187 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18188 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18189 GIVE_UP (20);
18190
18191 /* Compute the position at which we have to start displaying new
18192 lines. Some of the lines at the top of the window might be
18193 reusable because they are not displaying changed text. Find the
18194 last row in W's current matrix not affected by changes at the
18195 start of current_buffer. Value is null if changes start in the
18196 first line of window. */
18197 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18198 if (last_unchanged_at_beg_row)
18199 {
18200 /* Avoid starting to display in the middle of a character, a TAB
18201 for instance. This is easier than to set up the iterator
18202 exactly, and it's not a frequent case, so the additional
18203 effort wouldn't really pay off. */
18204 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18205 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18206 && last_unchanged_at_beg_row > w->current_matrix->rows)
18207 --last_unchanged_at_beg_row;
18208
18209 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18210 GIVE_UP (17);
18211
18212 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18213 GIVE_UP (18);
18214 start_pos = it.current.pos;
18215
18216 /* Start displaying new lines in the desired matrix at the same
18217 vpos we would use in the current matrix, i.e. below
18218 last_unchanged_at_beg_row. */
18219 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18220 current_matrix);
18221 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18222 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18223
18224 eassert (it.hpos == 0 && it.current_x == 0);
18225 }
18226 else
18227 {
18228 /* There are no reusable lines at the start of the window.
18229 Start displaying in the first text line. */
18230 start_display (&it, w, start);
18231 it.vpos = it.first_vpos;
18232 start_pos = it.current.pos;
18233 }
18234
18235 /* Find the first row that is not affected by changes at the end of
18236 the buffer. Value will be null if there is no unchanged row, in
18237 which case we must redisplay to the end of the window. delta
18238 will be set to the value by which buffer positions beginning with
18239 first_unchanged_at_end_row have to be adjusted due to text
18240 changes. */
18241 first_unchanged_at_end_row
18242 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18243 IF_DEBUG (debug_delta = delta);
18244 IF_DEBUG (debug_delta_bytes = delta_bytes);
18245
18246 /* Set stop_pos to the buffer position up to which we will have to
18247 display new lines. If first_unchanged_at_end_row != NULL, this
18248 is the buffer position of the start of the line displayed in that
18249 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18250 that we don't stop at a buffer position. */
18251 stop_pos = 0;
18252 if (first_unchanged_at_end_row)
18253 {
18254 eassert (last_unchanged_at_beg_row == NULL
18255 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18256
18257 /* If this is a continuation line, move forward to the next one
18258 that isn't. Changes in lines above affect this line.
18259 Caution: this may move first_unchanged_at_end_row to a row
18260 not displaying text. */
18261 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18262 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18263 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18264 < it.last_visible_y))
18265 ++first_unchanged_at_end_row;
18266
18267 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18268 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18269 >= it.last_visible_y))
18270 first_unchanged_at_end_row = NULL;
18271 else
18272 {
18273 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18274 + delta);
18275 first_unchanged_at_end_vpos
18276 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18277 eassert (stop_pos >= Z - END_UNCHANGED);
18278 }
18279 }
18280 else if (last_unchanged_at_beg_row == NULL)
18281 GIVE_UP (19);
18282
18283
18284 #ifdef GLYPH_DEBUG
18285
18286 /* Either there is no unchanged row at the end, or the one we have
18287 now displays text. This is a necessary condition for the window
18288 end pos calculation at the end of this function. */
18289 eassert (first_unchanged_at_end_row == NULL
18290 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18291
18292 debug_last_unchanged_at_beg_vpos
18293 = (last_unchanged_at_beg_row
18294 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18295 : -1);
18296 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18297
18298 #endif /* GLYPH_DEBUG */
18299
18300
18301 /* Display new lines. Set last_text_row to the last new line
18302 displayed which has text on it, i.e. might end up as being the
18303 line where the window_end_vpos is. */
18304 w->cursor.vpos = -1;
18305 last_text_row = NULL;
18306 overlay_arrow_seen = false;
18307 if (it.current_y < it.last_visible_y
18308 && !f->fonts_changed
18309 && (first_unchanged_at_end_row == NULL
18310 || IT_CHARPOS (it) < stop_pos))
18311 it.glyph_row->reversed_p = false;
18312 while (it.current_y < it.last_visible_y
18313 && !f->fonts_changed
18314 && (first_unchanged_at_end_row == NULL
18315 || IT_CHARPOS (it) < stop_pos))
18316 {
18317 if (display_line (&it))
18318 last_text_row = it.glyph_row - 1;
18319 }
18320
18321 if (f->fonts_changed)
18322 return -1;
18323
18324 /* The redisplay iterations in display_line above could have
18325 triggered font-lock, which could have done something that
18326 invalidates IT->w window's end-point information, on which we
18327 rely below. E.g., one package, which will remain unnamed, used
18328 to install a font-lock-fontify-region-function that called
18329 bury-buffer, whose side effect is to switch the buffer displayed
18330 by IT->w, and that predictably resets IT->w's window_end_valid
18331 flag, which we already tested at the entry to this function.
18332 Amply punish such packages/modes by giving up on this
18333 optimization in those cases. */
18334 if (!w->window_end_valid)
18335 {
18336 clear_glyph_matrix (w->desired_matrix);
18337 return -1;
18338 }
18339
18340 /* Compute differences in buffer positions, y-positions etc. for
18341 lines reused at the bottom of the window. Compute what we can
18342 scroll. */
18343 if (first_unchanged_at_end_row
18344 /* No lines reused because we displayed everything up to the
18345 bottom of the window. */
18346 && it.current_y < it.last_visible_y)
18347 {
18348 dvpos = (it.vpos
18349 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18350 current_matrix));
18351 dy = it.current_y - first_unchanged_at_end_row->y;
18352 run.current_y = first_unchanged_at_end_row->y;
18353 run.desired_y = run.current_y + dy;
18354 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18355 }
18356 else
18357 {
18358 delta = delta_bytes = dvpos = dy
18359 = run.current_y = run.desired_y = run.height = 0;
18360 first_unchanged_at_end_row = NULL;
18361 }
18362 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18363
18364
18365 /* Find the cursor if not already found. We have to decide whether
18366 PT will appear on this window (it sometimes doesn't, but this is
18367 not a very frequent case.) This decision has to be made before
18368 the current matrix is altered. A value of cursor.vpos < 0 means
18369 that PT is either in one of the lines beginning at
18370 first_unchanged_at_end_row or below the window. Don't care for
18371 lines that might be displayed later at the window end; as
18372 mentioned, this is not a frequent case. */
18373 if (w->cursor.vpos < 0)
18374 {
18375 /* Cursor in unchanged rows at the top? */
18376 if (PT < CHARPOS (start_pos)
18377 && last_unchanged_at_beg_row)
18378 {
18379 row = row_containing_pos (w, PT,
18380 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18381 last_unchanged_at_beg_row + 1, 0);
18382 if (row)
18383 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18384 }
18385
18386 /* Start from first_unchanged_at_end_row looking for PT. */
18387 else if (first_unchanged_at_end_row)
18388 {
18389 row = row_containing_pos (w, PT - delta,
18390 first_unchanged_at_end_row, NULL, 0);
18391 if (row)
18392 set_cursor_from_row (w, row, w->current_matrix, delta,
18393 delta_bytes, dy, dvpos);
18394 }
18395
18396 /* Give up if cursor was not found. */
18397 if (w->cursor.vpos < 0)
18398 {
18399 clear_glyph_matrix (w->desired_matrix);
18400 return -1;
18401 }
18402 }
18403
18404 /* Don't let the cursor end in the scroll margins. */
18405 {
18406 int this_scroll_margin, cursor_height;
18407 int frame_line_height = default_line_pixel_height (w);
18408 int window_total_lines
18409 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18410
18411 this_scroll_margin =
18412 max (0, min (scroll_margin, window_total_lines / 4));
18413 this_scroll_margin *= frame_line_height;
18414 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18415
18416 if ((w->cursor.y < this_scroll_margin
18417 && CHARPOS (start) > BEGV)
18418 /* Old redisplay didn't take scroll margin into account at the bottom,
18419 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18420 || (w->cursor.y + (make_cursor_line_fully_visible_p
18421 ? cursor_height + this_scroll_margin
18422 : 1)) > it.last_visible_y)
18423 {
18424 w->cursor.vpos = -1;
18425 clear_glyph_matrix (w->desired_matrix);
18426 return -1;
18427 }
18428 }
18429
18430 /* Scroll the display. Do it before changing the current matrix so
18431 that xterm.c doesn't get confused about where the cursor glyph is
18432 found. */
18433 if (dy && run.height)
18434 {
18435 update_begin (f);
18436
18437 if (FRAME_WINDOW_P (f))
18438 {
18439 FRAME_RIF (f)->update_window_begin_hook (w);
18440 FRAME_RIF (f)->clear_window_mouse_face (w);
18441 FRAME_RIF (f)->scroll_run_hook (w, &run);
18442 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18443 }
18444 else
18445 {
18446 /* Terminal frame. In this case, dvpos gives the number of
18447 lines to scroll by; dvpos < 0 means scroll up. */
18448 int from_vpos
18449 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18450 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18451 int end = (WINDOW_TOP_EDGE_LINE (w)
18452 + WINDOW_WANTS_HEADER_LINE_P (w)
18453 + window_internal_height (w));
18454
18455 #if defined (HAVE_GPM) || defined (MSDOS)
18456 x_clear_window_mouse_face (w);
18457 #endif
18458 /* Perform the operation on the screen. */
18459 if (dvpos > 0)
18460 {
18461 /* Scroll last_unchanged_at_beg_row to the end of the
18462 window down dvpos lines. */
18463 set_terminal_window (f, end);
18464
18465 /* On dumb terminals delete dvpos lines at the end
18466 before inserting dvpos empty lines. */
18467 if (!FRAME_SCROLL_REGION_OK (f))
18468 ins_del_lines (f, end - dvpos, -dvpos);
18469
18470 /* Insert dvpos empty lines in front of
18471 last_unchanged_at_beg_row. */
18472 ins_del_lines (f, from, dvpos);
18473 }
18474 else if (dvpos < 0)
18475 {
18476 /* Scroll up last_unchanged_at_beg_vpos to the end of
18477 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18478 set_terminal_window (f, end);
18479
18480 /* Delete dvpos lines in front of
18481 last_unchanged_at_beg_vpos. ins_del_lines will set
18482 the cursor to the given vpos and emit |dvpos| delete
18483 line sequences. */
18484 ins_del_lines (f, from + dvpos, dvpos);
18485
18486 /* On a dumb terminal insert dvpos empty lines at the
18487 end. */
18488 if (!FRAME_SCROLL_REGION_OK (f))
18489 ins_del_lines (f, end + dvpos, -dvpos);
18490 }
18491
18492 set_terminal_window (f, 0);
18493 }
18494
18495 update_end (f);
18496 }
18497
18498 /* Shift reused rows of the current matrix to the right position.
18499 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18500 text. */
18501 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18502 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18503 if (dvpos < 0)
18504 {
18505 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18506 bottom_vpos, dvpos);
18507 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18508 bottom_vpos);
18509 }
18510 else if (dvpos > 0)
18511 {
18512 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18513 bottom_vpos, dvpos);
18514 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18515 first_unchanged_at_end_vpos + dvpos);
18516 }
18517
18518 /* For frame-based redisplay, make sure that current frame and window
18519 matrix are in sync with respect to glyph memory. */
18520 if (!FRAME_WINDOW_P (f))
18521 sync_frame_with_window_matrix_rows (w);
18522
18523 /* Adjust buffer positions in reused rows. */
18524 if (delta || delta_bytes)
18525 increment_matrix_positions (current_matrix,
18526 first_unchanged_at_end_vpos + dvpos,
18527 bottom_vpos, delta, delta_bytes);
18528
18529 /* Adjust Y positions. */
18530 if (dy)
18531 shift_glyph_matrix (w, current_matrix,
18532 first_unchanged_at_end_vpos + dvpos,
18533 bottom_vpos, dy);
18534
18535 if (first_unchanged_at_end_row)
18536 {
18537 first_unchanged_at_end_row += dvpos;
18538 if (first_unchanged_at_end_row->y >= it.last_visible_y
18539 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18540 first_unchanged_at_end_row = NULL;
18541 }
18542
18543 /* If scrolling up, there may be some lines to display at the end of
18544 the window. */
18545 last_text_row_at_end = NULL;
18546 if (dy < 0)
18547 {
18548 /* Scrolling up can leave for example a partially visible line
18549 at the end of the window to be redisplayed. */
18550 /* Set last_row to the glyph row in the current matrix where the
18551 window end line is found. It has been moved up or down in
18552 the matrix by dvpos. */
18553 int last_vpos = w->window_end_vpos + dvpos;
18554 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18555
18556 /* If last_row is the window end line, it should display text. */
18557 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18558
18559 /* If window end line was partially visible before, begin
18560 displaying at that line. Otherwise begin displaying with the
18561 line following it. */
18562 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18563 {
18564 init_to_row_start (&it, w, last_row);
18565 it.vpos = last_vpos;
18566 it.current_y = last_row->y;
18567 }
18568 else
18569 {
18570 init_to_row_end (&it, w, last_row);
18571 it.vpos = 1 + last_vpos;
18572 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18573 ++last_row;
18574 }
18575
18576 /* We may start in a continuation line. If so, we have to
18577 get the right continuation_lines_width and current_x. */
18578 it.continuation_lines_width = last_row->continuation_lines_width;
18579 it.hpos = it.current_x = 0;
18580
18581 /* Display the rest of the lines at the window end. */
18582 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18583 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18584 {
18585 /* Is it always sure that the display agrees with lines in
18586 the current matrix? I don't think so, so we mark rows
18587 displayed invalid in the current matrix by setting their
18588 enabled_p flag to false. */
18589 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18590 if (display_line (&it))
18591 last_text_row_at_end = it.glyph_row - 1;
18592 }
18593 }
18594
18595 /* Update window_end_pos and window_end_vpos. */
18596 if (first_unchanged_at_end_row && !last_text_row_at_end)
18597 {
18598 /* Window end line if one of the preserved rows from the current
18599 matrix. Set row to the last row displaying text in current
18600 matrix starting at first_unchanged_at_end_row, after
18601 scrolling. */
18602 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18603 row = find_last_row_displaying_text (w->current_matrix, &it,
18604 first_unchanged_at_end_row);
18605 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18606 adjust_window_ends (w, row, true);
18607 eassert (w->window_end_bytepos >= 0);
18608 IF_DEBUG (debug_method_add (w, "A"));
18609 }
18610 else if (last_text_row_at_end)
18611 {
18612 adjust_window_ends (w, last_text_row_at_end, false);
18613 eassert (w->window_end_bytepos >= 0);
18614 IF_DEBUG (debug_method_add (w, "B"));
18615 }
18616 else if (last_text_row)
18617 {
18618 /* We have displayed either to the end of the window or at the
18619 end of the window, i.e. the last row with text is to be found
18620 in the desired matrix. */
18621 adjust_window_ends (w, last_text_row, false);
18622 eassert (w->window_end_bytepos >= 0);
18623 }
18624 else if (first_unchanged_at_end_row == NULL
18625 && last_text_row == NULL
18626 && last_text_row_at_end == NULL)
18627 {
18628 /* Displayed to end of window, but no line containing text was
18629 displayed. Lines were deleted at the end of the window. */
18630 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18631 int vpos = w->window_end_vpos;
18632 struct glyph_row *current_row = current_matrix->rows + vpos;
18633 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18634
18635 for (row = NULL;
18636 row == NULL && vpos >= first_vpos;
18637 --vpos, --current_row, --desired_row)
18638 {
18639 if (desired_row->enabled_p)
18640 {
18641 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18642 row = desired_row;
18643 }
18644 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18645 row = current_row;
18646 }
18647
18648 eassert (row != NULL);
18649 w->window_end_vpos = vpos + 1;
18650 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18651 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18652 eassert (w->window_end_bytepos >= 0);
18653 IF_DEBUG (debug_method_add (w, "C"));
18654 }
18655 else
18656 emacs_abort ();
18657
18658 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18659 debug_end_vpos = w->window_end_vpos));
18660
18661 /* Record that display has not been completed. */
18662 w->window_end_valid = false;
18663 w->desired_matrix->no_scrolling_p = true;
18664 return 3;
18665
18666 #undef GIVE_UP
18667 }
18668
18669
18670 \f
18671 /***********************************************************************
18672 More debugging support
18673 ***********************************************************************/
18674
18675 #ifdef GLYPH_DEBUG
18676
18677 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18678 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18679 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18680
18681
18682 /* Dump the contents of glyph matrix MATRIX on stderr.
18683
18684 GLYPHS 0 means don't show glyph contents.
18685 GLYPHS 1 means show glyphs in short form
18686 GLYPHS > 1 means show glyphs in long form. */
18687
18688 void
18689 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18690 {
18691 int i;
18692 for (i = 0; i < matrix->nrows; ++i)
18693 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18694 }
18695
18696
18697 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18698 the glyph row and area where the glyph comes from. */
18699
18700 void
18701 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18702 {
18703 if (glyph->type == CHAR_GLYPH
18704 || glyph->type == GLYPHLESS_GLYPH)
18705 {
18706 fprintf (stderr,
18707 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18708 glyph - row->glyphs[TEXT_AREA],
18709 (glyph->type == CHAR_GLYPH
18710 ? 'C'
18711 : 'G'),
18712 glyph->charpos,
18713 (BUFFERP (glyph->object)
18714 ? 'B'
18715 : (STRINGP (glyph->object)
18716 ? 'S'
18717 : (NILP (glyph->object)
18718 ? '0'
18719 : '-'))),
18720 glyph->pixel_width,
18721 glyph->u.ch,
18722 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18723 ? glyph->u.ch
18724 : '.'),
18725 glyph->face_id,
18726 glyph->left_box_line_p,
18727 glyph->right_box_line_p);
18728 }
18729 else if (glyph->type == STRETCH_GLYPH)
18730 {
18731 fprintf (stderr,
18732 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18733 glyph - row->glyphs[TEXT_AREA],
18734 'S',
18735 glyph->charpos,
18736 (BUFFERP (glyph->object)
18737 ? 'B'
18738 : (STRINGP (glyph->object)
18739 ? 'S'
18740 : (NILP (glyph->object)
18741 ? '0'
18742 : '-'))),
18743 glyph->pixel_width,
18744 0,
18745 ' ',
18746 glyph->face_id,
18747 glyph->left_box_line_p,
18748 glyph->right_box_line_p);
18749 }
18750 else if (glyph->type == IMAGE_GLYPH)
18751 {
18752 fprintf (stderr,
18753 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18754 glyph - row->glyphs[TEXT_AREA],
18755 'I',
18756 glyph->charpos,
18757 (BUFFERP (glyph->object)
18758 ? 'B'
18759 : (STRINGP (glyph->object)
18760 ? 'S'
18761 : (NILP (glyph->object)
18762 ? '0'
18763 : '-'))),
18764 glyph->pixel_width,
18765 glyph->u.img_id,
18766 '.',
18767 glyph->face_id,
18768 glyph->left_box_line_p,
18769 glyph->right_box_line_p);
18770 }
18771 else if (glyph->type == COMPOSITE_GLYPH)
18772 {
18773 fprintf (stderr,
18774 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18775 glyph - row->glyphs[TEXT_AREA],
18776 '+',
18777 glyph->charpos,
18778 (BUFFERP (glyph->object)
18779 ? 'B'
18780 : (STRINGP (glyph->object)
18781 ? 'S'
18782 : (NILP (glyph->object)
18783 ? '0'
18784 : '-'))),
18785 glyph->pixel_width,
18786 glyph->u.cmp.id);
18787 if (glyph->u.cmp.automatic)
18788 fprintf (stderr,
18789 "[%d-%d]",
18790 glyph->slice.cmp.from, glyph->slice.cmp.to);
18791 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18792 glyph->face_id,
18793 glyph->left_box_line_p,
18794 glyph->right_box_line_p);
18795 }
18796 }
18797
18798
18799 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18800 GLYPHS 0 means don't show glyph contents.
18801 GLYPHS 1 means show glyphs in short form
18802 GLYPHS > 1 means show glyphs in long form. */
18803
18804 void
18805 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18806 {
18807 if (glyphs != 1)
18808 {
18809 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18810 fprintf (stderr, "==============================================================================\n");
18811
18812 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18813 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18814 vpos,
18815 MATRIX_ROW_START_CHARPOS (row),
18816 MATRIX_ROW_END_CHARPOS (row),
18817 row->used[TEXT_AREA],
18818 row->contains_overlapping_glyphs_p,
18819 row->enabled_p,
18820 row->truncated_on_left_p,
18821 row->truncated_on_right_p,
18822 row->continued_p,
18823 MATRIX_ROW_CONTINUATION_LINE_P (row),
18824 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18825 row->ends_at_zv_p,
18826 row->fill_line_p,
18827 row->ends_in_middle_of_char_p,
18828 row->starts_in_middle_of_char_p,
18829 row->mouse_face_p,
18830 row->x,
18831 row->y,
18832 row->pixel_width,
18833 row->height,
18834 row->visible_height,
18835 row->ascent,
18836 row->phys_ascent);
18837 /* The next 3 lines should align to "Start" in the header. */
18838 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18839 row->end.overlay_string_index,
18840 row->continuation_lines_width);
18841 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18842 CHARPOS (row->start.string_pos),
18843 CHARPOS (row->end.string_pos));
18844 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18845 row->end.dpvec_index);
18846 }
18847
18848 if (glyphs > 1)
18849 {
18850 int area;
18851
18852 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18853 {
18854 struct glyph *glyph = row->glyphs[area];
18855 struct glyph *glyph_end = glyph + row->used[area];
18856
18857 /* Glyph for a line end in text. */
18858 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18859 ++glyph_end;
18860
18861 if (glyph < glyph_end)
18862 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18863
18864 for (; glyph < glyph_end; ++glyph)
18865 dump_glyph (row, glyph, area);
18866 }
18867 }
18868 else if (glyphs == 1)
18869 {
18870 int area;
18871 char s[SHRT_MAX + 4];
18872
18873 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18874 {
18875 int i;
18876
18877 for (i = 0; i < row->used[area]; ++i)
18878 {
18879 struct glyph *glyph = row->glyphs[area] + i;
18880 if (i == row->used[area] - 1
18881 && area == TEXT_AREA
18882 && NILP (glyph->object)
18883 && glyph->type == CHAR_GLYPH
18884 && glyph->u.ch == ' ')
18885 {
18886 strcpy (&s[i], "[\\n]");
18887 i += 4;
18888 }
18889 else if (glyph->type == CHAR_GLYPH
18890 && glyph->u.ch < 0x80
18891 && glyph->u.ch >= ' ')
18892 s[i] = glyph->u.ch;
18893 else
18894 s[i] = '.';
18895 }
18896
18897 s[i] = '\0';
18898 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18899 }
18900 }
18901 }
18902
18903
18904 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18905 Sdump_glyph_matrix, 0, 1, "p",
18906 doc: /* Dump the current matrix of the selected window to stderr.
18907 Shows contents of glyph row structures. With non-nil
18908 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18909 glyphs in short form, otherwise show glyphs in long form.
18910
18911 Interactively, no argument means show glyphs in short form;
18912 with numeric argument, its value is passed as the GLYPHS flag. */)
18913 (Lisp_Object glyphs)
18914 {
18915 struct window *w = XWINDOW (selected_window);
18916 struct buffer *buffer = XBUFFER (w->contents);
18917
18918 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18919 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18920 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18921 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18922 fprintf (stderr, "=============================================\n");
18923 dump_glyph_matrix (w->current_matrix,
18924 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18925 return Qnil;
18926 }
18927
18928
18929 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18930 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18931 Only text-mode frames have frame glyph matrices. */)
18932 (void)
18933 {
18934 struct frame *f = XFRAME (selected_frame);
18935
18936 if (f->current_matrix)
18937 dump_glyph_matrix (f->current_matrix, 1);
18938 else
18939 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18940 return Qnil;
18941 }
18942
18943
18944 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18945 doc: /* Dump glyph row ROW to stderr.
18946 GLYPH 0 means don't dump glyphs.
18947 GLYPH 1 means dump glyphs in short form.
18948 GLYPH > 1 or omitted means dump glyphs in long form. */)
18949 (Lisp_Object row, Lisp_Object glyphs)
18950 {
18951 struct glyph_matrix *matrix;
18952 EMACS_INT vpos;
18953
18954 CHECK_NUMBER (row);
18955 matrix = XWINDOW (selected_window)->current_matrix;
18956 vpos = XINT (row);
18957 if (vpos >= 0 && vpos < matrix->nrows)
18958 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18959 vpos,
18960 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18961 return Qnil;
18962 }
18963
18964
18965 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18966 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18967 GLYPH 0 means don't dump glyphs.
18968 GLYPH 1 means dump glyphs in short form.
18969 GLYPH > 1 or omitted means dump glyphs in long form.
18970
18971 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18972 do nothing. */)
18973 (Lisp_Object row, Lisp_Object glyphs)
18974 {
18975 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18976 struct frame *sf = SELECTED_FRAME ();
18977 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18978 EMACS_INT vpos;
18979
18980 CHECK_NUMBER (row);
18981 vpos = XINT (row);
18982 if (vpos >= 0 && vpos < m->nrows)
18983 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18984 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18985 #endif
18986 return Qnil;
18987 }
18988
18989
18990 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18991 doc: /* Toggle tracing of redisplay.
18992 With ARG, turn tracing on if and only if ARG is positive. */)
18993 (Lisp_Object arg)
18994 {
18995 if (NILP (arg))
18996 trace_redisplay_p = !trace_redisplay_p;
18997 else
18998 {
18999 arg = Fprefix_numeric_value (arg);
19000 trace_redisplay_p = XINT (arg) > 0;
19001 }
19002
19003 return Qnil;
19004 }
19005
19006
19007 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19008 doc: /* Like `format', but print result to stderr.
19009 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19010 (ptrdiff_t nargs, Lisp_Object *args)
19011 {
19012 Lisp_Object s = Fformat (nargs, args);
19013 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19014 return Qnil;
19015 }
19016
19017 #endif /* GLYPH_DEBUG */
19018
19019
19020 \f
19021 /***********************************************************************
19022 Building Desired Matrix Rows
19023 ***********************************************************************/
19024
19025 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19026 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19027
19028 static struct glyph_row *
19029 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19030 {
19031 struct frame *f = XFRAME (WINDOW_FRAME (w));
19032 struct buffer *buffer = XBUFFER (w->contents);
19033 struct buffer *old = current_buffer;
19034 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19035 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19036 const unsigned char *arrow_end = arrow_string + arrow_len;
19037 const unsigned char *p;
19038 struct it it;
19039 bool multibyte_p;
19040 int n_glyphs_before;
19041
19042 set_buffer_temp (buffer);
19043 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19044 scratch_glyph_row.reversed_p = false;
19045 it.glyph_row->used[TEXT_AREA] = 0;
19046 SET_TEXT_POS (it.position, 0, 0);
19047
19048 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19049 p = arrow_string;
19050 while (p < arrow_end)
19051 {
19052 Lisp_Object face, ilisp;
19053
19054 /* Get the next character. */
19055 if (multibyte_p)
19056 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19057 else
19058 {
19059 it.c = it.char_to_display = *p, it.len = 1;
19060 if (! ASCII_CHAR_P (it.c))
19061 it.char_to_display = BYTE8_TO_CHAR (it.c);
19062 }
19063 p += it.len;
19064
19065 /* Get its face. */
19066 ilisp = make_number (p - arrow_string);
19067 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19068 it.face_id = compute_char_face (f, it.char_to_display, face);
19069
19070 /* Compute its width, get its glyphs. */
19071 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19072 SET_TEXT_POS (it.position, -1, -1);
19073 PRODUCE_GLYPHS (&it);
19074
19075 /* If this character doesn't fit any more in the line, we have
19076 to remove some glyphs. */
19077 if (it.current_x > it.last_visible_x)
19078 {
19079 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19080 break;
19081 }
19082 }
19083
19084 set_buffer_temp (old);
19085 return it.glyph_row;
19086 }
19087
19088
19089 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19090 glyphs to insert is determined by produce_special_glyphs. */
19091
19092 static void
19093 insert_left_trunc_glyphs (struct it *it)
19094 {
19095 struct it truncate_it;
19096 struct glyph *from, *end, *to, *toend;
19097
19098 eassert (!FRAME_WINDOW_P (it->f)
19099 || (!it->glyph_row->reversed_p
19100 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19101 || (it->glyph_row->reversed_p
19102 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19103
19104 /* Get the truncation glyphs. */
19105 truncate_it = *it;
19106 truncate_it.current_x = 0;
19107 truncate_it.face_id = DEFAULT_FACE_ID;
19108 truncate_it.glyph_row = &scratch_glyph_row;
19109 truncate_it.area = TEXT_AREA;
19110 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19111 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19112 truncate_it.object = Qnil;
19113 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19114
19115 /* Overwrite glyphs from IT with truncation glyphs. */
19116 if (!it->glyph_row->reversed_p)
19117 {
19118 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19119
19120 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19121 end = from + tused;
19122 to = it->glyph_row->glyphs[TEXT_AREA];
19123 toend = to + it->glyph_row->used[TEXT_AREA];
19124 if (FRAME_WINDOW_P (it->f))
19125 {
19126 /* On GUI frames, when variable-size fonts are displayed,
19127 the truncation glyphs may need more pixels than the row's
19128 glyphs they overwrite. We overwrite more glyphs to free
19129 enough screen real estate, and enlarge the stretch glyph
19130 on the right (see display_line), if there is one, to
19131 preserve the screen position of the truncation glyphs on
19132 the right. */
19133 int w = 0;
19134 struct glyph *g = to;
19135 short used;
19136
19137 /* The first glyph could be partially visible, in which case
19138 it->glyph_row->x will be negative. But we want the left
19139 truncation glyphs to be aligned at the left margin of the
19140 window, so we override the x coordinate at which the row
19141 will begin. */
19142 it->glyph_row->x = 0;
19143 while (g < toend && w < it->truncation_pixel_width)
19144 {
19145 w += g->pixel_width;
19146 ++g;
19147 }
19148 if (g - to - tused > 0)
19149 {
19150 memmove (to + tused, g, (toend - g) * sizeof(*g));
19151 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19152 }
19153 used = it->glyph_row->used[TEXT_AREA];
19154 if (it->glyph_row->truncated_on_right_p
19155 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19156 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19157 == STRETCH_GLYPH)
19158 {
19159 int extra = w - it->truncation_pixel_width;
19160
19161 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19162 }
19163 }
19164
19165 while (from < end)
19166 *to++ = *from++;
19167
19168 /* There may be padding glyphs left over. Overwrite them too. */
19169 if (!FRAME_WINDOW_P (it->f))
19170 {
19171 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19172 {
19173 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19174 while (from < end)
19175 *to++ = *from++;
19176 }
19177 }
19178
19179 if (to > toend)
19180 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19181 }
19182 else
19183 {
19184 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19185
19186 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19187 that back to front. */
19188 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19189 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19190 toend = it->glyph_row->glyphs[TEXT_AREA];
19191 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19192 if (FRAME_WINDOW_P (it->f))
19193 {
19194 int w = 0;
19195 struct glyph *g = to;
19196
19197 while (g >= toend && w < it->truncation_pixel_width)
19198 {
19199 w += g->pixel_width;
19200 --g;
19201 }
19202 if (to - g - tused > 0)
19203 to = g + tused;
19204 if (it->glyph_row->truncated_on_right_p
19205 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19206 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19207 {
19208 int extra = w - it->truncation_pixel_width;
19209
19210 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19211 }
19212 }
19213
19214 while (from >= end && to >= toend)
19215 *to-- = *from--;
19216 if (!FRAME_WINDOW_P (it->f))
19217 {
19218 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19219 {
19220 from =
19221 truncate_it.glyph_row->glyphs[TEXT_AREA]
19222 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19223 while (from >= end && to >= toend)
19224 *to-- = *from--;
19225 }
19226 }
19227 if (from >= end)
19228 {
19229 /* Need to free some room before prepending additional
19230 glyphs. */
19231 int move_by = from - end + 1;
19232 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19233 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19234
19235 for ( ; g >= g0; g--)
19236 g[move_by] = *g;
19237 while (from >= end)
19238 *to-- = *from--;
19239 it->glyph_row->used[TEXT_AREA] += move_by;
19240 }
19241 }
19242 }
19243
19244 /* Compute the hash code for ROW. */
19245 unsigned
19246 row_hash (struct glyph_row *row)
19247 {
19248 int area, k;
19249 unsigned hashval = 0;
19250
19251 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19252 for (k = 0; k < row->used[area]; ++k)
19253 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19254 + row->glyphs[area][k].u.val
19255 + row->glyphs[area][k].face_id
19256 + row->glyphs[area][k].padding_p
19257 + (row->glyphs[area][k].type << 2));
19258
19259 return hashval;
19260 }
19261
19262 /* Compute the pixel height and width of IT->glyph_row.
19263
19264 Most of the time, ascent and height of a display line will be equal
19265 to the max_ascent and max_height values of the display iterator
19266 structure. This is not the case if
19267
19268 1. We hit ZV without displaying anything. In this case, max_ascent
19269 and max_height will be zero.
19270
19271 2. We have some glyphs that don't contribute to the line height.
19272 (The glyph row flag contributes_to_line_height_p is for future
19273 pixmap extensions).
19274
19275 The first case is easily covered by using default values because in
19276 these cases, the line height does not really matter, except that it
19277 must not be zero. */
19278
19279 static void
19280 compute_line_metrics (struct it *it)
19281 {
19282 struct glyph_row *row = it->glyph_row;
19283
19284 if (FRAME_WINDOW_P (it->f))
19285 {
19286 int i, min_y, max_y;
19287
19288 /* The line may consist of one space only, that was added to
19289 place the cursor on it. If so, the row's height hasn't been
19290 computed yet. */
19291 if (row->height == 0)
19292 {
19293 if (it->max_ascent + it->max_descent == 0)
19294 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19295 row->ascent = it->max_ascent;
19296 row->height = it->max_ascent + it->max_descent;
19297 row->phys_ascent = it->max_phys_ascent;
19298 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19299 row->extra_line_spacing = it->max_extra_line_spacing;
19300 }
19301
19302 /* Compute the width of this line. */
19303 row->pixel_width = row->x;
19304 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19305 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19306
19307 eassert (row->pixel_width >= 0);
19308 eassert (row->ascent >= 0 && row->height > 0);
19309
19310 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19311 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19312
19313 /* If first line's physical ascent is larger than its logical
19314 ascent, use the physical ascent, and make the row taller.
19315 This makes accented characters fully visible. */
19316 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19317 && row->phys_ascent > row->ascent)
19318 {
19319 row->height += row->phys_ascent - row->ascent;
19320 row->ascent = row->phys_ascent;
19321 }
19322
19323 /* Compute how much of the line is visible. */
19324 row->visible_height = row->height;
19325
19326 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19327 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19328
19329 if (row->y < min_y)
19330 row->visible_height -= min_y - row->y;
19331 if (row->y + row->height > max_y)
19332 row->visible_height -= row->y + row->height - max_y;
19333 }
19334 else
19335 {
19336 row->pixel_width = row->used[TEXT_AREA];
19337 if (row->continued_p)
19338 row->pixel_width -= it->continuation_pixel_width;
19339 else if (row->truncated_on_right_p)
19340 row->pixel_width -= it->truncation_pixel_width;
19341 row->ascent = row->phys_ascent = 0;
19342 row->height = row->phys_height = row->visible_height = 1;
19343 row->extra_line_spacing = 0;
19344 }
19345
19346 /* Compute a hash code for this row. */
19347 row->hash = row_hash (row);
19348
19349 it->max_ascent = it->max_descent = 0;
19350 it->max_phys_ascent = it->max_phys_descent = 0;
19351 }
19352
19353
19354 /* Append one space to the glyph row of iterator IT if doing a
19355 window-based redisplay. The space has the same face as
19356 IT->face_id. Value is true if a space was added.
19357
19358 This function is called to make sure that there is always one glyph
19359 at the end of a glyph row that the cursor can be set on under
19360 window-systems. (If there weren't such a glyph we would not know
19361 how wide and tall a box cursor should be displayed).
19362
19363 At the same time this space let's a nicely handle clearing to the
19364 end of the line if the row ends in italic text. */
19365
19366 static bool
19367 append_space_for_newline (struct it *it, bool default_face_p)
19368 {
19369 if (FRAME_WINDOW_P (it->f))
19370 {
19371 int n = it->glyph_row->used[TEXT_AREA];
19372
19373 if (it->glyph_row->glyphs[TEXT_AREA] + n
19374 < it->glyph_row->glyphs[1 + TEXT_AREA])
19375 {
19376 /* Save some values that must not be changed.
19377 Must save IT->c and IT->len because otherwise
19378 ITERATOR_AT_END_P wouldn't work anymore after
19379 append_space_for_newline has been called. */
19380 enum display_element_type saved_what = it->what;
19381 int saved_c = it->c, saved_len = it->len;
19382 int saved_char_to_display = it->char_to_display;
19383 int saved_x = it->current_x;
19384 int saved_face_id = it->face_id;
19385 bool saved_box_end = it->end_of_box_run_p;
19386 struct text_pos saved_pos;
19387 Lisp_Object saved_object;
19388 struct face *face;
19389 struct glyph *g;
19390
19391 saved_object = it->object;
19392 saved_pos = it->position;
19393
19394 it->what = IT_CHARACTER;
19395 memset (&it->position, 0, sizeof it->position);
19396 it->object = Qnil;
19397 it->c = it->char_to_display = ' ';
19398 it->len = 1;
19399
19400 /* If the default face was remapped, be sure to use the
19401 remapped face for the appended newline. */
19402 if (default_face_p)
19403 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19404 else if (it->face_before_selective_p)
19405 it->face_id = it->saved_face_id;
19406 face = FACE_FROM_ID (it->f, it->face_id);
19407 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19408 /* In R2L rows, we will prepend a stretch glyph that will
19409 have the end_of_box_run_p flag set for it, so there's no
19410 need for the appended newline glyph to have that flag
19411 set. */
19412 if (it->glyph_row->reversed_p
19413 /* But if the appended newline glyph goes all the way to
19414 the end of the row, there will be no stretch glyph,
19415 so leave the box flag set. */
19416 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19417 it->end_of_box_run_p = false;
19418
19419 PRODUCE_GLYPHS (it);
19420
19421 #ifdef HAVE_WINDOW_SYSTEM
19422 /* Make sure this space glyph has the right ascent and
19423 descent values, or else cursor at end of line will look
19424 funny, and height of empty lines will be incorrect. */
19425 g = it->glyph_row->glyphs[TEXT_AREA] + n;
19426 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19427 if (n == 0)
19428 {
19429 Lisp_Object height, total_height;
19430 int extra_line_spacing = it->extra_line_spacing;
19431 int boff = font->baseline_offset;
19432
19433 if (font->vertical_centering)
19434 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19435
19436 it->object = saved_object; /* get_it_property needs this */
19437 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19438 /* Must do a subset of line height processing from
19439 x_produce_glyph for newline characters. */
19440 height = get_it_property (it, Qline_height);
19441 if (CONSP (height)
19442 && CONSP (XCDR (height))
19443 && NILP (XCDR (XCDR (height))))
19444 {
19445 total_height = XCAR (XCDR (height));
19446 height = XCAR (height);
19447 }
19448 else
19449 total_height = Qnil;
19450 height = calc_line_height_property (it, height, font, boff, true);
19451
19452 if (it->override_ascent >= 0)
19453 {
19454 it->ascent = it->override_ascent;
19455 it->descent = it->override_descent;
19456 boff = it->override_boff;
19457 }
19458 if (EQ (height, Qt))
19459 extra_line_spacing = 0;
19460 else
19461 {
19462 Lisp_Object spacing;
19463
19464 it->phys_ascent = it->ascent;
19465 it->phys_descent = it->descent;
19466 if (!NILP (height)
19467 && XINT (height) > it->ascent + it->descent)
19468 it->ascent = XINT (height) - it->descent;
19469
19470 if (!NILP (total_height))
19471 spacing = calc_line_height_property (it, total_height, font,
19472 boff, false);
19473 else
19474 {
19475 spacing = get_it_property (it, Qline_spacing);
19476 spacing = calc_line_height_property (it, spacing, font,
19477 boff, false);
19478 }
19479 if (INTEGERP (spacing))
19480 {
19481 extra_line_spacing = XINT (spacing);
19482 if (!NILP (total_height))
19483 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19484 }
19485 }
19486 if (extra_line_spacing > 0)
19487 {
19488 it->descent += extra_line_spacing;
19489 if (extra_line_spacing > it->max_extra_line_spacing)
19490 it->max_extra_line_spacing = extra_line_spacing;
19491 }
19492 it->max_ascent = it->ascent;
19493 it->max_descent = it->descent;
19494 /* Make sure compute_line_metrics recomputes the row height. */
19495 it->glyph_row->height = 0;
19496 }
19497
19498 g->ascent = it->max_ascent;
19499 g->descent = it->max_descent;
19500 #endif
19501
19502 it->override_ascent = -1;
19503 it->constrain_row_ascent_descent_p = false;
19504 it->current_x = saved_x;
19505 it->object = saved_object;
19506 it->position = saved_pos;
19507 it->what = saved_what;
19508 it->face_id = saved_face_id;
19509 it->len = saved_len;
19510 it->c = saved_c;
19511 it->char_to_display = saved_char_to_display;
19512 it->end_of_box_run_p = saved_box_end;
19513 return true;
19514 }
19515 }
19516
19517 return false;
19518 }
19519
19520
19521 /* Extend the face of the last glyph in the text area of IT->glyph_row
19522 to the end of the display line. Called from display_line. If the
19523 glyph row is empty, add a space glyph to it so that we know the
19524 face to draw. Set the glyph row flag fill_line_p. If the glyph
19525 row is R2L, prepend a stretch glyph to cover the empty space to the
19526 left of the leftmost glyph. */
19527
19528 static void
19529 extend_face_to_end_of_line (struct it *it)
19530 {
19531 struct face *face, *default_face;
19532 struct frame *f = it->f;
19533
19534 /* If line is already filled, do nothing. Non window-system frames
19535 get a grace of one more ``pixel'' because their characters are
19536 1-``pixel'' wide, so they hit the equality too early. This grace
19537 is needed only for R2L rows that are not continued, to produce
19538 one extra blank where we could display the cursor. */
19539 if ((it->current_x >= it->last_visible_x
19540 + (!FRAME_WINDOW_P (f)
19541 && it->glyph_row->reversed_p
19542 && !it->glyph_row->continued_p))
19543 /* If the window has display margins, we will need to extend
19544 their face even if the text area is filled. */
19545 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19546 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19547 return;
19548
19549 /* The default face, possibly remapped. */
19550 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19551
19552 /* Face extension extends the background and box of IT->face_id
19553 to the end of the line. If the background equals the background
19554 of the frame, we don't have to do anything. */
19555 if (it->face_before_selective_p)
19556 face = FACE_FROM_ID (f, it->saved_face_id);
19557 else
19558 face = FACE_FROM_ID (f, it->face_id);
19559
19560 if (FRAME_WINDOW_P (f)
19561 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19562 && face->box == FACE_NO_BOX
19563 && face->background == FRAME_BACKGROUND_PIXEL (f)
19564 #ifdef HAVE_WINDOW_SYSTEM
19565 && !face->stipple
19566 #endif
19567 && !it->glyph_row->reversed_p)
19568 return;
19569
19570 /* Set the glyph row flag indicating that the face of the last glyph
19571 in the text area has to be drawn to the end of the text area. */
19572 it->glyph_row->fill_line_p = true;
19573
19574 /* If current character of IT is not ASCII, make sure we have the
19575 ASCII face. This will be automatically undone the next time
19576 get_next_display_element returns a multibyte character. Note
19577 that the character will always be single byte in unibyte
19578 text. */
19579 if (!ASCII_CHAR_P (it->c))
19580 {
19581 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19582 }
19583
19584 if (FRAME_WINDOW_P (f))
19585 {
19586 /* If the row is empty, add a space with the current face of IT,
19587 so that we know which face to draw. */
19588 if (it->glyph_row->used[TEXT_AREA] == 0)
19589 {
19590 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19591 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19592 it->glyph_row->used[TEXT_AREA] = 1;
19593 }
19594 /* Mode line and the header line don't have margins, and
19595 likewise the frame's tool-bar window, if there is any. */
19596 if (!(it->glyph_row->mode_line_p
19597 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19598 || (WINDOWP (f->tool_bar_window)
19599 && it->w == XWINDOW (f->tool_bar_window))
19600 #endif
19601 ))
19602 {
19603 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19604 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19605 {
19606 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19607 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19608 default_face->id;
19609 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19610 }
19611 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19612 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19613 {
19614 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19615 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19616 default_face->id;
19617 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19618 }
19619 }
19620 #ifdef HAVE_WINDOW_SYSTEM
19621 if (it->glyph_row->reversed_p)
19622 {
19623 /* Prepend a stretch glyph to the row, such that the
19624 rightmost glyph will be drawn flushed all the way to the
19625 right margin of the window. The stretch glyph that will
19626 occupy the empty space, if any, to the left of the
19627 glyphs. */
19628 struct font *font = face->font ? face->font : FRAME_FONT (f);
19629 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19630 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19631 struct glyph *g;
19632 int row_width, stretch_ascent, stretch_width;
19633 struct text_pos saved_pos;
19634 int saved_face_id;
19635 bool saved_avoid_cursor, saved_box_start;
19636
19637 for (row_width = 0, g = row_start; g < row_end; g++)
19638 row_width += g->pixel_width;
19639
19640 /* FIXME: There are various minor display glitches in R2L
19641 rows when only one of the fringes is missing. The
19642 strange condition below produces the least bad effect. */
19643 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19644 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19645 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19646 stretch_width = window_box_width (it->w, TEXT_AREA);
19647 else
19648 stretch_width = it->last_visible_x - it->first_visible_x;
19649 stretch_width -= row_width;
19650
19651 if (stretch_width > 0)
19652 {
19653 stretch_ascent =
19654 (((it->ascent + it->descent)
19655 * FONT_BASE (font)) / FONT_HEIGHT (font));
19656 saved_pos = it->position;
19657 memset (&it->position, 0, sizeof it->position);
19658 saved_avoid_cursor = it->avoid_cursor_p;
19659 it->avoid_cursor_p = true;
19660 saved_face_id = it->face_id;
19661 saved_box_start = it->start_of_box_run_p;
19662 /* The last row's stretch glyph should get the default
19663 face, to avoid painting the rest of the window with
19664 the region face, if the region ends at ZV. */
19665 if (it->glyph_row->ends_at_zv_p)
19666 it->face_id = default_face->id;
19667 else
19668 it->face_id = face->id;
19669 it->start_of_box_run_p = false;
19670 append_stretch_glyph (it, Qnil, stretch_width,
19671 it->ascent + it->descent, stretch_ascent);
19672 it->position = saved_pos;
19673 it->avoid_cursor_p = saved_avoid_cursor;
19674 it->face_id = saved_face_id;
19675 it->start_of_box_run_p = saved_box_start;
19676 }
19677 /* If stretch_width comes out negative, it means that the
19678 last glyph is only partially visible. In R2L rows, we
19679 want the leftmost glyph to be partially visible, so we
19680 need to give the row the corresponding left offset. */
19681 if (stretch_width < 0)
19682 it->glyph_row->x = stretch_width;
19683 }
19684 #endif /* HAVE_WINDOW_SYSTEM */
19685 }
19686 else
19687 {
19688 /* Save some values that must not be changed. */
19689 int saved_x = it->current_x;
19690 struct text_pos saved_pos;
19691 Lisp_Object saved_object;
19692 enum display_element_type saved_what = it->what;
19693 int saved_face_id = it->face_id;
19694
19695 saved_object = it->object;
19696 saved_pos = it->position;
19697
19698 it->what = IT_CHARACTER;
19699 memset (&it->position, 0, sizeof it->position);
19700 it->object = Qnil;
19701 it->c = it->char_to_display = ' ';
19702 it->len = 1;
19703
19704 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19705 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19706 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19707 && !it->glyph_row->mode_line_p
19708 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19709 {
19710 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19711 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19712
19713 for (it->current_x = 0; g < e; g++)
19714 it->current_x += g->pixel_width;
19715
19716 it->area = LEFT_MARGIN_AREA;
19717 it->face_id = default_face->id;
19718 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19719 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19720 {
19721 PRODUCE_GLYPHS (it);
19722 /* term.c:produce_glyphs advances it->current_x only for
19723 TEXT_AREA. */
19724 it->current_x += it->pixel_width;
19725 }
19726
19727 it->current_x = saved_x;
19728 it->area = TEXT_AREA;
19729 }
19730
19731 /* The last row's blank glyphs should get the default face, to
19732 avoid painting the rest of the window with the region face,
19733 if the region ends at ZV. */
19734 if (it->glyph_row->ends_at_zv_p)
19735 it->face_id = default_face->id;
19736 else
19737 it->face_id = face->id;
19738 PRODUCE_GLYPHS (it);
19739
19740 while (it->current_x <= it->last_visible_x)
19741 PRODUCE_GLYPHS (it);
19742
19743 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19744 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19745 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19746 && !it->glyph_row->mode_line_p
19747 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19748 {
19749 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19750 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19751
19752 for ( ; g < e; g++)
19753 it->current_x += g->pixel_width;
19754
19755 it->area = RIGHT_MARGIN_AREA;
19756 it->face_id = default_face->id;
19757 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19758 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19759 {
19760 PRODUCE_GLYPHS (it);
19761 it->current_x += it->pixel_width;
19762 }
19763
19764 it->area = TEXT_AREA;
19765 }
19766
19767 /* Don't count these blanks really. It would let us insert a left
19768 truncation glyph below and make us set the cursor on them, maybe. */
19769 it->current_x = saved_x;
19770 it->object = saved_object;
19771 it->position = saved_pos;
19772 it->what = saved_what;
19773 it->face_id = saved_face_id;
19774 }
19775 }
19776
19777
19778 /* Value is true if text starting at CHARPOS in current_buffer is
19779 trailing whitespace. */
19780
19781 static bool
19782 trailing_whitespace_p (ptrdiff_t charpos)
19783 {
19784 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19785 int c = 0;
19786
19787 while (bytepos < ZV_BYTE
19788 && (c = FETCH_CHAR (bytepos),
19789 c == ' ' || c == '\t'))
19790 ++bytepos;
19791
19792 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19793 {
19794 if (bytepos != PT_BYTE)
19795 return true;
19796 }
19797 return false;
19798 }
19799
19800
19801 /* Highlight trailing whitespace, if any, in ROW. */
19802
19803 static void
19804 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19805 {
19806 int used = row->used[TEXT_AREA];
19807
19808 if (used)
19809 {
19810 struct glyph *start = row->glyphs[TEXT_AREA];
19811 struct glyph *glyph = start + used - 1;
19812
19813 if (row->reversed_p)
19814 {
19815 /* Right-to-left rows need to be processed in the opposite
19816 direction, so swap the edge pointers. */
19817 glyph = start;
19818 start = row->glyphs[TEXT_AREA] + used - 1;
19819 }
19820
19821 /* Skip over glyphs inserted to display the cursor at the
19822 end of a line, for extending the face of the last glyph
19823 to the end of the line on terminals, and for truncation
19824 and continuation glyphs. */
19825 if (!row->reversed_p)
19826 {
19827 while (glyph >= start
19828 && glyph->type == CHAR_GLYPH
19829 && NILP (glyph->object))
19830 --glyph;
19831 }
19832 else
19833 {
19834 while (glyph <= start
19835 && glyph->type == CHAR_GLYPH
19836 && NILP (glyph->object))
19837 ++glyph;
19838 }
19839
19840 /* If last glyph is a space or stretch, and it's trailing
19841 whitespace, set the face of all trailing whitespace glyphs in
19842 IT->glyph_row to `trailing-whitespace'. */
19843 if ((row->reversed_p ? glyph <= start : glyph >= start)
19844 && BUFFERP (glyph->object)
19845 && (glyph->type == STRETCH_GLYPH
19846 || (glyph->type == CHAR_GLYPH
19847 && glyph->u.ch == ' '))
19848 && trailing_whitespace_p (glyph->charpos))
19849 {
19850 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19851 if (face_id < 0)
19852 return;
19853
19854 if (!row->reversed_p)
19855 {
19856 while (glyph >= start
19857 && BUFFERP (glyph->object)
19858 && (glyph->type == STRETCH_GLYPH
19859 || (glyph->type == CHAR_GLYPH
19860 && glyph->u.ch == ' ')))
19861 (glyph--)->face_id = face_id;
19862 }
19863 else
19864 {
19865 while (glyph <= start
19866 && BUFFERP (glyph->object)
19867 && (glyph->type == STRETCH_GLYPH
19868 || (glyph->type == CHAR_GLYPH
19869 && glyph->u.ch == ' ')))
19870 (glyph++)->face_id = face_id;
19871 }
19872 }
19873 }
19874 }
19875
19876
19877 /* Value is true if glyph row ROW should be
19878 considered to hold the buffer position CHARPOS. */
19879
19880 static bool
19881 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19882 {
19883 bool result = true;
19884
19885 if (charpos == CHARPOS (row->end.pos)
19886 || charpos == MATRIX_ROW_END_CHARPOS (row))
19887 {
19888 /* Suppose the row ends on a string.
19889 Unless the row is continued, that means it ends on a newline
19890 in the string. If it's anything other than a display string
19891 (e.g., a before-string from an overlay), we don't want the
19892 cursor there. (This heuristic seems to give the optimal
19893 behavior for the various types of multi-line strings.)
19894 One exception: if the string has `cursor' property on one of
19895 its characters, we _do_ want the cursor there. */
19896 if (CHARPOS (row->end.string_pos) >= 0)
19897 {
19898 if (row->continued_p)
19899 result = true;
19900 else
19901 {
19902 /* Check for `display' property. */
19903 struct glyph *beg = row->glyphs[TEXT_AREA];
19904 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19905 struct glyph *glyph;
19906
19907 result = false;
19908 for (glyph = end; glyph >= beg; --glyph)
19909 if (STRINGP (glyph->object))
19910 {
19911 Lisp_Object prop
19912 = Fget_char_property (make_number (charpos),
19913 Qdisplay, Qnil);
19914 result =
19915 (!NILP (prop)
19916 && display_prop_string_p (prop, glyph->object));
19917 /* If there's a `cursor' property on one of the
19918 string's characters, this row is a cursor row,
19919 even though this is not a display string. */
19920 if (!result)
19921 {
19922 Lisp_Object s = glyph->object;
19923
19924 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19925 {
19926 ptrdiff_t gpos = glyph->charpos;
19927
19928 if (!NILP (Fget_char_property (make_number (gpos),
19929 Qcursor, s)))
19930 {
19931 result = true;
19932 break;
19933 }
19934 }
19935 }
19936 break;
19937 }
19938 }
19939 }
19940 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19941 {
19942 /* If the row ends in middle of a real character,
19943 and the line is continued, we want the cursor here.
19944 That's because CHARPOS (ROW->end.pos) would equal
19945 PT if PT is before the character. */
19946 if (!row->ends_in_ellipsis_p)
19947 result = row->continued_p;
19948 else
19949 /* If the row ends in an ellipsis, then
19950 CHARPOS (ROW->end.pos) will equal point after the
19951 invisible text. We want that position to be displayed
19952 after the ellipsis. */
19953 result = false;
19954 }
19955 /* If the row ends at ZV, display the cursor at the end of that
19956 row instead of at the start of the row below. */
19957 else
19958 result = row->ends_at_zv_p;
19959 }
19960
19961 return result;
19962 }
19963
19964 /* Value is true if glyph row ROW should be
19965 used to hold the cursor. */
19966
19967 static bool
19968 cursor_row_p (struct glyph_row *row)
19969 {
19970 return row_for_charpos_p (row, PT);
19971 }
19972
19973 \f
19974
19975 /* Push the property PROP so that it will be rendered at the current
19976 position in IT. Return true if PROP was successfully pushed, false
19977 otherwise. Called from handle_line_prefix to handle the
19978 `line-prefix' and `wrap-prefix' properties. */
19979
19980 static bool
19981 push_prefix_prop (struct it *it, Lisp_Object prop)
19982 {
19983 struct text_pos pos =
19984 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19985
19986 eassert (it->method == GET_FROM_BUFFER
19987 || it->method == GET_FROM_DISPLAY_VECTOR
19988 || it->method == GET_FROM_STRING
19989 || it->method == GET_FROM_IMAGE);
19990
19991 /* We need to save the current buffer/string position, so it will be
19992 restored by pop_it, because iterate_out_of_display_property
19993 depends on that being set correctly, but some situations leave
19994 it->position not yet set when this function is called. */
19995 push_it (it, &pos);
19996
19997 if (STRINGP (prop))
19998 {
19999 if (SCHARS (prop) == 0)
20000 {
20001 pop_it (it);
20002 return false;
20003 }
20004
20005 it->string = prop;
20006 it->string_from_prefix_prop_p = true;
20007 it->multibyte_p = STRING_MULTIBYTE (it->string);
20008 it->current.overlay_string_index = -1;
20009 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20010 it->end_charpos = it->string_nchars = SCHARS (it->string);
20011 it->method = GET_FROM_STRING;
20012 it->stop_charpos = 0;
20013 it->prev_stop = 0;
20014 it->base_level_stop = 0;
20015
20016 /* Force paragraph direction to be that of the parent
20017 buffer/string. */
20018 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20019 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20020 else
20021 it->paragraph_embedding = L2R;
20022
20023 /* Set up the bidi iterator for this display string. */
20024 if (it->bidi_p)
20025 {
20026 it->bidi_it.string.lstring = it->string;
20027 it->bidi_it.string.s = NULL;
20028 it->bidi_it.string.schars = it->end_charpos;
20029 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20030 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20031 it->bidi_it.string.unibyte = !it->multibyte_p;
20032 it->bidi_it.w = it->w;
20033 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20034 }
20035 }
20036 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20037 {
20038 it->method = GET_FROM_STRETCH;
20039 it->object = prop;
20040 }
20041 #ifdef HAVE_WINDOW_SYSTEM
20042 else if (IMAGEP (prop))
20043 {
20044 it->what = IT_IMAGE;
20045 it->image_id = lookup_image (it->f, prop);
20046 it->method = GET_FROM_IMAGE;
20047 }
20048 #endif /* HAVE_WINDOW_SYSTEM */
20049 else
20050 {
20051 pop_it (it); /* bogus display property, give up */
20052 return false;
20053 }
20054
20055 return true;
20056 }
20057
20058 /* Return the character-property PROP at the current position in IT. */
20059
20060 static Lisp_Object
20061 get_it_property (struct it *it, Lisp_Object prop)
20062 {
20063 Lisp_Object position, object = it->object;
20064
20065 if (STRINGP (object))
20066 position = make_number (IT_STRING_CHARPOS (*it));
20067 else if (BUFFERP (object))
20068 {
20069 position = make_number (IT_CHARPOS (*it));
20070 object = it->window;
20071 }
20072 else
20073 return Qnil;
20074
20075 return Fget_char_property (position, prop, object);
20076 }
20077
20078 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20079
20080 static void
20081 handle_line_prefix (struct it *it)
20082 {
20083 Lisp_Object prefix;
20084
20085 if (it->continuation_lines_width > 0)
20086 {
20087 prefix = get_it_property (it, Qwrap_prefix);
20088 if (NILP (prefix))
20089 prefix = Vwrap_prefix;
20090 }
20091 else
20092 {
20093 prefix = get_it_property (it, Qline_prefix);
20094 if (NILP (prefix))
20095 prefix = Vline_prefix;
20096 }
20097 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20098 {
20099 /* If the prefix is wider than the window, and we try to wrap
20100 it, it would acquire its own wrap prefix, and so on till the
20101 iterator stack overflows. So, don't wrap the prefix. */
20102 it->line_wrap = TRUNCATE;
20103 it->avoid_cursor_p = true;
20104 }
20105 }
20106
20107 \f
20108
20109 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20110 only for R2L lines from display_line and display_string, when they
20111 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20112 the line/string needs to be continued on the next glyph row. */
20113 static void
20114 unproduce_glyphs (struct it *it, int n)
20115 {
20116 struct glyph *glyph, *end;
20117
20118 eassert (it->glyph_row);
20119 eassert (it->glyph_row->reversed_p);
20120 eassert (it->area == TEXT_AREA);
20121 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20122
20123 if (n > it->glyph_row->used[TEXT_AREA])
20124 n = it->glyph_row->used[TEXT_AREA];
20125 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20126 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20127 for ( ; glyph < end; glyph++)
20128 glyph[-n] = *glyph;
20129 }
20130
20131 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20132 and ROW->maxpos. */
20133 static void
20134 find_row_edges (struct it *it, struct glyph_row *row,
20135 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20136 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20137 {
20138 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20139 lines' rows is implemented for bidi-reordered rows. */
20140
20141 /* ROW->minpos is the value of min_pos, the minimal buffer position
20142 we have in ROW, or ROW->start.pos if that is smaller. */
20143 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20144 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20145 else
20146 /* We didn't find buffer positions smaller than ROW->start, or
20147 didn't find _any_ valid buffer positions in any of the glyphs,
20148 so we must trust the iterator's computed positions. */
20149 row->minpos = row->start.pos;
20150 if (max_pos <= 0)
20151 {
20152 max_pos = CHARPOS (it->current.pos);
20153 max_bpos = BYTEPOS (it->current.pos);
20154 }
20155
20156 /* Here are the various use-cases for ending the row, and the
20157 corresponding values for ROW->maxpos:
20158
20159 Line ends in a newline from buffer eol_pos + 1
20160 Line is continued from buffer max_pos + 1
20161 Line is truncated on right it->current.pos
20162 Line ends in a newline from string max_pos + 1(*)
20163 (*) + 1 only when line ends in a forward scan
20164 Line is continued from string max_pos
20165 Line is continued from display vector max_pos
20166 Line is entirely from a string min_pos == max_pos
20167 Line is entirely from a display vector min_pos == max_pos
20168 Line that ends at ZV ZV
20169
20170 If you discover other use-cases, please add them here as
20171 appropriate. */
20172 if (row->ends_at_zv_p)
20173 row->maxpos = it->current.pos;
20174 else if (row->used[TEXT_AREA])
20175 {
20176 bool seen_this_string = false;
20177 struct glyph_row *r1 = row - 1;
20178
20179 /* Did we see the same display string on the previous row? */
20180 if (STRINGP (it->object)
20181 /* this is not the first row */
20182 && row > it->w->desired_matrix->rows
20183 /* previous row is not the header line */
20184 && !r1->mode_line_p
20185 /* previous row also ends in a newline from a string */
20186 && r1->ends_in_newline_from_string_p)
20187 {
20188 struct glyph *start, *end;
20189
20190 /* Search for the last glyph of the previous row that came
20191 from buffer or string. Depending on whether the row is
20192 L2R or R2L, we need to process it front to back or the
20193 other way round. */
20194 if (!r1->reversed_p)
20195 {
20196 start = r1->glyphs[TEXT_AREA];
20197 end = start + r1->used[TEXT_AREA];
20198 /* Glyphs inserted by redisplay have nil as their object. */
20199 while (end > start
20200 && NILP ((end - 1)->object)
20201 && (end - 1)->charpos <= 0)
20202 --end;
20203 if (end > start)
20204 {
20205 if (EQ ((end - 1)->object, it->object))
20206 seen_this_string = true;
20207 }
20208 else
20209 /* If all the glyphs of the previous row were inserted
20210 by redisplay, it means the previous row was
20211 produced from a single newline, which is only
20212 possible if that newline came from the same string
20213 as the one which produced this ROW. */
20214 seen_this_string = true;
20215 }
20216 else
20217 {
20218 end = r1->glyphs[TEXT_AREA] - 1;
20219 start = end + r1->used[TEXT_AREA];
20220 while (end < start
20221 && NILP ((end + 1)->object)
20222 && (end + 1)->charpos <= 0)
20223 ++end;
20224 if (end < start)
20225 {
20226 if (EQ ((end + 1)->object, it->object))
20227 seen_this_string = true;
20228 }
20229 else
20230 seen_this_string = true;
20231 }
20232 }
20233 /* Take note of each display string that covers a newline only
20234 once, the first time we see it. This is for when a display
20235 string includes more than one newline in it. */
20236 if (row->ends_in_newline_from_string_p && !seen_this_string)
20237 {
20238 /* If we were scanning the buffer forward when we displayed
20239 the string, we want to account for at least one buffer
20240 position that belongs to this row (position covered by
20241 the display string), so that cursor positioning will
20242 consider this row as a candidate when point is at the end
20243 of the visual line represented by this row. This is not
20244 required when scanning back, because max_pos will already
20245 have a much larger value. */
20246 if (CHARPOS (row->end.pos) > max_pos)
20247 INC_BOTH (max_pos, max_bpos);
20248 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20249 }
20250 else if (CHARPOS (it->eol_pos) > 0)
20251 SET_TEXT_POS (row->maxpos,
20252 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20253 else if (row->continued_p)
20254 {
20255 /* If max_pos is different from IT's current position, it
20256 means IT->method does not belong to the display element
20257 at max_pos. However, it also means that the display
20258 element at max_pos was displayed in its entirety on this
20259 line, which is equivalent to saying that the next line
20260 starts at the next buffer position. */
20261 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20262 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20263 else
20264 {
20265 INC_BOTH (max_pos, max_bpos);
20266 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20267 }
20268 }
20269 else if (row->truncated_on_right_p)
20270 /* display_line already called reseat_at_next_visible_line_start,
20271 which puts the iterator at the beginning of the next line, in
20272 the logical order. */
20273 row->maxpos = it->current.pos;
20274 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20275 /* A line that is entirely from a string/image/stretch... */
20276 row->maxpos = row->minpos;
20277 else
20278 emacs_abort ();
20279 }
20280 else
20281 row->maxpos = it->current.pos;
20282 }
20283
20284 /* Construct the glyph row IT->glyph_row in the desired matrix of
20285 IT->w from text at the current position of IT. See dispextern.h
20286 for an overview of struct it. Value is true if
20287 IT->glyph_row displays text, as opposed to a line displaying ZV
20288 only. */
20289
20290 static bool
20291 display_line (struct it *it)
20292 {
20293 struct glyph_row *row = it->glyph_row;
20294 Lisp_Object overlay_arrow_string;
20295 struct it wrap_it;
20296 void *wrap_data = NULL;
20297 bool may_wrap = false;
20298 int wrap_x IF_LINT (= 0);
20299 int wrap_row_used = -1;
20300 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20301 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20302 int wrap_row_extra_line_spacing IF_LINT (= 0);
20303 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20304 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20305 int cvpos;
20306 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20307 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20308 bool pending_handle_line_prefix = false;
20309
20310 /* We always start displaying at hpos zero even if hscrolled. */
20311 eassert (it->hpos == 0 && it->current_x == 0);
20312
20313 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20314 >= it->w->desired_matrix->nrows)
20315 {
20316 it->w->nrows_scale_factor++;
20317 it->f->fonts_changed = true;
20318 return false;
20319 }
20320
20321 /* Clear the result glyph row and enable it. */
20322 prepare_desired_row (it->w, row, false);
20323
20324 row->y = it->current_y;
20325 row->start = it->start;
20326 row->continuation_lines_width = it->continuation_lines_width;
20327 row->displays_text_p = true;
20328 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20329 it->starts_in_middle_of_char_p = false;
20330
20331 /* Arrange the overlays nicely for our purposes. Usually, we call
20332 display_line on only one line at a time, in which case this
20333 can't really hurt too much, or we call it on lines which appear
20334 one after another in the buffer, in which case all calls to
20335 recenter_overlay_lists but the first will be pretty cheap. */
20336 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20337
20338 /* Move over display elements that are not visible because we are
20339 hscrolled. This may stop at an x-position < IT->first_visible_x
20340 if the first glyph is partially visible or if we hit a line end. */
20341 if (it->current_x < it->first_visible_x)
20342 {
20343 enum move_it_result move_result;
20344
20345 this_line_min_pos = row->start.pos;
20346 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20347 MOVE_TO_POS | MOVE_TO_X);
20348 /* If we are under a large hscroll, move_it_in_display_line_to
20349 could hit the end of the line without reaching
20350 it->first_visible_x. Pretend that we did reach it. This is
20351 especially important on a TTY, where we will call
20352 extend_face_to_end_of_line, which needs to know how many
20353 blank glyphs to produce. */
20354 if (it->current_x < it->first_visible_x
20355 && (move_result == MOVE_NEWLINE_OR_CR
20356 || move_result == MOVE_POS_MATCH_OR_ZV))
20357 it->current_x = it->first_visible_x;
20358
20359 /* Record the smallest positions seen while we moved over
20360 display elements that are not visible. This is needed by
20361 redisplay_internal for optimizing the case where the cursor
20362 stays inside the same line. The rest of this function only
20363 considers positions that are actually displayed, so
20364 RECORD_MAX_MIN_POS will not otherwise record positions that
20365 are hscrolled to the left of the left edge of the window. */
20366 min_pos = CHARPOS (this_line_min_pos);
20367 min_bpos = BYTEPOS (this_line_min_pos);
20368 }
20369 else if (it->area == TEXT_AREA)
20370 {
20371 /* We only do this when not calling move_it_in_display_line_to
20372 above, because that function calls itself handle_line_prefix. */
20373 handle_line_prefix (it);
20374 }
20375 else
20376 {
20377 /* Line-prefix and wrap-prefix are always displayed in the text
20378 area. But if this is the first call to display_line after
20379 init_iterator, the iterator might have been set up to write
20380 into a marginal area, e.g. if the line begins with some
20381 display property that writes to the margins. So we need to
20382 wait with the call to handle_line_prefix until whatever
20383 writes to the margin has done its job. */
20384 pending_handle_line_prefix = true;
20385 }
20386
20387 /* Get the initial row height. This is either the height of the
20388 text hscrolled, if there is any, or zero. */
20389 row->ascent = it->max_ascent;
20390 row->height = it->max_ascent + it->max_descent;
20391 row->phys_ascent = it->max_phys_ascent;
20392 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20393 row->extra_line_spacing = it->max_extra_line_spacing;
20394
20395 /* Utility macro to record max and min buffer positions seen until now. */
20396 #define RECORD_MAX_MIN_POS(IT) \
20397 do \
20398 { \
20399 bool composition_p \
20400 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20401 ptrdiff_t current_pos = \
20402 composition_p ? (IT)->cmp_it.charpos \
20403 : IT_CHARPOS (*(IT)); \
20404 ptrdiff_t current_bpos = \
20405 composition_p ? CHAR_TO_BYTE (current_pos) \
20406 : IT_BYTEPOS (*(IT)); \
20407 if (current_pos < min_pos) \
20408 { \
20409 min_pos = current_pos; \
20410 min_bpos = current_bpos; \
20411 } \
20412 if (IT_CHARPOS (*it) > max_pos) \
20413 { \
20414 max_pos = IT_CHARPOS (*it); \
20415 max_bpos = IT_BYTEPOS (*it); \
20416 } \
20417 } \
20418 while (false)
20419
20420 /* Loop generating characters. The loop is left with IT on the next
20421 character to display. */
20422 while (true)
20423 {
20424 int n_glyphs_before, hpos_before, x_before;
20425 int x, nglyphs;
20426 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20427
20428 /* Retrieve the next thing to display. Value is false if end of
20429 buffer reached. */
20430 if (!get_next_display_element (it))
20431 {
20432 /* Maybe add a space at the end of this line that is used to
20433 display the cursor there under X. Set the charpos of the
20434 first glyph of blank lines not corresponding to any text
20435 to -1. */
20436 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20437 row->exact_window_width_line_p = true;
20438 else if ((append_space_for_newline (it, true)
20439 && row->used[TEXT_AREA] == 1)
20440 || row->used[TEXT_AREA] == 0)
20441 {
20442 row->glyphs[TEXT_AREA]->charpos = -1;
20443 row->displays_text_p = false;
20444
20445 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20446 && (!MINI_WINDOW_P (it->w)
20447 || (minibuf_level && EQ (it->window, minibuf_window))))
20448 row->indicate_empty_line_p = true;
20449 }
20450
20451 it->continuation_lines_width = 0;
20452 row->ends_at_zv_p = true;
20453 /* A row that displays right-to-left text must always have
20454 its last face extended all the way to the end of line,
20455 even if this row ends in ZV, because we still write to
20456 the screen left to right. We also need to extend the
20457 last face if the default face is remapped to some
20458 different face, otherwise the functions that clear
20459 portions of the screen will clear with the default face's
20460 background color. */
20461 if (row->reversed_p
20462 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20463 extend_face_to_end_of_line (it);
20464 break;
20465 }
20466
20467 /* Now, get the metrics of what we want to display. This also
20468 generates glyphs in `row' (which is IT->glyph_row). */
20469 n_glyphs_before = row->used[TEXT_AREA];
20470 x = it->current_x;
20471
20472 /* Remember the line height so far in case the next element doesn't
20473 fit on the line. */
20474 if (it->line_wrap != TRUNCATE)
20475 {
20476 ascent = it->max_ascent;
20477 descent = it->max_descent;
20478 phys_ascent = it->max_phys_ascent;
20479 phys_descent = it->max_phys_descent;
20480
20481 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20482 {
20483 if (IT_DISPLAYING_WHITESPACE (it))
20484 may_wrap = true;
20485 else if (may_wrap)
20486 {
20487 SAVE_IT (wrap_it, *it, wrap_data);
20488 wrap_x = x;
20489 wrap_row_used = row->used[TEXT_AREA];
20490 wrap_row_ascent = row->ascent;
20491 wrap_row_height = row->height;
20492 wrap_row_phys_ascent = row->phys_ascent;
20493 wrap_row_phys_height = row->phys_height;
20494 wrap_row_extra_line_spacing = row->extra_line_spacing;
20495 wrap_row_min_pos = min_pos;
20496 wrap_row_min_bpos = min_bpos;
20497 wrap_row_max_pos = max_pos;
20498 wrap_row_max_bpos = max_bpos;
20499 may_wrap = false;
20500 }
20501 }
20502 }
20503
20504 PRODUCE_GLYPHS (it);
20505
20506 /* If this display element was in marginal areas, continue with
20507 the next one. */
20508 if (it->area != TEXT_AREA)
20509 {
20510 row->ascent = max (row->ascent, it->max_ascent);
20511 row->height = max (row->height, it->max_ascent + it->max_descent);
20512 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20513 row->phys_height = max (row->phys_height,
20514 it->max_phys_ascent + it->max_phys_descent);
20515 row->extra_line_spacing = max (row->extra_line_spacing,
20516 it->max_extra_line_spacing);
20517 set_iterator_to_next (it, true);
20518 /* If we didn't handle the line/wrap prefix above, and the
20519 call to set_iterator_to_next just switched to TEXT_AREA,
20520 process the prefix now. */
20521 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20522 {
20523 pending_handle_line_prefix = false;
20524 handle_line_prefix (it);
20525 }
20526 continue;
20527 }
20528
20529 /* Does the display element fit on the line? If we truncate
20530 lines, we should draw past the right edge of the window. If
20531 we don't truncate, we want to stop so that we can display the
20532 continuation glyph before the right margin. If lines are
20533 continued, there are two possible strategies for characters
20534 resulting in more than 1 glyph (e.g. tabs): Display as many
20535 glyphs as possible in this line and leave the rest for the
20536 continuation line, or display the whole element in the next
20537 line. Original redisplay did the former, so we do it also. */
20538 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20539 hpos_before = it->hpos;
20540 x_before = x;
20541
20542 if (/* Not a newline. */
20543 nglyphs > 0
20544 /* Glyphs produced fit entirely in the line. */
20545 && it->current_x < it->last_visible_x)
20546 {
20547 it->hpos += nglyphs;
20548 row->ascent = max (row->ascent, it->max_ascent);
20549 row->height = max (row->height, it->max_ascent + it->max_descent);
20550 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20551 row->phys_height = max (row->phys_height,
20552 it->max_phys_ascent + it->max_phys_descent);
20553 row->extra_line_spacing = max (row->extra_line_spacing,
20554 it->max_extra_line_spacing);
20555 if (it->current_x - it->pixel_width < it->first_visible_x
20556 /* In R2L rows, we arrange in extend_face_to_end_of_line
20557 to add a right offset to the line, by a suitable
20558 change to the stretch glyph that is the leftmost
20559 glyph of the line. */
20560 && !row->reversed_p)
20561 row->x = x - it->first_visible_x;
20562 /* Record the maximum and minimum buffer positions seen so
20563 far in glyphs that will be displayed by this row. */
20564 if (it->bidi_p)
20565 RECORD_MAX_MIN_POS (it);
20566 }
20567 else
20568 {
20569 int i, new_x;
20570 struct glyph *glyph;
20571
20572 for (i = 0; i < nglyphs; ++i, x = new_x)
20573 {
20574 /* Identify the glyphs added by the last call to
20575 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20576 the previous glyphs. */
20577 if (!row->reversed_p)
20578 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20579 else
20580 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20581 new_x = x + glyph->pixel_width;
20582
20583 if (/* Lines are continued. */
20584 it->line_wrap != TRUNCATE
20585 && (/* Glyph doesn't fit on the line. */
20586 new_x > it->last_visible_x
20587 /* Or it fits exactly on a window system frame. */
20588 || (new_x == it->last_visible_x
20589 && FRAME_WINDOW_P (it->f)
20590 && (row->reversed_p
20591 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20592 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20593 {
20594 /* End of a continued line. */
20595
20596 if (it->hpos == 0
20597 || (new_x == it->last_visible_x
20598 && FRAME_WINDOW_P (it->f)
20599 && (row->reversed_p
20600 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20601 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20602 {
20603 /* Current glyph is the only one on the line or
20604 fits exactly on the line. We must continue
20605 the line because we can't draw the cursor
20606 after the glyph. */
20607 row->continued_p = true;
20608 it->current_x = new_x;
20609 it->continuation_lines_width += new_x;
20610 ++it->hpos;
20611 if (i == nglyphs - 1)
20612 {
20613 /* If line-wrap is on, check if a previous
20614 wrap point was found. */
20615 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20616 && wrap_row_used > 0
20617 /* Even if there is a previous wrap
20618 point, continue the line here as
20619 usual, if (i) the previous character
20620 was a space or tab AND (ii) the
20621 current character is not. */
20622 && (!may_wrap
20623 || IT_DISPLAYING_WHITESPACE (it)))
20624 goto back_to_wrap;
20625
20626 /* Record the maximum and minimum buffer
20627 positions seen so far in glyphs that will be
20628 displayed by this row. */
20629 if (it->bidi_p)
20630 RECORD_MAX_MIN_POS (it);
20631 set_iterator_to_next (it, true);
20632 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20633 {
20634 if (!get_next_display_element (it))
20635 {
20636 row->exact_window_width_line_p = true;
20637 it->continuation_lines_width = 0;
20638 row->continued_p = false;
20639 row->ends_at_zv_p = true;
20640 }
20641 else if (ITERATOR_AT_END_OF_LINE_P (it))
20642 {
20643 row->continued_p = false;
20644 row->exact_window_width_line_p = true;
20645 }
20646 /* If line-wrap is on, check if a
20647 previous wrap point was found. */
20648 else if (wrap_row_used > 0
20649 /* Even if there is a previous wrap
20650 point, continue the line here as
20651 usual, if (i) the previous character
20652 was a space or tab AND (ii) the
20653 current character is not. */
20654 && (!may_wrap
20655 || IT_DISPLAYING_WHITESPACE (it)))
20656 goto back_to_wrap;
20657
20658 }
20659 }
20660 else if (it->bidi_p)
20661 RECORD_MAX_MIN_POS (it);
20662 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20663 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20664 extend_face_to_end_of_line (it);
20665 }
20666 else if (CHAR_GLYPH_PADDING_P (*glyph)
20667 && !FRAME_WINDOW_P (it->f))
20668 {
20669 /* A padding glyph that doesn't fit on this line.
20670 This means the whole character doesn't fit
20671 on the line. */
20672 if (row->reversed_p)
20673 unproduce_glyphs (it, row->used[TEXT_AREA]
20674 - n_glyphs_before);
20675 row->used[TEXT_AREA] = n_glyphs_before;
20676
20677 /* Fill the rest of the row with continuation
20678 glyphs like in 20.x. */
20679 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20680 < row->glyphs[1 + TEXT_AREA])
20681 produce_special_glyphs (it, IT_CONTINUATION);
20682
20683 row->continued_p = true;
20684 it->current_x = x_before;
20685 it->continuation_lines_width += x_before;
20686
20687 /* Restore the height to what it was before the
20688 element not fitting on the line. */
20689 it->max_ascent = ascent;
20690 it->max_descent = descent;
20691 it->max_phys_ascent = phys_ascent;
20692 it->max_phys_descent = phys_descent;
20693 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20694 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20695 extend_face_to_end_of_line (it);
20696 }
20697 else if (wrap_row_used > 0)
20698 {
20699 back_to_wrap:
20700 if (row->reversed_p)
20701 unproduce_glyphs (it,
20702 row->used[TEXT_AREA] - wrap_row_used);
20703 RESTORE_IT (it, &wrap_it, wrap_data);
20704 it->continuation_lines_width += wrap_x;
20705 row->used[TEXT_AREA] = wrap_row_used;
20706 row->ascent = wrap_row_ascent;
20707 row->height = wrap_row_height;
20708 row->phys_ascent = wrap_row_phys_ascent;
20709 row->phys_height = wrap_row_phys_height;
20710 row->extra_line_spacing = wrap_row_extra_line_spacing;
20711 min_pos = wrap_row_min_pos;
20712 min_bpos = wrap_row_min_bpos;
20713 max_pos = wrap_row_max_pos;
20714 max_bpos = wrap_row_max_bpos;
20715 row->continued_p = true;
20716 row->ends_at_zv_p = false;
20717 row->exact_window_width_line_p = false;
20718 it->continuation_lines_width += x;
20719
20720 /* Make sure that a non-default face is extended
20721 up to the right margin of the window. */
20722 extend_face_to_end_of_line (it);
20723 }
20724 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20725 {
20726 /* A TAB that extends past the right edge of the
20727 window. This produces a single glyph on
20728 window system frames. We leave the glyph in
20729 this row and let it fill the row, but don't
20730 consume the TAB. */
20731 if ((row->reversed_p
20732 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20733 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20734 produce_special_glyphs (it, IT_CONTINUATION);
20735 it->continuation_lines_width += it->last_visible_x;
20736 row->ends_in_middle_of_char_p = true;
20737 row->continued_p = true;
20738 glyph->pixel_width = it->last_visible_x - x;
20739 it->starts_in_middle_of_char_p = true;
20740 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20741 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20742 extend_face_to_end_of_line (it);
20743 }
20744 else
20745 {
20746 /* Something other than a TAB that draws past
20747 the right edge of the window. Restore
20748 positions to values before the element. */
20749 if (row->reversed_p)
20750 unproduce_glyphs (it, row->used[TEXT_AREA]
20751 - (n_glyphs_before + i));
20752 row->used[TEXT_AREA] = n_glyphs_before + i;
20753
20754 /* Display continuation glyphs. */
20755 it->current_x = x_before;
20756 it->continuation_lines_width += x;
20757 if (!FRAME_WINDOW_P (it->f)
20758 || (row->reversed_p
20759 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20760 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20761 produce_special_glyphs (it, IT_CONTINUATION);
20762 row->continued_p = true;
20763
20764 extend_face_to_end_of_line (it);
20765
20766 if (nglyphs > 1 && i > 0)
20767 {
20768 row->ends_in_middle_of_char_p = true;
20769 it->starts_in_middle_of_char_p = true;
20770 }
20771
20772 /* Restore the height to what it was before the
20773 element not fitting on the line. */
20774 it->max_ascent = ascent;
20775 it->max_descent = descent;
20776 it->max_phys_ascent = phys_ascent;
20777 it->max_phys_descent = phys_descent;
20778 }
20779
20780 break;
20781 }
20782 else if (new_x > it->first_visible_x)
20783 {
20784 /* Increment number of glyphs actually displayed. */
20785 ++it->hpos;
20786
20787 /* Record the maximum and minimum buffer positions
20788 seen so far in glyphs that will be displayed by
20789 this row. */
20790 if (it->bidi_p)
20791 RECORD_MAX_MIN_POS (it);
20792
20793 if (x < it->first_visible_x && !row->reversed_p)
20794 /* Glyph is partially visible, i.e. row starts at
20795 negative X position. Don't do that in R2L
20796 rows, where we arrange to add a right offset to
20797 the line in extend_face_to_end_of_line, by a
20798 suitable change to the stretch glyph that is
20799 the leftmost glyph of the line. */
20800 row->x = x - it->first_visible_x;
20801 /* When the last glyph of an R2L row only fits
20802 partially on the line, we need to set row->x to a
20803 negative offset, so that the leftmost glyph is
20804 the one that is partially visible. But if we are
20805 going to produce the truncation glyph, this will
20806 be taken care of in produce_special_glyphs. */
20807 if (row->reversed_p
20808 && new_x > it->last_visible_x
20809 && !(it->line_wrap == TRUNCATE
20810 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20811 {
20812 eassert (FRAME_WINDOW_P (it->f));
20813 row->x = it->last_visible_x - new_x;
20814 }
20815 }
20816 else
20817 {
20818 /* Glyph is completely off the left margin of the
20819 window. This should not happen because of the
20820 move_it_in_display_line at the start of this
20821 function, unless the text display area of the
20822 window is empty. */
20823 eassert (it->first_visible_x <= it->last_visible_x);
20824 }
20825 }
20826 /* Even if this display element produced no glyphs at all,
20827 we want to record its position. */
20828 if (it->bidi_p && nglyphs == 0)
20829 RECORD_MAX_MIN_POS (it);
20830
20831 row->ascent = max (row->ascent, it->max_ascent);
20832 row->height = max (row->height, it->max_ascent + it->max_descent);
20833 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20834 row->phys_height = max (row->phys_height,
20835 it->max_phys_ascent + it->max_phys_descent);
20836 row->extra_line_spacing = max (row->extra_line_spacing,
20837 it->max_extra_line_spacing);
20838
20839 /* End of this display line if row is continued. */
20840 if (row->continued_p || row->ends_at_zv_p)
20841 break;
20842 }
20843
20844 at_end_of_line:
20845 /* Is this a line end? If yes, we're also done, after making
20846 sure that a non-default face is extended up to the right
20847 margin of the window. */
20848 if (ITERATOR_AT_END_OF_LINE_P (it))
20849 {
20850 int used_before = row->used[TEXT_AREA];
20851
20852 row->ends_in_newline_from_string_p = STRINGP (it->object);
20853
20854 /* Add a space at the end of the line that is used to
20855 display the cursor there. */
20856 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20857 append_space_for_newline (it, false);
20858
20859 /* Extend the face to the end of the line. */
20860 extend_face_to_end_of_line (it);
20861
20862 /* Make sure we have the position. */
20863 if (used_before == 0)
20864 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20865
20866 /* Record the position of the newline, for use in
20867 find_row_edges. */
20868 it->eol_pos = it->current.pos;
20869
20870 /* Consume the line end. This skips over invisible lines. */
20871 set_iterator_to_next (it, true);
20872 it->continuation_lines_width = 0;
20873 break;
20874 }
20875
20876 /* Proceed with next display element. Note that this skips
20877 over lines invisible because of selective display. */
20878 set_iterator_to_next (it, true);
20879
20880 /* If we truncate lines, we are done when the last displayed
20881 glyphs reach past the right margin of the window. */
20882 if (it->line_wrap == TRUNCATE
20883 && ((FRAME_WINDOW_P (it->f)
20884 /* Images are preprocessed in produce_image_glyph such
20885 that they are cropped at the right edge of the
20886 window, so an image glyph will always end exactly at
20887 last_visible_x, even if there's no right fringe. */
20888 && ((row->reversed_p
20889 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20890 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20891 || it->what == IT_IMAGE))
20892 ? (it->current_x >= it->last_visible_x)
20893 : (it->current_x > it->last_visible_x)))
20894 {
20895 /* Maybe add truncation glyphs. */
20896 if (!FRAME_WINDOW_P (it->f)
20897 || (row->reversed_p
20898 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20899 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20900 {
20901 int i, n;
20902
20903 if (!row->reversed_p)
20904 {
20905 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20906 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20907 break;
20908 }
20909 else
20910 {
20911 for (i = 0; i < row->used[TEXT_AREA]; i++)
20912 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20913 break;
20914 /* Remove any padding glyphs at the front of ROW, to
20915 make room for the truncation glyphs we will be
20916 adding below. The loop below always inserts at
20917 least one truncation glyph, so also remove the
20918 last glyph added to ROW. */
20919 unproduce_glyphs (it, i + 1);
20920 /* Adjust i for the loop below. */
20921 i = row->used[TEXT_AREA] - (i + 1);
20922 }
20923
20924 /* produce_special_glyphs overwrites the last glyph, so
20925 we don't want that if we want to keep that last
20926 glyph, which means it's an image. */
20927 if (it->current_x > it->last_visible_x)
20928 {
20929 it->current_x = x_before;
20930 if (!FRAME_WINDOW_P (it->f))
20931 {
20932 for (n = row->used[TEXT_AREA]; i < n; ++i)
20933 {
20934 row->used[TEXT_AREA] = i;
20935 produce_special_glyphs (it, IT_TRUNCATION);
20936 }
20937 }
20938 else
20939 {
20940 row->used[TEXT_AREA] = i;
20941 produce_special_glyphs (it, IT_TRUNCATION);
20942 }
20943 it->hpos = hpos_before;
20944 }
20945 }
20946 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20947 {
20948 /* Don't truncate if we can overflow newline into fringe. */
20949 if (!get_next_display_element (it))
20950 {
20951 it->continuation_lines_width = 0;
20952 row->ends_at_zv_p = true;
20953 row->exact_window_width_line_p = true;
20954 break;
20955 }
20956 if (ITERATOR_AT_END_OF_LINE_P (it))
20957 {
20958 row->exact_window_width_line_p = true;
20959 goto at_end_of_line;
20960 }
20961 it->current_x = x_before;
20962 it->hpos = hpos_before;
20963 }
20964
20965 row->truncated_on_right_p = true;
20966 it->continuation_lines_width = 0;
20967 reseat_at_next_visible_line_start (it, false);
20968 /* We insist below that IT's position be at ZV because in
20969 bidi-reordered lines the character at visible line start
20970 might not be the character that follows the newline in
20971 the logical order. */
20972 if (IT_BYTEPOS (*it) > BEG_BYTE)
20973 row->ends_at_zv_p =
20974 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20975 else
20976 row->ends_at_zv_p = false;
20977 break;
20978 }
20979 }
20980
20981 if (wrap_data)
20982 bidi_unshelve_cache (wrap_data, true);
20983
20984 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20985 at the left window margin. */
20986 if (it->first_visible_x
20987 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20988 {
20989 if (!FRAME_WINDOW_P (it->f)
20990 || (((row->reversed_p
20991 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20992 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20993 /* Don't let insert_left_trunc_glyphs overwrite the
20994 first glyph of the row if it is an image. */
20995 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20996 insert_left_trunc_glyphs (it);
20997 row->truncated_on_left_p = true;
20998 }
20999
21000 /* Remember the position at which this line ends.
21001
21002 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21003 cannot be before the call to find_row_edges below, since that is
21004 where these positions are determined. */
21005 row->end = it->current;
21006 if (!it->bidi_p)
21007 {
21008 row->minpos = row->start.pos;
21009 row->maxpos = row->end.pos;
21010 }
21011 else
21012 {
21013 /* ROW->minpos and ROW->maxpos must be the smallest and
21014 `1 + the largest' buffer positions in ROW. But if ROW was
21015 bidi-reordered, these two positions can be anywhere in the
21016 row, so we must determine them now. */
21017 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21018 }
21019
21020 /* If the start of this line is the overlay arrow-position, then
21021 mark this glyph row as the one containing the overlay arrow.
21022 This is clearly a mess with variable size fonts. It would be
21023 better to let it be displayed like cursors under X. */
21024 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21025 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21026 !NILP (overlay_arrow_string)))
21027 {
21028 /* Overlay arrow in window redisplay is a fringe bitmap. */
21029 if (STRINGP (overlay_arrow_string))
21030 {
21031 struct glyph_row *arrow_row
21032 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21033 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21034 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21035 struct glyph *p = row->glyphs[TEXT_AREA];
21036 struct glyph *p2, *end;
21037
21038 /* Copy the arrow glyphs. */
21039 while (glyph < arrow_end)
21040 *p++ = *glyph++;
21041
21042 /* Throw away padding glyphs. */
21043 p2 = p;
21044 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21045 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21046 ++p2;
21047 if (p2 > p)
21048 {
21049 while (p2 < end)
21050 *p++ = *p2++;
21051 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21052 }
21053 }
21054 else
21055 {
21056 eassert (INTEGERP (overlay_arrow_string));
21057 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21058 }
21059 overlay_arrow_seen = true;
21060 }
21061
21062 /* Highlight trailing whitespace. */
21063 if (!NILP (Vshow_trailing_whitespace))
21064 highlight_trailing_whitespace (it->f, it->glyph_row);
21065
21066 /* Compute pixel dimensions of this line. */
21067 compute_line_metrics (it);
21068
21069 /* Implementation note: No changes in the glyphs of ROW or in their
21070 faces can be done past this point, because compute_line_metrics
21071 computes ROW's hash value and stores it within the glyph_row
21072 structure. */
21073
21074 /* Record whether this row ends inside an ellipsis. */
21075 row->ends_in_ellipsis_p
21076 = (it->method == GET_FROM_DISPLAY_VECTOR
21077 && it->ellipsis_p);
21078
21079 /* Save fringe bitmaps in this row. */
21080 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21081 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21082 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21083 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21084
21085 it->left_user_fringe_bitmap = 0;
21086 it->left_user_fringe_face_id = 0;
21087 it->right_user_fringe_bitmap = 0;
21088 it->right_user_fringe_face_id = 0;
21089
21090 /* Maybe set the cursor. */
21091 cvpos = it->w->cursor.vpos;
21092 if ((cvpos < 0
21093 /* In bidi-reordered rows, keep checking for proper cursor
21094 position even if one has been found already, because buffer
21095 positions in such rows change non-linearly with ROW->VPOS,
21096 when a line is continued. One exception: when we are at ZV,
21097 display cursor on the first suitable glyph row, since all
21098 the empty rows after that also have their position set to ZV. */
21099 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21100 lines' rows is implemented for bidi-reordered rows. */
21101 || (it->bidi_p
21102 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21103 && PT >= MATRIX_ROW_START_CHARPOS (row)
21104 && PT <= MATRIX_ROW_END_CHARPOS (row)
21105 && cursor_row_p (row))
21106 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21107
21108 /* Prepare for the next line. This line starts horizontally at (X
21109 HPOS) = (0 0). Vertical positions are incremented. As a
21110 convenience for the caller, IT->glyph_row is set to the next
21111 row to be used. */
21112 it->current_x = it->hpos = 0;
21113 it->current_y += row->height;
21114 SET_TEXT_POS (it->eol_pos, 0, 0);
21115 ++it->vpos;
21116 ++it->glyph_row;
21117 /* The next row should by default use the same value of the
21118 reversed_p flag as this one. set_iterator_to_next decides when
21119 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21120 the flag accordingly. */
21121 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21122 it->glyph_row->reversed_p = row->reversed_p;
21123 it->start = row->end;
21124 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21125
21126 #undef RECORD_MAX_MIN_POS
21127 }
21128
21129 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21130 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21131 doc: /* Return paragraph direction at point in BUFFER.
21132 Value is either `left-to-right' or `right-to-left'.
21133 If BUFFER is omitted or nil, it defaults to the current buffer.
21134
21135 Paragraph direction determines how the text in the paragraph is displayed.
21136 In left-to-right paragraphs, text begins at the left margin of the window
21137 and the reading direction is generally left to right. In right-to-left
21138 paragraphs, text begins at the right margin and is read from right to left.
21139
21140 See also `bidi-paragraph-direction'. */)
21141 (Lisp_Object buffer)
21142 {
21143 struct buffer *buf = current_buffer;
21144 struct buffer *old = buf;
21145
21146 if (! NILP (buffer))
21147 {
21148 CHECK_BUFFER (buffer);
21149 buf = XBUFFER (buffer);
21150 }
21151
21152 if (NILP (BVAR (buf, bidi_display_reordering))
21153 || NILP (BVAR (buf, enable_multibyte_characters))
21154 /* When we are loading loadup.el, the character property tables
21155 needed for bidi iteration are not yet available. */
21156 || !NILP (Vpurify_flag))
21157 return Qleft_to_right;
21158 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21159 return BVAR (buf, bidi_paragraph_direction);
21160 else
21161 {
21162 /* Determine the direction from buffer text. We could try to
21163 use current_matrix if it is up to date, but this seems fast
21164 enough as it is. */
21165 struct bidi_it itb;
21166 ptrdiff_t pos = BUF_PT (buf);
21167 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21168 int c;
21169 void *itb_data = bidi_shelve_cache ();
21170
21171 set_buffer_temp (buf);
21172 /* bidi_paragraph_init finds the base direction of the paragraph
21173 by searching forward from paragraph start. We need the base
21174 direction of the current or _previous_ paragraph, so we need
21175 to make sure we are within that paragraph. To that end, find
21176 the previous non-empty line. */
21177 if (pos >= ZV && pos > BEGV)
21178 DEC_BOTH (pos, bytepos);
21179 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21180 if (fast_looking_at (trailing_white_space,
21181 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21182 {
21183 while ((c = FETCH_BYTE (bytepos)) == '\n'
21184 || c == ' ' || c == '\t' || c == '\f')
21185 {
21186 if (bytepos <= BEGV_BYTE)
21187 break;
21188 bytepos--;
21189 pos--;
21190 }
21191 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21192 bytepos--;
21193 }
21194 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21195 itb.paragraph_dir = NEUTRAL_DIR;
21196 itb.string.s = NULL;
21197 itb.string.lstring = Qnil;
21198 itb.string.bufpos = 0;
21199 itb.string.from_disp_str = false;
21200 itb.string.unibyte = false;
21201 /* We have no window to use here for ignoring window-specific
21202 overlays. Using NULL for window pointer will cause
21203 compute_display_string_pos to use the current buffer. */
21204 itb.w = NULL;
21205 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21206 bidi_unshelve_cache (itb_data, false);
21207 set_buffer_temp (old);
21208 switch (itb.paragraph_dir)
21209 {
21210 case L2R:
21211 return Qleft_to_right;
21212 break;
21213 case R2L:
21214 return Qright_to_left;
21215 break;
21216 default:
21217 emacs_abort ();
21218 }
21219 }
21220 }
21221
21222 DEFUN ("bidi-find-overridden-directionality",
21223 Fbidi_find_overridden_directionality,
21224 Sbidi_find_overridden_directionality, 2, 3, 0,
21225 doc: /* Return position between FROM and TO where directionality was overridden.
21226
21227 This function returns the first character position in the specified
21228 region of OBJECT where there is a character whose `bidi-class' property
21229 is `L', but which was forced to display as `R' by a directional
21230 override, and likewise with characters whose `bidi-class' is `R'
21231 or `AL' that were forced to display as `L'.
21232
21233 If no such character is found, the function returns nil.
21234
21235 OBJECT is a Lisp string or buffer to search for overridden
21236 directionality, and defaults to the current buffer if nil or omitted.
21237 OBJECT can also be a window, in which case the function will search
21238 the buffer displayed in that window. Passing the window instead of
21239 a buffer is preferable when the buffer is displayed in some window,
21240 because this function will then be able to correctly account for
21241 window-specific overlays, which can affect the results.
21242
21243 Strong directional characters `L', `R', and `AL' can have their
21244 intrinsic directionality overridden by directional override
21245 control characters RLO (u+202e) and LRO (u+202d). See the
21246 function `get-char-code-property' for a way to inquire about
21247 the `bidi-class' property of a character. */)
21248 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21249 {
21250 struct buffer *buf = current_buffer;
21251 struct buffer *old = buf;
21252 struct window *w = NULL;
21253 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21254 struct bidi_it itb;
21255 ptrdiff_t from_pos, to_pos, from_bpos;
21256 void *itb_data;
21257
21258 if (!NILP (object))
21259 {
21260 if (BUFFERP (object))
21261 buf = XBUFFER (object);
21262 else if (WINDOWP (object))
21263 {
21264 w = decode_live_window (object);
21265 buf = XBUFFER (w->contents);
21266 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21267 }
21268 else
21269 CHECK_STRING (object);
21270 }
21271
21272 if (STRINGP (object))
21273 {
21274 /* Characters in unibyte strings are always treated by bidi.c as
21275 strong LTR. */
21276 if (!STRING_MULTIBYTE (object)
21277 /* When we are loading loadup.el, the character property
21278 tables needed for bidi iteration are not yet
21279 available. */
21280 || !NILP (Vpurify_flag))
21281 return Qnil;
21282
21283 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21284 if (from_pos >= SCHARS (object))
21285 return Qnil;
21286
21287 /* Set up the bidi iterator. */
21288 itb_data = bidi_shelve_cache ();
21289 itb.paragraph_dir = NEUTRAL_DIR;
21290 itb.string.lstring = object;
21291 itb.string.s = NULL;
21292 itb.string.schars = SCHARS (object);
21293 itb.string.bufpos = 0;
21294 itb.string.from_disp_str = false;
21295 itb.string.unibyte = false;
21296 itb.w = w;
21297 bidi_init_it (0, 0, frame_window_p, &itb);
21298 }
21299 else
21300 {
21301 /* Nothing this fancy can happen in unibyte buffers, or in a
21302 buffer that disabled reordering, or if FROM is at EOB. */
21303 if (NILP (BVAR (buf, bidi_display_reordering))
21304 || NILP (BVAR (buf, enable_multibyte_characters))
21305 /* When we are loading loadup.el, the character property
21306 tables needed for bidi iteration are not yet
21307 available. */
21308 || !NILP (Vpurify_flag))
21309 return Qnil;
21310
21311 set_buffer_temp (buf);
21312 validate_region (&from, &to);
21313 from_pos = XINT (from);
21314 to_pos = XINT (to);
21315 if (from_pos >= ZV)
21316 return Qnil;
21317
21318 /* Set up the bidi iterator. */
21319 itb_data = bidi_shelve_cache ();
21320 from_bpos = CHAR_TO_BYTE (from_pos);
21321 if (from_pos == BEGV)
21322 {
21323 itb.charpos = BEGV;
21324 itb.bytepos = BEGV_BYTE;
21325 }
21326 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21327 {
21328 itb.charpos = from_pos;
21329 itb.bytepos = from_bpos;
21330 }
21331 else
21332 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21333 -1, &itb.bytepos);
21334 itb.paragraph_dir = NEUTRAL_DIR;
21335 itb.string.s = NULL;
21336 itb.string.lstring = Qnil;
21337 itb.string.bufpos = 0;
21338 itb.string.from_disp_str = false;
21339 itb.string.unibyte = false;
21340 itb.w = w;
21341 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21342 }
21343
21344 ptrdiff_t found;
21345 do {
21346 /* For the purposes of this function, the actual base direction of
21347 the paragraph doesn't matter, so just set it to L2R. */
21348 bidi_paragraph_init (L2R, &itb, false);
21349 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21350 ;
21351 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21352
21353 bidi_unshelve_cache (itb_data, false);
21354 set_buffer_temp (old);
21355
21356 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21357 }
21358
21359 DEFUN ("move-point-visually", Fmove_point_visually,
21360 Smove_point_visually, 1, 1, 0,
21361 doc: /* Move point in the visual order in the specified DIRECTION.
21362 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21363 left.
21364
21365 Value is the new character position of point. */)
21366 (Lisp_Object direction)
21367 {
21368 struct window *w = XWINDOW (selected_window);
21369 struct buffer *b = XBUFFER (w->contents);
21370 struct glyph_row *row;
21371 int dir;
21372 Lisp_Object paragraph_dir;
21373
21374 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21375 (!(ROW)->continued_p \
21376 && NILP ((GLYPH)->object) \
21377 && (GLYPH)->type == CHAR_GLYPH \
21378 && (GLYPH)->u.ch == ' ' \
21379 && (GLYPH)->charpos >= 0 \
21380 && !(GLYPH)->avoid_cursor_p)
21381
21382 CHECK_NUMBER (direction);
21383 dir = XINT (direction);
21384 if (dir > 0)
21385 dir = 1;
21386 else
21387 dir = -1;
21388
21389 /* If current matrix is up-to-date, we can use the information
21390 recorded in the glyphs, at least as long as the goal is on the
21391 screen. */
21392 if (w->window_end_valid
21393 && !windows_or_buffers_changed
21394 && b
21395 && !b->clip_changed
21396 && !b->prevent_redisplay_optimizations_p
21397 && !window_outdated (w)
21398 /* We rely below on the cursor coordinates to be up to date, but
21399 we cannot trust them if some command moved point since the
21400 last complete redisplay. */
21401 && w->last_point == BUF_PT (b)
21402 && w->cursor.vpos >= 0
21403 && w->cursor.vpos < w->current_matrix->nrows
21404 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21405 {
21406 struct glyph *g = row->glyphs[TEXT_AREA];
21407 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21408 struct glyph *gpt = g + w->cursor.hpos;
21409
21410 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21411 {
21412 if (BUFFERP (g->object) && g->charpos != PT)
21413 {
21414 SET_PT (g->charpos);
21415 w->cursor.vpos = -1;
21416 return make_number (PT);
21417 }
21418 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21419 {
21420 ptrdiff_t new_pos;
21421
21422 if (BUFFERP (gpt->object))
21423 {
21424 new_pos = PT;
21425 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21426 new_pos += (row->reversed_p ? -dir : dir);
21427 else
21428 new_pos -= (row->reversed_p ? -dir : dir);
21429 }
21430 else if (BUFFERP (g->object))
21431 new_pos = g->charpos;
21432 else
21433 break;
21434 SET_PT (new_pos);
21435 w->cursor.vpos = -1;
21436 return make_number (PT);
21437 }
21438 else if (ROW_GLYPH_NEWLINE_P (row, g))
21439 {
21440 /* Glyphs inserted at the end of a non-empty line for
21441 positioning the cursor have zero charpos, so we must
21442 deduce the value of point by other means. */
21443 if (g->charpos > 0)
21444 SET_PT (g->charpos);
21445 else if (row->ends_at_zv_p && PT != ZV)
21446 SET_PT (ZV);
21447 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21448 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21449 else
21450 break;
21451 w->cursor.vpos = -1;
21452 return make_number (PT);
21453 }
21454 }
21455 if (g == e || NILP (g->object))
21456 {
21457 if (row->truncated_on_left_p || row->truncated_on_right_p)
21458 goto simulate_display;
21459 if (!row->reversed_p)
21460 row += dir;
21461 else
21462 row -= dir;
21463 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21464 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21465 goto simulate_display;
21466
21467 if (dir > 0)
21468 {
21469 if (row->reversed_p && !row->continued_p)
21470 {
21471 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21472 w->cursor.vpos = -1;
21473 return make_number (PT);
21474 }
21475 g = row->glyphs[TEXT_AREA];
21476 e = g + row->used[TEXT_AREA];
21477 for ( ; g < e; g++)
21478 {
21479 if (BUFFERP (g->object)
21480 /* Empty lines have only one glyph, which stands
21481 for the newline, and whose charpos is the
21482 buffer position of the newline. */
21483 || ROW_GLYPH_NEWLINE_P (row, g)
21484 /* When the buffer ends in a newline, the line at
21485 EOB also has one glyph, but its charpos is -1. */
21486 || (row->ends_at_zv_p
21487 && !row->reversed_p
21488 && NILP (g->object)
21489 && g->type == CHAR_GLYPH
21490 && g->u.ch == ' '))
21491 {
21492 if (g->charpos > 0)
21493 SET_PT (g->charpos);
21494 else if (!row->reversed_p
21495 && row->ends_at_zv_p
21496 && PT != ZV)
21497 SET_PT (ZV);
21498 else
21499 continue;
21500 w->cursor.vpos = -1;
21501 return make_number (PT);
21502 }
21503 }
21504 }
21505 else
21506 {
21507 if (!row->reversed_p && !row->continued_p)
21508 {
21509 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21510 w->cursor.vpos = -1;
21511 return make_number (PT);
21512 }
21513 e = row->glyphs[TEXT_AREA];
21514 g = e + row->used[TEXT_AREA] - 1;
21515 for ( ; g >= e; g--)
21516 {
21517 if (BUFFERP (g->object)
21518 || (ROW_GLYPH_NEWLINE_P (row, g)
21519 && g->charpos > 0)
21520 /* Empty R2L lines on GUI frames have the buffer
21521 position of the newline stored in the stretch
21522 glyph. */
21523 || g->type == STRETCH_GLYPH
21524 || (row->ends_at_zv_p
21525 && row->reversed_p
21526 && NILP (g->object)
21527 && g->type == CHAR_GLYPH
21528 && g->u.ch == ' '))
21529 {
21530 if (g->charpos > 0)
21531 SET_PT (g->charpos);
21532 else if (row->reversed_p
21533 && row->ends_at_zv_p
21534 && PT != ZV)
21535 SET_PT (ZV);
21536 else
21537 continue;
21538 w->cursor.vpos = -1;
21539 return make_number (PT);
21540 }
21541 }
21542 }
21543 }
21544 }
21545
21546 simulate_display:
21547
21548 /* If we wind up here, we failed to move by using the glyphs, so we
21549 need to simulate display instead. */
21550
21551 if (b)
21552 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21553 else
21554 paragraph_dir = Qleft_to_right;
21555 if (EQ (paragraph_dir, Qright_to_left))
21556 dir = -dir;
21557 if (PT <= BEGV && dir < 0)
21558 xsignal0 (Qbeginning_of_buffer);
21559 else if (PT >= ZV && dir > 0)
21560 xsignal0 (Qend_of_buffer);
21561 else
21562 {
21563 struct text_pos pt;
21564 struct it it;
21565 int pt_x, target_x, pixel_width, pt_vpos;
21566 bool at_eol_p;
21567 bool overshoot_expected = false;
21568 bool target_is_eol_p = false;
21569
21570 /* Setup the arena. */
21571 SET_TEXT_POS (pt, PT, PT_BYTE);
21572 start_display (&it, w, pt);
21573 /* When lines are truncated, we could be called with point
21574 outside of the windows edges, in which case move_it_*
21575 functions either prematurely stop at window's edge or jump to
21576 the next screen line, whereas we rely below on our ability to
21577 reach point, in order to start from its X coordinate. So we
21578 need to disregard the window's horizontal extent in that case. */
21579 if (it.line_wrap == TRUNCATE)
21580 it.last_visible_x = INFINITY;
21581
21582 if (it.cmp_it.id < 0
21583 && it.method == GET_FROM_STRING
21584 && it.area == TEXT_AREA
21585 && it.string_from_display_prop_p
21586 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21587 overshoot_expected = true;
21588
21589 /* Find the X coordinate of point. We start from the beginning
21590 of this or previous line to make sure we are before point in
21591 the logical order (since the move_it_* functions can only
21592 move forward). */
21593 reseat:
21594 reseat_at_previous_visible_line_start (&it);
21595 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21596 if (IT_CHARPOS (it) != PT)
21597 {
21598 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21599 -1, -1, -1, MOVE_TO_POS);
21600 /* If we missed point because the character there is
21601 displayed out of a display vector that has more than one
21602 glyph, retry expecting overshoot. */
21603 if (it.method == GET_FROM_DISPLAY_VECTOR
21604 && it.current.dpvec_index > 0
21605 && !overshoot_expected)
21606 {
21607 overshoot_expected = true;
21608 goto reseat;
21609 }
21610 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21611 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21612 }
21613 pt_x = it.current_x;
21614 pt_vpos = it.vpos;
21615 if (dir > 0 || overshoot_expected)
21616 {
21617 struct glyph_row *row = it.glyph_row;
21618
21619 /* When point is at beginning of line, we don't have
21620 information about the glyph there loaded into struct
21621 it. Calling get_next_display_element fixes that. */
21622 if (pt_x == 0)
21623 get_next_display_element (&it);
21624 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21625 it.glyph_row = NULL;
21626 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21627 it.glyph_row = row;
21628 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21629 it, lest it will become out of sync with it's buffer
21630 position. */
21631 it.current_x = pt_x;
21632 }
21633 else
21634 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21635 pixel_width = it.pixel_width;
21636 if (overshoot_expected && at_eol_p)
21637 pixel_width = 0;
21638 else if (pixel_width <= 0)
21639 pixel_width = 1;
21640
21641 /* If there's a display string (or something similar) at point,
21642 we are actually at the glyph to the left of point, so we need
21643 to correct the X coordinate. */
21644 if (overshoot_expected)
21645 {
21646 if (it.bidi_p)
21647 pt_x += pixel_width * it.bidi_it.scan_dir;
21648 else
21649 pt_x += pixel_width;
21650 }
21651
21652 /* Compute target X coordinate, either to the left or to the
21653 right of point. On TTY frames, all characters have the same
21654 pixel width of 1, so we can use that. On GUI frames we don't
21655 have an easy way of getting at the pixel width of the
21656 character to the left of point, so we use a different method
21657 of getting to that place. */
21658 if (dir > 0)
21659 target_x = pt_x + pixel_width;
21660 else
21661 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21662
21663 /* Target X coordinate could be one line above or below the line
21664 of point, in which case we need to adjust the target X
21665 coordinate. Also, if moving to the left, we need to begin at
21666 the left edge of the point's screen line. */
21667 if (dir < 0)
21668 {
21669 if (pt_x > 0)
21670 {
21671 start_display (&it, w, pt);
21672 if (it.line_wrap == TRUNCATE)
21673 it.last_visible_x = INFINITY;
21674 reseat_at_previous_visible_line_start (&it);
21675 it.current_x = it.current_y = it.hpos = 0;
21676 if (pt_vpos != 0)
21677 move_it_by_lines (&it, pt_vpos);
21678 }
21679 else
21680 {
21681 move_it_by_lines (&it, -1);
21682 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21683 target_is_eol_p = true;
21684 /* Under word-wrap, we don't know the x coordinate of
21685 the last character displayed on the previous line,
21686 which immediately precedes the wrap point. To find
21687 out its x coordinate, we try moving to the right
21688 margin of the window, which will stop at the wrap
21689 point, and then reset target_x to point at the
21690 character that precedes the wrap point. This is not
21691 needed on GUI frames, because (see below) there we
21692 move from the left margin one grapheme cluster at a
21693 time, and stop when we hit the wrap point. */
21694 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21695 {
21696 void *it_data = NULL;
21697 struct it it2;
21698
21699 SAVE_IT (it2, it, it_data);
21700 move_it_in_display_line_to (&it, ZV, target_x,
21701 MOVE_TO_POS | MOVE_TO_X);
21702 /* If we arrived at target_x, that _is_ the last
21703 character on the previous line. */
21704 if (it.current_x != target_x)
21705 target_x = it.current_x - 1;
21706 RESTORE_IT (&it, &it2, it_data);
21707 }
21708 }
21709 }
21710 else
21711 {
21712 if (at_eol_p
21713 || (target_x >= it.last_visible_x
21714 && it.line_wrap != TRUNCATE))
21715 {
21716 if (pt_x > 0)
21717 move_it_by_lines (&it, 0);
21718 move_it_by_lines (&it, 1);
21719 target_x = 0;
21720 }
21721 }
21722
21723 /* Move to the target X coordinate. */
21724 #ifdef HAVE_WINDOW_SYSTEM
21725 /* On GUI frames, as we don't know the X coordinate of the
21726 character to the left of point, moving point to the left
21727 requires walking, one grapheme cluster at a time, until we
21728 find ourself at a place immediately to the left of the
21729 character at point. */
21730 if (FRAME_WINDOW_P (it.f) && dir < 0)
21731 {
21732 struct text_pos new_pos;
21733 enum move_it_result rc = MOVE_X_REACHED;
21734
21735 if (it.current_x == 0)
21736 get_next_display_element (&it);
21737 if (it.what == IT_COMPOSITION)
21738 {
21739 new_pos.charpos = it.cmp_it.charpos;
21740 new_pos.bytepos = -1;
21741 }
21742 else
21743 new_pos = it.current.pos;
21744
21745 while (it.current_x + it.pixel_width <= target_x
21746 && (rc == MOVE_X_REACHED
21747 /* Under word-wrap, move_it_in_display_line_to
21748 stops at correct coordinates, but sometimes
21749 returns MOVE_POS_MATCH_OR_ZV. */
21750 || (it.line_wrap == WORD_WRAP
21751 && rc == MOVE_POS_MATCH_OR_ZV)))
21752 {
21753 int new_x = it.current_x + it.pixel_width;
21754
21755 /* For composed characters, we want the position of the
21756 first character in the grapheme cluster (usually, the
21757 composition's base character), whereas it.current
21758 might give us the position of the _last_ one, e.g. if
21759 the composition is rendered in reverse due to bidi
21760 reordering. */
21761 if (it.what == IT_COMPOSITION)
21762 {
21763 new_pos.charpos = it.cmp_it.charpos;
21764 new_pos.bytepos = -1;
21765 }
21766 else
21767 new_pos = it.current.pos;
21768 if (new_x == it.current_x)
21769 new_x++;
21770 rc = move_it_in_display_line_to (&it, ZV, new_x,
21771 MOVE_TO_POS | MOVE_TO_X);
21772 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21773 break;
21774 }
21775 /* The previous position we saw in the loop is the one we
21776 want. */
21777 if (new_pos.bytepos == -1)
21778 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21779 it.current.pos = new_pos;
21780 }
21781 else
21782 #endif
21783 if (it.current_x != target_x)
21784 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21785
21786 /* If we ended up in a display string that covers point, move to
21787 buffer position to the right in the visual order. */
21788 if (dir > 0)
21789 {
21790 while (IT_CHARPOS (it) == PT)
21791 {
21792 set_iterator_to_next (&it, false);
21793 if (!get_next_display_element (&it))
21794 break;
21795 }
21796 }
21797
21798 /* Move point to that position. */
21799 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21800 }
21801
21802 return make_number (PT);
21803
21804 #undef ROW_GLYPH_NEWLINE_P
21805 }
21806
21807 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21808 Sbidi_resolved_levels, 0, 1, 0,
21809 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21810
21811 The resolved levels are produced by the Emacs bidi reordering engine
21812 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21813 read the Unicode Standard Annex 9 (UAX#9) for background information
21814 about these levels.
21815
21816 VPOS is the zero-based number of the current window's screen line
21817 for which to produce the resolved levels. If VPOS is nil or omitted,
21818 it defaults to the screen line of point. If the window displays a
21819 header line, VPOS of zero will report on the header line, and first
21820 line of text in the window will have VPOS of 1.
21821
21822 Value is an array of resolved levels, indexed by glyph number.
21823 Glyphs are numbered from zero starting from the beginning of the
21824 screen line, i.e. the left edge of the window for left-to-right lines
21825 and from the right edge for right-to-left lines. The resolved levels
21826 are produced only for the window's text area; text in display margins
21827 is not included.
21828
21829 If the selected window's display is not up-to-date, or if the specified
21830 screen line does not display text, this function returns nil. It is
21831 highly recommended to bind this function to some simple key, like F8,
21832 in order to avoid these problems.
21833
21834 This function exists mainly for testing the correctness of the
21835 Emacs UBA implementation, in particular with the test suite. */)
21836 (Lisp_Object vpos)
21837 {
21838 struct window *w = XWINDOW (selected_window);
21839 struct buffer *b = XBUFFER (w->contents);
21840 int nrow;
21841 struct glyph_row *row;
21842
21843 if (NILP (vpos))
21844 {
21845 int d1, d2, d3, d4, d5;
21846
21847 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21848 }
21849 else
21850 {
21851 CHECK_NUMBER_COERCE_MARKER (vpos);
21852 nrow = XINT (vpos);
21853 }
21854
21855 /* We require up-to-date glyph matrix for this window. */
21856 if (w->window_end_valid
21857 && !windows_or_buffers_changed
21858 && b
21859 && !b->clip_changed
21860 && !b->prevent_redisplay_optimizations_p
21861 && !window_outdated (w)
21862 && nrow >= 0
21863 && nrow < w->current_matrix->nrows
21864 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21865 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21866 {
21867 struct glyph *g, *e, *g1;
21868 int nglyphs, i;
21869 Lisp_Object levels;
21870
21871 if (!row->reversed_p) /* Left-to-right glyph row. */
21872 {
21873 g = g1 = row->glyphs[TEXT_AREA];
21874 e = g + row->used[TEXT_AREA];
21875
21876 /* Skip over glyphs at the start of the row that was
21877 generated by redisplay for its own needs. */
21878 while (g < e
21879 && NILP (g->object)
21880 && g->charpos < 0)
21881 g++;
21882 g1 = g;
21883
21884 /* Count the "interesting" glyphs in this row. */
21885 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21886 nglyphs++;
21887
21888 /* Create and fill the array. */
21889 levels = make_uninit_vector (nglyphs);
21890 for (i = 0; g1 < g; i++, g1++)
21891 ASET (levels, i, make_number (g1->resolved_level));
21892 }
21893 else /* Right-to-left glyph row. */
21894 {
21895 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21896 e = row->glyphs[TEXT_AREA] - 1;
21897 while (g > e
21898 && NILP (g->object)
21899 && g->charpos < 0)
21900 g--;
21901 g1 = g;
21902 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21903 nglyphs++;
21904 levels = make_uninit_vector (nglyphs);
21905 for (i = 0; g1 > g; i++, g1--)
21906 ASET (levels, i, make_number (g1->resolved_level));
21907 }
21908 return levels;
21909 }
21910 else
21911 return Qnil;
21912 }
21913
21914
21915 \f
21916 /***********************************************************************
21917 Menu Bar
21918 ***********************************************************************/
21919
21920 /* Redisplay the menu bar in the frame for window W.
21921
21922 The menu bar of X frames that don't have X toolkit support is
21923 displayed in a special window W->frame->menu_bar_window.
21924
21925 The menu bar of terminal frames is treated specially as far as
21926 glyph matrices are concerned. Menu bar lines are not part of
21927 windows, so the update is done directly on the frame matrix rows
21928 for the menu bar. */
21929
21930 static void
21931 display_menu_bar (struct window *w)
21932 {
21933 struct frame *f = XFRAME (WINDOW_FRAME (w));
21934 struct it it;
21935 Lisp_Object items;
21936 int i;
21937
21938 /* Don't do all this for graphical frames. */
21939 #ifdef HAVE_NTGUI
21940 if (FRAME_W32_P (f))
21941 return;
21942 #endif
21943 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21944 if (FRAME_X_P (f))
21945 return;
21946 #endif
21947
21948 #ifdef HAVE_NS
21949 if (FRAME_NS_P (f))
21950 return;
21951 #endif /* HAVE_NS */
21952
21953 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21954 eassert (!FRAME_WINDOW_P (f));
21955 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21956 it.first_visible_x = 0;
21957 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21958 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21959 if (FRAME_WINDOW_P (f))
21960 {
21961 /* Menu bar lines are displayed in the desired matrix of the
21962 dummy window menu_bar_window. */
21963 struct window *menu_w;
21964 menu_w = XWINDOW (f->menu_bar_window);
21965 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21966 MENU_FACE_ID);
21967 it.first_visible_x = 0;
21968 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21969 }
21970 else
21971 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21972 {
21973 /* This is a TTY frame, i.e. character hpos/vpos are used as
21974 pixel x/y. */
21975 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21976 MENU_FACE_ID);
21977 it.first_visible_x = 0;
21978 it.last_visible_x = FRAME_COLS (f);
21979 }
21980
21981 /* FIXME: This should be controlled by a user option. See the
21982 comments in redisplay_tool_bar and display_mode_line about
21983 this. */
21984 it.paragraph_embedding = L2R;
21985
21986 /* Clear all rows of the menu bar. */
21987 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21988 {
21989 struct glyph_row *row = it.glyph_row + i;
21990 clear_glyph_row (row);
21991 row->enabled_p = true;
21992 row->full_width_p = true;
21993 row->reversed_p = false;
21994 }
21995
21996 /* Display all items of the menu bar. */
21997 items = FRAME_MENU_BAR_ITEMS (it.f);
21998 for (i = 0; i < ASIZE (items); i += 4)
21999 {
22000 Lisp_Object string;
22001
22002 /* Stop at nil string. */
22003 string = AREF (items, i + 1);
22004 if (NILP (string))
22005 break;
22006
22007 /* Remember where item was displayed. */
22008 ASET (items, i + 3, make_number (it.hpos));
22009
22010 /* Display the item, pad with one space. */
22011 if (it.current_x < it.last_visible_x)
22012 display_string (NULL, string, Qnil, 0, 0, &it,
22013 SCHARS (string) + 1, 0, 0, -1);
22014 }
22015
22016 /* Fill out the line with spaces. */
22017 if (it.current_x < it.last_visible_x)
22018 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22019
22020 /* Compute the total height of the lines. */
22021 compute_line_metrics (&it);
22022 }
22023
22024 /* Deep copy of a glyph row, including the glyphs. */
22025 static void
22026 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22027 {
22028 struct glyph *pointers[1 + LAST_AREA];
22029 int to_used = to->used[TEXT_AREA];
22030
22031 /* Save glyph pointers of TO. */
22032 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22033
22034 /* Do a structure assignment. */
22035 *to = *from;
22036
22037 /* Restore original glyph pointers of TO. */
22038 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22039
22040 /* Copy the glyphs. */
22041 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22042 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22043
22044 /* If we filled only part of the TO row, fill the rest with
22045 space_glyph (which will display as empty space). */
22046 if (to_used > from->used[TEXT_AREA])
22047 fill_up_frame_row_with_spaces (to, to_used);
22048 }
22049
22050 /* Display one menu item on a TTY, by overwriting the glyphs in the
22051 frame F's desired glyph matrix with glyphs produced from the menu
22052 item text. Called from term.c to display TTY drop-down menus one
22053 item at a time.
22054
22055 ITEM_TEXT is the menu item text as a C string.
22056
22057 FACE_ID is the face ID to be used for this menu item. FACE_ID
22058 could specify one of 3 faces: a face for an enabled item, a face
22059 for a disabled item, or a face for a selected item.
22060
22061 X and Y are coordinates of the first glyph in the frame's desired
22062 matrix to be overwritten by the menu item. Since this is a TTY, Y
22063 is the zero-based number of the glyph row and X is the zero-based
22064 glyph number in the row, starting from left, where to start
22065 displaying the item.
22066
22067 SUBMENU means this menu item drops down a submenu, which
22068 should be indicated by displaying a proper visual cue after the
22069 item text. */
22070
22071 void
22072 display_tty_menu_item (const char *item_text, int width, int face_id,
22073 int x, int y, bool submenu)
22074 {
22075 struct it it;
22076 struct frame *f = SELECTED_FRAME ();
22077 struct window *w = XWINDOW (f->selected_window);
22078 struct glyph_row *row;
22079 size_t item_len = strlen (item_text);
22080
22081 eassert (FRAME_TERMCAP_P (f));
22082
22083 /* Don't write beyond the matrix's last row. This can happen for
22084 TTY screens that are not high enough to show the entire menu.
22085 (This is actually a bit of defensive programming, as
22086 tty_menu_display already limits the number of menu items to one
22087 less than the number of screen lines.) */
22088 if (y >= f->desired_matrix->nrows)
22089 return;
22090
22091 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
22092 it.first_visible_x = 0;
22093 it.last_visible_x = FRAME_COLS (f) - 1;
22094 row = it.glyph_row;
22095 /* Start with the row contents from the current matrix. */
22096 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22097 bool saved_width = row->full_width_p;
22098 row->full_width_p = true;
22099 bool saved_reversed = row->reversed_p;
22100 row->reversed_p = false;
22101 row->enabled_p = true;
22102
22103 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22104 desired face. */
22105 eassert (x < f->desired_matrix->matrix_w);
22106 it.current_x = it.hpos = x;
22107 it.current_y = it.vpos = y;
22108 int saved_used = row->used[TEXT_AREA];
22109 bool saved_truncated = row->truncated_on_right_p;
22110 row->used[TEXT_AREA] = x;
22111 it.face_id = face_id;
22112 it.line_wrap = TRUNCATE;
22113
22114 /* FIXME: This should be controlled by a user option. See the
22115 comments in redisplay_tool_bar and display_mode_line about this.
22116 Also, if paragraph_embedding could ever be R2L, changes will be
22117 needed to avoid shifting to the right the row characters in
22118 term.c:append_glyph. */
22119 it.paragraph_embedding = L2R;
22120
22121 /* Pad with a space on the left. */
22122 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22123 width--;
22124 /* Display the menu item, pad with spaces to WIDTH. */
22125 if (submenu)
22126 {
22127 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22128 item_len, 0, FRAME_COLS (f) - 1, -1);
22129 width -= item_len;
22130 /* Indicate with " >" that there's a submenu. */
22131 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22132 FRAME_COLS (f) - 1, -1);
22133 }
22134 else
22135 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22136 width, 0, FRAME_COLS (f) - 1, -1);
22137
22138 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22139 row->truncated_on_right_p = saved_truncated;
22140 row->hash = row_hash (row);
22141 row->full_width_p = saved_width;
22142 row->reversed_p = saved_reversed;
22143 }
22144 \f
22145 /***********************************************************************
22146 Mode Line
22147 ***********************************************************************/
22148
22149 /* Redisplay mode lines in the window tree whose root is WINDOW.
22150 If FORCE, redisplay mode lines unconditionally.
22151 Otherwise, redisplay only mode lines that are garbaged. Value is
22152 the number of windows whose mode lines were redisplayed. */
22153
22154 static int
22155 redisplay_mode_lines (Lisp_Object window, bool force)
22156 {
22157 int nwindows = 0;
22158
22159 while (!NILP (window))
22160 {
22161 struct window *w = XWINDOW (window);
22162
22163 if (WINDOWP (w->contents))
22164 nwindows += redisplay_mode_lines (w->contents, force);
22165 else if (force
22166 || FRAME_GARBAGED_P (XFRAME (w->frame))
22167 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22168 {
22169 struct text_pos lpoint;
22170 struct buffer *old = current_buffer;
22171
22172 /* Set the window's buffer for the mode line display. */
22173 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22174 set_buffer_internal_1 (XBUFFER (w->contents));
22175
22176 /* Point refers normally to the selected window. For any
22177 other window, set up appropriate value. */
22178 if (!EQ (window, selected_window))
22179 {
22180 struct text_pos pt;
22181
22182 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22183 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22184 }
22185
22186 /* Display mode lines. */
22187 clear_glyph_matrix (w->desired_matrix);
22188 if (display_mode_lines (w))
22189 ++nwindows;
22190
22191 /* Restore old settings. */
22192 set_buffer_internal_1 (old);
22193 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22194 }
22195
22196 window = w->next;
22197 }
22198
22199 return nwindows;
22200 }
22201
22202
22203 /* Display the mode and/or header line of window W. Value is the
22204 sum number of mode lines and header lines displayed. */
22205
22206 static int
22207 display_mode_lines (struct window *w)
22208 {
22209 Lisp_Object old_selected_window = selected_window;
22210 Lisp_Object old_selected_frame = selected_frame;
22211 Lisp_Object new_frame = w->frame;
22212 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22213 int n = 0;
22214
22215 selected_frame = new_frame;
22216 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22217 or window's point, then we'd need select_window_1 here as well. */
22218 XSETWINDOW (selected_window, w);
22219 XFRAME (new_frame)->selected_window = selected_window;
22220
22221 /* These will be set while the mode line specs are processed. */
22222 line_number_displayed = false;
22223 w->column_number_displayed = -1;
22224
22225 if (WINDOW_WANTS_MODELINE_P (w))
22226 {
22227 struct window *sel_w = XWINDOW (old_selected_window);
22228
22229 /* Select mode line face based on the real selected window. */
22230 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22231 BVAR (current_buffer, mode_line_format));
22232 ++n;
22233 }
22234
22235 if (WINDOW_WANTS_HEADER_LINE_P (w))
22236 {
22237 display_mode_line (w, HEADER_LINE_FACE_ID,
22238 BVAR (current_buffer, header_line_format));
22239 ++n;
22240 }
22241
22242 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22243 selected_frame = old_selected_frame;
22244 selected_window = old_selected_window;
22245 if (n > 0)
22246 w->must_be_updated_p = true;
22247 return n;
22248 }
22249
22250
22251 /* Display mode or header line of window W. FACE_ID specifies which
22252 line to display; it is either MODE_LINE_FACE_ID or
22253 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22254 display. Value is the pixel height of the mode/header line
22255 displayed. */
22256
22257 static int
22258 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22259 {
22260 struct it it;
22261 struct face *face;
22262 ptrdiff_t count = SPECPDL_INDEX ();
22263
22264 init_iterator (&it, w, -1, -1, NULL, face_id);
22265 /* Don't extend on a previously drawn mode-line.
22266 This may happen if called from pos_visible_p. */
22267 it.glyph_row->enabled_p = false;
22268 prepare_desired_row (w, it.glyph_row, true);
22269
22270 it.glyph_row->mode_line_p = true;
22271
22272 /* FIXME: This should be controlled by a user option. But
22273 supporting such an option is not trivial, since the mode line is
22274 made up of many separate strings. */
22275 it.paragraph_embedding = L2R;
22276
22277 record_unwind_protect (unwind_format_mode_line,
22278 format_mode_line_unwind_data (NULL, NULL,
22279 Qnil, false));
22280
22281 mode_line_target = MODE_LINE_DISPLAY;
22282
22283 /* Temporarily make frame's keyboard the current kboard so that
22284 kboard-local variables in the mode_line_format will get the right
22285 values. */
22286 push_kboard (FRAME_KBOARD (it.f));
22287 record_unwind_save_match_data ();
22288 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22289 pop_kboard ();
22290
22291 unbind_to (count, Qnil);
22292
22293 /* Fill up with spaces. */
22294 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22295
22296 compute_line_metrics (&it);
22297 it.glyph_row->full_width_p = true;
22298 it.glyph_row->continued_p = false;
22299 it.glyph_row->truncated_on_left_p = false;
22300 it.glyph_row->truncated_on_right_p = false;
22301
22302 /* Make a 3D mode-line have a shadow at its right end. */
22303 face = FACE_FROM_ID (it.f, face_id);
22304 extend_face_to_end_of_line (&it);
22305 if (face->box != FACE_NO_BOX)
22306 {
22307 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22308 + it.glyph_row->used[TEXT_AREA] - 1);
22309 last->right_box_line_p = true;
22310 }
22311
22312 return it.glyph_row->height;
22313 }
22314
22315 /* Move element ELT in LIST to the front of LIST.
22316 Return the updated list. */
22317
22318 static Lisp_Object
22319 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22320 {
22321 register Lisp_Object tail, prev;
22322 register Lisp_Object tem;
22323
22324 tail = list;
22325 prev = Qnil;
22326 while (CONSP (tail))
22327 {
22328 tem = XCAR (tail);
22329
22330 if (EQ (elt, tem))
22331 {
22332 /* Splice out the link TAIL. */
22333 if (NILP (prev))
22334 list = XCDR (tail);
22335 else
22336 Fsetcdr (prev, XCDR (tail));
22337
22338 /* Now make it the first. */
22339 Fsetcdr (tail, list);
22340 return tail;
22341 }
22342 else
22343 prev = tail;
22344 tail = XCDR (tail);
22345 QUIT;
22346 }
22347
22348 /* Not found--return unchanged LIST. */
22349 return list;
22350 }
22351
22352 /* Contribute ELT to the mode line for window IT->w. How it
22353 translates into text depends on its data type.
22354
22355 IT describes the display environment in which we display, as usual.
22356
22357 DEPTH is the depth in recursion. It is used to prevent
22358 infinite recursion here.
22359
22360 FIELD_WIDTH is the number of characters the display of ELT should
22361 occupy in the mode line, and PRECISION is the maximum number of
22362 characters to display from ELT's representation. See
22363 display_string for details.
22364
22365 Returns the hpos of the end of the text generated by ELT.
22366
22367 PROPS is a property list to add to any string we encounter.
22368
22369 If RISKY, remove (disregard) any properties in any string
22370 we encounter, and ignore :eval and :propertize.
22371
22372 The global variable `mode_line_target' determines whether the
22373 output is passed to `store_mode_line_noprop',
22374 `store_mode_line_string', or `display_string'. */
22375
22376 static int
22377 display_mode_element (struct it *it, int depth, int field_width, int precision,
22378 Lisp_Object elt, Lisp_Object props, bool risky)
22379 {
22380 int n = 0, field, prec;
22381 bool literal = false;
22382
22383 tail_recurse:
22384 if (depth > 100)
22385 elt = build_string ("*too-deep*");
22386
22387 depth++;
22388
22389 switch (XTYPE (elt))
22390 {
22391 case Lisp_String:
22392 {
22393 /* A string: output it and check for %-constructs within it. */
22394 unsigned char c;
22395 ptrdiff_t offset = 0;
22396
22397 if (SCHARS (elt) > 0
22398 && (!NILP (props) || risky))
22399 {
22400 Lisp_Object oprops, aelt;
22401 oprops = Ftext_properties_at (make_number (0), elt);
22402
22403 /* If the starting string's properties are not what
22404 we want, translate the string. Also, if the string
22405 is risky, do that anyway. */
22406
22407 if (NILP (Fequal (props, oprops)) || risky)
22408 {
22409 /* If the starting string has properties,
22410 merge the specified ones onto the existing ones. */
22411 if (! NILP (oprops) && !risky)
22412 {
22413 Lisp_Object tem;
22414
22415 oprops = Fcopy_sequence (oprops);
22416 tem = props;
22417 while (CONSP (tem))
22418 {
22419 oprops = Fplist_put (oprops, XCAR (tem),
22420 XCAR (XCDR (tem)));
22421 tem = XCDR (XCDR (tem));
22422 }
22423 props = oprops;
22424 }
22425
22426 aelt = Fassoc (elt, mode_line_proptrans_alist);
22427 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22428 {
22429 /* AELT is what we want. Move it to the front
22430 without consing. */
22431 elt = XCAR (aelt);
22432 mode_line_proptrans_alist
22433 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22434 }
22435 else
22436 {
22437 Lisp_Object tem;
22438
22439 /* If AELT has the wrong props, it is useless.
22440 so get rid of it. */
22441 if (! NILP (aelt))
22442 mode_line_proptrans_alist
22443 = Fdelq (aelt, mode_line_proptrans_alist);
22444
22445 elt = Fcopy_sequence (elt);
22446 Fset_text_properties (make_number (0), Flength (elt),
22447 props, elt);
22448 /* Add this item to mode_line_proptrans_alist. */
22449 mode_line_proptrans_alist
22450 = Fcons (Fcons (elt, props),
22451 mode_line_proptrans_alist);
22452 /* Truncate mode_line_proptrans_alist
22453 to at most 50 elements. */
22454 tem = Fnthcdr (make_number (50),
22455 mode_line_proptrans_alist);
22456 if (! NILP (tem))
22457 XSETCDR (tem, Qnil);
22458 }
22459 }
22460 }
22461
22462 offset = 0;
22463
22464 if (literal)
22465 {
22466 prec = precision - n;
22467 switch (mode_line_target)
22468 {
22469 case MODE_LINE_NOPROP:
22470 case MODE_LINE_TITLE:
22471 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22472 break;
22473 case MODE_LINE_STRING:
22474 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22475 break;
22476 case MODE_LINE_DISPLAY:
22477 n += display_string (NULL, elt, Qnil, 0, 0, it,
22478 0, prec, 0, STRING_MULTIBYTE (elt));
22479 break;
22480 }
22481
22482 break;
22483 }
22484
22485 /* Handle the non-literal case. */
22486
22487 while ((precision <= 0 || n < precision)
22488 && SREF (elt, offset) != 0
22489 && (mode_line_target != MODE_LINE_DISPLAY
22490 || it->current_x < it->last_visible_x))
22491 {
22492 ptrdiff_t last_offset = offset;
22493
22494 /* Advance to end of string or next format specifier. */
22495 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22496 ;
22497
22498 if (offset - 1 != last_offset)
22499 {
22500 ptrdiff_t nchars, nbytes;
22501
22502 /* Output to end of string or up to '%'. Field width
22503 is length of string. Don't output more than
22504 PRECISION allows us. */
22505 offset--;
22506
22507 prec = c_string_width (SDATA (elt) + last_offset,
22508 offset - last_offset, precision - n,
22509 &nchars, &nbytes);
22510
22511 switch (mode_line_target)
22512 {
22513 case MODE_LINE_NOPROP:
22514 case MODE_LINE_TITLE:
22515 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22516 break;
22517 case MODE_LINE_STRING:
22518 {
22519 ptrdiff_t bytepos = last_offset;
22520 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22521 ptrdiff_t endpos = (precision <= 0
22522 ? string_byte_to_char (elt, offset)
22523 : charpos + nchars);
22524 Lisp_Object mode_string
22525 = Fsubstring (elt, make_number (charpos),
22526 make_number (endpos));
22527 n += store_mode_line_string (NULL, mode_string, false,
22528 0, 0, Qnil);
22529 }
22530 break;
22531 case MODE_LINE_DISPLAY:
22532 {
22533 ptrdiff_t bytepos = last_offset;
22534 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22535
22536 if (precision <= 0)
22537 nchars = string_byte_to_char (elt, offset) - charpos;
22538 n += display_string (NULL, elt, Qnil, 0, charpos,
22539 it, 0, nchars, 0,
22540 STRING_MULTIBYTE (elt));
22541 }
22542 break;
22543 }
22544 }
22545 else /* c == '%' */
22546 {
22547 ptrdiff_t percent_position = offset;
22548
22549 /* Get the specified minimum width. Zero means
22550 don't pad. */
22551 field = 0;
22552 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22553 field = field * 10 + c - '0';
22554
22555 /* Don't pad beyond the total padding allowed. */
22556 if (field_width - n > 0 && field > field_width - n)
22557 field = field_width - n;
22558
22559 /* Note that either PRECISION <= 0 or N < PRECISION. */
22560 prec = precision - n;
22561
22562 if (c == 'M')
22563 n += display_mode_element (it, depth, field, prec,
22564 Vglobal_mode_string, props,
22565 risky);
22566 else if (c != 0)
22567 {
22568 bool multibyte;
22569 ptrdiff_t bytepos, charpos;
22570 const char *spec;
22571 Lisp_Object string;
22572
22573 bytepos = percent_position;
22574 charpos = (STRING_MULTIBYTE (elt)
22575 ? string_byte_to_char (elt, bytepos)
22576 : bytepos);
22577 spec = decode_mode_spec (it->w, c, field, &string);
22578 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22579
22580 switch (mode_line_target)
22581 {
22582 case MODE_LINE_NOPROP:
22583 case MODE_LINE_TITLE:
22584 n += store_mode_line_noprop (spec, field, prec);
22585 break;
22586 case MODE_LINE_STRING:
22587 {
22588 Lisp_Object tem = build_string (spec);
22589 props = Ftext_properties_at (make_number (charpos), elt);
22590 /* Should only keep face property in props */
22591 n += store_mode_line_string (NULL, tem, false,
22592 field, prec, props);
22593 }
22594 break;
22595 case MODE_LINE_DISPLAY:
22596 {
22597 int nglyphs_before, nwritten;
22598
22599 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22600 nwritten = display_string (spec, string, elt,
22601 charpos, 0, it,
22602 field, prec, 0,
22603 multibyte);
22604
22605 /* Assign to the glyphs written above the
22606 string where the `%x' came from, position
22607 of the `%'. */
22608 if (nwritten > 0)
22609 {
22610 struct glyph *glyph
22611 = (it->glyph_row->glyphs[TEXT_AREA]
22612 + nglyphs_before);
22613 int i;
22614
22615 for (i = 0; i < nwritten; ++i)
22616 {
22617 glyph[i].object = elt;
22618 glyph[i].charpos = charpos;
22619 }
22620
22621 n += nwritten;
22622 }
22623 }
22624 break;
22625 }
22626 }
22627 else /* c == 0 */
22628 break;
22629 }
22630 }
22631 }
22632 break;
22633
22634 case Lisp_Symbol:
22635 /* A symbol: process the value of the symbol recursively
22636 as if it appeared here directly. Avoid error if symbol void.
22637 Special case: if value of symbol is a string, output the string
22638 literally. */
22639 {
22640 register Lisp_Object tem;
22641
22642 /* If the variable is not marked as risky to set
22643 then its contents are risky to use. */
22644 if (NILP (Fget (elt, Qrisky_local_variable)))
22645 risky = true;
22646
22647 tem = Fboundp (elt);
22648 if (!NILP (tem))
22649 {
22650 tem = Fsymbol_value (elt);
22651 /* If value is a string, output that string literally:
22652 don't check for % within it. */
22653 if (STRINGP (tem))
22654 literal = true;
22655
22656 if (!EQ (tem, elt))
22657 {
22658 /* Give up right away for nil or t. */
22659 elt = tem;
22660 goto tail_recurse;
22661 }
22662 }
22663 }
22664 break;
22665
22666 case Lisp_Cons:
22667 {
22668 register Lisp_Object car, tem;
22669
22670 /* A cons cell: five distinct cases.
22671 If first element is :eval or :propertize, do something special.
22672 If first element is a string or a cons, process all the elements
22673 and effectively concatenate them.
22674 If first element is a negative number, truncate displaying cdr to
22675 at most that many characters. If positive, pad (with spaces)
22676 to at least that many characters.
22677 If first element is a symbol, process the cadr or caddr recursively
22678 according to whether the symbol's value is non-nil or nil. */
22679 car = XCAR (elt);
22680 if (EQ (car, QCeval))
22681 {
22682 /* An element of the form (:eval FORM) means evaluate FORM
22683 and use the result as mode line elements. */
22684
22685 if (risky)
22686 break;
22687
22688 if (CONSP (XCDR (elt)))
22689 {
22690 Lisp_Object spec;
22691 spec = safe__eval (true, XCAR (XCDR (elt)));
22692 n += display_mode_element (it, depth, field_width - n,
22693 precision - n, spec, props,
22694 risky);
22695 }
22696 }
22697 else if (EQ (car, QCpropertize))
22698 {
22699 /* An element of the form (:propertize ELT PROPS...)
22700 means display ELT but applying properties PROPS. */
22701
22702 if (risky)
22703 break;
22704
22705 if (CONSP (XCDR (elt)))
22706 n += display_mode_element (it, depth, field_width - n,
22707 precision - n, XCAR (XCDR (elt)),
22708 XCDR (XCDR (elt)), risky);
22709 }
22710 else if (SYMBOLP (car))
22711 {
22712 tem = Fboundp (car);
22713 elt = XCDR (elt);
22714 if (!CONSP (elt))
22715 goto invalid;
22716 /* elt is now the cdr, and we know it is a cons cell.
22717 Use its car if CAR has a non-nil value. */
22718 if (!NILP (tem))
22719 {
22720 tem = Fsymbol_value (car);
22721 if (!NILP (tem))
22722 {
22723 elt = XCAR (elt);
22724 goto tail_recurse;
22725 }
22726 }
22727 /* Symbol's value is nil (or symbol is unbound)
22728 Get the cddr of the original list
22729 and if possible find the caddr and use that. */
22730 elt = XCDR (elt);
22731 if (NILP (elt))
22732 break;
22733 else if (!CONSP (elt))
22734 goto invalid;
22735 elt = XCAR (elt);
22736 goto tail_recurse;
22737 }
22738 else if (INTEGERP (car))
22739 {
22740 register int lim = XINT (car);
22741 elt = XCDR (elt);
22742 if (lim < 0)
22743 {
22744 /* Negative int means reduce maximum width. */
22745 if (precision <= 0)
22746 precision = -lim;
22747 else
22748 precision = min (precision, -lim);
22749 }
22750 else if (lim > 0)
22751 {
22752 /* Padding specified. Don't let it be more than
22753 current maximum. */
22754 if (precision > 0)
22755 lim = min (precision, lim);
22756
22757 /* If that's more padding than already wanted, queue it.
22758 But don't reduce padding already specified even if
22759 that is beyond the current truncation point. */
22760 field_width = max (lim, field_width);
22761 }
22762 goto tail_recurse;
22763 }
22764 else if (STRINGP (car) || CONSP (car))
22765 {
22766 Lisp_Object halftail = elt;
22767 int len = 0;
22768
22769 while (CONSP (elt)
22770 && (precision <= 0 || n < precision))
22771 {
22772 n += display_mode_element (it, depth,
22773 /* Do padding only after the last
22774 element in the list. */
22775 (! CONSP (XCDR (elt))
22776 ? field_width - n
22777 : 0),
22778 precision - n, XCAR (elt),
22779 props, risky);
22780 elt = XCDR (elt);
22781 len++;
22782 if ((len & 1) == 0)
22783 halftail = XCDR (halftail);
22784 /* Check for cycle. */
22785 if (EQ (halftail, elt))
22786 break;
22787 }
22788 }
22789 }
22790 break;
22791
22792 default:
22793 invalid:
22794 elt = build_string ("*invalid*");
22795 goto tail_recurse;
22796 }
22797
22798 /* Pad to FIELD_WIDTH. */
22799 if (field_width > 0 && n < field_width)
22800 {
22801 switch (mode_line_target)
22802 {
22803 case MODE_LINE_NOPROP:
22804 case MODE_LINE_TITLE:
22805 n += store_mode_line_noprop ("", field_width - n, 0);
22806 break;
22807 case MODE_LINE_STRING:
22808 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
22809 Qnil);
22810 break;
22811 case MODE_LINE_DISPLAY:
22812 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22813 0, 0, 0);
22814 break;
22815 }
22816 }
22817
22818 return n;
22819 }
22820
22821 /* Store a mode-line string element in mode_line_string_list.
22822
22823 If STRING is non-null, display that C string. Otherwise, the Lisp
22824 string LISP_STRING is displayed.
22825
22826 FIELD_WIDTH is the minimum number of output glyphs to produce.
22827 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22828 with spaces. FIELD_WIDTH <= 0 means don't pad.
22829
22830 PRECISION is the maximum number of characters to output from
22831 STRING. PRECISION <= 0 means don't truncate the string.
22832
22833 If COPY_STRING, make a copy of LISP_STRING before adding
22834 properties to the string.
22835
22836 PROPS are the properties to add to the string.
22837 The mode_line_string_face face property is always added to the string.
22838 */
22839
22840 static int
22841 store_mode_line_string (const char *string, Lisp_Object lisp_string,
22842 bool copy_string,
22843 int field_width, int precision, Lisp_Object props)
22844 {
22845 ptrdiff_t len;
22846 int n = 0;
22847
22848 if (string != NULL)
22849 {
22850 len = strlen (string);
22851 if (precision > 0 && len > precision)
22852 len = precision;
22853 lisp_string = make_string (string, len);
22854 if (NILP (props))
22855 props = mode_line_string_face_prop;
22856 else if (!NILP (mode_line_string_face))
22857 {
22858 Lisp_Object face = Fplist_get (props, Qface);
22859 props = Fcopy_sequence (props);
22860 if (NILP (face))
22861 face = mode_line_string_face;
22862 else
22863 face = list2 (face, mode_line_string_face);
22864 props = Fplist_put (props, Qface, face);
22865 }
22866 Fadd_text_properties (make_number (0), make_number (len),
22867 props, lisp_string);
22868 }
22869 else
22870 {
22871 len = XFASTINT (Flength (lisp_string));
22872 if (precision > 0 && len > precision)
22873 {
22874 len = precision;
22875 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22876 precision = -1;
22877 }
22878 if (!NILP (mode_line_string_face))
22879 {
22880 Lisp_Object face;
22881 if (NILP (props))
22882 props = Ftext_properties_at (make_number (0), lisp_string);
22883 face = Fplist_get (props, Qface);
22884 if (NILP (face))
22885 face = mode_line_string_face;
22886 else
22887 face = list2 (face, mode_line_string_face);
22888 props = list2 (Qface, face);
22889 if (copy_string)
22890 lisp_string = Fcopy_sequence (lisp_string);
22891 }
22892 if (!NILP (props))
22893 Fadd_text_properties (make_number (0), make_number (len),
22894 props, lisp_string);
22895 }
22896
22897 if (len > 0)
22898 {
22899 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22900 n += len;
22901 }
22902
22903 if (field_width > len)
22904 {
22905 field_width -= len;
22906 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22907 if (!NILP (props))
22908 Fadd_text_properties (make_number (0), make_number (field_width),
22909 props, lisp_string);
22910 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22911 n += field_width;
22912 }
22913
22914 return n;
22915 }
22916
22917
22918 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22919 1, 4, 0,
22920 doc: /* Format a string out of a mode line format specification.
22921 First arg FORMAT specifies the mode line format (see `mode-line-format'
22922 for details) to use.
22923
22924 By default, the format is evaluated for the currently selected window.
22925
22926 Optional second arg FACE specifies the face property to put on all
22927 characters for which no face is specified. The value nil means the
22928 default face. The value t means whatever face the window's mode line
22929 currently uses (either `mode-line' or `mode-line-inactive',
22930 depending on whether the window is the selected window or not).
22931 An integer value means the value string has no text
22932 properties.
22933
22934 Optional third and fourth args WINDOW and BUFFER specify the window
22935 and buffer to use as the context for the formatting (defaults
22936 are the selected window and the WINDOW's buffer). */)
22937 (Lisp_Object format, Lisp_Object face,
22938 Lisp_Object window, Lisp_Object buffer)
22939 {
22940 struct it it;
22941 int len;
22942 struct window *w;
22943 struct buffer *old_buffer = NULL;
22944 int face_id;
22945 bool no_props = INTEGERP (face);
22946 ptrdiff_t count = SPECPDL_INDEX ();
22947 Lisp_Object str;
22948 int string_start = 0;
22949
22950 w = decode_any_window (window);
22951 XSETWINDOW (window, w);
22952
22953 if (NILP (buffer))
22954 buffer = w->contents;
22955 CHECK_BUFFER (buffer);
22956
22957 /* Make formatting the modeline a non-op when noninteractive, otherwise
22958 there will be problems later caused by a partially initialized frame. */
22959 if (NILP (format) || noninteractive)
22960 return empty_unibyte_string;
22961
22962 if (no_props)
22963 face = Qnil;
22964
22965 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22966 : EQ (face, Qt) ? (EQ (window, selected_window)
22967 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22968 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22969 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22970 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22971 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22972 : DEFAULT_FACE_ID;
22973
22974 old_buffer = current_buffer;
22975
22976 /* Save things including mode_line_proptrans_alist,
22977 and set that to nil so that we don't alter the outer value. */
22978 record_unwind_protect (unwind_format_mode_line,
22979 format_mode_line_unwind_data
22980 (XFRAME (WINDOW_FRAME (w)),
22981 old_buffer, selected_window, true));
22982 mode_line_proptrans_alist = Qnil;
22983
22984 Fselect_window (window, Qt);
22985 set_buffer_internal_1 (XBUFFER (buffer));
22986
22987 init_iterator (&it, w, -1, -1, NULL, face_id);
22988
22989 if (no_props)
22990 {
22991 mode_line_target = MODE_LINE_NOPROP;
22992 mode_line_string_face_prop = Qnil;
22993 mode_line_string_list = Qnil;
22994 string_start = MODE_LINE_NOPROP_LEN (0);
22995 }
22996 else
22997 {
22998 mode_line_target = MODE_LINE_STRING;
22999 mode_line_string_list = Qnil;
23000 mode_line_string_face = face;
23001 mode_line_string_face_prop
23002 = NILP (face) ? Qnil : list2 (Qface, face);
23003 }
23004
23005 push_kboard (FRAME_KBOARD (it.f));
23006 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23007 pop_kboard ();
23008
23009 if (no_props)
23010 {
23011 len = MODE_LINE_NOPROP_LEN (string_start);
23012 str = make_string (mode_line_noprop_buf + string_start, len);
23013 }
23014 else
23015 {
23016 mode_line_string_list = Fnreverse (mode_line_string_list);
23017 str = Fmapconcat (Qidentity, mode_line_string_list,
23018 empty_unibyte_string);
23019 }
23020
23021 unbind_to (count, Qnil);
23022 return str;
23023 }
23024
23025 /* Write a null-terminated, right justified decimal representation of
23026 the positive integer D to BUF using a minimal field width WIDTH. */
23027
23028 static void
23029 pint2str (register char *buf, register int width, register ptrdiff_t d)
23030 {
23031 register char *p = buf;
23032
23033 if (d <= 0)
23034 *p++ = '0';
23035 else
23036 {
23037 while (d > 0)
23038 {
23039 *p++ = d % 10 + '0';
23040 d /= 10;
23041 }
23042 }
23043
23044 for (width -= (int) (p - buf); width > 0; --width)
23045 *p++ = ' ';
23046 *p-- = '\0';
23047 while (p > buf)
23048 {
23049 d = *buf;
23050 *buf++ = *p;
23051 *p-- = d;
23052 }
23053 }
23054
23055 /* Write a null-terminated, right justified decimal and "human
23056 readable" representation of the nonnegative integer D to BUF using
23057 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23058
23059 static const char power_letter[] =
23060 {
23061 0, /* no letter */
23062 'k', /* kilo */
23063 'M', /* mega */
23064 'G', /* giga */
23065 'T', /* tera */
23066 'P', /* peta */
23067 'E', /* exa */
23068 'Z', /* zetta */
23069 'Y' /* yotta */
23070 };
23071
23072 static void
23073 pint2hrstr (char *buf, int width, ptrdiff_t d)
23074 {
23075 /* We aim to represent the nonnegative integer D as
23076 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23077 ptrdiff_t quotient = d;
23078 int remainder = 0;
23079 /* -1 means: do not use TENTHS. */
23080 int tenths = -1;
23081 int exponent = 0;
23082
23083 /* Length of QUOTIENT.TENTHS as a string. */
23084 int length;
23085
23086 char * psuffix;
23087 char * p;
23088
23089 if (quotient >= 1000)
23090 {
23091 /* Scale to the appropriate EXPONENT. */
23092 do
23093 {
23094 remainder = quotient % 1000;
23095 quotient /= 1000;
23096 exponent++;
23097 }
23098 while (quotient >= 1000);
23099
23100 /* Round to nearest and decide whether to use TENTHS or not. */
23101 if (quotient <= 9)
23102 {
23103 tenths = remainder / 100;
23104 if (remainder % 100 >= 50)
23105 {
23106 if (tenths < 9)
23107 tenths++;
23108 else
23109 {
23110 quotient++;
23111 if (quotient == 10)
23112 tenths = -1;
23113 else
23114 tenths = 0;
23115 }
23116 }
23117 }
23118 else
23119 if (remainder >= 500)
23120 {
23121 if (quotient < 999)
23122 quotient++;
23123 else
23124 {
23125 quotient = 1;
23126 exponent++;
23127 tenths = 0;
23128 }
23129 }
23130 }
23131
23132 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23133 if (tenths == -1 && quotient <= 99)
23134 if (quotient <= 9)
23135 length = 1;
23136 else
23137 length = 2;
23138 else
23139 length = 3;
23140 p = psuffix = buf + max (width, length);
23141
23142 /* Print EXPONENT. */
23143 *psuffix++ = power_letter[exponent];
23144 *psuffix = '\0';
23145
23146 /* Print TENTHS. */
23147 if (tenths >= 0)
23148 {
23149 *--p = '0' + tenths;
23150 *--p = '.';
23151 }
23152
23153 /* Print QUOTIENT. */
23154 do
23155 {
23156 int digit = quotient % 10;
23157 *--p = '0' + digit;
23158 }
23159 while ((quotient /= 10) != 0);
23160
23161 /* Print leading spaces. */
23162 while (buf < p)
23163 *--p = ' ';
23164 }
23165
23166 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23167 If EOL_FLAG, set also a mnemonic character for end-of-line
23168 type of CODING_SYSTEM. Return updated pointer into BUF. */
23169
23170 static unsigned char invalid_eol_type[] = "(*invalid*)";
23171
23172 static char *
23173 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23174 {
23175 Lisp_Object val;
23176 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23177 const unsigned char *eol_str;
23178 int eol_str_len;
23179 /* The EOL conversion we are using. */
23180 Lisp_Object eoltype;
23181
23182 val = CODING_SYSTEM_SPEC (coding_system);
23183 eoltype = Qnil;
23184
23185 if (!VECTORP (val)) /* Not yet decided. */
23186 {
23187 *buf++ = multibyte ? '-' : ' ';
23188 if (eol_flag)
23189 eoltype = eol_mnemonic_undecided;
23190 /* Don't mention EOL conversion if it isn't decided. */
23191 }
23192 else
23193 {
23194 Lisp_Object attrs;
23195 Lisp_Object eolvalue;
23196
23197 attrs = AREF (val, 0);
23198 eolvalue = AREF (val, 2);
23199
23200 *buf++ = multibyte
23201 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23202 : ' ';
23203
23204 if (eol_flag)
23205 {
23206 /* The EOL conversion that is normal on this system. */
23207
23208 if (NILP (eolvalue)) /* Not yet decided. */
23209 eoltype = eol_mnemonic_undecided;
23210 else if (VECTORP (eolvalue)) /* Not yet decided. */
23211 eoltype = eol_mnemonic_undecided;
23212 else /* eolvalue is Qunix, Qdos, or Qmac. */
23213 eoltype = (EQ (eolvalue, Qunix)
23214 ? eol_mnemonic_unix
23215 : EQ (eolvalue, Qdos)
23216 ? eol_mnemonic_dos : eol_mnemonic_mac);
23217 }
23218 }
23219
23220 if (eol_flag)
23221 {
23222 /* Mention the EOL conversion if it is not the usual one. */
23223 if (STRINGP (eoltype))
23224 {
23225 eol_str = SDATA (eoltype);
23226 eol_str_len = SBYTES (eoltype);
23227 }
23228 else if (CHARACTERP (eoltype))
23229 {
23230 int c = XFASTINT (eoltype);
23231 return buf + CHAR_STRING (c, (unsigned char *) buf);
23232 }
23233 else
23234 {
23235 eol_str = invalid_eol_type;
23236 eol_str_len = sizeof (invalid_eol_type) - 1;
23237 }
23238 memcpy (buf, eol_str, eol_str_len);
23239 buf += eol_str_len;
23240 }
23241
23242 return buf;
23243 }
23244
23245 /* Return a string for the output of a mode line %-spec for window W,
23246 generated by character C. FIELD_WIDTH > 0 means pad the string
23247 returned with spaces to that value. Return a Lisp string in
23248 *STRING if the resulting string is taken from that Lisp string.
23249
23250 Note we operate on the current buffer for most purposes. */
23251
23252 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23253
23254 static const char *
23255 decode_mode_spec (struct window *w, register int c, int field_width,
23256 Lisp_Object *string)
23257 {
23258 Lisp_Object obj;
23259 struct frame *f = XFRAME (WINDOW_FRAME (w));
23260 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23261 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23262 produce strings from numerical values, so limit preposterously
23263 large values of FIELD_WIDTH to avoid overrunning the buffer's
23264 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23265 bytes plus the terminating null. */
23266 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23267 struct buffer *b = current_buffer;
23268
23269 obj = Qnil;
23270 *string = Qnil;
23271
23272 switch (c)
23273 {
23274 case '*':
23275 if (!NILP (BVAR (b, read_only)))
23276 return "%";
23277 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23278 return "*";
23279 return "-";
23280
23281 case '+':
23282 /* This differs from %* only for a modified read-only buffer. */
23283 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23284 return "*";
23285 if (!NILP (BVAR (b, read_only)))
23286 return "%";
23287 return "-";
23288
23289 case '&':
23290 /* This differs from %* in ignoring read-only-ness. */
23291 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23292 return "*";
23293 return "-";
23294
23295 case '%':
23296 return "%";
23297
23298 case '[':
23299 {
23300 int i;
23301 char *p;
23302
23303 if (command_loop_level > 5)
23304 return "[[[... ";
23305 p = decode_mode_spec_buf;
23306 for (i = 0; i < command_loop_level; i++)
23307 *p++ = '[';
23308 *p = 0;
23309 return decode_mode_spec_buf;
23310 }
23311
23312 case ']':
23313 {
23314 int i;
23315 char *p;
23316
23317 if (command_loop_level > 5)
23318 return " ...]]]";
23319 p = decode_mode_spec_buf;
23320 for (i = 0; i < command_loop_level; i++)
23321 *p++ = ']';
23322 *p = 0;
23323 return decode_mode_spec_buf;
23324 }
23325
23326 case '-':
23327 {
23328 register int i;
23329
23330 /* Let lots_of_dashes be a string of infinite length. */
23331 if (mode_line_target == MODE_LINE_NOPROP
23332 || mode_line_target == MODE_LINE_STRING)
23333 return "--";
23334 if (field_width <= 0
23335 || field_width > sizeof (lots_of_dashes))
23336 {
23337 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23338 decode_mode_spec_buf[i] = '-';
23339 decode_mode_spec_buf[i] = '\0';
23340 return decode_mode_spec_buf;
23341 }
23342 else
23343 return lots_of_dashes;
23344 }
23345
23346 case 'b':
23347 obj = BVAR (b, name);
23348 break;
23349
23350 case 'c':
23351 /* %c and %l are ignored in `frame-title-format'.
23352 (In redisplay_internal, the frame title is drawn _before_ the
23353 windows are updated, so the stuff which depends on actual
23354 window contents (such as %l) may fail to render properly, or
23355 even crash emacs.) */
23356 if (mode_line_target == MODE_LINE_TITLE)
23357 return "";
23358 else
23359 {
23360 ptrdiff_t col = current_column ();
23361 w->column_number_displayed = col;
23362 pint2str (decode_mode_spec_buf, width, col);
23363 return decode_mode_spec_buf;
23364 }
23365
23366 case 'e':
23367 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23368 {
23369 if (NILP (Vmemory_full))
23370 return "";
23371 else
23372 return "!MEM FULL! ";
23373 }
23374 #else
23375 return "";
23376 #endif
23377
23378 case 'F':
23379 /* %F displays the frame name. */
23380 if (!NILP (f->title))
23381 return SSDATA (f->title);
23382 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23383 return SSDATA (f->name);
23384 return "Emacs";
23385
23386 case 'f':
23387 obj = BVAR (b, filename);
23388 break;
23389
23390 case 'i':
23391 {
23392 ptrdiff_t size = ZV - BEGV;
23393 pint2str (decode_mode_spec_buf, width, size);
23394 return decode_mode_spec_buf;
23395 }
23396
23397 case 'I':
23398 {
23399 ptrdiff_t size = ZV - BEGV;
23400 pint2hrstr (decode_mode_spec_buf, width, size);
23401 return decode_mode_spec_buf;
23402 }
23403
23404 case 'l':
23405 {
23406 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23407 ptrdiff_t topline, nlines, height;
23408 ptrdiff_t junk;
23409
23410 /* %c and %l are ignored in `frame-title-format'. */
23411 if (mode_line_target == MODE_LINE_TITLE)
23412 return "";
23413
23414 startpos = marker_position (w->start);
23415 startpos_byte = marker_byte_position (w->start);
23416 height = WINDOW_TOTAL_LINES (w);
23417
23418 /* If we decided that this buffer isn't suitable for line numbers,
23419 don't forget that too fast. */
23420 if (w->base_line_pos == -1)
23421 goto no_value;
23422
23423 /* If the buffer is very big, don't waste time. */
23424 if (INTEGERP (Vline_number_display_limit)
23425 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23426 {
23427 w->base_line_pos = 0;
23428 w->base_line_number = 0;
23429 goto no_value;
23430 }
23431
23432 if (w->base_line_number > 0
23433 && w->base_line_pos > 0
23434 && w->base_line_pos <= startpos)
23435 {
23436 line = w->base_line_number;
23437 linepos = w->base_line_pos;
23438 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23439 }
23440 else
23441 {
23442 line = 1;
23443 linepos = BUF_BEGV (b);
23444 linepos_byte = BUF_BEGV_BYTE (b);
23445 }
23446
23447 /* Count lines from base line to window start position. */
23448 nlines = display_count_lines (linepos_byte,
23449 startpos_byte,
23450 startpos, &junk);
23451
23452 topline = nlines + line;
23453
23454 /* Determine a new base line, if the old one is too close
23455 or too far away, or if we did not have one.
23456 "Too close" means it's plausible a scroll-down would
23457 go back past it. */
23458 if (startpos == BUF_BEGV (b))
23459 {
23460 w->base_line_number = topline;
23461 w->base_line_pos = BUF_BEGV (b);
23462 }
23463 else if (nlines < height + 25 || nlines > height * 3 + 50
23464 || linepos == BUF_BEGV (b))
23465 {
23466 ptrdiff_t limit = BUF_BEGV (b);
23467 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23468 ptrdiff_t position;
23469 ptrdiff_t distance =
23470 (height * 2 + 30) * line_number_display_limit_width;
23471
23472 if (startpos - distance > limit)
23473 {
23474 limit = startpos - distance;
23475 limit_byte = CHAR_TO_BYTE (limit);
23476 }
23477
23478 nlines = display_count_lines (startpos_byte,
23479 limit_byte,
23480 - (height * 2 + 30),
23481 &position);
23482 /* If we couldn't find the lines we wanted within
23483 line_number_display_limit_width chars per line,
23484 give up on line numbers for this window. */
23485 if (position == limit_byte && limit == startpos - distance)
23486 {
23487 w->base_line_pos = -1;
23488 w->base_line_number = 0;
23489 goto no_value;
23490 }
23491
23492 w->base_line_number = topline - nlines;
23493 w->base_line_pos = BYTE_TO_CHAR (position);
23494 }
23495
23496 /* Now count lines from the start pos to point. */
23497 nlines = display_count_lines (startpos_byte,
23498 PT_BYTE, PT, &junk);
23499
23500 /* Record that we did display the line number. */
23501 line_number_displayed = true;
23502
23503 /* Make the string to show. */
23504 pint2str (decode_mode_spec_buf, width, topline + nlines);
23505 return decode_mode_spec_buf;
23506 no_value:
23507 {
23508 char *p = decode_mode_spec_buf;
23509 int pad = width - 2;
23510 while (pad-- > 0)
23511 *p++ = ' ';
23512 *p++ = '?';
23513 *p++ = '?';
23514 *p = '\0';
23515 return decode_mode_spec_buf;
23516 }
23517 }
23518 break;
23519
23520 case 'm':
23521 obj = BVAR (b, mode_name);
23522 break;
23523
23524 case 'n':
23525 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23526 return " Narrow";
23527 break;
23528
23529 case 'p':
23530 {
23531 ptrdiff_t pos = marker_position (w->start);
23532 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23533
23534 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23535 {
23536 if (pos <= BUF_BEGV (b))
23537 return "All";
23538 else
23539 return "Bottom";
23540 }
23541 else if (pos <= BUF_BEGV (b))
23542 return "Top";
23543 else
23544 {
23545 if (total > 1000000)
23546 /* Do it differently for a large value, to avoid overflow. */
23547 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23548 else
23549 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23550 /* We can't normally display a 3-digit number,
23551 so get us a 2-digit number that is close. */
23552 if (total == 100)
23553 total = 99;
23554 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23555 return decode_mode_spec_buf;
23556 }
23557 }
23558
23559 /* Display percentage of size above the bottom of the screen. */
23560 case 'P':
23561 {
23562 ptrdiff_t toppos = marker_position (w->start);
23563 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23564 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23565
23566 if (botpos >= BUF_ZV (b))
23567 {
23568 if (toppos <= BUF_BEGV (b))
23569 return "All";
23570 else
23571 return "Bottom";
23572 }
23573 else
23574 {
23575 if (total > 1000000)
23576 /* Do it differently for a large value, to avoid overflow. */
23577 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23578 else
23579 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23580 /* We can't normally display a 3-digit number,
23581 so get us a 2-digit number that is close. */
23582 if (total == 100)
23583 total = 99;
23584 if (toppos <= BUF_BEGV (b))
23585 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23586 else
23587 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23588 return decode_mode_spec_buf;
23589 }
23590 }
23591
23592 case 's':
23593 /* status of process */
23594 obj = Fget_buffer_process (Fcurrent_buffer ());
23595 if (NILP (obj))
23596 return "no process";
23597 #ifndef MSDOS
23598 obj = Fsymbol_name (Fprocess_status (obj));
23599 #endif
23600 break;
23601
23602 case '@':
23603 {
23604 ptrdiff_t count = inhibit_garbage_collection ();
23605 Lisp_Object curdir = BVAR (current_buffer, directory);
23606 Lisp_Object val = Qnil;
23607
23608 if (STRINGP (curdir))
23609 val = call1 (intern ("file-remote-p"), curdir);
23610
23611 unbind_to (count, Qnil);
23612
23613 if (NILP (val))
23614 return "-";
23615 else
23616 return "@";
23617 }
23618
23619 case 'z':
23620 /* coding-system (not including end-of-line format) */
23621 case 'Z':
23622 /* coding-system (including end-of-line type) */
23623 {
23624 bool eol_flag = (c == 'Z');
23625 char *p = decode_mode_spec_buf;
23626
23627 if (! FRAME_WINDOW_P (f))
23628 {
23629 /* No need to mention EOL here--the terminal never needs
23630 to do EOL conversion. */
23631 p = decode_mode_spec_coding (CODING_ID_NAME
23632 (FRAME_KEYBOARD_CODING (f)->id),
23633 p, false);
23634 p = decode_mode_spec_coding (CODING_ID_NAME
23635 (FRAME_TERMINAL_CODING (f)->id),
23636 p, false);
23637 }
23638 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23639 p, eol_flag);
23640
23641 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23642 #ifdef subprocesses
23643 obj = Fget_buffer_process (Fcurrent_buffer ());
23644 if (PROCESSP (obj))
23645 {
23646 p = decode_mode_spec_coding
23647 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23648 p = decode_mode_spec_coding
23649 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23650 }
23651 #endif /* subprocesses */
23652 #endif /* false */
23653 *p = 0;
23654 return decode_mode_spec_buf;
23655 }
23656 }
23657
23658 if (STRINGP (obj))
23659 {
23660 *string = obj;
23661 return SSDATA (obj);
23662 }
23663 else
23664 return "";
23665 }
23666
23667
23668 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23669 means count lines back from START_BYTE. But don't go beyond
23670 LIMIT_BYTE. Return the number of lines thus found (always
23671 nonnegative).
23672
23673 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23674 either the position COUNT lines after/before START_BYTE, if we
23675 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23676 COUNT lines. */
23677
23678 static ptrdiff_t
23679 display_count_lines (ptrdiff_t start_byte,
23680 ptrdiff_t limit_byte, ptrdiff_t count,
23681 ptrdiff_t *byte_pos_ptr)
23682 {
23683 register unsigned char *cursor;
23684 unsigned char *base;
23685
23686 register ptrdiff_t ceiling;
23687 register unsigned char *ceiling_addr;
23688 ptrdiff_t orig_count = count;
23689
23690 /* If we are not in selective display mode,
23691 check only for newlines. */
23692 bool selective_display
23693 = (!NILP (BVAR (current_buffer, selective_display))
23694 && !INTEGERP (BVAR (current_buffer, selective_display)));
23695
23696 if (count > 0)
23697 {
23698 while (start_byte < limit_byte)
23699 {
23700 ceiling = BUFFER_CEILING_OF (start_byte);
23701 ceiling = min (limit_byte - 1, ceiling);
23702 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23703 base = (cursor = BYTE_POS_ADDR (start_byte));
23704
23705 do
23706 {
23707 if (selective_display)
23708 {
23709 while (*cursor != '\n' && *cursor != 015
23710 && ++cursor != ceiling_addr)
23711 continue;
23712 if (cursor == ceiling_addr)
23713 break;
23714 }
23715 else
23716 {
23717 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23718 if (! cursor)
23719 break;
23720 }
23721
23722 cursor++;
23723
23724 if (--count == 0)
23725 {
23726 start_byte += cursor - base;
23727 *byte_pos_ptr = start_byte;
23728 return orig_count;
23729 }
23730 }
23731 while (cursor < ceiling_addr);
23732
23733 start_byte += ceiling_addr - base;
23734 }
23735 }
23736 else
23737 {
23738 while (start_byte > limit_byte)
23739 {
23740 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23741 ceiling = max (limit_byte, ceiling);
23742 ceiling_addr = BYTE_POS_ADDR (ceiling);
23743 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23744 while (true)
23745 {
23746 if (selective_display)
23747 {
23748 while (--cursor >= ceiling_addr
23749 && *cursor != '\n' && *cursor != 015)
23750 continue;
23751 if (cursor < ceiling_addr)
23752 break;
23753 }
23754 else
23755 {
23756 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23757 if (! cursor)
23758 break;
23759 }
23760
23761 if (++count == 0)
23762 {
23763 start_byte += cursor - base + 1;
23764 *byte_pos_ptr = start_byte;
23765 /* When scanning backwards, we should
23766 not count the newline posterior to which we stop. */
23767 return - orig_count - 1;
23768 }
23769 }
23770 start_byte += ceiling_addr - base;
23771 }
23772 }
23773
23774 *byte_pos_ptr = limit_byte;
23775
23776 if (count < 0)
23777 return - orig_count + count;
23778 return orig_count - count;
23779
23780 }
23781
23782
23783 \f
23784 /***********************************************************************
23785 Displaying strings
23786 ***********************************************************************/
23787
23788 /* Display a NUL-terminated string, starting with index START.
23789
23790 If STRING is non-null, display that C string. Otherwise, the Lisp
23791 string LISP_STRING is displayed. There's a case that STRING is
23792 non-null and LISP_STRING is not nil. It means STRING is a string
23793 data of LISP_STRING. In that case, we display LISP_STRING while
23794 ignoring its text properties.
23795
23796 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23797 FACE_STRING. Display STRING or LISP_STRING with the face at
23798 FACE_STRING_POS in FACE_STRING:
23799
23800 Display the string in the environment given by IT, but use the
23801 standard display table, temporarily.
23802
23803 FIELD_WIDTH is the minimum number of output glyphs to produce.
23804 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23805 with spaces. If STRING has more characters, more than FIELD_WIDTH
23806 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23807
23808 PRECISION is the maximum number of characters to output from
23809 STRING. PRECISION < 0 means don't truncate the string.
23810
23811 This is roughly equivalent to printf format specifiers:
23812
23813 FIELD_WIDTH PRECISION PRINTF
23814 ----------------------------------------
23815 -1 -1 %s
23816 -1 10 %.10s
23817 10 -1 %10s
23818 20 10 %20.10s
23819
23820 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23821 display them, and < 0 means obey the current buffer's value of
23822 enable_multibyte_characters.
23823
23824 Value is the number of columns displayed. */
23825
23826 static int
23827 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23828 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23829 int field_width, int precision, int max_x, int multibyte)
23830 {
23831 int hpos_at_start = it->hpos;
23832 int saved_face_id = it->face_id;
23833 struct glyph_row *row = it->glyph_row;
23834 ptrdiff_t it_charpos;
23835
23836 /* Initialize the iterator IT for iteration over STRING beginning
23837 with index START. */
23838 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23839 precision, field_width, multibyte);
23840 if (string && STRINGP (lisp_string))
23841 /* LISP_STRING is the one returned by decode_mode_spec. We should
23842 ignore its text properties. */
23843 it->stop_charpos = it->end_charpos;
23844
23845 /* If displaying STRING, set up the face of the iterator from
23846 FACE_STRING, if that's given. */
23847 if (STRINGP (face_string))
23848 {
23849 ptrdiff_t endptr;
23850 struct face *face;
23851
23852 it->face_id
23853 = face_at_string_position (it->w, face_string, face_string_pos,
23854 0, &endptr, it->base_face_id, false);
23855 face = FACE_FROM_ID (it->f, it->face_id);
23856 it->face_box_p = face->box != FACE_NO_BOX;
23857 }
23858
23859 /* Set max_x to the maximum allowed X position. Don't let it go
23860 beyond the right edge of the window. */
23861 if (max_x <= 0)
23862 max_x = it->last_visible_x;
23863 else
23864 max_x = min (max_x, it->last_visible_x);
23865
23866 /* Skip over display elements that are not visible. because IT->w is
23867 hscrolled. */
23868 if (it->current_x < it->first_visible_x)
23869 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23870 MOVE_TO_POS | MOVE_TO_X);
23871
23872 row->ascent = it->max_ascent;
23873 row->height = it->max_ascent + it->max_descent;
23874 row->phys_ascent = it->max_phys_ascent;
23875 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23876 row->extra_line_spacing = it->max_extra_line_spacing;
23877
23878 if (STRINGP (it->string))
23879 it_charpos = IT_STRING_CHARPOS (*it);
23880 else
23881 it_charpos = IT_CHARPOS (*it);
23882
23883 /* This condition is for the case that we are called with current_x
23884 past last_visible_x. */
23885 while (it->current_x < max_x)
23886 {
23887 int x_before, x, n_glyphs_before, i, nglyphs;
23888
23889 /* Get the next display element. */
23890 if (!get_next_display_element (it))
23891 break;
23892
23893 /* Produce glyphs. */
23894 x_before = it->current_x;
23895 n_glyphs_before = row->used[TEXT_AREA];
23896 PRODUCE_GLYPHS (it);
23897
23898 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23899 i = 0;
23900 x = x_before;
23901 while (i < nglyphs)
23902 {
23903 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23904
23905 if (it->line_wrap != TRUNCATE
23906 && x + glyph->pixel_width > max_x)
23907 {
23908 /* End of continued line or max_x reached. */
23909 if (CHAR_GLYPH_PADDING_P (*glyph))
23910 {
23911 /* A wide character is unbreakable. */
23912 if (row->reversed_p)
23913 unproduce_glyphs (it, row->used[TEXT_AREA]
23914 - n_glyphs_before);
23915 row->used[TEXT_AREA] = n_glyphs_before;
23916 it->current_x = x_before;
23917 }
23918 else
23919 {
23920 if (row->reversed_p)
23921 unproduce_glyphs (it, row->used[TEXT_AREA]
23922 - (n_glyphs_before + i));
23923 row->used[TEXT_AREA] = n_glyphs_before + i;
23924 it->current_x = x;
23925 }
23926 break;
23927 }
23928 else if (x + glyph->pixel_width >= it->first_visible_x)
23929 {
23930 /* Glyph is at least partially visible. */
23931 ++it->hpos;
23932 if (x < it->first_visible_x)
23933 row->x = x - it->first_visible_x;
23934 }
23935 else
23936 {
23937 /* Glyph is off the left margin of the display area.
23938 Should not happen. */
23939 emacs_abort ();
23940 }
23941
23942 row->ascent = max (row->ascent, it->max_ascent);
23943 row->height = max (row->height, it->max_ascent + it->max_descent);
23944 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23945 row->phys_height = max (row->phys_height,
23946 it->max_phys_ascent + it->max_phys_descent);
23947 row->extra_line_spacing = max (row->extra_line_spacing,
23948 it->max_extra_line_spacing);
23949 x += glyph->pixel_width;
23950 ++i;
23951 }
23952
23953 /* Stop if max_x reached. */
23954 if (i < nglyphs)
23955 break;
23956
23957 /* Stop at line ends. */
23958 if (ITERATOR_AT_END_OF_LINE_P (it))
23959 {
23960 it->continuation_lines_width = 0;
23961 break;
23962 }
23963
23964 set_iterator_to_next (it, true);
23965 if (STRINGP (it->string))
23966 it_charpos = IT_STRING_CHARPOS (*it);
23967 else
23968 it_charpos = IT_CHARPOS (*it);
23969
23970 /* Stop if truncating at the right edge. */
23971 if (it->line_wrap == TRUNCATE
23972 && it->current_x >= it->last_visible_x)
23973 {
23974 /* Add truncation mark, but don't do it if the line is
23975 truncated at a padding space. */
23976 if (it_charpos < it->string_nchars)
23977 {
23978 if (!FRAME_WINDOW_P (it->f))
23979 {
23980 int ii, n;
23981
23982 if (it->current_x > it->last_visible_x)
23983 {
23984 if (!row->reversed_p)
23985 {
23986 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23987 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23988 break;
23989 }
23990 else
23991 {
23992 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23993 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23994 break;
23995 unproduce_glyphs (it, ii + 1);
23996 ii = row->used[TEXT_AREA] - (ii + 1);
23997 }
23998 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23999 {
24000 row->used[TEXT_AREA] = ii;
24001 produce_special_glyphs (it, IT_TRUNCATION);
24002 }
24003 }
24004 produce_special_glyphs (it, IT_TRUNCATION);
24005 }
24006 row->truncated_on_right_p = true;
24007 }
24008 break;
24009 }
24010 }
24011
24012 /* Maybe insert a truncation at the left. */
24013 if (it->first_visible_x
24014 && it_charpos > 0)
24015 {
24016 if (!FRAME_WINDOW_P (it->f)
24017 || (row->reversed_p
24018 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24019 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24020 insert_left_trunc_glyphs (it);
24021 row->truncated_on_left_p = true;
24022 }
24023
24024 it->face_id = saved_face_id;
24025
24026 /* Value is number of columns displayed. */
24027 return it->hpos - hpos_at_start;
24028 }
24029
24030
24031 \f
24032 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24033 appears as an element of LIST or as the car of an element of LIST.
24034 If PROPVAL is a list, compare each element against LIST in that
24035 way, and return 1/2 if any element of PROPVAL is found in LIST.
24036 Otherwise return 0. This function cannot quit.
24037 The return value is 2 if the text is invisible but with an ellipsis
24038 and 1 if it's invisible and without an ellipsis. */
24039
24040 int
24041 invisible_prop (Lisp_Object propval, Lisp_Object list)
24042 {
24043 Lisp_Object tail, proptail;
24044
24045 for (tail = list; CONSP (tail); tail = XCDR (tail))
24046 {
24047 register Lisp_Object tem;
24048 tem = XCAR (tail);
24049 if (EQ (propval, tem))
24050 return 1;
24051 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24052 return NILP (XCDR (tem)) ? 1 : 2;
24053 }
24054
24055 if (CONSP (propval))
24056 {
24057 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
24058 {
24059 Lisp_Object propelt;
24060 propelt = XCAR (proptail);
24061 for (tail = list; CONSP (tail); tail = XCDR (tail))
24062 {
24063 register Lisp_Object tem;
24064 tem = XCAR (tail);
24065 if (EQ (propelt, tem))
24066 return 1;
24067 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
24068 return NILP (XCDR (tem)) ? 1 : 2;
24069 }
24070 }
24071 }
24072
24073 return 0;
24074 }
24075
24076 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
24077 doc: /* Non-nil if the property makes the text invisible.
24078 POS-OR-PROP can be a marker or number, in which case it is taken to be
24079 a position in the current buffer and the value of the `invisible' property
24080 is checked; or it can be some other value, which is then presumed to be the
24081 value of the `invisible' property of the text of interest.
24082 The non-nil value returned can be t for truly invisible text or something
24083 else if the text is replaced by an ellipsis. */)
24084 (Lisp_Object pos_or_prop)
24085 {
24086 Lisp_Object prop
24087 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
24088 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
24089 : pos_or_prop);
24090 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
24091 return (invis == 0 ? Qnil
24092 : invis == 1 ? Qt
24093 : make_number (invis));
24094 }
24095
24096 /* Calculate a width or height in pixels from a specification using
24097 the following elements:
24098
24099 SPEC ::=
24100 NUM - a (fractional) multiple of the default font width/height
24101 (NUM) - specifies exactly NUM pixels
24102 UNIT - a fixed number of pixels, see below.
24103 ELEMENT - size of a display element in pixels, see below.
24104 (NUM . SPEC) - equals NUM * SPEC
24105 (+ SPEC SPEC ...) - add pixel values
24106 (- SPEC SPEC ...) - subtract pixel values
24107 (- SPEC) - negate pixel value
24108
24109 NUM ::=
24110 INT or FLOAT - a number constant
24111 SYMBOL - use symbol's (buffer local) variable binding.
24112
24113 UNIT ::=
24114 in - pixels per inch *)
24115 mm - pixels per 1/1000 meter *)
24116 cm - pixels per 1/100 meter *)
24117 width - width of current font in pixels.
24118 height - height of current font in pixels.
24119
24120 *) using the ratio(s) defined in display-pixels-per-inch.
24121
24122 ELEMENT ::=
24123
24124 left-fringe - left fringe width in pixels
24125 right-fringe - right fringe width in pixels
24126
24127 left-margin - left margin width in pixels
24128 right-margin - right margin width in pixels
24129
24130 scroll-bar - scroll-bar area width in pixels
24131
24132 Examples:
24133
24134 Pixels corresponding to 5 inches:
24135 (5 . in)
24136
24137 Total width of non-text areas on left side of window (if scroll-bar is on left):
24138 '(space :width (+ left-fringe left-margin scroll-bar))
24139
24140 Align to first text column (in header line):
24141 '(space :align-to 0)
24142
24143 Align to middle of text area minus half the width of variable `my-image'
24144 containing a loaded image:
24145 '(space :align-to (0.5 . (- text my-image)))
24146
24147 Width of left margin minus width of 1 character in the default font:
24148 '(space :width (- left-margin 1))
24149
24150 Width of left margin minus width of 2 characters in the current font:
24151 '(space :width (- left-margin (2 . width)))
24152
24153 Center 1 character over left-margin (in header line):
24154 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24155
24156 Different ways to express width of left fringe plus left margin minus one pixel:
24157 '(space :width (- (+ left-fringe left-margin) (1)))
24158 '(space :width (+ left-fringe left-margin (- (1))))
24159 '(space :width (+ left-fringe left-margin (-1)))
24160
24161 */
24162
24163 static bool
24164 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24165 struct font *font, bool width_p, int *align_to)
24166 {
24167 double pixels;
24168
24169 # define OK_PIXELS(val) (*res = (val), true)
24170 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24171
24172 if (NILP (prop))
24173 return OK_PIXELS (0);
24174
24175 eassert (FRAME_LIVE_P (it->f));
24176
24177 if (SYMBOLP (prop))
24178 {
24179 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24180 {
24181 char *unit = SSDATA (SYMBOL_NAME (prop));
24182
24183 if (unit[0] == 'i' && unit[1] == 'n')
24184 pixels = 1.0;
24185 else if (unit[0] == 'm' && unit[1] == 'm')
24186 pixels = 25.4;
24187 else if (unit[0] == 'c' && unit[1] == 'm')
24188 pixels = 2.54;
24189 else
24190 pixels = 0;
24191 if (pixels > 0)
24192 {
24193 double ppi = (width_p ? FRAME_RES_X (it->f)
24194 : FRAME_RES_Y (it->f));
24195
24196 if (ppi > 0)
24197 return OK_PIXELS (ppi / pixels);
24198 return false;
24199 }
24200 }
24201
24202 #ifdef HAVE_WINDOW_SYSTEM
24203 if (EQ (prop, Qheight))
24204 return OK_PIXELS (font
24205 ? normal_char_height (font, -1)
24206 : FRAME_LINE_HEIGHT (it->f));
24207 if (EQ (prop, Qwidth))
24208 return OK_PIXELS (font
24209 ? FONT_WIDTH (font)
24210 : FRAME_COLUMN_WIDTH (it->f));
24211 #else
24212 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24213 return OK_PIXELS (1);
24214 #endif
24215
24216 if (EQ (prop, Qtext))
24217 return OK_PIXELS (width_p
24218 ? window_box_width (it->w, TEXT_AREA)
24219 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24220
24221 if (align_to && *align_to < 0)
24222 {
24223 *res = 0;
24224 if (EQ (prop, Qleft))
24225 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24226 if (EQ (prop, Qright))
24227 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24228 if (EQ (prop, Qcenter))
24229 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24230 + window_box_width (it->w, TEXT_AREA) / 2);
24231 if (EQ (prop, Qleft_fringe))
24232 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24233 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24234 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24235 if (EQ (prop, Qright_fringe))
24236 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24237 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24238 : window_box_right_offset (it->w, TEXT_AREA));
24239 if (EQ (prop, Qleft_margin))
24240 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24241 if (EQ (prop, Qright_margin))
24242 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24243 if (EQ (prop, Qscroll_bar))
24244 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24245 ? 0
24246 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24247 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24248 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24249 : 0)));
24250 }
24251 else
24252 {
24253 if (EQ (prop, Qleft_fringe))
24254 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24255 if (EQ (prop, Qright_fringe))
24256 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24257 if (EQ (prop, Qleft_margin))
24258 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24259 if (EQ (prop, Qright_margin))
24260 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24261 if (EQ (prop, Qscroll_bar))
24262 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24263 }
24264
24265 prop = buffer_local_value (prop, it->w->contents);
24266 if (EQ (prop, Qunbound))
24267 prop = Qnil;
24268 }
24269
24270 if (NUMBERP (prop))
24271 {
24272 int base_unit = (width_p
24273 ? FRAME_COLUMN_WIDTH (it->f)
24274 : FRAME_LINE_HEIGHT (it->f));
24275 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24276 }
24277
24278 if (CONSP (prop))
24279 {
24280 Lisp_Object car = XCAR (prop);
24281 Lisp_Object cdr = XCDR (prop);
24282
24283 if (SYMBOLP (car))
24284 {
24285 #ifdef HAVE_WINDOW_SYSTEM
24286 if (FRAME_WINDOW_P (it->f)
24287 && valid_image_p (prop))
24288 {
24289 ptrdiff_t id = lookup_image (it->f, prop);
24290 struct image *img = IMAGE_FROM_ID (it->f, id);
24291
24292 return OK_PIXELS (width_p ? img->width : img->height);
24293 }
24294 #endif
24295 if (EQ (car, Qplus) || EQ (car, Qminus))
24296 {
24297 bool first = true;
24298 double px;
24299
24300 pixels = 0;
24301 while (CONSP (cdr))
24302 {
24303 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24304 font, width_p, align_to))
24305 return false;
24306 if (first)
24307 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24308 else
24309 pixels += px;
24310 cdr = XCDR (cdr);
24311 }
24312 if (EQ (car, Qminus))
24313 pixels = -pixels;
24314 return OK_PIXELS (pixels);
24315 }
24316
24317 car = buffer_local_value (car, it->w->contents);
24318 if (EQ (car, Qunbound))
24319 car = Qnil;
24320 }
24321
24322 if (NUMBERP (car))
24323 {
24324 double fact;
24325 pixels = XFLOATINT (car);
24326 if (NILP (cdr))
24327 return OK_PIXELS (pixels);
24328 if (calc_pixel_width_or_height (&fact, it, cdr,
24329 font, width_p, align_to))
24330 return OK_PIXELS (pixels * fact);
24331 return false;
24332 }
24333
24334 return false;
24335 }
24336
24337 return false;
24338 }
24339
24340 void
24341 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24342 {
24343 #ifdef HAVE_WINDOW_SYSTEM
24344 normal_char_ascent_descent (font, -1, ascent, descent);
24345 #else
24346 *ascent = 1;
24347 *descent = 0;
24348 #endif
24349 }
24350
24351 \f
24352 /***********************************************************************
24353 Glyph Display
24354 ***********************************************************************/
24355
24356 #ifdef HAVE_WINDOW_SYSTEM
24357
24358 #ifdef GLYPH_DEBUG
24359
24360 void
24361 dump_glyph_string (struct glyph_string *s)
24362 {
24363 fprintf (stderr, "glyph string\n");
24364 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24365 s->x, s->y, s->width, s->height);
24366 fprintf (stderr, " ybase = %d\n", s->ybase);
24367 fprintf (stderr, " hl = %d\n", s->hl);
24368 fprintf (stderr, " left overhang = %d, right = %d\n",
24369 s->left_overhang, s->right_overhang);
24370 fprintf (stderr, " nchars = %d\n", s->nchars);
24371 fprintf (stderr, " extends to end of line = %d\n",
24372 s->extends_to_end_of_line_p);
24373 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24374 fprintf (stderr, " bg width = %d\n", s->background_width);
24375 }
24376
24377 #endif /* GLYPH_DEBUG */
24378
24379 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24380 of XChar2b structures for S; it can't be allocated in
24381 init_glyph_string because it must be allocated via `alloca'. W
24382 is the window on which S is drawn. ROW and AREA are the glyph row
24383 and area within the row from which S is constructed. START is the
24384 index of the first glyph structure covered by S. HL is a
24385 face-override for drawing S. */
24386
24387 #ifdef HAVE_NTGUI
24388 #define OPTIONAL_HDC(hdc) HDC hdc,
24389 #define DECLARE_HDC(hdc) HDC hdc;
24390 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24391 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24392 #endif
24393
24394 #ifndef OPTIONAL_HDC
24395 #define OPTIONAL_HDC(hdc)
24396 #define DECLARE_HDC(hdc)
24397 #define ALLOCATE_HDC(hdc, f)
24398 #define RELEASE_HDC(hdc, f)
24399 #endif
24400
24401 static void
24402 init_glyph_string (struct glyph_string *s,
24403 OPTIONAL_HDC (hdc)
24404 XChar2b *char2b, struct window *w, struct glyph_row *row,
24405 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24406 {
24407 memset (s, 0, sizeof *s);
24408 s->w = w;
24409 s->f = XFRAME (w->frame);
24410 #ifdef HAVE_NTGUI
24411 s->hdc = hdc;
24412 #endif
24413 s->display = FRAME_X_DISPLAY (s->f);
24414 s->window = FRAME_X_WINDOW (s->f);
24415 s->char2b = char2b;
24416 s->hl = hl;
24417 s->row = row;
24418 s->area = area;
24419 s->first_glyph = row->glyphs[area] + start;
24420 s->height = row->height;
24421 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24422 s->ybase = s->y + row->ascent;
24423 }
24424
24425
24426 /* Append the list of glyph strings with head H and tail T to the list
24427 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24428
24429 static void
24430 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24431 struct glyph_string *h, struct glyph_string *t)
24432 {
24433 if (h)
24434 {
24435 if (*head)
24436 (*tail)->next = h;
24437 else
24438 *head = h;
24439 h->prev = *tail;
24440 *tail = t;
24441 }
24442 }
24443
24444
24445 /* Prepend the list of glyph strings with head H and tail T to the
24446 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24447 result. */
24448
24449 static void
24450 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24451 struct glyph_string *h, struct glyph_string *t)
24452 {
24453 if (h)
24454 {
24455 if (*head)
24456 (*head)->prev = t;
24457 else
24458 *tail = t;
24459 t->next = *head;
24460 *head = h;
24461 }
24462 }
24463
24464
24465 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24466 Set *HEAD and *TAIL to the resulting list. */
24467
24468 static void
24469 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24470 struct glyph_string *s)
24471 {
24472 s->next = s->prev = NULL;
24473 append_glyph_string_lists (head, tail, s, s);
24474 }
24475
24476
24477 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24478 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24479 make sure that X resources for the face returned are allocated.
24480 Value is a pointer to a realized face that is ready for display if
24481 DISPLAY_P. */
24482
24483 static struct face *
24484 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24485 XChar2b *char2b, bool display_p)
24486 {
24487 struct face *face = FACE_FROM_ID (f, face_id);
24488 unsigned code = 0;
24489
24490 if (face->font)
24491 {
24492 code = face->font->driver->encode_char (face->font, c);
24493
24494 if (code == FONT_INVALID_CODE)
24495 code = 0;
24496 }
24497 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24498
24499 /* Make sure X resources of the face are allocated. */
24500 #ifdef HAVE_X_WINDOWS
24501 if (display_p)
24502 #endif
24503 {
24504 eassert (face != NULL);
24505 prepare_face_for_display (f, face);
24506 }
24507
24508 return face;
24509 }
24510
24511
24512 /* Get face and two-byte form of character glyph GLYPH on frame F.
24513 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24514 a pointer to a realized face that is ready for display. */
24515
24516 static struct face *
24517 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24518 XChar2b *char2b)
24519 {
24520 struct face *face;
24521 unsigned code = 0;
24522
24523 eassert (glyph->type == CHAR_GLYPH);
24524 face = FACE_FROM_ID (f, glyph->face_id);
24525
24526 /* Make sure X resources of the face are allocated. */
24527 eassert (face != NULL);
24528 prepare_face_for_display (f, face);
24529
24530 if (face->font)
24531 {
24532 if (CHAR_BYTE8_P (glyph->u.ch))
24533 code = CHAR_TO_BYTE8 (glyph->u.ch);
24534 else
24535 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24536
24537 if (code == FONT_INVALID_CODE)
24538 code = 0;
24539 }
24540
24541 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24542 return face;
24543 }
24544
24545
24546 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24547 Return true iff FONT has a glyph for C. */
24548
24549 static bool
24550 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24551 {
24552 unsigned code;
24553
24554 if (CHAR_BYTE8_P (c))
24555 code = CHAR_TO_BYTE8 (c);
24556 else
24557 code = font->driver->encode_char (font, c);
24558
24559 if (code == FONT_INVALID_CODE)
24560 return false;
24561 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24562 return true;
24563 }
24564
24565
24566 /* Fill glyph string S with composition components specified by S->cmp.
24567
24568 BASE_FACE is the base face of the composition.
24569 S->cmp_from is the index of the first component for S.
24570
24571 OVERLAPS non-zero means S should draw the foreground only, and use
24572 its physical height for clipping. See also draw_glyphs.
24573
24574 Value is the index of a component not in S. */
24575
24576 static int
24577 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24578 int overlaps)
24579 {
24580 int i;
24581 /* For all glyphs of this composition, starting at the offset
24582 S->cmp_from, until we reach the end of the definition or encounter a
24583 glyph that requires the different face, add it to S. */
24584 struct face *face;
24585
24586 eassert (s);
24587
24588 s->for_overlaps = overlaps;
24589 s->face = NULL;
24590 s->font = NULL;
24591 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24592 {
24593 int c = COMPOSITION_GLYPH (s->cmp, i);
24594
24595 /* TAB in a composition means display glyphs with padding space
24596 on the left or right. */
24597 if (c != '\t')
24598 {
24599 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24600 -1, Qnil);
24601
24602 face = get_char_face_and_encoding (s->f, c, face_id,
24603 s->char2b + i, true);
24604 if (face)
24605 {
24606 if (! s->face)
24607 {
24608 s->face = face;
24609 s->font = s->face->font;
24610 }
24611 else if (s->face != face)
24612 break;
24613 }
24614 }
24615 ++s->nchars;
24616 }
24617 s->cmp_to = i;
24618
24619 if (s->face == NULL)
24620 {
24621 s->face = base_face->ascii_face;
24622 s->font = s->face->font;
24623 }
24624
24625 /* All glyph strings for the same composition has the same width,
24626 i.e. the width set for the first component of the composition. */
24627 s->width = s->first_glyph->pixel_width;
24628
24629 /* If the specified font could not be loaded, use the frame's
24630 default font, but record the fact that we couldn't load it in
24631 the glyph string so that we can draw rectangles for the
24632 characters of the glyph string. */
24633 if (s->font == NULL)
24634 {
24635 s->font_not_found_p = true;
24636 s->font = FRAME_FONT (s->f);
24637 }
24638
24639 /* Adjust base line for subscript/superscript text. */
24640 s->ybase += s->first_glyph->voffset;
24641
24642 return s->cmp_to;
24643 }
24644
24645 static int
24646 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24647 int start, int end, int overlaps)
24648 {
24649 struct glyph *glyph, *last;
24650 Lisp_Object lgstring;
24651 int i;
24652
24653 s->for_overlaps = overlaps;
24654 glyph = s->row->glyphs[s->area] + start;
24655 last = s->row->glyphs[s->area] + end;
24656 s->cmp_id = glyph->u.cmp.id;
24657 s->cmp_from = glyph->slice.cmp.from;
24658 s->cmp_to = glyph->slice.cmp.to + 1;
24659 s->face = FACE_FROM_ID (s->f, face_id);
24660 lgstring = composition_gstring_from_id (s->cmp_id);
24661 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24662 glyph++;
24663 while (glyph < last
24664 && glyph->u.cmp.automatic
24665 && glyph->u.cmp.id == s->cmp_id
24666 && s->cmp_to == glyph->slice.cmp.from)
24667 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24668
24669 for (i = s->cmp_from; i < s->cmp_to; i++)
24670 {
24671 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24672 unsigned code = LGLYPH_CODE (lglyph);
24673
24674 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24675 }
24676 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24677 return glyph - s->row->glyphs[s->area];
24678 }
24679
24680
24681 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24682 See the comment of fill_glyph_string for arguments.
24683 Value is the index of the first glyph not in S. */
24684
24685
24686 static int
24687 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24688 int start, int end, int overlaps)
24689 {
24690 struct glyph *glyph, *last;
24691 int voffset;
24692
24693 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24694 s->for_overlaps = overlaps;
24695 glyph = s->row->glyphs[s->area] + start;
24696 last = s->row->glyphs[s->area] + end;
24697 voffset = glyph->voffset;
24698 s->face = FACE_FROM_ID (s->f, face_id);
24699 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24700 s->nchars = 1;
24701 s->width = glyph->pixel_width;
24702 glyph++;
24703 while (glyph < last
24704 && glyph->type == GLYPHLESS_GLYPH
24705 && glyph->voffset == voffset
24706 && glyph->face_id == face_id)
24707 {
24708 s->nchars++;
24709 s->width += glyph->pixel_width;
24710 glyph++;
24711 }
24712 s->ybase += voffset;
24713 return glyph - s->row->glyphs[s->area];
24714 }
24715
24716
24717 /* Fill glyph string S from a sequence of character glyphs.
24718
24719 FACE_ID is the face id of the string. START is the index of the
24720 first glyph to consider, END is the index of the last + 1.
24721 OVERLAPS non-zero means S should draw the foreground only, and use
24722 its physical height for clipping. See also draw_glyphs.
24723
24724 Value is the index of the first glyph not in S. */
24725
24726 static int
24727 fill_glyph_string (struct glyph_string *s, int face_id,
24728 int start, int end, int overlaps)
24729 {
24730 struct glyph *glyph, *last;
24731 int voffset;
24732 bool glyph_not_available_p;
24733
24734 eassert (s->f == XFRAME (s->w->frame));
24735 eassert (s->nchars == 0);
24736 eassert (start >= 0 && end > start);
24737
24738 s->for_overlaps = overlaps;
24739 glyph = s->row->glyphs[s->area] + start;
24740 last = s->row->glyphs[s->area] + end;
24741 voffset = glyph->voffset;
24742 s->padding_p = glyph->padding_p;
24743 glyph_not_available_p = glyph->glyph_not_available_p;
24744
24745 while (glyph < last
24746 && glyph->type == CHAR_GLYPH
24747 && glyph->voffset == voffset
24748 /* Same face id implies same font, nowadays. */
24749 && glyph->face_id == face_id
24750 && glyph->glyph_not_available_p == glyph_not_available_p)
24751 {
24752 s->face = get_glyph_face_and_encoding (s->f, glyph,
24753 s->char2b + s->nchars);
24754 ++s->nchars;
24755 eassert (s->nchars <= end - start);
24756 s->width += glyph->pixel_width;
24757 if (glyph++->padding_p != s->padding_p)
24758 break;
24759 }
24760
24761 s->font = s->face->font;
24762
24763 /* If the specified font could not be loaded, use the frame's font,
24764 but record the fact that we couldn't load it in
24765 S->font_not_found_p so that we can draw rectangles for the
24766 characters of the glyph string. */
24767 if (s->font == NULL || glyph_not_available_p)
24768 {
24769 s->font_not_found_p = true;
24770 s->font = FRAME_FONT (s->f);
24771 }
24772
24773 /* Adjust base line for subscript/superscript text. */
24774 s->ybase += voffset;
24775
24776 eassert (s->face && s->face->gc);
24777 return glyph - s->row->glyphs[s->area];
24778 }
24779
24780
24781 /* Fill glyph string S from image glyph S->first_glyph. */
24782
24783 static void
24784 fill_image_glyph_string (struct glyph_string *s)
24785 {
24786 eassert (s->first_glyph->type == IMAGE_GLYPH);
24787 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24788 eassert (s->img);
24789 s->slice = s->first_glyph->slice.img;
24790 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24791 s->font = s->face->font;
24792 s->width = s->first_glyph->pixel_width;
24793
24794 /* Adjust base line for subscript/superscript text. */
24795 s->ybase += s->first_glyph->voffset;
24796 }
24797
24798
24799 /* Fill glyph string S from a sequence of stretch glyphs.
24800
24801 START is the index of the first glyph to consider,
24802 END is the index of the last + 1.
24803
24804 Value is the index of the first glyph not in S. */
24805
24806 static int
24807 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24808 {
24809 struct glyph *glyph, *last;
24810 int voffset, face_id;
24811
24812 eassert (s->first_glyph->type == STRETCH_GLYPH);
24813
24814 glyph = s->row->glyphs[s->area] + start;
24815 last = s->row->glyphs[s->area] + end;
24816 face_id = glyph->face_id;
24817 s->face = FACE_FROM_ID (s->f, face_id);
24818 s->font = s->face->font;
24819 s->width = glyph->pixel_width;
24820 s->nchars = 1;
24821 voffset = glyph->voffset;
24822
24823 for (++glyph;
24824 (glyph < last
24825 && glyph->type == STRETCH_GLYPH
24826 && glyph->voffset == voffset
24827 && glyph->face_id == face_id);
24828 ++glyph)
24829 s->width += glyph->pixel_width;
24830
24831 /* Adjust base line for subscript/superscript text. */
24832 s->ybase += voffset;
24833
24834 /* The case that face->gc == 0 is handled when drawing the glyph
24835 string by calling prepare_face_for_display. */
24836 eassert (s->face);
24837 return glyph - s->row->glyphs[s->area];
24838 }
24839
24840 static struct font_metrics *
24841 get_per_char_metric (struct font *font, XChar2b *char2b)
24842 {
24843 static struct font_metrics metrics;
24844 unsigned code;
24845
24846 if (! font)
24847 return NULL;
24848 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24849 if (code == FONT_INVALID_CODE)
24850 return NULL;
24851 font->driver->text_extents (font, &code, 1, &metrics);
24852 return &metrics;
24853 }
24854
24855 /* A subroutine that computes "normal" values of ASCENT and DESCENT
24856 for FONT. Values are taken from font-global ones, except for fonts
24857 that claim preposterously large values, but whose glyphs actually
24858 have reasonable dimensions. C is the character to use for metrics
24859 if the font-global values are too large; if C is negative, the
24860 function selects a default character. */
24861 static void
24862 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
24863 {
24864 *ascent = FONT_BASE (font);
24865 *descent = FONT_DESCENT (font);
24866
24867 if (FONT_TOO_HIGH (font))
24868 {
24869 XChar2b char2b;
24870
24871 /* Get metrics of C, defaulting to a reasonably sized ASCII
24872 character. */
24873 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
24874 {
24875 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
24876
24877 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
24878 {
24879 /* We add 1 pixel to character dimensions as heuristics
24880 that produces nicer display, e.g. when the face has
24881 the box attribute. */
24882 *ascent = pcm->ascent + 1;
24883 *descent = pcm->descent + 1;
24884 }
24885 }
24886 }
24887 }
24888
24889 /* A subroutine that computes a reasonable "normal character height"
24890 for fonts that claim preposterously large vertical dimensions, but
24891 whose glyphs are actually reasonably sized. C is the character
24892 whose metrics to use for those fonts, or -1 for default
24893 character. */
24894 static int
24895 normal_char_height (struct font *font, int c)
24896 {
24897 int ascent, descent;
24898
24899 normal_char_ascent_descent (font, c, &ascent, &descent);
24900
24901 return ascent + descent;
24902 }
24903
24904 /* EXPORT for RIF:
24905 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24906 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24907 assumed to be zero. */
24908
24909 void
24910 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24911 {
24912 *left = *right = 0;
24913
24914 if (glyph->type == CHAR_GLYPH)
24915 {
24916 XChar2b char2b;
24917 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
24918 if (face->font)
24919 {
24920 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
24921 if (pcm)
24922 {
24923 if (pcm->rbearing > pcm->width)
24924 *right = pcm->rbearing - pcm->width;
24925 if (pcm->lbearing < 0)
24926 *left = -pcm->lbearing;
24927 }
24928 }
24929 }
24930 else if (glyph->type == COMPOSITE_GLYPH)
24931 {
24932 if (! glyph->u.cmp.automatic)
24933 {
24934 struct composition *cmp = composition_table[glyph->u.cmp.id];
24935
24936 if (cmp->rbearing > cmp->pixel_width)
24937 *right = cmp->rbearing - cmp->pixel_width;
24938 if (cmp->lbearing < 0)
24939 *left = - cmp->lbearing;
24940 }
24941 else
24942 {
24943 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24944 struct font_metrics metrics;
24945
24946 composition_gstring_width (gstring, glyph->slice.cmp.from,
24947 glyph->slice.cmp.to + 1, &metrics);
24948 if (metrics.rbearing > metrics.width)
24949 *right = metrics.rbearing - metrics.width;
24950 if (metrics.lbearing < 0)
24951 *left = - metrics.lbearing;
24952 }
24953 }
24954 }
24955
24956
24957 /* Return the index of the first glyph preceding glyph string S that
24958 is overwritten by S because of S's left overhang. Value is -1
24959 if no glyphs are overwritten. */
24960
24961 static int
24962 left_overwritten (struct glyph_string *s)
24963 {
24964 int k;
24965
24966 if (s->left_overhang)
24967 {
24968 int x = 0, i;
24969 struct glyph *glyphs = s->row->glyphs[s->area];
24970 int first = s->first_glyph - glyphs;
24971
24972 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24973 x -= glyphs[i].pixel_width;
24974
24975 k = i + 1;
24976 }
24977 else
24978 k = -1;
24979
24980 return k;
24981 }
24982
24983
24984 /* Return the index of the first glyph preceding glyph string S that
24985 is overwriting S because of its right overhang. Value is -1 if no
24986 glyph in front of S overwrites S. */
24987
24988 static int
24989 left_overwriting (struct glyph_string *s)
24990 {
24991 int i, k, x;
24992 struct glyph *glyphs = s->row->glyphs[s->area];
24993 int first = s->first_glyph - glyphs;
24994
24995 k = -1;
24996 x = 0;
24997 for (i = first - 1; i >= 0; --i)
24998 {
24999 int left, right;
25000 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25001 if (x + right > 0)
25002 k = i;
25003 x -= glyphs[i].pixel_width;
25004 }
25005
25006 return k;
25007 }
25008
25009
25010 /* Return the index of the last glyph following glyph string S that is
25011 overwritten by S because of S's right overhang. Value is -1 if
25012 no such glyph is found. */
25013
25014 static int
25015 right_overwritten (struct glyph_string *s)
25016 {
25017 int k = -1;
25018
25019 if (s->right_overhang)
25020 {
25021 int x = 0, i;
25022 struct glyph *glyphs = s->row->glyphs[s->area];
25023 int first = (s->first_glyph - glyphs
25024 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25025 int end = s->row->used[s->area];
25026
25027 for (i = first; i < end && s->right_overhang > x; ++i)
25028 x += glyphs[i].pixel_width;
25029
25030 k = i;
25031 }
25032
25033 return k;
25034 }
25035
25036
25037 /* Return the index of the last glyph following glyph string S that
25038 overwrites S because of its left overhang. Value is negative
25039 if no such glyph is found. */
25040
25041 static int
25042 right_overwriting (struct glyph_string *s)
25043 {
25044 int i, k, x;
25045 int end = s->row->used[s->area];
25046 struct glyph *glyphs = s->row->glyphs[s->area];
25047 int first = (s->first_glyph - glyphs
25048 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25049
25050 k = -1;
25051 x = 0;
25052 for (i = first; i < end; ++i)
25053 {
25054 int left, right;
25055 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25056 if (x - left < 0)
25057 k = i;
25058 x += glyphs[i].pixel_width;
25059 }
25060
25061 return k;
25062 }
25063
25064
25065 /* Set background width of glyph string S. START is the index of the
25066 first glyph following S. LAST_X is the right-most x-position + 1
25067 in the drawing area. */
25068
25069 static void
25070 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
25071 {
25072 /* If the face of this glyph string has to be drawn to the end of
25073 the drawing area, set S->extends_to_end_of_line_p. */
25074
25075 if (start == s->row->used[s->area]
25076 && ((s->row->fill_line_p
25077 && (s->hl == DRAW_NORMAL_TEXT
25078 || s->hl == DRAW_IMAGE_RAISED
25079 || s->hl == DRAW_IMAGE_SUNKEN))
25080 || s->hl == DRAW_MOUSE_FACE))
25081 s->extends_to_end_of_line_p = true;
25082
25083 /* If S extends its face to the end of the line, set its
25084 background_width to the distance to the right edge of the drawing
25085 area. */
25086 if (s->extends_to_end_of_line_p)
25087 s->background_width = last_x - s->x + 1;
25088 else
25089 s->background_width = s->width;
25090 }
25091
25092
25093 /* Compute overhangs and x-positions for glyph string S and its
25094 predecessors, or successors. X is the starting x-position for S.
25095 BACKWARD_P means process predecessors. */
25096
25097 static void
25098 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
25099 {
25100 if (backward_p)
25101 {
25102 while (s)
25103 {
25104 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25105 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25106 x -= s->width;
25107 s->x = x;
25108 s = s->prev;
25109 }
25110 }
25111 else
25112 {
25113 while (s)
25114 {
25115 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25116 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25117 s->x = x;
25118 x += s->width;
25119 s = s->next;
25120 }
25121 }
25122 }
25123
25124
25125
25126 /* The following macros are only called from draw_glyphs below.
25127 They reference the following parameters of that function directly:
25128 `w', `row', `area', and `overlap_p'
25129 as well as the following local variables:
25130 `s', `f', and `hdc' (in W32) */
25131
25132 #ifdef HAVE_NTGUI
25133 /* On W32, silently add local `hdc' variable to argument list of
25134 init_glyph_string. */
25135 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25136 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25137 #else
25138 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25139 init_glyph_string (s, char2b, w, row, area, start, hl)
25140 #endif
25141
25142 /* Add a glyph string for a stretch glyph to the list of strings
25143 between HEAD and TAIL. START is the index of the stretch glyph in
25144 row area AREA of glyph row ROW. END is the index of the last glyph
25145 in that glyph row area. X is the current output position assigned
25146 to the new glyph string constructed. HL overrides that face of the
25147 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25148 is the right-most x-position of the drawing area. */
25149
25150 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25151 and below -- keep them on one line. */
25152 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25153 do \
25154 { \
25155 s = alloca (sizeof *s); \
25156 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25157 START = fill_stretch_glyph_string (s, START, END); \
25158 append_glyph_string (&HEAD, &TAIL, s); \
25159 s->x = (X); \
25160 } \
25161 while (false)
25162
25163
25164 /* Add a glyph string for an image glyph to the list of strings
25165 between HEAD and TAIL. START is the index of the image glyph in
25166 row area AREA of glyph row ROW. END is the index of the last glyph
25167 in that glyph row area. X is the current output position assigned
25168 to the new glyph string constructed. HL overrides that face of the
25169 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25170 is the right-most x-position of the drawing area. */
25171
25172 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25173 do \
25174 { \
25175 s = alloca (sizeof *s); \
25176 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25177 fill_image_glyph_string (s); \
25178 append_glyph_string (&HEAD, &TAIL, s); \
25179 ++START; \
25180 s->x = (X); \
25181 } \
25182 while (false)
25183
25184
25185 /* Add a glyph string for a sequence of character glyphs to the list
25186 of strings between HEAD and TAIL. START is the index of the first
25187 glyph in row area AREA of glyph row ROW that is part of the new
25188 glyph string. END is the index of the last glyph in that glyph row
25189 area. X is the current output position assigned to the new glyph
25190 string constructed. HL overrides that face of the glyph; e.g. it
25191 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25192 right-most x-position of the drawing area. */
25193
25194 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25195 do \
25196 { \
25197 int face_id; \
25198 XChar2b *char2b; \
25199 \
25200 face_id = (row)->glyphs[area][START].face_id; \
25201 \
25202 s = alloca (sizeof *s); \
25203 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25204 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25205 append_glyph_string (&HEAD, &TAIL, s); \
25206 s->x = (X); \
25207 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25208 } \
25209 while (false)
25210
25211
25212 /* Add a glyph string for a composite sequence to the list of strings
25213 between HEAD and TAIL. START is the index of the first glyph in
25214 row area AREA of glyph row ROW that is part of the new glyph
25215 string. END is the index of the last glyph in that glyph row area.
25216 X is the current output position assigned to the new glyph string
25217 constructed. HL overrides that face of the glyph; e.g. it is
25218 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25219 x-position of the drawing area. */
25220
25221 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25222 do { \
25223 int face_id = (row)->glyphs[area][START].face_id; \
25224 struct face *base_face = FACE_FROM_ID (f, face_id); \
25225 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25226 struct composition *cmp = composition_table[cmp_id]; \
25227 XChar2b *char2b; \
25228 struct glyph_string *first_s = NULL; \
25229 int n; \
25230 \
25231 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25232 \
25233 /* Make glyph_strings for each glyph sequence that is drawable by \
25234 the same face, and append them to HEAD/TAIL. */ \
25235 for (n = 0; n < cmp->glyph_len;) \
25236 { \
25237 s = alloca (sizeof *s); \
25238 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25239 append_glyph_string (&(HEAD), &(TAIL), s); \
25240 s->cmp = cmp; \
25241 s->cmp_from = n; \
25242 s->x = (X); \
25243 if (n == 0) \
25244 first_s = s; \
25245 n = fill_composite_glyph_string (s, base_face, overlaps); \
25246 } \
25247 \
25248 ++START; \
25249 s = first_s; \
25250 } while (false)
25251
25252
25253 /* Add a glyph string for a glyph-string sequence to the list of strings
25254 between HEAD and TAIL. */
25255
25256 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25257 do { \
25258 int face_id; \
25259 XChar2b *char2b; \
25260 Lisp_Object gstring; \
25261 \
25262 face_id = (row)->glyphs[area][START].face_id; \
25263 gstring = (composition_gstring_from_id \
25264 ((row)->glyphs[area][START].u.cmp.id)); \
25265 s = alloca (sizeof *s); \
25266 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25267 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25268 append_glyph_string (&(HEAD), &(TAIL), s); \
25269 s->x = (X); \
25270 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25271 } while (false)
25272
25273
25274 /* Add a glyph string for a sequence of glyphless character's glyphs
25275 to the list of strings between HEAD and TAIL. The meanings of
25276 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25277
25278 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25279 do \
25280 { \
25281 int face_id; \
25282 \
25283 face_id = (row)->glyphs[area][START].face_id; \
25284 \
25285 s = alloca (sizeof *s); \
25286 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25287 append_glyph_string (&HEAD, &TAIL, s); \
25288 s->x = (X); \
25289 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25290 overlaps); \
25291 } \
25292 while (false)
25293
25294
25295 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25296 of AREA of glyph row ROW on window W between indices START and END.
25297 HL overrides the face for drawing glyph strings, e.g. it is
25298 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25299 x-positions of the drawing area.
25300
25301 This is an ugly monster macro construct because we must use alloca
25302 to allocate glyph strings (because draw_glyphs can be called
25303 asynchronously). */
25304
25305 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25306 do \
25307 { \
25308 HEAD = TAIL = NULL; \
25309 while (START < END) \
25310 { \
25311 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25312 switch (first_glyph->type) \
25313 { \
25314 case CHAR_GLYPH: \
25315 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25316 HL, X, LAST_X); \
25317 break; \
25318 \
25319 case COMPOSITE_GLYPH: \
25320 if (first_glyph->u.cmp.automatic) \
25321 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25322 HL, X, LAST_X); \
25323 else \
25324 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25325 HL, X, LAST_X); \
25326 break; \
25327 \
25328 case STRETCH_GLYPH: \
25329 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25330 HL, X, LAST_X); \
25331 break; \
25332 \
25333 case IMAGE_GLYPH: \
25334 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25335 HL, X, LAST_X); \
25336 break; \
25337 \
25338 case GLYPHLESS_GLYPH: \
25339 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25340 HL, X, LAST_X); \
25341 break; \
25342 \
25343 default: \
25344 emacs_abort (); \
25345 } \
25346 \
25347 if (s) \
25348 { \
25349 set_glyph_string_background_width (s, START, LAST_X); \
25350 (X) += s->width; \
25351 } \
25352 } \
25353 } while (false)
25354
25355
25356 /* Draw glyphs between START and END in AREA of ROW on window W,
25357 starting at x-position X. X is relative to AREA in W. HL is a
25358 face-override with the following meaning:
25359
25360 DRAW_NORMAL_TEXT draw normally
25361 DRAW_CURSOR draw in cursor face
25362 DRAW_MOUSE_FACE draw in mouse face.
25363 DRAW_INVERSE_VIDEO draw in mode line face
25364 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25365 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25366
25367 If OVERLAPS is non-zero, draw only the foreground of characters and
25368 clip to the physical height of ROW. Non-zero value also defines
25369 the overlapping part to be drawn:
25370
25371 OVERLAPS_PRED overlap with preceding rows
25372 OVERLAPS_SUCC overlap with succeeding rows
25373 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25374 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25375
25376 Value is the x-position reached, relative to AREA of W. */
25377
25378 static int
25379 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25380 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25381 enum draw_glyphs_face hl, int overlaps)
25382 {
25383 struct glyph_string *head, *tail;
25384 struct glyph_string *s;
25385 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25386 int i, j, x_reached, last_x, area_left = 0;
25387 struct frame *f = XFRAME (WINDOW_FRAME (w));
25388 DECLARE_HDC (hdc);
25389
25390 ALLOCATE_HDC (hdc, f);
25391
25392 /* Let's rather be paranoid than getting a SEGV. */
25393 end = min (end, row->used[area]);
25394 start = clip_to_bounds (0, start, end);
25395
25396 /* Translate X to frame coordinates. Set last_x to the right
25397 end of the drawing area. */
25398 if (row->full_width_p)
25399 {
25400 /* X is relative to the left edge of W, without scroll bars
25401 or fringes. */
25402 area_left = WINDOW_LEFT_EDGE_X (w);
25403 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25404 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25405 }
25406 else
25407 {
25408 area_left = window_box_left (w, area);
25409 last_x = area_left + window_box_width (w, area);
25410 }
25411 x += area_left;
25412
25413 /* Build a doubly-linked list of glyph_string structures between
25414 head and tail from what we have to draw. Note that the macro
25415 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25416 the reason we use a separate variable `i'. */
25417 i = start;
25418 USE_SAFE_ALLOCA;
25419 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25420 if (tail)
25421 x_reached = tail->x + tail->background_width;
25422 else
25423 x_reached = x;
25424
25425 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25426 the row, redraw some glyphs in front or following the glyph
25427 strings built above. */
25428 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25429 {
25430 struct glyph_string *h, *t;
25431 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25432 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25433 bool check_mouse_face = false;
25434 int dummy_x = 0;
25435
25436 /* If mouse highlighting is on, we may need to draw adjacent
25437 glyphs using mouse-face highlighting. */
25438 if (area == TEXT_AREA && row->mouse_face_p
25439 && hlinfo->mouse_face_beg_row >= 0
25440 && hlinfo->mouse_face_end_row >= 0)
25441 {
25442 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25443
25444 if (row_vpos >= hlinfo->mouse_face_beg_row
25445 && row_vpos <= hlinfo->mouse_face_end_row)
25446 {
25447 check_mouse_face = true;
25448 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25449 ? hlinfo->mouse_face_beg_col : 0;
25450 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25451 ? hlinfo->mouse_face_end_col
25452 : row->used[TEXT_AREA];
25453 }
25454 }
25455
25456 /* Compute overhangs for all glyph strings. */
25457 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25458 for (s = head; s; s = s->next)
25459 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25460
25461 /* Prepend glyph strings for glyphs in front of the first glyph
25462 string that are overwritten because of the first glyph
25463 string's left overhang. The background of all strings
25464 prepended must be drawn because the first glyph string
25465 draws over it. */
25466 i = left_overwritten (head);
25467 if (i >= 0)
25468 {
25469 enum draw_glyphs_face overlap_hl;
25470
25471 /* If this row contains mouse highlighting, attempt to draw
25472 the overlapped glyphs with the correct highlight. This
25473 code fails if the overlap encompasses more than one glyph
25474 and mouse-highlight spans only some of these glyphs.
25475 However, making it work perfectly involves a lot more
25476 code, and I don't know if the pathological case occurs in
25477 practice, so we'll stick to this for now. --- cyd */
25478 if (check_mouse_face
25479 && mouse_beg_col < start && mouse_end_col > i)
25480 overlap_hl = DRAW_MOUSE_FACE;
25481 else
25482 overlap_hl = DRAW_NORMAL_TEXT;
25483
25484 if (hl != overlap_hl)
25485 clip_head = head;
25486 j = i;
25487 BUILD_GLYPH_STRINGS (j, start, h, t,
25488 overlap_hl, dummy_x, last_x);
25489 start = i;
25490 compute_overhangs_and_x (t, head->x, true);
25491 prepend_glyph_string_lists (&head, &tail, h, t);
25492 if (clip_head == NULL)
25493 clip_head = head;
25494 }
25495
25496 /* Prepend glyph strings for glyphs in front of the first glyph
25497 string that overwrite that glyph string because of their
25498 right overhang. For these strings, only the foreground must
25499 be drawn, because it draws over the glyph string at `head'.
25500 The background must not be drawn because this would overwrite
25501 right overhangs of preceding glyphs for which no glyph
25502 strings exist. */
25503 i = left_overwriting (head);
25504 if (i >= 0)
25505 {
25506 enum draw_glyphs_face overlap_hl;
25507
25508 if (check_mouse_face
25509 && mouse_beg_col < start && mouse_end_col > i)
25510 overlap_hl = DRAW_MOUSE_FACE;
25511 else
25512 overlap_hl = DRAW_NORMAL_TEXT;
25513
25514 if (hl == overlap_hl || clip_head == NULL)
25515 clip_head = head;
25516 BUILD_GLYPH_STRINGS (i, start, h, t,
25517 overlap_hl, dummy_x, last_x);
25518 for (s = h; s; s = s->next)
25519 s->background_filled_p = true;
25520 compute_overhangs_and_x (t, head->x, true);
25521 prepend_glyph_string_lists (&head, &tail, h, t);
25522 }
25523
25524 /* Append glyphs strings for glyphs following the last glyph
25525 string tail that are overwritten by tail. The background of
25526 these strings has to be drawn because tail's foreground draws
25527 over it. */
25528 i = right_overwritten (tail);
25529 if (i >= 0)
25530 {
25531 enum draw_glyphs_face overlap_hl;
25532
25533 if (check_mouse_face
25534 && mouse_beg_col < i && mouse_end_col > end)
25535 overlap_hl = DRAW_MOUSE_FACE;
25536 else
25537 overlap_hl = DRAW_NORMAL_TEXT;
25538
25539 if (hl != overlap_hl)
25540 clip_tail = tail;
25541 BUILD_GLYPH_STRINGS (end, i, h, t,
25542 overlap_hl, x, last_x);
25543 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25544 we don't have `end = i;' here. */
25545 compute_overhangs_and_x (h, tail->x + tail->width, false);
25546 append_glyph_string_lists (&head, &tail, h, t);
25547 if (clip_tail == NULL)
25548 clip_tail = tail;
25549 }
25550
25551 /* Append glyph strings for glyphs following the last glyph
25552 string tail that overwrite tail. The foreground of such
25553 glyphs has to be drawn because it writes into the background
25554 of tail. The background must not be drawn because it could
25555 paint over the foreground of following glyphs. */
25556 i = right_overwriting (tail);
25557 if (i >= 0)
25558 {
25559 enum draw_glyphs_face overlap_hl;
25560 if (check_mouse_face
25561 && mouse_beg_col < i && mouse_end_col > end)
25562 overlap_hl = DRAW_MOUSE_FACE;
25563 else
25564 overlap_hl = DRAW_NORMAL_TEXT;
25565
25566 if (hl == overlap_hl || clip_tail == NULL)
25567 clip_tail = tail;
25568 i++; /* We must include the Ith glyph. */
25569 BUILD_GLYPH_STRINGS (end, i, h, t,
25570 overlap_hl, x, last_x);
25571 for (s = h; s; s = s->next)
25572 s->background_filled_p = true;
25573 compute_overhangs_and_x (h, tail->x + tail->width, false);
25574 append_glyph_string_lists (&head, &tail, h, t);
25575 }
25576 if (clip_head || clip_tail)
25577 for (s = head; s; s = s->next)
25578 {
25579 s->clip_head = clip_head;
25580 s->clip_tail = clip_tail;
25581 }
25582 }
25583
25584 /* Draw all strings. */
25585 for (s = head; s; s = s->next)
25586 FRAME_RIF (f)->draw_glyph_string (s);
25587
25588 #ifndef HAVE_NS
25589 /* When focus a sole frame and move horizontally, this clears on_p
25590 causing a failure to erase prev cursor position. */
25591 if (area == TEXT_AREA
25592 && !row->full_width_p
25593 /* When drawing overlapping rows, only the glyph strings'
25594 foreground is drawn, which doesn't erase a cursor
25595 completely. */
25596 && !overlaps)
25597 {
25598 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25599 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25600 : (tail ? tail->x + tail->background_width : x));
25601 x0 -= area_left;
25602 x1 -= area_left;
25603
25604 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25605 row->y, MATRIX_ROW_BOTTOM_Y (row));
25606 }
25607 #endif
25608
25609 /* Value is the x-position up to which drawn, relative to AREA of W.
25610 This doesn't include parts drawn because of overhangs. */
25611 if (row->full_width_p)
25612 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25613 else
25614 x_reached -= area_left;
25615
25616 RELEASE_HDC (hdc, f);
25617
25618 SAFE_FREE ();
25619 return x_reached;
25620 }
25621
25622 /* Expand row matrix if too narrow. Don't expand if area
25623 is not present. */
25624
25625 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25626 { \
25627 if (!it->f->fonts_changed \
25628 && (it->glyph_row->glyphs[area] \
25629 < it->glyph_row->glyphs[area + 1])) \
25630 { \
25631 it->w->ncols_scale_factor++; \
25632 it->f->fonts_changed = true; \
25633 } \
25634 }
25635
25636 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25637 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25638
25639 static void
25640 append_glyph (struct it *it)
25641 {
25642 struct glyph *glyph;
25643 enum glyph_row_area area = it->area;
25644
25645 eassert (it->glyph_row);
25646 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25647
25648 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25649 if (glyph < it->glyph_row->glyphs[area + 1])
25650 {
25651 /* If the glyph row is reversed, we need to prepend the glyph
25652 rather than append it. */
25653 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25654 {
25655 struct glyph *g;
25656
25657 /* Make room for the additional glyph. */
25658 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25659 g[1] = *g;
25660 glyph = it->glyph_row->glyphs[area];
25661 }
25662 glyph->charpos = CHARPOS (it->position);
25663 glyph->object = it->object;
25664 if (it->pixel_width > 0)
25665 {
25666 glyph->pixel_width = it->pixel_width;
25667 glyph->padding_p = false;
25668 }
25669 else
25670 {
25671 /* Assure at least 1-pixel width. Otherwise, cursor can't
25672 be displayed correctly. */
25673 glyph->pixel_width = 1;
25674 glyph->padding_p = true;
25675 }
25676 glyph->ascent = it->ascent;
25677 glyph->descent = it->descent;
25678 glyph->voffset = it->voffset;
25679 glyph->type = CHAR_GLYPH;
25680 glyph->avoid_cursor_p = it->avoid_cursor_p;
25681 glyph->multibyte_p = it->multibyte_p;
25682 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25683 {
25684 /* In R2L rows, the left and the right box edges need to be
25685 drawn in reverse direction. */
25686 glyph->right_box_line_p = it->start_of_box_run_p;
25687 glyph->left_box_line_p = it->end_of_box_run_p;
25688 }
25689 else
25690 {
25691 glyph->left_box_line_p = it->start_of_box_run_p;
25692 glyph->right_box_line_p = it->end_of_box_run_p;
25693 }
25694 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25695 || it->phys_descent > it->descent);
25696 glyph->glyph_not_available_p = it->glyph_not_available_p;
25697 glyph->face_id = it->face_id;
25698 glyph->u.ch = it->char_to_display;
25699 glyph->slice.img = null_glyph_slice;
25700 glyph->font_type = FONT_TYPE_UNKNOWN;
25701 if (it->bidi_p)
25702 {
25703 glyph->resolved_level = it->bidi_it.resolved_level;
25704 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25705 glyph->bidi_type = it->bidi_it.type;
25706 }
25707 else
25708 {
25709 glyph->resolved_level = 0;
25710 glyph->bidi_type = UNKNOWN_BT;
25711 }
25712 ++it->glyph_row->used[area];
25713 }
25714 else
25715 IT_EXPAND_MATRIX_WIDTH (it, area);
25716 }
25717
25718 /* Store one glyph for the composition IT->cmp_it.id in
25719 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25720 non-null. */
25721
25722 static void
25723 append_composite_glyph (struct it *it)
25724 {
25725 struct glyph *glyph;
25726 enum glyph_row_area area = it->area;
25727
25728 eassert (it->glyph_row);
25729
25730 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25731 if (glyph < it->glyph_row->glyphs[area + 1])
25732 {
25733 /* If the glyph row is reversed, we need to prepend the glyph
25734 rather than append it. */
25735 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25736 {
25737 struct glyph *g;
25738
25739 /* Make room for the new glyph. */
25740 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25741 g[1] = *g;
25742 glyph = it->glyph_row->glyphs[it->area];
25743 }
25744 glyph->charpos = it->cmp_it.charpos;
25745 glyph->object = it->object;
25746 glyph->pixel_width = it->pixel_width;
25747 glyph->ascent = it->ascent;
25748 glyph->descent = it->descent;
25749 glyph->voffset = it->voffset;
25750 glyph->type = COMPOSITE_GLYPH;
25751 if (it->cmp_it.ch < 0)
25752 {
25753 glyph->u.cmp.automatic = false;
25754 glyph->u.cmp.id = it->cmp_it.id;
25755 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25756 }
25757 else
25758 {
25759 glyph->u.cmp.automatic = true;
25760 glyph->u.cmp.id = it->cmp_it.id;
25761 glyph->slice.cmp.from = it->cmp_it.from;
25762 glyph->slice.cmp.to = it->cmp_it.to - 1;
25763 }
25764 glyph->avoid_cursor_p = it->avoid_cursor_p;
25765 glyph->multibyte_p = it->multibyte_p;
25766 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25767 {
25768 /* In R2L rows, the left and the right box edges need to be
25769 drawn in reverse direction. */
25770 glyph->right_box_line_p = it->start_of_box_run_p;
25771 glyph->left_box_line_p = it->end_of_box_run_p;
25772 }
25773 else
25774 {
25775 glyph->left_box_line_p = it->start_of_box_run_p;
25776 glyph->right_box_line_p = it->end_of_box_run_p;
25777 }
25778 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25779 || it->phys_descent > it->descent);
25780 glyph->padding_p = false;
25781 glyph->glyph_not_available_p = false;
25782 glyph->face_id = it->face_id;
25783 glyph->font_type = FONT_TYPE_UNKNOWN;
25784 if (it->bidi_p)
25785 {
25786 glyph->resolved_level = it->bidi_it.resolved_level;
25787 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25788 glyph->bidi_type = it->bidi_it.type;
25789 }
25790 ++it->glyph_row->used[area];
25791 }
25792 else
25793 IT_EXPAND_MATRIX_WIDTH (it, area);
25794 }
25795
25796
25797 /* Change IT->ascent and IT->height according to the setting of
25798 IT->voffset. */
25799
25800 static void
25801 take_vertical_position_into_account (struct it *it)
25802 {
25803 if (it->voffset)
25804 {
25805 if (it->voffset < 0)
25806 /* Increase the ascent so that we can display the text higher
25807 in the line. */
25808 it->ascent -= it->voffset;
25809 else
25810 /* Increase the descent so that we can display the text lower
25811 in the line. */
25812 it->descent += it->voffset;
25813 }
25814 }
25815
25816
25817 /* Produce glyphs/get display metrics for the image IT is loaded with.
25818 See the description of struct display_iterator in dispextern.h for
25819 an overview of struct display_iterator. */
25820
25821 static void
25822 produce_image_glyph (struct it *it)
25823 {
25824 struct image *img;
25825 struct face *face;
25826 int glyph_ascent, crop;
25827 struct glyph_slice slice;
25828
25829 eassert (it->what == IT_IMAGE);
25830
25831 face = FACE_FROM_ID (it->f, it->face_id);
25832 eassert (face);
25833 /* Make sure X resources of the face is loaded. */
25834 prepare_face_for_display (it->f, face);
25835
25836 if (it->image_id < 0)
25837 {
25838 /* Fringe bitmap. */
25839 it->ascent = it->phys_ascent = 0;
25840 it->descent = it->phys_descent = 0;
25841 it->pixel_width = 0;
25842 it->nglyphs = 0;
25843 return;
25844 }
25845
25846 img = IMAGE_FROM_ID (it->f, it->image_id);
25847 eassert (img);
25848 /* Make sure X resources of the image is loaded. */
25849 prepare_image_for_display (it->f, img);
25850
25851 slice.x = slice.y = 0;
25852 slice.width = img->width;
25853 slice.height = img->height;
25854
25855 if (INTEGERP (it->slice.x))
25856 slice.x = XINT (it->slice.x);
25857 else if (FLOATP (it->slice.x))
25858 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25859
25860 if (INTEGERP (it->slice.y))
25861 slice.y = XINT (it->slice.y);
25862 else if (FLOATP (it->slice.y))
25863 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25864
25865 if (INTEGERP (it->slice.width))
25866 slice.width = XINT (it->slice.width);
25867 else if (FLOATP (it->slice.width))
25868 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25869
25870 if (INTEGERP (it->slice.height))
25871 slice.height = XINT (it->slice.height);
25872 else if (FLOATP (it->slice.height))
25873 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25874
25875 if (slice.x >= img->width)
25876 slice.x = img->width;
25877 if (slice.y >= img->height)
25878 slice.y = img->height;
25879 if (slice.x + slice.width >= img->width)
25880 slice.width = img->width - slice.x;
25881 if (slice.y + slice.height > img->height)
25882 slice.height = img->height - slice.y;
25883
25884 if (slice.width == 0 || slice.height == 0)
25885 return;
25886
25887 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25888
25889 it->descent = slice.height - glyph_ascent;
25890 if (slice.y == 0)
25891 it->descent += img->vmargin;
25892 if (slice.y + slice.height == img->height)
25893 it->descent += img->vmargin;
25894 it->phys_descent = it->descent;
25895
25896 it->pixel_width = slice.width;
25897 if (slice.x == 0)
25898 it->pixel_width += img->hmargin;
25899 if (slice.x + slice.width == img->width)
25900 it->pixel_width += img->hmargin;
25901
25902 /* It's quite possible for images to have an ascent greater than
25903 their height, so don't get confused in that case. */
25904 if (it->descent < 0)
25905 it->descent = 0;
25906
25907 it->nglyphs = 1;
25908
25909 if (face->box != FACE_NO_BOX)
25910 {
25911 if (face->box_line_width > 0)
25912 {
25913 if (slice.y == 0)
25914 it->ascent += face->box_line_width;
25915 if (slice.y + slice.height == img->height)
25916 it->descent += face->box_line_width;
25917 }
25918
25919 if (it->start_of_box_run_p && slice.x == 0)
25920 it->pixel_width += eabs (face->box_line_width);
25921 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25922 it->pixel_width += eabs (face->box_line_width);
25923 }
25924
25925 take_vertical_position_into_account (it);
25926
25927 /* Automatically crop wide image glyphs at right edge so we can
25928 draw the cursor on same display row. */
25929 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25930 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25931 {
25932 it->pixel_width -= crop;
25933 slice.width -= crop;
25934 }
25935
25936 if (it->glyph_row)
25937 {
25938 struct glyph *glyph;
25939 enum glyph_row_area area = it->area;
25940
25941 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25942 if (it->glyph_row->reversed_p)
25943 {
25944 struct glyph *g;
25945
25946 /* Make room for the new glyph. */
25947 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25948 g[1] = *g;
25949 glyph = it->glyph_row->glyphs[it->area];
25950 }
25951 if (glyph < it->glyph_row->glyphs[area + 1])
25952 {
25953 glyph->charpos = CHARPOS (it->position);
25954 glyph->object = it->object;
25955 glyph->pixel_width = it->pixel_width;
25956 glyph->ascent = glyph_ascent;
25957 glyph->descent = it->descent;
25958 glyph->voffset = it->voffset;
25959 glyph->type = IMAGE_GLYPH;
25960 glyph->avoid_cursor_p = it->avoid_cursor_p;
25961 glyph->multibyte_p = it->multibyte_p;
25962 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25963 {
25964 /* In R2L rows, the left and the right box edges need to be
25965 drawn in reverse direction. */
25966 glyph->right_box_line_p = it->start_of_box_run_p;
25967 glyph->left_box_line_p = it->end_of_box_run_p;
25968 }
25969 else
25970 {
25971 glyph->left_box_line_p = it->start_of_box_run_p;
25972 glyph->right_box_line_p = it->end_of_box_run_p;
25973 }
25974 glyph->overlaps_vertically_p = false;
25975 glyph->padding_p = false;
25976 glyph->glyph_not_available_p = false;
25977 glyph->face_id = it->face_id;
25978 glyph->u.img_id = img->id;
25979 glyph->slice.img = slice;
25980 glyph->font_type = FONT_TYPE_UNKNOWN;
25981 if (it->bidi_p)
25982 {
25983 glyph->resolved_level = it->bidi_it.resolved_level;
25984 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25985 glyph->bidi_type = it->bidi_it.type;
25986 }
25987 ++it->glyph_row->used[area];
25988 }
25989 else
25990 IT_EXPAND_MATRIX_WIDTH (it, area);
25991 }
25992 }
25993
25994
25995 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25996 of the glyph, WIDTH and HEIGHT are the width and height of the
25997 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25998
25999 static void
26000 append_stretch_glyph (struct it *it, Lisp_Object object,
26001 int width, int height, int ascent)
26002 {
26003 struct glyph *glyph;
26004 enum glyph_row_area area = it->area;
26005
26006 eassert (ascent >= 0 && ascent <= height);
26007
26008 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26009 if (glyph < it->glyph_row->glyphs[area + 1])
26010 {
26011 /* If the glyph row is reversed, we need to prepend the glyph
26012 rather than append it. */
26013 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26014 {
26015 struct glyph *g;
26016
26017 /* Make room for the additional glyph. */
26018 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26019 g[1] = *g;
26020 glyph = it->glyph_row->glyphs[area];
26021
26022 /* Decrease the width of the first glyph of the row that
26023 begins before first_visible_x (e.g., due to hscroll).
26024 This is so the overall width of the row becomes smaller
26025 by the scroll amount, and the stretch glyph appended by
26026 extend_face_to_end_of_line will be wider, to shift the
26027 row glyphs to the right. (In L2R rows, the corresponding
26028 left-shift effect is accomplished by setting row->x to a
26029 negative value, which won't work with R2L rows.)
26030
26031 This must leave us with a positive value of WIDTH, since
26032 otherwise the call to move_it_in_display_line_to at the
26033 beginning of display_line would have got past the entire
26034 first glyph, and then it->current_x would have been
26035 greater or equal to it->first_visible_x. */
26036 if (it->current_x < it->first_visible_x)
26037 width -= it->first_visible_x - it->current_x;
26038 eassert (width > 0);
26039 }
26040 glyph->charpos = CHARPOS (it->position);
26041 glyph->object = object;
26042 glyph->pixel_width = width;
26043 glyph->ascent = ascent;
26044 glyph->descent = height - ascent;
26045 glyph->voffset = it->voffset;
26046 glyph->type = STRETCH_GLYPH;
26047 glyph->avoid_cursor_p = it->avoid_cursor_p;
26048 glyph->multibyte_p = it->multibyte_p;
26049 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26050 {
26051 /* In R2L rows, the left and the right box edges need to be
26052 drawn in reverse direction. */
26053 glyph->right_box_line_p = it->start_of_box_run_p;
26054 glyph->left_box_line_p = it->end_of_box_run_p;
26055 }
26056 else
26057 {
26058 glyph->left_box_line_p = it->start_of_box_run_p;
26059 glyph->right_box_line_p = it->end_of_box_run_p;
26060 }
26061 glyph->overlaps_vertically_p = false;
26062 glyph->padding_p = false;
26063 glyph->glyph_not_available_p = false;
26064 glyph->face_id = it->face_id;
26065 glyph->u.stretch.ascent = ascent;
26066 glyph->u.stretch.height = height;
26067 glyph->slice.img = null_glyph_slice;
26068 glyph->font_type = FONT_TYPE_UNKNOWN;
26069 if (it->bidi_p)
26070 {
26071 glyph->resolved_level = it->bidi_it.resolved_level;
26072 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26073 glyph->bidi_type = it->bidi_it.type;
26074 }
26075 else
26076 {
26077 glyph->resolved_level = 0;
26078 glyph->bidi_type = UNKNOWN_BT;
26079 }
26080 ++it->glyph_row->used[area];
26081 }
26082 else
26083 IT_EXPAND_MATRIX_WIDTH (it, area);
26084 }
26085
26086 #endif /* HAVE_WINDOW_SYSTEM */
26087
26088 /* Produce a stretch glyph for iterator IT. IT->object is the value
26089 of the glyph property displayed. The value must be a list
26090 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
26091 being recognized:
26092
26093 1. `:width WIDTH' specifies that the space should be WIDTH *
26094 canonical char width wide. WIDTH may be an integer or floating
26095 point number.
26096
26097 2. `:relative-width FACTOR' specifies that the width of the stretch
26098 should be computed from the width of the first character having the
26099 `glyph' property, and should be FACTOR times that width.
26100
26101 3. `:align-to HPOS' specifies that the space should be wide enough
26102 to reach HPOS, a value in canonical character units.
26103
26104 Exactly one of the above pairs must be present.
26105
26106 4. `:height HEIGHT' specifies that the height of the stretch produced
26107 should be HEIGHT, measured in canonical character units.
26108
26109 5. `:relative-height FACTOR' specifies that the height of the
26110 stretch should be FACTOR times the height of the characters having
26111 the glyph property.
26112
26113 Either none or exactly one of 4 or 5 must be present.
26114
26115 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26116 of the stretch should be used for the ascent of the stretch.
26117 ASCENT must be in the range 0 <= ASCENT <= 100. */
26118
26119 void
26120 produce_stretch_glyph (struct it *it)
26121 {
26122 /* (space :width WIDTH :height HEIGHT ...) */
26123 Lisp_Object prop, plist;
26124 int width = 0, height = 0, align_to = -1;
26125 bool zero_width_ok_p = false;
26126 double tem;
26127 struct font *font = NULL;
26128
26129 #ifdef HAVE_WINDOW_SYSTEM
26130 int ascent = 0;
26131 bool zero_height_ok_p = false;
26132
26133 if (FRAME_WINDOW_P (it->f))
26134 {
26135 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26136 font = face->font ? face->font : FRAME_FONT (it->f);
26137 prepare_face_for_display (it->f, face);
26138 }
26139 #endif
26140
26141 /* List should start with `space'. */
26142 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26143 plist = XCDR (it->object);
26144
26145 /* Compute the width of the stretch. */
26146 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26147 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26148 {
26149 /* Absolute width `:width WIDTH' specified and valid. */
26150 zero_width_ok_p = true;
26151 width = (int)tem;
26152 }
26153 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26154 {
26155 /* Relative width `:relative-width FACTOR' specified and valid.
26156 Compute the width of the characters having the `glyph'
26157 property. */
26158 struct it it2;
26159 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26160
26161 it2 = *it;
26162 if (it->multibyte_p)
26163 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26164 else
26165 {
26166 it2.c = it2.char_to_display = *p, it2.len = 1;
26167 if (! ASCII_CHAR_P (it2.c))
26168 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26169 }
26170
26171 it2.glyph_row = NULL;
26172 it2.what = IT_CHARACTER;
26173 PRODUCE_GLYPHS (&it2);
26174 width = NUMVAL (prop) * it2.pixel_width;
26175 }
26176 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26177 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26178 &align_to))
26179 {
26180 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26181 align_to = (align_to < 0
26182 ? 0
26183 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26184 else if (align_to < 0)
26185 align_to = window_box_left_offset (it->w, TEXT_AREA);
26186 width = max (0, (int)tem + align_to - it->current_x);
26187 zero_width_ok_p = true;
26188 }
26189 else
26190 /* Nothing specified -> width defaults to canonical char width. */
26191 width = FRAME_COLUMN_WIDTH (it->f);
26192
26193 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26194 width = 1;
26195
26196 #ifdef HAVE_WINDOW_SYSTEM
26197 /* Compute height. */
26198 if (FRAME_WINDOW_P (it->f))
26199 {
26200 int default_height = normal_char_height (font, ' ');
26201
26202 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26203 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26204 {
26205 height = (int)tem;
26206 zero_height_ok_p = true;
26207 }
26208 else if (prop = Fplist_get (plist, QCrelative_height),
26209 NUMVAL (prop) > 0)
26210 height = default_height * NUMVAL (prop);
26211 else
26212 height = default_height;
26213
26214 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26215 height = 1;
26216
26217 /* Compute percentage of height used for ascent. If
26218 `:ascent ASCENT' is present and valid, use that. Otherwise,
26219 derive the ascent from the font in use. */
26220 if (prop = Fplist_get (plist, QCascent),
26221 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26222 ascent = height * NUMVAL (prop) / 100.0;
26223 else if (!NILP (prop)
26224 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26225 ascent = min (max (0, (int)tem), height);
26226 else
26227 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26228 }
26229 else
26230 #endif /* HAVE_WINDOW_SYSTEM */
26231 height = 1;
26232
26233 if (width > 0 && it->line_wrap != TRUNCATE
26234 && it->current_x + width > it->last_visible_x)
26235 {
26236 width = it->last_visible_x - it->current_x;
26237 #ifdef HAVE_WINDOW_SYSTEM
26238 /* Subtract one more pixel from the stretch width, but only on
26239 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26240 width -= FRAME_WINDOW_P (it->f);
26241 #endif
26242 }
26243
26244 if (width > 0 && height > 0 && it->glyph_row)
26245 {
26246 Lisp_Object o_object = it->object;
26247 Lisp_Object object = it->stack[it->sp - 1].string;
26248 int n = width;
26249
26250 if (!STRINGP (object))
26251 object = it->w->contents;
26252 #ifdef HAVE_WINDOW_SYSTEM
26253 if (FRAME_WINDOW_P (it->f))
26254 append_stretch_glyph (it, object, width, height, ascent);
26255 else
26256 #endif
26257 {
26258 it->object = object;
26259 it->char_to_display = ' ';
26260 it->pixel_width = it->len = 1;
26261 while (n--)
26262 tty_append_glyph (it);
26263 it->object = o_object;
26264 }
26265 }
26266
26267 it->pixel_width = width;
26268 #ifdef HAVE_WINDOW_SYSTEM
26269 if (FRAME_WINDOW_P (it->f))
26270 {
26271 it->ascent = it->phys_ascent = ascent;
26272 it->descent = it->phys_descent = height - it->ascent;
26273 it->nglyphs = width > 0 && height > 0;
26274 take_vertical_position_into_account (it);
26275 }
26276 else
26277 #endif
26278 it->nglyphs = width;
26279 }
26280
26281 /* Get information about special display element WHAT in an
26282 environment described by IT. WHAT is one of IT_TRUNCATION or
26283 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26284 non-null glyph_row member. This function ensures that fields like
26285 face_id, c, len of IT are left untouched. */
26286
26287 static void
26288 produce_special_glyphs (struct it *it, enum display_element_type what)
26289 {
26290 struct it temp_it;
26291 Lisp_Object gc;
26292 GLYPH glyph;
26293
26294 temp_it = *it;
26295 temp_it.object = Qnil;
26296 memset (&temp_it.current, 0, sizeof temp_it.current);
26297
26298 if (what == IT_CONTINUATION)
26299 {
26300 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26301 if (it->bidi_it.paragraph_dir == R2L)
26302 SET_GLYPH_FROM_CHAR (glyph, '/');
26303 else
26304 SET_GLYPH_FROM_CHAR (glyph, '\\');
26305 if (it->dp
26306 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26307 {
26308 /* FIXME: Should we mirror GC for R2L lines? */
26309 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26310 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26311 }
26312 }
26313 else if (what == IT_TRUNCATION)
26314 {
26315 /* Truncation glyph. */
26316 SET_GLYPH_FROM_CHAR (glyph, '$');
26317 if (it->dp
26318 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26319 {
26320 /* FIXME: Should we mirror GC for R2L lines? */
26321 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26322 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26323 }
26324 }
26325 else
26326 emacs_abort ();
26327
26328 #ifdef HAVE_WINDOW_SYSTEM
26329 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26330 is turned off, we precede the truncation/continuation glyphs by a
26331 stretch glyph whose width is computed such that these special
26332 glyphs are aligned at the window margin, even when very different
26333 fonts are used in different glyph rows. */
26334 if (FRAME_WINDOW_P (temp_it.f)
26335 /* init_iterator calls this with it->glyph_row == NULL, and it
26336 wants only the pixel width of the truncation/continuation
26337 glyphs. */
26338 && temp_it.glyph_row
26339 /* insert_left_trunc_glyphs calls us at the beginning of the
26340 row, and it has its own calculation of the stretch glyph
26341 width. */
26342 && temp_it.glyph_row->used[TEXT_AREA] > 0
26343 && (temp_it.glyph_row->reversed_p
26344 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26345 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26346 {
26347 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26348
26349 if (stretch_width > 0)
26350 {
26351 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26352 struct font *font =
26353 face->font ? face->font : FRAME_FONT (temp_it.f);
26354 int stretch_ascent =
26355 (((temp_it.ascent + temp_it.descent)
26356 * FONT_BASE (font)) / FONT_HEIGHT (font));
26357
26358 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26359 temp_it.ascent + temp_it.descent,
26360 stretch_ascent);
26361 }
26362 }
26363 #endif
26364
26365 temp_it.dp = NULL;
26366 temp_it.what = IT_CHARACTER;
26367 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26368 temp_it.face_id = GLYPH_FACE (glyph);
26369 temp_it.len = CHAR_BYTES (temp_it.c);
26370
26371 PRODUCE_GLYPHS (&temp_it);
26372 it->pixel_width = temp_it.pixel_width;
26373 it->nglyphs = temp_it.nglyphs;
26374 }
26375
26376 #ifdef HAVE_WINDOW_SYSTEM
26377
26378 /* Calculate line-height and line-spacing properties.
26379 An integer value specifies explicit pixel value.
26380 A float value specifies relative value to current face height.
26381 A cons (float . face-name) specifies relative value to
26382 height of specified face font.
26383
26384 Returns height in pixels, or nil. */
26385
26386 static Lisp_Object
26387 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26388 int boff, bool override)
26389 {
26390 Lisp_Object face_name = Qnil;
26391 int ascent, descent, height;
26392
26393 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26394 return val;
26395
26396 if (CONSP (val))
26397 {
26398 face_name = XCAR (val);
26399 val = XCDR (val);
26400 if (!NUMBERP (val))
26401 val = make_number (1);
26402 if (NILP (face_name))
26403 {
26404 height = it->ascent + it->descent;
26405 goto scale;
26406 }
26407 }
26408
26409 if (NILP (face_name))
26410 {
26411 font = FRAME_FONT (it->f);
26412 boff = FRAME_BASELINE_OFFSET (it->f);
26413 }
26414 else if (EQ (face_name, Qt))
26415 {
26416 override = false;
26417 }
26418 else
26419 {
26420 int face_id;
26421 struct face *face;
26422
26423 face_id = lookup_named_face (it->f, face_name, false);
26424 if (face_id < 0)
26425 return make_number (-1);
26426
26427 face = FACE_FROM_ID (it->f, face_id);
26428 font = face->font;
26429 if (font == NULL)
26430 return make_number (-1);
26431 boff = font->baseline_offset;
26432 if (font->vertical_centering)
26433 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26434 }
26435
26436 normal_char_ascent_descent (font, -1, &ascent, &descent);
26437
26438 if (override)
26439 {
26440 it->override_ascent = ascent;
26441 it->override_descent = descent;
26442 it->override_boff = boff;
26443 }
26444
26445 height = ascent + descent;
26446
26447 scale:
26448 if (FLOATP (val))
26449 height = (int)(XFLOAT_DATA (val) * height);
26450 else if (INTEGERP (val))
26451 height *= XINT (val);
26452
26453 return make_number (height);
26454 }
26455
26456
26457 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26458 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26459 and only if this is for a character for which no font was found.
26460
26461 If the display method (it->glyphless_method) is
26462 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26463 length of the acronym or the hexadecimal string, UPPER_XOFF and
26464 UPPER_YOFF are pixel offsets for the upper part of the string,
26465 LOWER_XOFF and LOWER_YOFF are for the lower part.
26466
26467 For the other display methods, LEN through LOWER_YOFF are zero. */
26468
26469 static void
26470 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26471 short upper_xoff, short upper_yoff,
26472 short lower_xoff, short lower_yoff)
26473 {
26474 struct glyph *glyph;
26475 enum glyph_row_area area = it->area;
26476
26477 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26478 if (glyph < it->glyph_row->glyphs[area + 1])
26479 {
26480 /* If the glyph row is reversed, we need to prepend the glyph
26481 rather than append it. */
26482 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26483 {
26484 struct glyph *g;
26485
26486 /* Make room for the additional glyph. */
26487 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26488 g[1] = *g;
26489 glyph = it->glyph_row->glyphs[area];
26490 }
26491 glyph->charpos = CHARPOS (it->position);
26492 glyph->object = it->object;
26493 glyph->pixel_width = it->pixel_width;
26494 glyph->ascent = it->ascent;
26495 glyph->descent = it->descent;
26496 glyph->voffset = it->voffset;
26497 glyph->type = GLYPHLESS_GLYPH;
26498 glyph->u.glyphless.method = it->glyphless_method;
26499 glyph->u.glyphless.for_no_font = for_no_font;
26500 glyph->u.glyphless.len = len;
26501 glyph->u.glyphless.ch = it->c;
26502 glyph->slice.glyphless.upper_xoff = upper_xoff;
26503 glyph->slice.glyphless.upper_yoff = upper_yoff;
26504 glyph->slice.glyphless.lower_xoff = lower_xoff;
26505 glyph->slice.glyphless.lower_yoff = lower_yoff;
26506 glyph->avoid_cursor_p = it->avoid_cursor_p;
26507 glyph->multibyte_p = it->multibyte_p;
26508 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26509 {
26510 /* In R2L rows, the left and the right box edges need to be
26511 drawn in reverse direction. */
26512 glyph->right_box_line_p = it->start_of_box_run_p;
26513 glyph->left_box_line_p = it->end_of_box_run_p;
26514 }
26515 else
26516 {
26517 glyph->left_box_line_p = it->start_of_box_run_p;
26518 glyph->right_box_line_p = it->end_of_box_run_p;
26519 }
26520 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26521 || it->phys_descent > it->descent);
26522 glyph->padding_p = false;
26523 glyph->glyph_not_available_p = false;
26524 glyph->face_id = face_id;
26525 glyph->font_type = FONT_TYPE_UNKNOWN;
26526 if (it->bidi_p)
26527 {
26528 glyph->resolved_level = it->bidi_it.resolved_level;
26529 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26530 glyph->bidi_type = it->bidi_it.type;
26531 }
26532 ++it->glyph_row->used[area];
26533 }
26534 else
26535 IT_EXPAND_MATRIX_WIDTH (it, area);
26536 }
26537
26538
26539 /* Produce a glyph for a glyphless character for iterator IT.
26540 IT->glyphless_method specifies which method to use for displaying
26541 the character. See the description of enum
26542 glyphless_display_method in dispextern.h for the detail.
26543
26544 FOR_NO_FONT is true if and only if this is for a character for
26545 which no font was found. ACRONYM, if non-nil, is an acronym string
26546 for the character. */
26547
26548 static void
26549 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26550 {
26551 int face_id;
26552 struct face *face;
26553 struct font *font;
26554 int base_width, base_height, width, height;
26555 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26556 int len;
26557
26558 /* Get the metrics of the base font. We always refer to the current
26559 ASCII face. */
26560 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26561 font = face->font ? face->font : FRAME_FONT (it->f);
26562 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
26563 it->ascent += font->baseline_offset;
26564 it->descent -= font->baseline_offset;
26565 base_height = it->ascent + it->descent;
26566 base_width = font->average_width;
26567
26568 face_id = merge_glyphless_glyph_face (it);
26569
26570 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26571 {
26572 it->pixel_width = THIN_SPACE_WIDTH;
26573 len = 0;
26574 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26575 }
26576 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26577 {
26578 width = CHAR_WIDTH (it->c);
26579 if (width == 0)
26580 width = 1;
26581 else if (width > 4)
26582 width = 4;
26583 it->pixel_width = base_width * width;
26584 len = 0;
26585 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26586 }
26587 else
26588 {
26589 char buf[7];
26590 const char *str;
26591 unsigned int code[6];
26592 int upper_len;
26593 int ascent, descent;
26594 struct font_metrics metrics_upper, metrics_lower;
26595
26596 face = FACE_FROM_ID (it->f, face_id);
26597 font = face->font ? face->font : FRAME_FONT (it->f);
26598 prepare_face_for_display (it->f, face);
26599
26600 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26601 {
26602 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26603 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26604 if (CONSP (acronym))
26605 acronym = XCAR (acronym);
26606 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26607 }
26608 else
26609 {
26610 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26611 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
26612 str = buf;
26613 }
26614 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26615 code[len] = font->driver->encode_char (font, str[len]);
26616 upper_len = (len + 1) / 2;
26617 font->driver->text_extents (font, code, upper_len,
26618 &metrics_upper);
26619 font->driver->text_extents (font, code + upper_len, len - upper_len,
26620 &metrics_lower);
26621
26622
26623
26624 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26625 width = max (metrics_upper.width, metrics_lower.width) + 4;
26626 upper_xoff = upper_yoff = 2; /* the typical case */
26627 if (base_width >= width)
26628 {
26629 /* Align the upper to the left, the lower to the right. */
26630 it->pixel_width = base_width;
26631 lower_xoff = base_width - 2 - metrics_lower.width;
26632 }
26633 else
26634 {
26635 /* Center the shorter one. */
26636 it->pixel_width = width;
26637 if (metrics_upper.width >= metrics_lower.width)
26638 lower_xoff = (width - metrics_lower.width) / 2;
26639 else
26640 {
26641 /* FIXME: This code doesn't look right. It formerly was
26642 missing the "lower_xoff = 0;", which couldn't have
26643 been right since it left lower_xoff uninitialized. */
26644 lower_xoff = 0;
26645 upper_xoff = (width - metrics_upper.width) / 2;
26646 }
26647 }
26648
26649 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26650 top, bottom, and between upper and lower strings. */
26651 height = (metrics_upper.ascent + metrics_upper.descent
26652 + metrics_lower.ascent + metrics_lower.descent) + 5;
26653 /* Center vertically.
26654 H:base_height, D:base_descent
26655 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26656
26657 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26658 descent = D - H/2 + h/2;
26659 lower_yoff = descent - 2 - ld;
26660 upper_yoff = lower_yoff - la - 1 - ud; */
26661 ascent = - (it->descent - (base_height + height + 1) / 2);
26662 descent = it->descent - (base_height - height) / 2;
26663 lower_yoff = descent - 2 - metrics_lower.descent;
26664 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26665 - metrics_upper.descent);
26666 /* Don't make the height shorter than the base height. */
26667 if (height > base_height)
26668 {
26669 it->ascent = ascent;
26670 it->descent = descent;
26671 }
26672 }
26673
26674 it->phys_ascent = it->ascent;
26675 it->phys_descent = it->descent;
26676 if (it->glyph_row)
26677 append_glyphless_glyph (it, face_id, for_no_font, len,
26678 upper_xoff, upper_yoff,
26679 lower_xoff, lower_yoff);
26680 it->nglyphs = 1;
26681 take_vertical_position_into_account (it);
26682 }
26683
26684
26685 /* RIF:
26686 Produce glyphs/get display metrics for the display element IT is
26687 loaded with. See the description of struct it in dispextern.h
26688 for an overview of struct it. */
26689
26690 void
26691 x_produce_glyphs (struct it *it)
26692 {
26693 int extra_line_spacing = it->extra_line_spacing;
26694
26695 it->glyph_not_available_p = false;
26696
26697 if (it->what == IT_CHARACTER)
26698 {
26699 XChar2b char2b;
26700 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26701 struct font *font = face->font;
26702 struct font_metrics *pcm = NULL;
26703 int boff; /* Baseline offset. */
26704
26705 if (font == NULL)
26706 {
26707 /* When no suitable font is found, display this character by
26708 the method specified in the first extra slot of
26709 Vglyphless_char_display. */
26710 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26711
26712 eassert (it->what == IT_GLYPHLESS);
26713 produce_glyphless_glyph (it, true,
26714 STRINGP (acronym) ? acronym : Qnil);
26715 goto done;
26716 }
26717
26718 boff = font->baseline_offset;
26719 if (font->vertical_centering)
26720 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26721
26722 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26723 {
26724 it->nglyphs = 1;
26725
26726 if (it->override_ascent >= 0)
26727 {
26728 it->ascent = it->override_ascent;
26729 it->descent = it->override_descent;
26730 boff = it->override_boff;
26731 }
26732 else
26733 {
26734 it->ascent = FONT_BASE (font) + boff;
26735 it->descent = FONT_DESCENT (font) - boff;
26736 }
26737
26738 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26739 {
26740 pcm = get_per_char_metric (font, &char2b);
26741 if (pcm->width == 0
26742 && pcm->rbearing == 0 && pcm->lbearing == 0)
26743 pcm = NULL;
26744 }
26745
26746 if (pcm)
26747 {
26748 it->phys_ascent = pcm->ascent + boff;
26749 it->phys_descent = pcm->descent - boff;
26750 it->pixel_width = pcm->width;
26751 /* Don't use font-global values for ascent and descent
26752 if they result in an exceedingly large line height. */
26753 if (it->override_ascent < 0)
26754 {
26755 if (FONT_TOO_HIGH (font))
26756 {
26757 it->ascent = it->phys_ascent;
26758 it->descent = it->phys_descent;
26759 /* These limitations are enforced by an
26760 assertion near the end of this function. */
26761 if (it->ascent < 0)
26762 it->ascent = 0;
26763 if (it->descent < 0)
26764 it->descent = 0;
26765 }
26766 }
26767 }
26768 else
26769 {
26770 it->glyph_not_available_p = true;
26771 it->phys_ascent = it->ascent;
26772 it->phys_descent = it->descent;
26773 it->pixel_width = font->space_width;
26774 }
26775
26776 if (it->constrain_row_ascent_descent_p)
26777 {
26778 if (it->descent > it->max_descent)
26779 {
26780 it->ascent += it->descent - it->max_descent;
26781 it->descent = it->max_descent;
26782 }
26783 if (it->ascent > it->max_ascent)
26784 {
26785 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26786 it->ascent = it->max_ascent;
26787 }
26788 it->phys_ascent = min (it->phys_ascent, it->ascent);
26789 it->phys_descent = min (it->phys_descent, it->descent);
26790 extra_line_spacing = 0;
26791 }
26792
26793 /* If this is a space inside a region of text with
26794 `space-width' property, change its width. */
26795 bool stretched_p
26796 = it->char_to_display == ' ' && !NILP (it->space_width);
26797 if (stretched_p)
26798 it->pixel_width *= XFLOATINT (it->space_width);
26799
26800 /* If face has a box, add the box thickness to the character
26801 height. If character has a box line to the left and/or
26802 right, add the box line width to the character's width. */
26803 if (face->box != FACE_NO_BOX)
26804 {
26805 int thick = face->box_line_width;
26806
26807 if (thick > 0)
26808 {
26809 it->ascent += thick;
26810 it->descent += thick;
26811 }
26812 else
26813 thick = -thick;
26814
26815 if (it->start_of_box_run_p)
26816 it->pixel_width += thick;
26817 if (it->end_of_box_run_p)
26818 it->pixel_width += thick;
26819 }
26820
26821 /* If face has an overline, add the height of the overline
26822 (1 pixel) and a 1 pixel margin to the character height. */
26823 if (face->overline_p)
26824 it->ascent += overline_margin;
26825
26826 if (it->constrain_row_ascent_descent_p)
26827 {
26828 if (it->ascent > it->max_ascent)
26829 it->ascent = it->max_ascent;
26830 if (it->descent > it->max_descent)
26831 it->descent = it->max_descent;
26832 }
26833
26834 take_vertical_position_into_account (it);
26835
26836 /* If we have to actually produce glyphs, do it. */
26837 if (it->glyph_row)
26838 {
26839 if (stretched_p)
26840 {
26841 /* Translate a space with a `space-width' property
26842 into a stretch glyph. */
26843 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26844 / FONT_HEIGHT (font));
26845 append_stretch_glyph (it, it->object, it->pixel_width,
26846 it->ascent + it->descent, ascent);
26847 }
26848 else
26849 append_glyph (it);
26850
26851 /* If characters with lbearing or rbearing are displayed
26852 in this line, record that fact in a flag of the
26853 glyph row. This is used to optimize X output code. */
26854 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26855 it->glyph_row->contains_overlapping_glyphs_p = true;
26856 }
26857 if (! stretched_p && it->pixel_width == 0)
26858 /* We assure that all visible glyphs have at least 1-pixel
26859 width. */
26860 it->pixel_width = 1;
26861 }
26862 else if (it->char_to_display == '\n')
26863 {
26864 /* A newline has no width, but we need the height of the
26865 line. But if previous part of the line sets a height,
26866 don't increase that height. */
26867
26868 Lisp_Object height;
26869 Lisp_Object total_height = Qnil;
26870
26871 it->override_ascent = -1;
26872 it->pixel_width = 0;
26873 it->nglyphs = 0;
26874
26875 height = get_it_property (it, Qline_height);
26876 /* Split (line-height total-height) list. */
26877 if (CONSP (height)
26878 && CONSP (XCDR (height))
26879 && NILP (XCDR (XCDR (height))))
26880 {
26881 total_height = XCAR (XCDR (height));
26882 height = XCAR (height);
26883 }
26884 height = calc_line_height_property (it, height, font, boff, true);
26885
26886 if (it->override_ascent >= 0)
26887 {
26888 it->ascent = it->override_ascent;
26889 it->descent = it->override_descent;
26890 boff = it->override_boff;
26891 }
26892 else
26893 {
26894 if (FONT_TOO_HIGH (font))
26895 {
26896 it->ascent = font->pixel_size + boff - 1;
26897 it->descent = -boff + 1;
26898 if (it->descent < 0)
26899 it->descent = 0;
26900 }
26901 else
26902 {
26903 it->ascent = FONT_BASE (font) + boff;
26904 it->descent = FONT_DESCENT (font) - boff;
26905 }
26906 }
26907
26908 if (EQ (height, Qt))
26909 {
26910 if (it->descent > it->max_descent)
26911 {
26912 it->ascent += it->descent - it->max_descent;
26913 it->descent = it->max_descent;
26914 }
26915 if (it->ascent > it->max_ascent)
26916 {
26917 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26918 it->ascent = it->max_ascent;
26919 }
26920 it->phys_ascent = min (it->phys_ascent, it->ascent);
26921 it->phys_descent = min (it->phys_descent, it->descent);
26922 it->constrain_row_ascent_descent_p = true;
26923 extra_line_spacing = 0;
26924 }
26925 else
26926 {
26927 Lisp_Object spacing;
26928
26929 it->phys_ascent = it->ascent;
26930 it->phys_descent = it->descent;
26931
26932 if ((it->max_ascent > 0 || it->max_descent > 0)
26933 && face->box != FACE_NO_BOX
26934 && face->box_line_width > 0)
26935 {
26936 it->ascent += face->box_line_width;
26937 it->descent += face->box_line_width;
26938 }
26939 if (!NILP (height)
26940 && XINT (height) > it->ascent + it->descent)
26941 it->ascent = XINT (height) - it->descent;
26942
26943 if (!NILP (total_height))
26944 spacing = calc_line_height_property (it, total_height, font,
26945 boff, false);
26946 else
26947 {
26948 spacing = get_it_property (it, Qline_spacing);
26949 spacing = calc_line_height_property (it, spacing, font,
26950 boff, false);
26951 }
26952 if (INTEGERP (spacing))
26953 {
26954 extra_line_spacing = XINT (spacing);
26955 if (!NILP (total_height))
26956 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26957 }
26958 }
26959 }
26960 else /* i.e. (it->char_to_display == '\t') */
26961 {
26962 if (font->space_width > 0)
26963 {
26964 int tab_width = it->tab_width * font->space_width;
26965 int x = it->current_x + it->continuation_lines_width;
26966 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26967
26968 /* If the distance from the current position to the next tab
26969 stop is less than a space character width, use the
26970 tab stop after that. */
26971 if (next_tab_x - x < font->space_width)
26972 next_tab_x += tab_width;
26973
26974 it->pixel_width = next_tab_x - x;
26975 it->nglyphs = 1;
26976 if (FONT_TOO_HIGH (font))
26977 {
26978 if (get_char_glyph_code (' ', font, &char2b))
26979 {
26980 pcm = get_per_char_metric (font, &char2b);
26981 if (pcm->width == 0
26982 && pcm->rbearing == 0 && pcm->lbearing == 0)
26983 pcm = NULL;
26984 }
26985
26986 if (pcm)
26987 {
26988 it->ascent = pcm->ascent + boff;
26989 it->descent = pcm->descent - boff;
26990 }
26991 else
26992 {
26993 it->ascent = font->pixel_size + boff - 1;
26994 it->descent = -boff + 1;
26995 }
26996 if (it->ascent < 0)
26997 it->ascent = 0;
26998 if (it->descent < 0)
26999 it->descent = 0;
27000 }
27001 else
27002 {
27003 it->ascent = FONT_BASE (font) + boff;
27004 it->descent = FONT_DESCENT (font) - boff;
27005 }
27006 it->phys_ascent = it->ascent;
27007 it->phys_descent = it->descent;
27008
27009 if (it->glyph_row)
27010 {
27011 append_stretch_glyph (it, it->object, it->pixel_width,
27012 it->ascent + it->descent, it->ascent);
27013 }
27014 }
27015 else
27016 {
27017 it->pixel_width = 0;
27018 it->nglyphs = 1;
27019 }
27020 }
27021
27022 if (FONT_TOO_HIGH (font))
27023 {
27024 int font_ascent, font_descent;
27025
27026 /* For very large fonts, where we ignore the declared font
27027 dimensions, and go by per-character metrics instead,
27028 don't let the row ascent and descent values (and the row
27029 height computed from them) be smaller than the "normal"
27030 character metrics. This avoids unpleasant effects
27031 whereby lines on display would change their height
27032 depending on which characters are shown. */
27033 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27034 it->max_ascent = max (it->max_ascent, font_ascent);
27035 it->max_descent = max (it->max_descent, font_descent);
27036 }
27037 }
27038 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
27039 {
27040 /* A static composition.
27041
27042 Note: A composition is represented as one glyph in the
27043 glyph matrix. There are no padding glyphs.
27044
27045 Important note: pixel_width, ascent, and descent are the
27046 values of what is drawn by draw_glyphs (i.e. the values of
27047 the overall glyphs composed). */
27048 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27049 int boff; /* baseline offset */
27050 struct composition *cmp = composition_table[it->cmp_it.id];
27051 int glyph_len = cmp->glyph_len;
27052 struct font *font = face->font;
27053
27054 it->nglyphs = 1;
27055
27056 /* If we have not yet calculated pixel size data of glyphs of
27057 the composition for the current face font, calculate them
27058 now. Theoretically, we have to check all fonts for the
27059 glyphs, but that requires much time and memory space. So,
27060 here we check only the font of the first glyph. This may
27061 lead to incorrect display, but it's very rare, and C-l
27062 (recenter-top-bottom) can correct the display anyway. */
27063 if (! cmp->font || cmp->font != font)
27064 {
27065 /* Ascent and descent of the font of the first character
27066 of this composition (adjusted by baseline offset).
27067 Ascent and descent of overall glyphs should not be less
27068 than these, respectively. */
27069 int font_ascent, font_descent, font_height;
27070 /* Bounding box of the overall glyphs. */
27071 int leftmost, rightmost, lowest, highest;
27072 int lbearing, rbearing;
27073 int i, width, ascent, descent;
27074 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
27075 XChar2b char2b;
27076 struct font_metrics *pcm;
27077 ptrdiff_t pos;
27078
27079 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
27080 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
27081 break;
27082 bool right_padded = glyph_len < cmp->glyph_len;
27083 for (i = 0; i < glyph_len; i++)
27084 {
27085 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
27086 break;
27087 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27088 }
27089 bool left_padded = i > 0;
27090
27091 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
27092 : IT_CHARPOS (*it));
27093 /* If no suitable font is found, use the default font. */
27094 bool font_not_found_p = font == NULL;
27095 if (font_not_found_p)
27096 {
27097 face = face->ascii_face;
27098 font = face->font;
27099 }
27100 boff = font->baseline_offset;
27101 if (font->vertical_centering)
27102 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27103 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27104 font_ascent += boff;
27105 font_descent -= boff;
27106 font_height = font_ascent + font_descent;
27107
27108 cmp->font = font;
27109
27110 pcm = NULL;
27111 if (! font_not_found_p)
27112 {
27113 get_char_face_and_encoding (it->f, c, it->face_id,
27114 &char2b, false);
27115 pcm = get_per_char_metric (font, &char2b);
27116 }
27117
27118 /* Initialize the bounding box. */
27119 if (pcm)
27120 {
27121 width = cmp->glyph_len > 0 ? pcm->width : 0;
27122 ascent = pcm->ascent;
27123 descent = pcm->descent;
27124 lbearing = pcm->lbearing;
27125 rbearing = pcm->rbearing;
27126 }
27127 else
27128 {
27129 width = cmp->glyph_len > 0 ? font->space_width : 0;
27130 ascent = FONT_BASE (font);
27131 descent = FONT_DESCENT (font);
27132 lbearing = 0;
27133 rbearing = width;
27134 }
27135
27136 rightmost = width;
27137 leftmost = 0;
27138 lowest = - descent + boff;
27139 highest = ascent + boff;
27140
27141 if (! font_not_found_p
27142 && font->default_ascent
27143 && CHAR_TABLE_P (Vuse_default_ascent)
27144 && !NILP (Faref (Vuse_default_ascent,
27145 make_number (it->char_to_display))))
27146 highest = font->default_ascent + boff;
27147
27148 /* Draw the first glyph at the normal position. It may be
27149 shifted to right later if some other glyphs are drawn
27150 at the left. */
27151 cmp->offsets[i * 2] = 0;
27152 cmp->offsets[i * 2 + 1] = boff;
27153 cmp->lbearing = lbearing;
27154 cmp->rbearing = rbearing;
27155
27156 /* Set cmp->offsets for the remaining glyphs. */
27157 for (i++; i < glyph_len; i++)
27158 {
27159 int left, right, btm, top;
27160 int ch = COMPOSITION_GLYPH (cmp, i);
27161 int face_id;
27162 struct face *this_face;
27163
27164 if (ch == '\t')
27165 ch = ' ';
27166 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27167 this_face = FACE_FROM_ID (it->f, face_id);
27168 font = this_face->font;
27169
27170 if (font == NULL)
27171 pcm = NULL;
27172 else
27173 {
27174 get_char_face_and_encoding (it->f, ch, face_id,
27175 &char2b, false);
27176 pcm = get_per_char_metric (font, &char2b);
27177 }
27178 if (! pcm)
27179 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27180 else
27181 {
27182 width = pcm->width;
27183 ascent = pcm->ascent;
27184 descent = pcm->descent;
27185 lbearing = pcm->lbearing;
27186 rbearing = pcm->rbearing;
27187 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27188 {
27189 /* Relative composition with or without
27190 alternate chars. */
27191 left = (leftmost + rightmost - width) / 2;
27192 btm = - descent + boff;
27193 if (font->relative_compose
27194 && (! CHAR_TABLE_P (Vignore_relative_composition)
27195 || NILP (Faref (Vignore_relative_composition,
27196 make_number (ch)))))
27197 {
27198
27199 if (- descent >= font->relative_compose)
27200 /* One extra pixel between two glyphs. */
27201 btm = highest + 1;
27202 else if (ascent <= 0)
27203 /* One extra pixel between two glyphs. */
27204 btm = lowest - 1 - ascent - descent;
27205 }
27206 }
27207 else
27208 {
27209 /* A composition rule is specified by an integer
27210 value that encodes global and new reference
27211 points (GREF and NREF). GREF and NREF are
27212 specified by numbers as below:
27213
27214 0---1---2 -- ascent
27215 | |
27216 | |
27217 | |
27218 9--10--11 -- center
27219 | |
27220 ---3---4---5--- baseline
27221 | |
27222 6---7---8 -- descent
27223 */
27224 int rule = COMPOSITION_RULE (cmp, i);
27225 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27226
27227 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27228 grefx = gref % 3, nrefx = nref % 3;
27229 grefy = gref / 3, nrefy = nref / 3;
27230 if (xoff)
27231 xoff = font_height * (xoff - 128) / 256;
27232 if (yoff)
27233 yoff = font_height * (yoff - 128) / 256;
27234
27235 left = (leftmost
27236 + grefx * (rightmost - leftmost) / 2
27237 - nrefx * width / 2
27238 + xoff);
27239
27240 btm = ((grefy == 0 ? highest
27241 : grefy == 1 ? 0
27242 : grefy == 2 ? lowest
27243 : (highest + lowest) / 2)
27244 - (nrefy == 0 ? ascent + descent
27245 : nrefy == 1 ? descent - boff
27246 : nrefy == 2 ? 0
27247 : (ascent + descent) / 2)
27248 + yoff);
27249 }
27250
27251 cmp->offsets[i * 2] = left;
27252 cmp->offsets[i * 2 + 1] = btm + descent;
27253
27254 /* Update the bounding box of the overall glyphs. */
27255 if (width > 0)
27256 {
27257 right = left + width;
27258 if (left < leftmost)
27259 leftmost = left;
27260 if (right > rightmost)
27261 rightmost = right;
27262 }
27263 top = btm + descent + ascent;
27264 if (top > highest)
27265 highest = top;
27266 if (btm < lowest)
27267 lowest = btm;
27268
27269 if (cmp->lbearing > left + lbearing)
27270 cmp->lbearing = left + lbearing;
27271 if (cmp->rbearing < left + rbearing)
27272 cmp->rbearing = left + rbearing;
27273 }
27274 }
27275
27276 /* If there are glyphs whose x-offsets are negative,
27277 shift all glyphs to the right and make all x-offsets
27278 non-negative. */
27279 if (leftmost < 0)
27280 {
27281 for (i = 0; i < cmp->glyph_len; i++)
27282 cmp->offsets[i * 2] -= leftmost;
27283 rightmost -= leftmost;
27284 cmp->lbearing -= leftmost;
27285 cmp->rbearing -= leftmost;
27286 }
27287
27288 if (left_padded && cmp->lbearing < 0)
27289 {
27290 for (i = 0; i < cmp->glyph_len; i++)
27291 cmp->offsets[i * 2] -= cmp->lbearing;
27292 rightmost -= cmp->lbearing;
27293 cmp->rbearing -= cmp->lbearing;
27294 cmp->lbearing = 0;
27295 }
27296 if (right_padded && rightmost < cmp->rbearing)
27297 {
27298 rightmost = cmp->rbearing;
27299 }
27300
27301 cmp->pixel_width = rightmost;
27302 cmp->ascent = highest;
27303 cmp->descent = - lowest;
27304 if (cmp->ascent < font_ascent)
27305 cmp->ascent = font_ascent;
27306 if (cmp->descent < font_descent)
27307 cmp->descent = font_descent;
27308 }
27309
27310 if (it->glyph_row
27311 && (cmp->lbearing < 0
27312 || cmp->rbearing > cmp->pixel_width))
27313 it->glyph_row->contains_overlapping_glyphs_p = true;
27314
27315 it->pixel_width = cmp->pixel_width;
27316 it->ascent = it->phys_ascent = cmp->ascent;
27317 it->descent = it->phys_descent = cmp->descent;
27318 if (face->box != FACE_NO_BOX)
27319 {
27320 int thick = face->box_line_width;
27321
27322 if (thick > 0)
27323 {
27324 it->ascent += thick;
27325 it->descent += thick;
27326 }
27327 else
27328 thick = - thick;
27329
27330 if (it->start_of_box_run_p)
27331 it->pixel_width += thick;
27332 if (it->end_of_box_run_p)
27333 it->pixel_width += thick;
27334 }
27335
27336 /* If face has an overline, add the height of the overline
27337 (1 pixel) and a 1 pixel margin to the character height. */
27338 if (face->overline_p)
27339 it->ascent += overline_margin;
27340
27341 take_vertical_position_into_account (it);
27342 if (it->ascent < 0)
27343 it->ascent = 0;
27344 if (it->descent < 0)
27345 it->descent = 0;
27346
27347 if (it->glyph_row && cmp->glyph_len > 0)
27348 append_composite_glyph (it);
27349 }
27350 else if (it->what == IT_COMPOSITION)
27351 {
27352 /* A dynamic (automatic) composition. */
27353 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27354 Lisp_Object gstring;
27355 struct font_metrics metrics;
27356
27357 it->nglyphs = 1;
27358
27359 gstring = composition_gstring_from_id (it->cmp_it.id);
27360 it->pixel_width
27361 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27362 &metrics);
27363 if (it->glyph_row
27364 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27365 it->glyph_row->contains_overlapping_glyphs_p = true;
27366 it->ascent = it->phys_ascent = metrics.ascent;
27367 it->descent = it->phys_descent = metrics.descent;
27368 if (face->box != FACE_NO_BOX)
27369 {
27370 int thick = face->box_line_width;
27371
27372 if (thick > 0)
27373 {
27374 it->ascent += thick;
27375 it->descent += thick;
27376 }
27377 else
27378 thick = - thick;
27379
27380 if (it->start_of_box_run_p)
27381 it->pixel_width += thick;
27382 if (it->end_of_box_run_p)
27383 it->pixel_width += thick;
27384 }
27385 /* If face has an overline, add the height of the overline
27386 (1 pixel) and a 1 pixel margin to the character height. */
27387 if (face->overline_p)
27388 it->ascent += overline_margin;
27389 take_vertical_position_into_account (it);
27390 if (it->ascent < 0)
27391 it->ascent = 0;
27392 if (it->descent < 0)
27393 it->descent = 0;
27394
27395 if (it->glyph_row)
27396 append_composite_glyph (it);
27397 }
27398 else if (it->what == IT_GLYPHLESS)
27399 produce_glyphless_glyph (it, false, Qnil);
27400 else if (it->what == IT_IMAGE)
27401 produce_image_glyph (it);
27402 else if (it->what == IT_STRETCH)
27403 produce_stretch_glyph (it);
27404
27405 done:
27406 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27407 because this isn't true for images with `:ascent 100'. */
27408 eassert (it->ascent >= 0 && it->descent >= 0);
27409 if (it->area == TEXT_AREA)
27410 it->current_x += it->pixel_width;
27411
27412 if (extra_line_spacing > 0)
27413 {
27414 it->descent += extra_line_spacing;
27415 if (extra_line_spacing > it->max_extra_line_spacing)
27416 it->max_extra_line_spacing = extra_line_spacing;
27417 }
27418
27419 it->max_ascent = max (it->max_ascent, it->ascent);
27420 it->max_descent = max (it->max_descent, it->descent);
27421 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27422 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27423 }
27424
27425 /* EXPORT for RIF:
27426 Output LEN glyphs starting at START at the nominal cursor position.
27427 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27428 being updated, and UPDATED_AREA is the area of that row being updated. */
27429
27430 void
27431 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27432 struct glyph *start, enum glyph_row_area updated_area, int len)
27433 {
27434 int x, hpos, chpos = w->phys_cursor.hpos;
27435
27436 eassert (updated_row);
27437 /* When the window is hscrolled, cursor hpos can legitimately be out
27438 of bounds, but we draw the cursor at the corresponding window
27439 margin in that case. */
27440 if (!updated_row->reversed_p && chpos < 0)
27441 chpos = 0;
27442 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27443 chpos = updated_row->used[TEXT_AREA] - 1;
27444
27445 block_input ();
27446
27447 /* Write glyphs. */
27448
27449 hpos = start - updated_row->glyphs[updated_area];
27450 x = draw_glyphs (w, w->output_cursor.x,
27451 updated_row, updated_area,
27452 hpos, hpos + len,
27453 DRAW_NORMAL_TEXT, 0);
27454
27455 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27456 if (updated_area == TEXT_AREA
27457 && w->phys_cursor_on_p
27458 && w->phys_cursor.vpos == w->output_cursor.vpos
27459 && chpos >= hpos
27460 && chpos < hpos + len)
27461 w->phys_cursor_on_p = false;
27462
27463 unblock_input ();
27464
27465 /* Advance the output cursor. */
27466 w->output_cursor.hpos += len;
27467 w->output_cursor.x = x;
27468 }
27469
27470
27471 /* EXPORT for RIF:
27472 Insert LEN glyphs from START at the nominal cursor position. */
27473
27474 void
27475 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27476 struct glyph *start, enum glyph_row_area updated_area, int len)
27477 {
27478 struct frame *f;
27479 int line_height, shift_by_width, shifted_region_width;
27480 struct glyph_row *row;
27481 struct glyph *glyph;
27482 int frame_x, frame_y;
27483 ptrdiff_t hpos;
27484
27485 eassert (updated_row);
27486 block_input ();
27487 f = XFRAME (WINDOW_FRAME (w));
27488
27489 /* Get the height of the line we are in. */
27490 row = updated_row;
27491 line_height = row->height;
27492
27493 /* Get the width of the glyphs to insert. */
27494 shift_by_width = 0;
27495 for (glyph = start; glyph < start + len; ++glyph)
27496 shift_by_width += glyph->pixel_width;
27497
27498 /* Get the width of the region to shift right. */
27499 shifted_region_width = (window_box_width (w, updated_area)
27500 - w->output_cursor.x
27501 - shift_by_width);
27502
27503 /* Shift right. */
27504 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27505 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27506
27507 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27508 line_height, shift_by_width);
27509
27510 /* Write the glyphs. */
27511 hpos = start - row->glyphs[updated_area];
27512 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27513 hpos, hpos + len,
27514 DRAW_NORMAL_TEXT, 0);
27515
27516 /* Advance the output cursor. */
27517 w->output_cursor.hpos += len;
27518 w->output_cursor.x += shift_by_width;
27519 unblock_input ();
27520 }
27521
27522
27523 /* EXPORT for RIF:
27524 Erase the current text line from the nominal cursor position
27525 (inclusive) to pixel column TO_X (exclusive). The idea is that
27526 everything from TO_X onward is already erased.
27527
27528 TO_X is a pixel position relative to UPDATED_AREA of currently
27529 updated window W. TO_X == -1 means clear to the end of this area. */
27530
27531 void
27532 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27533 enum glyph_row_area updated_area, int to_x)
27534 {
27535 struct frame *f;
27536 int max_x, min_y, max_y;
27537 int from_x, from_y, to_y;
27538
27539 eassert (updated_row);
27540 f = XFRAME (w->frame);
27541
27542 if (updated_row->full_width_p)
27543 max_x = (WINDOW_PIXEL_WIDTH (w)
27544 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27545 else
27546 max_x = window_box_width (w, updated_area);
27547 max_y = window_text_bottom_y (w);
27548
27549 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27550 of window. For TO_X > 0, truncate to end of drawing area. */
27551 if (to_x == 0)
27552 return;
27553 else if (to_x < 0)
27554 to_x = max_x;
27555 else
27556 to_x = min (to_x, max_x);
27557
27558 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27559
27560 /* Notice if the cursor will be cleared by this operation. */
27561 if (!updated_row->full_width_p)
27562 notice_overwritten_cursor (w, updated_area,
27563 w->output_cursor.x, -1,
27564 updated_row->y,
27565 MATRIX_ROW_BOTTOM_Y (updated_row));
27566
27567 from_x = w->output_cursor.x;
27568
27569 /* Translate to frame coordinates. */
27570 if (updated_row->full_width_p)
27571 {
27572 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27573 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27574 }
27575 else
27576 {
27577 int area_left = window_box_left (w, updated_area);
27578 from_x += area_left;
27579 to_x += area_left;
27580 }
27581
27582 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27583 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27584 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27585
27586 /* Prevent inadvertently clearing to end of the X window. */
27587 if (to_x > from_x && to_y > from_y)
27588 {
27589 block_input ();
27590 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27591 to_x - from_x, to_y - from_y);
27592 unblock_input ();
27593 }
27594 }
27595
27596 #endif /* HAVE_WINDOW_SYSTEM */
27597
27598
27599 \f
27600 /***********************************************************************
27601 Cursor types
27602 ***********************************************************************/
27603
27604 /* Value is the internal representation of the specified cursor type
27605 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27606 of the bar cursor. */
27607
27608 static enum text_cursor_kinds
27609 get_specified_cursor_type (Lisp_Object arg, int *width)
27610 {
27611 enum text_cursor_kinds type;
27612
27613 if (NILP (arg))
27614 return NO_CURSOR;
27615
27616 if (EQ (arg, Qbox))
27617 return FILLED_BOX_CURSOR;
27618
27619 if (EQ (arg, Qhollow))
27620 return HOLLOW_BOX_CURSOR;
27621
27622 if (EQ (arg, Qbar))
27623 {
27624 *width = 2;
27625 return BAR_CURSOR;
27626 }
27627
27628 if (CONSP (arg)
27629 && EQ (XCAR (arg), Qbar)
27630 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27631 {
27632 *width = XINT (XCDR (arg));
27633 return BAR_CURSOR;
27634 }
27635
27636 if (EQ (arg, Qhbar))
27637 {
27638 *width = 2;
27639 return HBAR_CURSOR;
27640 }
27641
27642 if (CONSP (arg)
27643 && EQ (XCAR (arg), Qhbar)
27644 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27645 {
27646 *width = XINT (XCDR (arg));
27647 return HBAR_CURSOR;
27648 }
27649
27650 /* Treat anything unknown as "hollow box cursor".
27651 It was bad to signal an error; people have trouble fixing
27652 .Xdefaults with Emacs, when it has something bad in it. */
27653 type = HOLLOW_BOX_CURSOR;
27654
27655 return type;
27656 }
27657
27658 /* Set the default cursor types for specified frame. */
27659 void
27660 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27661 {
27662 int width = 1;
27663 Lisp_Object tem;
27664
27665 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27666 FRAME_CURSOR_WIDTH (f) = width;
27667
27668 /* By default, set up the blink-off state depending on the on-state. */
27669
27670 tem = Fassoc (arg, Vblink_cursor_alist);
27671 if (!NILP (tem))
27672 {
27673 FRAME_BLINK_OFF_CURSOR (f)
27674 = get_specified_cursor_type (XCDR (tem), &width);
27675 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27676 }
27677 else
27678 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27679
27680 /* Make sure the cursor gets redrawn. */
27681 f->cursor_type_changed = true;
27682 }
27683
27684
27685 #ifdef HAVE_WINDOW_SYSTEM
27686
27687 /* Return the cursor we want to be displayed in window W. Return
27688 width of bar/hbar cursor through WIDTH arg. Return with
27689 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
27690 (i.e. if the `system caret' should track this cursor).
27691
27692 In a mini-buffer window, we want the cursor only to appear if we
27693 are reading input from this window. For the selected window, we
27694 want the cursor type given by the frame parameter or buffer local
27695 setting of cursor-type. If explicitly marked off, draw no cursor.
27696 In all other cases, we want a hollow box cursor. */
27697
27698 static enum text_cursor_kinds
27699 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27700 bool *active_cursor)
27701 {
27702 struct frame *f = XFRAME (w->frame);
27703 struct buffer *b = XBUFFER (w->contents);
27704 int cursor_type = DEFAULT_CURSOR;
27705 Lisp_Object alt_cursor;
27706 bool non_selected = false;
27707
27708 *active_cursor = true;
27709
27710 /* Echo area */
27711 if (cursor_in_echo_area
27712 && FRAME_HAS_MINIBUF_P (f)
27713 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27714 {
27715 if (w == XWINDOW (echo_area_window))
27716 {
27717 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27718 {
27719 *width = FRAME_CURSOR_WIDTH (f);
27720 return FRAME_DESIRED_CURSOR (f);
27721 }
27722 else
27723 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27724 }
27725
27726 *active_cursor = false;
27727 non_selected = true;
27728 }
27729
27730 /* Detect a nonselected window or nonselected frame. */
27731 else if (w != XWINDOW (f->selected_window)
27732 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27733 {
27734 *active_cursor = false;
27735
27736 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27737 return NO_CURSOR;
27738
27739 non_selected = true;
27740 }
27741
27742 /* Never display a cursor in a window in which cursor-type is nil. */
27743 if (NILP (BVAR (b, cursor_type)))
27744 return NO_CURSOR;
27745
27746 /* Get the normal cursor type for this window. */
27747 if (EQ (BVAR (b, cursor_type), Qt))
27748 {
27749 cursor_type = FRAME_DESIRED_CURSOR (f);
27750 *width = FRAME_CURSOR_WIDTH (f);
27751 }
27752 else
27753 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27754
27755 /* Use cursor-in-non-selected-windows instead
27756 for non-selected window or frame. */
27757 if (non_selected)
27758 {
27759 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27760 if (!EQ (Qt, alt_cursor))
27761 return get_specified_cursor_type (alt_cursor, width);
27762 /* t means modify the normal cursor type. */
27763 if (cursor_type == FILLED_BOX_CURSOR)
27764 cursor_type = HOLLOW_BOX_CURSOR;
27765 else if (cursor_type == BAR_CURSOR && *width > 1)
27766 --*width;
27767 return cursor_type;
27768 }
27769
27770 /* Use normal cursor if not blinked off. */
27771 if (!w->cursor_off_p)
27772 {
27773 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27774 {
27775 if (cursor_type == FILLED_BOX_CURSOR)
27776 {
27777 /* Using a block cursor on large images can be very annoying.
27778 So use a hollow cursor for "large" images.
27779 If image is not transparent (no mask), also use hollow cursor. */
27780 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27781 if (img != NULL && IMAGEP (img->spec))
27782 {
27783 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27784 where N = size of default frame font size.
27785 This should cover most of the "tiny" icons people may use. */
27786 if (!img->mask
27787 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27788 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27789 cursor_type = HOLLOW_BOX_CURSOR;
27790 }
27791 }
27792 else if (cursor_type != NO_CURSOR)
27793 {
27794 /* Display current only supports BOX and HOLLOW cursors for images.
27795 So for now, unconditionally use a HOLLOW cursor when cursor is
27796 not a solid box cursor. */
27797 cursor_type = HOLLOW_BOX_CURSOR;
27798 }
27799 }
27800 return cursor_type;
27801 }
27802
27803 /* Cursor is blinked off, so determine how to "toggle" it. */
27804
27805 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27806 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27807 return get_specified_cursor_type (XCDR (alt_cursor), width);
27808
27809 /* Then see if frame has specified a specific blink off cursor type. */
27810 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27811 {
27812 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27813 return FRAME_BLINK_OFF_CURSOR (f);
27814 }
27815
27816 #if false
27817 /* Some people liked having a permanently visible blinking cursor,
27818 while others had very strong opinions against it. So it was
27819 decided to remove it. KFS 2003-09-03 */
27820
27821 /* Finally perform built-in cursor blinking:
27822 filled box <-> hollow box
27823 wide [h]bar <-> narrow [h]bar
27824 narrow [h]bar <-> no cursor
27825 other type <-> no cursor */
27826
27827 if (cursor_type == FILLED_BOX_CURSOR)
27828 return HOLLOW_BOX_CURSOR;
27829
27830 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27831 {
27832 *width = 1;
27833 return cursor_type;
27834 }
27835 #endif
27836
27837 return NO_CURSOR;
27838 }
27839
27840
27841 /* Notice when the text cursor of window W has been completely
27842 overwritten by a drawing operation that outputs glyphs in AREA
27843 starting at X0 and ending at X1 in the line starting at Y0 and
27844 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27845 the rest of the line after X0 has been written. Y coordinates
27846 are window-relative. */
27847
27848 static void
27849 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27850 int x0, int x1, int y0, int y1)
27851 {
27852 int cx0, cx1, cy0, cy1;
27853 struct glyph_row *row;
27854
27855 if (!w->phys_cursor_on_p)
27856 return;
27857 if (area != TEXT_AREA)
27858 return;
27859
27860 if (w->phys_cursor.vpos < 0
27861 || w->phys_cursor.vpos >= w->current_matrix->nrows
27862 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27863 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27864 return;
27865
27866 if (row->cursor_in_fringe_p)
27867 {
27868 row->cursor_in_fringe_p = false;
27869 draw_fringe_bitmap (w, row, row->reversed_p);
27870 w->phys_cursor_on_p = false;
27871 return;
27872 }
27873
27874 cx0 = w->phys_cursor.x;
27875 cx1 = cx0 + w->phys_cursor_width;
27876 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27877 return;
27878
27879 /* The cursor image will be completely removed from the
27880 screen if the output area intersects the cursor area in
27881 y-direction. When we draw in [y0 y1[, and some part of
27882 the cursor is at y < y0, that part must have been drawn
27883 before. When scrolling, the cursor is erased before
27884 actually scrolling, so we don't come here. When not
27885 scrolling, the rows above the old cursor row must have
27886 changed, and in this case these rows must have written
27887 over the cursor image.
27888
27889 Likewise if part of the cursor is below y1, with the
27890 exception of the cursor being in the first blank row at
27891 the buffer and window end because update_text_area
27892 doesn't draw that row. (Except when it does, but
27893 that's handled in update_text_area.) */
27894
27895 cy0 = w->phys_cursor.y;
27896 cy1 = cy0 + w->phys_cursor_height;
27897 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27898 return;
27899
27900 w->phys_cursor_on_p = false;
27901 }
27902
27903 #endif /* HAVE_WINDOW_SYSTEM */
27904
27905 \f
27906 /************************************************************************
27907 Mouse Face
27908 ************************************************************************/
27909
27910 #ifdef HAVE_WINDOW_SYSTEM
27911
27912 /* EXPORT for RIF:
27913 Fix the display of area AREA of overlapping row ROW in window W
27914 with respect to the overlapping part OVERLAPS. */
27915
27916 void
27917 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27918 enum glyph_row_area area, int overlaps)
27919 {
27920 int i, x;
27921
27922 block_input ();
27923
27924 x = 0;
27925 for (i = 0; i < row->used[area];)
27926 {
27927 if (row->glyphs[area][i].overlaps_vertically_p)
27928 {
27929 int start = i, start_x = x;
27930
27931 do
27932 {
27933 x += row->glyphs[area][i].pixel_width;
27934 ++i;
27935 }
27936 while (i < row->used[area]
27937 && row->glyphs[area][i].overlaps_vertically_p);
27938
27939 draw_glyphs (w, start_x, row, area,
27940 start, i,
27941 DRAW_NORMAL_TEXT, overlaps);
27942 }
27943 else
27944 {
27945 x += row->glyphs[area][i].pixel_width;
27946 ++i;
27947 }
27948 }
27949
27950 unblock_input ();
27951 }
27952
27953
27954 /* EXPORT:
27955 Draw the cursor glyph of window W in glyph row ROW. See the
27956 comment of draw_glyphs for the meaning of HL. */
27957
27958 void
27959 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27960 enum draw_glyphs_face hl)
27961 {
27962 /* If cursor hpos is out of bounds, don't draw garbage. This can
27963 happen in mini-buffer windows when switching between echo area
27964 glyphs and mini-buffer. */
27965 if ((row->reversed_p
27966 ? (w->phys_cursor.hpos >= 0)
27967 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27968 {
27969 bool on_p = w->phys_cursor_on_p;
27970 int x1;
27971 int hpos = w->phys_cursor.hpos;
27972
27973 /* When the window is hscrolled, cursor hpos can legitimately be
27974 out of bounds, but we draw the cursor at the corresponding
27975 window margin in that case. */
27976 if (!row->reversed_p && hpos < 0)
27977 hpos = 0;
27978 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27979 hpos = row->used[TEXT_AREA] - 1;
27980
27981 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27982 hl, 0);
27983 w->phys_cursor_on_p = on_p;
27984
27985 if (hl == DRAW_CURSOR)
27986 w->phys_cursor_width = x1 - w->phys_cursor.x;
27987 /* When we erase the cursor, and ROW is overlapped by other
27988 rows, make sure that these overlapping parts of other rows
27989 are redrawn. */
27990 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27991 {
27992 w->phys_cursor_width = x1 - w->phys_cursor.x;
27993
27994 if (row > w->current_matrix->rows
27995 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27996 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27997 OVERLAPS_ERASED_CURSOR);
27998
27999 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
28000 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
28001 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
28002 OVERLAPS_ERASED_CURSOR);
28003 }
28004 }
28005 }
28006
28007
28008 /* Erase the image of a cursor of window W from the screen. */
28009
28010 void
28011 erase_phys_cursor (struct window *w)
28012 {
28013 struct frame *f = XFRAME (w->frame);
28014 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28015 int hpos = w->phys_cursor.hpos;
28016 int vpos = w->phys_cursor.vpos;
28017 bool mouse_face_here_p = false;
28018 struct glyph_matrix *active_glyphs = w->current_matrix;
28019 struct glyph_row *cursor_row;
28020 struct glyph *cursor_glyph;
28021 enum draw_glyphs_face hl;
28022
28023 /* No cursor displayed or row invalidated => nothing to do on the
28024 screen. */
28025 if (w->phys_cursor_type == NO_CURSOR)
28026 goto mark_cursor_off;
28027
28028 /* VPOS >= active_glyphs->nrows means that window has been resized.
28029 Don't bother to erase the cursor. */
28030 if (vpos >= active_glyphs->nrows)
28031 goto mark_cursor_off;
28032
28033 /* If row containing cursor is marked invalid, there is nothing we
28034 can do. */
28035 cursor_row = MATRIX_ROW (active_glyphs, vpos);
28036 if (!cursor_row->enabled_p)
28037 goto mark_cursor_off;
28038
28039 /* If line spacing is > 0, old cursor may only be partially visible in
28040 window after split-window. So adjust visible height. */
28041 cursor_row->visible_height = min (cursor_row->visible_height,
28042 window_text_bottom_y (w) - cursor_row->y);
28043
28044 /* If row is completely invisible, don't attempt to delete a cursor which
28045 isn't there. This can happen if cursor is at top of a window, and
28046 we switch to a buffer with a header line in that window. */
28047 if (cursor_row->visible_height <= 0)
28048 goto mark_cursor_off;
28049
28050 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
28051 if (cursor_row->cursor_in_fringe_p)
28052 {
28053 cursor_row->cursor_in_fringe_p = false;
28054 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
28055 goto mark_cursor_off;
28056 }
28057
28058 /* This can happen when the new row is shorter than the old one.
28059 In this case, either draw_glyphs or clear_end_of_line
28060 should have cleared the cursor. Note that we wouldn't be
28061 able to erase the cursor in this case because we don't have a
28062 cursor glyph at hand. */
28063 if ((cursor_row->reversed_p
28064 ? (w->phys_cursor.hpos < 0)
28065 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
28066 goto mark_cursor_off;
28067
28068 /* When the window is hscrolled, cursor hpos can legitimately be out
28069 of bounds, but we draw the cursor at the corresponding window
28070 margin in that case. */
28071 if (!cursor_row->reversed_p && hpos < 0)
28072 hpos = 0;
28073 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
28074 hpos = cursor_row->used[TEXT_AREA] - 1;
28075
28076 /* If the cursor is in the mouse face area, redisplay that when
28077 we clear the cursor. */
28078 if (! NILP (hlinfo->mouse_face_window)
28079 && coords_in_mouse_face_p (w, hpos, vpos)
28080 /* Don't redraw the cursor's spot in mouse face if it is at the
28081 end of a line (on a newline). The cursor appears there, but
28082 mouse highlighting does not. */
28083 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
28084 mouse_face_here_p = true;
28085
28086 /* Maybe clear the display under the cursor. */
28087 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
28088 {
28089 int x, y;
28090 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
28091 int width;
28092
28093 cursor_glyph = get_phys_cursor_glyph (w);
28094 if (cursor_glyph == NULL)
28095 goto mark_cursor_off;
28096
28097 width = cursor_glyph->pixel_width;
28098 x = w->phys_cursor.x;
28099 if (x < 0)
28100 {
28101 width += x;
28102 x = 0;
28103 }
28104 width = min (width, window_box_width (w, TEXT_AREA) - x);
28105 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28106 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28107
28108 if (width > 0)
28109 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28110 }
28111
28112 /* Erase the cursor by redrawing the character underneath it. */
28113 if (mouse_face_here_p)
28114 hl = DRAW_MOUSE_FACE;
28115 else
28116 hl = DRAW_NORMAL_TEXT;
28117 draw_phys_cursor_glyph (w, cursor_row, hl);
28118
28119 mark_cursor_off:
28120 w->phys_cursor_on_p = false;
28121 w->phys_cursor_type = NO_CURSOR;
28122 }
28123
28124
28125 /* Display or clear cursor of window W. If !ON, clear the cursor.
28126 If ON, display the cursor; where to put the cursor is specified by
28127 HPOS, VPOS, X and Y. */
28128
28129 void
28130 display_and_set_cursor (struct window *w, bool on,
28131 int hpos, int vpos, int x, int y)
28132 {
28133 struct frame *f = XFRAME (w->frame);
28134 int new_cursor_type;
28135 int new_cursor_width;
28136 bool active_cursor;
28137 struct glyph_row *glyph_row;
28138 struct glyph *glyph;
28139
28140 /* This is pointless on invisible frames, and dangerous on garbaged
28141 windows and frames; in the latter case, the frame or window may
28142 be in the midst of changing its size, and x and y may be off the
28143 window. */
28144 if (! FRAME_VISIBLE_P (f)
28145 || FRAME_GARBAGED_P (f)
28146 || vpos >= w->current_matrix->nrows
28147 || hpos >= w->current_matrix->matrix_w)
28148 return;
28149
28150 /* If cursor is off and we want it off, return quickly. */
28151 if (!on && !w->phys_cursor_on_p)
28152 return;
28153
28154 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28155 /* If cursor row is not enabled, we don't really know where to
28156 display the cursor. */
28157 if (!glyph_row->enabled_p)
28158 {
28159 w->phys_cursor_on_p = false;
28160 return;
28161 }
28162
28163 glyph = NULL;
28164 if (!glyph_row->exact_window_width_line_p
28165 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
28166 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28167
28168 eassert (input_blocked_p ());
28169
28170 /* Set new_cursor_type to the cursor we want to be displayed. */
28171 new_cursor_type = get_window_cursor_type (w, glyph,
28172 &new_cursor_width, &active_cursor);
28173
28174 /* If cursor is currently being shown and we don't want it to be or
28175 it is in the wrong place, or the cursor type is not what we want,
28176 erase it. */
28177 if (w->phys_cursor_on_p
28178 && (!on
28179 || w->phys_cursor.x != x
28180 || w->phys_cursor.y != y
28181 /* HPOS can be negative in R2L rows whose
28182 exact_window_width_line_p flag is set (i.e. their newline
28183 would "overflow into the fringe"). */
28184 || hpos < 0
28185 || new_cursor_type != w->phys_cursor_type
28186 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28187 && new_cursor_width != w->phys_cursor_width)))
28188 erase_phys_cursor (w);
28189
28190 /* Don't check phys_cursor_on_p here because that flag is only set
28191 to false in some cases where we know that the cursor has been
28192 completely erased, to avoid the extra work of erasing the cursor
28193 twice. In other words, phys_cursor_on_p can be true and the cursor
28194 still not be visible, or it has only been partly erased. */
28195 if (on)
28196 {
28197 w->phys_cursor_ascent = glyph_row->ascent;
28198 w->phys_cursor_height = glyph_row->height;
28199
28200 /* Set phys_cursor_.* before x_draw_.* is called because some
28201 of them may need the information. */
28202 w->phys_cursor.x = x;
28203 w->phys_cursor.y = glyph_row->y;
28204 w->phys_cursor.hpos = hpos;
28205 w->phys_cursor.vpos = vpos;
28206 }
28207
28208 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28209 new_cursor_type, new_cursor_width,
28210 on, active_cursor);
28211 }
28212
28213
28214 /* Switch the display of W's cursor on or off, according to the value
28215 of ON. */
28216
28217 static void
28218 update_window_cursor (struct window *w, bool on)
28219 {
28220 /* Don't update cursor in windows whose frame is in the process
28221 of being deleted. */
28222 if (w->current_matrix)
28223 {
28224 int hpos = w->phys_cursor.hpos;
28225 int vpos = w->phys_cursor.vpos;
28226 struct glyph_row *row;
28227
28228 if (vpos >= w->current_matrix->nrows
28229 || hpos >= w->current_matrix->matrix_w)
28230 return;
28231
28232 row = MATRIX_ROW (w->current_matrix, vpos);
28233
28234 /* When the window is hscrolled, cursor hpos can legitimately be
28235 out of bounds, but we draw the cursor at the corresponding
28236 window margin in that case. */
28237 if (!row->reversed_p && hpos < 0)
28238 hpos = 0;
28239 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28240 hpos = row->used[TEXT_AREA] - 1;
28241
28242 block_input ();
28243 display_and_set_cursor (w, on, hpos, vpos,
28244 w->phys_cursor.x, w->phys_cursor.y);
28245 unblock_input ();
28246 }
28247 }
28248
28249
28250 /* Call update_window_cursor with parameter ON_P on all leaf windows
28251 in the window tree rooted at W. */
28252
28253 static void
28254 update_cursor_in_window_tree (struct window *w, bool on_p)
28255 {
28256 while (w)
28257 {
28258 if (WINDOWP (w->contents))
28259 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28260 else
28261 update_window_cursor (w, on_p);
28262
28263 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28264 }
28265 }
28266
28267
28268 /* EXPORT:
28269 Display the cursor on window W, or clear it, according to ON_P.
28270 Don't change the cursor's position. */
28271
28272 void
28273 x_update_cursor (struct frame *f, bool on_p)
28274 {
28275 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28276 }
28277
28278
28279 /* EXPORT:
28280 Clear the cursor of window W to background color, and mark the
28281 cursor as not shown. This is used when the text where the cursor
28282 is about to be rewritten. */
28283
28284 void
28285 x_clear_cursor (struct window *w)
28286 {
28287 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28288 update_window_cursor (w, false);
28289 }
28290
28291 #endif /* HAVE_WINDOW_SYSTEM */
28292
28293 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28294 and MSDOS. */
28295 static void
28296 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28297 int start_hpos, int end_hpos,
28298 enum draw_glyphs_face draw)
28299 {
28300 #ifdef HAVE_WINDOW_SYSTEM
28301 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28302 {
28303 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28304 return;
28305 }
28306 #endif
28307 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28308 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28309 #endif
28310 }
28311
28312 /* Display the active region described by mouse_face_* according to DRAW. */
28313
28314 static void
28315 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28316 {
28317 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28318 struct frame *f = XFRAME (WINDOW_FRAME (w));
28319
28320 if (/* If window is in the process of being destroyed, don't bother
28321 to do anything. */
28322 w->current_matrix != NULL
28323 /* Don't update mouse highlight if hidden. */
28324 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28325 /* Recognize when we are called to operate on rows that don't exist
28326 anymore. This can happen when a window is split. */
28327 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28328 {
28329 bool phys_cursor_on_p = w->phys_cursor_on_p;
28330 struct glyph_row *row, *first, *last;
28331
28332 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28333 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28334
28335 for (row = first; row <= last && row->enabled_p; ++row)
28336 {
28337 int start_hpos, end_hpos, start_x;
28338
28339 /* For all but the first row, the highlight starts at column 0. */
28340 if (row == first)
28341 {
28342 /* R2L rows have BEG and END in reversed order, but the
28343 screen drawing geometry is always left to right. So
28344 we need to mirror the beginning and end of the
28345 highlighted area in R2L rows. */
28346 if (!row->reversed_p)
28347 {
28348 start_hpos = hlinfo->mouse_face_beg_col;
28349 start_x = hlinfo->mouse_face_beg_x;
28350 }
28351 else if (row == last)
28352 {
28353 start_hpos = hlinfo->mouse_face_end_col;
28354 start_x = hlinfo->mouse_face_end_x;
28355 }
28356 else
28357 {
28358 start_hpos = 0;
28359 start_x = 0;
28360 }
28361 }
28362 else if (row->reversed_p && row == last)
28363 {
28364 start_hpos = hlinfo->mouse_face_end_col;
28365 start_x = hlinfo->mouse_face_end_x;
28366 }
28367 else
28368 {
28369 start_hpos = 0;
28370 start_x = 0;
28371 }
28372
28373 if (row == last)
28374 {
28375 if (!row->reversed_p)
28376 end_hpos = hlinfo->mouse_face_end_col;
28377 else if (row == first)
28378 end_hpos = hlinfo->mouse_face_beg_col;
28379 else
28380 {
28381 end_hpos = row->used[TEXT_AREA];
28382 if (draw == DRAW_NORMAL_TEXT)
28383 row->fill_line_p = true; /* Clear to end of line. */
28384 }
28385 }
28386 else if (row->reversed_p && row == first)
28387 end_hpos = hlinfo->mouse_face_beg_col;
28388 else
28389 {
28390 end_hpos = row->used[TEXT_AREA];
28391 if (draw == DRAW_NORMAL_TEXT)
28392 row->fill_line_p = true; /* Clear to end of line. */
28393 }
28394
28395 if (end_hpos > start_hpos)
28396 {
28397 draw_row_with_mouse_face (w, start_x, row,
28398 start_hpos, end_hpos, draw);
28399
28400 row->mouse_face_p
28401 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28402 }
28403 }
28404
28405 #ifdef HAVE_WINDOW_SYSTEM
28406 /* When we've written over the cursor, arrange for it to
28407 be displayed again. */
28408 if (FRAME_WINDOW_P (f)
28409 && phys_cursor_on_p && !w->phys_cursor_on_p)
28410 {
28411 int hpos = w->phys_cursor.hpos;
28412
28413 /* When the window is hscrolled, cursor hpos can legitimately be
28414 out of bounds, but we draw the cursor at the corresponding
28415 window margin in that case. */
28416 if (!row->reversed_p && hpos < 0)
28417 hpos = 0;
28418 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28419 hpos = row->used[TEXT_AREA] - 1;
28420
28421 block_input ();
28422 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28423 w->phys_cursor.x, w->phys_cursor.y);
28424 unblock_input ();
28425 }
28426 #endif /* HAVE_WINDOW_SYSTEM */
28427 }
28428
28429 #ifdef HAVE_WINDOW_SYSTEM
28430 /* Change the mouse cursor. */
28431 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28432 {
28433 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28434 if (draw == DRAW_NORMAL_TEXT
28435 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28436 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28437 else
28438 #endif
28439 if (draw == DRAW_MOUSE_FACE)
28440 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28441 else
28442 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28443 }
28444 #endif /* HAVE_WINDOW_SYSTEM */
28445 }
28446
28447 /* EXPORT:
28448 Clear out the mouse-highlighted active region.
28449 Redraw it un-highlighted first. Value is true if mouse
28450 face was actually drawn unhighlighted. */
28451
28452 bool
28453 clear_mouse_face (Mouse_HLInfo *hlinfo)
28454 {
28455 bool cleared
28456 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28457 if (cleared)
28458 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28459 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28460 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28461 hlinfo->mouse_face_window = Qnil;
28462 hlinfo->mouse_face_overlay = Qnil;
28463 return cleared;
28464 }
28465
28466 /* Return true if the coordinates HPOS and VPOS on windows W are
28467 within the mouse face on that window. */
28468 static bool
28469 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28470 {
28471 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28472
28473 /* Quickly resolve the easy cases. */
28474 if (!(WINDOWP (hlinfo->mouse_face_window)
28475 && XWINDOW (hlinfo->mouse_face_window) == w))
28476 return false;
28477 if (vpos < hlinfo->mouse_face_beg_row
28478 || vpos > hlinfo->mouse_face_end_row)
28479 return false;
28480 if (vpos > hlinfo->mouse_face_beg_row
28481 && vpos < hlinfo->mouse_face_end_row)
28482 return true;
28483
28484 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28485 {
28486 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28487 {
28488 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28489 return true;
28490 }
28491 else if ((vpos == hlinfo->mouse_face_beg_row
28492 && hpos >= hlinfo->mouse_face_beg_col)
28493 || (vpos == hlinfo->mouse_face_end_row
28494 && hpos < hlinfo->mouse_face_end_col))
28495 return true;
28496 }
28497 else
28498 {
28499 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28500 {
28501 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28502 return true;
28503 }
28504 else if ((vpos == hlinfo->mouse_face_beg_row
28505 && hpos <= hlinfo->mouse_face_beg_col)
28506 || (vpos == hlinfo->mouse_face_end_row
28507 && hpos > hlinfo->mouse_face_end_col))
28508 return true;
28509 }
28510 return false;
28511 }
28512
28513
28514 /* EXPORT:
28515 True if physical cursor of window W is within mouse face. */
28516
28517 bool
28518 cursor_in_mouse_face_p (struct window *w)
28519 {
28520 int hpos = w->phys_cursor.hpos;
28521 int vpos = w->phys_cursor.vpos;
28522 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28523
28524 /* When the window is hscrolled, cursor hpos can legitimately be out
28525 of bounds, but we draw the cursor at the corresponding window
28526 margin in that case. */
28527 if (!row->reversed_p && hpos < 0)
28528 hpos = 0;
28529 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28530 hpos = row->used[TEXT_AREA] - 1;
28531
28532 return coords_in_mouse_face_p (w, hpos, vpos);
28533 }
28534
28535
28536 \f
28537 /* Find the glyph rows START_ROW and END_ROW of window W that display
28538 characters between buffer positions START_CHARPOS and END_CHARPOS
28539 (excluding END_CHARPOS). DISP_STRING is a display string that
28540 covers these buffer positions. This is similar to
28541 row_containing_pos, but is more accurate when bidi reordering makes
28542 buffer positions change non-linearly with glyph rows. */
28543 static void
28544 rows_from_pos_range (struct window *w,
28545 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28546 Lisp_Object disp_string,
28547 struct glyph_row **start, struct glyph_row **end)
28548 {
28549 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28550 int last_y = window_text_bottom_y (w);
28551 struct glyph_row *row;
28552
28553 *start = NULL;
28554 *end = NULL;
28555
28556 while (!first->enabled_p
28557 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28558 first++;
28559
28560 /* Find the START row. */
28561 for (row = first;
28562 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28563 row++)
28564 {
28565 /* A row can potentially be the START row if the range of the
28566 characters it displays intersects the range
28567 [START_CHARPOS..END_CHARPOS). */
28568 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28569 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28570 /* See the commentary in row_containing_pos, for the
28571 explanation of the complicated way to check whether
28572 some position is beyond the end of the characters
28573 displayed by a row. */
28574 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28575 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28576 && !row->ends_at_zv_p
28577 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28578 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28579 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28580 && !row->ends_at_zv_p
28581 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28582 {
28583 /* Found a candidate row. Now make sure at least one of the
28584 glyphs it displays has a charpos from the range
28585 [START_CHARPOS..END_CHARPOS).
28586
28587 This is not obvious because bidi reordering could make
28588 buffer positions of a row be 1,2,3,102,101,100, and if we
28589 want to highlight characters in [50..60), we don't want
28590 this row, even though [50..60) does intersect [1..103),
28591 the range of character positions given by the row's start
28592 and end positions. */
28593 struct glyph *g = row->glyphs[TEXT_AREA];
28594 struct glyph *e = g + row->used[TEXT_AREA];
28595
28596 while (g < e)
28597 {
28598 if (((BUFFERP (g->object) || NILP (g->object))
28599 && start_charpos <= g->charpos && g->charpos < end_charpos)
28600 /* A glyph that comes from DISP_STRING is by
28601 definition to be highlighted. */
28602 || EQ (g->object, disp_string))
28603 *start = row;
28604 g++;
28605 }
28606 if (*start)
28607 break;
28608 }
28609 }
28610
28611 /* Find the END row. */
28612 if (!*start
28613 /* If the last row is partially visible, start looking for END
28614 from that row, instead of starting from FIRST. */
28615 && !(row->enabled_p
28616 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28617 row = first;
28618 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28619 {
28620 struct glyph_row *next = row + 1;
28621 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28622
28623 if (!next->enabled_p
28624 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28625 /* The first row >= START whose range of displayed characters
28626 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28627 is the row END + 1. */
28628 || (start_charpos < next_start
28629 && end_charpos < next_start)
28630 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28631 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28632 && !next->ends_at_zv_p
28633 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28634 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28635 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28636 && !next->ends_at_zv_p
28637 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28638 {
28639 *end = row;
28640 break;
28641 }
28642 else
28643 {
28644 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28645 but none of the characters it displays are in the range, it is
28646 also END + 1. */
28647 struct glyph *g = next->glyphs[TEXT_AREA];
28648 struct glyph *s = g;
28649 struct glyph *e = g + next->used[TEXT_AREA];
28650
28651 while (g < e)
28652 {
28653 if (((BUFFERP (g->object) || NILP (g->object))
28654 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28655 /* If the buffer position of the first glyph in
28656 the row is equal to END_CHARPOS, it means
28657 the last character to be highlighted is the
28658 newline of ROW, and we must consider NEXT as
28659 END, not END+1. */
28660 || (((!next->reversed_p && g == s)
28661 || (next->reversed_p && g == e - 1))
28662 && (g->charpos == end_charpos
28663 /* Special case for when NEXT is an
28664 empty line at ZV. */
28665 || (g->charpos == -1
28666 && !row->ends_at_zv_p
28667 && next_start == end_charpos)))))
28668 /* A glyph that comes from DISP_STRING is by
28669 definition to be highlighted. */
28670 || EQ (g->object, disp_string))
28671 break;
28672 g++;
28673 }
28674 if (g == e)
28675 {
28676 *end = row;
28677 break;
28678 }
28679 /* The first row that ends at ZV must be the last to be
28680 highlighted. */
28681 else if (next->ends_at_zv_p)
28682 {
28683 *end = next;
28684 break;
28685 }
28686 }
28687 }
28688 }
28689
28690 /* This function sets the mouse_face_* elements of HLINFO, assuming
28691 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28692 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28693 for the overlay or run of text properties specifying the mouse
28694 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28695 before-string and after-string that must also be highlighted.
28696 DISP_STRING, if non-nil, is a display string that may cover some
28697 or all of the highlighted text. */
28698
28699 static void
28700 mouse_face_from_buffer_pos (Lisp_Object window,
28701 Mouse_HLInfo *hlinfo,
28702 ptrdiff_t mouse_charpos,
28703 ptrdiff_t start_charpos,
28704 ptrdiff_t end_charpos,
28705 Lisp_Object before_string,
28706 Lisp_Object after_string,
28707 Lisp_Object disp_string)
28708 {
28709 struct window *w = XWINDOW (window);
28710 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28711 struct glyph_row *r1, *r2;
28712 struct glyph *glyph, *end;
28713 ptrdiff_t ignore, pos;
28714 int x;
28715
28716 eassert (NILP (disp_string) || STRINGP (disp_string));
28717 eassert (NILP (before_string) || STRINGP (before_string));
28718 eassert (NILP (after_string) || STRINGP (after_string));
28719
28720 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28721 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28722 if (r1 == NULL)
28723 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28724 /* If the before-string or display-string contains newlines,
28725 rows_from_pos_range skips to its last row. Move back. */
28726 if (!NILP (before_string) || !NILP (disp_string))
28727 {
28728 struct glyph_row *prev;
28729 while ((prev = r1 - 1, prev >= first)
28730 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28731 && prev->used[TEXT_AREA] > 0)
28732 {
28733 struct glyph *beg = prev->glyphs[TEXT_AREA];
28734 glyph = beg + prev->used[TEXT_AREA];
28735 while (--glyph >= beg && NILP (glyph->object));
28736 if (glyph < beg
28737 || !(EQ (glyph->object, before_string)
28738 || EQ (glyph->object, disp_string)))
28739 break;
28740 r1 = prev;
28741 }
28742 }
28743 if (r2 == NULL)
28744 {
28745 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28746 hlinfo->mouse_face_past_end = true;
28747 }
28748 else if (!NILP (after_string))
28749 {
28750 /* If the after-string has newlines, advance to its last row. */
28751 struct glyph_row *next;
28752 struct glyph_row *last
28753 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28754
28755 for (next = r2 + 1;
28756 next <= last
28757 && next->used[TEXT_AREA] > 0
28758 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28759 ++next)
28760 r2 = next;
28761 }
28762 /* The rest of the display engine assumes that mouse_face_beg_row is
28763 either above mouse_face_end_row or identical to it. But with
28764 bidi-reordered continued lines, the row for START_CHARPOS could
28765 be below the row for END_CHARPOS. If so, swap the rows and store
28766 them in correct order. */
28767 if (r1->y > r2->y)
28768 {
28769 struct glyph_row *tem = r2;
28770
28771 r2 = r1;
28772 r1 = tem;
28773 }
28774
28775 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28776 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28777
28778 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28779 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28780 could be anywhere in the row and in any order. The strategy
28781 below is to find the leftmost and the rightmost glyph that
28782 belongs to either of these 3 strings, or whose position is
28783 between START_CHARPOS and END_CHARPOS, and highlight all the
28784 glyphs between those two. This may cover more than just the text
28785 between START_CHARPOS and END_CHARPOS if the range of characters
28786 strides the bidi level boundary, e.g. if the beginning is in R2L
28787 text while the end is in L2R text or vice versa. */
28788 if (!r1->reversed_p)
28789 {
28790 /* This row is in a left to right paragraph. Scan it left to
28791 right. */
28792 glyph = r1->glyphs[TEXT_AREA];
28793 end = glyph + r1->used[TEXT_AREA];
28794 x = r1->x;
28795
28796 /* Skip truncation glyphs at the start of the glyph row. */
28797 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28798 for (; glyph < end
28799 && NILP (glyph->object)
28800 && glyph->charpos < 0;
28801 ++glyph)
28802 x += glyph->pixel_width;
28803
28804 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28805 or DISP_STRING, and the first glyph from buffer whose
28806 position is between START_CHARPOS and END_CHARPOS. */
28807 for (; glyph < end
28808 && !NILP (glyph->object)
28809 && !EQ (glyph->object, disp_string)
28810 && !(BUFFERP (glyph->object)
28811 && (glyph->charpos >= start_charpos
28812 && glyph->charpos < end_charpos));
28813 ++glyph)
28814 {
28815 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28816 are present at buffer positions between START_CHARPOS and
28817 END_CHARPOS, or if they come from an overlay. */
28818 if (EQ (glyph->object, before_string))
28819 {
28820 pos = string_buffer_position (before_string,
28821 start_charpos);
28822 /* If pos == 0, it means before_string came from an
28823 overlay, not from a buffer position. */
28824 if (!pos || (pos >= start_charpos && pos < end_charpos))
28825 break;
28826 }
28827 else if (EQ (glyph->object, after_string))
28828 {
28829 pos = string_buffer_position (after_string, end_charpos);
28830 if (!pos || (pos >= start_charpos && pos < end_charpos))
28831 break;
28832 }
28833 x += glyph->pixel_width;
28834 }
28835 hlinfo->mouse_face_beg_x = x;
28836 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28837 }
28838 else
28839 {
28840 /* This row is in a right to left paragraph. Scan it right to
28841 left. */
28842 struct glyph *g;
28843
28844 end = r1->glyphs[TEXT_AREA] - 1;
28845 glyph = end + r1->used[TEXT_AREA];
28846
28847 /* Skip truncation glyphs at the start of the glyph row. */
28848 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28849 for (; glyph > end
28850 && NILP (glyph->object)
28851 && glyph->charpos < 0;
28852 --glyph)
28853 ;
28854
28855 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28856 or DISP_STRING, and the first glyph from buffer whose
28857 position is between START_CHARPOS and END_CHARPOS. */
28858 for (; glyph > end
28859 && !NILP (glyph->object)
28860 && !EQ (glyph->object, disp_string)
28861 && !(BUFFERP (glyph->object)
28862 && (glyph->charpos >= start_charpos
28863 && glyph->charpos < end_charpos));
28864 --glyph)
28865 {
28866 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28867 are present at buffer positions between START_CHARPOS and
28868 END_CHARPOS, or if they come from an overlay. */
28869 if (EQ (glyph->object, before_string))
28870 {
28871 pos = string_buffer_position (before_string, start_charpos);
28872 /* If pos == 0, it means before_string came from an
28873 overlay, not from a buffer position. */
28874 if (!pos || (pos >= start_charpos && pos < end_charpos))
28875 break;
28876 }
28877 else if (EQ (glyph->object, after_string))
28878 {
28879 pos = string_buffer_position (after_string, end_charpos);
28880 if (!pos || (pos >= start_charpos && pos < end_charpos))
28881 break;
28882 }
28883 }
28884
28885 glyph++; /* first glyph to the right of the highlighted area */
28886 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28887 x += g->pixel_width;
28888 hlinfo->mouse_face_beg_x = x;
28889 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28890 }
28891
28892 /* If the highlight ends in a different row, compute GLYPH and END
28893 for the end row. Otherwise, reuse the values computed above for
28894 the row where the highlight begins. */
28895 if (r2 != r1)
28896 {
28897 if (!r2->reversed_p)
28898 {
28899 glyph = r2->glyphs[TEXT_AREA];
28900 end = glyph + r2->used[TEXT_AREA];
28901 x = r2->x;
28902 }
28903 else
28904 {
28905 end = r2->glyphs[TEXT_AREA] - 1;
28906 glyph = end + r2->used[TEXT_AREA];
28907 }
28908 }
28909
28910 if (!r2->reversed_p)
28911 {
28912 /* Skip truncation and continuation glyphs near the end of the
28913 row, and also blanks and stretch glyphs inserted by
28914 extend_face_to_end_of_line. */
28915 while (end > glyph
28916 && NILP ((end - 1)->object))
28917 --end;
28918 /* Scan the rest of the glyph row from the end, looking for the
28919 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28920 DISP_STRING, or whose position is between START_CHARPOS
28921 and END_CHARPOS */
28922 for (--end;
28923 end > glyph
28924 && !NILP (end->object)
28925 && !EQ (end->object, disp_string)
28926 && !(BUFFERP (end->object)
28927 && (end->charpos >= start_charpos
28928 && end->charpos < end_charpos));
28929 --end)
28930 {
28931 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28932 are present at buffer positions between START_CHARPOS and
28933 END_CHARPOS, or if they come from an overlay. */
28934 if (EQ (end->object, before_string))
28935 {
28936 pos = string_buffer_position (before_string, start_charpos);
28937 if (!pos || (pos >= start_charpos && pos < end_charpos))
28938 break;
28939 }
28940 else if (EQ (end->object, after_string))
28941 {
28942 pos = string_buffer_position (after_string, end_charpos);
28943 if (!pos || (pos >= start_charpos && pos < end_charpos))
28944 break;
28945 }
28946 }
28947 /* Find the X coordinate of the last glyph to be highlighted. */
28948 for (; glyph <= end; ++glyph)
28949 x += glyph->pixel_width;
28950
28951 hlinfo->mouse_face_end_x = x;
28952 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28953 }
28954 else
28955 {
28956 /* Skip truncation and continuation glyphs near the end of the
28957 row, and also blanks and stretch glyphs inserted by
28958 extend_face_to_end_of_line. */
28959 x = r2->x;
28960 end++;
28961 while (end < glyph
28962 && NILP (end->object))
28963 {
28964 x += end->pixel_width;
28965 ++end;
28966 }
28967 /* Scan the rest of the glyph row from the end, looking for the
28968 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28969 DISP_STRING, or whose position is between START_CHARPOS
28970 and END_CHARPOS */
28971 for ( ;
28972 end < glyph
28973 && !NILP (end->object)
28974 && !EQ (end->object, disp_string)
28975 && !(BUFFERP (end->object)
28976 && (end->charpos >= start_charpos
28977 && end->charpos < end_charpos));
28978 ++end)
28979 {
28980 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28981 are present at buffer positions between START_CHARPOS and
28982 END_CHARPOS, or if they come from an overlay. */
28983 if (EQ (end->object, before_string))
28984 {
28985 pos = string_buffer_position (before_string, start_charpos);
28986 if (!pos || (pos >= start_charpos && pos < end_charpos))
28987 break;
28988 }
28989 else if (EQ (end->object, after_string))
28990 {
28991 pos = string_buffer_position (after_string, end_charpos);
28992 if (!pos || (pos >= start_charpos && pos < end_charpos))
28993 break;
28994 }
28995 x += end->pixel_width;
28996 }
28997 /* If we exited the above loop because we arrived at the last
28998 glyph of the row, and its buffer position is still not in
28999 range, it means the last character in range is the preceding
29000 newline. Bump the end column and x values to get past the
29001 last glyph. */
29002 if (end == glyph
29003 && BUFFERP (end->object)
29004 && (end->charpos < start_charpos
29005 || end->charpos >= end_charpos))
29006 {
29007 x += end->pixel_width;
29008 ++end;
29009 }
29010 hlinfo->mouse_face_end_x = x;
29011 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
29012 }
29013
29014 hlinfo->mouse_face_window = window;
29015 hlinfo->mouse_face_face_id
29016 = face_at_buffer_position (w, mouse_charpos, &ignore,
29017 mouse_charpos + 1,
29018 !hlinfo->mouse_face_hidden, -1);
29019 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29020 }
29021
29022 /* The following function is not used anymore (replaced with
29023 mouse_face_from_string_pos), but I leave it here for the time
29024 being, in case someone would. */
29025
29026 #if false /* not used */
29027
29028 /* Find the position of the glyph for position POS in OBJECT in
29029 window W's current matrix, and return in *X, *Y the pixel
29030 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
29031
29032 RIGHT_P means return the position of the right edge of the glyph.
29033 !RIGHT_P means return the left edge position.
29034
29035 If no glyph for POS exists in the matrix, return the position of
29036 the glyph with the next smaller position that is in the matrix, if
29037 RIGHT_P is false. If RIGHT_P, and no glyph for POS
29038 exists in the matrix, return the position of the glyph with the
29039 next larger position in OBJECT.
29040
29041 Value is true if a glyph was found. */
29042
29043 static bool
29044 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
29045 int *hpos, int *vpos, int *x, int *y, bool right_p)
29046 {
29047 int yb = window_text_bottom_y (w);
29048 struct glyph_row *r;
29049 struct glyph *best_glyph = NULL;
29050 struct glyph_row *best_row = NULL;
29051 int best_x = 0;
29052
29053 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29054 r->enabled_p && r->y < yb;
29055 ++r)
29056 {
29057 struct glyph *g = r->glyphs[TEXT_AREA];
29058 struct glyph *e = g + r->used[TEXT_AREA];
29059 int gx;
29060
29061 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29062 if (EQ (g->object, object))
29063 {
29064 if (g->charpos == pos)
29065 {
29066 best_glyph = g;
29067 best_x = gx;
29068 best_row = r;
29069 goto found;
29070 }
29071 else if (best_glyph == NULL
29072 || ((eabs (g->charpos - pos)
29073 < eabs (best_glyph->charpos - pos))
29074 && (right_p
29075 ? g->charpos < pos
29076 : g->charpos > pos)))
29077 {
29078 best_glyph = g;
29079 best_x = gx;
29080 best_row = r;
29081 }
29082 }
29083 }
29084
29085 found:
29086
29087 if (best_glyph)
29088 {
29089 *x = best_x;
29090 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
29091
29092 if (right_p)
29093 {
29094 *x += best_glyph->pixel_width;
29095 ++*hpos;
29096 }
29097
29098 *y = best_row->y;
29099 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29100 }
29101
29102 return best_glyph != NULL;
29103 }
29104 #endif /* not used */
29105
29106 /* Find the positions of the first and the last glyphs in window W's
29107 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29108 (assumed to be a string), and return in HLINFO's mouse_face_*
29109 members the pixel and column/row coordinates of those glyphs. */
29110
29111 static void
29112 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29113 Lisp_Object object,
29114 ptrdiff_t startpos, ptrdiff_t endpos)
29115 {
29116 int yb = window_text_bottom_y (w);
29117 struct glyph_row *r;
29118 struct glyph *g, *e;
29119 int gx;
29120 bool found = false;
29121
29122 /* Find the glyph row with at least one position in the range
29123 [STARTPOS..ENDPOS), and the first glyph in that row whose
29124 position belongs to that range. */
29125 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29126 r->enabled_p && r->y < yb;
29127 ++r)
29128 {
29129 if (!r->reversed_p)
29130 {
29131 g = r->glyphs[TEXT_AREA];
29132 e = g + r->used[TEXT_AREA];
29133 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29134 if (EQ (g->object, object)
29135 && startpos <= g->charpos && g->charpos < endpos)
29136 {
29137 hlinfo->mouse_face_beg_row
29138 = MATRIX_ROW_VPOS (r, w->current_matrix);
29139 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29140 hlinfo->mouse_face_beg_x = gx;
29141 found = true;
29142 break;
29143 }
29144 }
29145 else
29146 {
29147 struct glyph *g1;
29148
29149 e = r->glyphs[TEXT_AREA];
29150 g = e + r->used[TEXT_AREA];
29151 for ( ; g > e; --g)
29152 if (EQ ((g-1)->object, object)
29153 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29154 {
29155 hlinfo->mouse_face_beg_row
29156 = MATRIX_ROW_VPOS (r, w->current_matrix);
29157 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29158 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29159 gx += g1->pixel_width;
29160 hlinfo->mouse_face_beg_x = gx;
29161 found = true;
29162 break;
29163 }
29164 }
29165 if (found)
29166 break;
29167 }
29168
29169 if (!found)
29170 return;
29171
29172 /* Starting with the next row, look for the first row which does NOT
29173 include any glyphs whose positions are in the range. */
29174 for (++r; r->enabled_p && r->y < yb; ++r)
29175 {
29176 g = r->glyphs[TEXT_AREA];
29177 e = g + r->used[TEXT_AREA];
29178 found = false;
29179 for ( ; g < e; ++g)
29180 if (EQ (g->object, object)
29181 && startpos <= g->charpos && g->charpos < endpos)
29182 {
29183 found = true;
29184 break;
29185 }
29186 if (!found)
29187 break;
29188 }
29189
29190 /* The highlighted region ends on the previous row. */
29191 r--;
29192
29193 /* Set the end row. */
29194 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29195
29196 /* Compute and set the end column and the end column's horizontal
29197 pixel coordinate. */
29198 if (!r->reversed_p)
29199 {
29200 g = r->glyphs[TEXT_AREA];
29201 e = g + r->used[TEXT_AREA];
29202 for ( ; e > g; --e)
29203 if (EQ ((e-1)->object, object)
29204 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29205 break;
29206 hlinfo->mouse_face_end_col = e - g;
29207
29208 for (gx = r->x; g < e; ++g)
29209 gx += g->pixel_width;
29210 hlinfo->mouse_face_end_x = gx;
29211 }
29212 else
29213 {
29214 e = r->glyphs[TEXT_AREA];
29215 g = e + r->used[TEXT_AREA];
29216 for (gx = r->x ; e < g; ++e)
29217 {
29218 if (EQ (e->object, object)
29219 && startpos <= e->charpos && e->charpos < endpos)
29220 break;
29221 gx += e->pixel_width;
29222 }
29223 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29224 hlinfo->mouse_face_end_x = gx;
29225 }
29226 }
29227
29228 #ifdef HAVE_WINDOW_SYSTEM
29229
29230 /* See if position X, Y is within a hot-spot of an image. */
29231
29232 static bool
29233 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29234 {
29235 if (!CONSP (hot_spot))
29236 return false;
29237
29238 if (EQ (XCAR (hot_spot), Qrect))
29239 {
29240 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29241 Lisp_Object rect = XCDR (hot_spot);
29242 Lisp_Object tem;
29243 if (!CONSP (rect))
29244 return false;
29245 if (!CONSP (XCAR (rect)))
29246 return false;
29247 if (!CONSP (XCDR (rect)))
29248 return false;
29249 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29250 return false;
29251 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29252 return false;
29253 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29254 return false;
29255 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29256 return false;
29257 return true;
29258 }
29259 else if (EQ (XCAR (hot_spot), Qcircle))
29260 {
29261 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29262 Lisp_Object circ = XCDR (hot_spot);
29263 Lisp_Object lr, lx0, ly0;
29264 if (CONSP (circ)
29265 && CONSP (XCAR (circ))
29266 && (lr = XCDR (circ), NUMBERP (lr))
29267 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29268 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29269 {
29270 double r = XFLOATINT (lr);
29271 double dx = XINT (lx0) - x;
29272 double dy = XINT (ly0) - y;
29273 return (dx * dx + dy * dy <= r * r);
29274 }
29275 }
29276 else if (EQ (XCAR (hot_spot), Qpoly))
29277 {
29278 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29279 if (VECTORP (XCDR (hot_spot)))
29280 {
29281 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29282 Lisp_Object *poly = v->contents;
29283 ptrdiff_t n = v->header.size;
29284 ptrdiff_t i;
29285 bool inside = false;
29286 Lisp_Object lx, ly;
29287 int x0, y0;
29288
29289 /* Need an even number of coordinates, and at least 3 edges. */
29290 if (n < 6 || n & 1)
29291 return false;
29292
29293 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29294 If count is odd, we are inside polygon. Pixels on edges
29295 may or may not be included depending on actual geometry of the
29296 polygon. */
29297 if ((lx = poly[n-2], !INTEGERP (lx))
29298 || (ly = poly[n-1], !INTEGERP (lx)))
29299 return false;
29300 x0 = XINT (lx), y0 = XINT (ly);
29301 for (i = 0; i < n; i += 2)
29302 {
29303 int x1 = x0, y1 = y0;
29304 if ((lx = poly[i], !INTEGERP (lx))
29305 || (ly = poly[i+1], !INTEGERP (ly)))
29306 return false;
29307 x0 = XINT (lx), y0 = XINT (ly);
29308
29309 /* Does this segment cross the X line? */
29310 if (x0 >= x)
29311 {
29312 if (x1 >= x)
29313 continue;
29314 }
29315 else if (x1 < x)
29316 continue;
29317 if (y > y0 && y > y1)
29318 continue;
29319 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29320 inside = !inside;
29321 }
29322 return inside;
29323 }
29324 }
29325 return false;
29326 }
29327
29328 Lisp_Object
29329 find_hot_spot (Lisp_Object map, int x, int y)
29330 {
29331 while (CONSP (map))
29332 {
29333 if (CONSP (XCAR (map))
29334 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29335 return XCAR (map);
29336 map = XCDR (map);
29337 }
29338
29339 return Qnil;
29340 }
29341
29342 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29343 3, 3, 0,
29344 doc: /* Lookup in image map MAP coordinates X and Y.
29345 An image map is an alist where each element has the format (AREA ID PLIST).
29346 An AREA is specified as either a rectangle, a circle, or a polygon:
29347 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29348 pixel coordinates of the upper left and bottom right corners.
29349 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29350 and the radius of the circle; r may be a float or integer.
29351 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29352 vector describes one corner in the polygon.
29353 Returns the alist element for the first matching AREA in MAP. */)
29354 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29355 {
29356 if (NILP (map))
29357 return Qnil;
29358
29359 CHECK_NUMBER (x);
29360 CHECK_NUMBER (y);
29361
29362 return find_hot_spot (map,
29363 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29364 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29365 }
29366
29367
29368 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29369 static void
29370 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29371 {
29372 /* Do not change cursor shape while dragging mouse. */
29373 if (EQ (do_mouse_tracking, Qdragging))
29374 return;
29375
29376 if (!NILP (pointer))
29377 {
29378 if (EQ (pointer, Qarrow))
29379 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29380 else if (EQ (pointer, Qhand))
29381 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29382 else if (EQ (pointer, Qtext))
29383 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29384 else if (EQ (pointer, intern ("hdrag")))
29385 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29386 else if (EQ (pointer, intern ("nhdrag")))
29387 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29388 #ifdef HAVE_X_WINDOWS
29389 else if (EQ (pointer, intern ("vdrag")))
29390 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29391 #endif
29392 else if (EQ (pointer, intern ("hourglass")))
29393 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29394 else if (EQ (pointer, Qmodeline))
29395 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29396 else
29397 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29398 }
29399
29400 if (cursor != No_Cursor)
29401 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29402 }
29403
29404 #endif /* HAVE_WINDOW_SYSTEM */
29405
29406 /* Take proper action when mouse has moved to the mode or header line
29407 or marginal area AREA of window W, x-position X and y-position Y.
29408 X is relative to the start of the text display area of W, so the
29409 width of bitmap areas and scroll bars must be subtracted to get a
29410 position relative to the start of the mode line. */
29411
29412 static void
29413 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29414 enum window_part area)
29415 {
29416 struct window *w = XWINDOW (window);
29417 struct frame *f = XFRAME (w->frame);
29418 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29419 #ifdef HAVE_WINDOW_SYSTEM
29420 Display_Info *dpyinfo;
29421 #endif
29422 Cursor cursor = No_Cursor;
29423 Lisp_Object pointer = Qnil;
29424 int dx, dy, width, height;
29425 ptrdiff_t charpos;
29426 Lisp_Object string, object = Qnil;
29427 Lisp_Object pos IF_LINT (= Qnil), help;
29428
29429 Lisp_Object mouse_face;
29430 int original_x_pixel = x;
29431 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29432 struct glyph_row *row IF_LINT (= 0);
29433
29434 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29435 {
29436 int x0;
29437 struct glyph *end;
29438
29439 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29440 returns them in row/column units! */
29441 string = mode_line_string (w, area, &x, &y, &charpos,
29442 &object, &dx, &dy, &width, &height);
29443
29444 row = (area == ON_MODE_LINE
29445 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29446 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29447
29448 /* Find the glyph under the mouse pointer. */
29449 if (row->mode_line_p && row->enabled_p)
29450 {
29451 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29452 end = glyph + row->used[TEXT_AREA];
29453
29454 for (x0 = original_x_pixel;
29455 glyph < end && x0 >= glyph->pixel_width;
29456 ++glyph)
29457 x0 -= glyph->pixel_width;
29458
29459 if (glyph >= end)
29460 glyph = NULL;
29461 }
29462 }
29463 else
29464 {
29465 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29466 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29467 returns them in row/column units! */
29468 string = marginal_area_string (w, area, &x, &y, &charpos,
29469 &object, &dx, &dy, &width, &height);
29470 }
29471
29472 help = Qnil;
29473
29474 #ifdef HAVE_WINDOW_SYSTEM
29475 if (IMAGEP (object))
29476 {
29477 Lisp_Object image_map, hotspot;
29478 if ((image_map = Fplist_get (XCDR (object), QCmap),
29479 !NILP (image_map))
29480 && (hotspot = find_hot_spot (image_map, dx, dy),
29481 CONSP (hotspot))
29482 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29483 {
29484 Lisp_Object plist;
29485
29486 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29487 If so, we could look for mouse-enter, mouse-leave
29488 properties in PLIST (and do something...). */
29489 hotspot = XCDR (hotspot);
29490 if (CONSP (hotspot)
29491 && (plist = XCAR (hotspot), CONSP (plist)))
29492 {
29493 pointer = Fplist_get (plist, Qpointer);
29494 if (NILP (pointer))
29495 pointer = Qhand;
29496 help = Fplist_get (plist, Qhelp_echo);
29497 if (!NILP (help))
29498 {
29499 help_echo_string = help;
29500 XSETWINDOW (help_echo_window, w);
29501 help_echo_object = w->contents;
29502 help_echo_pos = charpos;
29503 }
29504 }
29505 }
29506 if (NILP (pointer))
29507 pointer = Fplist_get (XCDR (object), QCpointer);
29508 }
29509 #endif /* HAVE_WINDOW_SYSTEM */
29510
29511 if (STRINGP (string))
29512 pos = make_number (charpos);
29513
29514 /* Set the help text and mouse pointer. If the mouse is on a part
29515 of the mode line without any text (e.g. past the right edge of
29516 the mode line text), use the default help text and pointer. */
29517 if (STRINGP (string) || area == ON_MODE_LINE)
29518 {
29519 /* Arrange to display the help by setting the global variables
29520 help_echo_string, help_echo_object, and help_echo_pos. */
29521 if (NILP (help))
29522 {
29523 if (STRINGP (string))
29524 help = Fget_text_property (pos, Qhelp_echo, string);
29525
29526 if (!NILP (help))
29527 {
29528 help_echo_string = help;
29529 XSETWINDOW (help_echo_window, w);
29530 help_echo_object = string;
29531 help_echo_pos = charpos;
29532 }
29533 else if (area == ON_MODE_LINE)
29534 {
29535 Lisp_Object default_help
29536 = buffer_local_value (Qmode_line_default_help_echo,
29537 w->contents);
29538
29539 if (STRINGP (default_help))
29540 {
29541 help_echo_string = default_help;
29542 XSETWINDOW (help_echo_window, w);
29543 help_echo_object = Qnil;
29544 help_echo_pos = -1;
29545 }
29546 }
29547 }
29548
29549 #ifdef HAVE_WINDOW_SYSTEM
29550 /* Change the mouse pointer according to what is under it. */
29551 if (FRAME_WINDOW_P (f))
29552 {
29553 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29554 || minibuf_level
29555 || NILP (Vresize_mini_windows));
29556
29557 dpyinfo = FRAME_DISPLAY_INFO (f);
29558 if (STRINGP (string))
29559 {
29560 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29561
29562 if (NILP (pointer))
29563 pointer = Fget_text_property (pos, Qpointer, string);
29564
29565 /* Change the mouse pointer according to what is under X/Y. */
29566 if (NILP (pointer)
29567 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29568 {
29569 Lisp_Object map;
29570 map = Fget_text_property (pos, Qlocal_map, string);
29571 if (!KEYMAPP (map))
29572 map = Fget_text_property (pos, Qkeymap, string);
29573 if (!KEYMAPP (map) && draggable)
29574 cursor = dpyinfo->vertical_scroll_bar_cursor;
29575 }
29576 }
29577 else if (draggable)
29578 /* Default mode-line pointer. */
29579 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29580 }
29581 #endif
29582 }
29583
29584 /* Change the mouse face according to what is under X/Y. */
29585 bool mouse_face_shown = false;
29586 if (STRINGP (string))
29587 {
29588 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29589 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29590 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29591 && glyph)
29592 {
29593 Lisp_Object b, e;
29594
29595 struct glyph * tmp_glyph;
29596
29597 int gpos;
29598 int gseq_length;
29599 int total_pixel_width;
29600 ptrdiff_t begpos, endpos, ignore;
29601
29602 int vpos, hpos;
29603
29604 b = Fprevious_single_property_change (make_number (charpos + 1),
29605 Qmouse_face, string, Qnil);
29606 if (NILP (b))
29607 begpos = 0;
29608 else
29609 begpos = XINT (b);
29610
29611 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29612 if (NILP (e))
29613 endpos = SCHARS (string);
29614 else
29615 endpos = XINT (e);
29616
29617 /* Calculate the glyph position GPOS of GLYPH in the
29618 displayed string, relative to the beginning of the
29619 highlighted part of the string.
29620
29621 Note: GPOS is different from CHARPOS. CHARPOS is the
29622 position of GLYPH in the internal string object. A mode
29623 line string format has structures which are converted to
29624 a flattened string by the Emacs Lisp interpreter. The
29625 internal string is an element of those structures. The
29626 displayed string is the flattened string. */
29627 tmp_glyph = row_start_glyph;
29628 while (tmp_glyph < glyph
29629 && (!(EQ (tmp_glyph->object, glyph->object)
29630 && begpos <= tmp_glyph->charpos
29631 && tmp_glyph->charpos < endpos)))
29632 tmp_glyph++;
29633 gpos = glyph - tmp_glyph;
29634
29635 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29636 the highlighted part of the displayed string to which
29637 GLYPH belongs. Note: GSEQ_LENGTH is different from
29638 SCHARS (STRING), because the latter returns the length of
29639 the internal string. */
29640 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29641 tmp_glyph > glyph
29642 && (!(EQ (tmp_glyph->object, glyph->object)
29643 && begpos <= tmp_glyph->charpos
29644 && tmp_glyph->charpos < endpos));
29645 tmp_glyph--)
29646 ;
29647 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29648
29649 /* Calculate the total pixel width of all the glyphs between
29650 the beginning of the highlighted area and GLYPH. */
29651 total_pixel_width = 0;
29652 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29653 total_pixel_width += tmp_glyph->pixel_width;
29654
29655 /* Pre calculation of re-rendering position. Note: X is in
29656 column units here, after the call to mode_line_string or
29657 marginal_area_string. */
29658 hpos = x - gpos;
29659 vpos = (area == ON_MODE_LINE
29660 ? (w->current_matrix)->nrows - 1
29661 : 0);
29662
29663 /* If GLYPH's position is included in the region that is
29664 already drawn in mouse face, we have nothing to do. */
29665 if ( EQ (window, hlinfo->mouse_face_window)
29666 && (!row->reversed_p
29667 ? (hlinfo->mouse_face_beg_col <= hpos
29668 && hpos < hlinfo->mouse_face_end_col)
29669 /* In R2L rows we swap BEG and END, see below. */
29670 : (hlinfo->mouse_face_end_col <= hpos
29671 && hpos < hlinfo->mouse_face_beg_col))
29672 && hlinfo->mouse_face_beg_row == vpos )
29673 return;
29674
29675 if (clear_mouse_face (hlinfo))
29676 cursor = No_Cursor;
29677
29678 if (!row->reversed_p)
29679 {
29680 hlinfo->mouse_face_beg_col = hpos;
29681 hlinfo->mouse_face_beg_x = original_x_pixel
29682 - (total_pixel_width + dx);
29683 hlinfo->mouse_face_end_col = hpos + gseq_length;
29684 hlinfo->mouse_face_end_x = 0;
29685 }
29686 else
29687 {
29688 /* In R2L rows, show_mouse_face expects BEG and END
29689 coordinates to be swapped. */
29690 hlinfo->mouse_face_end_col = hpos;
29691 hlinfo->mouse_face_end_x = original_x_pixel
29692 - (total_pixel_width + dx);
29693 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29694 hlinfo->mouse_face_beg_x = 0;
29695 }
29696
29697 hlinfo->mouse_face_beg_row = vpos;
29698 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29699 hlinfo->mouse_face_past_end = false;
29700 hlinfo->mouse_face_window = window;
29701
29702 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29703 charpos,
29704 0, &ignore,
29705 glyph->face_id,
29706 true);
29707 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29708 mouse_face_shown = true;
29709
29710 if (NILP (pointer))
29711 pointer = Qhand;
29712 }
29713 }
29714
29715 /* If mouse-face doesn't need to be shown, clear any existing
29716 mouse-face. */
29717 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
29718 clear_mouse_face (hlinfo);
29719
29720 #ifdef HAVE_WINDOW_SYSTEM
29721 if (FRAME_WINDOW_P (f))
29722 define_frame_cursor1 (f, cursor, pointer);
29723 #endif
29724 }
29725
29726
29727 /* EXPORT:
29728 Take proper action when the mouse has moved to position X, Y on
29729 frame F with regards to highlighting portions of display that have
29730 mouse-face properties. Also de-highlight portions of display where
29731 the mouse was before, set the mouse pointer shape as appropriate
29732 for the mouse coordinates, and activate help echo (tooltips).
29733 X and Y can be negative or out of range. */
29734
29735 void
29736 note_mouse_highlight (struct frame *f, int x, int y)
29737 {
29738 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29739 enum window_part part = ON_NOTHING;
29740 Lisp_Object window;
29741 struct window *w;
29742 Cursor cursor = No_Cursor;
29743 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29744 struct buffer *b;
29745
29746 /* When a menu is active, don't highlight because this looks odd. */
29747 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29748 if (popup_activated ())
29749 return;
29750 #endif
29751
29752 if (!f->glyphs_initialized_p
29753 || f->pointer_invisible)
29754 return;
29755
29756 hlinfo->mouse_face_mouse_x = x;
29757 hlinfo->mouse_face_mouse_y = y;
29758 hlinfo->mouse_face_mouse_frame = f;
29759
29760 if (hlinfo->mouse_face_defer)
29761 return;
29762
29763 /* Which window is that in? */
29764 window = window_from_coordinates (f, x, y, &part, true);
29765
29766 /* If displaying active text in another window, clear that. */
29767 if (! EQ (window, hlinfo->mouse_face_window)
29768 /* Also clear if we move out of text area in same window. */
29769 || (!NILP (hlinfo->mouse_face_window)
29770 && !NILP (window)
29771 && part != ON_TEXT
29772 && part != ON_MODE_LINE
29773 && part != ON_HEADER_LINE))
29774 clear_mouse_face (hlinfo);
29775
29776 /* Not on a window -> return. */
29777 if (!WINDOWP (window))
29778 return;
29779
29780 /* Reset help_echo_string. It will get recomputed below. */
29781 help_echo_string = Qnil;
29782
29783 /* Convert to window-relative pixel coordinates. */
29784 w = XWINDOW (window);
29785 frame_to_window_pixel_xy (w, &x, &y);
29786
29787 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29788 /* Handle tool-bar window differently since it doesn't display a
29789 buffer. */
29790 if (EQ (window, f->tool_bar_window))
29791 {
29792 note_tool_bar_highlight (f, x, y);
29793 return;
29794 }
29795 #endif
29796
29797 /* Mouse is on the mode, header line or margin? */
29798 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29799 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29800 {
29801 note_mode_line_or_margin_highlight (window, x, y, part);
29802
29803 #ifdef HAVE_WINDOW_SYSTEM
29804 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29805 {
29806 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29807 /* Show non-text cursor (Bug#16647). */
29808 goto set_cursor;
29809 }
29810 else
29811 #endif
29812 return;
29813 }
29814
29815 #ifdef HAVE_WINDOW_SYSTEM
29816 if (part == ON_VERTICAL_BORDER)
29817 {
29818 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29819 help_echo_string = build_string ("drag-mouse-1: resize");
29820 }
29821 else if (part == ON_RIGHT_DIVIDER)
29822 {
29823 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29824 help_echo_string = build_string ("drag-mouse-1: resize");
29825 }
29826 else if (part == ON_BOTTOM_DIVIDER)
29827 if (! WINDOW_BOTTOMMOST_P (w)
29828 || minibuf_level
29829 || NILP (Vresize_mini_windows))
29830 {
29831 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29832 help_echo_string = build_string ("drag-mouse-1: resize");
29833 }
29834 else
29835 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29836 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29837 || part == ON_VERTICAL_SCROLL_BAR
29838 || part == ON_HORIZONTAL_SCROLL_BAR)
29839 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29840 else
29841 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29842 #endif
29843
29844 /* Are we in a window whose display is up to date?
29845 And verify the buffer's text has not changed. */
29846 b = XBUFFER (w->contents);
29847 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29848 {
29849 int hpos, vpos, dx, dy, area = LAST_AREA;
29850 ptrdiff_t pos;
29851 struct glyph *glyph;
29852 Lisp_Object object;
29853 Lisp_Object mouse_face = Qnil, position;
29854 Lisp_Object *overlay_vec = NULL;
29855 ptrdiff_t i, noverlays;
29856 struct buffer *obuf;
29857 ptrdiff_t obegv, ozv;
29858 bool same_region;
29859
29860 /* Find the glyph under X/Y. */
29861 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29862
29863 #ifdef HAVE_WINDOW_SYSTEM
29864 /* Look for :pointer property on image. */
29865 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29866 {
29867 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29868 if (img != NULL && IMAGEP (img->spec))
29869 {
29870 Lisp_Object image_map, hotspot;
29871 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29872 !NILP (image_map))
29873 && (hotspot = find_hot_spot (image_map,
29874 glyph->slice.img.x + dx,
29875 glyph->slice.img.y + dy),
29876 CONSP (hotspot))
29877 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29878 {
29879 Lisp_Object plist;
29880
29881 /* Could check XCAR (hotspot) to see if we enter/leave
29882 this hot-spot.
29883 If so, we could look for mouse-enter, mouse-leave
29884 properties in PLIST (and do something...). */
29885 hotspot = XCDR (hotspot);
29886 if (CONSP (hotspot)
29887 && (plist = XCAR (hotspot), CONSP (plist)))
29888 {
29889 pointer = Fplist_get (plist, Qpointer);
29890 if (NILP (pointer))
29891 pointer = Qhand;
29892 help_echo_string = Fplist_get (plist, Qhelp_echo);
29893 if (!NILP (help_echo_string))
29894 {
29895 help_echo_window = window;
29896 help_echo_object = glyph->object;
29897 help_echo_pos = glyph->charpos;
29898 }
29899 }
29900 }
29901 if (NILP (pointer))
29902 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29903 }
29904 }
29905 #endif /* HAVE_WINDOW_SYSTEM */
29906
29907 /* Clear mouse face if X/Y not over text. */
29908 if (glyph == NULL
29909 || area != TEXT_AREA
29910 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29911 /* Glyph's OBJECT is nil for glyphs inserted by the
29912 display engine for its internal purposes, like truncation
29913 and continuation glyphs and blanks beyond the end of
29914 line's text on text terminals. If we are over such a
29915 glyph, we are not over any text. */
29916 || NILP (glyph->object)
29917 /* R2L rows have a stretch glyph at their front, which
29918 stands for no text, whereas L2R rows have no glyphs at
29919 all beyond the end of text. Treat such stretch glyphs
29920 like we do with NULL glyphs in L2R rows. */
29921 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29922 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29923 && glyph->type == STRETCH_GLYPH
29924 && glyph->avoid_cursor_p))
29925 {
29926 if (clear_mouse_face (hlinfo))
29927 cursor = No_Cursor;
29928 #ifdef HAVE_WINDOW_SYSTEM
29929 if (FRAME_WINDOW_P (f) && NILP (pointer))
29930 {
29931 if (area != TEXT_AREA)
29932 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29933 else
29934 pointer = Vvoid_text_area_pointer;
29935 }
29936 #endif
29937 goto set_cursor;
29938 }
29939
29940 pos = glyph->charpos;
29941 object = glyph->object;
29942 if (!STRINGP (object) && !BUFFERP (object))
29943 goto set_cursor;
29944
29945 /* If we get an out-of-range value, return now; avoid an error. */
29946 if (BUFFERP (object) && pos > BUF_Z (b))
29947 goto set_cursor;
29948
29949 /* Make the window's buffer temporarily current for
29950 overlays_at and compute_char_face. */
29951 obuf = current_buffer;
29952 current_buffer = b;
29953 obegv = BEGV;
29954 ozv = ZV;
29955 BEGV = BEG;
29956 ZV = Z;
29957
29958 /* Is this char mouse-active or does it have help-echo? */
29959 position = make_number (pos);
29960
29961 USE_SAFE_ALLOCA;
29962
29963 if (BUFFERP (object))
29964 {
29965 /* Put all the overlays we want in a vector in overlay_vec. */
29966 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
29967 /* Sort overlays into increasing priority order. */
29968 noverlays = sort_overlays (overlay_vec, noverlays, w);
29969 }
29970 else
29971 noverlays = 0;
29972
29973 if (NILP (Vmouse_highlight))
29974 {
29975 clear_mouse_face (hlinfo);
29976 goto check_help_echo;
29977 }
29978
29979 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29980
29981 if (same_region)
29982 cursor = No_Cursor;
29983
29984 /* Check mouse-face highlighting. */
29985 if (! same_region
29986 /* If there exists an overlay with mouse-face overlapping
29987 the one we are currently highlighting, we have to
29988 check if we enter the overlapping overlay, and then
29989 highlight only that. */
29990 || (OVERLAYP (hlinfo->mouse_face_overlay)
29991 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29992 {
29993 /* Find the highest priority overlay with a mouse-face. */
29994 Lisp_Object overlay = Qnil;
29995 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29996 {
29997 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29998 if (!NILP (mouse_face))
29999 overlay = overlay_vec[i];
30000 }
30001
30002 /* If we're highlighting the same overlay as before, there's
30003 no need to do that again. */
30004 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
30005 goto check_help_echo;
30006 hlinfo->mouse_face_overlay = overlay;
30007
30008 /* Clear the display of the old active region, if any. */
30009 if (clear_mouse_face (hlinfo))
30010 cursor = No_Cursor;
30011
30012 /* If no overlay applies, get a text property. */
30013 if (NILP (overlay))
30014 mouse_face = Fget_text_property (position, Qmouse_face, object);
30015
30016 /* Next, compute the bounds of the mouse highlighting and
30017 display it. */
30018 if (!NILP (mouse_face) && STRINGP (object))
30019 {
30020 /* The mouse-highlighting comes from a display string
30021 with a mouse-face. */
30022 Lisp_Object s, e;
30023 ptrdiff_t ignore;
30024
30025 s = Fprevious_single_property_change
30026 (make_number (pos + 1), Qmouse_face, object, Qnil);
30027 e = Fnext_single_property_change
30028 (position, Qmouse_face, object, Qnil);
30029 if (NILP (s))
30030 s = make_number (0);
30031 if (NILP (e))
30032 e = make_number (SCHARS (object));
30033 mouse_face_from_string_pos (w, hlinfo, object,
30034 XINT (s), XINT (e));
30035 hlinfo->mouse_face_past_end = false;
30036 hlinfo->mouse_face_window = window;
30037 hlinfo->mouse_face_face_id
30038 = face_at_string_position (w, object, pos, 0, &ignore,
30039 glyph->face_id, true);
30040 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30041 cursor = No_Cursor;
30042 }
30043 else
30044 {
30045 /* The mouse-highlighting, if any, comes from an overlay
30046 or text property in the buffer. */
30047 Lisp_Object buffer IF_LINT (= Qnil);
30048 Lisp_Object disp_string IF_LINT (= Qnil);
30049
30050 if (STRINGP (object))
30051 {
30052 /* If we are on a display string with no mouse-face,
30053 check if the text under it has one. */
30054 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
30055 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30056 pos = string_buffer_position (object, start);
30057 if (pos > 0)
30058 {
30059 mouse_face = get_char_property_and_overlay
30060 (make_number (pos), Qmouse_face, w->contents, &overlay);
30061 buffer = w->contents;
30062 disp_string = object;
30063 }
30064 }
30065 else
30066 {
30067 buffer = object;
30068 disp_string = Qnil;
30069 }
30070
30071 if (!NILP (mouse_face))
30072 {
30073 Lisp_Object before, after;
30074 Lisp_Object before_string, after_string;
30075 /* To correctly find the limits of mouse highlight
30076 in a bidi-reordered buffer, we must not use the
30077 optimization of limiting the search in
30078 previous-single-property-change and
30079 next-single-property-change, because
30080 rows_from_pos_range needs the real start and end
30081 positions to DTRT in this case. That's because
30082 the first row visible in a window does not
30083 necessarily display the character whose position
30084 is the smallest. */
30085 Lisp_Object lim1
30086 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30087 ? Fmarker_position (w->start)
30088 : Qnil;
30089 Lisp_Object lim2
30090 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30091 ? make_number (BUF_Z (XBUFFER (buffer))
30092 - w->window_end_pos)
30093 : Qnil;
30094
30095 if (NILP (overlay))
30096 {
30097 /* Handle the text property case. */
30098 before = Fprevious_single_property_change
30099 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30100 after = Fnext_single_property_change
30101 (make_number (pos), Qmouse_face, buffer, lim2);
30102 before_string = after_string = Qnil;
30103 }
30104 else
30105 {
30106 /* Handle the overlay case. */
30107 before = Foverlay_start (overlay);
30108 after = Foverlay_end (overlay);
30109 before_string = Foverlay_get (overlay, Qbefore_string);
30110 after_string = Foverlay_get (overlay, Qafter_string);
30111
30112 if (!STRINGP (before_string)) before_string = Qnil;
30113 if (!STRINGP (after_string)) after_string = Qnil;
30114 }
30115
30116 mouse_face_from_buffer_pos (window, hlinfo, pos,
30117 NILP (before)
30118 ? 1
30119 : XFASTINT (before),
30120 NILP (after)
30121 ? BUF_Z (XBUFFER (buffer))
30122 : XFASTINT (after),
30123 before_string, after_string,
30124 disp_string);
30125 cursor = No_Cursor;
30126 }
30127 }
30128 }
30129
30130 check_help_echo:
30131
30132 /* Look for a `help-echo' property. */
30133 if (NILP (help_echo_string)) {
30134 Lisp_Object help, overlay;
30135
30136 /* Check overlays first. */
30137 help = overlay = Qnil;
30138 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30139 {
30140 overlay = overlay_vec[i];
30141 help = Foverlay_get (overlay, Qhelp_echo);
30142 }
30143
30144 if (!NILP (help))
30145 {
30146 help_echo_string = help;
30147 help_echo_window = window;
30148 help_echo_object = overlay;
30149 help_echo_pos = pos;
30150 }
30151 else
30152 {
30153 Lisp_Object obj = glyph->object;
30154 ptrdiff_t charpos = glyph->charpos;
30155
30156 /* Try text properties. */
30157 if (STRINGP (obj)
30158 && charpos >= 0
30159 && charpos < SCHARS (obj))
30160 {
30161 help = Fget_text_property (make_number (charpos),
30162 Qhelp_echo, obj);
30163 if (NILP (help))
30164 {
30165 /* If the string itself doesn't specify a help-echo,
30166 see if the buffer text ``under'' it does. */
30167 struct glyph_row *r
30168 = MATRIX_ROW (w->current_matrix, vpos);
30169 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30170 ptrdiff_t p = string_buffer_position (obj, start);
30171 if (p > 0)
30172 {
30173 help = Fget_char_property (make_number (p),
30174 Qhelp_echo, w->contents);
30175 if (!NILP (help))
30176 {
30177 charpos = p;
30178 obj = w->contents;
30179 }
30180 }
30181 }
30182 }
30183 else if (BUFFERP (obj)
30184 && charpos >= BEGV
30185 && charpos < ZV)
30186 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30187 obj);
30188
30189 if (!NILP (help))
30190 {
30191 help_echo_string = help;
30192 help_echo_window = window;
30193 help_echo_object = obj;
30194 help_echo_pos = charpos;
30195 }
30196 }
30197 }
30198
30199 #ifdef HAVE_WINDOW_SYSTEM
30200 /* Look for a `pointer' property. */
30201 if (FRAME_WINDOW_P (f) && NILP (pointer))
30202 {
30203 /* Check overlays first. */
30204 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30205 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30206
30207 if (NILP (pointer))
30208 {
30209 Lisp_Object obj = glyph->object;
30210 ptrdiff_t charpos = glyph->charpos;
30211
30212 /* Try text properties. */
30213 if (STRINGP (obj)
30214 && charpos >= 0
30215 && charpos < SCHARS (obj))
30216 {
30217 pointer = Fget_text_property (make_number (charpos),
30218 Qpointer, obj);
30219 if (NILP (pointer))
30220 {
30221 /* If the string itself doesn't specify a pointer,
30222 see if the buffer text ``under'' it does. */
30223 struct glyph_row *r
30224 = MATRIX_ROW (w->current_matrix, vpos);
30225 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30226 ptrdiff_t p = string_buffer_position (obj, start);
30227 if (p > 0)
30228 pointer = Fget_char_property (make_number (p),
30229 Qpointer, w->contents);
30230 }
30231 }
30232 else if (BUFFERP (obj)
30233 && charpos >= BEGV
30234 && charpos < ZV)
30235 pointer = Fget_text_property (make_number (charpos),
30236 Qpointer, obj);
30237 }
30238 }
30239 #endif /* HAVE_WINDOW_SYSTEM */
30240
30241 BEGV = obegv;
30242 ZV = ozv;
30243 current_buffer = obuf;
30244 SAFE_FREE ();
30245 }
30246
30247 set_cursor:
30248
30249 #ifdef HAVE_WINDOW_SYSTEM
30250 if (FRAME_WINDOW_P (f))
30251 define_frame_cursor1 (f, cursor, pointer);
30252 #else
30253 /* This is here to prevent a compiler error, about "label at end of
30254 compound statement". */
30255 return;
30256 #endif
30257 }
30258
30259
30260 /* EXPORT for RIF:
30261 Clear any mouse-face on window W. This function is part of the
30262 redisplay interface, and is called from try_window_id and similar
30263 functions to ensure the mouse-highlight is off. */
30264
30265 void
30266 x_clear_window_mouse_face (struct window *w)
30267 {
30268 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30269 Lisp_Object window;
30270
30271 block_input ();
30272 XSETWINDOW (window, w);
30273 if (EQ (window, hlinfo->mouse_face_window))
30274 clear_mouse_face (hlinfo);
30275 unblock_input ();
30276 }
30277
30278
30279 /* EXPORT:
30280 Just discard the mouse face information for frame F, if any.
30281 This is used when the size of F is changed. */
30282
30283 void
30284 cancel_mouse_face (struct frame *f)
30285 {
30286 Lisp_Object window;
30287 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30288
30289 window = hlinfo->mouse_face_window;
30290 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30291 reset_mouse_highlight (hlinfo);
30292 }
30293
30294
30295 \f
30296 /***********************************************************************
30297 Exposure Events
30298 ***********************************************************************/
30299
30300 #ifdef HAVE_WINDOW_SYSTEM
30301
30302 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30303 which intersects rectangle R. R is in window-relative coordinates. */
30304
30305 static void
30306 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30307 enum glyph_row_area area)
30308 {
30309 struct glyph *first = row->glyphs[area];
30310 struct glyph *end = row->glyphs[area] + row->used[area];
30311 struct glyph *last;
30312 int first_x, start_x, x;
30313
30314 if (area == TEXT_AREA && row->fill_line_p)
30315 /* If row extends face to end of line write the whole line. */
30316 draw_glyphs (w, 0, row, area,
30317 0, row->used[area],
30318 DRAW_NORMAL_TEXT, 0);
30319 else
30320 {
30321 /* Set START_X to the window-relative start position for drawing glyphs of
30322 AREA. The first glyph of the text area can be partially visible.
30323 The first glyphs of other areas cannot. */
30324 start_x = window_box_left_offset (w, area);
30325 x = start_x;
30326 if (area == TEXT_AREA)
30327 x += row->x;
30328
30329 /* Find the first glyph that must be redrawn. */
30330 while (first < end
30331 && x + first->pixel_width < r->x)
30332 {
30333 x += first->pixel_width;
30334 ++first;
30335 }
30336
30337 /* Find the last one. */
30338 last = first;
30339 first_x = x;
30340 /* Use a signed int intermediate value to avoid catastrophic
30341 failures due to comparison between signed and unsigned, when
30342 x is negative (can happen for wide images that are hscrolled). */
30343 int r_end = r->x + r->width;
30344 while (last < end && x < r_end)
30345 {
30346 x += last->pixel_width;
30347 ++last;
30348 }
30349
30350 /* Repaint. */
30351 if (last > first)
30352 draw_glyphs (w, first_x - start_x, row, area,
30353 first - row->glyphs[area], last - row->glyphs[area],
30354 DRAW_NORMAL_TEXT, 0);
30355 }
30356 }
30357
30358
30359 /* Redraw the parts of the glyph row ROW on window W intersecting
30360 rectangle R. R is in window-relative coordinates. Value is
30361 true if mouse-face was overwritten. */
30362
30363 static bool
30364 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30365 {
30366 eassert (row->enabled_p);
30367
30368 if (row->mode_line_p || w->pseudo_window_p)
30369 draw_glyphs (w, 0, row, TEXT_AREA,
30370 0, row->used[TEXT_AREA],
30371 DRAW_NORMAL_TEXT, 0);
30372 else
30373 {
30374 if (row->used[LEFT_MARGIN_AREA])
30375 expose_area (w, row, r, LEFT_MARGIN_AREA);
30376 if (row->used[TEXT_AREA])
30377 expose_area (w, row, r, TEXT_AREA);
30378 if (row->used[RIGHT_MARGIN_AREA])
30379 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30380 draw_row_fringe_bitmaps (w, row);
30381 }
30382
30383 return row->mouse_face_p;
30384 }
30385
30386
30387 /* Redraw those parts of glyphs rows during expose event handling that
30388 overlap other rows. Redrawing of an exposed line writes over parts
30389 of lines overlapping that exposed line; this function fixes that.
30390
30391 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30392 row in W's current matrix that is exposed and overlaps other rows.
30393 LAST_OVERLAPPING_ROW is the last such row. */
30394
30395 static void
30396 expose_overlaps (struct window *w,
30397 struct glyph_row *first_overlapping_row,
30398 struct glyph_row *last_overlapping_row,
30399 XRectangle *r)
30400 {
30401 struct glyph_row *row;
30402
30403 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30404 if (row->overlapping_p)
30405 {
30406 eassert (row->enabled_p && !row->mode_line_p);
30407
30408 row->clip = r;
30409 if (row->used[LEFT_MARGIN_AREA])
30410 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30411
30412 if (row->used[TEXT_AREA])
30413 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30414
30415 if (row->used[RIGHT_MARGIN_AREA])
30416 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30417 row->clip = NULL;
30418 }
30419 }
30420
30421
30422 /* Return true if W's cursor intersects rectangle R. */
30423
30424 static bool
30425 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30426 {
30427 XRectangle cr, result;
30428 struct glyph *cursor_glyph;
30429 struct glyph_row *row;
30430
30431 if (w->phys_cursor.vpos >= 0
30432 && w->phys_cursor.vpos < w->current_matrix->nrows
30433 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30434 row->enabled_p)
30435 && row->cursor_in_fringe_p)
30436 {
30437 /* Cursor is in the fringe. */
30438 cr.x = window_box_right_offset (w,
30439 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30440 ? RIGHT_MARGIN_AREA
30441 : TEXT_AREA));
30442 cr.y = row->y;
30443 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30444 cr.height = row->height;
30445 return x_intersect_rectangles (&cr, r, &result);
30446 }
30447
30448 cursor_glyph = get_phys_cursor_glyph (w);
30449 if (cursor_glyph)
30450 {
30451 /* r is relative to W's box, but w->phys_cursor.x is relative
30452 to left edge of W's TEXT area. Adjust it. */
30453 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30454 cr.y = w->phys_cursor.y;
30455 cr.width = cursor_glyph->pixel_width;
30456 cr.height = w->phys_cursor_height;
30457 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30458 I assume the effect is the same -- and this is portable. */
30459 return x_intersect_rectangles (&cr, r, &result);
30460 }
30461 /* If we don't understand the format, pretend we're not in the hot-spot. */
30462 return false;
30463 }
30464
30465
30466 /* EXPORT:
30467 Draw a vertical window border to the right of window W if W doesn't
30468 have vertical scroll bars. */
30469
30470 void
30471 x_draw_vertical_border (struct window *w)
30472 {
30473 struct frame *f = XFRAME (WINDOW_FRAME (w));
30474
30475 /* We could do better, if we knew what type of scroll-bar the adjacent
30476 windows (on either side) have... But we don't :-(
30477 However, I think this works ok. ++KFS 2003-04-25 */
30478
30479 /* Redraw borders between horizontally adjacent windows. Don't
30480 do it for frames with vertical scroll bars because either the
30481 right scroll bar of a window, or the left scroll bar of its
30482 neighbor will suffice as a border. */
30483 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30484 return;
30485
30486 /* Note: It is necessary to redraw both the left and the right
30487 borders, for when only this single window W is being
30488 redisplayed. */
30489 if (!WINDOW_RIGHTMOST_P (w)
30490 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30491 {
30492 int x0, x1, y0, y1;
30493
30494 window_box_edges (w, &x0, &y0, &x1, &y1);
30495 y1 -= 1;
30496
30497 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30498 x1 -= 1;
30499
30500 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30501 }
30502
30503 if (!WINDOW_LEFTMOST_P (w)
30504 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30505 {
30506 int x0, x1, y0, y1;
30507
30508 window_box_edges (w, &x0, &y0, &x1, &y1);
30509 y1 -= 1;
30510
30511 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30512 x0 -= 1;
30513
30514 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30515 }
30516 }
30517
30518
30519 /* Draw window dividers for window W. */
30520
30521 void
30522 x_draw_right_divider (struct window *w)
30523 {
30524 struct frame *f = WINDOW_XFRAME (w);
30525
30526 if (w->mini || w->pseudo_window_p)
30527 return;
30528 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30529 {
30530 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30531 int x1 = WINDOW_RIGHT_EDGE_X (w);
30532 int y0 = WINDOW_TOP_EDGE_Y (w);
30533 /* The bottom divider prevails. */
30534 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30535
30536 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30537 }
30538 }
30539
30540 static void
30541 x_draw_bottom_divider (struct window *w)
30542 {
30543 struct frame *f = XFRAME (WINDOW_FRAME (w));
30544
30545 if (w->mini || w->pseudo_window_p)
30546 return;
30547 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30548 {
30549 int x0 = WINDOW_LEFT_EDGE_X (w);
30550 int x1 = WINDOW_RIGHT_EDGE_X (w);
30551 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30552 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30553
30554 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30555 }
30556 }
30557
30558 /* Redraw the part of window W intersection rectangle FR. Pixel
30559 coordinates in FR are frame-relative. Call this function with
30560 input blocked. Value is true if the exposure overwrites
30561 mouse-face. */
30562
30563 static bool
30564 expose_window (struct window *w, XRectangle *fr)
30565 {
30566 struct frame *f = XFRAME (w->frame);
30567 XRectangle wr, r;
30568 bool mouse_face_overwritten_p = false;
30569
30570 /* If window is not yet fully initialized, do nothing. This can
30571 happen when toolkit scroll bars are used and a window is split.
30572 Reconfiguring the scroll bar will generate an expose for a newly
30573 created window. */
30574 if (w->current_matrix == NULL)
30575 return false;
30576
30577 /* When we're currently updating the window, display and current
30578 matrix usually don't agree. Arrange for a thorough display
30579 later. */
30580 if (w->must_be_updated_p)
30581 {
30582 SET_FRAME_GARBAGED (f);
30583 return false;
30584 }
30585
30586 /* Frame-relative pixel rectangle of W. */
30587 wr.x = WINDOW_LEFT_EDGE_X (w);
30588 wr.y = WINDOW_TOP_EDGE_Y (w);
30589 wr.width = WINDOW_PIXEL_WIDTH (w);
30590 wr.height = WINDOW_PIXEL_HEIGHT (w);
30591
30592 if (x_intersect_rectangles (fr, &wr, &r))
30593 {
30594 int yb = window_text_bottom_y (w);
30595 struct glyph_row *row;
30596 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30597
30598 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30599 r.x, r.y, r.width, r.height));
30600
30601 /* Convert to window coordinates. */
30602 r.x -= WINDOW_LEFT_EDGE_X (w);
30603 r.y -= WINDOW_TOP_EDGE_Y (w);
30604
30605 /* Turn off the cursor. */
30606 bool cursor_cleared_p = (!w->pseudo_window_p
30607 && phys_cursor_in_rect_p (w, &r));
30608 if (cursor_cleared_p)
30609 x_clear_cursor (w);
30610
30611 /* If the row containing the cursor extends face to end of line,
30612 then expose_area might overwrite the cursor outside the
30613 rectangle and thus notice_overwritten_cursor might clear
30614 w->phys_cursor_on_p. We remember the original value and
30615 check later if it is changed. */
30616 bool phys_cursor_on_p = w->phys_cursor_on_p;
30617
30618 /* Use a signed int intermediate value to avoid catastrophic
30619 failures due to comparison between signed and unsigned, when
30620 y0 or y1 is negative (can happen for tall images). */
30621 int r_bottom = r.y + r.height;
30622
30623 /* Update lines intersecting rectangle R. */
30624 first_overlapping_row = last_overlapping_row = NULL;
30625 for (row = w->current_matrix->rows;
30626 row->enabled_p;
30627 ++row)
30628 {
30629 int y0 = row->y;
30630 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30631
30632 if ((y0 >= r.y && y0 < r_bottom)
30633 || (y1 > r.y && y1 < r_bottom)
30634 || (r.y >= y0 && r.y < y1)
30635 || (r_bottom > y0 && r_bottom < y1))
30636 {
30637 /* A header line may be overlapping, but there is no need
30638 to fix overlapping areas for them. KFS 2005-02-12 */
30639 if (row->overlapping_p && !row->mode_line_p)
30640 {
30641 if (first_overlapping_row == NULL)
30642 first_overlapping_row = row;
30643 last_overlapping_row = row;
30644 }
30645
30646 row->clip = fr;
30647 if (expose_line (w, row, &r))
30648 mouse_face_overwritten_p = true;
30649 row->clip = NULL;
30650 }
30651 else if (row->overlapping_p)
30652 {
30653 /* We must redraw a row overlapping the exposed area. */
30654 if (y0 < r.y
30655 ? y0 + row->phys_height > r.y
30656 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30657 {
30658 if (first_overlapping_row == NULL)
30659 first_overlapping_row = row;
30660 last_overlapping_row = row;
30661 }
30662 }
30663
30664 if (y1 >= yb)
30665 break;
30666 }
30667
30668 /* Display the mode line if there is one. */
30669 if (WINDOW_WANTS_MODELINE_P (w)
30670 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30671 row->enabled_p)
30672 && row->y < r_bottom)
30673 {
30674 if (expose_line (w, row, &r))
30675 mouse_face_overwritten_p = true;
30676 }
30677
30678 if (!w->pseudo_window_p)
30679 {
30680 /* Fix the display of overlapping rows. */
30681 if (first_overlapping_row)
30682 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30683 fr);
30684
30685 /* Draw border between windows. */
30686 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30687 x_draw_right_divider (w);
30688 else
30689 x_draw_vertical_border (w);
30690
30691 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30692 x_draw_bottom_divider (w);
30693
30694 /* Turn the cursor on again. */
30695 if (cursor_cleared_p
30696 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30697 update_window_cursor (w, true);
30698 }
30699 }
30700
30701 return mouse_face_overwritten_p;
30702 }
30703
30704
30705
30706 /* Redraw (parts) of all windows in the window tree rooted at W that
30707 intersect R. R contains frame pixel coordinates. Value is
30708 true if the exposure overwrites mouse-face. */
30709
30710 static bool
30711 expose_window_tree (struct window *w, XRectangle *r)
30712 {
30713 struct frame *f = XFRAME (w->frame);
30714 bool mouse_face_overwritten_p = false;
30715
30716 while (w && !FRAME_GARBAGED_P (f))
30717 {
30718 mouse_face_overwritten_p
30719 |= (WINDOWP (w->contents)
30720 ? expose_window_tree (XWINDOW (w->contents), r)
30721 : expose_window (w, r));
30722
30723 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30724 }
30725
30726 return mouse_face_overwritten_p;
30727 }
30728
30729
30730 /* EXPORT:
30731 Redisplay an exposed area of frame F. X and Y are the upper-left
30732 corner of the exposed rectangle. W and H are width and height of
30733 the exposed area. All are pixel values. W or H zero means redraw
30734 the entire frame. */
30735
30736 void
30737 expose_frame (struct frame *f, int x, int y, int w, int h)
30738 {
30739 XRectangle r;
30740 bool mouse_face_overwritten_p = false;
30741
30742 TRACE ((stderr, "expose_frame "));
30743
30744 /* No need to redraw if frame will be redrawn soon. */
30745 if (FRAME_GARBAGED_P (f))
30746 {
30747 TRACE ((stderr, " garbaged\n"));
30748 return;
30749 }
30750
30751 /* If basic faces haven't been realized yet, there is no point in
30752 trying to redraw anything. This can happen when we get an expose
30753 event while Emacs is starting, e.g. by moving another window. */
30754 if (FRAME_FACE_CACHE (f) == NULL
30755 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30756 {
30757 TRACE ((stderr, " no faces\n"));
30758 return;
30759 }
30760
30761 if (w == 0 || h == 0)
30762 {
30763 r.x = r.y = 0;
30764 r.width = FRAME_TEXT_WIDTH (f);
30765 r.height = FRAME_TEXT_HEIGHT (f);
30766 }
30767 else
30768 {
30769 r.x = x;
30770 r.y = y;
30771 r.width = w;
30772 r.height = h;
30773 }
30774
30775 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30776 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30777
30778 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30779 if (WINDOWP (f->tool_bar_window))
30780 mouse_face_overwritten_p
30781 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30782 #endif
30783
30784 #ifdef HAVE_X_WINDOWS
30785 #ifndef MSDOS
30786 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30787 if (WINDOWP (f->menu_bar_window))
30788 mouse_face_overwritten_p
30789 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30790 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30791 #endif
30792 #endif
30793
30794 /* Some window managers support a focus-follows-mouse style with
30795 delayed raising of frames. Imagine a partially obscured frame,
30796 and moving the mouse into partially obscured mouse-face on that
30797 frame. The visible part of the mouse-face will be highlighted,
30798 then the WM raises the obscured frame. With at least one WM, KDE
30799 2.1, Emacs is not getting any event for the raising of the frame
30800 (even tried with SubstructureRedirectMask), only Expose events.
30801 These expose events will draw text normally, i.e. not
30802 highlighted. Which means we must redo the highlight here.
30803 Subsume it under ``we love X''. --gerd 2001-08-15 */
30804 /* Included in Windows version because Windows most likely does not
30805 do the right thing if any third party tool offers
30806 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30807 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30808 {
30809 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30810 if (f == hlinfo->mouse_face_mouse_frame)
30811 {
30812 int mouse_x = hlinfo->mouse_face_mouse_x;
30813 int mouse_y = hlinfo->mouse_face_mouse_y;
30814 clear_mouse_face (hlinfo);
30815 note_mouse_highlight (f, mouse_x, mouse_y);
30816 }
30817 }
30818 }
30819
30820
30821 /* EXPORT:
30822 Determine the intersection of two rectangles R1 and R2. Return
30823 the intersection in *RESULT. Value is true if RESULT is not
30824 empty. */
30825
30826 bool
30827 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30828 {
30829 XRectangle *left, *right;
30830 XRectangle *upper, *lower;
30831 bool intersection_p = false;
30832
30833 /* Rearrange so that R1 is the left-most rectangle. */
30834 if (r1->x < r2->x)
30835 left = r1, right = r2;
30836 else
30837 left = r2, right = r1;
30838
30839 /* X0 of the intersection is right.x0, if this is inside R1,
30840 otherwise there is no intersection. */
30841 if (right->x <= left->x + left->width)
30842 {
30843 result->x = right->x;
30844
30845 /* The right end of the intersection is the minimum of
30846 the right ends of left and right. */
30847 result->width = (min (left->x + left->width, right->x + right->width)
30848 - result->x);
30849
30850 /* Same game for Y. */
30851 if (r1->y < r2->y)
30852 upper = r1, lower = r2;
30853 else
30854 upper = r2, lower = r1;
30855
30856 /* The upper end of the intersection is lower.y0, if this is inside
30857 of upper. Otherwise, there is no intersection. */
30858 if (lower->y <= upper->y + upper->height)
30859 {
30860 result->y = lower->y;
30861
30862 /* The lower end of the intersection is the minimum of the lower
30863 ends of upper and lower. */
30864 result->height = (min (lower->y + lower->height,
30865 upper->y + upper->height)
30866 - result->y);
30867 intersection_p = true;
30868 }
30869 }
30870
30871 return intersection_p;
30872 }
30873
30874 #endif /* HAVE_WINDOW_SYSTEM */
30875
30876 \f
30877 /***********************************************************************
30878 Initialization
30879 ***********************************************************************/
30880
30881 void
30882 syms_of_xdisp (void)
30883 {
30884 Vwith_echo_area_save_vector = Qnil;
30885 staticpro (&Vwith_echo_area_save_vector);
30886
30887 Vmessage_stack = Qnil;
30888 staticpro (&Vmessage_stack);
30889
30890 /* Non-nil means don't actually do any redisplay. */
30891 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30892
30893 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30894
30895 DEFVAR_BOOL("inhibit-message", inhibit_message,
30896 doc: /* Non-nil means calls to `message' are not displayed.
30897 They are still logged to the *Messages* buffer. */);
30898 inhibit_message = 0;
30899
30900 message_dolog_marker1 = Fmake_marker ();
30901 staticpro (&message_dolog_marker1);
30902 message_dolog_marker2 = Fmake_marker ();
30903 staticpro (&message_dolog_marker2);
30904 message_dolog_marker3 = Fmake_marker ();
30905 staticpro (&message_dolog_marker3);
30906
30907 #ifdef GLYPH_DEBUG
30908 defsubr (&Sdump_frame_glyph_matrix);
30909 defsubr (&Sdump_glyph_matrix);
30910 defsubr (&Sdump_glyph_row);
30911 defsubr (&Sdump_tool_bar_row);
30912 defsubr (&Strace_redisplay);
30913 defsubr (&Strace_to_stderr);
30914 #endif
30915 #ifdef HAVE_WINDOW_SYSTEM
30916 defsubr (&Stool_bar_height);
30917 defsubr (&Slookup_image_map);
30918 #endif
30919 defsubr (&Sline_pixel_height);
30920 defsubr (&Sformat_mode_line);
30921 defsubr (&Sinvisible_p);
30922 defsubr (&Scurrent_bidi_paragraph_direction);
30923 defsubr (&Swindow_text_pixel_size);
30924 defsubr (&Smove_point_visually);
30925 defsubr (&Sbidi_find_overridden_directionality);
30926
30927 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30928 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30929 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30930 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30931 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30932 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30933 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30934 DEFSYM (Qeval, "eval");
30935 DEFSYM (QCdata, ":data");
30936
30937 /* Names of text properties relevant for redisplay. */
30938 DEFSYM (Qdisplay, "display");
30939 DEFSYM (Qspace_width, "space-width");
30940 DEFSYM (Qraise, "raise");
30941 DEFSYM (Qslice, "slice");
30942 DEFSYM (Qspace, "space");
30943 DEFSYM (Qmargin, "margin");
30944 DEFSYM (Qpointer, "pointer");
30945 DEFSYM (Qleft_margin, "left-margin");
30946 DEFSYM (Qright_margin, "right-margin");
30947 DEFSYM (Qcenter, "center");
30948 DEFSYM (Qline_height, "line-height");
30949 DEFSYM (QCalign_to, ":align-to");
30950 DEFSYM (QCrelative_width, ":relative-width");
30951 DEFSYM (QCrelative_height, ":relative-height");
30952 DEFSYM (QCeval, ":eval");
30953 DEFSYM (QCpropertize, ":propertize");
30954 DEFSYM (QCfile, ":file");
30955 DEFSYM (Qfontified, "fontified");
30956 DEFSYM (Qfontification_functions, "fontification-functions");
30957
30958 /* Name of the face used to highlight trailing whitespace. */
30959 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30960
30961 /* Name and number of the face used to highlight escape glyphs. */
30962 DEFSYM (Qescape_glyph, "escape-glyph");
30963
30964 /* Name and number of the face used to highlight non-breaking spaces. */
30965 DEFSYM (Qnobreak_space, "nobreak-space");
30966
30967 /* The symbol 'image' which is the car of the lists used to represent
30968 images in Lisp. Also a tool bar style. */
30969 DEFSYM (Qimage, "image");
30970
30971 /* Tool bar styles. */
30972 DEFSYM (Qtext, "text");
30973 DEFSYM (Qboth, "both");
30974 DEFSYM (Qboth_horiz, "both-horiz");
30975 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30976
30977 /* The image map types. */
30978 DEFSYM (QCmap, ":map");
30979 DEFSYM (QCpointer, ":pointer");
30980 DEFSYM (Qrect, "rect");
30981 DEFSYM (Qcircle, "circle");
30982 DEFSYM (Qpoly, "poly");
30983
30984 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30985
30986 DEFSYM (Qgrow_only, "grow-only");
30987 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30988 DEFSYM (Qposition, "position");
30989 DEFSYM (Qbuffer_position, "buffer-position");
30990 DEFSYM (Qobject, "object");
30991
30992 /* Cursor shapes. */
30993 DEFSYM (Qbar, "bar");
30994 DEFSYM (Qhbar, "hbar");
30995 DEFSYM (Qbox, "box");
30996 DEFSYM (Qhollow, "hollow");
30997
30998 /* Pointer shapes. */
30999 DEFSYM (Qhand, "hand");
31000 DEFSYM (Qarrow, "arrow");
31001 /* also Qtext */
31002
31003 DEFSYM (Qdragging, "dragging");
31004
31005 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
31006
31007 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
31008 staticpro (&list_of_error);
31009
31010 /* Values of those variables at last redisplay are stored as
31011 properties on 'overlay-arrow-position' symbol. However, if
31012 Voverlay_arrow_position is a marker, last-arrow-position is its
31013 numerical position. */
31014 DEFSYM (Qlast_arrow_position, "last-arrow-position");
31015 DEFSYM (Qlast_arrow_string, "last-arrow-string");
31016
31017 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
31018 properties on a symbol in overlay-arrow-variable-list. */
31019 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
31020 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
31021
31022 echo_buffer[0] = echo_buffer[1] = Qnil;
31023 staticpro (&echo_buffer[0]);
31024 staticpro (&echo_buffer[1]);
31025
31026 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
31027 staticpro (&echo_area_buffer[0]);
31028 staticpro (&echo_area_buffer[1]);
31029
31030 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
31031 staticpro (&Vmessages_buffer_name);
31032
31033 mode_line_proptrans_alist = Qnil;
31034 staticpro (&mode_line_proptrans_alist);
31035 mode_line_string_list = Qnil;
31036 staticpro (&mode_line_string_list);
31037 mode_line_string_face = Qnil;
31038 staticpro (&mode_line_string_face);
31039 mode_line_string_face_prop = Qnil;
31040 staticpro (&mode_line_string_face_prop);
31041 Vmode_line_unwind_vector = Qnil;
31042 staticpro (&Vmode_line_unwind_vector);
31043
31044 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
31045
31046 help_echo_string = Qnil;
31047 staticpro (&help_echo_string);
31048 help_echo_object = Qnil;
31049 staticpro (&help_echo_object);
31050 help_echo_window = Qnil;
31051 staticpro (&help_echo_window);
31052 previous_help_echo_string = Qnil;
31053 staticpro (&previous_help_echo_string);
31054 help_echo_pos = -1;
31055
31056 DEFSYM (Qright_to_left, "right-to-left");
31057 DEFSYM (Qleft_to_right, "left-to-right");
31058 defsubr (&Sbidi_resolved_levels);
31059
31060 #ifdef HAVE_WINDOW_SYSTEM
31061 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
31062 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
31063 For example, if a block cursor is over a tab, it will be drawn as
31064 wide as that tab on the display. */);
31065 x_stretch_cursor_p = 0;
31066 #endif
31067
31068 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
31069 doc: /* Non-nil means highlight trailing whitespace.
31070 The face used for trailing whitespace is `trailing-whitespace'. */);
31071 Vshow_trailing_whitespace = Qnil;
31072
31073 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
31074 doc: /* Control highlighting of non-ASCII space and hyphen chars.
31075 If the value is t, Emacs highlights non-ASCII chars which have the
31076 same appearance as an ASCII space or hyphen, using the `nobreak-space'
31077 or `escape-glyph' face respectively.
31078
31079 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
31080 U+2011 (non-breaking hyphen) are affected.
31081
31082 Any other non-nil value means to display these characters as a escape
31083 glyph followed by an ordinary space or hyphen.
31084
31085 A value of nil means no special handling of these characters. */);
31086 Vnobreak_char_display = Qt;
31087
31088 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
31089 doc: /* The pointer shape to show in void text areas.
31090 A value of nil means to show the text pointer. Other options are
31091 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
31092 `hourglass'. */);
31093 Vvoid_text_area_pointer = Qarrow;
31094
31095 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
31096 doc: /* Non-nil means don't actually do any redisplay.
31097 This is used for internal purposes. */);
31098 Vinhibit_redisplay = Qnil;
31099
31100 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
31101 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
31102 Vglobal_mode_string = Qnil;
31103
31104 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31105 doc: /* Marker for where to display an arrow on top of the buffer text.
31106 This must be the beginning of a line in order to work.
31107 See also `overlay-arrow-string'. */);
31108 Voverlay_arrow_position = Qnil;
31109
31110 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31111 doc: /* String to display as an arrow in non-window frames.
31112 See also `overlay-arrow-position'. */);
31113 Voverlay_arrow_string = build_pure_c_string ("=>");
31114
31115 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31116 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31117 The symbols on this list are examined during redisplay to determine
31118 where to display overlay arrows. */);
31119 Voverlay_arrow_variable_list
31120 = list1 (intern_c_string ("overlay-arrow-position"));
31121
31122 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31123 doc: /* The number of lines to try scrolling a window by when point moves out.
31124 If that fails to bring point back on frame, point is centered instead.
31125 If this is zero, point is always centered after it moves off frame.
31126 If you want scrolling to always be a line at a time, you should set
31127 `scroll-conservatively' to a large value rather than set this to 1. */);
31128
31129 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31130 doc: /* Scroll up to this many lines, to bring point back on screen.
31131 If point moves off-screen, redisplay will scroll by up to
31132 `scroll-conservatively' lines in order to bring point just barely
31133 onto the screen again. If that cannot be done, then redisplay
31134 recenters point as usual.
31135
31136 If the value is greater than 100, redisplay will never recenter point,
31137 but will always scroll just enough text to bring point into view, even
31138 if you move far away.
31139
31140 A value of zero means always recenter point if it moves off screen. */);
31141 scroll_conservatively = 0;
31142
31143 DEFVAR_INT ("scroll-margin", scroll_margin,
31144 doc: /* Number of lines of margin at the top and bottom of a window.
31145 Recenter the window whenever point gets within this many lines
31146 of the top or bottom of the window. */);
31147 scroll_margin = 0;
31148
31149 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31150 doc: /* Pixels per inch value for non-window system displays.
31151 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31152 Vdisplay_pixels_per_inch = make_float (72.0);
31153
31154 #ifdef GLYPH_DEBUG
31155 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31156 #endif
31157
31158 DEFVAR_LISP ("truncate-partial-width-windows",
31159 Vtruncate_partial_width_windows,
31160 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31161 For an integer value, truncate lines in each window narrower than the
31162 full frame width, provided the window width is less than that integer;
31163 otherwise, respect the value of `truncate-lines'.
31164
31165 For any other non-nil value, truncate lines in all windows that do
31166 not span the full frame width.
31167
31168 A value of nil means to respect the value of `truncate-lines'.
31169
31170 If `word-wrap' is enabled, you might want to reduce this. */);
31171 Vtruncate_partial_width_windows = make_number (50);
31172
31173 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31174 doc: /* Maximum buffer size for which line number should be displayed.
31175 If the buffer is bigger than this, the line number does not appear
31176 in the mode line. A value of nil means no limit. */);
31177 Vline_number_display_limit = Qnil;
31178
31179 DEFVAR_INT ("line-number-display-limit-width",
31180 line_number_display_limit_width,
31181 doc: /* Maximum line width (in characters) for line number display.
31182 If the average length of the lines near point is bigger than this, then the
31183 line number may be omitted from the mode line. */);
31184 line_number_display_limit_width = 200;
31185
31186 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31187 doc: /* Non-nil means highlight region even in nonselected windows. */);
31188 highlight_nonselected_windows = false;
31189
31190 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31191 doc: /* Non-nil if more than one frame is visible on this display.
31192 Minibuffer-only frames don't count, but iconified frames do.
31193 This variable is not guaranteed to be accurate except while processing
31194 `frame-title-format' and `icon-title-format'. */);
31195
31196 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31197 doc: /* Template for displaying the title bar of visible frames.
31198 (Assuming the window manager supports this feature.)
31199
31200 This variable has the same structure as `mode-line-format', except that
31201 the %c and %l constructs are ignored. It is used only on frames for
31202 which no explicit name has been set (see `modify-frame-parameters'). */);
31203
31204 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31205 doc: /* Template for displaying the title bar of an iconified frame.
31206 (Assuming the window manager supports this feature.)
31207 This variable has the same structure as `mode-line-format' (which see),
31208 and is used only on frames for which no explicit name has been set
31209 (see `modify-frame-parameters'). */);
31210 Vicon_title_format
31211 = Vframe_title_format
31212 = listn (CONSTYPE_PURE, 3,
31213 intern_c_string ("multiple-frames"),
31214 build_pure_c_string ("%b"),
31215 listn (CONSTYPE_PURE, 4,
31216 empty_unibyte_string,
31217 intern_c_string ("invocation-name"),
31218 build_pure_c_string ("@"),
31219 intern_c_string ("system-name")));
31220
31221 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31222 doc: /* Maximum number of lines to keep in the message log buffer.
31223 If nil, disable message logging. If t, log messages but don't truncate
31224 the buffer when it becomes large. */);
31225 Vmessage_log_max = make_number (1000);
31226
31227 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
31228 doc: /* Functions called during redisplay, if window sizes have changed.
31229 The value should be a list of functions that take one argument.
31230 During the first part of redisplay, for each frame, if any of its windows
31231 have changed size since the last redisplay, or have been split or deleted,
31232 all the functions in the list are called, with the frame as argument.
31233 If redisplay decides to resize the minibuffer window, it calls these
31234 functions on behalf of that as well. */);
31235 Vwindow_size_change_functions = Qnil;
31236
31237 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31238 doc: /* List of functions to call before redisplaying a window with scrolling.
31239 Each function is called with two arguments, the window and its new
31240 display-start position.
31241 These functions are called whenever the `window-start' marker is modified,
31242 either to point into another buffer (e.g. via `set-window-buffer') or another
31243 place in the same buffer.
31244 Note that the value of `window-end' is not valid when these functions are
31245 called.
31246
31247 Warning: Do not use this feature to alter the way the window
31248 is scrolled. It is not designed for that, and such use probably won't
31249 work. */);
31250 Vwindow_scroll_functions = Qnil;
31251
31252 DEFVAR_LISP ("window-text-change-functions",
31253 Vwindow_text_change_functions,
31254 doc: /* Functions to call in redisplay when text in the window might change. */);
31255 Vwindow_text_change_functions = Qnil;
31256
31257 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31258 doc: /* Functions called when redisplay of a window reaches the end trigger.
31259 Each function is called with two arguments, the window and the end trigger value.
31260 See `set-window-redisplay-end-trigger'. */);
31261 Vredisplay_end_trigger_functions = Qnil;
31262
31263 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31264 doc: /* Non-nil means autoselect window with mouse pointer.
31265 If nil, do not autoselect windows.
31266 A positive number means delay autoselection by that many seconds: a
31267 window is autoselected only after the mouse has remained in that
31268 window for the duration of the delay.
31269 A negative number has a similar effect, but causes windows to be
31270 autoselected only after the mouse has stopped moving. (Because of
31271 the way Emacs compares mouse events, you will occasionally wait twice
31272 that time before the window gets selected.)
31273 Any other value means to autoselect window instantaneously when the
31274 mouse pointer enters it.
31275
31276 Autoselection selects the minibuffer only if it is active, and never
31277 unselects the minibuffer if it is active.
31278
31279 When customizing this variable make sure that the actual value of
31280 `focus-follows-mouse' matches the behavior of your window manager. */);
31281 Vmouse_autoselect_window = Qnil;
31282
31283 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31284 doc: /* Non-nil means automatically resize tool-bars.
31285 This dynamically changes the tool-bar's height to the minimum height
31286 that is needed to make all tool-bar items visible.
31287 If value is `grow-only', the tool-bar's height is only increased
31288 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31289 Vauto_resize_tool_bars = Qt;
31290
31291 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31292 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31293 auto_raise_tool_bar_buttons_p = true;
31294
31295 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31296 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31297 make_cursor_line_fully_visible_p = true;
31298
31299 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31300 doc: /* Border below tool-bar in pixels.
31301 If an integer, use it as the height of the border.
31302 If it is one of `internal-border-width' or `border-width', use the
31303 value of the corresponding frame parameter.
31304 Otherwise, no border is added below the tool-bar. */);
31305 Vtool_bar_border = Qinternal_border_width;
31306
31307 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31308 doc: /* Margin around tool-bar buttons in pixels.
31309 If an integer, use that for both horizontal and vertical margins.
31310 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31311 HORZ specifying the horizontal margin, and VERT specifying the
31312 vertical margin. */);
31313 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31314
31315 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31316 doc: /* Relief thickness of tool-bar buttons. */);
31317 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31318
31319 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31320 doc: /* Tool bar style to use.
31321 It can be one of
31322 image - show images only
31323 text - show text only
31324 both - show both, text below image
31325 both-horiz - show text to the right of the image
31326 text-image-horiz - show text to the left of the image
31327 any other - use system default or image if no system default.
31328
31329 This variable only affects the GTK+ toolkit version of Emacs. */);
31330 Vtool_bar_style = Qnil;
31331
31332 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31333 doc: /* Maximum number of characters a label can have to be shown.
31334 The tool bar style must also show labels for this to have any effect, see
31335 `tool-bar-style'. */);
31336 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31337
31338 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31339 doc: /* List of functions to call to fontify regions of text.
31340 Each function is called with one argument POS. Functions must
31341 fontify a region starting at POS in the current buffer, and give
31342 fontified regions the property `fontified'. */);
31343 Vfontification_functions = Qnil;
31344 Fmake_variable_buffer_local (Qfontification_functions);
31345
31346 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31347 unibyte_display_via_language_environment,
31348 doc: /* Non-nil means display unibyte text according to language environment.
31349 Specifically, this means that raw bytes in the range 160-255 decimal
31350 are displayed by converting them to the equivalent multibyte characters
31351 according to the current language environment. As a result, they are
31352 displayed according to the current fontset.
31353
31354 Note that this variable affects only how these bytes are displayed,
31355 but does not change the fact they are interpreted as raw bytes. */);
31356 unibyte_display_via_language_environment = false;
31357
31358 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31359 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31360 If a float, it specifies a fraction of the mini-window frame's height.
31361 If an integer, it specifies a number of lines. */);
31362 Vmax_mini_window_height = make_float (0.25);
31363
31364 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31365 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31366 A value of nil means don't automatically resize mini-windows.
31367 A value of t means resize them to fit the text displayed in them.
31368 A value of `grow-only', the default, means let mini-windows grow only;
31369 they return to their normal size when the minibuffer is closed, or the
31370 echo area becomes empty. */);
31371 Vresize_mini_windows = Qgrow_only;
31372
31373 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31374 doc: /* Alist specifying how to blink the cursor off.
31375 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31376 `cursor-type' frame-parameter or variable equals ON-STATE,
31377 comparing using `equal', Emacs uses OFF-STATE to specify
31378 how to blink it off. ON-STATE and OFF-STATE are values for
31379 the `cursor-type' frame parameter.
31380
31381 If a frame's ON-STATE has no entry in this list,
31382 the frame's other specifications determine how to blink the cursor off. */);
31383 Vblink_cursor_alist = Qnil;
31384
31385 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31386 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31387 If non-nil, windows are automatically scrolled horizontally to make
31388 point visible. */);
31389 automatic_hscrolling_p = true;
31390 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31391
31392 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31393 doc: /* How many columns away from the window edge point is allowed to get
31394 before automatic hscrolling will horizontally scroll the window. */);
31395 hscroll_margin = 5;
31396
31397 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31398 doc: /* How many columns to scroll the window when point gets too close to the edge.
31399 When point is less than `hscroll-margin' columns from the window
31400 edge, automatic hscrolling will scroll the window by the amount of columns
31401 determined by this variable. If its value is a positive integer, scroll that
31402 many columns. If it's a positive floating-point number, it specifies the
31403 fraction of the window's width to scroll. If it's nil or zero, point will be
31404 centered horizontally after the scroll. Any other value, including negative
31405 numbers, are treated as if the value were zero.
31406
31407 Automatic hscrolling always moves point outside the scroll margin, so if
31408 point was more than scroll step columns inside the margin, the window will
31409 scroll more than the value given by the scroll step.
31410
31411 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31412 and `scroll-right' overrides this variable's effect. */);
31413 Vhscroll_step = make_number (0);
31414
31415 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31416 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31417 Bind this around calls to `message' to let it take effect. */);
31418 message_truncate_lines = false;
31419
31420 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31421 doc: /* Normal hook run to update the menu bar definitions.
31422 Redisplay runs this hook before it redisplays the menu bar.
31423 This is used to update menus such as Buffers, whose contents depend on
31424 various data. */);
31425 Vmenu_bar_update_hook = Qnil;
31426
31427 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31428 doc: /* Frame for which we are updating a menu.
31429 The enable predicate for a menu binding should check this variable. */);
31430 Vmenu_updating_frame = Qnil;
31431
31432 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31433 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31434 inhibit_menubar_update = false;
31435
31436 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31437 doc: /* Prefix prepended to all continuation lines at display time.
31438 The value may be a string, an image, or a stretch-glyph; it is
31439 interpreted in the same way as the value of a `display' text property.
31440
31441 This variable is overridden by any `wrap-prefix' text or overlay
31442 property.
31443
31444 To add a prefix to non-continuation lines, use `line-prefix'. */);
31445 Vwrap_prefix = Qnil;
31446 DEFSYM (Qwrap_prefix, "wrap-prefix");
31447 Fmake_variable_buffer_local (Qwrap_prefix);
31448
31449 DEFVAR_LISP ("line-prefix", Vline_prefix,
31450 doc: /* Prefix prepended to all non-continuation lines at display time.
31451 The value may be a string, an image, or a stretch-glyph; it is
31452 interpreted in the same way as the value of a `display' text property.
31453
31454 This variable is overridden by any `line-prefix' text or overlay
31455 property.
31456
31457 To add a prefix to continuation lines, use `wrap-prefix'. */);
31458 Vline_prefix = Qnil;
31459 DEFSYM (Qline_prefix, "line-prefix");
31460 Fmake_variable_buffer_local (Qline_prefix);
31461
31462 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31463 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31464 inhibit_eval_during_redisplay = false;
31465
31466 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31467 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31468 inhibit_free_realized_faces = false;
31469
31470 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31471 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31472 Intended for use during debugging and for testing bidi display;
31473 see biditest.el in the test suite. */);
31474 inhibit_bidi_mirroring = false;
31475
31476 #ifdef GLYPH_DEBUG
31477 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31478 doc: /* Inhibit try_window_id display optimization. */);
31479 inhibit_try_window_id = false;
31480
31481 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31482 doc: /* Inhibit try_window_reusing display optimization. */);
31483 inhibit_try_window_reusing = false;
31484
31485 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31486 doc: /* Inhibit try_cursor_movement display optimization. */);
31487 inhibit_try_cursor_movement = false;
31488 #endif /* GLYPH_DEBUG */
31489
31490 DEFVAR_INT ("overline-margin", overline_margin,
31491 doc: /* Space between overline and text, in pixels.
31492 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31493 margin to the character height. */);
31494 overline_margin = 2;
31495
31496 DEFVAR_INT ("underline-minimum-offset",
31497 underline_minimum_offset,
31498 doc: /* Minimum distance between baseline and underline.
31499 This can improve legibility of underlined text at small font sizes,
31500 particularly when using variable `x-use-underline-position-properties'
31501 with fonts that specify an UNDERLINE_POSITION relatively close to the
31502 baseline. The default value is 1. */);
31503 underline_minimum_offset = 1;
31504
31505 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31506 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31507 This feature only works when on a window system that can change
31508 cursor shapes. */);
31509 display_hourglass_p = true;
31510
31511 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31512 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31513 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31514
31515 #ifdef HAVE_WINDOW_SYSTEM
31516 hourglass_atimer = NULL;
31517 hourglass_shown_p = false;
31518 #endif /* HAVE_WINDOW_SYSTEM */
31519
31520 /* Name of the face used to display glyphless characters. */
31521 DEFSYM (Qglyphless_char, "glyphless-char");
31522
31523 /* Method symbols for Vglyphless_char_display. */
31524 DEFSYM (Qhex_code, "hex-code");
31525 DEFSYM (Qempty_box, "empty-box");
31526 DEFSYM (Qthin_space, "thin-space");
31527 DEFSYM (Qzero_width, "zero-width");
31528
31529 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31530 doc: /* Function run just before redisplay.
31531 It is called with one argument, which is the set of windows that are to
31532 be redisplayed. This set can be nil (meaning, only the selected window),
31533 or t (meaning all windows). */);
31534 Vpre_redisplay_function = intern ("ignore");
31535
31536 /* Symbol for the purpose of Vglyphless_char_display. */
31537 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31538 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31539
31540 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31541 doc: /* Char-table defining glyphless characters.
31542 Each element, if non-nil, should be one of the following:
31543 an ASCII acronym string: display this string in a box
31544 `hex-code': display the hexadecimal code of a character in a box
31545 `empty-box': display as an empty box
31546 `thin-space': display as 1-pixel width space
31547 `zero-width': don't display
31548 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31549 display method for graphical terminals and text terminals respectively.
31550 GRAPHICAL and TEXT should each have one of the values listed above.
31551
31552 The char-table has one extra slot to control the display of a character for
31553 which no font is found. This slot only takes effect on graphical terminals.
31554 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31555 `thin-space'. The default is `empty-box'.
31556
31557 If a character has a non-nil entry in an active display table, the
31558 display table takes effect; in this case, Emacs does not consult
31559 `glyphless-char-display' at all. */);
31560 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31561 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31562 Qempty_box);
31563
31564 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31565 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31566 Vdebug_on_message = Qnil;
31567
31568 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31569 doc: /* */);
31570 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
31571
31572 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31573 doc: /* */);
31574 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
31575
31576 DEFVAR_LISP ("redisplay--variables", Vredisplay__variables,
31577 doc: /* A hash-table of variables changing which triggers a thorough redisplay. */);
31578 Vredisplay__variables = Qnil;
31579 }
31580
31581
31582 /* Initialize this module when Emacs starts. */
31583
31584 void
31585 init_xdisp (void)
31586 {
31587 CHARPOS (this_line_start_pos) = 0;
31588
31589 if (!noninteractive)
31590 {
31591 struct window *m = XWINDOW (minibuf_window);
31592 Lisp_Object frame = m->frame;
31593 struct frame *f = XFRAME (frame);
31594 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31595 struct window *r = XWINDOW (root);
31596 int i;
31597
31598 echo_area_window = minibuf_window;
31599
31600 r->top_line = FRAME_TOP_MARGIN (f);
31601 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31602 r->total_cols = FRAME_COLS (f);
31603 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31604 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31605 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31606
31607 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31608 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31609 m->total_cols = FRAME_COLS (f);
31610 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31611 m->total_lines = 1;
31612 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31613
31614 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31615 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31616 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31617
31618 /* The default ellipsis glyphs `...'. */
31619 for (i = 0; i < 3; ++i)
31620 default_invis_vector[i] = make_number ('.');
31621 }
31622
31623 {
31624 /* Allocate the buffer for frame titles.
31625 Also used for `format-mode-line'. */
31626 int size = 100;
31627 mode_line_noprop_buf = xmalloc (size);
31628 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31629 mode_line_noprop_ptr = mode_line_noprop_buf;
31630 mode_line_target = MODE_LINE_DISPLAY;
31631 }
31632
31633 help_echo_showing_p = false;
31634 }
31635
31636 #ifdef HAVE_WINDOW_SYSTEM
31637
31638 /* Platform-independent portion of hourglass implementation. */
31639
31640 /* Timer function of hourglass_atimer. */
31641
31642 static void
31643 show_hourglass (struct atimer *timer)
31644 {
31645 /* The timer implementation will cancel this timer automatically
31646 after this function has run. Set hourglass_atimer to null
31647 so that we know the timer doesn't have to be canceled. */
31648 hourglass_atimer = NULL;
31649
31650 if (!hourglass_shown_p)
31651 {
31652 Lisp_Object tail, frame;
31653
31654 block_input ();
31655
31656 FOR_EACH_FRAME (tail, frame)
31657 {
31658 struct frame *f = XFRAME (frame);
31659
31660 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31661 && FRAME_RIF (f)->show_hourglass)
31662 FRAME_RIF (f)->show_hourglass (f);
31663 }
31664
31665 hourglass_shown_p = true;
31666 unblock_input ();
31667 }
31668 }
31669
31670 /* Cancel a currently active hourglass timer, and start a new one. */
31671
31672 void
31673 start_hourglass (void)
31674 {
31675 struct timespec delay;
31676
31677 cancel_hourglass ();
31678
31679 if (INTEGERP (Vhourglass_delay)
31680 && XINT (Vhourglass_delay) > 0)
31681 delay = make_timespec (min (XINT (Vhourglass_delay),
31682 TYPE_MAXIMUM (time_t)),
31683 0);
31684 else if (FLOATP (Vhourglass_delay)
31685 && XFLOAT_DATA (Vhourglass_delay) > 0)
31686 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31687 else
31688 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31689
31690 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31691 show_hourglass, NULL);
31692 }
31693
31694 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31695 shown. */
31696
31697 void
31698 cancel_hourglass (void)
31699 {
31700 if (hourglass_atimer)
31701 {
31702 cancel_atimer (hourglass_atimer);
31703 hourglass_atimer = NULL;
31704 }
31705
31706 if (hourglass_shown_p)
31707 {
31708 Lisp_Object tail, frame;
31709
31710 block_input ();
31711
31712 FOR_EACH_FRAME (tail, frame)
31713 {
31714 struct frame *f = XFRAME (frame);
31715
31716 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31717 && FRAME_RIF (f)->hide_hourglass)
31718 FRAME_RIF (f)->hide_hourglass (f);
31719 #ifdef HAVE_NTGUI
31720 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31721 else if (!FRAME_W32_P (f))
31722 w32_arrow_cursor ();
31723 #endif
31724 }
31725
31726 hourglass_shown_p = false;
31727 unblock_input ();
31728 }
31729 }
31730
31731 #endif /* HAVE_WINDOW_SYSTEM */