]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merge branch 'master' of git.sv.gnu.org:/srv/git/emacs
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2015 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
104
105 . try_window
106
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
112
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
117
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
130
131 Desired matrices.
132
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
139
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
146
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
156
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
163
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
175
176 Frame matrices.
177
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
184
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
196
197 Bidirectional display.
198
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
211
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
220
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
239
240 Bidirectional display and character compositions
241
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
246
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
266
267 Moving an iterator in bidirectional text
268 without producing glyphs
269
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
288
289 #include <config.h>
290 #include <stdio.h>
291 #include <limits.h>
292
293 #include "lisp.h"
294 #include "atimer.h"
295 #include "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 /* Remember the current face id in case glyphs specify faces.
4587 IT's face is restored in set_iterator_to_next.
4588 saved_face_id was set to preceding char's face in handle_stop. */
4589 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4590 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4591
4592 /* If the ellipsis represents buffer text, it means we advanced in
4593 the buffer, so we should no longer ignore overlay strings. */
4594 if (it->method == GET_FROM_BUFFER)
4595 it->ignore_overlay_strings_at_pos_p = false;
4596
4597 it->method = GET_FROM_DISPLAY_VECTOR;
4598 it->ellipsis_p = true;
4599 }
4600
4601
4602 \f
4603 /***********************************************************************
4604 'display' property
4605 ***********************************************************************/
4606
4607 /* Set up iterator IT from `display' property at its current position.
4608 Called from handle_stop.
4609 We return HANDLED_RETURN if some part of the display property
4610 overrides the display of the buffer text itself.
4611 Otherwise we return HANDLED_NORMALLY. */
4612
4613 static enum prop_handled
4614 handle_display_prop (struct it *it)
4615 {
4616 Lisp_Object propval, object, overlay;
4617 struct text_pos *position;
4618 ptrdiff_t bufpos;
4619 /* Nonzero if some property replaces the display of the text itself. */
4620 int display_replaced = 0;
4621
4622 if (STRINGP (it->string))
4623 {
4624 object = it->string;
4625 position = &it->current.string_pos;
4626 bufpos = CHARPOS (it->current.pos);
4627 }
4628 else
4629 {
4630 XSETWINDOW (object, it->w);
4631 position = &it->current.pos;
4632 bufpos = CHARPOS (*position);
4633 }
4634
4635 /* Reset those iterator values set from display property values. */
4636 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4637 it->space_width = Qnil;
4638 it->font_height = Qnil;
4639 it->voffset = 0;
4640
4641 /* We don't support recursive `display' properties, i.e. string
4642 values that have a string `display' property, that have a string
4643 `display' property etc. */
4644 if (!it->string_from_display_prop_p)
4645 it->area = TEXT_AREA;
4646
4647 propval = get_char_property_and_overlay (make_number (position->charpos),
4648 Qdisplay, object, &overlay);
4649 if (NILP (propval))
4650 return HANDLED_NORMALLY;
4651 /* Now OVERLAY is the overlay that gave us this property, or nil
4652 if it was a text property. */
4653
4654 if (!STRINGP (it->string))
4655 object = it->w->contents;
4656
4657 display_replaced = handle_display_spec (it, propval, object, overlay,
4658 position, bufpos,
4659 FRAME_WINDOW_P (it->f));
4660 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4661 }
4662
4663 /* Subroutine of handle_display_prop. Returns non-zero if the display
4664 specification in SPEC is a replacing specification, i.e. it would
4665 replace the text covered by `display' property with something else,
4666 such as an image or a display string. If SPEC includes any kind or
4667 `(space ...) specification, the value is 2; this is used by
4668 compute_display_string_pos, which see.
4669
4670 See handle_single_display_spec for documentation of arguments.
4671 FRAME_WINDOW_P is true if the window being redisplayed is on a
4672 GUI frame; this argument is used only if IT is NULL, see below.
4673
4674 IT can be NULL, if this is called by the bidi reordering code
4675 through compute_display_string_pos, which see. In that case, this
4676 function only examines SPEC, but does not otherwise "handle" it, in
4677 the sense that it doesn't set up members of IT from the display
4678 spec. */
4679 static int
4680 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4681 Lisp_Object overlay, struct text_pos *position,
4682 ptrdiff_t bufpos, bool frame_window_p)
4683 {
4684 int replacing = 0;
4685
4686 if (CONSP (spec)
4687 /* Simple specifications. */
4688 && !EQ (XCAR (spec), Qimage)
4689 && !EQ (XCAR (spec), Qspace)
4690 && !EQ (XCAR (spec), Qwhen)
4691 && !EQ (XCAR (spec), Qslice)
4692 && !EQ (XCAR (spec), Qspace_width)
4693 && !EQ (XCAR (spec), Qheight)
4694 && !EQ (XCAR (spec), Qraise)
4695 /* Marginal area specifications. */
4696 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4697 && !EQ (XCAR (spec), Qleft_fringe)
4698 && !EQ (XCAR (spec), Qright_fringe)
4699 && !NILP (XCAR (spec)))
4700 {
4701 for (; CONSP (spec); spec = XCDR (spec))
4702 {
4703 int rv = handle_single_display_spec (it, XCAR (spec), object,
4704 overlay, position, bufpos,
4705 replacing, frame_window_p);
4706 if (rv != 0)
4707 {
4708 replacing = rv;
4709 /* If some text in a string is replaced, `position' no
4710 longer points to the position of `object'. */
4711 if (!it || STRINGP (object))
4712 break;
4713 }
4714 }
4715 }
4716 else if (VECTORP (spec))
4717 {
4718 ptrdiff_t i;
4719 for (i = 0; i < ASIZE (spec); ++i)
4720 {
4721 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4722 overlay, position, bufpos,
4723 replacing, frame_window_p);
4724 if (rv != 0)
4725 {
4726 replacing = rv;
4727 /* If some text in a string is replaced, `position' no
4728 longer points to the position of `object'. */
4729 if (!it || STRINGP (object))
4730 break;
4731 }
4732 }
4733 }
4734 else
4735 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4736 bufpos, 0, frame_window_p);
4737 return replacing;
4738 }
4739
4740 /* Value is the position of the end of the `display' property starting
4741 at START_POS in OBJECT. */
4742
4743 static struct text_pos
4744 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4745 {
4746 Lisp_Object end;
4747 struct text_pos end_pos;
4748
4749 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4750 Qdisplay, object, Qnil);
4751 CHARPOS (end_pos) = XFASTINT (end);
4752 if (STRINGP (object))
4753 compute_string_pos (&end_pos, start_pos, it->string);
4754 else
4755 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4756
4757 return end_pos;
4758 }
4759
4760
4761 /* Set up IT from a single `display' property specification SPEC. OBJECT
4762 is the object in which the `display' property was found. *POSITION
4763 is the position in OBJECT at which the `display' property was found.
4764 BUFPOS is the buffer position of OBJECT (different from POSITION if
4765 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4766 previously saw a display specification which already replaced text
4767 display with something else, for example an image; we ignore such
4768 properties after the first one has been processed.
4769
4770 OVERLAY is the overlay this `display' property came from,
4771 or nil if it was a text property.
4772
4773 If SPEC is a `space' or `image' specification, and in some other
4774 cases too, set *POSITION to the position where the `display'
4775 property ends.
4776
4777 If IT is NULL, only examine the property specification in SPEC, but
4778 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4779 is intended to be displayed in a window on a GUI frame.
4780
4781 Value is non-zero if something was found which replaces the display
4782 of buffer or string text. */
4783
4784 static int
4785 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4786 Lisp_Object overlay, struct text_pos *position,
4787 ptrdiff_t bufpos, int display_replaced,
4788 bool frame_window_p)
4789 {
4790 Lisp_Object form;
4791 Lisp_Object location, value;
4792 struct text_pos start_pos = *position;
4793
4794 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4795 If the result is non-nil, use VALUE instead of SPEC. */
4796 form = Qt;
4797 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4798 {
4799 spec = XCDR (spec);
4800 if (!CONSP (spec))
4801 return 0;
4802 form = XCAR (spec);
4803 spec = XCDR (spec);
4804 }
4805
4806 if (!NILP (form) && !EQ (form, Qt))
4807 {
4808 ptrdiff_t count = SPECPDL_INDEX ();
4809
4810 /* Bind `object' to the object having the `display' property, a
4811 buffer or string. Bind `position' to the position in the
4812 object where the property was found, and `buffer-position'
4813 to the current position in the buffer. */
4814
4815 if (NILP (object))
4816 XSETBUFFER (object, current_buffer);
4817 specbind (Qobject, object);
4818 specbind (Qposition, make_number (CHARPOS (*position)));
4819 specbind (Qbuffer_position, make_number (bufpos));
4820 form = safe_eval (form);
4821 unbind_to (count, Qnil);
4822 }
4823
4824 if (NILP (form))
4825 return 0;
4826
4827 /* Handle `(height HEIGHT)' specifications. */
4828 if (CONSP (spec)
4829 && EQ (XCAR (spec), Qheight)
4830 && CONSP (XCDR (spec)))
4831 {
4832 if (it)
4833 {
4834 if (!FRAME_WINDOW_P (it->f))
4835 return 0;
4836
4837 it->font_height = XCAR (XCDR (spec));
4838 if (!NILP (it->font_height))
4839 {
4840 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4841 int new_height = -1;
4842
4843 if (CONSP (it->font_height)
4844 && (EQ (XCAR (it->font_height), Qplus)
4845 || EQ (XCAR (it->font_height), Qminus))
4846 && CONSP (XCDR (it->font_height))
4847 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4848 {
4849 /* `(+ N)' or `(- N)' where N is an integer. */
4850 int steps = XINT (XCAR (XCDR (it->font_height)));
4851 if (EQ (XCAR (it->font_height), Qplus))
4852 steps = - steps;
4853 it->face_id = smaller_face (it->f, it->face_id, steps);
4854 }
4855 else if (FUNCTIONP (it->font_height))
4856 {
4857 /* Call function with current height as argument.
4858 Value is the new height. */
4859 Lisp_Object height;
4860 height = safe_call1 (it->font_height,
4861 face->lface[LFACE_HEIGHT_INDEX]);
4862 if (NUMBERP (height))
4863 new_height = XFLOATINT (height);
4864 }
4865 else if (NUMBERP (it->font_height))
4866 {
4867 /* Value is a multiple of the canonical char height. */
4868 struct face *f;
4869
4870 f = FACE_FROM_ID (it->f,
4871 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4872 new_height = (XFLOATINT (it->font_height)
4873 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4874 }
4875 else
4876 {
4877 /* Evaluate IT->font_height with `height' bound to the
4878 current specified height to get the new height. */
4879 ptrdiff_t count = SPECPDL_INDEX ();
4880
4881 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4882 value = safe_eval (it->font_height);
4883 unbind_to (count, Qnil);
4884
4885 if (NUMBERP (value))
4886 new_height = XFLOATINT (value);
4887 }
4888
4889 if (new_height > 0)
4890 it->face_id = face_with_height (it->f, it->face_id, new_height);
4891 }
4892 }
4893
4894 return 0;
4895 }
4896
4897 /* Handle `(space-width WIDTH)'. */
4898 if (CONSP (spec)
4899 && EQ (XCAR (spec), Qspace_width)
4900 && CONSP (XCDR (spec)))
4901 {
4902 if (it)
4903 {
4904 if (!FRAME_WINDOW_P (it->f))
4905 return 0;
4906
4907 value = XCAR (XCDR (spec));
4908 if (NUMBERP (value) && XFLOATINT (value) > 0)
4909 it->space_width = value;
4910 }
4911
4912 return 0;
4913 }
4914
4915 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4916 if (CONSP (spec)
4917 && EQ (XCAR (spec), Qslice))
4918 {
4919 Lisp_Object tem;
4920
4921 if (it)
4922 {
4923 if (!FRAME_WINDOW_P (it->f))
4924 return 0;
4925
4926 if (tem = XCDR (spec), CONSP (tem))
4927 {
4928 it->slice.x = XCAR (tem);
4929 if (tem = XCDR (tem), CONSP (tem))
4930 {
4931 it->slice.y = XCAR (tem);
4932 if (tem = XCDR (tem), CONSP (tem))
4933 {
4934 it->slice.width = XCAR (tem);
4935 if (tem = XCDR (tem), CONSP (tem))
4936 it->slice.height = XCAR (tem);
4937 }
4938 }
4939 }
4940 }
4941
4942 return 0;
4943 }
4944
4945 /* Handle `(raise FACTOR)'. */
4946 if (CONSP (spec)
4947 && EQ (XCAR (spec), Qraise)
4948 && CONSP (XCDR (spec)))
4949 {
4950 if (it)
4951 {
4952 if (!FRAME_WINDOW_P (it->f))
4953 return 0;
4954
4955 #ifdef HAVE_WINDOW_SYSTEM
4956 value = XCAR (XCDR (spec));
4957 if (NUMBERP (value))
4958 {
4959 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4960 it->voffset = - (XFLOATINT (value)
4961 * (normal_char_height (face->font, -1)));
4962 }
4963 #endif /* HAVE_WINDOW_SYSTEM */
4964 }
4965
4966 return 0;
4967 }
4968
4969 /* Don't handle the other kinds of display specifications
4970 inside a string that we got from a `display' property. */
4971 if (it && it->string_from_display_prop_p)
4972 return 0;
4973
4974 /* Characters having this form of property are not displayed, so
4975 we have to find the end of the property. */
4976 if (it)
4977 {
4978 start_pos = *position;
4979 *position = display_prop_end (it, object, start_pos);
4980 /* If the display property comes from an overlay, don't consider
4981 any potential stop_charpos values before the end of that
4982 overlay. Since display_prop_end will happily find another
4983 'display' property coming from some other overlay or text
4984 property on buffer positions before this overlay's end, we
4985 need to ignore them, or else we risk displaying this
4986 overlay's display string/image twice. */
4987 if (!NILP (overlay))
4988 {
4989 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4990
4991 if (ovendpos > CHARPOS (*position))
4992 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
4993 }
4994 }
4995 value = Qnil;
4996
4997 /* Stop the scan at that end position--we assume that all
4998 text properties change there. */
4999 if (it)
5000 it->stop_charpos = position->charpos;
5001
5002 /* Handle `(left-fringe BITMAP [FACE])'
5003 and `(right-fringe BITMAP [FACE])'. */
5004 if (CONSP (spec)
5005 && (EQ (XCAR (spec), Qleft_fringe)
5006 || EQ (XCAR (spec), Qright_fringe))
5007 && CONSP (XCDR (spec)))
5008 {
5009 int fringe_bitmap;
5010
5011 if (it)
5012 {
5013 if (!FRAME_WINDOW_P (it->f))
5014 /* If we return here, POSITION has been advanced
5015 across the text with this property. */
5016 {
5017 /* Synchronize the bidi iterator with POSITION. This is
5018 needed because we are not going to push the iterator
5019 on behalf of this display property, so there will be
5020 no pop_it call to do this synchronization for us. */
5021 if (it->bidi_p)
5022 {
5023 it->position = *position;
5024 iterate_out_of_display_property (it);
5025 *position = it->position;
5026 }
5027 return 1;
5028 }
5029 }
5030 else if (!frame_window_p)
5031 return 1;
5032
5033 #ifdef HAVE_WINDOW_SYSTEM
5034 value = XCAR (XCDR (spec));
5035 if (!SYMBOLP (value)
5036 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
5037 /* If we return here, POSITION has been advanced
5038 across the text with this property. */
5039 {
5040 if (it && it->bidi_p)
5041 {
5042 it->position = *position;
5043 iterate_out_of_display_property (it);
5044 *position = it->position;
5045 }
5046 return 1;
5047 }
5048
5049 if (it)
5050 {
5051 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5052
5053 if (CONSP (XCDR (XCDR (spec))))
5054 {
5055 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5056 int face_id2 = lookup_derived_face (it->f, face_name,
5057 FRINGE_FACE_ID, false);
5058 if (face_id2 >= 0)
5059 face_id = face_id2;
5060 }
5061
5062 /* Save current settings of IT so that we can restore them
5063 when we are finished with the glyph property value. */
5064 push_it (it, position);
5065
5066 it->area = TEXT_AREA;
5067 it->what = IT_IMAGE;
5068 it->image_id = -1; /* no image */
5069 it->position = start_pos;
5070 it->object = NILP (object) ? it->w->contents : object;
5071 it->method = GET_FROM_IMAGE;
5072 it->from_overlay = Qnil;
5073 it->face_id = face_id;
5074 it->from_disp_prop_p = true;
5075
5076 /* Say that we haven't consumed the characters with
5077 `display' property yet. The call to pop_it in
5078 set_iterator_to_next will clean this up. */
5079 *position = start_pos;
5080
5081 if (EQ (XCAR (spec), Qleft_fringe))
5082 {
5083 it->left_user_fringe_bitmap = fringe_bitmap;
5084 it->left_user_fringe_face_id = face_id;
5085 }
5086 else
5087 {
5088 it->right_user_fringe_bitmap = fringe_bitmap;
5089 it->right_user_fringe_face_id = face_id;
5090 }
5091 }
5092 #endif /* HAVE_WINDOW_SYSTEM */
5093 return 1;
5094 }
5095
5096 /* Prepare to handle `((margin left-margin) ...)',
5097 `((margin right-margin) ...)' and `((margin nil) ...)'
5098 prefixes for display specifications. */
5099 location = Qunbound;
5100 if (CONSP (spec) && CONSP (XCAR (spec)))
5101 {
5102 Lisp_Object tem;
5103
5104 value = XCDR (spec);
5105 if (CONSP (value))
5106 value = XCAR (value);
5107
5108 tem = XCAR (spec);
5109 if (EQ (XCAR (tem), Qmargin)
5110 && (tem = XCDR (tem),
5111 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5112 (NILP (tem)
5113 || EQ (tem, Qleft_margin)
5114 || EQ (tem, Qright_margin))))
5115 location = tem;
5116 }
5117
5118 if (EQ (location, Qunbound))
5119 {
5120 location = Qnil;
5121 value = spec;
5122 }
5123
5124 /* After this point, VALUE is the property after any
5125 margin prefix has been stripped. It must be a string,
5126 an image specification, or `(space ...)'.
5127
5128 LOCATION specifies where to display: `left-margin',
5129 `right-margin' or nil. */
5130
5131 bool valid_p = (STRINGP (value)
5132 #ifdef HAVE_WINDOW_SYSTEM
5133 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5134 && valid_image_p (value))
5135 #endif /* not HAVE_WINDOW_SYSTEM */
5136 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5137
5138 if (valid_p && display_replaced == 0)
5139 {
5140 int retval = 1;
5141
5142 if (!it)
5143 {
5144 /* Callers need to know whether the display spec is any kind
5145 of `(space ...)' spec that is about to affect text-area
5146 display. */
5147 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5148 retval = 2;
5149 return retval;
5150 }
5151
5152 /* Save current settings of IT so that we can restore them
5153 when we are finished with the glyph property value. */
5154 push_it (it, position);
5155 it->from_overlay = overlay;
5156 it->from_disp_prop_p = true;
5157
5158 if (NILP (location))
5159 it->area = TEXT_AREA;
5160 else if (EQ (location, Qleft_margin))
5161 it->area = LEFT_MARGIN_AREA;
5162 else
5163 it->area = RIGHT_MARGIN_AREA;
5164
5165 if (STRINGP (value))
5166 {
5167 it->string = value;
5168 it->multibyte_p = STRING_MULTIBYTE (it->string);
5169 it->current.overlay_string_index = -1;
5170 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5171 it->end_charpos = it->string_nchars = SCHARS (it->string);
5172 it->method = GET_FROM_STRING;
5173 it->stop_charpos = 0;
5174 it->prev_stop = 0;
5175 it->base_level_stop = 0;
5176 it->string_from_display_prop_p = true;
5177 /* Say that we haven't consumed the characters with
5178 `display' property yet. The call to pop_it in
5179 set_iterator_to_next will clean this up. */
5180 if (BUFFERP (object))
5181 *position = start_pos;
5182
5183 /* Force paragraph direction to be that of the parent
5184 object. If the parent object's paragraph direction is
5185 not yet determined, default to L2R. */
5186 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5187 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5188 else
5189 it->paragraph_embedding = L2R;
5190
5191 /* Set up the bidi iterator for this display string. */
5192 if (it->bidi_p)
5193 {
5194 it->bidi_it.string.lstring = it->string;
5195 it->bidi_it.string.s = NULL;
5196 it->bidi_it.string.schars = it->end_charpos;
5197 it->bidi_it.string.bufpos = bufpos;
5198 it->bidi_it.string.from_disp_str = true;
5199 it->bidi_it.string.unibyte = !it->multibyte_p;
5200 it->bidi_it.w = it->w;
5201 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5202 }
5203 }
5204 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5205 {
5206 it->method = GET_FROM_STRETCH;
5207 it->object = value;
5208 *position = it->position = start_pos;
5209 retval = 1 + (it->area == TEXT_AREA);
5210 }
5211 #ifdef HAVE_WINDOW_SYSTEM
5212 else
5213 {
5214 it->what = IT_IMAGE;
5215 it->image_id = lookup_image (it->f, value);
5216 it->position = start_pos;
5217 it->object = NILP (object) ? it->w->contents : object;
5218 it->method = GET_FROM_IMAGE;
5219
5220 /* Say that we haven't consumed the characters with
5221 `display' property yet. The call to pop_it in
5222 set_iterator_to_next will clean this up. */
5223 *position = start_pos;
5224 }
5225 #endif /* HAVE_WINDOW_SYSTEM */
5226
5227 return retval;
5228 }
5229
5230 /* Invalid property or property not supported. Restore
5231 POSITION to what it was before. */
5232 *position = start_pos;
5233 return 0;
5234 }
5235
5236 /* Check if PROP is a display property value whose text should be
5237 treated as intangible. OVERLAY is the overlay from which PROP
5238 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5239 specify the buffer position covered by PROP. */
5240
5241 bool
5242 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5243 ptrdiff_t charpos, ptrdiff_t bytepos)
5244 {
5245 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5246 struct text_pos position;
5247
5248 SET_TEXT_POS (position, charpos, bytepos);
5249 return (handle_display_spec (NULL, prop, Qnil, overlay,
5250 &position, charpos, frame_window_p)
5251 != 0);
5252 }
5253
5254
5255 /* Return true if PROP is a display sub-property value containing STRING.
5256
5257 Implementation note: this and the following function are really
5258 special cases of handle_display_spec and
5259 handle_single_display_spec, and should ideally use the same code.
5260 Until they do, these two pairs must be consistent and must be
5261 modified in sync. */
5262
5263 static bool
5264 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5265 {
5266 if (EQ (string, prop))
5267 return true;
5268
5269 /* Skip over `when FORM'. */
5270 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5271 {
5272 prop = XCDR (prop);
5273 if (!CONSP (prop))
5274 return false;
5275 /* Actually, the condition following `when' should be eval'ed,
5276 like handle_single_display_spec does, and we should return
5277 false if it evaluates to nil. However, this function is
5278 called only when the buffer was already displayed and some
5279 glyph in the glyph matrix was found to come from a display
5280 string. Therefore, the condition was already evaluated, and
5281 the result was non-nil, otherwise the display string wouldn't
5282 have been displayed and we would have never been called for
5283 this property. Thus, we can skip the evaluation and assume
5284 its result is non-nil. */
5285 prop = XCDR (prop);
5286 }
5287
5288 if (CONSP (prop))
5289 /* Skip over `margin LOCATION'. */
5290 if (EQ (XCAR (prop), Qmargin))
5291 {
5292 prop = XCDR (prop);
5293 if (!CONSP (prop))
5294 return false;
5295
5296 prop = XCDR (prop);
5297 if (!CONSP (prop))
5298 return false;
5299 }
5300
5301 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5302 }
5303
5304
5305 /* Return true if STRING appears in the `display' property PROP. */
5306
5307 static bool
5308 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5309 {
5310 if (CONSP (prop)
5311 && !EQ (XCAR (prop), Qwhen)
5312 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5313 {
5314 /* A list of sub-properties. */
5315 while (CONSP (prop))
5316 {
5317 if (single_display_spec_string_p (XCAR (prop), string))
5318 return true;
5319 prop = XCDR (prop);
5320 }
5321 }
5322 else if (VECTORP (prop))
5323 {
5324 /* A vector of sub-properties. */
5325 ptrdiff_t i;
5326 for (i = 0; i < ASIZE (prop); ++i)
5327 if (single_display_spec_string_p (AREF (prop, i), string))
5328 return true;
5329 }
5330 else
5331 return single_display_spec_string_p (prop, string);
5332
5333 return false;
5334 }
5335
5336 /* Look for STRING in overlays and text properties in the current
5337 buffer, between character positions FROM and TO (excluding TO).
5338 BACK_P means look back (in this case, TO is supposed to be
5339 less than FROM).
5340 Value is the first character position where STRING was found, or
5341 zero if it wasn't found before hitting TO.
5342
5343 This function may only use code that doesn't eval because it is
5344 called asynchronously from note_mouse_highlight. */
5345
5346 static ptrdiff_t
5347 string_buffer_position_lim (Lisp_Object string,
5348 ptrdiff_t from, ptrdiff_t to, bool back_p)
5349 {
5350 Lisp_Object limit, prop, pos;
5351 bool found = false;
5352
5353 pos = make_number (max (from, BEGV));
5354
5355 if (!back_p) /* looking forward */
5356 {
5357 limit = make_number (min (to, ZV));
5358 while (!found && !EQ (pos, limit))
5359 {
5360 prop = Fget_char_property (pos, Qdisplay, Qnil);
5361 if (!NILP (prop) && display_prop_string_p (prop, string))
5362 found = true;
5363 else
5364 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5365 limit);
5366 }
5367 }
5368 else /* looking back */
5369 {
5370 limit = make_number (max (to, BEGV));
5371 while (!found && !EQ (pos, limit))
5372 {
5373 prop = Fget_char_property (pos, Qdisplay, Qnil);
5374 if (!NILP (prop) && display_prop_string_p (prop, string))
5375 found = true;
5376 else
5377 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5378 limit);
5379 }
5380 }
5381
5382 return found ? XINT (pos) : 0;
5383 }
5384
5385 /* Determine which buffer position in current buffer STRING comes from.
5386 AROUND_CHARPOS is an approximate position where it could come from.
5387 Value is the buffer position or 0 if it couldn't be determined.
5388
5389 This function is necessary because we don't record buffer positions
5390 in glyphs generated from strings (to keep struct glyph small).
5391 This function may only use code that doesn't eval because it is
5392 called asynchronously from note_mouse_highlight. */
5393
5394 static ptrdiff_t
5395 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5396 {
5397 const int MAX_DISTANCE = 1000;
5398 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5399 around_charpos + MAX_DISTANCE,
5400 false);
5401
5402 if (!found)
5403 found = string_buffer_position_lim (string, around_charpos,
5404 around_charpos - MAX_DISTANCE, true);
5405 return found;
5406 }
5407
5408
5409 \f
5410 /***********************************************************************
5411 `composition' property
5412 ***********************************************************************/
5413
5414 /* Set up iterator IT from `composition' property at its current
5415 position. Called from handle_stop. */
5416
5417 static enum prop_handled
5418 handle_composition_prop (struct it *it)
5419 {
5420 Lisp_Object prop, string;
5421 ptrdiff_t pos, pos_byte, start, end;
5422
5423 if (STRINGP (it->string))
5424 {
5425 unsigned char *s;
5426
5427 pos = IT_STRING_CHARPOS (*it);
5428 pos_byte = IT_STRING_BYTEPOS (*it);
5429 string = it->string;
5430 s = SDATA (string) + pos_byte;
5431 it->c = STRING_CHAR (s);
5432 }
5433 else
5434 {
5435 pos = IT_CHARPOS (*it);
5436 pos_byte = IT_BYTEPOS (*it);
5437 string = Qnil;
5438 it->c = FETCH_CHAR (pos_byte);
5439 }
5440
5441 /* If there's a valid composition and point is not inside of the
5442 composition (in the case that the composition is from the current
5443 buffer), draw a glyph composed from the composition components. */
5444 if (find_composition (pos, -1, &start, &end, &prop, string)
5445 && composition_valid_p (start, end, prop)
5446 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5447 {
5448 if (start < pos)
5449 /* As we can't handle this situation (perhaps font-lock added
5450 a new composition), we just return here hoping that next
5451 redisplay will detect this composition much earlier. */
5452 return HANDLED_NORMALLY;
5453 if (start != pos)
5454 {
5455 if (STRINGP (it->string))
5456 pos_byte = string_char_to_byte (it->string, start);
5457 else
5458 pos_byte = CHAR_TO_BYTE (start);
5459 }
5460 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5461 prop, string);
5462
5463 if (it->cmp_it.id >= 0)
5464 {
5465 it->cmp_it.ch = -1;
5466 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5467 it->cmp_it.nglyphs = -1;
5468 }
5469 }
5470
5471 return HANDLED_NORMALLY;
5472 }
5473
5474
5475 \f
5476 /***********************************************************************
5477 Overlay strings
5478 ***********************************************************************/
5479
5480 /* The following structure is used to record overlay strings for
5481 later sorting in load_overlay_strings. */
5482
5483 struct overlay_entry
5484 {
5485 Lisp_Object overlay;
5486 Lisp_Object string;
5487 EMACS_INT priority;
5488 bool after_string_p;
5489 };
5490
5491
5492 /* Set up iterator IT from overlay strings at its current position.
5493 Called from handle_stop. */
5494
5495 static enum prop_handled
5496 handle_overlay_change (struct it *it)
5497 {
5498 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5499 return HANDLED_RECOMPUTE_PROPS;
5500 else
5501 return HANDLED_NORMALLY;
5502 }
5503
5504
5505 /* Set up the next overlay string for delivery by IT, if there is an
5506 overlay string to deliver. Called by set_iterator_to_next when the
5507 end of the current overlay string is reached. If there are more
5508 overlay strings to display, IT->string and
5509 IT->current.overlay_string_index are set appropriately here.
5510 Otherwise IT->string is set to nil. */
5511
5512 static void
5513 next_overlay_string (struct it *it)
5514 {
5515 ++it->current.overlay_string_index;
5516 if (it->current.overlay_string_index == it->n_overlay_strings)
5517 {
5518 /* No more overlay strings. Restore IT's settings to what
5519 they were before overlay strings were processed, and
5520 continue to deliver from current_buffer. */
5521
5522 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5523 pop_it (it);
5524 eassert (it->sp > 0
5525 || (NILP (it->string)
5526 && it->method == GET_FROM_BUFFER
5527 && it->stop_charpos >= BEGV
5528 && it->stop_charpos <= it->end_charpos));
5529 it->current.overlay_string_index = -1;
5530 it->n_overlay_strings = 0;
5531 /* If there's an empty display string on the stack, pop the
5532 stack, to resync the bidi iterator with IT's position. Such
5533 empty strings are pushed onto the stack in
5534 get_overlay_strings_1. */
5535 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5536 pop_it (it);
5537
5538 /* Since we've exhausted overlay strings at this buffer
5539 position, set the flag to ignore overlays until we move to
5540 another position. The flag is reset in
5541 next_element_from_buffer. */
5542 it->ignore_overlay_strings_at_pos_p = true;
5543
5544 /* If we're at the end of the buffer, record that we have
5545 processed the overlay strings there already, so that
5546 next_element_from_buffer doesn't try it again. */
5547 if (NILP (it->string)
5548 && IT_CHARPOS (*it) >= it->end_charpos
5549 && it->overlay_strings_charpos >= it->end_charpos)
5550 it->overlay_strings_at_end_processed_p = true;
5551 /* Note: we reset overlay_strings_charpos only here, to make
5552 sure the just-processed overlays were indeed at EOB.
5553 Otherwise, overlays on text with invisible text property,
5554 which are processed with IT's position past the invisible
5555 text, might fool us into thinking the overlays at EOB were
5556 already processed (linum-mode can cause this, for
5557 example). */
5558 it->overlay_strings_charpos = -1;
5559 }
5560 else
5561 {
5562 /* There are more overlay strings to process. If
5563 IT->current.overlay_string_index has advanced to a position
5564 where we must load IT->overlay_strings with more strings, do
5565 it. We must load at the IT->overlay_strings_charpos where
5566 IT->n_overlay_strings was originally computed; when invisible
5567 text is present, this might not be IT_CHARPOS (Bug#7016). */
5568 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5569
5570 if (it->current.overlay_string_index && i == 0)
5571 load_overlay_strings (it, it->overlay_strings_charpos);
5572
5573 /* Initialize IT to deliver display elements from the overlay
5574 string. */
5575 it->string = it->overlay_strings[i];
5576 it->multibyte_p = STRING_MULTIBYTE (it->string);
5577 SET_TEXT_POS (it->current.string_pos, 0, 0);
5578 it->method = GET_FROM_STRING;
5579 it->stop_charpos = 0;
5580 it->end_charpos = SCHARS (it->string);
5581 if (it->cmp_it.stop_pos >= 0)
5582 it->cmp_it.stop_pos = 0;
5583 it->prev_stop = 0;
5584 it->base_level_stop = 0;
5585
5586 /* Set up the bidi iterator for this overlay string. */
5587 if (it->bidi_p)
5588 {
5589 it->bidi_it.string.lstring = it->string;
5590 it->bidi_it.string.s = NULL;
5591 it->bidi_it.string.schars = SCHARS (it->string);
5592 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5593 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5594 it->bidi_it.string.unibyte = !it->multibyte_p;
5595 it->bidi_it.w = it->w;
5596 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5597 }
5598 }
5599
5600 CHECK_IT (it);
5601 }
5602
5603
5604 /* Compare two overlay_entry structures E1 and E2. Used as a
5605 comparison function for qsort in load_overlay_strings. Overlay
5606 strings for the same position are sorted so that
5607
5608 1. All after-strings come in front of before-strings, except
5609 when they come from the same overlay.
5610
5611 2. Within after-strings, strings are sorted so that overlay strings
5612 from overlays with higher priorities come first.
5613
5614 2. Within before-strings, strings are sorted so that overlay
5615 strings from overlays with higher priorities come last.
5616
5617 Value is analogous to strcmp. */
5618
5619
5620 static int
5621 compare_overlay_entries (const void *e1, const void *e2)
5622 {
5623 struct overlay_entry const *entry1 = e1;
5624 struct overlay_entry const *entry2 = e2;
5625 int result;
5626
5627 if (entry1->after_string_p != entry2->after_string_p)
5628 {
5629 /* Let after-strings appear in front of before-strings if
5630 they come from different overlays. */
5631 if (EQ (entry1->overlay, entry2->overlay))
5632 result = entry1->after_string_p ? 1 : -1;
5633 else
5634 result = entry1->after_string_p ? -1 : 1;
5635 }
5636 else if (entry1->priority != entry2->priority)
5637 {
5638 if (entry1->after_string_p)
5639 /* After-strings sorted in order of decreasing priority. */
5640 result = entry2->priority < entry1->priority ? -1 : 1;
5641 else
5642 /* Before-strings sorted in order of increasing priority. */
5643 result = entry1->priority < entry2->priority ? -1 : 1;
5644 }
5645 else
5646 result = 0;
5647
5648 return result;
5649 }
5650
5651
5652 /* Load the vector IT->overlay_strings with overlay strings from IT's
5653 current buffer position, or from CHARPOS if that is > 0. Set
5654 IT->n_overlays to the total number of overlay strings found.
5655
5656 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5657 a time. On entry into load_overlay_strings,
5658 IT->current.overlay_string_index gives the number of overlay
5659 strings that have already been loaded by previous calls to this
5660 function.
5661
5662 IT->add_overlay_start contains an additional overlay start
5663 position to consider for taking overlay strings from, if non-zero.
5664 This position comes into play when the overlay has an `invisible'
5665 property, and both before and after-strings. When we've skipped to
5666 the end of the overlay, because of its `invisible' property, we
5667 nevertheless want its before-string to appear.
5668 IT->add_overlay_start will contain the overlay start position
5669 in this case.
5670
5671 Overlay strings are sorted so that after-string strings come in
5672 front of before-string strings. Within before and after-strings,
5673 strings are sorted by overlay priority. See also function
5674 compare_overlay_entries. */
5675
5676 static void
5677 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5678 {
5679 Lisp_Object overlay, window, str, invisible;
5680 struct Lisp_Overlay *ov;
5681 ptrdiff_t start, end;
5682 ptrdiff_t n = 0, i, j;
5683 int invis;
5684 struct overlay_entry entriesbuf[20];
5685 ptrdiff_t size = ARRAYELTS (entriesbuf);
5686 struct overlay_entry *entries = entriesbuf;
5687 USE_SAFE_ALLOCA;
5688
5689 if (charpos <= 0)
5690 charpos = IT_CHARPOS (*it);
5691
5692 /* Append the overlay string STRING of overlay OVERLAY to vector
5693 `entries' which has size `size' and currently contains `n'
5694 elements. AFTER_P means STRING is an after-string of
5695 OVERLAY. */
5696 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5697 do \
5698 { \
5699 Lisp_Object priority; \
5700 \
5701 if (n == size) \
5702 { \
5703 struct overlay_entry *old = entries; \
5704 SAFE_NALLOCA (entries, 2, size); \
5705 memcpy (entries, old, size * sizeof *entries); \
5706 size *= 2; \
5707 } \
5708 \
5709 entries[n].string = (STRING); \
5710 entries[n].overlay = (OVERLAY); \
5711 priority = Foverlay_get ((OVERLAY), Qpriority); \
5712 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5713 entries[n].after_string_p = (AFTER_P); \
5714 ++n; \
5715 } \
5716 while (false)
5717
5718 /* Process overlay before the overlay center. */
5719 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5720 {
5721 XSETMISC (overlay, ov);
5722 eassert (OVERLAYP (overlay));
5723 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5724 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5725
5726 if (end < charpos)
5727 break;
5728
5729 /* Skip this overlay if it doesn't start or end at IT's current
5730 position. */
5731 if (end != charpos && start != charpos)
5732 continue;
5733
5734 /* Skip this overlay if it doesn't apply to IT->w. */
5735 window = Foverlay_get (overlay, Qwindow);
5736 if (WINDOWP (window) && XWINDOW (window) != it->w)
5737 continue;
5738
5739 /* If the text ``under'' the overlay is invisible, both before-
5740 and after-strings from this overlay are visible; start and
5741 end position are indistinguishable. */
5742 invisible = Foverlay_get (overlay, Qinvisible);
5743 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5744
5745 /* If overlay has a non-empty before-string, record it. */
5746 if ((start == charpos || (end == charpos && invis != 0))
5747 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5748 && SCHARS (str))
5749 RECORD_OVERLAY_STRING (overlay, str, false);
5750
5751 /* If overlay has a non-empty after-string, record it. */
5752 if ((end == charpos || (start == charpos && invis != 0))
5753 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5754 && SCHARS (str))
5755 RECORD_OVERLAY_STRING (overlay, str, true);
5756 }
5757
5758 /* Process overlays after the overlay center. */
5759 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5760 {
5761 XSETMISC (overlay, ov);
5762 eassert (OVERLAYP (overlay));
5763 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5764 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5765
5766 if (start > charpos)
5767 break;
5768
5769 /* Skip this overlay if it doesn't start or end at IT's current
5770 position. */
5771 if (end != charpos && start != charpos)
5772 continue;
5773
5774 /* Skip this overlay if it doesn't apply to IT->w. */
5775 window = Foverlay_get (overlay, Qwindow);
5776 if (WINDOWP (window) && XWINDOW (window) != it->w)
5777 continue;
5778
5779 /* If the text ``under'' the overlay is invisible, it has a zero
5780 dimension, and both before- and after-strings apply. */
5781 invisible = Foverlay_get (overlay, Qinvisible);
5782 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5783
5784 /* If overlay has a non-empty before-string, record it. */
5785 if ((start == charpos || (end == charpos && invis != 0))
5786 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5787 && SCHARS (str))
5788 RECORD_OVERLAY_STRING (overlay, str, false);
5789
5790 /* If overlay has a non-empty after-string, record it. */
5791 if ((end == charpos || (start == charpos && invis != 0))
5792 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5793 && SCHARS (str))
5794 RECORD_OVERLAY_STRING (overlay, str, true);
5795 }
5796
5797 #undef RECORD_OVERLAY_STRING
5798
5799 /* Sort entries. */
5800 if (n > 1)
5801 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5802
5803 /* Record number of overlay strings, and where we computed it. */
5804 it->n_overlay_strings = n;
5805 it->overlay_strings_charpos = charpos;
5806
5807 /* IT->current.overlay_string_index is the number of overlay strings
5808 that have already been consumed by IT. Copy some of the
5809 remaining overlay strings to IT->overlay_strings. */
5810 i = 0;
5811 j = it->current.overlay_string_index;
5812 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5813 {
5814 it->overlay_strings[i] = entries[j].string;
5815 it->string_overlays[i++] = entries[j++].overlay;
5816 }
5817
5818 CHECK_IT (it);
5819 SAFE_FREE ();
5820 }
5821
5822
5823 /* Get the first chunk of overlay strings at IT's current buffer
5824 position, or at CHARPOS if that is > 0. Value is true if at
5825 least one overlay string was found. */
5826
5827 static bool
5828 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5829 {
5830 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5831 process. This fills IT->overlay_strings with strings, and sets
5832 IT->n_overlay_strings to the total number of strings to process.
5833 IT->pos.overlay_string_index has to be set temporarily to zero
5834 because load_overlay_strings needs this; it must be set to -1
5835 when no overlay strings are found because a zero value would
5836 indicate a position in the first overlay string. */
5837 it->current.overlay_string_index = 0;
5838 load_overlay_strings (it, charpos);
5839
5840 /* If we found overlay strings, set up IT to deliver display
5841 elements from the first one. Otherwise set up IT to deliver
5842 from current_buffer. */
5843 if (it->n_overlay_strings)
5844 {
5845 /* Make sure we know settings in current_buffer, so that we can
5846 restore meaningful values when we're done with the overlay
5847 strings. */
5848 if (compute_stop_p)
5849 compute_stop_pos (it);
5850 eassert (it->face_id >= 0);
5851
5852 /* Save IT's settings. They are restored after all overlay
5853 strings have been processed. */
5854 eassert (!compute_stop_p || it->sp == 0);
5855
5856 /* When called from handle_stop, there might be an empty display
5857 string loaded. In that case, don't bother saving it. But
5858 don't use this optimization with the bidi iterator, since we
5859 need the corresponding pop_it call to resync the bidi
5860 iterator's position with IT's position, after we are done
5861 with the overlay strings. (The corresponding call to pop_it
5862 in case of an empty display string is in
5863 next_overlay_string.) */
5864 if (!(!it->bidi_p
5865 && STRINGP (it->string) && !SCHARS (it->string)))
5866 push_it (it, NULL);
5867
5868 /* Set up IT to deliver display elements from the first overlay
5869 string. */
5870 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5871 it->string = it->overlay_strings[0];
5872 it->from_overlay = Qnil;
5873 it->stop_charpos = 0;
5874 eassert (STRINGP (it->string));
5875 it->end_charpos = SCHARS (it->string);
5876 it->prev_stop = 0;
5877 it->base_level_stop = 0;
5878 it->multibyte_p = STRING_MULTIBYTE (it->string);
5879 it->method = GET_FROM_STRING;
5880 it->from_disp_prop_p = 0;
5881
5882 /* Force paragraph direction to be that of the parent
5883 buffer. */
5884 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5885 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5886 else
5887 it->paragraph_embedding = L2R;
5888
5889 /* Set up the bidi iterator for this overlay string. */
5890 if (it->bidi_p)
5891 {
5892 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5893
5894 it->bidi_it.string.lstring = it->string;
5895 it->bidi_it.string.s = NULL;
5896 it->bidi_it.string.schars = SCHARS (it->string);
5897 it->bidi_it.string.bufpos = pos;
5898 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5899 it->bidi_it.string.unibyte = !it->multibyte_p;
5900 it->bidi_it.w = it->w;
5901 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5902 }
5903 return true;
5904 }
5905
5906 it->current.overlay_string_index = -1;
5907 return false;
5908 }
5909
5910 static bool
5911 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5912 {
5913 it->string = Qnil;
5914 it->method = GET_FROM_BUFFER;
5915
5916 get_overlay_strings_1 (it, charpos, true);
5917
5918 CHECK_IT (it);
5919
5920 /* Value is true if we found at least one overlay string. */
5921 return STRINGP (it->string);
5922 }
5923
5924
5925 \f
5926 /***********************************************************************
5927 Saving and restoring state
5928 ***********************************************************************/
5929
5930 /* Save current settings of IT on IT->stack. Called, for example,
5931 before setting up IT for an overlay string, to be able to restore
5932 IT's settings to what they were after the overlay string has been
5933 processed. If POSITION is non-NULL, it is the position to save on
5934 the stack instead of IT->position. */
5935
5936 static void
5937 push_it (struct it *it, struct text_pos *position)
5938 {
5939 struct iterator_stack_entry *p;
5940
5941 eassert (it->sp < IT_STACK_SIZE);
5942 p = it->stack + it->sp;
5943
5944 p->stop_charpos = it->stop_charpos;
5945 p->prev_stop = it->prev_stop;
5946 p->base_level_stop = it->base_level_stop;
5947 p->cmp_it = it->cmp_it;
5948 eassert (it->face_id >= 0);
5949 p->face_id = it->face_id;
5950 p->string = it->string;
5951 p->method = it->method;
5952 p->from_overlay = it->from_overlay;
5953 switch (p->method)
5954 {
5955 case GET_FROM_IMAGE:
5956 p->u.image.object = it->object;
5957 p->u.image.image_id = it->image_id;
5958 p->u.image.slice = it->slice;
5959 break;
5960 case GET_FROM_STRETCH:
5961 p->u.stretch.object = it->object;
5962 break;
5963 case GET_FROM_BUFFER:
5964 case GET_FROM_DISPLAY_VECTOR:
5965 case GET_FROM_STRING:
5966 case GET_FROM_C_STRING:
5967 break;
5968 default:
5969 emacs_abort ();
5970 }
5971 p->position = position ? *position : it->position;
5972 p->current = it->current;
5973 p->end_charpos = it->end_charpos;
5974 p->string_nchars = it->string_nchars;
5975 p->area = it->area;
5976 p->multibyte_p = it->multibyte_p;
5977 p->avoid_cursor_p = it->avoid_cursor_p;
5978 p->space_width = it->space_width;
5979 p->font_height = it->font_height;
5980 p->voffset = it->voffset;
5981 p->string_from_display_prop_p = it->string_from_display_prop_p;
5982 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5983 p->display_ellipsis_p = false;
5984 p->line_wrap = it->line_wrap;
5985 p->bidi_p = it->bidi_p;
5986 p->paragraph_embedding = it->paragraph_embedding;
5987 p->from_disp_prop_p = it->from_disp_prop_p;
5988 ++it->sp;
5989
5990 /* Save the state of the bidi iterator as well. */
5991 if (it->bidi_p)
5992 bidi_push_it (&it->bidi_it);
5993 }
5994
5995 static void
5996 iterate_out_of_display_property (struct it *it)
5997 {
5998 bool buffer_p = !STRINGP (it->string);
5999 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6000 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6001
6002 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6003
6004 /* Maybe initialize paragraph direction. If we are at the beginning
6005 of a new paragraph, next_element_from_buffer may not have a
6006 chance to do that. */
6007 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6008 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6009 /* prev_stop can be zero, so check against BEGV as well. */
6010 while (it->bidi_it.charpos >= bob
6011 && it->prev_stop <= it->bidi_it.charpos
6012 && it->bidi_it.charpos < CHARPOS (it->position)
6013 && it->bidi_it.charpos < eob)
6014 bidi_move_to_visually_next (&it->bidi_it);
6015 /* Record the stop_pos we just crossed, for when we cross it
6016 back, maybe. */
6017 if (it->bidi_it.charpos > CHARPOS (it->position))
6018 it->prev_stop = CHARPOS (it->position);
6019 /* If we ended up not where pop_it put us, resync IT's
6020 positional members with the bidi iterator. */
6021 if (it->bidi_it.charpos != CHARPOS (it->position))
6022 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6023 if (buffer_p)
6024 it->current.pos = it->position;
6025 else
6026 it->current.string_pos = it->position;
6027 }
6028
6029 /* Restore IT's settings from IT->stack. Called, for example, when no
6030 more overlay strings must be processed, and we return to delivering
6031 display elements from a buffer, or when the end of a string from a
6032 `display' property is reached and we return to delivering display
6033 elements from an overlay string, or from a buffer. */
6034
6035 static void
6036 pop_it (struct it *it)
6037 {
6038 struct iterator_stack_entry *p;
6039 bool from_display_prop = it->from_disp_prop_p;
6040 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6041
6042 eassert (it->sp > 0);
6043 --it->sp;
6044 p = it->stack + it->sp;
6045 it->stop_charpos = p->stop_charpos;
6046 it->prev_stop = p->prev_stop;
6047 it->base_level_stop = p->base_level_stop;
6048 it->cmp_it = p->cmp_it;
6049 it->face_id = p->face_id;
6050 it->current = p->current;
6051 it->position = p->position;
6052 it->string = p->string;
6053 it->from_overlay = p->from_overlay;
6054 if (NILP (it->string))
6055 SET_TEXT_POS (it->current.string_pos, -1, -1);
6056 it->method = p->method;
6057 switch (it->method)
6058 {
6059 case GET_FROM_IMAGE:
6060 it->image_id = p->u.image.image_id;
6061 it->object = p->u.image.object;
6062 it->slice = p->u.image.slice;
6063 break;
6064 case GET_FROM_STRETCH:
6065 it->object = p->u.stretch.object;
6066 break;
6067 case GET_FROM_BUFFER:
6068 it->object = it->w->contents;
6069 break;
6070 case GET_FROM_STRING:
6071 {
6072 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6073
6074 /* Restore the face_box_p flag, since it could have been
6075 overwritten by the face of the object that we just finished
6076 displaying. */
6077 if (face)
6078 it->face_box_p = face->box != FACE_NO_BOX;
6079 it->object = it->string;
6080 }
6081 break;
6082 case GET_FROM_DISPLAY_VECTOR:
6083 if (it->s)
6084 it->method = GET_FROM_C_STRING;
6085 else if (STRINGP (it->string))
6086 it->method = GET_FROM_STRING;
6087 else
6088 {
6089 it->method = GET_FROM_BUFFER;
6090 it->object = it->w->contents;
6091 }
6092 break;
6093 case GET_FROM_C_STRING:
6094 break;
6095 default:
6096 emacs_abort ();
6097 }
6098 it->end_charpos = p->end_charpos;
6099 it->string_nchars = p->string_nchars;
6100 it->area = p->area;
6101 it->multibyte_p = p->multibyte_p;
6102 it->avoid_cursor_p = p->avoid_cursor_p;
6103 it->space_width = p->space_width;
6104 it->font_height = p->font_height;
6105 it->voffset = p->voffset;
6106 it->string_from_display_prop_p = p->string_from_display_prop_p;
6107 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6108 it->line_wrap = p->line_wrap;
6109 it->bidi_p = p->bidi_p;
6110 it->paragraph_embedding = p->paragraph_embedding;
6111 it->from_disp_prop_p = p->from_disp_prop_p;
6112 if (it->bidi_p)
6113 {
6114 bidi_pop_it (&it->bidi_it);
6115 /* Bidi-iterate until we get out of the portion of text, if any,
6116 covered by a `display' text property or by an overlay with
6117 `display' property. (We cannot just jump there, because the
6118 internal coherency of the bidi iterator state can not be
6119 preserved across such jumps.) We also must determine the
6120 paragraph base direction if the overlay we just processed is
6121 at the beginning of a new paragraph. */
6122 if (from_display_prop
6123 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6124 iterate_out_of_display_property (it);
6125
6126 eassert ((BUFFERP (it->object)
6127 && IT_CHARPOS (*it) == it->bidi_it.charpos
6128 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6129 || (STRINGP (it->object)
6130 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6131 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6132 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6133 }
6134 /* If we move the iterator over text covered by a display property
6135 to a new buffer position, any info about previously seen overlays
6136 is no longer valid. */
6137 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6138 it->ignore_overlay_strings_at_pos_p = false;
6139 }
6140
6141
6142 \f
6143 /***********************************************************************
6144 Moving over lines
6145 ***********************************************************************/
6146
6147 /* Set IT's current position to the previous line start. */
6148
6149 static void
6150 back_to_previous_line_start (struct it *it)
6151 {
6152 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6153
6154 DEC_BOTH (cp, bp);
6155 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6156 }
6157
6158
6159 /* Move IT to the next line start.
6160
6161 Value is true if a newline was found. Set *SKIPPED_P to true if
6162 we skipped over part of the text (as opposed to moving the iterator
6163 continuously over the text). Otherwise, don't change the value
6164 of *SKIPPED_P.
6165
6166 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6167 iterator on the newline, if it was found.
6168
6169 Newlines may come from buffer text, overlay strings, or strings
6170 displayed via the `display' property. That's the reason we can't
6171 simply use find_newline_no_quit.
6172
6173 Note that this function may not skip over invisible text that is so
6174 because of text properties and immediately follows a newline. If
6175 it would, function reseat_at_next_visible_line_start, when called
6176 from set_iterator_to_next, would effectively make invisible
6177 characters following a newline part of the wrong glyph row, which
6178 leads to wrong cursor motion. */
6179
6180 static bool
6181 forward_to_next_line_start (struct it *it, bool *skipped_p,
6182 struct bidi_it *bidi_it_prev)
6183 {
6184 ptrdiff_t old_selective;
6185 bool newline_found_p = false;
6186 int n;
6187 const int MAX_NEWLINE_DISTANCE = 500;
6188
6189 /* If already on a newline, just consume it to avoid unintended
6190 skipping over invisible text below. */
6191 if (it->what == IT_CHARACTER
6192 && it->c == '\n'
6193 && CHARPOS (it->position) == IT_CHARPOS (*it))
6194 {
6195 if (it->bidi_p && bidi_it_prev)
6196 *bidi_it_prev = it->bidi_it;
6197 set_iterator_to_next (it, false);
6198 it->c = 0;
6199 return true;
6200 }
6201
6202 /* Don't handle selective display in the following. It's (a)
6203 unnecessary because it's done by the caller, and (b) leads to an
6204 infinite recursion because next_element_from_ellipsis indirectly
6205 calls this function. */
6206 old_selective = it->selective;
6207 it->selective = 0;
6208
6209 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6210 from buffer text. */
6211 for (n = 0;
6212 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6213 n += !STRINGP (it->string))
6214 {
6215 if (!get_next_display_element (it))
6216 return false;
6217 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6218 if (newline_found_p && it->bidi_p && bidi_it_prev)
6219 *bidi_it_prev = it->bidi_it;
6220 set_iterator_to_next (it, false);
6221 }
6222
6223 /* If we didn't find a newline near enough, see if we can use a
6224 short-cut. */
6225 if (!newline_found_p)
6226 {
6227 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6228 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6229 1, &bytepos);
6230 Lisp_Object pos;
6231
6232 eassert (!STRINGP (it->string));
6233
6234 /* If there isn't any `display' property in sight, and no
6235 overlays, we can just use the position of the newline in
6236 buffer text. */
6237 if (it->stop_charpos >= limit
6238 || ((pos = Fnext_single_property_change (make_number (start),
6239 Qdisplay, Qnil,
6240 make_number (limit)),
6241 NILP (pos))
6242 && next_overlay_change (start) == ZV))
6243 {
6244 if (!it->bidi_p)
6245 {
6246 IT_CHARPOS (*it) = limit;
6247 IT_BYTEPOS (*it) = bytepos;
6248 }
6249 else
6250 {
6251 struct bidi_it bprev;
6252
6253 /* Help bidi.c avoid expensive searches for display
6254 properties and overlays, by telling it that there are
6255 none up to `limit'. */
6256 if (it->bidi_it.disp_pos < limit)
6257 {
6258 it->bidi_it.disp_pos = limit;
6259 it->bidi_it.disp_prop = 0;
6260 }
6261 do {
6262 bprev = it->bidi_it;
6263 bidi_move_to_visually_next (&it->bidi_it);
6264 } while (it->bidi_it.charpos != limit);
6265 IT_CHARPOS (*it) = limit;
6266 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6267 if (bidi_it_prev)
6268 *bidi_it_prev = bprev;
6269 }
6270 *skipped_p = newline_found_p = true;
6271 }
6272 else
6273 {
6274 while (get_next_display_element (it)
6275 && !newline_found_p)
6276 {
6277 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6278 if (newline_found_p && it->bidi_p && bidi_it_prev)
6279 *bidi_it_prev = it->bidi_it;
6280 set_iterator_to_next (it, false);
6281 }
6282 }
6283 }
6284
6285 it->selective = old_selective;
6286 return newline_found_p;
6287 }
6288
6289
6290 /* Set IT's current position to the previous visible line start. Skip
6291 invisible text that is so either due to text properties or due to
6292 selective display. Caution: this does not change IT->current_x and
6293 IT->hpos. */
6294
6295 static void
6296 back_to_previous_visible_line_start (struct it *it)
6297 {
6298 while (IT_CHARPOS (*it) > BEGV)
6299 {
6300 back_to_previous_line_start (it);
6301
6302 if (IT_CHARPOS (*it) <= BEGV)
6303 break;
6304
6305 /* If selective > 0, then lines indented more than its value are
6306 invisible. */
6307 if (it->selective > 0
6308 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6309 it->selective))
6310 continue;
6311
6312 /* Check the newline before point for invisibility. */
6313 {
6314 Lisp_Object prop;
6315 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6316 Qinvisible, it->window);
6317 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6318 continue;
6319 }
6320
6321 if (IT_CHARPOS (*it) <= BEGV)
6322 break;
6323
6324 {
6325 struct it it2;
6326 void *it2data = NULL;
6327 ptrdiff_t pos;
6328 ptrdiff_t beg, end;
6329 Lisp_Object val, overlay;
6330
6331 SAVE_IT (it2, *it, it2data);
6332
6333 /* If newline is part of a composition, continue from start of composition */
6334 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6335 && beg < IT_CHARPOS (*it))
6336 goto replaced;
6337
6338 /* If newline is replaced by a display property, find start of overlay
6339 or interval and continue search from that point. */
6340 pos = --IT_CHARPOS (it2);
6341 --IT_BYTEPOS (it2);
6342 it2.sp = 0;
6343 bidi_unshelve_cache (NULL, false);
6344 it2.string_from_display_prop_p = false;
6345 it2.from_disp_prop_p = false;
6346 if (handle_display_prop (&it2) == HANDLED_RETURN
6347 && !NILP (val = get_char_property_and_overlay
6348 (make_number (pos), Qdisplay, Qnil, &overlay))
6349 && (OVERLAYP (overlay)
6350 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6351 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6352 {
6353 RESTORE_IT (it, it, it2data);
6354 goto replaced;
6355 }
6356
6357 /* Newline is not replaced by anything -- so we are done. */
6358 RESTORE_IT (it, it, it2data);
6359 break;
6360
6361 replaced:
6362 if (beg < BEGV)
6363 beg = BEGV;
6364 IT_CHARPOS (*it) = beg;
6365 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6366 }
6367 }
6368
6369 it->continuation_lines_width = 0;
6370
6371 eassert (IT_CHARPOS (*it) >= BEGV);
6372 eassert (IT_CHARPOS (*it) == BEGV
6373 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6374 CHECK_IT (it);
6375 }
6376
6377
6378 /* Reseat iterator IT at the previous visible line start. Skip
6379 invisible text that is so either due to text properties or due to
6380 selective display. At the end, update IT's overlay information,
6381 face information etc. */
6382
6383 void
6384 reseat_at_previous_visible_line_start (struct it *it)
6385 {
6386 back_to_previous_visible_line_start (it);
6387 reseat (it, it->current.pos, true);
6388 CHECK_IT (it);
6389 }
6390
6391
6392 /* Reseat iterator IT on the next visible line start in the current
6393 buffer. ON_NEWLINE_P means position IT on the newline
6394 preceding the line start. Skip over invisible text that is so
6395 because of selective display. Compute faces, overlays etc at the
6396 new position. Note that this function does not skip over text that
6397 is invisible because of text properties. */
6398
6399 static void
6400 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6401 {
6402 bool skipped_p = false;
6403 struct bidi_it bidi_it_prev;
6404 bool newline_found_p
6405 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6406
6407 /* Skip over lines that are invisible because they are indented
6408 more than the value of IT->selective. */
6409 if (it->selective > 0)
6410 while (IT_CHARPOS (*it) < ZV
6411 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6412 it->selective))
6413 {
6414 eassert (IT_BYTEPOS (*it) == BEGV
6415 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6416 newline_found_p =
6417 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6418 }
6419
6420 /* Position on the newline if that's what's requested. */
6421 if (on_newline_p && newline_found_p)
6422 {
6423 if (STRINGP (it->string))
6424 {
6425 if (IT_STRING_CHARPOS (*it) > 0)
6426 {
6427 if (!it->bidi_p)
6428 {
6429 --IT_STRING_CHARPOS (*it);
6430 --IT_STRING_BYTEPOS (*it);
6431 }
6432 else
6433 {
6434 /* We need to restore the bidi iterator to the state
6435 it had on the newline, and resync the IT's
6436 position with that. */
6437 it->bidi_it = bidi_it_prev;
6438 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6439 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6440 }
6441 }
6442 }
6443 else if (IT_CHARPOS (*it) > BEGV)
6444 {
6445 if (!it->bidi_p)
6446 {
6447 --IT_CHARPOS (*it);
6448 --IT_BYTEPOS (*it);
6449 }
6450 else
6451 {
6452 /* We need to restore the bidi iterator to the state it
6453 had on the newline and resync IT with that. */
6454 it->bidi_it = bidi_it_prev;
6455 IT_CHARPOS (*it) = it->bidi_it.charpos;
6456 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6457 }
6458 reseat (it, it->current.pos, false);
6459 }
6460 }
6461 else if (skipped_p)
6462 reseat (it, it->current.pos, false);
6463
6464 CHECK_IT (it);
6465 }
6466
6467
6468 \f
6469 /***********************************************************************
6470 Changing an iterator's position
6471 ***********************************************************************/
6472
6473 /* Change IT's current position to POS in current_buffer.
6474 If FORCE_P, always check for text properties at the new position.
6475 Otherwise, text properties are only looked up if POS >=
6476 IT->check_charpos of a property. */
6477
6478 static void
6479 reseat (struct it *it, struct text_pos pos, bool force_p)
6480 {
6481 ptrdiff_t original_pos = IT_CHARPOS (*it);
6482
6483 reseat_1 (it, pos, false);
6484
6485 /* Determine where to check text properties. Avoid doing it
6486 where possible because text property lookup is very expensive. */
6487 if (force_p
6488 || CHARPOS (pos) > it->stop_charpos
6489 || CHARPOS (pos) < original_pos)
6490 {
6491 if (it->bidi_p)
6492 {
6493 /* For bidi iteration, we need to prime prev_stop and
6494 base_level_stop with our best estimations. */
6495 /* Implementation note: Of course, POS is not necessarily a
6496 stop position, so assigning prev_pos to it is a lie; we
6497 should have called compute_stop_backwards. However, if
6498 the current buffer does not include any R2L characters,
6499 that call would be a waste of cycles, because the
6500 iterator will never move back, and thus never cross this
6501 "fake" stop position. So we delay that backward search
6502 until the time we really need it, in next_element_from_buffer. */
6503 if (CHARPOS (pos) != it->prev_stop)
6504 it->prev_stop = CHARPOS (pos);
6505 if (CHARPOS (pos) < it->base_level_stop)
6506 it->base_level_stop = 0; /* meaning it's unknown */
6507 handle_stop (it);
6508 }
6509 else
6510 {
6511 handle_stop (it);
6512 it->prev_stop = it->base_level_stop = 0;
6513 }
6514
6515 }
6516
6517 CHECK_IT (it);
6518 }
6519
6520
6521 /* Change IT's buffer position to POS. SET_STOP_P means set
6522 IT->stop_pos to POS, also. */
6523
6524 static void
6525 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6526 {
6527 /* Don't call this function when scanning a C string. */
6528 eassert (it->s == NULL);
6529
6530 /* POS must be a reasonable value. */
6531 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6532
6533 it->current.pos = it->position = pos;
6534 it->end_charpos = ZV;
6535 it->dpvec = NULL;
6536 it->current.dpvec_index = -1;
6537 it->current.overlay_string_index = -1;
6538 IT_STRING_CHARPOS (*it) = -1;
6539 IT_STRING_BYTEPOS (*it) = -1;
6540 it->string = Qnil;
6541 it->method = GET_FROM_BUFFER;
6542 it->object = it->w->contents;
6543 it->area = TEXT_AREA;
6544 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6545 it->sp = 0;
6546 it->string_from_display_prop_p = false;
6547 it->string_from_prefix_prop_p = false;
6548
6549 it->from_disp_prop_p = false;
6550 it->face_before_selective_p = false;
6551 if (it->bidi_p)
6552 {
6553 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6554 &it->bidi_it);
6555 bidi_unshelve_cache (NULL, false);
6556 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6557 it->bidi_it.string.s = NULL;
6558 it->bidi_it.string.lstring = Qnil;
6559 it->bidi_it.string.bufpos = 0;
6560 it->bidi_it.string.from_disp_str = false;
6561 it->bidi_it.string.unibyte = false;
6562 it->bidi_it.w = it->w;
6563 }
6564
6565 if (set_stop_p)
6566 {
6567 it->stop_charpos = CHARPOS (pos);
6568 it->base_level_stop = CHARPOS (pos);
6569 }
6570 /* This make the information stored in it->cmp_it invalidate. */
6571 it->cmp_it.id = -1;
6572 }
6573
6574
6575 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6576 If S is non-null, it is a C string to iterate over. Otherwise,
6577 STRING gives a Lisp string to iterate over.
6578
6579 If PRECISION > 0, don't return more then PRECISION number of
6580 characters from the string.
6581
6582 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6583 characters have been returned. FIELD_WIDTH < 0 means an infinite
6584 field width.
6585
6586 MULTIBYTE = 0 means disable processing of multibyte characters,
6587 MULTIBYTE > 0 means enable it,
6588 MULTIBYTE < 0 means use IT->multibyte_p.
6589
6590 IT must be initialized via a prior call to init_iterator before
6591 calling this function. */
6592
6593 static void
6594 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6595 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6596 int multibyte)
6597 {
6598 /* No text property checks performed by default, but see below. */
6599 it->stop_charpos = -1;
6600
6601 /* Set iterator position and end position. */
6602 memset (&it->current, 0, sizeof it->current);
6603 it->current.overlay_string_index = -1;
6604 it->current.dpvec_index = -1;
6605 eassert (charpos >= 0);
6606
6607 /* If STRING is specified, use its multibyteness, otherwise use the
6608 setting of MULTIBYTE, if specified. */
6609 if (multibyte >= 0)
6610 it->multibyte_p = multibyte > 0;
6611
6612 /* Bidirectional reordering of strings is controlled by the default
6613 value of bidi-display-reordering. Don't try to reorder while
6614 loading loadup.el, as the necessary character property tables are
6615 not yet available. */
6616 it->bidi_p =
6617 NILP (Vpurify_flag)
6618 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6619
6620 if (s == NULL)
6621 {
6622 eassert (STRINGP (string));
6623 it->string = string;
6624 it->s = NULL;
6625 it->end_charpos = it->string_nchars = SCHARS (string);
6626 it->method = GET_FROM_STRING;
6627 it->current.string_pos = string_pos (charpos, string);
6628
6629 if (it->bidi_p)
6630 {
6631 it->bidi_it.string.lstring = string;
6632 it->bidi_it.string.s = NULL;
6633 it->bidi_it.string.schars = it->end_charpos;
6634 it->bidi_it.string.bufpos = 0;
6635 it->bidi_it.string.from_disp_str = false;
6636 it->bidi_it.string.unibyte = !it->multibyte_p;
6637 it->bidi_it.w = it->w;
6638 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6639 FRAME_WINDOW_P (it->f), &it->bidi_it);
6640 }
6641 }
6642 else
6643 {
6644 it->s = (const unsigned char *) s;
6645 it->string = Qnil;
6646
6647 /* Note that we use IT->current.pos, not it->current.string_pos,
6648 for displaying C strings. */
6649 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6650 if (it->multibyte_p)
6651 {
6652 it->current.pos = c_string_pos (charpos, s, true);
6653 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6654 }
6655 else
6656 {
6657 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6658 it->end_charpos = it->string_nchars = strlen (s);
6659 }
6660
6661 if (it->bidi_p)
6662 {
6663 it->bidi_it.string.lstring = Qnil;
6664 it->bidi_it.string.s = (const unsigned char *) s;
6665 it->bidi_it.string.schars = it->end_charpos;
6666 it->bidi_it.string.bufpos = 0;
6667 it->bidi_it.string.from_disp_str = false;
6668 it->bidi_it.string.unibyte = !it->multibyte_p;
6669 it->bidi_it.w = it->w;
6670 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6671 &it->bidi_it);
6672 }
6673 it->method = GET_FROM_C_STRING;
6674 }
6675
6676 /* PRECISION > 0 means don't return more than PRECISION characters
6677 from the string. */
6678 if (precision > 0 && it->end_charpos - charpos > precision)
6679 {
6680 it->end_charpos = it->string_nchars = charpos + precision;
6681 if (it->bidi_p)
6682 it->bidi_it.string.schars = it->end_charpos;
6683 }
6684
6685 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6686 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6687 FIELD_WIDTH < 0 means infinite field width. This is useful for
6688 padding with `-' at the end of a mode line. */
6689 if (field_width < 0)
6690 field_width = INFINITY;
6691 /* Implementation note: We deliberately don't enlarge
6692 it->bidi_it.string.schars here to fit it->end_charpos, because
6693 the bidi iterator cannot produce characters out of thin air. */
6694 if (field_width > it->end_charpos - charpos)
6695 it->end_charpos = charpos + field_width;
6696
6697 /* Use the standard display table for displaying strings. */
6698 if (DISP_TABLE_P (Vstandard_display_table))
6699 it->dp = XCHAR_TABLE (Vstandard_display_table);
6700
6701 it->stop_charpos = charpos;
6702 it->prev_stop = charpos;
6703 it->base_level_stop = 0;
6704 if (it->bidi_p)
6705 {
6706 it->bidi_it.first_elt = true;
6707 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6708 it->bidi_it.disp_pos = -1;
6709 }
6710 if (s == NULL && it->multibyte_p)
6711 {
6712 ptrdiff_t endpos = SCHARS (it->string);
6713 if (endpos > it->end_charpos)
6714 endpos = it->end_charpos;
6715 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6716 it->string);
6717 }
6718 CHECK_IT (it);
6719 }
6720
6721
6722 \f
6723 /***********************************************************************
6724 Iteration
6725 ***********************************************************************/
6726
6727 /* Map enum it_method value to corresponding next_element_from_* function. */
6728
6729 typedef bool (*next_element_function) (struct it *);
6730
6731 static next_element_function const get_next_element[NUM_IT_METHODS] =
6732 {
6733 next_element_from_buffer,
6734 next_element_from_display_vector,
6735 next_element_from_string,
6736 next_element_from_c_string,
6737 next_element_from_image,
6738 next_element_from_stretch
6739 };
6740
6741 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6742
6743
6744 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6745 (possibly with the following characters). */
6746
6747 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6748 ((IT)->cmp_it.id >= 0 \
6749 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6750 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6751 END_CHARPOS, (IT)->w, \
6752 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6753 (IT)->string)))
6754
6755
6756 /* Lookup the char-table Vglyphless_char_display for character C (-1
6757 if we want information for no-font case), and return the display
6758 method symbol. By side-effect, update it->what and
6759 it->glyphless_method. This function is called from
6760 get_next_display_element for each character element, and from
6761 x_produce_glyphs when no suitable font was found. */
6762
6763 Lisp_Object
6764 lookup_glyphless_char_display (int c, struct it *it)
6765 {
6766 Lisp_Object glyphless_method = Qnil;
6767
6768 if (CHAR_TABLE_P (Vglyphless_char_display)
6769 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6770 {
6771 if (c >= 0)
6772 {
6773 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6774 if (CONSP (glyphless_method))
6775 glyphless_method = FRAME_WINDOW_P (it->f)
6776 ? XCAR (glyphless_method)
6777 : XCDR (glyphless_method);
6778 }
6779 else
6780 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6781 }
6782
6783 retry:
6784 if (NILP (glyphless_method))
6785 {
6786 if (c >= 0)
6787 /* The default is to display the character by a proper font. */
6788 return Qnil;
6789 /* The default for the no-font case is to display an empty box. */
6790 glyphless_method = Qempty_box;
6791 }
6792 if (EQ (glyphless_method, Qzero_width))
6793 {
6794 if (c >= 0)
6795 return glyphless_method;
6796 /* This method can't be used for the no-font case. */
6797 glyphless_method = Qempty_box;
6798 }
6799 if (EQ (glyphless_method, Qthin_space))
6800 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6801 else if (EQ (glyphless_method, Qempty_box))
6802 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6803 else if (EQ (glyphless_method, Qhex_code))
6804 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6805 else if (STRINGP (glyphless_method))
6806 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6807 else
6808 {
6809 /* Invalid value. We use the default method. */
6810 glyphless_method = Qnil;
6811 goto retry;
6812 }
6813 it->what = IT_GLYPHLESS;
6814 return glyphless_method;
6815 }
6816
6817 /* Merge escape glyph face and cache the result. */
6818
6819 static struct frame *last_escape_glyph_frame = NULL;
6820 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6821 static int last_escape_glyph_merged_face_id = 0;
6822
6823 static int
6824 merge_escape_glyph_face (struct it *it)
6825 {
6826 int face_id;
6827
6828 if (it->f == last_escape_glyph_frame
6829 && it->face_id == last_escape_glyph_face_id)
6830 face_id = last_escape_glyph_merged_face_id;
6831 else
6832 {
6833 /* Merge the `escape-glyph' face into the current face. */
6834 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6835 last_escape_glyph_frame = it->f;
6836 last_escape_glyph_face_id = it->face_id;
6837 last_escape_glyph_merged_face_id = face_id;
6838 }
6839 return face_id;
6840 }
6841
6842 /* Likewise for glyphless glyph face. */
6843
6844 static struct frame *last_glyphless_glyph_frame = NULL;
6845 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6846 static int last_glyphless_glyph_merged_face_id = 0;
6847
6848 int
6849 merge_glyphless_glyph_face (struct it *it)
6850 {
6851 int face_id;
6852
6853 if (it->f == last_glyphless_glyph_frame
6854 && it->face_id == last_glyphless_glyph_face_id)
6855 face_id = last_glyphless_glyph_merged_face_id;
6856 else
6857 {
6858 /* Merge the `glyphless-char' face into the current face. */
6859 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6860 last_glyphless_glyph_frame = it->f;
6861 last_glyphless_glyph_face_id = it->face_id;
6862 last_glyphless_glyph_merged_face_id = face_id;
6863 }
6864 return face_id;
6865 }
6866
6867 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6868 be called before redisplaying windows, and when the frame's face
6869 cache is freed. */
6870 void
6871 forget_escape_and_glyphless_faces (void)
6872 {
6873 last_escape_glyph_frame = NULL;
6874 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6875 last_glyphless_glyph_frame = NULL;
6876 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6877 }
6878
6879 /* Load IT's display element fields with information about the next
6880 display element from the current position of IT. Value is false if
6881 end of buffer (or C string) is reached. */
6882
6883 static bool
6884 get_next_display_element (struct it *it)
6885 {
6886 /* True means that we found a display element. False means that
6887 we hit the end of what we iterate over. Performance note: the
6888 function pointer `method' used here turns out to be faster than
6889 using a sequence of if-statements. */
6890 bool success_p;
6891
6892 get_next:
6893 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6894
6895 if (it->what == IT_CHARACTER)
6896 {
6897 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6898 and only if (a) the resolved directionality of that character
6899 is R..." */
6900 /* FIXME: Do we need an exception for characters from display
6901 tables? */
6902 if (it->bidi_p && it->bidi_it.type == STRONG_R
6903 && !inhibit_bidi_mirroring)
6904 it->c = bidi_mirror_char (it->c);
6905 /* Map via display table or translate control characters.
6906 IT->c, IT->len etc. have been set to the next character by
6907 the function call above. If we have a display table, and it
6908 contains an entry for IT->c, translate it. Don't do this if
6909 IT->c itself comes from a display table, otherwise we could
6910 end up in an infinite recursion. (An alternative could be to
6911 count the recursion depth of this function and signal an
6912 error when a certain maximum depth is reached.) Is it worth
6913 it? */
6914 if (success_p && it->dpvec == NULL)
6915 {
6916 Lisp_Object dv;
6917 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6918 bool nonascii_space_p = false;
6919 bool nonascii_hyphen_p = false;
6920 int c = it->c; /* This is the character to display. */
6921
6922 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6923 {
6924 eassert (SINGLE_BYTE_CHAR_P (c));
6925 if (unibyte_display_via_language_environment)
6926 {
6927 c = DECODE_CHAR (unibyte, c);
6928 if (c < 0)
6929 c = BYTE8_TO_CHAR (it->c);
6930 }
6931 else
6932 c = BYTE8_TO_CHAR (it->c);
6933 }
6934
6935 if (it->dp
6936 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6937 VECTORP (dv)))
6938 {
6939 struct Lisp_Vector *v = XVECTOR (dv);
6940
6941 /* Return the first character from the display table
6942 entry, if not empty. If empty, don't display the
6943 current character. */
6944 if (v->header.size)
6945 {
6946 it->dpvec_char_len = it->len;
6947 it->dpvec = v->contents;
6948 it->dpend = v->contents + v->header.size;
6949 it->current.dpvec_index = 0;
6950 it->dpvec_face_id = -1;
6951 it->saved_face_id = it->face_id;
6952 it->method = GET_FROM_DISPLAY_VECTOR;
6953 it->ellipsis_p = false;
6954 }
6955 else
6956 {
6957 set_iterator_to_next (it, false);
6958 }
6959 goto get_next;
6960 }
6961
6962 if (! NILP (lookup_glyphless_char_display (c, it)))
6963 {
6964 if (it->what == IT_GLYPHLESS)
6965 goto done;
6966 /* Don't display this character. */
6967 set_iterator_to_next (it, false);
6968 goto get_next;
6969 }
6970
6971 /* If `nobreak-char-display' is non-nil, we display
6972 non-ASCII spaces and hyphens specially. */
6973 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6974 {
6975 if (c == NO_BREAK_SPACE)
6976 nonascii_space_p = true;
6977 else if (c == SOFT_HYPHEN || c == HYPHEN
6978 || c == NON_BREAKING_HYPHEN)
6979 nonascii_hyphen_p = true;
6980 }
6981
6982 /* Translate control characters into `\003' or `^C' form.
6983 Control characters coming from a display table entry are
6984 currently not translated because we use IT->dpvec to hold
6985 the translation. This could easily be changed but I
6986 don't believe that it is worth doing.
6987
6988 The characters handled by `nobreak-char-display' must be
6989 translated too.
6990
6991 Non-printable characters and raw-byte characters are also
6992 translated to octal form. */
6993 if (((c < ' ' || c == 127) /* ASCII control chars. */
6994 ? (it->area != TEXT_AREA
6995 /* In mode line, treat \n, \t like other crl chars. */
6996 || (c != '\t'
6997 && it->glyph_row
6998 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6999 || (c != '\n' && c != '\t'))
7000 : (nonascii_space_p
7001 || nonascii_hyphen_p
7002 || CHAR_BYTE8_P (c)
7003 || ! CHAR_PRINTABLE_P (c))))
7004 {
7005 /* C is a control character, non-ASCII space/hyphen,
7006 raw-byte, or a non-printable character which must be
7007 displayed either as '\003' or as `^C' where the '\\'
7008 and '^' can be defined in the display table. Fill
7009 IT->ctl_chars with glyphs for what we have to
7010 display. Then, set IT->dpvec to these glyphs. */
7011 Lisp_Object gc;
7012 int ctl_len;
7013 int face_id;
7014 int lface_id = 0;
7015 int escape_glyph;
7016
7017 /* Handle control characters with ^. */
7018
7019 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7020 {
7021 int g;
7022
7023 g = '^'; /* default glyph for Control */
7024 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7025 if (it->dp
7026 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7027 {
7028 g = GLYPH_CODE_CHAR (gc);
7029 lface_id = GLYPH_CODE_FACE (gc);
7030 }
7031
7032 face_id = (lface_id
7033 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7034 : merge_escape_glyph_face (it));
7035
7036 XSETINT (it->ctl_chars[0], g);
7037 XSETINT (it->ctl_chars[1], c ^ 0100);
7038 ctl_len = 2;
7039 goto display_control;
7040 }
7041
7042 /* Handle non-ascii space in the mode where it only gets
7043 highlighting. */
7044
7045 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7046 {
7047 /* Merge `nobreak-space' into the current face. */
7048 face_id = merge_faces (it->f, Qnobreak_space, 0,
7049 it->face_id);
7050 XSETINT (it->ctl_chars[0], ' ');
7051 ctl_len = 1;
7052 goto display_control;
7053 }
7054
7055 /* Handle sequences that start with the "escape glyph". */
7056
7057 /* the default escape glyph is \. */
7058 escape_glyph = '\\';
7059
7060 if (it->dp
7061 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7062 {
7063 escape_glyph = GLYPH_CODE_CHAR (gc);
7064 lface_id = GLYPH_CODE_FACE (gc);
7065 }
7066
7067 face_id = (lface_id
7068 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7069 : merge_escape_glyph_face (it));
7070
7071 /* Draw non-ASCII hyphen with just highlighting: */
7072
7073 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7074 {
7075 XSETINT (it->ctl_chars[0], '-');
7076 ctl_len = 1;
7077 goto display_control;
7078 }
7079
7080 /* Draw non-ASCII space/hyphen with escape glyph: */
7081
7082 if (nonascii_space_p || nonascii_hyphen_p)
7083 {
7084 XSETINT (it->ctl_chars[0], escape_glyph);
7085 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7086 ctl_len = 2;
7087 goto display_control;
7088 }
7089
7090 {
7091 char str[10];
7092 int len, i;
7093
7094 if (CHAR_BYTE8_P (c))
7095 /* Display \200 instead of \17777600. */
7096 c = CHAR_TO_BYTE8 (c);
7097 len = sprintf (str, "%03o", c + 0u);
7098
7099 XSETINT (it->ctl_chars[0], escape_glyph);
7100 for (i = 0; i < len; i++)
7101 XSETINT (it->ctl_chars[i + 1], str[i]);
7102 ctl_len = len + 1;
7103 }
7104
7105 display_control:
7106 /* Set up IT->dpvec and return first character from it. */
7107 it->dpvec_char_len = it->len;
7108 it->dpvec = it->ctl_chars;
7109 it->dpend = it->dpvec + ctl_len;
7110 it->current.dpvec_index = 0;
7111 it->dpvec_face_id = face_id;
7112 it->saved_face_id = it->face_id;
7113 it->method = GET_FROM_DISPLAY_VECTOR;
7114 it->ellipsis_p = false;
7115 goto get_next;
7116 }
7117 it->char_to_display = c;
7118 }
7119 else if (success_p)
7120 {
7121 it->char_to_display = it->c;
7122 }
7123 }
7124
7125 #ifdef HAVE_WINDOW_SYSTEM
7126 /* Adjust face id for a multibyte character. There are no multibyte
7127 character in unibyte text. */
7128 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7129 && it->multibyte_p
7130 && success_p
7131 && FRAME_WINDOW_P (it->f))
7132 {
7133 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7134
7135 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7136 {
7137 /* Automatic composition with glyph-string. */
7138 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7139
7140 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7141 }
7142 else
7143 {
7144 ptrdiff_t pos = (it->s ? -1
7145 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7146 : IT_CHARPOS (*it));
7147 int c;
7148
7149 if (it->what == IT_CHARACTER)
7150 c = it->char_to_display;
7151 else
7152 {
7153 struct composition *cmp = composition_table[it->cmp_it.id];
7154 int i;
7155
7156 c = ' ';
7157 for (i = 0; i < cmp->glyph_len; i++)
7158 /* TAB in a composition means display glyphs with
7159 padding space on the left or right. */
7160 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7161 break;
7162 }
7163 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7164 }
7165 }
7166 #endif /* HAVE_WINDOW_SYSTEM */
7167
7168 done:
7169 /* Is this character the last one of a run of characters with
7170 box? If yes, set IT->end_of_box_run_p to true. */
7171 if (it->face_box_p
7172 && it->s == NULL)
7173 {
7174 if (it->method == GET_FROM_STRING && it->sp)
7175 {
7176 int face_id = underlying_face_id (it);
7177 struct face *face = FACE_FROM_ID (it->f, face_id);
7178
7179 if (face)
7180 {
7181 if (face->box == FACE_NO_BOX)
7182 {
7183 /* If the box comes from face properties in a
7184 display string, check faces in that string. */
7185 int string_face_id = face_after_it_pos (it);
7186 it->end_of_box_run_p
7187 = (FACE_FROM_ID (it->f, string_face_id)->box
7188 == FACE_NO_BOX);
7189 }
7190 /* Otherwise, the box comes from the underlying face.
7191 If this is the last string character displayed, check
7192 the next buffer location. */
7193 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7194 /* n_overlay_strings is unreliable unless
7195 overlay_string_index is non-negative. */
7196 && ((it->current.overlay_string_index >= 0
7197 && (it->current.overlay_string_index
7198 == it->n_overlay_strings - 1))
7199 /* A string from display property. */
7200 || it->from_disp_prop_p))
7201 {
7202 ptrdiff_t ignore;
7203 int next_face_id;
7204 struct text_pos pos = it->current.pos;
7205
7206 /* For a string from a display property, the next
7207 buffer position is stored in the 'position'
7208 member of the iteration stack slot below the
7209 current one, see handle_single_display_spec. By
7210 contrast, it->current.pos was is not yet updated
7211 to point to that buffer position; that will
7212 happen in pop_it, after we finish displaying the
7213 current string. Note that we already checked
7214 above that it->sp is positive, so subtracting one
7215 from it is safe. */
7216 if (it->from_disp_prop_p)
7217 pos = (it->stack + it->sp - 1)->position;
7218 else
7219 INC_TEXT_POS (pos, it->multibyte_p);
7220
7221 if (CHARPOS (pos) >= ZV)
7222 it->end_of_box_run_p = true;
7223 else
7224 {
7225 next_face_id = face_at_buffer_position
7226 (it->w, CHARPOS (pos), &ignore,
7227 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, false, -1);
7228 it->end_of_box_run_p
7229 = (FACE_FROM_ID (it->f, next_face_id)->box
7230 == FACE_NO_BOX);
7231 }
7232 }
7233 }
7234 }
7235 /* next_element_from_display_vector sets this flag according to
7236 faces of the display vector glyphs, see there. */
7237 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7238 {
7239 int face_id = face_after_it_pos (it);
7240 it->end_of_box_run_p
7241 = (face_id != it->face_id
7242 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7243 }
7244 }
7245 /* If we reached the end of the object we've been iterating (e.g., a
7246 display string or an overlay string), and there's something on
7247 IT->stack, proceed with what's on the stack. It doesn't make
7248 sense to return false if there's unprocessed stuff on the stack,
7249 because otherwise that stuff will never be displayed. */
7250 if (!success_p && it->sp > 0)
7251 {
7252 set_iterator_to_next (it, false);
7253 success_p = get_next_display_element (it);
7254 }
7255
7256 /* Value is false if end of buffer or string reached. */
7257 return success_p;
7258 }
7259
7260
7261 /* Move IT to the next display element.
7262
7263 RESEAT_P means if called on a newline in buffer text,
7264 skip to the next visible line start.
7265
7266 Functions get_next_display_element and set_iterator_to_next are
7267 separate because I find this arrangement easier to handle than a
7268 get_next_display_element function that also increments IT's
7269 position. The way it is we can first look at an iterator's current
7270 display element, decide whether it fits on a line, and if it does,
7271 increment the iterator position. The other way around we probably
7272 would either need a flag indicating whether the iterator has to be
7273 incremented the next time, or we would have to implement a
7274 decrement position function which would not be easy to write. */
7275
7276 void
7277 set_iterator_to_next (struct it *it, bool reseat_p)
7278 {
7279 /* Reset flags indicating start and end of a sequence of characters
7280 with box. Reset them at the start of this function because
7281 moving the iterator to a new position might set them. */
7282 it->start_of_box_run_p = it->end_of_box_run_p = false;
7283
7284 switch (it->method)
7285 {
7286 case GET_FROM_BUFFER:
7287 /* The current display element of IT is a character from
7288 current_buffer. Advance in the buffer, and maybe skip over
7289 invisible lines that are so because of selective display. */
7290 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7291 reseat_at_next_visible_line_start (it, false);
7292 else if (it->cmp_it.id >= 0)
7293 {
7294 /* We are currently getting glyphs from a composition. */
7295 if (! it->bidi_p)
7296 {
7297 IT_CHARPOS (*it) += it->cmp_it.nchars;
7298 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7299 }
7300 else
7301 {
7302 int i;
7303
7304 /* Update IT's char/byte positions to point to the first
7305 character of the next grapheme cluster, or to the
7306 character visually after the current composition. */
7307 for (i = 0; i < it->cmp_it.nchars; i++)
7308 bidi_move_to_visually_next (&it->bidi_it);
7309 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7310 IT_CHARPOS (*it) = it->bidi_it.charpos;
7311 }
7312
7313 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7314 && it->cmp_it.to < it->cmp_it.nglyphs)
7315 {
7316 /* Composition created while scanning forward. Proceed
7317 to the next grapheme cluster. */
7318 it->cmp_it.from = it->cmp_it.to;
7319 }
7320 else if ((it->bidi_p && it->cmp_it.reversed_p)
7321 && it->cmp_it.from > 0)
7322 {
7323 /* Composition created while scanning backward. Proceed
7324 to the previous grapheme cluster. */
7325 it->cmp_it.to = it->cmp_it.from;
7326 }
7327 else
7328 {
7329 /* No more grapheme clusters in this composition.
7330 Find the next stop position. */
7331 ptrdiff_t stop = it->end_charpos;
7332
7333 if (it->bidi_it.scan_dir < 0)
7334 /* Now we are scanning backward and don't know
7335 where to stop. */
7336 stop = -1;
7337 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7338 IT_BYTEPOS (*it), stop, Qnil);
7339 }
7340 }
7341 else
7342 {
7343 eassert (it->len != 0);
7344
7345 if (!it->bidi_p)
7346 {
7347 IT_BYTEPOS (*it) += it->len;
7348 IT_CHARPOS (*it) += 1;
7349 }
7350 else
7351 {
7352 int prev_scan_dir = it->bidi_it.scan_dir;
7353 /* If this is a new paragraph, determine its base
7354 direction (a.k.a. its base embedding level). */
7355 if (it->bidi_it.new_paragraph)
7356 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7357 false);
7358 bidi_move_to_visually_next (&it->bidi_it);
7359 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7360 IT_CHARPOS (*it) = it->bidi_it.charpos;
7361 if (prev_scan_dir != it->bidi_it.scan_dir)
7362 {
7363 /* As the scan direction was changed, we must
7364 re-compute the stop position for composition. */
7365 ptrdiff_t stop = it->end_charpos;
7366 if (it->bidi_it.scan_dir < 0)
7367 stop = -1;
7368 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7369 IT_BYTEPOS (*it), stop, Qnil);
7370 }
7371 }
7372 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7373 }
7374 break;
7375
7376 case GET_FROM_C_STRING:
7377 /* Current display element of IT is from a C string. */
7378 if (!it->bidi_p
7379 /* If the string position is beyond string's end, it means
7380 next_element_from_c_string is padding the string with
7381 blanks, in which case we bypass the bidi iterator,
7382 because it cannot deal with such virtual characters. */
7383 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7384 {
7385 IT_BYTEPOS (*it) += it->len;
7386 IT_CHARPOS (*it) += 1;
7387 }
7388 else
7389 {
7390 bidi_move_to_visually_next (&it->bidi_it);
7391 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7392 IT_CHARPOS (*it) = it->bidi_it.charpos;
7393 }
7394 break;
7395
7396 case GET_FROM_DISPLAY_VECTOR:
7397 /* Current display element of IT is from a display table entry.
7398 Advance in the display table definition. Reset it to null if
7399 end reached, and continue with characters from buffers/
7400 strings. */
7401 ++it->current.dpvec_index;
7402
7403 /* Restore face of the iterator to what they were before the
7404 display vector entry (these entries may contain faces). */
7405 it->face_id = it->saved_face_id;
7406
7407 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7408 {
7409 bool recheck_faces = it->ellipsis_p;
7410
7411 if (it->s)
7412 it->method = GET_FROM_C_STRING;
7413 else if (STRINGP (it->string))
7414 it->method = GET_FROM_STRING;
7415 else
7416 {
7417 it->method = GET_FROM_BUFFER;
7418 it->object = it->w->contents;
7419 }
7420
7421 it->dpvec = NULL;
7422 it->current.dpvec_index = -1;
7423
7424 /* Skip over characters which were displayed via IT->dpvec. */
7425 if (it->dpvec_char_len < 0)
7426 reseat_at_next_visible_line_start (it, true);
7427 else if (it->dpvec_char_len > 0)
7428 {
7429 it->len = it->dpvec_char_len;
7430 set_iterator_to_next (it, reseat_p);
7431 }
7432
7433 /* Maybe recheck faces after display vector. */
7434 if (recheck_faces)
7435 {
7436 if (it->method == GET_FROM_STRING)
7437 it->stop_charpos = IT_STRING_CHARPOS (*it);
7438 else
7439 it->stop_charpos = IT_CHARPOS (*it);
7440 }
7441 }
7442 break;
7443
7444 case GET_FROM_STRING:
7445 /* Current display element is a character from a Lisp string. */
7446 eassert (it->s == NULL && STRINGP (it->string));
7447 /* Don't advance past string end. These conditions are true
7448 when set_iterator_to_next is called at the end of
7449 get_next_display_element, in which case the Lisp string is
7450 already exhausted, and all we want is pop the iterator
7451 stack. */
7452 if (it->current.overlay_string_index >= 0)
7453 {
7454 /* This is an overlay string, so there's no padding with
7455 spaces, and the number of characters in the string is
7456 where the string ends. */
7457 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7458 goto consider_string_end;
7459 }
7460 else
7461 {
7462 /* Not an overlay string. There could be padding, so test
7463 against it->end_charpos. */
7464 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7465 goto consider_string_end;
7466 }
7467 if (it->cmp_it.id >= 0)
7468 {
7469 /* We are delivering display elements from a composition.
7470 Update the string position past the grapheme cluster
7471 we've just processed. */
7472 if (! it->bidi_p)
7473 {
7474 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7475 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7476 }
7477 else
7478 {
7479 int i;
7480
7481 for (i = 0; i < it->cmp_it.nchars; i++)
7482 bidi_move_to_visually_next (&it->bidi_it);
7483 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7484 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7485 }
7486
7487 /* Did we exhaust all the grapheme clusters of this
7488 composition? */
7489 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7490 && (it->cmp_it.to < it->cmp_it.nglyphs))
7491 {
7492 /* Not all the grapheme clusters were processed yet;
7493 advance to the next cluster. */
7494 it->cmp_it.from = it->cmp_it.to;
7495 }
7496 else if ((it->bidi_p && it->cmp_it.reversed_p)
7497 && it->cmp_it.from > 0)
7498 {
7499 /* Likewise: advance to the next cluster, but going in
7500 the reverse direction. */
7501 it->cmp_it.to = it->cmp_it.from;
7502 }
7503 else
7504 {
7505 /* This composition was fully processed; find the next
7506 candidate place for checking for composed
7507 characters. */
7508 /* Always limit string searches to the string length;
7509 any padding spaces are not part of the string, and
7510 there cannot be any compositions in that padding. */
7511 ptrdiff_t stop = SCHARS (it->string);
7512
7513 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7514 stop = -1;
7515 else if (it->end_charpos < stop)
7516 {
7517 /* Cf. PRECISION in reseat_to_string: we might be
7518 limited in how many of the string characters we
7519 need to deliver. */
7520 stop = it->end_charpos;
7521 }
7522 composition_compute_stop_pos (&it->cmp_it,
7523 IT_STRING_CHARPOS (*it),
7524 IT_STRING_BYTEPOS (*it), stop,
7525 it->string);
7526 }
7527 }
7528 else
7529 {
7530 if (!it->bidi_p
7531 /* If the string position is beyond string's end, it
7532 means next_element_from_string is padding the string
7533 with blanks, in which case we bypass the bidi
7534 iterator, because it cannot deal with such virtual
7535 characters. */
7536 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7537 {
7538 IT_STRING_BYTEPOS (*it) += it->len;
7539 IT_STRING_CHARPOS (*it) += 1;
7540 }
7541 else
7542 {
7543 int prev_scan_dir = it->bidi_it.scan_dir;
7544
7545 bidi_move_to_visually_next (&it->bidi_it);
7546 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7547 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7548 /* If the scan direction changes, we may need to update
7549 the place where to check for composed characters. */
7550 if (prev_scan_dir != it->bidi_it.scan_dir)
7551 {
7552 ptrdiff_t stop = SCHARS (it->string);
7553
7554 if (it->bidi_it.scan_dir < 0)
7555 stop = -1;
7556 else if (it->end_charpos < stop)
7557 stop = it->end_charpos;
7558
7559 composition_compute_stop_pos (&it->cmp_it,
7560 IT_STRING_CHARPOS (*it),
7561 IT_STRING_BYTEPOS (*it), stop,
7562 it->string);
7563 }
7564 }
7565 }
7566
7567 consider_string_end:
7568
7569 if (it->current.overlay_string_index >= 0)
7570 {
7571 /* IT->string is an overlay string. Advance to the
7572 next, if there is one. */
7573 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7574 {
7575 it->ellipsis_p = false;
7576 next_overlay_string (it);
7577 if (it->ellipsis_p)
7578 setup_for_ellipsis (it, 0);
7579 }
7580 }
7581 else
7582 {
7583 /* IT->string is not an overlay string. If we reached
7584 its end, and there is something on IT->stack, proceed
7585 with what is on the stack. This can be either another
7586 string, this time an overlay string, or a buffer. */
7587 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7588 && it->sp > 0)
7589 {
7590 pop_it (it);
7591 if (it->method == GET_FROM_STRING)
7592 goto consider_string_end;
7593 }
7594 }
7595 break;
7596
7597 case GET_FROM_IMAGE:
7598 case GET_FROM_STRETCH:
7599 /* The position etc with which we have to proceed are on
7600 the stack. The position may be at the end of a string,
7601 if the `display' property takes up the whole string. */
7602 eassert (it->sp > 0);
7603 pop_it (it);
7604 if (it->method == GET_FROM_STRING)
7605 goto consider_string_end;
7606 break;
7607
7608 default:
7609 /* There are no other methods defined, so this should be a bug. */
7610 emacs_abort ();
7611 }
7612
7613 eassert (it->method != GET_FROM_STRING
7614 || (STRINGP (it->string)
7615 && IT_STRING_CHARPOS (*it) >= 0));
7616 }
7617
7618 /* Load IT's display element fields with information about the next
7619 display element which comes from a display table entry or from the
7620 result of translating a control character to one of the forms `^C'
7621 or `\003'.
7622
7623 IT->dpvec holds the glyphs to return as characters.
7624 IT->saved_face_id holds the face id before the display vector--it
7625 is restored into IT->face_id in set_iterator_to_next. */
7626
7627 static bool
7628 next_element_from_display_vector (struct it *it)
7629 {
7630 Lisp_Object gc;
7631 int prev_face_id = it->face_id;
7632 int next_face_id;
7633
7634 /* Precondition. */
7635 eassert (it->dpvec && it->current.dpvec_index >= 0);
7636
7637 it->face_id = it->saved_face_id;
7638
7639 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7640 That seemed totally bogus - so I changed it... */
7641 gc = it->dpvec[it->current.dpvec_index];
7642
7643 if (GLYPH_CODE_P (gc))
7644 {
7645 struct face *this_face, *prev_face, *next_face;
7646
7647 it->c = GLYPH_CODE_CHAR (gc);
7648 it->len = CHAR_BYTES (it->c);
7649
7650 /* The entry may contain a face id to use. Such a face id is
7651 the id of a Lisp face, not a realized face. A face id of
7652 zero means no face is specified. */
7653 if (it->dpvec_face_id >= 0)
7654 it->face_id = it->dpvec_face_id;
7655 else
7656 {
7657 int lface_id = GLYPH_CODE_FACE (gc);
7658 if (lface_id > 0)
7659 it->face_id = merge_faces (it->f, Qt, lface_id,
7660 it->saved_face_id);
7661 }
7662
7663 /* Glyphs in the display vector could have the box face, so we
7664 need to set the related flags in the iterator, as
7665 appropriate. */
7666 this_face = FACE_FROM_ID (it->f, it->face_id);
7667 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7668
7669 /* Is this character the first character of a box-face run? */
7670 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7671 && (!prev_face
7672 || prev_face->box == FACE_NO_BOX));
7673
7674 /* For the last character of the box-face run, we need to look
7675 either at the next glyph from the display vector, or at the
7676 face we saw before the display vector. */
7677 next_face_id = it->saved_face_id;
7678 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7679 {
7680 if (it->dpvec_face_id >= 0)
7681 next_face_id = it->dpvec_face_id;
7682 else
7683 {
7684 int lface_id =
7685 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7686
7687 if (lface_id > 0)
7688 next_face_id = merge_faces (it->f, Qt, lface_id,
7689 it->saved_face_id);
7690 }
7691 }
7692 next_face = FACE_FROM_ID (it->f, next_face_id);
7693 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7694 && (!next_face
7695 || next_face->box == FACE_NO_BOX));
7696 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7697 }
7698 else
7699 /* Display table entry is invalid. Return a space. */
7700 it->c = ' ', it->len = 1;
7701
7702 /* Don't change position and object of the iterator here. They are
7703 still the values of the character that had this display table
7704 entry or was translated, and that's what we want. */
7705 it->what = IT_CHARACTER;
7706 return true;
7707 }
7708
7709 /* Get the first element of string/buffer in the visual order, after
7710 being reseated to a new position in a string or a buffer. */
7711 static void
7712 get_visually_first_element (struct it *it)
7713 {
7714 bool string_p = STRINGP (it->string) || it->s;
7715 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7716 ptrdiff_t bob = (string_p ? 0 : BEGV);
7717
7718 if (STRINGP (it->string))
7719 {
7720 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7721 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7722 }
7723 else
7724 {
7725 it->bidi_it.charpos = IT_CHARPOS (*it);
7726 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7727 }
7728
7729 if (it->bidi_it.charpos == eob)
7730 {
7731 /* Nothing to do, but reset the FIRST_ELT flag, like
7732 bidi_paragraph_init does, because we are not going to
7733 call it. */
7734 it->bidi_it.first_elt = false;
7735 }
7736 else if (it->bidi_it.charpos == bob
7737 || (!string_p
7738 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7739 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7740 {
7741 /* If we are at the beginning of a line/string, we can produce
7742 the next element right away. */
7743 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7744 bidi_move_to_visually_next (&it->bidi_it);
7745 }
7746 else
7747 {
7748 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7749
7750 /* We need to prime the bidi iterator starting at the line's or
7751 string's beginning, before we will be able to produce the
7752 next element. */
7753 if (string_p)
7754 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7755 else
7756 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7757 IT_BYTEPOS (*it), -1,
7758 &it->bidi_it.bytepos);
7759 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7760 do
7761 {
7762 /* Now return to buffer/string position where we were asked
7763 to get the next display element, and produce that. */
7764 bidi_move_to_visually_next (&it->bidi_it);
7765 }
7766 while (it->bidi_it.bytepos != orig_bytepos
7767 && it->bidi_it.charpos < eob);
7768 }
7769
7770 /* Adjust IT's position information to where we ended up. */
7771 if (STRINGP (it->string))
7772 {
7773 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7774 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7775 }
7776 else
7777 {
7778 IT_CHARPOS (*it) = it->bidi_it.charpos;
7779 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7780 }
7781
7782 if (STRINGP (it->string) || !it->s)
7783 {
7784 ptrdiff_t stop, charpos, bytepos;
7785
7786 if (STRINGP (it->string))
7787 {
7788 eassert (!it->s);
7789 stop = SCHARS (it->string);
7790 if (stop > it->end_charpos)
7791 stop = it->end_charpos;
7792 charpos = IT_STRING_CHARPOS (*it);
7793 bytepos = IT_STRING_BYTEPOS (*it);
7794 }
7795 else
7796 {
7797 stop = it->end_charpos;
7798 charpos = IT_CHARPOS (*it);
7799 bytepos = IT_BYTEPOS (*it);
7800 }
7801 if (it->bidi_it.scan_dir < 0)
7802 stop = -1;
7803 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7804 it->string);
7805 }
7806 }
7807
7808 /* Load IT with the next display element from Lisp string IT->string.
7809 IT->current.string_pos is the current position within the string.
7810 If IT->current.overlay_string_index >= 0, the Lisp string is an
7811 overlay string. */
7812
7813 static bool
7814 next_element_from_string (struct it *it)
7815 {
7816 struct text_pos position;
7817
7818 eassert (STRINGP (it->string));
7819 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7820 eassert (IT_STRING_CHARPOS (*it) >= 0);
7821 position = it->current.string_pos;
7822
7823 /* With bidi reordering, the character to display might not be the
7824 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7825 that we were reseat()ed to a new string, whose paragraph
7826 direction is not known. */
7827 if (it->bidi_p && it->bidi_it.first_elt)
7828 {
7829 get_visually_first_element (it);
7830 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7831 }
7832
7833 /* Time to check for invisible text? */
7834 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7835 {
7836 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7837 {
7838 if (!(!it->bidi_p
7839 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7840 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7841 {
7842 /* With bidi non-linear iteration, we could find
7843 ourselves far beyond the last computed stop_charpos,
7844 with several other stop positions in between that we
7845 missed. Scan them all now, in buffer's logical
7846 order, until we find and handle the last stop_charpos
7847 that precedes our current position. */
7848 handle_stop_backwards (it, it->stop_charpos);
7849 return GET_NEXT_DISPLAY_ELEMENT (it);
7850 }
7851 else
7852 {
7853 if (it->bidi_p)
7854 {
7855 /* Take note of the stop position we just moved
7856 across, for when we will move back across it. */
7857 it->prev_stop = it->stop_charpos;
7858 /* If we are at base paragraph embedding level, take
7859 note of the last stop position seen at this
7860 level. */
7861 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7862 it->base_level_stop = it->stop_charpos;
7863 }
7864 handle_stop (it);
7865
7866 /* Since a handler may have changed IT->method, we must
7867 recurse here. */
7868 return GET_NEXT_DISPLAY_ELEMENT (it);
7869 }
7870 }
7871 else if (it->bidi_p
7872 /* If we are before prev_stop, we may have overstepped
7873 on our way backwards a stop_pos, and if so, we need
7874 to handle that stop_pos. */
7875 && IT_STRING_CHARPOS (*it) < it->prev_stop
7876 /* We can sometimes back up for reasons that have nothing
7877 to do with bidi reordering. E.g., compositions. The
7878 code below is only needed when we are above the base
7879 embedding level, so test for that explicitly. */
7880 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7881 {
7882 /* If we lost track of base_level_stop, we have no better
7883 place for handle_stop_backwards to start from than string
7884 beginning. This happens, e.g., when we were reseated to
7885 the previous screenful of text by vertical-motion. */
7886 if (it->base_level_stop <= 0
7887 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7888 it->base_level_stop = 0;
7889 handle_stop_backwards (it, it->base_level_stop);
7890 return GET_NEXT_DISPLAY_ELEMENT (it);
7891 }
7892 }
7893
7894 if (it->current.overlay_string_index >= 0)
7895 {
7896 /* Get the next character from an overlay string. In overlay
7897 strings, there is no field width or padding with spaces to
7898 do. */
7899 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7900 {
7901 it->what = IT_EOB;
7902 return false;
7903 }
7904 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7905 IT_STRING_BYTEPOS (*it),
7906 it->bidi_it.scan_dir < 0
7907 ? -1
7908 : SCHARS (it->string))
7909 && next_element_from_composition (it))
7910 {
7911 return true;
7912 }
7913 else if (STRING_MULTIBYTE (it->string))
7914 {
7915 const unsigned char *s = (SDATA (it->string)
7916 + IT_STRING_BYTEPOS (*it));
7917 it->c = string_char_and_length (s, &it->len);
7918 }
7919 else
7920 {
7921 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7922 it->len = 1;
7923 }
7924 }
7925 else
7926 {
7927 /* Get the next character from a Lisp string that is not an
7928 overlay string. Such strings come from the mode line, for
7929 example. We may have to pad with spaces, or truncate the
7930 string. See also next_element_from_c_string. */
7931 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7932 {
7933 it->what = IT_EOB;
7934 return false;
7935 }
7936 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7937 {
7938 /* Pad with spaces. */
7939 it->c = ' ', it->len = 1;
7940 CHARPOS (position) = BYTEPOS (position) = -1;
7941 }
7942 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7943 IT_STRING_BYTEPOS (*it),
7944 it->bidi_it.scan_dir < 0
7945 ? -1
7946 : it->string_nchars)
7947 && next_element_from_composition (it))
7948 {
7949 return true;
7950 }
7951 else if (STRING_MULTIBYTE (it->string))
7952 {
7953 const unsigned char *s = (SDATA (it->string)
7954 + IT_STRING_BYTEPOS (*it));
7955 it->c = string_char_and_length (s, &it->len);
7956 }
7957 else
7958 {
7959 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7960 it->len = 1;
7961 }
7962 }
7963
7964 /* Record what we have and where it came from. */
7965 it->what = IT_CHARACTER;
7966 it->object = it->string;
7967 it->position = position;
7968 return true;
7969 }
7970
7971
7972 /* Load IT with next display element from C string IT->s.
7973 IT->string_nchars is the maximum number of characters to return
7974 from the string. IT->end_charpos may be greater than
7975 IT->string_nchars when this function is called, in which case we
7976 may have to return padding spaces. Value is false if end of string
7977 reached, including padding spaces. */
7978
7979 static bool
7980 next_element_from_c_string (struct it *it)
7981 {
7982 bool success_p = true;
7983
7984 eassert (it->s);
7985 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7986 it->what = IT_CHARACTER;
7987 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7988 it->object = make_number (0);
7989
7990 /* With bidi reordering, the character to display might not be the
7991 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
7992 we were reseated to a new string, whose paragraph direction is
7993 not known. */
7994 if (it->bidi_p && it->bidi_it.first_elt)
7995 get_visually_first_element (it);
7996
7997 /* IT's position can be greater than IT->string_nchars in case a
7998 field width or precision has been specified when the iterator was
7999 initialized. */
8000 if (IT_CHARPOS (*it) >= it->end_charpos)
8001 {
8002 /* End of the game. */
8003 it->what = IT_EOB;
8004 success_p = false;
8005 }
8006 else if (IT_CHARPOS (*it) >= it->string_nchars)
8007 {
8008 /* Pad with spaces. */
8009 it->c = ' ', it->len = 1;
8010 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8011 }
8012 else if (it->multibyte_p)
8013 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8014 else
8015 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8016
8017 return success_p;
8018 }
8019
8020
8021 /* Set up IT to return characters from an ellipsis, if appropriate.
8022 The definition of the ellipsis glyphs may come from a display table
8023 entry. This function fills IT with the first glyph from the
8024 ellipsis if an ellipsis is to be displayed. */
8025
8026 static bool
8027 next_element_from_ellipsis (struct it *it)
8028 {
8029 if (it->selective_display_ellipsis_p)
8030 setup_for_ellipsis (it, it->len);
8031 else
8032 {
8033 /* The face at the current position may be different from the
8034 face we find after the invisible text. Remember what it
8035 was in IT->saved_face_id, and signal that it's there by
8036 setting face_before_selective_p. */
8037 it->saved_face_id = it->face_id;
8038 it->method = GET_FROM_BUFFER;
8039 it->object = it->w->contents;
8040 reseat_at_next_visible_line_start (it, true);
8041 it->face_before_selective_p = true;
8042 }
8043
8044 return GET_NEXT_DISPLAY_ELEMENT (it);
8045 }
8046
8047
8048 /* Deliver an image display element. The iterator IT is already
8049 filled with image information (done in handle_display_prop). Value
8050 is always true. */
8051
8052
8053 static bool
8054 next_element_from_image (struct it *it)
8055 {
8056 it->what = IT_IMAGE;
8057 return true;
8058 }
8059
8060
8061 /* Fill iterator IT with next display element from a stretch glyph
8062 property. IT->object is the value of the text property. Value is
8063 always true. */
8064
8065 static bool
8066 next_element_from_stretch (struct it *it)
8067 {
8068 it->what = IT_STRETCH;
8069 return true;
8070 }
8071
8072 /* Scan backwards from IT's current position until we find a stop
8073 position, or until BEGV. This is called when we find ourself
8074 before both the last known prev_stop and base_level_stop while
8075 reordering bidirectional text. */
8076
8077 static void
8078 compute_stop_pos_backwards (struct it *it)
8079 {
8080 const int SCAN_BACK_LIMIT = 1000;
8081 struct text_pos pos;
8082 struct display_pos save_current = it->current;
8083 struct text_pos save_position = it->position;
8084 ptrdiff_t charpos = IT_CHARPOS (*it);
8085 ptrdiff_t where_we_are = charpos;
8086 ptrdiff_t save_stop_pos = it->stop_charpos;
8087 ptrdiff_t save_end_pos = it->end_charpos;
8088
8089 eassert (NILP (it->string) && !it->s);
8090 eassert (it->bidi_p);
8091 it->bidi_p = false;
8092 do
8093 {
8094 it->end_charpos = min (charpos + 1, ZV);
8095 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8096 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8097 reseat_1 (it, pos, false);
8098 compute_stop_pos (it);
8099 /* We must advance forward, right? */
8100 if (it->stop_charpos <= charpos)
8101 emacs_abort ();
8102 }
8103 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8104
8105 if (it->stop_charpos <= where_we_are)
8106 it->prev_stop = it->stop_charpos;
8107 else
8108 it->prev_stop = BEGV;
8109 it->bidi_p = true;
8110 it->current = save_current;
8111 it->position = save_position;
8112 it->stop_charpos = save_stop_pos;
8113 it->end_charpos = save_end_pos;
8114 }
8115
8116 /* Scan forward from CHARPOS in the current buffer/string, until we
8117 find a stop position > current IT's position. Then handle the stop
8118 position before that. This is called when we bump into a stop
8119 position while reordering bidirectional text. CHARPOS should be
8120 the last previously processed stop_pos (or BEGV/0, if none were
8121 processed yet) whose position is less that IT's current
8122 position. */
8123
8124 static void
8125 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8126 {
8127 bool bufp = !STRINGP (it->string);
8128 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8129 struct display_pos save_current = it->current;
8130 struct text_pos save_position = it->position;
8131 struct text_pos pos1;
8132 ptrdiff_t next_stop;
8133
8134 /* Scan in strict logical order. */
8135 eassert (it->bidi_p);
8136 it->bidi_p = false;
8137 do
8138 {
8139 it->prev_stop = charpos;
8140 if (bufp)
8141 {
8142 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8143 reseat_1 (it, pos1, false);
8144 }
8145 else
8146 it->current.string_pos = string_pos (charpos, it->string);
8147 compute_stop_pos (it);
8148 /* We must advance forward, right? */
8149 if (it->stop_charpos <= it->prev_stop)
8150 emacs_abort ();
8151 charpos = it->stop_charpos;
8152 }
8153 while (charpos <= where_we_are);
8154
8155 it->bidi_p = true;
8156 it->current = save_current;
8157 it->position = save_position;
8158 next_stop = it->stop_charpos;
8159 it->stop_charpos = it->prev_stop;
8160 handle_stop (it);
8161 it->stop_charpos = next_stop;
8162 }
8163
8164 /* Load IT with the next display element from current_buffer. Value
8165 is false if end of buffer reached. IT->stop_charpos is the next
8166 position at which to stop and check for text properties or buffer
8167 end. */
8168
8169 static bool
8170 next_element_from_buffer (struct it *it)
8171 {
8172 bool success_p = true;
8173
8174 eassert (IT_CHARPOS (*it) >= BEGV);
8175 eassert (NILP (it->string) && !it->s);
8176 eassert (!it->bidi_p
8177 || (EQ (it->bidi_it.string.lstring, Qnil)
8178 && it->bidi_it.string.s == NULL));
8179
8180 /* With bidi reordering, the character to display might not be the
8181 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8182 we were reseat()ed to a new buffer position, which is potentially
8183 a different paragraph. */
8184 if (it->bidi_p && it->bidi_it.first_elt)
8185 {
8186 get_visually_first_element (it);
8187 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8188 }
8189
8190 if (IT_CHARPOS (*it) >= it->stop_charpos)
8191 {
8192 if (IT_CHARPOS (*it) >= it->end_charpos)
8193 {
8194 bool overlay_strings_follow_p;
8195
8196 /* End of the game, except when overlay strings follow that
8197 haven't been returned yet. */
8198 if (it->overlay_strings_at_end_processed_p)
8199 overlay_strings_follow_p = false;
8200 else
8201 {
8202 it->overlay_strings_at_end_processed_p = true;
8203 overlay_strings_follow_p = get_overlay_strings (it, 0);
8204 }
8205
8206 if (overlay_strings_follow_p)
8207 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8208 else
8209 {
8210 it->what = IT_EOB;
8211 it->position = it->current.pos;
8212 success_p = false;
8213 }
8214 }
8215 else if (!(!it->bidi_p
8216 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8217 || IT_CHARPOS (*it) == it->stop_charpos))
8218 {
8219 /* With bidi non-linear iteration, we could find ourselves
8220 far beyond the last computed stop_charpos, with several
8221 other stop positions in between that we missed. Scan
8222 them all now, in buffer's logical order, until we find
8223 and handle the last stop_charpos that precedes our
8224 current position. */
8225 handle_stop_backwards (it, it->stop_charpos);
8226 it->ignore_overlay_strings_at_pos_p = false;
8227 return GET_NEXT_DISPLAY_ELEMENT (it);
8228 }
8229 else
8230 {
8231 if (it->bidi_p)
8232 {
8233 /* Take note of the stop position we just moved across,
8234 for when we will move back across it. */
8235 it->prev_stop = it->stop_charpos;
8236 /* If we are at base paragraph embedding level, take
8237 note of the last stop position seen at this
8238 level. */
8239 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8240 it->base_level_stop = it->stop_charpos;
8241 }
8242 handle_stop (it);
8243 it->ignore_overlay_strings_at_pos_p = false;
8244 return GET_NEXT_DISPLAY_ELEMENT (it);
8245 }
8246 }
8247 else if (it->bidi_p
8248 /* If we are before prev_stop, we may have overstepped on
8249 our way backwards a stop_pos, and if so, we need to
8250 handle that stop_pos. */
8251 && IT_CHARPOS (*it) < it->prev_stop
8252 /* We can sometimes back up for reasons that have nothing
8253 to do with bidi reordering. E.g., compositions. The
8254 code below is only needed when we are above the base
8255 embedding level, so test for that explicitly. */
8256 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8257 {
8258 if (it->base_level_stop <= 0
8259 || IT_CHARPOS (*it) < it->base_level_stop)
8260 {
8261 /* If we lost track of base_level_stop, we need to find
8262 prev_stop by looking backwards. This happens, e.g., when
8263 we were reseated to the previous screenful of text by
8264 vertical-motion. */
8265 it->base_level_stop = BEGV;
8266 compute_stop_pos_backwards (it);
8267 handle_stop_backwards (it, it->prev_stop);
8268 }
8269 else
8270 handle_stop_backwards (it, it->base_level_stop);
8271 it->ignore_overlay_strings_at_pos_p = false;
8272 return GET_NEXT_DISPLAY_ELEMENT (it);
8273 }
8274 else
8275 {
8276 /* No face changes, overlays etc. in sight, so just return a
8277 character from current_buffer. */
8278 unsigned char *p;
8279 ptrdiff_t stop;
8280
8281 /* We moved to the next buffer position, so any info about
8282 previously seen overlays is no longer valid. */
8283 it->ignore_overlay_strings_at_pos_p = false;
8284
8285 /* Maybe run the redisplay end trigger hook. Performance note:
8286 This doesn't seem to cost measurable time. */
8287 if (it->redisplay_end_trigger_charpos
8288 && it->glyph_row
8289 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8290 run_redisplay_end_trigger_hook (it);
8291
8292 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8293 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8294 stop)
8295 && next_element_from_composition (it))
8296 {
8297 return true;
8298 }
8299
8300 /* Get the next character, maybe multibyte. */
8301 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8302 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8303 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8304 else
8305 it->c = *p, it->len = 1;
8306
8307 /* Record what we have and where it came from. */
8308 it->what = IT_CHARACTER;
8309 it->object = it->w->contents;
8310 it->position = it->current.pos;
8311
8312 /* Normally we return the character found above, except when we
8313 really want to return an ellipsis for selective display. */
8314 if (it->selective)
8315 {
8316 if (it->c == '\n')
8317 {
8318 /* A value of selective > 0 means hide lines indented more
8319 than that number of columns. */
8320 if (it->selective > 0
8321 && IT_CHARPOS (*it) + 1 < ZV
8322 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8323 IT_BYTEPOS (*it) + 1,
8324 it->selective))
8325 {
8326 success_p = next_element_from_ellipsis (it);
8327 it->dpvec_char_len = -1;
8328 }
8329 }
8330 else if (it->c == '\r' && it->selective == -1)
8331 {
8332 /* A value of selective == -1 means that everything from the
8333 CR to the end of the line is invisible, with maybe an
8334 ellipsis displayed for it. */
8335 success_p = next_element_from_ellipsis (it);
8336 it->dpvec_char_len = -1;
8337 }
8338 }
8339 }
8340
8341 /* Value is false if end of buffer reached. */
8342 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8343 return success_p;
8344 }
8345
8346
8347 /* Run the redisplay end trigger hook for IT. */
8348
8349 static void
8350 run_redisplay_end_trigger_hook (struct it *it)
8351 {
8352 /* IT->glyph_row should be non-null, i.e. we should be actually
8353 displaying something, or otherwise we should not run the hook. */
8354 eassert (it->glyph_row);
8355
8356 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8357 it->redisplay_end_trigger_charpos = 0;
8358
8359 /* Since we are *trying* to run these functions, don't try to run
8360 them again, even if they get an error. */
8361 wset_redisplay_end_trigger (it->w, Qnil);
8362 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8363 make_number (charpos));
8364
8365 /* Notice if it changed the face of the character we are on. */
8366 handle_face_prop (it);
8367 }
8368
8369
8370 /* Deliver a composition display element. Unlike the other
8371 next_element_from_XXX, this function is not registered in the array
8372 get_next_element[]. It is called from next_element_from_buffer and
8373 next_element_from_string when necessary. */
8374
8375 static bool
8376 next_element_from_composition (struct it *it)
8377 {
8378 it->what = IT_COMPOSITION;
8379 it->len = it->cmp_it.nbytes;
8380 if (STRINGP (it->string))
8381 {
8382 if (it->c < 0)
8383 {
8384 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8385 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8386 return false;
8387 }
8388 it->position = it->current.string_pos;
8389 it->object = it->string;
8390 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8391 IT_STRING_BYTEPOS (*it), it->string);
8392 }
8393 else
8394 {
8395 if (it->c < 0)
8396 {
8397 IT_CHARPOS (*it) += it->cmp_it.nchars;
8398 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8399 if (it->bidi_p)
8400 {
8401 if (it->bidi_it.new_paragraph)
8402 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8403 false);
8404 /* Resync the bidi iterator with IT's new position.
8405 FIXME: this doesn't support bidirectional text. */
8406 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8407 bidi_move_to_visually_next (&it->bidi_it);
8408 }
8409 return false;
8410 }
8411 it->position = it->current.pos;
8412 it->object = it->w->contents;
8413 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8414 IT_BYTEPOS (*it), Qnil);
8415 }
8416 return true;
8417 }
8418
8419
8420 \f
8421 /***********************************************************************
8422 Moving an iterator without producing glyphs
8423 ***********************************************************************/
8424
8425 /* Check if iterator is at a position corresponding to a valid buffer
8426 position after some move_it_ call. */
8427
8428 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8429 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8430
8431
8432 /* Move iterator IT to a specified buffer or X position within one
8433 line on the display without producing glyphs.
8434
8435 OP should be a bit mask including some or all of these bits:
8436 MOVE_TO_X: Stop upon reaching x-position TO_X.
8437 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8438 Regardless of OP's value, stop upon reaching the end of the display line.
8439
8440 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8441 This means, in particular, that TO_X includes window's horizontal
8442 scroll amount.
8443
8444 The return value has several possible values that
8445 say what condition caused the scan to stop:
8446
8447 MOVE_POS_MATCH_OR_ZV
8448 - when TO_POS or ZV was reached.
8449
8450 MOVE_X_REACHED
8451 -when TO_X was reached before TO_POS or ZV were reached.
8452
8453 MOVE_LINE_CONTINUED
8454 - when we reached the end of the display area and the line must
8455 be continued.
8456
8457 MOVE_LINE_TRUNCATED
8458 - when we reached the end of the display area and the line is
8459 truncated.
8460
8461 MOVE_NEWLINE_OR_CR
8462 - when we stopped at a line end, i.e. a newline or a CR and selective
8463 display is on. */
8464
8465 static enum move_it_result
8466 move_it_in_display_line_to (struct it *it,
8467 ptrdiff_t to_charpos, int to_x,
8468 enum move_operation_enum op)
8469 {
8470 enum move_it_result result = MOVE_UNDEFINED;
8471 struct glyph_row *saved_glyph_row;
8472 struct it wrap_it, atpos_it, atx_it, ppos_it;
8473 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8474 void *ppos_data = NULL;
8475 bool may_wrap = false;
8476 enum it_method prev_method = it->method;
8477 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8478 bool saw_smaller_pos = prev_pos < to_charpos;
8479
8480 /* Don't produce glyphs in produce_glyphs. */
8481 saved_glyph_row = it->glyph_row;
8482 it->glyph_row = NULL;
8483
8484 /* Use wrap_it to save a copy of IT wherever a word wrap could
8485 occur. Use atpos_it to save a copy of IT at the desired buffer
8486 position, if found, so that we can scan ahead and check if the
8487 word later overshoots the window edge. Use atx_it similarly, for
8488 pixel positions. */
8489 wrap_it.sp = -1;
8490 atpos_it.sp = -1;
8491 atx_it.sp = -1;
8492
8493 /* Use ppos_it under bidi reordering to save a copy of IT for the
8494 initial position. We restore that position in IT when we have
8495 scanned the entire display line without finding a match for
8496 TO_CHARPOS and all the character positions are greater than
8497 TO_CHARPOS. We then restart the scan from the initial position,
8498 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8499 the closest to TO_CHARPOS. */
8500 if (it->bidi_p)
8501 {
8502 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8503 {
8504 SAVE_IT (ppos_it, *it, ppos_data);
8505 closest_pos = IT_CHARPOS (*it);
8506 }
8507 else
8508 closest_pos = ZV;
8509 }
8510
8511 #define BUFFER_POS_REACHED_P() \
8512 ((op & MOVE_TO_POS) != 0 \
8513 && BUFFERP (it->object) \
8514 && (IT_CHARPOS (*it) == to_charpos \
8515 || ((!it->bidi_p \
8516 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8517 && IT_CHARPOS (*it) > to_charpos) \
8518 || (it->what == IT_COMPOSITION \
8519 && ((IT_CHARPOS (*it) > to_charpos \
8520 && to_charpos >= it->cmp_it.charpos) \
8521 || (IT_CHARPOS (*it) < to_charpos \
8522 && to_charpos <= it->cmp_it.charpos)))) \
8523 && (it->method == GET_FROM_BUFFER \
8524 || (it->method == GET_FROM_DISPLAY_VECTOR \
8525 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8526
8527 /* If there's a line-/wrap-prefix, handle it. */
8528 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8529 && it->current_y < it->last_visible_y)
8530 handle_line_prefix (it);
8531
8532 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8533 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8534
8535 while (true)
8536 {
8537 int x, i, ascent = 0, descent = 0;
8538
8539 /* Utility macro to reset an iterator with x, ascent, and descent. */
8540 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8541 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8542 (IT)->max_descent = descent)
8543
8544 /* Stop if we move beyond TO_CHARPOS (after an image or a
8545 display string or stretch glyph). */
8546 if ((op & MOVE_TO_POS) != 0
8547 && BUFFERP (it->object)
8548 && it->method == GET_FROM_BUFFER
8549 && (((!it->bidi_p
8550 /* When the iterator is at base embedding level, we
8551 are guaranteed that characters are delivered for
8552 display in strictly increasing order of their
8553 buffer positions. */
8554 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8555 && IT_CHARPOS (*it) > to_charpos)
8556 || (it->bidi_p
8557 && (prev_method == GET_FROM_IMAGE
8558 || prev_method == GET_FROM_STRETCH
8559 || prev_method == GET_FROM_STRING)
8560 /* Passed TO_CHARPOS from left to right. */
8561 && ((prev_pos < to_charpos
8562 && IT_CHARPOS (*it) > to_charpos)
8563 /* Passed TO_CHARPOS from right to left. */
8564 || (prev_pos > to_charpos
8565 && IT_CHARPOS (*it) < to_charpos)))))
8566 {
8567 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8568 {
8569 result = MOVE_POS_MATCH_OR_ZV;
8570 break;
8571 }
8572 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8573 /* If wrap_it is valid, the current position might be in a
8574 word that is wrapped. So, save the iterator in
8575 atpos_it and continue to see if wrapping happens. */
8576 SAVE_IT (atpos_it, *it, atpos_data);
8577 }
8578
8579 /* Stop when ZV reached.
8580 We used to stop here when TO_CHARPOS reached as well, but that is
8581 too soon if this glyph does not fit on this line. So we handle it
8582 explicitly below. */
8583 if (!get_next_display_element (it))
8584 {
8585 result = MOVE_POS_MATCH_OR_ZV;
8586 break;
8587 }
8588
8589 if (it->line_wrap == TRUNCATE)
8590 {
8591 if (BUFFER_POS_REACHED_P ())
8592 {
8593 result = MOVE_POS_MATCH_OR_ZV;
8594 break;
8595 }
8596 }
8597 else
8598 {
8599 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8600 {
8601 if (IT_DISPLAYING_WHITESPACE (it))
8602 may_wrap = true;
8603 else if (may_wrap)
8604 {
8605 /* We have reached a glyph that follows one or more
8606 whitespace characters. If the position is
8607 already found, we are done. */
8608 if (atpos_it.sp >= 0)
8609 {
8610 RESTORE_IT (it, &atpos_it, atpos_data);
8611 result = MOVE_POS_MATCH_OR_ZV;
8612 goto done;
8613 }
8614 if (atx_it.sp >= 0)
8615 {
8616 RESTORE_IT (it, &atx_it, atx_data);
8617 result = MOVE_X_REACHED;
8618 goto done;
8619 }
8620 /* Otherwise, we can wrap here. */
8621 SAVE_IT (wrap_it, *it, wrap_data);
8622 may_wrap = false;
8623 }
8624 }
8625 }
8626
8627 /* Remember the line height for the current line, in case
8628 the next element doesn't fit on the line. */
8629 ascent = it->max_ascent;
8630 descent = it->max_descent;
8631
8632 /* The call to produce_glyphs will get the metrics of the
8633 display element IT is loaded with. Record the x-position
8634 before this display element, in case it doesn't fit on the
8635 line. */
8636 x = it->current_x;
8637
8638 PRODUCE_GLYPHS (it);
8639
8640 if (it->area != TEXT_AREA)
8641 {
8642 prev_method = it->method;
8643 if (it->method == GET_FROM_BUFFER)
8644 prev_pos = IT_CHARPOS (*it);
8645 set_iterator_to_next (it, true);
8646 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8647 SET_TEXT_POS (this_line_min_pos,
8648 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8649 if (it->bidi_p
8650 && (op & MOVE_TO_POS)
8651 && IT_CHARPOS (*it) > to_charpos
8652 && IT_CHARPOS (*it) < closest_pos)
8653 closest_pos = IT_CHARPOS (*it);
8654 continue;
8655 }
8656
8657 /* The number of glyphs we get back in IT->nglyphs will normally
8658 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8659 character on a terminal frame, or (iii) a line end. For the
8660 second case, IT->nglyphs - 1 padding glyphs will be present.
8661 (On X frames, there is only one glyph produced for a
8662 composite character.)
8663
8664 The behavior implemented below means, for continuation lines,
8665 that as many spaces of a TAB as fit on the current line are
8666 displayed there. For terminal frames, as many glyphs of a
8667 multi-glyph character are displayed in the current line, too.
8668 This is what the old redisplay code did, and we keep it that
8669 way. Under X, the whole shape of a complex character must
8670 fit on the line or it will be completely displayed in the
8671 next line.
8672
8673 Note that both for tabs and padding glyphs, all glyphs have
8674 the same width. */
8675 if (it->nglyphs)
8676 {
8677 /* More than one glyph or glyph doesn't fit on line. All
8678 glyphs have the same width. */
8679 int single_glyph_width = it->pixel_width / it->nglyphs;
8680 int new_x;
8681 int x_before_this_char = x;
8682 int hpos_before_this_char = it->hpos;
8683
8684 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8685 {
8686 new_x = x + single_glyph_width;
8687
8688 /* We want to leave anything reaching TO_X to the caller. */
8689 if ((op & MOVE_TO_X) && new_x > to_x)
8690 {
8691 if (BUFFER_POS_REACHED_P ())
8692 {
8693 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8694 goto buffer_pos_reached;
8695 if (atpos_it.sp < 0)
8696 {
8697 SAVE_IT (atpos_it, *it, atpos_data);
8698 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8699 }
8700 }
8701 else
8702 {
8703 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8704 {
8705 it->current_x = x;
8706 result = MOVE_X_REACHED;
8707 break;
8708 }
8709 if (atx_it.sp < 0)
8710 {
8711 SAVE_IT (atx_it, *it, atx_data);
8712 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8713 }
8714 }
8715 }
8716
8717 if (/* Lines are continued. */
8718 it->line_wrap != TRUNCATE
8719 && (/* And glyph doesn't fit on the line. */
8720 new_x > it->last_visible_x
8721 /* Or it fits exactly and we're on a window
8722 system frame. */
8723 || (new_x == it->last_visible_x
8724 && FRAME_WINDOW_P (it->f)
8725 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8726 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8727 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8728 {
8729 if (/* IT->hpos == 0 means the very first glyph
8730 doesn't fit on the line, e.g. a wide image. */
8731 it->hpos == 0
8732 || (new_x == it->last_visible_x
8733 && FRAME_WINDOW_P (it->f)))
8734 {
8735 ++it->hpos;
8736 it->current_x = new_x;
8737
8738 /* The character's last glyph just barely fits
8739 in this row. */
8740 if (i == it->nglyphs - 1)
8741 {
8742 /* If this is the destination position,
8743 return a position *before* it in this row,
8744 now that we know it fits in this row. */
8745 if (BUFFER_POS_REACHED_P ())
8746 {
8747 if (it->line_wrap != WORD_WRAP
8748 || wrap_it.sp < 0
8749 /* If we've just found whitespace to
8750 wrap, effectively ignore the
8751 previous wrap point -- it is no
8752 longer relevant, but we won't
8753 have an opportunity to update it,
8754 since we've reached the edge of
8755 this screen line. */
8756 || (may_wrap
8757 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8758 {
8759 it->hpos = hpos_before_this_char;
8760 it->current_x = x_before_this_char;
8761 result = MOVE_POS_MATCH_OR_ZV;
8762 break;
8763 }
8764 if (it->line_wrap == WORD_WRAP
8765 && atpos_it.sp < 0)
8766 {
8767 SAVE_IT (atpos_it, *it, atpos_data);
8768 atpos_it.current_x = x_before_this_char;
8769 atpos_it.hpos = hpos_before_this_char;
8770 }
8771 }
8772
8773 prev_method = it->method;
8774 if (it->method == GET_FROM_BUFFER)
8775 prev_pos = IT_CHARPOS (*it);
8776 set_iterator_to_next (it, true);
8777 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8778 SET_TEXT_POS (this_line_min_pos,
8779 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8780 /* On graphical terminals, newlines may
8781 "overflow" into the fringe if
8782 overflow-newline-into-fringe is non-nil.
8783 On text terminals, and on graphical
8784 terminals with no right margin, newlines
8785 may overflow into the last glyph on the
8786 display line.*/
8787 if (!FRAME_WINDOW_P (it->f)
8788 || ((it->bidi_p
8789 && it->bidi_it.paragraph_dir == R2L)
8790 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8791 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8792 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8793 {
8794 if (!get_next_display_element (it))
8795 {
8796 result = MOVE_POS_MATCH_OR_ZV;
8797 break;
8798 }
8799 if (BUFFER_POS_REACHED_P ())
8800 {
8801 if (ITERATOR_AT_END_OF_LINE_P (it))
8802 result = MOVE_POS_MATCH_OR_ZV;
8803 else
8804 result = MOVE_LINE_CONTINUED;
8805 break;
8806 }
8807 if (ITERATOR_AT_END_OF_LINE_P (it)
8808 && (it->line_wrap != WORD_WRAP
8809 || wrap_it.sp < 0
8810 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8811 {
8812 result = MOVE_NEWLINE_OR_CR;
8813 break;
8814 }
8815 }
8816 }
8817 }
8818 else
8819 IT_RESET_X_ASCENT_DESCENT (it);
8820
8821 /* If the screen line ends with whitespace, and we
8822 are under word-wrap, don't use wrap_it: it is no
8823 longer relevant, but we won't have an opportunity
8824 to update it, since we are done with this screen
8825 line. */
8826 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8827 {
8828 /* If we've found TO_X, go back there, as we now
8829 know the last word fits on this screen line. */
8830 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8831 && atx_it.sp >= 0)
8832 {
8833 RESTORE_IT (it, &atx_it, atx_data);
8834 atpos_it.sp = -1;
8835 atx_it.sp = -1;
8836 result = MOVE_X_REACHED;
8837 break;
8838 }
8839 }
8840 else if (wrap_it.sp >= 0)
8841 {
8842 RESTORE_IT (it, &wrap_it, wrap_data);
8843 atpos_it.sp = -1;
8844 atx_it.sp = -1;
8845 }
8846
8847 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8848 IT_CHARPOS (*it)));
8849 result = MOVE_LINE_CONTINUED;
8850 break;
8851 }
8852
8853 if (BUFFER_POS_REACHED_P ())
8854 {
8855 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8856 goto buffer_pos_reached;
8857 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8858 {
8859 SAVE_IT (atpos_it, *it, atpos_data);
8860 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8861 }
8862 }
8863
8864 if (new_x > it->first_visible_x)
8865 {
8866 /* Glyph is visible. Increment number of glyphs that
8867 would be displayed. */
8868 ++it->hpos;
8869 }
8870 }
8871
8872 if (result != MOVE_UNDEFINED)
8873 break;
8874 }
8875 else if (BUFFER_POS_REACHED_P ())
8876 {
8877 buffer_pos_reached:
8878 IT_RESET_X_ASCENT_DESCENT (it);
8879 result = MOVE_POS_MATCH_OR_ZV;
8880 break;
8881 }
8882 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8883 {
8884 /* Stop when TO_X specified and reached. This check is
8885 necessary here because of lines consisting of a line end,
8886 only. The line end will not produce any glyphs and we
8887 would never get MOVE_X_REACHED. */
8888 eassert (it->nglyphs == 0);
8889 result = MOVE_X_REACHED;
8890 break;
8891 }
8892
8893 /* Is this a line end? If yes, we're done. */
8894 if (ITERATOR_AT_END_OF_LINE_P (it))
8895 {
8896 /* If we are past TO_CHARPOS, but never saw any character
8897 positions smaller than TO_CHARPOS, return
8898 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8899 did. */
8900 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8901 {
8902 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8903 {
8904 if (closest_pos < ZV)
8905 {
8906 RESTORE_IT (it, &ppos_it, ppos_data);
8907 /* Don't recurse if closest_pos is equal to
8908 to_charpos, since we have just tried that. */
8909 if (closest_pos != to_charpos)
8910 move_it_in_display_line_to (it, closest_pos, -1,
8911 MOVE_TO_POS);
8912 result = MOVE_POS_MATCH_OR_ZV;
8913 }
8914 else
8915 goto buffer_pos_reached;
8916 }
8917 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8918 && IT_CHARPOS (*it) > to_charpos)
8919 goto buffer_pos_reached;
8920 else
8921 result = MOVE_NEWLINE_OR_CR;
8922 }
8923 else
8924 result = MOVE_NEWLINE_OR_CR;
8925 break;
8926 }
8927
8928 prev_method = it->method;
8929 if (it->method == GET_FROM_BUFFER)
8930 prev_pos = IT_CHARPOS (*it);
8931 /* The current display element has been consumed. Advance
8932 to the next. */
8933 set_iterator_to_next (it, true);
8934 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8935 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8936 if (IT_CHARPOS (*it) < to_charpos)
8937 saw_smaller_pos = true;
8938 if (it->bidi_p
8939 && (op & MOVE_TO_POS)
8940 && IT_CHARPOS (*it) >= to_charpos
8941 && IT_CHARPOS (*it) < closest_pos)
8942 closest_pos = IT_CHARPOS (*it);
8943
8944 /* Stop if lines are truncated and IT's current x-position is
8945 past the right edge of the window now. */
8946 if (it->line_wrap == TRUNCATE
8947 && it->current_x >= it->last_visible_x)
8948 {
8949 if (!FRAME_WINDOW_P (it->f)
8950 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8951 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8952 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8953 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8954 {
8955 bool at_eob_p = false;
8956
8957 if ((at_eob_p = !get_next_display_element (it))
8958 || BUFFER_POS_REACHED_P ()
8959 /* If we are past TO_CHARPOS, but never saw any
8960 character positions smaller than TO_CHARPOS,
8961 return MOVE_POS_MATCH_OR_ZV, like the
8962 unidirectional display did. */
8963 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8964 && !saw_smaller_pos
8965 && IT_CHARPOS (*it) > to_charpos))
8966 {
8967 if (it->bidi_p
8968 && !BUFFER_POS_REACHED_P ()
8969 && !at_eob_p && closest_pos < ZV)
8970 {
8971 RESTORE_IT (it, &ppos_it, ppos_data);
8972 if (closest_pos != to_charpos)
8973 move_it_in_display_line_to (it, closest_pos, -1,
8974 MOVE_TO_POS);
8975 }
8976 result = MOVE_POS_MATCH_OR_ZV;
8977 break;
8978 }
8979 if (ITERATOR_AT_END_OF_LINE_P (it))
8980 {
8981 result = MOVE_NEWLINE_OR_CR;
8982 break;
8983 }
8984 }
8985 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8986 && !saw_smaller_pos
8987 && IT_CHARPOS (*it) > to_charpos)
8988 {
8989 if (closest_pos < ZV)
8990 {
8991 RESTORE_IT (it, &ppos_it, ppos_data);
8992 if (closest_pos != to_charpos)
8993 move_it_in_display_line_to (it, closest_pos, -1,
8994 MOVE_TO_POS);
8995 }
8996 result = MOVE_POS_MATCH_OR_ZV;
8997 break;
8998 }
8999 result = MOVE_LINE_TRUNCATED;
9000 break;
9001 }
9002 #undef IT_RESET_X_ASCENT_DESCENT
9003 }
9004
9005 #undef BUFFER_POS_REACHED_P
9006
9007 /* If we scanned beyond to_pos and didn't find a point to wrap at,
9008 restore the saved iterator. */
9009 if (atpos_it.sp >= 0)
9010 RESTORE_IT (it, &atpos_it, atpos_data);
9011 else if (atx_it.sp >= 0)
9012 RESTORE_IT (it, &atx_it, atx_data);
9013
9014 done:
9015
9016 if (atpos_data)
9017 bidi_unshelve_cache (atpos_data, true);
9018 if (atx_data)
9019 bidi_unshelve_cache (atx_data, true);
9020 if (wrap_data)
9021 bidi_unshelve_cache (wrap_data, true);
9022 if (ppos_data)
9023 bidi_unshelve_cache (ppos_data, true);
9024
9025 /* Restore the iterator settings altered at the beginning of this
9026 function. */
9027 it->glyph_row = saved_glyph_row;
9028 return result;
9029 }
9030
9031 /* For external use. */
9032 void
9033 move_it_in_display_line (struct it *it,
9034 ptrdiff_t to_charpos, int to_x,
9035 enum move_operation_enum op)
9036 {
9037 if (it->line_wrap == WORD_WRAP
9038 && (op & MOVE_TO_X))
9039 {
9040 struct it save_it;
9041 void *save_data = NULL;
9042 int skip;
9043
9044 SAVE_IT (save_it, *it, save_data);
9045 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9046 /* When word-wrap is on, TO_X may lie past the end
9047 of a wrapped line. Then it->current is the
9048 character on the next line, so backtrack to the
9049 space before the wrap point. */
9050 if (skip == MOVE_LINE_CONTINUED)
9051 {
9052 int prev_x = max (it->current_x - 1, 0);
9053 RESTORE_IT (it, &save_it, save_data);
9054 move_it_in_display_line_to
9055 (it, -1, prev_x, MOVE_TO_X);
9056 }
9057 else
9058 bidi_unshelve_cache (save_data, true);
9059 }
9060 else
9061 move_it_in_display_line_to (it, to_charpos, to_x, op);
9062 }
9063
9064
9065 /* Move IT forward until it satisfies one or more of the criteria in
9066 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9067
9068 OP is a bit-mask that specifies where to stop, and in particular,
9069 which of those four position arguments makes a difference. See the
9070 description of enum move_operation_enum.
9071
9072 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9073 screen line, this function will set IT to the next position that is
9074 displayed to the right of TO_CHARPOS on the screen.
9075
9076 Return the maximum pixel length of any line scanned but never more
9077 than it.last_visible_x. */
9078
9079 int
9080 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9081 {
9082 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9083 int line_height, line_start_x = 0, reached = 0;
9084 int max_current_x = 0;
9085 void *backup_data = NULL;
9086
9087 for (;;)
9088 {
9089 if (op & MOVE_TO_VPOS)
9090 {
9091 /* If no TO_CHARPOS and no TO_X specified, stop at the
9092 start of the line TO_VPOS. */
9093 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9094 {
9095 if (it->vpos == to_vpos)
9096 {
9097 reached = 1;
9098 break;
9099 }
9100 else
9101 skip = move_it_in_display_line_to (it, -1, -1, 0);
9102 }
9103 else
9104 {
9105 /* TO_VPOS >= 0 means stop at TO_X in the line at
9106 TO_VPOS, or at TO_POS, whichever comes first. */
9107 if (it->vpos == to_vpos)
9108 {
9109 reached = 2;
9110 break;
9111 }
9112
9113 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9114
9115 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9116 {
9117 reached = 3;
9118 break;
9119 }
9120 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9121 {
9122 /* We have reached TO_X but not in the line we want. */
9123 skip = move_it_in_display_line_to (it, to_charpos,
9124 -1, MOVE_TO_POS);
9125 if (skip == MOVE_POS_MATCH_OR_ZV)
9126 {
9127 reached = 4;
9128 break;
9129 }
9130 }
9131 }
9132 }
9133 else if (op & MOVE_TO_Y)
9134 {
9135 struct it it_backup;
9136
9137 if (it->line_wrap == WORD_WRAP)
9138 SAVE_IT (it_backup, *it, backup_data);
9139
9140 /* TO_Y specified means stop at TO_X in the line containing
9141 TO_Y---or at TO_CHARPOS if this is reached first. The
9142 problem is that we can't really tell whether the line
9143 contains TO_Y before we have completely scanned it, and
9144 this may skip past TO_X. What we do is to first scan to
9145 TO_X.
9146
9147 If TO_X is not specified, use a TO_X of zero. The reason
9148 is to make the outcome of this function more predictable.
9149 If we didn't use TO_X == 0, we would stop at the end of
9150 the line which is probably not what a caller would expect
9151 to happen. */
9152 skip = move_it_in_display_line_to
9153 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9154 (MOVE_TO_X | (op & MOVE_TO_POS)));
9155
9156 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9157 if (skip == MOVE_POS_MATCH_OR_ZV)
9158 reached = 5;
9159 else if (skip == MOVE_X_REACHED)
9160 {
9161 /* If TO_X was reached, we want to know whether TO_Y is
9162 in the line. We know this is the case if the already
9163 scanned glyphs make the line tall enough. Otherwise,
9164 we must check by scanning the rest of the line. */
9165 line_height = it->max_ascent + it->max_descent;
9166 if (to_y >= it->current_y
9167 && to_y < it->current_y + line_height)
9168 {
9169 reached = 6;
9170 break;
9171 }
9172 SAVE_IT (it_backup, *it, backup_data);
9173 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9174 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9175 op & MOVE_TO_POS);
9176 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9177 line_height = it->max_ascent + it->max_descent;
9178 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9179
9180 if (to_y >= it->current_y
9181 && to_y < it->current_y + line_height)
9182 {
9183 /* If TO_Y is in this line and TO_X was reached
9184 above, we scanned too far. We have to restore
9185 IT's settings to the ones before skipping. But
9186 keep the more accurate values of max_ascent and
9187 max_descent we've found while skipping the rest
9188 of the line, for the sake of callers, such as
9189 pos_visible_p, that need to know the line
9190 height. */
9191 int max_ascent = it->max_ascent;
9192 int max_descent = it->max_descent;
9193
9194 RESTORE_IT (it, &it_backup, backup_data);
9195 it->max_ascent = max_ascent;
9196 it->max_descent = max_descent;
9197 reached = 6;
9198 }
9199 else
9200 {
9201 skip = skip2;
9202 if (skip == MOVE_POS_MATCH_OR_ZV)
9203 reached = 7;
9204 }
9205 }
9206 else
9207 {
9208 /* Check whether TO_Y is in this line. */
9209 line_height = it->max_ascent + it->max_descent;
9210 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9211
9212 if (to_y >= it->current_y
9213 && to_y < it->current_y + line_height)
9214 {
9215 if (to_y > it->current_y)
9216 max_current_x = max (it->current_x, max_current_x);
9217
9218 /* When word-wrap is on, TO_X may lie past the end
9219 of a wrapped line. Then it->current is the
9220 character on the next line, so backtrack to the
9221 space before the wrap point. */
9222 if (skip == MOVE_LINE_CONTINUED
9223 && it->line_wrap == WORD_WRAP)
9224 {
9225 int prev_x = max (it->current_x - 1, 0);
9226 RESTORE_IT (it, &it_backup, backup_data);
9227 skip = move_it_in_display_line_to
9228 (it, -1, prev_x, MOVE_TO_X);
9229 }
9230
9231 reached = 6;
9232 }
9233 }
9234
9235 if (reached)
9236 {
9237 max_current_x = max (it->current_x, max_current_x);
9238 break;
9239 }
9240 }
9241 else if (BUFFERP (it->object)
9242 && (it->method == GET_FROM_BUFFER
9243 || it->method == GET_FROM_STRETCH)
9244 && IT_CHARPOS (*it) >= to_charpos
9245 /* Under bidi iteration, a call to set_iterator_to_next
9246 can scan far beyond to_charpos if the initial
9247 portion of the next line needs to be reordered. In
9248 that case, give move_it_in_display_line_to another
9249 chance below. */
9250 && !(it->bidi_p
9251 && it->bidi_it.scan_dir == -1))
9252 skip = MOVE_POS_MATCH_OR_ZV;
9253 else
9254 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9255
9256 switch (skip)
9257 {
9258 case MOVE_POS_MATCH_OR_ZV:
9259 max_current_x = max (it->current_x, max_current_x);
9260 reached = 8;
9261 goto out;
9262
9263 case MOVE_NEWLINE_OR_CR:
9264 max_current_x = max (it->current_x, max_current_x);
9265 set_iterator_to_next (it, true);
9266 it->continuation_lines_width = 0;
9267 break;
9268
9269 case MOVE_LINE_TRUNCATED:
9270 max_current_x = it->last_visible_x;
9271 it->continuation_lines_width = 0;
9272 reseat_at_next_visible_line_start (it, false);
9273 if ((op & MOVE_TO_POS) != 0
9274 && IT_CHARPOS (*it) > to_charpos)
9275 {
9276 reached = 9;
9277 goto out;
9278 }
9279 break;
9280
9281 case MOVE_LINE_CONTINUED:
9282 max_current_x = it->last_visible_x;
9283 /* For continued lines ending in a tab, some of the glyphs
9284 associated with the tab are displayed on the current
9285 line. Since it->current_x does not include these glyphs,
9286 we use it->last_visible_x instead. */
9287 if (it->c == '\t')
9288 {
9289 it->continuation_lines_width += it->last_visible_x;
9290 /* When moving by vpos, ensure that the iterator really
9291 advances to the next line (bug#847, bug#969). Fixme:
9292 do we need to do this in other circumstances? */
9293 if (it->current_x != it->last_visible_x
9294 && (op & MOVE_TO_VPOS)
9295 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9296 {
9297 line_start_x = it->current_x + it->pixel_width
9298 - it->last_visible_x;
9299 if (FRAME_WINDOW_P (it->f))
9300 {
9301 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9302 struct font *face_font = face->font;
9303
9304 /* When display_line produces a continued line
9305 that ends in a TAB, it skips a tab stop that
9306 is closer than the font's space character
9307 width (see x_produce_glyphs where it produces
9308 the stretch glyph which represents a TAB).
9309 We need to reproduce the same logic here. */
9310 eassert (face_font);
9311 if (face_font)
9312 {
9313 if (line_start_x < face_font->space_width)
9314 line_start_x
9315 += it->tab_width * face_font->space_width;
9316 }
9317 }
9318 set_iterator_to_next (it, false);
9319 }
9320 }
9321 else
9322 it->continuation_lines_width += it->current_x;
9323 break;
9324
9325 default:
9326 emacs_abort ();
9327 }
9328
9329 /* Reset/increment for the next run. */
9330 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9331 it->current_x = line_start_x;
9332 line_start_x = 0;
9333 it->hpos = 0;
9334 it->current_y += it->max_ascent + it->max_descent;
9335 ++it->vpos;
9336 last_height = it->max_ascent + it->max_descent;
9337 it->max_ascent = it->max_descent = 0;
9338 }
9339
9340 out:
9341
9342 /* On text terminals, we may stop at the end of a line in the middle
9343 of a multi-character glyph. If the glyph itself is continued,
9344 i.e. it is actually displayed on the next line, don't treat this
9345 stopping point as valid; move to the next line instead (unless
9346 that brings us offscreen). */
9347 if (!FRAME_WINDOW_P (it->f)
9348 && op & MOVE_TO_POS
9349 && IT_CHARPOS (*it) == to_charpos
9350 && it->what == IT_CHARACTER
9351 && it->nglyphs > 1
9352 && it->line_wrap == WINDOW_WRAP
9353 && it->current_x == it->last_visible_x - 1
9354 && it->c != '\n'
9355 && it->c != '\t'
9356 && it->w->window_end_valid
9357 && it->vpos < it->w->window_end_vpos)
9358 {
9359 it->continuation_lines_width += it->current_x;
9360 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9361 it->current_y += it->max_ascent + it->max_descent;
9362 ++it->vpos;
9363 last_height = it->max_ascent + it->max_descent;
9364 }
9365
9366 if (backup_data)
9367 bidi_unshelve_cache (backup_data, true);
9368
9369 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9370
9371 return max_current_x;
9372 }
9373
9374
9375 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9376
9377 If DY > 0, move IT backward at least that many pixels. DY = 0
9378 means move IT backward to the preceding line start or BEGV. This
9379 function may move over more than DY pixels if IT->current_y - DY
9380 ends up in the middle of a line; in this case IT->current_y will be
9381 set to the top of the line moved to. */
9382
9383 void
9384 move_it_vertically_backward (struct it *it, int dy)
9385 {
9386 int nlines, h;
9387 struct it it2, it3;
9388 void *it2data = NULL, *it3data = NULL;
9389 ptrdiff_t start_pos;
9390 int nchars_per_row
9391 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9392 ptrdiff_t pos_limit;
9393
9394 move_further_back:
9395 eassert (dy >= 0);
9396
9397 start_pos = IT_CHARPOS (*it);
9398
9399 /* Estimate how many newlines we must move back. */
9400 nlines = max (1, dy / default_line_pixel_height (it->w));
9401 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9402 pos_limit = BEGV;
9403 else
9404 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9405
9406 /* Set the iterator's position that many lines back. But don't go
9407 back more than NLINES full screen lines -- this wins a day with
9408 buffers which have very long lines. */
9409 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9410 back_to_previous_visible_line_start (it);
9411
9412 /* Reseat the iterator here. When moving backward, we don't want
9413 reseat to skip forward over invisible text, set up the iterator
9414 to deliver from overlay strings at the new position etc. So,
9415 use reseat_1 here. */
9416 reseat_1 (it, it->current.pos, true);
9417
9418 /* We are now surely at a line start. */
9419 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9420 reordering is in effect. */
9421 it->continuation_lines_width = 0;
9422
9423 /* Move forward and see what y-distance we moved. First move to the
9424 start of the next line so that we get its height. We need this
9425 height to be able to tell whether we reached the specified
9426 y-distance. */
9427 SAVE_IT (it2, *it, it2data);
9428 it2.max_ascent = it2.max_descent = 0;
9429 do
9430 {
9431 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9432 MOVE_TO_POS | MOVE_TO_VPOS);
9433 }
9434 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9435 /* If we are in a display string which starts at START_POS,
9436 and that display string includes a newline, and we are
9437 right after that newline (i.e. at the beginning of a
9438 display line), exit the loop, because otherwise we will
9439 infloop, since move_it_to will see that it is already at
9440 START_POS and will not move. */
9441 || (it2.method == GET_FROM_STRING
9442 && IT_CHARPOS (it2) == start_pos
9443 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9444 eassert (IT_CHARPOS (*it) >= BEGV);
9445 SAVE_IT (it3, it2, it3data);
9446
9447 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9448 eassert (IT_CHARPOS (*it) >= BEGV);
9449 /* H is the actual vertical distance from the position in *IT
9450 and the starting position. */
9451 h = it2.current_y - it->current_y;
9452 /* NLINES is the distance in number of lines. */
9453 nlines = it2.vpos - it->vpos;
9454
9455 /* Correct IT's y and vpos position
9456 so that they are relative to the starting point. */
9457 it->vpos -= nlines;
9458 it->current_y -= h;
9459
9460 if (dy == 0)
9461 {
9462 /* DY == 0 means move to the start of the screen line. The
9463 value of nlines is > 0 if continuation lines were involved,
9464 or if the original IT position was at start of a line. */
9465 RESTORE_IT (it, it, it2data);
9466 if (nlines > 0)
9467 move_it_by_lines (it, nlines);
9468 /* The above code moves us to some position NLINES down,
9469 usually to its first glyph (leftmost in an L2R line), but
9470 that's not necessarily the start of the line, under bidi
9471 reordering. We want to get to the character position
9472 that is immediately after the newline of the previous
9473 line. */
9474 if (it->bidi_p
9475 && !it->continuation_lines_width
9476 && !STRINGP (it->string)
9477 && IT_CHARPOS (*it) > BEGV
9478 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9479 {
9480 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9481
9482 DEC_BOTH (cp, bp);
9483 cp = find_newline_no_quit (cp, bp, -1, NULL);
9484 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9485 }
9486 bidi_unshelve_cache (it3data, true);
9487 }
9488 else
9489 {
9490 /* The y-position we try to reach, relative to *IT.
9491 Note that H has been subtracted in front of the if-statement. */
9492 int target_y = it->current_y + h - dy;
9493 int y0 = it3.current_y;
9494 int y1;
9495 int line_height;
9496
9497 RESTORE_IT (&it3, &it3, it3data);
9498 y1 = line_bottom_y (&it3);
9499 line_height = y1 - y0;
9500 RESTORE_IT (it, it, it2data);
9501 /* If we did not reach target_y, try to move further backward if
9502 we can. If we moved too far backward, try to move forward. */
9503 if (target_y < it->current_y
9504 /* This is heuristic. In a window that's 3 lines high, with
9505 a line height of 13 pixels each, recentering with point
9506 on the bottom line will try to move -39/2 = 19 pixels
9507 backward. Try to avoid moving into the first line. */
9508 && (it->current_y - target_y
9509 > min (window_box_height (it->w), line_height * 2 / 3))
9510 && IT_CHARPOS (*it) > BEGV)
9511 {
9512 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9513 target_y - it->current_y));
9514 dy = it->current_y - target_y;
9515 goto move_further_back;
9516 }
9517 else if (target_y >= it->current_y + line_height
9518 && IT_CHARPOS (*it) < ZV)
9519 {
9520 /* Should move forward by at least one line, maybe more.
9521
9522 Note: Calling move_it_by_lines can be expensive on
9523 terminal frames, where compute_motion is used (via
9524 vmotion) to do the job, when there are very long lines
9525 and truncate-lines is nil. That's the reason for
9526 treating terminal frames specially here. */
9527
9528 if (!FRAME_WINDOW_P (it->f))
9529 move_it_vertically (it, target_y - it->current_y);
9530 else
9531 {
9532 do
9533 {
9534 move_it_by_lines (it, 1);
9535 }
9536 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9537 }
9538 }
9539 }
9540 }
9541
9542
9543 /* Move IT by a specified amount of pixel lines DY. DY negative means
9544 move backwards. DY = 0 means move to start of screen line. At the
9545 end, IT will be on the start of a screen line. */
9546
9547 void
9548 move_it_vertically (struct it *it, int dy)
9549 {
9550 if (dy <= 0)
9551 move_it_vertically_backward (it, -dy);
9552 else
9553 {
9554 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9555 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9556 MOVE_TO_POS | MOVE_TO_Y);
9557 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9558
9559 /* If buffer ends in ZV without a newline, move to the start of
9560 the line to satisfy the post-condition. */
9561 if (IT_CHARPOS (*it) == ZV
9562 && ZV > BEGV
9563 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9564 move_it_by_lines (it, 0);
9565 }
9566 }
9567
9568
9569 /* Move iterator IT past the end of the text line it is in. */
9570
9571 void
9572 move_it_past_eol (struct it *it)
9573 {
9574 enum move_it_result rc;
9575
9576 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9577 if (rc == MOVE_NEWLINE_OR_CR)
9578 set_iterator_to_next (it, false);
9579 }
9580
9581
9582 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9583 negative means move up. DVPOS == 0 means move to the start of the
9584 screen line.
9585
9586 Optimization idea: If we would know that IT->f doesn't use
9587 a face with proportional font, we could be faster for
9588 truncate-lines nil. */
9589
9590 void
9591 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9592 {
9593
9594 /* The commented-out optimization uses vmotion on terminals. This
9595 gives bad results, because elements like it->what, on which
9596 callers such as pos_visible_p rely, aren't updated. */
9597 /* struct position pos;
9598 if (!FRAME_WINDOW_P (it->f))
9599 {
9600 struct text_pos textpos;
9601
9602 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9603 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9604 reseat (it, textpos, true);
9605 it->vpos += pos.vpos;
9606 it->current_y += pos.vpos;
9607 }
9608 else */
9609
9610 if (dvpos == 0)
9611 {
9612 /* DVPOS == 0 means move to the start of the screen line. */
9613 move_it_vertically_backward (it, 0);
9614 /* Let next call to line_bottom_y calculate real line height. */
9615 last_height = 0;
9616 }
9617 else if (dvpos > 0)
9618 {
9619 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9620 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9621 {
9622 /* Only move to the next buffer position if we ended up in a
9623 string from display property, not in an overlay string
9624 (before-string or after-string). That is because the
9625 latter don't conceal the underlying buffer position, so
9626 we can ask to move the iterator to the exact position we
9627 are interested in. Note that, even if we are already at
9628 IT_CHARPOS (*it), the call below is not a no-op, as it
9629 will detect that we are at the end of the string, pop the
9630 iterator, and compute it->current_x and it->hpos
9631 correctly. */
9632 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9633 -1, -1, -1, MOVE_TO_POS);
9634 }
9635 }
9636 else
9637 {
9638 struct it it2;
9639 void *it2data = NULL;
9640 ptrdiff_t start_charpos, i;
9641 int nchars_per_row
9642 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9643 bool hit_pos_limit = false;
9644 ptrdiff_t pos_limit;
9645
9646 /* Start at the beginning of the screen line containing IT's
9647 position. This may actually move vertically backwards,
9648 in case of overlays, so adjust dvpos accordingly. */
9649 dvpos += it->vpos;
9650 move_it_vertically_backward (it, 0);
9651 dvpos -= it->vpos;
9652
9653 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9654 screen lines, and reseat the iterator there. */
9655 start_charpos = IT_CHARPOS (*it);
9656 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9657 pos_limit = BEGV;
9658 else
9659 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9660
9661 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9662 back_to_previous_visible_line_start (it);
9663 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9664 hit_pos_limit = true;
9665 reseat (it, it->current.pos, true);
9666
9667 /* Move further back if we end up in a string or an image. */
9668 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9669 {
9670 /* First try to move to start of display line. */
9671 dvpos += it->vpos;
9672 move_it_vertically_backward (it, 0);
9673 dvpos -= it->vpos;
9674 if (IT_POS_VALID_AFTER_MOVE_P (it))
9675 break;
9676 /* If start of line is still in string or image,
9677 move further back. */
9678 back_to_previous_visible_line_start (it);
9679 reseat (it, it->current.pos, true);
9680 dvpos--;
9681 }
9682
9683 it->current_x = it->hpos = 0;
9684
9685 /* Above call may have moved too far if continuation lines
9686 are involved. Scan forward and see if it did. */
9687 SAVE_IT (it2, *it, it2data);
9688 it2.vpos = it2.current_y = 0;
9689 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9690 it->vpos -= it2.vpos;
9691 it->current_y -= it2.current_y;
9692 it->current_x = it->hpos = 0;
9693
9694 /* If we moved too far back, move IT some lines forward. */
9695 if (it2.vpos > -dvpos)
9696 {
9697 int delta = it2.vpos + dvpos;
9698
9699 RESTORE_IT (&it2, &it2, it2data);
9700 SAVE_IT (it2, *it, it2data);
9701 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9702 /* Move back again if we got too far ahead. */
9703 if (IT_CHARPOS (*it) >= start_charpos)
9704 RESTORE_IT (it, &it2, it2data);
9705 else
9706 bidi_unshelve_cache (it2data, true);
9707 }
9708 else if (hit_pos_limit && pos_limit > BEGV
9709 && dvpos < 0 && it2.vpos < -dvpos)
9710 {
9711 /* If we hit the limit, but still didn't make it far enough
9712 back, that means there's a display string with a newline
9713 covering a large chunk of text, and that caused
9714 back_to_previous_visible_line_start try to go too far.
9715 Punish those who commit such atrocities by going back
9716 until we've reached DVPOS, after lifting the limit, which
9717 could make it slow for very long lines. "If it hurts,
9718 don't do that!" */
9719 dvpos += it2.vpos;
9720 RESTORE_IT (it, it, it2data);
9721 for (i = -dvpos; i > 0; --i)
9722 {
9723 back_to_previous_visible_line_start (it);
9724 it->vpos--;
9725 }
9726 reseat_1 (it, it->current.pos, true);
9727 }
9728 else
9729 RESTORE_IT (it, it, it2data);
9730 }
9731 }
9732
9733 /* Return true if IT points into the middle of a display vector. */
9734
9735 bool
9736 in_display_vector_p (struct it *it)
9737 {
9738 return (it->method == GET_FROM_DISPLAY_VECTOR
9739 && it->current.dpvec_index > 0
9740 && it->dpvec + it->current.dpvec_index != it->dpend);
9741 }
9742
9743 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9744 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9745 WINDOW must be a live window and defaults to the selected one. The
9746 return value is a cons of the maximum pixel-width of any text line and
9747 the maximum pixel-height of all text lines.
9748
9749 The optional argument FROM, if non-nil, specifies the first text
9750 position and defaults to the minimum accessible position of the buffer.
9751 If FROM is t, use the minimum accessible position that is not a newline
9752 character. TO, if non-nil, specifies the last text position and
9753 defaults to the maximum accessible position of the buffer. If TO is t,
9754 use the maximum accessible position that is not a newline character.
9755
9756 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9757 width that can be returned. X-LIMIT nil or omitted, means to use the
9758 pixel-width of WINDOW's body; use this if you do not intend to change
9759 the width of WINDOW. Use the maximum width WINDOW may assume if you
9760 intend to change WINDOW's width. In any case, text whose x-coordinate
9761 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9762 can take some time, it's always a good idea to make this argument as
9763 small as possible; in particular, if the buffer contains long lines that
9764 shall be truncated anyway.
9765
9766 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9767 height that can be returned. Text lines whose y-coordinate is beyond
9768 Y-LIMIT are ignored. Since calculating the text height of a large
9769 buffer can take some time, it makes sense to specify this argument if
9770 the size of the buffer is unknown.
9771
9772 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9773 include the height of the mode- or header-line of WINDOW in the return
9774 value. If it is either the symbol `mode-line' or `header-line', include
9775 only the height of that line, if present, in the return value. If t,
9776 include the height of both, if present, in the return value. */)
9777 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9778 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9779 {
9780 struct window *w = decode_live_window (window);
9781 Lisp_Object buffer = w->contents;
9782 struct buffer *b;
9783 struct it it;
9784 struct buffer *old_b = NULL;
9785 ptrdiff_t start, end, pos;
9786 struct text_pos startp;
9787 void *itdata = NULL;
9788 int c, max_y = -1, x = 0, y = 0;
9789
9790 CHECK_BUFFER (buffer);
9791 b = XBUFFER (buffer);
9792
9793 if (b != current_buffer)
9794 {
9795 old_b = current_buffer;
9796 set_buffer_internal (b);
9797 }
9798
9799 if (NILP (from))
9800 start = BEGV;
9801 else if (EQ (from, Qt))
9802 {
9803 start = pos = BEGV;
9804 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9805 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9806 start = pos;
9807 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9808 start = pos;
9809 }
9810 else
9811 {
9812 CHECK_NUMBER_COERCE_MARKER (from);
9813 start = min (max (XINT (from), BEGV), ZV);
9814 }
9815
9816 if (NILP (to))
9817 end = ZV;
9818 else if (EQ (to, Qt))
9819 {
9820 end = pos = ZV;
9821 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9822 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9823 end = pos;
9824 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9825 end = pos;
9826 }
9827 else
9828 {
9829 CHECK_NUMBER_COERCE_MARKER (to);
9830 end = max (start, min (XINT (to), ZV));
9831 }
9832
9833 if (!NILP (y_limit))
9834 {
9835 CHECK_NUMBER (y_limit);
9836 max_y = min (XINT (y_limit), INT_MAX);
9837 }
9838
9839 itdata = bidi_shelve_cache ();
9840 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9841 start_display (&it, w, startp);
9842
9843 if (NILP (x_limit))
9844 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9845 else
9846 {
9847 CHECK_NUMBER (x_limit);
9848 it.last_visible_x = min (XINT (x_limit), INFINITY);
9849 /* Actually, we never want move_it_to stop at to_x. But to make
9850 sure that move_it_in_display_line_to always moves far enough,
9851 we set it to INT_MAX and specify MOVE_TO_X. */
9852 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9853 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9854 }
9855
9856 y = it.current_y + it.max_ascent + it.max_descent;
9857
9858 if (!EQ (mode_and_header_line, Qheader_line)
9859 && !EQ (mode_and_header_line, Qt))
9860 /* Do not count the header-line which was counted automatically by
9861 start_display. */
9862 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9863
9864 if (EQ (mode_and_header_line, Qmode_line)
9865 || EQ (mode_and_header_line, Qt))
9866 /* Do count the mode-line which is not included automatically by
9867 start_display. */
9868 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9869
9870 bidi_unshelve_cache (itdata, false);
9871
9872 if (old_b)
9873 set_buffer_internal (old_b);
9874
9875 return Fcons (make_number (x), make_number (y));
9876 }
9877 \f
9878 /***********************************************************************
9879 Messages
9880 ***********************************************************************/
9881
9882 /* Return the number of arguments the format string FORMAT needs. */
9883
9884 static ptrdiff_t
9885 format_nargs (char const *format)
9886 {
9887 ptrdiff_t nargs = 0;
9888 for (char const *p = format; (p = strchr (p, '%')); p++)
9889 if (p[1] == '%')
9890 p++;
9891 else
9892 nargs++;
9893 return nargs;
9894 }
9895
9896 /* Add a message with format string FORMAT and formatted arguments
9897 to *Messages*. */
9898
9899 void
9900 add_to_log (const char *format, ...)
9901 {
9902 va_list ap;
9903 va_start (ap, format);
9904 vadd_to_log (format, ap);
9905 va_end (ap);
9906 }
9907
9908 void
9909 vadd_to_log (char const *format, va_list ap)
9910 {
9911 ptrdiff_t form_nargs = format_nargs (format);
9912 ptrdiff_t nargs = 1 + form_nargs;
9913 Lisp_Object args[10];
9914 eassert (nargs <= ARRAYELTS (args));
9915 AUTO_STRING (args0, format);
9916 args[0] = args0;
9917 for (ptrdiff_t i = 1; i <= nargs; i++)
9918 args[i] = va_arg (ap, Lisp_Object);
9919 Lisp_Object msg = Qnil;
9920 msg = Fformat_message (nargs, args);
9921
9922 ptrdiff_t len = SBYTES (msg) + 1;
9923 USE_SAFE_ALLOCA;
9924 char *buffer = SAFE_ALLOCA (len);
9925 memcpy (buffer, SDATA (msg), len);
9926
9927 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
9928 SAFE_FREE ();
9929 }
9930
9931
9932 /* Output a newline in the *Messages* buffer if "needs" one. */
9933
9934 void
9935 message_log_maybe_newline (void)
9936 {
9937 if (message_log_need_newline)
9938 message_dolog ("", 0, true, false);
9939 }
9940
9941
9942 /* Add a string M of length NBYTES to the message log, optionally
9943 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9944 true, means interpret the contents of M as multibyte. This
9945 function calls low-level routines in order to bypass text property
9946 hooks, etc. which might not be safe to run.
9947
9948 This may GC (insert may run before/after change hooks),
9949 so the buffer M must NOT point to a Lisp string. */
9950
9951 void
9952 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9953 {
9954 const unsigned char *msg = (const unsigned char *) m;
9955
9956 if (!NILP (Vmemory_full))
9957 return;
9958
9959 if (!NILP (Vmessage_log_max))
9960 {
9961 struct buffer *oldbuf;
9962 Lisp_Object oldpoint, oldbegv, oldzv;
9963 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9964 ptrdiff_t point_at_end = 0;
9965 ptrdiff_t zv_at_end = 0;
9966 Lisp_Object old_deactivate_mark;
9967
9968 old_deactivate_mark = Vdeactivate_mark;
9969 oldbuf = current_buffer;
9970
9971 /* Ensure the Messages buffer exists, and switch to it.
9972 If we created it, set the major-mode. */
9973 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
9974 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9975 if (newbuffer
9976 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9977 call0 (intern ("messages-buffer-mode"));
9978
9979 bset_undo_list (current_buffer, Qt);
9980 bset_cache_long_scans (current_buffer, Qnil);
9981
9982 oldpoint = message_dolog_marker1;
9983 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9984 oldbegv = message_dolog_marker2;
9985 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9986 oldzv = message_dolog_marker3;
9987 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9988
9989 if (PT == Z)
9990 point_at_end = 1;
9991 if (ZV == Z)
9992 zv_at_end = 1;
9993
9994 BEGV = BEG;
9995 BEGV_BYTE = BEG_BYTE;
9996 ZV = Z;
9997 ZV_BYTE = Z_BYTE;
9998 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9999
10000 /* Insert the string--maybe converting multibyte to single byte
10001 or vice versa, so that all the text fits the buffer. */
10002 if (multibyte
10003 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10004 {
10005 ptrdiff_t i;
10006 int c, char_bytes;
10007 char work[1];
10008
10009 /* Convert a multibyte string to single-byte
10010 for the *Message* buffer. */
10011 for (i = 0; i < nbytes; i += char_bytes)
10012 {
10013 c = string_char_and_length (msg + i, &char_bytes);
10014 work[0] = CHAR_TO_BYTE8 (c);
10015 insert_1_both (work, 1, 1, true, false, false);
10016 }
10017 }
10018 else if (! multibyte
10019 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10020 {
10021 ptrdiff_t i;
10022 int c, char_bytes;
10023 unsigned char str[MAX_MULTIBYTE_LENGTH];
10024 /* Convert a single-byte string to multibyte
10025 for the *Message* buffer. */
10026 for (i = 0; i < nbytes; i++)
10027 {
10028 c = msg[i];
10029 MAKE_CHAR_MULTIBYTE (c);
10030 char_bytes = CHAR_STRING (c, str);
10031 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10032 }
10033 }
10034 else if (nbytes)
10035 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10036 true, false, false);
10037
10038 if (nlflag)
10039 {
10040 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10041 printmax_t dups;
10042
10043 insert_1_both ("\n", 1, 1, true, false, false);
10044
10045 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10046 this_bol = PT;
10047 this_bol_byte = PT_BYTE;
10048
10049 /* See if this line duplicates the previous one.
10050 If so, combine duplicates. */
10051 if (this_bol > BEG)
10052 {
10053 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10054 prev_bol = PT;
10055 prev_bol_byte = PT_BYTE;
10056
10057 dups = message_log_check_duplicate (prev_bol_byte,
10058 this_bol_byte);
10059 if (dups)
10060 {
10061 del_range_both (prev_bol, prev_bol_byte,
10062 this_bol, this_bol_byte, false);
10063 if (dups > 1)
10064 {
10065 char dupstr[sizeof " [ times]"
10066 + INT_STRLEN_BOUND (printmax_t)];
10067
10068 /* If you change this format, don't forget to also
10069 change message_log_check_duplicate. */
10070 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10071 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10072 insert_1_both (dupstr, duplen, duplen,
10073 true, false, true);
10074 }
10075 }
10076 }
10077
10078 /* If we have more than the desired maximum number of lines
10079 in the *Messages* buffer now, delete the oldest ones.
10080 This is safe because we don't have undo in this buffer. */
10081
10082 if (NATNUMP (Vmessage_log_max))
10083 {
10084 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10085 -XFASTINT (Vmessage_log_max) - 1, false);
10086 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10087 }
10088 }
10089 BEGV = marker_position (oldbegv);
10090 BEGV_BYTE = marker_byte_position (oldbegv);
10091
10092 if (zv_at_end)
10093 {
10094 ZV = Z;
10095 ZV_BYTE = Z_BYTE;
10096 }
10097 else
10098 {
10099 ZV = marker_position (oldzv);
10100 ZV_BYTE = marker_byte_position (oldzv);
10101 }
10102
10103 if (point_at_end)
10104 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10105 else
10106 /* We can't do Fgoto_char (oldpoint) because it will run some
10107 Lisp code. */
10108 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10109 marker_byte_position (oldpoint));
10110
10111 unchain_marker (XMARKER (oldpoint));
10112 unchain_marker (XMARKER (oldbegv));
10113 unchain_marker (XMARKER (oldzv));
10114
10115 /* We called insert_1_both above with its 5th argument (PREPARE)
10116 false, which prevents insert_1_both from calling
10117 prepare_to_modify_buffer, which in turns prevents us from
10118 incrementing windows_or_buffers_changed even if *Messages* is
10119 shown in some window. So we must manually set
10120 windows_or_buffers_changed here to make up for that. */
10121 windows_or_buffers_changed = old_windows_or_buffers_changed;
10122 bset_redisplay (current_buffer);
10123
10124 set_buffer_internal (oldbuf);
10125
10126 message_log_need_newline = !nlflag;
10127 Vdeactivate_mark = old_deactivate_mark;
10128 }
10129 }
10130
10131
10132 /* We are at the end of the buffer after just having inserted a newline.
10133 (Note: We depend on the fact we won't be crossing the gap.)
10134 Check to see if the most recent message looks a lot like the previous one.
10135 Return 0 if different, 1 if the new one should just replace it, or a
10136 value N > 1 if we should also append " [N times]". */
10137
10138 static intmax_t
10139 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10140 {
10141 ptrdiff_t i;
10142 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10143 bool seen_dots = false;
10144 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10145 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10146
10147 for (i = 0; i < len; i++)
10148 {
10149 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10150 seen_dots = true;
10151 if (p1[i] != p2[i])
10152 return seen_dots;
10153 }
10154 p1 += len;
10155 if (*p1 == '\n')
10156 return 2;
10157 if (*p1++ == ' ' && *p1++ == '[')
10158 {
10159 char *pend;
10160 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10161 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10162 return n + 1;
10163 }
10164 return 0;
10165 }
10166 \f
10167
10168 /* Display an echo area message M with a specified length of NBYTES
10169 bytes. The string may include null characters. If M is not a
10170 string, clear out any existing message, and let the mini-buffer
10171 text show through.
10172
10173 This function cancels echoing. */
10174
10175 void
10176 message3 (Lisp_Object m)
10177 {
10178 clear_message (true, true);
10179 cancel_echoing ();
10180
10181 /* First flush out any partial line written with print. */
10182 message_log_maybe_newline ();
10183 if (STRINGP (m))
10184 {
10185 ptrdiff_t nbytes = SBYTES (m);
10186 bool multibyte = STRING_MULTIBYTE (m);
10187 char *buffer;
10188 USE_SAFE_ALLOCA;
10189 SAFE_ALLOCA_STRING (buffer, m);
10190 message_dolog (buffer, nbytes, true, multibyte);
10191 SAFE_FREE ();
10192 }
10193 if (! inhibit_message)
10194 message3_nolog (m);
10195 }
10196
10197 /* Log the message M to stderr. Log an empty line if M is not a string. */
10198
10199 static void
10200 message_to_stderr (Lisp_Object m)
10201 {
10202 if (noninteractive_need_newline)
10203 {
10204 noninteractive_need_newline = false;
10205 fputc ('\n', stderr);
10206 }
10207 if (STRINGP (m))
10208 {
10209 Lisp_Object s = ENCODE_SYSTEM (m);
10210 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10211 }
10212 if (!cursor_in_echo_area)
10213 fputc ('\n', stderr);
10214 fflush (stderr);
10215 }
10216
10217 /* The non-logging version of message3.
10218 This does not cancel echoing, because it is used for echoing.
10219 Perhaps we need to make a separate function for echoing
10220 and make this cancel echoing. */
10221
10222 void
10223 message3_nolog (Lisp_Object m)
10224 {
10225 struct frame *sf = SELECTED_FRAME ();
10226
10227 if (FRAME_INITIAL_P (sf))
10228 message_to_stderr (m);
10229 /* Error messages get reported properly by cmd_error, so this must be just an
10230 informative message; if the frame hasn't really been initialized yet, just
10231 toss it. */
10232 else if (INTERACTIVE && sf->glyphs_initialized_p)
10233 {
10234 /* Get the frame containing the mini-buffer
10235 that the selected frame is using. */
10236 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10237 Lisp_Object frame = XWINDOW (mini_window)->frame;
10238 struct frame *f = XFRAME (frame);
10239
10240 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10241 Fmake_frame_visible (frame);
10242
10243 if (STRINGP (m) && SCHARS (m) > 0)
10244 {
10245 set_message (m);
10246 if (minibuffer_auto_raise)
10247 Fraise_frame (frame);
10248 /* Assume we are not echoing.
10249 (If we are, echo_now will override this.) */
10250 echo_message_buffer = Qnil;
10251 }
10252 else
10253 clear_message (true, true);
10254
10255 do_pending_window_change (false);
10256 echo_area_display (true);
10257 do_pending_window_change (false);
10258 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10259 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10260 }
10261 }
10262
10263
10264 /* Display a null-terminated echo area message M. If M is 0, clear
10265 out any existing message, and let the mini-buffer text show through.
10266
10267 The buffer M must continue to exist until after the echo area gets
10268 cleared or some other message gets displayed there. Do not pass
10269 text that is stored in a Lisp string. Do not pass text in a buffer
10270 that was alloca'd. */
10271
10272 void
10273 message1 (const char *m)
10274 {
10275 message3 (m ? build_unibyte_string (m) : Qnil);
10276 }
10277
10278
10279 /* The non-logging counterpart of message1. */
10280
10281 void
10282 message1_nolog (const char *m)
10283 {
10284 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10285 }
10286
10287 /* Display a message M which contains a single %s
10288 which gets replaced with STRING. */
10289
10290 void
10291 message_with_string (const char *m, Lisp_Object string, bool log)
10292 {
10293 CHECK_STRING (string);
10294
10295 bool need_message;
10296 if (noninteractive)
10297 need_message = !!m;
10298 else if (!INTERACTIVE)
10299 need_message = false;
10300 else
10301 {
10302 /* The frame whose minibuffer we're going to display the message on.
10303 It may be larger than the selected frame, so we need
10304 to use its buffer, not the selected frame's buffer. */
10305 Lisp_Object mini_window;
10306 struct frame *f, *sf = SELECTED_FRAME ();
10307
10308 /* Get the frame containing the minibuffer
10309 that the selected frame is using. */
10310 mini_window = FRAME_MINIBUF_WINDOW (sf);
10311 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10312
10313 /* Error messages get reported properly by cmd_error, so this must be
10314 just an informative message; if the frame hasn't really been
10315 initialized yet, just toss it. */
10316 need_message = f->glyphs_initialized_p;
10317 }
10318
10319 if (need_message)
10320 {
10321 AUTO_STRING (fmt, m);
10322 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10323
10324 if (noninteractive)
10325 message_to_stderr (msg);
10326 else
10327 {
10328 if (log)
10329 message3 (msg);
10330 else
10331 message3_nolog (msg);
10332
10333 /* Print should start at the beginning of the message
10334 buffer next time. */
10335 message_buf_print = false;
10336 }
10337 }
10338 }
10339
10340
10341 /* Dump an informative message to the minibuf. If M is 0, clear out
10342 any existing message, and let the mini-buffer text show through.
10343
10344 The message must be safe ASCII and the format must not contain ` or
10345 '. If your message and format do not fit into this category,
10346 convert your arguments to Lisp objects and use Fmessage instead. */
10347
10348 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10349 vmessage (const char *m, va_list ap)
10350 {
10351 if (noninteractive)
10352 {
10353 if (m)
10354 {
10355 if (noninteractive_need_newline)
10356 putc ('\n', stderr);
10357 noninteractive_need_newline = false;
10358 vfprintf (stderr, m, ap);
10359 if (!cursor_in_echo_area)
10360 fprintf (stderr, "\n");
10361 fflush (stderr);
10362 }
10363 }
10364 else if (INTERACTIVE)
10365 {
10366 /* The frame whose mini-buffer we're going to display the message
10367 on. It may be larger than the selected frame, so we need to
10368 use its buffer, not the selected frame's buffer. */
10369 Lisp_Object mini_window;
10370 struct frame *f, *sf = SELECTED_FRAME ();
10371
10372 /* Get the frame containing the mini-buffer
10373 that the selected frame is using. */
10374 mini_window = FRAME_MINIBUF_WINDOW (sf);
10375 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10376
10377 /* Error messages get reported properly by cmd_error, so this must be
10378 just an informative message; if the frame hasn't really been
10379 initialized yet, just toss it. */
10380 if (f->glyphs_initialized_p)
10381 {
10382 if (m)
10383 {
10384 ptrdiff_t len;
10385 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10386 USE_SAFE_ALLOCA;
10387 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10388
10389 len = doprnt (message_buf, maxsize, m, 0, ap);
10390
10391 message3 (make_string (message_buf, len));
10392 SAFE_FREE ();
10393 }
10394 else
10395 message1 (0);
10396
10397 /* Print should start at the beginning of the message
10398 buffer next time. */
10399 message_buf_print = false;
10400 }
10401 }
10402 }
10403
10404 void
10405 message (const char *m, ...)
10406 {
10407 va_list ap;
10408 va_start (ap, m);
10409 vmessage (m, ap);
10410 va_end (ap);
10411 }
10412
10413
10414 /* Display the current message in the current mini-buffer. This is
10415 only called from error handlers in process.c, and is not time
10416 critical. */
10417
10418 void
10419 update_echo_area (void)
10420 {
10421 if (!NILP (echo_area_buffer[0]))
10422 {
10423 Lisp_Object string;
10424 string = Fcurrent_message ();
10425 message3 (string);
10426 }
10427 }
10428
10429
10430 /* Make sure echo area buffers in `echo_buffers' are live.
10431 If they aren't, make new ones. */
10432
10433 static void
10434 ensure_echo_area_buffers (void)
10435 {
10436 int i;
10437
10438 for (i = 0; i < 2; ++i)
10439 if (!BUFFERP (echo_buffer[i])
10440 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10441 {
10442 char name[30];
10443 Lisp_Object old_buffer;
10444 int j;
10445
10446 old_buffer = echo_buffer[i];
10447 echo_buffer[i] = Fget_buffer_create
10448 (make_formatted_string (name, " *Echo Area %d*", i));
10449 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10450 /* to force word wrap in echo area -
10451 it was decided to postpone this*/
10452 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10453
10454 for (j = 0; j < 2; ++j)
10455 if (EQ (old_buffer, echo_area_buffer[j]))
10456 echo_area_buffer[j] = echo_buffer[i];
10457 }
10458 }
10459
10460
10461 /* Call FN with args A1..A2 with either the current or last displayed
10462 echo_area_buffer as current buffer.
10463
10464 WHICH zero means use the current message buffer
10465 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10466 from echo_buffer[] and clear it.
10467
10468 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10469 suitable buffer from echo_buffer[] and clear it.
10470
10471 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10472 that the current message becomes the last displayed one, make
10473 choose a suitable buffer for echo_area_buffer[0], and clear it.
10474
10475 Value is what FN returns. */
10476
10477 static bool
10478 with_echo_area_buffer (struct window *w, int which,
10479 bool (*fn) (ptrdiff_t, Lisp_Object),
10480 ptrdiff_t a1, Lisp_Object a2)
10481 {
10482 Lisp_Object buffer;
10483 bool this_one, the_other, clear_buffer_p, rc;
10484 ptrdiff_t count = SPECPDL_INDEX ();
10485
10486 /* If buffers aren't live, make new ones. */
10487 ensure_echo_area_buffers ();
10488
10489 clear_buffer_p = false;
10490
10491 if (which == 0)
10492 this_one = false, the_other = true;
10493 else if (which > 0)
10494 this_one = true, the_other = false;
10495 else
10496 {
10497 this_one = false, the_other = true;
10498 clear_buffer_p = true;
10499
10500 /* We need a fresh one in case the current echo buffer equals
10501 the one containing the last displayed echo area message. */
10502 if (!NILP (echo_area_buffer[this_one])
10503 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10504 echo_area_buffer[this_one] = Qnil;
10505 }
10506
10507 /* Choose a suitable buffer from echo_buffer[] is we don't
10508 have one. */
10509 if (NILP (echo_area_buffer[this_one]))
10510 {
10511 echo_area_buffer[this_one]
10512 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10513 ? echo_buffer[the_other]
10514 : echo_buffer[this_one]);
10515 clear_buffer_p = true;
10516 }
10517
10518 buffer = echo_area_buffer[this_one];
10519
10520 /* Don't get confused by reusing the buffer used for echoing
10521 for a different purpose. */
10522 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10523 cancel_echoing ();
10524
10525 record_unwind_protect (unwind_with_echo_area_buffer,
10526 with_echo_area_buffer_unwind_data (w));
10527
10528 /* Make the echo area buffer current. Note that for display
10529 purposes, it is not necessary that the displayed window's buffer
10530 == current_buffer, except for text property lookup. So, let's
10531 only set that buffer temporarily here without doing a full
10532 Fset_window_buffer. We must also change w->pointm, though,
10533 because otherwise an assertions in unshow_buffer fails, and Emacs
10534 aborts. */
10535 set_buffer_internal_1 (XBUFFER (buffer));
10536 if (w)
10537 {
10538 wset_buffer (w, buffer);
10539 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10540 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10541 }
10542
10543 bset_undo_list (current_buffer, Qt);
10544 bset_read_only (current_buffer, Qnil);
10545 specbind (Qinhibit_read_only, Qt);
10546 specbind (Qinhibit_modification_hooks, Qt);
10547
10548 if (clear_buffer_p && Z > BEG)
10549 del_range (BEG, Z);
10550
10551 eassert (BEGV >= BEG);
10552 eassert (ZV <= Z && ZV >= BEGV);
10553
10554 rc = fn (a1, a2);
10555
10556 eassert (BEGV >= BEG);
10557 eassert (ZV <= Z && ZV >= BEGV);
10558
10559 unbind_to (count, Qnil);
10560 return rc;
10561 }
10562
10563
10564 /* Save state that should be preserved around the call to the function
10565 FN called in with_echo_area_buffer. */
10566
10567 static Lisp_Object
10568 with_echo_area_buffer_unwind_data (struct window *w)
10569 {
10570 int i = 0;
10571 Lisp_Object vector, tmp;
10572
10573 /* Reduce consing by keeping one vector in
10574 Vwith_echo_area_save_vector. */
10575 vector = Vwith_echo_area_save_vector;
10576 Vwith_echo_area_save_vector = Qnil;
10577
10578 if (NILP (vector))
10579 vector = Fmake_vector (make_number (11), Qnil);
10580
10581 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10582 ASET (vector, i, Vdeactivate_mark); ++i;
10583 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10584
10585 if (w)
10586 {
10587 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10588 ASET (vector, i, w->contents); ++i;
10589 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10590 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10591 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10592 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10593 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10594 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10595 }
10596 else
10597 {
10598 int end = i + 8;
10599 for (; i < end; ++i)
10600 ASET (vector, i, Qnil);
10601 }
10602
10603 eassert (i == ASIZE (vector));
10604 return vector;
10605 }
10606
10607
10608 /* Restore global state from VECTOR which was created by
10609 with_echo_area_buffer_unwind_data. */
10610
10611 static void
10612 unwind_with_echo_area_buffer (Lisp_Object vector)
10613 {
10614 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10615 Vdeactivate_mark = AREF (vector, 1);
10616 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10617
10618 if (WINDOWP (AREF (vector, 3)))
10619 {
10620 struct window *w;
10621 Lisp_Object buffer;
10622
10623 w = XWINDOW (AREF (vector, 3));
10624 buffer = AREF (vector, 4);
10625
10626 wset_buffer (w, buffer);
10627 set_marker_both (w->pointm, buffer,
10628 XFASTINT (AREF (vector, 5)),
10629 XFASTINT (AREF (vector, 6)));
10630 set_marker_both (w->old_pointm, buffer,
10631 XFASTINT (AREF (vector, 7)),
10632 XFASTINT (AREF (vector, 8)));
10633 set_marker_both (w->start, buffer,
10634 XFASTINT (AREF (vector, 9)),
10635 XFASTINT (AREF (vector, 10)));
10636 }
10637
10638 Vwith_echo_area_save_vector = vector;
10639 }
10640
10641
10642 /* Set up the echo area for use by print functions. MULTIBYTE_P
10643 means we will print multibyte. */
10644
10645 void
10646 setup_echo_area_for_printing (bool multibyte_p)
10647 {
10648 /* If we can't find an echo area any more, exit. */
10649 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10650 Fkill_emacs (Qnil);
10651
10652 ensure_echo_area_buffers ();
10653
10654 if (!message_buf_print)
10655 {
10656 /* A message has been output since the last time we printed.
10657 Choose a fresh echo area buffer. */
10658 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10659 echo_area_buffer[0] = echo_buffer[1];
10660 else
10661 echo_area_buffer[0] = echo_buffer[0];
10662
10663 /* Switch to that buffer and clear it. */
10664 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10665 bset_truncate_lines (current_buffer, Qnil);
10666
10667 if (Z > BEG)
10668 {
10669 ptrdiff_t count = SPECPDL_INDEX ();
10670 specbind (Qinhibit_read_only, Qt);
10671 /* Note that undo recording is always disabled. */
10672 del_range (BEG, Z);
10673 unbind_to (count, Qnil);
10674 }
10675 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10676
10677 /* Set up the buffer for the multibyteness we need. */
10678 if (multibyte_p
10679 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10680 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10681
10682 /* Raise the frame containing the echo area. */
10683 if (minibuffer_auto_raise)
10684 {
10685 struct frame *sf = SELECTED_FRAME ();
10686 Lisp_Object mini_window;
10687 mini_window = FRAME_MINIBUF_WINDOW (sf);
10688 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10689 }
10690
10691 message_log_maybe_newline ();
10692 message_buf_print = true;
10693 }
10694 else
10695 {
10696 if (NILP (echo_area_buffer[0]))
10697 {
10698 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10699 echo_area_buffer[0] = echo_buffer[1];
10700 else
10701 echo_area_buffer[0] = echo_buffer[0];
10702 }
10703
10704 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10705 {
10706 /* Someone switched buffers between print requests. */
10707 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10708 bset_truncate_lines (current_buffer, Qnil);
10709 }
10710 }
10711 }
10712
10713
10714 /* Display an echo area message in window W. Value is true if W's
10715 height is changed. If display_last_displayed_message_p,
10716 display the message that was last displayed, otherwise
10717 display the current message. */
10718
10719 static bool
10720 display_echo_area (struct window *w)
10721 {
10722 bool no_message_p, window_height_changed_p;
10723
10724 /* Temporarily disable garbage collections while displaying the echo
10725 area. This is done because a GC can print a message itself.
10726 That message would modify the echo area buffer's contents while a
10727 redisplay of the buffer is going on, and seriously confuse
10728 redisplay. */
10729 ptrdiff_t count = inhibit_garbage_collection ();
10730
10731 /* If there is no message, we must call display_echo_area_1
10732 nevertheless because it resizes the window. But we will have to
10733 reset the echo_area_buffer in question to nil at the end because
10734 with_echo_area_buffer will sets it to an empty buffer. */
10735 bool i = display_last_displayed_message_p;
10736 no_message_p = NILP (echo_area_buffer[i]);
10737
10738 window_height_changed_p
10739 = with_echo_area_buffer (w, display_last_displayed_message_p,
10740 display_echo_area_1,
10741 (intptr_t) w, Qnil);
10742
10743 if (no_message_p)
10744 echo_area_buffer[i] = Qnil;
10745
10746 unbind_to (count, Qnil);
10747 return window_height_changed_p;
10748 }
10749
10750
10751 /* Helper for display_echo_area. Display the current buffer which
10752 contains the current echo area message in window W, a mini-window,
10753 a pointer to which is passed in A1. A2..A4 are currently not used.
10754 Change the height of W so that all of the message is displayed.
10755 Value is true if height of W was changed. */
10756
10757 static bool
10758 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10759 {
10760 intptr_t i1 = a1;
10761 struct window *w = (struct window *) i1;
10762 Lisp_Object window;
10763 struct text_pos start;
10764
10765 /* We are about to enter redisplay without going through
10766 redisplay_internal, so we need to forget these faces by hand
10767 here. */
10768 forget_escape_and_glyphless_faces ();
10769
10770 /* Do this before displaying, so that we have a large enough glyph
10771 matrix for the display. If we can't get enough space for the
10772 whole text, display the last N lines. That works by setting w->start. */
10773 bool window_height_changed_p = resize_mini_window (w, false);
10774
10775 /* Use the starting position chosen by resize_mini_window. */
10776 SET_TEXT_POS_FROM_MARKER (start, w->start);
10777
10778 /* Display. */
10779 clear_glyph_matrix (w->desired_matrix);
10780 XSETWINDOW (window, w);
10781 try_window (window, start, 0);
10782
10783 return window_height_changed_p;
10784 }
10785
10786
10787 /* Resize the echo area window to exactly the size needed for the
10788 currently displayed message, if there is one. If a mini-buffer
10789 is active, don't shrink it. */
10790
10791 void
10792 resize_echo_area_exactly (void)
10793 {
10794 if (BUFFERP (echo_area_buffer[0])
10795 && WINDOWP (echo_area_window))
10796 {
10797 struct window *w = XWINDOW (echo_area_window);
10798 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10799 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10800 (intptr_t) w, resize_exactly);
10801 if (resized_p)
10802 {
10803 windows_or_buffers_changed = 42;
10804 update_mode_lines = 30;
10805 redisplay_internal ();
10806 }
10807 }
10808 }
10809
10810
10811 /* Callback function for with_echo_area_buffer, when used from
10812 resize_echo_area_exactly. A1 contains a pointer to the window to
10813 resize, EXACTLY non-nil means resize the mini-window exactly to the
10814 size of the text displayed. A3 and A4 are not used. Value is what
10815 resize_mini_window returns. */
10816
10817 static bool
10818 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10819 {
10820 intptr_t i1 = a1;
10821 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10822 }
10823
10824
10825 /* Resize mini-window W to fit the size of its contents. EXACT_P
10826 means size the window exactly to the size needed. Otherwise, it's
10827 only enlarged until W's buffer is empty.
10828
10829 Set W->start to the right place to begin display. If the whole
10830 contents fit, start at the beginning. Otherwise, start so as
10831 to make the end of the contents appear. This is particularly
10832 important for y-or-n-p, but seems desirable generally.
10833
10834 Value is true if the window height has been changed. */
10835
10836 bool
10837 resize_mini_window (struct window *w, bool exact_p)
10838 {
10839 struct frame *f = XFRAME (w->frame);
10840 bool window_height_changed_p = false;
10841
10842 eassert (MINI_WINDOW_P (w));
10843
10844 /* By default, start display at the beginning. */
10845 set_marker_both (w->start, w->contents,
10846 BUF_BEGV (XBUFFER (w->contents)),
10847 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10848
10849 /* Don't resize windows while redisplaying a window; it would
10850 confuse redisplay functions when the size of the window they are
10851 displaying changes from under them. Such a resizing can happen,
10852 for instance, when which-func prints a long message while
10853 we are running fontification-functions. We're running these
10854 functions with safe_call which binds inhibit-redisplay to t. */
10855 if (!NILP (Vinhibit_redisplay))
10856 return false;
10857
10858 /* Nil means don't try to resize. */
10859 if (NILP (Vresize_mini_windows)
10860 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10861 return false;
10862
10863 if (!FRAME_MINIBUF_ONLY_P (f))
10864 {
10865 struct it it;
10866 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10867 + WINDOW_PIXEL_HEIGHT (w));
10868 int unit = FRAME_LINE_HEIGHT (f);
10869 int height, max_height;
10870 struct text_pos start;
10871 struct buffer *old_current_buffer = NULL;
10872
10873 if (current_buffer != XBUFFER (w->contents))
10874 {
10875 old_current_buffer = current_buffer;
10876 set_buffer_internal (XBUFFER (w->contents));
10877 }
10878
10879 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10880
10881 /* Compute the max. number of lines specified by the user. */
10882 if (FLOATP (Vmax_mini_window_height))
10883 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10884 else if (INTEGERP (Vmax_mini_window_height))
10885 max_height = XINT (Vmax_mini_window_height) * unit;
10886 else
10887 max_height = total_height / 4;
10888
10889 /* Correct that max. height if it's bogus. */
10890 max_height = clip_to_bounds (unit, max_height, total_height);
10891
10892 /* Find out the height of the text in the window. */
10893 if (it.line_wrap == TRUNCATE)
10894 height = unit;
10895 else
10896 {
10897 last_height = 0;
10898 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10899 if (it.max_ascent == 0 && it.max_descent == 0)
10900 height = it.current_y + last_height;
10901 else
10902 height = it.current_y + it.max_ascent + it.max_descent;
10903 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10904 }
10905
10906 /* Compute a suitable window start. */
10907 if (height > max_height)
10908 {
10909 height = (max_height / unit) * unit;
10910 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10911 move_it_vertically_backward (&it, height - unit);
10912 start = it.current.pos;
10913 }
10914 else
10915 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10916 SET_MARKER_FROM_TEXT_POS (w->start, start);
10917
10918 if (EQ (Vresize_mini_windows, Qgrow_only))
10919 {
10920 /* Let it grow only, until we display an empty message, in which
10921 case the window shrinks again. */
10922 if (height > WINDOW_PIXEL_HEIGHT (w))
10923 {
10924 int old_height = WINDOW_PIXEL_HEIGHT (w);
10925
10926 FRAME_WINDOWS_FROZEN (f) = true;
10927 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10928 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10929 }
10930 else if (height < WINDOW_PIXEL_HEIGHT (w)
10931 && (exact_p || BEGV == ZV))
10932 {
10933 int old_height = WINDOW_PIXEL_HEIGHT (w);
10934
10935 FRAME_WINDOWS_FROZEN (f) = false;
10936 shrink_mini_window (w, true);
10937 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10938 }
10939 }
10940 else
10941 {
10942 /* Always resize to exact size needed. */
10943 if (height > WINDOW_PIXEL_HEIGHT (w))
10944 {
10945 int old_height = WINDOW_PIXEL_HEIGHT (w);
10946
10947 FRAME_WINDOWS_FROZEN (f) = true;
10948 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10949 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10950 }
10951 else if (height < WINDOW_PIXEL_HEIGHT (w))
10952 {
10953 int old_height = WINDOW_PIXEL_HEIGHT (w);
10954
10955 FRAME_WINDOWS_FROZEN (f) = false;
10956 shrink_mini_window (w, true);
10957
10958 if (height)
10959 {
10960 FRAME_WINDOWS_FROZEN (f) = true;
10961 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10962 }
10963
10964 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10965 }
10966 }
10967
10968 if (old_current_buffer)
10969 set_buffer_internal (old_current_buffer);
10970 }
10971
10972 return window_height_changed_p;
10973 }
10974
10975
10976 /* Value is the current message, a string, or nil if there is no
10977 current message. */
10978
10979 Lisp_Object
10980 current_message (void)
10981 {
10982 Lisp_Object msg;
10983
10984 if (!BUFFERP (echo_area_buffer[0]))
10985 msg = Qnil;
10986 else
10987 {
10988 with_echo_area_buffer (0, 0, current_message_1,
10989 (intptr_t) &msg, Qnil);
10990 if (NILP (msg))
10991 echo_area_buffer[0] = Qnil;
10992 }
10993
10994 return msg;
10995 }
10996
10997
10998 static bool
10999 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11000 {
11001 intptr_t i1 = a1;
11002 Lisp_Object *msg = (Lisp_Object *) i1;
11003
11004 if (Z > BEG)
11005 *msg = make_buffer_string (BEG, Z, true);
11006 else
11007 *msg = Qnil;
11008 return false;
11009 }
11010
11011
11012 /* Push the current message on Vmessage_stack for later restoration
11013 by restore_message. Value is true if the current message isn't
11014 empty. This is a relatively infrequent operation, so it's not
11015 worth optimizing. */
11016
11017 bool
11018 push_message (void)
11019 {
11020 Lisp_Object msg = current_message ();
11021 Vmessage_stack = Fcons (msg, Vmessage_stack);
11022 return STRINGP (msg);
11023 }
11024
11025
11026 /* Restore message display from the top of Vmessage_stack. */
11027
11028 void
11029 restore_message (void)
11030 {
11031 eassert (CONSP (Vmessage_stack));
11032 message3_nolog (XCAR (Vmessage_stack));
11033 }
11034
11035
11036 /* Handler for unwind-protect calling pop_message. */
11037
11038 void
11039 pop_message_unwind (void)
11040 {
11041 /* Pop the top-most entry off Vmessage_stack. */
11042 eassert (CONSP (Vmessage_stack));
11043 Vmessage_stack = XCDR (Vmessage_stack);
11044 }
11045
11046
11047 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11048 exits. If the stack is not empty, we have a missing pop_message
11049 somewhere. */
11050
11051 void
11052 check_message_stack (void)
11053 {
11054 if (!NILP (Vmessage_stack))
11055 emacs_abort ();
11056 }
11057
11058
11059 /* Truncate to NCHARS what will be displayed in the echo area the next
11060 time we display it---but don't redisplay it now. */
11061
11062 void
11063 truncate_echo_area (ptrdiff_t nchars)
11064 {
11065 if (nchars == 0)
11066 echo_area_buffer[0] = Qnil;
11067 else if (!noninteractive
11068 && INTERACTIVE
11069 && !NILP (echo_area_buffer[0]))
11070 {
11071 struct frame *sf = SELECTED_FRAME ();
11072 /* Error messages get reported properly by cmd_error, so this must be
11073 just an informative message; if the frame hasn't really been
11074 initialized yet, just toss it. */
11075 if (sf->glyphs_initialized_p)
11076 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11077 }
11078 }
11079
11080
11081 /* Helper function for truncate_echo_area. Truncate the current
11082 message to at most NCHARS characters. */
11083
11084 static bool
11085 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11086 {
11087 if (BEG + nchars < Z)
11088 del_range (BEG + nchars, Z);
11089 if (Z == BEG)
11090 echo_area_buffer[0] = Qnil;
11091 return false;
11092 }
11093
11094 /* Set the current message to STRING. */
11095
11096 static void
11097 set_message (Lisp_Object string)
11098 {
11099 eassert (STRINGP (string));
11100
11101 message_enable_multibyte = STRING_MULTIBYTE (string);
11102
11103 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11104 message_buf_print = false;
11105 help_echo_showing_p = false;
11106
11107 if (STRINGP (Vdebug_on_message)
11108 && STRINGP (string)
11109 && fast_string_match (Vdebug_on_message, string) >= 0)
11110 call_debugger (list2 (Qerror, string));
11111 }
11112
11113
11114 /* Helper function for set_message. First argument is ignored and second
11115 argument has the same meaning as for set_message.
11116 This function is called with the echo area buffer being current. */
11117
11118 static bool
11119 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11120 {
11121 eassert (STRINGP (string));
11122
11123 /* Change multibyteness of the echo buffer appropriately. */
11124 if (message_enable_multibyte
11125 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11126 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11127
11128 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11129 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11130 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11131
11132 /* Insert new message at BEG. */
11133 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11134
11135 /* This function takes care of single/multibyte conversion.
11136 We just have to ensure that the echo area buffer has the right
11137 setting of enable_multibyte_characters. */
11138 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11139
11140 return false;
11141 }
11142
11143
11144 /* Clear messages. CURRENT_P means clear the current message.
11145 LAST_DISPLAYED_P means clear the message last displayed. */
11146
11147 void
11148 clear_message (bool current_p, bool last_displayed_p)
11149 {
11150 if (current_p)
11151 {
11152 echo_area_buffer[0] = Qnil;
11153 message_cleared_p = true;
11154 }
11155
11156 if (last_displayed_p)
11157 echo_area_buffer[1] = Qnil;
11158
11159 message_buf_print = false;
11160 }
11161
11162 /* Clear garbaged frames.
11163
11164 This function is used where the old redisplay called
11165 redraw_garbaged_frames which in turn called redraw_frame which in
11166 turn called clear_frame. The call to clear_frame was a source of
11167 flickering. I believe a clear_frame is not necessary. It should
11168 suffice in the new redisplay to invalidate all current matrices,
11169 and ensure a complete redisplay of all windows. */
11170
11171 static void
11172 clear_garbaged_frames (void)
11173 {
11174 if (frame_garbaged)
11175 {
11176 Lisp_Object tail, frame;
11177
11178 FOR_EACH_FRAME (tail, frame)
11179 {
11180 struct frame *f = XFRAME (frame);
11181
11182 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11183 {
11184 if (f->resized_p)
11185 redraw_frame (f);
11186 else
11187 clear_current_matrices (f);
11188 fset_redisplay (f);
11189 f->garbaged = false;
11190 f->resized_p = false;
11191 }
11192 }
11193
11194 frame_garbaged = false;
11195 }
11196 }
11197
11198
11199 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11200 selected_frame. */
11201
11202 static void
11203 echo_area_display (bool update_frame_p)
11204 {
11205 Lisp_Object mini_window;
11206 struct window *w;
11207 struct frame *f;
11208 bool window_height_changed_p = false;
11209 struct frame *sf = SELECTED_FRAME ();
11210
11211 mini_window = FRAME_MINIBUF_WINDOW (sf);
11212 w = XWINDOW (mini_window);
11213 f = XFRAME (WINDOW_FRAME (w));
11214
11215 /* Don't display if frame is invisible or not yet initialized. */
11216 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11217 return;
11218
11219 #ifdef HAVE_WINDOW_SYSTEM
11220 /* When Emacs starts, selected_frame may be the initial terminal
11221 frame. If we let this through, a message would be displayed on
11222 the terminal. */
11223 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11224 return;
11225 #endif /* HAVE_WINDOW_SYSTEM */
11226
11227 /* Redraw garbaged frames. */
11228 clear_garbaged_frames ();
11229
11230 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11231 {
11232 echo_area_window = mini_window;
11233 window_height_changed_p = display_echo_area (w);
11234 w->must_be_updated_p = true;
11235
11236 /* Update the display, unless called from redisplay_internal.
11237 Also don't update the screen during redisplay itself. The
11238 update will happen at the end of redisplay, and an update
11239 here could cause confusion. */
11240 if (update_frame_p && !redisplaying_p)
11241 {
11242 int n = 0;
11243
11244 /* If the display update has been interrupted by pending
11245 input, update mode lines in the frame. Due to the
11246 pending input, it might have been that redisplay hasn't
11247 been called, so that mode lines above the echo area are
11248 garbaged. This looks odd, so we prevent it here. */
11249 if (!display_completed)
11250 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11251
11252 if (window_height_changed_p
11253 /* Don't do this if Emacs is shutting down. Redisplay
11254 needs to run hooks. */
11255 && !NILP (Vrun_hooks))
11256 {
11257 /* Must update other windows. Likewise as in other
11258 cases, don't let this update be interrupted by
11259 pending input. */
11260 ptrdiff_t count = SPECPDL_INDEX ();
11261 specbind (Qredisplay_dont_pause, Qt);
11262 fset_redisplay (f);
11263 redisplay_internal ();
11264 unbind_to (count, Qnil);
11265 }
11266 else if (FRAME_WINDOW_P (f) && n == 0)
11267 {
11268 /* Window configuration is the same as before.
11269 Can do with a display update of the echo area,
11270 unless we displayed some mode lines. */
11271 update_single_window (w);
11272 flush_frame (f);
11273 }
11274 else
11275 update_frame (f, true, true);
11276
11277 /* If cursor is in the echo area, make sure that the next
11278 redisplay displays the minibuffer, so that the cursor will
11279 be replaced with what the minibuffer wants. */
11280 if (cursor_in_echo_area)
11281 wset_redisplay (XWINDOW (mini_window));
11282 }
11283 }
11284 else if (!EQ (mini_window, selected_window))
11285 wset_redisplay (XWINDOW (mini_window));
11286
11287 /* Last displayed message is now the current message. */
11288 echo_area_buffer[1] = echo_area_buffer[0];
11289 /* Inform read_char that we're not echoing. */
11290 echo_message_buffer = Qnil;
11291
11292 /* Prevent redisplay optimization in redisplay_internal by resetting
11293 this_line_start_pos. This is done because the mini-buffer now
11294 displays the message instead of its buffer text. */
11295 if (EQ (mini_window, selected_window))
11296 CHARPOS (this_line_start_pos) = 0;
11297
11298 if (window_height_changed_p)
11299 {
11300 fset_redisplay (f);
11301
11302 /* If window configuration was changed, frames may have been
11303 marked garbaged. Clear them or we will experience
11304 surprises wrt scrolling.
11305 FIXME: How/why/when? */
11306 clear_garbaged_frames ();
11307 }
11308 }
11309
11310 /* True if W's buffer was changed but not saved. */
11311
11312 static bool
11313 window_buffer_changed (struct window *w)
11314 {
11315 struct buffer *b = XBUFFER (w->contents);
11316
11317 eassert (BUFFER_LIVE_P (b));
11318
11319 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11320 }
11321
11322 /* True if W has %c in its mode line and mode line should be updated. */
11323
11324 static bool
11325 mode_line_update_needed (struct window *w)
11326 {
11327 return (w->column_number_displayed != -1
11328 && !(PT == w->last_point && !window_outdated (w))
11329 && (w->column_number_displayed != current_column ()));
11330 }
11331
11332 /* True if window start of W is frozen and may not be changed during
11333 redisplay. */
11334
11335 static bool
11336 window_frozen_p (struct window *w)
11337 {
11338 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11339 {
11340 Lisp_Object window;
11341
11342 XSETWINDOW (window, w);
11343 if (MINI_WINDOW_P (w))
11344 return false;
11345 else if (EQ (window, selected_window))
11346 return false;
11347 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11348 && EQ (window, Vminibuf_scroll_window))
11349 /* This special window can't be frozen too. */
11350 return false;
11351 else
11352 return true;
11353 }
11354 return false;
11355 }
11356
11357 /***********************************************************************
11358 Mode Lines and Frame Titles
11359 ***********************************************************************/
11360
11361 /* A buffer for constructing non-propertized mode-line strings and
11362 frame titles in it; allocated from the heap in init_xdisp and
11363 resized as needed in store_mode_line_noprop_char. */
11364
11365 static char *mode_line_noprop_buf;
11366
11367 /* The buffer's end, and a current output position in it. */
11368
11369 static char *mode_line_noprop_buf_end;
11370 static char *mode_line_noprop_ptr;
11371
11372 #define MODE_LINE_NOPROP_LEN(start) \
11373 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11374
11375 static enum {
11376 MODE_LINE_DISPLAY = 0,
11377 MODE_LINE_TITLE,
11378 MODE_LINE_NOPROP,
11379 MODE_LINE_STRING
11380 } mode_line_target;
11381
11382 /* Alist that caches the results of :propertize.
11383 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11384 static Lisp_Object mode_line_proptrans_alist;
11385
11386 /* List of strings making up the mode-line. */
11387 static Lisp_Object mode_line_string_list;
11388
11389 /* Base face property when building propertized mode line string. */
11390 static Lisp_Object mode_line_string_face;
11391 static Lisp_Object mode_line_string_face_prop;
11392
11393
11394 /* Unwind data for mode line strings */
11395
11396 static Lisp_Object Vmode_line_unwind_vector;
11397
11398 static Lisp_Object
11399 format_mode_line_unwind_data (struct frame *target_frame,
11400 struct buffer *obuf,
11401 Lisp_Object owin,
11402 bool save_proptrans)
11403 {
11404 Lisp_Object vector, tmp;
11405
11406 /* Reduce consing by keeping one vector in
11407 Vwith_echo_area_save_vector. */
11408 vector = Vmode_line_unwind_vector;
11409 Vmode_line_unwind_vector = Qnil;
11410
11411 if (NILP (vector))
11412 vector = Fmake_vector (make_number (10), Qnil);
11413
11414 ASET (vector, 0, make_number (mode_line_target));
11415 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11416 ASET (vector, 2, mode_line_string_list);
11417 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11418 ASET (vector, 4, mode_line_string_face);
11419 ASET (vector, 5, mode_line_string_face_prop);
11420
11421 if (obuf)
11422 XSETBUFFER (tmp, obuf);
11423 else
11424 tmp = Qnil;
11425 ASET (vector, 6, tmp);
11426 ASET (vector, 7, owin);
11427 if (target_frame)
11428 {
11429 /* Similarly to `with-selected-window', if the operation selects
11430 a window on another frame, we must restore that frame's
11431 selected window, and (for a tty) the top-frame. */
11432 ASET (vector, 8, target_frame->selected_window);
11433 if (FRAME_TERMCAP_P (target_frame))
11434 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11435 }
11436
11437 return vector;
11438 }
11439
11440 static void
11441 unwind_format_mode_line (Lisp_Object vector)
11442 {
11443 Lisp_Object old_window = AREF (vector, 7);
11444 Lisp_Object target_frame_window = AREF (vector, 8);
11445 Lisp_Object old_top_frame = AREF (vector, 9);
11446
11447 mode_line_target = XINT (AREF (vector, 0));
11448 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11449 mode_line_string_list = AREF (vector, 2);
11450 if (! EQ (AREF (vector, 3), Qt))
11451 mode_line_proptrans_alist = AREF (vector, 3);
11452 mode_line_string_face = AREF (vector, 4);
11453 mode_line_string_face_prop = AREF (vector, 5);
11454
11455 /* Select window before buffer, since it may change the buffer. */
11456 if (!NILP (old_window))
11457 {
11458 /* If the operation that we are unwinding had selected a window
11459 on a different frame, reset its frame-selected-window. For a
11460 text terminal, reset its top-frame if necessary. */
11461 if (!NILP (target_frame_window))
11462 {
11463 Lisp_Object frame
11464 = WINDOW_FRAME (XWINDOW (target_frame_window));
11465
11466 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11467 Fselect_window (target_frame_window, Qt);
11468
11469 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11470 Fselect_frame (old_top_frame, Qt);
11471 }
11472
11473 Fselect_window (old_window, Qt);
11474 }
11475
11476 if (!NILP (AREF (vector, 6)))
11477 {
11478 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11479 ASET (vector, 6, Qnil);
11480 }
11481
11482 Vmode_line_unwind_vector = vector;
11483 }
11484
11485
11486 /* Store a single character C for the frame title in mode_line_noprop_buf.
11487 Re-allocate mode_line_noprop_buf if necessary. */
11488
11489 static void
11490 store_mode_line_noprop_char (char c)
11491 {
11492 /* If output position has reached the end of the allocated buffer,
11493 increase the buffer's size. */
11494 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11495 {
11496 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11497 ptrdiff_t size = len;
11498 mode_line_noprop_buf =
11499 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11500 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11501 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11502 }
11503
11504 *mode_line_noprop_ptr++ = c;
11505 }
11506
11507
11508 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11509 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11510 characters that yield more columns than PRECISION; PRECISION <= 0
11511 means copy the whole string. Pad with spaces until FIELD_WIDTH
11512 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11513 pad. Called from display_mode_element when it is used to build a
11514 frame title. */
11515
11516 static int
11517 store_mode_line_noprop (const char *string, int field_width, int precision)
11518 {
11519 const unsigned char *str = (const unsigned char *) string;
11520 int n = 0;
11521 ptrdiff_t dummy, nbytes;
11522
11523 /* Copy at most PRECISION chars from STR. */
11524 nbytes = strlen (string);
11525 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11526 while (nbytes--)
11527 store_mode_line_noprop_char (*str++);
11528
11529 /* Fill up with spaces until FIELD_WIDTH reached. */
11530 while (field_width > 0
11531 && n < field_width)
11532 {
11533 store_mode_line_noprop_char (' ');
11534 ++n;
11535 }
11536
11537 return n;
11538 }
11539
11540 /***********************************************************************
11541 Frame Titles
11542 ***********************************************************************/
11543
11544 #ifdef HAVE_WINDOW_SYSTEM
11545
11546 /* Set the title of FRAME, if it has changed. The title format is
11547 Vicon_title_format if FRAME is iconified, otherwise it is
11548 frame_title_format. */
11549
11550 static void
11551 x_consider_frame_title (Lisp_Object frame)
11552 {
11553 struct frame *f = XFRAME (frame);
11554
11555 if ((FRAME_WINDOW_P (f)
11556 || FRAME_MINIBUF_ONLY_P (f)
11557 || f->explicit_name)
11558 && NILP (Fframe_parameter (frame, Qtooltip)))
11559 {
11560 /* Do we have more than one visible frame on this X display? */
11561 Lisp_Object tail, other_frame, fmt;
11562 ptrdiff_t title_start;
11563 char *title;
11564 ptrdiff_t len;
11565 struct it it;
11566 ptrdiff_t count = SPECPDL_INDEX ();
11567
11568 FOR_EACH_FRAME (tail, other_frame)
11569 {
11570 struct frame *tf = XFRAME (other_frame);
11571
11572 if (tf != f
11573 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11574 && !FRAME_MINIBUF_ONLY_P (tf)
11575 && !EQ (other_frame, tip_frame)
11576 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11577 break;
11578 }
11579
11580 /* Set global variable indicating that multiple frames exist. */
11581 multiple_frames = CONSP (tail);
11582
11583 /* Switch to the buffer of selected window of the frame. Set up
11584 mode_line_target so that display_mode_element will output into
11585 mode_line_noprop_buf; then display the title. */
11586 record_unwind_protect (unwind_format_mode_line,
11587 format_mode_line_unwind_data
11588 (f, current_buffer, selected_window, false));
11589
11590 Fselect_window (f->selected_window, Qt);
11591 set_buffer_internal_1
11592 (XBUFFER (XWINDOW (f->selected_window)->contents));
11593 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11594
11595 mode_line_target = MODE_LINE_TITLE;
11596 title_start = MODE_LINE_NOPROP_LEN (0);
11597 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11598 NULL, DEFAULT_FACE_ID);
11599 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11600 len = MODE_LINE_NOPROP_LEN (title_start);
11601 title = mode_line_noprop_buf + title_start;
11602 unbind_to (count, Qnil);
11603
11604 /* Set the title only if it's changed. This avoids consing in
11605 the common case where it hasn't. (If it turns out that we've
11606 already wasted too much time by walking through the list with
11607 display_mode_element, then we might need to optimize at a
11608 higher level than this.) */
11609 if (! STRINGP (f->name)
11610 || SBYTES (f->name) != len
11611 || memcmp (title, SDATA (f->name), len) != 0)
11612 x_implicitly_set_name (f, make_string (title, len), Qnil);
11613 }
11614 }
11615
11616 #endif /* not HAVE_WINDOW_SYSTEM */
11617
11618 \f
11619 /***********************************************************************
11620 Menu Bars
11621 ***********************************************************************/
11622
11623 /* True if we will not redisplay all visible windows. */
11624 #define REDISPLAY_SOME_P() \
11625 ((windows_or_buffers_changed == 0 \
11626 || windows_or_buffers_changed == REDISPLAY_SOME) \
11627 && (update_mode_lines == 0 \
11628 || update_mode_lines == REDISPLAY_SOME))
11629
11630 /* Prepare for redisplay by updating menu-bar item lists when
11631 appropriate. This can call eval. */
11632
11633 static void
11634 prepare_menu_bars (void)
11635 {
11636 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11637 bool some_windows = REDISPLAY_SOME_P ();
11638 Lisp_Object tooltip_frame;
11639
11640 #ifdef HAVE_WINDOW_SYSTEM
11641 tooltip_frame = tip_frame;
11642 #else
11643 tooltip_frame = Qnil;
11644 #endif
11645
11646 if (FUNCTIONP (Vpre_redisplay_function))
11647 {
11648 Lisp_Object windows = all_windows ? Qt : Qnil;
11649 if (all_windows && some_windows)
11650 {
11651 Lisp_Object ws = window_list ();
11652 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11653 {
11654 Lisp_Object this = XCAR (ws);
11655 struct window *w = XWINDOW (this);
11656 if (w->redisplay
11657 || XFRAME (w->frame)->redisplay
11658 || XBUFFER (w->contents)->text->redisplay)
11659 {
11660 windows = Fcons (this, windows);
11661 }
11662 }
11663 }
11664 safe__call1 (true, Vpre_redisplay_function, windows);
11665 }
11666
11667 /* Update all frame titles based on their buffer names, etc. We do
11668 this before the menu bars so that the buffer-menu will show the
11669 up-to-date frame titles. */
11670 #ifdef HAVE_WINDOW_SYSTEM
11671 if (all_windows)
11672 {
11673 Lisp_Object tail, frame;
11674
11675 FOR_EACH_FRAME (tail, frame)
11676 {
11677 struct frame *f = XFRAME (frame);
11678 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11679 if (some_windows
11680 && !f->redisplay
11681 && !w->redisplay
11682 && !XBUFFER (w->contents)->text->redisplay)
11683 continue;
11684
11685 if (!EQ (frame, tooltip_frame)
11686 && (FRAME_ICONIFIED_P (f)
11687 || FRAME_VISIBLE_P (f) == 1
11688 /* Exclude TTY frames that are obscured because they
11689 are not the top frame on their console. This is
11690 because x_consider_frame_title actually switches
11691 to the frame, which for TTY frames means it is
11692 marked as garbaged, and will be completely
11693 redrawn on the next redisplay cycle. This causes
11694 TTY frames to be completely redrawn, when there
11695 are more than one of them, even though nothing
11696 should be changed on display. */
11697 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11698 x_consider_frame_title (frame);
11699 }
11700 }
11701 #endif /* HAVE_WINDOW_SYSTEM */
11702
11703 /* Update the menu bar item lists, if appropriate. This has to be
11704 done before any actual redisplay or generation of display lines. */
11705
11706 if (all_windows)
11707 {
11708 Lisp_Object tail, frame;
11709 ptrdiff_t count = SPECPDL_INDEX ();
11710 /* True means that update_menu_bar has run its hooks
11711 so any further calls to update_menu_bar shouldn't do so again. */
11712 bool menu_bar_hooks_run = false;
11713
11714 record_unwind_save_match_data ();
11715
11716 FOR_EACH_FRAME (tail, frame)
11717 {
11718 struct frame *f = XFRAME (frame);
11719 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11720
11721 /* Ignore tooltip frame. */
11722 if (EQ (frame, tooltip_frame))
11723 continue;
11724
11725 if (some_windows
11726 && !f->redisplay
11727 && !w->redisplay
11728 && !XBUFFER (w->contents)->text->redisplay)
11729 continue;
11730
11731 /* If a window on this frame changed size, report that to
11732 the user and clear the size-change flag. */
11733 if (FRAME_WINDOW_SIZES_CHANGED (f))
11734 {
11735 Lisp_Object functions;
11736
11737 /* Clear flag first in case we get an error below. */
11738 FRAME_WINDOW_SIZES_CHANGED (f) = false;
11739 functions = Vwindow_size_change_functions;
11740
11741 while (CONSP (functions))
11742 {
11743 if (!EQ (XCAR (functions), Qt))
11744 call1 (XCAR (functions), frame);
11745 functions = XCDR (functions);
11746 }
11747 }
11748
11749 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11750 #ifdef HAVE_WINDOW_SYSTEM
11751 update_tool_bar (f, false);
11752 #endif
11753 }
11754
11755 unbind_to (count, Qnil);
11756 }
11757 else
11758 {
11759 struct frame *sf = SELECTED_FRAME ();
11760 update_menu_bar (sf, true, false);
11761 #ifdef HAVE_WINDOW_SYSTEM
11762 update_tool_bar (sf, true);
11763 #endif
11764 }
11765 }
11766
11767
11768 /* Update the menu bar item list for frame F. This has to be done
11769 before we start to fill in any display lines, because it can call
11770 eval.
11771
11772 If SAVE_MATCH_DATA, we must save and restore it here.
11773
11774 If HOOKS_RUN, a previous call to update_menu_bar
11775 already ran the menu bar hooks for this redisplay, so there
11776 is no need to run them again. The return value is the
11777 updated value of this flag, to pass to the next call. */
11778
11779 static bool
11780 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11781 {
11782 Lisp_Object window;
11783 struct window *w;
11784
11785 /* If called recursively during a menu update, do nothing. This can
11786 happen when, for instance, an activate-menubar-hook causes a
11787 redisplay. */
11788 if (inhibit_menubar_update)
11789 return hooks_run;
11790
11791 window = FRAME_SELECTED_WINDOW (f);
11792 w = XWINDOW (window);
11793
11794 if (FRAME_WINDOW_P (f)
11795 ?
11796 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11797 || defined (HAVE_NS) || defined (USE_GTK)
11798 FRAME_EXTERNAL_MENU_BAR (f)
11799 #else
11800 FRAME_MENU_BAR_LINES (f) > 0
11801 #endif
11802 : FRAME_MENU_BAR_LINES (f) > 0)
11803 {
11804 /* If the user has switched buffers or windows, we need to
11805 recompute to reflect the new bindings. But we'll
11806 recompute when update_mode_lines is set too; that means
11807 that people can use force-mode-line-update to request
11808 that the menu bar be recomputed. The adverse effect on
11809 the rest of the redisplay algorithm is about the same as
11810 windows_or_buffers_changed anyway. */
11811 if (windows_or_buffers_changed
11812 /* This used to test w->update_mode_line, but we believe
11813 there is no need to recompute the menu in that case. */
11814 || update_mode_lines
11815 || window_buffer_changed (w))
11816 {
11817 struct buffer *prev = current_buffer;
11818 ptrdiff_t count = SPECPDL_INDEX ();
11819
11820 specbind (Qinhibit_menubar_update, Qt);
11821
11822 set_buffer_internal_1 (XBUFFER (w->contents));
11823 if (save_match_data)
11824 record_unwind_save_match_data ();
11825 if (NILP (Voverriding_local_map_menu_flag))
11826 {
11827 specbind (Qoverriding_terminal_local_map, Qnil);
11828 specbind (Qoverriding_local_map, Qnil);
11829 }
11830
11831 if (!hooks_run)
11832 {
11833 /* Run the Lucid hook. */
11834 safe_run_hooks (Qactivate_menubar_hook);
11835
11836 /* If it has changed current-menubar from previous value,
11837 really recompute the menu-bar from the value. */
11838 if (! NILP (Vlucid_menu_bar_dirty_flag))
11839 call0 (Qrecompute_lucid_menubar);
11840
11841 safe_run_hooks (Qmenu_bar_update_hook);
11842
11843 hooks_run = true;
11844 }
11845
11846 XSETFRAME (Vmenu_updating_frame, f);
11847 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11848
11849 /* Redisplay the menu bar in case we changed it. */
11850 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11851 || defined (HAVE_NS) || defined (USE_GTK)
11852 if (FRAME_WINDOW_P (f))
11853 {
11854 #if defined (HAVE_NS)
11855 /* All frames on Mac OS share the same menubar. So only
11856 the selected frame should be allowed to set it. */
11857 if (f == SELECTED_FRAME ())
11858 #endif
11859 set_frame_menubar (f, false, false);
11860 }
11861 else
11862 /* On a terminal screen, the menu bar is an ordinary screen
11863 line, and this makes it get updated. */
11864 w->update_mode_line = true;
11865 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11866 /* In the non-toolkit version, the menu bar is an ordinary screen
11867 line, and this makes it get updated. */
11868 w->update_mode_line = true;
11869 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11870
11871 unbind_to (count, Qnil);
11872 set_buffer_internal_1 (prev);
11873 }
11874 }
11875
11876 return hooks_run;
11877 }
11878
11879 /***********************************************************************
11880 Tool-bars
11881 ***********************************************************************/
11882
11883 #ifdef HAVE_WINDOW_SYSTEM
11884
11885 /* Select `frame' temporarily without running all the code in
11886 do_switch_frame.
11887 FIXME: Maybe do_switch_frame should be trimmed down similarly
11888 when `norecord' is set. */
11889 static void
11890 fast_set_selected_frame (Lisp_Object frame)
11891 {
11892 if (!EQ (selected_frame, frame))
11893 {
11894 selected_frame = frame;
11895 selected_window = XFRAME (frame)->selected_window;
11896 }
11897 }
11898
11899 /* Update the tool-bar item list for frame F. This has to be done
11900 before we start to fill in any display lines. Called from
11901 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
11902 and restore it here. */
11903
11904 static void
11905 update_tool_bar (struct frame *f, bool save_match_data)
11906 {
11907 #if defined (USE_GTK) || defined (HAVE_NS)
11908 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11909 #else
11910 bool do_update = (WINDOWP (f->tool_bar_window)
11911 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11912 #endif
11913
11914 if (do_update)
11915 {
11916 Lisp_Object window;
11917 struct window *w;
11918
11919 window = FRAME_SELECTED_WINDOW (f);
11920 w = XWINDOW (window);
11921
11922 /* If the user has switched buffers or windows, we need to
11923 recompute to reflect the new bindings. But we'll
11924 recompute when update_mode_lines is set too; that means
11925 that people can use force-mode-line-update to request
11926 that the menu bar be recomputed. The adverse effect on
11927 the rest of the redisplay algorithm is about the same as
11928 windows_or_buffers_changed anyway. */
11929 if (windows_or_buffers_changed
11930 || w->update_mode_line
11931 || update_mode_lines
11932 || window_buffer_changed (w))
11933 {
11934 struct buffer *prev = current_buffer;
11935 ptrdiff_t count = SPECPDL_INDEX ();
11936 Lisp_Object frame, new_tool_bar;
11937 int new_n_tool_bar;
11938
11939 /* Set current_buffer to the buffer of the selected
11940 window of the frame, so that we get the right local
11941 keymaps. */
11942 set_buffer_internal_1 (XBUFFER (w->contents));
11943
11944 /* Save match data, if we must. */
11945 if (save_match_data)
11946 record_unwind_save_match_data ();
11947
11948 /* Make sure that we don't accidentally use bogus keymaps. */
11949 if (NILP (Voverriding_local_map_menu_flag))
11950 {
11951 specbind (Qoverriding_terminal_local_map, Qnil);
11952 specbind (Qoverriding_local_map, Qnil);
11953 }
11954
11955 /* We must temporarily set the selected frame to this frame
11956 before calling tool_bar_items, because the calculation of
11957 the tool-bar keymap uses the selected frame (see
11958 `tool-bar-make-keymap' in tool-bar.el). */
11959 eassert (EQ (selected_window,
11960 /* Since we only explicitly preserve selected_frame,
11961 check that selected_window would be redundant. */
11962 XFRAME (selected_frame)->selected_window));
11963 record_unwind_protect (fast_set_selected_frame, selected_frame);
11964 XSETFRAME (frame, f);
11965 fast_set_selected_frame (frame);
11966
11967 /* Build desired tool-bar items from keymaps. */
11968 new_tool_bar
11969 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11970 &new_n_tool_bar);
11971
11972 /* Redisplay the tool-bar if we changed it. */
11973 if (new_n_tool_bar != f->n_tool_bar_items
11974 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11975 {
11976 /* Redisplay that happens asynchronously due to an expose event
11977 may access f->tool_bar_items. Make sure we update both
11978 variables within BLOCK_INPUT so no such event interrupts. */
11979 block_input ();
11980 fset_tool_bar_items (f, new_tool_bar);
11981 f->n_tool_bar_items = new_n_tool_bar;
11982 w->update_mode_line = true;
11983 unblock_input ();
11984 }
11985
11986 unbind_to (count, Qnil);
11987 set_buffer_internal_1 (prev);
11988 }
11989 }
11990 }
11991
11992 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11993
11994 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11995 F's desired tool-bar contents. F->tool_bar_items must have
11996 been set up previously by calling prepare_menu_bars. */
11997
11998 static void
11999 build_desired_tool_bar_string (struct frame *f)
12000 {
12001 int i, size, size_needed;
12002 Lisp_Object image, plist;
12003
12004 image = plist = Qnil;
12005
12006 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12007 Otherwise, make a new string. */
12008
12009 /* The size of the string we might be able to reuse. */
12010 size = (STRINGP (f->desired_tool_bar_string)
12011 ? SCHARS (f->desired_tool_bar_string)
12012 : 0);
12013
12014 /* We need one space in the string for each image. */
12015 size_needed = f->n_tool_bar_items;
12016
12017 /* Reuse f->desired_tool_bar_string, if possible. */
12018 if (size < size_needed || NILP (f->desired_tool_bar_string))
12019 fset_desired_tool_bar_string
12020 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12021 else
12022 {
12023 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12024 Fremove_text_properties (make_number (0), make_number (size),
12025 props, f->desired_tool_bar_string);
12026 }
12027
12028 /* Put a `display' property on the string for the images to display,
12029 put a `menu_item' property on tool-bar items with a value that
12030 is the index of the item in F's tool-bar item vector. */
12031 for (i = 0; i < f->n_tool_bar_items; ++i)
12032 {
12033 #define PROP(IDX) \
12034 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12035
12036 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12037 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12038 int hmargin, vmargin, relief, idx, end;
12039
12040 /* If image is a vector, choose the image according to the
12041 button state. */
12042 image = PROP (TOOL_BAR_ITEM_IMAGES);
12043 if (VECTORP (image))
12044 {
12045 if (enabled_p)
12046 idx = (selected_p
12047 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12048 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12049 else
12050 idx = (selected_p
12051 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12052 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12053
12054 eassert (ASIZE (image) >= idx);
12055 image = AREF (image, idx);
12056 }
12057 else
12058 idx = -1;
12059
12060 /* Ignore invalid image specifications. */
12061 if (!valid_image_p (image))
12062 continue;
12063
12064 /* Display the tool-bar button pressed, or depressed. */
12065 plist = Fcopy_sequence (XCDR (image));
12066
12067 /* Compute margin and relief to draw. */
12068 relief = (tool_bar_button_relief >= 0
12069 ? tool_bar_button_relief
12070 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12071 hmargin = vmargin = relief;
12072
12073 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12074 INT_MAX - max (hmargin, vmargin)))
12075 {
12076 hmargin += XFASTINT (Vtool_bar_button_margin);
12077 vmargin += XFASTINT (Vtool_bar_button_margin);
12078 }
12079 else if (CONSP (Vtool_bar_button_margin))
12080 {
12081 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12082 INT_MAX - hmargin))
12083 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12084
12085 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12086 INT_MAX - vmargin))
12087 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12088 }
12089
12090 if (auto_raise_tool_bar_buttons_p)
12091 {
12092 /* Add a `:relief' property to the image spec if the item is
12093 selected. */
12094 if (selected_p)
12095 {
12096 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12097 hmargin -= relief;
12098 vmargin -= relief;
12099 }
12100 }
12101 else
12102 {
12103 /* If image is selected, display it pressed, i.e. with a
12104 negative relief. If it's not selected, display it with a
12105 raised relief. */
12106 plist = Fplist_put (plist, QCrelief,
12107 (selected_p
12108 ? make_number (-relief)
12109 : make_number (relief)));
12110 hmargin -= relief;
12111 vmargin -= relief;
12112 }
12113
12114 /* Put a margin around the image. */
12115 if (hmargin || vmargin)
12116 {
12117 if (hmargin == vmargin)
12118 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12119 else
12120 plist = Fplist_put (plist, QCmargin,
12121 Fcons (make_number (hmargin),
12122 make_number (vmargin)));
12123 }
12124
12125 /* If button is not enabled, and we don't have special images
12126 for the disabled state, make the image appear disabled by
12127 applying an appropriate algorithm to it. */
12128 if (!enabled_p && idx < 0)
12129 plist = Fplist_put (plist, QCconversion, Qdisabled);
12130
12131 /* Put a `display' text property on the string for the image to
12132 display. Put a `menu-item' property on the string that gives
12133 the start of this item's properties in the tool-bar items
12134 vector. */
12135 image = Fcons (Qimage, plist);
12136 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12137 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12138
12139 /* Let the last image hide all remaining spaces in the tool bar
12140 string. The string can be longer than needed when we reuse a
12141 previous string. */
12142 if (i + 1 == f->n_tool_bar_items)
12143 end = SCHARS (f->desired_tool_bar_string);
12144 else
12145 end = i + 1;
12146 Fadd_text_properties (make_number (i), make_number (end),
12147 props, f->desired_tool_bar_string);
12148 #undef PROP
12149 }
12150 }
12151
12152
12153 /* Display one line of the tool-bar of frame IT->f.
12154
12155 HEIGHT specifies the desired height of the tool-bar line.
12156 If the actual height of the glyph row is less than HEIGHT, the
12157 row's height is increased to HEIGHT, and the icons are centered
12158 vertically in the new height.
12159
12160 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12161 count a final empty row in case the tool-bar width exactly matches
12162 the window width.
12163 */
12164
12165 static void
12166 display_tool_bar_line (struct it *it, int height)
12167 {
12168 struct glyph_row *row = it->glyph_row;
12169 int max_x = it->last_visible_x;
12170 struct glyph *last;
12171
12172 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12173 clear_glyph_row (row);
12174 row->enabled_p = true;
12175 row->y = it->current_y;
12176
12177 /* Note that this isn't made use of if the face hasn't a box,
12178 so there's no need to check the face here. */
12179 it->start_of_box_run_p = true;
12180
12181 while (it->current_x < max_x)
12182 {
12183 int x, n_glyphs_before, i, nglyphs;
12184 struct it it_before;
12185
12186 /* Get the next display element. */
12187 if (!get_next_display_element (it))
12188 {
12189 /* Don't count empty row if we are counting needed tool-bar lines. */
12190 if (height < 0 && !it->hpos)
12191 return;
12192 break;
12193 }
12194
12195 /* Produce glyphs. */
12196 n_glyphs_before = row->used[TEXT_AREA];
12197 it_before = *it;
12198
12199 PRODUCE_GLYPHS (it);
12200
12201 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12202 i = 0;
12203 x = it_before.current_x;
12204 while (i < nglyphs)
12205 {
12206 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12207
12208 if (x + glyph->pixel_width > max_x)
12209 {
12210 /* Glyph doesn't fit on line. Backtrack. */
12211 row->used[TEXT_AREA] = n_glyphs_before;
12212 *it = it_before;
12213 /* If this is the only glyph on this line, it will never fit on the
12214 tool-bar, so skip it. But ensure there is at least one glyph,
12215 so we don't accidentally disable the tool-bar. */
12216 if (n_glyphs_before == 0
12217 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12218 break;
12219 goto out;
12220 }
12221
12222 ++it->hpos;
12223 x += glyph->pixel_width;
12224 ++i;
12225 }
12226
12227 /* Stop at line end. */
12228 if (ITERATOR_AT_END_OF_LINE_P (it))
12229 break;
12230
12231 set_iterator_to_next (it, true);
12232 }
12233
12234 out:;
12235
12236 row->displays_text_p = row->used[TEXT_AREA] != 0;
12237
12238 /* Use default face for the border below the tool bar.
12239
12240 FIXME: When auto-resize-tool-bars is grow-only, there is
12241 no additional border below the possibly empty tool-bar lines.
12242 So to make the extra empty lines look "normal", we have to
12243 use the tool-bar face for the border too. */
12244 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12245 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12246 it->face_id = DEFAULT_FACE_ID;
12247
12248 extend_face_to_end_of_line (it);
12249 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12250 last->right_box_line_p = true;
12251 if (last == row->glyphs[TEXT_AREA])
12252 last->left_box_line_p = true;
12253
12254 /* Make line the desired height and center it vertically. */
12255 if ((height -= it->max_ascent + it->max_descent) > 0)
12256 {
12257 /* Don't add more than one line height. */
12258 height %= FRAME_LINE_HEIGHT (it->f);
12259 it->max_ascent += height / 2;
12260 it->max_descent += (height + 1) / 2;
12261 }
12262
12263 compute_line_metrics (it);
12264
12265 /* If line is empty, make it occupy the rest of the tool-bar. */
12266 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12267 {
12268 row->height = row->phys_height = it->last_visible_y - row->y;
12269 row->visible_height = row->height;
12270 row->ascent = row->phys_ascent = 0;
12271 row->extra_line_spacing = 0;
12272 }
12273
12274 row->full_width_p = true;
12275 row->continued_p = false;
12276 row->truncated_on_left_p = false;
12277 row->truncated_on_right_p = false;
12278
12279 it->current_x = it->hpos = 0;
12280 it->current_y += row->height;
12281 ++it->vpos;
12282 ++it->glyph_row;
12283 }
12284
12285
12286 /* Value is the number of pixels needed to make all tool-bar items of
12287 frame F visible. The actual number of glyph rows needed is
12288 returned in *N_ROWS if non-NULL. */
12289 static int
12290 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12291 {
12292 struct window *w = XWINDOW (f->tool_bar_window);
12293 struct it it;
12294 /* tool_bar_height is called from redisplay_tool_bar after building
12295 the desired matrix, so use (unused) mode-line row as temporary row to
12296 avoid destroying the first tool-bar row. */
12297 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12298
12299 /* Initialize an iterator for iteration over
12300 F->desired_tool_bar_string in the tool-bar window of frame F. */
12301 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12302 temp_row->reversed_p = false;
12303 it.first_visible_x = 0;
12304 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12305 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12306 it.paragraph_embedding = L2R;
12307
12308 while (!ITERATOR_AT_END_P (&it))
12309 {
12310 clear_glyph_row (temp_row);
12311 it.glyph_row = temp_row;
12312 display_tool_bar_line (&it, -1);
12313 }
12314 clear_glyph_row (temp_row);
12315
12316 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12317 if (n_rows)
12318 *n_rows = it.vpos > 0 ? it.vpos : -1;
12319
12320 if (pixelwise)
12321 return it.current_y;
12322 else
12323 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12324 }
12325
12326 #endif /* !USE_GTK && !HAVE_NS */
12327
12328 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12329 0, 2, 0,
12330 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12331 If FRAME is nil or omitted, use the selected frame. Optional argument
12332 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12333 (Lisp_Object frame, Lisp_Object pixelwise)
12334 {
12335 int height = 0;
12336
12337 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12338 struct frame *f = decode_any_frame (frame);
12339
12340 if (WINDOWP (f->tool_bar_window)
12341 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12342 {
12343 update_tool_bar (f, true);
12344 if (f->n_tool_bar_items)
12345 {
12346 build_desired_tool_bar_string (f);
12347 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12348 }
12349 }
12350 #endif
12351
12352 return make_number (height);
12353 }
12354
12355
12356 /* Display the tool-bar of frame F. Value is true if tool-bar's
12357 height should be changed. */
12358 static bool
12359 redisplay_tool_bar (struct frame *f)
12360 {
12361 f->tool_bar_redisplayed = true;
12362 #if defined (USE_GTK) || defined (HAVE_NS)
12363
12364 if (FRAME_EXTERNAL_TOOL_BAR (f))
12365 update_frame_tool_bar (f);
12366 return false;
12367
12368 #else /* !USE_GTK && !HAVE_NS */
12369
12370 struct window *w;
12371 struct it it;
12372 struct glyph_row *row;
12373
12374 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12375 do anything. This means you must start with tool-bar-lines
12376 non-zero to get the auto-sizing effect. Or in other words, you
12377 can turn off tool-bars by specifying tool-bar-lines zero. */
12378 if (!WINDOWP (f->tool_bar_window)
12379 || (w = XWINDOW (f->tool_bar_window),
12380 WINDOW_TOTAL_LINES (w) == 0))
12381 return false;
12382
12383 /* Set up an iterator for the tool-bar window. */
12384 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12385 it.first_visible_x = 0;
12386 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12387 row = it.glyph_row;
12388 row->reversed_p = false;
12389
12390 /* Build a string that represents the contents of the tool-bar. */
12391 build_desired_tool_bar_string (f);
12392 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12393 /* FIXME: This should be controlled by a user option. But it
12394 doesn't make sense to have an R2L tool bar if the menu bar cannot
12395 be drawn also R2L, and making the menu bar R2L is tricky due
12396 toolkit-specific code that implements it. If an R2L tool bar is
12397 ever supported, display_tool_bar_line should also be augmented to
12398 call unproduce_glyphs like display_line and display_string
12399 do. */
12400 it.paragraph_embedding = L2R;
12401
12402 if (f->n_tool_bar_rows == 0)
12403 {
12404 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12405
12406 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12407 {
12408 x_change_tool_bar_height (f, new_height);
12409 frame_default_tool_bar_height = new_height;
12410 /* Always do that now. */
12411 clear_glyph_matrix (w->desired_matrix);
12412 f->fonts_changed = true;
12413 return true;
12414 }
12415 }
12416
12417 /* Display as many lines as needed to display all tool-bar items. */
12418
12419 if (f->n_tool_bar_rows > 0)
12420 {
12421 int border, rows, height, extra;
12422
12423 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12424 border = XINT (Vtool_bar_border);
12425 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12426 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12427 else if (EQ (Vtool_bar_border, Qborder_width))
12428 border = f->border_width;
12429 else
12430 border = 0;
12431 if (border < 0)
12432 border = 0;
12433
12434 rows = f->n_tool_bar_rows;
12435 height = max (1, (it.last_visible_y - border) / rows);
12436 extra = it.last_visible_y - border - height * rows;
12437
12438 while (it.current_y < it.last_visible_y)
12439 {
12440 int h = 0;
12441 if (extra > 0 && rows-- > 0)
12442 {
12443 h = (extra + rows - 1) / rows;
12444 extra -= h;
12445 }
12446 display_tool_bar_line (&it, height + h);
12447 }
12448 }
12449 else
12450 {
12451 while (it.current_y < it.last_visible_y)
12452 display_tool_bar_line (&it, 0);
12453 }
12454
12455 /* It doesn't make much sense to try scrolling in the tool-bar
12456 window, so don't do it. */
12457 w->desired_matrix->no_scrolling_p = true;
12458 w->must_be_updated_p = true;
12459
12460 if (!NILP (Vauto_resize_tool_bars))
12461 {
12462 bool change_height_p = true;
12463
12464 /* If we couldn't display everything, change the tool-bar's
12465 height if there is room for more. */
12466 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12467 change_height_p = true;
12468
12469 /* We subtract 1 because display_tool_bar_line advances the
12470 glyph_row pointer before returning to its caller. We want to
12471 examine the last glyph row produced by
12472 display_tool_bar_line. */
12473 row = it.glyph_row - 1;
12474
12475 /* If there are blank lines at the end, except for a partially
12476 visible blank line at the end that is smaller than
12477 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12478 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12479 && row->height >= FRAME_LINE_HEIGHT (f))
12480 change_height_p = true;
12481
12482 /* If row displays tool-bar items, but is partially visible,
12483 change the tool-bar's height. */
12484 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12485 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12486 change_height_p = true;
12487
12488 /* Resize windows as needed by changing the `tool-bar-lines'
12489 frame parameter. */
12490 if (change_height_p)
12491 {
12492 int nrows;
12493 int new_height = tool_bar_height (f, &nrows, true);
12494
12495 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12496 && !f->minimize_tool_bar_window_p)
12497 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12498 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12499 f->minimize_tool_bar_window_p = false;
12500
12501 if (change_height_p)
12502 {
12503 x_change_tool_bar_height (f, new_height);
12504 frame_default_tool_bar_height = new_height;
12505 clear_glyph_matrix (w->desired_matrix);
12506 f->n_tool_bar_rows = nrows;
12507 f->fonts_changed = true;
12508
12509 return true;
12510 }
12511 }
12512 }
12513
12514 f->minimize_tool_bar_window_p = false;
12515 return false;
12516
12517 #endif /* USE_GTK || HAVE_NS */
12518 }
12519
12520 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12521
12522 /* Get information about the tool-bar item which is displayed in GLYPH
12523 on frame F. Return in *PROP_IDX the index where tool-bar item
12524 properties start in F->tool_bar_items. Value is false if
12525 GLYPH doesn't display a tool-bar item. */
12526
12527 static bool
12528 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12529 {
12530 Lisp_Object prop;
12531 int charpos;
12532
12533 /* This function can be called asynchronously, which means we must
12534 exclude any possibility that Fget_text_property signals an
12535 error. */
12536 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12537 charpos = max (0, charpos);
12538
12539 /* Get the text property `menu-item' at pos. The value of that
12540 property is the start index of this item's properties in
12541 F->tool_bar_items. */
12542 prop = Fget_text_property (make_number (charpos),
12543 Qmenu_item, f->current_tool_bar_string);
12544 if (! INTEGERP (prop))
12545 return false;
12546 *prop_idx = XINT (prop);
12547 return true;
12548 }
12549
12550 \f
12551 /* Get information about the tool-bar item at position X/Y on frame F.
12552 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12553 the current matrix of the tool-bar window of F, or NULL if not
12554 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12555 item in F->tool_bar_items. Value is
12556
12557 -1 if X/Y is not on a tool-bar item
12558 0 if X/Y is on the same item that was highlighted before.
12559 1 otherwise. */
12560
12561 static int
12562 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12563 int *hpos, int *vpos, int *prop_idx)
12564 {
12565 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12566 struct window *w = XWINDOW (f->tool_bar_window);
12567 int area;
12568
12569 /* Find the glyph under X/Y. */
12570 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12571 if (*glyph == NULL)
12572 return -1;
12573
12574 /* Get the start of this tool-bar item's properties in
12575 f->tool_bar_items. */
12576 if (!tool_bar_item_info (f, *glyph, prop_idx))
12577 return -1;
12578
12579 /* Is mouse on the highlighted item? */
12580 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12581 && *vpos >= hlinfo->mouse_face_beg_row
12582 && *vpos <= hlinfo->mouse_face_end_row
12583 && (*vpos > hlinfo->mouse_face_beg_row
12584 || *hpos >= hlinfo->mouse_face_beg_col)
12585 && (*vpos < hlinfo->mouse_face_end_row
12586 || *hpos < hlinfo->mouse_face_end_col
12587 || hlinfo->mouse_face_past_end))
12588 return 0;
12589
12590 return 1;
12591 }
12592
12593
12594 /* EXPORT:
12595 Handle mouse button event on the tool-bar of frame F, at
12596 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12597 false for button release. MODIFIERS is event modifiers for button
12598 release. */
12599
12600 void
12601 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12602 int modifiers)
12603 {
12604 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12605 struct window *w = XWINDOW (f->tool_bar_window);
12606 int hpos, vpos, prop_idx;
12607 struct glyph *glyph;
12608 Lisp_Object enabled_p;
12609 int ts;
12610
12611 /* If not on the highlighted tool-bar item, and mouse-highlight is
12612 non-nil, return. This is so we generate the tool-bar button
12613 click only when the mouse button is released on the same item as
12614 where it was pressed. However, when mouse-highlight is disabled,
12615 generate the click when the button is released regardless of the
12616 highlight, since tool-bar items are not highlighted in that
12617 case. */
12618 frame_to_window_pixel_xy (w, &x, &y);
12619 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12620 if (ts == -1
12621 || (ts != 0 && !NILP (Vmouse_highlight)))
12622 return;
12623
12624 /* When mouse-highlight is off, generate the click for the item
12625 where the button was pressed, disregarding where it was
12626 released. */
12627 if (NILP (Vmouse_highlight) && !down_p)
12628 prop_idx = f->last_tool_bar_item;
12629
12630 /* If item is disabled, do nothing. */
12631 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12632 if (NILP (enabled_p))
12633 return;
12634
12635 if (down_p)
12636 {
12637 /* Show item in pressed state. */
12638 if (!NILP (Vmouse_highlight))
12639 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12640 f->last_tool_bar_item = prop_idx;
12641 }
12642 else
12643 {
12644 Lisp_Object key, frame;
12645 struct input_event event;
12646 EVENT_INIT (event);
12647
12648 /* Show item in released state. */
12649 if (!NILP (Vmouse_highlight))
12650 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12651
12652 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12653
12654 XSETFRAME (frame, f);
12655 event.kind = TOOL_BAR_EVENT;
12656 event.frame_or_window = frame;
12657 event.arg = frame;
12658 kbd_buffer_store_event (&event);
12659
12660 event.kind = TOOL_BAR_EVENT;
12661 event.frame_or_window = frame;
12662 event.arg = key;
12663 event.modifiers = modifiers;
12664 kbd_buffer_store_event (&event);
12665 f->last_tool_bar_item = -1;
12666 }
12667 }
12668
12669
12670 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12671 tool-bar window-relative coordinates X/Y. Called from
12672 note_mouse_highlight. */
12673
12674 static void
12675 note_tool_bar_highlight (struct frame *f, int x, int y)
12676 {
12677 Lisp_Object window = f->tool_bar_window;
12678 struct window *w = XWINDOW (window);
12679 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12680 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12681 int hpos, vpos;
12682 struct glyph *glyph;
12683 struct glyph_row *row;
12684 int i;
12685 Lisp_Object enabled_p;
12686 int prop_idx;
12687 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12688 bool mouse_down_p;
12689 int rc;
12690
12691 /* Function note_mouse_highlight is called with negative X/Y
12692 values when mouse moves outside of the frame. */
12693 if (x <= 0 || y <= 0)
12694 {
12695 clear_mouse_face (hlinfo);
12696 return;
12697 }
12698
12699 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12700 if (rc < 0)
12701 {
12702 /* Not on tool-bar item. */
12703 clear_mouse_face (hlinfo);
12704 return;
12705 }
12706 else if (rc == 0)
12707 /* On same tool-bar item as before. */
12708 goto set_help_echo;
12709
12710 clear_mouse_face (hlinfo);
12711
12712 /* Mouse is down, but on different tool-bar item? */
12713 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12714 && f == dpyinfo->last_mouse_frame);
12715
12716 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12717 return;
12718
12719 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12720
12721 /* If tool-bar item is not enabled, don't highlight it. */
12722 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12723 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12724 {
12725 /* Compute the x-position of the glyph. In front and past the
12726 image is a space. We include this in the highlighted area. */
12727 row = MATRIX_ROW (w->current_matrix, vpos);
12728 for (i = x = 0; i < hpos; ++i)
12729 x += row->glyphs[TEXT_AREA][i].pixel_width;
12730
12731 /* Record this as the current active region. */
12732 hlinfo->mouse_face_beg_col = hpos;
12733 hlinfo->mouse_face_beg_row = vpos;
12734 hlinfo->mouse_face_beg_x = x;
12735 hlinfo->mouse_face_past_end = false;
12736
12737 hlinfo->mouse_face_end_col = hpos + 1;
12738 hlinfo->mouse_face_end_row = vpos;
12739 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12740 hlinfo->mouse_face_window = window;
12741 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12742
12743 /* Display it as active. */
12744 show_mouse_face (hlinfo, draw);
12745 }
12746
12747 set_help_echo:
12748
12749 /* Set help_echo_string to a help string to display for this tool-bar item.
12750 XTread_socket does the rest. */
12751 help_echo_object = help_echo_window = Qnil;
12752 help_echo_pos = -1;
12753 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12754 if (NILP (help_echo_string))
12755 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12756 }
12757
12758 #endif /* !USE_GTK && !HAVE_NS */
12759
12760 #endif /* HAVE_WINDOW_SYSTEM */
12761
12762
12763 \f
12764 /************************************************************************
12765 Horizontal scrolling
12766 ************************************************************************/
12767
12768 /* For all leaf windows in the window tree rooted at WINDOW, set their
12769 hscroll value so that PT is (i) visible in the window, and (ii) so
12770 that it is not within a certain margin at the window's left and
12771 right border. Value is true if any window's hscroll has been
12772 changed. */
12773
12774 static bool
12775 hscroll_window_tree (Lisp_Object window)
12776 {
12777 bool hscrolled_p = false;
12778 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12779 int hscroll_step_abs = 0;
12780 double hscroll_step_rel = 0;
12781
12782 if (hscroll_relative_p)
12783 {
12784 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12785 if (hscroll_step_rel < 0)
12786 {
12787 hscroll_relative_p = false;
12788 hscroll_step_abs = 0;
12789 }
12790 }
12791 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12792 {
12793 hscroll_step_abs = XINT (Vhscroll_step);
12794 if (hscroll_step_abs < 0)
12795 hscroll_step_abs = 0;
12796 }
12797 else
12798 hscroll_step_abs = 0;
12799
12800 while (WINDOWP (window))
12801 {
12802 struct window *w = XWINDOW (window);
12803
12804 if (WINDOWP (w->contents))
12805 hscrolled_p |= hscroll_window_tree (w->contents);
12806 else if (w->cursor.vpos >= 0)
12807 {
12808 int h_margin;
12809 int text_area_width;
12810 struct glyph_row *cursor_row;
12811 struct glyph_row *bottom_row;
12812
12813 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12814 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12815 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12816 else
12817 cursor_row = bottom_row - 1;
12818
12819 if (!cursor_row->enabled_p)
12820 {
12821 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12822 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12823 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12824 else
12825 cursor_row = bottom_row - 1;
12826 }
12827 bool row_r2l_p = cursor_row->reversed_p;
12828
12829 text_area_width = window_box_width (w, TEXT_AREA);
12830
12831 /* Scroll when cursor is inside this scroll margin. */
12832 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12833
12834 /* If the position of this window's point has explicitly
12835 changed, no more suspend auto hscrolling. */
12836 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12837 w->suspend_auto_hscroll = false;
12838
12839 /* Remember window point. */
12840 Fset_marker (w->old_pointm,
12841 ((w == XWINDOW (selected_window))
12842 ? make_number (BUF_PT (XBUFFER (w->contents)))
12843 : Fmarker_position (w->pointm)),
12844 w->contents);
12845
12846 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12847 && !w->suspend_auto_hscroll
12848 /* In some pathological cases, like restoring a window
12849 configuration into a frame that is much smaller than
12850 the one from which the configuration was saved, we
12851 get glyph rows whose start and end have zero buffer
12852 positions, which we cannot handle below. Just skip
12853 such windows. */
12854 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12855 /* For left-to-right rows, hscroll when cursor is either
12856 (i) inside the right hscroll margin, or (ii) if it is
12857 inside the left margin and the window is already
12858 hscrolled. */
12859 && ((!row_r2l_p
12860 && ((w->hscroll && w->cursor.x <= h_margin)
12861 || (cursor_row->enabled_p
12862 && cursor_row->truncated_on_right_p
12863 && (w->cursor.x >= text_area_width - h_margin))))
12864 /* For right-to-left rows, the logic is similar,
12865 except that rules for scrolling to left and right
12866 are reversed. E.g., if cursor.x <= h_margin, we
12867 need to hscroll "to the right" unconditionally,
12868 and that will scroll the screen to the left so as
12869 to reveal the next portion of the row. */
12870 || (row_r2l_p
12871 && ((cursor_row->enabled_p
12872 /* FIXME: It is confusing to set the
12873 truncated_on_right_p flag when R2L rows
12874 are actually truncated on the left. */
12875 && cursor_row->truncated_on_right_p
12876 && w->cursor.x <= h_margin)
12877 || (w->hscroll
12878 && (w->cursor.x >= text_area_width - h_margin))))))
12879 {
12880 struct it it;
12881 ptrdiff_t hscroll;
12882 struct buffer *saved_current_buffer;
12883 ptrdiff_t pt;
12884 int wanted_x;
12885
12886 /* Find point in a display of infinite width. */
12887 saved_current_buffer = current_buffer;
12888 current_buffer = XBUFFER (w->contents);
12889
12890 if (w == XWINDOW (selected_window))
12891 pt = PT;
12892 else
12893 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12894
12895 /* Move iterator to pt starting at cursor_row->start in
12896 a line with infinite width. */
12897 init_to_row_start (&it, w, cursor_row);
12898 it.last_visible_x = INFINITY;
12899 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12900 current_buffer = saved_current_buffer;
12901
12902 /* Position cursor in window. */
12903 if (!hscroll_relative_p && hscroll_step_abs == 0)
12904 hscroll = max (0, (it.current_x
12905 - (ITERATOR_AT_END_OF_LINE_P (&it)
12906 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12907 : (text_area_width / 2))))
12908 / FRAME_COLUMN_WIDTH (it.f);
12909 else if ((!row_r2l_p
12910 && w->cursor.x >= text_area_width - h_margin)
12911 || (row_r2l_p && w->cursor.x <= h_margin))
12912 {
12913 if (hscroll_relative_p)
12914 wanted_x = text_area_width * (1 - hscroll_step_rel)
12915 - h_margin;
12916 else
12917 wanted_x = text_area_width
12918 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12919 - h_margin;
12920 hscroll
12921 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12922 }
12923 else
12924 {
12925 if (hscroll_relative_p)
12926 wanted_x = text_area_width * hscroll_step_rel
12927 + h_margin;
12928 else
12929 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12930 + h_margin;
12931 hscroll
12932 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12933 }
12934 hscroll = max (hscroll, w->min_hscroll);
12935
12936 /* Don't prevent redisplay optimizations if hscroll
12937 hasn't changed, as it will unnecessarily slow down
12938 redisplay. */
12939 if (w->hscroll != hscroll)
12940 {
12941 struct buffer *b = XBUFFER (w->contents);
12942 b->prevent_redisplay_optimizations_p = true;
12943 w->hscroll = hscroll;
12944 hscrolled_p = true;
12945 }
12946 }
12947 }
12948
12949 window = w->next;
12950 }
12951
12952 /* Value is true if hscroll of any leaf window has been changed. */
12953 return hscrolled_p;
12954 }
12955
12956
12957 /* Set hscroll so that cursor is visible and not inside horizontal
12958 scroll margins for all windows in the tree rooted at WINDOW. See
12959 also hscroll_window_tree above. Value is true if any window's
12960 hscroll has been changed. If it has, desired matrices on the frame
12961 of WINDOW are cleared. */
12962
12963 static bool
12964 hscroll_windows (Lisp_Object window)
12965 {
12966 bool hscrolled_p = hscroll_window_tree (window);
12967 if (hscrolled_p)
12968 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12969 return hscrolled_p;
12970 }
12971
12972
12973 \f
12974 /************************************************************************
12975 Redisplay
12976 ************************************************************************/
12977
12978 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
12979 This is sometimes handy to have in a debugger session. */
12980
12981 #ifdef GLYPH_DEBUG
12982
12983 /* First and last unchanged row for try_window_id. */
12984
12985 static int debug_first_unchanged_at_end_vpos;
12986 static int debug_last_unchanged_at_beg_vpos;
12987
12988 /* Delta vpos and y. */
12989
12990 static int debug_dvpos, debug_dy;
12991
12992 /* Delta in characters and bytes for try_window_id. */
12993
12994 static ptrdiff_t debug_delta, debug_delta_bytes;
12995
12996 /* Values of window_end_pos and window_end_vpos at the end of
12997 try_window_id. */
12998
12999 static ptrdiff_t debug_end_vpos;
13000
13001 /* Append a string to W->desired_matrix->method. FMT is a printf
13002 format string. If trace_redisplay_p is true also printf the
13003 resulting string to stderr. */
13004
13005 static void debug_method_add (struct window *, char const *, ...)
13006 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13007
13008 static void
13009 debug_method_add (struct window *w, char const *fmt, ...)
13010 {
13011 void *ptr = w;
13012 char *method = w->desired_matrix->method;
13013 int len = strlen (method);
13014 int size = sizeof w->desired_matrix->method;
13015 int remaining = size - len - 1;
13016 va_list ap;
13017
13018 if (len && remaining)
13019 {
13020 method[len] = '|';
13021 --remaining, ++len;
13022 }
13023
13024 va_start (ap, fmt);
13025 vsnprintf (method + len, remaining + 1, fmt, ap);
13026 va_end (ap);
13027
13028 if (trace_redisplay_p)
13029 fprintf (stderr, "%p (%s): %s\n",
13030 ptr,
13031 ((BUFFERP (w->contents)
13032 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13033 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13034 : "no buffer"),
13035 method + len);
13036 }
13037
13038 #endif /* GLYPH_DEBUG */
13039
13040
13041 /* Value is true if all changes in window W, which displays
13042 current_buffer, are in the text between START and END. START is a
13043 buffer position, END is given as a distance from Z. Used in
13044 redisplay_internal for display optimization. */
13045
13046 static bool
13047 text_outside_line_unchanged_p (struct window *w,
13048 ptrdiff_t start, ptrdiff_t end)
13049 {
13050 bool unchanged_p = true;
13051
13052 /* If text or overlays have changed, see where. */
13053 if (window_outdated (w))
13054 {
13055 /* Gap in the line? */
13056 if (GPT < start || Z - GPT < end)
13057 unchanged_p = false;
13058
13059 /* Changes start in front of the line, or end after it? */
13060 if (unchanged_p
13061 && (BEG_UNCHANGED < start - 1
13062 || END_UNCHANGED < end))
13063 unchanged_p = false;
13064
13065 /* If selective display, can't optimize if changes start at the
13066 beginning of the line. */
13067 if (unchanged_p
13068 && INTEGERP (BVAR (current_buffer, selective_display))
13069 && XINT (BVAR (current_buffer, selective_display)) > 0
13070 && (BEG_UNCHANGED < start || GPT <= start))
13071 unchanged_p = false;
13072
13073 /* If there are overlays at the start or end of the line, these
13074 may have overlay strings with newlines in them. A change at
13075 START, for instance, may actually concern the display of such
13076 overlay strings as well, and they are displayed on different
13077 lines. So, quickly rule out this case. (For the future, it
13078 might be desirable to implement something more telling than
13079 just BEG/END_UNCHANGED.) */
13080 if (unchanged_p)
13081 {
13082 if (BEG + BEG_UNCHANGED == start
13083 && overlay_touches_p (start))
13084 unchanged_p = false;
13085 if (END_UNCHANGED == end
13086 && overlay_touches_p (Z - end))
13087 unchanged_p = false;
13088 }
13089
13090 /* Under bidi reordering, adding or deleting a character in the
13091 beginning of a paragraph, before the first strong directional
13092 character, can change the base direction of the paragraph (unless
13093 the buffer specifies a fixed paragraph direction), which will
13094 require to redisplay the whole paragraph. It might be worthwhile
13095 to find the paragraph limits and widen the range of redisplayed
13096 lines to that, but for now just give up this optimization. */
13097 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13098 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13099 unchanged_p = false;
13100 }
13101
13102 return unchanged_p;
13103 }
13104
13105
13106 /* Do a frame update, taking possible shortcuts into account. This is
13107 the main external entry point for redisplay.
13108
13109 If the last redisplay displayed an echo area message and that message
13110 is no longer requested, we clear the echo area or bring back the
13111 mini-buffer if that is in use. */
13112
13113 void
13114 redisplay (void)
13115 {
13116 redisplay_internal ();
13117 }
13118
13119
13120 static Lisp_Object
13121 overlay_arrow_string_or_property (Lisp_Object var)
13122 {
13123 Lisp_Object val;
13124
13125 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13126 return val;
13127
13128 return Voverlay_arrow_string;
13129 }
13130
13131 /* Return true if there are any overlay-arrows in current_buffer. */
13132 static bool
13133 overlay_arrow_in_current_buffer_p (void)
13134 {
13135 Lisp_Object vlist;
13136
13137 for (vlist = Voverlay_arrow_variable_list;
13138 CONSP (vlist);
13139 vlist = XCDR (vlist))
13140 {
13141 Lisp_Object var = XCAR (vlist);
13142 Lisp_Object val;
13143
13144 if (!SYMBOLP (var))
13145 continue;
13146 val = find_symbol_value (var);
13147 if (MARKERP (val)
13148 && current_buffer == XMARKER (val)->buffer)
13149 return true;
13150 }
13151 return false;
13152 }
13153
13154
13155 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13156 has changed. */
13157
13158 static bool
13159 overlay_arrows_changed_p (void)
13160 {
13161 Lisp_Object vlist;
13162
13163 for (vlist = Voverlay_arrow_variable_list;
13164 CONSP (vlist);
13165 vlist = XCDR (vlist))
13166 {
13167 Lisp_Object var = XCAR (vlist);
13168 Lisp_Object val, pstr;
13169
13170 if (!SYMBOLP (var))
13171 continue;
13172 val = find_symbol_value (var);
13173 if (!MARKERP (val))
13174 continue;
13175 if (! EQ (COERCE_MARKER (val),
13176 Fget (var, Qlast_arrow_position))
13177 || ! (pstr = overlay_arrow_string_or_property (var),
13178 EQ (pstr, Fget (var, Qlast_arrow_string))))
13179 return true;
13180 }
13181 return false;
13182 }
13183
13184 /* Mark overlay arrows to be updated on next redisplay. */
13185
13186 static void
13187 update_overlay_arrows (int up_to_date)
13188 {
13189 Lisp_Object vlist;
13190
13191 for (vlist = Voverlay_arrow_variable_list;
13192 CONSP (vlist);
13193 vlist = XCDR (vlist))
13194 {
13195 Lisp_Object var = XCAR (vlist);
13196
13197 if (!SYMBOLP (var))
13198 continue;
13199
13200 if (up_to_date > 0)
13201 {
13202 Lisp_Object val = find_symbol_value (var);
13203 Fput (var, Qlast_arrow_position,
13204 COERCE_MARKER (val));
13205 Fput (var, Qlast_arrow_string,
13206 overlay_arrow_string_or_property (var));
13207 }
13208 else if (up_to_date < 0
13209 || !NILP (Fget (var, Qlast_arrow_position)))
13210 {
13211 Fput (var, Qlast_arrow_position, Qt);
13212 Fput (var, Qlast_arrow_string, Qt);
13213 }
13214 }
13215 }
13216
13217
13218 /* Return overlay arrow string to display at row.
13219 Return integer (bitmap number) for arrow bitmap in left fringe.
13220 Return nil if no overlay arrow. */
13221
13222 static Lisp_Object
13223 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13224 {
13225 Lisp_Object vlist;
13226
13227 for (vlist = Voverlay_arrow_variable_list;
13228 CONSP (vlist);
13229 vlist = XCDR (vlist))
13230 {
13231 Lisp_Object var = XCAR (vlist);
13232 Lisp_Object val;
13233
13234 if (!SYMBOLP (var))
13235 continue;
13236
13237 val = find_symbol_value (var);
13238
13239 if (MARKERP (val)
13240 && current_buffer == XMARKER (val)->buffer
13241 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13242 {
13243 if (FRAME_WINDOW_P (it->f)
13244 /* FIXME: if ROW->reversed_p is set, this should test
13245 the right fringe, not the left one. */
13246 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13247 {
13248 #ifdef HAVE_WINDOW_SYSTEM
13249 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13250 {
13251 int fringe_bitmap = lookup_fringe_bitmap (val);
13252 if (fringe_bitmap != 0)
13253 return make_number (fringe_bitmap);
13254 }
13255 #endif
13256 return make_number (-1); /* Use default arrow bitmap. */
13257 }
13258 return overlay_arrow_string_or_property (var);
13259 }
13260 }
13261
13262 return Qnil;
13263 }
13264
13265 /* Return true if point moved out of or into a composition. Otherwise
13266 return false. PREV_BUF and PREV_PT are the last point buffer and
13267 position. BUF and PT are the current point buffer and position. */
13268
13269 static bool
13270 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13271 struct buffer *buf, ptrdiff_t pt)
13272 {
13273 ptrdiff_t start, end;
13274 Lisp_Object prop;
13275 Lisp_Object buffer;
13276
13277 XSETBUFFER (buffer, buf);
13278 /* Check a composition at the last point if point moved within the
13279 same buffer. */
13280 if (prev_buf == buf)
13281 {
13282 if (prev_pt == pt)
13283 /* Point didn't move. */
13284 return false;
13285
13286 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13287 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13288 && composition_valid_p (start, end, prop)
13289 && start < prev_pt && end > prev_pt)
13290 /* The last point was within the composition. Return true iff
13291 point moved out of the composition. */
13292 return (pt <= start || pt >= end);
13293 }
13294
13295 /* Check a composition at the current point. */
13296 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13297 && find_composition (pt, -1, &start, &end, &prop, buffer)
13298 && composition_valid_p (start, end, prop)
13299 && start < pt && end > pt);
13300 }
13301
13302 /* Reconsider the clip changes of buffer which is displayed in W. */
13303
13304 static void
13305 reconsider_clip_changes (struct window *w)
13306 {
13307 struct buffer *b = XBUFFER (w->contents);
13308
13309 if (b->clip_changed
13310 && w->window_end_valid
13311 && w->current_matrix->buffer == b
13312 && w->current_matrix->zv == BUF_ZV (b)
13313 && w->current_matrix->begv == BUF_BEGV (b))
13314 b->clip_changed = false;
13315
13316 /* If display wasn't paused, and W is not a tool bar window, see if
13317 point has been moved into or out of a composition. In that case,
13318 set b->clip_changed to force updating the screen. If
13319 b->clip_changed has already been set, skip this check. */
13320 if (!b->clip_changed && w->window_end_valid)
13321 {
13322 ptrdiff_t pt = (w == XWINDOW (selected_window)
13323 ? PT : marker_position (w->pointm));
13324
13325 if ((w->current_matrix->buffer != b || pt != w->last_point)
13326 && check_point_in_composition (w->current_matrix->buffer,
13327 w->last_point, b, pt))
13328 b->clip_changed = true;
13329 }
13330 }
13331
13332 static void
13333 propagate_buffer_redisplay (void)
13334 { /* Resetting b->text->redisplay is problematic!
13335 We can't just reset it in the case that some window that displays
13336 it has not been redisplayed; and such a window can stay
13337 unredisplayed for a long time if it's currently invisible.
13338 But we do want to reset it at the end of redisplay otherwise
13339 its displayed windows will keep being redisplayed over and over
13340 again.
13341 So we copy all b->text->redisplay flags up to their windows here,
13342 such that mark_window_display_accurate can safely reset
13343 b->text->redisplay. */
13344 Lisp_Object ws = window_list ();
13345 for (; CONSP (ws); ws = XCDR (ws))
13346 {
13347 struct window *thisw = XWINDOW (XCAR (ws));
13348 struct buffer *thisb = XBUFFER (thisw->contents);
13349 if (thisb->text->redisplay)
13350 thisw->redisplay = true;
13351 }
13352 }
13353
13354 #define STOP_POLLING \
13355 do { if (! polling_stopped_here) stop_polling (); \
13356 polling_stopped_here = true; } while (false)
13357
13358 #define RESUME_POLLING \
13359 do { if (polling_stopped_here) start_polling (); \
13360 polling_stopped_here = false; } while (false)
13361
13362
13363 /* Perhaps in the future avoid recentering windows if it
13364 is not necessary; currently that causes some problems. */
13365
13366 static void
13367 redisplay_internal (void)
13368 {
13369 struct window *w = XWINDOW (selected_window);
13370 struct window *sw;
13371 struct frame *fr;
13372 bool pending;
13373 bool must_finish = false, match_p;
13374 struct text_pos tlbufpos, tlendpos;
13375 int number_of_visible_frames;
13376 ptrdiff_t count;
13377 struct frame *sf;
13378 bool polling_stopped_here = false;
13379 Lisp_Object tail, frame;
13380
13381 /* True means redisplay has to consider all windows on all
13382 frames. False, only selected_window is considered. */
13383 bool consider_all_windows_p;
13384
13385 /* True means redisplay has to redisplay the miniwindow. */
13386 bool update_miniwindow_p = false;
13387
13388 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13389
13390 /* No redisplay if running in batch mode or frame is not yet fully
13391 initialized, or redisplay is explicitly turned off by setting
13392 Vinhibit_redisplay. */
13393 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13394 || !NILP (Vinhibit_redisplay))
13395 return;
13396
13397 /* Don't examine these until after testing Vinhibit_redisplay.
13398 When Emacs is shutting down, perhaps because its connection to
13399 X has dropped, we should not look at them at all. */
13400 fr = XFRAME (w->frame);
13401 sf = SELECTED_FRAME ();
13402
13403 if (!fr->glyphs_initialized_p)
13404 return;
13405
13406 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13407 if (popup_activated ())
13408 return;
13409 #endif
13410
13411 /* I don't think this happens but let's be paranoid. */
13412 if (redisplaying_p)
13413 return;
13414
13415 /* Record a function that clears redisplaying_p
13416 when we leave this function. */
13417 count = SPECPDL_INDEX ();
13418 record_unwind_protect_void (unwind_redisplay);
13419 redisplaying_p = true;
13420 specbind (Qinhibit_free_realized_faces, Qnil);
13421
13422 /* Record this function, so it appears on the profiler's backtraces. */
13423 record_in_backtrace (Qredisplay_internal, 0, 0);
13424
13425 FOR_EACH_FRAME (tail, frame)
13426 XFRAME (frame)->already_hscrolled_p = false;
13427
13428 retry:
13429 /* Remember the currently selected window. */
13430 sw = w;
13431
13432 pending = false;
13433 forget_escape_and_glyphless_faces ();
13434
13435 inhibit_free_realized_faces = false;
13436
13437 /* If face_change, init_iterator will free all realized faces, which
13438 includes the faces referenced from current matrices. So, we
13439 can't reuse current matrices in this case. */
13440 if (face_change)
13441 windows_or_buffers_changed = 47;
13442
13443 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13444 && FRAME_TTY (sf)->previous_frame != sf)
13445 {
13446 /* Since frames on a single ASCII terminal share the same
13447 display area, displaying a different frame means redisplay
13448 the whole thing. */
13449 SET_FRAME_GARBAGED (sf);
13450 #ifndef DOS_NT
13451 set_tty_color_mode (FRAME_TTY (sf), sf);
13452 #endif
13453 FRAME_TTY (sf)->previous_frame = sf;
13454 }
13455
13456 /* Set the visible flags for all frames. Do this before checking for
13457 resized or garbaged frames; they want to know if their frames are
13458 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13459 number_of_visible_frames = 0;
13460
13461 FOR_EACH_FRAME (tail, frame)
13462 {
13463 struct frame *f = XFRAME (frame);
13464
13465 if (FRAME_VISIBLE_P (f))
13466 {
13467 ++number_of_visible_frames;
13468 /* Adjust matrices for visible frames only. */
13469 if (f->fonts_changed)
13470 {
13471 adjust_frame_glyphs (f);
13472 /* Disable all redisplay optimizations for this frame.
13473 This is because adjust_frame_glyphs resets the
13474 enabled_p flag for all glyph rows of all windows, so
13475 many optimizations will fail anyway, and some might
13476 fail to test that flag and do bogus things as
13477 result. */
13478 SET_FRAME_GARBAGED (f);
13479 f->fonts_changed = false;
13480 }
13481 /* If cursor type has been changed on the frame
13482 other than selected, consider all frames. */
13483 if (f != sf && f->cursor_type_changed)
13484 fset_redisplay (f);
13485 }
13486 clear_desired_matrices (f);
13487 }
13488
13489 /* Notice any pending interrupt request to change frame size. */
13490 do_pending_window_change (true);
13491
13492 /* do_pending_window_change could change the selected_window due to
13493 frame resizing which makes the selected window too small. */
13494 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13495 sw = w;
13496
13497 /* Clear frames marked as garbaged. */
13498 clear_garbaged_frames ();
13499
13500 /* Build menubar and tool-bar items. */
13501 if (NILP (Vmemory_full))
13502 prepare_menu_bars ();
13503
13504 reconsider_clip_changes (w);
13505
13506 /* In most cases selected window displays current buffer. */
13507 match_p = XBUFFER (w->contents) == current_buffer;
13508 if (match_p)
13509 {
13510 /* Detect case that we need to write or remove a star in the mode line. */
13511 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13512 w->update_mode_line = true;
13513
13514 if (mode_line_update_needed (w))
13515 w->update_mode_line = true;
13516
13517 /* If reconsider_clip_changes above decided that the narrowing
13518 in the current buffer changed, make sure all other windows
13519 showing that buffer will be redisplayed. */
13520 if (current_buffer->clip_changed)
13521 bset_update_mode_line (current_buffer);
13522 }
13523
13524 /* Normally the message* functions will have already displayed and
13525 updated the echo area, but the frame may have been trashed, or
13526 the update may have been preempted, so display the echo area
13527 again here. Checking message_cleared_p captures the case that
13528 the echo area should be cleared. */
13529 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13530 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13531 || (message_cleared_p
13532 && minibuf_level == 0
13533 /* If the mini-window is currently selected, this means the
13534 echo-area doesn't show through. */
13535 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13536 {
13537 echo_area_display (false);
13538
13539 if (message_cleared_p)
13540 update_miniwindow_p = true;
13541
13542 must_finish = true;
13543
13544 /* If we don't display the current message, don't clear the
13545 message_cleared_p flag, because, if we did, we wouldn't clear
13546 the echo area in the next redisplay which doesn't preserve
13547 the echo area. */
13548 if (!display_last_displayed_message_p)
13549 message_cleared_p = false;
13550 }
13551 else if (EQ (selected_window, minibuf_window)
13552 && (current_buffer->clip_changed || window_outdated (w))
13553 && resize_mini_window (w, false))
13554 {
13555 /* Resized active mini-window to fit the size of what it is
13556 showing if its contents might have changed. */
13557 must_finish = true;
13558
13559 /* If window configuration was changed, frames may have been
13560 marked garbaged. Clear them or we will experience
13561 surprises wrt scrolling. */
13562 clear_garbaged_frames ();
13563 }
13564
13565 if (windows_or_buffers_changed && !update_mode_lines)
13566 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13567 only the windows's contents needs to be refreshed, or whether the
13568 mode-lines also need a refresh. */
13569 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13570 ? REDISPLAY_SOME : 32);
13571
13572 /* If specs for an arrow have changed, do thorough redisplay
13573 to ensure we remove any arrow that should no longer exist. */
13574 if (overlay_arrows_changed_p ())
13575 /* Apparently, this is the only case where we update other windows,
13576 without updating other mode-lines. */
13577 windows_or_buffers_changed = 49;
13578
13579 consider_all_windows_p = (update_mode_lines
13580 || windows_or_buffers_changed);
13581
13582 #define AINC(a,i) \
13583 { \
13584 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13585 if (INTEGERP (entry)) \
13586 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13587 }
13588
13589 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13590 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13591
13592 /* Optimize the case that only the line containing the cursor in the
13593 selected window has changed. Variables starting with this_ are
13594 set in display_line and record information about the line
13595 containing the cursor. */
13596 tlbufpos = this_line_start_pos;
13597 tlendpos = this_line_end_pos;
13598 if (!consider_all_windows_p
13599 && CHARPOS (tlbufpos) > 0
13600 && !w->update_mode_line
13601 && !current_buffer->clip_changed
13602 && !current_buffer->prevent_redisplay_optimizations_p
13603 && FRAME_VISIBLE_P (XFRAME (w->frame))
13604 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13605 && !XFRAME (w->frame)->cursor_type_changed
13606 && !XFRAME (w->frame)->face_change
13607 /* Make sure recorded data applies to current buffer, etc. */
13608 && this_line_buffer == current_buffer
13609 && match_p
13610 && !w->force_start
13611 && !w->optional_new_start
13612 /* Point must be on the line that we have info recorded about. */
13613 && PT >= CHARPOS (tlbufpos)
13614 && PT <= Z - CHARPOS (tlendpos)
13615 /* All text outside that line, including its final newline,
13616 must be unchanged. */
13617 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13618 CHARPOS (tlendpos)))
13619 {
13620 if (CHARPOS (tlbufpos) > BEGV
13621 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13622 && (CHARPOS (tlbufpos) == ZV
13623 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13624 /* Former continuation line has disappeared by becoming empty. */
13625 goto cancel;
13626 else if (window_outdated (w) || MINI_WINDOW_P (w))
13627 {
13628 /* We have to handle the case of continuation around a
13629 wide-column character (see the comment in indent.c around
13630 line 1340).
13631
13632 For instance, in the following case:
13633
13634 -------- Insert --------
13635 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13636 J_I_ ==> J_I_ `^^' are cursors.
13637 ^^ ^^
13638 -------- --------
13639
13640 As we have to redraw the line above, we cannot use this
13641 optimization. */
13642
13643 struct it it;
13644 int line_height_before = this_line_pixel_height;
13645
13646 /* Note that start_display will handle the case that the
13647 line starting at tlbufpos is a continuation line. */
13648 start_display (&it, w, tlbufpos);
13649
13650 /* Implementation note: It this still necessary? */
13651 if (it.current_x != this_line_start_x)
13652 goto cancel;
13653
13654 TRACE ((stderr, "trying display optimization 1\n"));
13655 w->cursor.vpos = -1;
13656 overlay_arrow_seen = false;
13657 it.vpos = this_line_vpos;
13658 it.current_y = this_line_y;
13659 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13660 display_line (&it);
13661
13662 /* If line contains point, is not continued,
13663 and ends at same distance from eob as before, we win. */
13664 if (w->cursor.vpos >= 0
13665 /* Line is not continued, otherwise this_line_start_pos
13666 would have been set to 0 in display_line. */
13667 && CHARPOS (this_line_start_pos)
13668 /* Line ends as before. */
13669 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13670 /* Line has same height as before. Otherwise other lines
13671 would have to be shifted up or down. */
13672 && this_line_pixel_height == line_height_before)
13673 {
13674 /* If this is not the window's last line, we must adjust
13675 the charstarts of the lines below. */
13676 if (it.current_y < it.last_visible_y)
13677 {
13678 struct glyph_row *row
13679 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13680 ptrdiff_t delta, delta_bytes;
13681
13682 /* We used to distinguish between two cases here,
13683 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13684 when the line ends in a newline or the end of the
13685 buffer's accessible portion. But both cases did
13686 the same, so they were collapsed. */
13687 delta = (Z
13688 - CHARPOS (tlendpos)
13689 - MATRIX_ROW_START_CHARPOS (row));
13690 delta_bytes = (Z_BYTE
13691 - BYTEPOS (tlendpos)
13692 - MATRIX_ROW_START_BYTEPOS (row));
13693
13694 increment_matrix_positions (w->current_matrix,
13695 this_line_vpos + 1,
13696 w->current_matrix->nrows,
13697 delta, delta_bytes);
13698 }
13699
13700 /* If this row displays text now but previously didn't,
13701 or vice versa, w->window_end_vpos may have to be
13702 adjusted. */
13703 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13704 {
13705 if (w->window_end_vpos < this_line_vpos)
13706 w->window_end_vpos = this_line_vpos;
13707 }
13708 else if (w->window_end_vpos == this_line_vpos
13709 && this_line_vpos > 0)
13710 w->window_end_vpos = this_line_vpos - 1;
13711 w->window_end_valid = false;
13712
13713 /* Update hint: No need to try to scroll in update_window. */
13714 w->desired_matrix->no_scrolling_p = true;
13715
13716 #ifdef GLYPH_DEBUG
13717 *w->desired_matrix->method = 0;
13718 debug_method_add (w, "optimization 1");
13719 #endif
13720 #ifdef HAVE_WINDOW_SYSTEM
13721 update_window_fringes (w, false);
13722 #endif
13723 goto update;
13724 }
13725 else
13726 goto cancel;
13727 }
13728 else if (/* Cursor position hasn't changed. */
13729 PT == w->last_point
13730 /* Make sure the cursor was last displayed
13731 in this window. Otherwise we have to reposition it. */
13732
13733 /* PXW: Must be converted to pixels, probably. */
13734 && 0 <= w->cursor.vpos
13735 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13736 {
13737 if (!must_finish)
13738 {
13739 do_pending_window_change (true);
13740 /* If selected_window changed, redisplay again. */
13741 if (WINDOWP (selected_window)
13742 && (w = XWINDOW (selected_window)) != sw)
13743 goto retry;
13744
13745 /* We used to always goto end_of_redisplay here, but this
13746 isn't enough if we have a blinking cursor. */
13747 if (w->cursor_off_p == w->last_cursor_off_p)
13748 goto end_of_redisplay;
13749 }
13750 goto update;
13751 }
13752 /* If highlighting the region, or if the cursor is in the echo area,
13753 then we can't just move the cursor. */
13754 else if (NILP (Vshow_trailing_whitespace)
13755 && !cursor_in_echo_area)
13756 {
13757 struct it it;
13758 struct glyph_row *row;
13759
13760 /* Skip from tlbufpos to PT and see where it is. Note that
13761 PT may be in invisible text. If so, we will end at the
13762 next visible position. */
13763 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13764 NULL, DEFAULT_FACE_ID);
13765 it.current_x = this_line_start_x;
13766 it.current_y = this_line_y;
13767 it.vpos = this_line_vpos;
13768
13769 /* The call to move_it_to stops in front of PT, but
13770 moves over before-strings. */
13771 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13772
13773 if (it.vpos == this_line_vpos
13774 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13775 row->enabled_p))
13776 {
13777 eassert (this_line_vpos == it.vpos);
13778 eassert (this_line_y == it.current_y);
13779 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13780 #ifdef GLYPH_DEBUG
13781 *w->desired_matrix->method = 0;
13782 debug_method_add (w, "optimization 3");
13783 #endif
13784 goto update;
13785 }
13786 else
13787 goto cancel;
13788 }
13789
13790 cancel:
13791 /* Text changed drastically or point moved off of line. */
13792 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13793 }
13794
13795 CHARPOS (this_line_start_pos) = 0;
13796 ++clear_face_cache_count;
13797 #ifdef HAVE_WINDOW_SYSTEM
13798 ++clear_image_cache_count;
13799 #endif
13800
13801 /* Build desired matrices, and update the display. If
13802 consider_all_windows_p, do it for all windows on all frames that
13803 require redisplay, as specified by their 'redisplay' flag.
13804 Otherwise do it for selected_window, only. */
13805
13806 if (consider_all_windows_p)
13807 {
13808 FOR_EACH_FRAME (tail, frame)
13809 XFRAME (frame)->updated_p = false;
13810
13811 propagate_buffer_redisplay ();
13812
13813 FOR_EACH_FRAME (tail, frame)
13814 {
13815 struct frame *f = XFRAME (frame);
13816
13817 /* We don't have to do anything for unselected terminal
13818 frames. */
13819 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13820 && !EQ (FRAME_TTY (f)->top_frame, frame))
13821 continue;
13822
13823 retry_frame:
13824 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13825 {
13826 bool gcscrollbars
13827 /* Only GC scrollbars when we redisplay the whole frame. */
13828 = f->redisplay || !REDISPLAY_SOME_P ();
13829 bool f_redisplay_flag = f->redisplay;
13830 /* Mark all the scroll bars to be removed; we'll redeem
13831 the ones we want when we redisplay their windows. */
13832 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13833 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13834
13835 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13836 redisplay_windows (FRAME_ROOT_WINDOW (f));
13837 /* Remember that the invisible frames need to be redisplayed next
13838 time they're visible. */
13839 else if (!REDISPLAY_SOME_P ())
13840 f->redisplay = true;
13841
13842 /* The X error handler may have deleted that frame. */
13843 if (!FRAME_LIVE_P (f))
13844 continue;
13845
13846 /* Any scroll bars which redisplay_windows should have
13847 nuked should now go away. */
13848 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13849 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13850
13851 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13852 {
13853 /* If fonts changed on visible frame, display again. */
13854 if (f->fonts_changed)
13855 {
13856 adjust_frame_glyphs (f);
13857 /* Disable all redisplay optimizations for this
13858 frame. For the reasons, see the comment near
13859 the previous call to adjust_frame_glyphs above. */
13860 SET_FRAME_GARBAGED (f);
13861 f->fonts_changed = false;
13862 goto retry_frame;
13863 }
13864
13865 /* See if we have to hscroll. */
13866 if (!f->already_hscrolled_p)
13867 {
13868 f->already_hscrolled_p = true;
13869 if (hscroll_windows (f->root_window))
13870 goto retry_frame;
13871 }
13872
13873 /* If the frame's redisplay flag was not set before
13874 we went about redisplaying its windows, but it is
13875 set now, that means we employed some redisplay
13876 optimizations inside redisplay_windows, and
13877 bypassed producing some screen lines. But if
13878 f->redisplay is now set, it might mean the old
13879 faces are no longer valid (e.g., if redisplaying
13880 some window called some Lisp which defined a new
13881 face or redefined an existing face), so trying to
13882 use them in update_frame will segfault.
13883 Therefore, we must redisplay this frame. */
13884 if (!f_redisplay_flag && f->redisplay)
13885 goto retry_frame;
13886
13887 /* Prevent various kinds of signals during display
13888 update. stdio is not robust about handling
13889 signals, which can cause an apparent I/O error. */
13890 if (interrupt_input)
13891 unrequest_sigio ();
13892 STOP_POLLING;
13893
13894 pending |= update_frame (f, false, false);
13895 f->cursor_type_changed = false;
13896 f->updated_p = true;
13897 }
13898 }
13899 }
13900
13901 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13902
13903 if (!pending)
13904 {
13905 /* Do the mark_window_display_accurate after all windows have
13906 been redisplayed because this call resets flags in buffers
13907 which are needed for proper redisplay. */
13908 FOR_EACH_FRAME (tail, frame)
13909 {
13910 struct frame *f = XFRAME (frame);
13911 if (f->updated_p)
13912 {
13913 f->redisplay = false;
13914 mark_window_display_accurate (f->root_window, true);
13915 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13916 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13917 }
13918 }
13919 }
13920 }
13921 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13922 {
13923 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13924 struct frame *mini_frame;
13925
13926 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13927 /* Use list_of_error, not Qerror, so that
13928 we catch only errors and don't run the debugger. */
13929 internal_condition_case_1 (redisplay_window_1, selected_window,
13930 list_of_error,
13931 redisplay_window_error);
13932 if (update_miniwindow_p)
13933 internal_condition_case_1 (redisplay_window_1, mini_window,
13934 list_of_error,
13935 redisplay_window_error);
13936
13937 /* Compare desired and current matrices, perform output. */
13938
13939 update:
13940 /* If fonts changed, display again. Likewise if redisplay_window_1
13941 above caused some change (e.g., a change in faces) that requires
13942 considering the entire frame again. */
13943 if (sf->fonts_changed || sf->redisplay)
13944 {
13945 if (sf->redisplay)
13946 {
13947 /* Set this to force a more thorough redisplay.
13948 Otherwise, we might immediately loop back to the
13949 above "else-if" clause (since all the conditions that
13950 led here might still be true), and we will then
13951 infloop, because the selected-frame's redisplay flag
13952 is not (and cannot be) reset. */
13953 windows_or_buffers_changed = 50;
13954 }
13955 goto retry;
13956 }
13957
13958 /* Prevent freeing of realized faces, since desired matrices are
13959 pending that reference the faces we computed and cached. */
13960 inhibit_free_realized_faces = true;
13961
13962 /* Prevent various kinds of signals during display update.
13963 stdio is not robust about handling signals,
13964 which can cause an apparent I/O error. */
13965 if (interrupt_input)
13966 unrequest_sigio ();
13967 STOP_POLLING;
13968
13969 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13970 {
13971 if (hscroll_windows (selected_window))
13972 goto retry;
13973
13974 XWINDOW (selected_window)->must_be_updated_p = true;
13975 pending = update_frame (sf, false, false);
13976 sf->cursor_type_changed = false;
13977 }
13978
13979 /* We may have called echo_area_display at the top of this
13980 function. If the echo area is on another frame, that may
13981 have put text on a frame other than the selected one, so the
13982 above call to update_frame would not have caught it. Catch
13983 it here. */
13984 mini_window = FRAME_MINIBUF_WINDOW (sf);
13985 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13986
13987 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13988 {
13989 XWINDOW (mini_window)->must_be_updated_p = true;
13990 pending |= update_frame (mini_frame, false, false);
13991 mini_frame->cursor_type_changed = false;
13992 if (!pending && hscroll_windows (mini_window))
13993 goto retry;
13994 }
13995 }
13996
13997 /* If display was paused because of pending input, make sure we do a
13998 thorough update the next time. */
13999 if (pending)
14000 {
14001 /* Prevent the optimization at the beginning of
14002 redisplay_internal that tries a single-line update of the
14003 line containing the cursor in the selected window. */
14004 CHARPOS (this_line_start_pos) = 0;
14005
14006 /* Let the overlay arrow be updated the next time. */
14007 update_overlay_arrows (0);
14008
14009 /* If we pause after scrolling, some rows in the current
14010 matrices of some windows are not valid. */
14011 if (!WINDOW_FULL_WIDTH_P (w)
14012 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14013 update_mode_lines = 36;
14014 }
14015 else
14016 {
14017 if (!consider_all_windows_p)
14018 {
14019 /* This has already been done above if
14020 consider_all_windows_p is set. */
14021 if (XBUFFER (w->contents)->text->redisplay
14022 && buffer_window_count (XBUFFER (w->contents)) > 1)
14023 /* This can happen if b->text->redisplay was set during
14024 jit-lock. */
14025 propagate_buffer_redisplay ();
14026 mark_window_display_accurate_1 (w, true);
14027
14028 /* Say overlay arrows are up to date. */
14029 update_overlay_arrows (1);
14030
14031 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14032 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14033 }
14034
14035 update_mode_lines = 0;
14036 windows_or_buffers_changed = 0;
14037 }
14038
14039 /* Start SIGIO interrupts coming again. Having them off during the
14040 code above makes it less likely one will discard output, but not
14041 impossible, since there might be stuff in the system buffer here.
14042 But it is much hairier to try to do anything about that. */
14043 if (interrupt_input)
14044 request_sigio ();
14045 RESUME_POLLING;
14046
14047 /* If a frame has become visible which was not before, redisplay
14048 again, so that we display it. Expose events for such a frame
14049 (which it gets when becoming visible) don't call the parts of
14050 redisplay constructing glyphs, so simply exposing a frame won't
14051 display anything in this case. So, we have to display these
14052 frames here explicitly. */
14053 if (!pending)
14054 {
14055 int new_count = 0;
14056
14057 FOR_EACH_FRAME (tail, frame)
14058 {
14059 if (XFRAME (frame)->visible)
14060 new_count++;
14061 }
14062
14063 if (new_count != number_of_visible_frames)
14064 windows_or_buffers_changed = 52;
14065 }
14066
14067 /* Change frame size now if a change is pending. */
14068 do_pending_window_change (true);
14069
14070 /* If we just did a pending size change, or have additional
14071 visible frames, or selected_window changed, redisplay again. */
14072 if ((windows_or_buffers_changed && !pending)
14073 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14074 goto retry;
14075
14076 /* Clear the face and image caches.
14077
14078 We used to do this only if consider_all_windows_p. But the cache
14079 needs to be cleared if a timer creates images in the current
14080 buffer (e.g. the test case in Bug#6230). */
14081
14082 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14083 {
14084 clear_face_cache (false);
14085 clear_face_cache_count = 0;
14086 }
14087
14088 #ifdef HAVE_WINDOW_SYSTEM
14089 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14090 {
14091 clear_image_caches (Qnil);
14092 clear_image_cache_count = 0;
14093 }
14094 #endif /* HAVE_WINDOW_SYSTEM */
14095
14096 end_of_redisplay:
14097 #ifdef HAVE_NS
14098 ns_set_doc_edited ();
14099 #endif
14100 if (interrupt_input && interrupts_deferred)
14101 request_sigio ();
14102
14103 unbind_to (count, Qnil);
14104 RESUME_POLLING;
14105 }
14106
14107
14108 /* Redisplay, but leave alone any recent echo area message unless
14109 another message has been requested in its place.
14110
14111 This is useful in situations where you need to redisplay but no
14112 user action has occurred, making it inappropriate for the message
14113 area to be cleared. See tracking_off and
14114 wait_reading_process_output for examples of these situations.
14115
14116 FROM_WHERE is an integer saying from where this function was
14117 called. This is useful for debugging. */
14118
14119 void
14120 redisplay_preserve_echo_area (int from_where)
14121 {
14122 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14123
14124 if (!NILP (echo_area_buffer[1]))
14125 {
14126 /* We have a previously displayed message, but no current
14127 message. Redisplay the previous message. */
14128 display_last_displayed_message_p = true;
14129 redisplay_internal ();
14130 display_last_displayed_message_p = false;
14131 }
14132 else
14133 redisplay_internal ();
14134
14135 flush_frame (SELECTED_FRAME ());
14136 }
14137
14138
14139 /* Function registered with record_unwind_protect in redisplay_internal. */
14140
14141 static void
14142 unwind_redisplay (void)
14143 {
14144 redisplaying_p = false;
14145 }
14146
14147
14148 /* Mark the display of leaf window W as accurate or inaccurate.
14149 If ACCURATE_P, mark display of W as accurate.
14150 If !ACCURATE_P, arrange for W to be redisplayed the next
14151 time redisplay_internal is called. */
14152
14153 static void
14154 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14155 {
14156 struct buffer *b = XBUFFER (w->contents);
14157
14158 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14159 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14160 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14161
14162 if (accurate_p)
14163 {
14164 b->clip_changed = false;
14165 b->prevent_redisplay_optimizations_p = false;
14166 eassert (buffer_window_count (b) > 0);
14167 /* Resetting b->text->redisplay is problematic!
14168 In order to make it safer to do it here, redisplay_internal must
14169 have copied all b->text->redisplay to their respective windows. */
14170 b->text->redisplay = false;
14171
14172 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14173 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14174 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14175 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14176
14177 w->current_matrix->buffer = b;
14178 w->current_matrix->begv = BUF_BEGV (b);
14179 w->current_matrix->zv = BUF_ZV (b);
14180
14181 w->last_cursor_vpos = w->cursor.vpos;
14182 w->last_cursor_off_p = w->cursor_off_p;
14183
14184 if (w == XWINDOW (selected_window))
14185 w->last_point = BUF_PT (b);
14186 else
14187 w->last_point = marker_position (w->pointm);
14188
14189 w->window_end_valid = true;
14190 w->update_mode_line = false;
14191 }
14192
14193 w->redisplay = !accurate_p;
14194 }
14195
14196
14197 /* Mark the display of windows in the window tree rooted at WINDOW as
14198 accurate or inaccurate. If ACCURATE_P, mark display of
14199 windows as accurate. If !ACCURATE_P, arrange for windows to
14200 be redisplayed the next time redisplay_internal is called. */
14201
14202 void
14203 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14204 {
14205 struct window *w;
14206
14207 for (; !NILP (window); window = w->next)
14208 {
14209 w = XWINDOW (window);
14210 if (WINDOWP (w->contents))
14211 mark_window_display_accurate (w->contents, accurate_p);
14212 else
14213 mark_window_display_accurate_1 (w, accurate_p);
14214 }
14215
14216 if (accurate_p)
14217 update_overlay_arrows (1);
14218 else
14219 /* Force a thorough redisplay the next time by setting
14220 last_arrow_position and last_arrow_string to t, which is
14221 unequal to any useful value of Voverlay_arrow_... */
14222 update_overlay_arrows (-1);
14223 }
14224
14225
14226 /* Return value in display table DP (Lisp_Char_Table *) for character
14227 C. Since a display table doesn't have any parent, we don't have to
14228 follow parent. Do not call this function directly but use the
14229 macro DISP_CHAR_VECTOR. */
14230
14231 Lisp_Object
14232 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14233 {
14234 Lisp_Object val;
14235
14236 if (ASCII_CHAR_P (c))
14237 {
14238 val = dp->ascii;
14239 if (SUB_CHAR_TABLE_P (val))
14240 val = XSUB_CHAR_TABLE (val)->contents[c];
14241 }
14242 else
14243 {
14244 Lisp_Object table;
14245
14246 XSETCHAR_TABLE (table, dp);
14247 val = char_table_ref (table, c);
14248 }
14249 if (NILP (val))
14250 val = dp->defalt;
14251 return val;
14252 }
14253
14254
14255 \f
14256 /***********************************************************************
14257 Window Redisplay
14258 ***********************************************************************/
14259
14260 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14261
14262 static void
14263 redisplay_windows (Lisp_Object window)
14264 {
14265 while (!NILP (window))
14266 {
14267 struct window *w = XWINDOW (window);
14268
14269 if (WINDOWP (w->contents))
14270 redisplay_windows (w->contents);
14271 else if (BUFFERP (w->contents))
14272 {
14273 displayed_buffer = XBUFFER (w->contents);
14274 /* Use list_of_error, not Qerror, so that
14275 we catch only errors and don't run the debugger. */
14276 internal_condition_case_1 (redisplay_window_0, window,
14277 list_of_error,
14278 redisplay_window_error);
14279 }
14280
14281 window = w->next;
14282 }
14283 }
14284
14285 static Lisp_Object
14286 redisplay_window_error (Lisp_Object ignore)
14287 {
14288 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14289 return Qnil;
14290 }
14291
14292 static Lisp_Object
14293 redisplay_window_0 (Lisp_Object window)
14294 {
14295 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14296 redisplay_window (window, false);
14297 return Qnil;
14298 }
14299
14300 static Lisp_Object
14301 redisplay_window_1 (Lisp_Object window)
14302 {
14303 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14304 redisplay_window (window, true);
14305 return Qnil;
14306 }
14307 \f
14308
14309 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14310 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14311 which positions recorded in ROW differ from current buffer
14312 positions.
14313
14314 Return true iff cursor is on this row. */
14315
14316 static bool
14317 set_cursor_from_row (struct window *w, struct glyph_row *row,
14318 struct glyph_matrix *matrix,
14319 ptrdiff_t delta, ptrdiff_t delta_bytes,
14320 int dy, int dvpos)
14321 {
14322 struct glyph *glyph = row->glyphs[TEXT_AREA];
14323 struct glyph *end = glyph + row->used[TEXT_AREA];
14324 struct glyph *cursor = NULL;
14325 /* The last known character position in row. */
14326 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14327 int x = row->x;
14328 ptrdiff_t pt_old = PT - delta;
14329 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14330 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14331 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14332 /* A glyph beyond the edge of TEXT_AREA which we should never
14333 touch. */
14334 struct glyph *glyphs_end = end;
14335 /* True means we've found a match for cursor position, but that
14336 glyph has the avoid_cursor_p flag set. */
14337 bool match_with_avoid_cursor = false;
14338 /* True means we've seen at least one glyph that came from a
14339 display string. */
14340 bool string_seen = false;
14341 /* Largest and smallest buffer positions seen so far during scan of
14342 glyph row. */
14343 ptrdiff_t bpos_max = pos_before;
14344 ptrdiff_t bpos_min = pos_after;
14345 /* Last buffer position covered by an overlay string with an integer
14346 `cursor' property. */
14347 ptrdiff_t bpos_covered = 0;
14348 /* True means the display string on which to display the cursor
14349 comes from a text property, not from an overlay. */
14350 bool string_from_text_prop = false;
14351
14352 /* Don't even try doing anything if called for a mode-line or
14353 header-line row, since the rest of the code isn't prepared to
14354 deal with such calamities. */
14355 eassert (!row->mode_line_p);
14356 if (row->mode_line_p)
14357 return false;
14358
14359 /* Skip over glyphs not having an object at the start and the end of
14360 the row. These are special glyphs like truncation marks on
14361 terminal frames. */
14362 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14363 {
14364 if (!row->reversed_p)
14365 {
14366 while (glyph < end
14367 && NILP (glyph->object)
14368 && glyph->charpos < 0)
14369 {
14370 x += glyph->pixel_width;
14371 ++glyph;
14372 }
14373 while (end > glyph
14374 && NILP ((end - 1)->object)
14375 /* CHARPOS is zero for blanks and stretch glyphs
14376 inserted by extend_face_to_end_of_line. */
14377 && (end - 1)->charpos <= 0)
14378 --end;
14379 glyph_before = glyph - 1;
14380 glyph_after = end;
14381 }
14382 else
14383 {
14384 struct glyph *g;
14385
14386 /* If the glyph row is reversed, we need to process it from back
14387 to front, so swap the edge pointers. */
14388 glyphs_end = end = glyph - 1;
14389 glyph += row->used[TEXT_AREA] - 1;
14390
14391 while (glyph > end + 1
14392 && NILP (glyph->object)
14393 && glyph->charpos < 0)
14394 {
14395 --glyph;
14396 x -= glyph->pixel_width;
14397 }
14398 if (NILP (glyph->object) && glyph->charpos < 0)
14399 --glyph;
14400 /* By default, in reversed rows we put the cursor on the
14401 rightmost (first in the reading order) glyph. */
14402 for (g = end + 1; g < glyph; g++)
14403 x += g->pixel_width;
14404 while (end < glyph
14405 && NILP ((end + 1)->object)
14406 && (end + 1)->charpos <= 0)
14407 ++end;
14408 glyph_before = glyph + 1;
14409 glyph_after = end;
14410 }
14411 }
14412 else if (row->reversed_p)
14413 {
14414 /* In R2L rows that don't display text, put the cursor on the
14415 rightmost glyph. Case in point: an empty last line that is
14416 part of an R2L paragraph. */
14417 cursor = end - 1;
14418 /* Avoid placing the cursor on the last glyph of the row, where
14419 on terminal frames we hold the vertical border between
14420 adjacent windows. */
14421 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14422 && !WINDOW_RIGHTMOST_P (w)
14423 && cursor == row->glyphs[LAST_AREA] - 1)
14424 cursor--;
14425 x = -1; /* will be computed below, at label compute_x */
14426 }
14427
14428 /* Step 1: Try to find the glyph whose character position
14429 corresponds to point. If that's not possible, find 2 glyphs
14430 whose character positions are the closest to point, one before
14431 point, the other after it. */
14432 if (!row->reversed_p)
14433 while (/* not marched to end of glyph row */
14434 glyph < end
14435 /* glyph was not inserted by redisplay for internal purposes */
14436 && !NILP (glyph->object))
14437 {
14438 if (BUFFERP (glyph->object))
14439 {
14440 ptrdiff_t dpos = glyph->charpos - pt_old;
14441
14442 if (glyph->charpos > bpos_max)
14443 bpos_max = glyph->charpos;
14444 if (glyph->charpos < bpos_min)
14445 bpos_min = glyph->charpos;
14446 if (!glyph->avoid_cursor_p)
14447 {
14448 /* If we hit point, we've found the glyph on which to
14449 display the cursor. */
14450 if (dpos == 0)
14451 {
14452 match_with_avoid_cursor = false;
14453 break;
14454 }
14455 /* See if we've found a better approximation to
14456 POS_BEFORE or to POS_AFTER. */
14457 if (0 > dpos && dpos > pos_before - pt_old)
14458 {
14459 pos_before = glyph->charpos;
14460 glyph_before = glyph;
14461 }
14462 else if (0 < dpos && dpos < pos_after - pt_old)
14463 {
14464 pos_after = glyph->charpos;
14465 glyph_after = glyph;
14466 }
14467 }
14468 else if (dpos == 0)
14469 match_with_avoid_cursor = true;
14470 }
14471 else if (STRINGP (glyph->object))
14472 {
14473 Lisp_Object chprop;
14474 ptrdiff_t glyph_pos = glyph->charpos;
14475
14476 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14477 glyph->object);
14478 if (!NILP (chprop))
14479 {
14480 /* If the string came from a `display' text property,
14481 look up the buffer position of that property and
14482 use that position to update bpos_max, as if we
14483 actually saw such a position in one of the row's
14484 glyphs. This helps with supporting integer values
14485 of `cursor' property on the display string in
14486 situations where most or all of the row's buffer
14487 text is completely covered by display properties,
14488 so that no glyph with valid buffer positions is
14489 ever seen in the row. */
14490 ptrdiff_t prop_pos =
14491 string_buffer_position_lim (glyph->object, pos_before,
14492 pos_after, false);
14493
14494 if (prop_pos >= pos_before)
14495 bpos_max = prop_pos;
14496 }
14497 if (INTEGERP (chprop))
14498 {
14499 bpos_covered = bpos_max + XINT (chprop);
14500 /* If the `cursor' property covers buffer positions up
14501 to and including point, we should display cursor on
14502 this glyph. Note that, if a `cursor' property on one
14503 of the string's characters has an integer value, we
14504 will break out of the loop below _before_ we get to
14505 the position match above. IOW, integer values of
14506 the `cursor' property override the "exact match for
14507 point" strategy of positioning the cursor. */
14508 /* Implementation note: bpos_max == pt_old when, e.g.,
14509 we are in an empty line, where bpos_max is set to
14510 MATRIX_ROW_START_CHARPOS, see above. */
14511 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14512 {
14513 cursor = glyph;
14514 break;
14515 }
14516 }
14517
14518 string_seen = true;
14519 }
14520 x += glyph->pixel_width;
14521 ++glyph;
14522 }
14523 else if (glyph > end) /* row is reversed */
14524 while (!NILP (glyph->object))
14525 {
14526 if (BUFFERP (glyph->object))
14527 {
14528 ptrdiff_t dpos = glyph->charpos - pt_old;
14529
14530 if (glyph->charpos > bpos_max)
14531 bpos_max = glyph->charpos;
14532 if (glyph->charpos < bpos_min)
14533 bpos_min = glyph->charpos;
14534 if (!glyph->avoid_cursor_p)
14535 {
14536 if (dpos == 0)
14537 {
14538 match_with_avoid_cursor = false;
14539 break;
14540 }
14541 if (0 > dpos && dpos > pos_before - pt_old)
14542 {
14543 pos_before = glyph->charpos;
14544 glyph_before = glyph;
14545 }
14546 else if (0 < dpos && dpos < pos_after - pt_old)
14547 {
14548 pos_after = glyph->charpos;
14549 glyph_after = glyph;
14550 }
14551 }
14552 else if (dpos == 0)
14553 match_with_avoid_cursor = true;
14554 }
14555 else if (STRINGP (glyph->object))
14556 {
14557 Lisp_Object chprop;
14558 ptrdiff_t glyph_pos = glyph->charpos;
14559
14560 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14561 glyph->object);
14562 if (!NILP (chprop))
14563 {
14564 ptrdiff_t prop_pos =
14565 string_buffer_position_lim (glyph->object, pos_before,
14566 pos_after, false);
14567
14568 if (prop_pos >= pos_before)
14569 bpos_max = prop_pos;
14570 }
14571 if (INTEGERP (chprop))
14572 {
14573 bpos_covered = bpos_max + XINT (chprop);
14574 /* If the `cursor' property covers buffer positions up
14575 to and including point, we should display cursor on
14576 this glyph. */
14577 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14578 {
14579 cursor = glyph;
14580 break;
14581 }
14582 }
14583 string_seen = true;
14584 }
14585 --glyph;
14586 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14587 {
14588 x--; /* can't use any pixel_width */
14589 break;
14590 }
14591 x -= glyph->pixel_width;
14592 }
14593
14594 /* Step 2: If we didn't find an exact match for point, we need to
14595 look for a proper place to put the cursor among glyphs between
14596 GLYPH_BEFORE and GLYPH_AFTER. */
14597 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14598 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14599 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14600 {
14601 /* An empty line has a single glyph whose OBJECT is nil and
14602 whose CHARPOS is the position of a newline on that line.
14603 Note that on a TTY, there are more glyphs after that, which
14604 were produced by extend_face_to_end_of_line, but their
14605 CHARPOS is zero or negative. */
14606 bool empty_line_p =
14607 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14608 && NILP (glyph->object) && glyph->charpos > 0
14609 /* On a TTY, continued and truncated rows also have a glyph at
14610 their end whose OBJECT is nil and whose CHARPOS is
14611 positive (the continuation and truncation glyphs), but such
14612 rows are obviously not "empty". */
14613 && !(row->continued_p || row->truncated_on_right_p));
14614
14615 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14616 {
14617 ptrdiff_t ellipsis_pos;
14618
14619 /* Scan back over the ellipsis glyphs. */
14620 if (!row->reversed_p)
14621 {
14622 ellipsis_pos = (glyph - 1)->charpos;
14623 while (glyph > row->glyphs[TEXT_AREA]
14624 && (glyph - 1)->charpos == ellipsis_pos)
14625 glyph--, x -= glyph->pixel_width;
14626 /* That loop always goes one position too far, including
14627 the glyph before the ellipsis. So scan forward over
14628 that one. */
14629 x += glyph->pixel_width;
14630 glyph++;
14631 }
14632 else /* row is reversed */
14633 {
14634 ellipsis_pos = (glyph + 1)->charpos;
14635 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14636 && (glyph + 1)->charpos == ellipsis_pos)
14637 glyph++, x += glyph->pixel_width;
14638 x -= glyph->pixel_width;
14639 glyph--;
14640 }
14641 }
14642 else if (match_with_avoid_cursor)
14643 {
14644 cursor = glyph_after;
14645 x = -1;
14646 }
14647 else if (string_seen)
14648 {
14649 int incr = row->reversed_p ? -1 : +1;
14650
14651 /* Need to find the glyph that came out of a string which is
14652 present at point. That glyph is somewhere between
14653 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14654 positioned between POS_BEFORE and POS_AFTER in the
14655 buffer. */
14656 struct glyph *start, *stop;
14657 ptrdiff_t pos = pos_before;
14658
14659 x = -1;
14660
14661 /* If the row ends in a newline from a display string,
14662 reordering could have moved the glyphs belonging to the
14663 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14664 in this case we extend the search to the last glyph in
14665 the row that was not inserted by redisplay. */
14666 if (row->ends_in_newline_from_string_p)
14667 {
14668 glyph_after = end;
14669 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14670 }
14671
14672 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14673 correspond to POS_BEFORE and POS_AFTER, respectively. We
14674 need START and STOP in the order that corresponds to the
14675 row's direction as given by its reversed_p flag. If the
14676 directionality of characters between POS_BEFORE and
14677 POS_AFTER is the opposite of the row's base direction,
14678 these characters will have been reordered for display,
14679 and we need to reverse START and STOP. */
14680 if (!row->reversed_p)
14681 {
14682 start = min (glyph_before, glyph_after);
14683 stop = max (glyph_before, glyph_after);
14684 }
14685 else
14686 {
14687 start = max (glyph_before, glyph_after);
14688 stop = min (glyph_before, glyph_after);
14689 }
14690 for (glyph = start + incr;
14691 row->reversed_p ? glyph > stop : glyph < stop; )
14692 {
14693
14694 /* Any glyphs that come from the buffer are here because
14695 of bidi reordering. Skip them, and only pay
14696 attention to glyphs that came from some string. */
14697 if (STRINGP (glyph->object))
14698 {
14699 Lisp_Object str;
14700 ptrdiff_t tem;
14701 /* If the display property covers the newline, we
14702 need to search for it one position farther. */
14703 ptrdiff_t lim = pos_after
14704 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14705
14706 string_from_text_prop = false;
14707 str = glyph->object;
14708 tem = string_buffer_position_lim (str, pos, lim, false);
14709 if (tem == 0 /* from overlay */
14710 || pos <= tem)
14711 {
14712 /* If the string from which this glyph came is
14713 found in the buffer at point, or at position
14714 that is closer to point than pos_after, then
14715 we've found the glyph we've been looking for.
14716 If it comes from an overlay (tem == 0), and
14717 it has the `cursor' property on one of its
14718 glyphs, record that glyph as a candidate for
14719 displaying the cursor. (As in the
14720 unidirectional version, we will display the
14721 cursor on the last candidate we find.) */
14722 if (tem == 0
14723 || tem == pt_old
14724 || (tem - pt_old > 0 && tem < pos_after))
14725 {
14726 /* The glyphs from this string could have
14727 been reordered. Find the one with the
14728 smallest string position. Or there could
14729 be a character in the string with the
14730 `cursor' property, which means display
14731 cursor on that character's glyph. */
14732 ptrdiff_t strpos = glyph->charpos;
14733
14734 if (tem)
14735 {
14736 cursor = glyph;
14737 string_from_text_prop = true;
14738 }
14739 for ( ;
14740 (row->reversed_p ? glyph > stop : glyph < stop)
14741 && EQ (glyph->object, str);
14742 glyph += incr)
14743 {
14744 Lisp_Object cprop;
14745 ptrdiff_t gpos = glyph->charpos;
14746
14747 cprop = Fget_char_property (make_number (gpos),
14748 Qcursor,
14749 glyph->object);
14750 if (!NILP (cprop))
14751 {
14752 cursor = glyph;
14753 break;
14754 }
14755 if (tem && glyph->charpos < strpos)
14756 {
14757 strpos = glyph->charpos;
14758 cursor = glyph;
14759 }
14760 }
14761
14762 if (tem == pt_old
14763 || (tem - pt_old > 0 && tem < pos_after))
14764 goto compute_x;
14765 }
14766 if (tem)
14767 pos = tem + 1; /* don't find previous instances */
14768 }
14769 /* This string is not what we want; skip all of the
14770 glyphs that came from it. */
14771 while ((row->reversed_p ? glyph > stop : glyph < stop)
14772 && EQ (glyph->object, str))
14773 glyph += incr;
14774 }
14775 else
14776 glyph += incr;
14777 }
14778
14779 /* If we reached the end of the line, and END was from a string,
14780 the cursor is not on this line. */
14781 if (cursor == NULL
14782 && (row->reversed_p ? glyph <= end : glyph >= end)
14783 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14784 && STRINGP (end->object)
14785 && row->continued_p)
14786 return false;
14787 }
14788 /* A truncated row may not include PT among its character positions.
14789 Setting the cursor inside the scroll margin will trigger
14790 recalculation of hscroll in hscroll_window_tree. But if a
14791 display string covers point, defer to the string-handling
14792 code below to figure this out. */
14793 else if (row->truncated_on_left_p && pt_old < bpos_min)
14794 {
14795 cursor = glyph_before;
14796 x = -1;
14797 }
14798 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14799 /* Zero-width characters produce no glyphs. */
14800 || (!empty_line_p
14801 && (row->reversed_p
14802 ? glyph_after > glyphs_end
14803 : glyph_after < glyphs_end)))
14804 {
14805 cursor = glyph_after;
14806 x = -1;
14807 }
14808 }
14809
14810 compute_x:
14811 if (cursor != NULL)
14812 glyph = cursor;
14813 else if (glyph == glyphs_end
14814 && pos_before == pos_after
14815 && STRINGP ((row->reversed_p
14816 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14817 : row->glyphs[TEXT_AREA])->object))
14818 {
14819 /* If all the glyphs of this row came from strings, put the
14820 cursor on the first glyph of the row. This avoids having the
14821 cursor outside of the text area in this very rare and hard
14822 use case. */
14823 glyph =
14824 row->reversed_p
14825 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14826 : row->glyphs[TEXT_AREA];
14827 }
14828 if (x < 0)
14829 {
14830 struct glyph *g;
14831
14832 /* Need to compute x that corresponds to GLYPH. */
14833 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14834 {
14835 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14836 emacs_abort ();
14837 x += g->pixel_width;
14838 }
14839 }
14840
14841 /* ROW could be part of a continued line, which, under bidi
14842 reordering, might have other rows whose start and end charpos
14843 occlude point. Only set w->cursor if we found a better
14844 approximation to the cursor position than we have from previously
14845 examined candidate rows belonging to the same continued line. */
14846 if (/* We already have a candidate row. */
14847 w->cursor.vpos >= 0
14848 /* That candidate is not the row we are processing. */
14849 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14850 /* Make sure cursor.vpos specifies a row whose start and end
14851 charpos occlude point, and it is valid candidate for being a
14852 cursor-row. This is because some callers of this function
14853 leave cursor.vpos at the row where the cursor was displayed
14854 during the last redisplay cycle. */
14855 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14856 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14857 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14858 {
14859 struct glyph *g1
14860 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14861
14862 /* Don't consider glyphs that are outside TEXT_AREA. */
14863 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14864 return false;
14865 /* Keep the candidate whose buffer position is the closest to
14866 point or has the `cursor' property. */
14867 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14868 w->cursor.hpos >= 0
14869 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14870 && ((BUFFERP (g1->object)
14871 && (g1->charpos == pt_old /* An exact match always wins. */
14872 || (BUFFERP (glyph->object)
14873 && eabs (g1->charpos - pt_old)
14874 < eabs (glyph->charpos - pt_old))))
14875 /* Previous candidate is a glyph from a string that has
14876 a non-nil `cursor' property. */
14877 || (STRINGP (g1->object)
14878 && (!NILP (Fget_char_property (make_number (g1->charpos),
14879 Qcursor, g1->object))
14880 /* Previous candidate is from the same display
14881 string as this one, and the display string
14882 came from a text property. */
14883 || (EQ (g1->object, glyph->object)
14884 && string_from_text_prop)
14885 /* this candidate is from newline and its
14886 position is not an exact match */
14887 || (NILP (glyph->object)
14888 && glyph->charpos != pt_old)))))
14889 return false;
14890 /* If this candidate gives an exact match, use that. */
14891 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14892 /* If this candidate is a glyph created for the
14893 terminating newline of a line, and point is on that
14894 newline, it wins because it's an exact match. */
14895 || (!row->continued_p
14896 && NILP (glyph->object)
14897 && glyph->charpos == 0
14898 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14899 /* Otherwise, keep the candidate that comes from a row
14900 spanning less buffer positions. This may win when one or
14901 both candidate positions are on glyphs that came from
14902 display strings, for which we cannot compare buffer
14903 positions. */
14904 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14905 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14906 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14907 return false;
14908 }
14909 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14910 w->cursor.x = x;
14911 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14912 w->cursor.y = row->y + dy;
14913
14914 if (w == XWINDOW (selected_window))
14915 {
14916 if (!row->continued_p
14917 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14918 && row->x == 0)
14919 {
14920 this_line_buffer = XBUFFER (w->contents);
14921
14922 CHARPOS (this_line_start_pos)
14923 = MATRIX_ROW_START_CHARPOS (row) + delta;
14924 BYTEPOS (this_line_start_pos)
14925 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14926
14927 CHARPOS (this_line_end_pos)
14928 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14929 BYTEPOS (this_line_end_pos)
14930 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14931
14932 this_line_y = w->cursor.y;
14933 this_line_pixel_height = row->height;
14934 this_line_vpos = w->cursor.vpos;
14935 this_line_start_x = row->x;
14936 }
14937 else
14938 CHARPOS (this_line_start_pos) = 0;
14939 }
14940
14941 return true;
14942 }
14943
14944
14945 /* Run window scroll functions, if any, for WINDOW with new window
14946 start STARTP. Sets the window start of WINDOW to that position.
14947
14948 We assume that the window's buffer is really current. */
14949
14950 static struct text_pos
14951 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14952 {
14953 struct window *w = XWINDOW (window);
14954 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14955
14956 eassert (current_buffer == XBUFFER (w->contents));
14957
14958 if (!NILP (Vwindow_scroll_functions))
14959 {
14960 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14961 make_number (CHARPOS (startp)));
14962 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14963 /* In case the hook functions switch buffers. */
14964 set_buffer_internal (XBUFFER (w->contents));
14965 }
14966
14967 return startp;
14968 }
14969
14970
14971 /* Make sure the line containing the cursor is fully visible.
14972 A value of true means there is nothing to be done.
14973 (Either the line is fully visible, or it cannot be made so,
14974 or we cannot tell.)
14975
14976 If FORCE_P, return false even if partial visible cursor row
14977 is higher than window.
14978
14979 If CURRENT_MATRIX_P, use the information from the
14980 window's current glyph matrix; otherwise use the desired glyph
14981 matrix.
14982
14983 A value of false means the caller should do scrolling
14984 as if point had gone off the screen. */
14985
14986 static bool
14987 cursor_row_fully_visible_p (struct window *w, bool force_p,
14988 bool current_matrix_p)
14989 {
14990 struct glyph_matrix *matrix;
14991 struct glyph_row *row;
14992 int window_height;
14993
14994 if (!make_cursor_line_fully_visible_p)
14995 return true;
14996
14997 /* It's not always possible to find the cursor, e.g, when a window
14998 is full of overlay strings. Don't do anything in that case. */
14999 if (w->cursor.vpos < 0)
15000 return true;
15001
15002 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15003 row = MATRIX_ROW (matrix, w->cursor.vpos);
15004
15005 /* If the cursor row is not partially visible, there's nothing to do. */
15006 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15007 return true;
15008
15009 /* If the row the cursor is in is taller than the window's height,
15010 it's not clear what to do, so do nothing. */
15011 window_height = window_box_height (w);
15012 if (row->height >= window_height)
15013 {
15014 if (!force_p || MINI_WINDOW_P (w)
15015 || w->vscroll || w->cursor.vpos == 0)
15016 return true;
15017 }
15018 return false;
15019 }
15020
15021
15022 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15023 means only WINDOW is redisplayed in redisplay_internal.
15024 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15025 in redisplay_window to bring a partially visible line into view in
15026 the case that only the cursor has moved.
15027
15028 LAST_LINE_MISFIT should be true if we're scrolling because the
15029 last screen line's vertical height extends past the end of the screen.
15030
15031 Value is
15032
15033 1 if scrolling succeeded
15034
15035 0 if scrolling didn't find point.
15036
15037 -1 if new fonts have been loaded so that we must interrupt
15038 redisplay, adjust glyph matrices, and try again. */
15039
15040 enum
15041 {
15042 SCROLLING_SUCCESS,
15043 SCROLLING_FAILED,
15044 SCROLLING_NEED_LARGER_MATRICES
15045 };
15046
15047 /* If scroll-conservatively is more than this, never recenter.
15048
15049 If you change this, don't forget to update the doc string of
15050 `scroll-conservatively' and the Emacs manual. */
15051 #define SCROLL_LIMIT 100
15052
15053 static int
15054 try_scrolling (Lisp_Object window, bool just_this_one_p,
15055 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15056 bool temp_scroll_step, bool last_line_misfit)
15057 {
15058 struct window *w = XWINDOW (window);
15059 struct frame *f = XFRAME (w->frame);
15060 struct text_pos pos, startp;
15061 struct it it;
15062 int this_scroll_margin, scroll_max, rc, height;
15063 int dy = 0, amount_to_scroll = 0;
15064 bool scroll_down_p = false;
15065 int extra_scroll_margin_lines = last_line_misfit;
15066 Lisp_Object aggressive;
15067 /* We will never try scrolling more than this number of lines. */
15068 int scroll_limit = SCROLL_LIMIT;
15069 int frame_line_height = default_line_pixel_height (w);
15070 int window_total_lines
15071 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15072
15073 #ifdef GLYPH_DEBUG
15074 debug_method_add (w, "try_scrolling");
15075 #endif
15076
15077 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15078
15079 /* Compute scroll margin height in pixels. We scroll when point is
15080 within this distance from the top or bottom of the window. */
15081 if (scroll_margin > 0)
15082 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15083 * frame_line_height;
15084 else
15085 this_scroll_margin = 0;
15086
15087 /* Force arg_scroll_conservatively to have a reasonable value, to
15088 avoid scrolling too far away with slow move_it_* functions. Note
15089 that the user can supply scroll-conservatively equal to
15090 `most-positive-fixnum', which can be larger than INT_MAX. */
15091 if (arg_scroll_conservatively > scroll_limit)
15092 {
15093 arg_scroll_conservatively = scroll_limit + 1;
15094 scroll_max = scroll_limit * frame_line_height;
15095 }
15096 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15097 /* Compute how much we should try to scroll maximally to bring
15098 point into view. */
15099 scroll_max = (max (scroll_step,
15100 max (arg_scroll_conservatively, temp_scroll_step))
15101 * frame_line_height);
15102 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15103 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15104 /* We're trying to scroll because of aggressive scrolling but no
15105 scroll_step is set. Choose an arbitrary one. */
15106 scroll_max = 10 * frame_line_height;
15107 else
15108 scroll_max = 0;
15109
15110 too_near_end:
15111
15112 /* Decide whether to scroll down. */
15113 if (PT > CHARPOS (startp))
15114 {
15115 int scroll_margin_y;
15116
15117 /* Compute the pixel ypos of the scroll margin, then move IT to
15118 either that ypos or PT, whichever comes first. */
15119 start_display (&it, w, startp);
15120 scroll_margin_y = it.last_visible_y - this_scroll_margin
15121 - frame_line_height * extra_scroll_margin_lines;
15122 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15123 (MOVE_TO_POS | MOVE_TO_Y));
15124
15125 if (PT > CHARPOS (it.current.pos))
15126 {
15127 int y0 = line_bottom_y (&it);
15128 /* Compute how many pixels below window bottom to stop searching
15129 for PT. This avoids costly search for PT that is far away if
15130 the user limited scrolling by a small number of lines, but
15131 always finds PT if scroll_conservatively is set to a large
15132 number, such as most-positive-fixnum. */
15133 int slack = max (scroll_max, 10 * frame_line_height);
15134 int y_to_move = it.last_visible_y + slack;
15135
15136 /* Compute the distance from the scroll margin to PT or to
15137 the scroll limit, whichever comes first. This should
15138 include the height of the cursor line, to make that line
15139 fully visible. */
15140 move_it_to (&it, PT, -1, y_to_move,
15141 -1, MOVE_TO_POS | MOVE_TO_Y);
15142 dy = line_bottom_y (&it) - y0;
15143
15144 if (dy > scroll_max)
15145 return SCROLLING_FAILED;
15146
15147 if (dy > 0)
15148 scroll_down_p = true;
15149 }
15150 }
15151
15152 if (scroll_down_p)
15153 {
15154 /* Point is in or below the bottom scroll margin, so move the
15155 window start down. If scrolling conservatively, move it just
15156 enough down to make point visible. If scroll_step is set,
15157 move it down by scroll_step. */
15158 if (arg_scroll_conservatively)
15159 amount_to_scroll
15160 = min (max (dy, frame_line_height),
15161 frame_line_height * arg_scroll_conservatively);
15162 else if (scroll_step || temp_scroll_step)
15163 amount_to_scroll = scroll_max;
15164 else
15165 {
15166 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15167 height = WINDOW_BOX_TEXT_HEIGHT (w);
15168 if (NUMBERP (aggressive))
15169 {
15170 double float_amount = XFLOATINT (aggressive) * height;
15171 int aggressive_scroll = float_amount;
15172 if (aggressive_scroll == 0 && float_amount > 0)
15173 aggressive_scroll = 1;
15174 /* Don't let point enter the scroll margin near top of
15175 the window. This could happen if the value of
15176 scroll_up_aggressively is too large and there are
15177 non-zero margins, because scroll_up_aggressively
15178 means put point that fraction of window height
15179 _from_the_bottom_margin_. */
15180 if (aggressive_scroll + 2 * this_scroll_margin > height)
15181 aggressive_scroll = height - 2 * this_scroll_margin;
15182 amount_to_scroll = dy + aggressive_scroll;
15183 }
15184 }
15185
15186 if (amount_to_scroll <= 0)
15187 return SCROLLING_FAILED;
15188
15189 start_display (&it, w, startp);
15190 if (arg_scroll_conservatively <= scroll_limit)
15191 move_it_vertically (&it, amount_to_scroll);
15192 else
15193 {
15194 /* Extra precision for users who set scroll-conservatively
15195 to a large number: make sure the amount we scroll
15196 the window start is never less than amount_to_scroll,
15197 which was computed as distance from window bottom to
15198 point. This matters when lines at window top and lines
15199 below window bottom have different height. */
15200 struct it it1;
15201 void *it1data = NULL;
15202 /* We use a temporary it1 because line_bottom_y can modify
15203 its argument, if it moves one line down; see there. */
15204 int start_y;
15205
15206 SAVE_IT (it1, it, it1data);
15207 start_y = line_bottom_y (&it1);
15208 do {
15209 RESTORE_IT (&it, &it, it1data);
15210 move_it_by_lines (&it, 1);
15211 SAVE_IT (it1, it, it1data);
15212 } while (IT_CHARPOS (it) < ZV
15213 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15214 bidi_unshelve_cache (it1data, true);
15215 }
15216
15217 /* If STARTP is unchanged, move it down another screen line. */
15218 if (IT_CHARPOS (it) == CHARPOS (startp))
15219 move_it_by_lines (&it, 1);
15220 startp = it.current.pos;
15221 }
15222 else
15223 {
15224 struct text_pos scroll_margin_pos = startp;
15225 int y_offset = 0;
15226
15227 /* See if point is inside the scroll margin at the top of the
15228 window. */
15229 if (this_scroll_margin)
15230 {
15231 int y_start;
15232
15233 start_display (&it, w, startp);
15234 y_start = it.current_y;
15235 move_it_vertically (&it, this_scroll_margin);
15236 scroll_margin_pos = it.current.pos;
15237 /* If we didn't move enough before hitting ZV, request
15238 additional amount of scroll, to move point out of the
15239 scroll margin. */
15240 if (IT_CHARPOS (it) == ZV
15241 && it.current_y - y_start < this_scroll_margin)
15242 y_offset = this_scroll_margin - (it.current_y - y_start);
15243 }
15244
15245 if (PT < CHARPOS (scroll_margin_pos))
15246 {
15247 /* Point is in the scroll margin at the top of the window or
15248 above what is displayed in the window. */
15249 int y0, y_to_move;
15250
15251 /* Compute the vertical distance from PT to the scroll
15252 margin position. Move as far as scroll_max allows, or
15253 one screenful, or 10 screen lines, whichever is largest.
15254 Give up if distance is greater than scroll_max or if we
15255 didn't reach the scroll margin position. */
15256 SET_TEXT_POS (pos, PT, PT_BYTE);
15257 start_display (&it, w, pos);
15258 y0 = it.current_y;
15259 y_to_move = max (it.last_visible_y,
15260 max (scroll_max, 10 * frame_line_height));
15261 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15262 y_to_move, -1,
15263 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15264 dy = it.current_y - y0;
15265 if (dy > scroll_max
15266 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15267 return SCROLLING_FAILED;
15268
15269 /* Additional scroll for when ZV was too close to point. */
15270 dy += y_offset;
15271
15272 /* Compute new window start. */
15273 start_display (&it, w, startp);
15274
15275 if (arg_scroll_conservatively)
15276 amount_to_scroll = max (dy, frame_line_height
15277 * max (scroll_step, temp_scroll_step));
15278 else if (scroll_step || temp_scroll_step)
15279 amount_to_scroll = scroll_max;
15280 else
15281 {
15282 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15283 height = WINDOW_BOX_TEXT_HEIGHT (w);
15284 if (NUMBERP (aggressive))
15285 {
15286 double float_amount = XFLOATINT (aggressive) * height;
15287 int aggressive_scroll = float_amount;
15288 if (aggressive_scroll == 0 && float_amount > 0)
15289 aggressive_scroll = 1;
15290 /* Don't let point enter the scroll margin near
15291 bottom of the window, if the value of
15292 scroll_down_aggressively happens to be too
15293 large. */
15294 if (aggressive_scroll + 2 * this_scroll_margin > height)
15295 aggressive_scroll = height - 2 * this_scroll_margin;
15296 amount_to_scroll = dy + aggressive_scroll;
15297 }
15298 }
15299
15300 if (amount_to_scroll <= 0)
15301 return SCROLLING_FAILED;
15302
15303 move_it_vertically_backward (&it, amount_to_scroll);
15304 startp = it.current.pos;
15305 }
15306 }
15307
15308 /* Run window scroll functions. */
15309 startp = run_window_scroll_functions (window, startp);
15310
15311 /* Display the window. Give up if new fonts are loaded, or if point
15312 doesn't appear. */
15313 if (!try_window (window, startp, 0))
15314 rc = SCROLLING_NEED_LARGER_MATRICES;
15315 else if (w->cursor.vpos < 0)
15316 {
15317 clear_glyph_matrix (w->desired_matrix);
15318 rc = SCROLLING_FAILED;
15319 }
15320 else
15321 {
15322 /* Maybe forget recorded base line for line number display. */
15323 if (!just_this_one_p
15324 || current_buffer->clip_changed
15325 || BEG_UNCHANGED < CHARPOS (startp))
15326 w->base_line_number = 0;
15327
15328 /* If cursor ends up on a partially visible line,
15329 treat that as being off the bottom of the screen. */
15330 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15331 false)
15332 /* It's possible that the cursor is on the first line of the
15333 buffer, which is partially obscured due to a vscroll
15334 (Bug#7537). In that case, avoid looping forever. */
15335 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15336 {
15337 clear_glyph_matrix (w->desired_matrix);
15338 ++extra_scroll_margin_lines;
15339 goto too_near_end;
15340 }
15341 rc = SCROLLING_SUCCESS;
15342 }
15343
15344 return rc;
15345 }
15346
15347
15348 /* Compute a suitable window start for window W if display of W starts
15349 on a continuation line. Value is true if a new window start
15350 was computed.
15351
15352 The new window start will be computed, based on W's width, starting
15353 from the start of the continued line. It is the start of the
15354 screen line with the minimum distance from the old start W->start. */
15355
15356 static bool
15357 compute_window_start_on_continuation_line (struct window *w)
15358 {
15359 struct text_pos pos, start_pos;
15360 bool window_start_changed_p = false;
15361
15362 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15363
15364 /* If window start is on a continuation line... Window start may be
15365 < BEGV in case there's invisible text at the start of the
15366 buffer (M-x rmail, for example). */
15367 if (CHARPOS (start_pos) > BEGV
15368 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15369 {
15370 struct it it;
15371 struct glyph_row *row;
15372
15373 /* Handle the case that the window start is out of range. */
15374 if (CHARPOS (start_pos) < BEGV)
15375 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15376 else if (CHARPOS (start_pos) > ZV)
15377 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15378
15379 /* Find the start of the continued line. This should be fast
15380 because find_newline is fast (newline cache). */
15381 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15382 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15383 row, DEFAULT_FACE_ID);
15384 reseat_at_previous_visible_line_start (&it);
15385
15386 /* If the line start is "too far" away from the window start,
15387 say it takes too much time to compute a new window start. */
15388 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15389 /* PXW: Do we need upper bounds here? */
15390 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15391 {
15392 int min_distance, distance;
15393
15394 /* Move forward by display lines to find the new window
15395 start. If window width was enlarged, the new start can
15396 be expected to be > the old start. If window width was
15397 decreased, the new window start will be < the old start.
15398 So, we're looking for the display line start with the
15399 minimum distance from the old window start. */
15400 pos = it.current.pos;
15401 min_distance = INFINITY;
15402 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15403 distance < min_distance)
15404 {
15405 min_distance = distance;
15406 pos = it.current.pos;
15407 if (it.line_wrap == WORD_WRAP)
15408 {
15409 /* Under WORD_WRAP, move_it_by_lines is likely to
15410 overshoot and stop not at the first, but the
15411 second character from the left margin. So in
15412 that case, we need a more tight control on the X
15413 coordinate of the iterator than move_it_by_lines
15414 promises in its contract. The method is to first
15415 go to the last (rightmost) visible character of a
15416 line, then move to the leftmost character on the
15417 next line in a separate call. */
15418 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15419 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15420 move_it_to (&it, ZV, 0,
15421 it.current_y + it.max_ascent + it.max_descent, -1,
15422 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15423 }
15424 else
15425 move_it_by_lines (&it, 1);
15426 }
15427
15428 /* Set the window start there. */
15429 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15430 window_start_changed_p = true;
15431 }
15432 }
15433
15434 return window_start_changed_p;
15435 }
15436
15437
15438 /* Try cursor movement in case text has not changed in window WINDOW,
15439 with window start STARTP. Value is
15440
15441 CURSOR_MOVEMENT_SUCCESS if successful
15442
15443 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15444
15445 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15446 display. *SCROLL_STEP is set to true, under certain circumstances, if
15447 we want to scroll as if scroll-step were set to 1. See the code.
15448
15449 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15450 which case we have to abort this redisplay, and adjust matrices
15451 first. */
15452
15453 enum
15454 {
15455 CURSOR_MOVEMENT_SUCCESS,
15456 CURSOR_MOVEMENT_CANNOT_BE_USED,
15457 CURSOR_MOVEMENT_MUST_SCROLL,
15458 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15459 };
15460
15461 static int
15462 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15463 bool *scroll_step)
15464 {
15465 struct window *w = XWINDOW (window);
15466 struct frame *f = XFRAME (w->frame);
15467 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15468
15469 #ifdef GLYPH_DEBUG
15470 if (inhibit_try_cursor_movement)
15471 return rc;
15472 #endif
15473
15474 /* Previously, there was a check for Lisp integer in the
15475 if-statement below. Now, this field is converted to
15476 ptrdiff_t, thus zero means invalid position in a buffer. */
15477 eassert (w->last_point > 0);
15478 /* Likewise there was a check whether window_end_vpos is nil or larger
15479 than the window. Now window_end_vpos is int and so never nil, but
15480 let's leave eassert to check whether it fits in the window. */
15481 eassert (!w->window_end_valid
15482 || w->window_end_vpos < w->current_matrix->nrows);
15483
15484 /* Handle case where text has not changed, only point, and it has
15485 not moved off the frame. */
15486 if (/* Point may be in this window. */
15487 PT >= CHARPOS (startp)
15488 /* Selective display hasn't changed. */
15489 && !current_buffer->clip_changed
15490 /* Function force-mode-line-update is used to force a thorough
15491 redisplay. It sets either windows_or_buffers_changed or
15492 update_mode_lines. So don't take a shortcut here for these
15493 cases. */
15494 && !update_mode_lines
15495 && !windows_or_buffers_changed
15496 && !f->cursor_type_changed
15497 && NILP (Vshow_trailing_whitespace)
15498 /* This code is not used for mini-buffer for the sake of the case
15499 of redisplaying to replace an echo area message; since in
15500 that case the mini-buffer contents per se are usually
15501 unchanged. This code is of no real use in the mini-buffer
15502 since the handling of this_line_start_pos, etc., in redisplay
15503 handles the same cases. */
15504 && !EQ (window, minibuf_window)
15505 && (FRAME_WINDOW_P (f)
15506 || !overlay_arrow_in_current_buffer_p ()))
15507 {
15508 int this_scroll_margin, top_scroll_margin;
15509 struct glyph_row *row = NULL;
15510 int frame_line_height = default_line_pixel_height (w);
15511 int window_total_lines
15512 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15513
15514 #ifdef GLYPH_DEBUG
15515 debug_method_add (w, "cursor movement");
15516 #endif
15517
15518 /* Scroll if point within this distance from the top or bottom
15519 of the window. This is a pixel value. */
15520 if (scroll_margin > 0)
15521 {
15522 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15523 this_scroll_margin *= frame_line_height;
15524 }
15525 else
15526 this_scroll_margin = 0;
15527
15528 top_scroll_margin = this_scroll_margin;
15529 if (WINDOW_WANTS_HEADER_LINE_P (w))
15530 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15531
15532 /* Start with the row the cursor was displayed during the last
15533 not paused redisplay. Give up if that row is not valid. */
15534 if (w->last_cursor_vpos < 0
15535 || w->last_cursor_vpos >= w->current_matrix->nrows)
15536 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15537 else
15538 {
15539 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15540 if (row->mode_line_p)
15541 ++row;
15542 if (!row->enabled_p)
15543 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15544 }
15545
15546 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15547 {
15548 bool scroll_p = false, must_scroll = false;
15549 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15550
15551 if (PT > w->last_point)
15552 {
15553 /* Point has moved forward. */
15554 while (MATRIX_ROW_END_CHARPOS (row) < PT
15555 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15556 {
15557 eassert (row->enabled_p);
15558 ++row;
15559 }
15560
15561 /* If the end position of a row equals the start
15562 position of the next row, and PT is at that position,
15563 we would rather display cursor in the next line. */
15564 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15565 && MATRIX_ROW_END_CHARPOS (row) == PT
15566 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15567 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15568 && !cursor_row_p (row))
15569 ++row;
15570
15571 /* If within the scroll margin, scroll. Note that
15572 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15573 the next line would be drawn, and that
15574 this_scroll_margin can be zero. */
15575 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15576 || PT > MATRIX_ROW_END_CHARPOS (row)
15577 /* Line is completely visible last line in window
15578 and PT is to be set in the next line. */
15579 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15580 && PT == MATRIX_ROW_END_CHARPOS (row)
15581 && !row->ends_at_zv_p
15582 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15583 scroll_p = true;
15584 }
15585 else if (PT < w->last_point)
15586 {
15587 /* Cursor has to be moved backward. Note that PT >=
15588 CHARPOS (startp) because of the outer if-statement. */
15589 while (!row->mode_line_p
15590 && (MATRIX_ROW_START_CHARPOS (row) > PT
15591 || (MATRIX_ROW_START_CHARPOS (row) == PT
15592 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15593 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15594 row > w->current_matrix->rows
15595 && (row-1)->ends_in_newline_from_string_p))))
15596 && (row->y > top_scroll_margin
15597 || CHARPOS (startp) == BEGV))
15598 {
15599 eassert (row->enabled_p);
15600 --row;
15601 }
15602
15603 /* Consider the following case: Window starts at BEGV,
15604 there is invisible, intangible text at BEGV, so that
15605 display starts at some point START > BEGV. It can
15606 happen that we are called with PT somewhere between
15607 BEGV and START. Try to handle that case. */
15608 if (row < w->current_matrix->rows
15609 || row->mode_line_p)
15610 {
15611 row = w->current_matrix->rows;
15612 if (row->mode_line_p)
15613 ++row;
15614 }
15615
15616 /* Due to newlines in overlay strings, we may have to
15617 skip forward over overlay strings. */
15618 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15619 && MATRIX_ROW_END_CHARPOS (row) == PT
15620 && !cursor_row_p (row))
15621 ++row;
15622
15623 /* If within the scroll margin, scroll. */
15624 if (row->y < top_scroll_margin
15625 && CHARPOS (startp) != BEGV)
15626 scroll_p = true;
15627 }
15628 else
15629 {
15630 /* Cursor did not move. So don't scroll even if cursor line
15631 is partially visible, as it was so before. */
15632 rc = CURSOR_MOVEMENT_SUCCESS;
15633 }
15634
15635 if (PT < MATRIX_ROW_START_CHARPOS (row)
15636 || PT > MATRIX_ROW_END_CHARPOS (row))
15637 {
15638 /* if PT is not in the glyph row, give up. */
15639 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15640 must_scroll = true;
15641 }
15642 else if (rc != CURSOR_MOVEMENT_SUCCESS
15643 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15644 {
15645 struct glyph_row *row1;
15646
15647 /* If rows are bidi-reordered and point moved, back up
15648 until we find a row that does not belong to a
15649 continuation line. This is because we must consider
15650 all rows of a continued line as candidates for the
15651 new cursor positioning, since row start and end
15652 positions change non-linearly with vertical position
15653 in such rows. */
15654 /* FIXME: Revisit this when glyph ``spilling'' in
15655 continuation lines' rows is implemented for
15656 bidi-reordered rows. */
15657 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15658 MATRIX_ROW_CONTINUATION_LINE_P (row);
15659 --row)
15660 {
15661 /* If we hit the beginning of the displayed portion
15662 without finding the first row of a continued
15663 line, give up. */
15664 if (row <= row1)
15665 {
15666 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15667 break;
15668 }
15669 eassert (row->enabled_p);
15670 }
15671 }
15672 if (must_scroll)
15673 ;
15674 else if (rc != CURSOR_MOVEMENT_SUCCESS
15675 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15676 /* Make sure this isn't a header line by any chance, since
15677 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15678 && !row->mode_line_p
15679 && make_cursor_line_fully_visible_p)
15680 {
15681 if (PT == MATRIX_ROW_END_CHARPOS (row)
15682 && !row->ends_at_zv_p
15683 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15684 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15685 else if (row->height > window_box_height (w))
15686 {
15687 /* If we end up in a partially visible line, let's
15688 make it fully visible, except when it's taller
15689 than the window, in which case we can't do much
15690 about it. */
15691 *scroll_step = true;
15692 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15693 }
15694 else
15695 {
15696 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15697 if (!cursor_row_fully_visible_p (w, false, true))
15698 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15699 else
15700 rc = CURSOR_MOVEMENT_SUCCESS;
15701 }
15702 }
15703 else if (scroll_p)
15704 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15705 else if (rc != CURSOR_MOVEMENT_SUCCESS
15706 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15707 {
15708 /* With bidi-reordered rows, there could be more than
15709 one candidate row whose start and end positions
15710 occlude point. We need to let set_cursor_from_row
15711 find the best candidate. */
15712 /* FIXME: Revisit this when glyph ``spilling'' in
15713 continuation lines' rows is implemented for
15714 bidi-reordered rows. */
15715 bool rv = false;
15716
15717 do
15718 {
15719 bool at_zv_p = false, exact_match_p = false;
15720
15721 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15722 && PT <= MATRIX_ROW_END_CHARPOS (row)
15723 && cursor_row_p (row))
15724 rv |= set_cursor_from_row (w, row, w->current_matrix,
15725 0, 0, 0, 0);
15726 /* As soon as we've found the exact match for point,
15727 or the first suitable row whose ends_at_zv_p flag
15728 is set, we are done. */
15729 if (rv)
15730 {
15731 at_zv_p = MATRIX_ROW (w->current_matrix,
15732 w->cursor.vpos)->ends_at_zv_p;
15733 if (!at_zv_p
15734 && w->cursor.hpos >= 0
15735 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15736 w->cursor.vpos))
15737 {
15738 struct glyph_row *candidate =
15739 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15740 struct glyph *g =
15741 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15742 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15743
15744 exact_match_p =
15745 (BUFFERP (g->object) && g->charpos == PT)
15746 || (NILP (g->object)
15747 && (g->charpos == PT
15748 || (g->charpos == 0 && endpos - 1 == PT)));
15749 }
15750 if (at_zv_p || exact_match_p)
15751 {
15752 rc = CURSOR_MOVEMENT_SUCCESS;
15753 break;
15754 }
15755 }
15756 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15757 break;
15758 ++row;
15759 }
15760 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15761 || row->continued_p)
15762 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15763 || (MATRIX_ROW_START_CHARPOS (row) == PT
15764 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15765 /* If we didn't find any candidate rows, or exited the
15766 loop before all the candidates were examined, signal
15767 to the caller that this method failed. */
15768 if (rc != CURSOR_MOVEMENT_SUCCESS
15769 && !(rv
15770 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15771 && !row->continued_p))
15772 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15773 else if (rv)
15774 rc = CURSOR_MOVEMENT_SUCCESS;
15775 }
15776 else
15777 {
15778 do
15779 {
15780 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15781 {
15782 rc = CURSOR_MOVEMENT_SUCCESS;
15783 break;
15784 }
15785 ++row;
15786 }
15787 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15788 && MATRIX_ROW_START_CHARPOS (row) == PT
15789 && cursor_row_p (row));
15790 }
15791 }
15792 }
15793
15794 return rc;
15795 }
15796
15797
15798 void
15799 set_vertical_scroll_bar (struct window *w)
15800 {
15801 ptrdiff_t start, end, whole;
15802
15803 /* Calculate the start and end positions for the current window.
15804 At some point, it would be nice to choose between scrollbars
15805 which reflect the whole buffer size, with special markers
15806 indicating narrowing, and scrollbars which reflect only the
15807 visible region.
15808
15809 Note that mini-buffers sometimes aren't displaying any text. */
15810 if (!MINI_WINDOW_P (w)
15811 || (w == XWINDOW (minibuf_window)
15812 && NILP (echo_area_buffer[0])))
15813 {
15814 struct buffer *buf = XBUFFER (w->contents);
15815 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15816 start = marker_position (w->start) - BUF_BEGV (buf);
15817 /* I don't think this is guaranteed to be right. For the
15818 moment, we'll pretend it is. */
15819 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15820
15821 if (end < start)
15822 end = start;
15823 if (whole < (end - start))
15824 whole = end - start;
15825 }
15826 else
15827 start = end = whole = 0;
15828
15829 /* Indicate what this scroll bar ought to be displaying now. */
15830 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15831 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15832 (w, end - start, whole, start);
15833 }
15834
15835
15836 void
15837 set_horizontal_scroll_bar (struct window *w)
15838 {
15839 int start, end, whole, portion;
15840
15841 if (!MINI_WINDOW_P (w)
15842 || (w == XWINDOW (minibuf_window)
15843 && NILP (echo_area_buffer[0])))
15844 {
15845 struct buffer *b = XBUFFER (w->contents);
15846 struct buffer *old_buffer = NULL;
15847 struct it it;
15848 struct text_pos startp;
15849
15850 if (b != current_buffer)
15851 {
15852 old_buffer = current_buffer;
15853 set_buffer_internal (b);
15854 }
15855
15856 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15857 start_display (&it, w, startp);
15858 it.last_visible_x = INT_MAX;
15859 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15860 MOVE_TO_X | MOVE_TO_Y);
15861 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15862 window_box_height (w), -1,
15863 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15864
15865 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15866 end = start + window_box_width (w, TEXT_AREA);
15867 portion = end - start;
15868 /* After enlarging a horizontally scrolled window such that it
15869 gets at least as wide as the text it contains, make sure that
15870 the thumb doesn't fill the entire scroll bar so we can still
15871 drag it back to see the entire text. */
15872 whole = max (whole, end);
15873
15874 if (it.bidi_p)
15875 {
15876 Lisp_Object pdir;
15877
15878 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15879 if (EQ (pdir, Qright_to_left))
15880 {
15881 start = whole - end;
15882 end = start + portion;
15883 }
15884 }
15885
15886 if (old_buffer)
15887 set_buffer_internal (old_buffer);
15888 }
15889 else
15890 start = end = whole = portion = 0;
15891
15892 w->hscroll_whole = whole;
15893
15894 /* Indicate what this scroll bar ought to be displaying now. */
15895 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15896 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15897 (w, portion, whole, start);
15898 }
15899
15900
15901 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
15902 selected_window is redisplayed.
15903
15904 We can return without actually redisplaying the window if fonts has been
15905 changed on window's frame. In that case, redisplay_internal will retry.
15906
15907 As one of the important parts of redisplaying a window, we need to
15908 decide whether the previous window-start position (stored in the
15909 window's w->start marker position) is still valid, and if it isn't,
15910 recompute it. Some details about that:
15911
15912 . The previous window-start could be in a continuation line, in
15913 which case we need to recompute it when the window width
15914 changes. See compute_window_start_on_continuation_line and its
15915 call below.
15916
15917 . The text that changed since last redisplay could include the
15918 previous window-start position. In that case, we try to salvage
15919 what we can from the current glyph matrix by calling
15920 try_scrolling, which see.
15921
15922 . Some Emacs command could force us to use a specific window-start
15923 position by setting the window's force_start flag, or gently
15924 propose doing that by setting the window's optional_new_start
15925 flag. In these cases, we try using the specified start point if
15926 that succeeds (i.e. the window desired matrix is successfully
15927 recomputed, and point location is within the window). In case
15928 of optional_new_start, we first check if the specified start
15929 position is feasible, i.e. if it will allow point to be
15930 displayed in the window. If using the specified start point
15931 fails, e.g., if new fonts are needed to be loaded, we abort the
15932 redisplay cycle and leave it up to the next cycle to figure out
15933 things.
15934
15935 . Note that the window's force_start flag is sometimes set by
15936 redisplay itself, when it decides that the previous window start
15937 point is fine and should be kept. Search for "goto force_start"
15938 below to see the details. Like the values of window-start
15939 specified outside of redisplay, these internally-deduced values
15940 are tested for feasibility, and ignored if found to be
15941 unfeasible.
15942
15943 . Note that the function try_window, used to completely redisplay
15944 a window, accepts the window's start point as its argument.
15945 This is used several times in the redisplay code to control
15946 where the window start will be, according to user options such
15947 as scroll-conservatively, and also to ensure the screen line
15948 showing point will be fully (as opposed to partially) visible on
15949 display. */
15950
15951 static void
15952 redisplay_window (Lisp_Object window, bool just_this_one_p)
15953 {
15954 struct window *w = XWINDOW (window);
15955 struct frame *f = XFRAME (w->frame);
15956 struct buffer *buffer = XBUFFER (w->contents);
15957 struct buffer *old = current_buffer;
15958 struct text_pos lpoint, opoint, startp;
15959 bool update_mode_line;
15960 int tem;
15961 struct it it;
15962 /* Record it now because it's overwritten. */
15963 bool current_matrix_up_to_date_p = false;
15964 bool used_current_matrix_p = false;
15965 /* This is less strict than current_matrix_up_to_date_p.
15966 It indicates that the buffer contents and narrowing are unchanged. */
15967 bool buffer_unchanged_p = false;
15968 bool temp_scroll_step = false;
15969 ptrdiff_t count = SPECPDL_INDEX ();
15970 int rc;
15971 int centering_position = -1;
15972 bool last_line_misfit = false;
15973 ptrdiff_t beg_unchanged, end_unchanged;
15974 int frame_line_height;
15975
15976 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15977 opoint = lpoint;
15978
15979 #ifdef GLYPH_DEBUG
15980 *w->desired_matrix->method = 0;
15981 #endif
15982
15983 if (!just_this_one_p
15984 && REDISPLAY_SOME_P ()
15985 && !w->redisplay
15986 && !w->update_mode_line
15987 && !f->face_change
15988 && !f->redisplay
15989 && !buffer->text->redisplay
15990 && BUF_PT (buffer) == w->last_point)
15991 return;
15992
15993 /* Make sure that both W's markers are valid. */
15994 eassert (XMARKER (w->start)->buffer == buffer);
15995 eassert (XMARKER (w->pointm)->buffer == buffer);
15996
15997 /* We come here again if we need to run window-text-change-functions
15998 below. */
15999 restart:
16000 reconsider_clip_changes (w);
16001 frame_line_height = default_line_pixel_height (w);
16002
16003 /* Has the mode line to be updated? */
16004 update_mode_line = (w->update_mode_line
16005 || update_mode_lines
16006 || buffer->clip_changed
16007 || buffer->prevent_redisplay_optimizations_p);
16008
16009 if (!just_this_one_p)
16010 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16011 cleverly elsewhere. */
16012 w->must_be_updated_p = true;
16013
16014 if (MINI_WINDOW_P (w))
16015 {
16016 if (w == XWINDOW (echo_area_window)
16017 && !NILP (echo_area_buffer[0]))
16018 {
16019 if (update_mode_line)
16020 /* We may have to update a tty frame's menu bar or a
16021 tool-bar. Example `M-x C-h C-h C-g'. */
16022 goto finish_menu_bars;
16023 else
16024 /* We've already displayed the echo area glyphs in this window. */
16025 goto finish_scroll_bars;
16026 }
16027 else if ((w != XWINDOW (minibuf_window)
16028 || minibuf_level == 0)
16029 /* When buffer is nonempty, redisplay window normally. */
16030 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16031 /* Quail displays non-mini buffers in minibuffer window.
16032 In that case, redisplay the window normally. */
16033 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16034 {
16035 /* W is a mini-buffer window, but it's not active, so clear
16036 it. */
16037 int yb = window_text_bottom_y (w);
16038 struct glyph_row *row;
16039 int y;
16040
16041 for (y = 0, row = w->desired_matrix->rows;
16042 y < yb;
16043 y += row->height, ++row)
16044 blank_row (w, row, y);
16045 goto finish_scroll_bars;
16046 }
16047
16048 clear_glyph_matrix (w->desired_matrix);
16049 }
16050
16051 /* Otherwise set up data on this window; select its buffer and point
16052 value. */
16053 /* Really select the buffer, for the sake of buffer-local
16054 variables. */
16055 set_buffer_internal_1 (XBUFFER (w->contents));
16056
16057 current_matrix_up_to_date_p
16058 = (w->window_end_valid
16059 && !current_buffer->clip_changed
16060 && !current_buffer->prevent_redisplay_optimizations_p
16061 && !window_outdated (w));
16062
16063 /* Run the window-text-change-functions
16064 if it is possible that the text on the screen has changed
16065 (either due to modification of the text, or any other reason). */
16066 if (!current_matrix_up_to_date_p
16067 && !NILP (Vwindow_text_change_functions))
16068 {
16069 safe_run_hooks (Qwindow_text_change_functions);
16070 goto restart;
16071 }
16072
16073 beg_unchanged = BEG_UNCHANGED;
16074 end_unchanged = END_UNCHANGED;
16075
16076 SET_TEXT_POS (opoint, PT, PT_BYTE);
16077
16078 specbind (Qinhibit_point_motion_hooks, Qt);
16079
16080 buffer_unchanged_p
16081 = (w->window_end_valid
16082 && !current_buffer->clip_changed
16083 && !window_outdated (w));
16084
16085 /* When windows_or_buffers_changed is non-zero, we can't rely
16086 on the window end being valid, so set it to zero there. */
16087 if (windows_or_buffers_changed)
16088 {
16089 /* If window starts on a continuation line, maybe adjust the
16090 window start in case the window's width changed. */
16091 if (XMARKER (w->start)->buffer == current_buffer)
16092 compute_window_start_on_continuation_line (w);
16093
16094 w->window_end_valid = false;
16095 /* If so, we also can't rely on current matrix
16096 and should not fool try_cursor_movement below. */
16097 current_matrix_up_to_date_p = false;
16098 }
16099
16100 /* Some sanity checks. */
16101 CHECK_WINDOW_END (w);
16102 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16103 emacs_abort ();
16104 if (BYTEPOS (opoint) < CHARPOS (opoint))
16105 emacs_abort ();
16106
16107 if (mode_line_update_needed (w))
16108 update_mode_line = true;
16109
16110 /* Point refers normally to the selected window. For any other
16111 window, set up appropriate value. */
16112 if (!EQ (window, selected_window))
16113 {
16114 ptrdiff_t new_pt = marker_position (w->pointm);
16115 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16116
16117 if (new_pt < BEGV)
16118 {
16119 new_pt = BEGV;
16120 new_pt_byte = BEGV_BYTE;
16121 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16122 }
16123 else if (new_pt > (ZV - 1))
16124 {
16125 new_pt = ZV;
16126 new_pt_byte = ZV_BYTE;
16127 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16128 }
16129
16130 /* We don't use SET_PT so that the point-motion hooks don't run. */
16131 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16132 }
16133
16134 /* If any of the character widths specified in the display table
16135 have changed, invalidate the width run cache. It's true that
16136 this may be a bit late to catch such changes, but the rest of
16137 redisplay goes (non-fatally) haywire when the display table is
16138 changed, so why should we worry about doing any better? */
16139 if (current_buffer->width_run_cache
16140 || (current_buffer->base_buffer
16141 && current_buffer->base_buffer->width_run_cache))
16142 {
16143 struct Lisp_Char_Table *disptab = buffer_display_table ();
16144
16145 if (! disptab_matches_widthtab
16146 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16147 {
16148 struct buffer *buf = current_buffer;
16149
16150 if (buf->base_buffer)
16151 buf = buf->base_buffer;
16152 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16153 recompute_width_table (current_buffer, disptab);
16154 }
16155 }
16156
16157 /* If window-start is screwed up, choose a new one. */
16158 if (XMARKER (w->start)->buffer != current_buffer)
16159 goto recenter;
16160
16161 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16162
16163 /* If someone specified a new starting point but did not insist,
16164 check whether it can be used. */
16165 if ((w->optional_new_start || window_frozen_p (w))
16166 && CHARPOS (startp) >= BEGV
16167 && CHARPOS (startp) <= ZV)
16168 {
16169 ptrdiff_t it_charpos;
16170
16171 w->optional_new_start = false;
16172 start_display (&it, w, startp);
16173 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16174 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16175 /* Record IT's position now, since line_bottom_y might change
16176 that. */
16177 it_charpos = IT_CHARPOS (it);
16178 /* Make sure we set the force_start flag only if the cursor row
16179 will be fully visible. Otherwise, the code under force_start
16180 label below will try to move point back into view, which is
16181 not what the code which sets optional_new_start wants. */
16182 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16183 && !w->force_start)
16184 {
16185 if (it_charpos == PT)
16186 w->force_start = true;
16187 /* IT may overshoot PT if text at PT is invisible. */
16188 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16189 w->force_start = true;
16190 #ifdef GLYPH_DEBUG
16191 if (w->force_start)
16192 {
16193 if (window_frozen_p (w))
16194 debug_method_add (w, "set force_start from frozen window start");
16195 else
16196 debug_method_add (w, "set force_start from optional_new_start");
16197 }
16198 #endif
16199 }
16200 }
16201
16202 force_start:
16203
16204 /* Handle case where place to start displaying has been specified,
16205 unless the specified location is outside the accessible range. */
16206 if (w->force_start)
16207 {
16208 /* We set this later on if we have to adjust point. */
16209 int new_vpos = -1;
16210
16211 w->force_start = false;
16212 w->vscroll = 0;
16213 w->window_end_valid = false;
16214
16215 /* Forget any recorded base line for line number display. */
16216 if (!buffer_unchanged_p)
16217 w->base_line_number = 0;
16218
16219 /* Redisplay the mode line. Select the buffer properly for that.
16220 Also, run the hook window-scroll-functions
16221 because we have scrolled. */
16222 /* Note, we do this after clearing force_start because
16223 if there's an error, it is better to forget about force_start
16224 than to get into an infinite loop calling the hook functions
16225 and having them get more errors. */
16226 if (!update_mode_line
16227 || ! NILP (Vwindow_scroll_functions))
16228 {
16229 update_mode_line = true;
16230 w->update_mode_line = true;
16231 startp = run_window_scroll_functions (window, startp);
16232 }
16233
16234 if (CHARPOS (startp) < BEGV)
16235 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16236 else if (CHARPOS (startp) > ZV)
16237 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16238
16239 /* Redisplay, then check if cursor has been set during the
16240 redisplay. Give up if new fonts were loaded. */
16241 /* We used to issue a CHECK_MARGINS argument to try_window here,
16242 but this causes scrolling to fail when point begins inside
16243 the scroll margin (bug#148) -- cyd */
16244 if (!try_window (window, startp, 0))
16245 {
16246 w->force_start = true;
16247 clear_glyph_matrix (w->desired_matrix);
16248 goto need_larger_matrices;
16249 }
16250
16251 if (w->cursor.vpos < 0)
16252 {
16253 /* If point does not appear, try to move point so it does
16254 appear. The desired matrix has been built above, so we
16255 can use it here. */
16256 new_vpos = window_box_height (w) / 2;
16257 }
16258
16259 if (!cursor_row_fully_visible_p (w, false, false))
16260 {
16261 /* Point does appear, but on a line partly visible at end of window.
16262 Move it back to a fully-visible line. */
16263 new_vpos = window_box_height (w);
16264 /* But if window_box_height suggests a Y coordinate that is
16265 not less than we already have, that line will clearly not
16266 be fully visible, so give up and scroll the display.
16267 This can happen when the default face uses a font whose
16268 dimensions are different from the frame's default
16269 font. */
16270 if (new_vpos >= w->cursor.y)
16271 {
16272 w->cursor.vpos = -1;
16273 clear_glyph_matrix (w->desired_matrix);
16274 goto try_to_scroll;
16275 }
16276 }
16277 else if (w->cursor.vpos >= 0)
16278 {
16279 /* Some people insist on not letting point enter the scroll
16280 margin, even though this part handles windows that didn't
16281 scroll at all. */
16282 int window_total_lines
16283 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16284 int margin = min (scroll_margin, window_total_lines / 4);
16285 int pixel_margin = margin * frame_line_height;
16286 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16287
16288 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16289 below, which finds the row to move point to, advances by
16290 the Y coordinate of the _next_ row, see the definition of
16291 MATRIX_ROW_BOTTOM_Y. */
16292 if (w->cursor.vpos < margin + header_line)
16293 {
16294 w->cursor.vpos = -1;
16295 clear_glyph_matrix (w->desired_matrix);
16296 goto try_to_scroll;
16297 }
16298 else
16299 {
16300 int window_height = window_box_height (w);
16301
16302 if (header_line)
16303 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16304 if (w->cursor.y >= window_height - pixel_margin)
16305 {
16306 w->cursor.vpos = -1;
16307 clear_glyph_matrix (w->desired_matrix);
16308 goto try_to_scroll;
16309 }
16310 }
16311 }
16312
16313 /* If we need to move point for either of the above reasons,
16314 now actually do it. */
16315 if (new_vpos >= 0)
16316 {
16317 struct glyph_row *row;
16318
16319 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16320 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16321 ++row;
16322
16323 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16324 MATRIX_ROW_START_BYTEPOS (row));
16325
16326 if (w != XWINDOW (selected_window))
16327 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16328 else if (current_buffer == old)
16329 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16330
16331 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16332
16333 /* Re-run pre-redisplay-function so it can update the region
16334 according to the new position of point. */
16335 /* Other than the cursor, w's redisplay is done so we can set its
16336 redisplay to false. Also the buffer's redisplay can be set to
16337 false, since propagate_buffer_redisplay should have already
16338 propagated its info to `w' anyway. */
16339 w->redisplay = false;
16340 XBUFFER (w->contents)->text->redisplay = false;
16341 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16342
16343 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16344 {
16345 /* pre-redisplay-function made changes (e.g. move the region)
16346 that require another round of redisplay. */
16347 clear_glyph_matrix (w->desired_matrix);
16348 if (!try_window (window, startp, 0))
16349 goto need_larger_matrices;
16350 }
16351 }
16352 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16353 {
16354 clear_glyph_matrix (w->desired_matrix);
16355 goto try_to_scroll;
16356 }
16357
16358 #ifdef GLYPH_DEBUG
16359 debug_method_add (w, "forced window start");
16360 #endif
16361 goto done;
16362 }
16363
16364 /* Handle case where text has not changed, only point, and it has
16365 not moved off the frame, and we are not retrying after hscroll.
16366 (current_matrix_up_to_date_p is true when retrying.) */
16367 if (current_matrix_up_to_date_p
16368 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16369 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16370 {
16371 switch (rc)
16372 {
16373 case CURSOR_MOVEMENT_SUCCESS:
16374 used_current_matrix_p = true;
16375 goto done;
16376
16377 case CURSOR_MOVEMENT_MUST_SCROLL:
16378 goto try_to_scroll;
16379
16380 default:
16381 emacs_abort ();
16382 }
16383 }
16384 /* If current starting point was originally the beginning of a line
16385 but no longer is, find a new starting point. */
16386 else if (w->start_at_line_beg
16387 && !(CHARPOS (startp) <= BEGV
16388 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16389 {
16390 #ifdef GLYPH_DEBUG
16391 debug_method_add (w, "recenter 1");
16392 #endif
16393 goto recenter;
16394 }
16395
16396 /* Try scrolling with try_window_id. Value is > 0 if update has
16397 been done, it is -1 if we know that the same window start will
16398 not work. It is 0 if unsuccessful for some other reason. */
16399 else if ((tem = try_window_id (w)) != 0)
16400 {
16401 #ifdef GLYPH_DEBUG
16402 debug_method_add (w, "try_window_id %d", tem);
16403 #endif
16404
16405 if (f->fonts_changed)
16406 goto need_larger_matrices;
16407 if (tem > 0)
16408 goto done;
16409
16410 /* Otherwise try_window_id has returned -1 which means that we
16411 don't want the alternative below this comment to execute. */
16412 }
16413 else if (CHARPOS (startp) >= BEGV
16414 && CHARPOS (startp) <= ZV
16415 && PT >= CHARPOS (startp)
16416 && (CHARPOS (startp) < ZV
16417 /* Avoid starting at end of buffer. */
16418 || CHARPOS (startp) == BEGV
16419 || !window_outdated (w)))
16420 {
16421 int d1, d2, d5, d6;
16422 int rtop, rbot;
16423
16424 /* If first window line is a continuation line, and window start
16425 is inside the modified region, but the first change is before
16426 current window start, we must select a new window start.
16427
16428 However, if this is the result of a down-mouse event (e.g. by
16429 extending the mouse-drag-overlay), we don't want to select a
16430 new window start, since that would change the position under
16431 the mouse, resulting in an unwanted mouse-movement rather
16432 than a simple mouse-click. */
16433 if (!w->start_at_line_beg
16434 && NILP (do_mouse_tracking)
16435 && CHARPOS (startp) > BEGV
16436 && CHARPOS (startp) > BEG + beg_unchanged
16437 && CHARPOS (startp) <= Z - end_unchanged
16438 /* Even if w->start_at_line_beg is nil, a new window may
16439 start at a line_beg, since that's how set_buffer_window
16440 sets it. So, we need to check the return value of
16441 compute_window_start_on_continuation_line. (See also
16442 bug#197). */
16443 && XMARKER (w->start)->buffer == current_buffer
16444 && compute_window_start_on_continuation_line (w)
16445 /* It doesn't make sense to force the window start like we
16446 do at label force_start if it is already known that point
16447 will not be fully visible in the resulting window, because
16448 doing so will move point from its correct position
16449 instead of scrolling the window to bring point into view.
16450 See bug#9324. */
16451 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16452 /* A very tall row could need more than the window height,
16453 in which case we accept that it is partially visible. */
16454 && (rtop != 0) == (rbot != 0))
16455 {
16456 w->force_start = true;
16457 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16458 #ifdef GLYPH_DEBUG
16459 debug_method_add (w, "recomputed window start in continuation line");
16460 #endif
16461 goto force_start;
16462 }
16463
16464 #ifdef GLYPH_DEBUG
16465 debug_method_add (w, "same window start");
16466 #endif
16467
16468 /* Try to redisplay starting at same place as before.
16469 If point has not moved off frame, accept the results. */
16470 if (!current_matrix_up_to_date_p
16471 /* Don't use try_window_reusing_current_matrix in this case
16472 because a window scroll function can have changed the
16473 buffer. */
16474 || !NILP (Vwindow_scroll_functions)
16475 || MINI_WINDOW_P (w)
16476 || !(used_current_matrix_p
16477 = try_window_reusing_current_matrix (w)))
16478 {
16479 IF_DEBUG (debug_method_add (w, "1"));
16480 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16481 /* -1 means we need to scroll.
16482 0 means we need new matrices, but fonts_changed
16483 is set in that case, so we will detect it below. */
16484 goto try_to_scroll;
16485 }
16486
16487 if (f->fonts_changed)
16488 goto need_larger_matrices;
16489
16490 if (w->cursor.vpos >= 0)
16491 {
16492 if (!just_this_one_p
16493 || current_buffer->clip_changed
16494 || BEG_UNCHANGED < CHARPOS (startp))
16495 /* Forget any recorded base line for line number display. */
16496 w->base_line_number = 0;
16497
16498 if (!cursor_row_fully_visible_p (w, true, false))
16499 {
16500 clear_glyph_matrix (w->desired_matrix);
16501 last_line_misfit = true;
16502 }
16503 /* Drop through and scroll. */
16504 else
16505 goto done;
16506 }
16507 else
16508 clear_glyph_matrix (w->desired_matrix);
16509 }
16510
16511 try_to_scroll:
16512
16513 /* Redisplay the mode line. Select the buffer properly for that. */
16514 if (!update_mode_line)
16515 {
16516 update_mode_line = true;
16517 w->update_mode_line = true;
16518 }
16519
16520 /* Try to scroll by specified few lines. */
16521 if ((scroll_conservatively
16522 || emacs_scroll_step
16523 || temp_scroll_step
16524 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16525 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16526 && CHARPOS (startp) >= BEGV
16527 && CHARPOS (startp) <= ZV)
16528 {
16529 /* The function returns -1 if new fonts were loaded, 1 if
16530 successful, 0 if not successful. */
16531 int ss = try_scrolling (window, just_this_one_p,
16532 scroll_conservatively,
16533 emacs_scroll_step,
16534 temp_scroll_step, last_line_misfit);
16535 switch (ss)
16536 {
16537 case SCROLLING_SUCCESS:
16538 goto done;
16539
16540 case SCROLLING_NEED_LARGER_MATRICES:
16541 goto need_larger_matrices;
16542
16543 case SCROLLING_FAILED:
16544 break;
16545
16546 default:
16547 emacs_abort ();
16548 }
16549 }
16550
16551 /* Finally, just choose a place to start which positions point
16552 according to user preferences. */
16553
16554 recenter:
16555
16556 #ifdef GLYPH_DEBUG
16557 debug_method_add (w, "recenter");
16558 #endif
16559
16560 /* Forget any previously recorded base line for line number display. */
16561 if (!buffer_unchanged_p)
16562 w->base_line_number = 0;
16563
16564 /* Determine the window start relative to point. */
16565 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16566 it.current_y = it.last_visible_y;
16567 if (centering_position < 0)
16568 {
16569 int window_total_lines
16570 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16571 int margin
16572 = scroll_margin > 0
16573 ? min (scroll_margin, window_total_lines / 4)
16574 : 0;
16575 ptrdiff_t margin_pos = CHARPOS (startp);
16576 Lisp_Object aggressive;
16577 bool scrolling_up;
16578
16579 /* If there is a scroll margin at the top of the window, find
16580 its character position. */
16581 if (margin
16582 /* Cannot call start_display if startp is not in the
16583 accessible region of the buffer. This can happen when we
16584 have just switched to a different buffer and/or changed
16585 its restriction. In that case, startp is initialized to
16586 the character position 1 (BEGV) because we did not yet
16587 have chance to display the buffer even once. */
16588 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16589 {
16590 struct it it1;
16591 void *it1data = NULL;
16592
16593 SAVE_IT (it1, it, it1data);
16594 start_display (&it1, w, startp);
16595 move_it_vertically (&it1, margin * frame_line_height);
16596 margin_pos = IT_CHARPOS (it1);
16597 RESTORE_IT (&it, &it, it1data);
16598 }
16599 scrolling_up = PT > margin_pos;
16600 aggressive =
16601 scrolling_up
16602 ? BVAR (current_buffer, scroll_up_aggressively)
16603 : BVAR (current_buffer, scroll_down_aggressively);
16604
16605 if (!MINI_WINDOW_P (w)
16606 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16607 {
16608 int pt_offset = 0;
16609
16610 /* Setting scroll-conservatively overrides
16611 scroll-*-aggressively. */
16612 if (!scroll_conservatively && NUMBERP (aggressive))
16613 {
16614 double float_amount = XFLOATINT (aggressive);
16615
16616 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16617 if (pt_offset == 0 && float_amount > 0)
16618 pt_offset = 1;
16619 if (pt_offset && margin > 0)
16620 margin -= 1;
16621 }
16622 /* Compute how much to move the window start backward from
16623 point so that point will be displayed where the user
16624 wants it. */
16625 if (scrolling_up)
16626 {
16627 centering_position = it.last_visible_y;
16628 if (pt_offset)
16629 centering_position -= pt_offset;
16630 centering_position -=
16631 (frame_line_height * (1 + margin + last_line_misfit)
16632 + WINDOW_HEADER_LINE_HEIGHT (w));
16633 /* Don't let point enter the scroll margin near top of
16634 the window. */
16635 if (centering_position < margin * frame_line_height)
16636 centering_position = margin * frame_line_height;
16637 }
16638 else
16639 centering_position = margin * frame_line_height + pt_offset;
16640 }
16641 else
16642 /* Set the window start half the height of the window backward
16643 from point. */
16644 centering_position = window_box_height (w) / 2;
16645 }
16646 move_it_vertically_backward (&it, centering_position);
16647
16648 eassert (IT_CHARPOS (it) >= BEGV);
16649
16650 /* The function move_it_vertically_backward may move over more
16651 than the specified y-distance. If it->w is small, e.g. a
16652 mini-buffer window, we may end up in front of the window's
16653 display area. Start displaying at the start of the line
16654 containing PT in this case. */
16655 if (it.current_y <= 0)
16656 {
16657 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16658 move_it_vertically_backward (&it, 0);
16659 it.current_y = 0;
16660 }
16661
16662 it.current_x = it.hpos = 0;
16663
16664 /* Set the window start position here explicitly, to avoid an
16665 infinite loop in case the functions in window-scroll-functions
16666 get errors. */
16667 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16668
16669 /* Run scroll hooks. */
16670 startp = run_window_scroll_functions (window, it.current.pos);
16671
16672 /* Redisplay the window. */
16673 if (!current_matrix_up_to_date_p
16674 || windows_or_buffers_changed
16675 || f->cursor_type_changed
16676 /* Don't use try_window_reusing_current_matrix in this case
16677 because it can have changed the buffer. */
16678 || !NILP (Vwindow_scroll_functions)
16679 || !just_this_one_p
16680 || MINI_WINDOW_P (w)
16681 || !(used_current_matrix_p
16682 = try_window_reusing_current_matrix (w)))
16683 try_window (window, startp, 0);
16684
16685 /* If new fonts have been loaded (due to fontsets), give up. We
16686 have to start a new redisplay since we need to re-adjust glyph
16687 matrices. */
16688 if (f->fonts_changed)
16689 goto need_larger_matrices;
16690
16691 /* If cursor did not appear assume that the middle of the window is
16692 in the first line of the window. Do it again with the next line.
16693 (Imagine a window of height 100, displaying two lines of height
16694 60. Moving back 50 from it->last_visible_y will end in the first
16695 line.) */
16696 if (w->cursor.vpos < 0)
16697 {
16698 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16699 {
16700 clear_glyph_matrix (w->desired_matrix);
16701 move_it_by_lines (&it, 1);
16702 try_window (window, it.current.pos, 0);
16703 }
16704 else if (PT < IT_CHARPOS (it))
16705 {
16706 clear_glyph_matrix (w->desired_matrix);
16707 move_it_by_lines (&it, -1);
16708 try_window (window, it.current.pos, 0);
16709 }
16710 else
16711 {
16712 /* Not much we can do about it. */
16713 }
16714 }
16715
16716 /* Consider the following case: Window starts at BEGV, there is
16717 invisible, intangible text at BEGV, so that display starts at
16718 some point START > BEGV. It can happen that we are called with
16719 PT somewhere between BEGV and START. Try to handle that case,
16720 and similar ones. */
16721 if (w->cursor.vpos < 0)
16722 {
16723 /* First, try locating the proper glyph row for PT. */
16724 struct glyph_row *row =
16725 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16726
16727 /* Sometimes point is at the beginning of invisible text that is
16728 before the 1st character displayed in the row. In that case,
16729 row_containing_pos fails to find the row, because no glyphs
16730 with appropriate buffer positions are present in the row.
16731 Therefore, we next try to find the row which shows the 1st
16732 position after the invisible text. */
16733 if (!row)
16734 {
16735 Lisp_Object val =
16736 get_char_property_and_overlay (make_number (PT), Qinvisible,
16737 Qnil, NULL);
16738
16739 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
16740 {
16741 ptrdiff_t alt_pos;
16742 Lisp_Object invis_end =
16743 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16744 Qnil, Qnil);
16745
16746 if (NATNUMP (invis_end))
16747 alt_pos = XFASTINT (invis_end);
16748 else
16749 alt_pos = ZV;
16750 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16751 NULL, 0);
16752 }
16753 }
16754 /* Finally, fall back on the first row of the window after the
16755 header line (if any). This is slightly better than not
16756 displaying the cursor at all. */
16757 if (!row)
16758 {
16759 row = w->current_matrix->rows;
16760 if (row->mode_line_p)
16761 ++row;
16762 }
16763 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16764 }
16765
16766 if (!cursor_row_fully_visible_p (w, false, false))
16767 {
16768 /* If vscroll is enabled, disable it and try again. */
16769 if (w->vscroll)
16770 {
16771 w->vscroll = 0;
16772 clear_glyph_matrix (w->desired_matrix);
16773 goto recenter;
16774 }
16775
16776 /* Users who set scroll-conservatively to a large number want
16777 point just above/below the scroll margin. If we ended up
16778 with point's row partially visible, move the window start to
16779 make that row fully visible and out of the margin. */
16780 if (scroll_conservatively > SCROLL_LIMIT)
16781 {
16782 int window_total_lines
16783 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16784 int margin =
16785 scroll_margin > 0
16786 ? min (scroll_margin, window_total_lines / 4)
16787 : 0;
16788 bool move_down = w->cursor.vpos >= window_total_lines / 2;
16789
16790 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16791 clear_glyph_matrix (w->desired_matrix);
16792 if (1 == try_window (window, it.current.pos,
16793 TRY_WINDOW_CHECK_MARGINS))
16794 goto done;
16795 }
16796
16797 /* If centering point failed to make the whole line visible,
16798 put point at the top instead. That has to make the whole line
16799 visible, if it can be done. */
16800 if (centering_position == 0)
16801 goto done;
16802
16803 clear_glyph_matrix (w->desired_matrix);
16804 centering_position = 0;
16805 goto recenter;
16806 }
16807
16808 done:
16809
16810 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16811 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16812 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16813
16814 /* Display the mode line, if we must. */
16815 if ((update_mode_line
16816 /* If window not full width, must redo its mode line
16817 if (a) the window to its side is being redone and
16818 (b) we do a frame-based redisplay. This is a consequence
16819 of how inverted lines are drawn in frame-based redisplay. */
16820 || (!just_this_one_p
16821 && !FRAME_WINDOW_P (f)
16822 && !WINDOW_FULL_WIDTH_P (w))
16823 /* Line number to display. */
16824 || w->base_line_pos > 0
16825 /* Column number is displayed and different from the one displayed. */
16826 || (w->column_number_displayed != -1
16827 && (w->column_number_displayed != current_column ())))
16828 /* This means that the window has a mode line. */
16829 && (WINDOW_WANTS_MODELINE_P (w)
16830 || WINDOW_WANTS_HEADER_LINE_P (w)))
16831 {
16832
16833 display_mode_lines (w);
16834
16835 /* If mode line height has changed, arrange for a thorough
16836 immediate redisplay using the correct mode line height. */
16837 if (WINDOW_WANTS_MODELINE_P (w)
16838 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16839 {
16840 f->fonts_changed = true;
16841 w->mode_line_height = -1;
16842 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16843 = DESIRED_MODE_LINE_HEIGHT (w);
16844 }
16845
16846 /* If header line height has changed, arrange for a thorough
16847 immediate redisplay using the correct header line height. */
16848 if (WINDOW_WANTS_HEADER_LINE_P (w)
16849 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16850 {
16851 f->fonts_changed = true;
16852 w->header_line_height = -1;
16853 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16854 = DESIRED_HEADER_LINE_HEIGHT (w);
16855 }
16856
16857 if (f->fonts_changed)
16858 goto need_larger_matrices;
16859 }
16860
16861 if (!line_number_displayed && w->base_line_pos != -1)
16862 {
16863 w->base_line_pos = 0;
16864 w->base_line_number = 0;
16865 }
16866
16867 finish_menu_bars:
16868
16869 /* When we reach a frame's selected window, redo the frame's menu
16870 bar and the frame's title. */
16871 if (update_mode_line
16872 && EQ (FRAME_SELECTED_WINDOW (f), window))
16873 {
16874 bool redisplay_menu_p;
16875
16876 if (FRAME_WINDOW_P (f))
16877 {
16878 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16879 || defined (HAVE_NS) || defined (USE_GTK)
16880 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16881 #else
16882 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16883 #endif
16884 }
16885 else
16886 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16887
16888 if (redisplay_menu_p)
16889 display_menu_bar (w);
16890
16891 #ifdef HAVE_WINDOW_SYSTEM
16892 if (FRAME_WINDOW_P (f))
16893 {
16894 #if defined (USE_GTK) || defined (HAVE_NS)
16895 if (FRAME_EXTERNAL_TOOL_BAR (f))
16896 redisplay_tool_bar (f);
16897 #else
16898 if (WINDOWP (f->tool_bar_window)
16899 && (FRAME_TOOL_BAR_LINES (f) > 0
16900 || !NILP (Vauto_resize_tool_bars))
16901 && redisplay_tool_bar (f))
16902 ignore_mouse_drag_p = true;
16903 #endif
16904 }
16905 x_consider_frame_title (w->frame);
16906 #endif
16907 }
16908
16909 #ifdef HAVE_WINDOW_SYSTEM
16910 if (FRAME_WINDOW_P (f)
16911 && update_window_fringes (w, (just_this_one_p
16912 || (!used_current_matrix_p && !overlay_arrow_seen)
16913 || w->pseudo_window_p)))
16914 {
16915 update_begin (f);
16916 block_input ();
16917 if (draw_window_fringes (w, true))
16918 {
16919 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16920 x_draw_right_divider (w);
16921 else
16922 x_draw_vertical_border (w);
16923 }
16924 unblock_input ();
16925 update_end (f);
16926 }
16927
16928 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16929 x_draw_bottom_divider (w);
16930 #endif /* HAVE_WINDOW_SYSTEM */
16931
16932 /* We go to this label, with fonts_changed set, if it is
16933 necessary to try again using larger glyph matrices.
16934 We have to redeem the scroll bar even in this case,
16935 because the loop in redisplay_internal expects that. */
16936 need_larger_matrices:
16937 ;
16938 finish_scroll_bars:
16939
16940 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16941 {
16942 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16943 /* Set the thumb's position and size. */
16944 set_vertical_scroll_bar (w);
16945
16946 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16947 /* Set the thumb's position and size. */
16948 set_horizontal_scroll_bar (w);
16949
16950 /* Note that we actually used the scroll bar attached to this
16951 window, so it shouldn't be deleted at the end of redisplay. */
16952 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16953 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16954 }
16955
16956 /* Restore current_buffer and value of point in it. The window
16957 update may have changed the buffer, so first make sure `opoint'
16958 is still valid (Bug#6177). */
16959 if (CHARPOS (opoint) < BEGV)
16960 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16961 else if (CHARPOS (opoint) > ZV)
16962 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16963 else
16964 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16965
16966 set_buffer_internal_1 (old);
16967 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16968 shorter. This can be caused by log truncation in *Messages*. */
16969 if (CHARPOS (lpoint) <= ZV)
16970 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16971
16972 unbind_to (count, Qnil);
16973 }
16974
16975
16976 /* Build the complete desired matrix of WINDOW with a window start
16977 buffer position POS.
16978
16979 Value is 1 if successful. It is zero if fonts were loaded during
16980 redisplay which makes re-adjusting glyph matrices necessary, and -1
16981 if point would appear in the scroll margins.
16982 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16983 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16984 set in FLAGS.) */
16985
16986 int
16987 try_window (Lisp_Object window, struct text_pos pos, int flags)
16988 {
16989 struct window *w = XWINDOW (window);
16990 struct it it;
16991 struct glyph_row *last_text_row = NULL;
16992 struct frame *f = XFRAME (w->frame);
16993 int frame_line_height = default_line_pixel_height (w);
16994
16995 /* Make POS the new window start. */
16996 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16997
16998 /* Mark cursor position as unknown. No overlay arrow seen. */
16999 w->cursor.vpos = -1;
17000 overlay_arrow_seen = false;
17001
17002 /* Initialize iterator and info to start at POS. */
17003 start_display (&it, w, pos);
17004 it.glyph_row->reversed_p = false;
17005
17006 /* Display all lines of W. */
17007 while (it.current_y < it.last_visible_y)
17008 {
17009 if (display_line (&it))
17010 last_text_row = it.glyph_row - 1;
17011 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17012 return 0;
17013 }
17014
17015 /* Don't let the cursor end in the scroll margins. */
17016 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17017 && !MINI_WINDOW_P (w))
17018 {
17019 int this_scroll_margin;
17020 int window_total_lines
17021 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17022
17023 if (scroll_margin > 0)
17024 {
17025 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17026 this_scroll_margin *= frame_line_height;
17027 }
17028 else
17029 this_scroll_margin = 0;
17030
17031 if ((w->cursor.y >= 0 /* not vscrolled */
17032 && w->cursor.y < this_scroll_margin
17033 && CHARPOS (pos) > BEGV
17034 && IT_CHARPOS (it) < ZV)
17035 /* rms: considering make_cursor_line_fully_visible_p here
17036 seems to give wrong results. We don't want to recenter
17037 when the last line is partly visible, we want to allow
17038 that case to be handled in the usual way. */
17039 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17040 {
17041 w->cursor.vpos = -1;
17042 clear_glyph_matrix (w->desired_matrix);
17043 return -1;
17044 }
17045 }
17046
17047 /* If bottom moved off end of frame, change mode line percentage. */
17048 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17049 w->update_mode_line = true;
17050
17051 /* Set window_end_pos to the offset of the last character displayed
17052 on the window from the end of current_buffer. Set
17053 window_end_vpos to its row number. */
17054 if (last_text_row)
17055 {
17056 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17057 adjust_window_ends (w, last_text_row, false);
17058 eassert
17059 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17060 w->window_end_vpos)));
17061 }
17062 else
17063 {
17064 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17065 w->window_end_pos = Z - ZV;
17066 w->window_end_vpos = 0;
17067 }
17068
17069 /* But that is not valid info until redisplay finishes. */
17070 w->window_end_valid = false;
17071 return 1;
17072 }
17073
17074
17075 \f
17076 /************************************************************************
17077 Window redisplay reusing current matrix when buffer has not changed
17078 ************************************************************************/
17079
17080 /* Try redisplay of window W showing an unchanged buffer with a
17081 different window start than the last time it was displayed by
17082 reusing its current matrix. Value is true if successful.
17083 W->start is the new window start. */
17084
17085 static bool
17086 try_window_reusing_current_matrix (struct window *w)
17087 {
17088 struct frame *f = XFRAME (w->frame);
17089 struct glyph_row *bottom_row;
17090 struct it it;
17091 struct run run;
17092 struct text_pos start, new_start;
17093 int nrows_scrolled, i;
17094 struct glyph_row *last_text_row;
17095 struct glyph_row *last_reused_text_row;
17096 struct glyph_row *start_row;
17097 int start_vpos, min_y, max_y;
17098
17099 #ifdef GLYPH_DEBUG
17100 if (inhibit_try_window_reusing)
17101 return false;
17102 #endif
17103
17104 if (/* This function doesn't handle terminal frames. */
17105 !FRAME_WINDOW_P (f)
17106 /* Don't try to reuse the display if windows have been split
17107 or such. */
17108 || windows_or_buffers_changed
17109 || f->cursor_type_changed)
17110 return false;
17111
17112 /* Can't do this if showing trailing whitespace. */
17113 if (!NILP (Vshow_trailing_whitespace))
17114 return false;
17115
17116 /* If top-line visibility has changed, give up. */
17117 if (WINDOW_WANTS_HEADER_LINE_P (w)
17118 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17119 return false;
17120
17121 /* Give up if old or new display is scrolled vertically. We could
17122 make this function handle this, but right now it doesn't. */
17123 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17124 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17125 return false;
17126
17127 /* The variable new_start now holds the new window start. The old
17128 start `start' can be determined from the current matrix. */
17129 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17130 start = start_row->minpos;
17131 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17132
17133 /* Clear the desired matrix for the display below. */
17134 clear_glyph_matrix (w->desired_matrix);
17135
17136 if (CHARPOS (new_start) <= CHARPOS (start))
17137 {
17138 /* Don't use this method if the display starts with an ellipsis
17139 displayed for invisible text. It's not easy to handle that case
17140 below, and it's certainly not worth the effort since this is
17141 not a frequent case. */
17142 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17143 return false;
17144
17145 IF_DEBUG (debug_method_add (w, "twu1"));
17146
17147 /* Display up to a row that can be reused. The variable
17148 last_text_row is set to the last row displayed that displays
17149 text. Note that it.vpos == 0 if or if not there is a
17150 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17151 start_display (&it, w, new_start);
17152 w->cursor.vpos = -1;
17153 last_text_row = last_reused_text_row = NULL;
17154
17155 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17156 {
17157 /* If we have reached into the characters in the START row,
17158 that means the line boundaries have changed. So we
17159 can't start copying with the row START. Maybe it will
17160 work to start copying with the following row. */
17161 while (IT_CHARPOS (it) > CHARPOS (start))
17162 {
17163 /* Advance to the next row as the "start". */
17164 start_row++;
17165 start = start_row->minpos;
17166 /* If there are no more rows to try, or just one, give up. */
17167 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17168 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17169 || CHARPOS (start) == ZV)
17170 {
17171 clear_glyph_matrix (w->desired_matrix);
17172 return false;
17173 }
17174
17175 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17176 }
17177 /* If we have reached alignment, we can copy the rest of the
17178 rows. */
17179 if (IT_CHARPOS (it) == CHARPOS (start)
17180 /* Don't accept "alignment" inside a display vector,
17181 since start_row could have started in the middle of
17182 that same display vector (thus their character
17183 positions match), and we have no way of telling if
17184 that is the case. */
17185 && it.current.dpvec_index < 0)
17186 break;
17187
17188 it.glyph_row->reversed_p = false;
17189 if (display_line (&it))
17190 last_text_row = it.glyph_row - 1;
17191
17192 }
17193
17194 /* A value of current_y < last_visible_y means that we stopped
17195 at the previous window start, which in turn means that we
17196 have at least one reusable row. */
17197 if (it.current_y < it.last_visible_y)
17198 {
17199 struct glyph_row *row;
17200
17201 /* IT.vpos always starts from 0; it counts text lines. */
17202 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17203
17204 /* Find PT if not already found in the lines displayed. */
17205 if (w->cursor.vpos < 0)
17206 {
17207 int dy = it.current_y - start_row->y;
17208
17209 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17210 row = row_containing_pos (w, PT, row, NULL, dy);
17211 if (row)
17212 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17213 dy, nrows_scrolled);
17214 else
17215 {
17216 clear_glyph_matrix (w->desired_matrix);
17217 return false;
17218 }
17219 }
17220
17221 /* Scroll the display. Do it before the current matrix is
17222 changed. The problem here is that update has not yet
17223 run, i.e. part of the current matrix is not up to date.
17224 scroll_run_hook will clear the cursor, and use the
17225 current matrix to get the height of the row the cursor is
17226 in. */
17227 run.current_y = start_row->y;
17228 run.desired_y = it.current_y;
17229 run.height = it.last_visible_y - it.current_y;
17230
17231 if (run.height > 0 && run.current_y != run.desired_y)
17232 {
17233 update_begin (f);
17234 FRAME_RIF (f)->update_window_begin_hook (w);
17235 FRAME_RIF (f)->clear_window_mouse_face (w);
17236 FRAME_RIF (f)->scroll_run_hook (w, &run);
17237 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17238 update_end (f);
17239 }
17240
17241 /* Shift current matrix down by nrows_scrolled lines. */
17242 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17243 rotate_matrix (w->current_matrix,
17244 start_vpos,
17245 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17246 nrows_scrolled);
17247
17248 /* Disable lines that must be updated. */
17249 for (i = 0; i < nrows_scrolled; ++i)
17250 (start_row + i)->enabled_p = false;
17251
17252 /* Re-compute Y positions. */
17253 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17254 max_y = it.last_visible_y;
17255 for (row = start_row + nrows_scrolled;
17256 row < bottom_row;
17257 ++row)
17258 {
17259 row->y = it.current_y;
17260 row->visible_height = row->height;
17261
17262 if (row->y < min_y)
17263 row->visible_height -= min_y - row->y;
17264 if (row->y + row->height > max_y)
17265 row->visible_height -= row->y + row->height - max_y;
17266 if (row->fringe_bitmap_periodic_p)
17267 row->redraw_fringe_bitmaps_p = true;
17268
17269 it.current_y += row->height;
17270
17271 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17272 last_reused_text_row = row;
17273 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17274 break;
17275 }
17276
17277 /* Disable lines in the current matrix which are now
17278 below the window. */
17279 for (++row; row < bottom_row; ++row)
17280 row->enabled_p = row->mode_line_p = false;
17281 }
17282
17283 /* Update window_end_pos etc.; last_reused_text_row is the last
17284 reused row from the current matrix containing text, if any.
17285 The value of last_text_row is the last displayed line
17286 containing text. */
17287 if (last_reused_text_row)
17288 adjust_window_ends (w, last_reused_text_row, true);
17289 else if (last_text_row)
17290 adjust_window_ends (w, last_text_row, false);
17291 else
17292 {
17293 /* This window must be completely empty. */
17294 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17295 w->window_end_pos = Z - ZV;
17296 w->window_end_vpos = 0;
17297 }
17298 w->window_end_valid = false;
17299
17300 /* Update hint: don't try scrolling again in update_window. */
17301 w->desired_matrix->no_scrolling_p = true;
17302
17303 #ifdef GLYPH_DEBUG
17304 debug_method_add (w, "try_window_reusing_current_matrix 1");
17305 #endif
17306 return true;
17307 }
17308 else if (CHARPOS (new_start) > CHARPOS (start))
17309 {
17310 struct glyph_row *pt_row, *row;
17311 struct glyph_row *first_reusable_row;
17312 struct glyph_row *first_row_to_display;
17313 int dy;
17314 int yb = window_text_bottom_y (w);
17315
17316 /* Find the row starting at new_start, if there is one. Don't
17317 reuse a partially visible line at the end. */
17318 first_reusable_row = start_row;
17319 while (first_reusable_row->enabled_p
17320 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17321 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17322 < CHARPOS (new_start)))
17323 ++first_reusable_row;
17324
17325 /* Give up if there is no row to reuse. */
17326 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17327 || !first_reusable_row->enabled_p
17328 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17329 != CHARPOS (new_start)))
17330 return false;
17331
17332 /* We can reuse fully visible rows beginning with
17333 first_reusable_row to the end of the window. Set
17334 first_row_to_display to the first row that cannot be reused.
17335 Set pt_row to the row containing point, if there is any. */
17336 pt_row = NULL;
17337 for (first_row_to_display = first_reusable_row;
17338 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17339 ++first_row_to_display)
17340 {
17341 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17342 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17343 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17344 && first_row_to_display->ends_at_zv_p
17345 && pt_row == NULL)))
17346 pt_row = first_row_to_display;
17347 }
17348
17349 /* Start displaying at the start of first_row_to_display. */
17350 eassert (first_row_to_display->y < yb);
17351 init_to_row_start (&it, w, first_row_to_display);
17352
17353 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17354 - start_vpos);
17355 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17356 - nrows_scrolled);
17357 it.current_y = (first_row_to_display->y - first_reusable_row->y
17358 + WINDOW_HEADER_LINE_HEIGHT (w));
17359
17360 /* Display lines beginning with first_row_to_display in the
17361 desired matrix. Set last_text_row to the last row displayed
17362 that displays text. */
17363 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17364 if (pt_row == NULL)
17365 w->cursor.vpos = -1;
17366 last_text_row = NULL;
17367 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17368 if (display_line (&it))
17369 last_text_row = it.glyph_row - 1;
17370
17371 /* If point is in a reused row, adjust y and vpos of the cursor
17372 position. */
17373 if (pt_row)
17374 {
17375 w->cursor.vpos -= nrows_scrolled;
17376 w->cursor.y -= first_reusable_row->y - start_row->y;
17377 }
17378
17379 /* Give up if point isn't in a row displayed or reused. (This
17380 also handles the case where w->cursor.vpos < nrows_scrolled
17381 after the calls to display_line, which can happen with scroll
17382 margins. See bug#1295.) */
17383 if (w->cursor.vpos < 0)
17384 {
17385 clear_glyph_matrix (w->desired_matrix);
17386 return false;
17387 }
17388
17389 /* Scroll the display. */
17390 run.current_y = first_reusable_row->y;
17391 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17392 run.height = it.last_visible_y - run.current_y;
17393 dy = run.current_y - run.desired_y;
17394
17395 if (run.height)
17396 {
17397 update_begin (f);
17398 FRAME_RIF (f)->update_window_begin_hook (w);
17399 FRAME_RIF (f)->clear_window_mouse_face (w);
17400 FRAME_RIF (f)->scroll_run_hook (w, &run);
17401 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17402 update_end (f);
17403 }
17404
17405 /* Adjust Y positions of reused rows. */
17406 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17407 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17408 max_y = it.last_visible_y;
17409 for (row = first_reusable_row; row < first_row_to_display; ++row)
17410 {
17411 row->y -= dy;
17412 row->visible_height = row->height;
17413 if (row->y < min_y)
17414 row->visible_height -= min_y - row->y;
17415 if (row->y + row->height > max_y)
17416 row->visible_height -= row->y + row->height - max_y;
17417 if (row->fringe_bitmap_periodic_p)
17418 row->redraw_fringe_bitmaps_p = true;
17419 }
17420
17421 /* Scroll the current matrix. */
17422 eassert (nrows_scrolled > 0);
17423 rotate_matrix (w->current_matrix,
17424 start_vpos,
17425 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17426 -nrows_scrolled);
17427
17428 /* Disable rows not reused. */
17429 for (row -= nrows_scrolled; row < bottom_row; ++row)
17430 row->enabled_p = false;
17431
17432 /* Point may have moved to a different line, so we cannot assume that
17433 the previous cursor position is valid; locate the correct row. */
17434 if (pt_row)
17435 {
17436 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17437 row < bottom_row
17438 && PT >= MATRIX_ROW_END_CHARPOS (row)
17439 && !row->ends_at_zv_p;
17440 row++)
17441 {
17442 w->cursor.vpos++;
17443 w->cursor.y = row->y;
17444 }
17445 if (row < bottom_row)
17446 {
17447 /* Can't simply scan the row for point with
17448 bidi-reordered glyph rows. Let set_cursor_from_row
17449 figure out where to put the cursor, and if it fails,
17450 give up. */
17451 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17452 {
17453 if (!set_cursor_from_row (w, row, w->current_matrix,
17454 0, 0, 0, 0))
17455 {
17456 clear_glyph_matrix (w->desired_matrix);
17457 return false;
17458 }
17459 }
17460 else
17461 {
17462 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17463 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17464
17465 for (; glyph < end
17466 && (!BUFFERP (glyph->object)
17467 || glyph->charpos < PT);
17468 glyph++)
17469 {
17470 w->cursor.hpos++;
17471 w->cursor.x += glyph->pixel_width;
17472 }
17473 }
17474 }
17475 }
17476
17477 /* Adjust window end. A null value of last_text_row means that
17478 the window end is in reused rows which in turn means that
17479 only its vpos can have changed. */
17480 if (last_text_row)
17481 adjust_window_ends (w, last_text_row, false);
17482 else
17483 w->window_end_vpos -= nrows_scrolled;
17484
17485 w->window_end_valid = false;
17486 w->desired_matrix->no_scrolling_p = true;
17487
17488 #ifdef GLYPH_DEBUG
17489 debug_method_add (w, "try_window_reusing_current_matrix 2");
17490 #endif
17491 return true;
17492 }
17493
17494 return false;
17495 }
17496
17497
17498 \f
17499 /************************************************************************
17500 Window redisplay reusing current matrix when buffer has changed
17501 ************************************************************************/
17502
17503 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17504 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17505 ptrdiff_t *, ptrdiff_t *);
17506 static struct glyph_row *
17507 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17508 struct glyph_row *);
17509
17510
17511 /* Return the last row in MATRIX displaying text. If row START is
17512 non-null, start searching with that row. IT gives the dimensions
17513 of the display. Value is null if matrix is empty; otherwise it is
17514 a pointer to the row found. */
17515
17516 static struct glyph_row *
17517 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17518 struct glyph_row *start)
17519 {
17520 struct glyph_row *row, *row_found;
17521
17522 /* Set row_found to the last row in IT->w's current matrix
17523 displaying text. The loop looks funny but think of partially
17524 visible lines. */
17525 row_found = NULL;
17526 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17527 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17528 {
17529 eassert (row->enabled_p);
17530 row_found = row;
17531 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17532 break;
17533 ++row;
17534 }
17535
17536 return row_found;
17537 }
17538
17539
17540 /* Return the last row in the current matrix of W that is not affected
17541 by changes at the start of current_buffer that occurred since W's
17542 current matrix was built. Value is null if no such row exists.
17543
17544 BEG_UNCHANGED us the number of characters unchanged at the start of
17545 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17546 first changed character in current_buffer. Characters at positions <
17547 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17548 when the current matrix was built. */
17549
17550 static struct glyph_row *
17551 find_last_unchanged_at_beg_row (struct window *w)
17552 {
17553 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17554 struct glyph_row *row;
17555 struct glyph_row *row_found = NULL;
17556 int yb = window_text_bottom_y (w);
17557
17558 /* Find the last row displaying unchanged text. */
17559 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17560 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17561 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17562 ++row)
17563 {
17564 if (/* If row ends before first_changed_pos, it is unchanged,
17565 except in some case. */
17566 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17567 /* When row ends in ZV and we write at ZV it is not
17568 unchanged. */
17569 && !row->ends_at_zv_p
17570 /* When first_changed_pos is the end of a continued line,
17571 row is not unchanged because it may be no longer
17572 continued. */
17573 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17574 && (row->continued_p
17575 || row->exact_window_width_line_p))
17576 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17577 needs to be recomputed, so don't consider this row as
17578 unchanged. This happens when the last line was
17579 bidi-reordered and was killed immediately before this
17580 redisplay cycle. In that case, ROW->end stores the
17581 buffer position of the first visual-order character of
17582 the killed text, which is now beyond ZV. */
17583 && CHARPOS (row->end.pos) <= ZV)
17584 row_found = row;
17585
17586 /* Stop if last visible row. */
17587 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17588 break;
17589 }
17590
17591 return row_found;
17592 }
17593
17594
17595 /* Find the first glyph row in the current matrix of W that is not
17596 affected by changes at the end of current_buffer since the
17597 time W's current matrix was built.
17598
17599 Return in *DELTA the number of chars by which buffer positions in
17600 unchanged text at the end of current_buffer must be adjusted.
17601
17602 Return in *DELTA_BYTES the corresponding number of bytes.
17603
17604 Value is null if no such row exists, i.e. all rows are affected by
17605 changes. */
17606
17607 static struct glyph_row *
17608 find_first_unchanged_at_end_row (struct window *w,
17609 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17610 {
17611 struct glyph_row *row;
17612 struct glyph_row *row_found = NULL;
17613
17614 *delta = *delta_bytes = 0;
17615
17616 /* Display must not have been paused, otherwise the current matrix
17617 is not up to date. */
17618 eassert (w->window_end_valid);
17619
17620 /* A value of window_end_pos >= END_UNCHANGED means that the window
17621 end is in the range of changed text. If so, there is no
17622 unchanged row at the end of W's current matrix. */
17623 if (w->window_end_pos >= END_UNCHANGED)
17624 return NULL;
17625
17626 /* Set row to the last row in W's current matrix displaying text. */
17627 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17628
17629 /* If matrix is entirely empty, no unchanged row exists. */
17630 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17631 {
17632 /* The value of row is the last glyph row in the matrix having a
17633 meaningful buffer position in it. The end position of row
17634 corresponds to window_end_pos. This allows us to translate
17635 buffer positions in the current matrix to current buffer
17636 positions for characters not in changed text. */
17637 ptrdiff_t Z_old =
17638 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17639 ptrdiff_t Z_BYTE_old =
17640 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17641 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17642 struct glyph_row *first_text_row
17643 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17644
17645 *delta = Z - Z_old;
17646 *delta_bytes = Z_BYTE - Z_BYTE_old;
17647
17648 /* Set last_unchanged_pos to the buffer position of the last
17649 character in the buffer that has not been changed. Z is the
17650 index + 1 of the last character in current_buffer, i.e. by
17651 subtracting END_UNCHANGED we get the index of the last
17652 unchanged character, and we have to add BEG to get its buffer
17653 position. */
17654 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17655 last_unchanged_pos_old = last_unchanged_pos - *delta;
17656
17657 /* Search backward from ROW for a row displaying a line that
17658 starts at a minimum position >= last_unchanged_pos_old. */
17659 for (; row > first_text_row; --row)
17660 {
17661 /* This used to abort, but it can happen.
17662 It is ok to just stop the search instead here. KFS. */
17663 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17664 break;
17665
17666 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17667 row_found = row;
17668 }
17669 }
17670
17671 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17672
17673 return row_found;
17674 }
17675
17676
17677 /* Make sure that glyph rows in the current matrix of window W
17678 reference the same glyph memory as corresponding rows in the
17679 frame's frame matrix. This function is called after scrolling W's
17680 current matrix on a terminal frame in try_window_id and
17681 try_window_reusing_current_matrix. */
17682
17683 static void
17684 sync_frame_with_window_matrix_rows (struct window *w)
17685 {
17686 struct frame *f = XFRAME (w->frame);
17687 struct glyph_row *window_row, *window_row_end, *frame_row;
17688
17689 /* Preconditions: W must be a leaf window and full-width. Its frame
17690 must have a frame matrix. */
17691 eassert (BUFFERP (w->contents));
17692 eassert (WINDOW_FULL_WIDTH_P (w));
17693 eassert (!FRAME_WINDOW_P (f));
17694
17695 /* If W is a full-width window, glyph pointers in W's current matrix
17696 have, by definition, to be the same as glyph pointers in the
17697 corresponding frame matrix. Note that frame matrices have no
17698 marginal areas (see build_frame_matrix). */
17699 window_row = w->current_matrix->rows;
17700 window_row_end = window_row + w->current_matrix->nrows;
17701 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17702 while (window_row < window_row_end)
17703 {
17704 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17705 struct glyph *end = window_row->glyphs[LAST_AREA];
17706
17707 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17708 frame_row->glyphs[TEXT_AREA] = start;
17709 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17710 frame_row->glyphs[LAST_AREA] = end;
17711
17712 /* Disable frame rows whose corresponding window rows have
17713 been disabled in try_window_id. */
17714 if (!window_row->enabled_p)
17715 frame_row->enabled_p = false;
17716
17717 ++window_row, ++frame_row;
17718 }
17719 }
17720
17721
17722 /* Find the glyph row in window W containing CHARPOS. Consider all
17723 rows between START and END (not inclusive). END null means search
17724 all rows to the end of the display area of W. Value is the row
17725 containing CHARPOS or null. */
17726
17727 struct glyph_row *
17728 row_containing_pos (struct window *w, ptrdiff_t charpos,
17729 struct glyph_row *start, struct glyph_row *end, int dy)
17730 {
17731 struct glyph_row *row = start;
17732 struct glyph_row *best_row = NULL;
17733 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17734 int last_y;
17735
17736 /* If we happen to start on a header-line, skip that. */
17737 if (row->mode_line_p)
17738 ++row;
17739
17740 if ((end && row >= end) || !row->enabled_p)
17741 return NULL;
17742
17743 last_y = window_text_bottom_y (w) - dy;
17744
17745 while (true)
17746 {
17747 /* Give up if we have gone too far. */
17748 if (end && row >= end)
17749 return NULL;
17750 /* This formerly returned if they were equal.
17751 I think that both quantities are of a "last plus one" type;
17752 if so, when they are equal, the row is within the screen. -- rms. */
17753 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17754 return NULL;
17755
17756 /* If it is in this row, return this row. */
17757 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17758 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17759 /* The end position of a row equals the start
17760 position of the next row. If CHARPOS is there, we
17761 would rather consider it displayed in the next
17762 line, except when this line ends in ZV. */
17763 && !row_for_charpos_p (row, charpos)))
17764 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17765 {
17766 struct glyph *g;
17767
17768 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17769 || (!best_row && !row->continued_p))
17770 return row;
17771 /* In bidi-reordered rows, there could be several rows whose
17772 edges surround CHARPOS, all of these rows belonging to
17773 the same continued line. We need to find the row which
17774 fits CHARPOS the best. */
17775 for (g = row->glyphs[TEXT_AREA];
17776 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17777 g++)
17778 {
17779 if (!STRINGP (g->object))
17780 {
17781 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17782 {
17783 mindif = eabs (g->charpos - charpos);
17784 best_row = row;
17785 /* Exact match always wins. */
17786 if (mindif == 0)
17787 return best_row;
17788 }
17789 }
17790 }
17791 }
17792 else if (best_row && !row->continued_p)
17793 return best_row;
17794 ++row;
17795 }
17796 }
17797
17798
17799 /* Try to redisplay window W by reusing its existing display. W's
17800 current matrix must be up to date when this function is called,
17801 i.e., window_end_valid must be true.
17802
17803 Value is
17804
17805 >= 1 if successful, i.e. display has been updated
17806 specifically:
17807 1 means the changes were in front of a newline that precedes
17808 the window start, and the whole current matrix was reused
17809 2 means the changes were after the last position displayed
17810 in the window, and the whole current matrix was reused
17811 3 means portions of the current matrix were reused, while
17812 some of the screen lines were redrawn
17813 -1 if redisplay with same window start is known not to succeed
17814 0 if otherwise unsuccessful
17815
17816 The following steps are performed:
17817
17818 1. Find the last row in the current matrix of W that is not
17819 affected by changes at the start of current_buffer. If no such row
17820 is found, give up.
17821
17822 2. Find the first row in W's current matrix that is not affected by
17823 changes at the end of current_buffer. Maybe there is no such row.
17824
17825 3. Display lines beginning with the row + 1 found in step 1 to the
17826 row found in step 2 or, if step 2 didn't find a row, to the end of
17827 the window.
17828
17829 4. If cursor is not known to appear on the window, give up.
17830
17831 5. If display stopped at the row found in step 2, scroll the
17832 display and current matrix as needed.
17833
17834 6. Maybe display some lines at the end of W, if we must. This can
17835 happen under various circumstances, like a partially visible line
17836 becoming fully visible, or because newly displayed lines are displayed
17837 in smaller font sizes.
17838
17839 7. Update W's window end information. */
17840
17841 static int
17842 try_window_id (struct window *w)
17843 {
17844 struct frame *f = XFRAME (w->frame);
17845 struct glyph_matrix *current_matrix = w->current_matrix;
17846 struct glyph_matrix *desired_matrix = w->desired_matrix;
17847 struct glyph_row *last_unchanged_at_beg_row;
17848 struct glyph_row *first_unchanged_at_end_row;
17849 struct glyph_row *row;
17850 struct glyph_row *bottom_row;
17851 int bottom_vpos;
17852 struct it it;
17853 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17854 int dvpos, dy;
17855 struct text_pos start_pos;
17856 struct run run;
17857 int first_unchanged_at_end_vpos = 0;
17858 struct glyph_row *last_text_row, *last_text_row_at_end;
17859 struct text_pos start;
17860 ptrdiff_t first_changed_charpos, last_changed_charpos;
17861
17862 #ifdef GLYPH_DEBUG
17863 if (inhibit_try_window_id)
17864 return 0;
17865 #endif
17866
17867 /* This is handy for debugging. */
17868 #if false
17869 #define GIVE_UP(X) \
17870 do { \
17871 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
17872 return 0; \
17873 } while (false)
17874 #else
17875 #define GIVE_UP(X) return 0
17876 #endif
17877
17878 SET_TEXT_POS_FROM_MARKER (start, w->start);
17879
17880 /* Don't use this for mini-windows because these can show
17881 messages and mini-buffers, and we don't handle that here. */
17882 if (MINI_WINDOW_P (w))
17883 GIVE_UP (1);
17884
17885 /* This flag is used to prevent redisplay optimizations. */
17886 if (windows_or_buffers_changed || f->cursor_type_changed)
17887 GIVE_UP (2);
17888
17889 /* This function's optimizations cannot be used if overlays have
17890 changed in the buffer displayed by the window, so give up if they
17891 have. */
17892 if (w->last_overlay_modified != OVERLAY_MODIFF)
17893 GIVE_UP (200);
17894
17895 /* Verify that narrowing has not changed.
17896 Also verify that we were not told to prevent redisplay optimizations.
17897 It would be nice to further
17898 reduce the number of cases where this prevents try_window_id. */
17899 if (current_buffer->clip_changed
17900 || current_buffer->prevent_redisplay_optimizations_p)
17901 GIVE_UP (3);
17902
17903 /* Window must either use window-based redisplay or be full width. */
17904 if (!FRAME_WINDOW_P (f)
17905 && (!FRAME_LINE_INS_DEL_OK (f)
17906 || !WINDOW_FULL_WIDTH_P (w)))
17907 GIVE_UP (4);
17908
17909 /* Give up if point is known NOT to appear in W. */
17910 if (PT < CHARPOS (start))
17911 GIVE_UP (5);
17912
17913 /* Another way to prevent redisplay optimizations. */
17914 if (w->last_modified == 0)
17915 GIVE_UP (6);
17916
17917 /* Verify that window is not hscrolled. */
17918 if (w->hscroll != 0)
17919 GIVE_UP (7);
17920
17921 /* Verify that display wasn't paused. */
17922 if (!w->window_end_valid)
17923 GIVE_UP (8);
17924
17925 /* Likewise if highlighting trailing whitespace. */
17926 if (!NILP (Vshow_trailing_whitespace))
17927 GIVE_UP (11);
17928
17929 /* Can't use this if overlay arrow position and/or string have
17930 changed. */
17931 if (overlay_arrows_changed_p ())
17932 GIVE_UP (12);
17933
17934 /* When word-wrap is on, adding a space to the first word of a
17935 wrapped line can change the wrap position, altering the line
17936 above it. It might be worthwhile to handle this more
17937 intelligently, but for now just redisplay from scratch. */
17938 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17939 GIVE_UP (21);
17940
17941 /* Under bidi reordering, adding or deleting a character in the
17942 beginning of a paragraph, before the first strong directional
17943 character, can change the base direction of the paragraph (unless
17944 the buffer specifies a fixed paragraph direction), which will
17945 require to redisplay the whole paragraph. It might be worthwhile
17946 to find the paragraph limits and widen the range of redisplayed
17947 lines to that, but for now just give up this optimization and
17948 redisplay from scratch. */
17949 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17950 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17951 GIVE_UP (22);
17952
17953 /* Give up if the buffer has line-spacing set, as Lisp-level changes
17954 to that variable require thorough redisplay. */
17955 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
17956 GIVE_UP (23);
17957
17958 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17959 only if buffer has really changed. The reason is that the gap is
17960 initially at Z for freshly visited files. The code below would
17961 set end_unchanged to 0 in that case. */
17962 if (MODIFF > SAVE_MODIFF
17963 /* This seems to happen sometimes after saving a buffer. */
17964 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17965 {
17966 if (GPT - BEG < BEG_UNCHANGED)
17967 BEG_UNCHANGED = GPT - BEG;
17968 if (Z - GPT < END_UNCHANGED)
17969 END_UNCHANGED = Z - GPT;
17970 }
17971
17972 /* The position of the first and last character that has been changed. */
17973 first_changed_charpos = BEG + BEG_UNCHANGED;
17974 last_changed_charpos = Z - END_UNCHANGED;
17975
17976 /* If window starts after a line end, and the last change is in
17977 front of that newline, then changes don't affect the display.
17978 This case happens with stealth-fontification. Note that although
17979 the display is unchanged, glyph positions in the matrix have to
17980 be adjusted, of course. */
17981 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17982 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17983 && ((last_changed_charpos < CHARPOS (start)
17984 && CHARPOS (start) == BEGV)
17985 || (last_changed_charpos < CHARPOS (start) - 1
17986 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17987 {
17988 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17989 struct glyph_row *r0;
17990
17991 /* Compute how many chars/bytes have been added to or removed
17992 from the buffer. */
17993 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17994 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17995 Z_delta = Z - Z_old;
17996 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17997
17998 /* Give up if PT is not in the window. Note that it already has
17999 been checked at the start of try_window_id that PT is not in
18000 front of the window start. */
18001 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18002 GIVE_UP (13);
18003
18004 /* If window start is unchanged, we can reuse the whole matrix
18005 as is, after adjusting glyph positions. No need to compute
18006 the window end again, since its offset from Z hasn't changed. */
18007 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18008 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18009 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18010 /* PT must not be in a partially visible line. */
18011 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18012 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18013 {
18014 /* Adjust positions in the glyph matrix. */
18015 if (Z_delta || Z_delta_bytes)
18016 {
18017 struct glyph_row *r1
18018 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18019 increment_matrix_positions (w->current_matrix,
18020 MATRIX_ROW_VPOS (r0, current_matrix),
18021 MATRIX_ROW_VPOS (r1, current_matrix),
18022 Z_delta, Z_delta_bytes);
18023 }
18024
18025 /* Set the cursor. */
18026 row = row_containing_pos (w, PT, r0, NULL, 0);
18027 if (row)
18028 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18029 return 1;
18030 }
18031 }
18032
18033 /* Handle the case that changes are all below what is displayed in
18034 the window, and that PT is in the window. This shortcut cannot
18035 be taken if ZV is visible in the window, and text has been added
18036 there that is visible in the window. */
18037 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18038 /* ZV is not visible in the window, or there are no
18039 changes at ZV, actually. */
18040 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18041 || first_changed_charpos == last_changed_charpos))
18042 {
18043 struct glyph_row *r0;
18044
18045 /* Give up if PT is not in the window. Note that it already has
18046 been checked at the start of try_window_id that PT is not in
18047 front of the window start. */
18048 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18049 GIVE_UP (14);
18050
18051 /* If window start is unchanged, we can reuse the whole matrix
18052 as is, without changing glyph positions since no text has
18053 been added/removed in front of the window end. */
18054 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18055 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18056 /* PT must not be in a partially visible line. */
18057 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18058 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18059 {
18060 /* We have to compute the window end anew since text
18061 could have been added/removed after it. */
18062 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18063 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18064
18065 /* Set the cursor. */
18066 row = row_containing_pos (w, PT, r0, NULL, 0);
18067 if (row)
18068 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18069 return 2;
18070 }
18071 }
18072
18073 /* Give up if window start is in the changed area.
18074
18075 The condition used to read
18076
18077 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18078
18079 but why that was tested escapes me at the moment. */
18080 if (CHARPOS (start) >= first_changed_charpos
18081 && CHARPOS (start) <= last_changed_charpos)
18082 GIVE_UP (15);
18083
18084 /* Check that window start agrees with the start of the first glyph
18085 row in its current matrix. Check this after we know the window
18086 start is not in changed text, otherwise positions would not be
18087 comparable. */
18088 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18089 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18090 GIVE_UP (16);
18091
18092 /* Give up if the window ends in strings. Overlay strings
18093 at the end are difficult to handle, so don't try. */
18094 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18095 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18096 GIVE_UP (20);
18097
18098 /* Compute the position at which we have to start displaying new
18099 lines. Some of the lines at the top of the window might be
18100 reusable because they are not displaying changed text. Find the
18101 last row in W's current matrix not affected by changes at the
18102 start of current_buffer. Value is null if changes start in the
18103 first line of window. */
18104 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18105 if (last_unchanged_at_beg_row)
18106 {
18107 /* Avoid starting to display in the middle of a character, a TAB
18108 for instance. This is easier than to set up the iterator
18109 exactly, and it's not a frequent case, so the additional
18110 effort wouldn't really pay off. */
18111 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18112 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18113 && last_unchanged_at_beg_row > w->current_matrix->rows)
18114 --last_unchanged_at_beg_row;
18115
18116 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18117 GIVE_UP (17);
18118
18119 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18120 GIVE_UP (18);
18121 start_pos = it.current.pos;
18122
18123 /* Start displaying new lines in the desired matrix at the same
18124 vpos we would use in the current matrix, i.e. below
18125 last_unchanged_at_beg_row. */
18126 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18127 current_matrix);
18128 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18129 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18130
18131 eassert (it.hpos == 0 && it.current_x == 0);
18132 }
18133 else
18134 {
18135 /* There are no reusable lines at the start of the window.
18136 Start displaying in the first text line. */
18137 start_display (&it, w, start);
18138 it.vpos = it.first_vpos;
18139 start_pos = it.current.pos;
18140 }
18141
18142 /* Find the first row that is not affected by changes at the end of
18143 the buffer. Value will be null if there is no unchanged row, in
18144 which case we must redisplay to the end of the window. delta
18145 will be set to the value by which buffer positions beginning with
18146 first_unchanged_at_end_row have to be adjusted due to text
18147 changes. */
18148 first_unchanged_at_end_row
18149 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18150 IF_DEBUG (debug_delta = delta);
18151 IF_DEBUG (debug_delta_bytes = delta_bytes);
18152
18153 /* Set stop_pos to the buffer position up to which we will have to
18154 display new lines. If first_unchanged_at_end_row != NULL, this
18155 is the buffer position of the start of the line displayed in that
18156 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18157 that we don't stop at a buffer position. */
18158 stop_pos = 0;
18159 if (first_unchanged_at_end_row)
18160 {
18161 eassert (last_unchanged_at_beg_row == NULL
18162 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18163
18164 /* If this is a continuation line, move forward to the next one
18165 that isn't. Changes in lines above affect this line.
18166 Caution: this may move first_unchanged_at_end_row to a row
18167 not displaying text. */
18168 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18169 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18170 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18171 < it.last_visible_y))
18172 ++first_unchanged_at_end_row;
18173
18174 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18175 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18176 >= it.last_visible_y))
18177 first_unchanged_at_end_row = NULL;
18178 else
18179 {
18180 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18181 + delta);
18182 first_unchanged_at_end_vpos
18183 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18184 eassert (stop_pos >= Z - END_UNCHANGED);
18185 }
18186 }
18187 else if (last_unchanged_at_beg_row == NULL)
18188 GIVE_UP (19);
18189
18190
18191 #ifdef GLYPH_DEBUG
18192
18193 /* Either there is no unchanged row at the end, or the one we have
18194 now displays text. This is a necessary condition for the window
18195 end pos calculation at the end of this function. */
18196 eassert (first_unchanged_at_end_row == NULL
18197 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18198
18199 debug_last_unchanged_at_beg_vpos
18200 = (last_unchanged_at_beg_row
18201 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18202 : -1);
18203 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18204
18205 #endif /* GLYPH_DEBUG */
18206
18207
18208 /* Display new lines. Set last_text_row to the last new line
18209 displayed which has text on it, i.e. might end up as being the
18210 line where the window_end_vpos is. */
18211 w->cursor.vpos = -1;
18212 last_text_row = NULL;
18213 overlay_arrow_seen = false;
18214 if (it.current_y < it.last_visible_y
18215 && !f->fonts_changed
18216 && (first_unchanged_at_end_row == NULL
18217 || IT_CHARPOS (it) < stop_pos))
18218 it.glyph_row->reversed_p = false;
18219 while (it.current_y < it.last_visible_y
18220 && !f->fonts_changed
18221 && (first_unchanged_at_end_row == NULL
18222 || IT_CHARPOS (it) < stop_pos))
18223 {
18224 if (display_line (&it))
18225 last_text_row = it.glyph_row - 1;
18226 }
18227
18228 if (f->fonts_changed)
18229 return -1;
18230
18231 /* The redisplay iterations in display_line above could have
18232 triggered font-lock, which could have done something that
18233 invalidates IT->w window's end-point information, on which we
18234 rely below. E.g., one package, which will remain unnamed, used
18235 to install a font-lock-fontify-region-function that called
18236 bury-buffer, whose side effect is to switch the buffer displayed
18237 by IT->w, and that predictably resets IT->w's window_end_valid
18238 flag, which we already tested at the entry to this function.
18239 Amply punish such packages/modes by giving up on this
18240 optimization in those cases. */
18241 if (!w->window_end_valid)
18242 {
18243 clear_glyph_matrix (w->desired_matrix);
18244 return -1;
18245 }
18246
18247 /* Compute differences in buffer positions, y-positions etc. for
18248 lines reused at the bottom of the window. Compute what we can
18249 scroll. */
18250 if (first_unchanged_at_end_row
18251 /* No lines reused because we displayed everything up to the
18252 bottom of the window. */
18253 && it.current_y < it.last_visible_y)
18254 {
18255 dvpos = (it.vpos
18256 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18257 current_matrix));
18258 dy = it.current_y - first_unchanged_at_end_row->y;
18259 run.current_y = first_unchanged_at_end_row->y;
18260 run.desired_y = run.current_y + dy;
18261 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18262 }
18263 else
18264 {
18265 delta = delta_bytes = dvpos = dy
18266 = run.current_y = run.desired_y = run.height = 0;
18267 first_unchanged_at_end_row = NULL;
18268 }
18269 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18270
18271
18272 /* Find the cursor if not already found. We have to decide whether
18273 PT will appear on this window (it sometimes doesn't, but this is
18274 not a very frequent case.) This decision has to be made before
18275 the current matrix is altered. A value of cursor.vpos < 0 means
18276 that PT is either in one of the lines beginning at
18277 first_unchanged_at_end_row or below the window. Don't care for
18278 lines that might be displayed later at the window end; as
18279 mentioned, this is not a frequent case. */
18280 if (w->cursor.vpos < 0)
18281 {
18282 /* Cursor in unchanged rows at the top? */
18283 if (PT < CHARPOS (start_pos)
18284 && last_unchanged_at_beg_row)
18285 {
18286 row = row_containing_pos (w, PT,
18287 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18288 last_unchanged_at_beg_row + 1, 0);
18289 if (row)
18290 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18291 }
18292
18293 /* Start from first_unchanged_at_end_row looking for PT. */
18294 else if (first_unchanged_at_end_row)
18295 {
18296 row = row_containing_pos (w, PT - delta,
18297 first_unchanged_at_end_row, NULL, 0);
18298 if (row)
18299 set_cursor_from_row (w, row, w->current_matrix, delta,
18300 delta_bytes, dy, dvpos);
18301 }
18302
18303 /* Give up if cursor was not found. */
18304 if (w->cursor.vpos < 0)
18305 {
18306 clear_glyph_matrix (w->desired_matrix);
18307 return -1;
18308 }
18309 }
18310
18311 /* Don't let the cursor end in the scroll margins. */
18312 {
18313 int this_scroll_margin, cursor_height;
18314 int frame_line_height = default_line_pixel_height (w);
18315 int window_total_lines
18316 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18317
18318 this_scroll_margin =
18319 max (0, min (scroll_margin, window_total_lines / 4));
18320 this_scroll_margin *= frame_line_height;
18321 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18322
18323 if ((w->cursor.y < this_scroll_margin
18324 && CHARPOS (start) > BEGV)
18325 /* Old redisplay didn't take scroll margin into account at the bottom,
18326 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18327 || (w->cursor.y + (make_cursor_line_fully_visible_p
18328 ? cursor_height + this_scroll_margin
18329 : 1)) > it.last_visible_y)
18330 {
18331 w->cursor.vpos = -1;
18332 clear_glyph_matrix (w->desired_matrix);
18333 return -1;
18334 }
18335 }
18336
18337 /* Scroll the display. Do it before changing the current matrix so
18338 that xterm.c doesn't get confused about where the cursor glyph is
18339 found. */
18340 if (dy && run.height)
18341 {
18342 update_begin (f);
18343
18344 if (FRAME_WINDOW_P (f))
18345 {
18346 FRAME_RIF (f)->update_window_begin_hook (w);
18347 FRAME_RIF (f)->clear_window_mouse_face (w);
18348 FRAME_RIF (f)->scroll_run_hook (w, &run);
18349 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18350 }
18351 else
18352 {
18353 /* Terminal frame. In this case, dvpos gives the number of
18354 lines to scroll by; dvpos < 0 means scroll up. */
18355 int from_vpos
18356 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18357 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18358 int end = (WINDOW_TOP_EDGE_LINE (w)
18359 + WINDOW_WANTS_HEADER_LINE_P (w)
18360 + window_internal_height (w));
18361
18362 #if defined (HAVE_GPM) || defined (MSDOS)
18363 x_clear_window_mouse_face (w);
18364 #endif
18365 /* Perform the operation on the screen. */
18366 if (dvpos > 0)
18367 {
18368 /* Scroll last_unchanged_at_beg_row to the end of the
18369 window down dvpos lines. */
18370 set_terminal_window (f, end);
18371
18372 /* On dumb terminals delete dvpos lines at the end
18373 before inserting dvpos empty lines. */
18374 if (!FRAME_SCROLL_REGION_OK (f))
18375 ins_del_lines (f, end - dvpos, -dvpos);
18376
18377 /* Insert dvpos empty lines in front of
18378 last_unchanged_at_beg_row. */
18379 ins_del_lines (f, from, dvpos);
18380 }
18381 else if (dvpos < 0)
18382 {
18383 /* Scroll up last_unchanged_at_beg_vpos to the end of
18384 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18385 set_terminal_window (f, end);
18386
18387 /* Delete dvpos lines in front of
18388 last_unchanged_at_beg_vpos. ins_del_lines will set
18389 the cursor to the given vpos and emit |dvpos| delete
18390 line sequences. */
18391 ins_del_lines (f, from + dvpos, dvpos);
18392
18393 /* On a dumb terminal insert dvpos empty lines at the
18394 end. */
18395 if (!FRAME_SCROLL_REGION_OK (f))
18396 ins_del_lines (f, end + dvpos, -dvpos);
18397 }
18398
18399 set_terminal_window (f, 0);
18400 }
18401
18402 update_end (f);
18403 }
18404
18405 /* Shift reused rows of the current matrix to the right position.
18406 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18407 text. */
18408 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18409 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18410 if (dvpos < 0)
18411 {
18412 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18413 bottom_vpos, dvpos);
18414 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18415 bottom_vpos);
18416 }
18417 else if (dvpos > 0)
18418 {
18419 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18420 bottom_vpos, dvpos);
18421 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18422 first_unchanged_at_end_vpos + dvpos);
18423 }
18424
18425 /* For frame-based redisplay, make sure that current frame and window
18426 matrix are in sync with respect to glyph memory. */
18427 if (!FRAME_WINDOW_P (f))
18428 sync_frame_with_window_matrix_rows (w);
18429
18430 /* Adjust buffer positions in reused rows. */
18431 if (delta || delta_bytes)
18432 increment_matrix_positions (current_matrix,
18433 first_unchanged_at_end_vpos + dvpos,
18434 bottom_vpos, delta, delta_bytes);
18435
18436 /* Adjust Y positions. */
18437 if (dy)
18438 shift_glyph_matrix (w, current_matrix,
18439 first_unchanged_at_end_vpos + dvpos,
18440 bottom_vpos, dy);
18441
18442 if (first_unchanged_at_end_row)
18443 {
18444 first_unchanged_at_end_row += dvpos;
18445 if (first_unchanged_at_end_row->y >= it.last_visible_y
18446 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18447 first_unchanged_at_end_row = NULL;
18448 }
18449
18450 /* If scrolling up, there may be some lines to display at the end of
18451 the window. */
18452 last_text_row_at_end = NULL;
18453 if (dy < 0)
18454 {
18455 /* Scrolling up can leave for example a partially visible line
18456 at the end of the window to be redisplayed. */
18457 /* Set last_row to the glyph row in the current matrix where the
18458 window end line is found. It has been moved up or down in
18459 the matrix by dvpos. */
18460 int last_vpos = w->window_end_vpos + dvpos;
18461 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18462
18463 /* If last_row is the window end line, it should display text. */
18464 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18465
18466 /* If window end line was partially visible before, begin
18467 displaying at that line. Otherwise begin displaying with the
18468 line following it. */
18469 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18470 {
18471 init_to_row_start (&it, w, last_row);
18472 it.vpos = last_vpos;
18473 it.current_y = last_row->y;
18474 }
18475 else
18476 {
18477 init_to_row_end (&it, w, last_row);
18478 it.vpos = 1 + last_vpos;
18479 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18480 ++last_row;
18481 }
18482
18483 /* We may start in a continuation line. If so, we have to
18484 get the right continuation_lines_width and current_x. */
18485 it.continuation_lines_width = last_row->continuation_lines_width;
18486 it.hpos = it.current_x = 0;
18487
18488 /* Display the rest of the lines at the window end. */
18489 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18490 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18491 {
18492 /* Is it always sure that the display agrees with lines in
18493 the current matrix? I don't think so, so we mark rows
18494 displayed invalid in the current matrix by setting their
18495 enabled_p flag to false. */
18496 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18497 if (display_line (&it))
18498 last_text_row_at_end = it.glyph_row - 1;
18499 }
18500 }
18501
18502 /* Update window_end_pos and window_end_vpos. */
18503 if (first_unchanged_at_end_row && !last_text_row_at_end)
18504 {
18505 /* Window end line if one of the preserved rows from the current
18506 matrix. Set row to the last row displaying text in current
18507 matrix starting at first_unchanged_at_end_row, after
18508 scrolling. */
18509 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18510 row = find_last_row_displaying_text (w->current_matrix, &it,
18511 first_unchanged_at_end_row);
18512 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18513 adjust_window_ends (w, row, true);
18514 eassert (w->window_end_bytepos >= 0);
18515 IF_DEBUG (debug_method_add (w, "A"));
18516 }
18517 else if (last_text_row_at_end)
18518 {
18519 adjust_window_ends (w, last_text_row_at_end, false);
18520 eassert (w->window_end_bytepos >= 0);
18521 IF_DEBUG (debug_method_add (w, "B"));
18522 }
18523 else if (last_text_row)
18524 {
18525 /* We have displayed either to the end of the window or at the
18526 end of the window, i.e. the last row with text is to be found
18527 in the desired matrix. */
18528 adjust_window_ends (w, last_text_row, false);
18529 eassert (w->window_end_bytepos >= 0);
18530 }
18531 else if (first_unchanged_at_end_row == NULL
18532 && last_text_row == NULL
18533 && last_text_row_at_end == NULL)
18534 {
18535 /* Displayed to end of window, but no line containing text was
18536 displayed. Lines were deleted at the end of the window. */
18537 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18538 int vpos = w->window_end_vpos;
18539 struct glyph_row *current_row = current_matrix->rows + vpos;
18540 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18541
18542 for (row = NULL;
18543 row == NULL && vpos >= first_vpos;
18544 --vpos, --current_row, --desired_row)
18545 {
18546 if (desired_row->enabled_p)
18547 {
18548 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18549 row = desired_row;
18550 }
18551 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18552 row = current_row;
18553 }
18554
18555 eassert (row != NULL);
18556 w->window_end_vpos = vpos + 1;
18557 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18558 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18559 eassert (w->window_end_bytepos >= 0);
18560 IF_DEBUG (debug_method_add (w, "C"));
18561 }
18562 else
18563 emacs_abort ();
18564
18565 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18566 debug_end_vpos = w->window_end_vpos));
18567
18568 /* Record that display has not been completed. */
18569 w->window_end_valid = false;
18570 w->desired_matrix->no_scrolling_p = true;
18571 return 3;
18572
18573 #undef GIVE_UP
18574 }
18575
18576
18577 \f
18578 /***********************************************************************
18579 More debugging support
18580 ***********************************************************************/
18581
18582 #ifdef GLYPH_DEBUG
18583
18584 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18585 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18586 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18587
18588
18589 /* Dump the contents of glyph matrix MATRIX on stderr.
18590
18591 GLYPHS 0 means don't show glyph contents.
18592 GLYPHS 1 means show glyphs in short form
18593 GLYPHS > 1 means show glyphs in long form. */
18594
18595 void
18596 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18597 {
18598 int i;
18599 for (i = 0; i < matrix->nrows; ++i)
18600 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18601 }
18602
18603
18604 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18605 the glyph row and area where the glyph comes from. */
18606
18607 void
18608 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18609 {
18610 if (glyph->type == CHAR_GLYPH
18611 || glyph->type == GLYPHLESS_GLYPH)
18612 {
18613 fprintf (stderr,
18614 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18615 glyph - row->glyphs[TEXT_AREA],
18616 (glyph->type == CHAR_GLYPH
18617 ? 'C'
18618 : 'G'),
18619 glyph->charpos,
18620 (BUFFERP (glyph->object)
18621 ? 'B'
18622 : (STRINGP (glyph->object)
18623 ? 'S'
18624 : (NILP (glyph->object)
18625 ? '0'
18626 : '-'))),
18627 glyph->pixel_width,
18628 glyph->u.ch,
18629 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18630 ? glyph->u.ch
18631 : '.'),
18632 glyph->face_id,
18633 glyph->left_box_line_p,
18634 glyph->right_box_line_p);
18635 }
18636 else if (glyph->type == STRETCH_GLYPH)
18637 {
18638 fprintf (stderr,
18639 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18640 glyph - row->glyphs[TEXT_AREA],
18641 'S',
18642 glyph->charpos,
18643 (BUFFERP (glyph->object)
18644 ? 'B'
18645 : (STRINGP (glyph->object)
18646 ? 'S'
18647 : (NILP (glyph->object)
18648 ? '0'
18649 : '-'))),
18650 glyph->pixel_width,
18651 0,
18652 ' ',
18653 glyph->face_id,
18654 glyph->left_box_line_p,
18655 glyph->right_box_line_p);
18656 }
18657 else if (glyph->type == IMAGE_GLYPH)
18658 {
18659 fprintf (stderr,
18660 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18661 glyph - row->glyphs[TEXT_AREA],
18662 'I',
18663 glyph->charpos,
18664 (BUFFERP (glyph->object)
18665 ? 'B'
18666 : (STRINGP (glyph->object)
18667 ? 'S'
18668 : (NILP (glyph->object)
18669 ? '0'
18670 : '-'))),
18671 glyph->pixel_width,
18672 glyph->u.img_id,
18673 '.',
18674 glyph->face_id,
18675 glyph->left_box_line_p,
18676 glyph->right_box_line_p);
18677 }
18678 else if (glyph->type == COMPOSITE_GLYPH)
18679 {
18680 fprintf (stderr,
18681 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18682 glyph - row->glyphs[TEXT_AREA],
18683 '+',
18684 glyph->charpos,
18685 (BUFFERP (glyph->object)
18686 ? 'B'
18687 : (STRINGP (glyph->object)
18688 ? 'S'
18689 : (NILP (glyph->object)
18690 ? '0'
18691 : '-'))),
18692 glyph->pixel_width,
18693 glyph->u.cmp.id);
18694 if (glyph->u.cmp.automatic)
18695 fprintf (stderr,
18696 "[%d-%d]",
18697 glyph->slice.cmp.from, glyph->slice.cmp.to);
18698 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18699 glyph->face_id,
18700 glyph->left_box_line_p,
18701 glyph->right_box_line_p);
18702 }
18703 }
18704
18705
18706 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18707 GLYPHS 0 means don't show glyph contents.
18708 GLYPHS 1 means show glyphs in short form
18709 GLYPHS > 1 means show glyphs in long form. */
18710
18711 void
18712 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18713 {
18714 if (glyphs != 1)
18715 {
18716 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18717 fprintf (stderr, "==============================================================================\n");
18718
18719 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18720 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18721 vpos,
18722 MATRIX_ROW_START_CHARPOS (row),
18723 MATRIX_ROW_END_CHARPOS (row),
18724 row->used[TEXT_AREA],
18725 row->contains_overlapping_glyphs_p,
18726 row->enabled_p,
18727 row->truncated_on_left_p,
18728 row->truncated_on_right_p,
18729 row->continued_p,
18730 MATRIX_ROW_CONTINUATION_LINE_P (row),
18731 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18732 row->ends_at_zv_p,
18733 row->fill_line_p,
18734 row->ends_in_middle_of_char_p,
18735 row->starts_in_middle_of_char_p,
18736 row->mouse_face_p,
18737 row->x,
18738 row->y,
18739 row->pixel_width,
18740 row->height,
18741 row->visible_height,
18742 row->ascent,
18743 row->phys_ascent);
18744 /* The next 3 lines should align to "Start" in the header. */
18745 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18746 row->end.overlay_string_index,
18747 row->continuation_lines_width);
18748 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18749 CHARPOS (row->start.string_pos),
18750 CHARPOS (row->end.string_pos));
18751 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18752 row->end.dpvec_index);
18753 }
18754
18755 if (glyphs > 1)
18756 {
18757 int area;
18758
18759 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18760 {
18761 struct glyph *glyph = row->glyphs[area];
18762 struct glyph *glyph_end = glyph + row->used[area];
18763
18764 /* Glyph for a line end in text. */
18765 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18766 ++glyph_end;
18767
18768 if (glyph < glyph_end)
18769 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18770
18771 for (; glyph < glyph_end; ++glyph)
18772 dump_glyph (row, glyph, area);
18773 }
18774 }
18775 else if (glyphs == 1)
18776 {
18777 int area;
18778 char s[SHRT_MAX + 4];
18779
18780 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18781 {
18782 int i;
18783
18784 for (i = 0; i < row->used[area]; ++i)
18785 {
18786 struct glyph *glyph = row->glyphs[area] + i;
18787 if (i == row->used[area] - 1
18788 && area == TEXT_AREA
18789 && NILP (glyph->object)
18790 && glyph->type == CHAR_GLYPH
18791 && glyph->u.ch == ' ')
18792 {
18793 strcpy (&s[i], "[\\n]");
18794 i += 4;
18795 }
18796 else if (glyph->type == CHAR_GLYPH
18797 && glyph->u.ch < 0x80
18798 && glyph->u.ch >= ' ')
18799 s[i] = glyph->u.ch;
18800 else
18801 s[i] = '.';
18802 }
18803
18804 s[i] = '\0';
18805 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18806 }
18807 }
18808 }
18809
18810
18811 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18812 Sdump_glyph_matrix, 0, 1, "p",
18813 doc: /* Dump the current matrix of the selected window to stderr.
18814 Shows contents of glyph row structures. With non-nil
18815 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18816 glyphs in short form, otherwise show glyphs in long form.
18817
18818 Interactively, no argument means show glyphs in short form;
18819 with numeric argument, its value is passed as the GLYPHS flag. */)
18820 (Lisp_Object glyphs)
18821 {
18822 struct window *w = XWINDOW (selected_window);
18823 struct buffer *buffer = XBUFFER (w->contents);
18824
18825 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18826 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18827 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18828 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18829 fprintf (stderr, "=============================================\n");
18830 dump_glyph_matrix (w->current_matrix,
18831 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18832 return Qnil;
18833 }
18834
18835
18836 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18837 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18838 Only text-mode frames have frame glyph matrices. */)
18839 (void)
18840 {
18841 struct frame *f = XFRAME (selected_frame);
18842
18843 if (f->current_matrix)
18844 dump_glyph_matrix (f->current_matrix, 1);
18845 else
18846 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18847 return Qnil;
18848 }
18849
18850
18851 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18852 doc: /* Dump glyph row ROW to stderr.
18853 GLYPH 0 means don't dump glyphs.
18854 GLYPH 1 means dump glyphs in short form.
18855 GLYPH > 1 or omitted means dump glyphs in long form. */)
18856 (Lisp_Object row, Lisp_Object glyphs)
18857 {
18858 struct glyph_matrix *matrix;
18859 EMACS_INT vpos;
18860
18861 CHECK_NUMBER (row);
18862 matrix = XWINDOW (selected_window)->current_matrix;
18863 vpos = XINT (row);
18864 if (vpos >= 0 && vpos < matrix->nrows)
18865 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18866 vpos,
18867 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18868 return Qnil;
18869 }
18870
18871
18872 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18873 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18874 GLYPH 0 means don't dump glyphs.
18875 GLYPH 1 means dump glyphs in short form.
18876 GLYPH > 1 or omitted means dump glyphs in long form.
18877
18878 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18879 do nothing. */)
18880 (Lisp_Object row, Lisp_Object glyphs)
18881 {
18882 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18883 struct frame *sf = SELECTED_FRAME ();
18884 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18885 EMACS_INT vpos;
18886
18887 CHECK_NUMBER (row);
18888 vpos = XINT (row);
18889 if (vpos >= 0 && vpos < m->nrows)
18890 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18891 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18892 #endif
18893 return Qnil;
18894 }
18895
18896
18897 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18898 doc: /* Toggle tracing of redisplay.
18899 With ARG, turn tracing on if and only if ARG is positive. */)
18900 (Lisp_Object arg)
18901 {
18902 if (NILP (arg))
18903 trace_redisplay_p = !trace_redisplay_p;
18904 else
18905 {
18906 arg = Fprefix_numeric_value (arg);
18907 trace_redisplay_p = XINT (arg) > 0;
18908 }
18909
18910 return Qnil;
18911 }
18912
18913
18914 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18915 doc: /* Like `format', but print result to stderr.
18916 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18917 (ptrdiff_t nargs, Lisp_Object *args)
18918 {
18919 Lisp_Object s = Fformat (nargs, args);
18920 fwrite (SDATA (s), 1, SBYTES (s), stderr);
18921 return Qnil;
18922 }
18923
18924 #endif /* GLYPH_DEBUG */
18925
18926
18927 \f
18928 /***********************************************************************
18929 Building Desired Matrix Rows
18930 ***********************************************************************/
18931
18932 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18933 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18934
18935 static struct glyph_row *
18936 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18937 {
18938 struct frame *f = XFRAME (WINDOW_FRAME (w));
18939 struct buffer *buffer = XBUFFER (w->contents);
18940 struct buffer *old = current_buffer;
18941 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18942 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
18943 const unsigned char *arrow_end = arrow_string + arrow_len;
18944 const unsigned char *p;
18945 struct it it;
18946 bool multibyte_p;
18947 int n_glyphs_before;
18948
18949 set_buffer_temp (buffer);
18950 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18951 scratch_glyph_row.reversed_p = false;
18952 it.glyph_row->used[TEXT_AREA] = 0;
18953 SET_TEXT_POS (it.position, 0, 0);
18954
18955 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18956 p = arrow_string;
18957 while (p < arrow_end)
18958 {
18959 Lisp_Object face, ilisp;
18960
18961 /* Get the next character. */
18962 if (multibyte_p)
18963 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18964 else
18965 {
18966 it.c = it.char_to_display = *p, it.len = 1;
18967 if (! ASCII_CHAR_P (it.c))
18968 it.char_to_display = BYTE8_TO_CHAR (it.c);
18969 }
18970 p += it.len;
18971
18972 /* Get its face. */
18973 ilisp = make_number (p - arrow_string);
18974 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18975 it.face_id = compute_char_face (f, it.char_to_display, face);
18976
18977 /* Compute its width, get its glyphs. */
18978 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18979 SET_TEXT_POS (it.position, -1, -1);
18980 PRODUCE_GLYPHS (&it);
18981
18982 /* If this character doesn't fit any more in the line, we have
18983 to remove some glyphs. */
18984 if (it.current_x > it.last_visible_x)
18985 {
18986 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18987 break;
18988 }
18989 }
18990
18991 set_buffer_temp (old);
18992 return it.glyph_row;
18993 }
18994
18995
18996 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18997 glyphs to insert is determined by produce_special_glyphs. */
18998
18999 static void
19000 insert_left_trunc_glyphs (struct it *it)
19001 {
19002 struct it truncate_it;
19003 struct glyph *from, *end, *to, *toend;
19004
19005 eassert (!FRAME_WINDOW_P (it->f)
19006 || (!it->glyph_row->reversed_p
19007 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19008 || (it->glyph_row->reversed_p
19009 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19010
19011 /* Get the truncation glyphs. */
19012 truncate_it = *it;
19013 truncate_it.current_x = 0;
19014 truncate_it.face_id = DEFAULT_FACE_ID;
19015 truncate_it.glyph_row = &scratch_glyph_row;
19016 truncate_it.area = TEXT_AREA;
19017 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19018 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19019 truncate_it.object = Qnil;
19020 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19021
19022 /* Overwrite glyphs from IT with truncation glyphs. */
19023 if (!it->glyph_row->reversed_p)
19024 {
19025 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19026
19027 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19028 end = from + tused;
19029 to = it->glyph_row->glyphs[TEXT_AREA];
19030 toend = to + it->glyph_row->used[TEXT_AREA];
19031 if (FRAME_WINDOW_P (it->f))
19032 {
19033 /* On GUI frames, when variable-size fonts are displayed,
19034 the truncation glyphs may need more pixels than the row's
19035 glyphs they overwrite. We overwrite more glyphs to free
19036 enough screen real estate, and enlarge the stretch glyph
19037 on the right (see display_line), if there is one, to
19038 preserve the screen position of the truncation glyphs on
19039 the right. */
19040 int w = 0;
19041 struct glyph *g = to;
19042 short used;
19043
19044 /* The first glyph could be partially visible, in which case
19045 it->glyph_row->x will be negative. But we want the left
19046 truncation glyphs to be aligned at the left margin of the
19047 window, so we override the x coordinate at which the row
19048 will begin. */
19049 it->glyph_row->x = 0;
19050 while (g < toend && w < it->truncation_pixel_width)
19051 {
19052 w += g->pixel_width;
19053 ++g;
19054 }
19055 if (g - to - tused > 0)
19056 {
19057 memmove (to + tused, g, (toend - g) * sizeof(*g));
19058 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19059 }
19060 used = it->glyph_row->used[TEXT_AREA];
19061 if (it->glyph_row->truncated_on_right_p
19062 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19063 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19064 == STRETCH_GLYPH)
19065 {
19066 int extra = w - it->truncation_pixel_width;
19067
19068 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19069 }
19070 }
19071
19072 while (from < end)
19073 *to++ = *from++;
19074
19075 /* There may be padding glyphs left over. Overwrite them too. */
19076 if (!FRAME_WINDOW_P (it->f))
19077 {
19078 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19079 {
19080 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19081 while (from < end)
19082 *to++ = *from++;
19083 }
19084 }
19085
19086 if (to > toend)
19087 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19088 }
19089 else
19090 {
19091 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19092
19093 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19094 that back to front. */
19095 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19096 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19097 toend = it->glyph_row->glyphs[TEXT_AREA];
19098 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19099 if (FRAME_WINDOW_P (it->f))
19100 {
19101 int w = 0;
19102 struct glyph *g = to;
19103
19104 while (g >= toend && w < it->truncation_pixel_width)
19105 {
19106 w += g->pixel_width;
19107 --g;
19108 }
19109 if (to - g - tused > 0)
19110 to = g + tused;
19111 if (it->glyph_row->truncated_on_right_p
19112 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19113 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19114 {
19115 int extra = w - it->truncation_pixel_width;
19116
19117 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19118 }
19119 }
19120
19121 while (from >= end && to >= toend)
19122 *to-- = *from--;
19123 if (!FRAME_WINDOW_P (it->f))
19124 {
19125 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19126 {
19127 from =
19128 truncate_it.glyph_row->glyphs[TEXT_AREA]
19129 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19130 while (from >= end && to >= toend)
19131 *to-- = *from--;
19132 }
19133 }
19134 if (from >= end)
19135 {
19136 /* Need to free some room before prepending additional
19137 glyphs. */
19138 int move_by = from - end + 1;
19139 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19140 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19141
19142 for ( ; g >= g0; g--)
19143 g[move_by] = *g;
19144 while (from >= end)
19145 *to-- = *from--;
19146 it->glyph_row->used[TEXT_AREA] += move_by;
19147 }
19148 }
19149 }
19150
19151 /* Compute the hash code for ROW. */
19152 unsigned
19153 row_hash (struct glyph_row *row)
19154 {
19155 int area, k;
19156 unsigned hashval = 0;
19157
19158 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19159 for (k = 0; k < row->used[area]; ++k)
19160 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19161 + row->glyphs[area][k].u.val
19162 + row->glyphs[area][k].face_id
19163 + row->glyphs[area][k].padding_p
19164 + (row->glyphs[area][k].type << 2));
19165
19166 return hashval;
19167 }
19168
19169 /* Compute the pixel height and width of IT->glyph_row.
19170
19171 Most of the time, ascent and height of a display line will be equal
19172 to the max_ascent and max_height values of the display iterator
19173 structure. This is not the case if
19174
19175 1. We hit ZV without displaying anything. In this case, max_ascent
19176 and max_height will be zero.
19177
19178 2. We have some glyphs that don't contribute to the line height.
19179 (The glyph row flag contributes_to_line_height_p is for future
19180 pixmap extensions).
19181
19182 The first case is easily covered by using default values because in
19183 these cases, the line height does not really matter, except that it
19184 must not be zero. */
19185
19186 static void
19187 compute_line_metrics (struct it *it)
19188 {
19189 struct glyph_row *row = it->glyph_row;
19190
19191 if (FRAME_WINDOW_P (it->f))
19192 {
19193 int i, min_y, max_y;
19194
19195 /* The line may consist of one space only, that was added to
19196 place the cursor on it. If so, the row's height hasn't been
19197 computed yet. */
19198 if (row->height == 0)
19199 {
19200 if (it->max_ascent + it->max_descent == 0)
19201 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19202 row->ascent = it->max_ascent;
19203 row->height = it->max_ascent + it->max_descent;
19204 row->phys_ascent = it->max_phys_ascent;
19205 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19206 row->extra_line_spacing = it->max_extra_line_spacing;
19207 }
19208
19209 /* Compute the width of this line. */
19210 row->pixel_width = row->x;
19211 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19212 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19213
19214 eassert (row->pixel_width >= 0);
19215 eassert (row->ascent >= 0 && row->height > 0);
19216
19217 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19218 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19219
19220 /* If first line's physical ascent is larger than its logical
19221 ascent, use the physical ascent, and make the row taller.
19222 This makes accented characters fully visible. */
19223 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19224 && row->phys_ascent > row->ascent)
19225 {
19226 row->height += row->phys_ascent - row->ascent;
19227 row->ascent = row->phys_ascent;
19228 }
19229
19230 /* Compute how much of the line is visible. */
19231 row->visible_height = row->height;
19232
19233 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19234 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19235
19236 if (row->y < min_y)
19237 row->visible_height -= min_y - row->y;
19238 if (row->y + row->height > max_y)
19239 row->visible_height -= row->y + row->height - max_y;
19240 }
19241 else
19242 {
19243 row->pixel_width = row->used[TEXT_AREA];
19244 if (row->continued_p)
19245 row->pixel_width -= it->continuation_pixel_width;
19246 else if (row->truncated_on_right_p)
19247 row->pixel_width -= it->truncation_pixel_width;
19248 row->ascent = row->phys_ascent = 0;
19249 row->height = row->phys_height = row->visible_height = 1;
19250 row->extra_line_spacing = 0;
19251 }
19252
19253 /* Compute a hash code for this row. */
19254 row->hash = row_hash (row);
19255
19256 it->max_ascent = it->max_descent = 0;
19257 it->max_phys_ascent = it->max_phys_descent = 0;
19258 }
19259
19260
19261 /* Append one space to the glyph row of iterator IT if doing a
19262 window-based redisplay. The space has the same face as
19263 IT->face_id. Value is true if a space was added.
19264
19265 This function is called to make sure that there is always one glyph
19266 at the end of a glyph row that the cursor can be set on under
19267 window-systems. (If there weren't such a glyph we would not know
19268 how wide and tall a box cursor should be displayed).
19269
19270 At the same time this space let's a nicely handle clearing to the
19271 end of the line if the row ends in italic text. */
19272
19273 static bool
19274 append_space_for_newline (struct it *it, bool default_face_p)
19275 {
19276 if (FRAME_WINDOW_P (it->f))
19277 {
19278 int n = it->glyph_row->used[TEXT_AREA];
19279
19280 if (it->glyph_row->glyphs[TEXT_AREA] + n
19281 < it->glyph_row->glyphs[1 + TEXT_AREA])
19282 {
19283 /* Save some values that must not be changed.
19284 Must save IT->c and IT->len because otherwise
19285 ITERATOR_AT_END_P wouldn't work anymore after
19286 append_space_for_newline has been called. */
19287 enum display_element_type saved_what = it->what;
19288 int saved_c = it->c, saved_len = it->len;
19289 int saved_char_to_display = it->char_to_display;
19290 int saved_x = it->current_x;
19291 int saved_face_id = it->face_id;
19292 bool saved_box_end = it->end_of_box_run_p;
19293 struct text_pos saved_pos;
19294 Lisp_Object saved_object;
19295 struct face *face;
19296 struct glyph *g;
19297
19298 saved_object = it->object;
19299 saved_pos = it->position;
19300
19301 it->what = IT_CHARACTER;
19302 memset (&it->position, 0, sizeof it->position);
19303 it->object = Qnil;
19304 it->c = it->char_to_display = ' ';
19305 it->len = 1;
19306
19307 /* If the default face was remapped, be sure to use the
19308 remapped face for the appended newline. */
19309 if (default_face_p)
19310 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19311 else if (it->face_before_selective_p)
19312 it->face_id = it->saved_face_id;
19313 face = FACE_FROM_ID (it->f, it->face_id);
19314 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19315 /* In R2L rows, we will prepend a stretch glyph that will
19316 have the end_of_box_run_p flag set for it, so there's no
19317 need for the appended newline glyph to have that flag
19318 set. */
19319 if (it->glyph_row->reversed_p
19320 /* But if the appended newline glyph goes all the way to
19321 the end of the row, there will be no stretch glyph,
19322 so leave the box flag set. */
19323 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19324 it->end_of_box_run_p = false;
19325
19326 PRODUCE_GLYPHS (it);
19327
19328 #ifdef HAVE_WINDOW_SYSTEM
19329 /* Make sure this space glyph has the right ascent and
19330 descent values, or else cursor at end of line will look
19331 funny, and height of empty lines will be incorrect. */
19332 g = it->glyph_row->glyphs[TEXT_AREA] + n;
19333 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19334 if (n == 0)
19335 {
19336 Lisp_Object height, total_height;
19337 int extra_line_spacing = it->extra_line_spacing;
19338 int boff = font->baseline_offset;
19339
19340 if (font->vertical_centering)
19341 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19342
19343 it->object = saved_object; /* get_it_property needs this */
19344 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19345 /* Must do a subset of line height processing from
19346 x_produce_glyph for newline characters. */
19347 height = get_it_property (it, Qline_height);
19348 if (CONSP (height)
19349 && CONSP (XCDR (height))
19350 && NILP (XCDR (XCDR (height))))
19351 {
19352 total_height = XCAR (XCDR (height));
19353 height = XCAR (height);
19354 }
19355 else
19356 total_height = Qnil;
19357 height = calc_line_height_property (it, height, font, boff, true);
19358
19359 if (it->override_ascent >= 0)
19360 {
19361 it->ascent = it->override_ascent;
19362 it->descent = it->override_descent;
19363 boff = it->override_boff;
19364 }
19365 if (EQ (height, Qt))
19366 extra_line_spacing = 0;
19367 else
19368 {
19369 Lisp_Object spacing;
19370
19371 it->phys_ascent = it->ascent;
19372 it->phys_descent = it->descent;
19373 if (!NILP (height)
19374 && XINT (height) > it->ascent + it->descent)
19375 it->ascent = XINT (height) - it->descent;
19376
19377 if (!NILP (total_height))
19378 spacing = calc_line_height_property (it, total_height, font,
19379 boff, false);
19380 else
19381 {
19382 spacing = get_it_property (it, Qline_spacing);
19383 spacing = calc_line_height_property (it, spacing, font,
19384 boff, false);
19385 }
19386 if (INTEGERP (spacing))
19387 {
19388 extra_line_spacing = XINT (spacing);
19389 if (!NILP (total_height))
19390 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19391 }
19392 }
19393 if (extra_line_spacing > 0)
19394 {
19395 it->descent += extra_line_spacing;
19396 if (extra_line_spacing > it->max_extra_line_spacing)
19397 it->max_extra_line_spacing = extra_line_spacing;
19398 }
19399 it->max_ascent = it->ascent;
19400 it->max_descent = it->descent;
19401 /* Make sure compute_line_metrics recomputes the row height. */
19402 it->glyph_row->height = 0;
19403 }
19404
19405 g->ascent = it->max_ascent;
19406 g->descent = it->max_descent;
19407 #endif
19408
19409 it->override_ascent = -1;
19410 it->constrain_row_ascent_descent_p = false;
19411 it->current_x = saved_x;
19412 it->object = saved_object;
19413 it->position = saved_pos;
19414 it->what = saved_what;
19415 it->face_id = saved_face_id;
19416 it->len = saved_len;
19417 it->c = saved_c;
19418 it->char_to_display = saved_char_to_display;
19419 it->end_of_box_run_p = saved_box_end;
19420 return true;
19421 }
19422 }
19423
19424 return false;
19425 }
19426
19427
19428 /* Extend the face of the last glyph in the text area of IT->glyph_row
19429 to the end of the display line. Called from display_line. If the
19430 glyph row is empty, add a space glyph to it so that we know the
19431 face to draw. Set the glyph row flag fill_line_p. If the glyph
19432 row is R2L, prepend a stretch glyph to cover the empty space to the
19433 left of the leftmost glyph. */
19434
19435 static void
19436 extend_face_to_end_of_line (struct it *it)
19437 {
19438 struct face *face, *default_face;
19439 struct frame *f = it->f;
19440
19441 /* If line is already filled, do nothing. Non window-system frames
19442 get a grace of one more ``pixel'' because their characters are
19443 1-``pixel'' wide, so they hit the equality too early. This grace
19444 is needed only for R2L rows that are not continued, to produce
19445 one extra blank where we could display the cursor. */
19446 if ((it->current_x >= it->last_visible_x
19447 + (!FRAME_WINDOW_P (f)
19448 && it->glyph_row->reversed_p
19449 && !it->glyph_row->continued_p))
19450 /* If the window has display margins, we will need to extend
19451 their face even if the text area is filled. */
19452 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19453 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19454 return;
19455
19456 /* The default face, possibly remapped. */
19457 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19458
19459 /* Face extension extends the background and box of IT->face_id
19460 to the end of the line. If the background equals the background
19461 of the frame, we don't have to do anything. */
19462 if (it->face_before_selective_p)
19463 face = FACE_FROM_ID (f, it->saved_face_id);
19464 else
19465 face = FACE_FROM_ID (f, it->face_id);
19466
19467 if (FRAME_WINDOW_P (f)
19468 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19469 && face->box == FACE_NO_BOX
19470 && face->background == FRAME_BACKGROUND_PIXEL (f)
19471 #ifdef HAVE_WINDOW_SYSTEM
19472 && !face->stipple
19473 #endif
19474 && !it->glyph_row->reversed_p)
19475 return;
19476
19477 /* Set the glyph row flag indicating that the face of the last glyph
19478 in the text area has to be drawn to the end of the text area. */
19479 it->glyph_row->fill_line_p = true;
19480
19481 /* If current character of IT is not ASCII, make sure we have the
19482 ASCII face. This will be automatically undone the next time
19483 get_next_display_element returns a multibyte character. Note
19484 that the character will always be single byte in unibyte
19485 text. */
19486 if (!ASCII_CHAR_P (it->c))
19487 {
19488 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19489 }
19490
19491 if (FRAME_WINDOW_P (f))
19492 {
19493 /* If the row is empty, add a space with the current face of IT,
19494 so that we know which face to draw. */
19495 if (it->glyph_row->used[TEXT_AREA] == 0)
19496 {
19497 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19498 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19499 it->glyph_row->used[TEXT_AREA] = 1;
19500 }
19501 /* Mode line and the header line don't have margins, and
19502 likewise the frame's tool-bar window, if there is any. */
19503 if (!(it->glyph_row->mode_line_p
19504 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19505 || (WINDOWP (f->tool_bar_window)
19506 && it->w == XWINDOW (f->tool_bar_window))
19507 #endif
19508 ))
19509 {
19510 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19511 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19512 {
19513 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19514 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19515 default_face->id;
19516 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19517 }
19518 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19519 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19520 {
19521 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19522 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19523 default_face->id;
19524 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19525 }
19526 }
19527 #ifdef HAVE_WINDOW_SYSTEM
19528 if (it->glyph_row->reversed_p)
19529 {
19530 /* Prepend a stretch glyph to the row, such that the
19531 rightmost glyph will be drawn flushed all the way to the
19532 right margin of the window. The stretch glyph that will
19533 occupy the empty space, if any, to the left of the
19534 glyphs. */
19535 struct font *font = face->font ? face->font : FRAME_FONT (f);
19536 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19537 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19538 struct glyph *g;
19539 int row_width, stretch_ascent, stretch_width;
19540 struct text_pos saved_pos;
19541 int saved_face_id;
19542 bool saved_avoid_cursor, saved_box_start;
19543
19544 for (row_width = 0, g = row_start; g < row_end; g++)
19545 row_width += g->pixel_width;
19546
19547 /* FIXME: There are various minor display glitches in R2L
19548 rows when only one of the fringes is missing. The
19549 strange condition below produces the least bad effect. */
19550 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19551 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19552 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19553 stretch_width = window_box_width (it->w, TEXT_AREA);
19554 else
19555 stretch_width = it->last_visible_x - it->first_visible_x;
19556 stretch_width -= row_width;
19557
19558 if (stretch_width > 0)
19559 {
19560 stretch_ascent =
19561 (((it->ascent + it->descent)
19562 * FONT_BASE (font)) / FONT_HEIGHT (font));
19563 saved_pos = it->position;
19564 memset (&it->position, 0, sizeof it->position);
19565 saved_avoid_cursor = it->avoid_cursor_p;
19566 it->avoid_cursor_p = true;
19567 saved_face_id = it->face_id;
19568 saved_box_start = it->start_of_box_run_p;
19569 /* The last row's stretch glyph should get the default
19570 face, to avoid painting the rest of the window with
19571 the region face, if the region ends at ZV. */
19572 if (it->glyph_row->ends_at_zv_p)
19573 it->face_id = default_face->id;
19574 else
19575 it->face_id = face->id;
19576 it->start_of_box_run_p = false;
19577 append_stretch_glyph (it, Qnil, stretch_width,
19578 it->ascent + it->descent, stretch_ascent);
19579 it->position = saved_pos;
19580 it->avoid_cursor_p = saved_avoid_cursor;
19581 it->face_id = saved_face_id;
19582 it->start_of_box_run_p = saved_box_start;
19583 }
19584 /* If stretch_width comes out negative, it means that the
19585 last glyph is only partially visible. In R2L rows, we
19586 want the leftmost glyph to be partially visible, so we
19587 need to give the row the corresponding left offset. */
19588 if (stretch_width < 0)
19589 it->glyph_row->x = stretch_width;
19590 }
19591 #endif /* HAVE_WINDOW_SYSTEM */
19592 }
19593 else
19594 {
19595 /* Save some values that must not be changed. */
19596 int saved_x = it->current_x;
19597 struct text_pos saved_pos;
19598 Lisp_Object saved_object;
19599 enum display_element_type saved_what = it->what;
19600 int saved_face_id = it->face_id;
19601
19602 saved_object = it->object;
19603 saved_pos = it->position;
19604
19605 it->what = IT_CHARACTER;
19606 memset (&it->position, 0, sizeof it->position);
19607 it->object = Qnil;
19608 it->c = it->char_to_display = ' ';
19609 it->len = 1;
19610
19611 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19612 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19613 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19614 && !it->glyph_row->mode_line_p
19615 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19616 {
19617 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19618 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19619
19620 for (it->current_x = 0; g < e; g++)
19621 it->current_x += g->pixel_width;
19622
19623 it->area = LEFT_MARGIN_AREA;
19624 it->face_id = default_face->id;
19625 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19626 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19627 {
19628 PRODUCE_GLYPHS (it);
19629 /* term.c:produce_glyphs advances it->current_x only for
19630 TEXT_AREA. */
19631 it->current_x += it->pixel_width;
19632 }
19633
19634 it->current_x = saved_x;
19635 it->area = TEXT_AREA;
19636 }
19637
19638 /* The last row's blank glyphs should get the default face, to
19639 avoid painting the rest of the window with the region face,
19640 if the region ends at ZV. */
19641 if (it->glyph_row->ends_at_zv_p)
19642 it->face_id = default_face->id;
19643 else
19644 it->face_id = face->id;
19645 PRODUCE_GLYPHS (it);
19646
19647 while (it->current_x <= it->last_visible_x)
19648 PRODUCE_GLYPHS (it);
19649
19650 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19651 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19652 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19653 && !it->glyph_row->mode_line_p
19654 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19655 {
19656 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19657 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19658
19659 for ( ; g < e; g++)
19660 it->current_x += g->pixel_width;
19661
19662 it->area = RIGHT_MARGIN_AREA;
19663 it->face_id = default_face->id;
19664 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19665 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19666 {
19667 PRODUCE_GLYPHS (it);
19668 it->current_x += it->pixel_width;
19669 }
19670
19671 it->area = TEXT_AREA;
19672 }
19673
19674 /* Don't count these blanks really. It would let us insert a left
19675 truncation glyph below and make us set the cursor on them, maybe. */
19676 it->current_x = saved_x;
19677 it->object = saved_object;
19678 it->position = saved_pos;
19679 it->what = saved_what;
19680 it->face_id = saved_face_id;
19681 }
19682 }
19683
19684
19685 /* Value is true if text starting at CHARPOS in current_buffer is
19686 trailing whitespace. */
19687
19688 static bool
19689 trailing_whitespace_p (ptrdiff_t charpos)
19690 {
19691 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19692 int c = 0;
19693
19694 while (bytepos < ZV_BYTE
19695 && (c = FETCH_CHAR (bytepos),
19696 c == ' ' || c == '\t'))
19697 ++bytepos;
19698
19699 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19700 {
19701 if (bytepos != PT_BYTE)
19702 return true;
19703 }
19704 return false;
19705 }
19706
19707
19708 /* Highlight trailing whitespace, if any, in ROW. */
19709
19710 static void
19711 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19712 {
19713 int used = row->used[TEXT_AREA];
19714
19715 if (used)
19716 {
19717 struct glyph *start = row->glyphs[TEXT_AREA];
19718 struct glyph *glyph = start + used - 1;
19719
19720 if (row->reversed_p)
19721 {
19722 /* Right-to-left rows need to be processed in the opposite
19723 direction, so swap the edge pointers. */
19724 glyph = start;
19725 start = row->glyphs[TEXT_AREA] + used - 1;
19726 }
19727
19728 /* Skip over glyphs inserted to display the cursor at the
19729 end of a line, for extending the face of the last glyph
19730 to the end of the line on terminals, and for truncation
19731 and continuation glyphs. */
19732 if (!row->reversed_p)
19733 {
19734 while (glyph >= start
19735 && glyph->type == CHAR_GLYPH
19736 && NILP (glyph->object))
19737 --glyph;
19738 }
19739 else
19740 {
19741 while (glyph <= start
19742 && glyph->type == CHAR_GLYPH
19743 && NILP (glyph->object))
19744 ++glyph;
19745 }
19746
19747 /* If last glyph is a space or stretch, and it's trailing
19748 whitespace, set the face of all trailing whitespace glyphs in
19749 IT->glyph_row to `trailing-whitespace'. */
19750 if ((row->reversed_p ? glyph <= start : glyph >= start)
19751 && BUFFERP (glyph->object)
19752 && (glyph->type == STRETCH_GLYPH
19753 || (glyph->type == CHAR_GLYPH
19754 && glyph->u.ch == ' '))
19755 && trailing_whitespace_p (glyph->charpos))
19756 {
19757 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19758 if (face_id < 0)
19759 return;
19760
19761 if (!row->reversed_p)
19762 {
19763 while (glyph >= start
19764 && BUFFERP (glyph->object)
19765 && (glyph->type == STRETCH_GLYPH
19766 || (glyph->type == CHAR_GLYPH
19767 && glyph->u.ch == ' ')))
19768 (glyph--)->face_id = face_id;
19769 }
19770 else
19771 {
19772 while (glyph <= start
19773 && BUFFERP (glyph->object)
19774 && (glyph->type == STRETCH_GLYPH
19775 || (glyph->type == CHAR_GLYPH
19776 && glyph->u.ch == ' ')))
19777 (glyph++)->face_id = face_id;
19778 }
19779 }
19780 }
19781 }
19782
19783
19784 /* Value is true if glyph row ROW should be
19785 considered to hold the buffer position CHARPOS. */
19786
19787 static bool
19788 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19789 {
19790 bool result = true;
19791
19792 if (charpos == CHARPOS (row->end.pos)
19793 || charpos == MATRIX_ROW_END_CHARPOS (row))
19794 {
19795 /* Suppose the row ends on a string.
19796 Unless the row is continued, that means it ends on a newline
19797 in the string. If it's anything other than a display string
19798 (e.g., a before-string from an overlay), we don't want the
19799 cursor there. (This heuristic seems to give the optimal
19800 behavior for the various types of multi-line strings.)
19801 One exception: if the string has `cursor' property on one of
19802 its characters, we _do_ want the cursor there. */
19803 if (CHARPOS (row->end.string_pos) >= 0)
19804 {
19805 if (row->continued_p)
19806 result = true;
19807 else
19808 {
19809 /* Check for `display' property. */
19810 struct glyph *beg = row->glyphs[TEXT_AREA];
19811 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19812 struct glyph *glyph;
19813
19814 result = false;
19815 for (glyph = end; glyph >= beg; --glyph)
19816 if (STRINGP (glyph->object))
19817 {
19818 Lisp_Object prop
19819 = Fget_char_property (make_number (charpos),
19820 Qdisplay, Qnil);
19821 result =
19822 (!NILP (prop)
19823 && display_prop_string_p (prop, glyph->object));
19824 /* If there's a `cursor' property on one of the
19825 string's characters, this row is a cursor row,
19826 even though this is not a display string. */
19827 if (!result)
19828 {
19829 Lisp_Object s = glyph->object;
19830
19831 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19832 {
19833 ptrdiff_t gpos = glyph->charpos;
19834
19835 if (!NILP (Fget_char_property (make_number (gpos),
19836 Qcursor, s)))
19837 {
19838 result = true;
19839 break;
19840 }
19841 }
19842 }
19843 break;
19844 }
19845 }
19846 }
19847 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19848 {
19849 /* If the row ends in middle of a real character,
19850 and the line is continued, we want the cursor here.
19851 That's because CHARPOS (ROW->end.pos) would equal
19852 PT if PT is before the character. */
19853 if (!row->ends_in_ellipsis_p)
19854 result = row->continued_p;
19855 else
19856 /* If the row ends in an ellipsis, then
19857 CHARPOS (ROW->end.pos) will equal point after the
19858 invisible text. We want that position to be displayed
19859 after the ellipsis. */
19860 result = false;
19861 }
19862 /* If the row ends at ZV, display the cursor at the end of that
19863 row instead of at the start of the row below. */
19864 else
19865 result = row->ends_at_zv_p;
19866 }
19867
19868 return result;
19869 }
19870
19871 /* Value is true if glyph row ROW should be
19872 used to hold the cursor. */
19873
19874 static bool
19875 cursor_row_p (struct glyph_row *row)
19876 {
19877 return row_for_charpos_p (row, PT);
19878 }
19879
19880 \f
19881
19882 /* Push the property PROP so that it will be rendered at the current
19883 position in IT. Return true if PROP was successfully pushed, false
19884 otherwise. Called from handle_line_prefix to handle the
19885 `line-prefix' and `wrap-prefix' properties. */
19886
19887 static bool
19888 push_prefix_prop (struct it *it, Lisp_Object prop)
19889 {
19890 struct text_pos pos =
19891 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19892
19893 eassert (it->method == GET_FROM_BUFFER
19894 || it->method == GET_FROM_DISPLAY_VECTOR
19895 || it->method == GET_FROM_STRING
19896 || it->method == GET_FROM_IMAGE);
19897
19898 /* We need to save the current buffer/string position, so it will be
19899 restored by pop_it, because iterate_out_of_display_property
19900 depends on that being set correctly, but some situations leave
19901 it->position not yet set when this function is called. */
19902 push_it (it, &pos);
19903
19904 if (STRINGP (prop))
19905 {
19906 if (SCHARS (prop) == 0)
19907 {
19908 pop_it (it);
19909 return false;
19910 }
19911
19912 it->string = prop;
19913 it->string_from_prefix_prop_p = true;
19914 it->multibyte_p = STRING_MULTIBYTE (it->string);
19915 it->current.overlay_string_index = -1;
19916 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19917 it->end_charpos = it->string_nchars = SCHARS (it->string);
19918 it->method = GET_FROM_STRING;
19919 it->stop_charpos = 0;
19920 it->prev_stop = 0;
19921 it->base_level_stop = 0;
19922
19923 /* Force paragraph direction to be that of the parent
19924 buffer/string. */
19925 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19926 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19927 else
19928 it->paragraph_embedding = L2R;
19929
19930 /* Set up the bidi iterator for this display string. */
19931 if (it->bidi_p)
19932 {
19933 it->bidi_it.string.lstring = it->string;
19934 it->bidi_it.string.s = NULL;
19935 it->bidi_it.string.schars = it->end_charpos;
19936 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19937 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19938 it->bidi_it.string.unibyte = !it->multibyte_p;
19939 it->bidi_it.w = it->w;
19940 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19941 }
19942 }
19943 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19944 {
19945 it->method = GET_FROM_STRETCH;
19946 it->object = prop;
19947 }
19948 #ifdef HAVE_WINDOW_SYSTEM
19949 else if (IMAGEP (prop))
19950 {
19951 it->what = IT_IMAGE;
19952 it->image_id = lookup_image (it->f, prop);
19953 it->method = GET_FROM_IMAGE;
19954 }
19955 #endif /* HAVE_WINDOW_SYSTEM */
19956 else
19957 {
19958 pop_it (it); /* bogus display property, give up */
19959 return false;
19960 }
19961
19962 return true;
19963 }
19964
19965 /* Return the character-property PROP at the current position in IT. */
19966
19967 static Lisp_Object
19968 get_it_property (struct it *it, Lisp_Object prop)
19969 {
19970 Lisp_Object position, object = it->object;
19971
19972 if (STRINGP (object))
19973 position = make_number (IT_STRING_CHARPOS (*it));
19974 else if (BUFFERP (object))
19975 {
19976 position = make_number (IT_CHARPOS (*it));
19977 object = it->window;
19978 }
19979 else
19980 return Qnil;
19981
19982 return Fget_char_property (position, prop, object);
19983 }
19984
19985 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19986
19987 static void
19988 handle_line_prefix (struct it *it)
19989 {
19990 Lisp_Object prefix;
19991
19992 if (it->continuation_lines_width > 0)
19993 {
19994 prefix = get_it_property (it, Qwrap_prefix);
19995 if (NILP (prefix))
19996 prefix = Vwrap_prefix;
19997 }
19998 else
19999 {
20000 prefix = get_it_property (it, Qline_prefix);
20001 if (NILP (prefix))
20002 prefix = Vline_prefix;
20003 }
20004 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20005 {
20006 /* If the prefix is wider than the window, and we try to wrap
20007 it, it would acquire its own wrap prefix, and so on till the
20008 iterator stack overflows. So, don't wrap the prefix. */
20009 it->line_wrap = TRUNCATE;
20010 it->avoid_cursor_p = true;
20011 }
20012 }
20013
20014 \f
20015
20016 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20017 only for R2L lines from display_line and display_string, when they
20018 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20019 the line/string needs to be continued on the next glyph row. */
20020 static void
20021 unproduce_glyphs (struct it *it, int n)
20022 {
20023 struct glyph *glyph, *end;
20024
20025 eassert (it->glyph_row);
20026 eassert (it->glyph_row->reversed_p);
20027 eassert (it->area == TEXT_AREA);
20028 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20029
20030 if (n > it->glyph_row->used[TEXT_AREA])
20031 n = it->glyph_row->used[TEXT_AREA];
20032 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20033 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20034 for ( ; glyph < end; glyph++)
20035 glyph[-n] = *glyph;
20036 }
20037
20038 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20039 and ROW->maxpos. */
20040 static void
20041 find_row_edges (struct it *it, struct glyph_row *row,
20042 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20043 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20044 {
20045 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20046 lines' rows is implemented for bidi-reordered rows. */
20047
20048 /* ROW->minpos is the value of min_pos, the minimal buffer position
20049 we have in ROW, or ROW->start.pos if that is smaller. */
20050 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20051 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20052 else
20053 /* We didn't find buffer positions smaller than ROW->start, or
20054 didn't find _any_ valid buffer positions in any of the glyphs,
20055 so we must trust the iterator's computed positions. */
20056 row->minpos = row->start.pos;
20057 if (max_pos <= 0)
20058 {
20059 max_pos = CHARPOS (it->current.pos);
20060 max_bpos = BYTEPOS (it->current.pos);
20061 }
20062
20063 /* Here are the various use-cases for ending the row, and the
20064 corresponding values for ROW->maxpos:
20065
20066 Line ends in a newline from buffer eol_pos + 1
20067 Line is continued from buffer max_pos + 1
20068 Line is truncated on right it->current.pos
20069 Line ends in a newline from string max_pos + 1(*)
20070 (*) + 1 only when line ends in a forward scan
20071 Line is continued from string max_pos
20072 Line is continued from display vector max_pos
20073 Line is entirely from a string min_pos == max_pos
20074 Line is entirely from a display vector min_pos == max_pos
20075 Line that ends at ZV ZV
20076
20077 If you discover other use-cases, please add them here as
20078 appropriate. */
20079 if (row->ends_at_zv_p)
20080 row->maxpos = it->current.pos;
20081 else if (row->used[TEXT_AREA])
20082 {
20083 bool seen_this_string = false;
20084 struct glyph_row *r1 = row - 1;
20085
20086 /* Did we see the same display string on the previous row? */
20087 if (STRINGP (it->object)
20088 /* this is not the first row */
20089 && row > it->w->desired_matrix->rows
20090 /* previous row is not the header line */
20091 && !r1->mode_line_p
20092 /* previous row also ends in a newline from a string */
20093 && r1->ends_in_newline_from_string_p)
20094 {
20095 struct glyph *start, *end;
20096
20097 /* Search for the last glyph of the previous row that came
20098 from buffer or string. Depending on whether the row is
20099 L2R or R2L, we need to process it front to back or the
20100 other way round. */
20101 if (!r1->reversed_p)
20102 {
20103 start = r1->glyphs[TEXT_AREA];
20104 end = start + r1->used[TEXT_AREA];
20105 /* Glyphs inserted by redisplay have nil as their object. */
20106 while (end > start
20107 && NILP ((end - 1)->object)
20108 && (end - 1)->charpos <= 0)
20109 --end;
20110 if (end > start)
20111 {
20112 if (EQ ((end - 1)->object, it->object))
20113 seen_this_string = true;
20114 }
20115 else
20116 /* If all the glyphs of the previous row were inserted
20117 by redisplay, it means the previous row was
20118 produced from a single newline, which is only
20119 possible if that newline came from the same string
20120 as the one which produced this ROW. */
20121 seen_this_string = true;
20122 }
20123 else
20124 {
20125 end = r1->glyphs[TEXT_AREA] - 1;
20126 start = end + r1->used[TEXT_AREA];
20127 while (end < start
20128 && NILP ((end + 1)->object)
20129 && (end + 1)->charpos <= 0)
20130 ++end;
20131 if (end < start)
20132 {
20133 if (EQ ((end + 1)->object, it->object))
20134 seen_this_string = true;
20135 }
20136 else
20137 seen_this_string = true;
20138 }
20139 }
20140 /* Take note of each display string that covers a newline only
20141 once, the first time we see it. This is for when a display
20142 string includes more than one newline in it. */
20143 if (row->ends_in_newline_from_string_p && !seen_this_string)
20144 {
20145 /* If we were scanning the buffer forward when we displayed
20146 the string, we want to account for at least one buffer
20147 position that belongs to this row (position covered by
20148 the display string), so that cursor positioning will
20149 consider this row as a candidate when point is at the end
20150 of the visual line represented by this row. This is not
20151 required when scanning back, because max_pos will already
20152 have a much larger value. */
20153 if (CHARPOS (row->end.pos) > max_pos)
20154 INC_BOTH (max_pos, max_bpos);
20155 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20156 }
20157 else if (CHARPOS (it->eol_pos) > 0)
20158 SET_TEXT_POS (row->maxpos,
20159 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20160 else if (row->continued_p)
20161 {
20162 /* If max_pos is different from IT's current position, it
20163 means IT->method does not belong to the display element
20164 at max_pos. However, it also means that the display
20165 element at max_pos was displayed in its entirety on this
20166 line, which is equivalent to saying that the next line
20167 starts at the next buffer position. */
20168 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20169 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20170 else
20171 {
20172 INC_BOTH (max_pos, max_bpos);
20173 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20174 }
20175 }
20176 else if (row->truncated_on_right_p)
20177 /* display_line already called reseat_at_next_visible_line_start,
20178 which puts the iterator at the beginning of the next line, in
20179 the logical order. */
20180 row->maxpos = it->current.pos;
20181 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20182 /* A line that is entirely from a string/image/stretch... */
20183 row->maxpos = row->minpos;
20184 else
20185 emacs_abort ();
20186 }
20187 else
20188 row->maxpos = it->current.pos;
20189 }
20190
20191 /* Construct the glyph row IT->glyph_row in the desired matrix of
20192 IT->w from text at the current position of IT. See dispextern.h
20193 for an overview of struct it. Value is true if
20194 IT->glyph_row displays text, as opposed to a line displaying ZV
20195 only. */
20196
20197 static bool
20198 display_line (struct it *it)
20199 {
20200 struct glyph_row *row = it->glyph_row;
20201 Lisp_Object overlay_arrow_string;
20202 struct it wrap_it;
20203 void *wrap_data = NULL;
20204 bool may_wrap = false;
20205 int wrap_x IF_LINT (= 0);
20206 int wrap_row_used = -1;
20207 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20208 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20209 int wrap_row_extra_line_spacing IF_LINT (= 0);
20210 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20211 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20212 int cvpos;
20213 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20214 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20215 bool pending_handle_line_prefix = false;
20216
20217 /* We always start displaying at hpos zero even if hscrolled. */
20218 eassert (it->hpos == 0 && it->current_x == 0);
20219
20220 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20221 >= it->w->desired_matrix->nrows)
20222 {
20223 it->w->nrows_scale_factor++;
20224 it->f->fonts_changed = true;
20225 return false;
20226 }
20227
20228 /* Clear the result glyph row and enable it. */
20229 prepare_desired_row (it->w, row, false);
20230
20231 row->y = it->current_y;
20232 row->start = it->start;
20233 row->continuation_lines_width = it->continuation_lines_width;
20234 row->displays_text_p = true;
20235 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20236 it->starts_in_middle_of_char_p = false;
20237
20238 /* Arrange the overlays nicely for our purposes. Usually, we call
20239 display_line on only one line at a time, in which case this
20240 can't really hurt too much, or we call it on lines which appear
20241 one after another in the buffer, in which case all calls to
20242 recenter_overlay_lists but the first will be pretty cheap. */
20243 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20244
20245 /* Move over display elements that are not visible because we are
20246 hscrolled. This may stop at an x-position < IT->first_visible_x
20247 if the first glyph is partially visible or if we hit a line end. */
20248 if (it->current_x < it->first_visible_x)
20249 {
20250 enum move_it_result move_result;
20251
20252 this_line_min_pos = row->start.pos;
20253 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20254 MOVE_TO_POS | MOVE_TO_X);
20255 /* If we are under a large hscroll, move_it_in_display_line_to
20256 could hit the end of the line without reaching
20257 it->first_visible_x. Pretend that we did reach it. This is
20258 especially important on a TTY, where we will call
20259 extend_face_to_end_of_line, which needs to know how many
20260 blank glyphs to produce. */
20261 if (it->current_x < it->first_visible_x
20262 && (move_result == MOVE_NEWLINE_OR_CR
20263 || move_result == MOVE_POS_MATCH_OR_ZV))
20264 it->current_x = it->first_visible_x;
20265
20266 /* Record the smallest positions seen while we moved over
20267 display elements that are not visible. This is needed by
20268 redisplay_internal for optimizing the case where the cursor
20269 stays inside the same line. The rest of this function only
20270 considers positions that are actually displayed, so
20271 RECORD_MAX_MIN_POS will not otherwise record positions that
20272 are hscrolled to the left of the left edge of the window. */
20273 min_pos = CHARPOS (this_line_min_pos);
20274 min_bpos = BYTEPOS (this_line_min_pos);
20275 }
20276 else if (it->area == TEXT_AREA)
20277 {
20278 /* We only do this when not calling move_it_in_display_line_to
20279 above, because that function calls itself handle_line_prefix. */
20280 handle_line_prefix (it);
20281 }
20282 else
20283 {
20284 /* Line-prefix and wrap-prefix are always displayed in the text
20285 area. But if this is the first call to display_line after
20286 init_iterator, the iterator might have been set up to write
20287 into a marginal area, e.g. if the line begins with some
20288 display property that writes to the margins. So we need to
20289 wait with the call to handle_line_prefix until whatever
20290 writes to the margin has done its job. */
20291 pending_handle_line_prefix = true;
20292 }
20293
20294 /* Get the initial row height. This is either the height of the
20295 text hscrolled, if there is any, or zero. */
20296 row->ascent = it->max_ascent;
20297 row->height = it->max_ascent + it->max_descent;
20298 row->phys_ascent = it->max_phys_ascent;
20299 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20300 row->extra_line_spacing = it->max_extra_line_spacing;
20301
20302 /* Utility macro to record max and min buffer positions seen until now. */
20303 #define RECORD_MAX_MIN_POS(IT) \
20304 do \
20305 { \
20306 bool composition_p \
20307 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20308 ptrdiff_t current_pos = \
20309 composition_p ? (IT)->cmp_it.charpos \
20310 : IT_CHARPOS (*(IT)); \
20311 ptrdiff_t current_bpos = \
20312 composition_p ? CHAR_TO_BYTE (current_pos) \
20313 : IT_BYTEPOS (*(IT)); \
20314 if (current_pos < min_pos) \
20315 { \
20316 min_pos = current_pos; \
20317 min_bpos = current_bpos; \
20318 } \
20319 if (IT_CHARPOS (*it) > max_pos) \
20320 { \
20321 max_pos = IT_CHARPOS (*it); \
20322 max_bpos = IT_BYTEPOS (*it); \
20323 } \
20324 } \
20325 while (false)
20326
20327 /* Loop generating characters. The loop is left with IT on the next
20328 character to display. */
20329 while (true)
20330 {
20331 int n_glyphs_before, hpos_before, x_before;
20332 int x, nglyphs;
20333 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20334
20335 /* Retrieve the next thing to display. Value is false if end of
20336 buffer reached. */
20337 if (!get_next_display_element (it))
20338 {
20339 /* Maybe add a space at the end of this line that is used to
20340 display the cursor there under X. Set the charpos of the
20341 first glyph of blank lines not corresponding to any text
20342 to -1. */
20343 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20344 row->exact_window_width_line_p = true;
20345 else if ((append_space_for_newline (it, true)
20346 && row->used[TEXT_AREA] == 1)
20347 || row->used[TEXT_AREA] == 0)
20348 {
20349 row->glyphs[TEXT_AREA]->charpos = -1;
20350 row->displays_text_p = false;
20351
20352 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20353 && (!MINI_WINDOW_P (it->w)
20354 || (minibuf_level && EQ (it->window, minibuf_window))))
20355 row->indicate_empty_line_p = true;
20356 }
20357
20358 it->continuation_lines_width = 0;
20359 row->ends_at_zv_p = true;
20360 /* A row that displays right-to-left text must always have
20361 its last face extended all the way to the end of line,
20362 even if this row ends in ZV, because we still write to
20363 the screen left to right. We also need to extend the
20364 last face if the default face is remapped to some
20365 different face, otherwise the functions that clear
20366 portions of the screen will clear with the default face's
20367 background color. */
20368 if (row->reversed_p
20369 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20370 extend_face_to_end_of_line (it);
20371 break;
20372 }
20373
20374 /* Now, get the metrics of what we want to display. This also
20375 generates glyphs in `row' (which is IT->glyph_row). */
20376 n_glyphs_before = row->used[TEXT_AREA];
20377 x = it->current_x;
20378
20379 /* Remember the line height so far in case the next element doesn't
20380 fit on the line. */
20381 if (it->line_wrap != TRUNCATE)
20382 {
20383 ascent = it->max_ascent;
20384 descent = it->max_descent;
20385 phys_ascent = it->max_phys_ascent;
20386 phys_descent = it->max_phys_descent;
20387
20388 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20389 {
20390 if (IT_DISPLAYING_WHITESPACE (it))
20391 may_wrap = true;
20392 else if (may_wrap)
20393 {
20394 SAVE_IT (wrap_it, *it, wrap_data);
20395 wrap_x = x;
20396 wrap_row_used = row->used[TEXT_AREA];
20397 wrap_row_ascent = row->ascent;
20398 wrap_row_height = row->height;
20399 wrap_row_phys_ascent = row->phys_ascent;
20400 wrap_row_phys_height = row->phys_height;
20401 wrap_row_extra_line_spacing = row->extra_line_spacing;
20402 wrap_row_min_pos = min_pos;
20403 wrap_row_min_bpos = min_bpos;
20404 wrap_row_max_pos = max_pos;
20405 wrap_row_max_bpos = max_bpos;
20406 may_wrap = false;
20407 }
20408 }
20409 }
20410
20411 PRODUCE_GLYPHS (it);
20412
20413 /* If this display element was in marginal areas, continue with
20414 the next one. */
20415 if (it->area != TEXT_AREA)
20416 {
20417 row->ascent = max (row->ascent, it->max_ascent);
20418 row->height = max (row->height, it->max_ascent + it->max_descent);
20419 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20420 row->phys_height = max (row->phys_height,
20421 it->max_phys_ascent + it->max_phys_descent);
20422 row->extra_line_spacing = max (row->extra_line_spacing,
20423 it->max_extra_line_spacing);
20424 set_iterator_to_next (it, true);
20425 /* If we didn't handle the line/wrap prefix above, and the
20426 call to set_iterator_to_next just switched to TEXT_AREA,
20427 process the prefix now. */
20428 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20429 {
20430 pending_handle_line_prefix = false;
20431 handle_line_prefix (it);
20432 }
20433 continue;
20434 }
20435
20436 /* Does the display element fit on the line? If we truncate
20437 lines, we should draw past the right edge of the window. If
20438 we don't truncate, we want to stop so that we can display the
20439 continuation glyph before the right margin. If lines are
20440 continued, there are two possible strategies for characters
20441 resulting in more than 1 glyph (e.g. tabs): Display as many
20442 glyphs as possible in this line and leave the rest for the
20443 continuation line, or display the whole element in the next
20444 line. Original redisplay did the former, so we do it also. */
20445 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20446 hpos_before = it->hpos;
20447 x_before = x;
20448
20449 if (/* Not a newline. */
20450 nglyphs > 0
20451 /* Glyphs produced fit entirely in the line. */
20452 && it->current_x < it->last_visible_x)
20453 {
20454 it->hpos += nglyphs;
20455 row->ascent = max (row->ascent, it->max_ascent);
20456 row->height = max (row->height, it->max_ascent + it->max_descent);
20457 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20458 row->phys_height = max (row->phys_height,
20459 it->max_phys_ascent + it->max_phys_descent);
20460 row->extra_line_spacing = max (row->extra_line_spacing,
20461 it->max_extra_line_spacing);
20462 if (it->current_x - it->pixel_width < it->first_visible_x
20463 /* In R2L rows, we arrange in extend_face_to_end_of_line
20464 to add a right offset to the line, by a suitable
20465 change to the stretch glyph that is the leftmost
20466 glyph of the line. */
20467 && !row->reversed_p)
20468 row->x = x - it->first_visible_x;
20469 /* Record the maximum and minimum buffer positions seen so
20470 far in glyphs that will be displayed by this row. */
20471 if (it->bidi_p)
20472 RECORD_MAX_MIN_POS (it);
20473 }
20474 else
20475 {
20476 int i, new_x;
20477 struct glyph *glyph;
20478
20479 for (i = 0; i < nglyphs; ++i, x = new_x)
20480 {
20481 /* Identify the glyphs added by the last call to
20482 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20483 the previous glyphs. */
20484 if (!row->reversed_p)
20485 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20486 else
20487 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20488 new_x = x + glyph->pixel_width;
20489
20490 if (/* Lines are continued. */
20491 it->line_wrap != TRUNCATE
20492 && (/* Glyph doesn't fit on the line. */
20493 new_x > it->last_visible_x
20494 /* Or it fits exactly on a window system frame. */
20495 || (new_x == it->last_visible_x
20496 && FRAME_WINDOW_P (it->f)
20497 && (row->reversed_p
20498 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20499 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20500 {
20501 /* End of a continued line. */
20502
20503 if (it->hpos == 0
20504 || (new_x == it->last_visible_x
20505 && FRAME_WINDOW_P (it->f)
20506 && (row->reversed_p
20507 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20508 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20509 {
20510 /* Current glyph is the only one on the line or
20511 fits exactly on the line. We must continue
20512 the line because we can't draw the cursor
20513 after the glyph. */
20514 row->continued_p = true;
20515 it->current_x = new_x;
20516 it->continuation_lines_width += new_x;
20517 ++it->hpos;
20518 if (i == nglyphs - 1)
20519 {
20520 /* If line-wrap is on, check if a previous
20521 wrap point was found. */
20522 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20523 && wrap_row_used > 0
20524 /* Even if there is a previous wrap
20525 point, continue the line here as
20526 usual, if (i) the previous character
20527 was a space or tab AND (ii) the
20528 current character is not. */
20529 && (!may_wrap
20530 || IT_DISPLAYING_WHITESPACE (it)))
20531 goto back_to_wrap;
20532
20533 /* Record the maximum and minimum buffer
20534 positions seen so far in glyphs that will be
20535 displayed by this row. */
20536 if (it->bidi_p)
20537 RECORD_MAX_MIN_POS (it);
20538 set_iterator_to_next (it, true);
20539 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20540 {
20541 if (!get_next_display_element (it))
20542 {
20543 row->exact_window_width_line_p = true;
20544 it->continuation_lines_width = 0;
20545 row->continued_p = false;
20546 row->ends_at_zv_p = true;
20547 }
20548 else if (ITERATOR_AT_END_OF_LINE_P (it))
20549 {
20550 row->continued_p = false;
20551 row->exact_window_width_line_p = true;
20552 }
20553 /* If line-wrap is on, check if a
20554 previous wrap point was found. */
20555 else if (wrap_row_used > 0
20556 /* Even if there is a previous wrap
20557 point, continue the line here as
20558 usual, if (i) the previous character
20559 was a space or tab AND (ii) the
20560 current character is not. */
20561 && (!may_wrap
20562 || IT_DISPLAYING_WHITESPACE (it)))
20563 goto back_to_wrap;
20564
20565 }
20566 }
20567 else if (it->bidi_p)
20568 RECORD_MAX_MIN_POS (it);
20569 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20570 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20571 extend_face_to_end_of_line (it);
20572 }
20573 else if (CHAR_GLYPH_PADDING_P (*glyph)
20574 && !FRAME_WINDOW_P (it->f))
20575 {
20576 /* A padding glyph that doesn't fit on this line.
20577 This means the whole character doesn't fit
20578 on the line. */
20579 if (row->reversed_p)
20580 unproduce_glyphs (it, row->used[TEXT_AREA]
20581 - n_glyphs_before);
20582 row->used[TEXT_AREA] = n_glyphs_before;
20583
20584 /* Fill the rest of the row with continuation
20585 glyphs like in 20.x. */
20586 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20587 < row->glyphs[1 + TEXT_AREA])
20588 produce_special_glyphs (it, IT_CONTINUATION);
20589
20590 row->continued_p = true;
20591 it->current_x = x_before;
20592 it->continuation_lines_width += x_before;
20593
20594 /* Restore the height to what it was before the
20595 element not fitting on the line. */
20596 it->max_ascent = ascent;
20597 it->max_descent = descent;
20598 it->max_phys_ascent = phys_ascent;
20599 it->max_phys_descent = phys_descent;
20600 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20601 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20602 extend_face_to_end_of_line (it);
20603 }
20604 else if (wrap_row_used > 0)
20605 {
20606 back_to_wrap:
20607 if (row->reversed_p)
20608 unproduce_glyphs (it,
20609 row->used[TEXT_AREA] - wrap_row_used);
20610 RESTORE_IT (it, &wrap_it, wrap_data);
20611 it->continuation_lines_width += wrap_x;
20612 row->used[TEXT_AREA] = wrap_row_used;
20613 row->ascent = wrap_row_ascent;
20614 row->height = wrap_row_height;
20615 row->phys_ascent = wrap_row_phys_ascent;
20616 row->phys_height = wrap_row_phys_height;
20617 row->extra_line_spacing = wrap_row_extra_line_spacing;
20618 min_pos = wrap_row_min_pos;
20619 min_bpos = wrap_row_min_bpos;
20620 max_pos = wrap_row_max_pos;
20621 max_bpos = wrap_row_max_bpos;
20622 row->continued_p = true;
20623 row->ends_at_zv_p = false;
20624 row->exact_window_width_line_p = false;
20625 it->continuation_lines_width += x;
20626
20627 /* Make sure that a non-default face is extended
20628 up to the right margin of the window. */
20629 extend_face_to_end_of_line (it);
20630 }
20631 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20632 {
20633 /* A TAB that extends past the right edge of the
20634 window. This produces a single glyph on
20635 window system frames. We leave the glyph in
20636 this row and let it fill the row, but don't
20637 consume the TAB. */
20638 if ((row->reversed_p
20639 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20640 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20641 produce_special_glyphs (it, IT_CONTINUATION);
20642 it->continuation_lines_width += it->last_visible_x;
20643 row->ends_in_middle_of_char_p = true;
20644 row->continued_p = true;
20645 glyph->pixel_width = it->last_visible_x - x;
20646 it->starts_in_middle_of_char_p = true;
20647 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20648 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20649 extend_face_to_end_of_line (it);
20650 }
20651 else
20652 {
20653 /* Something other than a TAB that draws past
20654 the right edge of the window. Restore
20655 positions to values before the element. */
20656 if (row->reversed_p)
20657 unproduce_glyphs (it, row->used[TEXT_AREA]
20658 - (n_glyphs_before + i));
20659 row->used[TEXT_AREA] = n_glyphs_before + i;
20660
20661 /* Display continuation glyphs. */
20662 it->current_x = x_before;
20663 it->continuation_lines_width += x;
20664 if (!FRAME_WINDOW_P (it->f)
20665 || (row->reversed_p
20666 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20667 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20668 produce_special_glyphs (it, IT_CONTINUATION);
20669 row->continued_p = true;
20670
20671 extend_face_to_end_of_line (it);
20672
20673 if (nglyphs > 1 && i > 0)
20674 {
20675 row->ends_in_middle_of_char_p = true;
20676 it->starts_in_middle_of_char_p = true;
20677 }
20678
20679 /* Restore the height to what it was before the
20680 element not fitting on the line. */
20681 it->max_ascent = ascent;
20682 it->max_descent = descent;
20683 it->max_phys_ascent = phys_ascent;
20684 it->max_phys_descent = phys_descent;
20685 }
20686
20687 break;
20688 }
20689 else if (new_x > it->first_visible_x)
20690 {
20691 /* Increment number of glyphs actually displayed. */
20692 ++it->hpos;
20693
20694 /* Record the maximum and minimum buffer positions
20695 seen so far in glyphs that will be displayed by
20696 this row. */
20697 if (it->bidi_p)
20698 RECORD_MAX_MIN_POS (it);
20699
20700 if (x < it->first_visible_x && !row->reversed_p)
20701 /* Glyph is partially visible, i.e. row starts at
20702 negative X position. Don't do that in R2L
20703 rows, where we arrange to add a right offset to
20704 the line in extend_face_to_end_of_line, by a
20705 suitable change to the stretch glyph that is
20706 the leftmost glyph of the line. */
20707 row->x = x - it->first_visible_x;
20708 /* When the last glyph of an R2L row only fits
20709 partially on the line, we need to set row->x to a
20710 negative offset, so that the leftmost glyph is
20711 the one that is partially visible. But if we are
20712 going to produce the truncation glyph, this will
20713 be taken care of in produce_special_glyphs. */
20714 if (row->reversed_p
20715 && new_x > it->last_visible_x
20716 && !(it->line_wrap == TRUNCATE
20717 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20718 {
20719 eassert (FRAME_WINDOW_P (it->f));
20720 row->x = it->last_visible_x - new_x;
20721 }
20722 }
20723 else
20724 {
20725 /* Glyph is completely off the left margin of the
20726 window. This should not happen because of the
20727 move_it_in_display_line at the start of this
20728 function, unless the text display area of the
20729 window is empty. */
20730 eassert (it->first_visible_x <= it->last_visible_x);
20731 }
20732 }
20733 /* Even if this display element produced no glyphs at all,
20734 we want to record its position. */
20735 if (it->bidi_p && nglyphs == 0)
20736 RECORD_MAX_MIN_POS (it);
20737
20738 row->ascent = max (row->ascent, it->max_ascent);
20739 row->height = max (row->height, it->max_ascent + it->max_descent);
20740 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20741 row->phys_height = max (row->phys_height,
20742 it->max_phys_ascent + it->max_phys_descent);
20743 row->extra_line_spacing = max (row->extra_line_spacing,
20744 it->max_extra_line_spacing);
20745
20746 /* End of this display line if row is continued. */
20747 if (row->continued_p || row->ends_at_zv_p)
20748 break;
20749 }
20750
20751 at_end_of_line:
20752 /* Is this a line end? If yes, we're also done, after making
20753 sure that a non-default face is extended up to the right
20754 margin of the window. */
20755 if (ITERATOR_AT_END_OF_LINE_P (it))
20756 {
20757 int used_before = row->used[TEXT_AREA];
20758
20759 row->ends_in_newline_from_string_p = STRINGP (it->object);
20760
20761 /* Add a space at the end of the line that is used to
20762 display the cursor there. */
20763 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20764 append_space_for_newline (it, false);
20765
20766 /* Extend the face to the end of the line. */
20767 extend_face_to_end_of_line (it);
20768
20769 /* Make sure we have the position. */
20770 if (used_before == 0)
20771 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20772
20773 /* Record the position of the newline, for use in
20774 find_row_edges. */
20775 it->eol_pos = it->current.pos;
20776
20777 /* Consume the line end. This skips over invisible lines. */
20778 set_iterator_to_next (it, true);
20779 it->continuation_lines_width = 0;
20780 break;
20781 }
20782
20783 /* Proceed with next display element. Note that this skips
20784 over lines invisible because of selective display. */
20785 set_iterator_to_next (it, true);
20786
20787 /* If we truncate lines, we are done when the last displayed
20788 glyphs reach past the right margin of the window. */
20789 if (it->line_wrap == TRUNCATE
20790 && ((FRAME_WINDOW_P (it->f)
20791 /* Images are preprocessed in produce_image_glyph such
20792 that they are cropped at the right edge of the
20793 window, so an image glyph will always end exactly at
20794 last_visible_x, even if there's no right fringe. */
20795 && ((row->reversed_p
20796 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20797 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20798 || it->what == IT_IMAGE))
20799 ? (it->current_x >= it->last_visible_x)
20800 : (it->current_x > it->last_visible_x)))
20801 {
20802 /* Maybe add truncation glyphs. */
20803 if (!FRAME_WINDOW_P (it->f)
20804 || (row->reversed_p
20805 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20806 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20807 {
20808 int i, n;
20809
20810 if (!row->reversed_p)
20811 {
20812 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20813 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20814 break;
20815 }
20816 else
20817 {
20818 for (i = 0; i < row->used[TEXT_AREA]; i++)
20819 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20820 break;
20821 /* Remove any padding glyphs at the front of ROW, to
20822 make room for the truncation glyphs we will be
20823 adding below. The loop below always inserts at
20824 least one truncation glyph, so also remove the
20825 last glyph added to ROW. */
20826 unproduce_glyphs (it, i + 1);
20827 /* Adjust i for the loop below. */
20828 i = row->used[TEXT_AREA] - (i + 1);
20829 }
20830
20831 /* produce_special_glyphs overwrites the last glyph, so
20832 we don't want that if we want to keep that last
20833 glyph, which means it's an image. */
20834 if (it->current_x > it->last_visible_x)
20835 {
20836 it->current_x = x_before;
20837 if (!FRAME_WINDOW_P (it->f))
20838 {
20839 for (n = row->used[TEXT_AREA]; i < n; ++i)
20840 {
20841 row->used[TEXT_AREA] = i;
20842 produce_special_glyphs (it, IT_TRUNCATION);
20843 }
20844 }
20845 else
20846 {
20847 row->used[TEXT_AREA] = i;
20848 produce_special_glyphs (it, IT_TRUNCATION);
20849 }
20850 it->hpos = hpos_before;
20851 }
20852 }
20853 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20854 {
20855 /* Don't truncate if we can overflow newline into fringe. */
20856 if (!get_next_display_element (it))
20857 {
20858 it->continuation_lines_width = 0;
20859 row->ends_at_zv_p = true;
20860 row->exact_window_width_line_p = true;
20861 break;
20862 }
20863 if (ITERATOR_AT_END_OF_LINE_P (it))
20864 {
20865 row->exact_window_width_line_p = true;
20866 goto at_end_of_line;
20867 }
20868 it->current_x = x_before;
20869 it->hpos = hpos_before;
20870 }
20871
20872 row->truncated_on_right_p = true;
20873 it->continuation_lines_width = 0;
20874 reseat_at_next_visible_line_start (it, false);
20875 /* We insist below that IT's position be at ZV because in
20876 bidi-reordered lines the character at visible line start
20877 might not be the character that follows the newline in
20878 the logical order. */
20879 if (IT_BYTEPOS (*it) > BEG_BYTE)
20880 row->ends_at_zv_p =
20881 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20882 else
20883 row->ends_at_zv_p = false;
20884 break;
20885 }
20886 }
20887
20888 if (wrap_data)
20889 bidi_unshelve_cache (wrap_data, true);
20890
20891 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20892 at the left window margin. */
20893 if (it->first_visible_x
20894 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20895 {
20896 if (!FRAME_WINDOW_P (it->f)
20897 || (((row->reversed_p
20898 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20899 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20900 /* Don't let insert_left_trunc_glyphs overwrite the
20901 first glyph of the row if it is an image. */
20902 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20903 insert_left_trunc_glyphs (it);
20904 row->truncated_on_left_p = true;
20905 }
20906
20907 /* Remember the position at which this line ends.
20908
20909 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20910 cannot be before the call to find_row_edges below, since that is
20911 where these positions are determined. */
20912 row->end = it->current;
20913 if (!it->bidi_p)
20914 {
20915 row->minpos = row->start.pos;
20916 row->maxpos = row->end.pos;
20917 }
20918 else
20919 {
20920 /* ROW->minpos and ROW->maxpos must be the smallest and
20921 `1 + the largest' buffer positions in ROW. But if ROW was
20922 bidi-reordered, these two positions can be anywhere in the
20923 row, so we must determine them now. */
20924 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20925 }
20926
20927 /* If the start of this line is the overlay arrow-position, then
20928 mark this glyph row as the one containing the overlay arrow.
20929 This is clearly a mess with variable size fonts. It would be
20930 better to let it be displayed like cursors under X. */
20931 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20932 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20933 !NILP (overlay_arrow_string)))
20934 {
20935 /* Overlay arrow in window redisplay is a fringe bitmap. */
20936 if (STRINGP (overlay_arrow_string))
20937 {
20938 struct glyph_row *arrow_row
20939 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20940 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20941 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20942 struct glyph *p = row->glyphs[TEXT_AREA];
20943 struct glyph *p2, *end;
20944
20945 /* Copy the arrow glyphs. */
20946 while (glyph < arrow_end)
20947 *p++ = *glyph++;
20948
20949 /* Throw away padding glyphs. */
20950 p2 = p;
20951 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20952 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20953 ++p2;
20954 if (p2 > p)
20955 {
20956 while (p2 < end)
20957 *p++ = *p2++;
20958 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20959 }
20960 }
20961 else
20962 {
20963 eassert (INTEGERP (overlay_arrow_string));
20964 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20965 }
20966 overlay_arrow_seen = true;
20967 }
20968
20969 /* Highlight trailing whitespace. */
20970 if (!NILP (Vshow_trailing_whitespace))
20971 highlight_trailing_whitespace (it->f, it->glyph_row);
20972
20973 /* Compute pixel dimensions of this line. */
20974 compute_line_metrics (it);
20975
20976 /* Implementation note: No changes in the glyphs of ROW or in their
20977 faces can be done past this point, because compute_line_metrics
20978 computes ROW's hash value and stores it within the glyph_row
20979 structure. */
20980
20981 /* Record whether this row ends inside an ellipsis. */
20982 row->ends_in_ellipsis_p
20983 = (it->method == GET_FROM_DISPLAY_VECTOR
20984 && it->ellipsis_p);
20985
20986 /* Save fringe bitmaps in this row. */
20987 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20988 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20989 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20990 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20991
20992 it->left_user_fringe_bitmap = 0;
20993 it->left_user_fringe_face_id = 0;
20994 it->right_user_fringe_bitmap = 0;
20995 it->right_user_fringe_face_id = 0;
20996
20997 /* Maybe set the cursor. */
20998 cvpos = it->w->cursor.vpos;
20999 if ((cvpos < 0
21000 /* In bidi-reordered rows, keep checking for proper cursor
21001 position even if one has been found already, because buffer
21002 positions in such rows change non-linearly with ROW->VPOS,
21003 when a line is continued. One exception: when we are at ZV,
21004 display cursor on the first suitable glyph row, since all
21005 the empty rows after that also have their position set to ZV. */
21006 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21007 lines' rows is implemented for bidi-reordered rows. */
21008 || (it->bidi_p
21009 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21010 && PT >= MATRIX_ROW_START_CHARPOS (row)
21011 && PT <= MATRIX_ROW_END_CHARPOS (row)
21012 && cursor_row_p (row))
21013 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21014
21015 /* Prepare for the next line. This line starts horizontally at (X
21016 HPOS) = (0 0). Vertical positions are incremented. As a
21017 convenience for the caller, IT->glyph_row is set to the next
21018 row to be used. */
21019 it->current_x = it->hpos = 0;
21020 it->current_y += row->height;
21021 SET_TEXT_POS (it->eol_pos, 0, 0);
21022 ++it->vpos;
21023 ++it->glyph_row;
21024 /* The next row should by default use the same value of the
21025 reversed_p flag as this one. set_iterator_to_next decides when
21026 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21027 the flag accordingly. */
21028 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21029 it->glyph_row->reversed_p = row->reversed_p;
21030 it->start = row->end;
21031 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21032
21033 #undef RECORD_MAX_MIN_POS
21034 }
21035
21036 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21037 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21038 doc: /* Return paragraph direction at point in BUFFER.
21039 Value is either `left-to-right' or `right-to-left'.
21040 If BUFFER is omitted or nil, it defaults to the current buffer.
21041
21042 Paragraph direction determines how the text in the paragraph is displayed.
21043 In left-to-right paragraphs, text begins at the left margin of the window
21044 and the reading direction is generally left to right. In right-to-left
21045 paragraphs, text begins at the right margin and is read from right to left.
21046
21047 See also `bidi-paragraph-direction'. */)
21048 (Lisp_Object buffer)
21049 {
21050 struct buffer *buf = current_buffer;
21051 struct buffer *old = buf;
21052
21053 if (! NILP (buffer))
21054 {
21055 CHECK_BUFFER (buffer);
21056 buf = XBUFFER (buffer);
21057 }
21058
21059 if (NILP (BVAR (buf, bidi_display_reordering))
21060 || NILP (BVAR (buf, enable_multibyte_characters))
21061 /* When we are loading loadup.el, the character property tables
21062 needed for bidi iteration are not yet available. */
21063 || !NILP (Vpurify_flag))
21064 return Qleft_to_right;
21065 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21066 return BVAR (buf, bidi_paragraph_direction);
21067 else
21068 {
21069 /* Determine the direction from buffer text. We could try to
21070 use current_matrix if it is up to date, but this seems fast
21071 enough as it is. */
21072 struct bidi_it itb;
21073 ptrdiff_t pos = BUF_PT (buf);
21074 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21075 int c;
21076 void *itb_data = bidi_shelve_cache ();
21077
21078 set_buffer_temp (buf);
21079 /* bidi_paragraph_init finds the base direction of the paragraph
21080 by searching forward from paragraph start. We need the base
21081 direction of the current or _previous_ paragraph, so we need
21082 to make sure we are within that paragraph. To that end, find
21083 the previous non-empty line. */
21084 if (pos >= ZV && pos > BEGV)
21085 DEC_BOTH (pos, bytepos);
21086 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21087 if (fast_looking_at (trailing_white_space,
21088 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21089 {
21090 while ((c = FETCH_BYTE (bytepos)) == '\n'
21091 || c == ' ' || c == '\t' || c == '\f')
21092 {
21093 if (bytepos <= BEGV_BYTE)
21094 break;
21095 bytepos--;
21096 pos--;
21097 }
21098 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21099 bytepos--;
21100 }
21101 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21102 itb.paragraph_dir = NEUTRAL_DIR;
21103 itb.string.s = NULL;
21104 itb.string.lstring = Qnil;
21105 itb.string.bufpos = 0;
21106 itb.string.from_disp_str = false;
21107 itb.string.unibyte = false;
21108 /* We have no window to use here for ignoring window-specific
21109 overlays. Using NULL for window pointer will cause
21110 compute_display_string_pos to use the current buffer. */
21111 itb.w = NULL;
21112 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21113 bidi_unshelve_cache (itb_data, false);
21114 set_buffer_temp (old);
21115 switch (itb.paragraph_dir)
21116 {
21117 case L2R:
21118 return Qleft_to_right;
21119 break;
21120 case R2L:
21121 return Qright_to_left;
21122 break;
21123 default:
21124 emacs_abort ();
21125 }
21126 }
21127 }
21128
21129 DEFUN ("bidi-find-overridden-directionality",
21130 Fbidi_find_overridden_directionality,
21131 Sbidi_find_overridden_directionality, 2, 3, 0,
21132 doc: /* Return position between FROM and TO where directionality was overridden.
21133
21134 This function returns the first character position in the specified
21135 region of OBJECT where there is a character whose `bidi-class' property
21136 is `L', but which was forced to display as `R' by a directional
21137 override, and likewise with characters whose `bidi-class' is `R'
21138 or `AL' that were forced to display as `L'.
21139
21140 If no such character is found, the function returns nil.
21141
21142 OBJECT is a Lisp string or buffer to search for overridden
21143 directionality, and defaults to the current buffer if nil or omitted.
21144 OBJECT can also be a window, in which case the function will search
21145 the buffer displayed in that window. Passing the window instead of
21146 a buffer is preferable when the buffer is displayed in some window,
21147 because this function will then be able to correctly account for
21148 window-specific overlays, which can affect the results.
21149
21150 Strong directional characters `L', `R', and `AL' can have their
21151 intrinsic directionality overridden by directional override
21152 control characters RLO (u+202e) and LRO (u+202d). See the
21153 function `get-char-code-property' for a way to inquire about
21154 the `bidi-class' property of a character. */)
21155 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21156 {
21157 struct buffer *buf = current_buffer;
21158 struct buffer *old = buf;
21159 struct window *w = NULL;
21160 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21161 struct bidi_it itb;
21162 ptrdiff_t from_pos, to_pos, from_bpos;
21163 void *itb_data;
21164
21165 if (!NILP (object))
21166 {
21167 if (BUFFERP (object))
21168 buf = XBUFFER (object);
21169 else if (WINDOWP (object))
21170 {
21171 w = decode_live_window (object);
21172 buf = XBUFFER (w->contents);
21173 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21174 }
21175 else
21176 CHECK_STRING (object);
21177 }
21178
21179 if (STRINGP (object))
21180 {
21181 /* Characters in unibyte strings are always treated by bidi.c as
21182 strong LTR. */
21183 if (!STRING_MULTIBYTE (object)
21184 /* When we are loading loadup.el, the character property
21185 tables needed for bidi iteration are not yet
21186 available. */
21187 || !NILP (Vpurify_flag))
21188 return Qnil;
21189
21190 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21191 if (from_pos >= SCHARS (object))
21192 return Qnil;
21193
21194 /* Set up the bidi iterator. */
21195 itb_data = bidi_shelve_cache ();
21196 itb.paragraph_dir = NEUTRAL_DIR;
21197 itb.string.lstring = object;
21198 itb.string.s = NULL;
21199 itb.string.schars = SCHARS (object);
21200 itb.string.bufpos = 0;
21201 itb.string.from_disp_str = false;
21202 itb.string.unibyte = false;
21203 itb.w = w;
21204 bidi_init_it (0, 0, frame_window_p, &itb);
21205 }
21206 else
21207 {
21208 /* Nothing this fancy can happen in unibyte buffers, or in a
21209 buffer that disabled reordering, or if FROM is at EOB. */
21210 if (NILP (BVAR (buf, bidi_display_reordering))
21211 || NILP (BVAR (buf, enable_multibyte_characters))
21212 /* When we are loading loadup.el, the character property
21213 tables needed for bidi iteration are not yet
21214 available. */
21215 || !NILP (Vpurify_flag))
21216 return Qnil;
21217
21218 set_buffer_temp (buf);
21219 validate_region (&from, &to);
21220 from_pos = XINT (from);
21221 to_pos = XINT (to);
21222 if (from_pos >= ZV)
21223 return Qnil;
21224
21225 /* Set up the bidi iterator. */
21226 itb_data = bidi_shelve_cache ();
21227 from_bpos = CHAR_TO_BYTE (from_pos);
21228 if (from_pos == BEGV)
21229 {
21230 itb.charpos = BEGV;
21231 itb.bytepos = BEGV_BYTE;
21232 }
21233 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21234 {
21235 itb.charpos = from_pos;
21236 itb.bytepos = from_bpos;
21237 }
21238 else
21239 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21240 -1, &itb.bytepos);
21241 itb.paragraph_dir = NEUTRAL_DIR;
21242 itb.string.s = NULL;
21243 itb.string.lstring = Qnil;
21244 itb.string.bufpos = 0;
21245 itb.string.from_disp_str = false;
21246 itb.string.unibyte = false;
21247 itb.w = w;
21248 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21249 }
21250
21251 ptrdiff_t found;
21252 do {
21253 /* For the purposes of this function, the actual base direction of
21254 the paragraph doesn't matter, so just set it to L2R. */
21255 bidi_paragraph_init (L2R, &itb, false);
21256 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21257 ;
21258 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21259
21260 bidi_unshelve_cache (itb_data, false);
21261 set_buffer_temp (old);
21262
21263 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21264 }
21265
21266 DEFUN ("move-point-visually", Fmove_point_visually,
21267 Smove_point_visually, 1, 1, 0,
21268 doc: /* Move point in the visual order in the specified DIRECTION.
21269 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21270 left.
21271
21272 Value is the new character position of point. */)
21273 (Lisp_Object direction)
21274 {
21275 struct window *w = XWINDOW (selected_window);
21276 struct buffer *b = XBUFFER (w->contents);
21277 struct glyph_row *row;
21278 int dir;
21279 Lisp_Object paragraph_dir;
21280
21281 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21282 (!(ROW)->continued_p \
21283 && NILP ((GLYPH)->object) \
21284 && (GLYPH)->type == CHAR_GLYPH \
21285 && (GLYPH)->u.ch == ' ' \
21286 && (GLYPH)->charpos >= 0 \
21287 && !(GLYPH)->avoid_cursor_p)
21288
21289 CHECK_NUMBER (direction);
21290 dir = XINT (direction);
21291 if (dir > 0)
21292 dir = 1;
21293 else
21294 dir = -1;
21295
21296 /* If current matrix is up-to-date, we can use the information
21297 recorded in the glyphs, at least as long as the goal is on the
21298 screen. */
21299 if (w->window_end_valid
21300 && !windows_or_buffers_changed
21301 && b
21302 && !b->clip_changed
21303 && !b->prevent_redisplay_optimizations_p
21304 && !window_outdated (w)
21305 /* We rely below on the cursor coordinates to be up to date, but
21306 we cannot trust them if some command moved point since the
21307 last complete redisplay. */
21308 && w->last_point == BUF_PT (b)
21309 && w->cursor.vpos >= 0
21310 && w->cursor.vpos < w->current_matrix->nrows
21311 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21312 {
21313 struct glyph *g = row->glyphs[TEXT_AREA];
21314 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21315 struct glyph *gpt = g + w->cursor.hpos;
21316
21317 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21318 {
21319 if (BUFFERP (g->object) && g->charpos != PT)
21320 {
21321 SET_PT (g->charpos);
21322 w->cursor.vpos = -1;
21323 return make_number (PT);
21324 }
21325 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21326 {
21327 ptrdiff_t new_pos;
21328
21329 if (BUFFERP (gpt->object))
21330 {
21331 new_pos = PT;
21332 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21333 new_pos += (row->reversed_p ? -dir : dir);
21334 else
21335 new_pos -= (row->reversed_p ? -dir : dir);
21336 }
21337 else if (BUFFERP (g->object))
21338 new_pos = g->charpos;
21339 else
21340 break;
21341 SET_PT (new_pos);
21342 w->cursor.vpos = -1;
21343 return make_number (PT);
21344 }
21345 else if (ROW_GLYPH_NEWLINE_P (row, g))
21346 {
21347 /* Glyphs inserted at the end of a non-empty line for
21348 positioning the cursor have zero charpos, so we must
21349 deduce the value of point by other means. */
21350 if (g->charpos > 0)
21351 SET_PT (g->charpos);
21352 else if (row->ends_at_zv_p && PT != ZV)
21353 SET_PT (ZV);
21354 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21355 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21356 else
21357 break;
21358 w->cursor.vpos = -1;
21359 return make_number (PT);
21360 }
21361 }
21362 if (g == e || NILP (g->object))
21363 {
21364 if (row->truncated_on_left_p || row->truncated_on_right_p)
21365 goto simulate_display;
21366 if (!row->reversed_p)
21367 row += dir;
21368 else
21369 row -= dir;
21370 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21371 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21372 goto simulate_display;
21373
21374 if (dir > 0)
21375 {
21376 if (row->reversed_p && !row->continued_p)
21377 {
21378 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21379 w->cursor.vpos = -1;
21380 return make_number (PT);
21381 }
21382 g = row->glyphs[TEXT_AREA];
21383 e = g + row->used[TEXT_AREA];
21384 for ( ; g < e; g++)
21385 {
21386 if (BUFFERP (g->object)
21387 /* Empty lines have only one glyph, which stands
21388 for the newline, and whose charpos is the
21389 buffer position of the newline. */
21390 || ROW_GLYPH_NEWLINE_P (row, g)
21391 /* When the buffer ends in a newline, the line at
21392 EOB also has one glyph, but its charpos is -1. */
21393 || (row->ends_at_zv_p
21394 && !row->reversed_p
21395 && NILP (g->object)
21396 && g->type == CHAR_GLYPH
21397 && g->u.ch == ' '))
21398 {
21399 if (g->charpos > 0)
21400 SET_PT (g->charpos);
21401 else if (!row->reversed_p
21402 && row->ends_at_zv_p
21403 && PT != ZV)
21404 SET_PT (ZV);
21405 else
21406 continue;
21407 w->cursor.vpos = -1;
21408 return make_number (PT);
21409 }
21410 }
21411 }
21412 else
21413 {
21414 if (!row->reversed_p && !row->continued_p)
21415 {
21416 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21417 w->cursor.vpos = -1;
21418 return make_number (PT);
21419 }
21420 e = row->glyphs[TEXT_AREA];
21421 g = e + row->used[TEXT_AREA] - 1;
21422 for ( ; g >= e; g--)
21423 {
21424 if (BUFFERP (g->object)
21425 || (ROW_GLYPH_NEWLINE_P (row, g)
21426 && g->charpos > 0)
21427 /* Empty R2L lines on GUI frames have the buffer
21428 position of the newline stored in the stretch
21429 glyph. */
21430 || g->type == STRETCH_GLYPH
21431 || (row->ends_at_zv_p
21432 && row->reversed_p
21433 && NILP (g->object)
21434 && g->type == CHAR_GLYPH
21435 && g->u.ch == ' '))
21436 {
21437 if (g->charpos > 0)
21438 SET_PT (g->charpos);
21439 else if (row->reversed_p
21440 && row->ends_at_zv_p
21441 && PT != ZV)
21442 SET_PT (ZV);
21443 else
21444 continue;
21445 w->cursor.vpos = -1;
21446 return make_number (PT);
21447 }
21448 }
21449 }
21450 }
21451 }
21452
21453 simulate_display:
21454
21455 /* If we wind up here, we failed to move by using the glyphs, so we
21456 need to simulate display instead. */
21457
21458 if (b)
21459 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21460 else
21461 paragraph_dir = Qleft_to_right;
21462 if (EQ (paragraph_dir, Qright_to_left))
21463 dir = -dir;
21464 if (PT <= BEGV && dir < 0)
21465 xsignal0 (Qbeginning_of_buffer);
21466 else if (PT >= ZV && dir > 0)
21467 xsignal0 (Qend_of_buffer);
21468 else
21469 {
21470 struct text_pos pt;
21471 struct it it;
21472 int pt_x, target_x, pixel_width, pt_vpos;
21473 bool at_eol_p;
21474 bool overshoot_expected = false;
21475 bool target_is_eol_p = false;
21476
21477 /* Setup the arena. */
21478 SET_TEXT_POS (pt, PT, PT_BYTE);
21479 start_display (&it, w, pt);
21480 /* When lines are truncated, we could be called with point
21481 outside of the windows edges, in which case move_it_*
21482 functions either prematurely stop at window's edge or jump to
21483 the next screen line, whereas we rely below on our ability to
21484 reach point, in order to start from its X coordinate. So we
21485 need to disregard the window's horizontal extent in that case. */
21486 if (it.line_wrap == TRUNCATE)
21487 it.last_visible_x = INFINITY;
21488
21489 if (it.cmp_it.id < 0
21490 && it.method == GET_FROM_STRING
21491 && it.area == TEXT_AREA
21492 && it.string_from_display_prop_p
21493 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21494 overshoot_expected = true;
21495
21496 /* Find the X coordinate of point. We start from the beginning
21497 of this or previous line to make sure we are before point in
21498 the logical order (since the move_it_* functions can only
21499 move forward). */
21500 reseat:
21501 reseat_at_previous_visible_line_start (&it);
21502 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21503 if (IT_CHARPOS (it) != PT)
21504 {
21505 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21506 -1, -1, -1, MOVE_TO_POS);
21507 /* If we missed point because the character there is
21508 displayed out of a display vector that has more than one
21509 glyph, retry expecting overshoot. */
21510 if (it.method == GET_FROM_DISPLAY_VECTOR
21511 && it.current.dpvec_index > 0
21512 && !overshoot_expected)
21513 {
21514 overshoot_expected = true;
21515 goto reseat;
21516 }
21517 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21518 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21519 }
21520 pt_x = it.current_x;
21521 pt_vpos = it.vpos;
21522 if (dir > 0 || overshoot_expected)
21523 {
21524 struct glyph_row *row = it.glyph_row;
21525
21526 /* When point is at beginning of line, we don't have
21527 information about the glyph there loaded into struct
21528 it. Calling get_next_display_element fixes that. */
21529 if (pt_x == 0)
21530 get_next_display_element (&it);
21531 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21532 it.glyph_row = NULL;
21533 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21534 it.glyph_row = row;
21535 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21536 it, lest it will become out of sync with it's buffer
21537 position. */
21538 it.current_x = pt_x;
21539 }
21540 else
21541 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21542 pixel_width = it.pixel_width;
21543 if (overshoot_expected && at_eol_p)
21544 pixel_width = 0;
21545 else if (pixel_width <= 0)
21546 pixel_width = 1;
21547
21548 /* If there's a display string (or something similar) at point,
21549 we are actually at the glyph to the left of point, so we need
21550 to correct the X coordinate. */
21551 if (overshoot_expected)
21552 {
21553 if (it.bidi_p)
21554 pt_x += pixel_width * it.bidi_it.scan_dir;
21555 else
21556 pt_x += pixel_width;
21557 }
21558
21559 /* Compute target X coordinate, either to the left or to the
21560 right of point. On TTY frames, all characters have the same
21561 pixel width of 1, so we can use that. On GUI frames we don't
21562 have an easy way of getting at the pixel width of the
21563 character to the left of point, so we use a different method
21564 of getting to that place. */
21565 if (dir > 0)
21566 target_x = pt_x + pixel_width;
21567 else
21568 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21569
21570 /* Target X coordinate could be one line above or below the line
21571 of point, in which case we need to adjust the target X
21572 coordinate. Also, if moving to the left, we need to begin at
21573 the left edge of the point's screen line. */
21574 if (dir < 0)
21575 {
21576 if (pt_x > 0)
21577 {
21578 start_display (&it, w, pt);
21579 if (it.line_wrap == TRUNCATE)
21580 it.last_visible_x = INFINITY;
21581 reseat_at_previous_visible_line_start (&it);
21582 it.current_x = it.current_y = it.hpos = 0;
21583 if (pt_vpos != 0)
21584 move_it_by_lines (&it, pt_vpos);
21585 }
21586 else
21587 {
21588 move_it_by_lines (&it, -1);
21589 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21590 target_is_eol_p = true;
21591 /* Under word-wrap, we don't know the x coordinate of
21592 the last character displayed on the previous line,
21593 which immediately precedes the wrap point. To find
21594 out its x coordinate, we try moving to the right
21595 margin of the window, which will stop at the wrap
21596 point, and then reset target_x to point at the
21597 character that precedes the wrap point. This is not
21598 needed on GUI frames, because (see below) there we
21599 move from the left margin one grapheme cluster at a
21600 time, and stop when we hit the wrap point. */
21601 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21602 {
21603 void *it_data = NULL;
21604 struct it it2;
21605
21606 SAVE_IT (it2, it, it_data);
21607 move_it_in_display_line_to (&it, ZV, target_x,
21608 MOVE_TO_POS | MOVE_TO_X);
21609 /* If we arrived at target_x, that _is_ the last
21610 character on the previous line. */
21611 if (it.current_x != target_x)
21612 target_x = it.current_x - 1;
21613 RESTORE_IT (&it, &it2, it_data);
21614 }
21615 }
21616 }
21617 else
21618 {
21619 if (at_eol_p
21620 || (target_x >= it.last_visible_x
21621 && it.line_wrap != TRUNCATE))
21622 {
21623 if (pt_x > 0)
21624 move_it_by_lines (&it, 0);
21625 move_it_by_lines (&it, 1);
21626 target_x = 0;
21627 }
21628 }
21629
21630 /* Move to the target X coordinate. */
21631 #ifdef HAVE_WINDOW_SYSTEM
21632 /* On GUI frames, as we don't know the X coordinate of the
21633 character to the left of point, moving point to the left
21634 requires walking, one grapheme cluster at a time, until we
21635 find ourself at a place immediately to the left of the
21636 character at point. */
21637 if (FRAME_WINDOW_P (it.f) && dir < 0)
21638 {
21639 struct text_pos new_pos;
21640 enum move_it_result rc = MOVE_X_REACHED;
21641
21642 if (it.current_x == 0)
21643 get_next_display_element (&it);
21644 if (it.what == IT_COMPOSITION)
21645 {
21646 new_pos.charpos = it.cmp_it.charpos;
21647 new_pos.bytepos = -1;
21648 }
21649 else
21650 new_pos = it.current.pos;
21651
21652 while (it.current_x + it.pixel_width <= target_x
21653 && (rc == MOVE_X_REACHED
21654 /* Under word-wrap, move_it_in_display_line_to
21655 stops at correct coordinates, but sometimes
21656 returns MOVE_POS_MATCH_OR_ZV. */
21657 || (it.line_wrap == WORD_WRAP
21658 && rc == MOVE_POS_MATCH_OR_ZV)))
21659 {
21660 int new_x = it.current_x + it.pixel_width;
21661
21662 /* For composed characters, we want the position of the
21663 first character in the grapheme cluster (usually, the
21664 composition's base character), whereas it.current
21665 might give us the position of the _last_ one, e.g. if
21666 the composition is rendered in reverse due to bidi
21667 reordering. */
21668 if (it.what == IT_COMPOSITION)
21669 {
21670 new_pos.charpos = it.cmp_it.charpos;
21671 new_pos.bytepos = -1;
21672 }
21673 else
21674 new_pos = it.current.pos;
21675 if (new_x == it.current_x)
21676 new_x++;
21677 rc = move_it_in_display_line_to (&it, ZV, new_x,
21678 MOVE_TO_POS | MOVE_TO_X);
21679 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21680 break;
21681 }
21682 /* The previous position we saw in the loop is the one we
21683 want. */
21684 if (new_pos.bytepos == -1)
21685 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21686 it.current.pos = new_pos;
21687 }
21688 else
21689 #endif
21690 if (it.current_x != target_x)
21691 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21692
21693 /* If we ended up in a display string that covers point, move to
21694 buffer position to the right in the visual order. */
21695 if (dir > 0)
21696 {
21697 while (IT_CHARPOS (it) == PT)
21698 {
21699 set_iterator_to_next (&it, false);
21700 if (!get_next_display_element (&it))
21701 break;
21702 }
21703 }
21704
21705 /* Move point to that position. */
21706 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21707 }
21708
21709 return make_number (PT);
21710
21711 #undef ROW_GLYPH_NEWLINE_P
21712 }
21713
21714 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21715 Sbidi_resolved_levels, 0, 1, 0,
21716 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21717
21718 The resolved levels are produced by the Emacs bidi reordering engine
21719 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21720 read the Unicode Standard Annex 9 (UAX#9) for background information
21721 about these levels.
21722
21723 VPOS is the zero-based number of the current window's screen line
21724 for which to produce the resolved levels. If VPOS is nil or omitted,
21725 it defaults to the screen line of point. If the window displays a
21726 header line, VPOS of zero will report on the header line, and first
21727 line of text in the window will have VPOS of 1.
21728
21729 Value is an array of resolved levels, indexed by glyph number.
21730 Glyphs are numbered from zero starting from the beginning of the
21731 screen line, i.e. the left edge of the window for left-to-right lines
21732 and from the right edge for right-to-left lines. The resolved levels
21733 are produced only for the window's text area; text in display margins
21734 is not included.
21735
21736 If the selected window's display is not up-to-date, or if the specified
21737 screen line does not display text, this function returns nil. It is
21738 highly recommended to bind this function to some simple key, like F8,
21739 in order to avoid these problems.
21740
21741 This function exists mainly for testing the correctness of the
21742 Emacs UBA implementation, in particular with the test suite. */)
21743 (Lisp_Object vpos)
21744 {
21745 struct window *w = XWINDOW (selected_window);
21746 struct buffer *b = XBUFFER (w->contents);
21747 int nrow;
21748 struct glyph_row *row;
21749
21750 if (NILP (vpos))
21751 {
21752 int d1, d2, d3, d4, d5;
21753
21754 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21755 }
21756 else
21757 {
21758 CHECK_NUMBER_COERCE_MARKER (vpos);
21759 nrow = XINT (vpos);
21760 }
21761
21762 /* We require up-to-date glyph matrix for this window. */
21763 if (w->window_end_valid
21764 && !windows_or_buffers_changed
21765 && b
21766 && !b->clip_changed
21767 && !b->prevent_redisplay_optimizations_p
21768 && !window_outdated (w)
21769 && nrow >= 0
21770 && nrow < w->current_matrix->nrows
21771 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21772 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21773 {
21774 struct glyph *g, *e, *g1;
21775 int nglyphs, i;
21776 Lisp_Object levels;
21777
21778 if (!row->reversed_p) /* Left-to-right glyph row. */
21779 {
21780 g = g1 = row->glyphs[TEXT_AREA];
21781 e = g + row->used[TEXT_AREA];
21782
21783 /* Skip over glyphs at the start of the row that was
21784 generated by redisplay for its own needs. */
21785 while (g < e
21786 && NILP (g->object)
21787 && g->charpos < 0)
21788 g++;
21789 g1 = g;
21790
21791 /* Count the "interesting" glyphs in this row. */
21792 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21793 nglyphs++;
21794
21795 /* Create and fill the array. */
21796 levels = make_uninit_vector (nglyphs);
21797 for (i = 0; g1 < g; i++, g1++)
21798 ASET (levels, i, make_number (g1->resolved_level));
21799 }
21800 else /* Right-to-left glyph row. */
21801 {
21802 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21803 e = row->glyphs[TEXT_AREA] - 1;
21804 while (g > e
21805 && NILP (g->object)
21806 && g->charpos < 0)
21807 g--;
21808 g1 = g;
21809 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21810 nglyphs++;
21811 levels = make_uninit_vector (nglyphs);
21812 for (i = 0; g1 > g; i++, g1--)
21813 ASET (levels, i, make_number (g1->resolved_level));
21814 }
21815 return levels;
21816 }
21817 else
21818 return Qnil;
21819 }
21820
21821
21822 \f
21823 /***********************************************************************
21824 Menu Bar
21825 ***********************************************************************/
21826
21827 /* Redisplay the menu bar in the frame for window W.
21828
21829 The menu bar of X frames that don't have X toolkit support is
21830 displayed in a special window W->frame->menu_bar_window.
21831
21832 The menu bar of terminal frames is treated specially as far as
21833 glyph matrices are concerned. Menu bar lines are not part of
21834 windows, so the update is done directly on the frame matrix rows
21835 for the menu bar. */
21836
21837 static void
21838 display_menu_bar (struct window *w)
21839 {
21840 struct frame *f = XFRAME (WINDOW_FRAME (w));
21841 struct it it;
21842 Lisp_Object items;
21843 int i;
21844
21845 /* Don't do all this for graphical frames. */
21846 #ifdef HAVE_NTGUI
21847 if (FRAME_W32_P (f))
21848 return;
21849 #endif
21850 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21851 if (FRAME_X_P (f))
21852 return;
21853 #endif
21854
21855 #ifdef HAVE_NS
21856 if (FRAME_NS_P (f))
21857 return;
21858 #endif /* HAVE_NS */
21859
21860 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21861 eassert (!FRAME_WINDOW_P (f));
21862 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21863 it.first_visible_x = 0;
21864 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21865 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21866 if (FRAME_WINDOW_P (f))
21867 {
21868 /* Menu bar lines are displayed in the desired matrix of the
21869 dummy window menu_bar_window. */
21870 struct window *menu_w;
21871 menu_w = XWINDOW (f->menu_bar_window);
21872 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21873 MENU_FACE_ID);
21874 it.first_visible_x = 0;
21875 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21876 }
21877 else
21878 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21879 {
21880 /* This is a TTY frame, i.e. character hpos/vpos are used as
21881 pixel x/y. */
21882 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21883 MENU_FACE_ID);
21884 it.first_visible_x = 0;
21885 it.last_visible_x = FRAME_COLS (f);
21886 }
21887
21888 /* FIXME: This should be controlled by a user option. See the
21889 comments in redisplay_tool_bar and display_mode_line about
21890 this. */
21891 it.paragraph_embedding = L2R;
21892
21893 /* Clear all rows of the menu bar. */
21894 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21895 {
21896 struct glyph_row *row = it.glyph_row + i;
21897 clear_glyph_row (row);
21898 row->enabled_p = true;
21899 row->full_width_p = true;
21900 row->reversed_p = false;
21901 }
21902
21903 /* Display all items of the menu bar. */
21904 items = FRAME_MENU_BAR_ITEMS (it.f);
21905 for (i = 0; i < ASIZE (items); i += 4)
21906 {
21907 Lisp_Object string;
21908
21909 /* Stop at nil string. */
21910 string = AREF (items, i + 1);
21911 if (NILP (string))
21912 break;
21913
21914 /* Remember where item was displayed. */
21915 ASET (items, i + 3, make_number (it.hpos));
21916
21917 /* Display the item, pad with one space. */
21918 if (it.current_x < it.last_visible_x)
21919 display_string (NULL, string, Qnil, 0, 0, &it,
21920 SCHARS (string) + 1, 0, 0, -1);
21921 }
21922
21923 /* Fill out the line with spaces. */
21924 if (it.current_x < it.last_visible_x)
21925 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21926
21927 /* Compute the total height of the lines. */
21928 compute_line_metrics (&it);
21929 }
21930
21931 /* Deep copy of a glyph row, including the glyphs. */
21932 static void
21933 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21934 {
21935 struct glyph *pointers[1 + LAST_AREA];
21936 int to_used = to->used[TEXT_AREA];
21937
21938 /* Save glyph pointers of TO. */
21939 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21940
21941 /* Do a structure assignment. */
21942 *to = *from;
21943
21944 /* Restore original glyph pointers of TO. */
21945 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21946
21947 /* Copy the glyphs. */
21948 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21949 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21950
21951 /* If we filled only part of the TO row, fill the rest with
21952 space_glyph (which will display as empty space). */
21953 if (to_used > from->used[TEXT_AREA])
21954 fill_up_frame_row_with_spaces (to, to_used);
21955 }
21956
21957 /* Display one menu item on a TTY, by overwriting the glyphs in the
21958 frame F's desired glyph matrix with glyphs produced from the menu
21959 item text. Called from term.c to display TTY drop-down menus one
21960 item at a time.
21961
21962 ITEM_TEXT is the menu item text as a C string.
21963
21964 FACE_ID is the face ID to be used for this menu item. FACE_ID
21965 could specify one of 3 faces: a face for an enabled item, a face
21966 for a disabled item, or a face for a selected item.
21967
21968 X and Y are coordinates of the first glyph in the frame's desired
21969 matrix to be overwritten by the menu item. Since this is a TTY, Y
21970 is the zero-based number of the glyph row and X is the zero-based
21971 glyph number in the row, starting from left, where to start
21972 displaying the item.
21973
21974 SUBMENU means this menu item drops down a submenu, which
21975 should be indicated by displaying a proper visual cue after the
21976 item text. */
21977
21978 void
21979 display_tty_menu_item (const char *item_text, int width, int face_id,
21980 int x, int y, bool submenu)
21981 {
21982 struct it it;
21983 struct frame *f = SELECTED_FRAME ();
21984 struct window *w = XWINDOW (f->selected_window);
21985 struct glyph_row *row;
21986 size_t item_len = strlen (item_text);
21987
21988 eassert (FRAME_TERMCAP_P (f));
21989
21990 /* Don't write beyond the matrix's last row. This can happen for
21991 TTY screens that are not high enough to show the entire menu.
21992 (This is actually a bit of defensive programming, as
21993 tty_menu_display already limits the number of menu items to one
21994 less than the number of screen lines.) */
21995 if (y >= f->desired_matrix->nrows)
21996 return;
21997
21998 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21999 it.first_visible_x = 0;
22000 it.last_visible_x = FRAME_COLS (f) - 1;
22001 row = it.glyph_row;
22002 /* Start with the row contents from the current matrix. */
22003 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22004 bool saved_width = row->full_width_p;
22005 row->full_width_p = true;
22006 bool saved_reversed = row->reversed_p;
22007 row->reversed_p = false;
22008 row->enabled_p = true;
22009
22010 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22011 desired face. */
22012 eassert (x < f->desired_matrix->matrix_w);
22013 it.current_x = it.hpos = x;
22014 it.current_y = it.vpos = y;
22015 int saved_used = row->used[TEXT_AREA];
22016 bool saved_truncated = row->truncated_on_right_p;
22017 row->used[TEXT_AREA] = x;
22018 it.face_id = face_id;
22019 it.line_wrap = TRUNCATE;
22020
22021 /* FIXME: This should be controlled by a user option. See the
22022 comments in redisplay_tool_bar and display_mode_line about this.
22023 Also, if paragraph_embedding could ever be R2L, changes will be
22024 needed to avoid shifting to the right the row characters in
22025 term.c:append_glyph. */
22026 it.paragraph_embedding = L2R;
22027
22028 /* Pad with a space on the left. */
22029 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22030 width--;
22031 /* Display the menu item, pad with spaces to WIDTH. */
22032 if (submenu)
22033 {
22034 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22035 item_len, 0, FRAME_COLS (f) - 1, -1);
22036 width -= item_len;
22037 /* Indicate with " >" that there's a submenu. */
22038 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22039 FRAME_COLS (f) - 1, -1);
22040 }
22041 else
22042 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22043 width, 0, FRAME_COLS (f) - 1, -1);
22044
22045 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22046 row->truncated_on_right_p = saved_truncated;
22047 row->hash = row_hash (row);
22048 row->full_width_p = saved_width;
22049 row->reversed_p = saved_reversed;
22050 }
22051 \f
22052 /***********************************************************************
22053 Mode Line
22054 ***********************************************************************/
22055
22056 /* Redisplay mode lines in the window tree whose root is WINDOW.
22057 If FORCE, redisplay mode lines unconditionally.
22058 Otherwise, redisplay only mode lines that are garbaged. Value is
22059 the number of windows whose mode lines were redisplayed. */
22060
22061 static int
22062 redisplay_mode_lines (Lisp_Object window, bool force)
22063 {
22064 int nwindows = 0;
22065
22066 while (!NILP (window))
22067 {
22068 struct window *w = XWINDOW (window);
22069
22070 if (WINDOWP (w->contents))
22071 nwindows += redisplay_mode_lines (w->contents, force);
22072 else if (force
22073 || FRAME_GARBAGED_P (XFRAME (w->frame))
22074 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22075 {
22076 struct text_pos lpoint;
22077 struct buffer *old = current_buffer;
22078
22079 /* Set the window's buffer for the mode line display. */
22080 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22081 set_buffer_internal_1 (XBUFFER (w->contents));
22082
22083 /* Point refers normally to the selected window. For any
22084 other window, set up appropriate value. */
22085 if (!EQ (window, selected_window))
22086 {
22087 struct text_pos pt;
22088
22089 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22090 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22091 }
22092
22093 /* Display mode lines. */
22094 clear_glyph_matrix (w->desired_matrix);
22095 if (display_mode_lines (w))
22096 ++nwindows;
22097
22098 /* Restore old settings. */
22099 set_buffer_internal_1 (old);
22100 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22101 }
22102
22103 window = w->next;
22104 }
22105
22106 return nwindows;
22107 }
22108
22109
22110 /* Display the mode and/or header line of window W. Value is the
22111 sum number of mode lines and header lines displayed. */
22112
22113 static int
22114 display_mode_lines (struct window *w)
22115 {
22116 Lisp_Object old_selected_window = selected_window;
22117 Lisp_Object old_selected_frame = selected_frame;
22118 Lisp_Object new_frame = w->frame;
22119 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22120 int n = 0;
22121
22122 selected_frame = new_frame;
22123 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22124 or window's point, then we'd need select_window_1 here as well. */
22125 XSETWINDOW (selected_window, w);
22126 XFRAME (new_frame)->selected_window = selected_window;
22127
22128 /* These will be set while the mode line specs are processed. */
22129 line_number_displayed = false;
22130 w->column_number_displayed = -1;
22131
22132 if (WINDOW_WANTS_MODELINE_P (w))
22133 {
22134 struct window *sel_w = XWINDOW (old_selected_window);
22135
22136 /* Select mode line face based on the real selected window. */
22137 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22138 BVAR (current_buffer, mode_line_format));
22139 ++n;
22140 }
22141
22142 if (WINDOW_WANTS_HEADER_LINE_P (w))
22143 {
22144 display_mode_line (w, HEADER_LINE_FACE_ID,
22145 BVAR (current_buffer, header_line_format));
22146 ++n;
22147 }
22148
22149 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22150 selected_frame = old_selected_frame;
22151 selected_window = old_selected_window;
22152 if (n > 0)
22153 w->must_be_updated_p = true;
22154 return n;
22155 }
22156
22157
22158 /* Display mode or header line of window W. FACE_ID specifies which
22159 line to display; it is either MODE_LINE_FACE_ID or
22160 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22161 display. Value is the pixel height of the mode/header line
22162 displayed. */
22163
22164 static int
22165 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22166 {
22167 struct it it;
22168 struct face *face;
22169 ptrdiff_t count = SPECPDL_INDEX ();
22170
22171 init_iterator (&it, w, -1, -1, NULL, face_id);
22172 /* Don't extend on a previously drawn mode-line.
22173 This may happen if called from pos_visible_p. */
22174 it.glyph_row->enabled_p = false;
22175 prepare_desired_row (w, it.glyph_row, true);
22176
22177 it.glyph_row->mode_line_p = true;
22178
22179 /* FIXME: This should be controlled by a user option. But
22180 supporting such an option is not trivial, since the mode line is
22181 made up of many separate strings. */
22182 it.paragraph_embedding = L2R;
22183
22184 record_unwind_protect (unwind_format_mode_line,
22185 format_mode_line_unwind_data (NULL, NULL,
22186 Qnil, false));
22187
22188 mode_line_target = MODE_LINE_DISPLAY;
22189
22190 /* Temporarily make frame's keyboard the current kboard so that
22191 kboard-local variables in the mode_line_format will get the right
22192 values. */
22193 push_kboard (FRAME_KBOARD (it.f));
22194 record_unwind_save_match_data ();
22195 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22196 pop_kboard ();
22197
22198 unbind_to (count, Qnil);
22199
22200 /* Fill up with spaces. */
22201 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22202
22203 compute_line_metrics (&it);
22204 it.glyph_row->full_width_p = true;
22205 it.glyph_row->continued_p = false;
22206 it.glyph_row->truncated_on_left_p = false;
22207 it.glyph_row->truncated_on_right_p = false;
22208
22209 /* Make a 3D mode-line have a shadow at its right end. */
22210 face = FACE_FROM_ID (it.f, face_id);
22211 extend_face_to_end_of_line (&it);
22212 if (face->box != FACE_NO_BOX)
22213 {
22214 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22215 + it.glyph_row->used[TEXT_AREA] - 1);
22216 last->right_box_line_p = true;
22217 }
22218
22219 return it.glyph_row->height;
22220 }
22221
22222 /* Move element ELT in LIST to the front of LIST.
22223 Return the updated list. */
22224
22225 static Lisp_Object
22226 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22227 {
22228 register Lisp_Object tail, prev;
22229 register Lisp_Object tem;
22230
22231 tail = list;
22232 prev = Qnil;
22233 while (CONSP (tail))
22234 {
22235 tem = XCAR (tail);
22236
22237 if (EQ (elt, tem))
22238 {
22239 /* Splice out the link TAIL. */
22240 if (NILP (prev))
22241 list = XCDR (tail);
22242 else
22243 Fsetcdr (prev, XCDR (tail));
22244
22245 /* Now make it the first. */
22246 Fsetcdr (tail, list);
22247 return tail;
22248 }
22249 else
22250 prev = tail;
22251 tail = XCDR (tail);
22252 QUIT;
22253 }
22254
22255 /* Not found--return unchanged LIST. */
22256 return list;
22257 }
22258
22259 /* Contribute ELT to the mode line for window IT->w. How it
22260 translates into text depends on its data type.
22261
22262 IT describes the display environment in which we display, as usual.
22263
22264 DEPTH is the depth in recursion. It is used to prevent
22265 infinite recursion here.
22266
22267 FIELD_WIDTH is the number of characters the display of ELT should
22268 occupy in the mode line, and PRECISION is the maximum number of
22269 characters to display from ELT's representation. See
22270 display_string for details.
22271
22272 Returns the hpos of the end of the text generated by ELT.
22273
22274 PROPS is a property list to add to any string we encounter.
22275
22276 If RISKY, remove (disregard) any properties in any string
22277 we encounter, and ignore :eval and :propertize.
22278
22279 The global variable `mode_line_target' determines whether the
22280 output is passed to `store_mode_line_noprop',
22281 `store_mode_line_string', or `display_string'. */
22282
22283 static int
22284 display_mode_element (struct it *it, int depth, int field_width, int precision,
22285 Lisp_Object elt, Lisp_Object props, bool risky)
22286 {
22287 int n = 0, field, prec;
22288 bool literal = false;
22289
22290 tail_recurse:
22291 if (depth > 100)
22292 elt = build_string ("*too-deep*");
22293
22294 depth++;
22295
22296 switch (XTYPE (elt))
22297 {
22298 case Lisp_String:
22299 {
22300 /* A string: output it and check for %-constructs within it. */
22301 unsigned char c;
22302 ptrdiff_t offset = 0;
22303
22304 if (SCHARS (elt) > 0
22305 && (!NILP (props) || risky))
22306 {
22307 Lisp_Object oprops, aelt;
22308 oprops = Ftext_properties_at (make_number (0), elt);
22309
22310 /* If the starting string's properties are not what
22311 we want, translate the string. Also, if the string
22312 is risky, do that anyway. */
22313
22314 if (NILP (Fequal (props, oprops)) || risky)
22315 {
22316 /* If the starting string has properties,
22317 merge the specified ones onto the existing ones. */
22318 if (! NILP (oprops) && !risky)
22319 {
22320 Lisp_Object tem;
22321
22322 oprops = Fcopy_sequence (oprops);
22323 tem = props;
22324 while (CONSP (tem))
22325 {
22326 oprops = Fplist_put (oprops, XCAR (tem),
22327 XCAR (XCDR (tem)));
22328 tem = XCDR (XCDR (tem));
22329 }
22330 props = oprops;
22331 }
22332
22333 aelt = Fassoc (elt, mode_line_proptrans_alist);
22334 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22335 {
22336 /* AELT is what we want. Move it to the front
22337 without consing. */
22338 elt = XCAR (aelt);
22339 mode_line_proptrans_alist
22340 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22341 }
22342 else
22343 {
22344 Lisp_Object tem;
22345
22346 /* If AELT has the wrong props, it is useless.
22347 so get rid of it. */
22348 if (! NILP (aelt))
22349 mode_line_proptrans_alist
22350 = Fdelq (aelt, mode_line_proptrans_alist);
22351
22352 elt = Fcopy_sequence (elt);
22353 Fset_text_properties (make_number (0), Flength (elt),
22354 props, elt);
22355 /* Add this item to mode_line_proptrans_alist. */
22356 mode_line_proptrans_alist
22357 = Fcons (Fcons (elt, props),
22358 mode_line_proptrans_alist);
22359 /* Truncate mode_line_proptrans_alist
22360 to at most 50 elements. */
22361 tem = Fnthcdr (make_number (50),
22362 mode_line_proptrans_alist);
22363 if (! NILP (tem))
22364 XSETCDR (tem, Qnil);
22365 }
22366 }
22367 }
22368
22369 offset = 0;
22370
22371 if (literal)
22372 {
22373 prec = precision - n;
22374 switch (mode_line_target)
22375 {
22376 case MODE_LINE_NOPROP:
22377 case MODE_LINE_TITLE:
22378 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22379 break;
22380 case MODE_LINE_STRING:
22381 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22382 break;
22383 case MODE_LINE_DISPLAY:
22384 n += display_string (NULL, elt, Qnil, 0, 0, it,
22385 0, prec, 0, STRING_MULTIBYTE (elt));
22386 break;
22387 }
22388
22389 break;
22390 }
22391
22392 /* Handle the non-literal case. */
22393
22394 while ((precision <= 0 || n < precision)
22395 && SREF (elt, offset) != 0
22396 && (mode_line_target != MODE_LINE_DISPLAY
22397 || it->current_x < it->last_visible_x))
22398 {
22399 ptrdiff_t last_offset = offset;
22400
22401 /* Advance to end of string or next format specifier. */
22402 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22403 ;
22404
22405 if (offset - 1 != last_offset)
22406 {
22407 ptrdiff_t nchars, nbytes;
22408
22409 /* Output to end of string or up to '%'. Field width
22410 is length of string. Don't output more than
22411 PRECISION allows us. */
22412 offset--;
22413
22414 prec = c_string_width (SDATA (elt) + last_offset,
22415 offset - last_offset, precision - n,
22416 &nchars, &nbytes);
22417
22418 switch (mode_line_target)
22419 {
22420 case MODE_LINE_NOPROP:
22421 case MODE_LINE_TITLE:
22422 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22423 break;
22424 case MODE_LINE_STRING:
22425 {
22426 ptrdiff_t bytepos = last_offset;
22427 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22428 ptrdiff_t endpos = (precision <= 0
22429 ? string_byte_to_char (elt, offset)
22430 : charpos + nchars);
22431 Lisp_Object mode_string
22432 = Fsubstring (elt, make_number (charpos),
22433 make_number (endpos));
22434 n += store_mode_line_string (NULL, mode_string, false,
22435 0, 0, Qnil);
22436 }
22437 break;
22438 case MODE_LINE_DISPLAY:
22439 {
22440 ptrdiff_t bytepos = last_offset;
22441 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22442
22443 if (precision <= 0)
22444 nchars = string_byte_to_char (elt, offset) - charpos;
22445 n += display_string (NULL, elt, Qnil, 0, charpos,
22446 it, 0, nchars, 0,
22447 STRING_MULTIBYTE (elt));
22448 }
22449 break;
22450 }
22451 }
22452 else /* c == '%' */
22453 {
22454 ptrdiff_t percent_position = offset;
22455
22456 /* Get the specified minimum width. Zero means
22457 don't pad. */
22458 field = 0;
22459 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22460 field = field * 10 + c - '0';
22461
22462 /* Don't pad beyond the total padding allowed. */
22463 if (field_width - n > 0 && field > field_width - n)
22464 field = field_width - n;
22465
22466 /* Note that either PRECISION <= 0 or N < PRECISION. */
22467 prec = precision - n;
22468
22469 if (c == 'M')
22470 n += display_mode_element (it, depth, field, prec,
22471 Vglobal_mode_string, props,
22472 risky);
22473 else if (c != 0)
22474 {
22475 bool multibyte;
22476 ptrdiff_t bytepos, charpos;
22477 const char *spec;
22478 Lisp_Object string;
22479
22480 bytepos = percent_position;
22481 charpos = (STRING_MULTIBYTE (elt)
22482 ? string_byte_to_char (elt, bytepos)
22483 : bytepos);
22484 spec = decode_mode_spec (it->w, c, field, &string);
22485 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22486
22487 switch (mode_line_target)
22488 {
22489 case MODE_LINE_NOPROP:
22490 case MODE_LINE_TITLE:
22491 n += store_mode_line_noprop (spec, field, prec);
22492 break;
22493 case MODE_LINE_STRING:
22494 {
22495 Lisp_Object tem = build_string (spec);
22496 props = Ftext_properties_at (make_number (charpos), elt);
22497 /* Should only keep face property in props */
22498 n += store_mode_line_string (NULL, tem, false,
22499 field, prec, props);
22500 }
22501 break;
22502 case MODE_LINE_DISPLAY:
22503 {
22504 int nglyphs_before, nwritten;
22505
22506 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22507 nwritten = display_string (spec, string, elt,
22508 charpos, 0, it,
22509 field, prec, 0,
22510 multibyte);
22511
22512 /* Assign to the glyphs written above the
22513 string where the `%x' came from, position
22514 of the `%'. */
22515 if (nwritten > 0)
22516 {
22517 struct glyph *glyph
22518 = (it->glyph_row->glyphs[TEXT_AREA]
22519 + nglyphs_before);
22520 int i;
22521
22522 for (i = 0; i < nwritten; ++i)
22523 {
22524 glyph[i].object = elt;
22525 glyph[i].charpos = charpos;
22526 }
22527
22528 n += nwritten;
22529 }
22530 }
22531 break;
22532 }
22533 }
22534 else /* c == 0 */
22535 break;
22536 }
22537 }
22538 }
22539 break;
22540
22541 case Lisp_Symbol:
22542 /* A symbol: process the value of the symbol recursively
22543 as if it appeared here directly. Avoid error if symbol void.
22544 Special case: if value of symbol is a string, output the string
22545 literally. */
22546 {
22547 register Lisp_Object tem;
22548
22549 /* If the variable is not marked as risky to set
22550 then its contents are risky to use. */
22551 if (NILP (Fget (elt, Qrisky_local_variable)))
22552 risky = true;
22553
22554 tem = Fboundp (elt);
22555 if (!NILP (tem))
22556 {
22557 tem = Fsymbol_value (elt);
22558 /* If value is a string, output that string literally:
22559 don't check for % within it. */
22560 if (STRINGP (tem))
22561 literal = true;
22562
22563 if (!EQ (tem, elt))
22564 {
22565 /* Give up right away for nil or t. */
22566 elt = tem;
22567 goto tail_recurse;
22568 }
22569 }
22570 }
22571 break;
22572
22573 case Lisp_Cons:
22574 {
22575 register Lisp_Object car, tem;
22576
22577 /* A cons cell: five distinct cases.
22578 If first element is :eval or :propertize, do something special.
22579 If first element is a string or a cons, process all the elements
22580 and effectively concatenate them.
22581 If first element is a negative number, truncate displaying cdr to
22582 at most that many characters. If positive, pad (with spaces)
22583 to at least that many characters.
22584 If first element is a symbol, process the cadr or caddr recursively
22585 according to whether the symbol's value is non-nil or nil. */
22586 car = XCAR (elt);
22587 if (EQ (car, QCeval))
22588 {
22589 /* An element of the form (:eval FORM) means evaluate FORM
22590 and use the result as mode line elements. */
22591
22592 if (risky)
22593 break;
22594
22595 if (CONSP (XCDR (elt)))
22596 {
22597 Lisp_Object spec;
22598 spec = safe__eval (true, XCAR (XCDR (elt)));
22599 n += display_mode_element (it, depth, field_width - n,
22600 precision - n, spec, props,
22601 risky);
22602 }
22603 }
22604 else if (EQ (car, QCpropertize))
22605 {
22606 /* An element of the form (:propertize ELT PROPS...)
22607 means display ELT but applying properties PROPS. */
22608
22609 if (risky)
22610 break;
22611
22612 if (CONSP (XCDR (elt)))
22613 n += display_mode_element (it, depth, field_width - n,
22614 precision - n, XCAR (XCDR (elt)),
22615 XCDR (XCDR (elt)), risky);
22616 }
22617 else if (SYMBOLP (car))
22618 {
22619 tem = Fboundp (car);
22620 elt = XCDR (elt);
22621 if (!CONSP (elt))
22622 goto invalid;
22623 /* elt is now the cdr, and we know it is a cons cell.
22624 Use its car if CAR has a non-nil value. */
22625 if (!NILP (tem))
22626 {
22627 tem = Fsymbol_value (car);
22628 if (!NILP (tem))
22629 {
22630 elt = XCAR (elt);
22631 goto tail_recurse;
22632 }
22633 }
22634 /* Symbol's value is nil (or symbol is unbound)
22635 Get the cddr of the original list
22636 and if possible find the caddr and use that. */
22637 elt = XCDR (elt);
22638 if (NILP (elt))
22639 break;
22640 else if (!CONSP (elt))
22641 goto invalid;
22642 elt = XCAR (elt);
22643 goto tail_recurse;
22644 }
22645 else if (INTEGERP (car))
22646 {
22647 register int lim = XINT (car);
22648 elt = XCDR (elt);
22649 if (lim < 0)
22650 {
22651 /* Negative int means reduce maximum width. */
22652 if (precision <= 0)
22653 precision = -lim;
22654 else
22655 precision = min (precision, -lim);
22656 }
22657 else if (lim > 0)
22658 {
22659 /* Padding specified. Don't let it be more than
22660 current maximum. */
22661 if (precision > 0)
22662 lim = min (precision, lim);
22663
22664 /* If that's more padding than already wanted, queue it.
22665 But don't reduce padding already specified even if
22666 that is beyond the current truncation point. */
22667 field_width = max (lim, field_width);
22668 }
22669 goto tail_recurse;
22670 }
22671 else if (STRINGP (car) || CONSP (car))
22672 {
22673 Lisp_Object halftail = elt;
22674 int len = 0;
22675
22676 while (CONSP (elt)
22677 && (precision <= 0 || n < precision))
22678 {
22679 n += display_mode_element (it, depth,
22680 /* Do padding only after the last
22681 element in the list. */
22682 (! CONSP (XCDR (elt))
22683 ? field_width - n
22684 : 0),
22685 precision - n, XCAR (elt),
22686 props, risky);
22687 elt = XCDR (elt);
22688 len++;
22689 if ((len & 1) == 0)
22690 halftail = XCDR (halftail);
22691 /* Check for cycle. */
22692 if (EQ (halftail, elt))
22693 break;
22694 }
22695 }
22696 }
22697 break;
22698
22699 default:
22700 invalid:
22701 elt = build_string ("*invalid*");
22702 goto tail_recurse;
22703 }
22704
22705 /* Pad to FIELD_WIDTH. */
22706 if (field_width > 0 && n < field_width)
22707 {
22708 switch (mode_line_target)
22709 {
22710 case MODE_LINE_NOPROP:
22711 case MODE_LINE_TITLE:
22712 n += store_mode_line_noprop ("", field_width - n, 0);
22713 break;
22714 case MODE_LINE_STRING:
22715 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
22716 Qnil);
22717 break;
22718 case MODE_LINE_DISPLAY:
22719 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22720 0, 0, 0);
22721 break;
22722 }
22723 }
22724
22725 return n;
22726 }
22727
22728 /* Store a mode-line string element in mode_line_string_list.
22729
22730 If STRING is non-null, display that C string. Otherwise, the Lisp
22731 string LISP_STRING is displayed.
22732
22733 FIELD_WIDTH is the minimum number of output glyphs to produce.
22734 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22735 with spaces. FIELD_WIDTH <= 0 means don't pad.
22736
22737 PRECISION is the maximum number of characters to output from
22738 STRING. PRECISION <= 0 means don't truncate the string.
22739
22740 If COPY_STRING, make a copy of LISP_STRING before adding
22741 properties to the string.
22742
22743 PROPS are the properties to add to the string.
22744 The mode_line_string_face face property is always added to the string.
22745 */
22746
22747 static int
22748 store_mode_line_string (const char *string, Lisp_Object lisp_string,
22749 bool copy_string,
22750 int field_width, int precision, Lisp_Object props)
22751 {
22752 ptrdiff_t len;
22753 int n = 0;
22754
22755 if (string != NULL)
22756 {
22757 len = strlen (string);
22758 if (precision > 0 && len > precision)
22759 len = precision;
22760 lisp_string = make_string (string, len);
22761 if (NILP (props))
22762 props = mode_line_string_face_prop;
22763 else if (!NILP (mode_line_string_face))
22764 {
22765 Lisp_Object face = Fplist_get (props, Qface);
22766 props = Fcopy_sequence (props);
22767 if (NILP (face))
22768 face = mode_line_string_face;
22769 else
22770 face = list2 (face, mode_line_string_face);
22771 props = Fplist_put (props, Qface, face);
22772 }
22773 Fadd_text_properties (make_number (0), make_number (len),
22774 props, lisp_string);
22775 }
22776 else
22777 {
22778 len = XFASTINT (Flength (lisp_string));
22779 if (precision > 0 && len > precision)
22780 {
22781 len = precision;
22782 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22783 precision = -1;
22784 }
22785 if (!NILP (mode_line_string_face))
22786 {
22787 Lisp_Object face;
22788 if (NILP (props))
22789 props = Ftext_properties_at (make_number (0), lisp_string);
22790 face = Fplist_get (props, Qface);
22791 if (NILP (face))
22792 face = mode_line_string_face;
22793 else
22794 face = list2 (face, mode_line_string_face);
22795 props = list2 (Qface, face);
22796 if (copy_string)
22797 lisp_string = Fcopy_sequence (lisp_string);
22798 }
22799 if (!NILP (props))
22800 Fadd_text_properties (make_number (0), make_number (len),
22801 props, lisp_string);
22802 }
22803
22804 if (len > 0)
22805 {
22806 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22807 n += len;
22808 }
22809
22810 if (field_width > len)
22811 {
22812 field_width -= len;
22813 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22814 if (!NILP (props))
22815 Fadd_text_properties (make_number (0), make_number (field_width),
22816 props, lisp_string);
22817 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22818 n += field_width;
22819 }
22820
22821 return n;
22822 }
22823
22824
22825 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22826 1, 4, 0,
22827 doc: /* Format a string out of a mode line format specification.
22828 First arg FORMAT specifies the mode line format (see `mode-line-format'
22829 for details) to use.
22830
22831 By default, the format is evaluated for the currently selected window.
22832
22833 Optional second arg FACE specifies the face property to put on all
22834 characters for which no face is specified. The value nil means the
22835 default face. The value t means whatever face the window's mode line
22836 currently uses (either `mode-line' or `mode-line-inactive',
22837 depending on whether the window is the selected window or not).
22838 An integer value means the value string has no text
22839 properties.
22840
22841 Optional third and fourth args WINDOW and BUFFER specify the window
22842 and buffer to use as the context for the formatting (defaults
22843 are the selected window and the WINDOW's buffer). */)
22844 (Lisp_Object format, Lisp_Object face,
22845 Lisp_Object window, Lisp_Object buffer)
22846 {
22847 struct it it;
22848 int len;
22849 struct window *w;
22850 struct buffer *old_buffer = NULL;
22851 int face_id;
22852 bool no_props = INTEGERP (face);
22853 ptrdiff_t count = SPECPDL_INDEX ();
22854 Lisp_Object str;
22855 int string_start = 0;
22856
22857 w = decode_any_window (window);
22858 XSETWINDOW (window, w);
22859
22860 if (NILP (buffer))
22861 buffer = w->contents;
22862 CHECK_BUFFER (buffer);
22863
22864 /* Make formatting the modeline a non-op when noninteractive, otherwise
22865 there will be problems later caused by a partially initialized frame. */
22866 if (NILP (format) || noninteractive)
22867 return empty_unibyte_string;
22868
22869 if (no_props)
22870 face = Qnil;
22871
22872 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22873 : EQ (face, Qt) ? (EQ (window, selected_window)
22874 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22875 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22876 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22877 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22878 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22879 : DEFAULT_FACE_ID;
22880
22881 old_buffer = current_buffer;
22882
22883 /* Save things including mode_line_proptrans_alist,
22884 and set that to nil so that we don't alter the outer value. */
22885 record_unwind_protect (unwind_format_mode_line,
22886 format_mode_line_unwind_data
22887 (XFRAME (WINDOW_FRAME (w)),
22888 old_buffer, selected_window, true));
22889 mode_line_proptrans_alist = Qnil;
22890
22891 Fselect_window (window, Qt);
22892 set_buffer_internal_1 (XBUFFER (buffer));
22893
22894 init_iterator (&it, w, -1, -1, NULL, face_id);
22895
22896 if (no_props)
22897 {
22898 mode_line_target = MODE_LINE_NOPROP;
22899 mode_line_string_face_prop = Qnil;
22900 mode_line_string_list = Qnil;
22901 string_start = MODE_LINE_NOPROP_LEN (0);
22902 }
22903 else
22904 {
22905 mode_line_target = MODE_LINE_STRING;
22906 mode_line_string_list = Qnil;
22907 mode_line_string_face = face;
22908 mode_line_string_face_prop
22909 = NILP (face) ? Qnil : list2 (Qface, face);
22910 }
22911
22912 push_kboard (FRAME_KBOARD (it.f));
22913 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22914 pop_kboard ();
22915
22916 if (no_props)
22917 {
22918 len = MODE_LINE_NOPROP_LEN (string_start);
22919 str = make_string (mode_line_noprop_buf + string_start, len);
22920 }
22921 else
22922 {
22923 mode_line_string_list = Fnreverse (mode_line_string_list);
22924 str = Fmapconcat (Qidentity, mode_line_string_list,
22925 empty_unibyte_string);
22926 }
22927
22928 unbind_to (count, Qnil);
22929 return str;
22930 }
22931
22932 /* Write a null-terminated, right justified decimal representation of
22933 the positive integer D to BUF using a minimal field width WIDTH. */
22934
22935 static void
22936 pint2str (register char *buf, register int width, register ptrdiff_t d)
22937 {
22938 register char *p = buf;
22939
22940 if (d <= 0)
22941 *p++ = '0';
22942 else
22943 {
22944 while (d > 0)
22945 {
22946 *p++ = d % 10 + '0';
22947 d /= 10;
22948 }
22949 }
22950
22951 for (width -= (int) (p - buf); width > 0; --width)
22952 *p++ = ' ';
22953 *p-- = '\0';
22954 while (p > buf)
22955 {
22956 d = *buf;
22957 *buf++ = *p;
22958 *p-- = d;
22959 }
22960 }
22961
22962 /* Write a null-terminated, right justified decimal and "human
22963 readable" representation of the nonnegative integer D to BUF using
22964 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22965
22966 static const char power_letter[] =
22967 {
22968 0, /* no letter */
22969 'k', /* kilo */
22970 'M', /* mega */
22971 'G', /* giga */
22972 'T', /* tera */
22973 'P', /* peta */
22974 'E', /* exa */
22975 'Z', /* zetta */
22976 'Y' /* yotta */
22977 };
22978
22979 static void
22980 pint2hrstr (char *buf, int width, ptrdiff_t d)
22981 {
22982 /* We aim to represent the nonnegative integer D as
22983 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22984 ptrdiff_t quotient = d;
22985 int remainder = 0;
22986 /* -1 means: do not use TENTHS. */
22987 int tenths = -1;
22988 int exponent = 0;
22989
22990 /* Length of QUOTIENT.TENTHS as a string. */
22991 int length;
22992
22993 char * psuffix;
22994 char * p;
22995
22996 if (quotient >= 1000)
22997 {
22998 /* Scale to the appropriate EXPONENT. */
22999 do
23000 {
23001 remainder = quotient % 1000;
23002 quotient /= 1000;
23003 exponent++;
23004 }
23005 while (quotient >= 1000);
23006
23007 /* Round to nearest and decide whether to use TENTHS or not. */
23008 if (quotient <= 9)
23009 {
23010 tenths = remainder / 100;
23011 if (remainder % 100 >= 50)
23012 {
23013 if (tenths < 9)
23014 tenths++;
23015 else
23016 {
23017 quotient++;
23018 if (quotient == 10)
23019 tenths = -1;
23020 else
23021 tenths = 0;
23022 }
23023 }
23024 }
23025 else
23026 if (remainder >= 500)
23027 {
23028 if (quotient < 999)
23029 quotient++;
23030 else
23031 {
23032 quotient = 1;
23033 exponent++;
23034 tenths = 0;
23035 }
23036 }
23037 }
23038
23039 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23040 if (tenths == -1 && quotient <= 99)
23041 if (quotient <= 9)
23042 length = 1;
23043 else
23044 length = 2;
23045 else
23046 length = 3;
23047 p = psuffix = buf + max (width, length);
23048
23049 /* Print EXPONENT. */
23050 *psuffix++ = power_letter[exponent];
23051 *psuffix = '\0';
23052
23053 /* Print TENTHS. */
23054 if (tenths >= 0)
23055 {
23056 *--p = '0' + tenths;
23057 *--p = '.';
23058 }
23059
23060 /* Print QUOTIENT. */
23061 do
23062 {
23063 int digit = quotient % 10;
23064 *--p = '0' + digit;
23065 }
23066 while ((quotient /= 10) != 0);
23067
23068 /* Print leading spaces. */
23069 while (buf < p)
23070 *--p = ' ';
23071 }
23072
23073 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23074 If EOL_FLAG, set also a mnemonic character for end-of-line
23075 type of CODING_SYSTEM. Return updated pointer into BUF. */
23076
23077 static unsigned char invalid_eol_type[] = "(*invalid*)";
23078
23079 static char *
23080 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23081 {
23082 Lisp_Object val;
23083 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23084 const unsigned char *eol_str;
23085 int eol_str_len;
23086 /* The EOL conversion we are using. */
23087 Lisp_Object eoltype;
23088
23089 val = CODING_SYSTEM_SPEC (coding_system);
23090 eoltype = Qnil;
23091
23092 if (!VECTORP (val)) /* Not yet decided. */
23093 {
23094 *buf++ = multibyte ? '-' : ' ';
23095 if (eol_flag)
23096 eoltype = eol_mnemonic_undecided;
23097 /* Don't mention EOL conversion if it isn't decided. */
23098 }
23099 else
23100 {
23101 Lisp_Object attrs;
23102 Lisp_Object eolvalue;
23103
23104 attrs = AREF (val, 0);
23105 eolvalue = AREF (val, 2);
23106
23107 *buf++ = multibyte
23108 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23109 : ' ';
23110
23111 if (eol_flag)
23112 {
23113 /* The EOL conversion that is normal on this system. */
23114
23115 if (NILP (eolvalue)) /* Not yet decided. */
23116 eoltype = eol_mnemonic_undecided;
23117 else if (VECTORP (eolvalue)) /* Not yet decided. */
23118 eoltype = eol_mnemonic_undecided;
23119 else /* eolvalue is Qunix, Qdos, or Qmac. */
23120 eoltype = (EQ (eolvalue, Qunix)
23121 ? eol_mnemonic_unix
23122 : EQ (eolvalue, Qdos)
23123 ? eol_mnemonic_dos : eol_mnemonic_mac);
23124 }
23125 }
23126
23127 if (eol_flag)
23128 {
23129 /* Mention the EOL conversion if it is not the usual one. */
23130 if (STRINGP (eoltype))
23131 {
23132 eol_str = SDATA (eoltype);
23133 eol_str_len = SBYTES (eoltype);
23134 }
23135 else if (CHARACTERP (eoltype))
23136 {
23137 int c = XFASTINT (eoltype);
23138 return buf + CHAR_STRING (c, (unsigned char *) buf);
23139 }
23140 else
23141 {
23142 eol_str = invalid_eol_type;
23143 eol_str_len = sizeof (invalid_eol_type) - 1;
23144 }
23145 memcpy (buf, eol_str, eol_str_len);
23146 buf += eol_str_len;
23147 }
23148
23149 return buf;
23150 }
23151
23152 /* Return a string for the output of a mode line %-spec for window W,
23153 generated by character C. FIELD_WIDTH > 0 means pad the string
23154 returned with spaces to that value. Return a Lisp string in
23155 *STRING if the resulting string is taken from that Lisp string.
23156
23157 Note we operate on the current buffer for most purposes. */
23158
23159 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23160
23161 static const char *
23162 decode_mode_spec (struct window *w, register int c, int field_width,
23163 Lisp_Object *string)
23164 {
23165 Lisp_Object obj;
23166 struct frame *f = XFRAME (WINDOW_FRAME (w));
23167 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23168 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23169 produce strings from numerical values, so limit preposterously
23170 large values of FIELD_WIDTH to avoid overrunning the buffer's
23171 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23172 bytes plus the terminating null. */
23173 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23174 struct buffer *b = current_buffer;
23175
23176 obj = Qnil;
23177 *string = Qnil;
23178
23179 switch (c)
23180 {
23181 case '*':
23182 if (!NILP (BVAR (b, read_only)))
23183 return "%";
23184 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23185 return "*";
23186 return "-";
23187
23188 case '+':
23189 /* This differs from %* only for a modified read-only buffer. */
23190 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23191 return "*";
23192 if (!NILP (BVAR (b, read_only)))
23193 return "%";
23194 return "-";
23195
23196 case '&':
23197 /* This differs from %* in ignoring read-only-ness. */
23198 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23199 return "*";
23200 return "-";
23201
23202 case '%':
23203 return "%";
23204
23205 case '[':
23206 {
23207 int i;
23208 char *p;
23209
23210 if (command_loop_level > 5)
23211 return "[[[... ";
23212 p = decode_mode_spec_buf;
23213 for (i = 0; i < command_loop_level; i++)
23214 *p++ = '[';
23215 *p = 0;
23216 return decode_mode_spec_buf;
23217 }
23218
23219 case ']':
23220 {
23221 int i;
23222 char *p;
23223
23224 if (command_loop_level > 5)
23225 return " ...]]]";
23226 p = decode_mode_spec_buf;
23227 for (i = 0; i < command_loop_level; i++)
23228 *p++ = ']';
23229 *p = 0;
23230 return decode_mode_spec_buf;
23231 }
23232
23233 case '-':
23234 {
23235 register int i;
23236
23237 /* Let lots_of_dashes be a string of infinite length. */
23238 if (mode_line_target == MODE_LINE_NOPROP
23239 || mode_line_target == MODE_LINE_STRING)
23240 return "--";
23241 if (field_width <= 0
23242 || field_width > sizeof (lots_of_dashes))
23243 {
23244 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23245 decode_mode_spec_buf[i] = '-';
23246 decode_mode_spec_buf[i] = '\0';
23247 return decode_mode_spec_buf;
23248 }
23249 else
23250 return lots_of_dashes;
23251 }
23252
23253 case 'b':
23254 obj = BVAR (b, name);
23255 break;
23256
23257 case 'c':
23258 /* %c and %l are ignored in `frame-title-format'.
23259 (In redisplay_internal, the frame title is drawn _before_ the
23260 windows are updated, so the stuff which depends on actual
23261 window contents (such as %l) may fail to render properly, or
23262 even crash emacs.) */
23263 if (mode_line_target == MODE_LINE_TITLE)
23264 return "";
23265 else
23266 {
23267 ptrdiff_t col = current_column ();
23268 w->column_number_displayed = col;
23269 pint2str (decode_mode_spec_buf, width, col);
23270 return decode_mode_spec_buf;
23271 }
23272
23273 case 'e':
23274 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23275 {
23276 if (NILP (Vmemory_full))
23277 return "";
23278 else
23279 return "!MEM FULL! ";
23280 }
23281 #else
23282 return "";
23283 #endif
23284
23285 case 'F':
23286 /* %F displays the frame name. */
23287 if (!NILP (f->title))
23288 return SSDATA (f->title);
23289 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23290 return SSDATA (f->name);
23291 return "Emacs";
23292
23293 case 'f':
23294 obj = BVAR (b, filename);
23295 break;
23296
23297 case 'i':
23298 {
23299 ptrdiff_t size = ZV - BEGV;
23300 pint2str (decode_mode_spec_buf, width, size);
23301 return decode_mode_spec_buf;
23302 }
23303
23304 case 'I':
23305 {
23306 ptrdiff_t size = ZV - BEGV;
23307 pint2hrstr (decode_mode_spec_buf, width, size);
23308 return decode_mode_spec_buf;
23309 }
23310
23311 case 'l':
23312 {
23313 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23314 ptrdiff_t topline, nlines, height;
23315 ptrdiff_t junk;
23316
23317 /* %c and %l are ignored in `frame-title-format'. */
23318 if (mode_line_target == MODE_LINE_TITLE)
23319 return "";
23320
23321 startpos = marker_position (w->start);
23322 startpos_byte = marker_byte_position (w->start);
23323 height = WINDOW_TOTAL_LINES (w);
23324
23325 /* If we decided that this buffer isn't suitable for line numbers,
23326 don't forget that too fast. */
23327 if (w->base_line_pos == -1)
23328 goto no_value;
23329
23330 /* If the buffer is very big, don't waste time. */
23331 if (INTEGERP (Vline_number_display_limit)
23332 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23333 {
23334 w->base_line_pos = 0;
23335 w->base_line_number = 0;
23336 goto no_value;
23337 }
23338
23339 if (w->base_line_number > 0
23340 && w->base_line_pos > 0
23341 && w->base_line_pos <= startpos)
23342 {
23343 line = w->base_line_number;
23344 linepos = w->base_line_pos;
23345 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23346 }
23347 else
23348 {
23349 line = 1;
23350 linepos = BUF_BEGV (b);
23351 linepos_byte = BUF_BEGV_BYTE (b);
23352 }
23353
23354 /* Count lines from base line to window start position. */
23355 nlines = display_count_lines (linepos_byte,
23356 startpos_byte,
23357 startpos, &junk);
23358
23359 topline = nlines + line;
23360
23361 /* Determine a new base line, if the old one is too close
23362 or too far away, or if we did not have one.
23363 "Too close" means it's plausible a scroll-down would
23364 go back past it. */
23365 if (startpos == BUF_BEGV (b))
23366 {
23367 w->base_line_number = topline;
23368 w->base_line_pos = BUF_BEGV (b);
23369 }
23370 else if (nlines < height + 25 || nlines > height * 3 + 50
23371 || linepos == BUF_BEGV (b))
23372 {
23373 ptrdiff_t limit = BUF_BEGV (b);
23374 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23375 ptrdiff_t position;
23376 ptrdiff_t distance =
23377 (height * 2 + 30) * line_number_display_limit_width;
23378
23379 if (startpos - distance > limit)
23380 {
23381 limit = startpos - distance;
23382 limit_byte = CHAR_TO_BYTE (limit);
23383 }
23384
23385 nlines = display_count_lines (startpos_byte,
23386 limit_byte,
23387 - (height * 2 + 30),
23388 &position);
23389 /* If we couldn't find the lines we wanted within
23390 line_number_display_limit_width chars per line,
23391 give up on line numbers for this window. */
23392 if (position == limit_byte && limit == startpos - distance)
23393 {
23394 w->base_line_pos = -1;
23395 w->base_line_number = 0;
23396 goto no_value;
23397 }
23398
23399 w->base_line_number = topline - nlines;
23400 w->base_line_pos = BYTE_TO_CHAR (position);
23401 }
23402
23403 /* Now count lines from the start pos to point. */
23404 nlines = display_count_lines (startpos_byte,
23405 PT_BYTE, PT, &junk);
23406
23407 /* Record that we did display the line number. */
23408 line_number_displayed = true;
23409
23410 /* Make the string to show. */
23411 pint2str (decode_mode_spec_buf, width, topline + nlines);
23412 return decode_mode_spec_buf;
23413 no_value:
23414 {
23415 char *p = decode_mode_spec_buf;
23416 int pad = width - 2;
23417 while (pad-- > 0)
23418 *p++ = ' ';
23419 *p++ = '?';
23420 *p++ = '?';
23421 *p = '\0';
23422 return decode_mode_spec_buf;
23423 }
23424 }
23425 break;
23426
23427 case 'm':
23428 obj = BVAR (b, mode_name);
23429 break;
23430
23431 case 'n':
23432 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23433 return " Narrow";
23434 break;
23435
23436 case 'p':
23437 {
23438 ptrdiff_t pos = marker_position (w->start);
23439 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23440
23441 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23442 {
23443 if (pos <= BUF_BEGV (b))
23444 return "All";
23445 else
23446 return "Bottom";
23447 }
23448 else if (pos <= BUF_BEGV (b))
23449 return "Top";
23450 else
23451 {
23452 if (total > 1000000)
23453 /* Do it differently for a large value, to avoid overflow. */
23454 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23455 else
23456 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23457 /* We can't normally display a 3-digit number,
23458 so get us a 2-digit number that is close. */
23459 if (total == 100)
23460 total = 99;
23461 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23462 return decode_mode_spec_buf;
23463 }
23464 }
23465
23466 /* Display percentage of size above the bottom of the screen. */
23467 case 'P':
23468 {
23469 ptrdiff_t toppos = marker_position (w->start);
23470 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23471 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23472
23473 if (botpos >= BUF_ZV (b))
23474 {
23475 if (toppos <= BUF_BEGV (b))
23476 return "All";
23477 else
23478 return "Bottom";
23479 }
23480 else
23481 {
23482 if (total > 1000000)
23483 /* Do it differently for a large value, to avoid overflow. */
23484 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23485 else
23486 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23487 /* We can't normally display a 3-digit number,
23488 so get us a 2-digit number that is close. */
23489 if (total == 100)
23490 total = 99;
23491 if (toppos <= BUF_BEGV (b))
23492 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23493 else
23494 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23495 return decode_mode_spec_buf;
23496 }
23497 }
23498
23499 case 's':
23500 /* status of process */
23501 obj = Fget_buffer_process (Fcurrent_buffer ());
23502 if (NILP (obj))
23503 return "no process";
23504 #ifndef MSDOS
23505 obj = Fsymbol_name (Fprocess_status (obj));
23506 #endif
23507 break;
23508
23509 case '@':
23510 {
23511 ptrdiff_t count = inhibit_garbage_collection ();
23512 Lisp_Object curdir = BVAR (current_buffer, directory);
23513 Lisp_Object val = Qnil;
23514
23515 if (STRINGP (curdir))
23516 val = call1 (intern ("file-remote-p"), curdir);
23517
23518 unbind_to (count, Qnil);
23519
23520 if (NILP (val))
23521 return "-";
23522 else
23523 return "@";
23524 }
23525
23526 case 'z':
23527 /* coding-system (not including end-of-line format) */
23528 case 'Z':
23529 /* coding-system (including end-of-line type) */
23530 {
23531 bool eol_flag = (c == 'Z');
23532 char *p = decode_mode_spec_buf;
23533
23534 if (! FRAME_WINDOW_P (f))
23535 {
23536 /* No need to mention EOL here--the terminal never needs
23537 to do EOL conversion. */
23538 p = decode_mode_spec_coding (CODING_ID_NAME
23539 (FRAME_KEYBOARD_CODING (f)->id),
23540 p, false);
23541 p = decode_mode_spec_coding (CODING_ID_NAME
23542 (FRAME_TERMINAL_CODING (f)->id),
23543 p, false);
23544 }
23545 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23546 p, eol_flag);
23547
23548 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23549 #ifdef subprocesses
23550 obj = Fget_buffer_process (Fcurrent_buffer ());
23551 if (PROCESSP (obj))
23552 {
23553 p = decode_mode_spec_coding
23554 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23555 p = decode_mode_spec_coding
23556 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23557 }
23558 #endif /* subprocesses */
23559 #endif /* false */
23560 *p = 0;
23561 return decode_mode_spec_buf;
23562 }
23563 }
23564
23565 if (STRINGP (obj))
23566 {
23567 *string = obj;
23568 return SSDATA (obj);
23569 }
23570 else
23571 return "";
23572 }
23573
23574
23575 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23576 means count lines back from START_BYTE. But don't go beyond
23577 LIMIT_BYTE. Return the number of lines thus found (always
23578 nonnegative).
23579
23580 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23581 either the position COUNT lines after/before START_BYTE, if we
23582 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23583 COUNT lines. */
23584
23585 static ptrdiff_t
23586 display_count_lines (ptrdiff_t start_byte,
23587 ptrdiff_t limit_byte, ptrdiff_t count,
23588 ptrdiff_t *byte_pos_ptr)
23589 {
23590 register unsigned char *cursor;
23591 unsigned char *base;
23592
23593 register ptrdiff_t ceiling;
23594 register unsigned char *ceiling_addr;
23595 ptrdiff_t orig_count = count;
23596
23597 /* If we are not in selective display mode,
23598 check only for newlines. */
23599 bool selective_display
23600 = (!NILP (BVAR (current_buffer, selective_display))
23601 && !INTEGERP (BVAR (current_buffer, selective_display)));
23602
23603 if (count > 0)
23604 {
23605 while (start_byte < limit_byte)
23606 {
23607 ceiling = BUFFER_CEILING_OF (start_byte);
23608 ceiling = min (limit_byte - 1, ceiling);
23609 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23610 base = (cursor = BYTE_POS_ADDR (start_byte));
23611
23612 do
23613 {
23614 if (selective_display)
23615 {
23616 while (*cursor != '\n' && *cursor != 015
23617 && ++cursor != ceiling_addr)
23618 continue;
23619 if (cursor == ceiling_addr)
23620 break;
23621 }
23622 else
23623 {
23624 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23625 if (! cursor)
23626 break;
23627 }
23628
23629 cursor++;
23630
23631 if (--count == 0)
23632 {
23633 start_byte += cursor - base;
23634 *byte_pos_ptr = start_byte;
23635 return orig_count;
23636 }
23637 }
23638 while (cursor < ceiling_addr);
23639
23640 start_byte += ceiling_addr - base;
23641 }
23642 }
23643 else
23644 {
23645 while (start_byte > limit_byte)
23646 {
23647 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23648 ceiling = max (limit_byte, ceiling);
23649 ceiling_addr = BYTE_POS_ADDR (ceiling);
23650 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23651 while (true)
23652 {
23653 if (selective_display)
23654 {
23655 while (--cursor >= ceiling_addr
23656 && *cursor != '\n' && *cursor != 015)
23657 continue;
23658 if (cursor < ceiling_addr)
23659 break;
23660 }
23661 else
23662 {
23663 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23664 if (! cursor)
23665 break;
23666 }
23667
23668 if (++count == 0)
23669 {
23670 start_byte += cursor - base + 1;
23671 *byte_pos_ptr = start_byte;
23672 /* When scanning backwards, we should
23673 not count the newline posterior to which we stop. */
23674 return - orig_count - 1;
23675 }
23676 }
23677 start_byte += ceiling_addr - base;
23678 }
23679 }
23680
23681 *byte_pos_ptr = limit_byte;
23682
23683 if (count < 0)
23684 return - orig_count + count;
23685 return orig_count - count;
23686
23687 }
23688
23689
23690 \f
23691 /***********************************************************************
23692 Displaying strings
23693 ***********************************************************************/
23694
23695 /* Display a NUL-terminated string, starting with index START.
23696
23697 If STRING is non-null, display that C string. Otherwise, the Lisp
23698 string LISP_STRING is displayed. There's a case that STRING is
23699 non-null and LISP_STRING is not nil. It means STRING is a string
23700 data of LISP_STRING. In that case, we display LISP_STRING while
23701 ignoring its text properties.
23702
23703 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23704 FACE_STRING. Display STRING or LISP_STRING with the face at
23705 FACE_STRING_POS in FACE_STRING:
23706
23707 Display the string in the environment given by IT, but use the
23708 standard display table, temporarily.
23709
23710 FIELD_WIDTH is the minimum number of output glyphs to produce.
23711 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23712 with spaces. If STRING has more characters, more than FIELD_WIDTH
23713 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23714
23715 PRECISION is the maximum number of characters to output from
23716 STRING. PRECISION < 0 means don't truncate the string.
23717
23718 This is roughly equivalent to printf format specifiers:
23719
23720 FIELD_WIDTH PRECISION PRINTF
23721 ----------------------------------------
23722 -1 -1 %s
23723 -1 10 %.10s
23724 10 -1 %10s
23725 20 10 %20.10s
23726
23727 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23728 display them, and < 0 means obey the current buffer's value of
23729 enable_multibyte_characters.
23730
23731 Value is the number of columns displayed. */
23732
23733 static int
23734 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23735 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23736 int field_width, int precision, int max_x, int multibyte)
23737 {
23738 int hpos_at_start = it->hpos;
23739 int saved_face_id = it->face_id;
23740 struct glyph_row *row = it->glyph_row;
23741 ptrdiff_t it_charpos;
23742
23743 /* Initialize the iterator IT for iteration over STRING beginning
23744 with index START. */
23745 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23746 precision, field_width, multibyte);
23747 if (string && STRINGP (lisp_string))
23748 /* LISP_STRING is the one returned by decode_mode_spec. We should
23749 ignore its text properties. */
23750 it->stop_charpos = it->end_charpos;
23751
23752 /* If displaying STRING, set up the face of the iterator from
23753 FACE_STRING, if that's given. */
23754 if (STRINGP (face_string))
23755 {
23756 ptrdiff_t endptr;
23757 struct face *face;
23758
23759 it->face_id
23760 = face_at_string_position (it->w, face_string, face_string_pos,
23761 0, &endptr, it->base_face_id, false);
23762 face = FACE_FROM_ID (it->f, it->face_id);
23763 it->face_box_p = face->box != FACE_NO_BOX;
23764 }
23765
23766 /* Set max_x to the maximum allowed X position. Don't let it go
23767 beyond the right edge of the window. */
23768 if (max_x <= 0)
23769 max_x = it->last_visible_x;
23770 else
23771 max_x = min (max_x, it->last_visible_x);
23772
23773 /* Skip over display elements that are not visible. because IT->w is
23774 hscrolled. */
23775 if (it->current_x < it->first_visible_x)
23776 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23777 MOVE_TO_POS | MOVE_TO_X);
23778
23779 row->ascent = it->max_ascent;
23780 row->height = it->max_ascent + it->max_descent;
23781 row->phys_ascent = it->max_phys_ascent;
23782 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23783 row->extra_line_spacing = it->max_extra_line_spacing;
23784
23785 if (STRINGP (it->string))
23786 it_charpos = IT_STRING_CHARPOS (*it);
23787 else
23788 it_charpos = IT_CHARPOS (*it);
23789
23790 /* This condition is for the case that we are called with current_x
23791 past last_visible_x. */
23792 while (it->current_x < max_x)
23793 {
23794 int x_before, x, n_glyphs_before, i, nglyphs;
23795
23796 /* Get the next display element. */
23797 if (!get_next_display_element (it))
23798 break;
23799
23800 /* Produce glyphs. */
23801 x_before = it->current_x;
23802 n_glyphs_before = row->used[TEXT_AREA];
23803 PRODUCE_GLYPHS (it);
23804
23805 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23806 i = 0;
23807 x = x_before;
23808 while (i < nglyphs)
23809 {
23810 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23811
23812 if (it->line_wrap != TRUNCATE
23813 && x + glyph->pixel_width > max_x)
23814 {
23815 /* End of continued line or max_x reached. */
23816 if (CHAR_GLYPH_PADDING_P (*glyph))
23817 {
23818 /* A wide character is unbreakable. */
23819 if (row->reversed_p)
23820 unproduce_glyphs (it, row->used[TEXT_AREA]
23821 - n_glyphs_before);
23822 row->used[TEXT_AREA] = n_glyphs_before;
23823 it->current_x = x_before;
23824 }
23825 else
23826 {
23827 if (row->reversed_p)
23828 unproduce_glyphs (it, row->used[TEXT_AREA]
23829 - (n_glyphs_before + i));
23830 row->used[TEXT_AREA] = n_glyphs_before + i;
23831 it->current_x = x;
23832 }
23833 break;
23834 }
23835 else if (x + glyph->pixel_width >= it->first_visible_x)
23836 {
23837 /* Glyph is at least partially visible. */
23838 ++it->hpos;
23839 if (x < it->first_visible_x)
23840 row->x = x - it->first_visible_x;
23841 }
23842 else
23843 {
23844 /* Glyph is off the left margin of the display area.
23845 Should not happen. */
23846 emacs_abort ();
23847 }
23848
23849 row->ascent = max (row->ascent, it->max_ascent);
23850 row->height = max (row->height, it->max_ascent + it->max_descent);
23851 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23852 row->phys_height = max (row->phys_height,
23853 it->max_phys_ascent + it->max_phys_descent);
23854 row->extra_line_spacing = max (row->extra_line_spacing,
23855 it->max_extra_line_spacing);
23856 x += glyph->pixel_width;
23857 ++i;
23858 }
23859
23860 /* Stop if max_x reached. */
23861 if (i < nglyphs)
23862 break;
23863
23864 /* Stop at line ends. */
23865 if (ITERATOR_AT_END_OF_LINE_P (it))
23866 {
23867 it->continuation_lines_width = 0;
23868 break;
23869 }
23870
23871 set_iterator_to_next (it, true);
23872 if (STRINGP (it->string))
23873 it_charpos = IT_STRING_CHARPOS (*it);
23874 else
23875 it_charpos = IT_CHARPOS (*it);
23876
23877 /* Stop if truncating at the right edge. */
23878 if (it->line_wrap == TRUNCATE
23879 && it->current_x >= it->last_visible_x)
23880 {
23881 /* Add truncation mark, but don't do it if the line is
23882 truncated at a padding space. */
23883 if (it_charpos < it->string_nchars)
23884 {
23885 if (!FRAME_WINDOW_P (it->f))
23886 {
23887 int ii, n;
23888
23889 if (it->current_x > it->last_visible_x)
23890 {
23891 if (!row->reversed_p)
23892 {
23893 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23894 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23895 break;
23896 }
23897 else
23898 {
23899 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23900 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23901 break;
23902 unproduce_glyphs (it, ii + 1);
23903 ii = row->used[TEXT_AREA] - (ii + 1);
23904 }
23905 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23906 {
23907 row->used[TEXT_AREA] = ii;
23908 produce_special_glyphs (it, IT_TRUNCATION);
23909 }
23910 }
23911 produce_special_glyphs (it, IT_TRUNCATION);
23912 }
23913 row->truncated_on_right_p = true;
23914 }
23915 break;
23916 }
23917 }
23918
23919 /* Maybe insert a truncation at the left. */
23920 if (it->first_visible_x
23921 && it_charpos > 0)
23922 {
23923 if (!FRAME_WINDOW_P (it->f)
23924 || (row->reversed_p
23925 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23926 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23927 insert_left_trunc_glyphs (it);
23928 row->truncated_on_left_p = true;
23929 }
23930
23931 it->face_id = saved_face_id;
23932
23933 /* Value is number of columns displayed. */
23934 return it->hpos - hpos_at_start;
23935 }
23936
23937
23938 \f
23939 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23940 appears as an element of LIST or as the car of an element of LIST.
23941 If PROPVAL is a list, compare each element against LIST in that
23942 way, and return 1/2 if any element of PROPVAL is found in LIST.
23943 Otherwise return 0. This function cannot quit.
23944 The return value is 2 if the text is invisible but with an ellipsis
23945 and 1 if it's invisible and without an ellipsis. */
23946
23947 int
23948 invisible_prop (Lisp_Object propval, Lisp_Object list)
23949 {
23950 Lisp_Object tail, proptail;
23951
23952 for (tail = list; CONSP (tail); tail = XCDR (tail))
23953 {
23954 register Lisp_Object tem;
23955 tem = XCAR (tail);
23956 if (EQ (propval, tem))
23957 return 1;
23958 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23959 return NILP (XCDR (tem)) ? 1 : 2;
23960 }
23961
23962 if (CONSP (propval))
23963 {
23964 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23965 {
23966 Lisp_Object propelt;
23967 propelt = XCAR (proptail);
23968 for (tail = list; CONSP (tail); tail = XCDR (tail))
23969 {
23970 register Lisp_Object tem;
23971 tem = XCAR (tail);
23972 if (EQ (propelt, tem))
23973 return 1;
23974 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23975 return NILP (XCDR (tem)) ? 1 : 2;
23976 }
23977 }
23978 }
23979
23980 return 0;
23981 }
23982
23983 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23984 doc: /* Non-nil if the property makes the text invisible.
23985 POS-OR-PROP can be a marker or number, in which case it is taken to be
23986 a position in the current buffer and the value of the `invisible' property
23987 is checked; or it can be some other value, which is then presumed to be the
23988 value of the `invisible' property of the text of interest.
23989 The non-nil value returned can be t for truly invisible text or something
23990 else if the text is replaced by an ellipsis. */)
23991 (Lisp_Object pos_or_prop)
23992 {
23993 Lisp_Object prop
23994 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23995 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23996 : pos_or_prop);
23997 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23998 return (invis == 0 ? Qnil
23999 : invis == 1 ? Qt
24000 : make_number (invis));
24001 }
24002
24003 /* Calculate a width or height in pixels from a specification using
24004 the following elements:
24005
24006 SPEC ::=
24007 NUM - a (fractional) multiple of the default font width/height
24008 (NUM) - specifies exactly NUM pixels
24009 UNIT - a fixed number of pixels, see below.
24010 ELEMENT - size of a display element in pixels, see below.
24011 (NUM . SPEC) - equals NUM * SPEC
24012 (+ SPEC SPEC ...) - add pixel values
24013 (- SPEC SPEC ...) - subtract pixel values
24014 (- SPEC) - negate pixel value
24015
24016 NUM ::=
24017 INT or FLOAT - a number constant
24018 SYMBOL - use symbol's (buffer local) variable binding.
24019
24020 UNIT ::=
24021 in - pixels per inch *)
24022 mm - pixels per 1/1000 meter *)
24023 cm - pixels per 1/100 meter *)
24024 width - width of current font in pixels.
24025 height - height of current font in pixels.
24026
24027 *) using the ratio(s) defined in display-pixels-per-inch.
24028
24029 ELEMENT ::=
24030
24031 left-fringe - left fringe width in pixels
24032 right-fringe - right fringe width in pixels
24033
24034 left-margin - left margin width in pixels
24035 right-margin - right margin width in pixels
24036
24037 scroll-bar - scroll-bar area width in pixels
24038
24039 Examples:
24040
24041 Pixels corresponding to 5 inches:
24042 (5 . in)
24043
24044 Total width of non-text areas on left side of window (if scroll-bar is on left):
24045 '(space :width (+ left-fringe left-margin scroll-bar))
24046
24047 Align to first text column (in header line):
24048 '(space :align-to 0)
24049
24050 Align to middle of text area minus half the width of variable `my-image'
24051 containing a loaded image:
24052 '(space :align-to (0.5 . (- text my-image)))
24053
24054 Width of left margin minus width of 1 character in the default font:
24055 '(space :width (- left-margin 1))
24056
24057 Width of left margin minus width of 2 characters in the current font:
24058 '(space :width (- left-margin (2 . width)))
24059
24060 Center 1 character over left-margin (in header line):
24061 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24062
24063 Different ways to express width of left fringe plus left margin minus one pixel:
24064 '(space :width (- (+ left-fringe left-margin) (1)))
24065 '(space :width (+ left-fringe left-margin (- (1))))
24066 '(space :width (+ left-fringe left-margin (-1)))
24067
24068 */
24069
24070 static bool
24071 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24072 struct font *font, bool width_p, int *align_to)
24073 {
24074 double pixels;
24075
24076 # define OK_PIXELS(val) (*res = (val), true)
24077 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24078
24079 if (NILP (prop))
24080 return OK_PIXELS (0);
24081
24082 eassert (FRAME_LIVE_P (it->f));
24083
24084 if (SYMBOLP (prop))
24085 {
24086 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24087 {
24088 char *unit = SSDATA (SYMBOL_NAME (prop));
24089
24090 if (unit[0] == 'i' && unit[1] == 'n')
24091 pixels = 1.0;
24092 else if (unit[0] == 'm' && unit[1] == 'm')
24093 pixels = 25.4;
24094 else if (unit[0] == 'c' && unit[1] == 'm')
24095 pixels = 2.54;
24096 else
24097 pixels = 0;
24098 if (pixels > 0)
24099 {
24100 double ppi = (width_p ? FRAME_RES_X (it->f)
24101 : FRAME_RES_Y (it->f));
24102
24103 if (ppi > 0)
24104 return OK_PIXELS (ppi / pixels);
24105 return false;
24106 }
24107 }
24108
24109 #ifdef HAVE_WINDOW_SYSTEM
24110 if (EQ (prop, Qheight))
24111 return OK_PIXELS (font
24112 ? normal_char_height (font, -1)
24113 : FRAME_LINE_HEIGHT (it->f));
24114 if (EQ (prop, Qwidth))
24115 return OK_PIXELS (font
24116 ? FONT_WIDTH (font)
24117 : FRAME_COLUMN_WIDTH (it->f));
24118 #else
24119 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24120 return OK_PIXELS (1);
24121 #endif
24122
24123 if (EQ (prop, Qtext))
24124 return OK_PIXELS (width_p
24125 ? window_box_width (it->w, TEXT_AREA)
24126 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24127
24128 if (align_to && *align_to < 0)
24129 {
24130 *res = 0;
24131 if (EQ (prop, Qleft))
24132 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24133 if (EQ (prop, Qright))
24134 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24135 if (EQ (prop, Qcenter))
24136 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24137 + window_box_width (it->w, TEXT_AREA) / 2);
24138 if (EQ (prop, Qleft_fringe))
24139 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24140 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24141 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24142 if (EQ (prop, Qright_fringe))
24143 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24144 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24145 : window_box_right_offset (it->w, TEXT_AREA));
24146 if (EQ (prop, Qleft_margin))
24147 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24148 if (EQ (prop, Qright_margin))
24149 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24150 if (EQ (prop, Qscroll_bar))
24151 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24152 ? 0
24153 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24154 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24155 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24156 : 0)));
24157 }
24158 else
24159 {
24160 if (EQ (prop, Qleft_fringe))
24161 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24162 if (EQ (prop, Qright_fringe))
24163 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24164 if (EQ (prop, Qleft_margin))
24165 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24166 if (EQ (prop, Qright_margin))
24167 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24168 if (EQ (prop, Qscroll_bar))
24169 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24170 }
24171
24172 prop = buffer_local_value (prop, it->w->contents);
24173 if (EQ (prop, Qunbound))
24174 prop = Qnil;
24175 }
24176
24177 if (NUMBERP (prop))
24178 {
24179 int base_unit = (width_p
24180 ? FRAME_COLUMN_WIDTH (it->f)
24181 : FRAME_LINE_HEIGHT (it->f));
24182 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24183 }
24184
24185 if (CONSP (prop))
24186 {
24187 Lisp_Object car = XCAR (prop);
24188 Lisp_Object cdr = XCDR (prop);
24189
24190 if (SYMBOLP (car))
24191 {
24192 #ifdef HAVE_WINDOW_SYSTEM
24193 if (FRAME_WINDOW_P (it->f)
24194 && valid_image_p (prop))
24195 {
24196 ptrdiff_t id = lookup_image (it->f, prop);
24197 struct image *img = IMAGE_FROM_ID (it->f, id);
24198
24199 return OK_PIXELS (width_p ? img->width : img->height);
24200 }
24201 #endif
24202 if (EQ (car, Qplus) || EQ (car, Qminus))
24203 {
24204 bool first = true;
24205 double px;
24206
24207 pixels = 0;
24208 while (CONSP (cdr))
24209 {
24210 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24211 font, width_p, align_to))
24212 return false;
24213 if (first)
24214 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24215 else
24216 pixels += px;
24217 cdr = XCDR (cdr);
24218 }
24219 if (EQ (car, Qminus))
24220 pixels = -pixels;
24221 return OK_PIXELS (pixels);
24222 }
24223
24224 car = buffer_local_value (car, it->w->contents);
24225 if (EQ (car, Qunbound))
24226 car = Qnil;
24227 }
24228
24229 if (NUMBERP (car))
24230 {
24231 double fact;
24232 pixels = XFLOATINT (car);
24233 if (NILP (cdr))
24234 return OK_PIXELS (pixels);
24235 if (calc_pixel_width_or_height (&fact, it, cdr,
24236 font, width_p, align_to))
24237 return OK_PIXELS (pixels * fact);
24238 return false;
24239 }
24240
24241 return false;
24242 }
24243
24244 return false;
24245 }
24246
24247 void
24248 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24249 {
24250 #ifdef HAVE_WINDOW_SYSTEM
24251 normal_char_ascent_descent (font, -1, ascent, descent);
24252 #else
24253 *ascent = 1;
24254 *descent = 0;
24255 #endif
24256 }
24257
24258 \f
24259 /***********************************************************************
24260 Glyph Display
24261 ***********************************************************************/
24262
24263 #ifdef HAVE_WINDOW_SYSTEM
24264
24265 #ifdef GLYPH_DEBUG
24266
24267 void
24268 dump_glyph_string (struct glyph_string *s)
24269 {
24270 fprintf (stderr, "glyph string\n");
24271 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24272 s->x, s->y, s->width, s->height);
24273 fprintf (stderr, " ybase = %d\n", s->ybase);
24274 fprintf (stderr, " hl = %d\n", s->hl);
24275 fprintf (stderr, " left overhang = %d, right = %d\n",
24276 s->left_overhang, s->right_overhang);
24277 fprintf (stderr, " nchars = %d\n", s->nchars);
24278 fprintf (stderr, " extends to end of line = %d\n",
24279 s->extends_to_end_of_line_p);
24280 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24281 fprintf (stderr, " bg width = %d\n", s->background_width);
24282 }
24283
24284 #endif /* GLYPH_DEBUG */
24285
24286 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24287 of XChar2b structures for S; it can't be allocated in
24288 init_glyph_string because it must be allocated via `alloca'. W
24289 is the window on which S is drawn. ROW and AREA are the glyph row
24290 and area within the row from which S is constructed. START is the
24291 index of the first glyph structure covered by S. HL is a
24292 face-override for drawing S. */
24293
24294 #ifdef HAVE_NTGUI
24295 #define OPTIONAL_HDC(hdc) HDC hdc,
24296 #define DECLARE_HDC(hdc) HDC hdc;
24297 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24298 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24299 #endif
24300
24301 #ifndef OPTIONAL_HDC
24302 #define OPTIONAL_HDC(hdc)
24303 #define DECLARE_HDC(hdc)
24304 #define ALLOCATE_HDC(hdc, f)
24305 #define RELEASE_HDC(hdc, f)
24306 #endif
24307
24308 static void
24309 init_glyph_string (struct glyph_string *s,
24310 OPTIONAL_HDC (hdc)
24311 XChar2b *char2b, struct window *w, struct glyph_row *row,
24312 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24313 {
24314 memset (s, 0, sizeof *s);
24315 s->w = w;
24316 s->f = XFRAME (w->frame);
24317 #ifdef HAVE_NTGUI
24318 s->hdc = hdc;
24319 #endif
24320 s->display = FRAME_X_DISPLAY (s->f);
24321 s->window = FRAME_X_WINDOW (s->f);
24322 s->char2b = char2b;
24323 s->hl = hl;
24324 s->row = row;
24325 s->area = area;
24326 s->first_glyph = row->glyphs[area] + start;
24327 s->height = row->height;
24328 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24329 s->ybase = s->y + row->ascent;
24330 }
24331
24332
24333 /* Append the list of glyph strings with head H and tail T to the list
24334 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24335
24336 static void
24337 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24338 struct glyph_string *h, struct glyph_string *t)
24339 {
24340 if (h)
24341 {
24342 if (*head)
24343 (*tail)->next = h;
24344 else
24345 *head = h;
24346 h->prev = *tail;
24347 *tail = t;
24348 }
24349 }
24350
24351
24352 /* Prepend the list of glyph strings with head H and tail T to the
24353 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24354 result. */
24355
24356 static void
24357 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24358 struct glyph_string *h, struct glyph_string *t)
24359 {
24360 if (h)
24361 {
24362 if (*head)
24363 (*head)->prev = t;
24364 else
24365 *tail = t;
24366 t->next = *head;
24367 *head = h;
24368 }
24369 }
24370
24371
24372 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24373 Set *HEAD and *TAIL to the resulting list. */
24374
24375 static void
24376 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24377 struct glyph_string *s)
24378 {
24379 s->next = s->prev = NULL;
24380 append_glyph_string_lists (head, tail, s, s);
24381 }
24382
24383
24384 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24385 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24386 make sure that X resources for the face returned are allocated.
24387 Value is a pointer to a realized face that is ready for display if
24388 DISPLAY_P. */
24389
24390 static struct face *
24391 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24392 XChar2b *char2b, bool display_p)
24393 {
24394 struct face *face = FACE_FROM_ID (f, face_id);
24395 unsigned code = 0;
24396
24397 if (face->font)
24398 {
24399 code = face->font->driver->encode_char (face->font, c);
24400
24401 if (code == FONT_INVALID_CODE)
24402 code = 0;
24403 }
24404 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24405
24406 /* Make sure X resources of the face are allocated. */
24407 #ifdef HAVE_X_WINDOWS
24408 if (display_p)
24409 #endif
24410 {
24411 eassert (face != NULL);
24412 prepare_face_for_display (f, face);
24413 }
24414
24415 return face;
24416 }
24417
24418
24419 /* Get face and two-byte form of character glyph GLYPH on frame F.
24420 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24421 a pointer to a realized face that is ready for display. */
24422
24423 static struct face *
24424 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24425 XChar2b *char2b)
24426 {
24427 struct face *face;
24428 unsigned code = 0;
24429
24430 eassert (glyph->type == CHAR_GLYPH);
24431 face = FACE_FROM_ID (f, glyph->face_id);
24432
24433 /* Make sure X resources of the face are allocated. */
24434 eassert (face != NULL);
24435 prepare_face_for_display (f, face);
24436
24437 if (face->font)
24438 {
24439 if (CHAR_BYTE8_P (glyph->u.ch))
24440 code = CHAR_TO_BYTE8 (glyph->u.ch);
24441 else
24442 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24443
24444 if (code == FONT_INVALID_CODE)
24445 code = 0;
24446 }
24447
24448 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24449 return face;
24450 }
24451
24452
24453 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24454 Return true iff FONT has a glyph for C. */
24455
24456 static bool
24457 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24458 {
24459 unsigned code;
24460
24461 if (CHAR_BYTE8_P (c))
24462 code = CHAR_TO_BYTE8 (c);
24463 else
24464 code = font->driver->encode_char (font, c);
24465
24466 if (code == FONT_INVALID_CODE)
24467 return false;
24468 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24469 return true;
24470 }
24471
24472
24473 /* Fill glyph string S with composition components specified by S->cmp.
24474
24475 BASE_FACE is the base face of the composition.
24476 S->cmp_from is the index of the first component for S.
24477
24478 OVERLAPS non-zero means S should draw the foreground only, and use
24479 its physical height for clipping. See also draw_glyphs.
24480
24481 Value is the index of a component not in S. */
24482
24483 static int
24484 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24485 int overlaps)
24486 {
24487 int i;
24488 /* For all glyphs of this composition, starting at the offset
24489 S->cmp_from, until we reach the end of the definition or encounter a
24490 glyph that requires the different face, add it to S. */
24491 struct face *face;
24492
24493 eassert (s);
24494
24495 s->for_overlaps = overlaps;
24496 s->face = NULL;
24497 s->font = NULL;
24498 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24499 {
24500 int c = COMPOSITION_GLYPH (s->cmp, i);
24501
24502 /* TAB in a composition means display glyphs with padding space
24503 on the left or right. */
24504 if (c != '\t')
24505 {
24506 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24507 -1, Qnil);
24508
24509 face = get_char_face_and_encoding (s->f, c, face_id,
24510 s->char2b + i, true);
24511 if (face)
24512 {
24513 if (! s->face)
24514 {
24515 s->face = face;
24516 s->font = s->face->font;
24517 }
24518 else if (s->face != face)
24519 break;
24520 }
24521 }
24522 ++s->nchars;
24523 }
24524 s->cmp_to = i;
24525
24526 if (s->face == NULL)
24527 {
24528 s->face = base_face->ascii_face;
24529 s->font = s->face->font;
24530 }
24531
24532 /* All glyph strings for the same composition has the same width,
24533 i.e. the width set for the first component of the composition. */
24534 s->width = s->first_glyph->pixel_width;
24535
24536 /* If the specified font could not be loaded, use the frame's
24537 default font, but record the fact that we couldn't load it in
24538 the glyph string so that we can draw rectangles for the
24539 characters of the glyph string. */
24540 if (s->font == NULL)
24541 {
24542 s->font_not_found_p = true;
24543 s->font = FRAME_FONT (s->f);
24544 }
24545
24546 /* Adjust base line for subscript/superscript text. */
24547 s->ybase += s->first_glyph->voffset;
24548
24549 return s->cmp_to;
24550 }
24551
24552 static int
24553 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24554 int start, int end, int overlaps)
24555 {
24556 struct glyph *glyph, *last;
24557 Lisp_Object lgstring;
24558 int i;
24559
24560 s->for_overlaps = overlaps;
24561 glyph = s->row->glyphs[s->area] + start;
24562 last = s->row->glyphs[s->area] + end;
24563 s->cmp_id = glyph->u.cmp.id;
24564 s->cmp_from = glyph->slice.cmp.from;
24565 s->cmp_to = glyph->slice.cmp.to + 1;
24566 s->face = FACE_FROM_ID (s->f, face_id);
24567 lgstring = composition_gstring_from_id (s->cmp_id);
24568 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24569 glyph++;
24570 while (glyph < last
24571 && glyph->u.cmp.automatic
24572 && glyph->u.cmp.id == s->cmp_id
24573 && s->cmp_to == glyph->slice.cmp.from)
24574 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24575
24576 for (i = s->cmp_from; i < s->cmp_to; i++)
24577 {
24578 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24579 unsigned code = LGLYPH_CODE (lglyph);
24580
24581 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24582 }
24583 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24584 return glyph - s->row->glyphs[s->area];
24585 }
24586
24587
24588 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24589 See the comment of fill_glyph_string for arguments.
24590 Value is the index of the first glyph not in S. */
24591
24592
24593 static int
24594 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24595 int start, int end, int overlaps)
24596 {
24597 struct glyph *glyph, *last;
24598 int voffset;
24599
24600 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24601 s->for_overlaps = overlaps;
24602 glyph = s->row->glyphs[s->area] + start;
24603 last = s->row->glyphs[s->area] + end;
24604 voffset = glyph->voffset;
24605 s->face = FACE_FROM_ID (s->f, face_id);
24606 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24607 s->nchars = 1;
24608 s->width = glyph->pixel_width;
24609 glyph++;
24610 while (glyph < last
24611 && glyph->type == GLYPHLESS_GLYPH
24612 && glyph->voffset == voffset
24613 && glyph->face_id == face_id)
24614 {
24615 s->nchars++;
24616 s->width += glyph->pixel_width;
24617 glyph++;
24618 }
24619 s->ybase += voffset;
24620 return glyph - s->row->glyphs[s->area];
24621 }
24622
24623
24624 /* Fill glyph string S from a sequence of character glyphs.
24625
24626 FACE_ID is the face id of the string. START is the index of the
24627 first glyph to consider, END is the index of the last + 1.
24628 OVERLAPS non-zero means S should draw the foreground only, and use
24629 its physical height for clipping. See also draw_glyphs.
24630
24631 Value is the index of the first glyph not in S. */
24632
24633 static int
24634 fill_glyph_string (struct glyph_string *s, int face_id,
24635 int start, int end, int overlaps)
24636 {
24637 struct glyph *glyph, *last;
24638 int voffset;
24639 bool glyph_not_available_p;
24640
24641 eassert (s->f == XFRAME (s->w->frame));
24642 eassert (s->nchars == 0);
24643 eassert (start >= 0 && end > start);
24644
24645 s->for_overlaps = overlaps;
24646 glyph = s->row->glyphs[s->area] + start;
24647 last = s->row->glyphs[s->area] + end;
24648 voffset = glyph->voffset;
24649 s->padding_p = glyph->padding_p;
24650 glyph_not_available_p = glyph->glyph_not_available_p;
24651
24652 while (glyph < last
24653 && glyph->type == CHAR_GLYPH
24654 && glyph->voffset == voffset
24655 /* Same face id implies same font, nowadays. */
24656 && glyph->face_id == face_id
24657 && glyph->glyph_not_available_p == glyph_not_available_p)
24658 {
24659 s->face = get_glyph_face_and_encoding (s->f, glyph,
24660 s->char2b + s->nchars);
24661 ++s->nchars;
24662 eassert (s->nchars <= end - start);
24663 s->width += glyph->pixel_width;
24664 if (glyph++->padding_p != s->padding_p)
24665 break;
24666 }
24667
24668 s->font = s->face->font;
24669
24670 /* If the specified font could not be loaded, use the frame's font,
24671 but record the fact that we couldn't load it in
24672 S->font_not_found_p so that we can draw rectangles for the
24673 characters of the glyph string. */
24674 if (s->font == NULL || glyph_not_available_p)
24675 {
24676 s->font_not_found_p = true;
24677 s->font = FRAME_FONT (s->f);
24678 }
24679
24680 /* Adjust base line for subscript/superscript text. */
24681 s->ybase += voffset;
24682
24683 eassert (s->face && s->face->gc);
24684 return glyph - s->row->glyphs[s->area];
24685 }
24686
24687
24688 /* Fill glyph string S from image glyph S->first_glyph. */
24689
24690 static void
24691 fill_image_glyph_string (struct glyph_string *s)
24692 {
24693 eassert (s->first_glyph->type == IMAGE_GLYPH);
24694 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24695 eassert (s->img);
24696 s->slice = s->first_glyph->slice.img;
24697 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24698 s->font = s->face->font;
24699 s->width = s->first_glyph->pixel_width;
24700
24701 /* Adjust base line for subscript/superscript text. */
24702 s->ybase += s->first_glyph->voffset;
24703 }
24704
24705
24706 /* Fill glyph string S from a sequence of stretch glyphs.
24707
24708 START is the index of the first glyph to consider,
24709 END is the index of the last + 1.
24710
24711 Value is the index of the first glyph not in S. */
24712
24713 static int
24714 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24715 {
24716 struct glyph *glyph, *last;
24717 int voffset, face_id;
24718
24719 eassert (s->first_glyph->type == STRETCH_GLYPH);
24720
24721 glyph = s->row->glyphs[s->area] + start;
24722 last = s->row->glyphs[s->area] + end;
24723 face_id = glyph->face_id;
24724 s->face = FACE_FROM_ID (s->f, face_id);
24725 s->font = s->face->font;
24726 s->width = glyph->pixel_width;
24727 s->nchars = 1;
24728 voffset = glyph->voffset;
24729
24730 for (++glyph;
24731 (glyph < last
24732 && glyph->type == STRETCH_GLYPH
24733 && glyph->voffset == voffset
24734 && glyph->face_id == face_id);
24735 ++glyph)
24736 s->width += glyph->pixel_width;
24737
24738 /* Adjust base line for subscript/superscript text. */
24739 s->ybase += voffset;
24740
24741 /* The case that face->gc == 0 is handled when drawing the glyph
24742 string by calling prepare_face_for_display. */
24743 eassert (s->face);
24744 return glyph - s->row->glyphs[s->area];
24745 }
24746
24747 static struct font_metrics *
24748 get_per_char_metric (struct font *font, XChar2b *char2b)
24749 {
24750 static struct font_metrics metrics;
24751 unsigned code;
24752
24753 if (! font)
24754 return NULL;
24755 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24756 if (code == FONT_INVALID_CODE)
24757 return NULL;
24758 font->driver->text_extents (font, &code, 1, &metrics);
24759 return &metrics;
24760 }
24761
24762 /* A subroutine that computes "normal" values of ASCENT and DESCENT
24763 for FONT. Values are taken from font-global ones, except for fonts
24764 that claim preposterously large values, but whose glyphs actually
24765 have reasonable dimensions. C is the character to use for metrics
24766 if the font-global values are too large; if C is negative, the
24767 function selects a default character. */
24768 static void
24769 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
24770 {
24771 *ascent = FONT_BASE (font);
24772 *descent = FONT_DESCENT (font);
24773
24774 if (FONT_TOO_HIGH (font))
24775 {
24776 XChar2b char2b;
24777
24778 /* Get metrics of C, defaulting to a reasonably sized ASCII
24779 character. */
24780 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
24781 {
24782 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
24783
24784 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
24785 {
24786 /* We add 1 pixel to character dimensions as heuristics
24787 that produces nicer display, e.g. when the face has
24788 the box attribute. */
24789 *ascent = pcm->ascent + 1;
24790 *descent = pcm->descent + 1;
24791 }
24792 }
24793 }
24794 }
24795
24796 /* A subroutine that computes a reasonable "normal character height"
24797 for fonts that claim preposterously large vertical dimensions, but
24798 whose glyphs are actually reasonably sized. C is the character
24799 whose metrics to use for those fonts, or -1 for default
24800 character. */
24801 static int
24802 normal_char_height (struct font *font, int c)
24803 {
24804 int ascent, descent;
24805
24806 normal_char_ascent_descent (font, c, &ascent, &descent);
24807
24808 return ascent + descent;
24809 }
24810
24811 /* EXPORT for RIF:
24812 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24813 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24814 assumed to be zero. */
24815
24816 void
24817 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24818 {
24819 *left = *right = 0;
24820
24821 if (glyph->type == CHAR_GLYPH)
24822 {
24823 XChar2b char2b;
24824 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
24825 if (face->font)
24826 {
24827 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
24828 if (pcm)
24829 {
24830 if (pcm->rbearing > pcm->width)
24831 *right = pcm->rbearing - pcm->width;
24832 if (pcm->lbearing < 0)
24833 *left = -pcm->lbearing;
24834 }
24835 }
24836 }
24837 else if (glyph->type == COMPOSITE_GLYPH)
24838 {
24839 if (! glyph->u.cmp.automatic)
24840 {
24841 struct composition *cmp = composition_table[glyph->u.cmp.id];
24842
24843 if (cmp->rbearing > cmp->pixel_width)
24844 *right = cmp->rbearing - cmp->pixel_width;
24845 if (cmp->lbearing < 0)
24846 *left = - cmp->lbearing;
24847 }
24848 else
24849 {
24850 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24851 struct font_metrics metrics;
24852
24853 composition_gstring_width (gstring, glyph->slice.cmp.from,
24854 glyph->slice.cmp.to + 1, &metrics);
24855 if (metrics.rbearing > metrics.width)
24856 *right = metrics.rbearing - metrics.width;
24857 if (metrics.lbearing < 0)
24858 *left = - metrics.lbearing;
24859 }
24860 }
24861 }
24862
24863
24864 /* Return the index of the first glyph preceding glyph string S that
24865 is overwritten by S because of S's left overhang. Value is -1
24866 if no glyphs are overwritten. */
24867
24868 static int
24869 left_overwritten (struct glyph_string *s)
24870 {
24871 int k;
24872
24873 if (s->left_overhang)
24874 {
24875 int x = 0, i;
24876 struct glyph *glyphs = s->row->glyphs[s->area];
24877 int first = s->first_glyph - glyphs;
24878
24879 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24880 x -= glyphs[i].pixel_width;
24881
24882 k = i + 1;
24883 }
24884 else
24885 k = -1;
24886
24887 return k;
24888 }
24889
24890
24891 /* Return the index of the first glyph preceding glyph string S that
24892 is overwriting S because of its right overhang. Value is -1 if no
24893 glyph in front of S overwrites S. */
24894
24895 static int
24896 left_overwriting (struct glyph_string *s)
24897 {
24898 int i, k, x;
24899 struct glyph *glyphs = s->row->glyphs[s->area];
24900 int first = s->first_glyph - glyphs;
24901
24902 k = -1;
24903 x = 0;
24904 for (i = first - 1; i >= 0; --i)
24905 {
24906 int left, right;
24907 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24908 if (x + right > 0)
24909 k = i;
24910 x -= glyphs[i].pixel_width;
24911 }
24912
24913 return k;
24914 }
24915
24916
24917 /* Return the index of the last glyph following glyph string S that is
24918 overwritten by S because of S's right overhang. Value is -1 if
24919 no such glyph is found. */
24920
24921 static int
24922 right_overwritten (struct glyph_string *s)
24923 {
24924 int k = -1;
24925
24926 if (s->right_overhang)
24927 {
24928 int x = 0, i;
24929 struct glyph *glyphs = s->row->glyphs[s->area];
24930 int first = (s->first_glyph - glyphs
24931 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24932 int end = s->row->used[s->area];
24933
24934 for (i = first; i < end && s->right_overhang > x; ++i)
24935 x += glyphs[i].pixel_width;
24936
24937 k = i;
24938 }
24939
24940 return k;
24941 }
24942
24943
24944 /* Return the index of the last glyph following glyph string S that
24945 overwrites S because of its left overhang. Value is negative
24946 if no such glyph is found. */
24947
24948 static int
24949 right_overwriting (struct glyph_string *s)
24950 {
24951 int i, k, x;
24952 int end = s->row->used[s->area];
24953 struct glyph *glyphs = s->row->glyphs[s->area];
24954 int first = (s->first_glyph - glyphs
24955 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24956
24957 k = -1;
24958 x = 0;
24959 for (i = first; i < end; ++i)
24960 {
24961 int left, right;
24962 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24963 if (x - left < 0)
24964 k = i;
24965 x += glyphs[i].pixel_width;
24966 }
24967
24968 return k;
24969 }
24970
24971
24972 /* Set background width of glyph string S. START is the index of the
24973 first glyph following S. LAST_X is the right-most x-position + 1
24974 in the drawing area. */
24975
24976 static void
24977 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24978 {
24979 /* If the face of this glyph string has to be drawn to the end of
24980 the drawing area, set S->extends_to_end_of_line_p. */
24981
24982 if (start == s->row->used[s->area]
24983 && ((s->row->fill_line_p
24984 && (s->hl == DRAW_NORMAL_TEXT
24985 || s->hl == DRAW_IMAGE_RAISED
24986 || s->hl == DRAW_IMAGE_SUNKEN))
24987 || s->hl == DRAW_MOUSE_FACE))
24988 s->extends_to_end_of_line_p = true;
24989
24990 /* If S extends its face to the end of the line, set its
24991 background_width to the distance to the right edge of the drawing
24992 area. */
24993 if (s->extends_to_end_of_line_p)
24994 s->background_width = last_x - s->x + 1;
24995 else
24996 s->background_width = s->width;
24997 }
24998
24999
25000 /* Compute overhangs and x-positions for glyph string S and its
25001 predecessors, or successors. X is the starting x-position for S.
25002 BACKWARD_P means process predecessors. */
25003
25004 static void
25005 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
25006 {
25007 if (backward_p)
25008 {
25009 while (s)
25010 {
25011 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25012 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25013 x -= s->width;
25014 s->x = x;
25015 s = s->prev;
25016 }
25017 }
25018 else
25019 {
25020 while (s)
25021 {
25022 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25023 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25024 s->x = x;
25025 x += s->width;
25026 s = s->next;
25027 }
25028 }
25029 }
25030
25031
25032
25033 /* The following macros are only called from draw_glyphs below.
25034 They reference the following parameters of that function directly:
25035 `w', `row', `area', and `overlap_p'
25036 as well as the following local variables:
25037 `s', `f', and `hdc' (in W32) */
25038
25039 #ifdef HAVE_NTGUI
25040 /* On W32, silently add local `hdc' variable to argument list of
25041 init_glyph_string. */
25042 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25043 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25044 #else
25045 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25046 init_glyph_string (s, char2b, w, row, area, start, hl)
25047 #endif
25048
25049 /* Add a glyph string for a stretch glyph to the list of strings
25050 between HEAD and TAIL. START is the index of the stretch glyph in
25051 row area AREA of glyph row ROW. END is the index of the last glyph
25052 in that glyph row area. X is the current output position assigned
25053 to the new glyph string constructed. HL overrides that face of the
25054 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25055 is the right-most x-position of the drawing area. */
25056
25057 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25058 and below -- keep them on one line. */
25059 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25060 do \
25061 { \
25062 s = alloca (sizeof *s); \
25063 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25064 START = fill_stretch_glyph_string (s, START, END); \
25065 append_glyph_string (&HEAD, &TAIL, s); \
25066 s->x = (X); \
25067 } \
25068 while (false)
25069
25070
25071 /* Add a glyph string for an image glyph to the list of strings
25072 between HEAD and TAIL. START is the index of the image glyph in
25073 row area AREA of glyph row ROW. END is the index of the last glyph
25074 in that glyph row area. X is the current output position assigned
25075 to the new glyph string constructed. HL overrides that face of the
25076 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25077 is the right-most x-position of the drawing area. */
25078
25079 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25080 do \
25081 { \
25082 s = alloca (sizeof *s); \
25083 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25084 fill_image_glyph_string (s); \
25085 append_glyph_string (&HEAD, &TAIL, s); \
25086 ++START; \
25087 s->x = (X); \
25088 } \
25089 while (false)
25090
25091
25092 /* Add a glyph string for a sequence of character glyphs to the list
25093 of strings between HEAD and TAIL. START is the index of the first
25094 glyph in row area AREA of glyph row ROW that is part of the new
25095 glyph string. END is the index of the last glyph in that glyph row
25096 area. X is the current output position assigned to the new glyph
25097 string constructed. HL overrides that face of the glyph; e.g. it
25098 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25099 right-most x-position of the drawing area. */
25100
25101 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25102 do \
25103 { \
25104 int face_id; \
25105 XChar2b *char2b; \
25106 \
25107 face_id = (row)->glyphs[area][START].face_id; \
25108 \
25109 s = alloca (sizeof *s); \
25110 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25111 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25112 append_glyph_string (&HEAD, &TAIL, s); \
25113 s->x = (X); \
25114 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25115 } \
25116 while (false)
25117
25118
25119 /* Add a glyph string for a composite sequence to the list of strings
25120 between HEAD and TAIL. START is the index of the first glyph in
25121 row area AREA of glyph row ROW that is part of the new glyph
25122 string. END is the index of the last glyph in that glyph row area.
25123 X is the current output position assigned to the new glyph string
25124 constructed. HL overrides that face of the glyph; e.g. it is
25125 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25126 x-position of the drawing area. */
25127
25128 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25129 do { \
25130 int face_id = (row)->glyphs[area][START].face_id; \
25131 struct face *base_face = FACE_FROM_ID (f, face_id); \
25132 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25133 struct composition *cmp = composition_table[cmp_id]; \
25134 XChar2b *char2b; \
25135 struct glyph_string *first_s = NULL; \
25136 int n; \
25137 \
25138 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25139 \
25140 /* Make glyph_strings for each glyph sequence that is drawable by \
25141 the same face, and append them to HEAD/TAIL. */ \
25142 for (n = 0; n < cmp->glyph_len;) \
25143 { \
25144 s = alloca (sizeof *s); \
25145 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25146 append_glyph_string (&(HEAD), &(TAIL), s); \
25147 s->cmp = cmp; \
25148 s->cmp_from = n; \
25149 s->x = (X); \
25150 if (n == 0) \
25151 first_s = s; \
25152 n = fill_composite_glyph_string (s, base_face, overlaps); \
25153 } \
25154 \
25155 ++START; \
25156 s = first_s; \
25157 } while (false)
25158
25159
25160 /* Add a glyph string for a glyph-string sequence to the list of strings
25161 between HEAD and TAIL. */
25162
25163 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25164 do { \
25165 int face_id; \
25166 XChar2b *char2b; \
25167 Lisp_Object gstring; \
25168 \
25169 face_id = (row)->glyphs[area][START].face_id; \
25170 gstring = (composition_gstring_from_id \
25171 ((row)->glyphs[area][START].u.cmp.id)); \
25172 s = alloca (sizeof *s); \
25173 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25174 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25175 append_glyph_string (&(HEAD), &(TAIL), s); \
25176 s->x = (X); \
25177 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25178 } while (false)
25179
25180
25181 /* Add a glyph string for a sequence of glyphless character's glyphs
25182 to the list of strings between HEAD and TAIL. The meanings of
25183 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25184
25185 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25186 do \
25187 { \
25188 int face_id; \
25189 \
25190 face_id = (row)->glyphs[area][START].face_id; \
25191 \
25192 s = alloca (sizeof *s); \
25193 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25194 append_glyph_string (&HEAD, &TAIL, s); \
25195 s->x = (X); \
25196 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25197 overlaps); \
25198 } \
25199 while (false)
25200
25201
25202 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25203 of AREA of glyph row ROW on window W between indices START and END.
25204 HL overrides the face for drawing glyph strings, e.g. it is
25205 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25206 x-positions of the drawing area.
25207
25208 This is an ugly monster macro construct because we must use alloca
25209 to allocate glyph strings (because draw_glyphs can be called
25210 asynchronously). */
25211
25212 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25213 do \
25214 { \
25215 HEAD = TAIL = NULL; \
25216 while (START < END) \
25217 { \
25218 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25219 switch (first_glyph->type) \
25220 { \
25221 case CHAR_GLYPH: \
25222 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25223 HL, X, LAST_X); \
25224 break; \
25225 \
25226 case COMPOSITE_GLYPH: \
25227 if (first_glyph->u.cmp.automatic) \
25228 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25229 HL, X, LAST_X); \
25230 else \
25231 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25232 HL, X, LAST_X); \
25233 break; \
25234 \
25235 case STRETCH_GLYPH: \
25236 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25237 HL, X, LAST_X); \
25238 break; \
25239 \
25240 case IMAGE_GLYPH: \
25241 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25242 HL, X, LAST_X); \
25243 break; \
25244 \
25245 case GLYPHLESS_GLYPH: \
25246 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25247 HL, X, LAST_X); \
25248 break; \
25249 \
25250 default: \
25251 emacs_abort (); \
25252 } \
25253 \
25254 if (s) \
25255 { \
25256 set_glyph_string_background_width (s, START, LAST_X); \
25257 (X) += s->width; \
25258 } \
25259 } \
25260 } while (false)
25261
25262
25263 /* Draw glyphs between START and END in AREA of ROW on window W,
25264 starting at x-position X. X is relative to AREA in W. HL is a
25265 face-override with the following meaning:
25266
25267 DRAW_NORMAL_TEXT draw normally
25268 DRAW_CURSOR draw in cursor face
25269 DRAW_MOUSE_FACE draw in mouse face.
25270 DRAW_INVERSE_VIDEO draw in mode line face
25271 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25272 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25273
25274 If OVERLAPS is non-zero, draw only the foreground of characters and
25275 clip to the physical height of ROW. Non-zero value also defines
25276 the overlapping part to be drawn:
25277
25278 OVERLAPS_PRED overlap with preceding rows
25279 OVERLAPS_SUCC overlap with succeeding rows
25280 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25281 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25282
25283 Value is the x-position reached, relative to AREA of W. */
25284
25285 static int
25286 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25287 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25288 enum draw_glyphs_face hl, int overlaps)
25289 {
25290 struct glyph_string *head, *tail;
25291 struct glyph_string *s;
25292 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25293 int i, j, x_reached, last_x, area_left = 0;
25294 struct frame *f = XFRAME (WINDOW_FRAME (w));
25295 DECLARE_HDC (hdc);
25296
25297 ALLOCATE_HDC (hdc, f);
25298
25299 /* Let's rather be paranoid than getting a SEGV. */
25300 end = min (end, row->used[area]);
25301 start = clip_to_bounds (0, start, end);
25302
25303 /* Translate X to frame coordinates. Set last_x to the right
25304 end of the drawing area. */
25305 if (row->full_width_p)
25306 {
25307 /* X is relative to the left edge of W, without scroll bars
25308 or fringes. */
25309 area_left = WINDOW_LEFT_EDGE_X (w);
25310 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25311 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25312 }
25313 else
25314 {
25315 area_left = window_box_left (w, area);
25316 last_x = area_left + window_box_width (w, area);
25317 }
25318 x += area_left;
25319
25320 /* Build a doubly-linked list of glyph_string structures between
25321 head and tail from what we have to draw. Note that the macro
25322 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25323 the reason we use a separate variable `i'. */
25324 i = start;
25325 USE_SAFE_ALLOCA;
25326 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25327 if (tail)
25328 x_reached = tail->x + tail->background_width;
25329 else
25330 x_reached = x;
25331
25332 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25333 the row, redraw some glyphs in front or following the glyph
25334 strings built above. */
25335 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25336 {
25337 struct glyph_string *h, *t;
25338 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25339 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25340 bool check_mouse_face = false;
25341 int dummy_x = 0;
25342
25343 /* If mouse highlighting is on, we may need to draw adjacent
25344 glyphs using mouse-face highlighting. */
25345 if (area == TEXT_AREA && row->mouse_face_p
25346 && hlinfo->mouse_face_beg_row >= 0
25347 && hlinfo->mouse_face_end_row >= 0)
25348 {
25349 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25350
25351 if (row_vpos >= hlinfo->mouse_face_beg_row
25352 && row_vpos <= hlinfo->mouse_face_end_row)
25353 {
25354 check_mouse_face = true;
25355 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25356 ? hlinfo->mouse_face_beg_col : 0;
25357 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25358 ? hlinfo->mouse_face_end_col
25359 : row->used[TEXT_AREA];
25360 }
25361 }
25362
25363 /* Compute overhangs for all glyph strings. */
25364 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25365 for (s = head; s; s = s->next)
25366 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25367
25368 /* Prepend glyph strings for glyphs in front of the first glyph
25369 string that are overwritten because of the first glyph
25370 string's left overhang. The background of all strings
25371 prepended must be drawn because the first glyph string
25372 draws over it. */
25373 i = left_overwritten (head);
25374 if (i >= 0)
25375 {
25376 enum draw_glyphs_face overlap_hl;
25377
25378 /* If this row contains mouse highlighting, attempt to draw
25379 the overlapped glyphs with the correct highlight. This
25380 code fails if the overlap encompasses more than one glyph
25381 and mouse-highlight spans only some of these glyphs.
25382 However, making it work perfectly involves a lot more
25383 code, and I don't know if the pathological case occurs in
25384 practice, so we'll stick to this for now. --- cyd */
25385 if (check_mouse_face
25386 && mouse_beg_col < start && mouse_end_col > i)
25387 overlap_hl = DRAW_MOUSE_FACE;
25388 else
25389 overlap_hl = DRAW_NORMAL_TEXT;
25390
25391 if (hl != overlap_hl)
25392 clip_head = head;
25393 j = i;
25394 BUILD_GLYPH_STRINGS (j, start, h, t,
25395 overlap_hl, dummy_x, last_x);
25396 start = i;
25397 compute_overhangs_and_x (t, head->x, true);
25398 prepend_glyph_string_lists (&head, &tail, h, t);
25399 if (clip_head == NULL)
25400 clip_head = head;
25401 }
25402
25403 /* Prepend glyph strings for glyphs in front of the first glyph
25404 string that overwrite that glyph string because of their
25405 right overhang. For these strings, only the foreground must
25406 be drawn, because it draws over the glyph string at `head'.
25407 The background must not be drawn because this would overwrite
25408 right overhangs of preceding glyphs for which no glyph
25409 strings exist. */
25410 i = left_overwriting (head);
25411 if (i >= 0)
25412 {
25413 enum draw_glyphs_face overlap_hl;
25414
25415 if (check_mouse_face
25416 && mouse_beg_col < start && mouse_end_col > i)
25417 overlap_hl = DRAW_MOUSE_FACE;
25418 else
25419 overlap_hl = DRAW_NORMAL_TEXT;
25420
25421 if (hl == overlap_hl || clip_head == NULL)
25422 clip_head = head;
25423 BUILD_GLYPH_STRINGS (i, start, h, t,
25424 overlap_hl, dummy_x, last_x);
25425 for (s = h; s; s = s->next)
25426 s->background_filled_p = true;
25427 compute_overhangs_and_x (t, head->x, true);
25428 prepend_glyph_string_lists (&head, &tail, h, t);
25429 }
25430
25431 /* Append glyphs strings for glyphs following the last glyph
25432 string tail that are overwritten by tail. The background of
25433 these strings has to be drawn because tail's foreground draws
25434 over it. */
25435 i = right_overwritten (tail);
25436 if (i >= 0)
25437 {
25438 enum draw_glyphs_face overlap_hl;
25439
25440 if (check_mouse_face
25441 && mouse_beg_col < i && mouse_end_col > end)
25442 overlap_hl = DRAW_MOUSE_FACE;
25443 else
25444 overlap_hl = DRAW_NORMAL_TEXT;
25445
25446 if (hl != overlap_hl)
25447 clip_tail = tail;
25448 BUILD_GLYPH_STRINGS (end, i, h, t,
25449 overlap_hl, x, last_x);
25450 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25451 we don't have `end = i;' here. */
25452 compute_overhangs_and_x (h, tail->x + tail->width, false);
25453 append_glyph_string_lists (&head, &tail, h, t);
25454 if (clip_tail == NULL)
25455 clip_tail = tail;
25456 }
25457
25458 /* Append glyph strings for glyphs following the last glyph
25459 string tail that overwrite tail. The foreground of such
25460 glyphs has to be drawn because it writes into the background
25461 of tail. The background must not be drawn because it could
25462 paint over the foreground of following glyphs. */
25463 i = right_overwriting (tail);
25464 if (i >= 0)
25465 {
25466 enum draw_glyphs_face overlap_hl;
25467 if (check_mouse_face
25468 && mouse_beg_col < i && mouse_end_col > end)
25469 overlap_hl = DRAW_MOUSE_FACE;
25470 else
25471 overlap_hl = DRAW_NORMAL_TEXT;
25472
25473 if (hl == overlap_hl || clip_tail == NULL)
25474 clip_tail = tail;
25475 i++; /* We must include the Ith glyph. */
25476 BUILD_GLYPH_STRINGS (end, i, h, t,
25477 overlap_hl, x, last_x);
25478 for (s = h; s; s = s->next)
25479 s->background_filled_p = true;
25480 compute_overhangs_and_x (h, tail->x + tail->width, false);
25481 append_glyph_string_lists (&head, &tail, h, t);
25482 }
25483 if (clip_head || clip_tail)
25484 for (s = head; s; s = s->next)
25485 {
25486 s->clip_head = clip_head;
25487 s->clip_tail = clip_tail;
25488 }
25489 }
25490
25491 /* Draw all strings. */
25492 for (s = head; s; s = s->next)
25493 FRAME_RIF (f)->draw_glyph_string (s);
25494
25495 #ifndef HAVE_NS
25496 /* When focus a sole frame and move horizontally, this clears on_p
25497 causing a failure to erase prev cursor position. */
25498 if (area == TEXT_AREA
25499 && !row->full_width_p
25500 /* When drawing overlapping rows, only the glyph strings'
25501 foreground is drawn, which doesn't erase a cursor
25502 completely. */
25503 && !overlaps)
25504 {
25505 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25506 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25507 : (tail ? tail->x + tail->background_width : x));
25508 x0 -= area_left;
25509 x1 -= area_left;
25510
25511 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25512 row->y, MATRIX_ROW_BOTTOM_Y (row));
25513 }
25514 #endif
25515
25516 /* Value is the x-position up to which drawn, relative to AREA of W.
25517 This doesn't include parts drawn because of overhangs. */
25518 if (row->full_width_p)
25519 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25520 else
25521 x_reached -= area_left;
25522
25523 RELEASE_HDC (hdc, f);
25524
25525 SAFE_FREE ();
25526 return x_reached;
25527 }
25528
25529 /* Expand row matrix if too narrow. Don't expand if area
25530 is not present. */
25531
25532 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25533 { \
25534 if (!it->f->fonts_changed \
25535 && (it->glyph_row->glyphs[area] \
25536 < it->glyph_row->glyphs[area + 1])) \
25537 { \
25538 it->w->ncols_scale_factor++; \
25539 it->f->fonts_changed = true; \
25540 } \
25541 }
25542
25543 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25544 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25545
25546 static void
25547 append_glyph (struct it *it)
25548 {
25549 struct glyph *glyph;
25550 enum glyph_row_area area = it->area;
25551
25552 eassert (it->glyph_row);
25553 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25554
25555 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25556 if (glyph < it->glyph_row->glyphs[area + 1])
25557 {
25558 /* If the glyph row is reversed, we need to prepend the glyph
25559 rather than append it. */
25560 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25561 {
25562 struct glyph *g;
25563
25564 /* Make room for the additional glyph. */
25565 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25566 g[1] = *g;
25567 glyph = it->glyph_row->glyphs[area];
25568 }
25569 glyph->charpos = CHARPOS (it->position);
25570 glyph->object = it->object;
25571 if (it->pixel_width > 0)
25572 {
25573 glyph->pixel_width = it->pixel_width;
25574 glyph->padding_p = false;
25575 }
25576 else
25577 {
25578 /* Assure at least 1-pixel width. Otherwise, cursor can't
25579 be displayed correctly. */
25580 glyph->pixel_width = 1;
25581 glyph->padding_p = true;
25582 }
25583 glyph->ascent = it->ascent;
25584 glyph->descent = it->descent;
25585 glyph->voffset = it->voffset;
25586 glyph->type = CHAR_GLYPH;
25587 glyph->avoid_cursor_p = it->avoid_cursor_p;
25588 glyph->multibyte_p = it->multibyte_p;
25589 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25590 {
25591 /* In R2L rows, the left and the right box edges need to be
25592 drawn in reverse direction. */
25593 glyph->right_box_line_p = it->start_of_box_run_p;
25594 glyph->left_box_line_p = it->end_of_box_run_p;
25595 }
25596 else
25597 {
25598 glyph->left_box_line_p = it->start_of_box_run_p;
25599 glyph->right_box_line_p = it->end_of_box_run_p;
25600 }
25601 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25602 || it->phys_descent > it->descent);
25603 glyph->glyph_not_available_p = it->glyph_not_available_p;
25604 glyph->face_id = it->face_id;
25605 glyph->u.ch = it->char_to_display;
25606 glyph->slice.img = null_glyph_slice;
25607 glyph->font_type = FONT_TYPE_UNKNOWN;
25608 if (it->bidi_p)
25609 {
25610 glyph->resolved_level = it->bidi_it.resolved_level;
25611 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25612 glyph->bidi_type = it->bidi_it.type;
25613 }
25614 else
25615 {
25616 glyph->resolved_level = 0;
25617 glyph->bidi_type = UNKNOWN_BT;
25618 }
25619 ++it->glyph_row->used[area];
25620 }
25621 else
25622 IT_EXPAND_MATRIX_WIDTH (it, area);
25623 }
25624
25625 /* Store one glyph for the composition IT->cmp_it.id in
25626 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25627 non-null. */
25628
25629 static void
25630 append_composite_glyph (struct it *it)
25631 {
25632 struct glyph *glyph;
25633 enum glyph_row_area area = it->area;
25634
25635 eassert (it->glyph_row);
25636
25637 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25638 if (glyph < it->glyph_row->glyphs[area + 1])
25639 {
25640 /* If the glyph row is reversed, we need to prepend the glyph
25641 rather than append it. */
25642 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25643 {
25644 struct glyph *g;
25645
25646 /* Make room for the new glyph. */
25647 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25648 g[1] = *g;
25649 glyph = it->glyph_row->glyphs[it->area];
25650 }
25651 glyph->charpos = it->cmp_it.charpos;
25652 glyph->object = it->object;
25653 glyph->pixel_width = it->pixel_width;
25654 glyph->ascent = it->ascent;
25655 glyph->descent = it->descent;
25656 glyph->voffset = it->voffset;
25657 glyph->type = COMPOSITE_GLYPH;
25658 if (it->cmp_it.ch < 0)
25659 {
25660 glyph->u.cmp.automatic = false;
25661 glyph->u.cmp.id = it->cmp_it.id;
25662 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25663 }
25664 else
25665 {
25666 glyph->u.cmp.automatic = true;
25667 glyph->u.cmp.id = it->cmp_it.id;
25668 glyph->slice.cmp.from = it->cmp_it.from;
25669 glyph->slice.cmp.to = it->cmp_it.to - 1;
25670 }
25671 glyph->avoid_cursor_p = it->avoid_cursor_p;
25672 glyph->multibyte_p = it->multibyte_p;
25673 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25674 {
25675 /* In R2L rows, the left and the right box edges need to be
25676 drawn in reverse direction. */
25677 glyph->right_box_line_p = it->start_of_box_run_p;
25678 glyph->left_box_line_p = it->end_of_box_run_p;
25679 }
25680 else
25681 {
25682 glyph->left_box_line_p = it->start_of_box_run_p;
25683 glyph->right_box_line_p = it->end_of_box_run_p;
25684 }
25685 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25686 || it->phys_descent > it->descent);
25687 glyph->padding_p = false;
25688 glyph->glyph_not_available_p = false;
25689 glyph->face_id = it->face_id;
25690 glyph->font_type = FONT_TYPE_UNKNOWN;
25691 if (it->bidi_p)
25692 {
25693 glyph->resolved_level = it->bidi_it.resolved_level;
25694 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25695 glyph->bidi_type = it->bidi_it.type;
25696 }
25697 ++it->glyph_row->used[area];
25698 }
25699 else
25700 IT_EXPAND_MATRIX_WIDTH (it, area);
25701 }
25702
25703
25704 /* Change IT->ascent and IT->height according to the setting of
25705 IT->voffset. */
25706
25707 static void
25708 take_vertical_position_into_account (struct it *it)
25709 {
25710 if (it->voffset)
25711 {
25712 if (it->voffset < 0)
25713 /* Increase the ascent so that we can display the text higher
25714 in the line. */
25715 it->ascent -= it->voffset;
25716 else
25717 /* Increase the descent so that we can display the text lower
25718 in the line. */
25719 it->descent += it->voffset;
25720 }
25721 }
25722
25723
25724 /* Produce glyphs/get display metrics for the image IT is loaded with.
25725 See the description of struct display_iterator in dispextern.h for
25726 an overview of struct display_iterator. */
25727
25728 static void
25729 produce_image_glyph (struct it *it)
25730 {
25731 struct image *img;
25732 struct face *face;
25733 int glyph_ascent, crop;
25734 struct glyph_slice slice;
25735
25736 eassert (it->what == IT_IMAGE);
25737
25738 face = FACE_FROM_ID (it->f, it->face_id);
25739 eassert (face);
25740 /* Make sure X resources of the face is loaded. */
25741 prepare_face_for_display (it->f, face);
25742
25743 if (it->image_id < 0)
25744 {
25745 /* Fringe bitmap. */
25746 it->ascent = it->phys_ascent = 0;
25747 it->descent = it->phys_descent = 0;
25748 it->pixel_width = 0;
25749 it->nglyphs = 0;
25750 return;
25751 }
25752
25753 img = IMAGE_FROM_ID (it->f, it->image_id);
25754 eassert (img);
25755 /* Make sure X resources of the image is loaded. */
25756 prepare_image_for_display (it->f, img);
25757
25758 slice.x = slice.y = 0;
25759 slice.width = img->width;
25760 slice.height = img->height;
25761
25762 if (INTEGERP (it->slice.x))
25763 slice.x = XINT (it->slice.x);
25764 else if (FLOATP (it->slice.x))
25765 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25766
25767 if (INTEGERP (it->slice.y))
25768 slice.y = XINT (it->slice.y);
25769 else if (FLOATP (it->slice.y))
25770 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25771
25772 if (INTEGERP (it->slice.width))
25773 slice.width = XINT (it->slice.width);
25774 else if (FLOATP (it->slice.width))
25775 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25776
25777 if (INTEGERP (it->slice.height))
25778 slice.height = XINT (it->slice.height);
25779 else if (FLOATP (it->slice.height))
25780 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25781
25782 if (slice.x >= img->width)
25783 slice.x = img->width;
25784 if (slice.y >= img->height)
25785 slice.y = img->height;
25786 if (slice.x + slice.width >= img->width)
25787 slice.width = img->width - slice.x;
25788 if (slice.y + slice.height > img->height)
25789 slice.height = img->height - slice.y;
25790
25791 if (slice.width == 0 || slice.height == 0)
25792 return;
25793
25794 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25795
25796 it->descent = slice.height - glyph_ascent;
25797 if (slice.y == 0)
25798 it->descent += img->vmargin;
25799 if (slice.y + slice.height == img->height)
25800 it->descent += img->vmargin;
25801 it->phys_descent = it->descent;
25802
25803 it->pixel_width = slice.width;
25804 if (slice.x == 0)
25805 it->pixel_width += img->hmargin;
25806 if (slice.x + slice.width == img->width)
25807 it->pixel_width += img->hmargin;
25808
25809 /* It's quite possible for images to have an ascent greater than
25810 their height, so don't get confused in that case. */
25811 if (it->descent < 0)
25812 it->descent = 0;
25813
25814 it->nglyphs = 1;
25815
25816 if (face->box != FACE_NO_BOX)
25817 {
25818 if (face->box_line_width > 0)
25819 {
25820 if (slice.y == 0)
25821 it->ascent += face->box_line_width;
25822 if (slice.y + slice.height == img->height)
25823 it->descent += face->box_line_width;
25824 }
25825
25826 if (it->start_of_box_run_p && slice.x == 0)
25827 it->pixel_width += eabs (face->box_line_width);
25828 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25829 it->pixel_width += eabs (face->box_line_width);
25830 }
25831
25832 take_vertical_position_into_account (it);
25833
25834 /* Automatically crop wide image glyphs at right edge so we can
25835 draw the cursor on same display row. */
25836 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25837 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25838 {
25839 it->pixel_width -= crop;
25840 slice.width -= crop;
25841 }
25842
25843 if (it->glyph_row)
25844 {
25845 struct glyph *glyph;
25846 enum glyph_row_area area = it->area;
25847
25848 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25849 if (it->glyph_row->reversed_p)
25850 {
25851 struct glyph *g;
25852
25853 /* Make room for the new glyph. */
25854 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25855 g[1] = *g;
25856 glyph = it->glyph_row->glyphs[it->area];
25857 }
25858 if (glyph < it->glyph_row->glyphs[area + 1])
25859 {
25860 glyph->charpos = CHARPOS (it->position);
25861 glyph->object = it->object;
25862 glyph->pixel_width = it->pixel_width;
25863 glyph->ascent = glyph_ascent;
25864 glyph->descent = it->descent;
25865 glyph->voffset = it->voffset;
25866 glyph->type = IMAGE_GLYPH;
25867 glyph->avoid_cursor_p = it->avoid_cursor_p;
25868 glyph->multibyte_p = it->multibyte_p;
25869 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25870 {
25871 /* In R2L rows, the left and the right box edges need to be
25872 drawn in reverse direction. */
25873 glyph->right_box_line_p = it->start_of_box_run_p;
25874 glyph->left_box_line_p = it->end_of_box_run_p;
25875 }
25876 else
25877 {
25878 glyph->left_box_line_p = it->start_of_box_run_p;
25879 glyph->right_box_line_p = it->end_of_box_run_p;
25880 }
25881 glyph->overlaps_vertically_p = false;
25882 glyph->padding_p = false;
25883 glyph->glyph_not_available_p = false;
25884 glyph->face_id = it->face_id;
25885 glyph->u.img_id = img->id;
25886 glyph->slice.img = slice;
25887 glyph->font_type = FONT_TYPE_UNKNOWN;
25888 if (it->bidi_p)
25889 {
25890 glyph->resolved_level = it->bidi_it.resolved_level;
25891 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25892 glyph->bidi_type = it->bidi_it.type;
25893 }
25894 ++it->glyph_row->used[area];
25895 }
25896 else
25897 IT_EXPAND_MATRIX_WIDTH (it, area);
25898 }
25899 }
25900
25901
25902 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25903 of the glyph, WIDTH and HEIGHT are the width and height of the
25904 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25905
25906 static void
25907 append_stretch_glyph (struct it *it, Lisp_Object object,
25908 int width, int height, int ascent)
25909 {
25910 struct glyph *glyph;
25911 enum glyph_row_area area = it->area;
25912
25913 eassert (ascent >= 0 && ascent <= height);
25914
25915 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25916 if (glyph < it->glyph_row->glyphs[area + 1])
25917 {
25918 /* If the glyph row is reversed, we need to prepend the glyph
25919 rather than append it. */
25920 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25921 {
25922 struct glyph *g;
25923
25924 /* Make room for the additional glyph. */
25925 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25926 g[1] = *g;
25927 glyph = it->glyph_row->glyphs[area];
25928
25929 /* Decrease the width of the first glyph of the row that
25930 begins before first_visible_x (e.g., due to hscroll).
25931 This is so the overall width of the row becomes smaller
25932 by the scroll amount, and the stretch glyph appended by
25933 extend_face_to_end_of_line will be wider, to shift the
25934 row glyphs to the right. (In L2R rows, the corresponding
25935 left-shift effect is accomplished by setting row->x to a
25936 negative value, which won't work with R2L rows.)
25937
25938 This must leave us with a positive value of WIDTH, since
25939 otherwise the call to move_it_in_display_line_to at the
25940 beginning of display_line would have got past the entire
25941 first glyph, and then it->current_x would have been
25942 greater or equal to it->first_visible_x. */
25943 if (it->current_x < it->first_visible_x)
25944 width -= it->first_visible_x - it->current_x;
25945 eassert (width > 0);
25946 }
25947 glyph->charpos = CHARPOS (it->position);
25948 glyph->object = object;
25949 glyph->pixel_width = width;
25950 glyph->ascent = ascent;
25951 glyph->descent = height - ascent;
25952 glyph->voffset = it->voffset;
25953 glyph->type = STRETCH_GLYPH;
25954 glyph->avoid_cursor_p = it->avoid_cursor_p;
25955 glyph->multibyte_p = it->multibyte_p;
25956 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25957 {
25958 /* In R2L rows, the left and the right box edges need to be
25959 drawn in reverse direction. */
25960 glyph->right_box_line_p = it->start_of_box_run_p;
25961 glyph->left_box_line_p = it->end_of_box_run_p;
25962 }
25963 else
25964 {
25965 glyph->left_box_line_p = it->start_of_box_run_p;
25966 glyph->right_box_line_p = it->end_of_box_run_p;
25967 }
25968 glyph->overlaps_vertically_p = false;
25969 glyph->padding_p = false;
25970 glyph->glyph_not_available_p = false;
25971 glyph->face_id = it->face_id;
25972 glyph->u.stretch.ascent = ascent;
25973 glyph->u.stretch.height = height;
25974 glyph->slice.img = null_glyph_slice;
25975 glyph->font_type = FONT_TYPE_UNKNOWN;
25976 if (it->bidi_p)
25977 {
25978 glyph->resolved_level = it->bidi_it.resolved_level;
25979 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25980 glyph->bidi_type = it->bidi_it.type;
25981 }
25982 else
25983 {
25984 glyph->resolved_level = 0;
25985 glyph->bidi_type = UNKNOWN_BT;
25986 }
25987 ++it->glyph_row->used[area];
25988 }
25989 else
25990 IT_EXPAND_MATRIX_WIDTH (it, area);
25991 }
25992
25993 #endif /* HAVE_WINDOW_SYSTEM */
25994
25995 /* Produce a stretch glyph for iterator IT. IT->object is the value
25996 of the glyph property displayed. The value must be a list
25997 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25998 being recognized:
25999
26000 1. `:width WIDTH' specifies that the space should be WIDTH *
26001 canonical char width wide. WIDTH may be an integer or floating
26002 point number.
26003
26004 2. `:relative-width FACTOR' specifies that the width of the stretch
26005 should be computed from the width of the first character having the
26006 `glyph' property, and should be FACTOR times that width.
26007
26008 3. `:align-to HPOS' specifies that the space should be wide enough
26009 to reach HPOS, a value in canonical character units.
26010
26011 Exactly one of the above pairs must be present.
26012
26013 4. `:height HEIGHT' specifies that the height of the stretch produced
26014 should be HEIGHT, measured in canonical character units.
26015
26016 5. `:relative-height FACTOR' specifies that the height of the
26017 stretch should be FACTOR times the height of the characters having
26018 the glyph property.
26019
26020 Either none or exactly one of 4 or 5 must be present.
26021
26022 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26023 of the stretch should be used for the ascent of the stretch.
26024 ASCENT must be in the range 0 <= ASCENT <= 100. */
26025
26026 void
26027 produce_stretch_glyph (struct it *it)
26028 {
26029 /* (space :width WIDTH :height HEIGHT ...) */
26030 Lisp_Object prop, plist;
26031 int width = 0, height = 0, align_to = -1;
26032 bool zero_width_ok_p = false;
26033 double tem;
26034 struct font *font = NULL;
26035
26036 #ifdef HAVE_WINDOW_SYSTEM
26037 int ascent = 0;
26038 bool zero_height_ok_p = false;
26039
26040 if (FRAME_WINDOW_P (it->f))
26041 {
26042 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26043 font = face->font ? face->font : FRAME_FONT (it->f);
26044 prepare_face_for_display (it->f, face);
26045 }
26046 #endif
26047
26048 /* List should start with `space'. */
26049 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26050 plist = XCDR (it->object);
26051
26052 /* Compute the width of the stretch. */
26053 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26054 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26055 {
26056 /* Absolute width `:width WIDTH' specified and valid. */
26057 zero_width_ok_p = true;
26058 width = (int)tem;
26059 }
26060 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26061 {
26062 /* Relative width `:relative-width FACTOR' specified and valid.
26063 Compute the width of the characters having the `glyph'
26064 property. */
26065 struct it it2;
26066 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26067
26068 it2 = *it;
26069 if (it->multibyte_p)
26070 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26071 else
26072 {
26073 it2.c = it2.char_to_display = *p, it2.len = 1;
26074 if (! ASCII_CHAR_P (it2.c))
26075 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26076 }
26077
26078 it2.glyph_row = NULL;
26079 it2.what = IT_CHARACTER;
26080 PRODUCE_GLYPHS (&it2);
26081 width = NUMVAL (prop) * it2.pixel_width;
26082 }
26083 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26084 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26085 &align_to))
26086 {
26087 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26088 align_to = (align_to < 0
26089 ? 0
26090 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26091 else if (align_to < 0)
26092 align_to = window_box_left_offset (it->w, TEXT_AREA);
26093 width = max (0, (int)tem + align_to - it->current_x);
26094 zero_width_ok_p = true;
26095 }
26096 else
26097 /* Nothing specified -> width defaults to canonical char width. */
26098 width = FRAME_COLUMN_WIDTH (it->f);
26099
26100 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26101 width = 1;
26102
26103 #ifdef HAVE_WINDOW_SYSTEM
26104 /* Compute height. */
26105 if (FRAME_WINDOW_P (it->f))
26106 {
26107 int default_height = normal_char_height (font, ' ');
26108
26109 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26110 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26111 {
26112 height = (int)tem;
26113 zero_height_ok_p = true;
26114 }
26115 else if (prop = Fplist_get (plist, QCrelative_height),
26116 NUMVAL (prop) > 0)
26117 height = default_height * NUMVAL (prop);
26118 else
26119 height = default_height;
26120
26121 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26122 height = 1;
26123
26124 /* Compute percentage of height used for ascent. If
26125 `:ascent ASCENT' is present and valid, use that. Otherwise,
26126 derive the ascent from the font in use. */
26127 if (prop = Fplist_get (plist, QCascent),
26128 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26129 ascent = height * NUMVAL (prop) / 100.0;
26130 else if (!NILP (prop)
26131 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26132 ascent = min (max (0, (int)tem), height);
26133 else
26134 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26135 }
26136 else
26137 #endif /* HAVE_WINDOW_SYSTEM */
26138 height = 1;
26139
26140 if (width > 0 && it->line_wrap != TRUNCATE
26141 && it->current_x + width > it->last_visible_x)
26142 {
26143 width = it->last_visible_x - it->current_x;
26144 #ifdef HAVE_WINDOW_SYSTEM
26145 /* Subtract one more pixel from the stretch width, but only on
26146 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26147 width -= FRAME_WINDOW_P (it->f);
26148 #endif
26149 }
26150
26151 if (width > 0 && height > 0 && it->glyph_row)
26152 {
26153 Lisp_Object o_object = it->object;
26154 Lisp_Object object = it->stack[it->sp - 1].string;
26155 int n = width;
26156
26157 if (!STRINGP (object))
26158 object = it->w->contents;
26159 #ifdef HAVE_WINDOW_SYSTEM
26160 if (FRAME_WINDOW_P (it->f))
26161 append_stretch_glyph (it, object, width, height, ascent);
26162 else
26163 #endif
26164 {
26165 it->object = object;
26166 it->char_to_display = ' ';
26167 it->pixel_width = it->len = 1;
26168 while (n--)
26169 tty_append_glyph (it);
26170 it->object = o_object;
26171 }
26172 }
26173
26174 it->pixel_width = width;
26175 #ifdef HAVE_WINDOW_SYSTEM
26176 if (FRAME_WINDOW_P (it->f))
26177 {
26178 it->ascent = it->phys_ascent = ascent;
26179 it->descent = it->phys_descent = height - it->ascent;
26180 it->nglyphs = width > 0 && height > 0;
26181 take_vertical_position_into_account (it);
26182 }
26183 else
26184 #endif
26185 it->nglyphs = width;
26186 }
26187
26188 /* Get information about special display element WHAT in an
26189 environment described by IT. WHAT is one of IT_TRUNCATION or
26190 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26191 non-null glyph_row member. This function ensures that fields like
26192 face_id, c, len of IT are left untouched. */
26193
26194 static void
26195 produce_special_glyphs (struct it *it, enum display_element_type what)
26196 {
26197 struct it temp_it;
26198 Lisp_Object gc;
26199 GLYPH glyph;
26200
26201 temp_it = *it;
26202 temp_it.object = Qnil;
26203 memset (&temp_it.current, 0, sizeof temp_it.current);
26204
26205 if (what == IT_CONTINUATION)
26206 {
26207 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26208 if (it->bidi_it.paragraph_dir == R2L)
26209 SET_GLYPH_FROM_CHAR (glyph, '/');
26210 else
26211 SET_GLYPH_FROM_CHAR (glyph, '\\');
26212 if (it->dp
26213 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26214 {
26215 /* FIXME: Should we mirror GC for R2L lines? */
26216 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26217 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26218 }
26219 }
26220 else if (what == IT_TRUNCATION)
26221 {
26222 /* Truncation glyph. */
26223 SET_GLYPH_FROM_CHAR (glyph, '$');
26224 if (it->dp
26225 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26226 {
26227 /* FIXME: Should we mirror GC for R2L lines? */
26228 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26229 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26230 }
26231 }
26232 else
26233 emacs_abort ();
26234
26235 #ifdef HAVE_WINDOW_SYSTEM
26236 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26237 is turned off, we precede the truncation/continuation glyphs by a
26238 stretch glyph whose width is computed such that these special
26239 glyphs are aligned at the window margin, even when very different
26240 fonts are used in different glyph rows. */
26241 if (FRAME_WINDOW_P (temp_it.f)
26242 /* init_iterator calls this with it->glyph_row == NULL, and it
26243 wants only the pixel width of the truncation/continuation
26244 glyphs. */
26245 && temp_it.glyph_row
26246 /* insert_left_trunc_glyphs calls us at the beginning of the
26247 row, and it has its own calculation of the stretch glyph
26248 width. */
26249 && temp_it.glyph_row->used[TEXT_AREA] > 0
26250 && (temp_it.glyph_row->reversed_p
26251 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26252 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26253 {
26254 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26255
26256 if (stretch_width > 0)
26257 {
26258 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26259 struct font *font =
26260 face->font ? face->font : FRAME_FONT (temp_it.f);
26261 int stretch_ascent =
26262 (((temp_it.ascent + temp_it.descent)
26263 * FONT_BASE (font)) / FONT_HEIGHT (font));
26264
26265 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26266 temp_it.ascent + temp_it.descent,
26267 stretch_ascent);
26268 }
26269 }
26270 #endif
26271
26272 temp_it.dp = NULL;
26273 temp_it.what = IT_CHARACTER;
26274 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26275 temp_it.face_id = GLYPH_FACE (glyph);
26276 temp_it.len = CHAR_BYTES (temp_it.c);
26277
26278 PRODUCE_GLYPHS (&temp_it);
26279 it->pixel_width = temp_it.pixel_width;
26280 it->nglyphs = temp_it.nglyphs;
26281 }
26282
26283 #ifdef HAVE_WINDOW_SYSTEM
26284
26285 /* Calculate line-height and line-spacing properties.
26286 An integer value specifies explicit pixel value.
26287 A float value specifies relative value to current face height.
26288 A cons (float . face-name) specifies relative value to
26289 height of specified face font.
26290
26291 Returns height in pixels, or nil. */
26292
26293 static Lisp_Object
26294 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26295 int boff, bool override)
26296 {
26297 Lisp_Object face_name = Qnil;
26298 int ascent, descent, height;
26299
26300 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26301 return val;
26302
26303 if (CONSP (val))
26304 {
26305 face_name = XCAR (val);
26306 val = XCDR (val);
26307 if (!NUMBERP (val))
26308 val = make_number (1);
26309 if (NILP (face_name))
26310 {
26311 height = it->ascent + it->descent;
26312 goto scale;
26313 }
26314 }
26315
26316 if (NILP (face_name))
26317 {
26318 font = FRAME_FONT (it->f);
26319 boff = FRAME_BASELINE_OFFSET (it->f);
26320 }
26321 else if (EQ (face_name, Qt))
26322 {
26323 override = false;
26324 }
26325 else
26326 {
26327 int face_id;
26328 struct face *face;
26329
26330 face_id = lookup_named_face (it->f, face_name, false);
26331 if (face_id < 0)
26332 return make_number (-1);
26333
26334 face = FACE_FROM_ID (it->f, face_id);
26335 font = face->font;
26336 if (font == NULL)
26337 return make_number (-1);
26338 boff = font->baseline_offset;
26339 if (font->vertical_centering)
26340 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26341 }
26342
26343 normal_char_ascent_descent (font, -1, &ascent, &descent);
26344
26345 if (override)
26346 {
26347 it->override_ascent = ascent;
26348 it->override_descent = descent;
26349 it->override_boff = boff;
26350 }
26351
26352 height = ascent + descent;
26353
26354 scale:
26355 if (FLOATP (val))
26356 height = (int)(XFLOAT_DATA (val) * height);
26357 else if (INTEGERP (val))
26358 height *= XINT (val);
26359
26360 return make_number (height);
26361 }
26362
26363
26364 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26365 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26366 and only if this is for a character for which no font was found.
26367
26368 If the display method (it->glyphless_method) is
26369 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26370 length of the acronym or the hexadecimal string, UPPER_XOFF and
26371 UPPER_YOFF are pixel offsets for the upper part of the string,
26372 LOWER_XOFF and LOWER_YOFF are for the lower part.
26373
26374 For the other display methods, LEN through LOWER_YOFF are zero. */
26375
26376 static void
26377 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26378 short upper_xoff, short upper_yoff,
26379 short lower_xoff, short lower_yoff)
26380 {
26381 struct glyph *glyph;
26382 enum glyph_row_area area = it->area;
26383
26384 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26385 if (glyph < it->glyph_row->glyphs[area + 1])
26386 {
26387 /* If the glyph row is reversed, we need to prepend the glyph
26388 rather than append it. */
26389 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26390 {
26391 struct glyph *g;
26392
26393 /* Make room for the additional glyph. */
26394 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26395 g[1] = *g;
26396 glyph = it->glyph_row->glyphs[area];
26397 }
26398 glyph->charpos = CHARPOS (it->position);
26399 glyph->object = it->object;
26400 glyph->pixel_width = it->pixel_width;
26401 glyph->ascent = it->ascent;
26402 glyph->descent = it->descent;
26403 glyph->voffset = it->voffset;
26404 glyph->type = GLYPHLESS_GLYPH;
26405 glyph->u.glyphless.method = it->glyphless_method;
26406 glyph->u.glyphless.for_no_font = for_no_font;
26407 glyph->u.glyphless.len = len;
26408 glyph->u.glyphless.ch = it->c;
26409 glyph->slice.glyphless.upper_xoff = upper_xoff;
26410 glyph->slice.glyphless.upper_yoff = upper_yoff;
26411 glyph->slice.glyphless.lower_xoff = lower_xoff;
26412 glyph->slice.glyphless.lower_yoff = lower_yoff;
26413 glyph->avoid_cursor_p = it->avoid_cursor_p;
26414 glyph->multibyte_p = it->multibyte_p;
26415 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26416 {
26417 /* In R2L rows, the left and the right box edges need to be
26418 drawn in reverse direction. */
26419 glyph->right_box_line_p = it->start_of_box_run_p;
26420 glyph->left_box_line_p = it->end_of_box_run_p;
26421 }
26422 else
26423 {
26424 glyph->left_box_line_p = it->start_of_box_run_p;
26425 glyph->right_box_line_p = it->end_of_box_run_p;
26426 }
26427 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26428 || it->phys_descent > it->descent);
26429 glyph->padding_p = false;
26430 glyph->glyph_not_available_p = false;
26431 glyph->face_id = face_id;
26432 glyph->font_type = FONT_TYPE_UNKNOWN;
26433 if (it->bidi_p)
26434 {
26435 glyph->resolved_level = it->bidi_it.resolved_level;
26436 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26437 glyph->bidi_type = it->bidi_it.type;
26438 }
26439 ++it->glyph_row->used[area];
26440 }
26441 else
26442 IT_EXPAND_MATRIX_WIDTH (it, area);
26443 }
26444
26445
26446 /* Produce a glyph for a glyphless character for iterator IT.
26447 IT->glyphless_method specifies which method to use for displaying
26448 the character. See the description of enum
26449 glyphless_display_method in dispextern.h for the detail.
26450
26451 FOR_NO_FONT is true if and only if this is for a character for
26452 which no font was found. ACRONYM, if non-nil, is an acronym string
26453 for the character. */
26454
26455 static void
26456 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26457 {
26458 int face_id;
26459 struct face *face;
26460 struct font *font;
26461 int base_width, base_height, width, height;
26462 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26463 int len;
26464
26465 /* Get the metrics of the base font. We always refer to the current
26466 ASCII face. */
26467 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26468 font = face->font ? face->font : FRAME_FONT (it->f);
26469 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
26470 it->ascent += font->baseline_offset;
26471 it->descent -= font->baseline_offset;
26472 base_height = it->ascent + it->descent;
26473 base_width = font->average_width;
26474
26475 face_id = merge_glyphless_glyph_face (it);
26476
26477 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26478 {
26479 it->pixel_width = THIN_SPACE_WIDTH;
26480 len = 0;
26481 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26482 }
26483 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26484 {
26485 width = CHAR_WIDTH (it->c);
26486 if (width == 0)
26487 width = 1;
26488 else if (width > 4)
26489 width = 4;
26490 it->pixel_width = base_width * width;
26491 len = 0;
26492 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26493 }
26494 else
26495 {
26496 char buf[7];
26497 const char *str;
26498 unsigned int code[6];
26499 int upper_len;
26500 int ascent, descent;
26501 struct font_metrics metrics_upper, metrics_lower;
26502
26503 face = FACE_FROM_ID (it->f, face_id);
26504 font = face->font ? face->font : FRAME_FONT (it->f);
26505 prepare_face_for_display (it->f, face);
26506
26507 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26508 {
26509 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26510 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26511 if (CONSP (acronym))
26512 acronym = XCAR (acronym);
26513 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26514 }
26515 else
26516 {
26517 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26518 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
26519 str = buf;
26520 }
26521 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26522 code[len] = font->driver->encode_char (font, str[len]);
26523 upper_len = (len + 1) / 2;
26524 font->driver->text_extents (font, code, upper_len,
26525 &metrics_upper);
26526 font->driver->text_extents (font, code + upper_len, len - upper_len,
26527 &metrics_lower);
26528
26529
26530
26531 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26532 width = max (metrics_upper.width, metrics_lower.width) + 4;
26533 upper_xoff = upper_yoff = 2; /* the typical case */
26534 if (base_width >= width)
26535 {
26536 /* Align the upper to the left, the lower to the right. */
26537 it->pixel_width = base_width;
26538 lower_xoff = base_width - 2 - metrics_lower.width;
26539 }
26540 else
26541 {
26542 /* Center the shorter one. */
26543 it->pixel_width = width;
26544 if (metrics_upper.width >= metrics_lower.width)
26545 lower_xoff = (width - metrics_lower.width) / 2;
26546 else
26547 {
26548 /* FIXME: This code doesn't look right. It formerly was
26549 missing the "lower_xoff = 0;", which couldn't have
26550 been right since it left lower_xoff uninitialized. */
26551 lower_xoff = 0;
26552 upper_xoff = (width - metrics_upper.width) / 2;
26553 }
26554 }
26555
26556 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26557 top, bottom, and between upper and lower strings. */
26558 height = (metrics_upper.ascent + metrics_upper.descent
26559 + metrics_lower.ascent + metrics_lower.descent) + 5;
26560 /* Center vertically.
26561 H:base_height, D:base_descent
26562 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26563
26564 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26565 descent = D - H/2 + h/2;
26566 lower_yoff = descent - 2 - ld;
26567 upper_yoff = lower_yoff - la - 1 - ud; */
26568 ascent = - (it->descent - (base_height + height + 1) / 2);
26569 descent = it->descent - (base_height - height) / 2;
26570 lower_yoff = descent - 2 - metrics_lower.descent;
26571 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26572 - metrics_upper.descent);
26573 /* Don't make the height shorter than the base height. */
26574 if (height > base_height)
26575 {
26576 it->ascent = ascent;
26577 it->descent = descent;
26578 }
26579 }
26580
26581 it->phys_ascent = it->ascent;
26582 it->phys_descent = it->descent;
26583 if (it->glyph_row)
26584 append_glyphless_glyph (it, face_id, for_no_font, len,
26585 upper_xoff, upper_yoff,
26586 lower_xoff, lower_yoff);
26587 it->nglyphs = 1;
26588 take_vertical_position_into_account (it);
26589 }
26590
26591
26592 /* RIF:
26593 Produce glyphs/get display metrics for the display element IT is
26594 loaded with. See the description of struct it in dispextern.h
26595 for an overview of struct it. */
26596
26597 void
26598 x_produce_glyphs (struct it *it)
26599 {
26600 int extra_line_spacing = it->extra_line_spacing;
26601
26602 it->glyph_not_available_p = false;
26603
26604 if (it->what == IT_CHARACTER)
26605 {
26606 XChar2b char2b;
26607 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26608 struct font *font = face->font;
26609 struct font_metrics *pcm = NULL;
26610 int boff; /* Baseline offset. */
26611
26612 if (font == NULL)
26613 {
26614 /* When no suitable font is found, display this character by
26615 the method specified in the first extra slot of
26616 Vglyphless_char_display. */
26617 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26618
26619 eassert (it->what == IT_GLYPHLESS);
26620 produce_glyphless_glyph (it, true,
26621 STRINGP (acronym) ? acronym : Qnil);
26622 goto done;
26623 }
26624
26625 boff = font->baseline_offset;
26626 if (font->vertical_centering)
26627 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26628
26629 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26630 {
26631 it->nglyphs = 1;
26632
26633 if (it->override_ascent >= 0)
26634 {
26635 it->ascent = it->override_ascent;
26636 it->descent = it->override_descent;
26637 boff = it->override_boff;
26638 }
26639 else
26640 {
26641 it->ascent = FONT_BASE (font) + boff;
26642 it->descent = FONT_DESCENT (font) - boff;
26643 }
26644
26645 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26646 {
26647 pcm = get_per_char_metric (font, &char2b);
26648 if (pcm->width == 0
26649 && pcm->rbearing == 0 && pcm->lbearing == 0)
26650 pcm = NULL;
26651 }
26652
26653 if (pcm)
26654 {
26655 it->phys_ascent = pcm->ascent + boff;
26656 it->phys_descent = pcm->descent - boff;
26657 it->pixel_width = pcm->width;
26658 /* Don't use font-global values for ascent and descent
26659 if they result in an exceedingly large line height. */
26660 if (it->override_ascent < 0)
26661 {
26662 if (FONT_TOO_HIGH (font))
26663 {
26664 it->ascent = it->phys_ascent;
26665 it->descent = it->phys_descent;
26666 /* These limitations are enforced by an
26667 assertion near the end of this function. */
26668 if (it->ascent < 0)
26669 it->ascent = 0;
26670 if (it->descent < 0)
26671 it->descent = 0;
26672 }
26673 }
26674 }
26675 else
26676 {
26677 it->glyph_not_available_p = true;
26678 it->phys_ascent = it->ascent;
26679 it->phys_descent = it->descent;
26680 it->pixel_width = font->space_width;
26681 }
26682
26683 if (it->constrain_row_ascent_descent_p)
26684 {
26685 if (it->descent > it->max_descent)
26686 {
26687 it->ascent += it->descent - it->max_descent;
26688 it->descent = it->max_descent;
26689 }
26690 if (it->ascent > it->max_ascent)
26691 {
26692 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26693 it->ascent = it->max_ascent;
26694 }
26695 it->phys_ascent = min (it->phys_ascent, it->ascent);
26696 it->phys_descent = min (it->phys_descent, it->descent);
26697 extra_line_spacing = 0;
26698 }
26699
26700 /* If this is a space inside a region of text with
26701 `space-width' property, change its width. */
26702 bool stretched_p
26703 = it->char_to_display == ' ' && !NILP (it->space_width);
26704 if (stretched_p)
26705 it->pixel_width *= XFLOATINT (it->space_width);
26706
26707 /* If face has a box, add the box thickness to the character
26708 height. If character has a box line to the left and/or
26709 right, add the box line width to the character's width. */
26710 if (face->box != FACE_NO_BOX)
26711 {
26712 int thick = face->box_line_width;
26713
26714 if (thick > 0)
26715 {
26716 it->ascent += thick;
26717 it->descent += thick;
26718 }
26719 else
26720 thick = -thick;
26721
26722 if (it->start_of_box_run_p)
26723 it->pixel_width += thick;
26724 if (it->end_of_box_run_p)
26725 it->pixel_width += thick;
26726 }
26727
26728 /* If face has an overline, add the height of the overline
26729 (1 pixel) and a 1 pixel margin to the character height. */
26730 if (face->overline_p)
26731 it->ascent += overline_margin;
26732
26733 if (it->constrain_row_ascent_descent_p)
26734 {
26735 if (it->ascent > it->max_ascent)
26736 it->ascent = it->max_ascent;
26737 if (it->descent > it->max_descent)
26738 it->descent = it->max_descent;
26739 }
26740
26741 take_vertical_position_into_account (it);
26742
26743 /* If we have to actually produce glyphs, do it. */
26744 if (it->glyph_row)
26745 {
26746 if (stretched_p)
26747 {
26748 /* Translate a space with a `space-width' property
26749 into a stretch glyph. */
26750 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26751 / FONT_HEIGHT (font));
26752 append_stretch_glyph (it, it->object, it->pixel_width,
26753 it->ascent + it->descent, ascent);
26754 }
26755 else
26756 append_glyph (it);
26757
26758 /* If characters with lbearing or rbearing are displayed
26759 in this line, record that fact in a flag of the
26760 glyph row. This is used to optimize X output code. */
26761 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26762 it->glyph_row->contains_overlapping_glyphs_p = true;
26763 }
26764 if (! stretched_p && it->pixel_width == 0)
26765 /* We assure that all visible glyphs have at least 1-pixel
26766 width. */
26767 it->pixel_width = 1;
26768 }
26769 else if (it->char_to_display == '\n')
26770 {
26771 /* A newline has no width, but we need the height of the
26772 line. But if previous part of the line sets a height,
26773 don't increase that height. */
26774
26775 Lisp_Object height;
26776 Lisp_Object total_height = Qnil;
26777
26778 it->override_ascent = -1;
26779 it->pixel_width = 0;
26780 it->nglyphs = 0;
26781
26782 height = get_it_property (it, Qline_height);
26783 /* Split (line-height total-height) list. */
26784 if (CONSP (height)
26785 && CONSP (XCDR (height))
26786 && NILP (XCDR (XCDR (height))))
26787 {
26788 total_height = XCAR (XCDR (height));
26789 height = XCAR (height);
26790 }
26791 height = calc_line_height_property (it, height, font, boff, true);
26792
26793 if (it->override_ascent >= 0)
26794 {
26795 it->ascent = it->override_ascent;
26796 it->descent = it->override_descent;
26797 boff = it->override_boff;
26798 }
26799 else
26800 {
26801 if (FONT_TOO_HIGH (font))
26802 {
26803 it->ascent = font->pixel_size + boff - 1;
26804 it->descent = -boff + 1;
26805 if (it->descent < 0)
26806 it->descent = 0;
26807 }
26808 else
26809 {
26810 it->ascent = FONT_BASE (font) + boff;
26811 it->descent = FONT_DESCENT (font) - boff;
26812 }
26813 }
26814
26815 if (EQ (height, Qt))
26816 {
26817 if (it->descent > it->max_descent)
26818 {
26819 it->ascent += it->descent - it->max_descent;
26820 it->descent = it->max_descent;
26821 }
26822 if (it->ascent > it->max_ascent)
26823 {
26824 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26825 it->ascent = it->max_ascent;
26826 }
26827 it->phys_ascent = min (it->phys_ascent, it->ascent);
26828 it->phys_descent = min (it->phys_descent, it->descent);
26829 it->constrain_row_ascent_descent_p = true;
26830 extra_line_spacing = 0;
26831 }
26832 else
26833 {
26834 Lisp_Object spacing;
26835
26836 it->phys_ascent = it->ascent;
26837 it->phys_descent = it->descent;
26838
26839 if ((it->max_ascent > 0 || it->max_descent > 0)
26840 && face->box != FACE_NO_BOX
26841 && face->box_line_width > 0)
26842 {
26843 it->ascent += face->box_line_width;
26844 it->descent += face->box_line_width;
26845 }
26846 if (!NILP (height)
26847 && XINT (height) > it->ascent + it->descent)
26848 it->ascent = XINT (height) - it->descent;
26849
26850 if (!NILP (total_height))
26851 spacing = calc_line_height_property (it, total_height, font,
26852 boff, false);
26853 else
26854 {
26855 spacing = get_it_property (it, Qline_spacing);
26856 spacing = calc_line_height_property (it, spacing, font,
26857 boff, false);
26858 }
26859 if (INTEGERP (spacing))
26860 {
26861 extra_line_spacing = XINT (spacing);
26862 if (!NILP (total_height))
26863 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26864 }
26865 }
26866 }
26867 else /* i.e. (it->char_to_display == '\t') */
26868 {
26869 if (font->space_width > 0)
26870 {
26871 int tab_width = it->tab_width * font->space_width;
26872 int x = it->current_x + it->continuation_lines_width;
26873 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26874
26875 /* If the distance from the current position to the next tab
26876 stop is less than a space character width, use the
26877 tab stop after that. */
26878 if (next_tab_x - x < font->space_width)
26879 next_tab_x += tab_width;
26880
26881 it->pixel_width = next_tab_x - x;
26882 it->nglyphs = 1;
26883 if (FONT_TOO_HIGH (font))
26884 {
26885 if (get_char_glyph_code (' ', font, &char2b))
26886 {
26887 pcm = get_per_char_metric (font, &char2b);
26888 if (pcm->width == 0
26889 && pcm->rbearing == 0 && pcm->lbearing == 0)
26890 pcm = NULL;
26891 }
26892
26893 if (pcm)
26894 {
26895 it->ascent = pcm->ascent + boff;
26896 it->descent = pcm->descent - boff;
26897 }
26898 else
26899 {
26900 it->ascent = font->pixel_size + boff - 1;
26901 it->descent = -boff + 1;
26902 }
26903 if (it->ascent < 0)
26904 it->ascent = 0;
26905 if (it->descent < 0)
26906 it->descent = 0;
26907 }
26908 else
26909 {
26910 it->ascent = FONT_BASE (font) + boff;
26911 it->descent = FONT_DESCENT (font) - boff;
26912 }
26913 it->phys_ascent = it->ascent;
26914 it->phys_descent = it->descent;
26915
26916 if (it->glyph_row)
26917 {
26918 append_stretch_glyph (it, it->object, it->pixel_width,
26919 it->ascent + it->descent, it->ascent);
26920 }
26921 }
26922 else
26923 {
26924 it->pixel_width = 0;
26925 it->nglyphs = 1;
26926 }
26927 }
26928
26929 if (FONT_TOO_HIGH (font))
26930 {
26931 int font_ascent, font_descent;
26932
26933 /* For very large fonts, where we ignore the declared font
26934 dimensions, and go by per-character metrics instead,
26935 don't let the row ascent and descent values (and the row
26936 height computed from them) be smaller than the "normal"
26937 character metrics. This avoids unpleasant effects
26938 whereby lines on display would change their height
26939 depending on which characters are shown. */
26940 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
26941 it->max_ascent = max (it->max_ascent, font_ascent);
26942 it->max_descent = max (it->max_descent, font_descent);
26943 }
26944 }
26945 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26946 {
26947 /* A static composition.
26948
26949 Note: A composition is represented as one glyph in the
26950 glyph matrix. There are no padding glyphs.
26951
26952 Important note: pixel_width, ascent, and descent are the
26953 values of what is drawn by draw_glyphs (i.e. the values of
26954 the overall glyphs composed). */
26955 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26956 int boff; /* baseline offset */
26957 struct composition *cmp = composition_table[it->cmp_it.id];
26958 int glyph_len = cmp->glyph_len;
26959 struct font *font = face->font;
26960
26961 it->nglyphs = 1;
26962
26963 /* If we have not yet calculated pixel size data of glyphs of
26964 the composition for the current face font, calculate them
26965 now. Theoretically, we have to check all fonts for the
26966 glyphs, but that requires much time and memory space. So,
26967 here we check only the font of the first glyph. This may
26968 lead to incorrect display, but it's very rare, and C-l
26969 (recenter-top-bottom) can correct the display anyway. */
26970 if (! cmp->font || cmp->font != font)
26971 {
26972 /* Ascent and descent of the font of the first character
26973 of this composition (adjusted by baseline offset).
26974 Ascent and descent of overall glyphs should not be less
26975 than these, respectively. */
26976 int font_ascent, font_descent, font_height;
26977 /* Bounding box of the overall glyphs. */
26978 int leftmost, rightmost, lowest, highest;
26979 int lbearing, rbearing;
26980 int i, width, ascent, descent;
26981 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26982 XChar2b char2b;
26983 struct font_metrics *pcm;
26984 ptrdiff_t pos;
26985
26986 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26987 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26988 break;
26989 bool right_padded = glyph_len < cmp->glyph_len;
26990 for (i = 0; i < glyph_len; i++)
26991 {
26992 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26993 break;
26994 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26995 }
26996 bool left_padded = i > 0;
26997
26998 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26999 : IT_CHARPOS (*it));
27000 /* If no suitable font is found, use the default font. */
27001 bool font_not_found_p = font == NULL;
27002 if (font_not_found_p)
27003 {
27004 face = face->ascii_face;
27005 font = face->font;
27006 }
27007 boff = font->baseline_offset;
27008 if (font->vertical_centering)
27009 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27010 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27011 font_ascent += boff;
27012 font_descent -= boff;
27013 font_height = font_ascent + font_descent;
27014
27015 cmp->font = font;
27016
27017 pcm = NULL;
27018 if (! font_not_found_p)
27019 {
27020 get_char_face_and_encoding (it->f, c, it->face_id,
27021 &char2b, false);
27022 pcm = get_per_char_metric (font, &char2b);
27023 }
27024
27025 /* Initialize the bounding box. */
27026 if (pcm)
27027 {
27028 width = cmp->glyph_len > 0 ? pcm->width : 0;
27029 ascent = pcm->ascent;
27030 descent = pcm->descent;
27031 lbearing = pcm->lbearing;
27032 rbearing = pcm->rbearing;
27033 }
27034 else
27035 {
27036 width = cmp->glyph_len > 0 ? font->space_width : 0;
27037 ascent = FONT_BASE (font);
27038 descent = FONT_DESCENT (font);
27039 lbearing = 0;
27040 rbearing = width;
27041 }
27042
27043 rightmost = width;
27044 leftmost = 0;
27045 lowest = - descent + boff;
27046 highest = ascent + boff;
27047
27048 if (! font_not_found_p
27049 && font->default_ascent
27050 && CHAR_TABLE_P (Vuse_default_ascent)
27051 && !NILP (Faref (Vuse_default_ascent,
27052 make_number (it->char_to_display))))
27053 highest = font->default_ascent + boff;
27054
27055 /* Draw the first glyph at the normal position. It may be
27056 shifted to right later if some other glyphs are drawn
27057 at the left. */
27058 cmp->offsets[i * 2] = 0;
27059 cmp->offsets[i * 2 + 1] = boff;
27060 cmp->lbearing = lbearing;
27061 cmp->rbearing = rbearing;
27062
27063 /* Set cmp->offsets for the remaining glyphs. */
27064 for (i++; i < glyph_len; i++)
27065 {
27066 int left, right, btm, top;
27067 int ch = COMPOSITION_GLYPH (cmp, i);
27068 int face_id;
27069 struct face *this_face;
27070
27071 if (ch == '\t')
27072 ch = ' ';
27073 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27074 this_face = FACE_FROM_ID (it->f, face_id);
27075 font = this_face->font;
27076
27077 if (font == NULL)
27078 pcm = NULL;
27079 else
27080 {
27081 get_char_face_and_encoding (it->f, ch, face_id,
27082 &char2b, false);
27083 pcm = get_per_char_metric (font, &char2b);
27084 }
27085 if (! pcm)
27086 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27087 else
27088 {
27089 width = pcm->width;
27090 ascent = pcm->ascent;
27091 descent = pcm->descent;
27092 lbearing = pcm->lbearing;
27093 rbearing = pcm->rbearing;
27094 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27095 {
27096 /* Relative composition with or without
27097 alternate chars. */
27098 left = (leftmost + rightmost - width) / 2;
27099 btm = - descent + boff;
27100 if (font->relative_compose
27101 && (! CHAR_TABLE_P (Vignore_relative_composition)
27102 || NILP (Faref (Vignore_relative_composition,
27103 make_number (ch)))))
27104 {
27105
27106 if (- descent >= font->relative_compose)
27107 /* One extra pixel between two glyphs. */
27108 btm = highest + 1;
27109 else if (ascent <= 0)
27110 /* One extra pixel between two glyphs. */
27111 btm = lowest - 1 - ascent - descent;
27112 }
27113 }
27114 else
27115 {
27116 /* A composition rule is specified by an integer
27117 value that encodes global and new reference
27118 points (GREF and NREF). GREF and NREF are
27119 specified by numbers as below:
27120
27121 0---1---2 -- ascent
27122 | |
27123 | |
27124 | |
27125 9--10--11 -- center
27126 | |
27127 ---3---4---5--- baseline
27128 | |
27129 6---7---8 -- descent
27130 */
27131 int rule = COMPOSITION_RULE (cmp, i);
27132 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27133
27134 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27135 grefx = gref % 3, nrefx = nref % 3;
27136 grefy = gref / 3, nrefy = nref / 3;
27137 if (xoff)
27138 xoff = font_height * (xoff - 128) / 256;
27139 if (yoff)
27140 yoff = font_height * (yoff - 128) / 256;
27141
27142 left = (leftmost
27143 + grefx * (rightmost - leftmost) / 2
27144 - nrefx * width / 2
27145 + xoff);
27146
27147 btm = ((grefy == 0 ? highest
27148 : grefy == 1 ? 0
27149 : grefy == 2 ? lowest
27150 : (highest + lowest) / 2)
27151 - (nrefy == 0 ? ascent + descent
27152 : nrefy == 1 ? descent - boff
27153 : nrefy == 2 ? 0
27154 : (ascent + descent) / 2)
27155 + yoff);
27156 }
27157
27158 cmp->offsets[i * 2] = left;
27159 cmp->offsets[i * 2 + 1] = btm + descent;
27160
27161 /* Update the bounding box of the overall glyphs. */
27162 if (width > 0)
27163 {
27164 right = left + width;
27165 if (left < leftmost)
27166 leftmost = left;
27167 if (right > rightmost)
27168 rightmost = right;
27169 }
27170 top = btm + descent + ascent;
27171 if (top > highest)
27172 highest = top;
27173 if (btm < lowest)
27174 lowest = btm;
27175
27176 if (cmp->lbearing > left + lbearing)
27177 cmp->lbearing = left + lbearing;
27178 if (cmp->rbearing < left + rbearing)
27179 cmp->rbearing = left + rbearing;
27180 }
27181 }
27182
27183 /* If there are glyphs whose x-offsets are negative,
27184 shift all glyphs to the right and make all x-offsets
27185 non-negative. */
27186 if (leftmost < 0)
27187 {
27188 for (i = 0; i < cmp->glyph_len; i++)
27189 cmp->offsets[i * 2] -= leftmost;
27190 rightmost -= leftmost;
27191 cmp->lbearing -= leftmost;
27192 cmp->rbearing -= leftmost;
27193 }
27194
27195 if (left_padded && cmp->lbearing < 0)
27196 {
27197 for (i = 0; i < cmp->glyph_len; i++)
27198 cmp->offsets[i * 2] -= cmp->lbearing;
27199 rightmost -= cmp->lbearing;
27200 cmp->rbearing -= cmp->lbearing;
27201 cmp->lbearing = 0;
27202 }
27203 if (right_padded && rightmost < cmp->rbearing)
27204 {
27205 rightmost = cmp->rbearing;
27206 }
27207
27208 cmp->pixel_width = rightmost;
27209 cmp->ascent = highest;
27210 cmp->descent = - lowest;
27211 if (cmp->ascent < font_ascent)
27212 cmp->ascent = font_ascent;
27213 if (cmp->descent < font_descent)
27214 cmp->descent = font_descent;
27215 }
27216
27217 if (it->glyph_row
27218 && (cmp->lbearing < 0
27219 || cmp->rbearing > cmp->pixel_width))
27220 it->glyph_row->contains_overlapping_glyphs_p = true;
27221
27222 it->pixel_width = cmp->pixel_width;
27223 it->ascent = it->phys_ascent = cmp->ascent;
27224 it->descent = it->phys_descent = cmp->descent;
27225 if (face->box != FACE_NO_BOX)
27226 {
27227 int thick = face->box_line_width;
27228
27229 if (thick > 0)
27230 {
27231 it->ascent += thick;
27232 it->descent += thick;
27233 }
27234 else
27235 thick = - thick;
27236
27237 if (it->start_of_box_run_p)
27238 it->pixel_width += thick;
27239 if (it->end_of_box_run_p)
27240 it->pixel_width += thick;
27241 }
27242
27243 /* If face has an overline, add the height of the overline
27244 (1 pixel) and a 1 pixel margin to the character height. */
27245 if (face->overline_p)
27246 it->ascent += overline_margin;
27247
27248 take_vertical_position_into_account (it);
27249 if (it->ascent < 0)
27250 it->ascent = 0;
27251 if (it->descent < 0)
27252 it->descent = 0;
27253
27254 if (it->glyph_row && cmp->glyph_len > 0)
27255 append_composite_glyph (it);
27256 }
27257 else if (it->what == IT_COMPOSITION)
27258 {
27259 /* A dynamic (automatic) composition. */
27260 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27261 Lisp_Object gstring;
27262 struct font_metrics metrics;
27263
27264 it->nglyphs = 1;
27265
27266 gstring = composition_gstring_from_id (it->cmp_it.id);
27267 it->pixel_width
27268 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27269 &metrics);
27270 if (it->glyph_row
27271 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27272 it->glyph_row->contains_overlapping_glyphs_p = true;
27273 it->ascent = it->phys_ascent = metrics.ascent;
27274 it->descent = it->phys_descent = metrics.descent;
27275 if (face->box != FACE_NO_BOX)
27276 {
27277 int thick = face->box_line_width;
27278
27279 if (thick > 0)
27280 {
27281 it->ascent += thick;
27282 it->descent += thick;
27283 }
27284 else
27285 thick = - thick;
27286
27287 if (it->start_of_box_run_p)
27288 it->pixel_width += thick;
27289 if (it->end_of_box_run_p)
27290 it->pixel_width += thick;
27291 }
27292 /* If face has an overline, add the height of the overline
27293 (1 pixel) and a 1 pixel margin to the character height. */
27294 if (face->overline_p)
27295 it->ascent += overline_margin;
27296 take_vertical_position_into_account (it);
27297 if (it->ascent < 0)
27298 it->ascent = 0;
27299 if (it->descent < 0)
27300 it->descent = 0;
27301
27302 if (it->glyph_row)
27303 append_composite_glyph (it);
27304 }
27305 else if (it->what == IT_GLYPHLESS)
27306 produce_glyphless_glyph (it, false, Qnil);
27307 else if (it->what == IT_IMAGE)
27308 produce_image_glyph (it);
27309 else if (it->what == IT_STRETCH)
27310 produce_stretch_glyph (it);
27311
27312 done:
27313 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27314 because this isn't true for images with `:ascent 100'. */
27315 eassert (it->ascent >= 0 && it->descent >= 0);
27316 if (it->area == TEXT_AREA)
27317 it->current_x += it->pixel_width;
27318
27319 if (extra_line_spacing > 0)
27320 {
27321 it->descent += extra_line_spacing;
27322 if (extra_line_spacing > it->max_extra_line_spacing)
27323 it->max_extra_line_spacing = extra_line_spacing;
27324 }
27325
27326 it->max_ascent = max (it->max_ascent, it->ascent);
27327 it->max_descent = max (it->max_descent, it->descent);
27328 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27329 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27330 }
27331
27332 /* EXPORT for RIF:
27333 Output LEN glyphs starting at START at the nominal cursor position.
27334 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27335 being updated, and UPDATED_AREA is the area of that row being updated. */
27336
27337 void
27338 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27339 struct glyph *start, enum glyph_row_area updated_area, int len)
27340 {
27341 int x, hpos, chpos = w->phys_cursor.hpos;
27342
27343 eassert (updated_row);
27344 /* When the window is hscrolled, cursor hpos can legitimately be out
27345 of bounds, but we draw the cursor at the corresponding window
27346 margin in that case. */
27347 if (!updated_row->reversed_p && chpos < 0)
27348 chpos = 0;
27349 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27350 chpos = updated_row->used[TEXT_AREA] - 1;
27351
27352 block_input ();
27353
27354 /* Write glyphs. */
27355
27356 hpos = start - updated_row->glyphs[updated_area];
27357 x = draw_glyphs (w, w->output_cursor.x,
27358 updated_row, updated_area,
27359 hpos, hpos + len,
27360 DRAW_NORMAL_TEXT, 0);
27361
27362 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27363 if (updated_area == TEXT_AREA
27364 && w->phys_cursor_on_p
27365 && w->phys_cursor.vpos == w->output_cursor.vpos
27366 && chpos >= hpos
27367 && chpos < hpos + len)
27368 w->phys_cursor_on_p = false;
27369
27370 unblock_input ();
27371
27372 /* Advance the output cursor. */
27373 w->output_cursor.hpos += len;
27374 w->output_cursor.x = x;
27375 }
27376
27377
27378 /* EXPORT for RIF:
27379 Insert LEN glyphs from START at the nominal cursor position. */
27380
27381 void
27382 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27383 struct glyph *start, enum glyph_row_area updated_area, int len)
27384 {
27385 struct frame *f;
27386 int line_height, shift_by_width, shifted_region_width;
27387 struct glyph_row *row;
27388 struct glyph *glyph;
27389 int frame_x, frame_y;
27390 ptrdiff_t hpos;
27391
27392 eassert (updated_row);
27393 block_input ();
27394 f = XFRAME (WINDOW_FRAME (w));
27395
27396 /* Get the height of the line we are in. */
27397 row = updated_row;
27398 line_height = row->height;
27399
27400 /* Get the width of the glyphs to insert. */
27401 shift_by_width = 0;
27402 for (glyph = start; glyph < start + len; ++glyph)
27403 shift_by_width += glyph->pixel_width;
27404
27405 /* Get the width of the region to shift right. */
27406 shifted_region_width = (window_box_width (w, updated_area)
27407 - w->output_cursor.x
27408 - shift_by_width);
27409
27410 /* Shift right. */
27411 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27412 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27413
27414 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27415 line_height, shift_by_width);
27416
27417 /* Write the glyphs. */
27418 hpos = start - row->glyphs[updated_area];
27419 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27420 hpos, hpos + len,
27421 DRAW_NORMAL_TEXT, 0);
27422
27423 /* Advance the output cursor. */
27424 w->output_cursor.hpos += len;
27425 w->output_cursor.x += shift_by_width;
27426 unblock_input ();
27427 }
27428
27429
27430 /* EXPORT for RIF:
27431 Erase the current text line from the nominal cursor position
27432 (inclusive) to pixel column TO_X (exclusive). The idea is that
27433 everything from TO_X onward is already erased.
27434
27435 TO_X is a pixel position relative to UPDATED_AREA of currently
27436 updated window W. TO_X == -1 means clear to the end of this area. */
27437
27438 void
27439 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27440 enum glyph_row_area updated_area, int to_x)
27441 {
27442 struct frame *f;
27443 int max_x, min_y, max_y;
27444 int from_x, from_y, to_y;
27445
27446 eassert (updated_row);
27447 f = XFRAME (w->frame);
27448
27449 if (updated_row->full_width_p)
27450 max_x = (WINDOW_PIXEL_WIDTH (w)
27451 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27452 else
27453 max_x = window_box_width (w, updated_area);
27454 max_y = window_text_bottom_y (w);
27455
27456 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27457 of window. For TO_X > 0, truncate to end of drawing area. */
27458 if (to_x == 0)
27459 return;
27460 else if (to_x < 0)
27461 to_x = max_x;
27462 else
27463 to_x = min (to_x, max_x);
27464
27465 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27466
27467 /* Notice if the cursor will be cleared by this operation. */
27468 if (!updated_row->full_width_p)
27469 notice_overwritten_cursor (w, updated_area,
27470 w->output_cursor.x, -1,
27471 updated_row->y,
27472 MATRIX_ROW_BOTTOM_Y (updated_row));
27473
27474 from_x = w->output_cursor.x;
27475
27476 /* Translate to frame coordinates. */
27477 if (updated_row->full_width_p)
27478 {
27479 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27480 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27481 }
27482 else
27483 {
27484 int area_left = window_box_left (w, updated_area);
27485 from_x += area_left;
27486 to_x += area_left;
27487 }
27488
27489 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27490 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27491 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27492
27493 /* Prevent inadvertently clearing to end of the X window. */
27494 if (to_x > from_x && to_y > from_y)
27495 {
27496 block_input ();
27497 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27498 to_x - from_x, to_y - from_y);
27499 unblock_input ();
27500 }
27501 }
27502
27503 #endif /* HAVE_WINDOW_SYSTEM */
27504
27505
27506 \f
27507 /***********************************************************************
27508 Cursor types
27509 ***********************************************************************/
27510
27511 /* Value is the internal representation of the specified cursor type
27512 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27513 of the bar cursor. */
27514
27515 static enum text_cursor_kinds
27516 get_specified_cursor_type (Lisp_Object arg, int *width)
27517 {
27518 enum text_cursor_kinds type;
27519
27520 if (NILP (arg))
27521 return NO_CURSOR;
27522
27523 if (EQ (arg, Qbox))
27524 return FILLED_BOX_CURSOR;
27525
27526 if (EQ (arg, Qhollow))
27527 return HOLLOW_BOX_CURSOR;
27528
27529 if (EQ (arg, Qbar))
27530 {
27531 *width = 2;
27532 return BAR_CURSOR;
27533 }
27534
27535 if (CONSP (arg)
27536 && EQ (XCAR (arg), Qbar)
27537 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27538 {
27539 *width = XINT (XCDR (arg));
27540 return BAR_CURSOR;
27541 }
27542
27543 if (EQ (arg, Qhbar))
27544 {
27545 *width = 2;
27546 return HBAR_CURSOR;
27547 }
27548
27549 if (CONSP (arg)
27550 && EQ (XCAR (arg), Qhbar)
27551 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27552 {
27553 *width = XINT (XCDR (arg));
27554 return HBAR_CURSOR;
27555 }
27556
27557 /* Treat anything unknown as "hollow box cursor".
27558 It was bad to signal an error; people have trouble fixing
27559 .Xdefaults with Emacs, when it has something bad in it. */
27560 type = HOLLOW_BOX_CURSOR;
27561
27562 return type;
27563 }
27564
27565 /* Set the default cursor types for specified frame. */
27566 void
27567 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27568 {
27569 int width = 1;
27570 Lisp_Object tem;
27571
27572 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27573 FRAME_CURSOR_WIDTH (f) = width;
27574
27575 /* By default, set up the blink-off state depending on the on-state. */
27576
27577 tem = Fassoc (arg, Vblink_cursor_alist);
27578 if (!NILP (tem))
27579 {
27580 FRAME_BLINK_OFF_CURSOR (f)
27581 = get_specified_cursor_type (XCDR (tem), &width);
27582 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27583 }
27584 else
27585 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27586
27587 /* Make sure the cursor gets redrawn. */
27588 f->cursor_type_changed = true;
27589 }
27590
27591
27592 #ifdef HAVE_WINDOW_SYSTEM
27593
27594 /* Return the cursor we want to be displayed in window W. Return
27595 width of bar/hbar cursor through WIDTH arg. Return with
27596 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
27597 (i.e. if the `system caret' should track this cursor).
27598
27599 In a mini-buffer window, we want the cursor only to appear if we
27600 are reading input from this window. For the selected window, we
27601 want the cursor type given by the frame parameter or buffer local
27602 setting of cursor-type. If explicitly marked off, draw no cursor.
27603 In all other cases, we want a hollow box cursor. */
27604
27605 static enum text_cursor_kinds
27606 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27607 bool *active_cursor)
27608 {
27609 struct frame *f = XFRAME (w->frame);
27610 struct buffer *b = XBUFFER (w->contents);
27611 int cursor_type = DEFAULT_CURSOR;
27612 Lisp_Object alt_cursor;
27613 bool non_selected = false;
27614
27615 *active_cursor = true;
27616
27617 /* Echo area */
27618 if (cursor_in_echo_area
27619 && FRAME_HAS_MINIBUF_P (f)
27620 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27621 {
27622 if (w == XWINDOW (echo_area_window))
27623 {
27624 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27625 {
27626 *width = FRAME_CURSOR_WIDTH (f);
27627 return FRAME_DESIRED_CURSOR (f);
27628 }
27629 else
27630 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27631 }
27632
27633 *active_cursor = false;
27634 non_selected = true;
27635 }
27636
27637 /* Detect a nonselected window or nonselected frame. */
27638 else if (w != XWINDOW (f->selected_window)
27639 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27640 {
27641 *active_cursor = false;
27642
27643 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27644 return NO_CURSOR;
27645
27646 non_selected = true;
27647 }
27648
27649 /* Never display a cursor in a window in which cursor-type is nil. */
27650 if (NILP (BVAR (b, cursor_type)))
27651 return NO_CURSOR;
27652
27653 /* Get the normal cursor type for this window. */
27654 if (EQ (BVAR (b, cursor_type), Qt))
27655 {
27656 cursor_type = FRAME_DESIRED_CURSOR (f);
27657 *width = FRAME_CURSOR_WIDTH (f);
27658 }
27659 else
27660 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27661
27662 /* Use cursor-in-non-selected-windows instead
27663 for non-selected window or frame. */
27664 if (non_selected)
27665 {
27666 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27667 if (!EQ (Qt, alt_cursor))
27668 return get_specified_cursor_type (alt_cursor, width);
27669 /* t means modify the normal cursor type. */
27670 if (cursor_type == FILLED_BOX_CURSOR)
27671 cursor_type = HOLLOW_BOX_CURSOR;
27672 else if (cursor_type == BAR_CURSOR && *width > 1)
27673 --*width;
27674 return cursor_type;
27675 }
27676
27677 /* Use normal cursor if not blinked off. */
27678 if (!w->cursor_off_p)
27679 {
27680 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27681 {
27682 if (cursor_type == FILLED_BOX_CURSOR)
27683 {
27684 /* Using a block cursor on large images can be very annoying.
27685 So use a hollow cursor for "large" images.
27686 If image is not transparent (no mask), also use hollow cursor. */
27687 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27688 if (img != NULL && IMAGEP (img->spec))
27689 {
27690 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27691 where N = size of default frame font size.
27692 This should cover most of the "tiny" icons people may use. */
27693 if (!img->mask
27694 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27695 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27696 cursor_type = HOLLOW_BOX_CURSOR;
27697 }
27698 }
27699 else if (cursor_type != NO_CURSOR)
27700 {
27701 /* Display current only supports BOX and HOLLOW cursors for images.
27702 So for now, unconditionally use a HOLLOW cursor when cursor is
27703 not a solid box cursor. */
27704 cursor_type = HOLLOW_BOX_CURSOR;
27705 }
27706 }
27707 return cursor_type;
27708 }
27709
27710 /* Cursor is blinked off, so determine how to "toggle" it. */
27711
27712 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27713 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27714 return get_specified_cursor_type (XCDR (alt_cursor), width);
27715
27716 /* Then see if frame has specified a specific blink off cursor type. */
27717 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27718 {
27719 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27720 return FRAME_BLINK_OFF_CURSOR (f);
27721 }
27722
27723 #if false
27724 /* Some people liked having a permanently visible blinking cursor,
27725 while others had very strong opinions against it. So it was
27726 decided to remove it. KFS 2003-09-03 */
27727
27728 /* Finally perform built-in cursor blinking:
27729 filled box <-> hollow box
27730 wide [h]bar <-> narrow [h]bar
27731 narrow [h]bar <-> no cursor
27732 other type <-> no cursor */
27733
27734 if (cursor_type == FILLED_BOX_CURSOR)
27735 return HOLLOW_BOX_CURSOR;
27736
27737 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27738 {
27739 *width = 1;
27740 return cursor_type;
27741 }
27742 #endif
27743
27744 return NO_CURSOR;
27745 }
27746
27747
27748 /* Notice when the text cursor of window W has been completely
27749 overwritten by a drawing operation that outputs glyphs in AREA
27750 starting at X0 and ending at X1 in the line starting at Y0 and
27751 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27752 the rest of the line after X0 has been written. Y coordinates
27753 are window-relative. */
27754
27755 static void
27756 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27757 int x0, int x1, int y0, int y1)
27758 {
27759 int cx0, cx1, cy0, cy1;
27760 struct glyph_row *row;
27761
27762 if (!w->phys_cursor_on_p)
27763 return;
27764 if (area != TEXT_AREA)
27765 return;
27766
27767 if (w->phys_cursor.vpos < 0
27768 || w->phys_cursor.vpos >= w->current_matrix->nrows
27769 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27770 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27771 return;
27772
27773 if (row->cursor_in_fringe_p)
27774 {
27775 row->cursor_in_fringe_p = false;
27776 draw_fringe_bitmap (w, row, row->reversed_p);
27777 w->phys_cursor_on_p = false;
27778 return;
27779 }
27780
27781 cx0 = w->phys_cursor.x;
27782 cx1 = cx0 + w->phys_cursor_width;
27783 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27784 return;
27785
27786 /* The cursor image will be completely removed from the
27787 screen if the output area intersects the cursor area in
27788 y-direction. When we draw in [y0 y1[, and some part of
27789 the cursor is at y < y0, that part must have been drawn
27790 before. When scrolling, the cursor is erased before
27791 actually scrolling, so we don't come here. When not
27792 scrolling, the rows above the old cursor row must have
27793 changed, and in this case these rows must have written
27794 over the cursor image.
27795
27796 Likewise if part of the cursor is below y1, with the
27797 exception of the cursor being in the first blank row at
27798 the buffer and window end because update_text_area
27799 doesn't draw that row. (Except when it does, but
27800 that's handled in update_text_area.) */
27801
27802 cy0 = w->phys_cursor.y;
27803 cy1 = cy0 + w->phys_cursor_height;
27804 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27805 return;
27806
27807 w->phys_cursor_on_p = false;
27808 }
27809
27810 #endif /* HAVE_WINDOW_SYSTEM */
27811
27812 \f
27813 /************************************************************************
27814 Mouse Face
27815 ************************************************************************/
27816
27817 #ifdef HAVE_WINDOW_SYSTEM
27818
27819 /* EXPORT for RIF:
27820 Fix the display of area AREA of overlapping row ROW in window W
27821 with respect to the overlapping part OVERLAPS. */
27822
27823 void
27824 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27825 enum glyph_row_area area, int overlaps)
27826 {
27827 int i, x;
27828
27829 block_input ();
27830
27831 x = 0;
27832 for (i = 0; i < row->used[area];)
27833 {
27834 if (row->glyphs[area][i].overlaps_vertically_p)
27835 {
27836 int start = i, start_x = x;
27837
27838 do
27839 {
27840 x += row->glyphs[area][i].pixel_width;
27841 ++i;
27842 }
27843 while (i < row->used[area]
27844 && row->glyphs[area][i].overlaps_vertically_p);
27845
27846 draw_glyphs (w, start_x, row, area,
27847 start, i,
27848 DRAW_NORMAL_TEXT, overlaps);
27849 }
27850 else
27851 {
27852 x += row->glyphs[area][i].pixel_width;
27853 ++i;
27854 }
27855 }
27856
27857 unblock_input ();
27858 }
27859
27860
27861 /* EXPORT:
27862 Draw the cursor glyph of window W in glyph row ROW. See the
27863 comment of draw_glyphs for the meaning of HL. */
27864
27865 void
27866 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27867 enum draw_glyphs_face hl)
27868 {
27869 /* If cursor hpos is out of bounds, don't draw garbage. This can
27870 happen in mini-buffer windows when switching between echo area
27871 glyphs and mini-buffer. */
27872 if ((row->reversed_p
27873 ? (w->phys_cursor.hpos >= 0)
27874 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27875 {
27876 bool on_p = w->phys_cursor_on_p;
27877 int x1;
27878 int hpos = w->phys_cursor.hpos;
27879
27880 /* When the window is hscrolled, cursor hpos can legitimately be
27881 out of bounds, but we draw the cursor at the corresponding
27882 window margin in that case. */
27883 if (!row->reversed_p && hpos < 0)
27884 hpos = 0;
27885 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27886 hpos = row->used[TEXT_AREA] - 1;
27887
27888 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27889 hl, 0);
27890 w->phys_cursor_on_p = on_p;
27891
27892 if (hl == DRAW_CURSOR)
27893 w->phys_cursor_width = x1 - w->phys_cursor.x;
27894 /* When we erase the cursor, and ROW is overlapped by other
27895 rows, make sure that these overlapping parts of other rows
27896 are redrawn. */
27897 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27898 {
27899 w->phys_cursor_width = x1 - w->phys_cursor.x;
27900
27901 if (row > w->current_matrix->rows
27902 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27903 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27904 OVERLAPS_ERASED_CURSOR);
27905
27906 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27907 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27908 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27909 OVERLAPS_ERASED_CURSOR);
27910 }
27911 }
27912 }
27913
27914
27915 /* Erase the image of a cursor of window W from the screen. */
27916
27917 void
27918 erase_phys_cursor (struct window *w)
27919 {
27920 struct frame *f = XFRAME (w->frame);
27921 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27922 int hpos = w->phys_cursor.hpos;
27923 int vpos = w->phys_cursor.vpos;
27924 bool mouse_face_here_p = false;
27925 struct glyph_matrix *active_glyphs = w->current_matrix;
27926 struct glyph_row *cursor_row;
27927 struct glyph *cursor_glyph;
27928 enum draw_glyphs_face hl;
27929
27930 /* No cursor displayed or row invalidated => nothing to do on the
27931 screen. */
27932 if (w->phys_cursor_type == NO_CURSOR)
27933 goto mark_cursor_off;
27934
27935 /* VPOS >= active_glyphs->nrows means that window has been resized.
27936 Don't bother to erase the cursor. */
27937 if (vpos >= active_glyphs->nrows)
27938 goto mark_cursor_off;
27939
27940 /* If row containing cursor is marked invalid, there is nothing we
27941 can do. */
27942 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27943 if (!cursor_row->enabled_p)
27944 goto mark_cursor_off;
27945
27946 /* If line spacing is > 0, old cursor may only be partially visible in
27947 window after split-window. So adjust visible height. */
27948 cursor_row->visible_height = min (cursor_row->visible_height,
27949 window_text_bottom_y (w) - cursor_row->y);
27950
27951 /* If row is completely invisible, don't attempt to delete a cursor which
27952 isn't there. This can happen if cursor is at top of a window, and
27953 we switch to a buffer with a header line in that window. */
27954 if (cursor_row->visible_height <= 0)
27955 goto mark_cursor_off;
27956
27957 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27958 if (cursor_row->cursor_in_fringe_p)
27959 {
27960 cursor_row->cursor_in_fringe_p = false;
27961 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27962 goto mark_cursor_off;
27963 }
27964
27965 /* This can happen when the new row is shorter than the old one.
27966 In this case, either draw_glyphs or clear_end_of_line
27967 should have cleared the cursor. Note that we wouldn't be
27968 able to erase the cursor in this case because we don't have a
27969 cursor glyph at hand. */
27970 if ((cursor_row->reversed_p
27971 ? (w->phys_cursor.hpos < 0)
27972 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27973 goto mark_cursor_off;
27974
27975 /* When the window is hscrolled, cursor hpos can legitimately be out
27976 of bounds, but we draw the cursor at the corresponding window
27977 margin in that case. */
27978 if (!cursor_row->reversed_p && hpos < 0)
27979 hpos = 0;
27980 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27981 hpos = cursor_row->used[TEXT_AREA] - 1;
27982
27983 /* If the cursor is in the mouse face area, redisplay that when
27984 we clear the cursor. */
27985 if (! NILP (hlinfo->mouse_face_window)
27986 && coords_in_mouse_face_p (w, hpos, vpos)
27987 /* Don't redraw the cursor's spot in mouse face if it is at the
27988 end of a line (on a newline). The cursor appears there, but
27989 mouse highlighting does not. */
27990 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27991 mouse_face_here_p = true;
27992
27993 /* Maybe clear the display under the cursor. */
27994 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27995 {
27996 int x, y;
27997 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27998 int width;
27999
28000 cursor_glyph = get_phys_cursor_glyph (w);
28001 if (cursor_glyph == NULL)
28002 goto mark_cursor_off;
28003
28004 width = cursor_glyph->pixel_width;
28005 x = w->phys_cursor.x;
28006 if (x < 0)
28007 {
28008 width += x;
28009 x = 0;
28010 }
28011 width = min (width, window_box_width (w, TEXT_AREA) - x);
28012 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28013 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28014
28015 if (width > 0)
28016 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28017 }
28018
28019 /* Erase the cursor by redrawing the character underneath it. */
28020 if (mouse_face_here_p)
28021 hl = DRAW_MOUSE_FACE;
28022 else
28023 hl = DRAW_NORMAL_TEXT;
28024 draw_phys_cursor_glyph (w, cursor_row, hl);
28025
28026 mark_cursor_off:
28027 w->phys_cursor_on_p = false;
28028 w->phys_cursor_type = NO_CURSOR;
28029 }
28030
28031
28032 /* Display or clear cursor of window W. If !ON, clear the cursor.
28033 If ON, display the cursor; where to put the cursor is specified by
28034 HPOS, VPOS, X and Y. */
28035
28036 void
28037 display_and_set_cursor (struct window *w, bool on,
28038 int hpos, int vpos, int x, int y)
28039 {
28040 struct frame *f = XFRAME (w->frame);
28041 int new_cursor_type;
28042 int new_cursor_width;
28043 bool active_cursor;
28044 struct glyph_row *glyph_row;
28045 struct glyph *glyph;
28046
28047 /* This is pointless on invisible frames, and dangerous on garbaged
28048 windows and frames; in the latter case, the frame or window may
28049 be in the midst of changing its size, and x and y may be off the
28050 window. */
28051 if (! FRAME_VISIBLE_P (f)
28052 || FRAME_GARBAGED_P (f)
28053 || vpos >= w->current_matrix->nrows
28054 || hpos >= w->current_matrix->matrix_w)
28055 return;
28056
28057 /* If cursor is off and we want it off, return quickly. */
28058 if (!on && !w->phys_cursor_on_p)
28059 return;
28060
28061 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28062 /* If cursor row is not enabled, we don't really know where to
28063 display the cursor. */
28064 if (!glyph_row->enabled_p)
28065 {
28066 w->phys_cursor_on_p = false;
28067 return;
28068 }
28069
28070 glyph = NULL;
28071 if (!glyph_row->exact_window_width_line_p
28072 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
28073 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28074
28075 eassert (input_blocked_p ());
28076
28077 /* Set new_cursor_type to the cursor we want to be displayed. */
28078 new_cursor_type = get_window_cursor_type (w, glyph,
28079 &new_cursor_width, &active_cursor);
28080
28081 /* If cursor is currently being shown and we don't want it to be or
28082 it is in the wrong place, or the cursor type is not what we want,
28083 erase it. */
28084 if (w->phys_cursor_on_p
28085 && (!on
28086 || w->phys_cursor.x != x
28087 || w->phys_cursor.y != y
28088 /* HPOS can be negative in R2L rows whose
28089 exact_window_width_line_p flag is set (i.e. their newline
28090 would "overflow into the fringe"). */
28091 || hpos < 0
28092 || new_cursor_type != w->phys_cursor_type
28093 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28094 && new_cursor_width != w->phys_cursor_width)))
28095 erase_phys_cursor (w);
28096
28097 /* Don't check phys_cursor_on_p here because that flag is only set
28098 to false in some cases where we know that the cursor has been
28099 completely erased, to avoid the extra work of erasing the cursor
28100 twice. In other words, phys_cursor_on_p can be true and the cursor
28101 still not be visible, or it has only been partly erased. */
28102 if (on)
28103 {
28104 w->phys_cursor_ascent = glyph_row->ascent;
28105 w->phys_cursor_height = glyph_row->height;
28106
28107 /* Set phys_cursor_.* before x_draw_.* is called because some
28108 of them may need the information. */
28109 w->phys_cursor.x = x;
28110 w->phys_cursor.y = glyph_row->y;
28111 w->phys_cursor.hpos = hpos;
28112 w->phys_cursor.vpos = vpos;
28113 }
28114
28115 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28116 new_cursor_type, new_cursor_width,
28117 on, active_cursor);
28118 }
28119
28120
28121 /* Switch the display of W's cursor on or off, according to the value
28122 of ON. */
28123
28124 static void
28125 update_window_cursor (struct window *w, bool on)
28126 {
28127 /* Don't update cursor in windows whose frame is in the process
28128 of being deleted. */
28129 if (w->current_matrix)
28130 {
28131 int hpos = w->phys_cursor.hpos;
28132 int vpos = w->phys_cursor.vpos;
28133 struct glyph_row *row;
28134
28135 if (vpos >= w->current_matrix->nrows
28136 || hpos >= w->current_matrix->matrix_w)
28137 return;
28138
28139 row = MATRIX_ROW (w->current_matrix, vpos);
28140
28141 /* When the window is hscrolled, cursor hpos can legitimately be
28142 out of bounds, but we draw the cursor at the corresponding
28143 window margin in that case. */
28144 if (!row->reversed_p && hpos < 0)
28145 hpos = 0;
28146 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28147 hpos = row->used[TEXT_AREA] - 1;
28148
28149 block_input ();
28150 display_and_set_cursor (w, on, hpos, vpos,
28151 w->phys_cursor.x, w->phys_cursor.y);
28152 unblock_input ();
28153 }
28154 }
28155
28156
28157 /* Call update_window_cursor with parameter ON_P on all leaf windows
28158 in the window tree rooted at W. */
28159
28160 static void
28161 update_cursor_in_window_tree (struct window *w, bool on_p)
28162 {
28163 while (w)
28164 {
28165 if (WINDOWP (w->contents))
28166 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28167 else
28168 update_window_cursor (w, on_p);
28169
28170 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28171 }
28172 }
28173
28174
28175 /* EXPORT:
28176 Display the cursor on window W, or clear it, according to ON_P.
28177 Don't change the cursor's position. */
28178
28179 void
28180 x_update_cursor (struct frame *f, bool on_p)
28181 {
28182 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28183 }
28184
28185
28186 /* EXPORT:
28187 Clear the cursor of window W to background color, and mark the
28188 cursor as not shown. This is used when the text where the cursor
28189 is about to be rewritten. */
28190
28191 void
28192 x_clear_cursor (struct window *w)
28193 {
28194 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28195 update_window_cursor (w, false);
28196 }
28197
28198 #endif /* HAVE_WINDOW_SYSTEM */
28199
28200 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28201 and MSDOS. */
28202 static void
28203 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28204 int start_hpos, int end_hpos,
28205 enum draw_glyphs_face draw)
28206 {
28207 #ifdef HAVE_WINDOW_SYSTEM
28208 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28209 {
28210 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28211 return;
28212 }
28213 #endif
28214 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28215 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28216 #endif
28217 }
28218
28219 /* Display the active region described by mouse_face_* according to DRAW. */
28220
28221 static void
28222 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28223 {
28224 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28225 struct frame *f = XFRAME (WINDOW_FRAME (w));
28226
28227 if (/* If window is in the process of being destroyed, don't bother
28228 to do anything. */
28229 w->current_matrix != NULL
28230 /* Don't update mouse highlight if hidden. */
28231 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28232 /* Recognize when we are called to operate on rows that don't exist
28233 anymore. This can happen when a window is split. */
28234 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28235 {
28236 bool phys_cursor_on_p = w->phys_cursor_on_p;
28237 struct glyph_row *row, *first, *last;
28238
28239 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28240 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28241
28242 for (row = first; row <= last && row->enabled_p; ++row)
28243 {
28244 int start_hpos, end_hpos, start_x;
28245
28246 /* For all but the first row, the highlight starts at column 0. */
28247 if (row == first)
28248 {
28249 /* R2L rows have BEG and END in reversed order, but the
28250 screen drawing geometry is always left to right. So
28251 we need to mirror the beginning and end of the
28252 highlighted area in R2L rows. */
28253 if (!row->reversed_p)
28254 {
28255 start_hpos = hlinfo->mouse_face_beg_col;
28256 start_x = hlinfo->mouse_face_beg_x;
28257 }
28258 else if (row == last)
28259 {
28260 start_hpos = hlinfo->mouse_face_end_col;
28261 start_x = hlinfo->mouse_face_end_x;
28262 }
28263 else
28264 {
28265 start_hpos = 0;
28266 start_x = 0;
28267 }
28268 }
28269 else if (row->reversed_p && row == last)
28270 {
28271 start_hpos = hlinfo->mouse_face_end_col;
28272 start_x = hlinfo->mouse_face_end_x;
28273 }
28274 else
28275 {
28276 start_hpos = 0;
28277 start_x = 0;
28278 }
28279
28280 if (row == last)
28281 {
28282 if (!row->reversed_p)
28283 end_hpos = hlinfo->mouse_face_end_col;
28284 else if (row == first)
28285 end_hpos = hlinfo->mouse_face_beg_col;
28286 else
28287 {
28288 end_hpos = row->used[TEXT_AREA];
28289 if (draw == DRAW_NORMAL_TEXT)
28290 row->fill_line_p = true; /* Clear to end of line. */
28291 }
28292 }
28293 else if (row->reversed_p && row == first)
28294 end_hpos = hlinfo->mouse_face_beg_col;
28295 else
28296 {
28297 end_hpos = row->used[TEXT_AREA];
28298 if (draw == DRAW_NORMAL_TEXT)
28299 row->fill_line_p = true; /* Clear to end of line. */
28300 }
28301
28302 if (end_hpos > start_hpos)
28303 {
28304 draw_row_with_mouse_face (w, start_x, row,
28305 start_hpos, end_hpos, draw);
28306
28307 row->mouse_face_p
28308 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28309 }
28310 }
28311
28312 #ifdef HAVE_WINDOW_SYSTEM
28313 /* When we've written over the cursor, arrange for it to
28314 be displayed again. */
28315 if (FRAME_WINDOW_P (f)
28316 && phys_cursor_on_p && !w->phys_cursor_on_p)
28317 {
28318 int hpos = w->phys_cursor.hpos;
28319
28320 /* When the window is hscrolled, cursor hpos can legitimately be
28321 out of bounds, but we draw the cursor at the corresponding
28322 window margin in that case. */
28323 if (!row->reversed_p && hpos < 0)
28324 hpos = 0;
28325 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28326 hpos = row->used[TEXT_AREA] - 1;
28327
28328 block_input ();
28329 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28330 w->phys_cursor.x, w->phys_cursor.y);
28331 unblock_input ();
28332 }
28333 #endif /* HAVE_WINDOW_SYSTEM */
28334 }
28335
28336 #ifdef HAVE_WINDOW_SYSTEM
28337 /* Change the mouse cursor. */
28338 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28339 {
28340 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28341 if (draw == DRAW_NORMAL_TEXT
28342 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28343 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28344 else
28345 #endif
28346 if (draw == DRAW_MOUSE_FACE)
28347 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28348 else
28349 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28350 }
28351 #endif /* HAVE_WINDOW_SYSTEM */
28352 }
28353
28354 /* EXPORT:
28355 Clear out the mouse-highlighted active region.
28356 Redraw it un-highlighted first. Value is true if mouse
28357 face was actually drawn unhighlighted. */
28358
28359 bool
28360 clear_mouse_face (Mouse_HLInfo *hlinfo)
28361 {
28362 bool cleared
28363 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28364 if (cleared)
28365 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28366 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28367 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28368 hlinfo->mouse_face_window = Qnil;
28369 hlinfo->mouse_face_overlay = Qnil;
28370 return cleared;
28371 }
28372
28373 /* Return true if the coordinates HPOS and VPOS on windows W are
28374 within the mouse face on that window. */
28375 static bool
28376 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28377 {
28378 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28379
28380 /* Quickly resolve the easy cases. */
28381 if (!(WINDOWP (hlinfo->mouse_face_window)
28382 && XWINDOW (hlinfo->mouse_face_window) == w))
28383 return false;
28384 if (vpos < hlinfo->mouse_face_beg_row
28385 || vpos > hlinfo->mouse_face_end_row)
28386 return false;
28387 if (vpos > hlinfo->mouse_face_beg_row
28388 && vpos < hlinfo->mouse_face_end_row)
28389 return true;
28390
28391 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28392 {
28393 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28394 {
28395 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28396 return true;
28397 }
28398 else if ((vpos == hlinfo->mouse_face_beg_row
28399 && hpos >= hlinfo->mouse_face_beg_col)
28400 || (vpos == hlinfo->mouse_face_end_row
28401 && hpos < hlinfo->mouse_face_end_col))
28402 return true;
28403 }
28404 else
28405 {
28406 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28407 {
28408 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28409 return true;
28410 }
28411 else if ((vpos == hlinfo->mouse_face_beg_row
28412 && hpos <= hlinfo->mouse_face_beg_col)
28413 || (vpos == hlinfo->mouse_face_end_row
28414 && hpos > hlinfo->mouse_face_end_col))
28415 return true;
28416 }
28417 return false;
28418 }
28419
28420
28421 /* EXPORT:
28422 True if physical cursor of window W is within mouse face. */
28423
28424 bool
28425 cursor_in_mouse_face_p (struct window *w)
28426 {
28427 int hpos = w->phys_cursor.hpos;
28428 int vpos = w->phys_cursor.vpos;
28429 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28430
28431 /* When the window is hscrolled, cursor hpos can legitimately be out
28432 of bounds, but we draw the cursor at the corresponding window
28433 margin in that case. */
28434 if (!row->reversed_p && hpos < 0)
28435 hpos = 0;
28436 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28437 hpos = row->used[TEXT_AREA] - 1;
28438
28439 return coords_in_mouse_face_p (w, hpos, vpos);
28440 }
28441
28442
28443 \f
28444 /* Find the glyph rows START_ROW and END_ROW of window W that display
28445 characters between buffer positions START_CHARPOS and END_CHARPOS
28446 (excluding END_CHARPOS). DISP_STRING is a display string that
28447 covers these buffer positions. This is similar to
28448 row_containing_pos, but is more accurate when bidi reordering makes
28449 buffer positions change non-linearly with glyph rows. */
28450 static void
28451 rows_from_pos_range (struct window *w,
28452 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28453 Lisp_Object disp_string,
28454 struct glyph_row **start, struct glyph_row **end)
28455 {
28456 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28457 int last_y = window_text_bottom_y (w);
28458 struct glyph_row *row;
28459
28460 *start = NULL;
28461 *end = NULL;
28462
28463 while (!first->enabled_p
28464 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28465 first++;
28466
28467 /* Find the START row. */
28468 for (row = first;
28469 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28470 row++)
28471 {
28472 /* A row can potentially be the START row if the range of the
28473 characters it displays intersects the range
28474 [START_CHARPOS..END_CHARPOS). */
28475 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28476 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28477 /* See the commentary in row_containing_pos, for the
28478 explanation of the complicated way to check whether
28479 some position is beyond the end of the characters
28480 displayed by a row. */
28481 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28482 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28483 && !row->ends_at_zv_p
28484 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28485 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28486 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28487 && !row->ends_at_zv_p
28488 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28489 {
28490 /* Found a candidate row. Now make sure at least one of the
28491 glyphs it displays has a charpos from the range
28492 [START_CHARPOS..END_CHARPOS).
28493
28494 This is not obvious because bidi reordering could make
28495 buffer positions of a row be 1,2,3,102,101,100, and if we
28496 want to highlight characters in [50..60), we don't want
28497 this row, even though [50..60) does intersect [1..103),
28498 the range of character positions given by the row's start
28499 and end positions. */
28500 struct glyph *g = row->glyphs[TEXT_AREA];
28501 struct glyph *e = g + row->used[TEXT_AREA];
28502
28503 while (g < e)
28504 {
28505 if (((BUFFERP (g->object) || NILP (g->object))
28506 && start_charpos <= g->charpos && g->charpos < end_charpos)
28507 /* A glyph that comes from DISP_STRING is by
28508 definition to be highlighted. */
28509 || EQ (g->object, disp_string))
28510 *start = row;
28511 g++;
28512 }
28513 if (*start)
28514 break;
28515 }
28516 }
28517
28518 /* Find the END row. */
28519 if (!*start
28520 /* If the last row is partially visible, start looking for END
28521 from that row, instead of starting from FIRST. */
28522 && !(row->enabled_p
28523 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28524 row = first;
28525 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28526 {
28527 struct glyph_row *next = row + 1;
28528 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28529
28530 if (!next->enabled_p
28531 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28532 /* The first row >= START whose range of displayed characters
28533 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28534 is the row END + 1. */
28535 || (start_charpos < next_start
28536 && end_charpos < next_start)
28537 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28538 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28539 && !next->ends_at_zv_p
28540 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28541 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28542 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28543 && !next->ends_at_zv_p
28544 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28545 {
28546 *end = row;
28547 break;
28548 }
28549 else
28550 {
28551 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28552 but none of the characters it displays are in the range, it is
28553 also END + 1. */
28554 struct glyph *g = next->glyphs[TEXT_AREA];
28555 struct glyph *s = g;
28556 struct glyph *e = g + next->used[TEXT_AREA];
28557
28558 while (g < e)
28559 {
28560 if (((BUFFERP (g->object) || NILP (g->object))
28561 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28562 /* If the buffer position of the first glyph in
28563 the row is equal to END_CHARPOS, it means
28564 the last character to be highlighted is the
28565 newline of ROW, and we must consider NEXT as
28566 END, not END+1. */
28567 || (((!next->reversed_p && g == s)
28568 || (next->reversed_p && g == e - 1))
28569 && (g->charpos == end_charpos
28570 /* Special case for when NEXT is an
28571 empty line at ZV. */
28572 || (g->charpos == -1
28573 && !row->ends_at_zv_p
28574 && next_start == end_charpos)))))
28575 /* A glyph that comes from DISP_STRING is by
28576 definition to be highlighted. */
28577 || EQ (g->object, disp_string))
28578 break;
28579 g++;
28580 }
28581 if (g == e)
28582 {
28583 *end = row;
28584 break;
28585 }
28586 /* The first row that ends at ZV must be the last to be
28587 highlighted. */
28588 else if (next->ends_at_zv_p)
28589 {
28590 *end = next;
28591 break;
28592 }
28593 }
28594 }
28595 }
28596
28597 /* This function sets the mouse_face_* elements of HLINFO, assuming
28598 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28599 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28600 for the overlay or run of text properties specifying the mouse
28601 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28602 before-string and after-string that must also be highlighted.
28603 DISP_STRING, if non-nil, is a display string that may cover some
28604 or all of the highlighted text. */
28605
28606 static void
28607 mouse_face_from_buffer_pos (Lisp_Object window,
28608 Mouse_HLInfo *hlinfo,
28609 ptrdiff_t mouse_charpos,
28610 ptrdiff_t start_charpos,
28611 ptrdiff_t end_charpos,
28612 Lisp_Object before_string,
28613 Lisp_Object after_string,
28614 Lisp_Object disp_string)
28615 {
28616 struct window *w = XWINDOW (window);
28617 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28618 struct glyph_row *r1, *r2;
28619 struct glyph *glyph, *end;
28620 ptrdiff_t ignore, pos;
28621 int x;
28622
28623 eassert (NILP (disp_string) || STRINGP (disp_string));
28624 eassert (NILP (before_string) || STRINGP (before_string));
28625 eassert (NILP (after_string) || STRINGP (after_string));
28626
28627 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28628 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28629 if (r1 == NULL)
28630 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28631 /* If the before-string or display-string contains newlines,
28632 rows_from_pos_range skips to its last row. Move back. */
28633 if (!NILP (before_string) || !NILP (disp_string))
28634 {
28635 struct glyph_row *prev;
28636 while ((prev = r1 - 1, prev >= first)
28637 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28638 && prev->used[TEXT_AREA] > 0)
28639 {
28640 struct glyph *beg = prev->glyphs[TEXT_AREA];
28641 glyph = beg + prev->used[TEXT_AREA];
28642 while (--glyph >= beg && NILP (glyph->object));
28643 if (glyph < beg
28644 || !(EQ (glyph->object, before_string)
28645 || EQ (glyph->object, disp_string)))
28646 break;
28647 r1 = prev;
28648 }
28649 }
28650 if (r2 == NULL)
28651 {
28652 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28653 hlinfo->mouse_face_past_end = true;
28654 }
28655 else if (!NILP (after_string))
28656 {
28657 /* If the after-string has newlines, advance to its last row. */
28658 struct glyph_row *next;
28659 struct glyph_row *last
28660 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28661
28662 for (next = r2 + 1;
28663 next <= last
28664 && next->used[TEXT_AREA] > 0
28665 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28666 ++next)
28667 r2 = next;
28668 }
28669 /* The rest of the display engine assumes that mouse_face_beg_row is
28670 either above mouse_face_end_row or identical to it. But with
28671 bidi-reordered continued lines, the row for START_CHARPOS could
28672 be below the row for END_CHARPOS. If so, swap the rows and store
28673 them in correct order. */
28674 if (r1->y > r2->y)
28675 {
28676 struct glyph_row *tem = r2;
28677
28678 r2 = r1;
28679 r1 = tem;
28680 }
28681
28682 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28683 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28684
28685 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28686 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28687 could be anywhere in the row and in any order. The strategy
28688 below is to find the leftmost and the rightmost glyph that
28689 belongs to either of these 3 strings, or whose position is
28690 between START_CHARPOS and END_CHARPOS, and highlight all the
28691 glyphs between those two. This may cover more than just the text
28692 between START_CHARPOS and END_CHARPOS if the range of characters
28693 strides the bidi level boundary, e.g. if the beginning is in R2L
28694 text while the end is in L2R text or vice versa. */
28695 if (!r1->reversed_p)
28696 {
28697 /* This row is in a left to right paragraph. Scan it left to
28698 right. */
28699 glyph = r1->glyphs[TEXT_AREA];
28700 end = glyph + r1->used[TEXT_AREA];
28701 x = r1->x;
28702
28703 /* Skip truncation glyphs at the start of the glyph row. */
28704 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28705 for (; glyph < end
28706 && NILP (glyph->object)
28707 && glyph->charpos < 0;
28708 ++glyph)
28709 x += glyph->pixel_width;
28710
28711 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28712 or DISP_STRING, and the first glyph from buffer whose
28713 position is between START_CHARPOS and END_CHARPOS. */
28714 for (; glyph < end
28715 && !NILP (glyph->object)
28716 && !EQ (glyph->object, disp_string)
28717 && !(BUFFERP (glyph->object)
28718 && (glyph->charpos >= start_charpos
28719 && glyph->charpos < end_charpos));
28720 ++glyph)
28721 {
28722 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28723 are present at buffer positions between START_CHARPOS and
28724 END_CHARPOS, or if they come from an overlay. */
28725 if (EQ (glyph->object, before_string))
28726 {
28727 pos = string_buffer_position (before_string,
28728 start_charpos);
28729 /* If pos == 0, it means before_string came from an
28730 overlay, not from a buffer position. */
28731 if (!pos || (pos >= start_charpos && pos < end_charpos))
28732 break;
28733 }
28734 else if (EQ (glyph->object, after_string))
28735 {
28736 pos = string_buffer_position (after_string, end_charpos);
28737 if (!pos || (pos >= start_charpos && pos < end_charpos))
28738 break;
28739 }
28740 x += glyph->pixel_width;
28741 }
28742 hlinfo->mouse_face_beg_x = x;
28743 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28744 }
28745 else
28746 {
28747 /* This row is in a right to left paragraph. Scan it right to
28748 left. */
28749 struct glyph *g;
28750
28751 end = r1->glyphs[TEXT_AREA] - 1;
28752 glyph = end + r1->used[TEXT_AREA];
28753
28754 /* Skip truncation glyphs at the start of the glyph row. */
28755 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28756 for (; glyph > end
28757 && NILP (glyph->object)
28758 && glyph->charpos < 0;
28759 --glyph)
28760 ;
28761
28762 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28763 or DISP_STRING, and the first glyph from buffer whose
28764 position is between START_CHARPOS and END_CHARPOS. */
28765 for (; glyph > end
28766 && !NILP (glyph->object)
28767 && !EQ (glyph->object, disp_string)
28768 && !(BUFFERP (glyph->object)
28769 && (glyph->charpos >= start_charpos
28770 && glyph->charpos < end_charpos));
28771 --glyph)
28772 {
28773 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28774 are present at buffer positions between START_CHARPOS and
28775 END_CHARPOS, or if they come from an overlay. */
28776 if (EQ (glyph->object, before_string))
28777 {
28778 pos = string_buffer_position (before_string, start_charpos);
28779 /* If pos == 0, it means before_string came from an
28780 overlay, not from a buffer position. */
28781 if (!pos || (pos >= start_charpos && pos < end_charpos))
28782 break;
28783 }
28784 else if (EQ (glyph->object, after_string))
28785 {
28786 pos = string_buffer_position (after_string, end_charpos);
28787 if (!pos || (pos >= start_charpos && pos < end_charpos))
28788 break;
28789 }
28790 }
28791
28792 glyph++; /* first glyph to the right of the highlighted area */
28793 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28794 x += g->pixel_width;
28795 hlinfo->mouse_face_beg_x = x;
28796 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28797 }
28798
28799 /* If the highlight ends in a different row, compute GLYPH and END
28800 for the end row. Otherwise, reuse the values computed above for
28801 the row where the highlight begins. */
28802 if (r2 != r1)
28803 {
28804 if (!r2->reversed_p)
28805 {
28806 glyph = r2->glyphs[TEXT_AREA];
28807 end = glyph + r2->used[TEXT_AREA];
28808 x = r2->x;
28809 }
28810 else
28811 {
28812 end = r2->glyphs[TEXT_AREA] - 1;
28813 glyph = end + r2->used[TEXT_AREA];
28814 }
28815 }
28816
28817 if (!r2->reversed_p)
28818 {
28819 /* Skip truncation and continuation glyphs near the end of the
28820 row, and also blanks and stretch glyphs inserted by
28821 extend_face_to_end_of_line. */
28822 while (end > glyph
28823 && NILP ((end - 1)->object))
28824 --end;
28825 /* Scan the rest of the glyph row from the end, looking for the
28826 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28827 DISP_STRING, or whose position is between START_CHARPOS
28828 and END_CHARPOS */
28829 for (--end;
28830 end > glyph
28831 && !NILP (end->object)
28832 && !EQ (end->object, disp_string)
28833 && !(BUFFERP (end->object)
28834 && (end->charpos >= start_charpos
28835 && end->charpos < end_charpos));
28836 --end)
28837 {
28838 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28839 are present at buffer positions between START_CHARPOS and
28840 END_CHARPOS, or if they come from an overlay. */
28841 if (EQ (end->object, before_string))
28842 {
28843 pos = string_buffer_position (before_string, start_charpos);
28844 if (!pos || (pos >= start_charpos && pos < end_charpos))
28845 break;
28846 }
28847 else if (EQ (end->object, after_string))
28848 {
28849 pos = string_buffer_position (after_string, end_charpos);
28850 if (!pos || (pos >= start_charpos && pos < end_charpos))
28851 break;
28852 }
28853 }
28854 /* Find the X coordinate of the last glyph to be highlighted. */
28855 for (; glyph <= end; ++glyph)
28856 x += glyph->pixel_width;
28857
28858 hlinfo->mouse_face_end_x = x;
28859 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28860 }
28861 else
28862 {
28863 /* Skip truncation and continuation glyphs near the end of the
28864 row, and also blanks and stretch glyphs inserted by
28865 extend_face_to_end_of_line. */
28866 x = r2->x;
28867 end++;
28868 while (end < glyph
28869 && NILP (end->object))
28870 {
28871 x += end->pixel_width;
28872 ++end;
28873 }
28874 /* Scan the rest of the glyph row from the end, looking for the
28875 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28876 DISP_STRING, or whose position is between START_CHARPOS
28877 and END_CHARPOS */
28878 for ( ;
28879 end < glyph
28880 && !NILP (end->object)
28881 && !EQ (end->object, disp_string)
28882 && !(BUFFERP (end->object)
28883 && (end->charpos >= start_charpos
28884 && end->charpos < end_charpos));
28885 ++end)
28886 {
28887 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28888 are present at buffer positions between START_CHARPOS and
28889 END_CHARPOS, or if they come from an overlay. */
28890 if (EQ (end->object, before_string))
28891 {
28892 pos = string_buffer_position (before_string, start_charpos);
28893 if (!pos || (pos >= start_charpos && pos < end_charpos))
28894 break;
28895 }
28896 else if (EQ (end->object, after_string))
28897 {
28898 pos = string_buffer_position (after_string, end_charpos);
28899 if (!pos || (pos >= start_charpos && pos < end_charpos))
28900 break;
28901 }
28902 x += end->pixel_width;
28903 }
28904 /* If we exited the above loop because we arrived at the last
28905 glyph of the row, and its buffer position is still not in
28906 range, it means the last character in range is the preceding
28907 newline. Bump the end column and x values to get past the
28908 last glyph. */
28909 if (end == glyph
28910 && BUFFERP (end->object)
28911 && (end->charpos < start_charpos
28912 || end->charpos >= end_charpos))
28913 {
28914 x += end->pixel_width;
28915 ++end;
28916 }
28917 hlinfo->mouse_face_end_x = x;
28918 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28919 }
28920
28921 hlinfo->mouse_face_window = window;
28922 hlinfo->mouse_face_face_id
28923 = face_at_buffer_position (w, mouse_charpos, &ignore,
28924 mouse_charpos + 1,
28925 !hlinfo->mouse_face_hidden, -1);
28926 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28927 }
28928
28929 /* The following function is not used anymore (replaced with
28930 mouse_face_from_string_pos), but I leave it here for the time
28931 being, in case someone would. */
28932
28933 #if false /* not used */
28934
28935 /* Find the position of the glyph for position POS in OBJECT in
28936 window W's current matrix, and return in *X, *Y the pixel
28937 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28938
28939 RIGHT_P means return the position of the right edge of the glyph.
28940 !RIGHT_P means return the left edge position.
28941
28942 If no glyph for POS exists in the matrix, return the position of
28943 the glyph with the next smaller position that is in the matrix, if
28944 RIGHT_P is false. If RIGHT_P, and no glyph for POS
28945 exists in the matrix, return the position of the glyph with the
28946 next larger position in OBJECT.
28947
28948 Value is true if a glyph was found. */
28949
28950 static bool
28951 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28952 int *hpos, int *vpos, int *x, int *y, bool right_p)
28953 {
28954 int yb = window_text_bottom_y (w);
28955 struct glyph_row *r;
28956 struct glyph *best_glyph = NULL;
28957 struct glyph_row *best_row = NULL;
28958 int best_x = 0;
28959
28960 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28961 r->enabled_p && r->y < yb;
28962 ++r)
28963 {
28964 struct glyph *g = r->glyphs[TEXT_AREA];
28965 struct glyph *e = g + r->used[TEXT_AREA];
28966 int gx;
28967
28968 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28969 if (EQ (g->object, object))
28970 {
28971 if (g->charpos == pos)
28972 {
28973 best_glyph = g;
28974 best_x = gx;
28975 best_row = r;
28976 goto found;
28977 }
28978 else if (best_glyph == NULL
28979 || ((eabs (g->charpos - pos)
28980 < eabs (best_glyph->charpos - pos))
28981 && (right_p
28982 ? g->charpos < pos
28983 : g->charpos > pos)))
28984 {
28985 best_glyph = g;
28986 best_x = gx;
28987 best_row = r;
28988 }
28989 }
28990 }
28991
28992 found:
28993
28994 if (best_glyph)
28995 {
28996 *x = best_x;
28997 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28998
28999 if (right_p)
29000 {
29001 *x += best_glyph->pixel_width;
29002 ++*hpos;
29003 }
29004
29005 *y = best_row->y;
29006 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29007 }
29008
29009 return best_glyph != NULL;
29010 }
29011 #endif /* not used */
29012
29013 /* Find the positions of the first and the last glyphs in window W's
29014 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29015 (assumed to be a string), and return in HLINFO's mouse_face_*
29016 members the pixel and column/row coordinates of those glyphs. */
29017
29018 static void
29019 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29020 Lisp_Object object,
29021 ptrdiff_t startpos, ptrdiff_t endpos)
29022 {
29023 int yb = window_text_bottom_y (w);
29024 struct glyph_row *r;
29025 struct glyph *g, *e;
29026 int gx;
29027 bool found = false;
29028
29029 /* Find the glyph row with at least one position in the range
29030 [STARTPOS..ENDPOS), and the first glyph in that row whose
29031 position belongs to that range. */
29032 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29033 r->enabled_p && r->y < yb;
29034 ++r)
29035 {
29036 if (!r->reversed_p)
29037 {
29038 g = r->glyphs[TEXT_AREA];
29039 e = g + r->used[TEXT_AREA];
29040 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29041 if (EQ (g->object, object)
29042 && startpos <= g->charpos && g->charpos < endpos)
29043 {
29044 hlinfo->mouse_face_beg_row
29045 = MATRIX_ROW_VPOS (r, w->current_matrix);
29046 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29047 hlinfo->mouse_face_beg_x = gx;
29048 found = true;
29049 break;
29050 }
29051 }
29052 else
29053 {
29054 struct glyph *g1;
29055
29056 e = r->glyphs[TEXT_AREA];
29057 g = e + r->used[TEXT_AREA];
29058 for ( ; g > e; --g)
29059 if (EQ ((g-1)->object, object)
29060 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29061 {
29062 hlinfo->mouse_face_beg_row
29063 = MATRIX_ROW_VPOS (r, w->current_matrix);
29064 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29065 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29066 gx += g1->pixel_width;
29067 hlinfo->mouse_face_beg_x = gx;
29068 found = true;
29069 break;
29070 }
29071 }
29072 if (found)
29073 break;
29074 }
29075
29076 if (!found)
29077 return;
29078
29079 /* Starting with the next row, look for the first row which does NOT
29080 include any glyphs whose positions are in the range. */
29081 for (++r; r->enabled_p && r->y < yb; ++r)
29082 {
29083 g = r->glyphs[TEXT_AREA];
29084 e = g + r->used[TEXT_AREA];
29085 found = false;
29086 for ( ; g < e; ++g)
29087 if (EQ (g->object, object)
29088 && startpos <= g->charpos && g->charpos < endpos)
29089 {
29090 found = true;
29091 break;
29092 }
29093 if (!found)
29094 break;
29095 }
29096
29097 /* The highlighted region ends on the previous row. */
29098 r--;
29099
29100 /* Set the end row. */
29101 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29102
29103 /* Compute and set the end column and the end column's horizontal
29104 pixel coordinate. */
29105 if (!r->reversed_p)
29106 {
29107 g = r->glyphs[TEXT_AREA];
29108 e = g + r->used[TEXT_AREA];
29109 for ( ; e > g; --e)
29110 if (EQ ((e-1)->object, object)
29111 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29112 break;
29113 hlinfo->mouse_face_end_col = e - g;
29114
29115 for (gx = r->x; g < e; ++g)
29116 gx += g->pixel_width;
29117 hlinfo->mouse_face_end_x = gx;
29118 }
29119 else
29120 {
29121 e = r->glyphs[TEXT_AREA];
29122 g = e + r->used[TEXT_AREA];
29123 for (gx = r->x ; e < g; ++e)
29124 {
29125 if (EQ (e->object, object)
29126 && startpos <= e->charpos && e->charpos < endpos)
29127 break;
29128 gx += e->pixel_width;
29129 }
29130 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29131 hlinfo->mouse_face_end_x = gx;
29132 }
29133 }
29134
29135 #ifdef HAVE_WINDOW_SYSTEM
29136
29137 /* See if position X, Y is within a hot-spot of an image. */
29138
29139 static bool
29140 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29141 {
29142 if (!CONSP (hot_spot))
29143 return false;
29144
29145 if (EQ (XCAR (hot_spot), Qrect))
29146 {
29147 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29148 Lisp_Object rect = XCDR (hot_spot);
29149 Lisp_Object tem;
29150 if (!CONSP (rect))
29151 return false;
29152 if (!CONSP (XCAR (rect)))
29153 return false;
29154 if (!CONSP (XCDR (rect)))
29155 return false;
29156 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29157 return false;
29158 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29159 return false;
29160 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29161 return false;
29162 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29163 return false;
29164 return true;
29165 }
29166 else if (EQ (XCAR (hot_spot), Qcircle))
29167 {
29168 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29169 Lisp_Object circ = XCDR (hot_spot);
29170 Lisp_Object lr, lx0, ly0;
29171 if (CONSP (circ)
29172 && CONSP (XCAR (circ))
29173 && (lr = XCDR (circ), NUMBERP (lr))
29174 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29175 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29176 {
29177 double r = XFLOATINT (lr);
29178 double dx = XINT (lx0) - x;
29179 double dy = XINT (ly0) - y;
29180 return (dx * dx + dy * dy <= r * r);
29181 }
29182 }
29183 else if (EQ (XCAR (hot_spot), Qpoly))
29184 {
29185 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29186 if (VECTORP (XCDR (hot_spot)))
29187 {
29188 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29189 Lisp_Object *poly = v->contents;
29190 ptrdiff_t n = v->header.size;
29191 ptrdiff_t i;
29192 bool inside = false;
29193 Lisp_Object lx, ly;
29194 int x0, y0;
29195
29196 /* Need an even number of coordinates, and at least 3 edges. */
29197 if (n < 6 || n & 1)
29198 return false;
29199
29200 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29201 If count is odd, we are inside polygon. Pixels on edges
29202 may or may not be included depending on actual geometry of the
29203 polygon. */
29204 if ((lx = poly[n-2], !INTEGERP (lx))
29205 || (ly = poly[n-1], !INTEGERP (lx)))
29206 return false;
29207 x0 = XINT (lx), y0 = XINT (ly);
29208 for (i = 0; i < n; i += 2)
29209 {
29210 int x1 = x0, y1 = y0;
29211 if ((lx = poly[i], !INTEGERP (lx))
29212 || (ly = poly[i+1], !INTEGERP (ly)))
29213 return false;
29214 x0 = XINT (lx), y0 = XINT (ly);
29215
29216 /* Does this segment cross the X line? */
29217 if (x0 >= x)
29218 {
29219 if (x1 >= x)
29220 continue;
29221 }
29222 else if (x1 < x)
29223 continue;
29224 if (y > y0 && y > y1)
29225 continue;
29226 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29227 inside = !inside;
29228 }
29229 return inside;
29230 }
29231 }
29232 return false;
29233 }
29234
29235 Lisp_Object
29236 find_hot_spot (Lisp_Object map, int x, int y)
29237 {
29238 while (CONSP (map))
29239 {
29240 if (CONSP (XCAR (map))
29241 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29242 return XCAR (map);
29243 map = XCDR (map);
29244 }
29245
29246 return Qnil;
29247 }
29248
29249 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29250 3, 3, 0,
29251 doc: /* Lookup in image map MAP coordinates X and Y.
29252 An image map is an alist where each element has the format (AREA ID PLIST).
29253 An AREA is specified as either a rectangle, a circle, or a polygon:
29254 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29255 pixel coordinates of the upper left and bottom right corners.
29256 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29257 and the radius of the circle; r may be a float or integer.
29258 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29259 vector describes one corner in the polygon.
29260 Returns the alist element for the first matching AREA in MAP. */)
29261 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29262 {
29263 if (NILP (map))
29264 return Qnil;
29265
29266 CHECK_NUMBER (x);
29267 CHECK_NUMBER (y);
29268
29269 return find_hot_spot (map,
29270 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29271 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29272 }
29273
29274
29275 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29276 static void
29277 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29278 {
29279 /* Do not change cursor shape while dragging mouse. */
29280 if (EQ (do_mouse_tracking, Qdragging))
29281 return;
29282
29283 if (!NILP (pointer))
29284 {
29285 if (EQ (pointer, Qarrow))
29286 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29287 else if (EQ (pointer, Qhand))
29288 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29289 else if (EQ (pointer, Qtext))
29290 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29291 else if (EQ (pointer, intern ("hdrag")))
29292 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29293 else if (EQ (pointer, intern ("nhdrag")))
29294 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29295 #ifdef HAVE_X_WINDOWS
29296 else if (EQ (pointer, intern ("vdrag")))
29297 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29298 #endif
29299 else if (EQ (pointer, intern ("hourglass")))
29300 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29301 else if (EQ (pointer, Qmodeline))
29302 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29303 else
29304 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29305 }
29306
29307 if (cursor != No_Cursor)
29308 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29309 }
29310
29311 #endif /* HAVE_WINDOW_SYSTEM */
29312
29313 /* Take proper action when mouse has moved to the mode or header line
29314 or marginal area AREA of window W, x-position X and y-position Y.
29315 X is relative to the start of the text display area of W, so the
29316 width of bitmap areas and scroll bars must be subtracted to get a
29317 position relative to the start of the mode line. */
29318
29319 static void
29320 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29321 enum window_part area)
29322 {
29323 struct window *w = XWINDOW (window);
29324 struct frame *f = XFRAME (w->frame);
29325 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29326 #ifdef HAVE_WINDOW_SYSTEM
29327 Display_Info *dpyinfo;
29328 #endif
29329 Cursor cursor = No_Cursor;
29330 Lisp_Object pointer = Qnil;
29331 int dx, dy, width, height;
29332 ptrdiff_t charpos;
29333 Lisp_Object string, object = Qnil;
29334 Lisp_Object pos IF_LINT (= Qnil), help;
29335
29336 Lisp_Object mouse_face;
29337 int original_x_pixel = x;
29338 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29339 struct glyph_row *row IF_LINT (= 0);
29340
29341 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29342 {
29343 int x0;
29344 struct glyph *end;
29345
29346 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29347 returns them in row/column units! */
29348 string = mode_line_string (w, area, &x, &y, &charpos,
29349 &object, &dx, &dy, &width, &height);
29350
29351 row = (area == ON_MODE_LINE
29352 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29353 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29354
29355 /* Find the glyph under the mouse pointer. */
29356 if (row->mode_line_p && row->enabled_p)
29357 {
29358 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29359 end = glyph + row->used[TEXT_AREA];
29360
29361 for (x0 = original_x_pixel;
29362 glyph < end && x0 >= glyph->pixel_width;
29363 ++glyph)
29364 x0 -= glyph->pixel_width;
29365
29366 if (glyph >= end)
29367 glyph = NULL;
29368 }
29369 }
29370 else
29371 {
29372 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29373 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29374 returns them in row/column units! */
29375 string = marginal_area_string (w, area, &x, &y, &charpos,
29376 &object, &dx, &dy, &width, &height);
29377 }
29378
29379 help = Qnil;
29380
29381 #ifdef HAVE_WINDOW_SYSTEM
29382 if (IMAGEP (object))
29383 {
29384 Lisp_Object image_map, hotspot;
29385 if ((image_map = Fplist_get (XCDR (object), QCmap),
29386 !NILP (image_map))
29387 && (hotspot = find_hot_spot (image_map, dx, dy),
29388 CONSP (hotspot))
29389 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29390 {
29391 Lisp_Object plist;
29392
29393 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29394 If so, we could look for mouse-enter, mouse-leave
29395 properties in PLIST (and do something...). */
29396 hotspot = XCDR (hotspot);
29397 if (CONSP (hotspot)
29398 && (plist = XCAR (hotspot), CONSP (plist)))
29399 {
29400 pointer = Fplist_get (plist, Qpointer);
29401 if (NILP (pointer))
29402 pointer = Qhand;
29403 help = Fplist_get (plist, Qhelp_echo);
29404 if (!NILP (help))
29405 {
29406 help_echo_string = help;
29407 XSETWINDOW (help_echo_window, w);
29408 help_echo_object = w->contents;
29409 help_echo_pos = charpos;
29410 }
29411 }
29412 }
29413 if (NILP (pointer))
29414 pointer = Fplist_get (XCDR (object), QCpointer);
29415 }
29416 #endif /* HAVE_WINDOW_SYSTEM */
29417
29418 if (STRINGP (string))
29419 pos = make_number (charpos);
29420
29421 /* Set the help text and mouse pointer. If the mouse is on a part
29422 of the mode line without any text (e.g. past the right edge of
29423 the mode line text), use the default help text and pointer. */
29424 if (STRINGP (string) || area == ON_MODE_LINE)
29425 {
29426 /* Arrange to display the help by setting the global variables
29427 help_echo_string, help_echo_object, and help_echo_pos. */
29428 if (NILP (help))
29429 {
29430 if (STRINGP (string))
29431 help = Fget_text_property (pos, Qhelp_echo, string);
29432
29433 if (!NILP (help))
29434 {
29435 help_echo_string = help;
29436 XSETWINDOW (help_echo_window, w);
29437 help_echo_object = string;
29438 help_echo_pos = charpos;
29439 }
29440 else if (area == ON_MODE_LINE)
29441 {
29442 Lisp_Object default_help
29443 = buffer_local_value (Qmode_line_default_help_echo,
29444 w->contents);
29445
29446 if (STRINGP (default_help))
29447 {
29448 help_echo_string = default_help;
29449 XSETWINDOW (help_echo_window, w);
29450 help_echo_object = Qnil;
29451 help_echo_pos = -1;
29452 }
29453 }
29454 }
29455
29456 #ifdef HAVE_WINDOW_SYSTEM
29457 /* Change the mouse pointer according to what is under it. */
29458 if (FRAME_WINDOW_P (f))
29459 {
29460 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29461 || minibuf_level
29462 || NILP (Vresize_mini_windows));
29463
29464 dpyinfo = FRAME_DISPLAY_INFO (f);
29465 if (STRINGP (string))
29466 {
29467 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29468
29469 if (NILP (pointer))
29470 pointer = Fget_text_property (pos, Qpointer, string);
29471
29472 /* Change the mouse pointer according to what is under X/Y. */
29473 if (NILP (pointer)
29474 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29475 {
29476 Lisp_Object map;
29477 map = Fget_text_property (pos, Qlocal_map, string);
29478 if (!KEYMAPP (map))
29479 map = Fget_text_property (pos, Qkeymap, string);
29480 if (!KEYMAPP (map) && draggable)
29481 cursor = dpyinfo->vertical_scroll_bar_cursor;
29482 }
29483 }
29484 else if (draggable)
29485 /* Default mode-line pointer. */
29486 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29487 }
29488 #endif
29489 }
29490
29491 /* Change the mouse face according to what is under X/Y. */
29492 bool mouse_face_shown = false;
29493 if (STRINGP (string))
29494 {
29495 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29496 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29497 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29498 && glyph)
29499 {
29500 Lisp_Object b, e;
29501
29502 struct glyph * tmp_glyph;
29503
29504 int gpos;
29505 int gseq_length;
29506 int total_pixel_width;
29507 ptrdiff_t begpos, endpos, ignore;
29508
29509 int vpos, hpos;
29510
29511 b = Fprevious_single_property_change (make_number (charpos + 1),
29512 Qmouse_face, string, Qnil);
29513 if (NILP (b))
29514 begpos = 0;
29515 else
29516 begpos = XINT (b);
29517
29518 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29519 if (NILP (e))
29520 endpos = SCHARS (string);
29521 else
29522 endpos = XINT (e);
29523
29524 /* Calculate the glyph position GPOS of GLYPH in the
29525 displayed string, relative to the beginning of the
29526 highlighted part of the string.
29527
29528 Note: GPOS is different from CHARPOS. CHARPOS is the
29529 position of GLYPH in the internal string object. A mode
29530 line string format has structures which are converted to
29531 a flattened string by the Emacs Lisp interpreter. The
29532 internal string is an element of those structures. The
29533 displayed string is the flattened string. */
29534 tmp_glyph = row_start_glyph;
29535 while (tmp_glyph < glyph
29536 && (!(EQ (tmp_glyph->object, glyph->object)
29537 && begpos <= tmp_glyph->charpos
29538 && tmp_glyph->charpos < endpos)))
29539 tmp_glyph++;
29540 gpos = glyph - tmp_glyph;
29541
29542 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29543 the highlighted part of the displayed string to which
29544 GLYPH belongs. Note: GSEQ_LENGTH is different from
29545 SCHARS (STRING), because the latter returns the length of
29546 the internal string. */
29547 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29548 tmp_glyph > glyph
29549 && (!(EQ (tmp_glyph->object, glyph->object)
29550 && begpos <= tmp_glyph->charpos
29551 && tmp_glyph->charpos < endpos));
29552 tmp_glyph--)
29553 ;
29554 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29555
29556 /* Calculate the total pixel width of all the glyphs between
29557 the beginning of the highlighted area and GLYPH. */
29558 total_pixel_width = 0;
29559 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29560 total_pixel_width += tmp_glyph->pixel_width;
29561
29562 /* Pre calculation of re-rendering position. Note: X is in
29563 column units here, after the call to mode_line_string or
29564 marginal_area_string. */
29565 hpos = x - gpos;
29566 vpos = (area == ON_MODE_LINE
29567 ? (w->current_matrix)->nrows - 1
29568 : 0);
29569
29570 /* If GLYPH's position is included in the region that is
29571 already drawn in mouse face, we have nothing to do. */
29572 if ( EQ (window, hlinfo->mouse_face_window)
29573 && (!row->reversed_p
29574 ? (hlinfo->mouse_face_beg_col <= hpos
29575 && hpos < hlinfo->mouse_face_end_col)
29576 /* In R2L rows we swap BEG and END, see below. */
29577 : (hlinfo->mouse_face_end_col <= hpos
29578 && hpos < hlinfo->mouse_face_beg_col))
29579 && hlinfo->mouse_face_beg_row == vpos )
29580 return;
29581
29582 if (clear_mouse_face (hlinfo))
29583 cursor = No_Cursor;
29584
29585 if (!row->reversed_p)
29586 {
29587 hlinfo->mouse_face_beg_col = hpos;
29588 hlinfo->mouse_face_beg_x = original_x_pixel
29589 - (total_pixel_width + dx);
29590 hlinfo->mouse_face_end_col = hpos + gseq_length;
29591 hlinfo->mouse_face_end_x = 0;
29592 }
29593 else
29594 {
29595 /* In R2L rows, show_mouse_face expects BEG and END
29596 coordinates to be swapped. */
29597 hlinfo->mouse_face_end_col = hpos;
29598 hlinfo->mouse_face_end_x = original_x_pixel
29599 - (total_pixel_width + dx);
29600 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29601 hlinfo->mouse_face_beg_x = 0;
29602 }
29603
29604 hlinfo->mouse_face_beg_row = vpos;
29605 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29606 hlinfo->mouse_face_past_end = false;
29607 hlinfo->mouse_face_window = window;
29608
29609 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29610 charpos,
29611 0, &ignore,
29612 glyph->face_id,
29613 true);
29614 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29615 mouse_face_shown = true;
29616
29617 if (NILP (pointer))
29618 pointer = Qhand;
29619 }
29620 }
29621
29622 /* If mouse-face doesn't need to be shown, clear any existing
29623 mouse-face. */
29624 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
29625 clear_mouse_face (hlinfo);
29626
29627 #ifdef HAVE_WINDOW_SYSTEM
29628 if (FRAME_WINDOW_P (f))
29629 define_frame_cursor1 (f, cursor, pointer);
29630 #endif
29631 }
29632
29633
29634 /* EXPORT:
29635 Take proper action when the mouse has moved to position X, Y on
29636 frame F with regards to highlighting portions of display that have
29637 mouse-face properties. Also de-highlight portions of display where
29638 the mouse was before, set the mouse pointer shape as appropriate
29639 for the mouse coordinates, and activate help echo (tooltips).
29640 X and Y can be negative or out of range. */
29641
29642 void
29643 note_mouse_highlight (struct frame *f, int x, int y)
29644 {
29645 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29646 enum window_part part = ON_NOTHING;
29647 Lisp_Object window;
29648 struct window *w;
29649 Cursor cursor = No_Cursor;
29650 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29651 struct buffer *b;
29652
29653 /* When a menu is active, don't highlight because this looks odd. */
29654 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29655 if (popup_activated ())
29656 return;
29657 #endif
29658
29659 if (!f->glyphs_initialized_p
29660 || f->pointer_invisible)
29661 return;
29662
29663 hlinfo->mouse_face_mouse_x = x;
29664 hlinfo->mouse_face_mouse_y = y;
29665 hlinfo->mouse_face_mouse_frame = f;
29666
29667 if (hlinfo->mouse_face_defer)
29668 return;
29669
29670 /* Which window is that in? */
29671 window = window_from_coordinates (f, x, y, &part, true);
29672
29673 /* If displaying active text in another window, clear that. */
29674 if (! EQ (window, hlinfo->mouse_face_window)
29675 /* Also clear if we move out of text area in same window. */
29676 || (!NILP (hlinfo->mouse_face_window)
29677 && !NILP (window)
29678 && part != ON_TEXT
29679 && part != ON_MODE_LINE
29680 && part != ON_HEADER_LINE))
29681 clear_mouse_face (hlinfo);
29682
29683 /* Not on a window -> return. */
29684 if (!WINDOWP (window))
29685 return;
29686
29687 /* Reset help_echo_string. It will get recomputed below. */
29688 help_echo_string = Qnil;
29689
29690 /* Convert to window-relative pixel coordinates. */
29691 w = XWINDOW (window);
29692 frame_to_window_pixel_xy (w, &x, &y);
29693
29694 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29695 /* Handle tool-bar window differently since it doesn't display a
29696 buffer. */
29697 if (EQ (window, f->tool_bar_window))
29698 {
29699 note_tool_bar_highlight (f, x, y);
29700 return;
29701 }
29702 #endif
29703
29704 /* Mouse is on the mode, header line or margin? */
29705 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29706 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29707 {
29708 note_mode_line_or_margin_highlight (window, x, y, part);
29709
29710 #ifdef HAVE_WINDOW_SYSTEM
29711 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29712 {
29713 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29714 /* Show non-text cursor (Bug#16647). */
29715 goto set_cursor;
29716 }
29717 else
29718 #endif
29719 return;
29720 }
29721
29722 #ifdef HAVE_WINDOW_SYSTEM
29723 if (part == ON_VERTICAL_BORDER)
29724 {
29725 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29726 help_echo_string = build_string ("drag-mouse-1: resize");
29727 }
29728 else if (part == ON_RIGHT_DIVIDER)
29729 {
29730 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29731 help_echo_string = build_string ("drag-mouse-1: resize");
29732 }
29733 else if (part == ON_BOTTOM_DIVIDER)
29734 if (! WINDOW_BOTTOMMOST_P (w)
29735 || minibuf_level
29736 || NILP (Vresize_mini_windows))
29737 {
29738 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29739 help_echo_string = build_string ("drag-mouse-1: resize");
29740 }
29741 else
29742 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29743 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29744 || part == ON_VERTICAL_SCROLL_BAR
29745 || part == ON_HORIZONTAL_SCROLL_BAR)
29746 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29747 else
29748 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29749 #endif
29750
29751 /* Are we in a window whose display is up to date?
29752 And verify the buffer's text has not changed. */
29753 b = XBUFFER (w->contents);
29754 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29755 {
29756 int hpos, vpos, dx, dy, area = LAST_AREA;
29757 ptrdiff_t pos;
29758 struct glyph *glyph;
29759 Lisp_Object object;
29760 Lisp_Object mouse_face = Qnil, position;
29761 Lisp_Object *overlay_vec = NULL;
29762 ptrdiff_t i, noverlays;
29763 struct buffer *obuf;
29764 ptrdiff_t obegv, ozv;
29765 bool same_region;
29766
29767 /* Find the glyph under X/Y. */
29768 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29769
29770 #ifdef HAVE_WINDOW_SYSTEM
29771 /* Look for :pointer property on image. */
29772 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29773 {
29774 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29775 if (img != NULL && IMAGEP (img->spec))
29776 {
29777 Lisp_Object image_map, hotspot;
29778 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29779 !NILP (image_map))
29780 && (hotspot = find_hot_spot (image_map,
29781 glyph->slice.img.x + dx,
29782 glyph->slice.img.y + dy),
29783 CONSP (hotspot))
29784 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29785 {
29786 Lisp_Object plist;
29787
29788 /* Could check XCAR (hotspot) to see if we enter/leave
29789 this hot-spot.
29790 If so, we could look for mouse-enter, mouse-leave
29791 properties in PLIST (and do something...). */
29792 hotspot = XCDR (hotspot);
29793 if (CONSP (hotspot)
29794 && (plist = XCAR (hotspot), CONSP (plist)))
29795 {
29796 pointer = Fplist_get (plist, Qpointer);
29797 if (NILP (pointer))
29798 pointer = Qhand;
29799 help_echo_string = Fplist_get (plist, Qhelp_echo);
29800 if (!NILP (help_echo_string))
29801 {
29802 help_echo_window = window;
29803 help_echo_object = glyph->object;
29804 help_echo_pos = glyph->charpos;
29805 }
29806 }
29807 }
29808 if (NILP (pointer))
29809 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29810 }
29811 }
29812 #endif /* HAVE_WINDOW_SYSTEM */
29813
29814 /* Clear mouse face if X/Y not over text. */
29815 if (glyph == NULL
29816 || area != TEXT_AREA
29817 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29818 /* Glyph's OBJECT is nil for glyphs inserted by the
29819 display engine for its internal purposes, like truncation
29820 and continuation glyphs and blanks beyond the end of
29821 line's text on text terminals. If we are over such a
29822 glyph, we are not over any text. */
29823 || NILP (glyph->object)
29824 /* R2L rows have a stretch glyph at their front, which
29825 stands for no text, whereas L2R rows have no glyphs at
29826 all beyond the end of text. Treat such stretch glyphs
29827 like we do with NULL glyphs in L2R rows. */
29828 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29829 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29830 && glyph->type == STRETCH_GLYPH
29831 && glyph->avoid_cursor_p))
29832 {
29833 if (clear_mouse_face (hlinfo))
29834 cursor = No_Cursor;
29835 #ifdef HAVE_WINDOW_SYSTEM
29836 if (FRAME_WINDOW_P (f) && NILP (pointer))
29837 {
29838 if (area != TEXT_AREA)
29839 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29840 else
29841 pointer = Vvoid_text_area_pointer;
29842 }
29843 #endif
29844 goto set_cursor;
29845 }
29846
29847 pos = glyph->charpos;
29848 object = glyph->object;
29849 if (!STRINGP (object) && !BUFFERP (object))
29850 goto set_cursor;
29851
29852 /* If we get an out-of-range value, return now; avoid an error. */
29853 if (BUFFERP (object) && pos > BUF_Z (b))
29854 goto set_cursor;
29855
29856 /* Make the window's buffer temporarily current for
29857 overlays_at and compute_char_face. */
29858 obuf = current_buffer;
29859 current_buffer = b;
29860 obegv = BEGV;
29861 ozv = ZV;
29862 BEGV = BEG;
29863 ZV = Z;
29864
29865 /* Is this char mouse-active or does it have help-echo? */
29866 position = make_number (pos);
29867
29868 USE_SAFE_ALLOCA;
29869
29870 if (BUFFERP (object))
29871 {
29872 /* Put all the overlays we want in a vector in overlay_vec. */
29873 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
29874 /* Sort overlays into increasing priority order. */
29875 noverlays = sort_overlays (overlay_vec, noverlays, w);
29876 }
29877 else
29878 noverlays = 0;
29879
29880 if (NILP (Vmouse_highlight))
29881 {
29882 clear_mouse_face (hlinfo);
29883 goto check_help_echo;
29884 }
29885
29886 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29887
29888 if (same_region)
29889 cursor = No_Cursor;
29890
29891 /* Check mouse-face highlighting. */
29892 if (! same_region
29893 /* If there exists an overlay with mouse-face overlapping
29894 the one we are currently highlighting, we have to
29895 check if we enter the overlapping overlay, and then
29896 highlight only that. */
29897 || (OVERLAYP (hlinfo->mouse_face_overlay)
29898 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29899 {
29900 /* Find the highest priority overlay with a mouse-face. */
29901 Lisp_Object overlay = Qnil;
29902 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29903 {
29904 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29905 if (!NILP (mouse_face))
29906 overlay = overlay_vec[i];
29907 }
29908
29909 /* If we're highlighting the same overlay as before, there's
29910 no need to do that again. */
29911 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29912 goto check_help_echo;
29913 hlinfo->mouse_face_overlay = overlay;
29914
29915 /* Clear the display of the old active region, if any. */
29916 if (clear_mouse_face (hlinfo))
29917 cursor = No_Cursor;
29918
29919 /* If no overlay applies, get a text property. */
29920 if (NILP (overlay))
29921 mouse_face = Fget_text_property (position, Qmouse_face, object);
29922
29923 /* Next, compute the bounds of the mouse highlighting and
29924 display it. */
29925 if (!NILP (mouse_face) && STRINGP (object))
29926 {
29927 /* The mouse-highlighting comes from a display string
29928 with a mouse-face. */
29929 Lisp_Object s, e;
29930 ptrdiff_t ignore;
29931
29932 s = Fprevious_single_property_change
29933 (make_number (pos + 1), Qmouse_face, object, Qnil);
29934 e = Fnext_single_property_change
29935 (position, Qmouse_face, object, Qnil);
29936 if (NILP (s))
29937 s = make_number (0);
29938 if (NILP (e))
29939 e = make_number (SCHARS (object));
29940 mouse_face_from_string_pos (w, hlinfo, object,
29941 XINT (s), XINT (e));
29942 hlinfo->mouse_face_past_end = false;
29943 hlinfo->mouse_face_window = window;
29944 hlinfo->mouse_face_face_id
29945 = face_at_string_position (w, object, pos, 0, &ignore,
29946 glyph->face_id, true);
29947 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29948 cursor = No_Cursor;
29949 }
29950 else
29951 {
29952 /* The mouse-highlighting, if any, comes from an overlay
29953 or text property in the buffer. */
29954 Lisp_Object buffer IF_LINT (= Qnil);
29955 Lisp_Object disp_string IF_LINT (= Qnil);
29956
29957 if (STRINGP (object))
29958 {
29959 /* If we are on a display string with no mouse-face,
29960 check if the text under it has one. */
29961 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29962 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29963 pos = string_buffer_position (object, start);
29964 if (pos > 0)
29965 {
29966 mouse_face = get_char_property_and_overlay
29967 (make_number (pos), Qmouse_face, w->contents, &overlay);
29968 buffer = w->contents;
29969 disp_string = object;
29970 }
29971 }
29972 else
29973 {
29974 buffer = object;
29975 disp_string = Qnil;
29976 }
29977
29978 if (!NILP (mouse_face))
29979 {
29980 Lisp_Object before, after;
29981 Lisp_Object before_string, after_string;
29982 /* To correctly find the limits of mouse highlight
29983 in a bidi-reordered buffer, we must not use the
29984 optimization of limiting the search in
29985 previous-single-property-change and
29986 next-single-property-change, because
29987 rows_from_pos_range needs the real start and end
29988 positions to DTRT in this case. That's because
29989 the first row visible in a window does not
29990 necessarily display the character whose position
29991 is the smallest. */
29992 Lisp_Object lim1
29993 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29994 ? Fmarker_position (w->start)
29995 : Qnil;
29996 Lisp_Object lim2
29997 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29998 ? make_number (BUF_Z (XBUFFER (buffer))
29999 - w->window_end_pos)
30000 : Qnil;
30001
30002 if (NILP (overlay))
30003 {
30004 /* Handle the text property case. */
30005 before = Fprevious_single_property_change
30006 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30007 after = Fnext_single_property_change
30008 (make_number (pos), Qmouse_face, buffer, lim2);
30009 before_string = after_string = Qnil;
30010 }
30011 else
30012 {
30013 /* Handle the overlay case. */
30014 before = Foverlay_start (overlay);
30015 after = Foverlay_end (overlay);
30016 before_string = Foverlay_get (overlay, Qbefore_string);
30017 after_string = Foverlay_get (overlay, Qafter_string);
30018
30019 if (!STRINGP (before_string)) before_string = Qnil;
30020 if (!STRINGP (after_string)) after_string = Qnil;
30021 }
30022
30023 mouse_face_from_buffer_pos (window, hlinfo, pos,
30024 NILP (before)
30025 ? 1
30026 : XFASTINT (before),
30027 NILP (after)
30028 ? BUF_Z (XBUFFER (buffer))
30029 : XFASTINT (after),
30030 before_string, after_string,
30031 disp_string);
30032 cursor = No_Cursor;
30033 }
30034 }
30035 }
30036
30037 check_help_echo:
30038
30039 /* Look for a `help-echo' property. */
30040 if (NILP (help_echo_string)) {
30041 Lisp_Object help, overlay;
30042
30043 /* Check overlays first. */
30044 help = overlay = Qnil;
30045 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30046 {
30047 overlay = overlay_vec[i];
30048 help = Foverlay_get (overlay, Qhelp_echo);
30049 }
30050
30051 if (!NILP (help))
30052 {
30053 help_echo_string = help;
30054 help_echo_window = window;
30055 help_echo_object = overlay;
30056 help_echo_pos = pos;
30057 }
30058 else
30059 {
30060 Lisp_Object obj = glyph->object;
30061 ptrdiff_t charpos = glyph->charpos;
30062
30063 /* Try text properties. */
30064 if (STRINGP (obj)
30065 && charpos >= 0
30066 && charpos < SCHARS (obj))
30067 {
30068 help = Fget_text_property (make_number (charpos),
30069 Qhelp_echo, obj);
30070 if (NILP (help))
30071 {
30072 /* If the string itself doesn't specify a help-echo,
30073 see if the buffer text ``under'' it does. */
30074 struct glyph_row *r
30075 = MATRIX_ROW (w->current_matrix, vpos);
30076 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30077 ptrdiff_t p = string_buffer_position (obj, start);
30078 if (p > 0)
30079 {
30080 help = Fget_char_property (make_number (p),
30081 Qhelp_echo, w->contents);
30082 if (!NILP (help))
30083 {
30084 charpos = p;
30085 obj = w->contents;
30086 }
30087 }
30088 }
30089 }
30090 else if (BUFFERP (obj)
30091 && charpos >= BEGV
30092 && charpos < ZV)
30093 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30094 obj);
30095
30096 if (!NILP (help))
30097 {
30098 help_echo_string = help;
30099 help_echo_window = window;
30100 help_echo_object = obj;
30101 help_echo_pos = charpos;
30102 }
30103 }
30104 }
30105
30106 #ifdef HAVE_WINDOW_SYSTEM
30107 /* Look for a `pointer' property. */
30108 if (FRAME_WINDOW_P (f) && NILP (pointer))
30109 {
30110 /* Check overlays first. */
30111 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30112 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30113
30114 if (NILP (pointer))
30115 {
30116 Lisp_Object obj = glyph->object;
30117 ptrdiff_t charpos = glyph->charpos;
30118
30119 /* Try text properties. */
30120 if (STRINGP (obj)
30121 && charpos >= 0
30122 && charpos < SCHARS (obj))
30123 {
30124 pointer = Fget_text_property (make_number (charpos),
30125 Qpointer, obj);
30126 if (NILP (pointer))
30127 {
30128 /* If the string itself doesn't specify a pointer,
30129 see if the buffer text ``under'' it does. */
30130 struct glyph_row *r
30131 = MATRIX_ROW (w->current_matrix, vpos);
30132 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30133 ptrdiff_t p = string_buffer_position (obj, start);
30134 if (p > 0)
30135 pointer = Fget_char_property (make_number (p),
30136 Qpointer, w->contents);
30137 }
30138 }
30139 else if (BUFFERP (obj)
30140 && charpos >= BEGV
30141 && charpos < ZV)
30142 pointer = Fget_text_property (make_number (charpos),
30143 Qpointer, obj);
30144 }
30145 }
30146 #endif /* HAVE_WINDOW_SYSTEM */
30147
30148 BEGV = obegv;
30149 ZV = ozv;
30150 current_buffer = obuf;
30151 SAFE_FREE ();
30152 }
30153
30154 set_cursor:
30155
30156 #ifdef HAVE_WINDOW_SYSTEM
30157 if (FRAME_WINDOW_P (f))
30158 define_frame_cursor1 (f, cursor, pointer);
30159 #else
30160 /* This is here to prevent a compiler error, about "label at end of
30161 compound statement". */
30162 return;
30163 #endif
30164 }
30165
30166
30167 /* EXPORT for RIF:
30168 Clear any mouse-face on window W. This function is part of the
30169 redisplay interface, and is called from try_window_id and similar
30170 functions to ensure the mouse-highlight is off. */
30171
30172 void
30173 x_clear_window_mouse_face (struct window *w)
30174 {
30175 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30176 Lisp_Object window;
30177
30178 block_input ();
30179 XSETWINDOW (window, w);
30180 if (EQ (window, hlinfo->mouse_face_window))
30181 clear_mouse_face (hlinfo);
30182 unblock_input ();
30183 }
30184
30185
30186 /* EXPORT:
30187 Just discard the mouse face information for frame F, if any.
30188 This is used when the size of F is changed. */
30189
30190 void
30191 cancel_mouse_face (struct frame *f)
30192 {
30193 Lisp_Object window;
30194 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30195
30196 window = hlinfo->mouse_face_window;
30197 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30198 reset_mouse_highlight (hlinfo);
30199 }
30200
30201
30202 \f
30203 /***********************************************************************
30204 Exposure Events
30205 ***********************************************************************/
30206
30207 #ifdef HAVE_WINDOW_SYSTEM
30208
30209 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30210 which intersects rectangle R. R is in window-relative coordinates. */
30211
30212 static void
30213 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30214 enum glyph_row_area area)
30215 {
30216 struct glyph *first = row->glyphs[area];
30217 struct glyph *end = row->glyphs[area] + row->used[area];
30218 struct glyph *last;
30219 int first_x, start_x, x;
30220
30221 if (area == TEXT_AREA && row->fill_line_p)
30222 /* If row extends face to end of line write the whole line. */
30223 draw_glyphs (w, 0, row, area,
30224 0, row->used[area],
30225 DRAW_NORMAL_TEXT, 0);
30226 else
30227 {
30228 /* Set START_X to the window-relative start position for drawing glyphs of
30229 AREA. The first glyph of the text area can be partially visible.
30230 The first glyphs of other areas cannot. */
30231 start_x = window_box_left_offset (w, area);
30232 x = start_x;
30233 if (area == TEXT_AREA)
30234 x += row->x;
30235
30236 /* Find the first glyph that must be redrawn. */
30237 while (first < end
30238 && x + first->pixel_width < r->x)
30239 {
30240 x += first->pixel_width;
30241 ++first;
30242 }
30243
30244 /* Find the last one. */
30245 last = first;
30246 first_x = x;
30247 /* Use a signed int intermediate value to avoid catastrophic
30248 failures due to comparison between signed and unsigned, when
30249 x is negative (can happen for wide images that are hscrolled). */
30250 int r_end = r->x + r->width;
30251 while (last < end && x < r_end)
30252 {
30253 x += last->pixel_width;
30254 ++last;
30255 }
30256
30257 /* Repaint. */
30258 if (last > first)
30259 draw_glyphs (w, first_x - start_x, row, area,
30260 first - row->glyphs[area], last - row->glyphs[area],
30261 DRAW_NORMAL_TEXT, 0);
30262 }
30263 }
30264
30265
30266 /* Redraw the parts of the glyph row ROW on window W intersecting
30267 rectangle R. R is in window-relative coordinates. Value is
30268 true if mouse-face was overwritten. */
30269
30270 static bool
30271 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30272 {
30273 eassert (row->enabled_p);
30274
30275 if (row->mode_line_p || w->pseudo_window_p)
30276 draw_glyphs (w, 0, row, TEXT_AREA,
30277 0, row->used[TEXT_AREA],
30278 DRAW_NORMAL_TEXT, 0);
30279 else
30280 {
30281 if (row->used[LEFT_MARGIN_AREA])
30282 expose_area (w, row, r, LEFT_MARGIN_AREA);
30283 if (row->used[TEXT_AREA])
30284 expose_area (w, row, r, TEXT_AREA);
30285 if (row->used[RIGHT_MARGIN_AREA])
30286 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30287 draw_row_fringe_bitmaps (w, row);
30288 }
30289
30290 return row->mouse_face_p;
30291 }
30292
30293
30294 /* Redraw those parts of glyphs rows during expose event handling that
30295 overlap other rows. Redrawing of an exposed line writes over parts
30296 of lines overlapping that exposed line; this function fixes that.
30297
30298 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30299 row in W's current matrix that is exposed and overlaps other rows.
30300 LAST_OVERLAPPING_ROW is the last such row. */
30301
30302 static void
30303 expose_overlaps (struct window *w,
30304 struct glyph_row *first_overlapping_row,
30305 struct glyph_row *last_overlapping_row,
30306 XRectangle *r)
30307 {
30308 struct glyph_row *row;
30309
30310 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30311 if (row->overlapping_p)
30312 {
30313 eassert (row->enabled_p && !row->mode_line_p);
30314
30315 row->clip = r;
30316 if (row->used[LEFT_MARGIN_AREA])
30317 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30318
30319 if (row->used[TEXT_AREA])
30320 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30321
30322 if (row->used[RIGHT_MARGIN_AREA])
30323 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30324 row->clip = NULL;
30325 }
30326 }
30327
30328
30329 /* Return true if W's cursor intersects rectangle R. */
30330
30331 static bool
30332 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30333 {
30334 XRectangle cr, result;
30335 struct glyph *cursor_glyph;
30336 struct glyph_row *row;
30337
30338 if (w->phys_cursor.vpos >= 0
30339 && w->phys_cursor.vpos < w->current_matrix->nrows
30340 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30341 row->enabled_p)
30342 && row->cursor_in_fringe_p)
30343 {
30344 /* Cursor is in the fringe. */
30345 cr.x = window_box_right_offset (w,
30346 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30347 ? RIGHT_MARGIN_AREA
30348 : TEXT_AREA));
30349 cr.y = row->y;
30350 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30351 cr.height = row->height;
30352 return x_intersect_rectangles (&cr, r, &result);
30353 }
30354
30355 cursor_glyph = get_phys_cursor_glyph (w);
30356 if (cursor_glyph)
30357 {
30358 /* r is relative to W's box, but w->phys_cursor.x is relative
30359 to left edge of W's TEXT area. Adjust it. */
30360 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30361 cr.y = w->phys_cursor.y;
30362 cr.width = cursor_glyph->pixel_width;
30363 cr.height = w->phys_cursor_height;
30364 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30365 I assume the effect is the same -- and this is portable. */
30366 return x_intersect_rectangles (&cr, r, &result);
30367 }
30368 /* If we don't understand the format, pretend we're not in the hot-spot. */
30369 return false;
30370 }
30371
30372
30373 /* EXPORT:
30374 Draw a vertical window border to the right of window W if W doesn't
30375 have vertical scroll bars. */
30376
30377 void
30378 x_draw_vertical_border (struct window *w)
30379 {
30380 struct frame *f = XFRAME (WINDOW_FRAME (w));
30381
30382 /* We could do better, if we knew what type of scroll-bar the adjacent
30383 windows (on either side) have... But we don't :-(
30384 However, I think this works ok. ++KFS 2003-04-25 */
30385
30386 /* Redraw borders between horizontally adjacent windows. Don't
30387 do it for frames with vertical scroll bars because either the
30388 right scroll bar of a window, or the left scroll bar of its
30389 neighbor will suffice as a border. */
30390 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30391 return;
30392
30393 /* Note: It is necessary to redraw both the left and the right
30394 borders, for when only this single window W is being
30395 redisplayed. */
30396 if (!WINDOW_RIGHTMOST_P (w)
30397 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30398 {
30399 int x0, x1, y0, y1;
30400
30401 window_box_edges (w, &x0, &y0, &x1, &y1);
30402 y1 -= 1;
30403
30404 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30405 x1 -= 1;
30406
30407 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30408 }
30409
30410 if (!WINDOW_LEFTMOST_P (w)
30411 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30412 {
30413 int x0, x1, y0, y1;
30414
30415 window_box_edges (w, &x0, &y0, &x1, &y1);
30416 y1 -= 1;
30417
30418 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30419 x0 -= 1;
30420
30421 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30422 }
30423 }
30424
30425
30426 /* Draw window dividers for window W. */
30427
30428 void
30429 x_draw_right_divider (struct window *w)
30430 {
30431 struct frame *f = WINDOW_XFRAME (w);
30432
30433 if (w->mini || w->pseudo_window_p)
30434 return;
30435 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30436 {
30437 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30438 int x1 = WINDOW_RIGHT_EDGE_X (w);
30439 int y0 = WINDOW_TOP_EDGE_Y (w);
30440 /* The bottom divider prevails. */
30441 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30442
30443 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30444 }
30445 }
30446
30447 static void
30448 x_draw_bottom_divider (struct window *w)
30449 {
30450 struct frame *f = XFRAME (WINDOW_FRAME (w));
30451
30452 if (w->mini || w->pseudo_window_p)
30453 return;
30454 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30455 {
30456 int x0 = WINDOW_LEFT_EDGE_X (w);
30457 int x1 = WINDOW_RIGHT_EDGE_X (w);
30458 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30459 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30460
30461 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30462 }
30463 }
30464
30465 /* Redraw the part of window W intersection rectangle FR. Pixel
30466 coordinates in FR are frame-relative. Call this function with
30467 input blocked. Value is true if the exposure overwrites
30468 mouse-face. */
30469
30470 static bool
30471 expose_window (struct window *w, XRectangle *fr)
30472 {
30473 struct frame *f = XFRAME (w->frame);
30474 XRectangle wr, r;
30475 bool mouse_face_overwritten_p = false;
30476
30477 /* If window is not yet fully initialized, do nothing. This can
30478 happen when toolkit scroll bars are used and a window is split.
30479 Reconfiguring the scroll bar will generate an expose for a newly
30480 created window. */
30481 if (w->current_matrix == NULL)
30482 return false;
30483
30484 /* When we're currently updating the window, display and current
30485 matrix usually don't agree. Arrange for a thorough display
30486 later. */
30487 if (w->must_be_updated_p)
30488 {
30489 SET_FRAME_GARBAGED (f);
30490 return false;
30491 }
30492
30493 /* Frame-relative pixel rectangle of W. */
30494 wr.x = WINDOW_LEFT_EDGE_X (w);
30495 wr.y = WINDOW_TOP_EDGE_Y (w);
30496 wr.width = WINDOW_PIXEL_WIDTH (w);
30497 wr.height = WINDOW_PIXEL_HEIGHT (w);
30498
30499 if (x_intersect_rectangles (fr, &wr, &r))
30500 {
30501 int yb = window_text_bottom_y (w);
30502 struct glyph_row *row;
30503 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30504
30505 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30506 r.x, r.y, r.width, r.height));
30507
30508 /* Convert to window coordinates. */
30509 r.x -= WINDOW_LEFT_EDGE_X (w);
30510 r.y -= WINDOW_TOP_EDGE_Y (w);
30511
30512 /* Turn off the cursor. */
30513 bool cursor_cleared_p = (!w->pseudo_window_p
30514 && phys_cursor_in_rect_p (w, &r));
30515 if (cursor_cleared_p)
30516 x_clear_cursor (w);
30517
30518 /* If the row containing the cursor extends face to end of line,
30519 then expose_area might overwrite the cursor outside the
30520 rectangle and thus notice_overwritten_cursor might clear
30521 w->phys_cursor_on_p. We remember the original value and
30522 check later if it is changed. */
30523 bool phys_cursor_on_p = w->phys_cursor_on_p;
30524
30525 /* Use a signed int intermediate value to avoid catastrophic
30526 failures due to comparison between signed and unsigned, when
30527 y0 or y1 is negative (can happen for tall images). */
30528 int r_bottom = r.y + r.height;
30529
30530 /* Update lines intersecting rectangle R. */
30531 first_overlapping_row = last_overlapping_row = NULL;
30532 for (row = w->current_matrix->rows;
30533 row->enabled_p;
30534 ++row)
30535 {
30536 int y0 = row->y;
30537 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30538
30539 if ((y0 >= r.y && y0 < r_bottom)
30540 || (y1 > r.y && y1 < r_bottom)
30541 || (r.y >= y0 && r.y < y1)
30542 || (r_bottom > y0 && r_bottom < y1))
30543 {
30544 /* A header line may be overlapping, but there is no need
30545 to fix overlapping areas for them. KFS 2005-02-12 */
30546 if (row->overlapping_p && !row->mode_line_p)
30547 {
30548 if (first_overlapping_row == NULL)
30549 first_overlapping_row = row;
30550 last_overlapping_row = row;
30551 }
30552
30553 row->clip = fr;
30554 if (expose_line (w, row, &r))
30555 mouse_face_overwritten_p = true;
30556 row->clip = NULL;
30557 }
30558 else if (row->overlapping_p)
30559 {
30560 /* We must redraw a row overlapping the exposed area. */
30561 if (y0 < r.y
30562 ? y0 + row->phys_height > r.y
30563 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30564 {
30565 if (first_overlapping_row == NULL)
30566 first_overlapping_row = row;
30567 last_overlapping_row = row;
30568 }
30569 }
30570
30571 if (y1 >= yb)
30572 break;
30573 }
30574
30575 /* Display the mode line if there is one. */
30576 if (WINDOW_WANTS_MODELINE_P (w)
30577 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30578 row->enabled_p)
30579 && row->y < r_bottom)
30580 {
30581 if (expose_line (w, row, &r))
30582 mouse_face_overwritten_p = true;
30583 }
30584
30585 if (!w->pseudo_window_p)
30586 {
30587 /* Fix the display of overlapping rows. */
30588 if (first_overlapping_row)
30589 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30590 fr);
30591
30592 /* Draw border between windows. */
30593 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30594 x_draw_right_divider (w);
30595 else
30596 x_draw_vertical_border (w);
30597
30598 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30599 x_draw_bottom_divider (w);
30600
30601 /* Turn the cursor on again. */
30602 if (cursor_cleared_p
30603 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30604 update_window_cursor (w, true);
30605 }
30606 }
30607
30608 return mouse_face_overwritten_p;
30609 }
30610
30611
30612
30613 /* Redraw (parts) of all windows in the window tree rooted at W that
30614 intersect R. R contains frame pixel coordinates. Value is
30615 true if the exposure overwrites mouse-face. */
30616
30617 static bool
30618 expose_window_tree (struct window *w, XRectangle *r)
30619 {
30620 struct frame *f = XFRAME (w->frame);
30621 bool mouse_face_overwritten_p = false;
30622
30623 while (w && !FRAME_GARBAGED_P (f))
30624 {
30625 mouse_face_overwritten_p
30626 |= (WINDOWP (w->contents)
30627 ? expose_window_tree (XWINDOW (w->contents), r)
30628 : expose_window (w, r));
30629
30630 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30631 }
30632
30633 return mouse_face_overwritten_p;
30634 }
30635
30636
30637 /* EXPORT:
30638 Redisplay an exposed area of frame F. X and Y are the upper-left
30639 corner of the exposed rectangle. W and H are width and height of
30640 the exposed area. All are pixel values. W or H zero means redraw
30641 the entire frame. */
30642
30643 void
30644 expose_frame (struct frame *f, int x, int y, int w, int h)
30645 {
30646 XRectangle r;
30647 bool mouse_face_overwritten_p = false;
30648
30649 TRACE ((stderr, "expose_frame "));
30650
30651 /* No need to redraw if frame will be redrawn soon. */
30652 if (FRAME_GARBAGED_P (f))
30653 {
30654 TRACE ((stderr, " garbaged\n"));
30655 return;
30656 }
30657
30658 /* If basic faces haven't been realized yet, there is no point in
30659 trying to redraw anything. This can happen when we get an expose
30660 event while Emacs is starting, e.g. by moving another window. */
30661 if (FRAME_FACE_CACHE (f) == NULL
30662 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30663 {
30664 TRACE ((stderr, " no faces\n"));
30665 return;
30666 }
30667
30668 if (w == 0 || h == 0)
30669 {
30670 r.x = r.y = 0;
30671 r.width = FRAME_TEXT_WIDTH (f);
30672 r.height = FRAME_TEXT_HEIGHT (f);
30673 }
30674 else
30675 {
30676 r.x = x;
30677 r.y = y;
30678 r.width = w;
30679 r.height = h;
30680 }
30681
30682 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30683 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30684
30685 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30686 if (WINDOWP (f->tool_bar_window))
30687 mouse_face_overwritten_p
30688 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30689 #endif
30690
30691 #ifdef HAVE_X_WINDOWS
30692 #ifndef MSDOS
30693 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30694 if (WINDOWP (f->menu_bar_window))
30695 mouse_face_overwritten_p
30696 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30697 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30698 #endif
30699 #endif
30700
30701 /* Some window managers support a focus-follows-mouse style with
30702 delayed raising of frames. Imagine a partially obscured frame,
30703 and moving the mouse into partially obscured mouse-face on that
30704 frame. The visible part of the mouse-face will be highlighted,
30705 then the WM raises the obscured frame. With at least one WM, KDE
30706 2.1, Emacs is not getting any event for the raising of the frame
30707 (even tried with SubstructureRedirectMask), only Expose events.
30708 These expose events will draw text normally, i.e. not
30709 highlighted. Which means we must redo the highlight here.
30710 Subsume it under ``we love X''. --gerd 2001-08-15 */
30711 /* Included in Windows version because Windows most likely does not
30712 do the right thing if any third party tool offers
30713 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30714 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30715 {
30716 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30717 if (f == hlinfo->mouse_face_mouse_frame)
30718 {
30719 int mouse_x = hlinfo->mouse_face_mouse_x;
30720 int mouse_y = hlinfo->mouse_face_mouse_y;
30721 clear_mouse_face (hlinfo);
30722 note_mouse_highlight (f, mouse_x, mouse_y);
30723 }
30724 }
30725 }
30726
30727
30728 /* EXPORT:
30729 Determine the intersection of two rectangles R1 and R2. Return
30730 the intersection in *RESULT. Value is true if RESULT is not
30731 empty. */
30732
30733 bool
30734 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30735 {
30736 XRectangle *left, *right;
30737 XRectangle *upper, *lower;
30738 bool intersection_p = false;
30739
30740 /* Rearrange so that R1 is the left-most rectangle. */
30741 if (r1->x < r2->x)
30742 left = r1, right = r2;
30743 else
30744 left = r2, right = r1;
30745
30746 /* X0 of the intersection is right.x0, if this is inside R1,
30747 otherwise there is no intersection. */
30748 if (right->x <= left->x + left->width)
30749 {
30750 result->x = right->x;
30751
30752 /* The right end of the intersection is the minimum of
30753 the right ends of left and right. */
30754 result->width = (min (left->x + left->width, right->x + right->width)
30755 - result->x);
30756
30757 /* Same game for Y. */
30758 if (r1->y < r2->y)
30759 upper = r1, lower = r2;
30760 else
30761 upper = r2, lower = r1;
30762
30763 /* The upper end of the intersection is lower.y0, if this is inside
30764 of upper. Otherwise, there is no intersection. */
30765 if (lower->y <= upper->y + upper->height)
30766 {
30767 result->y = lower->y;
30768
30769 /* The lower end of the intersection is the minimum of the lower
30770 ends of upper and lower. */
30771 result->height = (min (lower->y + lower->height,
30772 upper->y + upper->height)
30773 - result->y);
30774 intersection_p = true;
30775 }
30776 }
30777
30778 return intersection_p;
30779 }
30780
30781 #endif /* HAVE_WINDOW_SYSTEM */
30782
30783 \f
30784 /***********************************************************************
30785 Initialization
30786 ***********************************************************************/
30787
30788 void
30789 syms_of_xdisp (void)
30790 {
30791 Vwith_echo_area_save_vector = Qnil;
30792 staticpro (&Vwith_echo_area_save_vector);
30793
30794 Vmessage_stack = Qnil;
30795 staticpro (&Vmessage_stack);
30796
30797 /* Non-nil means don't actually do any redisplay. */
30798 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30799
30800 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30801
30802 DEFVAR_BOOL("inhibit-message", inhibit_message,
30803 doc: /* Non-nil means calls to `message' are not displayed.
30804 They are still logged to the *Messages* buffer. */);
30805 inhibit_message = 0;
30806
30807 message_dolog_marker1 = Fmake_marker ();
30808 staticpro (&message_dolog_marker1);
30809 message_dolog_marker2 = Fmake_marker ();
30810 staticpro (&message_dolog_marker2);
30811 message_dolog_marker3 = Fmake_marker ();
30812 staticpro (&message_dolog_marker3);
30813
30814 #ifdef GLYPH_DEBUG
30815 defsubr (&Sdump_frame_glyph_matrix);
30816 defsubr (&Sdump_glyph_matrix);
30817 defsubr (&Sdump_glyph_row);
30818 defsubr (&Sdump_tool_bar_row);
30819 defsubr (&Strace_redisplay);
30820 defsubr (&Strace_to_stderr);
30821 #endif
30822 #ifdef HAVE_WINDOW_SYSTEM
30823 defsubr (&Stool_bar_height);
30824 defsubr (&Slookup_image_map);
30825 #endif
30826 defsubr (&Sline_pixel_height);
30827 defsubr (&Sformat_mode_line);
30828 defsubr (&Sinvisible_p);
30829 defsubr (&Scurrent_bidi_paragraph_direction);
30830 defsubr (&Swindow_text_pixel_size);
30831 defsubr (&Smove_point_visually);
30832 defsubr (&Sbidi_find_overridden_directionality);
30833
30834 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30835 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30836 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30837 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30838 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30839 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30840 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30841 DEFSYM (Qeval, "eval");
30842 DEFSYM (QCdata, ":data");
30843
30844 /* Names of text properties relevant for redisplay. */
30845 DEFSYM (Qdisplay, "display");
30846 DEFSYM (Qspace_width, "space-width");
30847 DEFSYM (Qraise, "raise");
30848 DEFSYM (Qslice, "slice");
30849 DEFSYM (Qspace, "space");
30850 DEFSYM (Qmargin, "margin");
30851 DEFSYM (Qpointer, "pointer");
30852 DEFSYM (Qleft_margin, "left-margin");
30853 DEFSYM (Qright_margin, "right-margin");
30854 DEFSYM (Qcenter, "center");
30855 DEFSYM (Qline_height, "line-height");
30856 DEFSYM (QCalign_to, ":align-to");
30857 DEFSYM (QCrelative_width, ":relative-width");
30858 DEFSYM (QCrelative_height, ":relative-height");
30859 DEFSYM (QCeval, ":eval");
30860 DEFSYM (QCpropertize, ":propertize");
30861 DEFSYM (QCfile, ":file");
30862 DEFSYM (Qfontified, "fontified");
30863 DEFSYM (Qfontification_functions, "fontification-functions");
30864
30865 /* Name of the face used to highlight trailing whitespace. */
30866 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30867
30868 /* Name and number of the face used to highlight escape glyphs. */
30869 DEFSYM (Qescape_glyph, "escape-glyph");
30870
30871 /* Name and number of the face used to highlight non-breaking spaces. */
30872 DEFSYM (Qnobreak_space, "nobreak-space");
30873
30874 /* The symbol 'image' which is the car of the lists used to represent
30875 images in Lisp. Also a tool bar style. */
30876 DEFSYM (Qimage, "image");
30877
30878 /* Tool bar styles. */
30879 DEFSYM (Qtext, "text");
30880 DEFSYM (Qboth, "both");
30881 DEFSYM (Qboth_horiz, "both-horiz");
30882 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30883
30884 /* The image map types. */
30885 DEFSYM (QCmap, ":map");
30886 DEFSYM (QCpointer, ":pointer");
30887 DEFSYM (Qrect, "rect");
30888 DEFSYM (Qcircle, "circle");
30889 DEFSYM (Qpoly, "poly");
30890
30891 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30892
30893 DEFSYM (Qgrow_only, "grow-only");
30894 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30895 DEFSYM (Qposition, "position");
30896 DEFSYM (Qbuffer_position, "buffer-position");
30897 DEFSYM (Qobject, "object");
30898
30899 /* Cursor shapes. */
30900 DEFSYM (Qbar, "bar");
30901 DEFSYM (Qhbar, "hbar");
30902 DEFSYM (Qbox, "box");
30903 DEFSYM (Qhollow, "hollow");
30904
30905 /* Pointer shapes. */
30906 DEFSYM (Qhand, "hand");
30907 DEFSYM (Qarrow, "arrow");
30908 /* also Qtext */
30909
30910 DEFSYM (Qdragging, "dragging");
30911
30912 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30913
30914 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
30915 staticpro (&list_of_error);
30916
30917 /* Values of those variables at last redisplay are stored as
30918 properties on 'overlay-arrow-position' symbol. However, if
30919 Voverlay_arrow_position is a marker, last-arrow-position is its
30920 numerical position. */
30921 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30922 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30923
30924 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
30925 properties on a symbol in overlay-arrow-variable-list. */
30926 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30927 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30928
30929 echo_buffer[0] = echo_buffer[1] = Qnil;
30930 staticpro (&echo_buffer[0]);
30931 staticpro (&echo_buffer[1]);
30932
30933 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30934 staticpro (&echo_area_buffer[0]);
30935 staticpro (&echo_area_buffer[1]);
30936
30937 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30938 staticpro (&Vmessages_buffer_name);
30939
30940 mode_line_proptrans_alist = Qnil;
30941 staticpro (&mode_line_proptrans_alist);
30942 mode_line_string_list = Qnil;
30943 staticpro (&mode_line_string_list);
30944 mode_line_string_face = Qnil;
30945 staticpro (&mode_line_string_face);
30946 mode_line_string_face_prop = Qnil;
30947 staticpro (&mode_line_string_face_prop);
30948 Vmode_line_unwind_vector = Qnil;
30949 staticpro (&Vmode_line_unwind_vector);
30950
30951 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30952
30953 help_echo_string = Qnil;
30954 staticpro (&help_echo_string);
30955 help_echo_object = Qnil;
30956 staticpro (&help_echo_object);
30957 help_echo_window = Qnil;
30958 staticpro (&help_echo_window);
30959 previous_help_echo_string = Qnil;
30960 staticpro (&previous_help_echo_string);
30961 help_echo_pos = -1;
30962
30963 DEFSYM (Qright_to_left, "right-to-left");
30964 DEFSYM (Qleft_to_right, "left-to-right");
30965 defsubr (&Sbidi_resolved_levels);
30966
30967 #ifdef HAVE_WINDOW_SYSTEM
30968 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30969 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30970 For example, if a block cursor is over a tab, it will be drawn as
30971 wide as that tab on the display. */);
30972 x_stretch_cursor_p = 0;
30973 #endif
30974
30975 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30976 doc: /* Non-nil means highlight trailing whitespace.
30977 The face used for trailing whitespace is `trailing-whitespace'. */);
30978 Vshow_trailing_whitespace = Qnil;
30979
30980 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30981 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30982 If the value is t, Emacs highlights non-ASCII chars which have the
30983 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30984 or `escape-glyph' face respectively.
30985
30986 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30987 U+2011 (non-breaking hyphen) are affected.
30988
30989 Any other non-nil value means to display these characters as a escape
30990 glyph followed by an ordinary space or hyphen.
30991
30992 A value of nil means no special handling of these characters. */);
30993 Vnobreak_char_display = Qt;
30994
30995 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30996 doc: /* The pointer shape to show in void text areas.
30997 A value of nil means to show the text pointer. Other options are
30998 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
30999 `hourglass'. */);
31000 Vvoid_text_area_pointer = Qarrow;
31001
31002 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
31003 doc: /* Non-nil means don't actually do any redisplay.
31004 This is used for internal purposes. */);
31005 Vinhibit_redisplay = Qnil;
31006
31007 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
31008 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
31009 Vglobal_mode_string = Qnil;
31010
31011 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31012 doc: /* Marker for where to display an arrow on top of the buffer text.
31013 This must be the beginning of a line in order to work.
31014 See also `overlay-arrow-string'. */);
31015 Voverlay_arrow_position = Qnil;
31016
31017 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31018 doc: /* String to display as an arrow in non-window frames.
31019 See also `overlay-arrow-position'. */);
31020 Voverlay_arrow_string = build_pure_c_string ("=>");
31021
31022 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31023 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31024 The symbols on this list are examined during redisplay to determine
31025 where to display overlay arrows. */);
31026 Voverlay_arrow_variable_list
31027 = list1 (intern_c_string ("overlay-arrow-position"));
31028
31029 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31030 doc: /* The number of lines to try scrolling a window by when point moves out.
31031 If that fails to bring point back on frame, point is centered instead.
31032 If this is zero, point is always centered after it moves off frame.
31033 If you want scrolling to always be a line at a time, you should set
31034 `scroll-conservatively' to a large value rather than set this to 1. */);
31035
31036 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31037 doc: /* Scroll up to this many lines, to bring point back on screen.
31038 If point moves off-screen, redisplay will scroll by up to
31039 `scroll-conservatively' lines in order to bring point just barely
31040 onto the screen again. If that cannot be done, then redisplay
31041 recenters point as usual.
31042
31043 If the value is greater than 100, redisplay will never recenter point,
31044 but will always scroll just enough text to bring point into view, even
31045 if you move far away.
31046
31047 A value of zero means always recenter point if it moves off screen. */);
31048 scroll_conservatively = 0;
31049
31050 DEFVAR_INT ("scroll-margin", scroll_margin,
31051 doc: /* Number of lines of margin at the top and bottom of a window.
31052 Recenter the window whenever point gets within this many lines
31053 of the top or bottom of the window. */);
31054 scroll_margin = 0;
31055
31056 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31057 doc: /* Pixels per inch value for non-window system displays.
31058 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31059 Vdisplay_pixels_per_inch = make_float (72.0);
31060
31061 #ifdef GLYPH_DEBUG
31062 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31063 #endif
31064
31065 DEFVAR_LISP ("truncate-partial-width-windows",
31066 Vtruncate_partial_width_windows,
31067 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31068 For an integer value, truncate lines in each window narrower than the
31069 full frame width, provided the window width is less than that integer;
31070 otherwise, respect the value of `truncate-lines'.
31071
31072 For any other non-nil value, truncate lines in all windows that do
31073 not span the full frame width.
31074
31075 A value of nil means to respect the value of `truncate-lines'.
31076
31077 If `word-wrap' is enabled, you might want to reduce this. */);
31078 Vtruncate_partial_width_windows = make_number (50);
31079
31080 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31081 doc: /* Maximum buffer size for which line number should be displayed.
31082 If the buffer is bigger than this, the line number does not appear
31083 in the mode line. A value of nil means no limit. */);
31084 Vline_number_display_limit = Qnil;
31085
31086 DEFVAR_INT ("line-number-display-limit-width",
31087 line_number_display_limit_width,
31088 doc: /* Maximum line width (in characters) for line number display.
31089 If the average length of the lines near point is bigger than this, then the
31090 line number may be omitted from the mode line. */);
31091 line_number_display_limit_width = 200;
31092
31093 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31094 doc: /* Non-nil means highlight region even in nonselected windows. */);
31095 highlight_nonselected_windows = false;
31096
31097 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31098 doc: /* Non-nil if more than one frame is visible on this display.
31099 Minibuffer-only frames don't count, but iconified frames do.
31100 This variable is not guaranteed to be accurate except while processing
31101 `frame-title-format' and `icon-title-format'. */);
31102
31103 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31104 doc: /* Template for displaying the title bar of visible frames.
31105 (Assuming the window manager supports this feature.)
31106
31107 This variable has the same structure as `mode-line-format', except that
31108 the %c and %l constructs are ignored. It is used only on frames for
31109 which no explicit name has been set (see `modify-frame-parameters'). */);
31110
31111 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31112 doc: /* Template for displaying the title bar of an iconified frame.
31113 (Assuming the window manager supports this feature.)
31114 This variable has the same structure as `mode-line-format' (which see),
31115 and is used only on frames for which no explicit name has been set
31116 (see `modify-frame-parameters'). */);
31117 Vicon_title_format
31118 = Vframe_title_format
31119 = listn (CONSTYPE_PURE, 3,
31120 intern_c_string ("multiple-frames"),
31121 build_pure_c_string ("%b"),
31122 listn (CONSTYPE_PURE, 4,
31123 empty_unibyte_string,
31124 intern_c_string ("invocation-name"),
31125 build_pure_c_string ("@"),
31126 intern_c_string ("system-name")));
31127
31128 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31129 doc: /* Maximum number of lines to keep in the message log buffer.
31130 If nil, disable message logging. If t, log messages but don't truncate
31131 the buffer when it becomes large. */);
31132 Vmessage_log_max = make_number (1000);
31133
31134 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
31135 doc: /* Functions called before redisplay, if window sizes have changed.
31136 The value should be a list of functions that take one argument.
31137 Just before redisplay, for each frame, if any of its windows have changed
31138 size since the last redisplay, or have been split or deleted,
31139 all the functions in the list are called, with the frame as argument. */);
31140 Vwindow_size_change_functions = Qnil;
31141
31142 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31143 doc: /* List of functions to call before redisplaying a window with scrolling.
31144 Each function is called with two arguments, the window and its new
31145 display-start position.
31146 These functions are called whenever the `window-start' marker is modified,
31147 either to point into another buffer (e.g. via `set-window-buffer') or another
31148 place in the same buffer.
31149 Note that the value of `window-end' is not valid when these functions are
31150 called.
31151
31152 Warning: Do not use this feature to alter the way the window
31153 is scrolled. It is not designed for that, and such use probably won't
31154 work. */);
31155 Vwindow_scroll_functions = Qnil;
31156
31157 DEFVAR_LISP ("window-text-change-functions",
31158 Vwindow_text_change_functions,
31159 doc: /* Functions to call in redisplay when text in the window might change. */);
31160 Vwindow_text_change_functions = Qnil;
31161
31162 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31163 doc: /* Functions called when redisplay of a window reaches the end trigger.
31164 Each function is called with two arguments, the window and the end trigger value.
31165 See `set-window-redisplay-end-trigger'. */);
31166 Vredisplay_end_trigger_functions = Qnil;
31167
31168 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31169 doc: /* Non-nil means autoselect window with mouse pointer.
31170 If nil, do not autoselect windows.
31171 A positive number means delay autoselection by that many seconds: a
31172 window is autoselected only after the mouse has remained in that
31173 window for the duration of the delay.
31174 A negative number has a similar effect, but causes windows to be
31175 autoselected only after the mouse has stopped moving. (Because of
31176 the way Emacs compares mouse events, you will occasionally wait twice
31177 that time before the window gets selected.)
31178 Any other value means to autoselect window instantaneously when the
31179 mouse pointer enters it.
31180
31181 Autoselection selects the minibuffer only if it is active, and never
31182 unselects the minibuffer if it is active.
31183
31184 When customizing this variable make sure that the actual value of
31185 `focus-follows-mouse' matches the behavior of your window manager. */);
31186 Vmouse_autoselect_window = Qnil;
31187
31188 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31189 doc: /* Non-nil means automatically resize tool-bars.
31190 This dynamically changes the tool-bar's height to the minimum height
31191 that is needed to make all tool-bar items visible.
31192 If value is `grow-only', the tool-bar's height is only increased
31193 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31194 Vauto_resize_tool_bars = Qt;
31195
31196 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31197 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31198 auto_raise_tool_bar_buttons_p = true;
31199
31200 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31201 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31202 make_cursor_line_fully_visible_p = true;
31203
31204 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31205 doc: /* Border below tool-bar in pixels.
31206 If an integer, use it as the height of the border.
31207 If it is one of `internal-border-width' or `border-width', use the
31208 value of the corresponding frame parameter.
31209 Otherwise, no border is added below the tool-bar. */);
31210 Vtool_bar_border = Qinternal_border_width;
31211
31212 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31213 doc: /* Margin around tool-bar buttons in pixels.
31214 If an integer, use that for both horizontal and vertical margins.
31215 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31216 HORZ specifying the horizontal margin, and VERT specifying the
31217 vertical margin. */);
31218 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31219
31220 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31221 doc: /* Relief thickness of tool-bar buttons. */);
31222 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31223
31224 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31225 doc: /* Tool bar style to use.
31226 It can be one of
31227 image - show images only
31228 text - show text only
31229 both - show both, text below image
31230 both-horiz - show text to the right of the image
31231 text-image-horiz - show text to the left of the image
31232 any other - use system default or image if no system default.
31233
31234 This variable only affects the GTK+ toolkit version of Emacs. */);
31235 Vtool_bar_style = Qnil;
31236
31237 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31238 doc: /* Maximum number of characters a label can have to be shown.
31239 The tool bar style must also show labels for this to have any effect, see
31240 `tool-bar-style'. */);
31241 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31242
31243 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31244 doc: /* List of functions to call to fontify regions of text.
31245 Each function is called with one argument POS. Functions must
31246 fontify a region starting at POS in the current buffer, and give
31247 fontified regions the property `fontified'. */);
31248 Vfontification_functions = Qnil;
31249 Fmake_variable_buffer_local (Qfontification_functions);
31250
31251 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31252 unibyte_display_via_language_environment,
31253 doc: /* Non-nil means display unibyte text according to language environment.
31254 Specifically, this means that raw bytes in the range 160-255 decimal
31255 are displayed by converting them to the equivalent multibyte characters
31256 according to the current language environment. As a result, they are
31257 displayed according to the current fontset.
31258
31259 Note that this variable affects only how these bytes are displayed,
31260 but does not change the fact they are interpreted as raw bytes. */);
31261 unibyte_display_via_language_environment = false;
31262
31263 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31264 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31265 If a float, it specifies a fraction of the mini-window frame's height.
31266 If an integer, it specifies a number of lines. */);
31267 Vmax_mini_window_height = make_float (0.25);
31268
31269 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31270 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31271 A value of nil means don't automatically resize mini-windows.
31272 A value of t means resize them to fit the text displayed in them.
31273 A value of `grow-only', the default, means let mini-windows grow only;
31274 they return to their normal size when the minibuffer is closed, or the
31275 echo area becomes empty. */);
31276 Vresize_mini_windows = Qgrow_only;
31277
31278 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31279 doc: /* Alist specifying how to blink the cursor off.
31280 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31281 `cursor-type' frame-parameter or variable equals ON-STATE,
31282 comparing using `equal', Emacs uses OFF-STATE to specify
31283 how to blink it off. ON-STATE and OFF-STATE are values for
31284 the `cursor-type' frame parameter.
31285
31286 If a frame's ON-STATE has no entry in this list,
31287 the frame's other specifications determine how to blink the cursor off. */);
31288 Vblink_cursor_alist = Qnil;
31289
31290 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31291 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31292 If non-nil, windows are automatically scrolled horizontally to make
31293 point visible. */);
31294 automatic_hscrolling_p = true;
31295 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31296
31297 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31298 doc: /* How many columns away from the window edge point is allowed to get
31299 before automatic hscrolling will horizontally scroll the window. */);
31300 hscroll_margin = 5;
31301
31302 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31303 doc: /* How many columns to scroll the window when point gets too close to the edge.
31304 When point is less than `hscroll-margin' columns from the window
31305 edge, automatic hscrolling will scroll the window by the amount of columns
31306 determined by this variable. If its value is a positive integer, scroll that
31307 many columns. If it's a positive floating-point number, it specifies the
31308 fraction of the window's width to scroll. If it's nil or zero, point will be
31309 centered horizontally after the scroll. Any other value, including negative
31310 numbers, are treated as if the value were zero.
31311
31312 Automatic hscrolling always moves point outside the scroll margin, so if
31313 point was more than scroll step columns inside the margin, the window will
31314 scroll more than the value given by the scroll step.
31315
31316 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31317 and `scroll-right' overrides this variable's effect. */);
31318 Vhscroll_step = make_number (0);
31319
31320 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31321 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31322 Bind this around calls to `message' to let it take effect. */);
31323 message_truncate_lines = false;
31324
31325 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31326 doc: /* Normal hook run to update the menu bar definitions.
31327 Redisplay runs this hook before it redisplays the menu bar.
31328 This is used to update menus such as Buffers, whose contents depend on
31329 various data. */);
31330 Vmenu_bar_update_hook = Qnil;
31331
31332 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31333 doc: /* Frame for which we are updating a menu.
31334 The enable predicate for a menu binding should check this variable. */);
31335 Vmenu_updating_frame = Qnil;
31336
31337 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31338 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31339 inhibit_menubar_update = false;
31340
31341 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31342 doc: /* Prefix prepended to all continuation lines at display time.
31343 The value may be a string, an image, or a stretch-glyph; it is
31344 interpreted in the same way as the value of a `display' text property.
31345
31346 This variable is overridden by any `wrap-prefix' text or overlay
31347 property.
31348
31349 To add a prefix to non-continuation lines, use `line-prefix'. */);
31350 Vwrap_prefix = Qnil;
31351 DEFSYM (Qwrap_prefix, "wrap-prefix");
31352 Fmake_variable_buffer_local (Qwrap_prefix);
31353
31354 DEFVAR_LISP ("line-prefix", Vline_prefix,
31355 doc: /* Prefix prepended to all non-continuation lines at display time.
31356 The value may be a string, an image, or a stretch-glyph; it is
31357 interpreted in the same way as the value of a `display' text property.
31358
31359 This variable is overridden by any `line-prefix' text or overlay
31360 property.
31361
31362 To add a prefix to continuation lines, use `wrap-prefix'. */);
31363 Vline_prefix = Qnil;
31364 DEFSYM (Qline_prefix, "line-prefix");
31365 Fmake_variable_buffer_local (Qline_prefix);
31366
31367 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31368 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31369 inhibit_eval_during_redisplay = false;
31370
31371 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31372 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31373 inhibit_free_realized_faces = false;
31374
31375 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31376 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31377 Intended for use during debugging and for testing bidi display;
31378 see biditest.el in the test suite. */);
31379 inhibit_bidi_mirroring = false;
31380
31381 #ifdef GLYPH_DEBUG
31382 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31383 doc: /* Inhibit try_window_id display optimization. */);
31384 inhibit_try_window_id = false;
31385
31386 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31387 doc: /* Inhibit try_window_reusing display optimization. */);
31388 inhibit_try_window_reusing = false;
31389
31390 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31391 doc: /* Inhibit try_cursor_movement display optimization. */);
31392 inhibit_try_cursor_movement = false;
31393 #endif /* GLYPH_DEBUG */
31394
31395 DEFVAR_INT ("overline-margin", overline_margin,
31396 doc: /* Space between overline and text, in pixels.
31397 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31398 margin to the character height. */);
31399 overline_margin = 2;
31400
31401 DEFVAR_INT ("underline-minimum-offset",
31402 underline_minimum_offset,
31403 doc: /* Minimum distance between baseline and underline.
31404 This can improve legibility of underlined text at small font sizes,
31405 particularly when using variable `x-use-underline-position-properties'
31406 with fonts that specify an UNDERLINE_POSITION relatively close to the
31407 baseline. The default value is 1. */);
31408 underline_minimum_offset = 1;
31409
31410 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31411 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31412 This feature only works when on a window system that can change
31413 cursor shapes. */);
31414 display_hourglass_p = true;
31415
31416 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31417 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31418 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31419
31420 #ifdef HAVE_WINDOW_SYSTEM
31421 hourglass_atimer = NULL;
31422 hourglass_shown_p = false;
31423 #endif /* HAVE_WINDOW_SYSTEM */
31424
31425 /* Name of the face used to display glyphless characters. */
31426 DEFSYM (Qglyphless_char, "glyphless-char");
31427
31428 /* Method symbols for Vglyphless_char_display. */
31429 DEFSYM (Qhex_code, "hex-code");
31430 DEFSYM (Qempty_box, "empty-box");
31431 DEFSYM (Qthin_space, "thin-space");
31432 DEFSYM (Qzero_width, "zero-width");
31433
31434 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31435 doc: /* Function run just before redisplay.
31436 It is called with one argument, which is the set of windows that are to
31437 be redisplayed. This set can be nil (meaning, only the selected window),
31438 or t (meaning all windows). */);
31439 Vpre_redisplay_function = intern ("ignore");
31440
31441 /* Symbol for the purpose of Vglyphless_char_display. */
31442 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31443 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31444
31445 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31446 doc: /* Char-table defining glyphless characters.
31447 Each element, if non-nil, should be one of the following:
31448 an ASCII acronym string: display this string in a box
31449 `hex-code': display the hexadecimal code of a character in a box
31450 `empty-box': display as an empty box
31451 `thin-space': display as 1-pixel width space
31452 `zero-width': don't display
31453 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31454 display method for graphical terminals and text terminals respectively.
31455 GRAPHICAL and TEXT should each have one of the values listed above.
31456
31457 The char-table has one extra slot to control the display of a character for
31458 which no font is found. This slot only takes effect on graphical terminals.
31459 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31460 `thin-space'. The default is `empty-box'.
31461
31462 If a character has a non-nil entry in an active display table, the
31463 display table takes effect; in this case, Emacs does not consult
31464 `glyphless-char-display' at all. */);
31465 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31466 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31467 Qempty_box);
31468
31469 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31470 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31471 Vdebug_on_message = Qnil;
31472
31473 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31474 doc: /* */);
31475 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
31476
31477 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31478 doc: /* */);
31479 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
31480
31481 DEFVAR_LISP ("redisplay--variables", Vredisplay__variables,
31482 doc: /* A hash-table of variables changing which triggers a thorough redisplay. */);
31483 Vredisplay__variables = Qnil;
31484 }
31485
31486
31487 /* Initialize this module when Emacs starts. */
31488
31489 void
31490 init_xdisp (void)
31491 {
31492 CHARPOS (this_line_start_pos) = 0;
31493
31494 if (!noninteractive)
31495 {
31496 struct window *m = XWINDOW (minibuf_window);
31497 Lisp_Object frame = m->frame;
31498 struct frame *f = XFRAME (frame);
31499 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31500 struct window *r = XWINDOW (root);
31501 int i;
31502
31503 echo_area_window = minibuf_window;
31504
31505 r->top_line = FRAME_TOP_MARGIN (f);
31506 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31507 r->total_cols = FRAME_COLS (f);
31508 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31509 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31510 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31511
31512 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31513 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31514 m->total_cols = FRAME_COLS (f);
31515 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31516 m->total_lines = 1;
31517 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31518
31519 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31520 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31521 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31522
31523 /* The default ellipsis glyphs `...'. */
31524 for (i = 0; i < 3; ++i)
31525 default_invis_vector[i] = make_number ('.');
31526 }
31527
31528 {
31529 /* Allocate the buffer for frame titles.
31530 Also used for `format-mode-line'. */
31531 int size = 100;
31532 mode_line_noprop_buf = xmalloc (size);
31533 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31534 mode_line_noprop_ptr = mode_line_noprop_buf;
31535 mode_line_target = MODE_LINE_DISPLAY;
31536 }
31537
31538 help_echo_showing_p = false;
31539 }
31540
31541 #ifdef HAVE_WINDOW_SYSTEM
31542
31543 /* Platform-independent portion of hourglass implementation. */
31544
31545 /* Timer function of hourglass_atimer. */
31546
31547 static void
31548 show_hourglass (struct atimer *timer)
31549 {
31550 /* The timer implementation will cancel this timer automatically
31551 after this function has run. Set hourglass_atimer to null
31552 so that we know the timer doesn't have to be canceled. */
31553 hourglass_atimer = NULL;
31554
31555 if (!hourglass_shown_p)
31556 {
31557 Lisp_Object tail, frame;
31558
31559 block_input ();
31560
31561 FOR_EACH_FRAME (tail, frame)
31562 {
31563 struct frame *f = XFRAME (frame);
31564
31565 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31566 && FRAME_RIF (f)->show_hourglass)
31567 FRAME_RIF (f)->show_hourglass (f);
31568 }
31569
31570 hourglass_shown_p = true;
31571 unblock_input ();
31572 }
31573 }
31574
31575 /* Cancel a currently active hourglass timer, and start a new one. */
31576
31577 void
31578 start_hourglass (void)
31579 {
31580 struct timespec delay;
31581
31582 cancel_hourglass ();
31583
31584 if (INTEGERP (Vhourglass_delay)
31585 && XINT (Vhourglass_delay) > 0)
31586 delay = make_timespec (min (XINT (Vhourglass_delay),
31587 TYPE_MAXIMUM (time_t)),
31588 0);
31589 else if (FLOATP (Vhourglass_delay)
31590 && XFLOAT_DATA (Vhourglass_delay) > 0)
31591 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31592 else
31593 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31594
31595 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31596 show_hourglass, NULL);
31597 }
31598
31599 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31600 shown. */
31601
31602 void
31603 cancel_hourglass (void)
31604 {
31605 if (hourglass_atimer)
31606 {
31607 cancel_atimer (hourglass_atimer);
31608 hourglass_atimer = NULL;
31609 }
31610
31611 if (hourglass_shown_p)
31612 {
31613 Lisp_Object tail, frame;
31614
31615 block_input ();
31616
31617 FOR_EACH_FRAME (tail, frame)
31618 {
31619 struct frame *f = XFRAME (frame);
31620
31621 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31622 && FRAME_RIF (f)->hide_hourglass)
31623 FRAME_RIF (f)->hide_hourglass (f);
31624 #ifdef HAVE_NTGUI
31625 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31626 else if (!FRAME_W32_P (f))
31627 w32_arrow_cursor ();
31628 #endif
31629 }
31630
31631 hourglass_shown_p = false;
31632 unblock_input ();
31633 }
31634 }
31635
31636 #endif /* HAVE_WINDOW_SYSTEM */